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"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
49 typedef struct cp_token GTY (())
51 /* The kind of token. */
52 ENUM_BITFIELD (cpp_ttype) type : 8;
53 /* If this token is a keyword, this value indicates which keyword.
54 Otherwise, this value is RID_MAX. */
55 ENUM_BITFIELD (rid) keyword : 8;
58 /* Identifier for the pragma. */
59 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
60 /* True if this token is from a system header. */
61 BOOL_BITFIELD in_system_header : 1;
62 /* True if this token is from a context where it is implicitly extern "C" */
63 BOOL_BITFIELD implicit_extern_c : 1;
64 /* True for a CPP_NAME token that is not a keyword (i.e., for which
65 KEYWORD is RID_MAX) iff this name was looked up and found to be
66 ambiguous. An error has already been reported. */
67 BOOL_BITFIELD ambiguous_p : 1;
68 /* The value associated with this token, if any. */
70 /* The location at which this token was found. */
74 /* We use a stack of token pointer for saving token sets. */
75 typedef struct cp_token *cp_token_position;
76 DEF_VEC_P (cp_token_position);
77 DEF_VEC_ALLOC_P (cp_token_position,heap);
79 static const cp_token eof_token =
81 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
82 #if USE_MAPPED_LOCATION
89 /* The cp_lexer structure represents the C++ lexer. It is responsible
90 for managing the token stream from the preprocessor and supplying
91 it to the parser. Tokens are never added to the cp_lexer after
94 typedef struct cp_lexer GTY (())
96 /* The memory allocated for the buffer. NULL if this lexer does not
97 own the token buffer. */
98 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
99 /* If the lexer owns the buffer, this is the number of tokens in the
101 size_t buffer_length;
103 /* A pointer just past the last available token. The tokens
104 in this lexer are [buffer, last_token). */
105 cp_token_position GTY ((skip)) last_token;
107 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
108 no more available tokens. */
109 cp_token_position GTY ((skip)) next_token;
111 /* A stack indicating positions at which cp_lexer_save_tokens was
112 called. The top entry is the most recent position at which we
113 began saving tokens. If the stack is non-empty, we are saving
115 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
117 /* The next lexer in a linked list of lexers. */
118 struct cp_lexer *next;
120 /* True if we should output debugging information. */
123 /* True if we're in the context of parsing a pragma, and should not
124 increment past the end-of-line marker. */
128 /* cp_token_cache is a range of tokens. There is no need to represent
129 allocate heap memory for it, since tokens are never removed from the
130 lexer's array. There is also no need for the GC to walk through
131 a cp_token_cache, since everything in here is referenced through
134 typedef struct cp_token_cache GTY(())
136 /* The beginning of the token range. */
137 cp_token * GTY((skip)) first;
139 /* Points immediately after the last token in the range. */
140 cp_token * GTY ((skip)) last;
145 static cp_lexer *cp_lexer_new_main
147 static cp_lexer *cp_lexer_new_from_tokens
148 (cp_token_cache *tokens);
149 static void cp_lexer_destroy
151 static int cp_lexer_saving_tokens
153 static cp_token_position cp_lexer_token_position
155 static cp_token *cp_lexer_token_at
156 (cp_lexer *, cp_token_position);
157 static void cp_lexer_get_preprocessor_token
158 (cp_lexer *, cp_token *);
159 static inline cp_token *cp_lexer_peek_token
161 static cp_token *cp_lexer_peek_nth_token
162 (cp_lexer *, size_t);
163 static inline bool cp_lexer_next_token_is
164 (cp_lexer *, enum cpp_ttype);
165 static bool cp_lexer_next_token_is_not
166 (cp_lexer *, enum cpp_ttype);
167 static bool cp_lexer_next_token_is_keyword
168 (cp_lexer *, enum rid);
169 static cp_token *cp_lexer_consume_token
171 static void cp_lexer_purge_token
173 static void cp_lexer_purge_tokens_after
174 (cp_lexer *, cp_token_position);
175 static void cp_lexer_save_tokens
177 static void cp_lexer_commit_tokens
179 static void cp_lexer_rollback_tokens
181 #ifdef ENABLE_CHECKING
182 static void cp_lexer_print_token
183 (FILE *, cp_token *);
184 static inline bool cp_lexer_debugging_p
186 static void cp_lexer_start_debugging
187 (cp_lexer *) ATTRIBUTE_UNUSED;
188 static void cp_lexer_stop_debugging
189 (cp_lexer *) ATTRIBUTE_UNUSED;
191 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
192 about passing NULL to functions that require non-NULL arguments
193 (fputs, fprintf). It will never be used, so all we need is a value
194 of the right type that's guaranteed not to be NULL. */
195 #define cp_lexer_debug_stream stdout
196 #define cp_lexer_print_token(str, tok) (void) 0
197 #define cp_lexer_debugging_p(lexer) 0
198 #endif /* ENABLE_CHECKING */
200 static cp_token_cache *cp_token_cache_new
201 (cp_token *, cp_token *);
203 static void cp_parser_initial_pragma
206 /* Manifest constants. */
207 #define CP_LEXER_BUFFER_SIZE 10000
208 #define CP_SAVED_TOKEN_STACK 5
210 /* A token type for keywords, as opposed to ordinary identifiers. */
211 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
213 /* A token type for template-ids. If a template-id is processed while
214 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
215 the value of the CPP_TEMPLATE_ID is whatever was returned by
216 cp_parser_template_id. */
217 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
219 /* A token type for nested-name-specifiers. If a
220 nested-name-specifier is processed while parsing tentatively, it is
221 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
222 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
223 cp_parser_nested_name_specifier_opt. */
224 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
226 /* A token type for tokens that are not tokens at all; these are used
227 to represent slots in the array where there used to be a token
228 that has now been deleted. */
229 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
231 /* The number of token types, including C++-specific ones. */
232 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
236 #ifdef ENABLE_CHECKING
237 /* The stream to which debugging output should be written. */
238 static FILE *cp_lexer_debug_stream;
239 #endif /* ENABLE_CHECKING */
241 /* Create a new main C++ lexer, the lexer that gets tokens from the
245 cp_lexer_new_main (void)
247 cp_token first_token;
254 /* It's possible that parsing the first pragma will load a PCH file,
255 which is a GC collection point. So we have to do that before
256 allocating any memory. */
257 cp_parser_initial_pragma (&first_token);
259 /* Tell c_lex_with_flags not to merge string constants. */
260 c_lex_return_raw_strings = true;
262 c_common_no_more_pch ();
264 /* Allocate the memory. */
265 lexer = GGC_CNEW (cp_lexer);
267 #ifdef ENABLE_CHECKING
268 /* Initially we are not debugging. */
269 lexer->debugging_p = false;
270 #endif /* ENABLE_CHECKING */
271 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
272 CP_SAVED_TOKEN_STACK);
274 /* Create the buffer. */
275 alloc = CP_LEXER_BUFFER_SIZE;
276 buffer = GGC_NEWVEC (cp_token, alloc);
278 /* Put the first token in the buffer. */
283 /* Get the remaining tokens from the preprocessor. */
284 while (pos->type != CPP_EOF)
291 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
292 pos = buffer + space;
294 cp_lexer_get_preprocessor_token (lexer, pos);
296 lexer->buffer = buffer;
297 lexer->buffer_length = alloc - space;
298 lexer->last_token = pos;
299 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
301 /* Subsequent preprocessor diagnostics should use compiler
302 diagnostic functions to get the compiler source location. */
303 cpp_get_options (parse_in)->client_diagnostic = true;
304 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
306 gcc_assert (lexer->next_token->type != CPP_PURGED);
310 /* Create a new lexer whose token stream is primed with the tokens in
311 CACHE. When these tokens are exhausted, no new tokens will be read. */
314 cp_lexer_new_from_tokens (cp_token_cache *cache)
316 cp_token *first = cache->first;
317 cp_token *last = cache->last;
318 cp_lexer *lexer = GGC_CNEW (cp_lexer);
320 /* We do not own the buffer. */
321 lexer->buffer = NULL;
322 lexer->buffer_length = 0;
323 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
324 lexer->last_token = last;
326 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
327 CP_SAVED_TOKEN_STACK);
329 #ifdef ENABLE_CHECKING
330 /* Initially we are not debugging. */
331 lexer->debugging_p = false;
334 gcc_assert (lexer->next_token->type != CPP_PURGED);
338 /* Frees all resources associated with LEXER. */
341 cp_lexer_destroy (cp_lexer *lexer)
344 ggc_free (lexer->buffer);
345 VEC_free (cp_token_position, heap, lexer->saved_tokens);
349 /* Returns nonzero if debugging information should be output. */
351 #ifdef ENABLE_CHECKING
354 cp_lexer_debugging_p (cp_lexer *lexer)
356 return lexer->debugging_p;
359 #endif /* ENABLE_CHECKING */
361 static inline cp_token_position
362 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
364 gcc_assert (!previous_p || lexer->next_token != &eof_token);
366 return lexer->next_token - previous_p;
369 static inline cp_token *
370 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
375 /* nonzero if we are presently saving tokens. */
378 cp_lexer_saving_tokens (const cp_lexer* lexer)
380 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
383 /* Store the next token from the preprocessor in *TOKEN. Return true
387 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
390 static int is_extern_c = 0;
392 /* Get a new token from the preprocessor. */
394 = c_lex_with_flags (&token->value, &token->location, &token->flags);
395 token->keyword = RID_MAX;
396 token->pragma_kind = PRAGMA_NONE;
397 token->in_system_header = in_system_header;
399 /* On some systems, some header files are surrounded by an
400 implicit extern "C" block. Set a flag in the token if it
401 comes from such a header. */
402 is_extern_c += pending_lang_change;
403 pending_lang_change = 0;
404 token->implicit_extern_c = is_extern_c > 0;
406 /* Check to see if this token is a keyword. */
407 if (token->type == CPP_NAME)
409 if (C_IS_RESERVED_WORD (token->value))
411 /* Mark this token as a keyword. */
412 token->type = CPP_KEYWORD;
413 /* Record which keyword. */
414 token->keyword = C_RID_CODE (token->value);
415 /* Update the value. Some keywords are mapped to particular
416 entities, rather than simply having the value of the
417 corresponding IDENTIFIER_NODE. For example, `__const' is
418 mapped to `const'. */
419 token->value = ridpointers[token->keyword];
423 token->ambiguous_p = false;
424 token->keyword = RID_MAX;
427 /* Handle Objective-C++ keywords. */
428 else if (token->type == CPP_AT_NAME)
430 token->type = CPP_KEYWORD;
431 switch (C_RID_CODE (token->value))
433 /* Map 'class' to '@class', 'private' to '@private', etc. */
434 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438 case RID_THROW: token->keyword = RID_AT_THROW; break;
439 case RID_TRY: token->keyword = RID_AT_TRY; break;
440 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441 default: token->keyword = C_RID_CODE (token->value);
444 else if (token->type == CPP_PRAGMA)
446 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
447 token->pragma_kind = TREE_INT_CST_LOW (token->value);
452 /* Update the globals input_location and in_system_header from TOKEN. */
454 cp_lexer_set_source_position_from_token (cp_token *token)
456 if (token->type != CPP_EOF)
458 input_location = token->location;
459 in_system_header = token->in_system_header;
463 /* Return a pointer to the next token in the token stream, but do not
466 static inline cp_token *
467 cp_lexer_peek_token (cp_lexer *lexer)
469 if (cp_lexer_debugging_p (lexer))
471 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
472 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
473 putc ('\n', cp_lexer_debug_stream);
475 return lexer->next_token;
478 /* Return true if the next token has the indicated TYPE. */
481 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
483 return cp_lexer_peek_token (lexer)->type == type;
486 /* Return true if the next token does not have the indicated TYPE. */
489 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
491 return !cp_lexer_next_token_is (lexer, type);
494 /* Return true if the next token is the indicated KEYWORD. */
497 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
501 /* Peek at the next token. */
502 token = cp_lexer_peek_token (lexer);
503 /* Check to see if it is the indicated keyword. */
504 return token->keyword == keyword;
507 /* Return a pointer to the Nth token in the token stream. If N is 1,
508 then this is precisely equivalent to cp_lexer_peek_token (except
509 that it is not inline). One would like to disallow that case, but
510 there is one case (cp_parser_nth_token_starts_template_id) where
511 the caller passes a variable for N and it might be 1. */
514 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
518 /* N is 1-based, not zero-based. */
521 if (cp_lexer_debugging_p (lexer))
522 fprintf (cp_lexer_debug_stream,
523 "cp_lexer: peeking ahead %ld at token: ", (long)n);
526 token = lexer->next_token;
527 gcc_assert (!n || token != &eof_token);
531 if (token == lexer->last_token)
533 token = (cp_token *)&eof_token;
537 if (token->type != CPP_PURGED)
541 if (cp_lexer_debugging_p (lexer))
543 cp_lexer_print_token (cp_lexer_debug_stream, token);
544 putc ('\n', cp_lexer_debug_stream);
550 /* Return the next token, and advance the lexer's next_token pointer
551 to point to the next non-purged token. */
554 cp_lexer_consume_token (cp_lexer* lexer)
556 cp_token *token = lexer->next_token;
558 gcc_assert (token != &eof_token);
559 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
564 if (lexer->next_token == lexer->last_token)
566 lexer->next_token = (cp_token *)&eof_token;
571 while (lexer->next_token->type == CPP_PURGED);
573 cp_lexer_set_source_position_from_token (token);
575 /* Provide debugging output. */
576 if (cp_lexer_debugging_p (lexer))
578 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
579 cp_lexer_print_token (cp_lexer_debug_stream, token);
580 putc ('\n', cp_lexer_debug_stream);
586 /* Permanently remove the next token from the token stream, and
587 advance the next_token pointer to refer to the next non-purged
591 cp_lexer_purge_token (cp_lexer *lexer)
593 cp_token *tok = lexer->next_token;
595 gcc_assert (tok != &eof_token);
596 tok->type = CPP_PURGED;
597 tok->location = UNKNOWN_LOCATION;
598 tok->value = NULL_TREE;
599 tok->keyword = RID_MAX;
604 if (tok == lexer->last_token)
606 tok = (cp_token *)&eof_token;
610 while (tok->type == CPP_PURGED);
611 lexer->next_token = tok;
614 /* Permanently remove all tokens after TOK, up to, but not
615 including, the token that will be returned next by
616 cp_lexer_peek_token. */
619 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
621 cp_token *peek = lexer->next_token;
623 if (peek == &eof_token)
624 peek = lexer->last_token;
626 gcc_assert (tok < peek);
628 for ( tok += 1; tok != peek; tok += 1)
630 tok->type = CPP_PURGED;
631 tok->location = UNKNOWN_LOCATION;
632 tok->value = NULL_TREE;
633 tok->keyword = RID_MAX;
637 /* Begin saving tokens. All tokens consumed after this point will be
641 cp_lexer_save_tokens (cp_lexer* lexer)
643 /* Provide debugging output. */
644 if (cp_lexer_debugging_p (lexer))
645 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
647 VEC_safe_push (cp_token_position, heap,
648 lexer->saved_tokens, lexer->next_token);
651 /* Commit to the portion of the token stream most recently saved. */
654 cp_lexer_commit_tokens (cp_lexer* lexer)
656 /* Provide debugging output. */
657 if (cp_lexer_debugging_p (lexer))
658 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
660 VEC_pop (cp_token_position, lexer->saved_tokens);
663 /* Return all tokens saved since the last call to cp_lexer_save_tokens
664 to the token stream. Stop saving tokens. */
667 cp_lexer_rollback_tokens (cp_lexer* lexer)
669 /* Provide debugging output. */
670 if (cp_lexer_debugging_p (lexer))
671 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
673 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
676 /* Print a representation of the TOKEN on the STREAM. */
678 #ifdef ENABLE_CHECKING
681 cp_lexer_print_token (FILE * stream, cp_token *token)
683 /* We don't use cpp_type2name here because the parser defines
684 a few tokens of its own. */
685 static const char *const token_names[] = {
686 /* cpplib-defined token types */
692 /* C++ parser token types - see "Manifest constants", above. */
695 "NESTED_NAME_SPECIFIER",
699 /* If we have a name for the token, print it out. Otherwise, we
700 simply give the numeric code. */
701 gcc_assert (token->type < ARRAY_SIZE(token_names));
702 fputs (token_names[token->type], stream);
704 /* For some tokens, print the associated data. */
708 /* Some keywords have a value that is not an IDENTIFIER_NODE.
709 For example, `struct' is mapped to an INTEGER_CST. */
710 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
712 /* else fall through */
714 fputs (IDENTIFIER_POINTER (token->value), stream);
719 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
727 /* Start emitting debugging information. */
730 cp_lexer_start_debugging (cp_lexer* lexer)
732 lexer->debugging_p = true;
735 /* Stop emitting debugging information. */
738 cp_lexer_stop_debugging (cp_lexer* lexer)
740 lexer->debugging_p = false;
743 #endif /* ENABLE_CHECKING */
745 /* Create a new cp_token_cache, representing a range of tokens. */
747 static cp_token_cache *
748 cp_token_cache_new (cp_token *first, cp_token *last)
750 cp_token_cache *cache = GGC_NEW (cp_token_cache);
751 cache->first = first;
757 /* Decl-specifiers. */
759 static void clear_decl_specs
760 (cp_decl_specifier_seq *);
762 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
765 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
767 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
772 /* Nothing other than the parser should be creating declarators;
773 declarators are a semi-syntactic representation of C++ entities.
774 Other parts of the front end that need to create entities (like
775 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
777 static cp_declarator *make_call_declarator
778 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
779 static cp_declarator *make_array_declarator
780 (cp_declarator *, tree);
781 static cp_declarator *make_pointer_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_declarator *make_reference_declarator
784 (cp_cv_quals, cp_declarator *);
785 static cp_parameter_declarator *make_parameter_declarator
786 (cp_decl_specifier_seq *, cp_declarator *, tree);
787 static cp_declarator *make_ptrmem_declarator
788 (cp_cv_quals, tree, cp_declarator *);
790 cp_declarator *cp_error_declarator;
792 /* The obstack on which declarators and related data structures are
794 static struct obstack declarator_obstack;
796 /* Alloc BYTES from the declarator memory pool. */
799 alloc_declarator (size_t bytes)
801 return obstack_alloc (&declarator_obstack, bytes);
804 /* Allocate a declarator of the indicated KIND. Clear fields that are
805 common to all declarators. */
807 static cp_declarator *
808 make_declarator (cp_declarator_kind kind)
810 cp_declarator *declarator;
812 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
813 declarator->kind = kind;
814 declarator->attributes = NULL_TREE;
815 declarator->declarator = NULL;
820 /* Make a declarator for a generalized identifier. If
821 QUALIFYING_SCOPE is non-NULL, the identifier is
822 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
823 UNQUALIFIED_NAME. SFK indicates the kind of special function this
826 static cp_declarator *
827 make_id_declarator (tree qualifying_scope, tree unqualified_name,
828 special_function_kind sfk)
830 cp_declarator *declarator;
832 /* It is valid to write:
834 class C { void f(); };
838 The standard is not clear about whether `typedef const C D' is
839 legal; as of 2002-09-15 the committee is considering that
840 question. EDG 3.0 allows that syntax. Therefore, we do as
842 if (qualifying_scope && TYPE_P (qualifying_scope))
843 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
845 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
846 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
847 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
849 declarator = make_declarator (cdk_id);
850 declarator->u.id.qualifying_scope = qualifying_scope;
851 declarator->u.id.unqualified_name = unqualified_name;
852 declarator->u.id.sfk = sfk;
857 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
858 of modifiers such as const or volatile to apply to the pointer
859 type, represented as identifiers. */
862 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
864 cp_declarator *declarator;
866 declarator = make_declarator (cdk_pointer);
867 declarator->declarator = target;
868 declarator->u.pointer.qualifiers = cv_qualifiers;
869 declarator->u.pointer.class_type = NULL_TREE;
874 /* Like make_pointer_declarator -- but for references. */
877 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
879 cp_declarator *declarator;
881 declarator = make_declarator (cdk_reference);
882 declarator->declarator = target;
883 declarator->u.pointer.qualifiers = cv_qualifiers;
884 declarator->u.pointer.class_type = NULL_TREE;
889 /* Like make_pointer_declarator -- but for a pointer to a non-static
890 member of CLASS_TYPE. */
893 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
894 cp_declarator *pointee)
896 cp_declarator *declarator;
898 declarator = make_declarator (cdk_ptrmem);
899 declarator->declarator = pointee;
900 declarator->u.pointer.qualifiers = cv_qualifiers;
901 declarator->u.pointer.class_type = class_type;
906 /* Make a declarator for the function given by TARGET, with the
907 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
908 "const"-qualified member function. The EXCEPTION_SPECIFICATION
909 indicates what exceptions can be thrown. */
912 make_call_declarator (cp_declarator *target,
913 cp_parameter_declarator *parms,
914 cp_cv_quals cv_qualifiers,
915 tree exception_specification)
917 cp_declarator *declarator;
919 declarator = make_declarator (cdk_function);
920 declarator->declarator = target;
921 declarator->u.function.parameters = parms;
922 declarator->u.function.qualifiers = cv_qualifiers;
923 declarator->u.function.exception_specification = exception_specification;
928 /* Make a declarator for an array of BOUNDS elements, each of which is
929 defined by ELEMENT. */
932 make_array_declarator (cp_declarator *element, tree bounds)
934 cp_declarator *declarator;
936 declarator = make_declarator (cdk_array);
937 declarator->declarator = element;
938 declarator->u.array.bounds = bounds;
943 cp_parameter_declarator *no_parameters;
945 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
946 DECLARATOR and DEFAULT_ARGUMENT. */
948 cp_parameter_declarator *
949 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
950 cp_declarator *declarator,
951 tree default_argument)
953 cp_parameter_declarator *parameter;
955 parameter = ((cp_parameter_declarator *)
956 alloc_declarator (sizeof (cp_parameter_declarator)));
957 parameter->next = NULL;
959 parameter->decl_specifiers = *decl_specifiers;
961 clear_decl_specs (¶meter->decl_specifiers);
962 parameter->declarator = declarator;
963 parameter->default_argument = default_argument;
964 parameter->ellipsis_p = false;
974 A cp_parser parses the token stream as specified by the C++
975 grammar. Its job is purely parsing, not semantic analysis. For
976 example, the parser breaks the token stream into declarators,
977 expressions, statements, and other similar syntactic constructs.
978 It does not check that the types of the expressions on either side
979 of an assignment-statement are compatible, or that a function is
980 not declared with a parameter of type `void'.
982 The parser invokes routines elsewhere in the compiler to perform
983 semantic analysis and to build up the abstract syntax tree for the
986 The parser (and the template instantiation code, which is, in a
987 way, a close relative of parsing) are the only parts of the
988 compiler that should be calling push_scope and pop_scope, or
989 related functions. The parser (and template instantiation code)
990 keeps track of what scope is presently active; everything else
991 should simply honor that. (The code that generates static
992 initializers may also need to set the scope, in order to check
993 access control correctly when emitting the initializers.)
998 The parser is of the standard recursive-descent variety. Upcoming
999 tokens in the token stream are examined in order to determine which
1000 production to use when parsing a non-terminal. Some C++ constructs
1001 require arbitrary look ahead to disambiguate. For example, it is
1002 impossible, in the general case, to tell whether a statement is an
1003 expression or declaration without scanning the entire statement.
1004 Therefore, the parser is capable of "parsing tentatively." When the
1005 parser is not sure what construct comes next, it enters this mode.
1006 Then, while we attempt to parse the construct, the parser queues up
1007 error messages, rather than issuing them immediately, and saves the
1008 tokens it consumes. If the construct is parsed successfully, the
1009 parser "commits", i.e., it issues any queued error messages and
1010 the tokens that were being preserved are permanently discarded.
1011 If, however, the construct is not parsed successfully, the parser
1012 rolls back its state completely so that it can resume parsing using
1013 a different alternative.
1018 The performance of the parser could probably be improved substantially.
1019 We could often eliminate the need to parse tentatively by looking ahead
1020 a little bit. In some places, this approach might not entirely eliminate
1021 the need to parse tentatively, but it might still speed up the average
1024 /* Flags that are passed to some parsing functions. These values can
1025 be bitwise-ored together. */
1027 typedef enum cp_parser_flags
1030 CP_PARSER_FLAGS_NONE = 0x0,
1031 /* The construct is optional. If it is not present, then no error
1032 should be issued. */
1033 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1034 /* When parsing a type-specifier, do not allow user-defined types. */
1035 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1038 /* The different kinds of declarators we want to parse. */
1040 typedef enum cp_parser_declarator_kind
1042 /* We want an abstract declarator. */
1043 CP_PARSER_DECLARATOR_ABSTRACT,
1044 /* We want a named declarator. */
1045 CP_PARSER_DECLARATOR_NAMED,
1046 /* We don't mind, but the name must be an unqualified-id. */
1047 CP_PARSER_DECLARATOR_EITHER
1048 } cp_parser_declarator_kind;
1050 /* The precedence values used to parse binary expressions. The minimum value
1051 of PREC must be 1, because zero is reserved to quickly discriminate
1052 binary operators from other tokens. */
1057 PREC_LOGICAL_OR_EXPRESSION,
1058 PREC_LOGICAL_AND_EXPRESSION,
1059 PREC_INCLUSIVE_OR_EXPRESSION,
1060 PREC_EXCLUSIVE_OR_EXPRESSION,
1061 PREC_AND_EXPRESSION,
1062 PREC_EQUALITY_EXPRESSION,
1063 PREC_RELATIONAL_EXPRESSION,
1064 PREC_SHIFT_EXPRESSION,
1065 PREC_ADDITIVE_EXPRESSION,
1066 PREC_MULTIPLICATIVE_EXPRESSION,
1068 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1071 /* A mapping from a token type to a corresponding tree node type, with a
1072 precedence value. */
1074 typedef struct cp_parser_binary_operations_map_node
1076 /* The token type. */
1077 enum cpp_ttype token_type;
1078 /* The corresponding tree code. */
1079 enum tree_code tree_type;
1080 /* The precedence of this operator. */
1081 enum cp_parser_prec prec;
1082 } cp_parser_binary_operations_map_node;
1084 /* The status of a tentative parse. */
1086 typedef enum cp_parser_status_kind
1088 /* No errors have occurred. */
1089 CP_PARSER_STATUS_KIND_NO_ERROR,
1090 /* An error has occurred. */
1091 CP_PARSER_STATUS_KIND_ERROR,
1092 /* We are committed to this tentative parse, whether or not an error
1094 CP_PARSER_STATUS_KIND_COMMITTED
1095 } cp_parser_status_kind;
1097 typedef struct cp_parser_expression_stack_entry
1100 enum tree_code tree_type;
1102 } cp_parser_expression_stack_entry;
1104 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1105 entries because precedence levels on the stack are monotonically
1107 typedef struct cp_parser_expression_stack_entry
1108 cp_parser_expression_stack[NUM_PREC_VALUES];
1110 /* Context that is saved and restored when parsing tentatively. */
1111 typedef struct cp_parser_context GTY (())
1113 /* If this is a tentative parsing context, the status of the
1115 enum cp_parser_status_kind status;
1116 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1117 that are looked up in this context must be looked up both in the
1118 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1119 the context of the containing expression. */
1122 /* The next parsing context in the stack. */
1123 struct cp_parser_context *next;
1124 } cp_parser_context;
1128 /* Constructors and destructors. */
1130 static cp_parser_context *cp_parser_context_new
1131 (cp_parser_context *);
1133 /* Class variables. */
1135 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1137 /* The operator-precedence table used by cp_parser_binary_expression.
1138 Transformed into an associative array (binops_by_token) by
1141 static const cp_parser_binary_operations_map_node binops[] = {
1142 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1143 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1145 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1147 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1149 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1150 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1152 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1153 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1155 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1157 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1158 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1160 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1162 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1163 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1165 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1167 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1169 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1171 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1173 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1176 /* The same as binops, but initialized by cp_parser_new so that
1177 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1179 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1181 /* Constructors and destructors. */
1183 /* Construct a new context. The context below this one on the stack
1184 is given by NEXT. */
1186 static cp_parser_context *
1187 cp_parser_context_new (cp_parser_context* next)
1189 cp_parser_context *context;
1191 /* Allocate the storage. */
1192 if (cp_parser_context_free_list != NULL)
1194 /* Pull the first entry from the free list. */
1195 context = cp_parser_context_free_list;
1196 cp_parser_context_free_list = context->next;
1197 memset (context, 0, sizeof (*context));
1200 context = GGC_CNEW (cp_parser_context);
1202 /* No errors have occurred yet in this context. */
1203 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1204 /* If this is not the bottomost context, copy information that we
1205 need from the previous context. */
1208 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1209 expression, then we are parsing one in this context, too. */
1210 context->object_type = next->object_type;
1211 /* Thread the stack. */
1212 context->next = next;
1218 /* The cp_parser structure represents the C++ parser. */
1220 typedef struct cp_parser GTY(())
1222 /* The lexer from which we are obtaining tokens. */
1225 /* The scope in which names should be looked up. If NULL_TREE, then
1226 we look up names in the scope that is currently open in the
1227 source program. If non-NULL, this is either a TYPE or
1228 NAMESPACE_DECL for the scope in which we should look. It can
1229 also be ERROR_MARK, when we've parsed a bogus scope.
1231 This value is not cleared automatically after a name is looked
1232 up, so we must be careful to clear it before starting a new look
1233 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1234 will look up `Z' in the scope of `X', rather than the current
1235 scope.) Unfortunately, it is difficult to tell when name lookup
1236 is complete, because we sometimes peek at a token, look it up,
1237 and then decide not to consume it. */
1240 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1241 last lookup took place. OBJECT_SCOPE is used if an expression
1242 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1243 respectively. QUALIFYING_SCOPE is used for an expression of the
1244 form "X::Y"; it refers to X. */
1246 tree qualifying_scope;
1248 /* A stack of parsing contexts. All but the bottom entry on the
1249 stack will be tentative contexts.
1251 We parse tentatively in order to determine which construct is in
1252 use in some situations. For example, in order to determine
1253 whether a statement is an expression-statement or a
1254 declaration-statement we parse it tentatively as a
1255 declaration-statement. If that fails, we then reparse the same
1256 token stream as an expression-statement. */
1257 cp_parser_context *context;
1259 /* True if we are parsing GNU C++. If this flag is not set, then
1260 GNU extensions are not recognized. */
1261 bool allow_gnu_extensions_p;
1263 /* TRUE if the `>' token should be interpreted as the greater-than
1264 operator. FALSE if it is the end of a template-id or
1265 template-parameter-list. */
1266 bool greater_than_is_operator_p;
1268 /* TRUE if default arguments are allowed within a parameter list
1269 that starts at this point. FALSE if only a gnu extension makes
1270 them permissible. */
1271 bool default_arg_ok_p;
1273 /* TRUE if we are parsing an integral constant-expression. See
1274 [expr.const] for a precise definition. */
1275 bool integral_constant_expression_p;
1277 /* TRUE if we are parsing an integral constant-expression -- but a
1278 non-constant expression should be permitted as well. This flag
1279 is used when parsing an array bound so that GNU variable-length
1280 arrays are tolerated. */
1281 bool allow_non_integral_constant_expression_p;
1283 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1284 been seen that makes the expression non-constant. */
1285 bool non_integral_constant_expression_p;
1287 /* TRUE if local variable names and `this' are forbidden in the
1289 bool local_variables_forbidden_p;
1291 /* TRUE if the declaration we are parsing is part of a
1292 linkage-specification of the form `extern string-literal
1294 bool in_unbraced_linkage_specification_p;
1296 /* TRUE if we are presently parsing a declarator, after the
1297 direct-declarator. */
1298 bool in_declarator_p;
1300 /* TRUE if we are presently parsing a template-argument-list. */
1301 bool in_template_argument_list_p;
1303 /* TRUE if we are presently parsing the body of an
1304 iteration-statement. */
1305 bool in_iteration_statement_p;
1307 /* TRUE if we are presently parsing the body of a switch
1309 bool in_switch_statement_p;
1311 /* TRUE if we are parsing a type-id in an expression context. In
1312 such a situation, both "type (expr)" and "type (type)" are valid
1314 bool in_type_id_in_expr_p;
1316 /* TRUE if we are currently in a header file where declarations are
1317 implicitly extern "C". */
1318 bool implicit_extern_c;
1320 /* TRUE if strings in expressions should be translated to the execution
1322 bool translate_strings_p;
1324 /* If non-NULL, then we are parsing a construct where new type
1325 definitions are not permitted. The string stored here will be
1326 issued as an error message if a type is defined. */
1327 const char *type_definition_forbidden_message;
1329 /* A list of lists. The outer list is a stack, used for member
1330 functions of local classes. At each level there are two sub-list,
1331 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1332 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1333 TREE_VALUE's. The functions are chained in reverse declaration
1336 The TREE_PURPOSE sublist contains those functions with default
1337 arguments that need post processing, and the TREE_VALUE sublist
1338 contains those functions with definitions that need post
1341 These lists can only be processed once the outermost class being
1342 defined is complete. */
1343 tree unparsed_functions_queues;
1345 /* The number of classes whose definitions are currently in
1347 unsigned num_classes_being_defined;
1349 /* The number of template parameter lists that apply directly to the
1350 current declaration. */
1351 unsigned num_template_parameter_lists;
1354 /* The type of a function that parses some kind of expression. */
1355 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1359 /* Constructors and destructors. */
1361 static cp_parser *cp_parser_new
1364 /* Routines to parse various constructs.
1366 Those that return `tree' will return the error_mark_node (rather
1367 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1368 Sometimes, they will return an ordinary node if error-recovery was
1369 attempted, even though a parse error occurred. So, to check
1370 whether or not a parse error occurred, you should always use
1371 cp_parser_error_occurred. If the construct is optional (indicated
1372 either by an `_opt' in the name of the function that does the
1373 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1374 the construct is not present. */
1376 /* Lexical conventions [gram.lex] */
1378 static tree cp_parser_identifier
1380 static tree cp_parser_string_literal
1381 (cp_parser *, bool, bool);
1383 /* Basic concepts [gram.basic] */
1385 static bool cp_parser_translation_unit
1388 /* Expressions [gram.expr] */
1390 static tree cp_parser_primary_expression
1391 (cp_parser *, bool, bool, bool, cp_id_kind *);
1392 static tree cp_parser_id_expression
1393 (cp_parser *, bool, bool, bool *, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser *, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier_opt
1397 (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_nested_name_specifier
1399 (cp_parser *, bool, bool, bool, bool);
1400 static tree cp_parser_class_or_namespace_name
1401 (cp_parser *, bool, bool, bool, bool, bool);
1402 static tree cp_parser_postfix_expression
1403 (cp_parser *, bool, bool);
1404 static tree cp_parser_postfix_open_square_expression
1405 (cp_parser *, tree, bool);
1406 static tree cp_parser_postfix_dot_deref_expression
1407 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1408 static tree cp_parser_parenthesized_expression_list
1409 (cp_parser *, bool, bool, bool *);
1410 static void cp_parser_pseudo_destructor_name
1411 (cp_parser *, tree *, tree *);
1412 static tree cp_parser_unary_expression
1413 (cp_parser *, bool, bool);
1414 static enum tree_code cp_parser_unary_operator
1416 static tree cp_parser_new_expression
1418 static tree cp_parser_new_placement
1420 static tree cp_parser_new_type_id
1421 (cp_parser *, tree *);
1422 static cp_declarator *cp_parser_new_declarator_opt
1424 static cp_declarator *cp_parser_direct_new_declarator
1426 static tree cp_parser_new_initializer
1428 static tree cp_parser_delete_expression
1430 static tree cp_parser_cast_expression
1431 (cp_parser *, bool, bool);
1432 static tree cp_parser_binary_expression
1433 (cp_parser *, bool);
1434 static tree cp_parser_question_colon_clause
1435 (cp_parser *, tree);
1436 static tree cp_parser_assignment_expression
1437 (cp_parser *, bool);
1438 static enum tree_code cp_parser_assignment_operator_opt
1440 static tree cp_parser_expression
1441 (cp_parser *, bool);
1442 static tree cp_parser_constant_expression
1443 (cp_parser *, bool, bool *);
1444 static tree cp_parser_builtin_offsetof
1447 /* Statements [gram.stmt.stmt] */
1449 static void cp_parser_statement
1450 (cp_parser *, tree, bool);
1451 static tree cp_parser_labeled_statement
1452 (cp_parser *, tree, bool);
1453 static tree cp_parser_expression_statement
1454 (cp_parser *, tree);
1455 static tree cp_parser_compound_statement
1456 (cp_parser *, tree, bool);
1457 static void cp_parser_statement_seq_opt
1458 (cp_parser *, tree);
1459 static tree cp_parser_selection_statement
1461 static tree cp_parser_condition
1463 static tree cp_parser_iteration_statement
1465 static void cp_parser_for_init_statement
1467 static tree cp_parser_jump_statement
1469 static void cp_parser_declaration_statement
1472 static tree cp_parser_implicitly_scoped_statement
1474 static void cp_parser_already_scoped_statement
1477 /* Declarations [gram.dcl.dcl] */
1479 static void cp_parser_declaration_seq_opt
1481 static void cp_parser_declaration
1483 static void cp_parser_block_declaration
1484 (cp_parser *, bool);
1485 static void cp_parser_simple_declaration
1486 (cp_parser *, bool);
1487 static void cp_parser_decl_specifier_seq
1488 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1489 static tree cp_parser_storage_class_specifier_opt
1491 static tree cp_parser_function_specifier_opt
1492 (cp_parser *, cp_decl_specifier_seq *);
1493 static tree cp_parser_type_specifier
1494 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1496 static tree cp_parser_simple_type_specifier
1497 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1498 static tree cp_parser_type_name
1500 static tree cp_parser_elaborated_type_specifier
1501 (cp_parser *, bool, bool);
1502 static tree cp_parser_enum_specifier
1504 static void cp_parser_enumerator_list
1505 (cp_parser *, tree);
1506 static void cp_parser_enumerator_definition
1507 (cp_parser *, tree);
1508 static tree cp_parser_namespace_name
1510 static void cp_parser_namespace_definition
1512 static void cp_parser_namespace_body
1514 static tree cp_parser_qualified_namespace_specifier
1516 static void cp_parser_namespace_alias_definition
1518 static void cp_parser_using_declaration
1520 static void cp_parser_using_directive
1522 static void cp_parser_asm_definition
1524 static void cp_parser_linkage_specification
1527 /* Declarators [gram.dcl.decl] */
1529 static tree cp_parser_init_declarator
1530 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1531 static cp_declarator *cp_parser_declarator
1532 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1533 static cp_declarator *cp_parser_direct_declarator
1534 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1535 static enum tree_code cp_parser_ptr_operator
1536 (cp_parser *, tree *, cp_cv_quals *);
1537 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1539 static tree cp_parser_declarator_id
1541 static tree cp_parser_type_id
1543 static void cp_parser_type_specifier_seq
1544 (cp_parser *, bool, cp_decl_specifier_seq *);
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1547 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1548 (cp_parser *, bool *);
1549 static cp_parameter_declarator *cp_parser_parameter_declaration
1550 (cp_parser *, bool, bool *);
1551 static void cp_parser_function_body
1553 static tree cp_parser_initializer
1554 (cp_parser *, bool *, bool *);
1555 static tree cp_parser_initializer_clause
1556 (cp_parser *, bool *);
1557 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1558 (cp_parser *, bool *);
1560 static bool cp_parser_ctor_initializer_opt_and_function_body
1563 /* Classes [gram.class] */
1565 static tree cp_parser_class_name
1566 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1567 static tree cp_parser_class_specifier
1569 static tree cp_parser_class_head
1570 (cp_parser *, bool *, tree *);
1571 static enum tag_types cp_parser_class_key
1573 static void cp_parser_member_specification_opt
1575 static void cp_parser_member_declaration
1577 static tree cp_parser_pure_specifier
1579 static tree cp_parser_constant_initializer
1582 /* Derived classes [gram.class.derived] */
1584 static tree cp_parser_base_clause
1586 static tree cp_parser_base_specifier
1589 /* Special member functions [gram.special] */
1591 static tree cp_parser_conversion_function_id
1593 static tree cp_parser_conversion_type_id
1595 static cp_declarator *cp_parser_conversion_declarator_opt
1597 static bool cp_parser_ctor_initializer_opt
1599 static void cp_parser_mem_initializer_list
1601 static tree cp_parser_mem_initializer
1603 static tree cp_parser_mem_initializer_id
1606 /* Overloading [gram.over] */
1608 static tree cp_parser_operator_function_id
1610 static tree cp_parser_operator
1613 /* Templates [gram.temp] */
1615 static void cp_parser_template_declaration
1616 (cp_parser *, bool);
1617 static tree cp_parser_template_parameter_list
1619 static tree cp_parser_template_parameter
1620 (cp_parser *, bool *);
1621 static tree cp_parser_type_parameter
1623 static tree cp_parser_template_id
1624 (cp_parser *, bool, bool, bool);
1625 static tree cp_parser_template_name
1626 (cp_parser *, bool, bool, bool, bool *);
1627 static tree cp_parser_template_argument_list
1629 static tree cp_parser_template_argument
1631 static void cp_parser_explicit_instantiation
1633 static void cp_parser_explicit_specialization
1636 /* Exception handling [gram.exception] */
1638 static tree cp_parser_try_block
1640 static bool cp_parser_function_try_block
1642 static void cp_parser_handler_seq
1644 static void cp_parser_handler
1646 static tree cp_parser_exception_declaration
1648 static tree cp_parser_throw_expression
1650 static tree cp_parser_exception_specification_opt
1652 static tree cp_parser_type_id_list
1655 /* GNU Extensions */
1657 static tree cp_parser_asm_specification_opt
1659 static tree cp_parser_asm_operand_list
1661 static tree cp_parser_asm_clobber_list
1663 static tree cp_parser_attributes_opt
1665 static tree cp_parser_attribute_list
1667 static bool cp_parser_extension_opt
1668 (cp_parser *, int *);
1669 static void cp_parser_label_declaration
1672 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1673 static bool cp_parser_pragma
1674 (cp_parser *, enum pragma_context);
1676 /* Objective-C++ Productions */
1678 static tree cp_parser_objc_message_receiver
1680 static tree cp_parser_objc_message_args
1682 static tree cp_parser_objc_message_expression
1684 static tree cp_parser_objc_encode_expression
1686 static tree cp_parser_objc_defs_expression
1688 static tree cp_parser_objc_protocol_expression
1690 static tree cp_parser_objc_selector_expression
1692 static tree cp_parser_objc_expression
1694 static bool cp_parser_objc_selector_p
1696 static tree cp_parser_objc_selector
1698 static tree cp_parser_objc_protocol_refs_opt
1700 static void cp_parser_objc_declaration
1702 static tree cp_parser_objc_statement
1705 /* Utility Routines */
1707 static tree cp_parser_lookup_name
1708 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1709 static tree cp_parser_lookup_name_simple
1710 (cp_parser *, tree);
1711 static tree cp_parser_maybe_treat_template_as_class
1713 static bool cp_parser_check_declarator_template_parameters
1714 (cp_parser *, cp_declarator *);
1715 static bool cp_parser_check_template_parameters
1716 (cp_parser *, unsigned);
1717 static tree cp_parser_simple_cast_expression
1719 static tree cp_parser_global_scope_opt
1720 (cp_parser *, bool);
1721 static bool cp_parser_constructor_declarator_p
1722 (cp_parser *, bool);
1723 static tree cp_parser_function_definition_from_specifiers_and_declarator
1724 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1725 static tree cp_parser_function_definition_after_declarator
1726 (cp_parser *, bool);
1727 static void cp_parser_template_declaration_after_export
1728 (cp_parser *, bool);
1729 static tree cp_parser_single_declaration
1730 (cp_parser *, bool, bool *);
1731 static tree cp_parser_functional_cast
1732 (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1737 static void cp_parser_save_default_args
1738 (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740 (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742 (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744 (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1747 static void cp_parser_set_storage_class
1748 (cp_decl_specifier_seq *, cp_storage_class);
1749 static void cp_parser_set_decl_spec_type
1750 (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752 (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756 (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1759 static bool cp_parser_next_token_starts_class_definition_p
1761 static bool cp_parser_next_token_ends_template_argument_p
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764 (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1767 static void cp_parser_check_class_key
1768 (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1771 static bool cp_parser_optional_template_keyword
1773 static void cp_parser_pre_parsed_nested_name_specifier
1775 static void cp_parser_cache_group
1776 (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1779 static void cp_parser_commit_to_tentative_parse
1781 static void cp_parser_abort_tentative_parse
1783 static bool cp_parser_parse_definitely
1785 static inline bool cp_parser_parsing_tentatively
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1789 static void cp_parser_error
1790 (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792 (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1795 static void cp_parser_check_type_definition
1797 static void cp_parser_check_for_definition_in_return_type
1798 (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800 (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802 (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804 (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1807 static int cp_parser_skip_to_closing_parenthesis
1808 (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1813 static void cp_parser_skip_to_end_of_block_or_statement
1815 static void cp_parser_skip_to_closing_brace
1817 static void cp_parser_skip_until_found
1818 (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820 (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1823 static bool cp_parser_allow_gnu_extensions_p
1825 static bool cp_parser_is_string_literal
1827 static bool cp_parser_is_keyword
1828 (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830 (cp_parser *, tree, tree);
1832 /* Returns nonzero if we are parsing tentatively. */
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1837 return parser->context->next != NULL;
1840 /* Returns nonzero if TOKEN is a string literal. */
1843 cp_parser_is_string_literal (cp_token* token)
1845 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1853 return token->keyword == keyword;
1856 /* A minimum or maximum operator has been seen. As these are
1857 deprecated, issue a warning. */
1860 cp_parser_warn_min_max (void)
1862 if (warn_deprecated && !in_system_header)
1863 warning (0, "minimum/maximum operators are deprecated");
1866 /* If not parsing tentatively, issue a diagnostic of the form
1867 FILE:LINE: MESSAGE before TOKEN
1868 where TOKEN is the next token in the input stream. MESSAGE
1869 (specified by the caller) is usually of the form "expected
1873 cp_parser_error (cp_parser* parser, const char* message)
1875 if (!cp_parser_simulate_error (parser))
1877 cp_token *token = cp_lexer_peek_token (parser->lexer);
1878 /* This diagnostic makes more sense if it is tagged to the line
1879 of the token we just peeked at. */
1880 cp_lexer_set_source_position_from_token (token);
1882 if (token->type == CPP_PRAGMA)
1884 error ("%<#pragma%> is not allowed here");
1885 cp_parser_skip_to_pragma_eol (parser, token);
1889 c_parse_error (message,
1890 /* Because c_parser_error does not understand
1891 CPP_KEYWORD, keywords are treated like
1893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1898 /* Issue an error about name-lookup failing. NAME is the
1899 IDENTIFIER_NODE DECL is the result of
1900 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1901 the thing that we hoped to find. */
1904 cp_parser_name_lookup_error (cp_parser* parser,
1907 const char* desired)
1909 /* If name lookup completely failed, tell the user that NAME was not
1911 if (decl == error_mark_node)
1913 if (parser->scope && parser->scope != global_namespace)
1914 error ("%<%D::%D%> has not been declared",
1915 parser->scope, name);
1916 else if (parser->scope == global_namespace)
1917 error ("%<::%D%> has not been declared", name);
1918 else if (parser->object_scope
1919 && !CLASS_TYPE_P (parser->object_scope))
1920 error ("request for member %qD in non-class type %qT",
1921 name, parser->object_scope);
1922 else if (parser->object_scope)
1923 error ("%<%T::%D%> has not been declared",
1924 parser->object_scope, name);
1926 error ("%qD has not been declared", name);
1928 else if (parser->scope && parser->scope != global_namespace)
1929 error ("%<%D::%D%> %s", parser->scope, name, desired);
1930 else if (parser->scope == global_namespace)
1931 error ("%<::%D%> %s", name, desired);
1933 error ("%qD %s", name, desired);
1936 /* If we are parsing tentatively, remember that an error has occurred
1937 during this tentative parse. Returns true if the error was
1938 simulated; false if a message should be issued by the caller. */
1941 cp_parser_simulate_error (cp_parser* parser)
1943 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1945 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1951 /* This function is called when a type is defined. If type
1952 definitions are forbidden at this point, an error message is
1956 cp_parser_check_type_definition (cp_parser* parser)
1958 /* If types are forbidden here, issue a message. */
1959 if (parser->type_definition_forbidden_message)
1960 /* Use `%s' to print the string in case there are any escape
1961 characters in the message. */
1962 error ("%s", parser->type_definition_forbidden_message);
1965 /* This function is called when the DECLARATOR is processed. The TYPE
1966 was a type defined in the decl-specifiers. If it is invalid to
1967 define a type in the decl-specifiers for DECLARATOR, an error is
1971 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1974 /* [dcl.fct] forbids type definitions in return types.
1975 Unfortunately, it's not easy to know whether or not we are
1976 processing a return type until after the fact. */
1978 && (declarator->kind == cdk_pointer
1979 || declarator->kind == cdk_reference
1980 || declarator->kind == cdk_ptrmem))
1981 declarator = declarator->declarator;
1983 && declarator->kind == cdk_function)
1985 error ("new types may not be defined in a return type");
1986 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1991 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1992 "<" in any valid C++ program. If the next token is indeed "<",
1993 issue a message warning the user about what appears to be an
1994 invalid attempt to form a template-id. */
1997 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2000 cp_token_position start = 0;
2002 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2005 error ("%qT is not a template", type);
2006 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2007 error ("%qE is not a template", type);
2009 error ("invalid template-id");
2010 /* Remember the location of the invalid "<". */
2011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2012 start = cp_lexer_token_position (parser->lexer, true);
2013 /* Consume the "<". */
2014 cp_lexer_consume_token (parser->lexer);
2015 /* Parse the template arguments. */
2016 cp_parser_enclosed_template_argument_list (parser);
2017 /* Permanently remove the invalid template arguments so that
2018 this error message is not issued again. */
2020 cp_lexer_purge_tokens_after (parser->lexer, start);
2024 /* If parsing an integral constant-expression, issue an error message
2025 about the fact that THING appeared and return true. Otherwise,
2026 return false. In either case, set
2027 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2030 cp_parser_non_integral_constant_expression (cp_parser *parser,
2033 parser->non_integral_constant_expression_p = true;
2034 if (parser->integral_constant_expression_p)
2036 if (!parser->allow_non_integral_constant_expression_p)
2038 error ("%s cannot appear in a constant-expression", thing);
2045 /* Emit a diagnostic for an invalid type name. SCOPE is the
2046 qualifying scope (or NULL, if none) for ID. This function commits
2047 to the current active tentative parse, if any. (Otherwise, the
2048 problematic construct might be encountered again later, resulting
2049 in duplicate error messages.) */
2052 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2054 tree decl, old_scope;
2055 /* Try to lookup the identifier. */
2056 old_scope = parser->scope;
2057 parser->scope = scope;
2058 decl = cp_parser_lookup_name_simple (parser, id);
2059 parser->scope = old_scope;
2060 /* If the lookup found a template-name, it means that the user forgot
2061 to specify an argument list. Emit a useful error message. */
2062 if (TREE_CODE (decl) == TEMPLATE_DECL)
2063 error ("invalid use of template-name %qE without an argument list",
2065 else if (!parser->scope)
2067 /* Issue an error message. */
2068 error ("%qE does not name a type", id);
2069 /* If we're in a template class, it's possible that the user was
2070 referring to a type from a base class. For example:
2072 template <typename T> struct A { typedef T X; };
2073 template <typename T> struct B : public A<T> { X x; };
2075 The user should have said "typename A<T>::X". */
2076 if (processing_template_decl && current_class_type
2077 && TYPE_BINFO (current_class_type))
2081 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2085 tree base_type = BINFO_TYPE (b);
2086 if (CLASS_TYPE_P (base_type)
2087 && dependent_type_p (base_type))
2090 /* Go from a particular instantiation of the
2091 template (which will have an empty TYPE_FIELDs),
2092 to the main version. */
2093 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2094 for (field = TYPE_FIELDS (base_type);
2096 field = TREE_CHAIN (field))
2097 if (TREE_CODE (field) == TYPE_DECL
2098 && DECL_NAME (field) == id)
2100 inform ("(perhaps %<typename %T::%E%> was intended)",
2101 BINFO_TYPE (b), id);
2110 /* Here we diagnose qualified-ids where the scope is actually correct,
2111 but the identifier does not resolve to a valid type name. */
2112 else if (parser->scope != error_mark_node)
2114 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2115 error ("%qE in namespace %qE does not name a type",
2117 else if (TYPE_P (parser->scope))
2118 error ("%qE in class %qT does not name a type", id, parser->scope);
2122 cp_parser_commit_to_tentative_parse (parser);
2125 /* Check for a common situation where a type-name should be present,
2126 but is not, and issue a sensible error message. Returns true if an
2127 invalid type-name was detected.
2129 The situation handled by this function are variable declarations of the
2130 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2131 Usually, `ID' should name a type, but if we got here it means that it
2132 does not. We try to emit the best possible error message depending on
2133 how exactly the id-expression looks like.
2137 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2141 cp_parser_parse_tentatively (parser);
2142 id = cp_parser_id_expression (parser,
2143 /*template_keyword_p=*/false,
2144 /*check_dependency_p=*/true,
2145 /*template_p=*/NULL,
2146 /*declarator_p=*/true);
2147 /* After the id-expression, there should be a plain identifier,
2148 otherwise this is not a simple variable declaration. Also, if
2149 the scope is dependent, we cannot do much. */
2150 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2151 || (parser->scope && TYPE_P (parser->scope)
2152 && dependent_type_p (parser->scope)))
2154 cp_parser_abort_tentative_parse (parser);
2157 if (!cp_parser_parse_definitely (parser)
2158 || TREE_CODE (id) != IDENTIFIER_NODE)
2161 /* Emit a diagnostic for the invalid type. */
2162 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2163 /* Skip to the end of the declaration; there's no point in
2164 trying to process it. */
2165 cp_parser_skip_to_end_of_block_or_statement (parser);
2169 /* Consume tokens up to, and including, the next non-nested closing `)'.
2170 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2171 are doing error recovery. Returns -1 if OR_COMMA is true and we
2172 found an unnested comma. */
2175 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2180 unsigned paren_depth = 0;
2181 unsigned brace_depth = 0;
2183 if (recovering && !or_comma
2184 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2189 cp_token * token = cp_lexer_peek_token (parser->lexer);
2191 switch (token->type)
2194 case CPP_PRAGMA_EOL:
2195 /* If we've run out of tokens, then there is no closing `)'. */
2199 /* This matches the processing in skip_to_end_of_statement. */
2204 case CPP_OPEN_BRACE:
2207 case CPP_CLOSE_BRACE:
2213 if (recovering && or_comma && !brace_depth && !paren_depth)
2217 case CPP_OPEN_PAREN:
2222 case CPP_CLOSE_PAREN:
2223 if (!brace_depth && !paren_depth--)
2226 cp_lexer_consume_token (parser->lexer);
2235 /* Consume the token. */
2236 cp_lexer_consume_token (parser->lexer);
2240 /* Consume tokens until we reach the end of the current statement.
2241 Normally, that will be just before consuming a `;'. However, if a
2242 non-nested `}' comes first, then we stop before consuming that. */
2245 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2247 unsigned nesting_depth = 0;
2251 cp_token *token = cp_lexer_peek_token (parser->lexer);
2253 switch (token->type)
2256 case CPP_PRAGMA_EOL:
2257 /* If we've run out of tokens, stop. */
2261 /* If the next token is a `;', we have reached the end of the
2267 case CPP_CLOSE_BRACE:
2268 /* If this is a non-nested '}', stop before consuming it.
2269 That way, when confronted with something like:
2273 we stop before consuming the closing '}', even though we
2274 have not yet reached a `;'. */
2275 if (nesting_depth == 0)
2278 /* If it is the closing '}' for a block that we have
2279 scanned, stop -- but only after consuming the token.
2285 we will stop after the body of the erroneously declared
2286 function, but before consuming the following `typedef'
2288 if (--nesting_depth == 0)
2290 cp_lexer_consume_token (parser->lexer);
2294 case CPP_OPEN_BRACE:
2302 /* Consume the token. */
2303 cp_lexer_consume_token (parser->lexer);
2307 /* This function is called at the end of a statement or declaration.
2308 If the next token is a semicolon, it is consumed; otherwise, error
2309 recovery is attempted. */
2312 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2314 /* Look for the trailing `;'. */
2315 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2317 /* If there is additional (erroneous) input, skip to the end of
2319 cp_parser_skip_to_end_of_statement (parser);
2320 /* If the next token is now a `;', consume it. */
2321 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2322 cp_lexer_consume_token (parser->lexer);
2326 /* Skip tokens until we have consumed an entire block, or until we
2327 have consumed a non-nested `;'. */
2330 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2332 int nesting_depth = 0;
2334 while (nesting_depth >= 0)
2336 cp_token *token = cp_lexer_peek_token (parser->lexer);
2338 switch (token->type)
2341 case CPP_PRAGMA_EOL:
2342 /* If we've run out of tokens, stop. */
2346 /* Stop if this is an unnested ';'. */
2351 case CPP_CLOSE_BRACE:
2352 /* Stop if this is an unnested '}', or closes the outermost
2359 case CPP_OPEN_BRACE:
2368 /* Consume the token. */
2369 cp_lexer_consume_token (parser->lexer);
2373 /* Skip tokens until a non-nested closing curly brace is the next
2377 cp_parser_skip_to_closing_brace (cp_parser *parser)
2379 unsigned nesting_depth = 0;
2383 cp_token *token = cp_lexer_peek_token (parser->lexer);
2385 switch (token->type)
2388 case CPP_PRAGMA_EOL:
2389 /* If we've run out of tokens, stop. */
2392 case CPP_CLOSE_BRACE:
2393 /* If the next token is a non-nested `}', then we have reached
2394 the end of the current block. */
2395 if (nesting_depth-- == 0)
2399 case CPP_OPEN_BRACE:
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2409 /* Consume the token. */
2410 cp_lexer_consume_token (parser->lexer);
2414 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2415 parameter is the PRAGMA token, allowing us to purge the entire pragma
2419 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2423 parser->lexer->in_pragma = false;
2426 token = cp_lexer_consume_token (parser->lexer);
2427 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2429 /* Ensure that the pragma is not parsed again. */
2430 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2433 /* This is a simple wrapper around make_typename_type. When the id is
2434 an unresolved identifier node, we can provide a superior diagnostic
2435 using cp_parser_diagnose_invalid_type_name. */
2438 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2441 if (TREE_CODE (id) == IDENTIFIER_NODE)
2443 result = make_typename_type (scope, id, typename_type,
2444 /*complain=*/tf_none);
2445 if (result == error_mark_node)
2446 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2449 return make_typename_type (scope, id, typename_type, tf_error);
2453 /* Create a new C++ parser. */
2456 cp_parser_new (void)
2462 /* cp_lexer_new_main is called before calling ggc_alloc because
2463 cp_lexer_new_main might load a PCH file. */
2464 lexer = cp_lexer_new_main ();
2466 /* Initialize the binops_by_token so that we can get the tree
2467 directly from the token. */
2468 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2469 binops_by_token[binops[i].token_type] = binops[i];
2471 parser = GGC_CNEW (cp_parser);
2472 parser->lexer = lexer;
2473 parser->context = cp_parser_context_new (NULL);
2475 /* For now, we always accept GNU extensions. */
2476 parser->allow_gnu_extensions_p = 1;
2478 /* The `>' token is a greater-than operator, not the end of a
2480 parser->greater_than_is_operator_p = true;
2482 parser->default_arg_ok_p = true;
2484 /* We are not parsing a constant-expression. */
2485 parser->integral_constant_expression_p = false;
2486 parser->allow_non_integral_constant_expression_p = false;
2487 parser->non_integral_constant_expression_p = false;
2489 /* Local variable names are not forbidden. */
2490 parser->local_variables_forbidden_p = false;
2492 /* We are not processing an `extern "C"' declaration. */
2493 parser->in_unbraced_linkage_specification_p = false;
2495 /* We are not processing a declarator. */
2496 parser->in_declarator_p = false;
2498 /* We are not processing a template-argument-list. */
2499 parser->in_template_argument_list_p = false;
2501 /* We are not in an iteration statement. */
2502 parser->in_iteration_statement_p = false;
2504 /* We are not in a switch statement. */
2505 parser->in_switch_statement_p = false;
2507 /* We are not parsing a type-id inside an expression. */
2508 parser->in_type_id_in_expr_p = false;
2510 /* Declarations aren't implicitly extern "C". */
2511 parser->implicit_extern_c = false;
2513 /* String literals should be translated to the execution character set. */
2514 parser->translate_strings_p = true;
2516 /* The unparsed function queue is empty. */
2517 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2519 /* There are no classes being defined. */
2520 parser->num_classes_being_defined = 0;
2522 /* No template parameters apply. */
2523 parser->num_template_parameter_lists = 0;
2528 /* Create a cp_lexer structure which will emit the tokens in CACHE
2529 and push it onto the parser's lexer stack. This is used for delayed
2530 parsing of in-class method bodies and default arguments, and should
2531 not be confused with tentative parsing. */
2533 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2535 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2536 lexer->next = parser->lexer;
2537 parser->lexer = lexer;
2539 /* Move the current source position to that of the first token in the
2541 cp_lexer_set_source_position_from_token (lexer->next_token);
2544 /* Pop the top lexer off the parser stack. This is never used for the
2545 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2547 cp_parser_pop_lexer (cp_parser *parser)
2549 cp_lexer *lexer = parser->lexer;
2550 parser->lexer = lexer->next;
2551 cp_lexer_destroy (lexer);
2553 /* Put the current source position back where it was before this
2554 lexer was pushed. */
2555 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2558 /* Lexical conventions [gram.lex] */
2560 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2564 cp_parser_identifier (cp_parser* parser)
2568 /* Look for the identifier. */
2569 token = cp_parser_require (parser, CPP_NAME, "identifier");
2570 /* Return the value. */
2571 return token ? token->value : error_mark_node;
2574 /* Parse a sequence of adjacent string constants. Returns a
2575 TREE_STRING representing the combined, nul-terminated string
2576 constant. If TRANSLATE is true, translate the string to the
2577 execution character set. If WIDE_OK is true, a wide string is
2580 C++98 [lex.string] says that if a narrow string literal token is
2581 adjacent to a wide string literal token, the behavior is undefined.
2582 However, C99 6.4.5p4 says that this results in a wide string literal.
2583 We follow C99 here, for consistency with the C front end.
2585 This code is largely lifted from lex_string() in c-lex.c.
2587 FUTURE: ObjC++ will need to handle @-strings here. */
2589 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2594 struct obstack str_ob;
2595 cpp_string str, istr, *strs;
2598 tok = cp_lexer_peek_token (parser->lexer);
2599 if (!cp_parser_is_string_literal (tok))
2601 cp_parser_error (parser, "expected string-literal");
2602 return error_mark_node;
2605 /* Try to avoid the overhead of creating and destroying an obstack
2606 for the common case of just one string. */
2607 if (!cp_parser_is_string_literal
2608 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2610 cp_lexer_consume_token (parser->lexer);
2612 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2613 str.len = TREE_STRING_LENGTH (tok->value);
2615 if (tok->type == CPP_WSTRING)
2622 gcc_obstack_init (&str_ob);
2627 cp_lexer_consume_token (parser->lexer);
2629 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2630 str.len = TREE_STRING_LENGTH (tok->value);
2631 if (tok->type == CPP_WSTRING)
2634 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2636 tok = cp_lexer_peek_token (parser->lexer);
2638 while (cp_parser_is_string_literal (tok));
2640 strs = (cpp_string *) obstack_finish (&str_ob);
2643 if (wide && !wide_ok)
2645 cp_parser_error (parser, "a wide string is invalid in this context");
2649 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2650 (parse_in, strs, count, &istr, wide))
2652 value = build_string (istr.len, (char *)istr.text);
2653 free ((void *)istr.text);
2655 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2656 value = fix_string_type (value);
2659 /* cpp_interpret_string has issued an error. */
2660 value = error_mark_node;
2663 obstack_free (&str_ob, 0);
2669 /* Basic concepts [gram.basic] */
2671 /* Parse a translation-unit.
2674 declaration-seq [opt]
2676 Returns TRUE if all went well. */
2679 cp_parser_translation_unit (cp_parser* parser)
2681 /* The address of the first non-permanent object on the declarator
2683 static void *declarator_obstack_base;
2687 /* Create the declarator obstack, if necessary. */
2688 if (!cp_error_declarator)
2690 gcc_obstack_init (&declarator_obstack);
2691 /* Create the error declarator. */
2692 cp_error_declarator = make_declarator (cdk_error);
2693 /* Create the empty parameter list. */
2694 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2695 /* Remember where the base of the declarator obstack lies. */
2696 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2699 cp_parser_declaration_seq_opt (parser);
2701 /* If there are no tokens left then all went well. */
2702 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2704 /* Get rid of the token array; we don't need it any more. */
2705 cp_lexer_destroy (parser->lexer);
2706 parser->lexer = NULL;
2708 /* This file might have been a context that's implicitly extern
2709 "C". If so, pop the lang context. (Only relevant for PCH.) */
2710 if (parser->implicit_extern_c)
2712 pop_lang_context ();
2713 parser->implicit_extern_c = false;
2717 finish_translation_unit ();
2723 cp_parser_error (parser, "expected declaration");
2727 /* Make sure the declarator obstack was fully cleaned up. */
2728 gcc_assert (obstack_next_free (&declarator_obstack)
2729 == declarator_obstack_base);
2731 /* All went well. */
2735 /* Expressions [gram.expr] */
2737 /* Parse a primary-expression.
2748 ( compound-statement )
2749 __builtin_va_arg ( assignment-expression , type-id )
2751 Objective-C++ Extension:
2759 ADDRESS_P is true iff this expression was immediately preceded by
2760 "&" and therefore might denote a pointer-to-member. CAST_P is true
2761 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2762 true iff this expression is a template argument.
2764 Returns a representation of the expression. Upon return, *IDK
2765 indicates what kind of id-expression (if any) was present. */
2768 cp_parser_primary_expression (cp_parser *parser,
2771 bool template_arg_p,
2776 /* Assume the primary expression is not an id-expression. */
2777 *idk = CP_ID_KIND_NONE;
2779 /* Peek at the next token. */
2780 token = cp_lexer_peek_token (parser->lexer);
2781 switch (token->type)
2792 token = cp_lexer_consume_token (parser->lexer);
2793 /* Floating-point literals are only allowed in an integral
2794 constant expression if they are cast to an integral or
2795 enumeration type. */
2796 if (TREE_CODE (token->value) == REAL_CST
2797 && parser->integral_constant_expression_p
2800 /* CAST_P will be set even in invalid code like "int(2.7 +
2801 ...)". Therefore, we have to check that the next token
2802 is sure to end the cast. */
2805 cp_token *next_token;
2807 next_token = cp_lexer_peek_token (parser->lexer);
2808 if (/* The comma at the end of an
2809 enumerator-definition. */
2810 next_token->type != CPP_COMMA
2811 /* The curly brace at the end of an enum-specifier. */
2812 && next_token->type != CPP_CLOSE_BRACE
2813 /* The end of a statement. */
2814 && next_token->type != CPP_SEMICOLON
2815 /* The end of the cast-expression. */
2816 && next_token->type != CPP_CLOSE_PAREN
2817 /* The end of an array bound. */
2818 && next_token->type != CPP_CLOSE_SQUARE
2819 /* The closing ">" in a template-argument-list. */
2820 && (next_token->type != CPP_GREATER
2821 || parser->greater_than_is_operator_p))
2825 /* If we are within a cast, then the constraint that the
2826 cast is to an integral or enumeration type will be
2827 checked at that point. If we are not within a cast, then
2828 this code is invalid. */
2830 cp_parser_non_integral_constant_expression
2831 (parser, "floating-point literal");
2833 return token->value;
2837 /* ??? Should wide strings be allowed when parser->translate_strings_p
2838 is false (i.e. in attributes)? If not, we can kill the third
2839 argument to cp_parser_string_literal. */
2840 return cp_parser_string_literal (parser,
2841 parser->translate_strings_p,
2844 case CPP_OPEN_PAREN:
2847 bool saved_greater_than_is_operator_p;
2849 /* Consume the `('. */
2850 cp_lexer_consume_token (parser->lexer);
2851 /* Within a parenthesized expression, a `>' token is always
2852 the greater-than operator. */
2853 saved_greater_than_is_operator_p
2854 = parser->greater_than_is_operator_p;
2855 parser->greater_than_is_operator_p = true;
2856 /* If we see `( { ' then we are looking at the beginning of
2857 a GNU statement-expression. */
2858 if (cp_parser_allow_gnu_extensions_p (parser)
2859 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2861 /* Statement-expressions are not allowed by the standard. */
2863 pedwarn ("ISO C++ forbids braced-groups within expressions");
2865 /* And they're not allowed outside of a function-body; you
2866 cannot, for example, write:
2868 int i = ({ int j = 3; j + 1; });
2870 at class or namespace scope. */
2871 if (!at_function_scope_p ())
2872 error ("statement-expressions are allowed only inside functions");
2873 /* Start the statement-expression. */
2874 expr = begin_stmt_expr ();
2875 /* Parse the compound-statement. */
2876 cp_parser_compound_statement (parser, expr, false);
2878 expr = finish_stmt_expr (expr, false);
2882 /* Parse the parenthesized expression. */
2883 expr = cp_parser_expression (parser, cast_p);
2884 /* Let the front end know that this expression was
2885 enclosed in parentheses. This matters in case, for
2886 example, the expression is of the form `A::B', since
2887 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2889 finish_parenthesized_expr (expr);
2891 /* The `>' token might be the end of a template-id or
2892 template-parameter-list now. */
2893 parser->greater_than_is_operator_p
2894 = saved_greater_than_is_operator_p;
2895 /* Consume the `)'. */
2896 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2897 cp_parser_skip_to_end_of_statement (parser);
2903 switch (token->keyword)
2905 /* These two are the boolean literals. */
2907 cp_lexer_consume_token (parser->lexer);
2908 return boolean_true_node;
2910 cp_lexer_consume_token (parser->lexer);
2911 return boolean_false_node;
2913 /* The `__null' literal. */
2915 cp_lexer_consume_token (parser->lexer);
2918 /* Recognize the `this' keyword. */
2920 cp_lexer_consume_token (parser->lexer);
2921 if (parser->local_variables_forbidden_p)
2923 error ("%<this%> may not be used in this context");
2924 return error_mark_node;
2926 /* Pointers cannot appear in constant-expressions. */
2927 if (cp_parser_non_integral_constant_expression (parser,
2929 return error_mark_node;
2930 return finish_this_expr ();
2932 /* The `operator' keyword can be the beginning of an
2937 case RID_FUNCTION_NAME:
2938 case RID_PRETTY_FUNCTION_NAME:
2939 case RID_C99_FUNCTION_NAME:
2940 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2941 __func__ are the names of variables -- but they are
2942 treated specially. Therefore, they are handled here,
2943 rather than relying on the generic id-expression logic
2944 below. Grammatically, these names are id-expressions.
2946 Consume the token. */
2947 token = cp_lexer_consume_token (parser->lexer);
2948 /* Look up the name. */
2949 return finish_fname (token->value);
2956 /* The `__builtin_va_arg' construct is used to handle
2957 `va_arg'. Consume the `__builtin_va_arg' token. */
2958 cp_lexer_consume_token (parser->lexer);
2959 /* Look for the opening `('. */
2960 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2961 /* Now, parse the assignment-expression. */
2962 expression = cp_parser_assignment_expression (parser,
2964 /* Look for the `,'. */
2965 cp_parser_require (parser, CPP_COMMA, "`,'");
2966 /* Parse the type-id. */
2967 type = cp_parser_type_id (parser);
2968 /* Look for the closing `)'. */
2969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2970 /* Using `va_arg' in a constant-expression is not
2972 if (cp_parser_non_integral_constant_expression (parser,
2974 return error_mark_node;
2975 return build_x_va_arg (expression, type);
2979 return cp_parser_builtin_offsetof (parser);
2981 /* Objective-C++ expressions. */
2983 case RID_AT_PROTOCOL:
2984 case RID_AT_SELECTOR:
2985 return cp_parser_objc_expression (parser);
2988 cp_parser_error (parser, "expected primary-expression");
2989 return error_mark_node;
2992 /* An id-expression can start with either an identifier, a
2993 `::' as the beginning of a qualified-id, or the "operator"
2997 case CPP_TEMPLATE_ID:
2998 case CPP_NESTED_NAME_SPECIFIER:
3002 const char *error_msg;
3007 /* Parse the id-expression. */
3009 = cp_parser_id_expression (parser,
3010 /*template_keyword_p=*/false,
3011 /*check_dependency_p=*/true,
3013 /*declarator_p=*/false);
3014 if (id_expression == error_mark_node)
3015 return error_mark_node;
3016 token = cp_lexer_peek_token (parser->lexer);
3017 done = (token->type != CPP_OPEN_SQUARE
3018 && token->type != CPP_OPEN_PAREN
3019 && token->type != CPP_DOT
3020 && token->type != CPP_DEREF
3021 && token->type != CPP_PLUS_PLUS
3022 && token->type != CPP_MINUS_MINUS);
3023 /* If we have a template-id, then no further lookup is
3024 required. If the template-id was for a template-class, we
3025 will sometimes have a TYPE_DECL at this point. */
3026 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3027 || TREE_CODE (id_expression) == TYPE_DECL)
3028 decl = id_expression;
3029 /* Look up the name. */
3032 tree ambiguous_decls;
3034 decl = cp_parser_lookup_name (parser, id_expression,
3037 /*is_namespace=*/false,
3038 /*check_dependency=*/true,
3040 /* If the lookup was ambiguous, an error will already have
3042 if (ambiguous_decls)
3043 return error_mark_node;
3045 /* In Objective-C++, an instance variable (ivar) may be preferred
3046 to whatever cp_parser_lookup_name() found. */
3047 decl = objc_lookup_ivar (decl, id_expression);
3049 /* If name lookup gives us a SCOPE_REF, then the
3050 qualifying scope was dependent. */
3051 if (TREE_CODE (decl) == SCOPE_REF)
3053 /* Check to see if DECL is a local variable in a context
3054 where that is forbidden. */
3055 if (parser->local_variables_forbidden_p
3056 && local_variable_p (decl))
3058 /* It might be that we only found DECL because we are
3059 trying to be generous with pre-ISO scoping rules.
3060 For example, consider:
3064 for (int i = 0; i < 10; ++i) {}
3065 extern void f(int j = i);
3068 Here, name look up will originally find the out
3069 of scope `i'. We need to issue a warning message,
3070 but then use the global `i'. */
3071 decl = check_for_out_of_scope_variable (decl);
3072 if (local_variable_p (decl))
3074 error ("local variable %qD may not appear in this context",
3076 return error_mark_node;
3081 decl = (finish_id_expression
3082 (id_expression, decl, parser->scope,
3084 parser->integral_constant_expression_p,
3085 parser->allow_non_integral_constant_expression_p,
3086 &parser->non_integral_constant_expression_p,
3087 template_p, done, address_p,
3091 cp_parser_error (parser, error_msg);
3095 /* Anything else is an error. */
3097 /* ...unless we have an Objective-C++ message or string literal, that is. */
3098 if (c_dialect_objc ()
3099 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3100 return cp_parser_objc_expression (parser);
3102 cp_parser_error (parser, "expected primary-expression");
3103 return error_mark_node;
3107 /* Parse an id-expression.
3114 :: [opt] nested-name-specifier template [opt] unqualified-id
3116 :: operator-function-id
3119 Return a representation of the unqualified portion of the
3120 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3121 a `::' or nested-name-specifier.
3123 Often, if the id-expression was a qualified-id, the caller will
3124 want to make a SCOPE_REF to represent the qualified-id. This
3125 function does not do this in order to avoid wastefully creating
3126 SCOPE_REFs when they are not required.
3128 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3131 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3132 uninstantiated templates.
3134 If *TEMPLATE_P is non-NULL, it is set to true iff the
3135 `template' keyword is used to explicitly indicate that the entity
3136 named is a template.
3138 If DECLARATOR_P is true, the id-expression is appearing as part of
3139 a declarator, rather than as part of an expression. */
3142 cp_parser_id_expression (cp_parser *parser,
3143 bool template_keyword_p,
3144 bool check_dependency_p,
3148 bool global_scope_p;
3149 bool nested_name_specifier_p;
3151 /* Assume the `template' keyword was not used. */
3153 *template_p = template_keyword_p;
3155 /* Look for the optional `::' operator. */
3157 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3159 /* Look for the optional nested-name-specifier. */
3160 nested_name_specifier_p
3161 = (cp_parser_nested_name_specifier_opt (parser,
3162 /*typename_keyword_p=*/false,
3167 /* If there is a nested-name-specifier, then we are looking at
3168 the first qualified-id production. */
3169 if (nested_name_specifier_p)
3172 tree saved_object_scope;
3173 tree saved_qualifying_scope;
3174 tree unqualified_id;
3177 /* See if the next token is the `template' keyword. */
3179 template_p = &is_template;
3180 *template_p = cp_parser_optional_template_keyword (parser);
3181 /* Name lookup we do during the processing of the
3182 unqualified-id might obliterate SCOPE. */
3183 saved_scope = parser->scope;
3184 saved_object_scope = parser->object_scope;
3185 saved_qualifying_scope = parser->qualifying_scope;
3186 /* Process the final unqualified-id. */
3187 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3190 /* Restore the SAVED_SCOPE for our caller. */
3191 parser->scope = saved_scope;
3192 parser->object_scope = saved_object_scope;
3193 parser->qualifying_scope = saved_qualifying_scope;
3195 return unqualified_id;
3197 /* Otherwise, if we are in global scope, then we are looking at one
3198 of the other qualified-id productions. */
3199 else if (global_scope_p)
3204 /* Peek at the next token. */
3205 token = cp_lexer_peek_token (parser->lexer);
3207 /* If it's an identifier, and the next token is not a "<", then
3208 we can avoid the template-id case. This is an optimization
3209 for this common case. */
3210 if (token->type == CPP_NAME
3211 && !cp_parser_nth_token_starts_template_argument_list_p
3213 return cp_parser_identifier (parser);
3215 cp_parser_parse_tentatively (parser);
3216 /* Try a template-id. */
3217 id = cp_parser_template_id (parser,
3218 /*template_keyword_p=*/false,
3219 /*check_dependency_p=*/true,
3221 /* If that worked, we're done. */
3222 if (cp_parser_parse_definitely (parser))
3225 /* Peek at the next token. (Changes in the token buffer may
3226 have invalidated the pointer obtained above.) */
3227 token = cp_lexer_peek_token (parser->lexer);
3229 switch (token->type)
3232 return cp_parser_identifier (parser);
3235 if (token->keyword == RID_OPERATOR)
3236 return cp_parser_operator_function_id (parser);
3240 cp_parser_error (parser, "expected id-expression");
3241 return error_mark_node;
3245 return cp_parser_unqualified_id (parser, template_keyword_p,
3246 /*check_dependency_p=*/true,
3250 /* Parse an unqualified-id.
3254 operator-function-id
3255 conversion-function-id
3259 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3260 keyword, in a construct like `A::template ...'.
3262 Returns a representation of unqualified-id. For the `identifier'
3263 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3264 production a BIT_NOT_EXPR is returned; the operand of the
3265 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3266 other productions, see the documentation accompanying the
3267 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3268 names are looked up in uninstantiated templates. If DECLARATOR_P
3269 is true, the unqualified-id is appearing as part of a declarator,
3270 rather than as part of an expression. */
3273 cp_parser_unqualified_id (cp_parser* parser,
3274 bool template_keyword_p,
3275 bool check_dependency_p,
3280 /* Peek at the next token. */
3281 token = cp_lexer_peek_token (parser->lexer);
3283 switch (token->type)
3289 /* We don't know yet whether or not this will be a
3291 cp_parser_parse_tentatively (parser);
3292 /* Try a template-id. */
3293 id = cp_parser_template_id (parser, template_keyword_p,
3296 /* If it worked, we're done. */
3297 if (cp_parser_parse_definitely (parser))
3299 /* Otherwise, it's an ordinary identifier. */
3300 return cp_parser_identifier (parser);
3303 case CPP_TEMPLATE_ID:
3304 return cp_parser_template_id (parser, template_keyword_p,
3311 tree qualifying_scope;
3316 /* Consume the `~' token. */
3317 cp_lexer_consume_token (parser->lexer);
3318 /* Parse the class-name. The standard, as written, seems to
3321 template <typename T> struct S { ~S (); };
3322 template <typename T> S<T>::~S() {}
3324 is invalid, since `~' must be followed by a class-name, but
3325 `S<T>' is dependent, and so not known to be a class.
3326 That's not right; we need to look in uninstantiated
3327 templates. A further complication arises from:
3329 template <typename T> void f(T t) {
3333 Here, it is not possible to look up `T' in the scope of `T'
3334 itself. We must look in both the current scope, and the
3335 scope of the containing complete expression.
3337 Yet another issue is:
3346 The standard does not seem to say that the `S' in `~S'
3347 should refer to the type `S' and not the data member
3350 /* DR 244 says that we look up the name after the "~" in the
3351 same scope as we looked up the qualifying name. That idea
3352 isn't fully worked out; it's more complicated than that. */
3353 scope = parser->scope;
3354 object_scope = parser->object_scope;
3355 qualifying_scope = parser->qualifying_scope;
3357 /* If the name is of the form "X::~X" it's OK. */
3358 if (scope && TYPE_P (scope)
3359 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3360 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3362 && (cp_lexer_peek_token (parser->lexer)->value
3363 == TYPE_IDENTIFIER (scope)))
3365 cp_lexer_consume_token (parser->lexer);
3366 return build_nt (BIT_NOT_EXPR, scope);
3369 /* If there was an explicit qualification (S::~T), first look
3370 in the scope given by the qualification (i.e., S). */
3372 type_decl = NULL_TREE;
3375 cp_parser_parse_tentatively (parser);
3376 type_decl = cp_parser_class_name (parser,
3377 /*typename_keyword_p=*/false,
3378 /*template_keyword_p=*/false,
3380 /*check_dependency=*/false,
3381 /*class_head_p=*/false,
3383 if (cp_parser_parse_definitely (parser))
3386 /* In "N::S::~S", look in "N" as well. */
3387 if (!done && scope && qualifying_scope)
3389 cp_parser_parse_tentatively (parser);
3390 parser->scope = qualifying_scope;
3391 parser->object_scope = NULL_TREE;
3392 parser->qualifying_scope = NULL_TREE;
3394 = cp_parser_class_name (parser,
3395 /*typename_keyword_p=*/false,
3396 /*template_keyword_p=*/false,
3398 /*check_dependency=*/false,
3399 /*class_head_p=*/false,
3401 if (cp_parser_parse_definitely (parser))
3404 /* In "p->S::~T", look in the scope given by "*p" as well. */
3405 else if (!done && object_scope)
3407 cp_parser_parse_tentatively (parser);
3408 parser->scope = object_scope;
3409 parser->object_scope = NULL_TREE;
3410 parser->qualifying_scope = NULL_TREE;
3412 = cp_parser_class_name (parser,
3413 /*typename_keyword_p=*/false,
3414 /*template_keyword_p=*/false,
3416 /*check_dependency=*/false,
3417 /*class_head_p=*/false,
3419 if (cp_parser_parse_definitely (parser))
3422 /* Look in the surrounding context. */
3425 parser->scope = NULL_TREE;
3426 parser->object_scope = NULL_TREE;
3427 parser->qualifying_scope = NULL_TREE;
3429 = cp_parser_class_name (parser,
3430 /*typename_keyword_p=*/false,
3431 /*template_keyword_p=*/false,
3433 /*check_dependency=*/false,
3434 /*class_head_p=*/false,
3437 /* If an error occurred, assume that the name of the
3438 destructor is the same as the name of the qualifying
3439 class. That allows us to keep parsing after running
3440 into ill-formed destructor names. */
3441 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3442 return build_nt (BIT_NOT_EXPR, scope);
3443 else if (type_decl == error_mark_node)
3444 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 tree access_check = NULL_TREE;
3521 cp_token_position start = 0;
3524 /* If the next token corresponds to a nested name specifier, there
3525 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3526 false, it may have been true before, in which case something
3527 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3528 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3529 CHECK_DEPENDENCY_P is false, we have to fall through into the
3531 if (check_dependency_p
3532 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3534 cp_parser_pre_parsed_nested_name_specifier (parser);
3535 return parser->scope;
3538 /* Remember where the nested-name-specifier starts. */
3539 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 start = cp_lexer_token_position (parser->lexer, false);
3542 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 /* Retrieve any deferred checks. Do not pop this access checks yet
3722 so the memory will not be reclaimed during token replacing below. */
3723 access_check = get_deferred_access_checks ();
3725 /* If parsing tentatively, replace the sequence of tokens that makes
3726 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3727 token. That way, should we re-parse the token stream, we will
3728 not have to repeat the effort required to do the parse, nor will
3729 we issue duplicate error messages. */
3730 if (success && start)
3732 cp_token *token = cp_lexer_token_at (parser->lexer, start);
3734 /* Reset the contents of the START token. */
3735 token->type = CPP_NESTED_NAME_SPECIFIER;
3736 token->value = build_tree_list (access_check, parser->scope);
3737 TREE_TYPE (token->value) = parser->qualifying_scope;
3738 token->keyword = RID_MAX;
3740 /* Purge all subsequent tokens. */
3741 cp_lexer_purge_tokens_after (parser->lexer, start);
3744 pop_deferring_access_checks ();
3745 return success ? parser->scope : NULL_TREE;
3748 /* Parse a nested-name-specifier. See
3749 cp_parser_nested_name_specifier_opt for details. This function
3750 behaves identically, except that it will an issue an error if no
3751 nested-name-specifier is present. */
3754 cp_parser_nested_name_specifier (cp_parser *parser,
3755 bool typename_keyword_p,
3756 bool check_dependency_p,
3758 bool is_declaration)
3762 /* Look for the nested-name-specifier. */
3763 scope = cp_parser_nested_name_specifier_opt (parser,
3768 /* If it was not present, issue an error message. */
3771 cp_parser_error (parser, "expected nested-name-specifier");
3772 parser->scope = NULL_TREE;
3778 /* Parse a class-or-namespace-name.
3780 class-or-namespace-name:
3784 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3785 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3786 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3787 TYPE_P is TRUE iff the next name should be taken as a class-name,
3788 even the same name is declared to be another entity in the same
3791 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3792 specified by the class-or-namespace-name. If neither is found the
3793 ERROR_MARK_NODE is returned. */
3796 cp_parser_class_or_namespace_name (cp_parser *parser,
3797 bool typename_keyword_p,
3798 bool template_keyword_p,
3799 bool check_dependency_p,
3801 bool is_declaration)
3804 tree saved_qualifying_scope;
3805 tree saved_object_scope;
3809 /* Before we try to parse the class-name, we must save away the
3810 current PARSER->SCOPE since cp_parser_class_name will destroy
3812 saved_scope = parser->scope;
3813 saved_qualifying_scope = parser->qualifying_scope;
3814 saved_object_scope = parser->object_scope;
3815 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3816 there is no need to look for a namespace-name. */
3817 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3819 cp_parser_parse_tentatively (parser);
3820 scope = cp_parser_class_name (parser,
3823 type_p ? class_type : none_type,
3825 /*class_head_p=*/false,
3827 /* If that didn't work, try for a namespace-name. */
3828 if (!only_class_p && !cp_parser_parse_definitely (parser))
3830 /* Restore the saved scope. */
3831 parser->scope = saved_scope;
3832 parser->qualifying_scope = saved_qualifying_scope;
3833 parser->object_scope = saved_object_scope;
3834 /* If we are not looking at an identifier followed by the scope
3835 resolution operator, then this is not part of a
3836 nested-name-specifier. (Note that this function is only used
3837 to parse the components of a nested-name-specifier.) */
3838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3839 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3840 return error_mark_node;
3841 scope = cp_parser_namespace_name (parser);
3847 /* Parse a postfix-expression.
3851 postfix-expression [ expression ]
3852 postfix-expression ( expression-list [opt] )
3853 simple-type-specifier ( expression-list [opt] )
3854 typename :: [opt] nested-name-specifier identifier
3855 ( expression-list [opt] )
3856 typename :: [opt] nested-name-specifier template [opt] template-id
3857 ( expression-list [opt] )
3858 postfix-expression . template [opt] id-expression
3859 postfix-expression -> template [opt] id-expression
3860 postfix-expression . pseudo-destructor-name
3861 postfix-expression -> pseudo-destructor-name
3862 postfix-expression ++
3863 postfix-expression --
3864 dynamic_cast < type-id > ( expression )
3865 static_cast < type-id > ( expression )
3866 reinterpret_cast < type-id > ( expression )
3867 const_cast < type-id > ( expression )
3868 typeid ( expression )
3874 ( type-id ) { initializer-list , [opt] }
3876 This extension is a GNU version of the C99 compound-literal
3877 construct. (The C99 grammar uses `type-name' instead of `type-id',
3878 but they are essentially the same concept.)
3880 If ADDRESS_P is true, the postfix expression is the operand of the
3881 `&' operator. CAST_P is true if this expression is the target of a
3884 Returns a representation of the expression. */
3887 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3891 cp_id_kind idk = CP_ID_KIND_NONE;
3892 tree postfix_expression = NULL_TREE;
3894 /* Peek at the next token. */
3895 token = cp_lexer_peek_token (parser->lexer);
3896 /* Some of the productions are determined by keywords. */
3897 keyword = token->keyword;
3907 const char *saved_message;
3909 /* All of these can be handled in the same way from the point
3910 of view of parsing. Begin by consuming the token
3911 identifying the cast. */
3912 cp_lexer_consume_token (parser->lexer);
3914 /* New types cannot be defined in the cast. */
3915 saved_message = parser->type_definition_forbidden_message;
3916 parser->type_definition_forbidden_message
3917 = "types may not be defined in casts";
3919 /* Look for the opening `<'. */
3920 cp_parser_require (parser, CPP_LESS, "`<'");
3921 /* Parse the type to which we are casting. */
3922 type = cp_parser_type_id (parser);
3923 /* Look for the closing `>'. */
3924 cp_parser_require (parser, CPP_GREATER, "`>'");
3925 /* Restore the old message. */
3926 parser->type_definition_forbidden_message = saved_message;
3928 /* And the expression which is being cast. */
3929 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3930 expression = cp_parser_expression (parser, /*cast_p=*/true);
3931 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3933 /* Only type conversions to integral or enumeration types
3934 can be used in constant-expressions. */
3935 if (parser->integral_constant_expression_p
3936 && !dependent_type_p (type)
3937 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3938 && (cp_parser_non_integral_constant_expression
3940 "a cast to a type other than an integral or "
3941 "enumeration type")))
3942 return error_mark_node;
3948 = build_dynamic_cast (type, expression);
3952 = build_static_cast (type, expression);
3956 = build_reinterpret_cast (type, expression);
3960 = build_const_cast (type, expression);
3971 const char *saved_message;
3972 bool saved_in_type_id_in_expr_p;
3974 /* Consume the `typeid' token. */
3975 cp_lexer_consume_token (parser->lexer);
3976 /* Look for the `(' token. */
3977 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3978 /* Types cannot be defined in a `typeid' expression. */
3979 saved_message = parser->type_definition_forbidden_message;
3980 parser->type_definition_forbidden_message
3981 = "types may not be defined in a `typeid\' expression";
3982 /* We can't be sure yet whether we're looking at a type-id or an
3984 cp_parser_parse_tentatively (parser);
3985 /* Try a type-id first. */
3986 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3987 parser->in_type_id_in_expr_p = true;
3988 type = cp_parser_type_id (parser);
3989 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3990 /* Look for the `)' token. Otherwise, we can't be sure that
3991 we're not looking at an expression: consider `typeid (int
3992 (3))', for example. */
3993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3994 /* If all went well, simply lookup the type-id. */
3995 if (cp_parser_parse_definitely (parser))
3996 postfix_expression = get_typeid (type);
3997 /* Otherwise, fall back to the expression variant. */
4002 /* Look for an expression. */
4003 expression = cp_parser_expression (parser, /*cast_p=*/false);
4004 /* Compute its typeid. */
4005 postfix_expression = build_typeid (expression);
4006 /* Look for the `)' token. */
4007 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4009 /* `typeid' may not appear in an integral constant expression. */
4010 if (cp_parser_non_integral_constant_expression(parser,
4011 "`typeid' operator"))
4012 return error_mark_node;
4013 /* Restore the saved message. */
4014 parser->type_definition_forbidden_message = saved_message;
4021 /* The syntax permitted here is the same permitted for an
4022 elaborated-type-specifier. */
4023 type = cp_parser_elaborated_type_specifier (parser,
4024 /*is_friend=*/false,
4025 /*is_declaration=*/false);
4026 postfix_expression = cp_parser_functional_cast (parser, type);
4034 /* If the next thing is a simple-type-specifier, we may be
4035 looking at a functional cast. We could also be looking at
4036 an id-expression. So, we try the functional cast, and if
4037 that doesn't work we fall back to the primary-expression. */
4038 cp_parser_parse_tentatively (parser);
4039 /* Look for the simple-type-specifier. */
4040 type = cp_parser_simple_type_specifier (parser,
4041 /*decl_specs=*/NULL,
4042 CP_PARSER_FLAGS_NONE);
4043 /* Parse the cast itself. */
4044 if (!cp_parser_error_occurred (parser))
4046 = cp_parser_functional_cast (parser, type);
4047 /* If that worked, we're done. */
4048 if (cp_parser_parse_definitely (parser))
4051 /* If the functional-cast didn't work out, try a
4052 compound-literal. */
4053 if (cp_parser_allow_gnu_extensions_p (parser)
4054 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4056 VEC(constructor_elt,gc) *initializer_list = NULL;
4057 bool saved_in_type_id_in_expr_p;
4059 cp_parser_parse_tentatively (parser);
4060 /* Consume the `('. */
4061 cp_lexer_consume_token (parser->lexer);
4062 /* Parse the type. */
4063 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4064 parser->in_type_id_in_expr_p = true;
4065 type = cp_parser_type_id (parser);
4066 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4067 /* Look for the `)'. */
4068 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4069 /* Look for the `{'. */
4070 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4071 /* If things aren't going well, there's no need to
4073 if (!cp_parser_error_occurred (parser))
4075 bool non_constant_p;
4076 /* Parse the initializer-list. */
4078 = cp_parser_initializer_list (parser, &non_constant_p);
4079 /* Allow a trailing `,'. */
4080 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4081 cp_lexer_consume_token (parser->lexer);
4082 /* Look for the final `}'. */
4083 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4085 /* If that worked, we're definitely looking at a
4086 compound-literal expression. */
4087 if (cp_parser_parse_definitely (parser))
4089 /* Warn the user that a compound literal is not
4090 allowed in standard C++. */
4092 pedwarn ("ISO C++ forbids compound-literals");
4093 /* Form the representation of the compound-literal. */
4095 = finish_compound_literal (type, initializer_list);
4100 /* It must be a primary-expression. */
4102 = cp_parser_primary_expression (parser, address_p, cast_p,
4103 /*template_arg_p=*/false,
4109 /* Keep looping until the postfix-expression is complete. */
4112 if (idk == CP_ID_KIND_UNQUALIFIED
4113 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4114 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4115 /* It is not a Koenig lookup function call. */
4117 = unqualified_name_lookup_error (postfix_expression);
4119 /* Peek at the next token. */
4120 token = cp_lexer_peek_token (parser->lexer);
4122 switch (token->type)
4124 case CPP_OPEN_SQUARE:
4126 = cp_parser_postfix_open_square_expression (parser,
4129 idk = CP_ID_KIND_NONE;
4132 case CPP_OPEN_PAREN:
4133 /* postfix-expression ( expression-list [opt] ) */
4136 bool is_builtin_constant_p;
4137 bool saved_integral_constant_expression_p = false;
4138 bool saved_non_integral_constant_expression_p = false;
4141 is_builtin_constant_p
4142 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4143 if (is_builtin_constant_p)
4145 /* The whole point of __builtin_constant_p is to allow
4146 non-constant expressions to appear as arguments. */
4147 saved_integral_constant_expression_p
4148 = parser->integral_constant_expression_p;
4149 saved_non_integral_constant_expression_p
4150 = parser->non_integral_constant_expression_p;
4151 parser->integral_constant_expression_p = false;
4153 args = (cp_parser_parenthesized_expression_list
4154 (parser, /*is_attribute_list=*/false,
4156 /*non_constant_p=*/NULL));
4157 if (is_builtin_constant_p)
4159 parser->integral_constant_expression_p
4160 = saved_integral_constant_expression_p;
4161 parser->non_integral_constant_expression_p
4162 = saved_non_integral_constant_expression_p;
4165 if (args == error_mark_node)
4167 postfix_expression = error_mark_node;
4171 /* Function calls are not permitted in
4172 constant-expressions. */
4173 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4174 && cp_parser_non_integral_constant_expression (parser,
4177 postfix_expression = error_mark_node;
4182 if (idk == CP_ID_KIND_UNQUALIFIED)
4184 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4190 = perform_koenig_lookup (postfix_expression, args);
4194 = unqualified_fn_lookup_error (postfix_expression);
4196 /* We do not perform argument-dependent lookup if
4197 normal lookup finds a non-function, in accordance
4198 with the expected resolution of DR 218. */
4199 else if (args && is_overloaded_fn (postfix_expression))
4201 tree fn = get_first_fn (postfix_expression);
4203 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4204 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4206 /* Only do argument dependent lookup if regular
4207 lookup does not find a set of member functions.
4208 [basic.lookup.koenig]/2a */
4209 if (!DECL_FUNCTION_MEMBER_P (fn))
4213 = perform_koenig_lookup (postfix_expression, args);
4218 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4220 tree instance = TREE_OPERAND (postfix_expression, 0);
4221 tree fn = TREE_OPERAND (postfix_expression, 1);
4223 if (processing_template_decl
4224 && (type_dependent_expression_p (instance)
4225 || (!BASELINK_P (fn)
4226 && TREE_CODE (fn) != FIELD_DECL)
4227 || type_dependent_expression_p (fn)
4228 || any_type_dependent_arguments_p (args)))
4231 = build_min_nt (CALL_EXPR, postfix_expression,
4236 if (BASELINK_P (fn))
4238 = (build_new_method_call
4239 (instance, fn, args, NULL_TREE,
4240 (idk == CP_ID_KIND_QUALIFIED
4241 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4244 = finish_call_expr (postfix_expression, args,
4245 /*disallow_virtual=*/false,
4246 /*koenig_p=*/false);
4248 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4249 || TREE_CODE (postfix_expression) == MEMBER_REF
4250 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4251 postfix_expression = (build_offset_ref_call_from_tree
4252 (postfix_expression, args));
4253 else if (idk == CP_ID_KIND_QUALIFIED)
4254 /* A call to a static class member, or a namespace-scope
4257 = finish_call_expr (postfix_expression, args,
4258 /*disallow_virtual=*/true,
4261 /* All other function calls. */
4263 = finish_call_expr (postfix_expression, args,
4264 /*disallow_virtual=*/false,
4267 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4268 idk = CP_ID_KIND_NONE;
4274 /* postfix-expression . template [opt] id-expression
4275 postfix-expression . pseudo-destructor-name
4276 postfix-expression -> template [opt] id-expression
4277 postfix-expression -> pseudo-destructor-name */
4279 /* Consume the `.' or `->' operator. */
4280 cp_lexer_consume_token (parser->lexer);
4283 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4289 /* postfix-expression ++ */
4290 /* Consume the `++' token. */
4291 cp_lexer_consume_token (parser->lexer);
4292 /* Generate a representation for the complete expression. */
4294 = finish_increment_expr (postfix_expression,
4295 POSTINCREMENT_EXPR);
4296 /* Increments may not appear in constant-expressions. */
4297 if (cp_parser_non_integral_constant_expression (parser,
4299 postfix_expression = error_mark_node;
4300 idk = CP_ID_KIND_NONE;
4303 case CPP_MINUS_MINUS:
4304 /* postfix-expression -- */
4305 /* Consume the `--' token. */
4306 cp_lexer_consume_token (parser->lexer);
4307 /* Generate a representation for the complete expression. */
4309 = finish_increment_expr (postfix_expression,
4310 POSTDECREMENT_EXPR);
4311 /* Decrements may not appear in constant-expressions. */
4312 if (cp_parser_non_integral_constant_expression (parser,
4314 postfix_expression = error_mark_node;
4315 idk = CP_ID_KIND_NONE;
4319 return postfix_expression;
4323 /* We should never get here. */
4325 return error_mark_node;
4328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4329 by cp_parser_builtin_offsetof. We're looking for
4331 postfix-expression [ expression ]
4333 FOR_OFFSETOF is set if we're being called in that context, which
4334 changes how we deal with integer constant expressions. */
4337 cp_parser_postfix_open_square_expression (cp_parser *parser,
4338 tree postfix_expression,
4343 /* Consume the `[' token. */
4344 cp_lexer_consume_token (parser->lexer);
4346 /* Parse the index expression. */
4347 /* ??? For offsetof, there is a question of what to allow here. If
4348 offsetof is not being used in an integral constant expression context,
4349 then we *could* get the right answer by computing the value at runtime.
4350 If we are in an integral constant expression context, then we might
4351 could accept any constant expression; hard to say without analysis.
4352 Rather than open the barn door too wide right away, allow only integer
4353 constant expressions here. */
4355 index = cp_parser_constant_expression (parser, false, NULL);
4357 index = cp_parser_expression (parser, /*cast_p=*/false);
4359 /* Look for the closing `]'. */
4360 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4362 /* Build the ARRAY_REF. */
4363 postfix_expression = grok_array_decl (postfix_expression, index);
4365 /* When not doing offsetof, array references are not permitted in
4366 constant-expressions. */
4368 && (cp_parser_non_integral_constant_expression
4369 (parser, "an array reference")))
4370 postfix_expression = error_mark_node;
4372 return postfix_expression;
4375 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4376 by cp_parser_builtin_offsetof. We're looking for
4378 postfix-expression . template [opt] id-expression
4379 postfix-expression . pseudo-destructor-name
4380 postfix-expression -> template [opt] id-expression
4381 postfix-expression -> pseudo-destructor-name
4383 FOR_OFFSETOF is set if we're being called in that context. That sorta
4384 limits what of the above we'll actually accept, but nevermind.
4385 TOKEN_TYPE is the "." or "->" token, which will already have been
4386 removed from the stream. */
4389 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4390 enum cpp_ttype token_type,
4391 tree postfix_expression,
4392 bool for_offsetof, cp_id_kind *idk)
4396 bool pseudo_destructor_p;
4397 tree scope = NULL_TREE;
4399 /* If this is a `->' operator, dereference the pointer. */
4400 if (token_type == CPP_DEREF)
4401 postfix_expression = build_x_arrow (postfix_expression);
4402 /* Check to see whether or not the expression is type-dependent. */
4403 dependent_p = type_dependent_expression_p (postfix_expression);
4404 /* The identifier following the `->' or `.' is not qualified. */
4405 parser->scope = NULL_TREE;
4406 parser->qualifying_scope = NULL_TREE;
4407 parser->object_scope = NULL_TREE;
4408 *idk = CP_ID_KIND_NONE;
4409 /* Enter the scope corresponding to the type of the object
4410 given by the POSTFIX_EXPRESSION. */
4411 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4413 scope = TREE_TYPE (postfix_expression);
4414 /* According to the standard, no expression should ever have
4415 reference type. Unfortunately, we do not currently match
4416 the standard in this respect in that our internal representation
4417 of an expression may have reference type even when the standard
4418 says it does not. Therefore, we have to manually obtain the
4419 underlying type here. */
4420 scope = non_reference (scope);
4421 /* The type of the POSTFIX_EXPRESSION must be complete. */
4422 if (scope == unknown_type_node)
4424 error ("%qE does not have class type", postfix_expression);
4428 scope = complete_type_or_else (scope, NULL_TREE);
4429 /* Let the name lookup machinery know that we are processing a
4430 class member access expression. */
4431 parser->context->object_type = scope;
4432 /* If something went wrong, we want to be able to discern that case,
4433 as opposed to the case where there was no SCOPE due to the type
4434 of expression being dependent. */
4436 scope = error_mark_node;
4437 /* If the SCOPE was erroneous, make the various semantic analysis
4438 functions exit quickly -- and without issuing additional error
4440 if (scope == error_mark_node)
4441 postfix_expression = error_mark_node;
4444 /* Assume this expression is not a pseudo-destructor access. */
4445 pseudo_destructor_p = false;
4447 /* If the SCOPE is a scalar type, then, if this is a valid program,
4448 we must be looking at a pseudo-destructor-name. */
4449 if (scope && SCALAR_TYPE_P (scope))
4454 cp_parser_parse_tentatively (parser);
4455 /* Parse the pseudo-destructor-name. */
4457 cp_parser_pseudo_destructor_name (parser, &s, &type);
4458 if (cp_parser_parse_definitely (parser))
4460 pseudo_destructor_p = true;
4462 = finish_pseudo_destructor_expr (postfix_expression,
4463 s, TREE_TYPE (type));
4467 if (!pseudo_destructor_p)
4469 /* If the SCOPE is not a scalar type, we are looking at an
4470 ordinary class member access expression, rather than a
4471 pseudo-destructor-name. */
4473 /* Parse the id-expression. */
4474 name = (cp_parser_id_expression
4476 cp_parser_optional_template_keyword (parser),
4477 /*check_dependency_p=*/true,
4479 /*declarator_p=*/false));
4480 /* In general, build a SCOPE_REF if the member name is qualified.
4481 However, if the name was not dependent and has already been
4482 resolved; there is no need to build the SCOPE_REF. For example;
4484 struct X { void f(); };
4485 template <typename T> void f(T* t) { t->X::f(); }
4487 Even though "t" is dependent, "X::f" is not and has been resolved
4488 to a BASELINK; there is no need to include scope information. */
4490 /* But we do need to remember that there was an explicit scope for
4491 virtual function calls. */
4493 *idk = CP_ID_KIND_QUALIFIED;
4495 /* If the name is a template-id that names a type, we will get a
4496 TYPE_DECL here. That is invalid code. */
4497 if (TREE_CODE (name) == TYPE_DECL)
4499 error ("invalid use of %qD", name);
4500 postfix_expression = error_mark_node;
4504 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4506 name = build_qualified_name (/*type=*/NULL_TREE,
4510 parser->scope = NULL_TREE;
4511 parser->qualifying_scope = NULL_TREE;
4512 parser->object_scope = NULL_TREE;
4514 if (scope && name && BASELINK_P (name))
4515 adjust_result_of_qualified_name_lookup
4516 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4518 = finish_class_member_access_expr (postfix_expression, name,
4523 /* We no longer need to look up names in the scope of the object on
4524 the left-hand side of the `.' or `->' operator. */
4525 parser->context->object_type = NULL_TREE;
4527 /* Outside of offsetof, these operators may not appear in
4528 constant-expressions. */
4530 && (cp_parser_non_integral_constant_expression
4531 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4532 postfix_expression = error_mark_node;
4534 return postfix_expression;
4537 /* Parse a parenthesized expression-list.
4540 assignment-expression
4541 expression-list, assignment-expression
4546 identifier, expression-list
4548 CAST_P is true if this expression is the target of a cast.
4550 Returns a TREE_LIST. The TREE_VALUE of each node is a
4551 representation of an assignment-expression. Note that a TREE_LIST
4552 is returned even if there is only a single expression in the list.
4553 error_mark_node is returned if the ( and or ) are
4554 missing. NULL_TREE is returned on no expressions. The parentheses
4555 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4556 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4557 indicates whether or not all of the expressions in the list were
4561 cp_parser_parenthesized_expression_list (cp_parser* parser,
4562 bool is_attribute_list,
4564 bool *non_constant_p)
4566 tree expression_list = NULL_TREE;
4567 bool fold_expr_p = is_attribute_list;
4568 tree identifier = NULL_TREE;
4570 /* Assume all the expressions will be constant. */
4572 *non_constant_p = false;
4574 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4575 return error_mark_node;
4577 /* Consume expressions until there are no more. */
4578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4583 /* At the beginning of attribute lists, check to see if the
4584 next token is an identifier. */
4585 if (is_attribute_list
4586 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4590 /* Consume the identifier. */
4591 token = cp_lexer_consume_token (parser->lexer);
4592 /* Save the identifier. */
4593 identifier = token->value;
4597 /* Parse the next assignment-expression. */
4600 bool expr_non_constant_p;
4601 expr = (cp_parser_constant_expression
4602 (parser, /*allow_non_constant_p=*/true,
4603 &expr_non_constant_p));
4604 if (expr_non_constant_p)
4605 *non_constant_p = true;
4608 expr = cp_parser_assignment_expression (parser, cast_p);
4611 expr = fold_non_dependent_expr (expr);
4613 /* Add it to the list. We add error_mark_node
4614 expressions to the list, so that we can still tell if
4615 the correct form for a parenthesized expression-list
4616 is found. That gives better errors. */
4617 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4619 if (expr == error_mark_node)
4623 /* After the first item, attribute lists look the same as
4624 expression lists. */
4625 is_attribute_list = false;
4628 /* If the next token isn't a `,', then we are done. */
4629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4632 /* Otherwise, consume the `,' and keep going. */
4633 cp_lexer_consume_token (parser->lexer);
4636 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4641 /* We try and resync to an unnested comma, as that will give the
4642 user better diagnostics. */
4643 ending = cp_parser_skip_to_closing_parenthesis (parser,
4644 /*recovering=*/true,
4646 /*consume_paren=*/true);
4650 return error_mark_node;
4653 /* We built up the list in reverse order so we must reverse it now. */
4654 expression_list = nreverse (expression_list);
4656 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4658 return expression_list;
4661 /* Parse a pseudo-destructor-name.
4663 pseudo-destructor-name:
4664 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4665 :: [opt] nested-name-specifier template template-id :: ~ type-name
4666 :: [opt] nested-name-specifier [opt] ~ type-name
4668 If either of the first two productions is used, sets *SCOPE to the
4669 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4670 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4671 or ERROR_MARK_NODE if the parse fails. */
4674 cp_parser_pseudo_destructor_name (cp_parser* parser,
4678 bool nested_name_specifier_p;
4680 /* Assume that things will not work out. */
4681 *type = error_mark_node;
4683 /* Look for the optional `::' operator. */
4684 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4685 /* Look for the optional nested-name-specifier. */
4686 nested_name_specifier_p
4687 = (cp_parser_nested_name_specifier_opt (parser,
4688 /*typename_keyword_p=*/false,
4689 /*check_dependency_p=*/true,
4691 /*is_declaration=*/true)
4693 /* Now, if we saw a nested-name-specifier, we might be doing the
4694 second production. */
4695 if (nested_name_specifier_p
4696 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4698 /* Consume the `template' keyword. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Parse the template-id. */
4701 cp_parser_template_id (parser,
4702 /*template_keyword_p=*/true,
4703 /*check_dependency_p=*/false,
4704 /*is_declaration=*/true);
4705 /* Look for the `::' token. */
4706 cp_parser_require (parser, CPP_SCOPE, "`::'");
4708 /* If the next token is not a `~', then there might be some
4709 additional qualification. */
4710 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4712 /* Look for the type-name. */
4713 *scope = TREE_TYPE (cp_parser_type_name (parser));
4715 if (*scope == error_mark_node)
4718 /* If we don't have ::~, then something has gone wrong. Since
4719 the only caller of this function is looking for something
4720 after `.' or `->' after a scalar type, most likely the
4721 program is trying to get a member of a non-aggregate
4723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4724 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4726 cp_parser_error (parser, "request for member of non-aggregate type");
4730 /* Look for the `::' token. */
4731 cp_parser_require (parser, CPP_SCOPE, "`::'");
4736 /* Look for the `~'. */
4737 cp_parser_require (parser, CPP_COMPL, "`~'");
4738 /* Look for the type-name again. We are not responsible for
4739 checking that it matches the first type-name. */
4740 *type = cp_parser_type_name (parser);
4743 /* Parse a unary-expression.
4749 unary-operator cast-expression
4750 sizeof unary-expression
4758 __extension__ cast-expression
4759 __alignof__ unary-expression
4760 __alignof__ ( type-id )
4761 __real__ cast-expression
4762 __imag__ cast-expression
4765 ADDRESS_P is true iff the unary-expression is appearing as the
4766 operand of the `&' operator. CAST_P is true if this expression is
4767 the target of a cast.
4769 Returns a representation of the expression. */
4772 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4775 enum tree_code unary_operator;
4777 /* Peek at the next token. */
4778 token = cp_lexer_peek_token (parser->lexer);
4779 /* Some keywords give away the kind of expression. */
4780 if (token->type == CPP_KEYWORD)
4782 enum rid keyword = token->keyword;
4792 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4793 /* Consume the token. */
4794 cp_lexer_consume_token (parser->lexer);
4795 /* Parse the operand. */
4796 operand = cp_parser_sizeof_operand (parser, keyword);
4798 if (TYPE_P (operand))
4799 return cxx_sizeof_or_alignof_type (operand, op, true);
4801 return cxx_sizeof_or_alignof_expr (operand, op);
4805 return cp_parser_new_expression (parser);
4808 return cp_parser_delete_expression (parser);
4812 /* The saved value of the PEDANTIC flag. */
4816 /* Save away the PEDANTIC flag. */
4817 cp_parser_extension_opt (parser, &saved_pedantic);
4818 /* Parse the cast-expression. */
4819 expr = cp_parser_simple_cast_expression (parser);
4820 /* Restore the PEDANTIC flag. */
4821 pedantic = saved_pedantic;
4831 /* Consume the `__real__' or `__imag__' token. */
4832 cp_lexer_consume_token (parser->lexer);
4833 /* Parse the cast-expression. */
4834 expression = cp_parser_simple_cast_expression (parser);
4835 /* Create the complete representation. */
4836 return build_x_unary_op ((keyword == RID_REALPART
4837 ? REALPART_EXPR : IMAGPART_EXPR),
4847 /* Look for the `:: new' and `:: delete', which also signal the
4848 beginning of a new-expression, or delete-expression,
4849 respectively. If the next token is `::', then it might be one of
4851 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4855 /* See if the token after the `::' is one of the keywords in
4856 which we're interested. */
4857 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4858 /* If it's `new', we have a new-expression. */
4859 if (keyword == RID_NEW)
4860 return cp_parser_new_expression (parser);
4861 /* Similarly, for `delete'. */
4862 else if (keyword == RID_DELETE)
4863 return cp_parser_delete_expression (parser);
4866 /* Look for a unary operator. */
4867 unary_operator = cp_parser_unary_operator (token);
4868 /* The `++' and `--' operators can be handled similarly, even though
4869 they are not technically unary-operators in the grammar. */
4870 if (unary_operator == ERROR_MARK)
4872 if (token->type == CPP_PLUS_PLUS)
4873 unary_operator = PREINCREMENT_EXPR;
4874 else if (token->type == CPP_MINUS_MINUS)
4875 unary_operator = PREDECREMENT_EXPR;
4876 /* Handle the GNU address-of-label extension. */
4877 else if (cp_parser_allow_gnu_extensions_p (parser)
4878 && token->type == CPP_AND_AND)
4882 /* Consume the '&&' token. */
4883 cp_lexer_consume_token (parser->lexer);
4884 /* Look for the identifier. */
4885 identifier = cp_parser_identifier (parser);
4886 /* Create an expression representing the address. */
4887 return finish_label_address_expr (identifier);
4890 if (unary_operator != ERROR_MARK)
4892 tree cast_expression;
4893 tree expression = error_mark_node;
4894 const char *non_constant_p = NULL;
4896 /* Consume the operator token. */
4897 token = cp_lexer_consume_token (parser->lexer);
4898 /* Parse the cast-expression. */
4900 = cp_parser_cast_expression (parser,
4901 unary_operator == ADDR_EXPR,
4903 /* Now, build an appropriate representation. */
4904 switch (unary_operator)
4907 non_constant_p = "`*'";
4908 expression = build_x_indirect_ref (cast_expression, "unary *");
4912 non_constant_p = "`&'";
4915 expression = build_x_unary_op (unary_operator, cast_expression);
4918 case PREINCREMENT_EXPR:
4919 case PREDECREMENT_EXPR:
4920 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4923 case UNARY_PLUS_EXPR:
4925 case TRUTH_NOT_EXPR:
4926 expression = finish_unary_op_expr (unary_operator, cast_expression);
4934 && cp_parser_non_integral_constant_expression (parser,
4936 expression = error_mark_node;
4941 return cp_parser_postfix_expression (parser, address_p, cast_p);
4944 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4945 unary-operator, the corresponding tree code is returned. */
4947 static enum tree_code
4948 cp_parser_unary_operator (cp_token* token)
4950 switch (token->type)
4953 return INDIRECT_REF;
4959 return UNARY_PLUS_EXPR;
4965 return TRUTH_NOT_EXPR;
4968 return BIT_NOT_EXPR;
4975 /* Parse a new-expression.
4978 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4979 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4981 Returns a representation of the expression. */
4984 cp_parser_new_expression (cp_parser* parser)
4986 bool global_scope_p;
4992 /* Look for the optional `::' operator. */
4994 = (cp_parser_global_scope_opt (parser,
4995 /*current_scope_valid_p=*/false)
4997 /* Look for the `new' operator. */
4998 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4999 /* There's no easy way to tell a new-placement from the
5000 `( type-id )' construct. */
5001 cp_parser_parse_tentatively (parser);
5002 /* Look for a new-placement. */
5003 placement = cp_parser_new_placement (parser);
5004 /* If that didn't work out, there's no new-placement. */
5005 if (!cp_parser_parse_definitely (parser))
5006 placement = NULL_TREE;
5008 /* If the next token is a `(', then we have a parenthesized
5010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5012 /* Consume the `('. */
5013 cp_lexer_consume_token (parser->lexer);
5014 /* Parse the type-id. */
5015 type = cp_parser_type_id (parser);
5016 /* Look for the closing `)'. */
5017 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5018 /* There should not be a direct-new-declarator in this production,
5019 but GCC used to allowed this, so we check and emit a sensible error
5020 message for this case. */
5021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5023 error ("array bound forbidden after parenthesized type-id");
5024 inform ("try removing the parentheses around the type-id");
5025 cp_parser_direct_new_declarator (parser);
5029 /* Otherwise, there must be a new-type-id. */
5031 type = cp_parser_new_type_id (parser, &nelts);
5033 /* If the next token is a `(', then we have a new-initializer. */
5034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5035 initializer = cp_parser_new_initializer (parser);
5037 initializer = NULL_TREE;
5039 /* A new-expression may not appear in an integral constant
5041 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5042 return error_mark_node;
5044 /* Create a representation of the new-expression. */
5045 return build_new (placement, type, nelts, initializer, global_scope_p);
5048 /* Parse a new-placement.
5053 Returns the same representation as for an expression-list. */
5056 cp_parser_new_placement (cp_parser* parser)
5058 tree expression_list;
5060 /* Parse the expression-list. */
5061 expression_list = (cp_parser_parenthesized_expression_list
5062 (parser, false, /*cast_p=*/false,
5063 /*non_constant_p=*/NULL));
5065 return expression_list;
5068 /* Parse a new-type-id.
5071 type-specifier-seq new-declarator [opt]
5073 Returns the TYPE allocated. If the new-type-id indicates an array
5074 type, *NELTS is set to the number of elements in the last array
5075 bound; the TYPE will not include the last array bound. */
5078 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5080 cp_decl_specifier_seq type_specifier_seq;
5081 cp_declarator *new_declarator;
5082 cp_declarator *declarator;
5083 cp_declarator *outer_declarator;
5084 const char *saved_message;
5087 /* The type-specifier sequence must not contain type definitions.
5088 (It cannot contain declarations of new types either, but if they
5089 are not definitions we will catch that because they are not
5091 saved_message = parser->type_definition_forbidden_message;
5092 parser->type_definition_forbidden_message
5093 = "types may not be defined in a new-type-id";
5094 /* Parse the type-specifier-seq. */
5095 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5096 &type_specifier_seq);
5097 /* Restore the old message. */
5098 parser->type_definition_forbidden_message = saved_message;
5099 /* Parse the new-declarator. */
5100 new_declarator = cp_parser_new_declarator_opt (parser);
5102 /* Determine the number of elements in the last array dimension, if
5105 /* Skip down to the last array dimension. */
5106 declarator = new_declarator;
5107 outer_declarator = NULL;
5108 while (declarator && (declarator->kind == cdk_pointer
5109 || declarator->kind == cdk_ptrmem))
5111 outer_declarator = declarator;
5112 declarator = declarator->declarator;
5115 && declarator->kind == cdk_array
5116 && declarator->declarator
5117 && declarator->declarator->kind == cdk_array)
5119 outer_declarator = declarator;
5120 declarator = declarator->declarator;
5123 if (declarator && declarator->kind == cdk_array)
5125 *nelts = declarator->u.array.bounds;
5126 if (*nelts == error_mark_node)
5127 *nelts = integer_one_node;
5129 if (outer_declarator)
5130 outer_declarator->declarator = declarator->declarator;
5132 new_declarator = NULL;
5135 type = groktypename (&type_specifier_seq, new_declarator);
5136 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5138 *nelts = array_type_nelts_top (type);
5139 type = TREE_TYPE (type);
5144 /* Parse an (optional) new-declarator.
5147 ptr-operator new-declarator [opt]
5148 direct-new-declarator
5150 Returns the declarator. */
5152 static cp_declarator *
5153 cp_parser_new_declarator_opt (cp_parser* parser)
5155 enum tree_code code;
5157 cp_cv_quals cv_quals;
5159 /* We don't know if there's a ptr-operator next, or not. */
5160 cp_parser_parse_tentatively (parser);
5161 /* Look for a ptr-operator. */
5162 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5163 /* If that worked, look for more new-declarators. */
5164 if (cp_parser_parse_definitely (parser))
5166 cp_declarator *declarator;
5168 /* Parse another optional declarator. */
5169 declarator = cp_parser_new_declarator_opt (parser);
5171 /* Create the representation of the declarator. */
5173 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5174 else if (code == INDIRECT_REF)
5175 declarator = make_pointer_declarator (cv_quals, declarator);
5177 declarator = make_reference_declarator (cv_quals, declarator);
5182 /* If the next token is a `[', there is a direct-new-declarator. */
5183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5184 return cp_parser_direct_new_declarator (parser);
5189 /* Parse a direct-new-declarator.
5191 direct-new-declarator:
5193 direct-new-declarator [constant-expression]
5197 static cp_declarator *
5198 cp_parser_direct_new_declarator (cp_parser* parser)
5200 cp_declarator *declarator = NULL;
5206 /* Look for the opening `['. */
5207 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5208 /* The first expression is not required to be constant. */
5211 expression = cp_parser_expression (parser, /*cast_p=*/false);
5212 /* The standard requires that the expression have integral
5213 type. DR 74 adds enumeration types. We believe that the
5214 real intent is that these expressions be handled like the
5215 expression in a `switch' condition, which also allows
5216 classes with a single conversion to integral or
5217 enumeration type. */
5218 if (!processing_template_decl)
5221 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5226 error ("expression in new-declarator must have integral "
5227 "or enumeration type");
5228 expression = error_mark_node;
5232 /* But all the other expressions must be. */
5235 = cp_parser_constant_expression (parser,
5236 /*allow_non_constant=*/false,
5238 /* Look for the closing `]'. */
5239 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5241 /* Add this bound to the declarator. */
5242 declarator = make_array_declarator (declarator, expression);
5244 /* If the next token is not a `[', then there are no more
5246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5253 /* Parse a new-initializer.
5256 ( expression-list [opt] )
5258 Returns a representation of the expression-list. If there is no
5259 expression-list, VOID_ZERO_NODE is returned. */
5262 cp_parser_new_initializer (cp_parser* parser)
5264 tree expression_list;
5266 expression_list = (cp_parser_parenthesized_expression_list
5267 (parser, false, /*cast_p=*/false,
5268 /*non_constant_p=*/NULL));
5269 if (!expression_list)
5270 expression_list = void_zero_node;
5272 return expression_list;
5275 /* Parse a delete-expression.
5278 :: [opt] delete cast-expression
5279 :: [opt] delete [ ] cast-expression
5281 Returns a representation of the expression. */
5284 cp_parser_delete_expression (cp_parser* parser)
5286 bool global_scope_p;
5290 /* Look for the optional `::' operator. */
5292 = (cp_parser_global_scope_opt (parser,
5293 /*current_scope_valid_p=*/false)
5295 /* Look for the `delete' keyword. */
5296 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5297 /* See if the array syntax is in use. */
5298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5300 /* Consume the `[' token. */
5301 cp_lexer_consume_token (parser->lexer);
5302 /* Look for the `]' token. */
5303 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5304 /* Remember that this is the `[]' construct. */
5310 /* Parse the cast-expression. */
5311 expression = cp_parser_simple_cast_expression (parser);
5313 /* A delete-expression may not appear in an integral constant
5315 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5316 return error_mark_node;
5318 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5321 /* Parse a cast-expression.
5325 ( type-id ) cast-expression
5327 ADDRESS_P is true iff the unary-expression is appearing as the
5328 operand of the `&' operator. CAST_P is true if this expression is
5329 the target of a cast.
5331 Returns a representation of the expression. */
5334 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5336 /* If it's a `(', then we might be looking at a cast. */
5337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5339 tree type = NULL_TREE;
5340 tree expr = NULL_TREE;
5341 bool compound_literal_p;
5342 const char *saved_message;
5344 /* There's no way to know yet whether or not this is a cast.
5345 For example, `(int (3))' is a unary-expression, while `(int)
5346 3' is a cast. So, we resort to parsing tentatively. */
5347 cp_parser_parse_tentatively (parser);
5348 /* Types may not be defined in a cast. */
5349 saved_message = parser->type_definition_forbidden_message;
5350 parser->type_definition_forbidden_message
5351 = "types may not be defined in casts";
5352 /* Consume the `('. */
5353 cp_lexer_consume_token (parser->lexer);
5354 /* A very tricky bit is that `(struct S) { 3 }' is a
5355 compound-literal (which we permit in C++ as an extension).
5356 But, that construct is not a cast-expression -- it is a
5357 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5358 is legal; if the compound-literal were a cast-expression,
5359 you'd need an extra set of parentheses.) But, if we parse
5360 the type-id, and it happens to be a class-specifier, then we
5361 will commit to the parse at that point, because we cannot
5362 undo the action that is done when creating a new class. So,
5363 then we cannot back up and do a postfix-expression.
5365 Therefore, we scan ahead to the closing `)', and check to see
5366 if the token after the `)' is a `{'. If so, we are not
5367 looking at a cast-expression.
5369 Save tokens so that we can put them back. */
5370 cp_lexer_save_tokens (parser->lexer);
5371 /* Skip tokens until the next token is a closing parenthesis.
5372 If we find the closing `)', and the next token is a `{', then
5373 we are looking at a compound-literal. */
5375 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5376 /*consume_paren=*/true)
5377 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5378 /* Roll back the tokens we skipped. */
5379 cp_lexer_rollback_tokens (parser->lexer);
5380 /* If we were looking at a compound-literal, simulate an error
5381 so that the call to cp_parser_parse_definitely below will
5383 if (compound_literal_p)
5384 cp_parser_simulate_error (parser);
5387 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5388 parser->in_type_id_in_expr_p = true;
5389 /* Look for the type-id. */
5390 type = cp_parser_type_id (parser);
5391 /* Look for the closing `)'. */
5392 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5393 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5396 /* Restore the saved message. */
5397 parser->type_definition_forbidden_message = saved_message;
5399 /* If ok so far, parse the dependent expression. We cannot be
5400 sure it is a cast. Consider `(T ())'. It is a parenthesized
5401 ctor of T, but looks like a cast to function returning T
5402 without a dependent expression. */
5403 if (!cp_parser_error_occurred (parser))
5404 expr = cp_parser_cast_expression (parser,
5405 /*address_p=*/false,
5408 if (cp_parser_parse_definitely (parser))
5410 /* Warn about old-style casts, if so requested. */
5411 if (warn_old_style_cast
5412 && !in_system_header
5413 && !VOID_TYPE_P (type)
5414 && current_lang_name != lang_name_c)
5415 warning (0, "use of old-style cast");
5417 /* Only type conversions to integral or enumeration types
5418 can be used in constant-expressions. */
5419 if (parser->integral_constant_expression_p
5420 && !dependent_type_p (type)
5421 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5422 && (cp_parser_non_integral_constant_expression
5424 "a cast to a type other than an integral or "
5425 "enumeration type")))
5426 return error_mark_node;
5428 /* Perform the cast. */
5429 expr = build_c_cast (type, expr);
5434 /* If we get here, then it's not a cast, so it must be a
5435 unary-expression. */
5436 return cp_parser_unary_expression (parser, address_p, cast_p);
5439 /* Parse a binary expression of the general form:
5443 pm-expression .* cast-expression
5444 pm-expression ->* cast-expression
5446 multiplicative-expression:
5448 multiplicative-expression * pm-expression
5449 multiplicative-expression / pm-expression
5450 multiplicative-expression % pm-expression
5452 additive-expression:
5453 multiplicative-expression
5454 additive-expression + multiplicative-expression
5455 additive-expression - multiplicative-expression
5459 shift-expression << additive-expression
5460 shift-expression >> additive-expression
5462 relational-expression:
5464 relational-expression < shift-expression
5465 relational-expression > shift-expression
5466 relational-expression <= shift-expression
5467 relational-expression >= shift-expression
5471 relational-expression:
5472 relational-expression <? shift-expression
5473 relational-expression >? shift-expression
5475 equality-expression:
5476 relational-expression
5477 equality-expression == relational-expression
5478 equality-expression != relational-expression
5482 and-expression & equality-expression
5484 exclusive-or-expression:
5486 exclusive-or-expression ^ and-expression
5488 inclusive-or-expression:
5489 exclusive-or-expression
5490 inclusive-or-expression | exclusive-or-expression
5492 logical-and-expression:
5493 inclusive-or-expression
5494 logical-and-expression && inclusive-or-expression
5496 logical-or-expression:
5497 logical-and-expression
5498 logical-or-expression || logical-and-expression
5500 All these are implemented with a single function like:
5503 simple-cast-expression
5504 binary-expression <token> binary-expression
5506 CAST_P is true if this expression is the target of a cast.
5508 The binops_by_token map is used to get the tree codes for each <token> type.
5509 binary-expressions are associated according to a precedence table. */
5511 #define TOKEN_PRECEDENCE(token) \
5512 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5513 ? PREC_NOT_OPERATOR \
5514 : binops_by_token[token->type].prec)
5517 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5519 cp_parser_expression_stack stack;
5520 cp_parser_expression_stack_entry *sp = &stack[0];
5523 enum tree_code tree_type;
5524 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5527 /* Parse the first expression. */
5528 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5532 /* Get an operator token. */
5533 token = cp_lexer_peek_token (parser->lexer);
5534 if (token->type == CPP_MIN || token->type == CPP_MAX)
5535 cp_parser_warn_min_max ();
5537 new_prec = TOKEN_PRECEDENCE (token);
5539 /* Popping an entry off the stack means we completed a subexpression:
5540 - either we found a token which is not an operator (`>' where it is not
5541 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5542 will happen repeatedly;
5543 - or, we found an operator which has lower priority. This is the case
5544 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5546 if (new_prec <= prec)
5555 tree_type = binops_by_token[token->type].tree_type;
5557 /* We used the operator token. */
5558 cp_lexer_consume_token (parser->lexer);
5560 /* Extract another operand. It may be the RHS of this expression
5561 or the LHS of a new, higher priority expression. */
5562 rhs = cp_parser_simple_cast_expression (parser);
5564 /* Get another operator token. Look up its precedence to avoid
5565 building a useless (immediately popped) stack entry for common
5566 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5567 token = cp_lexer_peek_token (parser->lexer);
5568 lookahead_prec = TOKEN_PRECEDENCE (token);
5569 if (lookahead_prec > new_prec)
5571 /* ... and prepare to parse the RHS of the new, higher priority
5572 expression. Since precedence levels on the stack are
5573 monotonically increasing, we do not have to care about
5576 sp->tree_type = tree_type;
5581 new_prec = lookahead_prec;
5585 /* If the stack is not empty, we have parsed into LHS the right side
5586 (`4' in the example above) of an expression we had suspended.
5587 We can use the information on the stack to recover the LHS (`3')
5588 from the stack together with the tree code (`MULT_EXPR'), and
5589 the precedence of the higher level subexpression
5590 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5591 which will be used to actually build the additive expression. */
5594 tree_type = sp->tree_type;
5599 overloaded_p = false;
5600 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5602 /* If the binary operator required the use of an overloaded operator,
5603 then this expression cannot be an integral constant-expression.
5604 An overloaded operator can be used even if both operands are
5605 otherwise permissible in an integral constant-expression if at
5606 least one of the operands is of enumeration type. */
5609 && (cp_parser_non_integral_constant_expression
5610 (parser, "calls to overloaded operators")))
5611 return error_mark_node;
5618 /* Parse the `? expression : assignment-expression' part of a
5619 conditional-expression. The LOGICAL_OR_EXPR is the
5620 logical-or-expression that started the conditional-expression.
5621 Returns a representation of the entire conditional-expression.
5623 This routine is used by cp_parser_assignment_expression.
5625 ? expression : assignment-expression
5629 ? : assignment-expression */
5632 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5635 tree assignment_expr;
5637 /* Consume the `?' token. */
5638 cp_lexer_consume_token (parser->lexer);
5639 if (cp_parser_allow_gnu_extensions_p (parser)
5640 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5641 /* Implicit true clause. */
5644 /* Parse the expression. */
5645 expr = cp_parser_expression (parser, /*cast_p=*/false);
5647 /* The next token should be a `:'. */
5648 cp_parser_require (parser, CPP_COLON, "`:'");
5649 /* Parse the assignment-expression. */
5650 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5652 /* Build the conditional-expression. */
5653 return build_x_conditional_expr (logical_or_expr,
5658 /* Parse an assignment-expression.
5660 assignment-expression:
5661 conditional-expression
5662 logical-or-expression assignment-operator assignment_expression
5665 CAST_P is true if this expression is the target of a cast.
5667 Returns a representation for the expression. */
5670 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5674 /* If the next token is the `throw' keyword, then we're looking at
5675 a throw-expression. */
5676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5677 expr = cp_parser_throw_expression (parser);
5678 /* Otherwise, it must be that we are looking at a
5679 logical-or-expression. */
5682 /* Parse the binary expressions (logical-or-expression). */
5683 expr = cp_parser_binary_expression (parser, cast_p);
5684 /* If the next token is a `?' then we're actually looking at a
5685 conditional-expression. */
5686 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5687 return cp_parser_question_colon_clause (parser, expr);
5690 enum tree_code assignment_operator;
5692 /* If it's an assignment-operator, we're using the second
5695 = cp_parser_assignment_operator_opt (parser);
5696 if (assignment_operator != ERROR_MARK)
5700 /* Parse the right-hand side of the assignment. */
5701 rhs = cp_parser_assignment_expression (parser, cast_p);
5702 /* An assignment may not appear in a
5703 constant-expression. */
5704 if (cp_parser_non_integral_constant_expression (parser,
5706 return error_mark_node;
5707 /* Build the assignment expression. */
5708 expr = build_x_modify_expr (expr,
5709 assignment_operator,
5718 /* Parse an (optional) assignment-operator.
5720 assignment-operator: one of
5721 = *= /= %= += -= >>= <<= &= ^= |=
5725 assignment-operator: one of
5728 If the next token is an assignment operator, the corresponding tree
5729 code is returned, and the token is consumed. For example, for
5730 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5731 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5732 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5733 operator, ERROR_MARK is returned. */
5735 static enum tree_code
5736 cp_parser_assignment_operator_opt (cp_parser* parser)
5741 /* Peek at the next toen. */
5742 token = cp_lexer_peek_token (parser->lexer);
5744 switch (token->type)
5755 op = TRUNC_DIV_EXPR;
5759 op = TRUNC_MOD_EXPR;
5792 cp_parser_warn_min_max ();
5797 cp_parser_warn_min_max ();
5801 /* Nothing else is an assignment operator. */
5805 /* If it was an assignment operator, consume it. */
5806 if (op != ERROR_MARK)
5807 cp_lexer_consume_token (parser->lexer);
5812 /* Parse an expression.
5815 assignment-expression
5816 expression , assignment-expression
5818 CAST_P is true if this expression is the target of a cast.
5820 Returns a representation of the expression. */
5823 cp_parser_expression (cp_parser* parser, bool cast_p)
5825 tree expression = NULL_TREE;
5829 tree assignment_expression;
5831 /* Parse the next assignment-expression. */
5832 assignment_expression
5833 = cp_parser_assignment_expression (parser, cast_p);
5834 /* If this is the first assignment-expression, we can just
5837 expression = assignment_expression;
5839 expression = build_x_compound_expr (expression,
5840 assignment_expression);
5841 /* If the next token is not a comma, then we are done with the
5843 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5845 /* Consume the `,'. */
5846 cp_lexer_consume_token (parser->lexer);
5847 /* A comma operator cannot appear in a constant-expression. */
5848 if (cp_parser_non_integral_constant_expression (parser,
5849 "a comma operator"))
5850 expression = error_mark_node;
5856 /* Parse a constant-expression.
5858 constant-expression:
5859 conditional-expression
5861 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5862 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5863 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5864 is false, NON_CONSTANT_P should be NULL. */
5867 cp_parser_constant_expression (cp_parser* parser,
5868 bool allow_non_constant_p,
5869 bool *non_constant_p)
5871 bool saved_integral_constant_expression_p;
5872 bool saved_allow_non_integral_constant_expression_p;
5873 bool saved_non_integral_constant_expression_p;
5876 /* It might seem that we could simply parse the
5877 conditional-expression, and then check to see if it were
5878 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5879 one that the compiler can figure out is constant, possibly after
5880 doing some simplifications or optimizations. The standard has a
5881 precise definition of constant-expression, and we must honor
5882 that, even though it is somewhat more restrictive.
5888 is not a legal declaration, because `(2, 3)' is not a
5889 constant-expression. The `,' operator is forbidden in a
5890 constant-expression. However, GCC's constant-folding machinery
5891 will fold this operation to an INTEGER_CST for `3'. */
5893 /* Save the old settings. */
5894 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5895 saved_allow_non_integral_constant_expression_p
5896 = parser->allow_non_integral_constant_expression_p;
5897 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5898 /* We are now parsing a constant-expression. */
5899 parser->integral_constant_expression_p = true;
5900 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5901 parser->non_integral_constant_expression_p = false;
5902 /* Although the grammar says "conditional-expression", we parse an
5903 "assignment-expression", which also permits "throw-expression"
5904 and the use of assignment operators. In the case that
5905 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5906 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5907 actually essential that we look for an assignment-expression.
5908 For example, cp_parser_initializer_clauses uses this function to
5909 determine whether a particular assignment-expression is in fact
5911 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5912 /* Restore the old settings. */
5913 parser->integral_constant_expression_p
5914 = saved_integral_constant_expression_p;
5915 parser->allow_non_integral_constant_expression_p
5916 = saved_allow_non_integral_constant_expression_p;
5917 if (allow_non_constant_p)
5918 *non_constant_p = parser->non_integral_constant_expression_p;
5919 else if (parser->non_integral_constant_expression_p)
5920 expression = error_mark_node;
5921 parser->non_integral_constant_expression_p
5922 = saved_non_integral_constant_expression_p;
5927 /* Parse __builtin_offsetof.
5929 offsetof-expression:
5930 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5932 offsetof-member-designator:
5934 | offsetof-member-designator "." id-expression
5935 | offsetof-member-designator "[" expression "]"
5939 cp_parser_builtin_offsetof (cp_parser *parser)
5941 int save_ice_p, save_non_ice_p;
5945 /* We're about to accept non-integral-constant things, but will
5946 definitely yield an integral constant expression. Save and
5947 restore these values around our local parsing. */
5948 save_ice_p = parser->integral_constant_expression_p;
5949 save_non_ice_p = parser->non_integral_constant_expression_p;
5951 /* Consume the "__builtin_offsetof" token. */
5952 cp_lexer_consume_token (parser->lexer);
5953 /* Consume the opening `('. */
5954 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5955 /* Parse the type-id. */
5956 type = cp_parser_type_id (parser);
5957 /* Look for the `,'. */
5958 cp_parser_require (parser, CPP_COMMA, "`,'");
5960 /* Build the (type *)null that begins the traditional offsetof macro. */
5961 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5963 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5964 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5968 cp_token *token = cp_lexer_peek_token (parser->lexer);
5969 switch (token->type)
5971 case CPP_OPEN_SQUARE:
5972 /* offsetof-member-designator "[" expression "]" */
5973 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5977 /* offsetof-member-designator "." identifier */
5978 cp_lexer_consume_token (parser->lexer);
5979 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5983 case CPP_CLOSE_PAREN:
5984 /* Consume the ")" token. */
5985 cp_lexer_consume_token (parser->lexer);
5989 /* Error. We know the following require will fail, but
5990 that gives the proper error message. */
5991 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5992 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5993 expr = error_mark_node;
5999 /* If we're processing a template, we can't finish the semantics yet.
6000 Otherwise we can fold the entire expression now. */
6001 if (processing_template_decl)
6002 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6004 expr = fold_offsetof (expr);
6007 parser->integral_constant_expression_p = save_ice_p;
6008 parser->non_integral_constant_expression_p = save_non_ice_p;
6013 /* Statements [gram.stmt.stmt] */
6015 /* Parse a statement.
6019 expression-statement
6024 declaration-statement
6027 IN_COMPOUND is true when the statement is nested inside a
6028 cp_parser_compound_statement; this matters for certain pragmas. */
6031 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6036 location_t statement_location;
6039 /* There is no statement yet. */
6040 statement = NULL_TREE;
6041 /* Peek at the next token. */
6042 token = cp_lexer_peek_token (parser->lexer);
6043 /* Remember the location of the first token in the statement. */
6044 statement_location = token->location;
6045 /* If this is a keyword, then that will often determine what kind of
6046 statement we have. */
6047 if (token->type == CPP_KEYWORD)
6049 enum rid keyword = token->keyword;
6055 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6061 statement = cp_parser_selection_statement (parser);
6067 statement = cp_parser_iteration_statement (parser);
6074 statement = cp_parser_jump_statement (parser);
6077 /* Objective-C++ exception-handling constructs. */
6080 case RID_AT_FINALLY:
6081 case RID_AT_SYNCHRONIZED:
6083 statement = cp_parser_objc_statement (parser);
6087 statement = cp_parser_try_block (parser);
6091 /* It might be a keyword like `int' that can start a
6092 declaration-statement. */
6096 else if (token->type == CPP_NAME)
6098 /* If the next token is a `:', then we are looking at a
6099 labeled-statement. */
6100 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6101 if (token->type == CPP_COLON)
6102 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6105 /* Anything that starts with a `{' must be a compound-statement. */
6106 else if (token->type == CPP_OPEN_BRACE)
6107 statement = cp_parser_compound_statement (parser, NULL, false);
6108 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6109 a statement all its own. */
6110 else if (token->type == CPP_PRAGMA)
6112 /* Only certain OpenMP pragmas are attached to statements, and thus
6113 are considered statements themselves. All others are not. In
6114 the context of a compound, accept the pragma as a "statement" and
6115 return so that we can check for a close brace. Otherwise we
6116 require a real statement and must go back and read one. */
6118 cp_parser_pragma (parser, pragma_compound);
6119 else if (!cp_parser_pragma (parser, pragma_stmt))
6123 else if (token->type == CPP_EOF)
6125 cp_parser_error (parser, "expected statement");
6129 /* Everything else must be a declaration-statement or an
6130 expression-statement. Try for the declaration-statement
6131 first, unless we are looking at a `;', in which case we know that
6132 we have an expression-statement. */
6135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6137 cp_parser_parse_tentatively (parser);
6138 /* Try to parse the declaration-statement. */
6139 cp_parser_declaration_statement (parser);
6140 /* If that worked, we're done. */
6141 if (cp_parser_parse_definitely (parser))
6144 /* Look for an expression-statement instead. */
6145 statement = cp_parser_expression_statement (parser, in_statement_expr);
6148 /* Set the line number for the statement. */
6149 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6150 SET_EXPR_LOCATION (statement, statement_location);
6153 /* Parse a labeled-statement.
6156 identifier : statement
6157 case constant-expression : statement
6163 case constant-expression ... constant-expression : statement
6165 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6166 For an ordinary label, returns a LABEL_EXPR.
6168 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6169 inside a compound. */
6172 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6176 tree statement = error_mark_node;
6178 /* The next token should be an identifier. */
6179 token = cp_lexer_peek_token (parser->lexer);
6180 if (token->type != CPP_NAME
6181 && token->type != CPP_KEYWORD)
6183 cp_parser_error (parser, "expected labeled-statement");
6184 return error_mark_node;
6187 switch (token->keyword)
6194 /* Consume the `case' token. */
6195 cp_lexer_consume_token (parser->lexer);
6196 /* Parse the constant-expression. */
6197 expr = cp_parser_constant_expression (parser,
6198 /*allow_non_constant_p=*/false,
6201 ellipsis = cp_lexer_peek_token (parser->lexer);
6202 if (ellipsis->type == CPP_ELLIPSIS)
6204 /* Consume the `...' token. */
6205 cp_lexer_consume_token (parser->lexer);
6207 cp_parser_constant_expression (parser,
6208 /*allow_non_constant_p=*/false,
6210 /* We don't need to emit warnings here, as the common code
6211 will do this for us. */
6214 expr_hi = NULL_TREE;
6216 if (parser->in_switch_statement_p)
6217 statement = finish_case_label (expr, expr_hi);
6219 error ("case label %qE not within a switch statement", expr);
6224 /* Consume the `default' token. */
6225 cp_lexer_consume_token (parser->lexer);
6227 if (parser->in_switch_statement_p)
6228 statement = finish_case_label (NULL_TREE, NULL_TREE);
6230 error ("case label not within a switch statement");
6234 /* Anything else must be an ordinary label. */
6235 statement = finish_label_stmt (cp_parser_identifier (parser));
6239 /* Require the `:' token. */
6240 cp_parser_require (parser, CPP_COLON, "`:'");
6241 /* Parse the labeled statement. */
6242 cp_parser_statement (parser, in_statement_expr, in_compound);
6244 /* Return the label, in the case of a `case' or `default' label. */
6248 /* Parse an expression-statement.
6250 expression-statement:
6253 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6254 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6255 indicates whether this expression-statement is part of an
6256 expression statement. */
6259 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6261 tree statement = NULL_TREE;
6263 /* If the next token is a ';', then there is no expression
6265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6266 statement = cp_parser_expression (parser, /*cast_p=*/false);
6268 /* Consume the final `;'. */
6269 cp_parser_consume_semicolon_at_end_of_statement (parser);
6271 if (in_statement_expr
6272 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6273 /* This is the final expression statement of a statement
6275 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6277 statement = finish_expr_stmt (statement);
6284 /* Parse a compound-statement.
6287 { statement-seq [opt] }
6289 Returns a tree representing the statement. */
6292 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6297 /* Consume the `{'. */
6298 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6299 return error_mark_node;
6300 /* Begin the compound-statement. */
6301 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6302 /* Parse an (optional) statement-seq. */
6303 cp_parser_statement_seq_opt (parser, in_statement_expr);
6304 /* Finish the compound-statement. */
6305 finish_compound_stmt (compound_stmt);
6306 /* Consume the `}'. */
6307 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6309 return compound_stmt;
6312 /* Parse an (optional) statement-seq.
6316 statement-seq [opt] statement */
6319 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6321 /* Scan statements until there aren't any more. */
6324 cp_token *token = cp_lexer_peek_token (parser->lexer);
6326 /* If we're looking at a `}', then we've run out of statements. */
6327 if (token->type == CPP_CLOSE_BRACE
6328 || token->type == CPP_EOF
6329 || token->type == CPP_PRAGMA_EOL)
6332 /* Parse the statement. */
6333 cp_parser_statement (parser, in_statement_expr, true);
6337 /* Parse a selection-statement.
6339 selection-statement:
6340 if ( condition ) statement
6341 if ( condition ) statement else statement
6342 switch ( condition ) statement
6344 Returns the new IF_STMT or SWITCH_STMT. */
6347 cp_parser_selection_statement (cp_parser* parser)
6352 /* Peek at the next token. */
6353 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6355 /* See what kind of keyword it is. */
6356 keyword = token->keyword;
6365 /* Look for the `('. */
6366 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6368 cp_parser_skip_to_end_of_statement (parser);
6369 return error_mark_node;
6372 /* Begin the selection-statement. */
6373 if (keyword == RID_IF)
6374 statement = begin_if_stmt ();
6376 statement = begin_switch_stmt ();
6378 /* Parse the condition. */
6379 condition = cp_parser_condition (parser);
6380 /* Look for the `)'. */
6381 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6382 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6383 /*consume_paren=*/true);
6385 if (keyword == RID_IF)
6387 /* Add the condition. */
6388 finish_if_stmt_cond (condition, statement);
6390 /* Parse the then-clause. */
6391 cp_parser_implicitly_scoped_statement (parser);
6392 finish_then_clause (statement);
6394 /* If the next token is `else', parse the else-clause. */
6395 if (cp_lexer_next_token_is_keyword (parser->lexer,
6398 /* Consume the `else' keyword. */
6399 cp_lexer_consume_token (parser->lexer);
6400 begin_else_clause (statement);
6401 /* Parse the else-clause. */
6402 cp_parser_implicitly_scoped_statement (parser);
6403 finish_else_clause (statement);
6406 /* Now we're all done with the if-statement. */
6407 finish_if_stmt (statement);
6411 bool in_switch_statement_p;
6413 /* Add the condition. */
6414 finish_switch_cond (condition, statement);
6416 /* Parse the body of the switch-statement. */
6417 in_switch_statement_p = parser->in_switch_statement_p;
6418 parser->in_switch_statement_p = true;
6419 cp_parser_implicitly_scoped_statement (parser);
6420 parser->in_switch_statement_p = in_switch_statement_p;
6422 /* Now we're all done with the switch-statement. */
6423 finish_switch_stmt (statement);
6431 cp_parser_error (parser, "expected selection-statement");
6432 return error_mark_node;
6436 /* Parse a condition.
6440 type-specifier-seq declarator = assignment-expression
6445 type-specifier-seq declarator asm-specification [opt]
6446 attributes [opt] = assignment-expression
6448 Returns the expression that should be tested. */
6451 cp_parser_condition (cp_parser* parser)
6453 cp_decl_specifier_seq type_specifiers;
6454 const char *saved_message;
6456 /* Try the declaration first. */
6457 cp_parser_parse_tentatively (parser);
6458 /* New types are not allowed in the type-specifier-seq for a
6460 saved_message = parser->type_definition_forbidden_message;
6461 parser->type_definition_forbidden_message
6462 = "types may not be defined in conditions";
6463 /* Parse the type-specifier-seq. */
6464 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6466 /* Restore the saved message. */
6467 parser->type_definition_forbidden_message = saved_message;
6468 /* If all is well, we might be looking at a declaration. */
6469 if (!cp_parser_error_occurred (parser))
6472 tree asm_specification;
6474 cp_declarator *declarator;
6475 tree initializer = NULL_TREE;
6477 /* Parse the declarator. */
6478 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6479 /*ctor_dtor_or_conv_p=*/NULL,
6480 /*parenthesized_p=*/NULL,
6481 /*member_p=*/false);
6482 /* Parse the attributes. */
6483 attributes = cp_parser_attributes_opt (parser);
6484 /* Parse the asm-specification. */
6485 asm_specification = cp_parser_asm_specification_opt (parser);
6486 /* If the next token is not an `=', then we might still be
6487 looking at an expression. For example:
6491 looks like a decl-specifier-seq and a declarator -- but then
6492 there is no `=', so this is an expression. */
6493 cp_parser_require (parser, CPP_EQ, "`='");
6494 /* If we did see an `=', then we are looking at a declaration
6496 if (cp_parser_parse_definitely (parser))
6500 /* Create the declaration. */
6501 decl = start_decl (declarator, &type_specifiers,
6502 /*initialized_p=*/true,
6503 attributes, /*prefix_attributes=*/NULL_TREE,
6505 /* Parse the assignment-expression. */
6506 initializer = cp_parser_assignment_expression (parser,
6509 /* Process the initializer. */
6510 cp_finish_decl (decl,
6513 LOOKUP_ONLYCONVERTING);
6516 pop_scope (pushed_scope);
6518 return convert_from_reference (decl);
6521 /* If we didn't even get past the declarator successfully, we are
6522 definitely not looking at a declaration. */
6524 cp_parser_abort_tentative_parse (parser);
6526 /* Otherwise, we are looking at an expression. */
6527 return cp_parser_expression (parser, /*cast_p=*/false);
6530 /* Parse an iteration-statement.
6532 iteration-statement:
6533 while ( condition ) statement
6534 do statement while ( expression ) ;
6535 for ( for-init-statement condition [opt] ; expression [opt] )
6538 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6541 cp_parser_iteration_statement (cp_parser* parser)
6546 bool in_iteration_statement_p;
6549 /* Peek at the next token. */
6550 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6552 return error_mark_node;
6554 /* Remember whether or not we are already within an iteration
6556 in_iteration_statement_p = parser->in_iteration_statement_p;
6558 /* See what kind of keyword it is. */
6559 keyword = token->keyword;
6566 /* Begin the while-statement. */
6567 statement = begin_while_stmt ();
6568 /* Look for the `('. */
6569 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6570 /* Parse the condition. */
6571 condition = cp_parser_condition (parser);
6572 finish_while_stmt_cond (condition, statement);
6573 /* Look for the `)'. */
6574 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6575 /* Parse the dependent statement. */
6576 parser->in_iteration_statement_p = true;
6577 cp_parser_already_scoped_statement (parser);
6578 parser->in_iteration_statement_p = in_iteration_statement_p;
6579 /* We're done with the while-statement. */
6580 finish_while_stmt (statement);
6588 /* Begin the do-statement. */
6589 statement = begin_do_stmt ();
6590 /* Parse the body of the do-statement. */
6591 parser->in_iteration_statement_p = true;
6592 cp_parser_implicitly_scoped_statement (parser);
6593 parser->in_iteration_statement_p = in_iteration_statement_p;
6594 finish_do_body (statement);
6595 /* Look for the `while' keyword. */
6596 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6597 /* Look for the `('. */
6598 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6599 /* Parse the expression. */
6600 expression = cp_parser_expression (parser, /*cast_p=*/false);
6601 /* We're done with the do-statement. */
6602 finish_do_stmt (expression, statement);
6603 /* Look for the `)'. */
6604 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6605 /* Look for the `;'. */
6606 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6612 tree condition = NULL_TREE;
6613 tree expression = NULL_TREE;
6615 /* Begin the for-statement. */
6616 statement = begin_for_stmt ();
6617 /* Look for the `('. */
6618 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6619 /* Parse the initialization. */
6620 cp_parser_for_init_statement (parser);
6621 finish_for_init_stmt (statement);
6623 /* If there's a condition, process it. */
6624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6625 condition = cp_parser_condition (parser);
6626 finish_for_cond (condition, statement);
6627 /* Look for the `;'. */
6628 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6630 /* If there's an expression, process it. */
6631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6632 expression = cp_parser_expression (parser, /*cast_p=*/false);
6633 finish_for_expr (expression, statement);
6634 /* Look for the `)'. */
6635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6637 /* Parse the body of the for-statement. */
6638 parser->in_iteration_statement_p = true;
6639 cp_parser_already_scoped_statement (parser);
6640 parser->in_iteration_statement_p = in_iteration_statement_p;
6642 /* We're done with the for-statement. */
6643 finish_for_stmt (statement);
6648 cp_parser_error (parser, "expected iteration-statement");
6649 statement = error_mark_node;
6656 /* Parse a for-init-statement.
6659 expression-statement
6660 simple-declaration */
6663 cp_parser_for_init_statement (cp_parser* parser)
6665 /* If the next token is a `;', then we have an empty
6666 expression-statement. Grammatically, this is also a
6667 simple-declaration, but an invalid one, because it does not
6668 declare anything. Therefore, if we did not handle this case
6669 specially, we would issue an error message about an invalid
6671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6673 /* We're going to speculatively look for a declaration, falling back
6674 to an expression, if necessary. */
6675 cp_parser_parse_tentatively (parser);
6676 /* Parse the declaration. */
6677 cp_parser_simple_declaration (parser,
6678 /*function_definition_allowed_p=*/false);
6679 /* If the tentative parse failed, then we shall need to look for an
6680 expression-statement. */
6681 if (cp_parser_parse_definitely (parser))
6685 cp_parser_expression_statement (parser, false);
6688 /* Parse a jump-statement.
6693 return expression [opt] ;
6701 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6704 cp_parser_jump_statement (cp_parser* parser)
6706 tree statement = error_mark_node;
6710 /* Peek at the next token. */
6711 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6713 return error_mark_node;
6715 /* See what kind of keyword it is. */
6716 keyword = token->keyword;
6720 if (!parser->in_switch_statement_p
6721 && !parser->in_iteration_statement_p)
6723 error ("break statement not within loop or switch");
6724 statement = error_mark_node;
6727 statement = finish_break_stmt ();
6728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6732 if (!parser->in_iteration_statement_p)
6734 error ("continue statement not within a loop");
6735 statement = error_mark_node;
6738 statement = finish_continue_stmt ();
6739 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6746 /* If the next token is a `;', then there is no
6748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6749 expr = cp_parser_expression (parser, /*cast_p=*/false);
6752 /* Build the return-statement. */
6753 statement = finish_return_stmt (expr);
6754 /* Look for the final `;'. */
6755 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6760 /* Create the goto-statement. */
6761 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6763 /* Issue a warning about this use of a GNU extension. */
6765 pedwarn ("ISO C++ forbids computed gotos");
6766 /* Consume the '*' token. */
6767 cp_lexer_consume_token (parser->lexer);
6768 /* Parse the dependent expression. */
6769 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6772 finish_goto_stmt (cp_parser_identifier (parser));
6773 /* Look for the final `;'. */
6774 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6778 cp_parser_error (parser, "expected jump-statement");
6785 /* Parse a declaration-statement.
6787 declaration-statement:
6788 block-declaration */
6791 cp_parser_declaration_statement (cp_parser* parser)
6795 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6796 p = obstack_alloc (&declarator_obstack, 0);
6798 /* Parse the block-declaration. */
6799 cp_parser_block_declaration (parser, /*statement_p=*/true);
6801 /* Free any declarators allocated. */
6802 obstack_free (&declarator_obstack, p);
6804 /* Finish off the statement. */
6808 /* Some dependent statements (like `if (cond) statement'), are
6809 implicitly in their own scope. In other words, if the statement is
6810 a single statement (as opposed to a compound-statement), it is
6811 none-the-less treated as if it were enclosed in braces. Any
6812 declarations appearing in the dependent statement are out of scope
6813 after control passes that point. This function parses a statement,
6814 but ensures that is in its own scope, even if it is not a
6817 Returns the new statement. */
6820 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6824 /* If the token is not a `{', then we must take special action. */
6825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6827 /* Create a compound-statement. */
6828 statement = begin_compound_stmt (0);
6829 /* Parse the dependent-statement. */
6830 cp_parser_statement (parser, NULL_TREE, false);
6831 /* Finish the dummy compound-statement. */
6832 finish_compound_stmt (statement);
6834 /* Otherwise, we simply parse the statement directly. */
6836 statement = cp_parser_compound_statement (parser, NULL, false);
6838 /* Return the statement. */
6842 /* For some dependent statements (like `while (cond) statement'), we
6843 have already created a scope. Therefore, even if the dependent
6844 statement is a compound-statement, we do not want to create another
6848 cp_parser_already_scoped_statement (cp_parser* parser)
6850 /* If the token is a `{', then we must take special action. */
6851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6852 cp_parser_statement (parser, NULL_TREE, false);
6855 /* Avoid calling cp_parser_compound_statement, so that we
6856 don't create a new scope. Do everything else by hand. */
6857 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6858 cp_parser_statement_seq_opt (parser, NULL_TREE);
6859 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6863 /* Declarations [gram.dcl.dcl] */
6865 /* Parse an optional declaration-sequence.
6869 declaration-seq declaration */
6872 cp_parser_declaration_seq_opt (cp_parser* parser)
6878 token = cp_lexer_peek_token (parser->lexer);
6880 if (token->type == CPP_CLOSE_BRACE
6881 || token->type == CPP_EOF
6882 || token->type == CPP_PRAGMA_EOL)
6885 if (token->type == CPP_SEMICOLON)
6887 /* A declaration consisting of a single semicolon is
6888 invalid. Allow it unless we're being pedantic. */
6889 cp_lexer_consume_token (parser->lexer);
6890 if (pedantic && !in_system_header)
6891 pedwarn ("extra %<;%>");
6895 /* If we're entering or exiting a region that's implicitly
6896 extern "C", modify the lang context appropriately. */
6897 if (!parser->implicit_extern_c && token->implicit_extern_c)
6899 push_lang_context (lang_name_c);
6900 parser->implicit_extern_c = true;
6902 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6904 pop_lang_context ();
6905 parser->implicit_extern_c = false;
6908 if (token->type == CPP_PRAGMA)
6910 /* A top-level declaration can consist solely of a #pragma.
6911 A nested declaration cannot, so this is done here and not
6912 in cp_parser_declaration. (A #pragma at block scope is
6913 handled in cp_parser_statement.) */
6914 cp_parser_pragma (parser, pragma_external);
6918 /* Parse the declaration itself. */
6919 cp_parser_declaration (parser);
6923 /* Parse a declaration.
6928 template-declaration
6929 explicit-instantiation
6930 explicit-specialization
6931 linkage-specification
6932 namespace-definition
6937 __extension__ declaration */
6940 cp_parser_declaration (cp_parser* parser)
6947 /* Check for the `__extension__' keyword. */
6948 if (cp_parser_extension_opt (parser, &saved_pedantic))
6950 /* Parse the qualified declaration. */
6951 cp_parser_declaration (parser);
6952 /* Restore the PEDANTIC flag. */
6953 pedantic = saved_pedantic;
6958 /* Try to figure out what kind of declaration is present. */
6959 token1 = *cp_lexer_peek_token (parser->lexer);
6961 if (token1.type != CPP_EOF)
6962 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6965 token2.type = CPP_EOF;
6966 token2.keyword = RID_MAX;
6969 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6970 p = obstack_alloc (&declarator_obstack, 0);
6972 /* If the next token is `extern' and the following token is a string
6973 literal, then we have a linkage specification. */
6974 if (token1.keyword == RID_EXTERN
6975 && cp_parser_is_string_literal (&token2))
6976 cp_parser_linkage_specification (parser);
6977 /* If the next token is `template', then we have either a template
6978 declaration, an explicit instantiation, or an explicit
6980 else if (token1.keyword == RID_TEMPLATE)
6982 /* `template <>' indicates a template specialization. */
6983 if (token2.type == CPP_LESS
6984 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6985 cp_parser_explicit_specialization (parser);
6986 /* `template <' indicates a template declaration. */
6987 else if (token2.type == CPP_LESS)
6988 cp_parser_template_declaration (parser, /*member_p=*/false);
6989 /* Anything else must be an explicit instantiation. */
6991 cp_parser_explicit_instantiation (parser);
6993 /* If the next token is `export', then we have a template
6995 else if (token1.keyword == RID_EXPORT)
6996 cp_parser_template_declaration (parser, /*member_p=*/false);
6997 /* If the next token is `extern', 'static' or 'inline' and the one
6998 after that is `template', we have a GNU extended explicit
6999 instantiation directive. */
7000 else if (cp_parser_allow_gnu_extensions_p (parser)
7001 && (token1.keyword == RID_EXTERN
7002 || token1.keyword == RID_STATIC
7003 || token1.keyword == RID_INLINE)
7004 && token2.keyword == RID_TEMPLATE)
7005 cp_parser_explicit_instantiation (parser);
7006 /* If the next token is `namespace', check for a named or unnamed
7007 namespace definition. */
7008 else if (token1.keyword == RID_NAMESPACE
7009 && (/* A named namespace definition. */
7010 (token2.type == CPP_NAME
7011 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7013 /* An unnamed namespace definition. */
7014 || token2.type == CPP_OPEN_BRACE))
7015 cp_parser_namespace_definition (parser);
7016 /* Objective-C++ declaration/definition. */
7017 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7018 cp_parser_objc_declaration (parser);
7019 /* We must have either a block declaration or a function
7022 /* Try to parse a block-declaration, or a function-definition. */
7023 cp_parser_block_declaration (parser, /*statement_p=*/false);
7025 /* Free any declarators allocated. */
7026 obstack_free (&declarator_obstack, p);
7029 /* Parse a block-declaration.
7034 namespace-alias-definition
7041 __extension__ block-declaration
7044 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7045 part of a declaration-statement. */
7048 cp_parser_block_declaration (cp_parser *parser,
7054 /* Check for the `__extension__' keyword. */
7055 if (cp_parser_extension_opt (parser, &saved_pedantic))
7057 /* Parse the qualified declaration. */
7058 cp_parser_block_declaration (parser, statement_p);
7059 /* Restore the PEDANTIC flag. */
7060 pedantic = saved_pedantic;
7065 /* Peek at the next token to figure out which kind of declaration is
7067 token1 = cp_lexer_peek_token (parser->lexer);
7069 /* If the next keyword is `asm', we have an asm-definition. */
7070 if (token1->keyword == RID_ASM)
7073 cp_parser_commit_to_tentative_parse (parser);
7074 cp_parser_asm_definition (parser);
7076 /* If the next keyword is `namespace', we have a
7077 namespace-alias-definition. */
7078 else if (token1->keyword == RID_NAMESPACE)
7079 cp_parser_namespace_alias_definition (parser);
7080 /* If the next keyword is `using', we have either a
7081 using-declaration or a using-directive. */
7082 else if (token1->keyword == RID_USING)
7087 cp_parser_commit_to_tentative_parse (parser);
7088 /* If the token after `using' is `namespace', then we have a
7090 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7091 if (token2->keyword == RID_NAMESPACE)
7092 cp_parser_using_directive (parser);
7093 /* Otherwise, it's a using-declaration. */
7095 cp_parser_using_declaration (parser);
7097 /* If the next keyword is `__label__' we have a label declaration. */
7098 else if (token1->keyword == RID_LABEL)
7101 cp_parser_commit_to_tentative_parse (parser);
7102 cp_parser_label_declaration (parser);
7104 /* Anything else must be a simple-declaration. */
7106 cp_parser_simple_declaration (parser, !statement_p);
7109 /* Parse a simple-declaration.
7112 decl-specifier-seq [opt] init-declarator-list [opt] ;
7114 init-declarator-list:
7116 init-declarator-list , init-declarator
7118 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7119 function-definition as a simple-declaration. */
7122 cp_parser_simple_declaration (cp_parser* parser,
7123 bool function_definition_allowed_p)
7125 cp_decl_specifier_seq decl_specifiers;
7126 int declares_class_or_enum;
7127 bool saw_declarator;
7129 /* Defer access checks until we know what is being declared; the
7130 checks for names appearing in the decl-specifier-seq should be
7131 done as if we were in the scope of the thing being declared. */
7132 push_deferring_access_checks (dk_deferred);
7134 /* Parse the decl-specifier-seq. We have to keep track of whether
7135 or not the decl-specifier-seq declares a named class or
7136 enumeration type, since that is the only case in which the
7137 init-declarator-list is allowed to be empty.
7141 In a simple-declaration, the optional init-declarator-list can be
7142 omitted only when declaring a class or enumeration, that is when
7143 the decl-specifier-seq contains either a class-specifier, an
7144 elaborated-type-specifier, or an enum-specifier. */
7145 cp_parser_decl_specifier_seq (parser,
7146 CP_PARSER_FLAGS_OPTIONAL,
7148 &declares_class_or_enum);
7149 /* We no longer need to defer access checks. */
7150 stop_deferring_access_checks ();
7152 /* In a block scope, a valid declaration must always have a
7153 decl-specifier-seq. By not trying to parse declarators, we can
7154 resolve the declaration/expression ambiguity more quickly. */
7155 if (!function_definition_allowed_p
7156 && !decl_specifiers.any_specifiers_p)
7158 cp_parser_error (parser, "expected declaration");
7162 /* If the next two tokens are both identifiers, the code is
7163 erroneous. The usual cause of this situation is code like:
7167 where "T" should name a type -- but does not. */
7168 if (!decl_specifiers.type
7169 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7171 /* If parsing tentatively, we should commit; we really are
7172 looking at a declaration. */
7173 cp_parser_commit_to_tentative_parse (parser);
7178 /* If we have seen at least one decl-specifier, and the next token
7179 is not a parenthesis, then we must be looking at a declaration.
7180 (After "int (" we might be looking at a functional cast.) */
7181 if (decl_specifiers.any_specifiers_p
7182 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7183 cp_parser_commit_to_tentative_parse (parser);
7185 /* Keep going until we hit the `;' at the end of the simple
7187 saw_declarator = false;
7188 while (cp_lexer_next_token_is_not (parser->lexer,
7192 bool function_definition_p;
7197 /* If we are processing next declarator, coma is expected */
7198 token = cp_lexer_peek_token (parser->lexer);
7199 gcc_assert (token->type == CPP_COMMA);
7200 cp_lexer_consume_token (parser->lexer);
7203 saw_declarator = true;
7205 /* Parse the init-declarator. */
7206 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7207 function_definition_allowed_p,
7209 declares_class_or_enum,
7210 &function_definition_p);
7211 /* If an error occurred while parsing tentatively, exit quickly.
7212 (That usually happens when in the body of a function; each
7213 statement is treated as a declaration-statement until proven
7215 if (cp_parser_error_occurred (parser))
7217 /* Handle function definitions specially. */
7218 if (function_definition_p)
7220 /* If the next token is a `,', then we are probably
7221 processing something like:
7225 which is erroneous. */
7226 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7227 error ("mixing declarations and function-definitions is forbidden");
7228 /* Otherwise, we're done with the list of declarators. */
7231 pop_deferring_access_checks ();
7235 /* The next token should be either a `,' or a `;'. */
7236 token = cp_lexer_peek_token (parser->lexer);
7237 /* If it's a `,', there are more declarators to come. */
7238 if (token->type == CPP_COMMA)
7239 /* will be consumed next time around */;
7240 /* If it's a `;', we are done. */
7241 else if (token->type == CPP_SEMICOLON)
7243 /* Anything else is an error. */
7246 /* If we have already issued an error message we don't need
7247 to issue another one. */
7248 if (decl != error_mark_node
7249 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7250 cp_parser_error (parser, "expected %<,%> or %<;%>");
7251 /* Skip tokens until we reach the end of the statement. */
7252 cp_parser_skip_to_end_of_statement (parser);
7253 /* If the next token is now a `;', consume it. */
7254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7255 cp_lexer_consume_token (parser->lexer);
7258 /* After the first time around, a function-definition is not
7259 allowed -- even if it was OK at first. For example:
7264 function_definition_allowed_p = false;
7267 /* Issue an error message if no declarators are present, and the
7268 decl-specifier-seq does not itself declare a class or
7270 if (!saw_declarator)
7272 if (cp_parser_declares_only_class_p (parser))
7273 shadow_tag (&decl_specifiers);
7274 /* Perform any deferred access checks. */
7275 perform_deferred_access_checks ();
7278 /* Consume the `;'. */
7279 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7282 pop_deferring_access_checks ();
7285 /* Parse a decl-specifier-seq.
7288 decl-specifier-seq [opt] decl-specifier
7291 storage-class-specifier
7302 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7304 The parser flags FLAGS is used to control type-specifier parsing.
7306 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7309 1: one of the decl-specifiers is an elaborated-type-specifier
7310 (i.e., a type declaration)
7311 2: one of the decl-specifiers is an enum-specifier or a
7312 class-specifier (i.e., a type definition)
7317 cp_parser_decl_specifier_seq (cp_parser* parser,
7318 cp_parser_flags flags,
7319 cp_decl_specifier_seq *decl_specs,
7320 int* declares_class_or_enum)
7322 bool constructor_possible_p = !parser->in_declarator_p;
7324 /* Clear DECL_SPECS. */
7325 clear_decl_specs (decl_specs);
7327 /* Assume no class or enumeration type is declared. */
7328 *declares_class_or_enum = 0;
7330 /* Keep reading specifiers until there are no more to read. */
7334 bool found_decl_spec;
7337 /* Peek at the next token. */
7338 token = cp_lexer_peek_token (parser->lexer);
7339 /* Handle attributes. */
7340 if (token->keyword == RID_ATTRIBUTE)
7342 /* Parse the attributes. */
7343 decl_specs->attributes
7344 = chainon (decl_specs->attributes,
7345 cp_parser_attributes_opt (parser));
7348 /* Assume we will find a decl-specifier keyword. */
7349 found_decl_spec = true;
7350 /* If the next token is an appropriate keyword, we can simply
7351 add it to the list. */
7352 switch (token->keyword)
7357 if (decl_specs->specs[(int) ds_friend]++)
7358 error ("duplicate %<friend%>");
7359 /* Consume the token. */
7360 cp_lexer_consume_token (parser->lexer);
7363 /* function-specifier:
7370 cp_parser_function_specifier_opt (parser, decl_specs);
7376 ++decl_specs->specs[(int) ds_typedef];
7377 /* Consume the token. */
7378 cp_lexer_consume_token (parser->lexer);
7379 /* A constructor declarator cannot appear in a typedef. */
7380 constructor_possible_p = false;
7381 /* The "typedef" keyword can only occur in a declaration; we
7382 may as well commit at this point. */
7383 cp_parser_commit_to_tentative_parse (parser);
7386 /* storage-class-specifier:
7396 /* Consume the token. */
7397 cp_lexer_consume_token (parser->lexer);
7398 cp_parser_set_storage_class (decl_specs, sc_auto);
7401 /* Consume the token. */
7402 cp_lexer_consume_token (parser->lexer);
7403 cp_parser_set_storage_class (decl_specs, sc_register);
7406 /* Consume the token. */
7407 cp_lexer_consume_token (parser->lexer);
7408 if (decl_specs->specs[(int) ds_thread])
7410 error ("%<__thread%> before %<static%>");
7411 decl_specs->specs[(int) ds_thread] = 0;
7413 cp_parser_set_storage_class (decl_specs, sc_static);
7416 /* Consume the token. */
7417 cp_lexer_consume_token (parser->lexer);
7418 if (decl_specs->specs[(int) ds_thread])
7420 error ("%<__thread%> before %<extern%>");
7421 decl_specs->specs[(int) ds_thread] = 0;
7423 cp_parser_set_storage_class (decl_specs, sc_extern);
7426 /* Consume the token. */
7427 cp_lexer_consume_token (parser->lexer);
7428 cp_parser_set_storage_class (decl_specs, sc_mutable);
7431 /* Consume the token. */
7432 cp_lexer_consume_token (parser->lexer);
7433 ++decl_specs->specs[(int) ds_thread];
7437 /* We did not yet find a decl-specifier yet. */
7438 found_decl_spec = false;
7442 /* Constructors are a special case. The `S' in `S()' is not a
7443 decl-specifier; it is the beginning of the declarator. */
7446 && constructor_possible_p
7447 && (cp_parser_constructor_declarator_p
7448 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7450 /* If we don't have a DECL_SPEC yet, then we must be looking at
7451 a type-specifier. */
7452 if (!found_decl_spec && !constructor_p)
7454 int decl_spec_declares_class_or_enum;
7455 bool is_cv_qualifier;
7459 = cp_parser_type_specifier (parser, flags,
7461 /*is_declaration=*/true,
7462 &decl_spec_declares_class_or_enum,
7465 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7467 /* If this type-specifier referenced a user-defined type
7468 (a typedef, class-name, etc.), then we can't allow any
7469 more such type-specifiers henceforth.
7473 The longest sequence of decl-specifiers that could
7474 possibly be a type name is taken as the
7475 decl-specifier-seq of a declaration. The sequence shall
7476 be self-consistent as described below.
7480 As a general rule, at most one type-specifier is allowed
7481 in the complete decl-specifier-seq of a declaration. The
7482 only exceptions are the following:
7484 -- const or volatile can be combined with any other
7487 -- signed or unsigned can be combined with char, long,
7495 void g (const int Pc);
7497 Here, Pc is *not* part of the decl-specifier seq; it's
7498 the declarator. Therefore, once we see a type-specifier
7499 (other than a cv-qualifier), we forbid any additional
7500 user-defined types. We *do* still allow things like `int
7501 int' to be considered a decl-specifier-seq, and issue the
7502 error message later. */
7503 if (type_spec && !is_cv_qualifier)
7504 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7505 /* A constructor declarator cannot follow a type-specifier. */
7508 constructor_possible_p = false;
7509 found_decl_spec = true;
7513 /* If we still do not have a DECL_SPEC, then there are no more
7515 if (!found_decl_spec)
7518 decl_specs->any_specifiers_p = true;
7519 /* After we see one decl-specifier, further decl-specifiers are
7521 flags |= CP_PARSER_FLAGS_OPTIONAL;
7524 /* Don't allow a friend specifier with a class definition. */
7525 if (decl_specs->specs[(int) ds_friend] != 0
7526 && (*declares_class_or_enum & 2))
7527 error ("class definition may not be declared a friend");
7530 /* Parse an (optional) storage-class-specifier.
7532 storage-class-specifier:
7541 storage-class-specifier:
7544 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7547 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7549 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7557 /* Consume the token. */
7558 return cp_lexer_consume_token (parser->lexer)->value;
7565 /* Parse an (optional) function-specifier.
7572 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7573 Updates DECL_SPECS, if it is non-NULL. */
7576 cp_parser_function_specifier_opt (cp_parser* parser,
7577 cp_decl_specifier_seq *decl_specs)
7579 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7583 ++decl_specs->specs[(int) ds_inline];
7588 ++decl_specs->specs[(int) ds_virtual];
7593 ++decl_specs->specs[(int) ds_explicit];
7600 /* Consume the token. */
7601 return cp_lexer_consume_token (parser->lexer)->value;
7604 /* Parse a linkage-specification.
7606 linkage-specification:
7607 extern string-literal { declaration-seq [opt] }
7608 extern string-literal declaration */
7611 cp_parser_linkage_specification (cp_parser* parser)
7615 /* Look for the `extern' keyword. */
7616 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7618 /* Look for the string-literal. */
7619 linkage = cp_parser_string_literal (parser, false, false);
7621 /* Transform the literal into an identifier. If the literal is a
7622 wide-character string, or contains embedded NULs, then we can't
7623 handle it as the user wants. */
7624 if (strlen (TREE_STRING_POINTER (linkage))
7625 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7627 cp_parser_error (parser, "invalid linkage-specification");
7628 /* Assume C++ linkage. */
7629 linkage = lang_name_cplusplus;
7632 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7634 /* We're now using the new linkage. */
7635 push_lang_context (linkage);
7637 /* If the next token is a `{', then we're using the first
7639 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7641 /* Consume the `{' token. */
7642 cp_lexer_consume_token (parser->lexer);
7643 /* Parse the declarations. */
7644 cp_parser_declaration_seq_opt (parser);
7645 /* Look for the closing `}'. */
7646 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7648 /* Otherwise, there's just one declaration. */
7651 bool saved_in_unbraced_linkage_specification_p;
7653 saved_in_unbraced_linkage_specification_p
7654 = parser->in_unbraced_linkage_specification_p;
7655 parser->in_unbraced_linkage_specification_p = true;
7656 have_extern_spec = true;
7657 cp_parser_declaration (parser);
7658 have_extern_spec = false;
7659 parser->in_unbraced_linkage_specification_p
7660 = saved_in_unbraced_linkage_specification_p;
7663 /* We're done with the linkage-specification. */
7664 pop_lang_context ();
7667 /* Special member functions [gram.special] */
7669 /* Parse a conversion-function-id.
7671 conversion-function-id:
7672 operator conversion-type-id
7674 Returns an IDENTIFIER_NODE representing the operator. */
7677 cp_parser_conversion_function_id (cp_parser* parser)
7681 tree saved_qualifying_scope;
7682 tree saved_object_scope;
7683 tree pushed_scope = NULL_TREE;
7685 /* Look for the `operator' token. */
7686 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7687 return error_mark_node;
7688 /* When we parse the conversion-type-id, the current scope will be
7689 reset. However, we need that information in able to look up the
7690 conversion function later, so we save it here. */
7691 saved_scope = parser->scope;
7692 saved_qualifying_scope = parser->qualifying_scope;
7693 saved_object_scope = parser->object_scope;
7694 /* We must enter the scope of the class so that the names of
7695 entities declared within the class are available in the
7696 conversion-type-id. For example, consider:
7703 S::operator I() { ... }
7705 In order to see that `I' is a type-name in the definition, we
7706 must be in the scope of `S'. */
7708 pushed_scope = push_scope (saved_scope);
7709 /* Parse the conversion-type-id. */
7710 type = cp_parser_conversion_type_id (parser);
7711 /* Leave the scope of the class, if any. */
7713 pop_scope (pushed_scope);
7714 /* Restore the saved scope. */
7715 parser->scope = saved_scope;
7716 parser->qualifying_scope = saved_qualifying_scope;
7717 parser->object_scope = saved_object_scope;
7718 /* If the TYPE is invalid, indicate failure. */
7719 if (type == error_mark_node)
7720 return error_mark_node;
7721 return mangle_conv_op_name_for_type (type);
7724 /* Parse a conversion-type-id:
7727 type-specifier-seq conversion-declarator [opt]
7729 Returns the TYPE specified. */
7732 cp_parser_conversion_type_id (cp_parser* parser)
7735 cp_decl_specifier_seq type_specifiers;
7736 cp_declarator *declarator;
7737 tree type_specified;
7739 /* Parse the attributes. */
7740 attributes = cp_parser_attributes_opt (parser);
7741 /* Parse the type-specifiers. */
7742 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7744 /* If that didn't work, stop. */
7745 if (type_specifiers.type == error_mark_node)
7746 return error_mark_node;
7747 /* Parse the conversion-declarator. */
7748 declarator = cp_parser_conversion_declarator_opt (parser);
7750 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7751 /*initialized=*/0, &attributes);
7753 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7754 return type_specified;
7757 /* Parse an (optional) conversion-declarator.
7759 conversion-declarator:
7760 ptr-operator conversion-declarator [opt]
7764 static cp_declarator *
7765 cp_parser_conversion_declarator_opt (cp_parser* parser)
7767 enum tree_code code;
7769 cp_cv_quals cv_quals;
7771 /* We don't know if there's a ptr-operator next, or not. */
7772 cp_parser_parse_tentatively (parser);
7773 /* Try the ptr-operator. */
7774 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7775 /* If it worked, look for more conversion-declarators. */
7776 if (cp_parser_parse_definitely (parser))
7778 cp_declarator *declarator;
7780 /* Parse another optional declarator. */
7781 declarator = cp_parser_conversion_declarator_opt (parser);
7783 /* Create the representation of the declarator. */
7785 declarator = make_ptrmem_declarator (cv_quals, class_type,
7787 else if (code == INDIRECT_REF)
7788 declarator = make_pointer_declarator (cv_quals, declarator);
7790 declarator = make_reference_declarator (cv_quals, declarator);
7798 /* Parse an (optional) ctor-initializer.
7801 : mem-initializer-list
7803 Returns TRUE iff the ctor-initializer was actually present. */
7806 cp_parser_ctor_initializer_opt (cp_parser* parser)
7808 /* If the next token is not a `:', then there is no
7809 ctor-initializer. */
7810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7812 /* Do default initialization of any bases and members. */
7813 if (DECL_CONSTRUCTOR_P (current_function_decl))
7814 finish_mem_initializers (NULL_TREE);
7819 /* Consume the `:' token. */
7820 cp_lexer_consume_token (parser->lexer);
7821 /* And the mem-initializer-list. */
7822 cp_parser_mem_initializer_list (parser);
7827 /* Parse a mem-initializer-list.
7829 mem-initializer-list:
7831 mem-initializer , mem-initializer-list */
7834 cp_parser_mem_initializer_list (cp_parser* parser)
7836 tree mem_initializer_list = NULL_TREE;
7838 /* Let the semantic analysis code know that we are starting the
7839 mem-initializer-list. */
7840 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7841 error ("only constructors take base initializers");
7843 /* Loop through the list. */
7846 tree mem_initializer;
7848 /* Parse the mem-initializer. */
7849 mem_initializer = cp_parser_mem_initializer (parser);
7850 /* Add it to the list, unless it was erroneous. */
7851 if (mem_initializer != error_mark_node)
7853 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7854 mem_initializer_list = mem_initializer;
7856 /* If the next token is not a `,', we're done. */
7857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7859 /* Consume the `,' token. */
7860 cp_lexer_consume_token (parser->lexer);
7863 /* Perform semantic analysis. */
7864 if (DECL_CONSTRUCTOR_P (current_function_decl))
7865 finish_mem_initializers (mem_initializer_list);
7868 /* Parse a mem-initializer.
7871 mem-initializer-id ( expression-list [opt] )
7876 ( expression-list [opt] )
7878 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7879 class) or FIELD_DECL (for a non-static data member) to initialize;
7880 the TREE_VALUE is the expression-list. An empty initialization
7881 list is represented by void_list_node. */
7884 cp_parser_mem_initializer (cp_parser* parser)
7886 tree mem_initializer_id;
7887 tree expression_list;
7890 /* Find out what is being initialized. */
7891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7893 pedwarn ("anachronistic old-style base class initializer");
7894 mem_initializer_id = NULL_TREE;
7897 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7898 member = expand_member_init (mem_initializer_id);
7899 if (member && !DECL_P (member))
7900 in_base_initializer = 1;
7903 = cp_parser_parenthesized_expression_list (parser, false,
7905 /*non_constant_p=*/NULL);
7906 if (expression_list == error_mark_node)
7907 return error_mark_node;
7908 if (!expression_list)
7909 expression_list = void_type_node;
7911 in_base_initializer = 0;
7913 return member ? build_tree_list (member, expression_list) : error_mark_node;
7916 /* Parse a mem-initializer-id.
7919 :: [opt] nested-name-specifier [opt] class-name
7922 Returns a TYPE indicating the class to be initializer for the first
7923 production. Returns an IDENTIFIER_NODE indicating the data member
7924 to be initialized for the second production. */
7927 cp_parser_mem_initializer_id (cp_parser* parser)
7929 bool global_scope_p;
7930 bool nested_name_specifier_p;
7931 bool template_p = false;
7934 /* `typename' is not allowed in this context ([temp.res]). */
7935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7937 error ("keyword %<typename%> not allowed in this context (a qualified "
7938 "member initializer is implicitly a type)");
7939 cp_lexer_consume_token (parser->lexer);
7941 /* Look for the optional `::' operator. */
7943 = (cp_parser_global_scope_opt (parser,
7944 /*current_scope_valid_p=*/false)
7946 /* Look for the optional nested-name-specifier. The simplest way to
7951 The keyword `typename' is not permitted in a base-specifier or
7952 mem-initializer; in these contexts a qualified name that
7953 depends on a template-parameter is implicitly assumed to be a
7956 is to assume that we have seen the `typename' keyword at this
7958 nested_name_specifier_p
7959 = (cp_parser_nested_name_specifier_opt (parser,
7960 /*typename_keyword_p=*/true,
7961 /*check_dependency_p=*/true,
7963 /*is_declaration=*/true)
7965 if (nested_name_specifier_p)
7966 template_p = cp_parser_optional_template_keyword (parser);
7967 /* If there is a `::' operator or a nested-name-specifier, then we
7968 are definitely looking for a class-name. */
7969 if (global_scope_p || nested_name_specifier_p)
7970 return cp_parser_class_name (parser,
7971 /*typename_keyword_p=*/true,
7972 /*template_keyword_p=*/template_p,
7974 /*check_dependency_p=*/true,
7975 /*class_head_p=*/false,
7976 /*is_declaration=*/true);
7977 /* Otherwise, we could also be looking for an ordinary identifier. */
7978 cp_parser_parse_tentatively (parser);
7979 /* Try a class-name. */
7980 id = cp_parser_class_name (parser,
7981 /*typename_keyword_p=*/true,
7982 /*template_keyword_p=*/false,
7984 /*check_dependency_p=*/true,
7985 /*class_head_p=*/false,
7986 /*is_declaration=*/true);
7987 /* If we found one, we're done. */
7988 if (cp_parser_parse_definitely (parser))
7990 /* Otherwise, look for an ordinary identifier. */
7991 return cp_parser_identifier (parser);
7994 /* Overloading [gram.over] */
7996 /* Parse an operator-function-id.
7998 operator-function-id:
8001 Returns an IDENTIFIER_NODE for the operator which is a
8002 human-readable spelling of the identifier, e.g., `operator +'. */
8005 cp_parser_operator_function_id (cp_parser* parser)
8007 /* Look for the `operator' keyword. */
8008 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8009 return error_mark_node;
8010 /* And then the name of the operator itself. */
8011 return cp_parser_operator (parser);
8014 /* Parse an operator.
8017 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8018 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8019 || ++ -- , ->* -> () []
8026 Returns an IDENTIFIER_NODE for the operator which is a
8027 human-readable spelling of the identifier, e.g., `operator +'. */
8030 cp_parser_operator (cp_parser* parser)
8032 tree id = NULL_TREE;
8035 /* Peek at the next token. */
8036 token = cp_lexer_peek_token (parser->lexer);
8037 /* Figure out which operator we have. */
8038 switch (token->type)
8044 /* The keyword should be either `new' or `delete'. */
8045 if (token->keyword == RID_NEW)
8047 else if (token->keyword == RID_DELETE)
8052 /* Consume the `new' or `delete' token. */
8053 cp_lexer_consume_token (parser->lexer);
8055 /* Peek at the next token. */
8056 token = cp_lexer_peek_token (parser->lexer);
8057 /* If it's a `[' token then this is the array variant of the
8059 if (token->type == CPP_OPEN_SQUARE)
8061 /* Consume the `[' token. */
8062 cp_lexer_consume_token (parser->lexer);
8063 /* Look for the `]' token. */
8064 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8065 id = ansi_opname (op == NEW_EXPR
8066 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8068 /* Otherwise, we have the non-array variant. */
8070 id = ansi_opname (op);
8076 id = ansi_opname (PLUS_EXPR);
8080 id = ansi_opname (MINUS_EXPR);
8084 id = ansi_opname (MULT_EXPR);
8088 id = ansi_opname (TRUNC_DIV_EXPR);
8092 id = ansi_opname (TRUNC_MOD_EXPR);
8096 id = ansi_opname (BIT_XOR_EXPR);
8100 id = ansi_opname (BIT_AND_EXPR);
8104 id = ansi_opname (BIT_IOR_EXPR);
8108 id = ansi_opname (BIT_NOT_EXPR);
8112 id = ansi_opname (TRUTH_NOT_EXPR);
8116 id = ansi_assopname (NOP_EXPR);
8120 id = ansi_opname (LT_EXPR);
8124 id = ansi_opname (GT_EXPR);
8128 id = ansi_assopname (PLUS_EXPR);
8132 id = ansi_assopname (MINUS_EXPR);
8136 id = ansi_assopname (MULT_EXPR);
8140 id = ansi_assopname (TRUNC_DIV_EXPR);
8144 id = ansi_assopname (TRUNC_MOD_EXPR);
8148 id = ansi_assopname (BIT_XOR_EXPR);
8152 id = ansi_assopname (BIT_AND_EXPR);
8156 id = ansi_assopname (BIT_IOR_EXPR);
8160 id = ansi_opname (LSHIFT_EXPR);
8164 id = ansi_opname (RSHIFT_EXPR);
8168 id = ansi_assopname (LSHIFT_EXPR);
8172 id = ansi_assopname (RSHIFT_EXPR);
8176 id = ansi_opname (EQ_EXPR);
8180 id = ansi_opname (NE_EXPR);
8184 id = ansi_opname (LE_EXPR);
8187 case CPP_GREATER_EQ:
8188 id = ansi_opname (GE_EXPR);
8192 id = ansi_opname (TRUTH_ANDIF_EXPR);
8196 id = ansi_opname (TRUTH_ORIF_EXPR);
8200 id = ansi_opname (POSTINCREMENT_EXPR);
8203 case CPP_MINUS_MINUS:
8204 id = ansi_opname (PREDECREMENT_EXPR);
8208 id = ansi_opname (COMPOUND_EXPR);
8211 case CPP_DEREF_STAR:
8212 id = ansi_opname (MEMBER_REF);
8216 id = ansi_opname (COMPONENT_REF);
8219 case CPP_OPEN_PAREN:
8220 /* Consume the `('. */
8221 cp_lexer_consume_token (parser->lexer);
8222 /* Look for the matching `)'. */
8223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8224 return ansi_opname (CALL_EXPR);
8226 case CPP_OPEN_SQUARE:
8227 /* Consume the `['. */
8228 cp_lexer_consume_token (parser->lexer);
8229 /* Look for the matching `]'. */
8230 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8231 return ansi_opname (ARRAY_REF);
8235 id = ansi_opname (MIN_EXPR);
8236 cp_parser_warn_min_max ();
8240 id = ansi_opname (MAX_EXPR);
8241 cp_parser_warn_min_max ();
8245 id = ansi_assopname (MIN_EXPR);
8246 cp_parser_warn_min_max ();
8250 id = ansi_assopname (MAX_EXPR);
8251 cp_parser_warn_min_max ();
8255 /* Anything else is an error. */
8259 /* If we have selected an identifier, we need to consume the
8262 cp_lexer_consume_token (parser->lexer);
8263 /* Otherwise, no valid operator name was present. */
8266 cp_parser_error (parser, "expected operator");
8267 id = error_mark_node;
8273 /* Parse a template-declaration.
8275 template-declaration:
8276 export [opt] template < template-parameter-list > declaration
8278 If MEMBER_P is TRUE, this template-declaration occurs within a
8281 The grammar rule given by the standard isn't correct. What
8284 template-declaration:
8285 export [opt] template-parameter-list-seq
8286 decl-specifier-seq [opt] init-declarator [opt] ;
8287 export [opt] template-parameter-list-seq
8290 template-parameter-list-seq:
8291 template-parameter-list-seq [opt]
8292 template < template-parameter-list > */
8295 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8297 /* Check for `export'. */
8298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8300 /* Consume the `export' token. */
8301 cp_lexer_consume_token (parser->lexer);
8302 /* Warn that we do not support `export'. */
8303 warning (0, "keyword %<export%> not implemented, and will be ignored");
8306 cp_parser_template_declaration_after_export (parser, member_p);
8309 /* Parse a template-parameter-list.
8311 template-parameter-list:
8313 template-parameter-list , template-parameter
8315 Returns a TREE_LIST. Each node represents a template parameter.
8316 The nodes are connected via their TREE_CHAINs. */
8319 cp_parser_template_parameter_list (cp_parser* parser)
8321 tree parameter_list = NULL_TREE;
8323 begin_template_parm_list ();
8330 /* Parse the template-parameter. */
8331 parameter = cp_parser_template_parameter (parser, &is_non_type);
8332 /* Add it to the list. */
8333 if (parameter != error_mark_node)
8334 parameter_list = process_template_parm (parameter_list,
8337 /* Peek at the next token. */
8338 token = cp_lexer_peek_token (parser->lexer);
8339 /* If it's not a `,', we're done. */
8340 if (token->type != CPP_COMMA)
8342 /* Otherwise, consume the `,' token. */
8343 cp_lexer_consume_token (parser->lexer);
8346 return end_template_parm_list (parameter_list);
8349 /* Parse a template-parameter.
8353 parameter-declaration
8355 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8356 the parameter. The TREE_PURPOSE is the default value, if any.
8357 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8358 iff this parameter is a non-type parameter. */
8361 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8364 cp_parameter_declarator *parameter_declarator;
8367 /* Assume it is a type parameter or a template parameter. */
8368 *is_non_type = false;
8369 /* Peek at the next token. */
8370 token = cp_lexer_peek_token (parser->lexer);
8371 /* If it is `class' or `template', we have a type-parameter. */
8372 if (token->keyword == RID_TEMPLATE)
8373 return cp_parser_type_parameter (parser);
8374 /* If it is `class' or `typename' we do not know yet whether it is a
8375 type parameter or a non-type parameter. Consider:
8377 template <typename T, typename T::X X> ...
8381 template <class C, class D*> ...
8383 Here, the first parameter is a type parameter, and the second is
8384 a non-type parameter. We can tell by looking at the token after
8385 the identifier -- if it is a `,', `=', or `>' then we have a type
8387 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8389 /* Peek at the token after `class' or `typename'. */
8390 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8391 /* If it's an identifier, skip it. */
8392 if (token->type == CPP_NAME)
8393 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8394 /* Now, see if the token looks like the end of a template
8396 if (token->type == CPP_COMMA
8397 || token->type == CPP_EQ
8398 || token->type == CPP_GREATER)
8399 return cp_parser_type_parameter (parser);
8402 /* Otherwise, it is a non-type parameter.
8406 When parsing a default template-argument for a non-type
8407 template-parameter, the first non-nested `>' is taken as the end
8408 of the template parameter-list rather than a greater-than
8410 *is_non_type = true;
8411 parameter_declarator
8412 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8413 /*parenthesized_p=*/NULL);
8414 parm = grokdeclarator (parameter_declarator->declarator,
8415 ¶meter_declarator->decl_specifiers,
8416 PARM, /*initialized=*/0,
8418 if (parm == error_mark_node)
8419 return error_mark_node;
8420 return build_tree_list (parameter_declarator->default_argument, parm);
8423 /* Parse a type-parameter.
8426 class identifier [opt]
8427 class identifier [opt] = type-id
8428 typename identifier [opt]
8429 typename identifier [opt] = type-id
8430 template < template-parameter-list > class identifier [opt]
8431 template < template-parameter-list > class identifier [opt]
8434 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8435 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8436 the declaration of the parameter. */
8439 cp_parser_type_parameter (cp_parser* parser)
8444 /* Look for a keyword to tell us what kind of parameter this is. */
8445 token = cp_parser_require (parser, CPP_KEYWORD,
8446 "`class', `typename', or `template'");
8448 return error_mark_node;
8450 switch (token->keyword)
8456 tree default_argument;
8458 /* If the next token is an identifier, then it names the
8460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8461 identifier = cp_parser_identifier (parser);
8463 identifier = NULL_TREE;
8465 /* Create the parameter. */
8466 parameter = finish_template_type_parm (class_type_node, identifier);
8468 /* If the next token is an `=', we have a default argument. */
8469 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8471 /* Consume the `=' token. */
8472 cp_lexer_consume_token (parser->lexer);
8473 /* Parse the default-argument. */
8474 default_argument = cp_parser_type_id (parser);
8477 default_argument = NULL_TREE;
8479 /* Create the combined representation of the parameter and the
8480 default argument. */
8481 parameter = build_tree_list (default_argument, parameter);
8487 tree parameter_list;
8489 tree default_argument;
8491 /* Look for the `<'. */
8492 cp_parser_require (parser, CPP_LESS, "`<'");
8493 /* Parse the template-parameter-list. */
8494 parameter_list = cp_parser_template_parameter_list (parser);
8495 /* Look for the `>'. */
8496 cp_parser_require (parser, CPP_GREATER, "`>'");
8497 /* Look for the `class' keyword. */
8498 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8499 /* If the next token is an `=', then there is a
8500 default-argument. If the next token is a `>', we are at
8501 the end of the parameter-list. If the next token is a `,',
8502 then we are at the end of this parameter. */
8503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8504 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8505 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8507 identifier = cp_parser_identifier (parser);
8508 /* Treat invalid names as if the parameter were nameless. */
8509 if (identifier == error_mark_node)
8510 identifier = NULL_TREE;
8513 identifier = NULL_TREE;
8515 /* Create the template parameter. */
8516 parameter = finish_template_template_parm (class_type_node,
8519 /* If the next token is an `=', then there is a
8520 default-argument. */
8521 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8525 /* Consume the `='. */
8526 cp_lexer_consume_token (parser->lexer);
8527 /* Parse the id-expression. */
8529 = cp_parser_id_expression (parser,
8530 /*template_keyword_p=*/false,
8531 /*check_dependency_p=*/true,
8532 /*template_p=*/&is_template,
8533 /*declarator_p=*/false);
8534 if (TREE_CODE (default_argument) == TYPE_DECL)
8535 /* If the id-expression was a template-id that refers to
8536 a template-class, we already have the declaration here,
8537 so no further lookup is needed. */
8540 /* Look up the name. */
8542 = cp_parser_lookup_name (parser, default_argument,
8544 /*is_template=*/is_template,
8545 /*is_namespace=*/false,
8546 /*check_dependency=*/true,
8547 /*ambiguous_decls=*/NULL);
8548 /* See if the default argument is valid. */
8550 = check_template_template_default_arg (default_argument);
8553 default_argument = NULL_TREE;
8555 /* Create the combined representation of the parameter and the
8556 default argument. */
8557 parameter = build_tree_list (default_argument, parameter);
8569 /* Parse a template-id.
8572 template-name < template-argument-list [opt] >
8574 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8575 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8576 returned. Otherwise, if the template-name names a function, or set
8577 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8578 names a class, returns a TYPE_DECL for the specialization.
8580 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8581 uninstantiated templates. */
8584 cp_parser_template_id (cp_parser *parser,
8585 bool template_keyword_p,
8586 bool check_dependency_p,
8587 bool is_declaration)
8592 cp_token_position start_of_id = 0;
8593 tree access_check = NULL_TREE;
8594 cp_token *next_token, *next_token_2;
8597 /* If the next token corresponds to a template-id, there is no need
8599 next_token = cp_lexer_peek_token (parser->lexer);
8600 if (next_token->type == CPP_TEMPLATE_ID)
8605 /* Get the stored value. */
8606 value = cp_lexer_consume_token (parser->lexer)->value;
8607 /* Perform any access checks that were deferred. */
8608 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8609 perform_or_defer_access_check (TREE_PURPOSE (check),
8610 TREE_VALUE (check));
8611 /* Return the stored value. */
8612 return TREE_VALUE (value);
8615 /* Avoid performing name lookup if there is no possibility of
8616 finding a template-id. */
8617 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8618 || (next_token->type == CPP_NAME
8619 && !cp_parser_nth_token_starts_template_argument_list_p
8622 cp_parser_error (parser, "expected template-id");
8623 return error_mark_node;
8626 /* Remember where the template-id starts. */
8627 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8628 start_of_id = cp_lexer_token_position (parser->lexer, false);
8630 push_deferring_access_checks (dk_deferred);
8632 /* Parse the template-name. */
8633 is_identifier = false;
8634 template = cp_parser_template_name (parser, template_keyword_p,
8638 if (template == error_mark_node || is_identifier)
8640 pop_deferring_access_checks ();
8644 /* If we find the sequence `[:' after a template-name, it's probably
8645 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8646 parse correctly the argument list. */
8647 next_token = cp_lexer_peek_token (parser->lexer);
8648 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8649 if (next_token->type == CPP_OPEN_SQUARE
8650 && next_token->flags & DIGRAPH
8651 && next_token_2->type == CPP_COLON
8652 && !(next_token_2->flags & PREV_WHITE))
8654 cp_parser_parse_tentatively (parser);
8655 /* Change `:' into `::'. */
8656 next_token_2->type = CPP_SCOPE;
8657 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8659 cp_lexer_consume_token (parser->lexer);
8660 /* Parse the arguments. */
8661 arguments = cp_parser_enclosed_template_argument_list (parser);
8662 if (!cp_parser_parse_definitely (parser))
8664 /* If we couldn't parse an argument list, then we revert our changes
8665 and return simply an error. Maybe this is not a template-id
8667 next_token_2->type = CPP_COLON;
8668 cp_parser_error (parser, "expected %<<%>");
8669 pop_deferring_access_checks ();
8670 return error_mark_node;
8672 /* Otherwise, emit an error about the invalid digraph, but continue
8673 parsing because we got our argument list. */
8674 pedwarn ("%<<::%> cannot begin a template-argument list");
8675 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8676 "between %<<%> and %<::%>");
8677 if (!flag_permissive)
8682 inform ("(if you use -fpermissive G++ will accept your code)");
8689 /* Look for the `<' that starts the template-argument-list. */
8690 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8692 pop_deferring_access_checks ();
8693 return error_mark_node;
8695 /* Parse the arguments. */
8696 arguments = cp_parser_enclosed_template_argument_list (parser);
8699 /* Build a representation of the specialization. */
8700 if (TREE_CODE (template) == IDENTIFIER_NODE)
8701 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8702 else if (DECL_CLASS_TEMPLATE_P (template)
8703 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8705 = finish_template_type (template, arguments,
8706 cp_lexer_next_token_is (parser->lexer,
8710 /* If it's not a class-template or a template-template, it should be
8711 a function-template. */
8712 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8713 || TREE_CODE (template) == OVERLOAD
8714 || BASELINK_P (template)));
8716 template_id = lookup_template_function (template, arguments);
8719 /* Retrieve any deferred checks. Do not pop this access checks yet
8720 so the memory will not be reclaimed during token replacing below. */
8721 access_check = get_deferred_access_checks ();
8723 /* If parsing tentatively, replace the sequence of tokens that makes
8724 up the template-id with a CPP_TEMPLATE_ID token. That way,
8725 should we re-parse the token stream, we will not have to repeat
8726 the effort required to do the parse, nor will we issue duplicate
8727 error messages about problems during instantiation of the
8731 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8733 /* Reset the contents of the START_OF_ID token. */
8734 token->type = CPP_TEMPLATE_ID;
8735 token->value = build_tree_list (access_check, template_id);
8736 token->keyword = RID_MAX;
8738 /* Purge all subsequent tokens. */
8739 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8741 /* ??? Can we actually assume that, if template_id ==
8742 error_mark_node, we will have issued a diagnostic to the
8743 user, as opposed to simply marking the tentative parse as
8745 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8746 error ("parse error in template argument list");
8749 pop_deferring_access_checks ();
8753 /* Parse a template-name.
8758 The standard should actually say:
8762 operator-function-id
8764 A defect report has been filed about this issue.
8766 A conversion-function-id cannot be a template name because they cannot
8767 be part of a template-id. In fact, looking at this code:
8771 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8772 It is impossible to call a templated conversion-function-id with an
8773 explicit argument list, since the only allowed template parameter is
8774 the type to which it is converting.
8776 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8777 `template' keyword, in a construction like:
8781 In that case `f' is taken to be a template-name, even though there
8782 is no way of knowing for sure.
8784 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8785 name refers to a set of overloaded functions, at least one of which
8786 is a template, or an IDENTIFIER_NODE with the name of the template,
8787 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8788 names are looked up inside uninstantiated templates. */
8791 cp_parser_template_name (cp_parser* parser,
8792 bool template_keyword_p,
8793 bool check_dependency_p,
8794 bool is_declaration,
8795 bool *is_identifier)
8801 /* If the next token is `operator', then we have either an
8802 operator-function-id or a conversion-function-id. */
8803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8805 /* We don't know whether we're looking at an
8806 operator-function-id or a conversion-function-id. */
8807 cp_parser_parse_tentatively (parser);
8808 /* Try an operator-function-id. */
8809 identifier = cp_parser_operator_function_id (parser);
8810 /* If that didn't work, try a conversion-function-id. */
8811 if (!cp_parser_parse_definitely (parser))
8813 cp_parser_error (parser, "expected template-name");
8814 return error_mark_node;
8817 /* Look for the identifier. */
8819 identifier = cp_parser_identifier (parser);
8821 /* If we didn't find an identifier, we don't have a template-id. */
8822 if (identifier == error_mark_node)
8823 return error_mark_node;
8825 /* If the name immediately followed the `template' keyword, then it
8826 is a template-name. However, if the next token is not `<', then
8827 we do not treat it as a template-name, since it is not being used
8828 as part of a template-id. This enables us to handle constructs
8831 template <typename T> struct S { S(); };
8832 template <typename T> S<T>::S();
8834 correctly. We would treat `S' as a template -- if it were `S<T>'
8835 -- but we do not if there is no `<'. */
8837 if (processing_template_decl
8838 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8840 /* In a declaration, in a dependent context, we pretend that the
8841 "template" keyword was present in order to improve error
8842 recovery. For example, given:
8844 template <typename T> void f(T::X<int>);
8846 we want to treat "X<int>" as a template-id. */
8848 && !template_keyword_p
8849 && parser->scope && TYPE_P (parser->scope)
8850 && check_dependency_p
8851 && dependent_type_p (parser->scope)
8852 /* Do not do this for dtors (or ctors), since they never
8853 need the template keyword before their name. */
8854 && !constructor_name_p (identifier, parser->scope))
8856 cp_token_position start = 0;
8858 /* Explain what went wrong. */
8859 error ("non-template %qD used as template", identifier);
8860 inform ("use %<%T::template %D%> to indicate that it is a template",
8861 parser->scope, identifier);
8862 /* If parsing tentatively, find the location of the "<" token. */
8863 if (cp_parser_simulate_error (parser))
8864 start = cp_lexer_token_position (parser->lexer, true);
8865 /* Parse the template arguments so that we can issue error
8866 messages about them. */
8867 cp_lexer_consume_token (parser->lexer);
8868 cp_parser_enclosed_template_argument_list (parser);
8869 /* Skip tokens until we find a good place from which to
8870 continue parsing. */
8871 cp_parser_skip_to_closing_parenthesis (parser,
8872 /*recovering=*/true,
8874 /*consume_paren=*/false);
8875 /* If parsing tentatively, permanently remove the
8876 template argument list. That will prevent duplicate
8877 error messages from being issued about the missing
8878 "template" keyword. */
8880 cp_lexer_purge_tokens_after (parser->lexer, start);
8882 *is_identifier = true;
8886 /* If the "template" keyword is present, then there is generally
8887 no point in doing name-lookup, so we just return IDENTIFIER.
8888 But, if the qualifying scope is non-dependent then we can
8889 (and must) do name-lookup normally. */
8890 if (template_keyword_p
8892 || (TYPE_P (parser->scope)
8893 && dependent_type_p (parser->scope))))
8897 /* Look up the name. */
8898 decl = cp_parser_lookup_name (parser, identifier,
8900 /*is_template=*/false,
8901 /*is_namespace=*/false,
8903 /*ambiguous_decls=*/NULL);
8904 decl = maybe_get_template_decl_from_type_decl (decl);
8906 /* If DECL is a template, then the name was a template-name. */
8907 if (TREE_CODE (decl) == TEMPLATE_DECL)
8911 tree fn = NULL_TREE;
8913 /* The standard does not explicitly indicate whether a name that
8914 names a set of overloaded declarations, some of which are
8915 templates, is a template-name. However, such a name should
8916 be a template-name; otherwise, there is no way to form a
8917 template-id for the overloaded templates. */
8918 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8919 if (TREE_CODE (fns) == OVERLOAD)
8920 for (fn = fns; fn; fn = OVL_NEXT (fn))
8921 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8926 /* The name does not name a template. */
8927 cp_parser_error (parser, "expected template-name");
8928 return error_mark_node;
8932 /* If DECL is dependent, and refers to a function, then just return
8933 its name; we will look it up again during template instantiation. */
8934 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8936 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8937 if (TYPE_P (scope) && dependent_type_p (scope))
8944 /* Parse a template-argument-list.
8946 template-argument-list:
8948 template-argument-list , template-argument
8950 Returns a TREE_VEC containing the arguments. */
8953 cp_parser_template_argument_list (cp_parser* parser)
8955 tree fixed_args[10];
8956 unsigned n_args = 0;
8957 unsigned alloced = 10;
8958 tree *arg_ary = fixed_args;
8960 bool saved_in_template_argument_list_p;
8962 bool saved_non_ice_p;
8964 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8965 parser->in_template_argument_list_p = true;
8966 /* Even if the template-id appears in an integral
8967 constant-expression, the contents of the argument list do
8969 saved_ice_p = parser->integral_constant_expression_p;
8970 parser->integral_constant_expression_p = false;
8971 saved_non_ice_p = parser->non_integral_constant_expression_p;
8972 parser->non_integral_constant_expression_p = false;
8973 /* Parse the arguments. */
8979 /* Consume the comma. */
8980 cp_lexer_consume_token (parser->lexer);
8982 /* Parse the template-argument. */
8983 argument = cp_parser_template_argument (parser);
8984 if (n_args == alloced)
8988 if (arg_ary == fixed_args)
8990 arg_ary = XNEWVEC (tree, alloced);
8991 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8994 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
8996 arg_ary[n_args++] = argument;
8998 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9000 vec = make_tree_vec (n_args);
9003 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9005 if (arg_ary != fixed_args)
9007 parser->non_integral_constant_expression_p = saved_non_ice_p;
9008 parser->integral_constant_expression_p = saved_ice_p;
9009 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9013 /* Parse a template-argument.
9016 assignment-expression
9020 The representation is that of an assignment-expression, type-id, or
9021 id-expression -- except that the qualified id-expression is
9022 evaluated, so that the value returned is either a DECL or an
9025 Although the standard says "assignment-expression", it forbids
9026 throw-expressions or assignments in the template argument.
9027 Therefore, we use "conditional-expression" instead. */
9030 cp_parser_template_argument (cp_parser* parser)
9035 bool maybe_type_id = false;
9039 /* There's really no way to know what we're looking at, so we just
9040 try each alternative in order.
9044 In a template-argument, an ambiguity between a type-id and an
9045 expression is resolved to a type-id, regardless of the form of
9046 the corresponding template-parameter.
9048 Therefore, we try a type-id first. */
9049 cp_parser_parse_tentatively (parser);
9050 argument = cp_parser_type_id (parser);
9051 /* If there was no error parsing the type-id but the next token is a '>>',
9052 we probably found a typo for '> >'. But there are type-id which are
9053 also valid expressions. For instance:
9055 struct X { int operator >> (int); };
9056 template <int V> struct Foo {};
9059 Here 'X()' is a valid type-id of a function type, but the user just
9060 wanted to write the expression "X() >> 5". Thus, we remember that we
9061 found a valid type-id, but we still try to parse the argument as an
9062 expression to see what happens. */
9063 if (!cp_parser_error_occurred (parser)
9064 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9066 maybe_type_id = true;
9067 cp_parser_abort_tentative_parse (parser);
9071 /* If the next token isn't a `,' or a `>', then this argument wasn't
9072 really finished. This means that the argument is not a valid
9074 if (!cp_parser_next_token_ends_template_argument_p (parser))
9075 cp_parser_error (parser, "expected template-argument");
9076 /* If that worked, we're done. */
9077 if (cp_parser_parse_definitely (parser))
9080 /* We're still not sure what the argument will be. */
9081 cp_parser_parse_tentatively (parser);
9082 /* Try a template. */
9083 argument = cp_parser_id_expression (parser,
9084 /*template_keyword_p=*/false,
9085 /*check_dependency_p=*/true,
9087 /*declarator_p=*/false);
9088 /* If the next token isn't a `,' or a `>', then this argument wasn't
9090 if (!cp_parser_next_token_ends_template_argument_p (parser))
9091 cp_parser_error (parser, "expected template-argument");
9092 if (!cp_parser_error_occurred (parser))
9094 /* Figure out what is being referred to. If the id-expression
9095 was for a class template specialization, then we will have a
9096 TYPE_DECL at this point. There is no need to do name lookup
9097 at this point in that case. */
9098 if (TREE_CODE (argument) != TYPE_DECL)
9099 argument = cp_parser_lookup_name (parser, argument,
9101 /*is_template=*/template_p,
9102 /*is_namespace=*/false,
9103 /*check_dependency=*/true,
9104 /*ambiguous_decls=*/NULL);
9105 if (TREE_CODE (argument) != TEMPLATE_DECL
9106 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9107 cp_parser_error (parser, "expected template-name");
9109 if (cp_parser_parse_definitely (parser))
9111 /* It must be a non-type argument. There permitted cases are given
9112 in [temp.arg.nontype]:
9114 -- an integral constant-expression of integral or enumeration
9117 -- the name of a non-type template-parameter; or
9119 -- the name of an object or function with external linkage...
9121 -- the address of an object or function with external linkage...
9123 -- a pointer to member... */
9124 /* Look for a non-type template parameter. */
9125 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9127 cp_parser_parse_tentatively (parser);
9128 argument = cp_parser_primary_expression (parser,
9131 /*template_arg_p=*/true,
9133 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9134 || !cp_parser_next_token_ends_template_argument_p (parser))
9135 cp_parser_simulate_error (parser);
9136 if (cp_parser_parse_definitely (parser))
9140 /* If the next token is "&", the argument must be the address of an
9141 object or function with external linkage. */
9142 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9144 cp_lexer_consume_token (parser->lexer);
9145 /* See if we might have an id-expression. */
9146 token = cp_lexer_peek_token (parser->lexer);
9147 if (token->type == CPP_NAME
9148 || token->keyword == RID_OPERATOR
9149 || token->type == CPP_SCOPE
9150 || token->type == CPP_TEMPLATE_ID
9151 || token->type == CPP_NESTED_NAME_SPECIFIER)
9153 cp_parser_parse_tentatively (parser);
9154 argument = cp_parser_primary_expression (parser,
9157 /*template_arg_p=*/true,
9159 if (cp_parser_error_occurred (parser)
9160 || !cp_parser_next_token_ends_template_argument_p (parser))
9161 cp_parser_abort_tentative_parse (parser);
9164 if (TREE_CODE (argument) == INDIRECT_REF)
9166 gcc_assert (REFERENCE_REF_P (argument));
9167 argument = TREE_OPERAND (argument, 0);
9170 if (TREE_CODE (argument) == BASELINK)
9171 /* We don't need the information about what class was used
9172 to name the overloaded functions. */
9173 argument = BASELINK_FUNCTIONS (argument);
9175 if (TREE_CODE (argument) == VAR_DECL)
9177 /* A variable without external linkage might still be a
9178 valid constant-expression, so no error is issued here
9179 if the external-linkage check fails. */
9180 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9181 cp_parser_simulate_error (parser);
9183 else if (is_overloaded_fn (argument))
9184 /* All overloaded functions are allowed; if the external
9185 linkage test does not pass, an error will be issued
9189 && (TREE_CODE (argument) == OFFSET_REF
9190 || TREE_CODE (argument) == SCOPE_REF))
9191 /* A pointer-to-member. */
9193 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9196 cp_parser_simulate_error (parser);
9198 if (cp_parser_parse_definitely (parser))
9201 argument = build_x_unary_op (ADDR_EXPR, argument);
9206 /* If the argument started with "&", there are no other valid
9207 alternatives at this point. */
9210 cp_parser_error (parser, "invalid non-type template argument");
9211 return error_mark_node;
9214 /* If the argument wasn't successfully parsed as a type-id followed
9215 by '>>', the argument can only be a constant expression now.
9216 Otherwise, we try parsing the constant-expression tentatively,
9217 because the argument could really be a type-id. */
9219 cp_parser_parse_tentatively (parser);
9220 argument = cp_parser_constant_expression (parser,
9221 /*allow_non_constant_p=*/false,
9222 /*non_constant_p=*/NULL);
9223 argument = fold_non_dependent_expr (argument);
9226 if (!cp_parser_next_token_ends_template_argument_p (parser))
9227 cp_parser_error (parser, "expected template-argument");
9228 if (cp_parser_parse_definitely (parser))
9230 /* We did our best to parse the argument as a non type-id, but that
9231 was the only alternative that matched (albeit with a '>' after
9232 it). We can assume it's just a typo from the user, and a
9233 diagnostic will then be issued. */
9234 return cp_parser_type_id (parser);
9237 /* Parse an explicit-instantiation.
9239 explicit-instantiation:
9240 template declaration
9242 Although the standard says `declaration', what it really means is:
9244 explicit-instantiation:
9245 template decl-specifier-seq [opt] declarator [opt] ;
9247 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9248 supposed to be allowed. A defect report has been filed about this
9253 explicit-instantiation:
9254 storage-class-specifier template
9255 decl-specifier-seq [opt] declarator [opt] ;
9256 function-specifier template
9257 decl-specifier-seq [opt] declarator [opt] ; */
9260 cp_parser_explicit_instantiation (cp_parser* parser)
9262 int declares_class_or_enum;
9263 cp_decl_specifier_seq decl_specifiers;
9264 tree extension_specifier = NULL_TREE;
9266 /* Look for an (optional) storage-class-specifier or
9267 function-specifier. */
9268 if (cp_parser_allow_gnu_extensions_p (parser))
9271 = cp_parser_storage_class_specifier_opt (parser);
9272 if (!extension_specifier)
9274 = cp_parser_function_specifier_opt (parser,
9275 /*decl_specs=*/NULL);
9278 /* Look for the `template' keyword. */
9279 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9280 /* Let the front end know that we are processing an explicit
9282 begin_explicit_instantiation ();
9283 /* [temp.explicit] says that we are supposed to ignore access
9284 control while processing explicit instantiation directives. */
9285 push_deferring_access_checks (dk_no_check);
9286 /* Parse a decl-specifier-seq. */
9287 cp_parser_decl_specifier_seq (parser,
9288 CP_PARSER_FLAGS_OPTIONAL,
9290 &declares_class_or_enum);
9291 /* If there was exactly one decl-specifier, and it declared a class,
9292 and there's no declarator, then we have an explicit type
9294 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9298 type = check_tag_decl (&decl_specifiers);
9299 /* Turn access control back on for names used during
9300 template instantiation. */
9301 pop_deferring_access_checks ();
9303 do_type_instantiation (type, extension_specifier,
9304 /*complain=*/tf_error);
9308 cp_declarator *declarator;
9311 /* Parse the declarator. */
9313 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9314 /*ctor_dtor_or_conv_p=*/NULL,
9315 /*parenthesized_p=*/NULL,
9316 /*member_p=*/false);
9317 if (declares_class_or_enum & 2)
9318 cp_parser_check_for_definition_in_return_type (declarator,
9319 decl_specifiers.type);
9320 if (declarator != cp_error_declarator)
9322 decl = grokdeclarator (declarator, &decl_specifiers,
9324 /* Turn access control back on for names used during
9325 template instantiation. */
9326 pop_deferring_access_checks ();
9327 /* Do the explicit instantiation. */
9328 do_decl_instantiation (decl, extension_specifier);
9332 pop_deferring_access_checks ();
9333 /* Skip the body of the explicit instantiation. */
9334 cp_parser_skip_to_end_of_statement (parser);
9337 /* We're done with the instantiation. */
9338 end_explicit_instantiation ();
9340 cp_parser_consume_semicolon_at_end_of_statement (parser);
9343 /* Parse an explicit-specialization.
9345 explicit-specialization:
9346 template < > declaration
9348 Although the standard says `declaration', what it really means is:
9350 explicit-specialization:
9351 template <> decl-specifier [opt] init-declarator [opt] ;
9352 template <> function-definition
9353 template <> explicit-specialization
9354 template <> template-declaration */
9357 cp_parser_explicit_specialization (cp_parser* parser)
9360 /* Look for the `template' keyword. */
9361 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9362 /* Look for the `<'. */
9363 cp_parser_require (parser, CPP_LESS, "`<'");
9364 /* Look for the `>'. */
9365 cp_parser_require (parser, CPP_GREATER, "`>'");
9366 /* We have processed another parameter list. */
9367 ++parser->num_template_parameter_lists;
9370 A template ... explicit specialization ... shall not have C
9372 if (current_lang_name == lang_name_c)
9374 error ("template specialization with C linkage");
9375 /* Give it C++ linkage to avoid confusing other parts of the
9377 push_lang_context (lang_name_cplusplus);
9378 need_lang_pop = true;
9381 need_lang_pop = false;
9382 /* Let the front end know that we are beginning a specialization. */
9383 begin_specialization ();
9384 /* If the next keyword is `template', we need to figure out whether
9385 or not we're looking a template-declaration. */
9386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9388 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9389 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9390 cp_parser_template_declaration_after_export (parser,
9391 /*member_p=*/false);
9393 cp_parser_explicit_specialization (parser);
9396 /* Parse the dependent declaration. */
9397 cp_parser_single_declaration (parser,
9400 /* We're done with the specialization. */
9401 end_specialization ();
9402 /* For the erroneous case of a template with C linkage, we pushed an
9403 implicit C++ linkage scope; exit that scope now. */
9405 pop_lang_context ();
9406 /* We're done with this parameter list. */
9407 --parser->num_template_parameter_lists;
9410 /* Parse a type-specifier.
9413 simple-type-specifier
9416 elaborated-type-specifier
9424 Returns a representation of the type-specifier. For a
9425 class-specifier, enum-specifier, or elaborated-type-specifier, a
9426 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9428 The parser flags FLAGS is used to control type-specifier parsing.
9430 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9431 in a decl-specifier-seq.
9433 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9434 class-specifier, enum-specifier, or elaborated-type-specifier, then
9435 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9436 if a type is declared; 2 if it is defined. Otherwise, it is set to
9439 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9440 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9444 cp_parser_type_specifier (cp_parser* parser,
9445 cp_parser_flags flags,
9446 cp_decl_specifier_seq *decl_specs,
9447 bool is_declaration,
9448 int* declares_class_or_enum,
9449 bool* is_cv_qualifier)
9451 tree type_spec = NULL_TREE;
9454 cp_decl_spec ds = ds_last;
9456 /* Assume this type-specifier does not declare a new type. */
9457 if (declares_class_or_enum)
9458 *declares_class_or_enum = 0;
9459 /* And that it does not specify a cv-qualifier. */
9460 if (is_cv_qualifier)
9461 *is_cv_qualifier = false;
9462 /* Peek at the next token. */
9463 token = cp_lexer_peek_token (parser->lexer);
9465 /* If we're looking at a keyword, we can use that to guide the
9466 production we choose. */
9467 keyword = token->keyword;
9471 /* 'enum' [identifier] '{' introduces an enum-specifier;
9472 'enum' <anything else> introduces an elaborated-type-specifier. */
9473 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9474 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9475 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9478 if (parser->num_template_parameter_lists)
9480 error ("template declaration of %qs", "enum");
9481 cp_parser_skip_to_end_of_block_or_statement (parser);
9482 type_spec = error_mark_node;
9485 type_spec = cp_parser_enum_specifier (parser);
9487 if (declares_class_or_enum)
9488 *declares_class_or_enum = 2;
9490 cp_parser_set_decl_spec_type (decl_specs,
9492 /*user_defined_p=*/true);
9496 goto elaborated_type_specifier;
9498 /* Any of these indicate either a class-specifier, or an
9499 elaborated-type-specifier. */
9503 /* Parse tentatively so that we can back up if we don't find a
9505 cp_parser_parse_tentatively (parser);
9506 /* Look for the class-specifier. */
9507 type_spec = cp_parser_class_specifier (parser);
9508 /* If that worked, we're done. */
9509 if (cp_parser_parse_definitely (parser))
9511 if (declares_class_or_enum)
9512 *declares_class_or_enum = 2;
9514 cp_parser_set_decl_spec_type (decl_specs,
9516 /*user_defined_p=*/true);
9521 elaborated_type_specifier:
9522 /* We're declaring (not defining) a class or enum. */
9523 if (declares_class_or_enum)
9524 *declares_class_or_enum = 1;
9528 /* Look for an elaborated-type-specifier. */
9530 = (cp_parser_elaborated_type_specifier
9532 decl_specs && decl_specs->specs[(int) ds_friend],
9535 cp_parser_set_decl_spec_type (decl_specs,
9537 /*user_defined_p=*/true);
9542 if (is_cv_qualifier)
9543 *is_cv_qualifier = true;
9548 if (is_cv_qualifier)
9549 *is_cv_qualifier = true;
9554 if (is_cv_qualifier)
9555 *is_cv_qualifier = true;
9559 /* The `__complex__' keyword is a GNU extension. */
9567 /* Handle simple keywords. */
9572 ++decl_specs->specs[(int)ds];
9573 decl_specs->any_specifiers_p = true;
9575 return cp_lexer_consume_token (parser->lexer)->value;
9578 /* If we do not already have a type-specifier, assume we are looking
9579 at a simple-type-specifier. */
9580 type_spec = cp_parser_simple_type_specifier (parser,
9584 /* If we didn't find a type-specifier, and a type-specifier was not
9585 optional in this context, issue an error message. */
9586 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9588 cp_parser_error (parser, "expected type specifier");
9589 return error_mark_node;
9595 /* Parse a simple-type-specifier.
9597 simple-type-specifier:
9598 :: [opt] nested-name-specifier [opt] type-name
9599 :: [opt] nested-name-specifier template template-id
9614 simple-type-specifier:
9615 __typeof__ unary-expression
9616 __typeof__ ( type-id )
9618 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9619 appropriately updated. */
9622 cp_parser_simple_type_specifier (cp_parser* parser,
9623 cp_decl_specifier_seq *decl_specs,
9624 cp_parser_flags flags)
9626 tree type = NULL_TREE;
9629 /* Peek at the next token. */
9630 token = cp_lexer_peek_token (parser->lexer);
9632 /* If we're looking at a keyword, things are easy. */
9633 switch (token->keyword)
9637 decl_specs->explicit_char_p = true;
9638 type = char_type_node;
9641 type = wchar_type_node;
9644 type = boolean_type_node;
9648 ++decl_specs->specs[(int) ds_short];
9649 type = short_integer_type_node;
9653 decl_specs->explicit_int_p = true;
9654 type = integer_type_node;
9658 ++decl_specs->specs[(int) ds_long];
9659 type = long_integer_type_node;
9663 ++decl_specs->specs[(int) ds_signed];
9664 type = integer_type_node;
9668 ++decl_specs->specs[(int) ds_unsigned];
9669 type = unsigned_type_node;
9672 type = float_type_node;
9675 type = double_type_node;
9678 type = void_type_node;
9682 /* Consume the `typeof' token. */
9683 cp_lexer_consume_token (parser->lexer);
9684 /* Parse the operand to `typeof'. */
9685 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9686 /* If it is not already a TYPE, take its type. */
9688 type = finish_typeof (type);
9691 cp_parser_set_decl_spec_type (decl_specs, type,
9692 /*user_defined_p=*/true);
9700 /* If the type-specifier was for a built-in type, we're done. */
9705 /* Record the type. */
9707 && (token->keyword != RID_SIGNED
9708 && token->keyword != RID_UNSIGNED
9709 && token->keyword != RID_SHORT
9710 && token->keyword != RID_LONG))
9711 cp_parser_set_decl_spec_type (decl_specs,
9713 /*user_defined=*/false);
9715 decl_specs->any_specifiers_p = true;
9717 /* Consume the token. */
9718 id = cp_lexer_consume_token (parser->lexer)->value;
9720 /* There is no valid C++ program where a non-template type is
9721 followed by a "<". That usually indicates that the user thought
9722 that the type was a template. */
9723 cp_parser_check_for_invalid_template_id (parser, type);
9725 return TYPE_NAME (type);
9728 /* The type-specifier must be a user-defined type. */
9729 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9734 /* Don't gobble tokens or issue error messages if this is an
9735 optional type-specifier. */
9736 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9737 cp_parser_parse_tentatively (parser);
9739 /* Look for the optional `::' operator. */
9741 = (cp_parser_global_scope_opt (parser,
9742 /*current_scope_valid_p=*/false)
9744 /* Look for the nested-name specifier. */
9746 = (cp_parser_nested_name_specifier_opt (parser,
9747 /*typename_keyword_p=*/false,
9748 /*check_dependency_p=*/true,
9750 /*is_declaration=*/false)
9752 /* If we have seen a nested-name-specifier, and the next token
9753 is `template', then we are using the template-id production. */
9755 && cp_parser_optional_template_keyword (parser))
9757 /* Look for the template-id. */
9758 type = cp_parser_template_id (parser,
9759 /*template_keyword_p=*/true,
9760 /*check_dependency_p=*/true,
9761 /*is_declaration=*/false);
9762 /* If the template-id did not name a type, we are out of
9764 if (TREE_CODE (type) != TYPE_DECL)
9766 cp_parser_error (parser, "expected template-id for type");
9770 /* Otherwise, look for a type-name. */
9772 type = cp_parser_type_name (parser);
9773 /* Keep track of all name-lookups performed in class scopes. */
9777 && TREE_CODE (type) == TYPE_DECL
9778 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9779 maybe_note_name_used_in_class (DECL_NAME (type), type);
9780 /* If it didn't work out, we don't have a TYPE. */
9781 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9782 && !cp_parser_parse_definitely (parser))
9784 if (type && decl_specs)
9785 cp_parser_set_decl_spec_type (decl_specs, type,
9786 /*user_defined=*/true);
9789 /* If we didn't get a type-name, issue an error message. */
9790 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9792 cp_parser_error (parser, "expected type-name");
9793 return error_mark_node;
9796 /* There is no valid C++ program where a non-template type is
9797 followed by a "<". That usually indicates that the user thought
9798 that the type was a template. */
9799 if (type && type != error_mark_node)
9801 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9802 If it is, then the '<'...'>' enclose protocol names rather than
9803 template arguments, and so everything is fine. */
9804 if (c_dialect_objc ()
9805 && (objc_is_id (type) || objc_is_class_name (type)))
9807 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9808 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9810 /* Clobber the "unqualified" type previously entered into
9811 DECL_SPECS with the new, improved protocol-qualified version. */
9813 decl_specs->type = qual_type;
9818 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9824 /* Parse a type-name.
9837 Returns a TYPE_DECL for the type. */
9840 cp_parser_type_name (cp_parser* parser)
9845 /* We can't know yet whether it is a class-name or not. */
9846 cp_parser_parse_tentatively (parser);
9847 /* Try a class-name. */
9848 type_decl = cp_parser_class_name (parser,
9849 /*typename_keyword_p=*/false,
9850 /*template_keyword_p=*/false,
9852 /*check_dependency_p=*/true,
9853 /*class_head_p=*/false,
9854 /*is_declaration=*/false);
9855 /* If it's not a class-name, keep looking. */
9856 if (!cp_parser_parse_definitely (parser))
9858 /* It must be a typedef-name or an enum-name. */
9859 identifier = cp_parser_identifier (parser);
9860 if (identifier == error_mark_node)
9861 return error_mark_node;
9863 /* Look up the type-name. */
9864 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9866 if (TREE_CODE (type_decl) != TYPE_DECL
9867 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9869 /* See if this is an Objective-C type. */
9870 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9871 tree type = objc_get_protocol_qualified_type (identifier, protos);
9873 type_decl = TYPE_NAME (type);
9876 /* Issue an error if we did not find a type-name. */
9877 if (TREE_CODE (type_decl) != TYPE_DECL)
9879 if (!cp_parser_simulate_error (parser))
9880 cp_parser_name_lookup_error (parser, identifier, type_decl,
9882 type_decl = error_mark_node;
9884 /* Remember that the name was used in the definition of the
9885 current class so that we can check later to see if the
9886 meaning would have been different after the class was
9887 entirely defined. */
9888 else if (type_decl != error_mark_node
9890 maybe_note_name_used_in_class (identifier, type_decl);
9897 /* Parse an elaborated-type-specifier. Note that the grammar given
9898 here incorporates the resolution to DR68.
9900 elaborated-type-specifier:
9901 class-key :: [opt] nested-name-specifier [opt] identifier
9902 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9903 enum :: [opt] nested-name-specifier [opt] identifier
9904 typename :: [opt] nested-name-specifier identifier
9905 typename :: [opt] nested-name-specifier template [opt]
9910 elaborated-type-specifier:
9911 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9912 class-key attributes :: [opt] nested-name-specifier [opt]
9913 template [opt] template-id
9914 enum attributes :: [opt] nested-name-specifier [opt] identifier
9916 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9917 declared `friend'. If IS_DECLARATION is TRUE, then this
9918 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9919 something is being declared.
9921 Returns the TYPE specified. */
9924 cp_parser_elaborated_type_specifier (cp_parser* parser,
9926 bool is_declaration)
9928 enum tag_types tag_type;
9930 tree type = NULL_TREE;
9931 tree attributes = NULL_TREE;
9933 /* See if we're looking at the `enum' keyword. */
9934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9936 /* Consume the `enum' token. */
9937 cp_lexer_consume_token (parser->lexer);
9938 /* Remember that it's an enumeration type. */
9939 tag_type = enum_type;
9940 /* Parse the attributes. */
9941 attributes = cp_parser_attributes_opt (parser);
9943 /* Or, it might be `typename'. */
9944 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9947 /* Consume the `typename' token. */
9948 cp_lexer_consume_token (parser->lexer);
9949 /* Remember that it's a `typename' type. */
9950 tag_type = typename_type;
9951 /* The `typename' keyword is only allowed in templates. */
9952 if (!processing_template_decl)
9953 pedwarn ("using %<typename%> outside of template");
9955 /* Otherwise it must be a class-key. */
9958 tag_type = cp_parser_class_key (parser);
9959 if (tag_type == none_type)
9960 return error_mark_node;
9961 /* Parse the attributes. */
9962 attributes = cp_parser_attributes_opt (parser);
9965 /* Look for the `::' operator. */
9966 cp_parser_global_scope_opt (parser,
9967 /*current_scope_valid_p=*/false);
9968 /* Look for the nested-name-specifier. */
9969 if (tag_type == typename_type)
9971 if (!cp_parser_nested_name_specifier (parser,
9972 /*typename_keyword_p=*/true,
9973 /*check_dependency_p=*/true,
9976 return error_mark_node;
9979 /* Even though `typename' is not present, the proposed resolution
9980 to Core Issue 180 says that in `class A<T>::B', `B' should be
9981 considered a type-name, even if `A<T>' is dependent. */
9982 cp_parser_nested_name_specifier_opt (parser,
9983 /*typename_keyword_p=*/true,
9984 /*check_dependency_p=*/true,
9987 /* For everything but enumeration types, consider a template-id. */
9988 if (tag_type != enum_type)
9990 bool template_p = false;
9993 /* Allow the `template' keyword. */
9994 template_p = cp_parser_optional_template_keyword (parser);
9995 /* If we didn't see `template', we don't know if there's a
9996 template-id or not. */
9998 cp_parser_parse_tentatively (parser);
9999 /* Parse the template-id. */
10000 decl = cp_parser_template_id (parser, template_p,
10001 /*check_dependency_p=*/true,
10003 /* If we didn't find a template-id, look for an ordinary
10005 if (!template_p && !cp_parser_parse_definitely (parser))
10007 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10008 in effect, then we must assume that, upon instantiation, the
10009 template will correspond to a class. */
10010 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10011 && tag_type == typename_type)
10012 type = make_typename_type (parser->scope, decl,
10014 /*complain=*/tf_error);
10016 type = TREE_TYPE (decl);
10019 /* For an enumeration type, consider only a plain identifier. */
10022 identifier = cp_parser_identifier (parser);
10024 if (identifier == error_mark_node)
10026 parser->scope = NULL_TREE;
10027 return error_mark_node;
10030 /* For a `typename', we needn't call xref_tag. */
10031 if (tag_type == typename_type
10032 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10033 return cp_parser_make_typename_type (parser, parser->scope,
10035 /* Look up a qualified name in the usual way. */
10040 decl = cp_parser_lookup_name (parser, identifier,
10042 /*is_template=*/false,
10043 /*is_namespace=*/false,
10044 /*check_dependency=*/true,
10045 /*ambiguous_decls=*/NULL);
10047 /* If we are parsing friend declaration, DECL may be a
10048 TEMPLATE_DECL tree node here. However, we need to check
10049 whether this TEMPLATE_DECL results in valid code. Consider
10050 the following example:
10053 template <class T> class C {};
10056 template <class T> friend class N::C; // #1, valid code
10058 template <class T> class Y {
10059 friend class N::C; // #2, invalid code
10062 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10063 name lookup of `N::C'. We see that friend declaration must
10064 be template for the code to be valid. Note that
10065 processing_template_decl does not work here since it is
10066 always 1 for the above two cases. */
10068 decl = (cp_parser_maybe_treat_template_as_class
10069 (decl, /*tag_name_p=*/is_friend
10070 && parser->num_template_parameter_lists));
10072 if (TREE_CODE (decl) != TYPE_DECL)
10074 cp_parser_diagnose_invalid_type_name (parser,
10077 return error_mark_node;
10080 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10081 check_elaborated_type_specifier
10083 (parser->num_template_parameter_lists
10084 || DECL_SELF_REFERENCE_P (decl)));
10086 type = TREE_TYPE (decl);
10090 /* An elaborated-type-specifier sometimes introduces a new type and
10091 sometimes names an existing type. Normally, the rule is that it
10092 introduces a new type only if there is not an existing type of
10093 the same name already in scope. For example, given:
10096 void f() { struct S s; }
10098 the `struct S' in the body of `f' is the same `struct S' as in
10099 the global scope; the existing definition is used. However, if
10100 there were no global declaration, this would introduce a new
10101 local class named `S'.
10103 An exception to this rule applies to the following code:
10105 namespace N { struct S; }
10107 Here, the elaborated-type-specifier names a new type
10108 unconditionally; even if there is already an `S' in the
10109 containing scope this declaration names a new type.
10110 This exception only applies if the elaborated-type-specifier
10111 forms the complete declaration:
10115 A declaration consisting solely of `class-key identifier ;' is
10116 either a redeclaration of the name in the current scope or a
10117 forward declaration of the identifier as a class name. It
10118 introduces the name into the current scope.
10120 We are in this situation precisely when the next token is a `;'.
10122 An exception to the exception is that a `friend' declaration does
10123 *not* name a new type; i.e., given:
10125 struct S { friend struct T; };
10127 `T' is not a new type in the scope of `S'.
10129 Also, `new struct S' or `sizeof (struct S)' never results in the
10130 definition of a new type; a new type can only be declared in a
10131 declaration context. */
10137 /* Friends have special name lookup rules. */
10138 ts = ts_within_enclosing_non_class;
10139 else if (is_declaration
10140 && cp_lexer_next_token_is (parser->lexer,
10142 /* This is a `class-key identifier ;' */
10147 /* Warn about attributes. They are ignored. */
10149 warning (OPT_Wattributes,
10150 "type attributes are honored only at type definition");
10153 (parser->num_template_parameter_lists
10154 && (cp_parser_next_token_starts_class_definition_p (parser)
10155 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10156 /* An unqualified name was used to reference this type, so
10157 there were no qualifying templates. */
10158 if (!cp_parser_check_template_parameters (parser,
10159 /*num_templates=*/0))
10160 return error_mark_node;
10161 type = xref_tag (tag_type, identifier, ts, template_p);
10164 if (tag_type != enum_type)
10165 cp_parser_check_class_key (tag_type, type);
10167 /* A "<" cannot follow an elaborated type specifier. If that
10168 happens, the user was probably trying to form a template-id. */
10169 cp_parser_check_for_invalid_template_id (parser, type);
10174 /* Parse an enum-specifier.
10177 enum identifier [opt] { enumerator-list [opt] }
10180 enum identifier [opt] { enumerator-list [opt] } attributes
10182 Returns an ENUM_TYPE representing the enumeration. */
10185 cp_parser_enum_specifier (cp_parser* parser)
10190 /* Caller guarantees that the current token is 'enum', an identifier
10191 possibly follows, and the token after that is an opening brace.
10192 If we don't have an identifier, fabricate an anonymous name for
10193 the enumeration being defined. */
10194 cp_lexer_consume_token (parser->lexer);
10196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10197 identifier = cp_parser_identifier (parser);
10199 identifier = make_anon_name ();
10201 /* Issue an error message if type-definitions are forbidden here. */
10202 cp_parser_check_type_definition (parser);
10204 /* Create the new type. We do this before consuming the opening brace
10205 so the enum will be recorded as being on the line of its tag (or the
10206 'enum' keyword, if there is no tag). */
10207 type = start_enum (identifier);
10209 /* Consume the opening brace. */
10210 cp_lexer_consume_token (parser->lexer);
10212 /* If the next token is not '}', then there are some enumerators. */
10213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10214 cp_parser_enumerator_list (parser, type);
10216 /* Consume the final '}'. */
10217 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10219 /* Look for trailing attributes to apply to this enumeration, and
10220 apply them if appropriate. */
10221 if (cp_parser_allow_gnu_extensions_p (parser))
10223 tree trailing_attr = cp_parser_attributes_opt (parser);
10224 cplus_decl_attributes (&type,
10226 (int) ATTR_FLAG_TYPE_IN_PLACE);
10229 /* Finish up the enumeration. */
10230 finish_enum (type);
10235 /* Parse an enumerator-list. The enumerators all have the indicated
10239 enumerator-definition
10240 enumerator-list , enumerator-definition */
10243 cp_parser_enumerator_list (cp_parser* parser, tree type)
10247 /* Parse an enumerator-definition. */
10248 cp_parser_enumerator_definition (parser, type);
10250 /* If the next token is not a ',', we've reached the end of
10252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10254 /* Otherwise, consume the `,' and keep going. */
10255 cp_lexer_consume_token (parser->lexer);
10256 /* If the next token is a `}', there is a trailing comma. */
10257 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10259 if (pedantic && !in_system_header)
10260 pedwarn ("comma at end of enumerator list");
10266 /* Parse an enumerator-definition. The enumerator has the indicated
10269 enumerator-definition:
10271 enumerator = constant-expression
10277 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10282 /* Look for the identifier. */
10283 identifier = cp_parser_identifier (parser);
10284 if (identifier == error_mark_node)
10287 /* If the next token is an '=', then there is an explicit value. */
10288 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10290 /* Consume the `=' token. */
10291 cp_lexer_consume_token (parser->lexer);
10292 /* Parse the value. */
10293 value = cp_parser_constant_expression (parser,
10294 /*allow_non_constant_p=*/false,
10300 /* Create the enumerator. */
10301 build_enumerator (identifier, value, type);
10304 /* Parse a namespace-name.
10307 original-namespace-name
10310 Returns the NAMESPACE_DECL for the namespace. */
10313 cp_parser_namespace_name (cp_parser* parser)
10316 tree namespace_decl;
10318 /* Get the name of the namespace. */
10319 identifier = cp_parser_identifier (parser);
10320 if (identifier == error_mark_node)
10321 return error_mark_node;
10323 /* Look up the identifier in the currently active scope. Look only
10324 for namespaces, due to:
10326 [basic.lookup.udir]
10328 When looking up a namespace-name in a using-directive or alias
10329 definition, only namespace names are considered.
10333 [basic.lookup.qual]
10335 During the lookup of a name preceding the :: scope resolution
10336 operator, object, function, and enumerator names are ignored.
10338 (Note that cp_parser_class_or_namespace_name only calls this
10339 function if the token after the name is the scope resolution
10341 namespace_decl = cp_parser_lookup_name (parser, identifier,
10343 /*is_template=*/false,
10344 /*is_namespace=*/true,
10345 /*check_dependency=*/true,
10346 /*ambiguous_decls=*/NULL);
10347 /* If it's not a namespace, issue an error. */
10348 if (namespace_decl == error_mark_node
10349 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10351 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10352 error ("%qD is not a namespace-name", identifier);
10353 cp_parser_error (parser, "expected namespace-name");
10354 namespace_decl = error_mark_node;
10357 return namespace_decl;
10360 /* Parse a namespace-definition.
10362 namespace-definition:
10363 named-namespace-definition
10364 unnamed-namespace-definition
10366 named-namespace-definition:
10367 original-namespace-definition
10368 extension-namespace-definition
10370 original-namespace-definition:
10371 namespace identifier { namespace-body }
10373 extension-namespace-definition:
10374 namespace original-namespace-name { namespace-body }
10376 unnamed-namespace-definition:
10377 namespace { namespace-body } */
10380 cp_parser_namespace_definition (cp_parser* parser)
10384 /* Look for the `namespace' keyword. */
10385 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10387 /* Get the name of the namespace. We do not attempt to distinguish
10388 between an original-namespace-definition and an
10389 extension-namespace-definition at this point. The semantic
10390 analysis routines are responsible for that. */
10391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10392 identifier = cp_parser_identifier (parser);
10394 identifier = NULL_TREE;
10396 /* Look for the `{' to start the namespace. */
10397 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10398 /* Start the namespace. */
10399 push_namespace (identifier);
10400 /* Parse the body of the namespace. */
10401 cp_parser_namespace_body (parser);
10402 /* Finish the namespace. */
10404 /* Look for the final `}'. */
10405 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10408 /* Parse a namespace-body.
10411 declaration-seq [opt] */
10414 cp_parser_namespace_body (cp_parser* parser)
10416 cp_parser_declaration_seq_opt (parser);
10419 /* Parse a namespace-alias-definition.
10421 namespace-alias-definition:
10422 namespace identifier = qualified-namespace-specifier ; */
10425 cp_parser_namespace_alias_definition (cp_parser* parser)
10428 tree namespace_specifier;
10430 /* Look for the `namespace' keyword. */
10431 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10432 /* Look for the identifier. */
10433 identifier = cp_parser_identifier (parser);
10434 if (identifier == error_mark_node)
10436 /* Look for the `=' token. */
10437 cp_parser_require (parser, CPP_EQ, "`='");
10438 /* Look for the qualified-namespace-specifier. */
10439 namespace_specifier
10440 = cp_parser_qualified_namespace_specifier (parser);
10441 /* Look for the `;' token. */
10442 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10444 /* Register the alias in the symbol table. */
10445 do_namespace_alias (identifier, namespace_specifier);
10448 /* Parse a qualified-namespace-specifier.
10450 qualified-namespace-specifier:
10451 :: [opt] nested-name-specifier [opt] namespace-name
10453 Returns a NAMESPACE_DECL corresponding to the specified
10457 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10459 /* Look for the optional `::'. */
10460 cp_parser_global_scope_opt (parser,
10461 /*current_scope_valid_p=*/false);
10463 /* Look for the optional nested-name-specifier. */
10464 cp_parser_nested_name_specifier_opt (parser,
10465 /*typename_keyword_p=*/false,
10466 /*check_dependency_p=*/true,
10468 /*is_declaration=*/true);
10470 return cp_parser_namespace_name (parser);
10473 /* Parse a using-declaration.
10476 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10477 using :: unqualified-id ; */
10480 cp_parser_using_declaration (cp_parser* parser)
10483 bool typename_p = false;
10484 bool global_scope_p;
10489 /* Look for the `using' keyword. */
10490 cp_parser_require_keyword (parser, RID_USING, "`using'");
10492 /* Peek at the next token. */
10493 token = cp_lexer_peek_token (parser->lexer);
10494 /* See if it's `typename'. */
10495 if (token->keyword == RID_TYPENAME)
10497 /* Remember that we've seen it. */
10499 /* Consume the `typename' token. */
10500 cp_lexer_consume_token (parser->lexer);
10503 /* Look for the optional global scope qualification. */
10505 = (cp_parser_global_scope_opt (parser,
10506 /*current_scope_valid_p=*/false)
10509 /* If we saw `typename', or didn't see `::', then there must be a
10510 nested-name-specifier present. */
10511 if (typename_p || !global_scope_p)
10512 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10513 /*check_dependency_p=*/true,
10515 /*is_declaration=*/true);
10516 /* Otherwise, we could be in either of the two productions. In that
10517 case, treat the nested-name-specifier as optional. */
10519 qscope = cp_parser_nested_name_specifier_opt (parser,
10520 /*typename_keyword_p=*/false,
10521 /*check_dependency_p=*/true,
10523 /*is_declaration=*/true);
10525 qscope = global_namespace;
10527 /* Parse the unqualified-id. */
10528 identifier = cp_parser_unqualified_id (parser,
10529 /*template_keyword_p=*/false,
10530 /*check_dependency_p=*/true,
10531 /*declarator_p=*/true);
10533 /* The function we call to handle a using-declaration is different
10534 depending on what scope we are in. */
10535 if (qscope == error_mark_node || identifier == error_mark_node)
10537 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10538 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10539 /* [namespace.udecl]
10541 A using declaration shall not name a template-id. */
10542 error ("a template-id may not appear in a using-declaration");
10545 if (at_class_scope_p ())
10547 /* Create the USING_DECL. */
10548 decl = do_class_using_decl (parser->scope, identifier);
10549 /* Add it to the list of members in this class. */
10550 finish_member_declaration (decl);
10554 decl = cp_parser_lookup_name_simple (parser, identifier);
10555 if (decl == error_mark_node)
10556 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10557 else if (!at_namespace_scope_p ())
10558 do_local_using_decl (decl, qscope, identifier);
10560 do_toplevel_using_decl (decl, qscope, identifier);
10564 /* Look for the final `;'. */
10565 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10568 /* Parse a using-directive.
10571 using namespace :: [opt] nested-name-specifier [opt]
10572 namespace-name ; */
10575 cp_parser_using_directive (cp_parser* parser)
10577 tree namespace_decl;
10580 /* Look for the `using' keyword. */
10581 cp_parser_require_keyword (parser, RID_USING, "`using'");
10582 /* And the `namespace' keyword. */
10583 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10584 /* Look for the optional `::' operator. */
10585 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10586 /* And the optional nested-name-specifier. */
10587 cp_parser_nested_name_specifier_opt (parser,
10588 /*typename_keyword_p=*/false,
10589 /*check_dependency_p=*/true,
10591 /*is_declaration=*/true);
10592 /* Get the namespace being used. */
10593 namespace_decl = cp_parser_namespace_name (parser);
10594 /* And any specified attributes. */
10595 attribs = cp_parser_attributes_opt (parser);
10596 /* Update the symbol table. */
10597 parse_using_directive (namespace_decl, attribs);
10598 /* Look for the final `;'. */
10599 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10602 /* Parse an asm-definition.
10605 asm ( string-literal ) ;
10610 asm volatile [opt] ( string-literal ) ;
10611 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10612 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10613 : asm-operand-list [opt] ) ;
10614 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10615 : asm-operand-list [opt]
10616 : asm-operand-list [opt] ) ; */
10619 cp_parser_asm_definition (cp_parser* parser)
10622 tree outputs = NULL_TREE;
10623 tree inputs = NULL_TREE;
10624 tree clobbers = NULL_TREE;
10626 bool volatile_p = false;
10627 bool extended_p = false;
10629 /* Look for the `asm' keyword. */
10630 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10631 /* See if the next token is `volatile'. */
10632 if (cp_parser_allow_gnu_extensions_p (parser)
10633 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10635 /* Remember that we saw the `volatile' keyword. */
10637 /* Consume the token. */
10638 cp_lexer_consume_token (parser->lexer);
10640 /* Look for the opening `('. */
10641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10643 /* Look for the string. */
10644 string = cp_parser_string_literal (parser, false, false);
10645 if (string == error_mark_node)
10647 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10648 /*consume_paren=*/true);
10652 /* If we're allowing GNU extensions, check for the extended assembly
10653 syntax. Unfortunately, the `:' tokens need not be separated by
10654 a space in C, and so, for compatibility, we tolerate that here
10655 too. Doing that means that we have to treat the `::' operator as
10657 if (cp_parser_allow_gnu_extensions_p (parser)
10658 && at_function_scope_p ()
10659 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10660 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10662 bool inputs_p = false;
10663 bool clobbers_p = false;
10665 /* The extended syntax was used. */
10668 /* Look for outputs. */
10669 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10671 /* Consume the `:'. */
10672 cp_lexer_consume_token (parser->lexer);
10673 /* Parse the output-operands. */
10674 if (cp_lexer_next_token_is_not (parser->lexer,
10676 && cp_lexer_next_token_is_not (parser->lexer,
10678 && cp_lexer_next_token_is_not (parser->lexer,
10680 outputs = cp_parser_asm_operand_list (parser);
10682 /* If the next token is `::', there are no outputs, and the
10683 next token is the beginning of the inputs. */
10684 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10685 /* The inputs are coming next. */
10688 /* Look for inputs. */
10690 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10692 /* Consume the `:' or `::'. */
10693 cp_lexer_consume_token (parser->lexer);
10694 /* Parse the output-operands. */
10695 if (cp_lexer_next_token_is_not (parser->lexer,
10697 && cp_lexer_next_token_is_not (parser->lexer,
10699 inputs = cp_parser_asm_operand_list (parser);
10701 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10702 /* The clobbers are coming next. */
10705 /* Look for clobbers. */
10707 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10709 /* Consume the `:' or `::'. */
10710 cp_lexer_consume_token (parser->lexer);
10711 /* Parse the clobbers. */
10712 if (cp_lexer_next_token_is_not (parser->lexer,
10714 clobbers = cp_parser_asm_clobber_list (parser);
10717 /* Look for the closing `)'. */
10718 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10719 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10720 /*consume_paren=*/true);
10721 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10723 /* Create the ASM_EXPR. */
10724 if (at_function_scope_p ())
10726 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10728 /* If the extended syntax was not used, mark the ASM_EXPR. */
10731 tree temp = asm_stmt;
10732 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10733 temp = TREE_OPERAND (temp, 0);
10735 ASM_INPUT_P (temp) = 1;
10739 assemble_asm (string);
10742 /* Declarators [gram.dcl.decl] */
10744 /* Parse an init-declarator.
10747 declarator initializer [opt]
10752 declarator asm-specification [opt] attributes [opt] initializer [opt]
10754 function-definition:
10755 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10757 decl-specifier-seq [opt] declarator function-try-block
10761 function-definition:
10762 __extension__ function-definition
10764 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10765 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10766 then this declarator appears in a class scope. The new DECL created
10767 by this declarator is returned.
10769 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10770 for a function-definition here as well. If the declarator is a
10771 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10772 be TRUE upon return. By that point, the function-definition will
10773 have been completely parsed.
10775 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10779 cp_parser_init_declarator (cp_parser* parser,
10780 cp_decl_specifier_seq *decl_specifiers,
10781 bool function_definition_allowed_p,
10783 int declares_class_or_enum,
10784 bool* function_definition_p)
10787 cp_declarator *declarator;
10788 tree prefix_attributes;
10790 tree asm_specification;
10792 tree decl = NULL_TREE;
10794 bool is_initialized;
10795 bool is_parenthesized_init;
10796 bool is_non_constant_init;
10797 int ctor_dtor_or_conv_p;
10799 tree pushed_scope = NULL;
10801 /* Gather the attributes that were provided with the
10802 decl-specifiers. */
10803 prefix_attributes = decl_specifiers->attributes;
10805 /* Assume that this is not the declarator for a function
10807 if (function_definition_p)
10808 *function_definition_p = false;
10810 /* Defer access checks while parsing the declarator; we cannot know
10811 what names are accessible until we know what is being
10813 resume_deferring_access_checks ();
10815 /* Parse the declarator. */
10817 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10818 &ctor_dtor_or_conv_p,
10819 /*parenthesized_p=*/NULL,
10820 /*member_p=*/false);
10821 /* Gather up the deferred checks. */
10822 stop_deferring_access_checks ();
10824 /* If the DECLARATOR was erroneous, there's no need to go
10826 if (declarator == cp_error_declarator)
10827 return error_mark_node;
10829 if (declares_class_or_enum & 2)
10830 cp_parser_check_for_definition_in_return_type (declarator,
10831 decl_specifiers->type);
10833 /* Figure out what scope the entity declared by the DECLARATOR is
10834 located in. `grokdeclarator' sometimes changes the scope, so
10835 we compute it now. */
10836 scope = get_scope_of_declarator (declarator);
10838 /* If we're allowing GNU extensions, look for an asm-specification
10840 if (cp_parser_allow_gnu_extensions_p (parser))
10842 /* Look for an asm-specification. */
10843 asm_specification = cp_parser_asm_specification_opt (parser);
10844 /* And attributes. */
10845 attributes = cp_parser_attributes_opt (parser);
10849 asm_specification = NULL_TREE;
10850 attributes = NULL_TREE;
10853 /* Peek at the next token. */
10854 token = cp_lexer_peek_token (parser->lexer);
10855 /* Check to see if the token indicates the start of a
10856 function-definition. */
10857 if (cp_parser_token_starts_function_definition_p (token))
10859 if (!function_definition_allowed_p)
10861 /* If a function-definition should not appear here, issue an
10863 cp_parser_error (parser,
10864 "a function-definition is not allowed here");
10865 return error_mark_node;
10869 /* Neither attributes nor an asm-specification are allowed
10870 on a function-definition. */
10871 if (asm_specification)
10872 error ("an asm-specification is not allowed on a function-definition");
10874 error ("attributes are not allowed on a function-definition");
10875 /* This is a function-definition. */
10876 *function_definition_p = true;
10878 /* Parse the function definition. */
10880 decl = cp_parser_save_member_function_body (parser,
10883 prefix_attributes);
10886 = (cp_parser_function_definition_from_specifiers_and_declarator
10887 (parser, decl_specifiers, prefix_attributes, declarator));
10895 Only in function declarations for constructors, destructors, and
10896 type conversions can the decl-specifier-seq be omitted.
10898 We explicitly postpone this check past the point where we handle
10899 function-definitions because we tolerate function-definitions
10900 that are missing their return types in some modes. */
10901 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10903 cp_parser_error (parser,
10904 "expected constructor, destructor, or type conversion");
10905 return error_mark_node;
10908 /* An `=' or an `(' indicates an initializer. */
10909 is_initialized = (token->type == CPP_EQ
10910 || token->type == CPP_OPEN_PAREN);
10911 /* If the init-declarator isn't initialized and isn't followed by a
10912 `,' or `;', it's not a valid init-declarator. */
10913 if (!is_initialized
10914 && token->type != CPP_COMMA
10915 && token->type != CPP_SEMICOLON)
10917 cp_parser_error (parser, "expected initializer");
10918 return error_mark_node;
10921 /* Because start_decl has side-effects, we should only call it if we
10922 know we're going ahead. By this point, we know that we cannot
10923 possibly be looking at any other construct. */
10924 cp_parser_commit_to_tentative_parse (parser);
10926 /* If the decl specifiers were bad, issue an error now that we're
10927 sure this was intended to be a declarator. Then continue
10928 declaring the variable(s), as int, to try to cut down on further
10930 if (decl_specifiers->any_specifiers_p
10931 && decl_specifiers->type == error_mark_node)
10933 cp_parser_error (parser, "invalid type in declaration");
10934 decl_specifiers->type = integer_type_node;
10937 /* Check to see whether or not this declaration is a friend. */
10938 friend_p = cp_parser_friend_p (decl_specifiers);
10940 /* Check that the number of template-parameter-lists is OK. */
10941 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10942 return error_mark_node;
10944 /* Enter the newly declared entry in the symbol table. If we're
10945 processing a declaration in a class-specifier, we wait until
10946 after processing the initializer. */
10949 if (parser->in_unbraced_linkage_specification_p)
10951 decl_specifiers->storage_class = sc_extern;
10952 have_extern_spec = false;
10954 decl = start_decl (declarator, decl_specifiers,
10955 is_initialized, attributes, prefix_attributes,
10959 /* Enter the SCOPE. That way unqualified names appearing in the
10960 initializer will be looked up in SCOPE. */
10961 pushed_scope = push_scope (scope);
10963 /* Perform deferred access control checks, now that we know in which
10964 SCOPE the declared entity resides. */
10965 if (!member_p && decl)
10967 tree saved_current_function_decl = NULL_TREE;
10969 /* If the entity being declared is a function, pretend that we
10970 are in its scope. If it is a `friend', it may have access to
10971 things that would not otherwise be accessible. */
10972 if (TREE_CODE (decl) == FUNCTION_DECL)
10974 saved_current_function_decl = current_function_decl;
10975 current_function_decl = decl;
10978 /* Perform the access control checks for the declarator and the
10979 the decl-specifiers. */
10980 perform_deferred_access_checks ();
10982 /* Restore the saved value. */
10983 if (TREE_CODE (decl) == FUNCTION_DECL)
10984 current_function_decl = saved_current_function_decl;
10987 /* Parse the initializer. */
10988 if (is_initialized)
10989 initializer = cp_parser_initializer (parser,
10990 &is_parenthesized_init,
10991 &is_non_constant_init);
10994 initializer = NULL_TREE;
10995 is_parenthesized_init = false;
10996 is_non_constant_init = true;
10999 /* The old parser allows attributes to appear after a parenthesized
11000 initializer. Mark Mitchell proposed removing this functionality
11001 on the GCC mailing lists on 2002-08-13. This parser accepts the
11002 attributes -- but ignores them. */
11003 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11004 if (cp_parser_attributes_opt (parser))
11005 warning (OPT_Wattributes,
11006 "attributes after parenthesized initializer ignored");
11008 /* For an in-class declaration, use `grokfield' to create the
11014 pop_scope (pushed_scope);
11015 pushed_scope = false;
11017 decl = grokfield (declarator, decl_specifiers,
11018 initializer, /*asmspec=*/NULL_TREE,
11019 prefix_attributes);
11020 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11021 cp_parser_save_default_args (parser, decl);
11024 /* Finish processing the declaration. But, skip friend
11026 if (!friend_p && decl && decl != error_mark_node)
11028 cp_finish_decl (decl,
11031 /* If the initializer is in parentheses, then this is
11032 a direct-initialization, which means that an
11033 `explicit' constructor is OK. Otherwise, an
11034 `explicit' constructor cannot be used. */
11035 ((is_parenthesized_init || !is_initialized)
11036 ? 0 : LOOKUP_ONLYCONVERTING));
11038 if (!friend_p && pushed_scope)
11039 pop_scope (pushed_scope);
11041 /* Remember whether or not variables were initialized by
11042 constant-expressions. */
11043 if (decl && TREE_CODE (decl) == VAR_DECL
11044 && is_initialized && !is_non_constant_init)
11045 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11050 /* Parse a declarator.
11054 ptr-operator declarator
11056 abstract-declarator:
11057 ptr-operator abstract-declarator [opt]
11058 direct-abstract-declarator
11063 attributes [opt] direct-declarator
11064 attributes [opt] ptr-operator declarator
11066 abstract-declarator:
11067 attributes [opt] ptr-operator abstract-declarator [opt]
11068 attributes [opt] direct-abstract-declarator
11070 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11071 detect constructor, destructor or conversion operators. It is set
11072 to -1 if the declarator is a name, and +1 if it is a
11073 function. Otherwise it is set to zero. Usually you just want to
11074 test for >0, but internally the negative value is used.
11076 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11077 a decl-specifier-seq unless it declares a constructor, destructor,
11078 or conversion. It might seem that we could check this condition in
11079 semantic analysis, rather than parsing, but that makes it difficult
11080 to handle something like `f()'. We want to notice that there are
11081 no decl-specifiers, and therefore realize that this is an
11082 expression, not a declaration.)
11084 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11085 the declarator is a direct-declarator of the form "(...)".
11087 MEMBER_P is true iff this declarator is a member-declarator. */
11089 static cp_declarator *
11090 cp_parser_declarator (cp_parser* parser,
11091 cp_parser_declarator_kind dcl_kind,
11092 int* ctor_dtor_or_conv_p,
11093 bool* parenthesized_p,
11097 cp_declarator *declarator;
11098 enum tree_code code;
11099 cp_cv_quals cv_quals;
11101 tree attributes = NULL_TREE;
11103 /* Assume this is not a constructor, destructor, or type-conversion
11105 if (ctor_dtor_or_conv_p)
11106 *ctor_dtor_or_conv_p = 0;
11108 if (cp_parser_allow_gnu_extensions_p (parser))
11109 attributes = cp_parser_attributes_opt (parser);
11111 /* Peek at the next token. */
11112 token = cp_lexer_peek_token (parser->lexer);
11114 /* Check for the ptr-operator production. */
11115 cp_parser_parse_tentatively (parser);
11116 /* Parse the ptr-operator. */
11117 code = cp_parser_ptr_operator (parser,
11120 /* If that worked, then we have a ptr-operator. */
11121 if (cp_parser_parse_definitely (parser))
11123 /* If a ptr-operator was found, then this declarator was not
11125 if (parenthesized_p)
11126 *parenthesized_p = true;
11127 /* The dependent declarator is optional if we are parsing an
11128 abstract-declarator. */
11129 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11130 cp_parser_parse_tentatively (parser);
11132 /* Parse the dependent declarator. */
11133 declarator = cp_parser_declarator (parser, dcl_kind,
11134 /*ctor_dtor_or_conv_p=*/NULL,
11135 /*parenthesized_p=*/NULL,
11136 /*member_p=*/false);
11138 /* If we are parsing an abstract-declarator, we must handle the
11139 case where the dependent declarator is absent. */
11140 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11141 && !cp_parser_parse_definitely (parser))
11144 /* Build the representation of the ptr-operator. */
11146 declarator = make_ptrmem_declarator (cv_quals,
11149 else if (code == INDIRECT_REF)
11150 declarator = make_pointer_declarator (cv_quals, declarator);
11152 declarator = make_reference_declarator (cv_quals, declarator);
11154 /* Everything else is a direct-declarator. */
11157 if (parenthesized_p)
11158 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11160 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11161 ctor_dtor_or_conv_p,
11165 if (attributes && declarator != cp_error_declarator)
11166 declarator->attributes = attributes;
11171 /* Parse a direct-declarator or direct-abstract-declarator.
11175 direct-declarator ( parameter-declaration-clause )
11176 cv-qualifier-seq [opt]
11177 exception-specification [opt]
11178 direct-declarator [ constant-expression [opt] ]
11181 direct-abstract-declarator:
11182 direct-abstract-declarator [opt]
11183 ( parameter-declaration-clause )
11184 cv-qualifier-seq [opt]
11185 exception-specification [opt]
11186 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11187 ( abstract-declarator )
11189 Returns a representation of the declarator. DCL_KIND is
11190 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11191 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11192 we are parsing a direct-declarator. It is
11193 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11194 of ambiguity we prefer an abstract declarator, as per
11195 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11196 cp_parser_declarator. */
11198 static cp_declarator *
11199 cp_parser_direct_declarator (cp_parser* parser,
11200 cp_parser_declarator_kind dcl_kind,
11201 int* ctor_dtor_or_conv_p,
11205 cp_declarator *declarator = NULL;
11206 tree scope = NULL_TREE;
11207 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11208 bool saved_in_declarator_p = parser->in_declarator_p;
11210 tree pushed_scope = NULL_TREE;
11214 /* Peek at the next token. */
11215 token = cp_lexer_peek_token (parser->lexer);
11216 if (token->type == CPP_OPEN_PAREN)
11218 /* This is either a parameter-declaration-clause, or a
11219 parenthesized declarator. When we know we are parsing a
11220 named declarator, it must be a parenthesized declarator
11221 if FIRST is true. For instance, `(int)' is a
11222 parameter-declaration-clause, with an omitted
11223 direct-abstract-declarator. But `((*))', is a
11224 parenthesized abstract declarator. Finally, when T is a
11225 template parameter `(T)' is a
11226 parameter-declaration-clause, and not a parenthesized
11229 We first try and parse a parameter-declaration-clause,
11230 and then try a nested declarator (if FIRST is true).
11232 It is not an error for it not to be a
11233 parameter-declaration-clause, even when FIRST is
11239 The first is the declaration of a function while the
11240 second is a the definition of a variable, including its
11243 Having seen only the parenthesis, we cannot know which of
11244 these two alternatives should be selected. Even more
11245 complex are examples like:
11250 The former is a function-declaration; the latter is a
11251 variable initialization.
11253 Thus again, we try a parameter-declaration-clause, and if
11254 that fails, we back out and return. */
11256 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11258 cp_parameter_declarator *params;
11259 unsigned saved_num_template_parameter_lists;
11261 /* In a member-declarator, the only valid interpretation
11262 of a parenthesis is the start of a
11263 parameter-declaration-clause. (It is invalid to
11264 initialize a static data member with a parenthesized
11265 initializer; only the "=" form of initialization is
11268 cp_parser_parse_tentatively (parser);
11270 /* Consume the `('. */
11271 cp_lexer_consume_token (parser->lexer);
11274 /* If this is going to be an abstract declarator, we're
11275 in a declarator and we can't have default args. */
11276 parser->default_arg_ok_p = false;
11277 parser->in_declarator_p = true;
11280 /* Inside the function parameter list, surrounding
11281 template-parameter-lists do not apply. */
11282 saved_num_template_parameter_lists
11283 = parser->num_template_parameter_lists;
11284 parser->num_template_parameter_lists = 0;
11286 /* Parse the parameter-declaration-clause. */
11287 params = cp_parser_parameter_declaration_clause (parser);
11289 parser->num_template_parameter_lists
11290 = saved_num_template_parameter_lists;
11292 /* If all went well, parse the cv-qualifier-seq and the
11293 exception-specification. */
11294 if (member_p || cp_parser_parse_definitely (parser))
11296 cp_cv_quals cv_quals;
11297 tree exception_specification;
11299 if (ctor_dtor_or_conv_p)
11300 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11302 /* Consume the `)'. */
11303 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11305 /* Parse the cv-qualifier-seq. */
11306 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11307 /* And the exception-specification. */
11308 exception_specification
11309 = cp_parser_exception_specification_opt (parser);
11311 /* Create the function-declarator. */
11312 declarator = make_call_declarator (declarator,
11315 exception_specification);
11316 /* Any subsequent parameter lists are to do with
11317 return type, so are not those of the declared
11319 parser->default_arg_ok_p = false;
11321 /* Repeat the main loop. */
11326 /* If this is the first, we can try a parenthesized
11330 bool saved_in_type_id_in_expr_p;
11332 parser->default_arg_ok_p = saved_default_arg_ok_p;
11333 parser->in_declarator_p = saved_in_declarator_p;
11335 /* Consume the `('. */
11336 cp_lexer_consume_token (parser->lexer);
11337 /* Parse the nested declarator. */
11338 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11339 parser->in_type_id_in_expr_p = true;
11341 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11342 /*parenthesized_p=*/NULL,
11344 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11346 /* Expect a `)'. */
11347 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11348 declarator = cp_error_declarator;
11349 if (declarator == cp_error_declarator)
11352 goto handle_declarator;
11354 /* Otherwise, we must be done. */
11358 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11359 && token->type == CPP_OPEN_SQUARE)
11361 /* Parse an array-declarator. */
11364 if (ctor_dtor_or_conv_p)
11365 *ctor_dtor_or_conv_p = 0;
11368 parser->default_arg_ok_p = false;
11369 parser->in_declarator_p = true;
11370 /* Consume the `['. */
11371 cp_lexer_consume_token (parser->lexer);
11372 /* Peek at the next token. */
11373 token = cp_lexer_peek_token (parser->lexer);
11374 /* If the next token is `]', then there is no
11375 constant-expression. */
11376 if (token->type != CPP_CLOSE_SQUARE)
11378 bool non_constant_p;
11381 = cp_parser_constant_expression (parser,
11382 /*allow_non_constant=*/true,
11384 if (!non_constant_p)
11385 bounds = fold_non_dependent_expr (bounds);
11386 /* Normally, the array bound must be an integral constant
11387 expression. However, as an extension, we allow VLAs
11388 in function scopes. */
11389 else if (!at_function_scope_p ())
11391 error ("array bound is not an integer constant");
11392 bounds = error_mark_node;
11396 bounds = NULL_TREE;
11397 /* Look for the closing `]'. */
11398 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11400 declarator = cp_error_declarator;
11404 declarator = make_array_declarator (declarator, bounds);
11406 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11408 tree qualifying_scope;
11409 tree unqualified_name;
11410 special_function_kind sfk;
11412 /* Parse a declarator-id */
11413 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11414 cp_parser_parse_tentatively (parser);
11415 unqualified_name = cp_parser_declarator_id (parser);
11416 qualifying_scope = parser->scope;
11417 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11419 if (!cp_parser_parse_definitely (parser))
11420 unqualified_name = error_mark_node;
11421 else if (qualifying_scope
11422 || (TREE_CODE (unqualified_name)
11423 != IDENTIFIER_NODE))
11425 cp_parser_error (parser, "expected unqualified-id");
11426 unqualified_name = error_mark_node;
11430 if (unqualified_name == error_mark_node)
11432 declarator = cp_error_declarator;
11436 if (qualifying_scope && at_namespace_scope_p ()
11437 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11439 /* In the declaration of a member of a template class
11440 outside of the class itself, the SCOPE will sometimes
11441 be a TYPENAME_TYPE. For example, given:
11443 template <typename T>
11444 int S<T>::R::i = 3;
11446 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11447 this context, we must resolve S<T>::R to an ordinary
11448 type, rather than a typename type.
11450 The reason we normally avoid resolving TYPENAME_TYPEs
11451 is that a specialization of `S' might render
11452 `S<T>::R' not a type. However, if `S' is
11453 specialized, then this `i' will not be used, so there
11454 is no harm in resolving the types here. */
11457 /* Resolve the TYPENAME_TYPE. */
11458 type = resolve_typename_type (qualifying_scope,
11459 /*only_current_p=*/false);
11460 /* If that failed, the declarator is invalid. */
11461 if (type == error_mark_node)
11462 error ("%<%T::%D%> is not a type",
11463 TYPE_CONTEXT (qualifying_scope),
11464 TYPE_IDENTIFIER (qualifying_scope));
11465 qualifying_scope = type;
11469 if (unqualified_name)
11473 if (qualifying_scope
11474 && CLASS_TYPE_P (qualifying_scope))
11475 class_type = qualifying_scope;
11477 class_type = current_class_type;
11479 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11481 if (qualifying_scope
11482 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11484 error ("invalid use of constructor as a template");
11485 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11486 "the constructor in a qualified name",
11488 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11489 class_type, class_type);
11490 declarator = cp_error_declarator;
11493 else if (class_type
11494 && same_type_p (TREE_TYPE (unqualified_name),
11496 unqualified_name = constructor_name (class_type);
11499 /* We do not attempt to print the declarator
11500 here because we do not have enough
11501 information about its original syntactic
11503 error ("invalid declarator");
11504 declarator = cp_error_declarator;
11511 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11512 sfk = sfk_destructor;
11513 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11514 sfk = sfk_conversion;
11515 else if (/* There's no way to declare a constructor
11516 for an anonymous type, even if the type
11517 got a name for linkage purposes. */
11518 !TYPE_WAS_ANONYMOUS (class_type)
11519 && constructor_name_p (unqualified_name,
11522 unqualified_name = constructor_name (class_type);
11523 sfk = sfk_constructor;
11526 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11527 *ctor_dtor_or_conv_p = -1;
11530 declarator = make_id_declarator (qualifying_scope,
11533 declarator->id_loc = token->location;
11535 handle_declarator:;
11536 scope = get_scope_of_declarator (declarator);
11538 /* Any names that appear after the declarator-id for a
11539 member are looked up in the containing scope. */
11540 pushed_scope = push_scope (scope);
11541 parser->in_declarator_p = true;
11542 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11543 || (declarator && declarator->kind == cdk_id))
11544 /* Default args are only allowed on function
11546 parser->default_arg_ok_p = saved_default_arg_ok_p;
11548 parser->default_arg_ok_p = false;
11557 /* For an abstract declarator, we might wind up with nothing at this
11558 point. That's an error; the declarator is not optional. */
11560 cp_parser_error (parser, "expected declarator");
11562 /* If we entered a scope, we must exit it now. */
11564 pop_scope (pushed_scope);
11566 parser->default_arg_ok_p = saved_default_arg_ok_p;
11567 parser->in_declarator_p = saved_in_declarator_p;
11572 /* Parse a ptr-operator.
11575 * cv-qualifier-seq [opt]
11577 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11582 & cv-qualifier-seq [opt]
11584 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11585 Returns ADDR_EXPR if a reference was used. In the case of a
11586 pointer-to-member, *TYPE is filled in with the TYPE containing the
11587 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11588 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11589 ERROR_MARK if an error occurred. */
11591 static enum tree_code
11592 cp_parser_ptr_operator (cp_parser* parser,
11594 cp_cv_quals *cv_quals)
11596 enum tree_code code = ERROR_MARK;
11599 /* Assume that it's not a pointer-to-member. */
11601 /* And that there are no cv-qualifiers. */
11602 *cv_quals = TYPE_UNQUALIFIED;
11604 /* Peek at the next token. */
11605 token = cp_lexer_peek_token (parser->lexer);
11606 /* If it's a `*' or `&' we have a pointer or reference. */
11607 if (token->type == CPP_MULT || token->type == CPP_AND)
11609 /* Remember which ptr-operator we were processing. */
11610 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11612 /* Consume the `*' or `&'. */
11613 cp_lexer_consume_token (parser->lexer);
11615 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11616 `&', if we are allowing GNU extensions. (The only qualifier
11617 that can legally appear after `&' is `restrict', but that is
11618 enforced during semantic analysis. */
11619 if (code == INDIRECT_REF
11620 || cp_parser_allow_gnu_extensions_p (parser))
11621 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11625 /* Try the pointer-to-member case. */
11626 cp_parser_parse_tentatively (parser);
11627 /* Look for the optional `::' operator. */
11628 cp_parser_global_scope_opt (parser,
11629 /*current_scope_valid_p=*/false);
11630 /* Look for the nested-name specifier. */
11631 cp_parser_nested_name_specifier (parser,
11632 /*typename_keyword_p=*/false,
11633 /*check_dependency_p=*/true,
11635 /*is_declaration=*/false);
11636 /* If we found it, and the next token is a `*', then we are
11637 indeed looking at a pointer-to-member operator. */
11638 if (!cp_parser_error_occurred (parser)
11639 && cp_parser_require (parser, CPP_MULT, "`*'"))
11641 /* The type of which the member is a member is given by the
11643 *type = parser->scope;
11644 /* The next name will not be qualified. */
11645 parser->scope = NULL_TREE;
11646 parser->qualifying_scope = NULL_TREE;
11647 parser->object_scope = NULL_TREE;
11648 /* Indicate that the `*' operator was used. */
11649 code = INDIRECT_REF;
11650 /* Look for the optional cv-qualifier-seq. */
11651 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11653 /* If that didn't work we don't have a ptr-operator. */
11654 if (!cp_parser_parse_definitely (parser))
11655 cp_parser_error (parser, "expected ptr-operator");
11661 /* Parse an (optional) cv-qualifier-seq.
11664 cv-qualifier cv-qualifier-seq [opt]
11675 Returns a bitmask representing the cv-qualifiers. */
11678 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11680 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11685 cp_cv_quals cv_qualifier;
11687 /* Peek at the next token. */
11688 token = cp_lexer_peek_token (parser->lexer);
11689 /* See if it's a cv-qualifier. */
11690 switch (token->keyword)
11693 cv_qualifier = TYPE_QUAL_CONST;
11697 cv_qualifier = TYPE_QUAL_VOLATILE;
11701 cv_qualifier = TYPE_QUAL_RESTRICT;
11705 cv_qualifier = TYPE_UNQUALIFIED;
11712 if (cv_quals & cv_qualifier)
11714 error ("duplicate cv-qualifier");
11715 cp_lexer_purge_token (parser->lexer);
11719 cp_lexer_consume_token (parser->lexer);
11720 cv_quals |= cv_qualifier;
11727 /* Parse a declarator-id.
11731 :: [opt] nested-name-specifier [opt] type-name
11733 In the `id-expression' case, the value returned is as for
11734 cp_parser_id_expression if the id-expression was an unqualified-id.
11735 If the id-expression was a qualified-id, then a SCOPE_REF is
11736 returned. The first operand is the scope (either a NAMESPACE_DECL
11737 or TREE_TYPE), but the second is still just a representation of an
11741 cp_parser_declarator_id (cp_parser* parser)
11744 /* The expression must be an id-expression. Assume that qualified
11745 names are the names of types so that:
11748 int S<T>::R::i = 3;
11750 will work; we must treat `S<T>::R' as the name of a type.
11751 Similarly, assume that qualified names are templates, where
11755 int S<T>::R<T>::i = 3;
11758 id = cp_parser_id_expression (parser,
11759 /*template_keyword_p=*/false,
11760 /*check_dependency_p=*/false,
11761 /*template_p=*/NULL,
11762 /*declarator_p=*/true);
11763 if (BASELINK_P (id))
11764 id = BASELINK_FUNCTIONS (id);
11768 /* Parse a type-id.
11771 type-specifier-seq abstract-declarator [opt]
11773 Returns the TYPE specified. */
11776 cp_parser_type_id (cp_parser* parser)
11778 cp_decl_specifier_seq type_specifier_seq;
11779 cp_declarator *abstract_declarator;
11781 /* Parse the type-specifier-seq. */
11782 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11783 &type_specifier_seq);
11784 if (type_specifier_seq.type == error_mark_node)
11785 return error_mark_node;
11787 /* There might or might not be an abstract declarator. */
11788 cp_parser_parse_tentatively (parser);
11789 /* Look for the declarator. */
11790 abstract_declarator
11791 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11792 /*parenthesized_p=*/NULL,
11793 /*member_p=*/false);
11794 /* Check to see if there really was a declarator. */
11795 if (!cp_parser_parse_definitely (parser))
11796 abstract_declarator = NULL;
11798 return groktypename (&type_specifier_seq, abstract_declarator);
11801 /* Parse a type-specifier-seq.
11803 type-specifier-seq:
11804 type-specifier type-specifier-seq [opt]
11808 type-specifier-seq:
11809 attributes type-specifier-seq [opt]
11811 If IS_CONDITION is true, we are at the start of a "condition",
11812 e.g., we've just seen "if (".
11814 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11817 cp_parser_type_specifier_seq (cp_parser* parser,
11819 cp_decl_specifier_seq *type_specifier_seq)
11821 bool seen_type_specifier = false;
11822 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11824 /* Clear the TYPE_SPECIFIER_SEQ. */
11825 clear_decl_specs (type_specifier_seq);
11827 /* Parse the type-specifiers and attributes. */
11830 tree type_specifier;
11831 bool is_cv_qualifier;
11833 /* Check for attributes first. */
11834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11836 type_specifier_seq->attributes =
11837 chainon (type_specifier_seq->attributes,
11838 cp_parser_attributes_opt (parser));
11842 /* Look for the type-specifier. */
11843 type_specifier = cp_parser_type_specifier (parser,
11845 type_specifier_seq,
11846 /*is_declaration=*/false,
11849 if (!type_specifier)
11851 /* If the first type-specifier could not be found, this is not a
11852 type-specifier-seq at all. */
11853 if (!seen_type_specifier)
11855 cp_parser_error (parser, "expected type-specifier");
11856 type_specifier_seq->type = error_mark_node;
11859 /* If subsequent type-specifiers could not be found, the
11860 type-specifier-seq is complete. */
11864 seen_type_specifier = true;
11865 /* The standard says that a condition can be:
11867 type-specifier-seq declarator = assignment-expression
11874 we should treat the "S" as a declarator, not as a
11875 type-specifier. The standard doesn't say that explicitly for
11876 type-specifier-seq, but it does say that for
11877 decl-specifier-seq in an ordinary declaration. Perhaps it
11878 would be clearer just to allow a decl-specifier-seq here, and
11879 then add a semantic restriction that if any decl-specifiers
11880 that are not type-specifiers appear, the program is invalid. */
11881 if (is_condition && !is_cv_qualifier)
11882 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11888 /* Parse a parameter-declaration-clause.
11890 parameter-declaration-clause:
11891 parameter-declaration-list [opt] ... [opt]
11892 parameter-declaration-list , ...
11894 Returns a representation for the parameter declarations. A return
11895 value of NULL indicates a parameter-declaration-clause consisting
11896 only of an ellipsis. */
11898 static cp_parameter_declarator *
11899 cp_parser_parameter_declaration_clause (cp_parser* parser)
11901 cp_parameter_declarator *parameters;
11906 /* Peek at the next token. */
11907 token = cp_lexer_peek_token (parser->lexer);
11908 /* Check for trivial parameter-declaration-clauses. */
11909 if (token->type == CPP_ELLIPSIS)
11911 /* Consume the `...' token. */
11912 cp_lexer_consume_token (parser->lexer);
11915 else if (token->type == CPP_CLOSE_PAREN)
11916 /* There are no parameters. */
11918 #ifndef NO_IMPLICIT_EXTERN_C
11919 if (in_system_header && current_class_type == NULL
11920 && current_lang_name == lang_name_c)
11924 return no_parameters;
11926 /* Check for `(void)', too, which is a special case. */
11927 else if (token->keyword == RID_VOID
11928 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11929 == CPP_CLOSE_PAREN))
11931 /* Consume the `void' token. */
11932 cp_lexer_consume_token (parser->lexer);
11933 /* There are no parameters. */
11934 return no_parameters;
11937 /* Parse the parameter-declaration-list. */
11938 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11939 /* If a parse error occurred while parsing the
11940 parameter-declaration-list, then the entire
11941 parameter-declaration-clause is erroneous. */
11945 /* Peek at the next token. */
11946 token = cp_lexer_peek_token (parser->lexer);
11947 /* If it's a `,', the clause should terminate with an ellipsis. */
11948 if (token->type == CPP_COMMA)
11950 /* Consume the `,'. */
11951 cp_lexer_consume_token (parser->lexer);
11952 /* Expect an ellipsis. */
11954 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11956 /* It might also be `...' if the optional trailing `,' was
11958 else if (token->type == CPP_ELLIPSIS)
11960 /* Consume the `...' token. */
11961 cp_lexer_consume_token (parser->lexer);
11962 /* And remember that we saw it. */
11966 ellipsis_p = false;
11968 /* Finish the parameter list. */
11969 if (parameters && ellipsis_p)
11970 parameters->ellipsis_p = true;
11975 /* Parse a parameter-declaration-list.
11977 parameter-declaration-list:
11978 parameter-declaration
11979 parameter-declaration-list , parameter-declaration
11981 Returns a representation of the parameter-declaration-list, as for
11982 cp_parser_parameter_declaration_clause. However, the
11983 `void_list_node' is never appended to the list. Upon return,
11984 *IS_ERROR will be true iff an error occurred. */
11986 static cp_parameter_declarator *
11987 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11989 cp_parameter_declarator *parameters = NULL;
11990 cp_parameter_declarator **tail = ¶meters;
11992 /* Assume all will go well. */
11995 /* Look for more parameters. */
11998 cp_parameter_declarator *parameter;
11999 bool parenthesized_p;
12000 /* Parse the parameter. */
12002 = cp_parser_parameter_declaration (parser,
12003 /*template_parm_p=*/false,
12006 /* If a parse error occurred parsing the parameter declaration,
12007 then the entire parameter-declaration-list is erroneous. */
12014 /* Add the new parameter to the list. */
12016 tail = ¶meter->next;
12018 /* Peek at the next token. */
12019 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12020 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12021 /* These are for Objective-C++ */
12022 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12023 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12024 /* The parameter-declaration-list is complete. */
12026 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12030 /* Peek at the next token. */
12031 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12032 /* If it's an ellipsis, then the list is complete. */
12033 if (token->type == CPP_ELLIPSIS)
12035 /* Otherwise, there must be more parameters. Consume the
12037 cp_lexer_consume_token (parser->lexer);
12038 /* When parsing something like:
12040 int i(float f, double d)
12042 we can tell after seeing the declaration for "f" that we
12043 are not looking at an initialization of a variable "i",
12044 but rather at the declaration of a function "i".
12046 Due to the fact that the parsing of template arguments
12047 (as specified to a template-id) requires backtracking we
12048 cannot use this technique when inside a template argument
12050 if (!parser->in_template_argument_list_p
12051 && !parser->in_type_id_in_expr_p
12052 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12053 /* However, a parameter-declaration of the form
12054 "foat(f)" (which is a valid declaration of a
12055 parameter "f") can also be interpreted as an
12056 expression (the conversion of "f" to "float"). */
12057 && !parenthesized_p)
12058 cp_parser_commit_to_tentative_parse (parser);
12062 cp_parser_error (parser, "expected %<,%> or %<...%>");
12063 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12064 cp_parser_skip_to_closing_parenthesis (parser,
12065 /*recovering=*/true,
12066 /*or_comma=*/false,
12067 /*consume_paren=*/false);
12075 /* Parse a parameter declaration.
12077 parameter-declaration:
12078 decl-specifier-seq declarator
12079 decl-specifier-seq declarator = assignment-expression
12080 decl-specifier-seq abstract-declarator [opt]
12081 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12083 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12084 declares a template parameter. (In that case, a non-nested `>'
12085 token encountered during the parsing of the assignment-expression
12086 is not interpreted as a greater-than operator.)
12088 Returns a representation of the parameter, or NULL if an error
12089 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12090 true iff the declarator is of the form "(p)". */
12092 static cp_parameter_declarator *
12093 cp_parser_parameter_declaration (cp_parser *parser,
12094 bool template_parm_p,
12095 bool *parenthesized_p)
12097 int declares_class_or_enum;
12098 bool greater_than_is_operator_p;
12099 cp_decl_specifier_seq decl_specifiers;
12100 cp_declarator *declarator;
12101 tree default_argument;
12103 const char *saved_message;
12105 /* In a template parameter, `>' is not an operator.
12109 When parsing a default template-argument for a non-type
12110 template-parameter, the first non-nested `>' is taken as the end
12111 of the template parameter-list rather than a greater-than
12113 greater_than_is_operator_p = !template_parm_p;
12115 /* Type definitions may not appear in parameter types. */
12116 saved_message = parser->type_definition_forbidden_message;
12117 parser->type_definition_forbidden_message
12118 = "types may not be defined in parameter types";
12120 /* Parse the declaration-specifiers. */
12121 cp_parser_decl_specifier_seq (parser,
12122 CP_PARSER_FLAGS_NONE,
12124 &declares_class_or_enum);
12125 /* If an error occurred, there's no reason to attempt to parse the
12126 rest of the declaration. */
12127 if (cp_parser_error_occurred (parser))
12129 parser->type_definition_forbidden_message = saved_message;
12133 /* Peek at the next token. */
12134 token = cp_lexer_peek_token (parser->lexer);
12135 /* If the next token is a `)', `,', `=', `>', or `...', then there
12136 is no declarator. */
12137 if (token->type == CPP_CLOSE_PAREN
12138 || token->type == CPP_COMMA
12139 || token->type == CPP_EQ
12140 || token->type == CPP_ELLIPSIS
12141 || token->type == CPP_GREATER)
12144 if (parenthesized_p)
12145 *parenthesized_p = false;
12147 /* Otherwise, there should be a declarator. */
12150 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12151 parser->default_arg_ok_p = false;
12153 /* After seeing a decl-specifier-seq, if the next token is not a
12154 "(", there is no possibility that the code is a valid
12155 expression. Therefore, if parsing tentatively, we commit at
12157 if (!parser->in_template_argument_list_p
12158 /* In an expression context, having seen:
12162 we cannot be sure whether we are looking at a
12163 function-type (taking a "char" as a parameter) or a cast
12164 of some object of type "char" to "int". */
12165 && !parser->in_type_id_in_expr_p
12166 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12167 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12168 cp_parser_commit_to_tentative_parse (parser);
12169 /* Parse the declarator. */
12170 declarator = cp_parser_declarator (parser,
12171 CP_PARSER_DECLARATOR_EITHER,
12172 /*ctor_dtor_or_conv_p=*/NULL,
12174 /*member_p=*/false);
12175 parser->default_arg_ok_p = saved_default_arg_ok_p;
12176 /* After the declarator, allow more attributes. */
12177 decl_specifiers.attributes
12178 = chainon (decl_specifiers.attributes,
12179 cp_parser_attributes_opt (parser));
12182 /* The restriction on defining new types applies only to the type
12183 of the parameter, not to the default argument. */
12184 parser->type_definition_forbidden_message = saved_message;
12186 /* If the next token is `=', then process a default argument. */
12187 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12189 bool saved_greater_than_is_operator_p;
12190 /* Consume the `='. */
12191 cp_lexer_consume_token (parser->lexer);
12193 /* If we are defining a class, then the tokens that make up the
12194 default argument must be saved and processed later. */
12195 if (!template_parm_p && at_class_scope_p ()
12196 && TYPE_BEING_DEFINED (current_class_type))
12198 unsigned depth = 0;
12199 cp_token *first_token;
12202 /* Add tokens until we have processed the entire default
12203 argument. We add the range [first_token, token). */
12204 first_token = cp_lexer_peek_token (parser->lexer);
12209 /* Peek at the next token. */
12210 token = cp_lexer_peek_token (parser->lexer);
12211 /* What we do depends on what token we have. */
12212 switch (token->type)
12214 /* In valid code, a default argument must be
12215 immediately followed by a `,' `)', or `...'. */
12217 case CPP_CLOSE_PAREN:
12219 /* If we run into a non-nested `;', `}', or `]',
12220 then the code is invalid -- but the default
12221 argument is certainly over. */
12222 case CPP_SEMICOLON:
12223 case CPP_CLOSE_BRACE:
12224 case CPP_CLOSE_SQUARE:
12227 /* Update DEPTH, if necessary. */
12228 else if (token->type == CPP_CLOSE_PAREN
12229 || token->type == CPP_CLOSE_BRACE
12230 || token->type == CPP_CLOSE_SQUARE)
12234 case CPP_OPEN_PAREN:
12235 case CPP_OPEN_SQUARE:
12236 case CPP_OPEN_BRACE:
12241 /* If we see a non-nested `>', and `>' is not an
12242 operator, then it marks the end of the default
12244 if (!depth && !greater_than_is_operator_p)
12248 /* If we run out of tokens, issue an error message. */
12250 case CPP_PRAGMA_EOL:
12251 error ("file ends in default argument");
12257 /* In these cases, we should look for template-ids.
12258 For example, if the default argument is
12259 `X<int, double>()', we need to do name lookup to
12260 figure out whether or not `X' is a template; if
12261 so, the `,' does not end the default argument.
12263 That is not yet done. */
12270 /* If we've reached the end, stop. */
12274 /* Add the token to the token block. */
12275 token = cp_lexer_consume_token (parser->lexer);
12278 /* Create a DEFAULT_ARG to represented the unparsed default
12280 default_argument = make_node (DEFAULT_ARG);
12281 DEFARG_TOKENS (default_argument)
12282 = cp_token_cache_new (first_token, token);
12283 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12285 /* Outside of a class definition, we can just parse the
12286 assignment-expression. */
12289 bool saved_local_variables_forbidden_p;
12291 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12293 saved_greater_than_is_operator_p
12294 = parser->greater_than_is_operator_p;
12295 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12296 /* Local variable names (and the `this' keyword) may not
12297 appear in a default argument. */
12298 saved_local_variables_forbidden_p
12299 = parser->local_variables_forbidden_p;
12300 parser->local_variables_forbidden_p = true;
12301 /* Parse the assignment-expression. */
12303 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12304 /* Restore saved state. */
12305 parser->greater_than_is_operator_p
12306 = saved_greater_than_is_operator_p;
12307 parser->local_variables_forbidden_p
12308 = saved_local_variables_forbidden_p;
12310 if (!parser->default_arg_ok_p)
12312 if (!flag_pedantic_errors)
12313 warning (0, "deprecated use of default argument for parameter of non-function");
12316 error ("default arguments are only permitted for function parameters");
12317 default_argument = NULL_TREE;
12322 default_argument = NULL_TREE;
12324 return make_parameter_declarator (&decl_specifiers,
12329 /* Parse a function-body.
12332 compound_statement */
12335 cp_parser_function_body (cp_parser *parser)
12337 cp_parser_compound_statement (parser, NULL, false);
12340 /* Parse a ctor-initializer-opt followed by a function-body. Return
12341 true if a ctor-initializer was present. */
12344 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12347 bool ctor_initializer_p;
12349 /* Begin the function body. */
12350 body = begin_function_body ();
12351 /* Parse the optional ctor-initializer. */
12352 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12353 /* Parse the function-body. */
12354 cp_parser_function_body (parser);
12355 /* Finish the function body. */
12356 finish_function_body (body);
12358 return ctor_initializer_p;
12361 /* Parse an initializer.
12364 = initializer-clause
12365 ( expression-list )
12367 Returns an expression representing the initializer. If no
12368 initializer is present, NULL_TREE is returned.
12370 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12371 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12372 set to FALSE if there is no initializer present. If there is an
12373 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12374 is set to true; otherwise it is set to false. */
12377 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12378 bool* non_constant_p)
12383 /* Peek at the next token. */
12384 token = cp_lexer_peek_token (parser->lexer);
12386 /* Let our caller know whether or not this initializer was
12388 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12389 /* Assume that the initializer is constant. */
12390 *non_constant_p = false;
12392 if (token->type == CPP_EQ)
12394 /* Consume the `='. */
12395 cp_lexer_consume_token (parser->lexer);
12396 /* Parse the initializer-clause. */
12397 init = cp_parser_initializer_clause (parser, non_constant_p);
12399 else if (token->type == CPP_OPEN_PAREN)
12400 init = cp_parser_parenthesized_expression_list (parser, false,
12405 /* Anything else is an error. */
12406 cp_parser_error (parser, "expected initializer");
12407 init = error_mark_node;
12413 /* Parse an initializer-clause.
12415 initializer-clause:
12416 assignment-expression
12417 { initializer-list , [opt] }
12420 Returns an expression representing the initializer.
12422 If the `assignment-expression' production is used the value
12423 returned is simply a representation for the expression.
12425 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12426 the elements of the initializer-list (or NULL, if the last
12427 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12428 NULL_TREE. There is no way to detect whether or not the optional
12429 trailing `,' was provided. NON_CONSTANT_P is as for
12430 cp_parser_initializer. */
12433 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12437 /* Assume the expression is constant. */
12438 *non_constant_p = false;
12440 /* If it is not a `{', then we are looking at an
12441 assignment-expression. */
12442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12445 = cp_parser_constant_expression (parser,
12446 /*allow_non_constant_p=*/true,
12448 if (!*non_constant_p)
12449 initializer = fold_non_dependent_expr (initializer);
12453 /* Consume the `{' token. */
12454 cp_lexer_consume_token (parser->lexer);
12455 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12456 initializer = make_node (CONSTRUCTOR);
12457 /* If it's not a `}', then there is a non-trivial initializer. */
12458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12460 /* Parse the initializer list. */
12461 CONSTRUCTOR_ELTS (initializer)
12462 = cp_parser_initializer_list (parser, non_constant_p);
12463 /* A trailing `,' token is allowed. */
12464 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12465 cp_lexer_consume_token (parser->lexer);
12467 /* Now, there should be a trailing `}'. */
12468 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12471 return initializer;
12474 /* Parse an initializer-list.
12478 initializer-list , initializer-clause
12483 identifier : initializer-clause
12484 initializer-list, identifier : initializer-clause
12486 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12487 for the initializer. If the INDEX of the elt is non-NULL, it is the
12488 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12489 as for cp_parser_initializer. */
12491 static VEC(constructor_elt,gc) *
12492 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12494 VEC(constructor_elt,gc) *v = NULL;
12496 /* Assume all of the expressions are constant. */
12497 *non_constant_p = false;
12499 /* Parse the rest of the list. */
12505 bool clause_non_constant_p;
12507 /* If the next token is an identifier and the following one is a
12508 colon, we are looking at the GNU designated-initializer
12510 if (cp_parser_allow_gnu_extensions_p (parser)
12511 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12514 /* Consume the identifier. */
12515 identifier = cp_lexer_consume_token (parser->lexer)->value;
12516 /* Consume the `:'. */
12517 cp_lexer_consume_token (parser->lexer);
12520 identifier = NULL_TREE;
12522 /* Parse the initializer. */
12523 initializer = cp_parser_initializer_clause (parser,
12524 &clause_non_constant_p);
12525 /* If any clause is non-constant, so is the entire initializer. */
12526 if (clause_non_constant_p)
12527 *non_constant_p = true;
12529 /* Add it to the vector. */
12530 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12532 /* If the next token is not a comma, we have reached the end of
12534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12537 /* Peek at the next token. */
12538 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12539 /* If the next token is a `}', then we're still done. An
12540 initializer-clause can have a trailing `,' after the
12541 initializer-list and before the closing `}'. */
12542 if (token->type == CPP_CLOSE_BRACE)
12545 /* Consume the `,' token. */
12546 cp_lexer_consume_token (parser->lexer);
12552 /* Classes [gram.class] */
12554 /* Parse a class-name.
12560 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12561 to indicate that names looked up in dependent types should be
12562 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12563 keyword has been used to indicate that the name that appears next
12564 is a template. TAG_TYPE indicates the explicit tag given before
12565 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12566 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12567 is the class being defined in a class-head.
12569 Returns the TYPE_DECL representing the class. */
12572 cp_parser_class_name (cp_parser *parser,
12573 bool typename_keyword_p,
12574 bool template_keyword_p,
12575 enum tag_types tag_type,
12576 bool check_dependency_p,
12578 bool is_declaration)
12585 /* All class-names start with an identifier. */
12586 token = cp_lexer_peek_token (parser->lexer);
12587 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12589 cp_parser_error (parser, "expected class-name");
12590 return error_mark_node;
12593 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12594 to a template-id, so we save it here. */
12595 scope = parser->scope;
12596 if (scope == error_mark_node)
12597 return error_mark_node;
12599 /* Any name names a type if we're following the `typename' keyword
12600 in a qualified name where the enclosing scope is type-dependent. */
12601 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12602 && dependent_type_p (scope));
12603 /* Handle the common case (an identifier, but not a template-id)
12605 if (token->type == CPP_NAME
12606 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12608 cp_token *identifier_token;
12612 /* Look for the identifier. */
12613 identifier_token = cp_lexer_peek_token (parser->lexer);
12614 ambiguous_p = identifier_token->ambiguous_p;
12615 identifier = cp_parser_identifier (parser);
12616 /* If the next token isn't an identifier, we are certainly not
12617 looking at a class-name. */
12618 if (identifier == error_mark_node)
12619 decl = error_mark_node;
12620 /* If we know this is a type-name, there's no need to look it
12622 else if (typename_p)
12626 tree ambiguous_decls;
12627 /* If we already know that this lookup is ambiguous, then
12628 we've already issued an error message; there's no reason
12632 cp_parser_simulate_error (parser);
12633 return error_mark_node;
12635 /* If the next token is a `::', then the name must be a type
12638 [basic.lookup.qual]
12640 During the lookup for a name preceding the :: scope
12641 resolution operator, object, function, and enumerator
12642 names are ignored. */
12643 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12644 tag_type = typename_type;
12645 /* Look up the name. */
12646 decl = cp_parser_lookup_name (parser, identifier,
12648 /*is_template=*/false,
12649 /*is_namespace=*/false,
12650 check_dependency_p,
12652 if (ambiguous_decls)
12654 error ("reference to %qD is ambiguous", identifier);
12655 print_candidates (ambiguous_decls);
12656 if (cp_parser_parsing_tentatively (parser))
12658 identifier_token->ambiguous_p = true;
12659 cp_parser_simulate_error (parser);
12661 return error_mark_node;
12667 /* Try a template-id. */
12668 decl = cp_parser_template_id (parser, template_keyword_p,
12669 check_dependency_p,
12671 if (decl == error_mark_node)
12672 return error_mark_node;
12675 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12677 /* If this is a typename, create a TYPENAME_TYPE. */
12678 if (typename_p && decl != error_mark_node)
12680 decl = make_typename_type (scope, decl, typename_type,
12681 /*complain=*/tf_error);
12682 if (decl != error_mark_node)
12683 decl = TYPE_NAME (decl);
12686 /* Check to see that it is really the name of a class. */
12687 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12688 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12689 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12690 /* Situations like this:
12692 template <typename T> struct A {
12693 typename T::template X<int>::I i;
12696 are problematic. Is `T::template X<int>' a class-name? The
12697 standard does not seem to be definitive, but there is no other
12698 valid interpretation of the following `::'. Therefore, those
12699 names are considered class-names. */
12700 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12701 else if (decl == error_mark_node
12702 || TREE_CODE (decl) != TYPE_DECL
12703 || TREE_TYPE (decl) == error_mark_node
12704 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12706 cp_parser_error (parser, "expected class-name");
12707 return error_mark_node;
12713 /* Parse a class-specifier.
12716 class-head { member-specification [opt] }
12718 Returns the TREE_TYPE representing the class. */
12721 cp_parser_class_specifier (cp_parser* parser)
12725 tree attributes = NULL_TREE;
12726 int has_trailing_semicolon;
12727 bool nested_name_specifier_p;
12728 unsigned saved_num_template_parameter_lists;
12729 tree old_scope = NULL_TREE;
12730 tree scope = NULL_TREE;
12732 push_deferring_access_checks (dk_no_deferred);
12734 /* Parse the class-head. */
12735 type = cp_parser_class_head (parser,
12736 &nested_name_specifier_p,
12738 /* If the class-head was a semantic disaster, skip the entire body
12742 cp_parser_skip_to_end_of_block_or_statement (parser);
12743 pop_deferring_access_checks ();
12744 return error_mark_node;
12747 /* Look for the `{'. */
12748 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12750 pop_deferring_access_checks ();
12751 return error_mark_node;
12754 /* Issue an error message if type-definitions are forbidden here. */
12755 cp_parser_check_type_definition (parser);
12756 /* Remember that we are defining one more class. */
12757 ++parser->num_classes_being_defined;
12758 /* Inside the class, surrounding template-parameter-lists do not
12760 saved_num_template_parameter_lists
12761 = parser->num_template_parameter_lists;
12762 parser->num_template_parameter_lists = 0;
12764 /* Start the class. */
12765 if (nested_name_specifier_p)
12767 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12768 old_scope = push_inner_scope (scope);
12770 type = begin_class_definition (type);
12772 if (type == error_mark_node)
12773 /* If the type is erroneous, skip the entire body of the class. */
12774 cp_parser_skip_to_closing_brace (parser);
12776 /* Parse the member-specification. */
12777 cp_parser_member_specification_opt (parser);
12779 /* Look for the trailing `}'. */
12780 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12781 /* We get better error messages by noticing a common problem: a
12782 missing trailing `;'. */
12783 token = cp_lexer_peek_token (parser->lexer);
12784 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12785 /* Look for trailing attributes to apply to this class. */
12786 if (cp_parser_allow_gnu_extensions_p (parser))
12788 tree sub_attr = cp_parser_attributes_opt (parser);
12789 attributes = chainon (attributes, sub_attr);
12791 if (type != error_mark_node)
12792 type = finish_struct (type, attributes);
12793 if (nested_name_specifier_p)
12794 pop_inner_scope (old_scope, scope);
12795 /* If this class is not itself within the scope of another class,
12796 then we need to parse the bodies of all of the queued function
12797 definitions. Note that the queued functions defined in a class
12798 are not always processed immediately following the
12799 class-specifier for that class. Consider:
12802 struct B { void f() { sizeof (A); } };
12805 If `f' were processed before the processing of `A' were
12806 completed, there would be no way to compute the size of `A'.
12807 Note that the nesting we are interested in here is lexical --
12808 not the semantic nesting given by TYPE_CONTEXT. In particular,
12811 struct A { struct B; };
12812 struct A::B { void f() { } };
12814 there is no need to delay the parsing of `A::B::f'. */
12815 if (--parser->num_classes_being_defined == 0)
12819 tree class_type = NULL_TREE;
12820 tree pushed_scope = NULL_TREE;
12822 /* In a first pass, parse default arguments to the functions.
12823 Then, in a second pass, parse the bodies of the functions.
12824 This two-phased approach handles cases like:
12832 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12833 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12834 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12835 TREE_PURPOSE (parser->unparsed_functions_queues)
12836 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12838 fn = TREE_VALUE (queue_entry);
12839 /* If there are default arguments that have not yet been processed,
12840 take care of them now. */
12841 if (class_type != TREE_PURPOSE (queue_entry))
12844 pop_scope (pushed_scope);
12845 class_type = TREE_PURPOSE (queue_entry);
12846 pushed_scope = push_scope (class_type);
12848 /* Make sure that any template parameters are in scope. */
12849 maybe_begin_member_template_processing (fn);
12850 /* Parse the default argument expressions. */
12851 cp_parser_late_parsing_default_args (parser, fn);
12852 /* Remove any template parameters from the symbol table. */
12853 maybe_end_member_template_processing ();
12856 pop_scope (pushed_scope);
12857 /* Now parse the body of the functions. */
12858 for (TREE_VALUE (parser->unparsed_functions_queues)
12859 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12860 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12861 TREE_VALUE (parser->unparsed_functions_queues)
12862 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12864 /* Figure out which function we need to process. */
12865 fn = TREE_VALUE (queue_entry);
12866 /* Parse the function. */
12867 cp_parser_late_parsing_for_member (parser, fn);
12871 /* Put back any saved access checks. */
12872 pop_deferring_access_checks ();
12874 /* Restore the count of active template-parameter-lists. */
12875 parser->num_template_parameter_lists
12876 = saved_num_template_parameter_lists;
12881 /* Parse a class-head.
12884 class-key identifier [opt] base-clause [opt]
12885 class-key nested-name-specifier identifier base-clause [opt]
12886 class-key nested-name-specifier [opt] template-id
12890 class-key attributes identifier [opt] base-clause [opt]
12891 class-key attributes nested-name-specifier identifier base-clause [opt]
12892 class-key attributes nested-name-specifier [opt] template-id
12895 Returns the TYPE of the indicated class. Sets
12896 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12897 involving a nested-name-specifier was used, and FALSE otherwise.
12899 Returns error_mark_node if this is not a class-head.
12901 Returns NULL_TREE if the class-head is syntactically valid, but
12902 semantically invalid in a way that means we should skip the entire
12903 body of the class. */
12906 cp_parser_class_head (cp_parser* parser,
12907 bool* nested_name_specifier_p,
12908 tree *attributes_p)
12910 tree nested_name_specifier;
12911 enum tag_types class_key;
12912 tree id = NULL_TREE;
12913 tree type = NULL_TREE;
12915 bool template_id_p = false;
12916 bool qualified_p = false;
12917 bool invalid_nested_name_p = false;
12918 bool invalid_explicit_specialization_p = false;
12919 tree pushed_scope = NULL_TREE;
12920 unsigned num_templates;
12923 /* Assume no nested-name-specifier will be present. */
12924 *nested_name_specifier_p = false;
12925 /* Assume no template parameter lists will be used in defining the
12929 /* Look for the class-key. */
12930 class_key = cp_parser_class_key (parser);
12931 if (class_key == none_type)
12932 return error_mark_node;
12934 /* Parse the attributes. */
12935 attributes = cp_parser_attributes_opt (parser);
12937 /* If the next token is `::', that is invalid -- but sometimes
12938 people do try to write:
12942 Handle this gracefully by accepting the extra qualifier, and then
12943 issuing an error about it later if this really is a
12944 class-head. If it turns out just to be an elaborated type
12945 specifier, remain silent. */
12946 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12947 qualified_p = true;
12949 push_deferring_access_checks (dk_no_check);
12951 /* Determine the name of the class. Begin by looking for an
12952 optional nested-name-specifier. */
12953 nested_name_specifier
12954 = cp_parser_nested_name_specifier_opt (parser,
12955 /*typename_keyword_p=*/false,
12956 /*check_dependency_p=*/false,
12958 /*is_declaration=*/false);
12959 /* If there was a nested-name-specifier, then there *must* be an
12961 if (nested_name_specifier)
12963 /* Although the grammar says `identifier', it really means
12964 `class-name' or `template-name'. You are only allowed to
12965 define a class that has already been declared with this
12968 The proposed resolution for Core Issue 180 says that whever
12969 you see `class T::X' you should treat `X' as a type-name.
12971 It is OK to define an inaccessible class; for example:
12973 class A { class B; };
12976 We do not know if we will see a class-name, or a
12977 template-name. We look for a class-name first, in case the
12978 class-name is a template-id; if we looked for the
12979 template-name first we would stop after the template-name. */
12980 cp_parser_parse_tentatively (parser);
12981 type = cp_parser_class_name (parser,
12982 /*typename_keyword_p=*/false,
12983 /*template_keyword_p=*/false,
12985 /*check_dependency_p=*/false,
12986 /*class_head_p=*/true,
12987 /*is_declaration=*/false);
12988 /* If that didn't work, ignore the nested-name-specifier. */
12989 if (!cp_parser_parse_definitely (parser))
12991 invalid_nested_name_p = true;
12992 id = cp_parser_identifier (parser);
12993 if (id == error_mark_node)
12996 /* If we could not find a corresponding TYPE, treat this
12997 declaration like an unqualified declaration. */
12998 if (type == error_mark_node)
12999 nested_name_specifier = NULL_TREE;
13000 /* Otherwise, count the number of templates used in TYPE and its
13001 containing scopes. */
13006 for (scope = TREE_TYPE (type);
13007 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13008 scope = (TYPE_P (scope)
13009 ? TYPE_CONTEXT (scope)
13010 : DECL_CONTEXT (scope)))
13012 && CLASS_TYPE_P (scope)
13013 && CLASSTYPE_TEMPLATE_INFO (scope)
13014 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13015 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13019 /* Otherwise, the identifier is optional. */
13022 /* We don't know whether what comes next is a template-id,
13023 an identifier, or nothing at all. */
13024 cp_parser_parse_tentatively (parser);
13025 /* Check for a template-id. */
13026 id = cp_parser_template_id (parser,
13027 /*template_keyword_p=*/false,
13028 /*check_dependency_p=*/true,
13029 /*is_declaration=*/true);
13030 /* If that didn't work, it could still be an identifier. */
13031 if (!cp_parser_parse_definitely (parser))
13033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13034 id = cp_parser_identifier (parser);
13040 template_id_p = true;
13045 pop_deferring_access_checks ();
13048 cp_parser_check_for_invalid_template_id (parser, id);
13050 /* If it's not a `:' or a `{' then we can't really be looking at a
13051 class-head, since a class-head only appears as part of a
13052 class-specifier. We have to detect this situation before calling
13053 xref_tag, since that has irreversible side-effects. */
13054 if (!cp_parser_next_token_starts_class_definition_p (parser))
13056 cp_parser_error (parser, "expected %<{%> or %<:%>");
13057 return error_mark_node;
13060 /* At this point, we're going ahead with the class-specifier, even
13061 if some other problem occurs. */
13062 cp_parser_commit_to_tentative_parse (parser);
13063 /* Issue the error about the overly-qualified name now. */
13065 cp_parser_error (parser,
13066 "global qualification of class name is invalid");
13067 else if (invalid_nested_name_p)
13068 cp_parser_error (parser,
13069 "qualified name does not name a class");
13070 else if (nested_name_specifier)
13074 /* Reject typedef-names in class heads. */
13075 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13077 error ("invalid class name in declaration of %qD", type);
13082 /* Figure out in what scope the declaration is being placed. */
13083 scope = current_scope ();
13084 /* If that scope does not contain the scope in which the
13085 class was originally declared, the program is invalid. */
13086 if (scope && !is_ancestor (scope, nested_name_specifier))
13088 error ("declaration of %qD in %qD which does not enclose %qD",
13089 type, scope, nested_name_specifier);
13095 A declarator-id shall not be qualified exception of the
13096 definition of a ... nested class outside of its class
13097 ... [or] a the definition or explicit instantiation of a
13098 class member of a namespace outside of its namespace. */
13099 if (scope == nested_name_specifier)
13101 pedwarn ("extra qualification ignored");
13102 nested_name_specifier = NULL_TREE;
13106 /* An explicit-specialization must be preceded by "template <>". If
13107 it is not, try to recover gracefully. */
13108 if (at_namespace_scope_p ()
13109 && parser->num_template_parameter_lists == 0
13112 error ("an explicit specialization must be preceded by %<template <>%>");
13113 invalid_explicit_specialization_p = true;
13114 /* Take the same action that would have been taken by
13115 cp_parser_explicit_specialization. */
13116 ++parser->num_template_parameter_lists;
13117 begin_specialization ();
13119 /* There must be no "return" statements between this point and the
13120 end of this function; set "type "to the correct return value and
13121 use "goto done;" to return. */
13122 /* Make sure that the right number of template parameters were
13124 if (!cp_parser_check_template_parameters (parser, num_templates))
13126 /* If something went wrong, there is no point in even trying to
13127 process the class-definition. */
13132 /* Look up the type. */
13135 type = TREE_TYPE (id);
13136 maybe_process_partial_specialization (type);
13137 if (nested_name_specifier)
13138 pushed_scope = push_scope (nested_name_specifier);
13140 else if (nested_name_specifier)
13146 template <typename T> struct S { struct T };
13147 template <typename T> struct S<T>::T { };
13149 we will get a TYPENAME_TYPE when processing the definition of
13150 `S::T'. We need to resolve it to the actual type before we
13151 try to define it. */
13152 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13154 class_type = resolve_typename_type (TREE_TYPE (type),
13155 /*only_current_p=*/false);
13156 if (class_type != error_mark_node)
13157 type = TYPE_NAME (class_type);
13160 cp_parser_error (parser, "could not resolve typename type");
13161 type = error_mark_node;
13165 maybe_process_partial_specialization (TREE_TYPE (type));
13166 class_type = current_class_type;
13167 /* Enter the scope indicated by the nested-name-specifier. */
13168 pushed_scope = push_scope (nested_name_specifier);
13169 /* Get the canonical version of this type. */
13170 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13171 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13172 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13174 type = push_template_decl (type);
13175 if (type == error_mark_node)
13182 type = TREE_TYPE (type);
13183 *nested_name_specifier_p = true;
13185 else /* The name is not a nested name. */
13187 /* If the class was unnamed, create a dummy name. */
13189 id = make_anon_name ();
13190 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13191 parser->num_template_parameter_lists);
13194 /* Indicate whether this class was declared as a `class' or as a
13196 if (TREE_CODE (type) == RECORD_TYPE)
13197 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13198 cp_parser_check_class_key (class_key, type);
13200 /* If this type was already complete, and we see another definition,
13201 that's an error. */
13202 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13204 error ("redefinition of %q#T", type);
13205 error ("previous definition of %q+#T", type);
13210 /* We will have entered the scope containing the class; the names of
13211 base classes should be looked up in that context. For example:
13213 struct A { struct B {}; struct C; };
13214 struct A::C : B {};
13219 /* Get the list of base-classes, if there is one. */
13220 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13221 bases = cp_parser_base_clause (parser);
13223 /* Process the base classes. */
13224 xref_basetypes (type, bases);
13227 /* Leave the scope given by the nested-name-specifier. We will
13228 enter the class scope itself while processing the members. */
13230 pop_scope (pushed_scope);
13232 if (invalid_explicit_specialization_p)
13234 end_specialization ();
13235 --parser->num_template_parameter_lists;
13237 *attributes_p = attributes;
13241 /* Parse a class-key.
13248 Returns the kind of class-key specified, or none_type to indicate
13251 static enum tag_types
13252 cp_parser_class_key (cp_parser* parser)
13255 enum tag_types tag_type;
13257 /* Look for the class-key. */
13258 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13262 /* Check to see if the TOKEN is a class-key. */
13263 tag_type = cp_parser_token_is_class_key (token);
13265 cp_parser_error (parser, "expected class-key");
13269 /* Parse an (optional) member-specification.
13271 member-specification:
13272 member-declaration member-specification [opt]
13273 access-specifier : member-specification [opt] */
13276 cp_parser_member_specification_opt (cp_parser* parser)
13283 /* Peek at the next token. */
13284 token = cp_lexer_peek_token (parser->lexer);
13285 /* If it's a `}', or EOF then we've seen all the members. */
13286 if (token->type == CPP_CLOSE_BRACE
13287 || token->type == CPP_EOF
13288 || token->type == CPP_PRAGMA_EOL)
13291 /* See if this token is a keyword. */
13292 keyword = token->keyword;
13296 case RID_PROTECTED:
13298 /* Consume the access-specifier. */
13299 cp_lexer_consume_token (parser->lexer);
13300 /* Remember which access-specifier is active. */
13301 current_access_specifier = token->value;
13302 /* Look for the `:'. */
13303 cp_parser_require (parser, CPP_COLON, "`:'");
13307 /* Accept #pragmas at class scope. */
13308 if (token->type == CPP_PRAGMA)
13310 cp_parser_pragma (parser, pragma_external);
13314 /* Otherwise, the next construction must be a
13315 member-declaration. */
13316 cp_parser_member_declaration (parser);
13321 /* Parse a member-declaration.
13323 member-declaration:
13324 decl-specifier-seq [opt] member-declarator-list [opt] ;
13325 function-definition ; [opt]
13326 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13328 template-declaration
13330 member-declarator-list:
13332 member-declarator-list , member-declarator
13335 declarator pure-specifier [opt]
13336 declarator constant-initializer [opt]
13337 identifier [opt] : constant-expression
13341 member-declaration:
13342 __extension__ member-declaration
13345 declarator attributes [opt] pure-specifier [opt]
13346 declarator attributes [opt] constant-initializer [opt]
13347 identifier [opt] attributes [opt] : constant-expression */
13350 cp_parser_member_declaration (cp_parser* parser)
13352 cp_decl_specifier_seq decl_specifiers;
13353 tree prefix_attributes;
13355 int declares_class_or_enum;
13358 int saved_pedantic;
13360 /* Check for the `__extension__' keyword. */
13361 if (cp_parser_extension_opt (parser, &saved_pedantic))
13364 cp_parser_member_declaration (parser);
13365 /* Restore the old value of the PEDANTIC flag. */
13366 pedantic = saved_pedantic;
13371 /* Check for a template-declaration. */
13372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13374 /* An explicit specialization here is an error condition, and we
13375 expect the specialization handler to detect and report this. */
13376 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13377 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13378 cp_parser_explicit_specialization (parser);
13380 cp_parser_template_declaration (parser, /*member_p=*/true);
13385 /* Check for a using-declaration. */
13386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13388 /* Parse the using-declaration. */
13389 cp_parser_using_declaration (parser);
13394 /* Check for @defs. */
13395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13398 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13399 ivar = ivar_chains;
13403 ivar = TREE_CHAIN (member);
13404 TREE_CHAIN (member) = NULL_TREE;
13405 finish_member_declaration (member);
13410 /* Parse the decl-specifier-seq. */
13411 cp_parser_decl_specifier_seq (parser,
13412 CP_PARSER_FLAGS_OPTIONAL,
13414 &declares_class_or_enum);
13415 prefix_attributes = decl_specifiers.attributes;
13416 decl_specifiers.attributes = NULL_TREE;
13417 /* Check for an invalid type-name. */
13418 if (!decl_specifiers.type
13419 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13421 /* If there is no declarator, then the decl-specifier-seq should
13423 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13425 /* If there was no decl-specifier-seq, and the next token is a
13426 `;', then we have something like:
13432 Each member-declaration shall declare at least one member
13433 name of the class. */
13434 if (!decl_specifiers.any_specifiers_p)
13436 cp_token *token = cp_lexer_peek_token (parser->lexer);
13437 if (pedantic && !token->in_system_header)
13438 pedwarn ("%Hextra %<;%>", &token->location);
13444 /* See if this declaration is a friend. */
13445 friend_p = cp_parser_friend_p (&decl_specifiers);
13446 /* If there were decl-specifiers, check to see if there was
13447 a class-declaration. */
13448 type = check_tag_decl (&decl_specifiers);
13449 /* Nested classes have already been added to the class, but
13450 a `friend' needs to be explicitly registered. */
13453 /* If the `friend' keyword was present, the friend must
13454 be introduced with a class-key. */
13455 if (!declares_class_or_enum)
13456 error ("a class-key must be used when declaring a friend");
13459 template <typename T> struct A {
13460 friend struct A<T>::B;
13463 A<T>::B will be represented by a TYPENAME_TYPE, and
13464 therefore not recognized by check_tag_decl. */
13466 && decl_specifiers.type
13467 && TYPE_P (decl_specifiers.type))
13468 type = decl_specifiers.type;
13469 if (!type || !TYPE_P (type))
13470 error ("friend declaration does not name a class or "
13473 make_friend_class (current_class_type, type,
13474 /*complain=*/true);
13476 /* If there is no TYPE, an error message will already have
13478 else if (!type || type == error_mark_node)
13480 /* An anonymous aggregate has to be handled specially; such
13481 a declaration really declares a data member (with a
13482 particular type), as opposed to a nested class. */
13483 else if (ANON_AGGR_TYPE_P (type))
13485 /* Remove constructors and such from TYPE, now that we
13486 know it is an anonymous aggregate. */
13487 fixup_anonymous_aggr (type);
13488 /* And make the corresponding data member. */
13489 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13490 /* Add it to the class. */
13491 finish_member_declaration (decl);
13494 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13499 /* See if these declarations will be friends. */
13500 friend_p = cp_parser_friend_p (&decl_specifiers);
13502 /* Keep going until we hit the `;' at the end of the
13504 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13506 tree attributes = NULL_TREE;
13507 tree first_attribute;
13509 /* Peek at the next token. */
13510 token = cp_lexer_peek_token (parser->lexer);
13512 /* Check for a bitfield declaration. */
13513 if (token->type == CPP_COLON
13514 || (token->type == CPP_NAME
13515 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13521 /* Get the name of the bitfield. Note that we cannot just
13522 check TOKEN here because it may have been invalidated by
13523 the call to cp_lexer_peek_nth_token above. */
13524 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13525 identifier = cp_parser_identifier (parser);
13527 identifier = NULL_TREE;
13529 /* Consume the `:' token. */
13530 cp_lexer_consume_token (parser->lexer);
13531 /* Get the width of the bitfield. */
13533 = cp_parser_constant_expression (parser,
13534 /*allow_non_constant=*/false,
13537 /* Look for attributes that apply to the bitfield. */
13538 attributes = cp_parser_attributes_opt (parser);
13539 /* Remember which attributes are prefix attributes and
13541 first_attribute = attributes;
13542 /* Combine the attributes. */
13543 attributes = chainon (prefix_attributes, attributes);
13545 /* Create the bitfield declaration. */
13546 decl = grokbitfield (identifier
13547 ? make_id_declarator (NULL_TREE,
13553 /* Apply the attributes. */
13554 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13558 cp_declarator *declarator;
13560 tree asm_specification;
13561 int ctor_dtor_or_conv_p;
13563 /* Parse the declarator. */
13565 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13566 &ctor_dtor_or_conv_p,
13567 /*parenthesized_p=*/NULL,
13568 /*member_p=*/true);
13570 /* If something went wrong parsing the declarator, make sure
13571 that we at least consume some tokens. */
13572 if (declarator == cp_error_declarator)
13574 /* Skip to the end of the statement. */
13575 cp_parser_skip_to_end_of_statement (parser);
13576 /* If the next token is not a semicolon, that is
13577 probably because we just skipped over the body of
13578 a function. So, we consume a semicolon if
13579 present, but do not issue an error message if it
13581 if (cp_lexer_next_token_is (parser->lexer,
13583 cp_lexer_consume_token (parser->lexer);
13587 if (declares_class_or_enum & 2)
13588 cp_parser_check_for_definition_in_return_type
13589 (declarator, decl_specifiers.type);
13591 /* Look for an asm-specification. */
13592 asm_specification = cp_parser_asm_specification_opt (parser);
13593 /* Look for attributes that apply to the declaration. */
13594 attributes = cp_parser_attributes_opt (parser);
13595 /* Remember which attributes are prefix attributes and
13597 first_attribute = attributes;
13598 /* Combine the attributes. */
13599 attributes = chainon (prefix_attributes, attributes);
13601 /* If it's an `=', then we have a constant-initializer or a
13602 pure-specifier. It is not correct to parse the
13603 initializer before registering the member declaration
13604 since the member declaration should be in scope while
13605 its initializer is processed. However, the rest of the
13606 front end does not yet provide an interface that allows
13607 us to handle this correctly. */
13608 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13612 A pure-specifier shall be used only in the declaration of
13613 a virtual function.
13615 A member-declarator can contain a constant-initializer
13616 only if it declares a static member of integral or
13619 Therefore, if the DECLARATOR is for a function, we look
13620 for a pure-specifier; otherwise, we look for a
13621 constant-initializer. When we call `grokfield', it will
13622 perform more stringent semantics checks. */
13623 if (declarator->kind == cdk_function)
13624 initializer = cp_parser_pure_specifier (parser);
13626 /* Parse the initializer. */
13627 initializer = cp_parser_constant_initializer (parser);
13629 /* Otherwise, there is no initializer. */
13631 initializer = NULL_TREE;
13633 /* See if we are probably looking at a function
13634 definition. We are certainly not looking at a
13635 member-declarator. Calling `grokfield' has
13636 side-effects, so we must not do it unless we are sure
13637 that we are looking at a member-declarator. */
13638 if (cp_parser_token_starts_function_definition_p
13639 (cp_lexer_peek_token (parser->lexer)))
13641 /* The grammar does not allow a pure-specifier to be
13642 used when a member function is defined. (It is
13643 possible that this fact is an oversight in the
13644 standard, since a pure function may be defined
13645 outside of the class-specifier. */
13647 error ("pure-specifier on function-definition");
13648 decl = cp_parser_save_member_function_body (parser,
13652 /* If the member was not a friend, declare it here. */
13654 finish_member_declaration (decl);
13655 /* Peek at the next token. */
13656 token = cp_lexer_peek_token (parser->lexer);
13657 /* If the next token is a semicolon, consume it. */
13658 if (token->type == CPP_SEMICOLON)
13659 cp_lexer_consume_token (parser->lexer);
13664 /* Create the declaration. */
13665 decl = grokfield (declarator, &decl_specifiers,
13666 initializer, asm_specification,
13668 /* Any initialization must have been from a
13669 constant-expression. */
13670 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13671 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13675 /* Reset PREFIX_ATTRIBUTES. */
13676 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13677 attributes = TREE_CHAIN (attributes);
13679 TREE_CHAIN (attributes) = NULL_TREE;
13681 /* If there is any qualification still in effect, clear it
13682 now; we will be starting fresh with the next declarator. */
13683 parser->scope = NULL_TREE;
13684 parser->qualifying_scope = NULL_TREE;
13685 parser->object_scope = NULL_TREE;
13686 /* If it's a `,', then there are more declarators. */
13687 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13688 cp_lexer_consume_token (parser->lexer);
13689 /* If the next token isn't a `;', then we have a parse error. */
13690 else if (cp_lexer_next_token_is_not (parser->lexer,
13693 cp_parser_error (parser, "expected %<;%>");
13694 /* Skip tokens until we find a `;'. */
13695 cp_parser_skip_to_end_of_statement (parser);
13702 /* Add DECL to the list of members. */
13704 finish_member_declaration (decl);
13706 if (TREE_CODE (decl) == FUNCTION_DECL)
13707 cp_parser_save_default_args (parser, decl);
13712 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13715 /* Parse a pure-specifier.
13720 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13721 Otherwise, ERROR_MARK_NODE is returned. */
13724 cp_parser_pure_specifier (cp_parser* parser)
13728 /* Look for the `=' token. */
13729 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13730 return error_mark_node;
13731 /* Look for the `0' token. */
13732 token = cp_lexer_consume_token (parser->lexer);
13733 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13734 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13735 return integer_zero_node;
13737 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13738 cp_parser_skip_to_end_of_statement (parser);
13739 return error_mark_node;
13742 /* Parse a constant-initializer.
13744 constant-initializer:
13745 = constant-expression
13747 Returns a representation of the constant-expression. */
13750 cp_parser_constant_initializer (cp_parser* parser)
13752 /* Look for the `=' token. */
13753 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13754 return error_mark_node;
13756 /* It is invalid to write:
13758 struct S { static const int i = { 7 }; };
13761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13763 cp_parser_error (parser,
13764 "a brace-enclosed initializer is not allowed here");
13765 /* Consume the opening brace. */
13766 cp_lexer_consume_token (parser->lexer);
13767 /* Skip the initializer. */
13768 cp_parser_skip_to_closing_brace (parser);
13769 /* Look for the trailing `}'. */
13770 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13772 return error_mark_node;
13775 return cp_parser_constant_expression (parser,
13776 /*allow_non_constant=*/false,
13780 /* Derived classes [gram.class.derived] */
13782 /* Parse a base-clause.
13785 : base-specifier-list
13787 base-specifier-list:
13789 base-specifier-list , base-specifier
13791 Returns a TREE_LIST representing the base-classes, in the order in
13792 which they were declared. The representation of each node is as
13793 described by cp_parser_base_specifier.
13795 In the case that no bases are specified, this function will return
13796 NULL_TREE, not ERROR_MARK_NODE. */
13799 cp_parser_base_clause (cp_parser* parser)
13801 tree bases = NULL_TREE;
13803 /* Look for the `:' that begins the list. */
13804 cp_parser_require (parser, CPP_COLON, "`:'");
13806 /* Scan the base-specifier-list. */
13812 /* Look for the base-specifier. */
13813 base = cp_parser_base_specifier (parser);
13814 /* Add BASE to the front of the list. */
13815 if (base != error_mark_node)
13817 TREE_CHAIN (base) = bases;
13820 /* Peek at the next token. */
13821 token = cp_lexer_peek_token (parser->lexer);
13822 /* If it's not a comma, then the list is complete. */
13823 if (token->type != CPP_COMMA)
13825 /* Consume the `,'. */
13826 cp_lexer_consume_token (parser->lexer);
13829 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13830 base class had a qualified name. However, the next name that
13831 appears is certainly not qualified. */
13832 parser->scope = NULL_TREE;
13833 parser->qualifying_scope = NULL_TREE;
13834 parser->object_scope = NULL_TREE;
13836 return nreverse (bases);
13839 /* Parse a base-specifier.
13842 :: [opt] nested-name-specifier [opt] class-name
13843 virtual access-specifier [opt] :: [opt] nested-name-specifier
13845 access-specifier virtual [opt] :: [opt] nested-name-specifier
13848 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13849 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13850 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13851 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13854 cp_parser_base_specifier (cp_parser* parser)
13858 bool virtual_p = false;
13859 bool duplicate_virtual_error_issued_p = false;
13860 bool duplicate_access_error_issued_p = false;
13861 bool class_scope_p, template_p;
13862 tree access = access_default_node;
13865 /* Process the optional `virtual' and `access-specifier'. */
13868 /* Peek at the next token. */
13869 token = cp_lexer_peek_token (parser->lexer);
13870 /* Process `virtual'. */
13871 switch (token->keyword)
13874 /* If `virtual' appears more than once, issue an error. */
13875 if (virtual_p && !duplicate_virtual_error_issued_p)
13877 cp_parser_error (parser,
13878 "%<virtual%> specified more than once in base-specified");
13879 duplicate_virtual_error_issued_p = true;
13884 /* Consume the `virtual' token. */
13885 cp_lexer_consume_token (parser->lexer);
13890 case RID_PROTECTED:
13892 /* If more than one access specifier appears, issue an
13894 if (access != access_default_node
13895 && !duplicate_access_error_issued_p)
13897 cp_parser_error (parser,
13898 "more than one access specifier in base-specified");
13899 duplicate_access_error_issued_p = true;
13902 access = ridpointers[(int) token->keyword];
13904 /* Consume the access-specifier. */
13905 cp_lexer_consume_token (parser->lexer);
13914 /* It is not uncommon to see programs mechanically, erroneously, use
13915 the 'typename' keyword to denote (dependent) qualified types
13916 as base classes. */
13917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13919 if (!processing_template_decl)
13920 error ("keyword %<typename%> not allowed outside of templates");
13922 error ("keyword %<typename%> not allowed in this context "
13923 "(the base class is implicitly a type)");
13924 cp_lexer_consume_token (parser->lexer);
13927 /* Look for the optional `::' operator. */
13928 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13929 /* Look for the nested-name-specifier. The simplest way to
13934 The keyword `typename' is not permitted in a base-specifier or
13935 mem-initializer; in these contexts a qualified name that
13936 depends on a template-parameter is implicitly assumed to be a
13939 is to pretend that we have seen the `typename' keyword at this
13941 cp_parser_nested_name_specifier_opt (parser,
13942 /*typename_keyword_p=*/true,
13943 /*check_dependency_p=*/true,
13945 /*is_declaration=*/true);
13946 /* If the base class is given by a qualified name, assume that names
13947 we see are type names or templates, as appropriate. */
13948 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13949 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13951 /* Finally, look for the class-name. */
13952 type = cp_parser_class_name (parser,
13956 /*check_dependency_p=*/true,
13957 /*class_head_p=*/false,
13958 /*is_declaration=*/true);
13960 if (type == error_mark_node)
13961 return error_mark_node;
13963 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13966 /* Exception handling [gram.exception] */
13968 /* Parse an (optional) exception-specification.
13970 exception-specification:
13971 throw ( type-id-list [opt] )
13973 Returns a TREE_LIST representing the exception-specification. The
13974 TREE_VALUE of each node is a type. */
13977 cp_parser_exception_specification_opt (cp_parser* parser)
13982 /* Peek at the next token. */
13983 token = cp_lexer_peek_token (parser->lexer);
13984 /* If it's not `throw', then there's no exception-specification. */
13985 if (!cp_parser_is_keyword (token, RID_THROW))
13988 /* Consume the `throw'. */
13989 cp_lexer_consume_token (parser->lexer);
13991 /* Look for the `('. */
13992 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13994 /* Peek at the next token. */
13995 token = cp_lexer_peek_token (parser->lexer);
13996 /* If it's not a `)', then there is a type-id-list. */
13997 if (token->type != CPP_CLOSE_PAREN)
13999 const char *saved_message;
14001 /* Types may not be defined in an exception-specification. */
14002 saved_message = parser->type_definition_forbidden_message;
14003 parser->type_definition_forbidden_message
14004 = "types may not be defined in an exception-specification";
14005 /* Parse the type-id-list. */
14006 type_id_list = cp_parser_type_id_list (parser);
14007 /* Restore the saved message. */
14008 parser->type_definition_forbidden_message = saved_message;
14011 type_id_list = empty_except_spec;
14013 /* Look for the `)'. */
14014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14016 return type_id_list;
14019 /* Parse an (optional) type-id-list.
14023 type-id-list , type-id
14025 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14026 in the order that the types were presented. */
14029 cp_parser_type_id_list (cp_parser* parser)
14031 tree types = NULL_TREE;
14038 /* Get the next type-id. */
14039 type = cp_parser_type_id (parser);
14040 /* Add it to the list. */
14041 types = add_exception_specifier (types, type, /*complain=*/1);
14042 /* Peek at the next token. */
14043 token = cp_lexer_peek_token (parser->lexer);
14044 /* If it is not a `,', we are done. */
14045 if (token->type != CPP_COMMA)
14047 /* Consume the `,'. */
14048 cp_lexer_consume_token (parser->lexer);
14051 return nreverse (types);
14054 /* Parse a try-block.
14057 try compound-statement handler-seq */
14060 cp_parser_try_block (cp_parser* parser)
14064 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14065 try_block = begin_try_block ();
14066 cp_parser_compound_statement (parser, NULL, true);
14067 finish_try_block (try_block);
14068 cp_parser_handler_seq (parser);
14069 finish_handler_sequence (try_block);
14074 /* Parse a function-try-block.
14076 function-try-block:
14077 try ctor-initializer [opt] function-body handler-seq */
14080 cp_parser_function_try_block (cp_parser* parser)
14083 bool ctor_initializer_p;
14085 /* Look for the `try' keyword. */
14086 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14088 /* Let the rest of the front-end know where we are. */
14089 try_block = begin_function_try_block ();
14090 /* Parse the function-body. */
14092 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14093 /* We're done with the `try' part. */
14094 finish_function_try_block (try_block);
14095 /* Parse the handlers. */
14096 cp_parser_handler_seq (parser);
14097 /* We're done with the handlers. */
14098 finish_function_handler_sequence (try_block);
14100 return ctor_initializer_p;
14103 /* Parse a handler-seq.
14106 handler handler-seq [opt] */
14109 cp_parser_handler_seq (cp_parser* parser)
14115 /* Parse the handler. */
14116 cp_parser_handler (parser);
14117 /* Peek at the next token. */
14118 token = cp_lexer_peek_token (parser->lexer);
14119 /* If it's not `catch' then there are no more handlers. */
14120 if (!cp_parser_is_keyword (token, RID_CATCH))
14125 /* Parse a handler.
14128 catch ( exception-declaration ) compound-statement */
14131 cp_parser_handler (cp_parser* parser)
14136 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14137 handler = begin_handler ();
14138 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14139 declaration = cp_parser_exception_declaration (parser);
14140 finish_handler_parms (declaration, handler);
14141 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14142 cp_parser_compound_statement (parser, NULL, false);
14143 finish_handler (handler);
14146 /* Parse an exception-declaration.
14148 exception-declaration:
14149 type-specifier-seq declarator
14150 type-specifier-seq abstract-declarator
14154 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14155 ellipsis variant is used. */
14158 cp_parser_exception_declaration (cp_parser* parser)
14161 cp_decl_specifier_seq type_specifiers;
14162 cp_declarator *declarator;
14163 const char *saved_message;
14165 /* If it's an ellipsis, it's easy to handle. */
14166 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14168 /* Consume the `...' token. */
14169 cp_lexer_consume_token (parser->lexer);
14173 /* Types may not be defined in exception-declarations. */
14174 saved_message = parser->type_definition_forbidden_message;
14175 parser->type_definition_forbidden_message
14176 = "types may not be defined in exception-declarations";
14178 /* Parse the type-specifier-seq. */
14179 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14181 /* If it's a `)', then there is no declarator. */
14182 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14185 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14186 /*ctor_dtor_or_conv_p=*/NULL,
14187 /*parenthesized_p=*/NULL,
14188 /*member_p=*/false);
14190 /* Restore the saved message. */
14191 parser->type_definition_forbidden_message = saved_message;
14193 if (type_specifiers.any_specifiers_p)
14195 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14196 if (decl == NULL_TREE)
14197 error ("invalid catch parameter");
14205 /* Parse a throw-expression.
14208 throw assignment-expression [opt]
14210 Returns a THROW_EXPR representing the throw-expression. */
14213 cp_parser_throw_expression (cp_parser* parser)
14218 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14219 token = cp_lexer_peek_token (parser->lexer);
14220 /* Figure out whether or not there is an assignment-expression
14221 following the "throw" keyword. */
14222 if (token->type == CPP_COMMA
14223 || token->type == CPP_SEMICOLON
14224 || token->type == CPP_CLOSE_PAREN
14225 || token->type == CPP_CLOSE_SQUARE
14226 || token->type == CPP_CLOSE_BRACE
14227 || token->type == CPP_COLON)
14228 expression = NULL_TREE;
14230 expression = cp_parser_assignment_expression (parser,
14233 return build_throw (expression);
14236 /* GNU Extensions */
14238 /* Parse an (optional) asm-specification.
14241 asm ( string-literal )
14243 If the asm-specification is present, returns a STRING_CST
14244 corresponding to the string-literal. Otherwise, returns
14248 cp_parser_asm_specification_opt (cp_parser* parser)
14251 tree asm_specification;
14253 /* Peek at the next token. */
14254 token = cp_lexer_peek_token (parser->lexer);
14255 /* If the next token isn't the `asm' keyword, then there's no
14256 asm-specification. */
14257 if (!cp_parser_is_keyword (token, RID_ASM))
14260 /* Consume the `asm' token. */
14261 cp_lexer_consume_token (parser->lexer);
14262 /* Look for the `('. */
14263 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14265 /* Look for the string-literal. */
14266 asm_specification = cp_parser_string_literal (parser, false, false);
14268 /* Look for the `)'. */
14269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14271 return asm_specification;
14274 /* Parse an asm-operand-list.
14278 asm-operand-list , asm-operand
14281 string-literal ( expression )
14282 [ string-literal ] string-literal ( expression )
14284 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14285 each node is the expression. The TREE_PURPOSE is itself a
14286 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14287 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14288 is a STRING_CST for the string literal before the parenthesis. */
14291 cp_parser_asm_operand_list (cp_parser* parser)
14293 tree asm_operands = NULL_TREE;
14297 tree string_literal;
14301 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14303 /* Consume the `[' token. */
14304 cp_lexer_consume_token (parser->lexer);
14305 /* Read the operand name. */
14306 name = cp_parser_identifier (parser);
14307 if (name != error_mark_node)
14308 name = build_string (IDENTIFIER_LENGTH (name),
14309 IDENTIFIER_POINTER (name));
14310 /* Look for the closing `]'. */
14311 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14315 /* Look for the string-literal. */
14316 string_literal = cp_parser_string_literal (parser, false, false);
14318 /* Look for the `('. */
14319 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14320 /* Parse the expression. */
14321 expression = cp_parser_expression (parser, /*cast_p=*/false);
14322 /* Look for the `)'. */
14323 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14325 /* Add this operand to the list. */
14326 asm_operands = tree_cons (build_tree_list (name, string_literal),
14329 /* If the next token is not a `,', there are no more
14331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14333 /* Consume the `,'. */
14334 cp_lexer_consume_token (parser->lexer);
14337 return nreverse (asm_operands);
14340 /* Parse an asm-clobber-list.
14344 asm-clobber-list , string-literal
14346 Returns a TREE_LIST, indicating the clobbers in the order that they
14347 appeared. The TREE_VALUE of each node is a STRING_CST. */
14350 cp_parser_asm_clobber_list (cp_parser* parser)
14352 tree clobbers = NULL_TREE;
14356 tree string_literal;
14358 /* Look for the string literal. */
14359 string_literal = cp_parser_string_literal (parser, false, false);
14360 /* Add it to the list. */
14361 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14362 /* If the next token is not a `,', then the list is
14364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14366 /* Consume the `,' token. */
14367 cp_lexer_consume_token (parser->lexer);
14373 /* Parse an (optional) series of attributes.
14376 attributes attribute
14379 __attribute__ (( attribute-list [opt] ))
14381 The return value is as for cp_parser_attribute_list. */
14384 cp_parser_attributes_opt (cp_parser* parser)
14386 tree attributes = NULL_TREE;
14391 tree attribute_list;
14393 /* Peek at the next token. */
14394 token = cp_lexer_peek_token (parser->lexer);
14395 /* If it's not `__attribute__', then we're done. */
14396 if (token->keyword != RID_ATTRIBUTE)
14399 /* Consume the `__attribute__' keyword. */
14400 cp_lexer_consume_token (parser->lexer);
14401 /* Look for the two `(' tokens. */
14402 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14403 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14405 /* Peek at the next token. */
14406 token = cp_lexer_peek_token (parser->lexer);
14407 if (token->type != CPP_CLOSE_PAREN)
14408 /* Parse the attribute-list. */
14409 attribute_list = cp_parser_attribute_list (parser);
14411 /* If the next token is a `)', then there is no attribute
14413 attribute_list = NULL;
14415 /* Look for the two `)' tokens. */
14416 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14417 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14419 /* Add these new attributes to the list. */
14420 attributes = chainon (attributes, attribute_list);
14426 /* Parse an attribute-list.
14430 attribute-list , attribute
14434 identifier ( identifier )
14435 identifier ( identifier , expression-list )
14436 identifier ( expression-list )
14438 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14439 to an attribute. The TREE_PURPOSE of each node is the identifier
14440 indicating which attribute is in use. The TREE_VALUE represents
14441 the arguments, if any. */
14444 cp_parser_attribute_list (cp_parser* parser)
14446 tree attribute_list = NULL_TREE;
14447 bool save_translate_strings_p = parser->translate_strings_p;
14449 parser->translate_strings_p = false;
14456 /* Look for the identifier. We also allow keywords here; for
14457 example `__attribute__ ((const))' is legal. */
14458 token = cp_lexer_peek_token (parser->lexer);
14459 if (token->type == CPP_NAME
14460 || token->type == CPP_KEYWORD)
14462 /* Consume the token. */
14463 token = cp_lexer_consume_token (parser->lexer);
14465 /* Save away the identifier that indicates which attribute
14467 identifier = token->value;
14468 attribute = build_tree_list (identifier, NULL_TREE);
14470 /* Peek at the next token. */
14471 token = cp_lexer_peek_token (parser->lexer);
14472 /* If it's an `(', then parse the attribute arguments. */
14473 if (token->type == CPP_OPEN_PAREN)
14477 arguments = (cp_parser_parenthesized_expression_list
14478 (parser, true, /*cast_p=*/false,
14479 /*non_constant_p=*/NULL));
14480 /* Save the identifier and arguments away. */
14481 TREE_VALUE (attribute) = arguments;
14484 /* Add this attribute to the list. */
14485 TREE_CHAIN (attribute) = attribute_list;
14486 attribute_list = attribute;
14488 token = cp_lexer_peek_token (parser->lexer);
14490 /* Now, look for more attributes. If the next token isn't a
14491 `,', we're done. */
14492 if (token->type != CPP_COMMA)
14495 /* Consume the comma and keep going. */
14496 cp_lexer_consume_token (parser->lexer);
14498 parser->translate_strings_p = save_translate_strings_p;
14500 /* We built up the list in reverse order. */
14501 return nreverse (attribute_list);
14504 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14505 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14506 current value of the PEDANTIC flag, regardless of whether or not
14507 the `__extension__' keyword is present. The caller is responsible
14508 for restoring the value of the PEDANTIC flag. */
14511 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14513 /* Save the old value of the PEDANTIC flag. */
14514 *saved_pedantic = pedantic;
14516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14518 /* Consume the `__extension__' token. */
14519 cp_lexer_consume_token (parser->lexer);
14520 /* We're not being pedantic while the `__extension__' keyword is
14530 /* Parse a label declaration.
14533 __label__ label-declarator-seq ;
14535 label-declarator-seq:
14536 identifier , label-declarator-seq
14540 cp_parser_label_declaration (cp_parser* parser)
14542 /* Look for the `__label__' keyword. */
14543 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14549 /* Look for an identifier. */
14550 identifier = cp_parser_identifier (parser);
14551 /* If we failed, stop. */
14552 if (identifier == error_mark_node)
14554 /* Declare it as a label. */
14555 finish_label_decl (identifier);
14556 /* If the next token is a `;', stop. */
14557 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14559 /* Look for the `,' separating the label declarations. */
14560 cp_parser_require (parser, CPP_COMMA, "`,'");
14563 /* Look for the final `;'. */
14564 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14567 /* Support Functions */
14569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14570 NAME should have one of the representations used for an
14571 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14572 is returned. If PARSER->SCOPE is a dependent type, then a
14573 SCOPE_REF is returned.
14575 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14576 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14577 was formed. Abstractly, such entities should not be passed to this
14578 function, because they do not need to be looked up, but it is
14579 simpler to check for this special case here, rather than at the
14582 In cases not explicitly covered above, this function returns a
14583 DECL, OVERLOAD, or baselink representing the result of the lookup.
14584 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14587 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14588 (e.g., "struct") that was used. In that case bindings that do not
14589 refer to types are ignored.
14591 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14594 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14597 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14600 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14601 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14602 NULL_TREE otherwise. */
14605 cp_parser_lookup_name (cp_parser *parser, tree name,
14606 enum tag_types tag_type,
14609 bool check_dependency,
14610 tree *ambiguous_decls)
14614 tree object_type = parser->context->object_type;
14616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14617 flags |= LOOKUP_COMPLAIN;
14619 /* Assume that the lookup will be unambiguous. */
14620 if (ambiguous_decls)
14621 *ambiguous_decls = NULL_TREE;
14623 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14624 no longer valid. Note that if we are parsing tentatively, and
14625 the parse fails, OBJECT_TYPE will be automatically restored. */
14626 parser->context->object_type = NULL_TREE;
14628 if (name == error_mark_node)
14629 return error_mark_node;
14631 /* A template-id has already been resolved; there is no lookup to
14633 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14635 if (BASELINK_P (name))
14637 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14638 == TEMPLATE_ID_EXPR);
14642 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14643 it should already have been checked to make sure that the name
14644 used matches the type being destroyed. */
14645 if (TREE_CODE (name) == BIT_NOT_EXPR)
14649 /* Figure out to which type this destructor applies. */
14651 type = parser->scope;
14652 else if (object_type)
14653 type = object_type;
14655 type = current_class_type;
14656 /* If that's not a class type, there is no destructor. */
14657 if (!type || !CLASS_TYPE_P (type))
14658 return error_mark_node;
14659 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14660 lazily_declare_fn (sfk_destructor, type);
14661 if (!CLASSTYPE_DESTRUCTORS (type))
14662 return error_mark_node;
14663 /* If it was a class type, return the destructor. */
14664 return CLASSTYPE_DESTRUCTORS (type);
14667 /* By this point, the NAME should be an ordinary identifier. If
14668 the id-expression was a qualified name, the qualifying scope is
14669 stored in PARSER->SCOPE at this point. */
14670 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14672 /* Perform the lookup. */
14677 if (parser->scope == error_mark_node)
14678 return error_mark_node;
14680 /* If the SCOPE is dependent, the lookup must be deferred until
14681 the template is instantiated -- unless we are explicitly
14682 looking up names in uninstantiated templates. Even then, we
14683 cannot look up the name if the scope is not a class type; it
14684 might, for example, be a template type parameter. */
14685 dependent_p = (TYPE_P (parser->scope)
14686 && !(parser->in_declarator_p
14687 && currently_open_class (parser->scope))
14688 && dependent_type_p (parser->scope));
14689 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14696 /* The resolution to Core Issue 180 says that `struct
14697 A::B' should be considered a type-name, even if `A'
14699 type = make_typename_type (parser->scope, name, tag_type,
14700 /*complain=*/tf_error);
14701 decl = TYPE_NAME (type);
14703 else if (is_template
14704 && (cp_parser_next_token_ends_template_argument_p (parser)
14705 || cp_lexer_next_token_is (parser->lexer,
14707 decl = make_unbound_class_template (parser->scope,
14709 /*complain=*/tf_error);
14711 decl = build_qualified_name (/*type=*/NULL_TREE,
14712 parser->scope, name,
14717 tree pushed_scope = NULL_TREE;
14719 /* If PARSER->SCOPE is a dependent type, then it must be a
14720 class type, and we must not be checking dependencies;
14721 otherwise, we would have processed this lookup above. So
14722 that PARSER->SCOPE is not considered a dependent base by
14723 lookup_member, we must enter the scope here. */
14725 pushed_scope = push_scope (parser->scope);
14726 /* If the PARSER->SCOPE is a template specialization, it
14727 may be instantiated during name lookup. In that case,
14728 errors may be issued. Even if we rollback the current
14729 tentative parse, those errors are valid. */
14730 decl = lookup_qualified_name (parser->scope, name,
14731 tag_type != none_type,
14732 /*complain=*/true);
14734 pop_scope (pushed_scope);
14736 parser->qualifying_scope = parser->scope;
14737 parser->object_scope = NULL_TREE;
14739 else if (object_type)
14741 tree object_decl = NULL_TREE;
14742 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14743 OBJECT_TYPE is not a class. */
14744 if (CLASS_TYPE_P (object_type))
14745 /* If the OBJECT_TYPE is a template specialization, it may
14746 be instantiated during name lookup. In that case, errors
14747 may be issued. Even if we rollback the current tentative
14748 parse, those errors are valid. */
14749 object_decl = lookup_member (object_type,
14752 tag_type != none_type);
14753 /* Look it up in the enclosing context, too. */
14754 decl = lookup_name_real (name, tag_type != none_type,
14756 /*block_p=*/true, is_namespace, flags);
14757 parser->object_scope = object_type;
14758 parser->qualifying_scope = NULL_TREE;
14760 decl = object_decl;
14764 decl = lookup_name_real (name, tag_type != none_type,
14766 /*block_p=*/true, is_namespace, flags);
14767 parser->qualifying_scope = NULL_TREE;
14768 parser->object_scope = NULL_TREE;
14771 /* If the lookup failed, let our caller know. */
14772 if (!decl || decl == error_mark_node)
14773 return error_mark_node;
14775 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14776 if (TREE_CODE (decl) == TREE_LIST)
14778 if (ambiguous_decls)
14779 *ambiguous_decls = decl;
14780 /* The error message we have to print is too complicated for
14781 cp_parser_error, so we incorporate its actions directly. */
14782 if (!cp_parser_simulate_error (parser))
14784 error ("reference to %qD is ambiguous", name);
14785 print_candidates (decl);
14787 return error_mark_node;
14790 gcc_assert (DECL_P (decl)
14791 || TREE_CODE (decl) == OVERLOAD
14792 || TREE_CODE (decl) == SCOPE_REF
14793 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14794 || BASELINK_P (decl));
14796 /* If we have resolved the name of a member declaration, check to
14797 see if the declaration is accessible. When the name resolves to
14798 set of overloaded functions, accessibility is checked when
14799 overload resolution is done.
14801 During an explicit instantiation, access is not checked at all,
14802 as per [temp.explicit]. */
14804 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14809 /* Like cp_parser_lookup_name, but for use in the typical case where
14810 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14811 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14814 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14816 return cp_parser_lookup_name (parser, name,
14818 /*is_template=*/false,
14819 /*is_namespace=*/false,
14820 /*check_dependency=*/true,
14821 /*ambiguous_decls=*/NULL);
14824 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14825 the current context, return the TYPE_DECL. If TAG_NAME_P is
14826 true, the DECL indicates the class being defined in a class-head,
14827 or declared in an elaborated-type-specifier.
14829 Otherwise, return DECL. */
14832 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14834 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14835 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14838 template <typename T> struct B;
14841 template <typename T> struct A::B {};
14843 Similarly, in an elaborated-type-specifier:
14845 namespace N { struct X{}; }
14848 template <typename T> friend struct N::X;
14851 However, if the DECL refers to a class type, and we are in
14852 the scope of the class, then the name lookup automatically
14853 finds the TYPE_DECL created by build_self_reference rather
14854 than a TEMPLATE_DECL. For example, in:
14856 template <class T> struct S {
14860 there is no need to handle such case. */
14862 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14863 return DECL_TEMPLATE_RESULT (decl);
14868 /* If too many, or too few, template-parameter lists apply to the
14869 declarator, issue an error message. Returns TRUE if all went well,
14870 and FALSE otherwise. */
14873 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14874 cp_declarator *declarator)
14876 unsigned num_templates;
14878 /* We haven't seen any classes that involve template parameters yet. */
14881 switch (declarator->kind)
14884 if (declarator->u.id.qualifying_scope)
14889 scope = declarator->u.id.qualifying_scope;
14890 member = declarator->u.id.unqualified_name;
14892 while (scope && CLASS_TYPE_P (scope))
14894 /* You're supposed to have one `template <...>'
14895 for every template class, but you don't need one
14896 for a full specialization. For example:
14898 template <class T> struct S{};
14899 template <> struct S<int> { void f(); };
14900 void S<int>::f () {}
14902 is correct; there shouldn't be a `template <>' for
14903 the definition of `S<int>::f'. */
14904 if (CLASSTYPE_TEMPLATE_INFO (scope)
14905 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14906 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14907 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14910 scope = TYPE_CONTEXT (scope);
14913 else if (TREE_CODE (declarator->u.id.unqualified_name)
14914 == TEMPLATE_ID_EXPR)
14915 /* If the DECLARATOR has the form `X<y>' then it uses one
14916 additional level of template parameters. */
14919 return cp_parser_check_template_parameters (parser,
14925 case cdk_reference:
14927 return (cp_parser_check_declarator_template_parameters
14928 (parser, declarator->declarator));
14934 gcc_unreachable ();
14939 /* NUM_TEMPLATES were used in the current declaration. If that is
14940 invalid, return FALSE and issue an error messages. Otherwise,
14944 cp_parser_check_template_parameters (cp_parser* parser,
14945 unsigned num_templates)
14947 /* If there are more template classes than parameter lists, we have
14950 template <class T> void S<T>::R<T>::f (); */
14951 if (parser->num_template_parameter_lists < num_templates)
14953 error ("too few template-parameter-lists");
14956 /* If there are the same number of template classes and parameter
14957 lists, that's OK. */
14958 if (parser->num_template_parameter_lists == num_templates)
14960 /* If there are more, but only one more, then we are referring to a
14961 member template. That's OK too. */
14962 if (parser->num_template_parameter_lists == num_templates + 1)
14964 /* Otherwise, there are too many template parameter lists. We have
14967 template <class T> template <class U> void S::f(); */
14968 error ("too many template-parameter-lists");
14972 /* Parse an optional `::' token indicating that the following name is
14973 from the global namespace. If so, PARSER->SCOPE is set to the
14974 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14975 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14976 Returns the new value of PARSER->SCOPE, if the `::' token is
14977 present, and NULL_TREE otherwise. */
14980 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14984 /* Peek at the next token. */
14985 token = cp_lexer_peek_token (parser->lexer);
14986 /* If we're looking at a `::' token then we're starting from the
14987 global namespace, not our current location. */
14988 if (token->type == CPP_SCOPE)
14990 /* Consume the `::' token. */
14991 cp_lexer_consume_token (parser->lexer);
14992 /* Set the SCOPE so that we know where to start the lookup. */
14993 parser->scope = global_namespace;
14994 parser->qualifying_scope = global_namespace;
14995 parser->object_scope = NULL_TREE;
14997 return parser->scope;
14999 else if (!current_scope_valid_p)
15001 parser->scope = NULL_TREE;
15002 parser->qualifying_scope = NULL_TREE;
15003 parser->object_scope = NULL_TREE;
15009 /* Returns TRUE if the upcoming token sequence is the start of a
15010 constructor declarator. If FRIEND_P is true, the declarator is
15011 preceded by the `friend' specifier. */
15014 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15016 bool constructor_p;
15017 tree type_decl = NULL_TREE;
15018 bool nested_name_p;
15019 cp_token *next_token;
15021 /* The common case is that this is not a constructor declarator, so
15022 try to avoid doing lots of work if at all possible. It's not
15023 valid declare a constructor at function scope. */
15024 if (at_function_scope_p ())
15026 /* And only certain tokens can begin a constructor declarator. */
15027 next_token = cp_lexer_peek_token (parser->lexer);
15028 if (next_token->type != CPP_NAME
15029 && next_token->type != CPP_SCOPE
15030 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15031 && next_token->type != CPP_TEMPLATE_ID)
15034 /* Parse tentatively; we are going to roll back all of the tokens
15036 cp_parser_parse_tentatively (parser);
15037 /* Assume that we are looking at a constructor declarator. */
15038 constructor_p = true;
15040 /* Look for the optional `::' operator. */
15041 cp_parser_global_scope_opt (parser,
15042 /*current_scope_valid_p=*/false);
15043 /* Look for the nested-name-specifier. */
15045 = (cp_parser_nested_name_specifier_opt (parser,
15046 /*typename_keyword_p=*/false,
15047 /*check_dependency_p=*/false,
15049 /*is_declaration=*/false)
15051 /* Outside of a class-specifier, there must be a
15052 nested-name-specifier. */
15053 if (!nested_name_p &&
15054 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15056 constructor_p = false;
15057 /* If we still think that this might be a constructor-declarator,
15058 look for a class-name. */
15063 template <typename T> struct S { S(); };
15064 template <typename T> S<T>::S ();
15066 we must recognize that the nested `S' names a class.
15069 template <typename T> S<T>::S<T> ();
15071 we must recognize that the nested `S' names a template. */
15072 type_decl = cp_parser_class_name (parser,
15073 /*typename_keyword_p=*/false,
15074 /*template_keyword_p=*/false,
15076 /*check_dependency_p=*/false,
15077 /*class_head_p=*/false,
15078 /*is_declaration=*/false);
15079 /* If there was no class-name, then this is not a constructor. */
15080 constructor_p = !cp_parser_error_occurred (parser);
15083 /* If we're still considering a constructor, we have to see a `(',
15084 to begin the parameter-declaration-clause, followed by either a
15085 `)', an `...', or a decl-specifier. We need to check for a
15086 type-specifier to avoid being fooled into thinking that:
15090 is a constructor. (It is actually a function named `f' that
15091 takes one parameter (of type `int') and returns a value of type
15094 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15097 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15098 /* A parameter declaration begins with a decl-specifier,
15099 which is either the "attribute" keyword, a storage class
15100 specifier, or (usually) a type-specifier. */
15101 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15102 && !cp_parser_storage_class_specifier_opt (parser))
15105 tree pushed_scope = NULL_TREE;
15106 unsigned saved_num_template_parameter_lists;
15108 /* Names appearing in the type-specifier should be looked up
15109 in the scope of the class. */
15110 if (current_class_type)
15114 type = TREE_TYPE (type_decl);
15115 if (TREE_CODE (type) == TYPENAME_TYPE)
15117 type = resolve_typename_type (type,
15118 /*only_current_p=*/false);
15119 if (type == error_mark_node)
15121 cp_parser_abort_tentative_parse (parser);
15125 pushed_scope = push_scope (type);
15128 /* Inside the constructor parameter list, surrounding
15129 template-parameter-lists do not apply. */
15130 saved_num_template_parameter_lists
15131 = parser->num_template_parameter_lists;
15132 parser->num_template_parameter_lists = 0;
15134 /* Look for the type-specifier. */
15135 cp_parser_type_specifier (parser,
15136 CP_PARSER_FLAGS_NONE,
15137 /*decl_specs=*/NULL,
15138 /*is_declarator=*/true,
15139 /*declares_class_or_enum=*/NULL,
15140 /*is_cv_qualifier=*/NULL);
15142 parser->num_template_parameter_lists
15143 = saved_num_template_parameter_lists;
15145 /* Leave the scope of the class. */
15147 pop_scope (pushed_scope);
15149 constructor_p = !cp_parser_error_occurred (parser);
15153 constructor_p = false;
15154 /* We did not really want to consume any tokens. */
15155 cp_parser_abort_tentative_parse (parser);
15157 return constructor_p;
15160 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15161 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15162 they must be performed once we are in the scope of the function.
15164 Returns the function defined. */
15167 cp_parser_function_definition_from_specifiers_and_declarator
15168 (cp_parser* parser,
15169 cp_decl_specifier_seq *decl_specifiers,
15171 const cp_declarator *declarator)
15176 /* Begin the function-definition. */
15177 success_p = start_function (decl_specifiers, declarator, attributes);
15179 /* The things we're about to see are not directly qualified by any
15180 template headers we've seen thus far. */
15181 reset_specialization ();
15183 /* If there were names looked up in the decl-specifier-seq that we
15184 did not check, check them now. We must wait until we are in the
15185 scope of the function to perform the checks, since the function
15186 might be a friend. */
15187 perform_deferred_access_checks ();
15191 /* Skip the entire function. */
15192 error ("invalid function declaration");
15193 cp_parser_skip_to_end_of_block_or_statement (parser);
15194 fn = error_mark_node;
15197 fn = cp_parser_function_definition_after_declarator (parser,
15198 /*inline_p=*/false);
15203 /* Parse the part of a function-definition that follows the
15204 declarator. INLINE_P is TRUE iff this function is an inline
15205 function defined with a class-specifier.
15207 Returns the function defined. */
15210 cp_parser_function_definition_after_declarator (cp_parser* parser,
15214 bool ctor_initializer_p = false;
15215 bool saved_in_unbraced_linkage_specification_p;
15216 unsigned saved_num_template_parameter_lists;
15218 /* If the next token is `return', then the code may be trying to
15219 make use of the "named return value" extension that G++ used to
15221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15223 /* Consume the `return' keyword. */
15224 cp_lexer_consume_token (parser->lexer);
15225 /* Look for the identifier that indicates what value is to be
15227 cp_parser_identifier (parser);
15228 /* Issue an error message. */
15229 error ("named return values are no longer supported");
15230 /* Skip tokens until we reach the start of the function body. */
15233 cp_token *token = cp_lexer_peek_token (parser->lexer);
15234 if (token->type == CPP_OPEN_BRACE
15235 || token->type == CPP_EOF
15236 || token->type == CPP_PRAGMA_EOL)
15238 cp_lexer_consume_token (parser->lexer);
15241 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15242 anything declared inside `f'. */
15243 saved_in_unbraced_linkage_specification_p
15244 = parser->in_unbraced_linkage_specification_p;
15245 parser->in_unbraced_linkage_specification_p = false;
15246 /* Inside the function, surrounding template-parameter-lists do not
15248 saved_num_template_parameter_lists
15249 = parser->num_template_parameter_lists;
15250 parser->num_template_parameter_lists = 0;
15251 /* If the next token is `try', then we are looking at a
15252 function-try-block. */
15253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15254 ctor_initializer_p = cp_parser_function_try_block (parser);
15255 /* A function-try-block includes the function-body, so we only do
15256 this next part if we're not processing a function-try-block. */
15259 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15261 /* Finish the function. */
15262 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15263 (inline_p ? 2 : 0));
15264 /* Generate code for it, if necessary. */
15265 expand_or_defer_fn (fn);
15266 /* Restore the saved values. */
15267 parser->in_unbraced_linkage_specification_p
15268 = saved_in_unbraced_linkage_specification_p;
15269 parser->num_template_parameter_lists
15270 = saved_num_template_parameter_lists;
15275 /* Parse a template-declaration, assuming that the `export' (and
15276 `extern') keywords, if present, has already been scanned. MEMBER_P
15277 is as for cp_parser_template_declaration. */
15280 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15282 tree decl = NULL_TREE;
15283 tree parameter_list;
15284 bool friend_p = false;
15285 bool need_lang_pop;
15287 /* Look for the `template' keyword. */
15288 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15292 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15296 A template ... shall not have C linkage. */
15297 if (current_lang_name == lang_name_c)
15299 error ("template with C linkage");
15300 /* Give it C++ linkage to avoid confusing other parts of the
15302 push_lang_context (lang_name_cplusplus);
15303 need_lang_pop = true;
15306 need_lang_pop = false;
15307 /* If the next token is `>', then we have an invalid
15308 specialization. Rather than complain about an invalid template
15309 parameter, issue an error message here. */
15310 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15312 cp_parser_error (parser, "invalid explicit specialization");
15313 begin_specialization ();
15314 parameter_list = NULL_TREE;
15317 /* Parse the template parameters. */
15318 parameter_list = cp_parser_template_parameter_list (parser);
15320 /* Look for the `>'. */
15321 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15322 /* We just processed one more parameter list. */
15323 ++parser->num_template_parameter_lists;
15324 /* If the next token is `template', there are more template
15326 if (cp_lexer_next_token_is_keyword (parser->lexer,
15328 cp_parser_template_declaration_after_export (parser, member_p);
15331 /* There are no access checks when parsing a template, as we do not
15332 know if a specialization will be a friend. */
15333 push_deferring_access_checks (dk_no_check);
15335 decl = cp_parser_single_declaration (parser,
15339 pop_deferring_access_checks ();
15341 /* If this is a member template declaration, let the front
15343 if (member_p && !friend_p && decl)
15345 if (TREE_CODE (decl) == TYPE_DECL)
15346 cp_parser_check_access_in_redeclaration (decl);
15348 decl = finish_member_template_decl (decl);
15350 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15351 make_friend_class (current_class_type, TREE_TYPE (decl),
15352 /*complain=*/true);
15354 /* We are done with the current parameter list. */
15355 --parser->num_template_parameter_lists;
15358 finish_template_decl (parameter_list);
15360 /* Register member declarations. */
15361 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15362 finish_member_declaration (decl);
15363 /* For the erroneous case of a template with C linkage, we pushed an
15364 implicit C++ linkage scope; exit that scope now. */
15366 pop_lang_context ();
15367 /* If DECL is a function template, we must return to parse it later.
15368 (Even though there is no definition, there might be default
15369 arguments that need handling.) */
15370 if (member_p && decl
15371 && (TREE_CODE (decl) == FUNCTION_DECL
15372 || DECL_FUNCTION_TEMPLATE_P (decl)))
15373 TREE_VALUE (parser->unparsed_functions_queues)
15374 = tree_cons (NULL_TREE, decl,
15375 TREE_VALUE (parser->unparsed_functions_queues));
15378 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15379 `function-definition' sequence. MEMBER_P is true, this declaration
15380 appears in a class scope.
15382 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15383 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15386 cp_parser_single_declaration (cp_parser* parser,
15390 int declares_class_or_enum;
15391 tree decl = NULL_TREE;
15392 cp_decl_specifier_seq decl_specifiers;
15393 bool function_definition_p = false;
15395 /* This function is only used when processing a template
15397 gcc_assert (innermost_scope_kind () == sk_template_parms
15398 || innermost_scope_kind () == sk_template_spec);
15400 /* Defer access checks until we know what is being declared. */
15401 push_deferring_access_checks (dk_deferred);
15403 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15405 cp_parser_decl_specifier_seq (parser,
15406 CP_PARSER_FLAGS_OPTIONAL,
15408 &declares_class_or_enum);
15410 *friend_p = cp_parser_friend_p (&decl_specifiers);
15412 /* There are no template typedefs. */
15413 if (decl_specifiers.specs[(int) ds_typedef])
15415 error ("template declaration of %qs", "typedef");
15416 decl = error_mark_node;
15419 /* Gather up the access checks that occurred the
15420 decl-specifier-seq. */
15421 stop_deferring_access_checks ();
15423 /* Check for the declaration of a template class. */
15424 if (declares_class_or_enum)
15426 if (cp_parser_declares_only_class_p (parser))
15428 decl = shadow_tag (&decl_specifiers);
15433 friend template <typename T> struct A<T>::B;
15436 A<T>::B will be represented by a TYPENAME_TYPE, and
15437 therefore not recognized by shadow_tag. */
15438 if (friend_p && *friend_p
15440 && decl_specifiers.type
15441 && TYPE_P (decl_specifiers.type))
15442 decl = decl_specifiers.type;
15444 if (decl && decl != error_mark_node)
15445 decl = TYPE_NAME (decl);
15447 decl = error_mark_node;
15450 /* If it's not a template class, try for a template function. If
15451 the next token is a `;', then this declaration does not declare
15452 anything. But, if there were errors in the decl-specifiers, then
15453 the error might well have come from an attempted class-specifier.
15454 In that case, there's no need to warn about a missing declarator. */
15456 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15457 || decl_specifiers.type != error_mark_node))
15458 decl = cp_parser_init_declarator (parser,
15460 /*function_definition_allowed_p=*/true,
15462 declares_class_or_enum,
15463 &function_definition_p);
15465 pop_deferring_access_checks ();
15467 /* Clear any current qualification; whatever comes next is the start
15468 of something new. */
15469 parser->scope = NULL_TREE;
15470 parser->qualifying_scope = NULL_TREE;
15471 parser->object_scope = NULL_TREE;
15472 /* Look for a trailing `;' after the declaration. */
15473 if (!function_definition_p
15474 && (decl == error_mark_node
15475 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15476 cp_parser_skip_to_end_of_block_or_statement (parser);
15481 /* Parse a cast-expression that is not the operand of a unary "&". */
15484 cp_parser_simple_cast_expression (cp_parser *parser)
15486 return cp_parser_cast_expression (parser, /*address_p=*/false,
15490 /* Parse a functional cast to TYPE. Returns an expression
15491 representing the cast. */
15494 cp_parser_functional_cast (cp_parser* parser, tree type)
15496 tree expression_list;
15500 = cp_parser_parenthesized_expression_list (parser, false,
15502 /*non_constant_p=*/NULL);
15504 cast = build_functional_cast (type, expression_list);
15505 /* [expr.const]/1: In an integral constant expression "only type
15506 conversions to integral or enumeration type can be used". */
15507 if (TREE_CODE (type) == TYPE_DECL)
15508 type = TREE_TYPE (type);
15509 if (cast != error_mark_node && !dependent_type_p (type)
15510 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15512 if (cp_parser_non_integral_constant_expression
15513 (parser, "a call to a constructor"))
15514 return error_mark_node;
15519 /* Save the tokens that make up the body of a member function defined
15520 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15521 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15522 specifiers applied to the declaration. Returns the FUNCTION_DECL
15523 for the member function. */
15526 cp_parser_save_member_function_body (cp_parser* parser,
15527 cp_decl_specifier_seq *decl_specifiers,
15528 cp_declarator *declarator,
15535 /* Create the function-declaration. */
15536 fn = start_method (decl_specifiers, declarator, attributes);
15537 /* If something went badly wrong, bail out now. */
15538 if (fn == error_mark_node)
15540 /* If there's a function-body, skip it. */
15541 if (cp_parser_token_starts_function_definition_p
15542 (cp_lexer_peek_token (parser->lexer)))
15543 cp_parser_skip_to_end_of_block_or_statement (parser);
15544 return error_mark_node;
15547 /* Remember it, if there default args to post process. */
15548 cp_parser_save_default_args (parser, fn);
15550 /* Save away the tokens that make up the body of the
15552 first = parser->lexer->next_token;
15553 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15554 /* Handle function try blocks. */
15555 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15556 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15557 last = parser->lexer->next_token;
15559 /* Save away the inline definition; we will process it when the
15560 class is complete. */
15561 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15562 DECL_PENDING_INLINE_P (fn) = 1;
15564 /* We need to know that this was defined in the class, so that
15565 friend templates are handled correctly. */
15566 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15568 /* We're done with the inline definition. */
15569 finish_method (fn);
15571 /* Add FN to the queue of functions to be parsed later. */
15572 TREE_VALUE (parser->unparsed_functions_queues)
15573 = tree_cons (NULL_TREE, fn,
15574 TREE_VALUE (parser->unparsed_functions_queues));
15579 /* Parse a template-argument-list, as well as the trailing ">" (but
15580 not the opening ">"). See cp_parser_template_argument_list for the
15584 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15588 tree saved_qualifying_scope;
15589 tree saved_object_scope;
15590 bool saved_greater_than_is_operator_p;
15591 bool saved_skip_evaluation;
15595 When parsing a template-id, the first non-nested `>' is taken as
15596 the end of the template-argument-list rather than a greater-than
15598 saved_greater_than_is_operator_p
15599 = parser->greater_than_is_operator_p;
15600 parser->greater_than_is_operator_p = false;
15601 /* Parsing the argument list may modify SCOPE, so we save it
15603 saved_scope = parser->scope;
15604 saved_qualifying_scope = parser->qualifying_scope;
15605 saved_object_scope = parser->object_scope;
15606 /* We need to evaluate the template arguments, even though this
15607 template-id may be nested within a "sizeof". */
15608 saved_skip_evaluation = skip_evaluation;
15609 skip_evaluation = false;
15610 /* Parse the template-argument-list itself. */
15611 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15612 arguments = NULL_TREE;
15614 arguments = cp_parser_template_argument_list (parser);
15615 /* Look for the `>' that ends the template-argument-list. If we find
15616 a '>>' instead, it's probably just a typo. */
15617 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15619 if (!saved_greater_than_is_operator_p)
15621 /* If we're in a nested template argument list, the '>>' has
15622 to be a typo for '> >'. We emit the error message, but we
15623 continue parsing and we push a '>' as next token, so that
15624 the argument list will be parsed correctly. Note that the
15625 global source location is still on the token before the
15626 '>>', so we need to say explicitly where we want it. */
15627 cp_token *token = cp_lexer_peek_token (parser->lexer);
15628 error ("%H%<>>%> should be %<> >%> "
15629 "within a nested template argument list",
15632 /* ??? Proper recovery should terminate two levels of
15633 template argument list here. */
15634 token->type = CPP_GREATER;
15638 /* If this is not a nested template argument list, the '>>'
15639 is a typo for '>'. Emit an error message and continue.
15640 Same deal about the token location, but here we can get it
15641 right by consuming the '>>' before issuing the diagnostic. */
15642 cp_lexer_consume_token (parser->lexer);
15643 error ("spurious %<>>%>, use %<>%> to terminate "
15644 "a template argument list");
15648 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15649 /* The `>' token might be a greater-than operator again now. */
15650 parser->greater_than_is_operator_p
15651 = saved_greater_than_is_operator_p;
15652 /* Restore the SAVED_SCOPE. */
15653 parser->scope = saved_scope;
15654 parser->qualifying_scope = saved_qualifying_scope;
15655 parser->object_scope = saved_object_scope;
15656 skip_evaluation = saved_skip_evaluation;
15661 /* MEMBER_FUNCTION is a member function, or a friend. If default
15662 arguments, or the body of the function have not yet been parsed,
15666 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15668 /* If this member is a template, get the underlying
15670 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15671 member_function = DECL_TEMPLATE_RESULT (member_function);
15673 /* There should not be any class definitions in progress at this
15674 point; the bodies of members are only parsed outside of all class
15676 gcc_assert (parser->num_classes_being_defined == 0);
15677 /* While we're parsing the member functions we might encounter more
15678 classes. We want to handle them right away, but we don't want
15679 them getting mixed up with functions that are currently in the
15681 parser->unparsed_functions_queues
15682 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15684 /* Make sure that any template parameters are in scope. */
15685 maybe_begin_member_template_processing (member_function);
15687 /* If the body of the function has not yet been parsed, parse it
15689 if (DECL_PENDING_INLINE_P (member_function))
15691 tree function_scope;
15692 cp_token_cache *tokens;
15694 /* The function is no longer pending; we are processing it. */
15695 tokens = DECL_PENDING_INLINE_INFO (member_function);
15696 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15697 DECL_PENDING_INLINE_P (member_function) = 0;
15699 /* If this is a local class, enter the scope of the containing
15701 function_scope = current_function_decl;
15702 if (function_scope)
15703 push_function_context_to (function_scope);
15706 /* Push the body of the function onto the lexer stack. */
15707 cp_parser_push_lexer_for_tokens (parser, tokens);
15709 /* Let the front end know that we going to be defining this
15711 start_preparsed_function (member_function, NULL_TREE,
15712 SF_PRE_PARSED | SF_INCLASS_INLINE);
15714 /* Don't do access checking if it is a templated function. */
15715 if (processing_template_decl)
15716 push_deferring_access_checks (dk_no_check);
15718 /* Now, parse the body of the function. */
15719 cp_parser_function_definition_after_declarator (parser,
15720 /*inline_p=*/true);
15722 if (processing_template_decl)
15723 pop_deferring_access_checks ();
15725 /* Leave the scope of the containing function. */
15726 if (function_scope)
15727 pop_function_context_from (function_scope);
15728 cp_parser_pop_lexer (parser);
15731 /* Remove any template parameters from the symbol table. */
15732 maybe_end_member_template_processing ();
15734 /* Restore the queue. */
15735 parser->unparsed_functions_queues
15736 = TREE_CHAIN (parser->unparsed_functions_queues);
15739 /* If DECL contains any default args, remember it on the unparsed
15740 functions queue. */
15743 cp_parser_save_default_args (cp_parser* parser, tree decl)
15747 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15749 probe = TREE_CHAIN (probe))
15750 if (TREE_PURPOSE (probe))
15752 TREE_PURPOSE (parser->unparsed_functions_queues)
15753 = tree_cons (current_class_type, decl,
15754 TREE_PURPOSE (parser->unparsed_functions_queues));
15760 /* FN is a FUNCTION_DECL which may contains a parameter with an
15761 unparsed DEFAULT_ARG. Parse the default args now. This function
15762 assumes that the current scope is the scope in which the default
15763 argument should be processed. */
15766 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15768 bool saved_local_variables_forbidden_p;
15771 /* While we're parsing the default args, we might (due to the
15772 statement expression extension) encounter more classes. We want
15773 to handle them right away, but we don't want them getting mixed
15774 up with default args that are currently in the queue. */
15775 parser->unparsed_functions_queues
15776 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15778 /* Local variable names (and the `this' keyword) may not appear
15779 in a default argument. */
15780 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15781 parser->local_variables_forbidden_p = true;
15783 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15785 parm = TREE_CHAIN (parm))
15787 cp_token_cache *tokens;
15788 tree default_arg = TREE_PURPOSE (parm);
15790 VEC(tree,gc) *insts;
15797 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15798 /* This can happen for a friend declaration for a function
15799 already declared with default arguments. */
15802 /* Push the saved tokens for the default argument onto the parser's
15804 tokens = DEFARG_TOKENS (default_arg);
15805 cp_parser_push_lexer_for_tokens (parser, tokens);
15807 /* Parse the assignment-expression. */
15808 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15810 if (!processing_template_decl)
15811 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15813 TREE_PURPOSE (parm) = parsed_arg;
15815 /* Update any instantiations we've already created. */
15816 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15817 VEC_iterate (tree, insts, ix, copy); ix++)
15818 TREE_PURPOSE (copy) = parsed_arg;
15820 /* If the token stream has not been completely used up, then
15821 there was extra junk after the end of the default
15823 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15824 cp_parser_error (parser, "expected %<,%>");
15826 /* Revert to the main lexer. */
15827 cp_parser_pop_lexer (parser);
15830 /* Restore the state of local_variables_forbidden_p. */
15831 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15833 /* Restore the queue. */
15834 parser->unparsed_functions_queues
15835 = TREE_CHAIN (parser->unparsed_functions_queues);
15838 /* Parse the operand of `sizeof' (or a similar operator). Returns
15839 either a TYPE or an expression, depending on the form of the
15840 input. The KEYWORD indicates which kind of expression we have
15844 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15846 static const char *format;
15847 tree expr = NULL_TREE;
15848 const char *saved_message;
15849 bool saved_integral_constant_expression_p;
15850 bool saved_non_integral_constant_expression_p;
15852 /* Initialize FORMAT the first time we get here. */
15854 format = "types may not be defined in '%s' expressions";
15856 /* Types cannot be defined in a `sizeof' expression. Save away the
15858 saved_message = parser->type_definition_forbidden_message;
15859 /* And create the new one. */
15860 parser->type_definition_forbidden_message
15861 = XNEWVEC (const char, strlen (format)
15862 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15864 sprintf ((char *) parser->type_definition_forbidden_message,
15865 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15867 /* The restrictions on constant-expressions do not apply inside
15868 sizeof expressions. */
15869 saved_integral_constant_expression_p
15870 = parser->integral_constant_expression_p;
15871 saved_non_integral_constant_expression_p
15872 = parser->non_integral_constant_expression_p;
15873 parser->integral_constant_expression_p = false;
15875 /* Do not actually evaluate the expression. */
15877 /* If it's a `(', then we might be looking at the type-id
15879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15882 bool saved_in_type_id_in_expr_p;
15884 /* We can't be sure yet whether we're looking at a type-id or an
15886 cp_parser_parse_tentatively (parser);
15887 /* Consume the `('. */
15888 cp_lexer_consume_token (parser->lexer);
15889 /* Parse the type-id. */
15890 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15891 parser->in_type_id_in_expr_p = true;
15892 type = cp_parser_type_id (parser);
15893 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15894 /* Now, look for the trailing `)'. */
15895 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15896 /* If all went well, then we're done. */
15897 if (cp_parser_parse_definitely (parser))
15899 cp_decl_specifier_seq decl_specs;
15901 /* Build a trivial decl-specifier-seq. */
15902 clear_decl_specs (&decl_specs);
15903 decl_specs.type = type;
15905 /* Call grokdeclarator to figure out what type this is. */
15906 expr = grokdeclarator (NULL,
15910 /*attrlist=*/NULL);
15914 /* If the type-id production did not work out, then we must be
15915 looking at the unary-expression production. */
15917 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15919 /* Go back to evaluating expressions. */
15922 /* Free the message we created. */
15923 free ((char *) parser->type_definition_forbidden_message);
15924 /* And restore the old one. */
15925 parser->type_definition_forbidden_message = saved_message;
15926 parser->integral_constant_expression_p
15927 = saved_integral_constant_expression_p;
15928 parser->non_integral_constant_expression_p
15929 = saved_non_integral_constant_expression_p;
15934 /* If the current declaration has no declarator, return true. */
15937 cp_parser_declares_only_class_p (cp_parser *parser)
15939 /* If the next token is a `;' or a `,' then there is no
15941 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15942 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15945 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15948 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15949 cp_storage_class storage_class)
15951 if (decl_specs->storage_class != sc_none)
15952 decl_specs->multiple_storage_classes_p = true;
15954 decl_specs->storage_class = storage_class;
15957 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15958 is true, the type is a user-defined type; otherwise it is a
15959 built-in type specified by a keyword. */
15962 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15964 bool user_defined_p)
15966 decl_specs->any_specifiers_p = true;
15968 /* If the user tries to redeclare bool or wchar_t (with, for
15969 example, in "typedef int wchar_t;") we remember that this is what
15970 happened. In system headers, we ignore these declarations so
15971 that G++ can work with system headers that are not C++-safe. */
15972 if (decl_specs->specs[(int) ds_typedef]
15974 && (type_spec == boolean_type_node
15975 || type_spec == wchar_type_node)
15976 && (decl_specs->type
15977 || decl_specs->specs[(int) ds_long]
15978 || decl_specs->specs[(int) ds_short]
15979 || decl_specs->specs[(int) ds_unsigned]
15980 || decl_specs->specs[(int) ds_signed]))
15982 decl_specs->redefined_builtin_type = type_spec;
15983 if (!decl_specs->type)
15985 decl_specs->type = type_spec;
15986 decl_specs->user_defined_type_p = false;
15989 else if (decl_specs->type)
15990 decl_specs->multiple_types_p = true;
15993 decl_specs->type = type_spec;
15994 decl_specs->user_defined_type_p = user_defined_p;
15995 decl_specs->redefined_builtin_type = NULL_TREE;
15999 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16000 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16003 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16005 return decl_specifiers->specs[(int) ds_friend] != 0;
16008 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16009 issue an error message indicating that TOKEN_DESC was expected.
16011 Returns the token consumed, if the token had the appropriate type.
16012 Otherwise, returns NULL. */
16015 cp_parser_require (cp_parser* parser,
16016 enum cpp_ttype type,
16017 const char* token_desc)
16019 if (cp_lexer_next_token_is (parser->lexer, type))
16020 return cp_lexer_consume_token (parser->lexer);
16023 /* Output the MESSAGE -- unless we're parsing tentatively. */
16024 if (!cp_parser_simulate_error (parser))
16026 char *message = concat ("expected ", token_desc, NULL);
16027 cp_parser_error (parser, message);
16034 /* Like cp_parser_require, except that tokens will be skipped until
16035 the desired token is found. An error message is still produced if
16036 the next token is not as expected. */
16039 cp_parser_skip_until_found (cp_parser* parser,
16040 enum cpp_ttype type,
16041 const char* token_desc)
16044 unsigned nesting_depth = 0;
16046 if (cp_parser_require (parser, type, token_desc))
16049 /* Skip tokens until the desired token is found. */
16052 /* Peek at the next token. */
16053 token = cp_lexer_peek_token (parser->lexer);
16055 /* If we've reached the token we want, consume it and stop. */
16056 if (token->type == type && !nesting_depth)
16058 cp_lexer_consume_token (parser->lexer);
16062 switch (token->type)
16065 case CPP_PRAGMA_EOL:
16066 /* If we've run out of tokens, stop. */
16069 case CPP_OPEN_BRACE:
16070 case CPP_OPEN_PAREN:
16071 case CPP_OPEN_SQUARE:
16075 case CPP_CLOSE_BRACE:
16076 case CPP_CLOSE_PAREN:
16077 case CPP_CLOSE_SQUARE:
16078 if (nesting_depth-- == 0)
16086 /* Consume this token. */
16087 cp_lexer_consume_token (parser->lexer);
16091 /* If the next token is the indicated keyword, consume it. Otherwise,
16092 issue an error message indicating that TOKEN_DESC was expected.
16094 Returns the token consumed, if the token had the appropriate type.
16095 Otherwise, returns NULL. */
16098 cp_parser_require_keyword (cp_parser* parser,
16100 const char* token_desc)
16102 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16104 if (token && token->keyword != keyword)
16106 dyn_string_t error_msg;
16108 /* Format the error message. */
16109 error_msg = dyn_string_new (0);
16110 dyn_string_append_cstr (error_msg, "expected ");
16111 dyn_string_append_cstr (error_msg, token_desc);
16112 cp_parser_error (parser, error_msg->s);
16113 dyn_string_delete (error_msg);
16120 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16121 function-definition. */
16124 cp_parser_token_starts_function_definition_p (cp_token* token)
16126 return (/* An ordinary function-body begins with an `{'. */
16127 token->type == CPP_OPEN_BRACE
16128 /* A ctor-initializer begins with a `:'. */
16129 || token->type == CPP_COLON
16130 /* A function-try-block begins with `try'. */
16131 || token->keyword == RID_TRY
16132 /* The named return value extension begins with `return'. */
16133 || token->keyword == RID_RETURN);
16136 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16140 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16144 token = cp_lexer_peek_token (parser->lexer);
16145 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16148 /* Returns TRUE iff the next token is the "," or ">" ending a
16149 template-argument. */
16152 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16156 token = cp_lexer_peek_token (parser->lexer);
16157 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16160 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16161 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16164 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16169 token = cp_lexer_peek_nth_token (parser->lexer, n);
16170 if (token->type == CPP_LESS)
16172 /* Check for the sequence `<::' in the original code. It would be lexed as
16173 `[:', where `[' is a digraph, and there is no whitespace before
16175 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16178 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16179 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16185 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16186 or none_type otherwise. */
16188 static enum tag_types
16189 cp_parser_token_is_class_key (cp_token* token)
16191 switch (token->keyword)
16196 return record_type;
16205 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16208 cp_parser_check_class_key (enum tag_types class_key, tree type)
16210 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16211 pedwarn ("%qs tag used in naming %q#T",
16212 class_key == union_type ? "union"
16213 : class_key == record_type ? "struct" : "class",
16217 /* Issue an error message if DECL is redeclared with different
16218 access than its original declaration [class.access.spec/3].
16219 This applies to nested classes and nested class templates.
16223 cp_parser_check_access_in_redeclaration (tree decl)
16225 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16228 if ((TREE_PRIVATE (decl)
16229 != (current_access_specifier == access_private_node))
16230 || (TREE_PROTECTED (decl)
16231 != (current_access_specifier == access_protected_node)))
16232 error ("%qD redeclared with different access", decl);
16235 /* Look for the `template' keyword, as a syntactic disambiguator.
16236 Return TRUE iff it is present, in which case it will be
16240 cp_parser_optional_template_keyword (cp_parser *parser)
16242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16244 /* The `template' keyword can only be used within templates;
16245 outside templates the parser can always figure out what is a
16246 template and what is not. */
16247 if (!processing_template_decl)
16249 error ("%<template%> (as a disambiguator) is only allowed "
16250 "within templates");
16251 /* If this part of the token stream is rescanned, the same
16252 error message would be generated. So, we purge the token
16253 from the stream. */
16254 cp_lexer_purge_token (parser->lexer);
16259 /* Consume the `template' keyword. */
16260 cp_lexer_consume_token (parser->lexer);
16268 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16269 set PARSER->SCOPE, and perform other related actions. */
16272 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16277 /* Get the stored value. */
16278 value = cp_lexer_consume_token (parser->lexer)->value;
16279 /* Perform any access checks that were deferred. */
16280 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16281 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16282 /* Set the scope from the stored value. */
16283 parser->scope = TREE_VALUE (value);
16284 parser->qualifying_scope = TREE_TYPE (value);
16285 parser->object_scope = NULL_TREE;
16288 /* Consume tokens up through a non-nested END token. */
16291 cp_parser_cache_group (cp_parser *parser,
16292 enum cpp_ttype end,
16299 /* Abort a parenthesized expression if we encounter a brace. */
16300 if ((end == CPP_CLOSE_PAREN || depth == 0)
16301 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16303 /* If we've reached the end of the file, stop. */
16304 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16305 || (end != CPP_PRAGMA_EOL
16306 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16308 /* Consume the next token. */
16309 token = cp_lexer_consume_token (parser->lexer);
16310 /* See if it starts a new group. */
16311 if (token->type == CPP_OPEN_BRACE)
16313 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16317 else if (token->type == CPP_OPEN_PAREN)
16318 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16319 else if (token->type == CPP_PRAGMA)
16320 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16321 else if (token->type == end)
16326 /* Begin parsing tentatively. We always save tokens while parsing
16327 tentatively so that if the tentative parsing fails we can restore the
16331 cp_parser_parse_tentatively (cp_parser* parser)
16333 /* Enter a new parsing context. */
16334 parser->context = cp_parser_context_new (parser->context);
16335 /* Begin saving tokens. */
16336 cp_lexer_save_tokens (parser->lexer);
16337 /* In order to avoid repetitive access control error messages,
16338 access checks are queued up until we are no longer parsing
16340 push_deferring_access_checks (dk_deferred);
16343 /* Commit to the currently active tentative parse. */
16346 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16348 cp_parser_context *context;
16351 /* Mark all of the levels as committed. */
16352 lexer = parser->lexer;
16353 for (context = parser->context; context->next; context = context->next)
16355 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16357 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16358 while (!cp_lexer_saving_tokens (lexer))
16359 lexer = lexer->next;
16360 cp_lexer_commit_tokens (lexer);
16364 /* Abort the currently active tentative parse. All consumed tokens
16365 will be rolled back, and no diagnostics will be issued. */
16368 cp_parser_abort_tentative_parse (cp_parser* parser)
16370 cp_parser_simulate_error (parser);
16371 /* Now, pretend that we want to see if the construct was
16372 successfully parsed. */
16373 cp_parser_parse_definitely (parser);
16376 /* Stop parsing tentatively. If a parse error has occurred, restore the
16377 token stream. Otherwise, commit to the tokens we have consumed.
16378 Returns true if no error occurred; false otherwise. */
16381 cp_parser_parse_definitely (cp_parser* parser)
16383 bool error_occurred;
16384 cp_parser_context *context;
16386 /* Remember whether or not an error occurred, since we are about to
16387 destroy that information. */
16388 error_occurred = cp_parser_error_occurred (parser);
16389 /* Remove the topmost context from the stack. */
16390 context = parser->context;
16391 parser->context = context->next;
16392 /* If no parse errors occurred, commit to the tentative parse. */
16393 if (!error_occurred)
16395 /* Commit to the tokens read tentatively, unless that was
16397 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16398 cp_lexer_commit_tokens (parser->lexer);
16400 pop_to_parent_deferring_access_checks ();
16402 /* Otherwise, if errors occurred, roll back our state so that things
16403 are just as they were before we began the tentative parse. */
16406 cp_lexer_rollback_tokens (parser->lexer);
16407 pop_deferring_access_checks ();
16409 /* Add the context to the front of the free list. */
16410 context->next = cp_parser_context_free_list;
16411 cp_parser_context_free_list = context;
16413 return !error_occurred;
16416 /* Returns true if we are parsing tentatively and are not committed to
16417 this tentative parse. */
16420 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16422 return (cp_parser_parsing_tentatively (parser)
16423 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16426 /* Returns nonzero iff an error has occurred during the most recent
16427 tentative parse. */
16430 cp_parser_error_occurred (cp_parser* parser)
16432 return (cp_parser_parsing_tentatively (parser)
16433 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16436 /* Returns nonzero if GNU extensions are allowed. */
16439 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16441 return parser->allow_gnu_extensions_p;
16444 /* Objective-C++ Productions */
16447 /* Parse an Objective-C expression, which feeds into a primary-expression
16451 objc-message-expression
16452 objc-string-literal
16453 objc-encode-expression
16454 objc-protocol-expression
16455 objc-selector-expression
16457 Returns a tree representation of the expression. */
16460 cp_parser_objc_expression (cp_parser* parser)
16462 /* Try to figure out what kind of declaration is present. */
16463 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16467 case CPP_OPEN_SQUARE:
16468 return cp_parser_objc_message_expression (parser);
16470 case CPP_OBJC_STRING:
16471 kwd = cp_lexer_consume_token (parser->lexer);
16472 return objc_build_string_object (kwd->value);
16475 switch (kwd->keyword)
16477 case RID_AT_ENCODE:
16478 return cp_parser_objc_encode_expression (parser);
16480 case RID_AT_PROTOCOL:
16481 return cp_parser_objc_protocol_expression (parser);
16483 case RID_AT_SELECTOR:
16484 return cp_parser_objc_selector_expression (parser);
16490 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16491 cp_parser_skip_to_end_of_block_or_statement (parser);
16494 return error_mark_node;
16497 /* Parse an Objective-C message expression.
16499 objc-message-expression:
16500 [ objc-message-receiver objc-message-args ]
16502 Returns a representation of an Objective-C message. */
16505 cp_parser_objc_message_expression (cp_parser* parser)
16507 tree receiver, messageargs;
16509 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16510 receiver = cp_parser_objc_message_receiver (parser);
16511 messageargs = cp_parser_objc_message_args (parser);
16512 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16514 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16517 /* Parse an objc-message-receiver.
16519 objc-message-receiver:
16521 simple-type-specifier
16523 Returns a representation of the type or expression. */
16526 cp_parser_objc_message_receiver (cp_parser* parser)
16530 /* An Objective-C message receiver may be either (1) a type
16531 or (2) an expression. */
16532 cp_parser_parse_tentatively (parser);
16533 rcv = cp_parser_expression (parser, false);
16535 if (cp_parser_parse_definitely (parser))
16538 rcv = cp_parser_simple_type_specifier (parser,
16539 /*decl_specs=*/NULL,
16540 CP_PARSER_FLAGS_NONE);
16542 return objc_get_class_reference (rcv);
16545 /* Parse the arguments and selectors comprising an Objective-C message.
16550 objc-selector-args , objc-comma-args
16552 objc-selector-args:
16553 objc-selector [opt] : assignment-expression
16554 objc-selector-args objc-selector [opt] : assignment-expression
16557 assignment-expression
16558 objc-comma-args , assignment-expression
16560 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16561 selector arguments and TREE_VALUE containing a list of comma
16565 cp_parser_objc_message_args (cp_parser* parser)
16567 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16568 bool maybe_unary_selector_p = true;
16569 cp_token *token = cp_lexer_peek_token (parser->lexer);
16571 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16573 tree selector = NULL_TREE, arg;
16575 if (token->type != CPP_COLON)
16576 selector = cp_parser_objc_selector (parser);
16578 /* Detect if we have a unary selector. */
16579 if (maybe_unary_selector_p
16580 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16581 return build_tree_list (selector, NULL_TREE);
16583 maybe_unary_selector_p = false;
16584 cp_parser_require (parser, CPP_COLON, "`:'");
16585 arg = cp_parser_assignment_expression (parser, false);
16588 = chainon (sel_args,
16589 build_tree_list (selector, arg));
16591 token = cp_lexer_peek_token (parser->lexer);
16594 /* Handle non-selector arguments, if any. */
16595 while (token->type == CPP_COMMA)
16599 cp_lexer_consume_token (parser->lexer);
16600 arg = cp_parser_assignment_expression (parser, false);
16603 = chainon (addl_args,
16604 build_tree_list (NULL_TREE, arg));
16606 token = cp_lexer_peek_token (parser->lexer);
16609 return build_tree_list (sel_args, addl_args);
16612 /* Parse an Objective-C encode expression.
16614 objc-encode-expression:
16615 @encode objc-typename
16617 Returns an encoded representation of the type argument. */
16620 cp_parser_objc_encode_expression (cp_parser* parser)
16624 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16625 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16626 type = complete_type (cp_parser_type_id (parser));
16627 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16631 error ("%<@encode%> must specify a type as an argument");
16632 return error_mark_node;
16635 return objc_build_encode_expr (type);
16638 /* Parse an Objective-C @defs expression. */
16641 cp_parser_objc_defs_expression (cp_parser *parser)
16645 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16646 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16647 name = cp_parser_identifier (parser);
16648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16650 return objc_get_class_ivars (name);
16653 /* Parse an Objective-C protocol expression.
16655 objc-protocol-expression:
16656 @protocol ( identifier )
16658 Returns a representation of the protocol expression. */
16661 cp_parser_objc_protocol_expression (cp_parser* parser)
16665 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16667 proto = cp_parser_identifier (parser);
16668 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16670 return objc_build_protocol_expr (proto);
16673 /* Parse an Objective-C selector expression.
16675 objc-selector-expression:
16676 @selector ( objc-method-signature )
16678 objc-method-signature:
16684 objc-selector-seq objc-selector :
16686 Returns a representation of the method selector. */
16689 cp_parser_objc_selector_expression (cp_parser* parser)
16691 tree sel_seq = NULL_TREE;
16692 bool maybe_unary_selector_p = true;
16695 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16696 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16697 token = cp_lexer_peek_token (parser->lexer);
16699 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16700 || token->type == CPP_SCOPE)
16702 tree selector = NULL_TREE;
16704 if (token->type != CPP_COLON
16705 || token->type == CPP_SCOPE)
16706 selector = cp_parser_objc_selector (parser);
16708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16709 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16711 /* Detect if we have a unary selector. */
16712 if (maybe_unary_selector_p)
16714 sel_seq = selector;
16715 goto finish_selector;
16719 cp_parser_error (parser, "expected %<:%>");
16722 maybe_unary_selector_p = false;
16723 token = cp_lexer_consume_token (parser->lexer);
16725 if (token->type == CPP_SCOPE)
16728 = chainon (sel_seq,
16729 build_tree_list (selector, NULL_TREE));
16731 = chainon (sel_seq,
16732 build_tree_list (NULL_TREE, NULL_TREE));
16736 = chainon (sel_seq,
16737 build_tree_list (selector, NULL_TREE));
16739 token = cp_lexer_peek_token (parser->lexer);
16743 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16745 return objc_build_selector_expr (sel_seq);
16748 /* Parse a list of identifiers.
16750 objc-identifier-list:
16752 objc-identifier-list , identifier
16754 Returns a TREE_LIST of identifier nodes. */
16757 cp_parser_objc_identifier_list (cp_parser* parser)
16759 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16760 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16762 while (sep->type == CPP_COMMA)
16764 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16765 list = chainon (list,
16766 build_tree_list (NULL_TREE,
16767 cp_parser_identifier (parser)));
16768 sep = cp_lexer_peek_token (parser->lexer);
16774 /* Parse an Objective-C alias declaration.
16776 objc-alias-declaration:
16777 @compatibility_alias identifier identifier ;
16779 This function registers the alias mapping with the Objective-C front-end.
16780 It returns nothing. */
16783 cp_parser_objc_alias_declaration (cp_parser* parser)
16787 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16788 alias = cp_parser_identifier (parser);
16789 orig = cp_parser_identifier (parser);
16790 objc_declare_alias (alias, orig);
16791 cp_parser_consume_semicolon_at_end_of_statement (parser);
16794 /* Parse an Objective-C class forward-declaration.
16796 objc-class-declaration:
16797 @class objc-identifier-list ;
16799 The function registers the forward declarations with the Objective-C
16800 front-end. It returns nothing. */
16803 cp_parser_objc_class_declaration (cp_parser* parser)
16805 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16806 objc_declare_class (cp_parser_objc_identifier_list (parser));
16807 cp_parser_consume_semicolon_at_end_of_statement (parser);
16810 /* Parse a list of Objective-C protocol references.
16812 objc-protocol-refs-opt:
16813 objc-protocol-refs [opt]
16815 objc-protocol-refs:
16816 < objc-identifier-list >
16818 Returns a TREE_LIST of identifiers, if any. */
16821 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16823 tree protorefs = NULL_TREE;
16825 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16827 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16828 protorefs = cp_parser_objc_identifier_list (parser);
16829 cp_parser_require (parser, CPP_GREATER, "`>'");
16835 /* Parse a Objective-C visibility specification. */
16838 cp_parser_objc_visibility_spec (cp_parser* parser)
16840 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16842 switch (vis->keyword)
16844 case RID_AT_PRIVATE:
16845 objc_set_visibility (2);
16847 case RID_AT_PROTECTED:
16848 objc_set_visibility (0);
16850 case RID_AT_PUBLIC:
16851 objc_set_visibility (1);
16857 /* Eat '@private'/'@protected'/'@public'. */
16858 cp_lexer_consume_token (parser->lexer);
16861 /* Parse an Objective-C method type. */
16864 cp_parser_objc_method_type (cp_parser* parser)
16866 objc_set_method_type
16867 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16872 /* Parse an Objective-C protocol qualifier. */
16875 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16877 tree quals = NULL_TREE, node;
16878 cp_token *token = cp_lexer_peek_token (parser->lexer);
16880 node = token->value;
16882 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16883 && (node == ridpointers [(int) RID_IN]
16884 || node == ridpointers [(int) RID_OUT]
16885 || node == ridpointers [(int) RID_INOUT]
16886 || node == ridpointers [(int) RID_BYCOPY]
16887 || node == ridpointers [(int) RID_BYREF]
16888 || node == ridpointers [(int) RID_ONEWAY]))
16890 quals = tree_cons (NULL_TREE, node, quals);
16891 cp_lexer_consume_token (parser->lexer);
16892 token = cp_lexer_peek_token (parser->lexer);
16893 node = token->value;
16899 /* Parse an Objective-C typename. */
16902 cp_parser_objc_typename (cp_parser* parser)
16904 tree typename = NULL_TREE;
16906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16908 tree proto_quals, cp_type = NULL_TREE;
16910 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16911 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16913 /* An ObjC type name may consist of just protocol qualifiers, in which
16914 case the type shall default to 'id'. */
16915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16916 cp_type = cp_parser_type_id (parser);
16918 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16919 typename = build_tree_list (proto_quals, cp_type);
16925 /* Check to see if TYPE refers to an Objective-C selector name. */
16928 cp_parser_objc_selector_p (enum cpp_ttype type)
16930 return (type == CPP_NAME || type == CPP_KEYWORD
16931 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16932 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16933 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16934 || type == CPP_XOR || type == CPP_XOR_EQ);
16937 /* Parse an Objective-C selector. */
16940 cp_parser_objc_selector (cp_parser* parser)
16942 cp_token *token = cp_lexer_consume_token (parser->lexer);
16944 if (!cp_parser_objc_selector_p (token->type))
16946 error ("invalid Objective-C++ selector name");
16947 return error_mark_node;
16950 /* C++ operator names are allowed to appear in ObjC selectors. */
16951 switch (token->type)
16953 case CPP_AND_AND: return get_identifier ("and");
16954 case CPP_AND_EQ: return get_identifier ("and_eq");
16955 case CPP_AND: return get_identifier ("bitand");
16956 case CPP_OR: return get_identifier ("bitor");
16957 case CPP_COMPL: return get_identifier ("compl");
16958 case CPP_NOT: return get_identifier ("not");
16959 case CPP_NOT_EQ: return get_identifier ("not_eq");
16960 case CPP_OR_OR: return get_identifier ("or");
16961 case CPP_OR_EQ: return get_identifier ("or_eq");
16962 case CPP_XOR: return get_identifier ("xor");
16963 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16964 default: return token->value;
16968 /* Parse an Objective-C params list. */
16971 cp_parser_objc_method_keyword_params (cp_parser* parser)
16973 tree params = NULL_TREE;
16974 bool maybe_unary_selector_p = true;
16975 cp_token *token = cp_lexer_peek_token (parser->lexer);
16977 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16979 tree selector = NULL_TREE, typename, identifier;
16981 if (token->type != CPP_COLON)
16982 selector = cp_parser_objc_selector (parser);
16984 /* Detect if we have a unary selector. */
16985 if (maybe_unary_selector_p
16986 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16989 maybe_unary_selector_p = false;
16990 cp_parser_require (parser, CPP_COLON, "`:'");
16991 typename = cp_parser_objc_typename (parser);
16992 identifier = cp_parser_identifier (parser);
16996 objc_build_keyword_decl (selector,
17000 token = cp_lexer_peek_token (parser->lexer);
17006 /* Parse the non-keyword Objective-C params. */
17009 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17011 tree params = make_node (TREE_LIST);
17012 cp_token *token = cp_lexer_peek_token (parser->lexer);
17013 *ellipsisp = false; /* Initially, assume no ellipsis. */
17015 while (token->type == CPP_COMMA)
17017 cp_parameter_declarator *parmdecl;
17020 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17021 token = cp_lexer_peek_token (parser->lexer);
17023 if (token->type == CPP_ELLIPSIS)
17025 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17030 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17031 parm = grokdeclarator (parmdecl->declarator,
17032 &parmdecl->decl_specifiers,
17033 PARM, /*initialized=*/0,
17034 /*attrlist=*/NULL);
17036 chainon (params, build_tree_list (NULL_TREE, parm));
17037 token = cp_lexer_peek_token (parser->lexer);
17043 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17046 cp_parser_objc_interstitial_code (cp_parser* parser)
17048 cp_token *token = cp_lexer_peek_token (parser->lexer);
17050 /* If the next token is `extern' and the following token is a string
17051 literal, then we have a linkage specification. */
17052 if (token->keyword == RID_EXTERN
17053 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17054 cp_parser_linkage_specification (parser);
17055 /* Handle #pragma, if any. */
17056 else if (token->type == CPP_PRAGMA)
17057 cp_parser_pragma (parser, pragma_external);
17058 /* Allow stray semicolons. */
17059 else if (token->type == CPP_SEMICOLON)
17060 cp_lexer_consume_token (parser->lexer);
17061 /* Finally, try to parse a block-declaration, or a function-definition. */
17063 cp_parser_block_declaration (parser, /*statement_p=*/false);
17066 /* Parse a method signature. */
17069 cp_parser_objc_method_signature (cp_parser* parser)
17071 tree rettype, kwdparms, optparms;
17072 bool ellipsis = false;
17074 cp_parser_objc_method_type (parser);
17075 rettype = cp_parser_objc_typename (parser);
17076 kwdparms = cp_parser_objc_method_keyword_params (parser);
17077 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17079 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17082 /* Pars an Objective-C method prototype list. */
17085 cp_parser_objc_method_prototype_list (cp_parser* parser)
17087 cp_token *token = cp_lexer_peek_token (parser->lexer);
17089 while (token->keyword != RID_AT_END)
17091 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17093 objc_add_method_declaration
17094 (cp_parser_objc_method_signature (parser));
17095 cp_parser_consume_semicolon_at_end_of_statement (parser);
17098 /* Allow for interspersed non-ObjC++ code. */
17099 cp_parser_objc_interstitial_code (parser);
17101 token = cp_lexer_peek_token (parser->lexer);
17104 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17105 objc_finish_interface ();
17108 /* Parse an Objective-C method definition list. */
17111 cp_parser_objc_method_definition_list (cp_parser* parser)
17113 cp_token *token = cp_lexer_peek_token (parser->lexer);
17115 while (token->keyword != RID_AT_END)
17119 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17121 push_deferring_access_checks (dk_deferred);
17122 objc_start_method_definition
17123 (cp_parser_objc_method_signature (parser));
17125 /* For historical reasons, we accept an optional semicolon. */
17126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17127 cp_lexer_consume_token (parser->lexer);
17129 perform_deferred_access_checks ();
17130 stop_deferring_access_checks ();
17131 meth = cp_parser_function_definition_after_declarator (parser,
17133 pop_deferring_access_checks ();
17134 objc_finish_method_definition (meth);
17137 /* Allow for interspersed non-ObjC++ code. */
17138 cp_parser_objc_interstitial_code (parser);
17140 token = cp_lexer_peek_token (parser->lexer);
17143 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17144 objc_finish_implementation ();
17147 /* Parse Objective-C ivars. */
17150 cp_parser_objc_class_ivars (cp_parser* parser)
17152 cp_token *token = cp_lexer_peek_token (parser->lexer);
17154 if (token->type != CPP_OPEN_BRACE)
17155 return; /* No ivars specified. */
17157 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17158 token = cp_lexer_peek_token (parser->lexer);
17160 while (token->type != CPP_CLOSE_BRACE)
17162 cp_decl_specifier_seq declspecs;
17163 int decl_class_or_enum_p;
17164 tree prefix_attributes;
17166 cp_parser_objc_visibility_spec (parser);
17168 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17171 cp_parser_decl_specifier_seq (parser,
17172 CP_PARSER_FLAGS_OPTIONAL,
17174 &decl_class_or_enum_p);
17175 prefix_attributes = declspecs.attributes;
17176 declspecs.attributes = NULL_TREE;
17178 /* Keep going until we hit the `;' at the end of the
17180 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17182 tree width = NULL_TREE, attributes, first_attribute, decl;
17183 cp_declarator *declarator = NULL;
17184 int ctor_dtor_or_conv_p;
17186 /* Check for a (possibly unnamed) bitfield declaration. */
17187 token = cp_lexer_peek_token (parser->lexer);
17188 if (token->type == CPP_COLON)
17191 if (token->type == CPP_NAME
17192 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17195 /* Get the name of the bitfield. */
17196 declarator = make_id_declarator (NULL_TREE,
17197 cp_parser_identifier (parser),
17201 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17202 /* Get the width of the bitfield. */
17204 = cp_parser_constant_expression (parser,
17205 /*allow_non_constant=*/false,
17210 /* Parse the declarator. */
17212 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17213 &ctor_dtor_or_conv_p,
17214 /*parenthesized_p=*/NULL,
17215 /*member_p=*/false);
17218 /* Look for attributes that apply to the ivar. */
17219 attributes = cp_parser_attributes_opt (parser);
17220 /* Remember which attributes are prefix attributes and
17222 first_attribute = attributes;
17223 /* Combine the attributes. */
17224 attributes = chainon (prefix_attributes, attributes);
17228 /* Create the bitfield declaration. */
17229 decl = grokbitfield (declarator, &declspecs, width);
17230 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17233 decl = grokfield (declarator, &declspecs, NULL_TREE,
17234 NULL_TREE, attributes);
17236 /* Add the instance variable. */
17237 objc_add_instance_variable (decl);
17239 /* Reset PREFIX_ATTRIBUTES. */
17240 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17241 attributes = TREE_CHAIN (attributes);
17243 TREE_CHAIN (attributes) = NULL_TREE;
17245 token = cp_lexer_peek_token (parser->lexer);
17247 if (token->type == CPP_COMMA)
17249 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17255 cp_parser_consume_semicolon_at_end_of_statement (parser);
17256 token = cp_lexer_peek_token (parser->lexer);
17259 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17260 /* For historical reasons, we accept an optional semicolon. */
17261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17262 cp_lexer_consume_token (parser->lexer);
17265 /* Parse an Objective-C protocol declaration. */
17268 cp_parser_objc_protocol_declaration (cp_parser* parser)
17270 tree proto, protorefs;
17273 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17276 error ("identifier expected after %<@protocol%>");
17280 /* See if we have a forward declaration or a definition. */
17281 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17283 /* Try a forward declaration first. */
17284 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17286 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17288 cp_parser_consume_semicolon_at_end_of_statement (parser);
17291 /* Ok, we got a full-fledged definition (or at least should). */
17294 proto = cp_parser_identifier (parser);
17295 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17296 objc_start_protocol (proto, protorefs);
17297 cp_parser_objc_method_prototype_list (parser);
17301 /* Parse an Objective-C superclass or category. */
17304 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17307 cp_token *next = cp_lexer_peek_token (parser->lexer);
17309 *super = *categ = NULL_TREE;
17310 if (next->type == CPP_COLON)
17312 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17313 *super = cp_parser_identifier (parser);
17315 else if (next->type == CPP_OPEN_PAREN)
17317 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17318 *categ = cp_parser_identifier (parser);
17319 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17323 /* Parse an Objective-C class interface. */
17326 cp_parser_objc_class_interface (cp_parser* parser)
17328 tree name, super, categ, protos;
17330 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17331 name = cp_parser_identifier (parser);
17332 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17333 protos = cp_parser_objc_protocol_refs_opt (parser);
17335 /* We have either a class or a category on our hands. */
17337 objc_start_category_interface (name, categ, protos);
17340 objc_start_class_interface (name, super, protos);
17341 /* Handle instance variable declarations, if any. */
17342 cp_parser_objc_class_ivars (parser);
17343 objc_continue_interface ();
17346 cp_parser_objc_method_prototype_list (parser);
17349 /* Parse an Objective-C class implementation. */
17352 cp_parser_objc_class_implementation (cp_parser* parser)
17354 tree name, super, categ;
17356 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17357 name = cp_parser_identifier (parser);
17358 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17360 /* We have either a class or a category on our hands. */
17362 objc_start_category_implementation (name, categ);
17365 objc_start_class_implementation (name, super);
17366 /* Handle instance variable declarations, if any. */
17367 cp_parser_objc_class_ivars (parser);
17368 objc_continue_implementation ();
17371 cp_parser_objc_method_definition_list (parser);
17374 /* Consume the @end token and finish off the implementation. */
17377 cp_parser_objc_end_implementation (cp_parser* parser)
17379 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17380 objc_finish_implementation ();
17383 /* Parse an Objective-C declaration. */
17386 cp_parser_objc_declaration (cp_parser* parser)
17388 /* Try to figure out what kind of declaration is present. */
17389 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17391 switch (kwd->keyword)
17394 cp_parser_objc_alias_declaration (parser);
17397 cp_parser_objc_class_declaration (parser);
17399 case RID_AT_PROTOCOL:
17400 cp_parser_objc_protocol_declaration (parser);
17402 case RID_AT_INTERFACE:
17403 cp_parser_objc_class_interface (parser);
17405 case RID_AT_IMPLEMENTATION:
17406 cp_parser_objc_class_implementation (parser);
17409 cp_parser_objc_end_implementation (parser);
17412 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17413 cp_parser_skip_to_end_of_block_or_statement (parser);
17417 /* Parse an Objective-C try-catch-finally statement.
17419 objc-try-catch-finally-stmt:
17420 @try compound-statement objc-catch-clause-seq [opt]
17421 objc-finally-clause [opt]
17423 objc-catch-clause-seq:
17424 objc-catch-clause objc-catch-clause-seq [opt]
17427 @catch ( exception-declaration ) compound-statement
17429 objc-finally-clause
17430 @finally compound-statement
17432 Returns NULL_TREE. */
17435 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17436 location_t location;
17439 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17440 location = cp_lexer_peek_token (parser->lexer)->location;
17441 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17442 node, lest it get absorbed into the surrounding block. */
17443 stmt = push_stmt_list ();
17444 cp_parser_compound_statement (parser, NULL, false);
17445 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17447 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17449 cp_parameter_declarator *parmdecl;
17452 cp_lexer_consume_token (parser->lexer);
17453 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17454 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17455 parm = grokdeclarator (parmdecl->declarator,
17456 &parmdecl->decl_specifiers,
17457 PARM, /*initialized=*/0,
17458 /*attrlist=*/NULL);
17459 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17460 objc_begin_catch_clause (parm);
17461 cp_parser_compound_statement (parser, NULL, false);
17462 objc_finish_catch_clause ();
17465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17467 cp_lexer_consume_token (parser->lexer);
17468 location = cp_lexer_peek_token (parser->lexer)->location;
17469 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17470 node, lest it get absorbed into the surrounding block. */
17471 stmt = push_stmt_list ();
17472 cp_parser_compound_statement (parser, NULL, false);
17473 objc_build_finally_clause (location, pop_stmt_list (stmt));
17476 return objc_finish_try_stmt ();
17479 /* Parse an Objective-C synchronized statement.
17481 objc-synchronized-stmt:
17482 @synchronized ( expression ) compound-statement
17484 Returns NULL_TREE. */
17487 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17488 location_t location;
17491 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17493 location = cp_lexer_peek_token (parser->lexer)->location;
17494 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17495 lock = cp_parser_expression (parser, false);
17496 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17498 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17499 node, lest it get absorbed into the surrounding block. */
17500 stmt = push_stmt_list ();
17501 cp_parser_compound_statement (parser, NULL, false);
17503 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17506 /* Parse an Objective-C throw statement.
17509 @throw assignment-expression [opt] ;
17511 Returns a constructed '@throw' statement. */
17514 cp_parser_objc_throw_statement (cp_parser *parser) {
17515 tree expr = NULL_TREE;
17517 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17520 expr = cp_parser_assignment_expression (parser, false);
17522 cp_parser_consume_semicolon_at_end_of_statement (parser);
17524 return objc_build_throw_stmt (expr);
17527 /* Parse an Objective-C statement. */
17530 cp_parser_objc_statement (cp_parser * parser) {
17531 /* Try to figure out what kind of declaration is present. */
17532 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17534 switch (kwd->keyword)
17537 return cp_parser_objc_try_catch_finally_statement (parser);
17538 case RID_AT_SYNCHRONIZED:
17539 return cp_parser_objc_synchronized_statement (parser);
17541 return cp_parser_objc_throw_statement (parser);
17543 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17544 cp_parser_skip_to_end_of_block_or_statement (parser);
17547 return error_mark_node;
17551 static GTY (()) cp_parser *the_parser;
17554 /* Special handling for the first token or line in the file. The first
17555 thing in the file might be #pragma GCC pch_preprocess, which loads a
17556 PCH file, which is a GC collection point. So we need to handle this
17557 first pragma without benefit of an existing lexer structure.
17559 Always returns one token to the caller in *FIRST_TOKEN. This is
17560 either the true first token of the file, or the first token after
17561 the initial pragma. */
17564 cp_parser_initial_pragma (cp_token *first_token)
17568 cp_lexer_get_preprocessor_token (NULL, first_token);
17569 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17572 cp_lexer_get_preprocessor_token (NULL, first_token);
17573 if (first_token->type == CPP_STRING)
17575 name = first_token->value;
17577 cp_lexer_get_preprocessor_token (NULL, first_token);
17578 if (first_token->type != CPP_PRAGMA_EOL)
17579 error ("junk at end of %<#pragma GCC pch_preprocess%>");
17582 error ("expected string literal");
17584 /* Skip to the end of the pragma. */
17585 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17586 cp_lexer_get_preprocessor_token (NULL, first_token);
17588 /* Read one more token to return to our caller. */
17589 cp_lexer_get_preprocessor_token (NULL, first_token);
17591 /* Now actually load the PCH file. */
17593 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17596 /* Normal parsing of a pragma token. Here we can (and must) use the
17600 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17602 cp_token *pragma_tok;
17605 pragma_tok = cp_lexer_consume_token (parser->lexer);
17606 gcc_assert (pragma_tok->type == CPP_PRAGMA);
17607 parser->lexer->in_pragma = true;
17609 id = pragma_tok->pragma_kind;
17612 case PRAGMA_GCC_PCH_PREPROCESS:
17613 error ("%<#pragma GCC pch_preprocess%> must be first");
17617 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17618 c_invoke_pragma_handler (id);
17622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17626 /* The interface the pragma parsers have to the lexer. */
17629 pragma_lex (tree *value)
17632 enum cpp_ttype ret;
17634 tok = cp_lexer_peek_token (the_parser->lexer);
17637 *value = tok->value;
17639 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17641 else if (ret == CPP_STRING)
17642 *value = cp_parser_string_literal (the_parser, false, false);
17645 cp_lexer_consume_token (the_parser->lexer);
17646 if (ret == CPP_KEYWORD)
17654 /* External interface. */
17656 /* Parse one entire translation unit. */
17659 c_parse_file (void)
17661 bool error_occurred;
17662 static bool already_called = false;
17664 if (already_called)
17666 sorry ("inter-module optimizations not implemented for C++");
17669 already_called = true;
17671 the_parser = cp_parser_new ();
17672 push_deferring_access_checks (flag_access_control
17673 ? dk_no_deferred : dk_no_check);
17674 error_occurred = cp_parser_translation_unit (the_parser);
17678 /* This variable must be provided by every front end. */
17682 #include "gt-cp-parser.h"