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 cp_token_position start = 0;
3523 /* If the next token corresponds to a nested name specifier, there
3524 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3525 false, it may have been true before, in which case something
3526 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3527 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3528 CHECK_DEPENDENCY_P is false, we have to fall through into the
3530 if (check_dependency_p
3531 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3533 cp_parser_pre_parsed_nested_name_specifier (parser);
3534 return parser->scope;
3537 /* Remember where the nested-name-specifier starts. */
3538 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3540 start = cp_lexer_token_position (parser->lexer, false);
3541 push_deferring_access_checks (dk_deferred);
3548 tree saved_qualifying_scope;
3549 bool template_keyword_p;
3551 /* Spot cases that cannot be the beginning of a
3552 nested-name-specifier. */
3553 token = cp_lexer_peek_token (parser->lexer);
3555 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3556 the already parsed nested-name-specifier. */
3557 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3559 /* Grab the nested-name-specifier and continue the loop. */
3560 cp_parser_pre_parsed_nested_name_specifier (parser);
3565 /* Spot cases that cannot be the beginning of a
3566 nested-name-specifier. On the second and subsequent times
3567 through the loop, we look for the `template' keyword. */
3568 if (success && token->keyword == RID_TEMPLATE)
3570 /* A template-id can start a nested-name-specifier. */
3571 else if (token->type == CPP_TEMPLATE_ID)
3575 /* If the next token is not an identifier, then it is
3576 definitely not a class-or-namespace-name. */
3577 if (token->type != CPP_NAME)
3579 /* If the following token is neither a `<' (to begin a
3580 template-id), nor a `::', then we are not looking at a
3581 nested-name-specifier. */
3582 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3583 if (token->type != CPP_SCOPE
3584 && !cp_parser_nth_token_starts_template_argument_list_p
3589 /* The nested-name-specifier is optional, so we parse
3591 cp_parser_parse_tentatively (parser);
3593 /* Look for the optional `template' keyword, if this isn't the
3594 first time through the loop. */
3596 template_keyword_p = cp_parser_optional_template_keyword (parser);
3598 template_keyword_p = false;
3600 /* Save the old scope since the name lookup we are about to do
3601 might destroy it. */
3602 old_scope = parser->scope;
3603 saved_qualifying_scope = parser->qualifying_scope;
3604 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3605 look up names in "X<T>::I" in order to determine that "Y" is
3606 a template. So, if we have a typename at this point, we make
3607 an effort to look through it. */
3609 && !typename_keyword_p
3611 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3612 parser->scope = resolve_typename_type (parser->scope,
3613 /*only_current_p=*/false);
3614 /* Parse the qualifying entity. */
3616 = cp_parser_class_or_namespace_name (parser,
3622 /* Look for the `::' token. */
3623 cp_parser_require (parser, CPP_SCOPE, "`::'");
3625 /* If we found what we wanted, we keep going; otherwise, we're
3627 if (!cp_parser_parse_definitely (parser))
3629 bool error_p = false;
3631 /* Restore the OLD_SCOPE since it was valid before the
3632 failed attempt at finding the last
3633 class-or-namespace-name. */
3634 parser->scope = old_scope;
3635 parser->qualifying_scope = saved_qualifying_scope;
3636 /* If the next token is an identifier, and the one after
3637 that is a `::', then any valid interpretation would have
3638 found a class-or-namespace-name. */
3639 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3640 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3642 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3645 token = cp_lexer_consume_token (parser->lexer);
3648 if (!token->ambiguous_p)
3651 tree ambiguous_decls;
3653 decl = cp_parser_lookup_name (parser, token->value,
3655 /*is_template=*/false,
3656 /*is_namespace=*/false,
3657 /*check_dependency=*/true,
3659 if (TREE_CODE (decl) == TEMPLATE_DECL)
3660 error ("%qD used without template parameters", decl);
3661 else if (ambiguous_decls)
3663 error ("reference to %qD is ambiguous",
3665 print_candidates (ambiguous_decls);
3666 decl = error_mark_node;
3669 cp_parser_name_lookup_error
3670 (parser, token->value, decl,
3671 "is not a class or namespace");
3673 parser->scope = error_mark_node;
3675 /* Treat this as a successful nested-name-specifier
3680 If the name found is not a class-name (clause
3681 _class_) or namespace-name (_namespace.def_), the
3682 program is ill-formed. */
3685 cp_lexer_consume_token (parser->lexer);
3689 /* We've found one valid nested-name-specifier. */
3691 /* Name lookup always gives us a DECL. */
3692 if (TREE_CODE (new_scope) == TYPE_DECL)
3693 new_scope = TREE_TYPE (new_scope);
3694 /* Uses of "template" must be followed by actual templates. */
3695 if (template_keyword_p
3696 && !(CLASS_TYPE_P (new_scope)
3697 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3699 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3700 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3701 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3702 == TEMPLATE_ID_EXPR)))
3703 pedwarn (TYPE_P (new_scope)
3704 ? "%qT is not a template"
3705 : "%qD is not a template",
3707 /* If it is a class scope, try to complete it; we are about to
3708 be looking up names inside the class. */
3709 if (TYPE_P (new_scope)
3710 /* Since checking types for dependency can be expensive,
3711 avoid doing it if the type is already complete. */
3712 && !COMPLETE_TYPE_P (new_scope)
3713 /* Do not try to complete dependent types. */
3714 && !dependent_type_p (new_scope))
3715 new_scope = complete_type (new_scope);
3716 /* Make sure we look in the right scope the next time through
3718 parser->scope = new_scope;
3721 /* If parsing tentatively, replace the sequence of tokens that makes
3722 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3723 token. That way, should we re-parse the token stream, we will
3724 not have to repeat the effort required to do the parse, nor will
3725 we issue duplicate error messages. */
3726 if (success && start)
3731 token = cp_lexer_token_at (parser->lexer, start);
3732 /* Reset the contents of the START token. */
3733 token->type = CPP_NESTED_NAME_SPECIFIER;
3734 /* Retrieve any deferred checks. Do not pop this access checks yet
3735 so the memory will not be reclaimed during token replacing below. */
3736 access_checks = get_deferred_access_checks ();
3737 token->value = build_tree_list (copy_list (access_checks),
3739 TREE_TYPE (token->value) = parser->qualifying_scope;
3740 token->keyword = RID_MAX;
3742 /* Purge all subsequent tokens. */
3743 cp_lexer_purge_tokens_after (parser->lexer, start);
3747 pop_to_parent_deferring_access_checks ();
3749 return success ? parser->scope : NULL_TREE;
3752 /* Parse a nested-name-specifier. See
3753 cp_parser_nested_name_specifier_opt for details. This function
3754 behaves identically, except that it will an issue an error if no
3755 nested-name-specifier is present. */
3758 cp_parser_nested_name_specifier (cp_parser *parser,
3759 bool typename_keyword_p,
3760 bool check_dependency_p,
3762 bool is_declaration)
3766 /* Look for the nested-name-specifier. */
3767 scope = cp_parser_nested_name_specifier_opt (parser,
3772 /* If it was not present, issue an error message. */
3775 cp_parser_error (parser, "expected nested-name-specifier");
3776 parser->scope = NULL_TREE;
3782 /* Parse a class-or-namespace-name.
3784 class-or-namespace-name:
3788 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3789 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3790 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3791 TYPE_P is TRUE iff the next name should be taken as a class-name,
3792 even the same name is declared to be another entity in the same
3795 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3796 specified by the class-or-namespace-name. If neither is found the
3797 ERROR_MARK_NODE is returned. */
3800 cp_parser_class_or_namespace_name (cp_parser *parser,
3801 bool typename_keyword_p,
3802 bool template_keyword_p,
3803 bool check_dependency_p,
3805 bool is_declaration)
3808 tree saved_qualifying_scope;
3809 tree saved_object_scope;
3813 /* Before we try to parse the class-name, we must save away the
3814 current PARSER->SCOPE since cp_parser_class_name will destroy
3816 saved_scope = parser->scope;
3817 saved_qualifying_scope = parser->qualifying_scope;
3818 saved_object_scope = parser->object_scope;
3819 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3820 there is no need to look for a namespace-name. */
3821 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3823 cp_parser_parse_tentatively (parser);
3824 scope = cp_parser_class_name (parser,
3827 type_p ? class_type : none_type,
3829 /*class_head_p=*/false,
3831 /* If that didn't work, try for a namespace-name. */
3832 if (!only_class_p && !cp_parser_parse_definitely (parser))
3834 /* Restore the saved scope. */
3835 parser->scope = saved_scope;
3836 parser->qualifying_scope = saved_qualifying_scope;
3837 parser->object_scope = saved_object_scope;
3838 /* If we are not looking at an identifier followed by the scope
3839 resolution operator, then this is not part of a
3840 nested-name-specifier. (Note that this function is only used
3841 to parse the components of a nested-name-specifier.) */
3842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3843 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3844 return error_mark_node;
3845 scope = cp_parser_namespace_name (parser);
3851 /* Parse a postfix-expression.
3855 postfix-expression [ expression ]
3856 postfix-expression ( expression-list [opt] )
3857 simple-type-specifier ( expression-list [opt] )
3858 typename :: [opt] nested-name-specifier identifier
3859 ( expression-list [opt] )
3860 typename :: [opt] nested-name-specifier template [opt] template-id
3861 ( expression-list [opt] )
3862 postfix-expression . template [opt] id-expression
3863 postfix-expression -> template [opt] id-expression
3864 postfix-expression . pseudo-destructor-name
3865 postfix-expression -> pseudo-destructor-name
3866 postfix-expression ++
3867 postfix-expression --
3868 dynamic_cast < type-id > ( expression )
3869 static_cast < type-id > ( expression )
3870 reinterpret_cast < type-id > ( expression )
3871 const_cast < type-id > ( expression )
3872 typeid ( expression )
3878 ( type-id ) { initializer-list , [opt] }
3880 This extension is a GNU version of the C99 compound-literal
3881 construct. (The C99 grammar uses `type-name' instead of `type-id',
3882 but they are essentially the same concept.)
3884 If ADDRESS_P is true, the postfix expression is the operand of the
3885 `&' operator. CAST_P is true if this expression is the target of a
3888 Returns a representation of the expression. */
3891 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3895 cp_id_kind idk = CP_ID_KIND_NONE;
3896 tree postfix_expression = NULL_TREE;
3898 /* Peek at the next token. */
3899 token = cp_lexer_peek_token (parser->lexer);
3900 /* Some of the productions are determined by keywords. */
3901 keyword = token->keyword;
3911 const char *saved_message;
3913 /* All of these can be handled in the same way from the point
3914 of view of parsing. Begin by consuming the token
3915 identifying the cast. */
3916 cp_lexer_consume_token (parser->lexer);
3918 /* New types cannot be defined in the cast. */
3919 saved_message = parser->type_definition_forbidden_message;
3920 parser->type_definition_forbidden_message
3921 = "types may not be defined in casts";
3923 /* Look for the opening `<'. */
3924 cp_parser_require (parser, CPP_LESS, "`<'");
3925 /* Parse the type to which we are casting. */
3926 type = cp_parser_type_id (parser);
3927 /* Look for the closing `>'. */
3928 cp_parser_require (parser, CPP_GREATER, "`>'");
3929 /* Restore the old message. */
3930 parser->type_definition_forbidden_message = saved_message;
3932 /* And the expression which is being cast. */
3933 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3934 expression = cp_parser_expression (parser, /*cast_p=*/true);
3935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3937 /* Only type conversions to integral or enumeration types
3938 can be used in constant-expressions. */
3939 if (parser->integral_constant_expression_p
3940 && !dependent_type_p (type)
3941 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3942 && (cp_parser_non_integral_constant_expression
3944 "a cast to a type other than an integral or "
3945 "enumeration type")))
3946 return error_mark_node;
3952 = build_dynamic_cast (type, expression);
3956 = build_static_cast (type, expression);
3960 = build_reinterpret_cast (type, expression);
3964 = build_const_cast (type, expression);
3975 const char *saved_message;
3976 bool saved_in_type_id_in_expr_p;
3978 /* Consume the `typeid' token. */
3979 cp_lexer_consume_token (parser->lexer);
3980 /* Look for the `(' token. */
3981 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3982 /* Types cannot be defined in a `typeid' expression. */
3983 saved_message = parser->type_definition_forbidden_message;
3984 parser->type_definition_forbidden_message
3985 = "types may not be defined in a `typeid\' expression";
3986 /* We can't be sure yet whether we're looking at a type-id or an
3988 cp_parser_parse_tentatively (parser);
3989 /* Try a type-id first. */
3990 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3991 parser->in_type_id_in_expr_p = true;
3992 type = cp_parser_type_id (parser);
3993 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3994 /* Look for the `)' token. Otherwise, we can't be sure that
3995 we're not looking at an expression: consider `typeid (int
3996 (3))', for example. */
3997 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3998 /* If all went well, simply lookup the type-id. */
3999 if (cp_parser_parse_definitely (parser))
4000 postfix_expression = get_typeid (type);
4001 /* Otherwise, fall back to the expression variant. */
4006 /* Look for an expression. */
4007 expression = cp_parser_expression (parser, /*cast_p=*/false);
4008 /* Compute its typeid. */
4009 postfix_expression = build_typeid (expression);
4010 /* Look for the `)' token. */
4011 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4013 /* `typeid' may not appear in an integral constant expression. */
4014 if (cp_parser_non_integral_constant_expression(parser,
4015 "`typeid' operator"))
4016 return error_mark_node;
4017 /* Restore the saved message. */
4018 parser->type_definition_forbidden_message = saved_message;
4025 /* The syntax permitted here is the same permitted for an
4026 elaborated-type-specifier. */
4027 type = cp_parser_elaborated_type_specifier (parser,
4028 /*is_friend=*/false,
4029 /*is_declaration=*/false);
4030 postfix_expression = cp_parser_functional_cast (parser, type);
4038 /* If the next thing is a simple-type-specifier, we may be
4039 looking at a functional cast. We could also be looking at
4040 an id-expression. So, we try the functional cast, and if
4041 that doesn't work we fall back to the primary-expression. */
4042 cp_parser_parse_tentatively (parser);
4043 /* Look for the simple-type-specifier. */
4044 type = cp_parser_simple_type_specifier (parser,
4045 /*decl_specs=*/NULL,
4046 CP_PARSER_FLAGS_NONE);
4047 /* Parse the cast itself. */
4048 if (!cp_parser_error_occurred (parser))
4050 = cp_parser_functional_cast (parser, type);
4051 /* If that worked, we're done. */
4052 if (cp_parser_parse_definitely (parser))
4055 /* If the functional-cast didn't work out, try a
4056 compound-literal. */
4057 if (cp_parser_allow_gnu_extensions_p (parser)
4058 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4060 VEC(constructor_elt,gc) *initializer_list = NULL;
4061 bool saved_in_type_id_in_expr_p;
4063 cp_parser_parse_tentatively (parser);
4064 /* Consume the `('. */
4065 cp_lexer_consume_token (parser->lexer);
4066 /* Parse the type. */
4067 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4068 parser->in_type_id_in_expr_p = true;
4069 type = cp_parser_type_id (parser);
4070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4071 /* Look for the `)'. */
4072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4073 /* Look for the `{'. */
4074 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4075 /* If things aren't going well, there's no need to
4077 if (!cp_parser_error_occurred (parser))
4079 bool non_constant_p;
4080 /* Parse the initializer-list. */
4082 = cp_parser_initializer_list (parser, &non_constant_p);
4083 /* Allow a trailing `,'. */
4084 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4085 cp_lexer_consume_token (parser->lexer);
4086 /* Look for the final `}'. */
4087 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4089 /* If that worked, we're definitely looking at a
4090 compound-literal expression. */
4091 if (cp_parser_parse_definitely (parser))
4093 /* Warn the user that a compound literal is not
4094 allowed in standard C++. */
4096 pedwarn ("ISO C++ forbids compound-literals");
4097 /* Form the representation of the compound-literal. */
4099 = finish_compound_literal (type, initializer_list);
4104 /* It must be a primary-expression. */
4106 = cp_parser_primary_expression (parser, address_p, cast_p,
4107 /*template_arg_p=*/false,
4113 /* Keep looping until the postfix-expression is complete. */
4116 if (idk == CP_ID_KIND_UNQUALIFIED
4117 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4118 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4119 /* It is not a Koenig lookup function call. */
4121 = unqualified_name_lookup_error (postfix_expression);
4123 /* Peek at the next token. */
4124 token = cp_lexer_peek_token (parser->lexer);
4126 switch (token->type)
4128 case CPP_OPEN_SQUARE:
4130 = cp_parser_postfix_open_square_expression (parser,
4133 idk = CP_ID_KIND_NONE;
4136 case CPP_OPEN_PAREN:
4137 /* postfix-expression ( expression-list [opt] ) */
4140 bool is_builtin_constant_p;
4141 bool saved_integral_constant_expression_p = false;
4142 bool saved_non_integral_constant_expression_p = false;
4145 is_builtin_constant_p
4146 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4147 if (is_builtin_constant_p)
4149 /* The whole point of __builtin_constant_p is to allow
4150 non-constant expressions to appear as arguments. */
4151 saved_integral_constant_expression_p
4152 = parser->integral_constant_expression_p;
4153 saved_non_integral_constant_expression_p
4154 = parser->non_integral_constant_expression_p;
4155 parser->integral_constant_expression_p = false;
4157 args = (cp_parser_parenthesized_expression_list
4158 (parser, /*is_attribute_list=*/false,
4160 /*non_constant_p=*/NULL));
4161 if (is_builtin_constant_p)
4163 parser->integral_constant_expression_p
4164 = saved_integral_constant_expression_p;
4165 parser->non_integral_constant_expression_p
4166 = saved_non_integral_constant_expression_p;
4169 if (args == error_mark_node)
4171 postfix_expression = error_mark_node;
4175 /* Function calls are not permitted in
4176 constant-expressions. */
4177 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4178 && cp_parser_non_integral_constant_expression (parser,
4181 postfix_expression = error_mark_node;
4186 if (idk == CP_ID_KIND_UNQUALIFIED)
4188 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4194 = perform_koenig_lookup (postfix_expression, args);
4198 = unqualified_fn_lookup_error (postfix_expression);
4200 /* We do not perform argument-dependent lookup if
4201 normal lookup finds a non-function, in accordance
4202 with the expected resolution of DR 218. */
4203 else if (args && is_overloaded_fn (postfix_expression))
4205 tree fn = get_first_fn (postfix_expression);
4207 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4208 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4210 /* Only do argument dependent lookup if regular
4211 lookup does not find a set of member functions.
4212 [basic.lookup.koenig]/2a */
4213 if (!DECL_FUNCTION_MEMBER_P (fn))
4217 = perform_koenig_lookup (postfix_expression, args);
4222 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4224 tree instance = TREE_OPERAND (postfix_expression, 0);
4225 tree fn = TREE_OPERAND (postfix_expression, 1);
4227 if (processing_template_decl
4228 && (type_dependent_expression_p (instance)
4229 || (!BASELINK_P (fn)
4230 && TREE_CODE (fn) != FIELD_DECL)
4231 || type_dependent_expression_p (fn)
4232 || any_type_dependent_arguments_p (args)))
4235 = build_min_nt (CALL_EXPR, postfix_expression,
4240 if (BASELINK_P (fn))
4242 = (build_new_method_call
4243 (instance, fn, args, NULL_TREE,
4244 (idk == CP_ID_KIND_QUALIFIED
4245 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4248 = finish_call_expr (postfix_expression, args,
4249 /*disallow_virtual=*/false,
4250 /*koenig_p=*/false);
4252 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4253 || TREE_CODE (postfix_expression) == MEMBER_REF
4254 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4255 postfix_expression = (build_offset_ref_call_from_tree
4256 (postfix_expression, args));
4257 else if (idk == CP_ID_KIND_QUALIFIED)
4258 /* A call to a static class member, or a namespace-scope
4261 = finish_call_expr (postfix_expression, args,
4262 /*disallow_virtual=*/true,
4265 /* All other function calls. */
4267 = finish_call_expr (postfix_expression, args,
4268 /*disallow_virtual=*/false,
4271 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4272 idk = CP_ID_KIND_NONE;
4278 /* postfix-expression . template [opt] id-expression
4279 postfix-expression . pseudo-destructor-name
4280 postfix-expression -> template [opt] id-expression
4281 postfix-expression -> pseudo-destructor-name */
4283 /* Consume the `.' or `->' operator. */
4284 cp_lexer_consume_token (parser->lexer);
4287 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4293 /* postfix-expression ++ */
4294 /* Consume the `++' token. */
4295 cp_lexer_consume_token (parser->lexer);
4296 /* Generate a representation for the complete expression. */
4298 = finish_increment_expr (postfix_expression,
4299 POSTINCREMENT_EXPR);
4300 /* Increments may not appear in constant-expressions. */
4301 if (cp_parser_non_integral_constant_expression (parser,
4303 postfix_expression = error_mark_node;
4304 idk = CP_ID_KIND_NONE;
4307 case CPP_MINUS_MINUS:
4308 /* postfix-expression -- */
4309 /* Consume the `--' token. */
4310 cp_lexer_consume_token (parser->lexer);
4311 /* Generate a representation for the complete expression. */
4313 = finish_increment_expr (postfix_expression,
4314 POSTDECREMENT_EXPR);
4315 /* Decrements may not appear in constant-expressions. */
4316 if (cp_parser_non_integral_constant_expression (parser,
4318 postfix_expression = error_mark_node;
4319 idk = CP_ID_KIND_NONE;
4323 return postfix_expression;
4327 /* We should never get here. */
4329 return error_mark_node;
4332 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4333 by cp_parser_builtin_offsetof. We're looking for
4335 postfix-expression [ expression ]
4337 FOR_OFFSETOF is set if we're being called in that context, which
4338 changes how we deal with integer constant expressions. */
4341 cp_parser_postfix_open_square_expression (cp_parser *parser,
4342 tree postfix_expression,
4347 /* Consume the `[' token. */
4348 cp_lexer_consume_token (parser->lexer);
4350 /* Parse the index expression. */
4351 /* ??? For offsetof, there is a question of what to allow here. If
4352 offsetof is not being used in an integral constant expression context,
4353 then we *could* get the right answer by computing the value at runtime.
4354 If we are in an integral constant expression context, then we might
4355 could accept any constant expression; hard to say without analysis.
4356 Rather than open the barn door too wide right away, allow only integer
4357 constant expressions here. */
4359 index = cp_parser_constant_expression (parser, false, NULL);
4361 index = cp_parser_expression (parser, /*cast_p=*/false);
4363 /* Look for the closing `]'. */
4364 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4366 /* Build the ARRAY_REF. */
4367 postfix_expression = grok_array_decl (postfix_expression, index);
4369 /* When not doing offsetof, array references are not permitted in
4370 constant-expressions. */
4372 && (cp_parser_non_integral_constant_expression
4373 (parser, "an array reference")))
4374 postfix_expression = error_mark_node;
4376 return postfix_expression;
4379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4380 by cp_parser_builtin_offsetof. We're looking for
4382 postfix-expression . template [opt] id-expression
4383 postfix-expression . pseudo-destructor-name
4384 postfix-expression -> template [opt] id-expression
4385 postfix-expression -> pseudo-destructor-name
4387 FOR_OFFSETOF is set if we're being called in that context. That sorta
4388 limits what of the above we'll actually accept, but nevermind.
4389 TOKEN_TYPE is the "." or "->" token, which will already have been
4390 removed from the stream. */
4393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4394 enum cpp_ttype token_type,
4395 tree postfix_expression,
4396 bool for_offsetof, cp_id_kind *idk)
4400 bool pseudo_destructor_p;
4401 tree scope = NULL_TREE;
4403 /* If this is a `->' operator, dereference the pointer. */
4404 if (token_type == CPP_DEREF)
4405 postfix_expression = build_x_arrow (postfix_expression);
4406 /* Check to see whether or not the expression is type-dependent. */
4407 dependent_p = type_dependent_expression_p (postfix_expression);
4408 /* The identifier following the `->' or `.' is not qualified. */
4409 parser->scope = NULL_TREE;
4410 parser->qualifying_scope = NULL_TREE;
4411 parser->object_scope = NULL_TREE;
4412 *idk = CP_ID_KIND_NONE;
4413 /* Enter the scope corresponding to the type of the object
4414 given by the POSTFIX_EXPRESSION. */
4415 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4417 scope = TREE_TYPE (postfix_expression);
4418 /* According to the standard, no expression should ever have
4419 reference type. Unfortunately, we do not currently match
4420 the standard in this respect in that our internal representation
4421 of an expression may have reference type even when the standard
4422 says it does not. Therefore, we have to manually obtain the
4423 underlying type here. */
4424 scope = non_reference (scope);
4425 /* The type of the POSTFIX_EXPRESSION must be complete. */
4426 if (scope == unknown_type_node)
4428 error ("%qE does not have class type", postfix_expression);
4432 scope = complete_type_or_else (scope, NULL_TREE);
4433 /* Let the name lookup machinery know that we are processing a
4434 class member access expression. */
4435 parser->context->object_type = scope;
4436 /* If something went wrong, we want to be able to discern that case,
4437 as opposed to the case where there was no SCOPE due to the type
4438 of expression being dependent. */
4440 scope = error_mark_node;
4441 /* If the SCOPE was erroneous, make the various semantic analysis
4442 functions exit quickly -- and without issuing additional error
4444 if (scope == error_mark_node)
4445 postfix_expression = error_mark_node;
4448 /* Assume this expression is not a pseudo-destructor access. */
4449 pseudo_destructor_p = false;
4451 /* If the SCOPE is a scalar type, then, if this is a valid program,
4452 we must be looking at a pseudo-destructor-name. */
4453 if (scope && SCALAR_TYPE_P (scope))
4458 cp_parser_parse_tentatively (parser);
4459 /* Parse the pseudo-destructor-name. */
4461 cp_parser_pseudo_destructor_name (parser, &s, &type);
4462 if (cp_parser_parse_definitely (parser))
4464 pseudo_destructor_p = true;
4466 = finish_pseudo_destructor_expr (postfix_expression,
4467 s, TREE_TYPE (type));
4471 if (!pseudo_destructor_p)
4473 /* If the SCOPE is not a scalar type, we are looking at an
4474 ordinary class member access expression, rather than a
4475 pseudo-destructor-name. */
4477 /* Parse the id-expression. */
4478 name = (cp_parser_id_expression
4480 cp_parser_optional_template_keyword (parser),
4481 /*check_dependency_p=*/true,
4483 /*declarator_p=*/false));
4484 /* In general, build a SCOPE_REF if the member name is qualified.
4485 However, if the name was not dependent and has already been
4486 resolved; there is no need to build the SCOPE_REF. For example;
4488 struct X { void f(); };
4489 template <typename T> void f(T* t) { t->X::f(); }
4491 Even though "t" is dependent, "X::f" is not and has been resolved
4492 to a BASELINK; there is no need to include scope information. */
4494 /* But we do need to remember that there was an explicit scope for
4495 virtual function calls. */
4497 *idk = CP_ID_KIND_QUALIFIED;
4499 /* If the name is a template-id that names a type, we will get a
4500 TYPE_DECL here. That is invalid code. */
4501 if (TREE_CODE (name) == TYPE_DECL)
4503 error ("invalid use of %qD", name);
4504 postfix_expression = error_mark_node;
4508 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4510 name = build_qualified_name (/*type=*/NULL_TREE,
4514 parser->scope = NULL_TREE;
4515 parser->qualifying_scope = NULL_TREE;
4516 parser->object_scope = NULL_TREE;
4518 if (scope && name && BASELINK_P (name))
4519 adjust_result_of_qualified_name_lookup
4520 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4522 = finish_class_member_access_expr (postfix_expression, name,
4527 /* We no longer need to look up names in the scope of the object on
4528 the left-hand side of the `.' or `->' operator. */
4529 parser->context->object_type = NULL_TREE;
4531 /* Outside of offsetof, these operators may not appear in
4532 constant-expressions. */
4534 && (cp_parser_non_integral_constant_expression
4535 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4536 postfix_expression = error_mark_node;
4538 return postfix_expression;
4541 /* Parse a parenthesized expression-list.
4544 assignment-expression
4545 expression-list, assignment-expression
4550 identifier, expression-list
4552 CAST_P is true if this expression is the target of a cast.
4554 Returns a TREE_LIST. The TREE_VALUE of each node is a
4555 representation of an assignment-expression. Note that a TREE_LIST
4556 is returned even if there is only a single expression in the list.
4557 error_mark_node is returned if the ( and or ) are
4558 missing. NULL_TREE is returned on no expressions. The parentheses
4559 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4560 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4561 indicates whether or not all of the expressions in the list were
4565 cp_parser_parenthesized_expression_list (cp_parser* parser,
4566 bool is_attribute_list,
4568 bool *non_constant_p)
4570 tree expression_list = NULL_TREE;
4571 bool fold_expr_p = is_attribute_list;
4572 tree identifier = NULL_TREE;
4574 /* Assume all the expressions will be constant. */
4576 *non_constant_p = false;
4578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4579 return error_mark_node;
4581 /* Consume expressions until there are no more. */
4582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4587 /* At the beginning of attribute lists, check to see if the
4588 next token is an identifier. */
4589 if (is_attribute_list
4590 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4594 /* Consume the identifier. */
4595 token = cp_lexer_consume_token (parser->lexer);
4596 /* Save the identifier. */
4597 identifier = token->value;
4601 /* Parse the next assignment-expression. */
4604 bool expr_non_constant_p;
4605 expr = (cp_parser_constant_expression
4606 (parser, /*allow_non_constant_p=*/true,
4607 &expr_non_constant_p));
4608 if (expr_non_constant_p)
4609 *non_constant_p = true;
4612 expr = cp_parser_assignment_expression (parser, cast_p);
4615 expr = fold_non_dependent_expr (expr);
4617 /* Add it to the list. We add error_mark_node
4618 expressions to the list, so that we can still tell if
4619 the correct form for a parenthesized expression-list
4620 is found. That gives better errors. */
4621 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4623 if (expr == error_mark_node)
4627 /* After the first item, attribute lists look the same as
4628 expression lists. */
4629 is_attribute_list = false;
4632 /* If the next token isn't a `,', then we are done. */
4633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4636 /* Otherwise, consume the `,' and keep going. */
4637 cp_lexer_consume_token (parser->lexer);
4640 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4645 /* We try and resync to an unnested comma, as that will give the
4646 user better diagnostics. */
4647 ending = cp_parser_skip_to_closing_parenthesis (parser,
4648 /*recovering=*/true,
4650 /*consume_paren=*/true);
4654 return error_mark_node;
4657 /* We built up the list in reverse order so we must reverse it now. */
4658 expression_list = nreverse (expression_list);
4660 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4662 return expression_list;
4665 /* Parse a pseudo-destructor-name.
4667 pseudo-destructor-name:
4668 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4669 :: [opt] nested-name-specifier template template-id :: ~ type-name
4670 :: [opt] nested-name-specifier [opt] ~ type-name
4672 If either of the first two productions is used, sets *SCOPE to the
4673 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4674 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4675 or ERROR_MARK_NODE if the parse fails. */
4678 cp_parser_pseudo_destructor_name (cp_parser* parser,
4682 bool nested_name_specifier_p;
4684 /* Assume that things will not work out. */
4685 *type = error_mark_node;
4687 /* Look for the optional `::' operator. */
4688 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4689 /* Look for the optional nested-name-specifier. */
4690 nested_name_specifier_p
4691 = (cp_parser_nested_name_specifier_opt (parser,
4692 /*typename_keyword_p=*/false,
4693 /*check_dependency_p=*/true,
4695 /*is_declaration=*/true)
4697 /* Now, if we saw a nested-name-specifier, we might be doing the
4698 second production. */
4699 if (nested_name_specifier_p
4700 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4702 /* Consume the `template' keyword. */
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Parse the template-id. */
4705 cp_parser_template_id (parser,
4706 /*template_keyword_p=*/true,
4707 /*check_dependency_p=*/false,
4708 /*is_declaration=*/true);
4709 /* Look for the `::' token. */
4710 cp_parser_require (parser, CPP_SCOPE, "`::'");
4712 /* If the next token is not a `~', then there might be some
4713 additional qualification. */
4714 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4716 /* Look for the type-name. */
4717 *scope = TREE_TYPE (cp_parser_type_name (parser));
4719 if (*scope == error_mark_node)
4722 /* If we don't have ::~, then something has gone wrong. Since
4723 the only caller of this function is looking for something
4724 after `.' or `->' after a scalar type, most likely the
4725 program is trying to get a member of a non-aggregate
4727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4728 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4730 cp_parser_error (parser, "request for member of non-aggregate type");
4734 /* Look for the `::' token. */
4735 cp_parser_require (parser, CPP_SCOPE, "`::'");
4740 /* Look for the `~'. */
4741 cp_parser_require (parser, CPP_COMPL, "`~'");
4742 /* Look for the type-name again. We are not responsible for
4743 checking that it matches the first type-name. */
4744 *type = cp_parser_type_name (parser);
4747 /* Parse a unary-expression.
4753 unary-operator cast-expression
4754 sizeof unary-expression
4762 __extension__ cast-expression
4763 __alignof__ unary-expression
4764 __alignof__ ( type-id )
4765 __real__ cast-expression
4766 __imag__ cast-expression
4769 ADDRESS_P is true iff the unary-expression is appearing as the
4770 operand of the `&' operator. CAST_P is true if this expression is
4771 the target of a cast.
4773 Returns a representation of the expression. */
4776 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4779 enum tree_code unary_operator;
4781 /* Peek at the next token. */
4782 token = cp_lexer_peek_token (parser->lexer);
4783 /* Some keywords give away the kind of expression. */
4784 if (token->type == CPP_KEYWORD)
4786 enum rid keyword = token->keyword;
4796 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4797 /* Consume the token. */
4798 cp_lexer_consume_token (parser->lexer);
4799 /* Parse the operand. */
4800 operand = cp_parser_sizeof_operand (parser, keyword);
4802 if (TYPE_P (operand))
4803 return cxx_sizeof_or_alignof_type (operand, op, true);
4805 return cxx_sizeof_or_alignof_expr (operand, op);
4809 return cp_parser_new_expression (parser);
4812 return cp_parser_delete_expression (parser);
4816 /* The saved value of the PEDANTIC flag. */
4820 /* Save away the PEDANTIC flag. */
4821 cp_parser_extension_opt (parser, &saved_pedantic);
4822 /* Parse the cast-expression. */
4823 expr = cp_parser_simple_cast_expression (parser);
4824 /* Restore the PEDANTIC flag. */
4825 pedantic = saved_pedantic;
4835 /* Consume the `__real__' or `__imag__' token. */
4836 cp_lexer_consume_token (parser->lexer);
4837 /* Parse the cast-expression. */
4838 expression = cp_parser_simple_cast_expression (parser);
4839 /* Create the complete representation. */
4840 return build_x_unary_op ((keyword == RID_REALPART
4841 ? REALPART_EXPR : IMAGPART_EXPR),
4851 /* Look for the `:: new' and `:: delete', which also signal the
4852 beginning of a new-expression, or delete-expression,
4853 respectively. If the next token is `::', then it might be one of
4855 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4859 /* See if the token after the `::' is one of the keywords in
4860 which we're interested. */
4861 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4862 /* If it's `new', we have a new-expression. */
4863 if (keyword == RID_NEW)
4864 return cp_parser_new_expression (parser);
4865 /* Similarly, for `delete'. */
4866 else if (keyword == RID_DELETE)
4867 return cp_parser_delete_expression (parser);
4870 /* Look for a unary operator. */
4871 unary_operator = cp_parser_unary_operator (token);
4872 /* The `++' and `--' operators can be handled similarly, even though
4873 they are not technically unary-operators in the grammar. */
4874 if (unary_operator == ERROR_MARK)
4876 if (token->type == CPP_PLUS_PLUS)
4877 unary_operator = PREINCREMENT_EXPR;
4878 else if (token->type == CPP_MINUS_MINUS)
4879 unary_operator = PREDECREMENT_EXPR;
4880 /* Handle the GNU address-of-label extension. */
4881 else if (cp_parser_allow_gnu_extensions_p (parser)
4882 && token->type == CPP_AND_AND)
4886 /* Consume the '&&' token. */
4887 cp_lexer_consume_token (parser->lexer);
4888 /* Look for the identifier. */
4889 identifier = cp_parser_identifier (parser);
4890 /* Create an expression representing the address. */
4891 return finish_label_address_expr (identifier);
4894 if (unary_operator != ERROR_MARK)
4896 tree cast_expression;
4897 tree expression = error_mark_node;
4898 const char *non_constant_p = NULL;
4900 /* Consume the operator token. */
4901 token = cp_lexer_consume_token (parser->lexer);
4902 /* Parse the cast-expression. */
4904 = cp_parser_cast_expression (parser,
4905 unary_operator == ADDR_EXPR,
4907 /* Now, build an appropriate representation. */
4908 switch (unary_operator)
4911 non_constant_p = "`*'";
4912 expression = build_x_indirect_ref (cast_expression, "unary *");
4916 non_constant_p = "`&'";
4919 expression = build_x_unary_op (unary_operator, cast_expression);
4922 case PREINCREMENT_EXPR:
4923 case PREDECREMENT_EXPR:
4924 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4927 case UNARY_PLUS_EXPR:
4929 case TRUTH_NOT_EXPR:
4930 expression = finish_unary_op_expr (unary_operator, cast_expression);
4938 && cp_parser_non_integral_constant_expression (parser,
4940 expression = error_mark_node;
4945 return cp_parser_postfix_expression (parser, address_p, cast_p);
4948 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4949 unary-operator, the corresponding tree code is returned. */
4951 static enum tree_code
4952 cp_parser_unary_operator (cp_token* token)
4954 switch (token->type)
4957 return INDIRECT_REF;
4963 return UNARY_PLUS_EXPR;
4969 return TRUTH_NOT_EXPR;
4972 return BIT_NOT_EXPR;
4979 /* Parse a new-expression.
4982 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4983 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4985 Returns a representation of the expression. */
4988 cp_parser_new_expression (cp_parser* parser)
4990 bool global_scope_p;
4996 /* Look for the optional `::' operator. */
4998 = (cp_parser_global_scope_opt (parser,
4999 /*current_scope_valid_p=*/false)
5001 /* Look for the `new' operator. */
5002 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5003 /* There's no easy way to tell a new-placement from the
5004 `( type-id )' construct. */
5005 cp_parser_parse_tentatively (parser);
5006 /* Look for a new-placement. */
5007 placement = cp_parser_new_placement (parser);
5008 /* If that didn't work out, there's no new-placement. */
5009 if (!cp_parser_parse_definitely (parser))
5010 placement = NULL_TREE;
5012 /* If the next token is a `(', then we have a parenthesized
5014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5016 /* Consume the `('. */
5017 cp_lexer_consume_token (parser->lexer);
5018 /* Parse the type-id. */
5019 type = cp_parser_type_id (parser);
5020 /* Look for the closing `)'. */
5021 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5022 /* There should not be a direct-new-declarator in this production,
5023 but GCC used to allowed this, so we check and emit a sensible error
5024 message for this case. */
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5027 error ("array bound forbidden after parenthesized type-id");
5028 inform ("try removing the parentheses around the type-id");
5029 cp_parser_direct_new_declarator (parser);
5033 /* Otherwise, there must be a new-type-id. */
5035 type = cp_parser_new_type_id (parser, &nelts);
5037 /* If the next token is a `(', then we have a new-initializer. */
5038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5039 initializer = cp_parser_new_initializer (parser);
5041 initializer = NULL_TREE;
5043 /* A new-expression may not appear in an integral constant
5045 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5046 return error_mark_node;
5048 /* Create a representation of the new-expression. */
5049 return build_new (placement, type, nelts, initializer, global_scope_p);
5052 /* Parse a new-placement.
5057 Returns the same representation as for an expression-list. */
5060 cp_parser_new_placement (cp_parser* parser)
5062 tree expression_list;
5064 /* Parse the expression-list. */
5065 expression_list = (cp_parser_parenthesized_expression_list
5066 (parser, false, /*cast_p=*/false,
5067 /*non_constant_p=*/NULL));
5069 return expression_list;
5072 /* Parse a new-type-id.
5075 type-specifier-seq new-declarator [opt]
5077 Returns the TYPE allocated. If the new-type-id indicates an array
5078 type, *NELTS is set to the number of elements in the last array
5079 bound; the TYPE will not include the last array bound. */
5082 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5084 cp_decl_specifier_seq type_specifier_seq;
5085 cp_declarator *new_declarator;
5086 cp_declarator *declarator;
5087 cp_declarator *outer_declarator;
5088 const char *saved_message;
5091 /* The type-specifier sequence must not contain type definitions.
5092 (It cannot contain declarations of new types either, but if they
5093 are not definitions we will catch that because they are not
5095 saved_message = parser->type_definition_forbidden_message;
5096 parser->type_definition_forbidden_message
5097 = "types may not be defined in a new-type-id";
5098 /* Parse the type-specifier-seq. */
5099 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5100 &type_specifier_seq);
5101 /* Restore the old message. */
5102 parser->type_definition_forbidden_message = saved_message;
5103 /* Parse the new-declarator. */
5104 new_declarator = cp_parser_new_declarator_opt (parser);
5106 /* Determine the number of elements in the last array dimension, if
5109 /* Skip down to the last array dimension. */
5110 declarator = new_declarator;
5111 outer_declarator = NULL;
5112 while (declarator && (declarator->kind == cdk_pointer
5113 || declarator->kind == cdk_ptrmem))
5115 outer_declarator = declarator;
5116 declarator = declarator->declarator;
5119 && declarator->kind == cdk_array
5120 && declarator->declarator
5121 && declarator->declarator->kind == cdk_array)
5123 outer_declarator = declarator;
5124 declarator = declarator->declarator;
5127 if (declarator && declarator->kind == cdk_array)
5129 *nelts = declarator->u.array.bounds;
5130 if (*nelts == error_mark_node)
5131 *nelts = integer_one_node;
5133 if (outer_declarator)
5134 outer_declarator->declarator = declarator->declarator;
5136 new_declarator = NULL;
5139 type = groktypename (&type_specifier_seq, new_declarator);
5140 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5142 *nelts = array_type_nelts_top (type);
5143 type = TREE_TYPE (type);
5148 /* Parse an (optional) new-declarator.
5151 ptr-operator new-declarator [opt]
5152 direct-new-declarator
5154 Returns the declarator. */
5156 static cp_declarator *
5157 cp_parser_new_declarator_opt (cp_parser* parser)
5159 enum tree_code code;
5161 cp_cv_quals cv_quals;
5163 /* We don't know if there's a ptr-operator next, or not. */
5164 cp_parser_parse_tentatively (parser);
5165 /* Look for a ptr-operator. */
5166 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5167 /* If that worked, look for more new-declarators. */
5168 if (cp_parser_parse_definitely (parser))
5170 cp_declarator *declarator;
5172 /* Parse another optional declarator. */
5173 declarator = cp_parser_new_declarator_opt (parser);
5175 /* Create the representation of the declarator. */
5177 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5178 else if (code == INDIRECT_REF)
5179 declarator = make_pointer_declarator (cv_quals, declarator);
5181 declarator = make_reference_declarator (cv_quals, declarator);
5186 /* If the next token is a `[', there is a direct-new-declarator. */
5187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5188 return cp_parser_direct_new_declarator (parser);
5193 /* Parse a direct-new-declarator.
5195 direct-new-declarator:
5197 direct-new-declarator [constant-expression]
5201 static cp_declarator *
5202 cp_parser_direct_new_declarator (cp_parser* parser)
5204 cp_declarator *declarator = NULL;
5210 /* Look for the opening `['. */
5211 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5212 /* The first expression is not required to be constant. */
5215 expression = cp_parser_expression (parser, /*cast_p=*/false);
5216 /* The standard requires that the expression have integral
5217 type. DR 74 adds enumeration types. We believe that the
5218 real intent is that these expressions be handled like the
5219 expression in a `switch' condition, which also allows
5220 classes with a single conversion to integral or
5221 enumeration type. */
5222 if (!processing_template_decl)
5225 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5230 error ("expression in new-declarator must have integral "
5231 "or enumeration type");
5232 expression = error_mark_node;
5236 /* But all the other expressions must be. */
5239 = cp_parser_constant_expression (parser,
5240 /*allow_non_constant=*/false,
5242 /* Look for the closing `]'. */
5243 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5245 /* Add this bound to the declarator. */
5246 declarator = make_array_declarator (declarator, expression);
5248 /* If the next token is not a `[', then there are no more
5250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5257 /* Parse a new-initializer.
5260 ( expression-list [opt] )
5262 Returns a representation of the expression-list. If there is no
5263 expression-list, VOID_ZERO_NODE is returned. */
5266 cp_parser_new_initializer (cp_parser* parser)
5268 tree expression_list;
5270 expression_list = (cp_parser_parenthesized_expression_list
5271 (parser, false, /*cast_p=*/false,
5272 /*non_constant_p=*/NULL));
5273 if (!expression_list)
5274 expression_list = void_zero_node;
5276 return expression_list;
5279 /* Parse a delete-expression.
5282 :: [opt] delete cast-expression
5283 :: [opt] delete [ ] cast-expression
5285 Returns a representation of the expression. */
5288 cp_parser_delete_expression (cp_parser* parser)
5290 bool global_scope_p;
5294 /* Look for the optional `::' operator. */
5296 = (cp_parser_global_scope_opt (parser,
5297 /*current_scope_valid_p=*/false)
5299 /* Look for the `delete' keyword. */
5300 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5301 /* See if the array syntax is in use. */
5302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5304 /* Consume the `[' token. */
5305 cp_lexer_consume_token (parser->lexer);
5306 /* Look for the `]' token. */
5307 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5308 /* Remember that this is the `[]' construct. */
5314 /* Parse the cast-expression. */
5315 expression = cp_parser_simple_cast_expression (parser);
5317 /* A delete-expression may not appear in an integral constant
5319 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5320 return error_mark_node;
5322 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5325 /* Parse a cast-expression.
5329 ( type-id ) cast-expression
5331 ADDRESS_P is true iff the unary-expression is appearing as the
5332 operand of the `&' operator. CAST_P is true if this expression is
5333 the target of a cast.
5335 Returns a representation of the expression. */
5338 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5340 /* If it's a `(', then we might be looking at a cast. */
5341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5343 tree type = NULL_TREE;
5344 tree expr = NULL_TREE;
5345 bool compound_literal_p;
5346 const char *saved_message;
5348 /* There's no way to know yet whether or not this is a cast.
5349 For example, `(int (3))' is a unary-expression, while `(int)
5350 3' is a cast. So, we resort to parsing tentatively. */
5351 cp_parser_parse_tentatively (parser);
5352 /* Types may not be defined in a cast. */
5353 saved_message = parser->type_definition_forbidden_message;
5354 parser->type_definition_forbidden_message
5355 = "types may not be defined in casts";
5356 /* Consume the `('. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* A very tricky bit is that `(struct S) { 3 }' is a
5359 compound-literal (which we permit in C++ as an extension).
5360 But, that construct is not a cast-expression -- it is a
5361 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5362 is legal; if the compound-literal were a cast-expression,
5363 you'd need an extra set of parentheses.) But, if we parse
5364 the type-id, and it happens to be a class-specifier, then we
5365 will commit to the parse at that point, because we cannot
5366 undo the action that is done when creating a new class. So,
5367 then we cannot back up and do a postfix-expression.
5369 Therefore, we scan ahead to the closing `)', and check to see
5370 if the token after the `)' is a `{'. If so, we are not
5371 looking at a cast-expression.
5373 Save tokens so that we can put them back. */
5374 cp_lexer_save_tokens (parser->lexer);
5375 /* Skip tokens until the next token is a closing parenthesis.
5376 If we find the closing `)', and the next token is a `{', then
5377 we are looking at a compound-literal. */
5379 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5380 /*consume_paren=*/true)
5381 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5382 /* Roll back the tokens we skipped. */
5383 cp_lexer_rollback_tokens (parser->lexer);
5384 /* If we were looking at a compound-literal, simulate an error
5385 so that the call to cp_parser_parse_definitely below will
5387 if (compound_literal_p)
5388 cp_parser_simulate_error (parser);
5391 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5392 parser->in_type_id_in_expr_p = true;
5393 /* Look for the type-id. */
5394 type = cp_parser_type_id (parser);
5395 /* Look for the closing `)'. */
5396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5397 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5400 /* Restore the saved message. */
5401 parser->type_definition_forbidden_message = saved_message;
5403 /* If ok so far, parse the dependent expression. We cannot be
5404 sure it is a cast. Consider `(T ())'. It is a parenthesized
5405 ctor of T, but looks like a cast to function returning T
5406 without a dependent expression. */
5407 if (!cp_parser_error_occurred (parser))
5408 expr = cp_parser_cast_expression (parser,
5409 /*address_p=*/false,
5412 if (cp_parser_parse_definitely (parser))
5414 /* Warn about old-style casts, if so requested. */
5415 if (warn_old_style_cast
5416 && !in_system_header
5417 && !VOID_TYPE_P (type)
5418 && current_lang_name != lang_name_c)
5419 warning (0, "use of old-style cast");
5421 /* Only type conversions to integral or enumeration types
5422 can be used in constant-expressions. */
5423 if (parser->integral_constant_expression_p
5424 && !dependent_type_p (type)
5425 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5426 && (cp_parser_non_integral_constant_expression
5428 "a cast to a type other than an integral or "
5429 "enumeration type")))
5430 return error_mark_node;
5432 /* Perform the cast. */
5433 expr = build_c_cast (type, expr);
5438 /* If we get here, then it's not a cast, so it must be a
5439 unary-expression. */
5440 return cp_parser_unary_expression (parser, address_p, cast_p);
5443 /* Parse a binary expression of the general form:
5447 pm-expression .* cast-expression
5448 pm-expression ->* cast-expression
5450 multiplicative-expression:
5452 multiplicative-expression * pm-expression
5453 multiplicative-expression / pm-expression
5454 multiplicative-expression % pm-expression
5456 additive-expression:
5457 multiplicative-expression
5458 additive-expression + multiplicative-expression
5459 additive-expression - multiplicative-expression
5463 shift-expression << additive-expression
5464 shift-expression >> additive-expression
5466 relational-expression:
5468 relational-expression < shift-expression
5469 relational-expression > shift-expression
5470 relational-expression <= shift-expression
5471 relational-expression >= shift-expression
5475 relational-expression:
5476 relational-expression <? shift-expression
5477 relational-expression >? shift-expression
5479 equality-expression:
5480 relational-expression
5481 equality-expression == relational-expression
5482 equality-expression != relational-expression
5486 and-expression & equality-expression
5488 exclusive-or-expression:
5490 exclusive-or-expression ^ and-expression
5492 inclusive-or-expression:
5493 exclusive-or-expression
5494 inclusive-or-expression | exclusive-or-expression
5496 logical-and-expression:
5497 inclusive-or-expression
5498 logical-and-expression && inclusive-or-expression
5500 logical-or-expression:
5501 logical-and-expression
5502 logical-or-expression || logical-and-expression
5504 All these are implemented with a single function like:
5507 simple-cast-expression
5508 binary-expression <token> binary-expression
5510 CAST_P is true if this expression is the target of a cast.
5512 The binops_by_token map is used to get the tree codes for each <token> type.
5513 binary-expressions are associated according to a precedence table. */
5515 #define TOKEN_PRECEDENCE(token) \
5516 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5517 ? PREC_NOT_OPERATOR \
5518 : binops_by_token[token->type].prec)
5521 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5523 cp_parser_expression_stack stack;
5524 cp_parser_expression_stack_entry *sp = &stack[0];
5527 enum tree_code tree_type;
5528 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5531 /* Parse the first expression. */
5532 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5536 /* Get an operator token. */
5537 token = cp_lexer_peek_token (parser->lexer);
5538 if (token->type == CPP_MIN || token->type == CPP_MAX)
5539 cp_parser_warn_min_max ();
5541 new_prec = TOKEN_PRECEDENCE (token);
5543 /* Popping an entry off the stack means we completed a subexpression:
5544 - either we found a token which is not an operator (`>' where it is not
5545 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5546 will happen repeatedly;
5547 - or, we found an operator which has lower priority. This is the case
5548 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5550 if (new_prec <= prec)
5559 tree_type = binops_by_token[token->type].tree_type;
5561 /* We used the operator token. */
5562 cp_lexer_consume_token (parser->lexer);
5564 /* Extract another operand. It may be the RHS of this expression
5565 or the LHS of a new, higher priority expression. */
5566 rhs = cp_parser_simple_cast_expression (parser);
5568 /* Get another operator token. Look up its precedence to avoid
5569 building a useless (immediately popped) stack entry for common
5570 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5571 token = cp_lexer_peek_token (parser->lexer);
5572 lookahead_prec = TOKEN_PRECEDENCE (token);
5573 if (lookahead_prec > new_prec)
5575 /* ... and prepare to parse the RHS of the new, higher priority
5576 expression. Since precedence levels on the stack are
5577 monotonically increasing, we do not have to care about
5580 sp->tree_type = tree_type;
5585 new_prec = lookahead_prec;
5589 /* If the stack is not empty, we have parsed into LHS the right side
5590 (`4' in the example above) of an expression we had suspended.
5591 We can use the information on the stack to recover the LHS (`3')
5592 from the stack together with the tree code (`MULT_EXPR'), and
5593 the precedence of the higher level subexpression
5594 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5595 which will be used to actually build the additive expression. */
5598 tree_type = sp->tree_type;
5603 overloaded_p = false;
5604 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5606 /* If the binary operator required the use of an overloaded operator,
5607 then this expression cannot be an integral constant-expression.
5608 An overloaded operator can be used even if both operands are
5609 otherwise permissible in an integral constant-expression if at
5610 least one of the operands is of enumeration type. */
5613 && (cp_parser_non_integral_constant_expression
5614 (parser, "calls to overloaded operators")))
5615 return error_mark_node;
5622 /* Parse the `? expression : assignment-expression' part of a
5623 conditional-expression. The LOGICAL_OR_EXPR is the
5624 logical-or-expression that started the conditional-expression.
5625 Returns a representation of the entire conditional-expression.
5627 This routine is used by cp_parser_assignment_expression.
5629 ? expression : assignment-expression
5633 ? : assignment-expression */
5636 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5639 tree assignment_expr;
5641 /* Consume the `?' token. */
5642 cp_lexer_consume_token (parser->lexer);
5643 if (cp_parser_allow_gnu_extensions_p (parser)
5644 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5645 /* Implicit true clause. */
5648 /* Parse the expression. */
5649 expr = cp_parser_expression (parser, /*cast_p=*/false);
5651 /* The next token should be a `:'. */
5652 cp_parser_require (parser, CPP_COLON, "`:'");
5653 /* Parse the assignment-expression. */
5654 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5656 /* Build the conditional-expression. */
5657 return build_x_conditional_expr (logical_or_expr,
5662 /* Parse an assignment-expression.
5664 assignment-expression:
5665 conditional-expression
5666 logical-or-expression assignment-operator assignment_expression
5669 CAST_P is true if this expression is the target of a cast.
5671 Returns a representation for the expression. */
5674 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5678 /* If the next token is the `throw' keyword, then we're looking at
5679 a throw-expression. */
5680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5681 expr = cp_parser_throw_expression (parser);
5682 /* Otherwise, it must be that we are looking at a
5683 logical-or-expression. */
5686 /* Parse the binary expressions (logical-or-expression). */
5687 expr = cp_parser_binary_expression (parser, cast_p);
5688 /* If the next token is a `?' then we're actually looking at a
5689 conditional-expression. */
5690 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5691 return cp_parser_question_colon_clause (parser, expr);
5694 enum tree_code assignment_operator;
5696 /* If it's an assignment-operator, we're using the second
5699 = cp_parser_assignment_operator_opt (parser);
5700 if (assignment_operator != ERROR_MARK)
5704 /* Parse the right-hand side of the assignment. */
5705 rhs = cp_parser_assignment_expression (parser, cast_p);
5706 /* An assignment may not appear in a
5707 constant-expression. */
5708 if (cp_parser_non_integral_constant_expression (parser,
5710 return error_mark_node;
5711 /* Build the assignment expression. */
5712 expr = build_x_modify_expr (expr,
5713 assignment_operator,
5722 /* Parse an (optional) assignment-operator.
5724 assignment-operator: one of
5725 = *= /= %= += -= >>= <<= &= ^= |=
5729 assignment-operator: one of
5732 If the next token is an assignment operator, the corresponding tree
5733 code is returned, and the token is consumed. For example, for
5734 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5735 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5736 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5737 operator, ERROR_MARK is returned. */
5739 static enum tree_code
5740 cp_parser_assignment_operator_opt (cp_parser* parser)
5745 /* Peek at the next toen. */
5746 token = cp_lexer_peek_token (parser->lexer);
5748 switch (token->type)
5759 op = TRUNC_DIV_EXPR;
5763 op = TRUNC_MOD_EXPR;
5796 cp_parser_warn_min_max ();
5801 cp_parser_warn_min_max ();
5805 /* Nothing else is an assignment operator. */
5809 /* If it was an assignment operator, consume it. */
5810 if (op != ERROR_MARK)
5811 cp_lexer_consume_token (parser->lexer);
5816 /* Parse an expression.
5819 assignment-expression
5820 expression , assignment-expression
5822 CAST_P is true if this expression is the target of a cast.
5824 Returns a representation of the expression. */
5827 cp_parser_expression (cp_parser* parser, bool cast_p)
5829 tree expression = NULL_TREE;
5833 tree assignment_expression;
5835 /* Parse the next assignment-expression. */
5836 assignment_expression
5837 = cp_parser_assignment_expression (parser, cast_p);
5838 /* If this is the first assignment-expression, we can just
5841 expression = assignment_expression;
5843 expression = build_x_compound_expr (expression,
5844 assignment_expression);
5845 /* If the next token is not a comma, then we are done with the
5847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5849 /* Consume the `,'. */
5850 cp_lexer_consume_token (parser->lexer);
5851 /* A comma operator cannot appear in a constant-expression. */
5852 if (cp_parser_non_integral_constant_expression (parser,
5853 "a comma operator"))
5854 expression = error_mark_node;
5860 /* Parse a constant-expression.
5862 constant-expression:
5863 conditional-expression
5865 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5866 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5867 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5868 is false, NON_CONSTANT_P should be NULL. */
5871 cp_parser_constant_expression (cp_parser* parser,
5872 bool allow_non_constant_p,
5873 bool *non_constant_p)
5875 bool saved_integral_constant_expression_p;
5876 bool saved_allow_non_integral_constant_expression_p;
5877 bool saved_non_integral_constant_expression_p;
5880 /* It might seem that we could simply parse the
5881 conditional-expression, and then check to see if it were
5882 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5883 one that the compiler can figure out is constant, possibly after
5884 doing some simplifications or optimizations. The standard has a
5885 precise definition of constant-expression, and we must honor
5886 that, even though it is somewhat more restrictive.
5892 is not a legal declaration, because `(2, 3)' is not a
5893 constant-expression. The `,' operator is forbidden in a
5894 constant-expression. However, GCC's constant-folding machinery
5895 will fold this operation to an INTEGER_CST for `3'. */
5897 /* Save the old settings. */
5898 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5899 saved_allow_non_integral_constant_expression_p
5900 = parser->allow_non_integral_constant_expression_p;
5901 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5902 /* We are now parsing a constant-expression. */
5903 parser->integral_constant_expression_p = true;
5904 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5905 parser->non_integral_constant_expression_p = false;
5906 /* Although the grammar says "conditional-expression", we parse an
5907 "assignment-expression", which also permits "throw-expression"
5908 and the use of assignment operators. In the case that
5909 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5910 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5911 actually essential that we look for an assignment-expression.
5912 For example, cp_parser_initializer_clauses uses this function to
5913 determine whether a particular assignment-expression is in fact
5915 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5916 /* Restore the old settings. */
5917 parser->integral_constant_expression_p
5918 = saved_integral_constant_expression_p;
5919 parser->allow_non_integral_constant_expression_p
5920 = saved_allow_non_integral_constant_expression_p;
5921 if (allow_non_constant_p)
5922 *non_constant_p = parser->non_integral_constant_expression_p;
5923 else if (parser->non_integral_constant_expression_p)
5924 expression = error_mark_node;
5925 parser->non_integral_constant_expression_p
5926 = saved_non_integral_constant_expression_p;
5931 /* Parse __builtin_offsetof.
5933 offsetof-expression:
5934 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5936 offsetof-member-designator:
5938 | offsetof-member-designator "." id-expression
5939 | offsetof-member-designator "[" expression "]"
5943 cp_parser_builtin_offsetof (cp_parser *parser)
5945 int save_ice_p, save_non_ice_p;
5949 /* We're about to accept non-integral-constant things, but will
5950 definitely yield an integral constant expression. Save and
5951 restore these values around our local parsing. */
5952 save_ice_p = parser->integral_constant_expression_p;
5953 save_non_ice_p = parser->non_integral_constant_expression_p;
5955 /* Consume the "__builtin_offsetof" token. */
5956 cp_lexer_consume_token (parser->lexer);
5957 /* Consume the opening `('. */
5958 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5959 /* Parse the type-id. */
5960 type = cp_parser_type_id (parser);
5961 /* Look for the `,'. */
5962 cp_parser_require (parser, CPP_COMMA, "`,'");
5964 /* Build the (type *)null that begins the traditional offsetof macro. */
5965 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5967 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5968 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5972 cp_token *token = cp_lexer_peek_token (parser->lexer);
5973 switch (token->type)
5975 case CPP_OPEN_SQUARE:
5976 /* offsetof-member-designator "[" expression "]" */
5977 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5981 /* offsetof-member-designator "." identifier */
5982 cp_lexer_consume_token (parser->lexer);
5983 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5987 case CPP_CLOSE_PAREN:
5988 /* Consume the ")" token. */
5989 cp_lexer_consume_token (parser->lexer);
5993 /* Error. We know the following require will fail, but
5994 that gives the proper error message. */
5995 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5996 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5997 expr = error_mark_node;
6003 /* If we're processing a template, we can't finish the semantics yet.
6004 Otherwise we can fold the entire expression now. */
6005 if (processing_template_decl)
6006 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6008 expr = fold_offsetof (expr);
6011 parser->integral_constant_expression_p = save_ice_p;
6012 parser->non_integral_constant_expression_p = save_non_ice_p;
6017 /* Statements [gram.stmt.stmt] */
6019 /* Parse a statement.
6023 expression-statement
6028 declaration-statement
6031 IN_COMPOUND is true when the statement is nested inside a
6032 cp_parser_compound_statement; this matters for certain pragmas. */
6035 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6040 location_t statement_location;
6043 /* There is no statement yet. */
6044 statement = NULL_TREE;
6045 /* Peek at the next token. */
6046 token = cp_lexer_peek_token (parser->lexer);
6047 /* Remember the location of the first token in the statement. */
6048 statement_location = token->location;
6049 /* If this is a keyword, then that will often determine what kind of
6050 statement we have. */
6051 if (token->type == CPP_KEYWORD)
6053 enum rid keyword = token->keyword;
6059 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6065 statement = cp_parser_selection_statement (parser);
6071 statement = cp_parser_iteration_statement (parser);
6078 statement = cp_parser_jump_statement (parser);
6081 /* Objective-C++ exception-handling constructs. */
6084 case RID_AT_FINALLY:
6085 case RID_AT_SYNCHRONIZED:
6087 statement = cp_parser_objc_statement (parser);
6091 statement = cp_parser_try_block (parser);
6095 /* It might be a keyword like `int' that can start a
6096 declaration-statement. */
6100 else if (token->type == CPP_NAME)
6102 /* If the next token is a `:', then we are looking at a
6103 labeled-statement. */
6104 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6105 if (token->type == CPP_COLON)
6106 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6109 /* Anything that starts with a `{' must be a compound-statement. */
6110 else if (token->type == CPP_OPEN_BRACE)
6111 statement = cp_parser_compound_statement (parser, NULL, false);
6112 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6113 a statement all its own. */
6114 else if (token->type == CPP_PRAGMA)
6116 /* Only certain OpenMP pragmas are attached to statements, and thus
6117 are considered statements themselves. All others are not. In
6118 the context of a compound, accept the pragma as a "statement" and
6119 return so that we can check for a close brace. Otherwise we
6120 require a real statement and must go back and read one. */
6122 cp_parser_pragma (parser, pragma_compound);
6123 else if (!cp_parser_pragma (parser, pragma_stmt))
6127 else if (token->type == CPP_EOF)
6129 cp_parser_error (parser, "expected statement");
6133 /* Everything else must be a declaration-statement or an
6134 expression-statement. Try for the declaration-statement
6135 first, unless we are looking at a `;', in which case we know that
6136 we have an expression-statement. */
6139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6141 cp_parser_parse_tentatively (parser);
6142 /* Try to parse the declaration-statement. */
6143 cp_parser_declaration_statement (parser);
6144 /* If that worked, we're done. */
6145 if (cp_parser_parse_definitely (parser))
6148 /* Look for an expression-statement instead. */
6149 statement = cp_parser_expression_statement (parser, in_statement_expr);
6152 /* Set the line number for the statement. */
6153 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6154 SET_EXPR_LOCATION (statement, statement_location);
6157 /* Parse a labeled-statement.
6160 identifier : statement
6161 case constant-expression : statement
6167 case constant-expression ... constant-expression : statement
6169 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6170 For an ordinary label, returns a LABEL_EXPR.
6172 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6173 inside a compound. */
6176 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6180 tree statement = error_mark_node;
6182 /* The next token should be an identifier. */
6183 token = cp_lexer_peek_token (parser->lexer);
6184 if (token->type != CPP_NAME
6185 && token->type != CPP_KEYWORD)
6187 cp_parser_error (parser, "expected labeled-statement");
6188 return error_mark_node;
6191 switch (token->keyword)
6198 /* Consume the `case' token. */
6199 cp_lexer_consume_token (parser->lexer);
6200 /* Parse the constant-expression. */
6201 expr = cp_parser_constant_expression (parser,
6202 /*allow_non_constant_p=*/false,
6205 ellipsis = cp_lexer_peek_token (parser->lexer);
6206 if (ellipsis->type == CPP_ELLIPSIS)
6208 /* Consume the `...' token. */
6209 cp_lexer_consume_token (parser->lexer);
6211 cp_parser_constant_expression (parser,
6212 /*allow_non_constant_p=*/false,
6214 /* We don't need to emit warnings here, as the common code
6215 will do this for us. */
6218 expr_hi = NULL_TREE;
6220 if (parser->in_switch_statement_p)
6221 statement = finish_case_label (expr, expr_hi);
6223 error ("case label %qE not within a switch statement", expr);
6228 /* Consume the `default' token. */
6229 cp_lexer_consume_token (parser->lexer);
6231 if (parser->in_switch_statement_p)
6232 statement = finish_case_label (NULL_TREE, NULL_TREE);
6234 error ("case label not within a switch statement");
6238 /* Anything else must be an ordinary label. */
6239 statement = finish_label_stmt (cp_parser_identifier (parser));
6243 /* Require the `:' token. */
6244 cp_parser_require (parser, CPP_COLON, "`:'");
6245 /* Parse the labeled statement. */
6246 cp_parser_statement (parser, in_statement_expr, in_compound);
6248 /* Return the label, in the case of a `case' or `default' label. */
6252 /* Parse an expression-statement.
6254 expression-statement:
6257 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6258 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6259 indicates whether this expression-statement is part of an
6260 expression statement. */
6263 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6265 tree statement = NULL_TREE;
6267 /* If the next token is a ';', then there is no expression
6269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6270 statement = cp_parser_expression (parser, /*cast_p=*/false);
6272 /* Consume the final `;'. */
6273 cp_parser_consume_semicolon_at_end_of_statement (parser);
6275 if (in_statement_expr
6276 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6277 /* This is the final expression statement of a statement
6279 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6281 statement = finish_expr_stmt (statement);
6288 /* Parse a compound-statement.
6291 { statement-seq [opt] }
6293 Returns a tree representing the statement. */
6296 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6301 /* Consume the `{'. */
6302 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6303 return error_mark_node;
6304 /* Begin the compound-statement. */
6305 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6306 /* Parse an (optional) statement-seq. */
6307 cp_parser_statement_seq_opt (parser, in_statement_expr);
6308 /* Finish the compound-statement. */
6309 finish_compound_stmt (compound_stmt);
6310 /* Consume the `}'. */
6311 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6313 return compound_stmt;
6316 /* Parse an (optional) statement-seq.
6320 statement-seq [opt] statement */
6323 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6325 /* Scan statements until there aren't any more. */
6328 cp_token *token = cp_lexer_peek_token (parser->lexer);
6330 /* If we're looking at a `}', then we've run out of statements. */
6331 if (token->type == CPP_CLOSE_BRACE
6332 || token->type == CPP_EOF
6333 || token->type == CPP_PRAGMA_EOL)
6336 /* Parse the statement. */
6337 cp_parser_statement (parser, in_statement_expr, true);
6341 /* Parse a selection-statement.
6343 selection-statement:
6344 if ( condition ) statement
6345 if ( condition ) statement else statement
6346 switch ( condition ) statement
6348 Returns the new IF_STMT or SWITCH_STMT. */
6351 cp_parser_selection_statement (cp_parser* parser)
6356 /* Peek at the next token. */
6357 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6359 /* See what kind of keyword it is. */
6360 keyword = token->keyword;
6369 /* Look for the `('. */
6370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6372 cp_parser_skip_to_end_of_statement (parser);
6373 return error_mark_node;
6376 /* Begin the selection-statement. */
6377 if (keyword == RID_IF)
6378 statement = begin_if_stmt ();
6380 statement = begin_switch_stmt ();
6382 /* Parse the condition. */
6383 condition = cp_parser_condition (parser);
6384 /* Look for the `)'. */
6385 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6386 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6387 /*consume_paren=*/true);
6389 if (keyword == RID_IF)
6391 /* Add the condition. */
6392 finish_if_stmt_cond (condition, statement);
6394 /* Parse the then-clause. */
6395 cp_parser_implicitly_scoped_statement (parser);
6396 finish_then_clause (statement);
6398 /* If the next token is `else', parse the else-clause. */
6399 if (cp_lexer_next_token_is_keyword (parser->lexer,
6402 /* Consume the `else' keyword. */
6403 cp_lexer_consume_token (parser->lexer);
6404 begin_else_clause (statement);
6405 /* Parse the else-clause. */
6406 cp_parser_implicitly_scoped_statement (parser);
6407 finish_else_clause (statement);
6410 /* Now we're all done with the if-statement. */
6411 finish_if_stmt (statement);
6415 bool in_switch_statement_p;
6417 /* Add the condition. */
6418 finish_switch_cond (condition, statement);
6420 /* Parse the body of the switch-statement. */
6421 in_switch_statement_p = parser->in_switch_statement_p;
6422 parser->in_switch_statement_p = true;
6423 cp_parser_implicitly_scoped_statement (parser);
6424 parser->in_switch_statement_p = in_switch_statement_p;
6426 /* Now we're all done with the switch-statement. */
6427 finish_switch_stmt (statement);
6435 cp_parser_error (parser, "expected selection-statement");
6436 return error_mark_node;
6440 /* Parse a condition.
6444 type-specifier-seq declarator = assignment-expression
6449 type-specifier-seq declarator asm-specification [opt]
6450 attributes [opt] = assignment-expression
6452 Returns the expression that should be tested. */
6455 cp_parser_condition (cp_parser* parser)
6457 cp_decl_specifier_seq type_specifiers;
6458 const char *saved_message;
6460 /* Try the declaration first. */
6461 cp_parser_parse_tentatively (parser);
6462 /* New types are not allowed in the type-specifier-seq for a
6464 saved_message = parser->type_definition_forbidden_message;
6465 parser->type_definition_forbidden_message
6466 = "types may not be defined in conditions";
6467 /* Parse the type-specifier-seq. */
6468 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6470 /* Restore the saved message. */
6471 parser->type_definition_forbidden_message = saved_message;
6472 /* If all is well, we might be looking at a declaration. */
6473 if (!cp_parser_error_occurred (parser))
6476 tree asm_specification;
6478 cp_declarator *declarator;
6479 tree initializer = NULL_TREE;
6481 /* Parse the declarator. */
6482 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6483 /*ctor_dtor_or_conv_p=*/NULL,
6484 /*parenthesized_p=*/NULL,
6485 /*member_p=*/false);
6486 /* Parse the attributes. */
6487 attributes = cp_parser_attributes_opt (parser);
6488 /* Parse the asm-specification. */
6489 asm_specification = cp_parser_asm_specification_opt (parser);
6490 /* If the next token is not an `=', then we might still be
6491 looking at an expression. For example:
6495 looks like a decl-specifier-seq and a declarator -- but then
6496 there is no `=', so this is an expression. */
6497 cp_parser_require (parser, CPP_EQ, "`='");
6498 /* If we did see an `=', then we are looking at a declaration
6500 if (cp_parser_parse_definitely (parser))
6504 /* Create the declaration. */
6505 decl = start_decl (declarator, &type_specifiers,
6506 /*initialized_p=*/true,
6507 attributes, /*prefix_attributes=*/NULL_TREE,
6509 /* Parse the assignment-expression. */
6510 initializer = cp_parser_assignment_expression (parser,
6513 /* Process the initializer. */
6514 cp_finish_decl (decl,
6517 LOOKUP_ONLYCONVERTING);
6520 pop_scope (pushed_scope);
6522 return convert_from_reference (decl);
6525 /* If we didn't even get past the declarator successfully, we are
6526 definitely not looking at a declaration. */
6528 cp_parser_abort_tentative_parse (parser);
6530 /* Otherwise, we are looking at an expression. */
6531 return cp_parser_expression (parser, /*cast_p=*/false);
6534 /* Parse an iteration-statement.
6536 iteration-statement:
6537 while ( condition ) statement
6538 do statement while ( expression ) ;
6539 for ( for-init-statement condition [opt] ; expression [opt] )
6542 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6545 cp_parser_iteration_statement (cp_parser* parser)
6550 bool in_iteration_statement_p;
6553 /* Peek at the next token. */
6554 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6556 return error_mark_node;
6558 /* Remember whether or not we are already within an iteration
6560 in_iteration_statement_p = parser->in_iteration_statement_p;
6562 /* See what kind of keyword it is. */
6563 keyword = token->keyword;
6570 /* Begin the while-statement. */
6571 statement = begin_while_stmt ();
6572 /* Look for the `('. */
6573 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6574 /* Parse the condition. */
6575 condition = cp_parser_condition (parser);
6576 finish_while_stmt_cond (condition, statement);
6577 /* Look for the `)'. */
6578 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6579 /* Parse the dependent statement. */
6580 parser->in_iteration_statement_p = true;
6581 cp_parser_already_scoped_statement (parser);
6582 parser->in_iteration_statement_p = in_iteration_statement_p;
6583 /* We're done with the while-statement. */
6584 finish_while_stmt (statement);
6592 /* Begin the do-statement. */
6593 statement = begin_do_stmt ();
6594 /* Parse the body of the do-statement. */
6595 parser->in_iteration_statement_p = true;
6596 cp_parser_implicitly_scoped_statement (parser);
6597 parser->in_iteration_statement_p = in_iteration_statement_p;
6598 finish_do_body (statement);
6599 /* Look for the `while' keyword. */
6600 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6601 /* Look for the `('. */
6602 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6603 /* Parse the expression. */
6604 expression = cp_parser_expression (parser, /*cast_p=*/false);
6605 /* We're done with the do-statement. */
6606 finish_do_stmt (expression, statement);
6607 /* Look for the `)'. */
6608 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6609 /* Look for the `;'. */
6610 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6616 tree condition = NULL_TREE;
6617 tree expression = NULL_TREE;
6619 /* Begin the for-statement. */
6620 statement = begin_for_stmt ();
6621 /* Look for the `('. */
6622 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6623 /* Parse the initialization. */
6624 cp_parser_for_init_statement (parser);
6625 finish_for_init_stmt (statement);
6627 /* If there's a condition, process it. */
6628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6629 condition = cp_parser_condition (parser);
6630 finish_for_cond (condition, statement);
6631 /* Look for the `;'. */
6632 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6634 /* If there's an expression, process it. */
6635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6636 expression = cp_parser_expression (parser, /*cast_p=*/false);
6637 finish_for_expr (expression, statement);
6638 /* Look for the `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6641 /* Parse the body of the for-statement. */
6642 parser->in_iteration_statement_p = true;
6643 cp_parser_already_scoped_statement (parser);
6644 parser->in_iteration_statement_p = in_iteration_statement_p;
6646 /* We're done with the for-statement. */
6647 finish_for_stmt (statement);
6652 cp_parser_error (parser, "expected iteration-statement");
6653 statement = error_mark_node;
6660 /* Parse a for-init-statement.
6663 expression-statement
6664 simple-declaration */
6667 cp_parser_for_init_statement (cp_parser* parser)
6669 /* If the next token is a `;', then we have an empty
6670 expression-statement. Grammatically, this is also a
6671 simple-declaration, but an invalid one, because it does not
6672 declare anything. Therefore, if we did not handle this case
6673 specially, we would issue an error message about an invalid
6675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6677 /* We're going to speculatively look for a declaration, falling back
6678 to an expression, if necessary. */
6679 cp_parser_parse_tentatively (parser);
6680 /* Parse the declaration. */
6681 cp_parser_simple_declaration (parser,
6682 /*function_definition_allowed_p=*/false);
6683 /* If the tentative parse failed, then we shall need to look for an
6684 expression-statement. */
6685 if (cp_parser_parse_definitely (parser))
6689 cp_parser_expression_statement (parser, false);
6692 /* Parse a jump-statement.
6697 return expression [opt] ;
6705 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6708 cp_parser_jump_statement (cp_parser* parser)
6710 tree statement = error_mark_node;
6714 /* Peek at the next token. */
6715 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6717 return error_mark_node;
6719 /* See what kind of keyword it is. */
6720 keyword = token->keyword;
6724 if (!parser->in_switch_statement_p
6725 && !parser->in_iteration_statement_p)
6727 error ("break statement not within loop or switch");
6728 statement = error_mark_node;
6731 statement = finish_break_stmt ();
6732 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6736 if (!parser->in_iteration_statement_p)
6738 error ("continue statement not within a loop");
6739 statement = error_mark_node;
6742 statement = finish_continue_stmt ();
6743 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6750 /* If the next token is a `;', then there is no
6752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6753 expr = cp_parser_expression (parser, /*cast_p=*/false);
6756 /* Build the return-statement. */
6757 statement = finish_return_stmt (expr);
6758 /* Look for the final `;'. */
6759 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6764 /* Create the goto-statement. */
6765 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6767 /* Issue a warning about this use of a GNU extension. */
6769 pedwarn ("ISO C++ forbids computed gotos");
6770 /* Consume the '*' token. */
6771 cp_lexer_consume_token (parser->lexer);
6772 /* Parse the dependent expression. */
6773 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6776 finish_goto_stmt (cp_parser_identifier (parser));
6777 /* Look for the final `;'. */
6778 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6782 cp_parser_error (parser, "expected jump-statement");
6789 /* Parse a declaration-statement.
6791 declaration-statement:
6792 block-declaration */
6795 cp_parser_declaration_statement (cp_parser* parser)
6799 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6800 p = obstack_alloc (&declarator_obstack, 0);
6802 /* Parse the block-declaration. */
6803 cp_parser_block_declaration (parser, /*statement_p=*/true);
6805 /* Free any declarators allocated. */
6806 obstack_free (&declarator_obstack, p);
6808 /* Finish off the statement. */
6812 /* Some dependent statements (like `if (cond) statement'), are
6813 implicitly in their own scope. In other words, if the statement is
6814 a single statement (as opposed to a compound-statement), it is
6815 none-the-less treated as if it were enclosed in braces. Any
6816 declarations appearing in the dependent statement are out of scope
6817 after control passes that point. This function parses a statement,
6818 but ensures that is in its own scope, even if it is not a
6821 Returns the new statement. */
6824 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6828 /* If the token is not a `{', then we must take special action. */
6829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6831 /* Create a compound-statement. */
6832 statement = begin_compound_stmt (0);
6833 /* Parse the dependent-statement. */
6834 cp_parser_statement (parser, NULL_TREE, false);
6835 /* Finish the dummy compound-statement. */
6836 finish_compound_stmt (statement);
6838 /* Otherwise, we simply parse the statement directly. */
6840 statement = cp_parser_compound_statement (parser, NULL, false);
6842 /* Return the statement. */
6846 /* For some dependent statements (like `while (cond) statement'), we
6847 have already created a scope. Therefore, even if the dependent
6848 statement is a compound-statement, we do not want to create another
6852 cp_parser_already_scoped_statement (cp_parser* parser)
6854 /* If the token is a `{', then we must take special action. */
6855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6856 cp_parser_statement (parser, NULL_TREE, false);
6859 /* Avoid calling cp_parser_compound_statement, so that we
6860 don't create a new scope. Do everything else by hand. */
6861 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6862 cp_parser_statement_seq_opt (parser, NULL_TREE);
6863 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6867 /* Declarations [gram.dcl.dcl] */
6869 /* Parse an optional declaration-sequence.
6873 declaration-seq declaration */
6876 cp_parser_declaration_seq_opt (cp_parser* parser)
6882 token = cp_lexer_peek_token (parser->lexer);
6884 if (token->type == CPP_CLOSE_BRACE
6885 || token->type == CPP_EOF
6886 || token->type == CPP_PRAGMA_EOL)
6889 if (token->type == CPP_SEMICOLON)
6891 /* A declaration consisting of a single semicolon is
6892 invalid. Allow it unless we're being pedantic. */
6893 cp_lexer_consume_token (parser->lexer);
6894 if (pedantic && !in_system_header)
6895 pedwarn ("extra %<;%>");
6899 /* If we're entering or exiting a region that's implicitly
6900 extern "C", modify the lang context appropriately. */
6901 if (!parser->implicit_extern_c && token->implicit_extern_c)
6903 push_lang_context (lang_name_c);
6904 parser->implicit_extern_c = true;
6906 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6908 pop_lang_context ();
6909 parser->implicit_extern_c = false;
6912 if (token->type == CPP_PRAGMA)
6914 /* A top-level declaration can consist solely of a #pragma.
6915 A nested declaration cannot, so this is done here and not
6916 in cp_parser_declaration. (A #pragma at block scope is
6917 handled in cp_parser_statement.) */
6918 cp_parser_pragma (parser, pragma_external);
6922 /* Parse the declaration itself. */
6923 cp_parser_declaration (parser);
6927 /* Parse a declaration.
6932 template-declaration
6933 explicit-instantiation
6934 explicit-specialization
6935 linkage-specification
6936 namespace-definition
6941 __extension__ declaration */
6944 cp_parser_declaration (cp_parser* parser)
6951 /* Check for the `__extension__' keyword. */
6952 if (cp_parser_extension_opt (parser, &saved_pedantic))
6954 /* Parse the qualified declaration. */
6955 cp_parser_declaration (parser);
6956 /* Restore the PEDANTIC flag. */
6957 pedantic = saved_pedantic;
6962 /* Try to figure out what kind of declaration is present. */
6963 token1 = *cp_lexer_peek_token (parser->lexer);
6965 if (token1.type != CPP_EOF)
6966 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6969 token2.type = CPP_EOF;
6970 token2.keyword = RID_MAX;
6973 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6974 p = obstack_alloc (&declarator_obstack, 0);
6976 /* If the next token is `extern' and the following token is a string
6977 literal, then we have a linkage specification. */
6978 if (token1.keyword == RID_EXTERN
6979 && cp_parser_is_string_literal (&token2))
6980 cp_parser_linkage_specification (parser);
6981 /* If the next token is `template', then we have either a template
6982 declaration, an explicit instantiation, or an explicit
6984 else if (token1.keyword == RID_TEMPLATE)
6986 /* `template <>' indicates a template specialization. */
6987 if (token2.type == CPP_LESS
6988 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6989 cp_parser_explicit_specialization (parser);
6990 /* `template <' indicates a template declaration. */
6991 else if (token2.type == CPP_LESS)
6992 cp_parser_template_declaration (parser, /*member_p=*/false);
6993 /* Anything else must be an explicit instantiation. */
6995 cp_parser_explicit_instantiation (parser);
6997 /* If the next token is `export', then we have a template
6999 else if (token1.keyword == RID_EXPORT)
7000 cp_parser_template_declaration (parser, /*member_p=*/false);
7001 /* If the next token is `extern', 'static' or 'inline' and the one
7002 after that is `template', we have a GNU extended explicit
7003 instantiation directive. */
7004 else if (cp_parser_allow_gnu_extensions_p (parser)
7005 && (token1.keyword == RID_EXTERN
7006 || token1.keyword == RID_STATIC
7007 || token1.keyword == RID_INLINE)
7008 && token2.keyword == RID_TEMPLATE)
7009 cp_parser_explicit_instantiation (parser);
7010 /* If the next token is `namespace', check for a named or unnamed
7011 namespace definition. */
7012 else if (token1.keyword == RID_NAMESPACE
7013 && (/* A named namespace definition. */
7014 (token2.type == CPP_NAME
7015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7017 /* An unnamed namespace definition. */
7018 || token2.type == CPP_OPEN_BRACE))
7019 cp_parser_namespace_definition (parser);
7020 /* Objective-C++ declaration/definition. */
7021 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7022 cp_parser_objc_declaration (parser);
7023 /* We must have either a block declaration or a function
7026 /* Try to parse a block-declaration, or a function-definition. */
7027 cp_parser_block_declaration (parser, /*statement_p=*/false);
7029 /* Free any declarators allocated. */
7030 obstack_free (&declarator_obstack, p);
7033 /* Parse a block-declaration.
7038 namespace-alias-definition
7045 __extension__ block-declaration
7048 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7049 part of a declaration-statement. */
7052 cp_parser_block_declaration (cp_parser *parser,
7058 /* Check for the `__extension__' keyword. */
7059 if (cp_parser_extension_opt (parser, &saved_pedantic))
7061 /* Parse the qualified declaration. */
7062 cp_parser_block_declaration (parser, statement_p);
7063 /* Restore the PEDANTIC flag. */
7064 pedantic = saved_pedantic;
7069 /* Peek at the next token to figure out which kind of declaration is
7071 token1 = cp_lexer_peek_token (parser->lexer);
7073 /* If the next keyword is `asm', we have an asm-definition. */
7074 if (token1->keyword == RID_ASM)
7077 cp_parser_commit_to_tentative_parse (parser);
7078 cp_parser_asm_definition (parser);
7080 /* If the next keyword is `namespace', we have a
7081 namespace-alias-definition. */
7082 else if (token1->keyword == RID_NAMESPACE)
7083 cp_parser_namespace_alias_definition (parser);
7084 /* If the next keyword is `using', we have either a
7085 using-declaration or a using-directive. */
7086 else if (token1->keyword == RID_USING)
7091 cp_parser_commit_to_tentative_parse (parser);
7092 /* If the token after `using' is `namespace', then we have a
7094 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7095 if (token2->keyword == RID_NAMESPACE)
7096 cp_parser_using_directive (parser);
7097 /* Otherwise, it's a using-declaration. */
7099 cp_parser_using_declaration (parser);
7101 /* If the next keyword is `__label__' we have a label declaration. */
7102 else if (token1->keyword == RID_LABEL)
7105 cp_parser_commit_to_tentative_parse (parser);
7106 cp_parser_label_declaration (parser);
7108 /* Anything else must be a simple-declaration. */
7110 cp_parser_simple_declaration (parser, !statement_p);
7113 /* Parse a simple-declaration.
7116 decl-specifier-seq [opt] init-declarator-list [opt] ;
7118 init-declarator-list:
7120 init-declarator-list , init-declarator
7122 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7123 function-definition as a simple-declaration. */
7126 cp_parser_simple_declaration (cp_parser* parser,
7127 bool function_definition_allowed_p)
7129 cp_decl_specifier_seq decl_specifiers;
7130 int declares_class_or_enum;
7131 bool saw_declarator;
7133 /* Defer access checks until we know what is being declared; the
7134 checks for names appearing in the decl-specifier-seq should be
7135 done as if we were in the scope of the thing being declared. */
7136 push_deferring_access_checks (dk_deferred);
7138 /* Parse the decl-specifier-seq. We have to keep track of whether
7139 or not the decl-specifier-seq declares a named class or
7140 enumeration type, since that is the only case in which the
7141 init-declarator-list is allowed to be empty.
7145 In a simple-declaration, the optional init-declarator-list can be
7146 omitted only when declaring a class or enumeration, that is when
7147 the decl-specifier-seq contains either a class-specifier, an
7148 elaborated-type-specifier, or an enum-specifier. */
7149 cp_parser_decl_specifier_seq (parser,
7150 CP_PARSER_FLAGS_OPTIONAL,
7152 &declares_class_or_enum);
7153 /* We no longer need to defer access checks. */
7154 stop_deferring_access_checks ();
7156 /* In a block scope, a valid declaration must always have a
7157 decl-specifier-seq. By not trying to parse declarators, we can
7158 resolve the declaration/expression ambiguity more quickly. */
7159 if (!function_definition_allowed_p
7160 && !decl_specifiers.any_specifiers_p)
7162 cp_parser_error (parser, "expected declaration");
7166 /* If the next two tokens are both identifiers, the code is
7167 erroneous. The usual cause of this situation is code like:
7171 where "T" should name a type -- but does not. */
7172 if (!decl_specifiers.type
7173 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7175 /* If parsing tentatively, we should commit; we really are
7176 looking at a declaration. */
7177 cp_parser_commit_to_tentative_parse (parser);
7182 /* If we have seen at least one decl-specifier, and the next token
7183 is not a parenthesis, then we must be looking at a declaration.
7184 (After "int (" we might be looking at a functional cast.) */
7185 if (decl_specifiers.any_specifiers_p
7186 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7187 cp_parser_commit_to_tentative_parse (parser);
7189 /* Keep going until we hit the `;' at the end of the simple
7191 saw_declarator = false;
7192 while (cp_lexer_next_token_is_not (parser->lexer,
7196 bool function_definition_p;
7201 /* If we are processing next declarator, coma is expected */
7202 token = cp_lexer_peek_token (parser->lexer);
7203 gcc_assert (token->type == CPP_COMMA);
7204 cp_lexer_consume_token (parser->lexer);
7207 saw_declarator = true;
7209 /* Parse the init-declarator. */
7210 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7211 function_definition_allowed_p,
7213 declares_class_or_enum,
7214 &function_definition_p);
7215 /* If an error occurred while parsing tentatively, exit quickly.
7216 (That usually happens when in the body of a function; each
7217 statement is treated as a declaration-statement until proven
7219 if (cp_parser_error_occurred (parser))
7221 /* Handle function definitions specially. */
7222 if (function_definition_p)
7224 /* If the next token is a `,', then we are probably
7225 processing something like:
7229 which is erroneous. */
7230 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7231 error ("mixing declarations and function-definitions is forbidden");
7232 /* Otherwise, we're done with the list of declarators. */
7235 pop_deferring_access_checks ();
7239 /* The next token should be either a `,' or a `;'. */
7240 token = cp_lexer_peek_token (parser->lexer);
7241 /* If it's a `,', there are more declarators to come. */
7242 if (token->type == CPP_COMMA)
7243 /* will be consumed next time around */;
7244 /* If it's a `;', we are done. */
7245 else if (token->type == CPP_SEMICOLON)
7247 /* Anything else is an error. */
7250 /* If we have already issued an error message we don't need
7251 to issue another one. */
7252 if (decl != error_mark_node
7253 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7254 cp_parser_error (parser, "expected %<,%> or %<;%>");
7255 /* Skip tokens until we reach the end of the statement. */
7256 cp_parser_skip_to_end_of_statement (parser);
7257 /* If the next token is now a `;', consume it. */
7258 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7259 cp_lexer_consume_token (parser->lexer);
7262 /* After the first time around, a function-definition is not
7263 allowed -- even if it was OK at first. For example:
7268 function_definition_allowed_p = false;
7271 /* Issue an error message if no declarators are present, and the
7272 decl-specifier-seq does not itself declare a class or
7274 if (!saw_declarator)
7276 if (cp_parser_declares_only_class_p (parser))
7277 shadow_tag (&decl_specifiers);
7278 /* Perform any deferred access checks. */
7279 perform_deferred_access_checks ();
7282 /* Consume the `;'. */
7283 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7286 pop_deferring_access_checks ();
7289 /* Parse a decl-specifier-seq.
7292 decl-specifier-seq [opt] decl-specifier
7295 storage-class-specifier
7306 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7308 The parser flags FLAGS is used to control type-specifier parsing.
7310 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7313 1: one of the decl-specifiers is an elaborated-type-specifier
7314 (i.e., a type declaration)
7315 2: one of the decl-specifiers is an enum-specifier or a
7316 class-specifier (i.e., a type definition)
7321 cp_parser_decl_specifier_seq (cp_parser* parser,
7322 cp_parser_flags flags,
7323 cp_decl_specifier_seq *decl_specs,
7324 int* declares_class_or_enum)
7326 bool constructor_possible_p = !parser->in_declarator_p;
7328 /* Clear DECL_SPECS. */
7329 clear_decl_specs (decl_specs);
7331 /* Assume no class or enumeration type is declared. */
7332 *declares_class_or_enum = 0;
7334 /* Keep reading specifiers until there are no more to read. */
7338 bool found_decl_spec;
7341 /* Peek at the next token. */
7342 token = cp_lexer_peek_token (parser->lexer);
7343 /* Handle attributes. */
7344 if (token->keyword == RID_ATTRIBUTE)
7346 /* Parse the attributes. */
7347 decl_specs->attributes
7348 = chainon (decl_specs->attributes,
7349 cp_parser_attributes_opt (parser));
7352 /* Assume we will find a decl-specifier keyword. */
7353 found_decl_spec = true;
7354 /* If the next token is an appropriate keyword, we can simply
7355 add it to the list. */
7356 switch (token->keyword)
7361 if (decl_specs->specs[(int) ds_friend]++)
7362 error ("duplicate %<friend%>");
7363 /* Consume the token. */
7364 cp_lexer_consume_token (parser->lexer);
7367 /* function-specifier:
7374 cp_parser_function_specifier_opt (parser, decl_specs);
7380 ++decl_specs->specs[(int) ds_typedef];
7381 /* Consume the token. */
7382 cp_lexer_consume_token (parser->lexer);
7383 /* A constructor declarator cannot appear in a typedef. */
7384 constructor_possible_p = false;
7385 /* The "typedef" keyword can only occur in a declaration; we
7386 may as well commit at this point. */
7387 cp_parser_commit_to_tentative_parse (parser);
7390 /* storage-class-specifier:
7400 /* Consume the token. */
7401 cp_lexer_consume_token (parser->lexer);
7402 cp_parser_set_storage_class (decl_specs, sc_auto);
7405 /* Consume the token. */
7406 cp_lexer_consume_token (parser->lexer);
7407 cp_parser_set_storage_class (decl_specs, sc_register);
7410 /* Consume the token. */
7411 cp_lexer_consume_token (parser->lexer);
7412 if (decl_specs->specs[(int) ds_thread])
7414 error ("%<__thread%> before %<static%>");
7415 decl_specs->specs[(int) ds_thread] = 0;
7417 cp_parser_set_storage_class (decl_specs, sc_static);
7420 /* Consume the token. */
7421 cp_lexer_consume_token (parser->lexer);
7422 if (decl_specs->specs[(int) ds_thread])
7424 error ("%<__thread%> before %<extern%>");
7425 decl_specs->specs[(int) ds_thread] = 0;
7427 cp_parser_set_storage_class (decl_specs, sc_extern);
7430 /* Consume the token. */
7431 cp_lexer_consume_token (parser->lexer);
7432 cp_parser_set_storage_class (decl_specs, sc_mutable);
7435 /* Consume the token. */
7436 cp_lexer_consume_token (parser->lexer);
7437 ++decl_specs->specs[(int) ds_thread];
7441 /* We did not yet find a decl-specifier yet. */
7442 found_decl_spec = false;
7446 /* Constructors are a special case. The `S' in `S()' is not a
7447 decl-specifier; it is the beginning of the declarator. */
7450 && constructor_possible_p
7451 && (cp_parser_constructor_declarator_p
7452 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7454 /* If we don't have a DECL_SPEC yet, then we must be looking at
7455 a type-specifier. */
7456 if (!found_decl_spec && !constructor_p)
7458 int decl_spec_declares_class_or_enum;
7459 bool is_cv_qualifier;
7463 = cp_parser_type_specifier (parser, flags,
7465 /*is_declaration=*/true,
7466 &decl_spec_declares_class_or_enum,
7469 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7471 /* If this type-specifier referenced a user-defined type
7472 (a typedef, class-name, etc.), then we can't allow any
7473 more such type-specifiers henceforth.
7477 The longest sequence of decl-specifiers that could
7478 possibly be a type name is taken as the
7479 decl-specifier-seq of a declaration. The sequence shall
7480 be self-consistent as described below.
7484 As a general rule, at most one type-specifier is allowed
7485 in the complete decl-specifier-seq of a declaration. The
7486 only exceptions are the following:
7488 -- const or volatile can be combined with any other
7491 -- signed or unsigned can be combined with char, long,
7499 void g (const int Pc);
7501 Here, Pc is *not* part of the decl-specifier seq; it's
7502 the declarator. Therefore, once we see a type-specifier
7503 (other than a cv-qualifier), we forbid any additional
7504 user-defined types. We *do* still allow things like `int
7505 int' to be considered a decl-specifier-seq, and issue the
7506 error message later. */
7507 if (type_spec && !is_cv_qualifier)
7508 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7509 /* A constructor declarator cannot follow a type-specifier. */
7512 constructor_possible_p = false;
7513 found_decl_spec = true;
7517 /* If we still do not have a DECL_SPEC, then there are no more
7519 if (!found_decl_spec)
7522 decl_specs->any_specifiers_p = true;
7523 /* After we see one decl-specifier, further decl-specifiers are
7525 flags |= CP_PARSER_FLAGS_OPTIONAL;
7528 /* Don't allow a friend specifier with a class definition. */
7529 if (decl_specs->specs[(int) ds_friend] != 0
7530 && (*declares_class_or_enum & 2))
7531 error ("class definition may not be declared a friend");
7534 /* Parse an (optional) storage-class-specifier.
7536 storage-class-specifier:
7545 storage-class-specifier:
7548 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7551 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7553 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7561 /* Consume the token. */
7562 return cp_lexer_consume_token (parser->lexer)->value;
7569 /* Parse an (optional) function-specifier.
7576 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7577 Updates DECL_SPECS, if it is non-NULL. */
7580 cp_parser_function_specifier_opt (cp_parser* parser,
7581 cp_decl_specifier_seq *decl_specs)
7583 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7587 ++decl_specs->specs[(int) ds_inline];
7592 ++decl_specs->specs[(int) ds_virtual];
7597 ++decl_specs->specs[(int) ds_explicit];
7604 /* Consume the token. */
7605 return cp_lexer_consume_token (parser->lexer)->value;
7608 /* Parse a linkage-specification.
7610 linkage-specification:
7611 extern string-literal { declaration-seq [opt] }
7612 extern string-literal declaration */
7615 cp_parser_linkage_specification (cp_parser* parser)
7619 /* Look for the `extern' keyword. */
7620 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7622 /* Look for the string-literal. */
7623 linkage = cp_parser_string_literal (parser, false, false);
7625 /* Transform the literal into an identifier. If the literal is a
7626 wide-character string, or contains embedded NULs, then we can't
7627 handle it as the user wants. */
7628 if (strlen (TREE_STRING_POINTER (linkage))
7629 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7631 cp_parser_error (parser, "invalid linkage-specification");
7632 /* Assume C++ linkage. */
7633 linkage = lang_name_cplusplus;
7636 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7638 /* We're now using the new linkage. */
7639 push_lang_context (linkage);
7641 /* If the next token is a `{', then we're using the first
7643 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7645 /* Consume the `{' token. */
7646 cp_lexer_consume_token (parser->lexer);
7647 /* Parse the declarations. */
7648 cp_parser_declaration_seq_opt (parser);
7649 /* Look for the closing `}'. */
7650 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7652 /* Otherwise, there's just one declaration. */
7655 bool saved_in_unbraced_linkage_specification_p;
7657 saved_in_unbraced_linkage_specification_p
7658 = parser->in_unbraced_linkage_specification_p;
7659 parser->in_unbraced_linkage_specification_p = true;
7660 have_extern_spec = true;
7661 cp_parser_declaration (parser);
7662 have_extern_spec = false;
7663 parser->in_unbraced_linkage_specification_p
7664 = saved_in_unbraced_linkage_specification_p;
7667 /* We're done with the linkage-specification. */
7668 pop_lang_context ();
7671 /* Special member functions [gram.special] */
7673 /* Parse a conversion-function-id.
7675 conversion-function-id:
7676 operator conversion-type-id
7678 Returns an IDENTIFIER_NODE representing the operator. */
7681 cp_parser_conversion_function_id (cp_parser* parser)
7685 tree saved_qualifying_scope;
7686 tree saved_object_scope;
7687 tree pushed_scope = NULL_TREE;
7689 /* Look for the `operator' token. */
7690 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7691 return error_mark_node;
7692 /* When we parse the conversion-type-id, the current scope will be
7693 reset. However, we need that information in able to look up the
7694 conversion function later, so we save it here. */
7695 saved_scope = parser->scope;
7696 saved_qualifying_scope = parser->qualifying_scope;
7697 saved_object_scope = parser->object_scope;
7698 /* We must enter the scope of the class so that the names of
7699 entities declared within the class are available in the
7700 conversion-type-id. For example, consider:
7707 S::operator I() { ... }
7709 In order to see that `I' is a type-name in the definition, we
7710 must be in the scope of `S'. */
7712 pushed_scope = push_scope (saved_scope);
7713 /* Parse the conversion-type-id. */
7714 type = cp_parser_conversion_type_id (parser);
7715 /* Leave the scope of the class, if any. */
7717 pop_scope (pushed_scope);
7718 /* Restore the saved scope. */
7719 parser->scope = saved_scope;
7720 parser->qualifying_scope = saved_qualifying_scope;
7721 parser->object_scope = saved_object_scope;
7722 /* If the TYPE is invalid, indicate failure. */
7723 if (type == error_mark_node)
7724 return error_mark_node;
7725 return mangle_conv_op_name_for_type (type);
7728 /* Parse a conversion-type-id:
7731 type-specifier-seq conversion-declarator [opt]
7733 Returns the TYPE specified. */
7736 cp_parser_conversion_type_id (cp_parser* parser)
7739 cp_decl_specifier_seq type_specifiers;
7740 cp_declarator *declarator;
7741 tree type_specified;
7743 /* Parse the attributes. */
7744 attributes = cp_parser_attributes_opt (parser);
7745 /* Parse the type-specifiers. */
7746 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7748 /* If that didn't work, stop. */
7749 if (type_specifiers.type == error_mark_node)
7750 return error_mark_node;
7751 /* Parse the conversion-declarator. */
7752 declarator = cp_parser_conversion_declarator_opt (parser);
7754 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7755 /*initialized=*/0, &attributes);
7757 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7758 return type_specified;
7761 /* Parse an (optional) conversion-declarator.
7763 conversion-declarator:
7764 ptr-operator conversion-declarator [opt]
7768 static cp_declarator *
7769 cp_parser_conversion_declarator_opt (cp_parser* parser)
7771 enum tree_code code;
7773 cp_cv_quals cv_quals;
7775 /* We don't know if there's a ptr-operator next, or not. */
7776 cp_parser_parse_tentatively (parser);
7777 /* Try the ptr-operator. */
7778 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7779 /* If it worked, look for more conversion-declarators. */
7780 if (cp_parser_parse_definitely (parser))
7782 cp_declarator *declarator;
7784 /* Parse another optional declarator. */
7785 declarator = cp_parser_conversion_declarator_opt (parser);
7787 /* Create the representation of the declarator. */
7789 declarator = make_ptrmem_declarator (cv_quals, class_type,
7791 else if (code == INDIRECT_REF)
7792 declarator = make_pointer_declarator (cv_quals, declarator);
7794 declarator = make_reference_declarator (cv_quals, declarator);
7802 /* Parse an (optional) ctor-initializer.
7805 : mem-initializer-list
7807 Returns TRUE iff the ctor-initializer was actually present. */
7810 cp_parser_ctor_initializer_opt (cp_parser* parser)
7812 /* If the next token is not a `:', then there is no
7813 ctor-initializer. */
7814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7816 /* Do default initialization of any bases and members. */
7817 if (DECL_CONSTRUCTOR_P (current_function_decl))
7818 finish_mem_initializers (NULL_TREE);
7823 /* Consume the `:' token. */
7824 cp_lexer_consume_token (parser->lexer);
7825 /* And the mem-initializer-list. */
7826 cp_parser_mem_initializer_list (parser);
7831 /* Parse a mem-initializer-list.
7833 mem-initializer-list:
7835 mem-initializer , mem-initializer-list */
7838 cp_parser_mem_initializer_list (cp_parser* parser)
7840 tree mem_initializer_list = NULL_TREE;
7842 /* Let the semantic analysis code know that we are starting the
7843 mem-initializer-list. */
7844 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7845 error ("only constructors take base initializers");
7847 /* Loop through the list. */
7850 tree mem_initializer;
7852 /* Parse the mem-initializer. */
7853 mem_initializer = cp_parser_mem_initializer (parser);
7854 /* Add it to the list, unless it was erroneous. */
7855 if (mem_initializer != error_mark_node)
7857 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7858 mem_initializer_list = mem_initializer;
7860 /* If the next token is not a `,', we're done. */
7861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7863 /* Consume the `,' token. */
7864 cp_lexer_consume_token (parser->lexer);
7867 /* Perform semantic analysis. */
7868 if (DECL_CONSTRUCTOR_P (current_function_decl))
7869 finish_mem_initializers (mem_initializer_list);
7872 /* Parse a mem-initializer.
7875 mem-initializer-id ( expression-list [opt] )
7880 ( expression-list [opt] )
7882 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7883 class) or FIELD_DECL (for a non-static data member) to initialize;
7884 the TREE_VALUE is the expression-list. An empty initialization
7885 list is represented by void_list_node. */
7888 cp_parser_mem_initializer (cp_parser* parser)
7890 tree mem_initializer_id;
7891 tree expression_list;
7894 /* Find out what is being initialized. */
7895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7897 pedwarn ("anachronistic old-style base class initializer");
7898 mem_initializer_id = NULL_TREE;
7901 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7902 member = expand_member_init (mem_initializer_id);
7903 if (member && !DECL_P (member))
7904 in_base_initializer = 1;
7907 = cp_parser_parenthesized_expression_list (parser, false,
7909 /*non_constant_p=*/NULL);
7910 if (expression_list == error_mark_node)
7911 return error_mark_node;
7912 if (!expression_list)
7913 expression_list = void_type_node;
7915 in_base_initializer = 0;
7917 return member ? build_tree_list (member, expression_list) : error_mark_node;
7920 /* Parse a mem-initializer-id.
7923 :: [opt] nested-name-specifier [opt] class-name
7926 Returns a TYPE indicating the class to be initializer for the first
7927 production. Returns an IDENTIFIER_NODE indicating the data member
7928 to be initialized for the second production. */
7931 cp_parser_mem_initializer_id (cp_parser* parser)
7933 bool global_scope_p;
7934 bool nested_name_specifier_p;
7935 bool template_p = false;
7938 /* `typename' is not allowed in this context ([temp.res]). */
7939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7941 error ("keyword %<typename%> not allowed in this context (a qualified "
7942 "member initializer is implicitly a type)");
7943 cp_lexer_consume_token (parser->lexer);
7945 /* Look for the optional `::' operator. */
7947 = (cp_parser_global_scope_opt (parser,
7948 /*current_scope_valid_p=*/false)
7950 /* Look for the optional nested-name-specifier. The simplest way to
7955 The keyword `typename' is not permitted in a base-specifier or
7956 mem-initializer; in these contexts a qualified name that
7957 depends on a template-parameter is implicitly assumed to be a
7960 is to assume that we have seen the `typename' keyword at this
7962 nested_name_specifier_p
7963 = (cp_parser_nested_name_specifier_opt (parser,
7964 /*typename_keyword_p=*/true,
7965 /*check_dependency_p=*/true,
7967 /*is_declaration=*/true)
7969 if (nested_name_specifier_p)
7970 template_p = cp_parser_optional_template_keyword (parser);
7971 /* If there is a `::' operator or a nested-name-specifier, then we
7972 are definitely looking for a class-name. */
7973 if (global_scope_p || nested_name_specifier_p)
7974 return cp_parser_class_name (parser,
7975 /*typename_keyword_p=*/true,
7976 /*template_keyword_p=*/template_p,
7978 /*check_dependency_p=*/true,
7979 /*class_head_p=*/false,
7980 /*is_declaration=*/true);
7981 /* Otherwise, we could also be looking for an ordinary identifier. */
7982 cp_parser_parse_tentatively (parser);
7983 /* Try a class-name. */
7984 id = cp_parser_class_name (parser,
7985 /*typename_keyword_p=*/true,
7986 /*template_keyword_p=*/false,
7988 /*check_dependency_p=*/true,
7989 /*class_head_p=*/false,
7990 /*is_declaration=*/true);
7991 /* If we found one, we're done. */
7992 if (cp_parser_parse_definitely (parser))
7994 /* Otherwise, look for an ordinary identifier. */
7995 return cp_parser_identifier (parser);
7998 /* Overloading [gram.over] */
8000 /* Parse an operator-function-id.
8002 operator-function-id:
8005 Returns an IDENTIFIER_NODE for the operator which is a
8006 human-readable spelling of the identifier, e.g., `operator +'. */
8009 cp_parser_operator_function_id (cp_parser* parser)
8011 /* Look for the `operator' keyword. */
8012 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8013 return error_mark_node;
8014 /* And then the name of the operator itself. */
8015 return cp_parser_operator (parser);
8018 /* Parse an operator.
8021 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8022 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8023 || ++ -- , ->* -> () []
8030 Returns an IDENTIFIER_NODE for the operator which is a
8031 human-readable spelling of the identifier, e.g., `operator +'. */
8034 cp_parser_operator (cp_parser* parser)
8036 tree id = NULL_TREE;
8039 /* Peek at the next token. */
8040 token = cp_lexer_peek_token (parser->lexer);
8041 /* Figure out which operator we have. */
8042 switch (token->type)
8048 /* The keyword should be either `new' or `delete'. */
8049 if (token->keyword == RID_NEW)
8051 else if (token->keyword == RID_DELETE)
8056 /* Consume the `new' or `delete' token. */
8057 cp_lexer_consume_token (parser->lexer);
8059 /* Peek at the next token. */
8060 token = cp_lexer_peek_token (parser->lexer);
8061 /* If it's a `[' token then this is the array variant of the
8063 if (token->type == CPP_OPEN_SQUARE)
8065 /* Consume the `[' token. */
8066 cp_lexer_consume_token (parser->lexer);
8067 /* Look for the `]' token. */
8068 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8069 id = ansi_opname (op == NEW_EXPR
8070 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8072 /* Otherwise, we have the non-array variant. */
8074 id = ansi_opname (op);
8080 id = ansi_opname (PLUS_EXPR);
8084 id = ansi_opname (MINUS_EXPR);
8088 id = ansi_opname (MULT_EXPR);
8092 id = ansi_opname (TRUNC_DIV_EXPR);
8096 id = ansi_opname (TRUNC_MOD_EXPR);
8100 id = ansi_opname (BIT_XOR_EXPR);
8104 id = ansi_opname (BIT_AND_EXPR);
8108 id = ansi_opname (BIT_IOR_EXPR);
8112 id = ansi_opname (BIT_NOT_EXPR);
8116 id = ansi_opname (TRUTH_NOT_EXPR);
8120 id = ansi_assopname (NOP_EXPR);
8124 id = ansi_opname (LT_EXPR);
8128 id = ansi_opname (GT_EXPR);
8132 id = ansi_assopname (PLUS_EXPR);
8136 id = ansi_assopname (MINUS_EXPR);
8140 id = ansi_assopname (MULT_EXPR);
8144 id = ansi_assopname (TRUNC_DIV_EXPR);
8148 id = ansi_assopname (TRUNC_MOD_EXPR);
8152 id = ansi_assopname (BIT_XOR_EXPR);
8156 id = ansi_assopname (BIT_AND_EXPR);
8160 id = ansi_assopname (BIT_IOR_EXPR);
8164 id = ansi_opname (LSHIFT_EXPR);
8168 id = ansi_opname (RSHIFT_EXPR);
8172 id = ansi_assopname (LSHIFT_EXPR);
8176 id = ansi_assopname (RSHIFT_EXPR);
8180 id = ansi_opname (EQ_EXPR);
8184 id = ansi_opname (NE_EXPR);
8188 id = ansi_opname (LE_EXPR);
8191 case CPP_GREATER_EQ:
8192 id = ansi_opname (GE_EXPR);
8196 id = ansi_opname (TRUTH_ANDIF_EXPR);
8200 id = ansi_opname (TRUTH_ORIF_EXPR);
8204 id = ansi_opname (POSTINCREMENT_EXPR);
8207 case CPP_MINUS_MINUS:
8208 id = ansi_opname (PREDECREMENT_EXPR);
8212 id = ansi_opname (COMPOUND_EXPR);
8215 case CPP_DEREF_STAR:
8216 id = ansi_opname (MEMBER_REF);
8220 id = ansi_opname (COMPONENT_REF);
8223 case CPP_OPEN_PAREN:
8224 /* Consume the `('. */
8225 cp_lexer_consume_token (parser->lexer);
8226 /* Look for the matching `)'. */
8227 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8228 return ansi_opname (CALL_EXPR);
8230 case CPP_OPEN_SQUARE:
8231 /* Consume the `['. */
8232 cp_lexer_consume_token (parser->lexer);
8233 /* Look for the matching `]'. */
8234 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8235 return ansi_opname (ARRAY_REF);
8239 id = ansi_opname (MIN_EXPR);
8240 cp_parser_warn_min_max ();
8244 id = ansi_opname (MAX_EXPR);
8245 cp_parser_warn_min_max ();
8249 id = ansi_assopname (MIN_EXPR);
8250 cp_parser_warn_min_max ();
8254 id = ansi_assopname (MAX_EXPR);
8255 cp_parser_warn_min_max ();
8259 /* Anything else is an error. */
8263 /* If we have selected an identifier, we need to consume the
8266 cp_lexer_consume_token (parser->lexer);
8267 /* Otherwise, no valid operator name was present. */
8270 cp_parser_error (parser, "expected operator");
8271 id = error_mark_node;
8277 /* Parse a template-declaration.
8279 template-declaration:
8280 export [opt] template < template-parameter-list > declaration
8282 If MEMBER_P is TRUE, this template-declaration occurs within a
8285 The grammar rule given by the standard isn't correct. What
8288 template-declaration:
8289 export [opt] template-parameter-list-seq
8290 decl-specifier-seq [opt] init-declarator [opt] ;
8291 export [opt] template-parameter-list-seq
8294 template-parameter-list-seq:
8295 template-parameter-list-seq [opt]
8296 template < template-parameter-list > */
8299 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8301 /* Check for `export'. */
8302 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8304 /* Consume the `export' token. */
8305 cp_lexer_consume_token (parser->lexer);
8306 /* Warn that we do not support `export'. */
8307 warning (0, "keyword %<export%> not implemented, and will be ignored");
8310 cp_parser_template_declaration_after_export (parser, member_p);
8313 /* Parse a template-parameter-list.
8315 template-parameter-list:
8317 template-parameter-list , template-parameter
8319 Returns a TREE_LIST. Each node represents a template parameter.
8320 The nodes are connected via their TREE_CHAINs. */
8323 cp_parser_template_parameter_list (cp_parser* parser)
8325 tree parameter_list = NULL_TREE;
8327 begin_template_parm_list ();
8334 /* Parse the template-parameter. */
8335 parameter = cp_parser_template_parameter (parser, &is_non_type);
8336 /* Add it to the list. */
8337 if (parameter != error_mark_node)
8338 parameter_list = process_template_parm (parameter_list,
8341 /* Peek at the next token. */
8342 token = cp_lexer_peek_token (parser->lexer);
8343 /* If it's not a `,', we're done. */
8344 if (token->type != CPP_COMMA)
8346 /* Otherwise, consume the `,' token. */
8347 cp_lexer_consume_token (parser->lexer);
8350 return end_template_parm_list (parameter_list);
8353 /* Parse a template-parameter.
8357 parameter-declaration
8359 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8360 the parameter. The TREE_PURPOSE is the default value, if any.
8361 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8362 iff this parameter is a non-type parameter. */
8365 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8368 cp_parameter_declarator *parameter_declarator;
8371 /* Assume it is a type parameter or a template parameter. */
8372 *is_non_type = false;
8373 /* Peek at the next token. */
8374 token = cp_lexer_peek_token (parser->lexer);
8375 /* If it is `class' or `template', we have a type-parameter. */
8376 if (token->keyword == RID_TEMPLATE)
8377 return cp_parser_type_parameter (parser);
8378 /* If it is `class' or `typename' we do not know yet whether it is a
8379 type parameter or a non-type parameter. Consider:
8381 template <typename T, typename T::X X> ...
8385 template <class C, class D*> ...
8387 Here, the first parameter is a type parameter, and the second is
8388 a non-type parameter. We can tell by looking at the token after
8389 the identifier -- if it is a `,', `=', or `>' then we have a type
8391 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8393 /* Peek at the token after `class' or `typename'. */
8394 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8395 /* If it's an identifier, skip it. */
8396 if (token->type == CPP_NAME)
8397 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8398 /* Now, see if the token looks like the end of a template
8400 if (token->type == CPP_COMMA
8401 || token->type == CPP_EQ
8402 || token->type == CPP_GREATER)
8403 return cp_parser_type_parameter (parser);
8406 /* Otherwise, it is a non-type parameter.
8410 When parsing a default template-argument for a non-type
8411 template-parameter, the first non-nested `>' is taken as the end
8412 of the template parameter-list rather than a greater-than
8414 *is_non_type = true;
8415 parameter_declarator
8416 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8417 /*parenthesized_p=*/NULL);
8418 parm = grokdeclarator (parameter_declarator->declarator,
8419 ¶meter_declarator->decl_specifiers,
8420 PARM, /*initialized=*/0,
8422 if (parm == error_mark_node)
8423 return error_mark_node;
8424 return build_tree_list (parameter_declarator->default_argument, parm);
8427 /* Parse a type-parameter.
8430 class identifier [opt]
8431 class identifier [opt] = type-id
8432 typename identifier [opt]
8433 typename identifier [opt] = type-id
8434 template < template-parameter-list > class identifier [opt]
8435 template < template-parameter-list > class identifier [opt]
8438 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8439 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8440 the declaration of the parameter. */
8443 cp_parser_type_parameter (cp_parser* parser)
8448 /* Look for a keyword to tell us what kind of parameter this is. */
8449 token = cp_parser_require (parser, CPP_KEYWORD,
8450 "`class', `typename', or `template'");
8452 return error_mark_node;
8454 switch (token->keyword)
8460 tree default_argument;
8462 /* If the next token is an identifier, then it names the
8464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8465 identifier = cp_parser_identifier (parser);
8467 identifier = NULL_TREE;
8469 /* Create the parameter. */
8470 parameter = finish_template_type_parm (class_type_node, identifier);
8472 /* If the next token is an `=', we have a default argument. */
8473 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8475 /* Consume the `=' token. */
8476 cp_lexer_consume_token (parser->lexer);
8477 /* Parse the default-argument. */
8478 default_argument = cp_parser_type_id (parser);
8481 default_argument = NULL_TREE;
8483 /* Create the combined representation of the parameter and the
8484 default argument. */
8485 parameter = build_tree_list (default_argument, parameter);
8491 tree parameter_list;
8493 tree default_argument;
8495 /* Look for the `<'. */
8496 cp_parser_require (parser, CPP_LESS, "`<'");
8497 /* Parse the template-parameter-list. */
8498 parameter_list = cp_parser_template_parameter_list (parser);
8499 /* Look for the `>'. */
8500 cp_parser_require (parser, CPP_GREATER, "`>'");
8501 /* Look for the `class' keyword. */
8502 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8503 /* If the next token is an `=', then there is a
8504 default-argument. If the next token is a `>', we are at
8505 the end of the parameter-list. If the next token is a `,',
8506 then we are at the end of this parameter. */
8507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8508 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8509 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8511 identifier = cp_parser_identifier (parser);
8512 /* Treat invalid names as if the parameter were nameless. */
8513 if (identifier == error_mark_node)
8514 identifier = NULL_TREE;
8517 identifier = NULL_TREE;
8519 /* Create the template parameter. */
8520 parameter = finish_template_template_parm (class_type_node,
8523 /* If the next token is an `=', then there is a
8524 default-argument. */
8525 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8529 /* Consume the `='. */
8530 cp_lexer_consume_token (parser->lexer);
8531 /* Parse the id-expression. */
8533 = cp_parser_id_expression (parser,
8534 /*template_keyword_p=*/false,
8535 /*check_dependency_p=*/true,
8536 /*template_p=*/&is_template,
8537 /*declarator_p=*/false);
8538 if (TREE_CODE (default_argument) == TYPE_DECL)
8539 /* If the id-expression was a template-id that refers to
8540 a template-class, we already have the declaration here,
8541 so no further lookup is needed. */
8544 /* Look up the name. */
8546 = cp_parser_lookup_name (parser, default_argument,
8548 /*is_template=*/is_template,
8549 /*is_namespace=*/false,
8550 /*check_dependency=*/true,
8551 /*ambiguous_decls=*/NULL);
8552 /* See if the default argument is valid. */
8554 = check_template_template_default_arg (default_argument);
8557 default_argument = NULL_TREE;
8559 /* Create the combined representation of the parameter and the
8560 default argument. */
8561 parameter = build_tree_list (default_argument, parameter);
8573 /* Parse a template-id.
8576 template-name < template-argument-list [opt] >
8578 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8579 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8580 returned. Otherwise, if the template-name names a function, or set
8581 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8582 names a class, returns a TYPE_DECL for the specialization.
8584 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8585 uninstantiated templates. */
8588 cp_parser_template_id (cp_parser *parser,
8589 bool template_keyword_p,
8590 bool check_dependency_p,
8591 bool is_declaration)
8596 cp_token_position start_of_id = 0;
8597 tree access_check = NULL_TREE;
8598 cp_token *next_token, *next_token_2;
8601 /* If the next token corresponds to a template-id, there is no need
8603 next_token = cp_lexer_peek_token (parser->lexer);
8604 if (next_token->type == CPP_TEMPLATE_ID)
8609 /* Get the stored value. */
8610 value = cp_lexer_consume_token (parser->lexer)->value;
8611 /* Perform any access checks that were deferred. */
8612 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8613 perform_or_defer_access_check (TREE_PURPOSE (check),
8614 TREE_VALUE (check));
8615 /* Return the stored value. */
8616 return TREE_VALUE (value);
8619 /* Avoid performing name lookup if there is no possibility of
8620 finding a template-id. */
8621 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8622 || (next_token->type == CPP_NAME
8623 && !cp_parser_nth_token_starts_template_argument_list_p
8626 cp_parser_error (parser, "expected template-id");
8627 return error_mark_node;
8630 /* Remember where the template-id starts. */
8631 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8632 start_of_id = cp_lexer_token_position (parser->lexer, false);
8634 push_deferring_access_checks (dk_deferred);
8636 /* Parse the template-name. */
8637 is_identifier = false;
8638 template = cp_parser_template_name (parser, template_keyword_p,
8642 if (template == error_mark_node || is_identifier)
8644 pop_deferring_access_checks ();
8648 /* If we find the sequence `[:' after a template-name, it's probably
8649 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8650 parse correctly the argument list. */
8651 next_token = cp_lexer_peek_token (parser->lexer);
8652 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8653 if (next_token->type == CPP_OPEN_SQUARE
8654 && next_token->flags & DIGRAPH
8655 && next_token_2->type == CPP_COLON
8656 && !(next_token_2->flags & PREV_WHITE))
8658 cp_parser_parse_tentatively (parser);
8659 /* Change `:' into `::'. */
8660 next_token_2->type = CPP_SCOPE;
8661 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8663 cp_lexer_consume_token (parser->lexer);
8664 /* Parse the arguments. */
8665 arguments = cp_parser_enclosed_template_argument_list (parser);
8666 if (!cp_parser_parse_definitely (parser))
8668 /* If we couldn't parse an argument list, then we revert our changes
8669 and return simply an error. Maybe this is not a template-id
8671 next_token_2->type = CPP_COLON;
8672 cp_parser_error (parser, "expected %<<%>");
8673 pop_deferring_access_checks ();
8674 return error_mark_node;
8676 /* Otherwise, emit an error about the invalid digraph, but continue
8677 parsing because we got our argument list. */
8678 pedwarn ("%<<::%> cannot begin a template-argument list");
8679 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8680 "between %<<%> and %<::%>");
8681 if (!flag_permissive)
8686 inform ("(if you use -fpermissive G++ will accept your code)");
8693 /* Look for the `<' that starts the template-argument-list. */
8694 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8696 pop_deferring_access_checks ();
8697 return error_mark_node;
8699 /* Parse the arguments. */
8700 arguments = cp_parser_enclosed_template_argument_list (parser);
8703 /* Build a representation of the specialization. */
8704 if (TREE_CODE (template) == IDENTIFIER_NODE)
8705 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8706 else if (DECL_CLASS_TEMPLATE_P (template)
8707 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8709 = finish_template_type (template, arguments,
8710 cp_lexer_next_token_is (parser->lexer,
8714 /* If it's not a class-template or a template-template, it should be
8715 a function-template. */
8716 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8717 || TREE_CODE (template) == OVERLOAD
8718 || BASELINK_P (template)));
8720 template_id = lookup_template_function (template, arguments);
8723 /* Retrieve any deferred checks. Do not pop this access checks yet
8724 so the memory will not be reclaimed during token replacing below. */
8725 access_check = get_deferred_access_checks ();
8727 /* If parsing tentatively, replace the sequence of tokens that makes
8728 up the template-id with a CPP_TEMPLATE_ID token. That way,
8729 should we re-parse the token stream, we will not have to repeat
8730 the effort required to do the parse, nor will we issue duplicate
8731 error messages about problems during instantiation of the
8735 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8737 /* Reset the contents of the START_OF_ID token. */
8738 token->type = CPP_TEMPLATE_ID;
8739 token->value = build_tree_list (access_check, template_id);
8740 token->keyword = RID_MAX;
8742 /* Purge all subsequent tokens. */
8743 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8745 /* ??? Can we actually assume that, if template_id ==
8746 error_mark_node, we will have issued a diagnostic to the
8747 user, as opposed to simply marking the tentative parse as
8749 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8750 error ("parse error in template argument list");
8753 pop_deferring_access_checks ();
8757 /* Parse a template-name.
8762 The standard should actually say:
8766 operator-function-id
8768 A defect report has been filed about this issue.
8770 A conversion-function-id cannot be a template name because they cannot
8771 be part of a template-id. In fact, looking at this code:
8775 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8776 It is impossible to call a templated conversion-function-id with an
8777 explicit argument list, since the only allowed template parameter is
8778 the type to which it is converting.
8780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8781 `template' keyword, in a construction like:
8785 In that case `f' is taken to be a template-name, even though there
8786 is no way of knowing for sure.
8788 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8789 name refers to a set of overloaded functions, at least one of which
8790 is a template, or an IDENTIFIER_NODE with the name of the template,
8791 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8792 names are looked up inside uninstantiated templates. */
8795 cp_parser_template_name (cp_parser* parser,
8796 bool template_keyword_p,
8797 bool check_dependency_p,
8798 bool is_declaration,
8799 bool *is_identifier)
8805 /* If the next token is `operator', then we have either an
8806 operator-function-id or a conversion-function-id. */
8807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8809 /* We don't know whether we're looking at an
8810 operator-function-id or a conversion-function-id. */
8811 cp_parser_parse_tentatively (parser);
8812 /* Try an operator-function-id. */
8813 identifier = cp_parser_operator_function_id (parser);
8814 /* If that didn't work, try a conversion-function-id. */
8815 if (!cp_parser_parse_definitely (parser))
8817 cp_parser_error (parser, "expected template-name");
8818 return error_mark_node;
8821 /* Look for the identifier. */
8823 identifier = cp_parser_identifier (parser);
8825 /* If we didn't find an identifier, we don't have a template-id. */
8826 if (identifier == error_mark_node)
8827 return error_mark_node;
8829 /* If the name immediately followed the `template' keyword, then it
8830 is a template-name. However, if the next token is not `<', then
8831 we do not treat it as a template-name, since it is not being used
8832 as part of a template-id. This enables us to handle constructs
8835 template <typename T> struct S { S(); };
8836 template <typename T> S<T>::S();
8838 correctly. We would treat `S' as a template -- if it were `S<T>'
8839 -- but we do not if there is no `<'. */
8841 if (processing_template_decl
8842 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8844 /* In a declaration, in a dependent context, we pretend that the
8845 "template" keyword was present in order to improve error
8846 recovery. For example, given:
8848 template <typename T> void f(T::X<int>);
8850 we want to treat "X<int>" as a template-id. */
8852 && !template_keyword_p
8853 && parser->scope && TYPE_P (parser->scope)
8854 && check_dependency_p
8855 && dependent_type_p (parser->scope)
8856 /* Do not do this for dtors (or ctors), since they never
8857 need the template keyword before their name. */
8858 && !constructor_name_p (identifier, parser->scope))
8860 cp_token_position start = 0;
8862 /* Explain what went wrong. */
8863 error ("non-template %qD used as template", identifier);
8864 inform ("use %<%T::template %D%> to indicate that it is a template",
8865 parser->scope, identifier);
8866 /* If parsing tentatively, find the location of the "<" token. */
8867 if (cp_parser_simulate_error (parser))
8868 start = cp_lexer_token_position (parser->lexer, true);
8869 /* Parse the template arguments so that we can issue error
8870 messages about them. */
8871 cp_lexer_consume_token (parser->lexer);
8872 cp_parser_enclosed_template_argument_list (parser);
8873 /* Skip tokens until we find a good place from which to
8874 continue parsing. */
8875 cp_parser_skip_to_closing_parenthesis (parser,
8876 /*recovering=*/true,
8878 /*consume_paren=*/false);
8879 /* If parsing tentatively, permanently remove the
8880 template argument list. That will prevent duplicate
8881 error messages from being issued about the missing
8882 "template" keyword. */
8884 cp_lexer_purge_tokens_after (parser->lexer, start);
8886 *is_identifier = true;
8890 /* If the "template" keyword is present, then there is generally
8891 no point in doing name-lookup, so we just return IDENTIFIER.
8892 But, if the qualifying scope is non-dependent then we can
8893 (and must) do name-lookup normally. */
8894 if (template_keyword_p
8896 || (TYPE_P (parser->scope)
8897 && dependent_type_p (parser->scope))))
8901 /* Look up the name. */
8902 decl = cp_parser_lookup_name (parser, identifier,
8904 /*is_template=*/false,
8905 /*is_namespace=*/false,
8907 /*ambiguous_decls=*/NULL);
8908 decl = maybe_get_template_decl_from_type_decl (decl);
8910 /* If DECL is a template, then the name was a template-name. */
8911 if (TREE_CODE (decl) == TEMPLATE_DECL)
8915 tree fn = NULL_TREE;
8917 /* The standard does not explicitly indicate whether a name that
8918 names a set of overloaded declarations, some of which are
8919 templates, is a template-name. However, such a name should
8920 be a template-name; otherwise, there is no way to form a
8921 template-id for the overloaded templates. */
8922 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8923 if (TREE_CODE (fns) == OVERLOAD)
8924 for (fn = fns; fn; fn = OVL_NEXT (fn))
8925 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8930 /* The name does not name a template. */
8931 cp_parser_error (parser, "expected template-name");
8932 return error_mark_node;
8936 /* If DECL is dependent, and refers to a function, then just return
8937 its name; we will look it up again during template instantiation. */
8938 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8940 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8941 if (TYPE_P (scope) && dependent_type_p (scope))
8948 /* Parse a template-argument-list.
8950 template-argument-list:
8952 template-argument-list , template-argument
8954 Returns a TREE_VEC containing the arguments. */
8957 cp_parser_template_argument_list (cp_parser* parser)
8959 tree fixed_args[10];
8960 unsigned n_args = 0;
8961 unsigned alloced = 10;
8962 tree *arg_ary = fixed_args;
8964 bool saved_in_template_argument_list_p;
8966 bool saved_non_ice_p;
8968 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8969 parser->in_template_argument_list_p = true;
8970 /* Even if the template-id appears in an integral
8971 constant-expression, the contents of the argument list do
8973 saved_ice_p = parser->integral_constant_expression_p;
8974 parser->integral_constant_expression_p = false;
8975 saved_non_ice_p = parser->non_integral_constant_expression_p;
8976 parser->non_integral_constant_expression_p = false;
8977 /* Parse the arguments. */
8983 /* Consume the comma. */
8984 cp_lexer_consume_token (parser->lexer);
8986 /* Parse the template-argument. */
8987 argument = cp_parser_template_argument (parser);
8988 if (n_args == alloced)
8992 if (arg_ary == fixed_args)
8994 arg_ary = XNEWVEC (tree, alloced);
8995 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8998 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9000 arg_ary[n_args++] = argument;
9002 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9004 vec = make_tree_vec (n_args);
9007 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9009 if (arg_ary != fixed_args)
9011 parser->non_integral_constant_expression_p = saved_non_ice_p;
9012 parser->integral_constant_expression_p = saved_ice_p;
9013 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9017 /* Parse a template-argument.
9020 assignment-expression
9024 The representation is that of an assignment-expression, type-id, or
9025 id-expression -- except that the qualified id-expression is
9026 evaluated, so that the value returned is either a DECL or an
9029 Although the standard says "assignment-expression", it forbids
9030 throw-expressions or assignments in the template argument.
9031 Therefore, we use "conditional-expression" instead. */
9034 cp_parser_template_argument (cp_parser* parser)
9039 bool maybe_type_id = false;
9043 /* There's really no way to know what we're looking at, so we just
9044 try each alternative in order.
9048 In a template-argument, an ambiguity between a type-id and an
9049 expression is resolved to a type-id, regardless of the form of
9050 the corresponding template-parameter.
9052 Therefore, we try a type-id first. */
9053 cp_parser_parse_tentatively (parser);
9054 argument = cp_parser_type_id (parser);
9055 /* If there was no error parsing the type-id but the next token is a '>>',
9056 we probably found a typo for '> >'. But there are type-id which are
9057 also valid expressions. For instance:
9059 struct X { int operator >> (int); };
9060 template <int V> struct Foo {};
9063 Here 'X()' is a valid type-id of a function type, but the user just
9064 wanted to write the expression "X() >> 5". Thus, we remember that we
9065 found a valid type-id, but we still try to parse the argument as an
9066 expression to see what happens. */
9067 if (!cp_parser_error_occurred (parser)
9068 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9070 maybe_type_id = true;
9071 cp_parser_abort_tentative_parse (parser);
9075 /* If the next token isn't a `,' or a `>', then this argument wasn't
9076 really finished. This means that the argument is not a valid
9078 if (!cp_parser_next_token_ends_template_argument_p (parser))
9079 cp_parser_error (parser, "expected template-argument");
9080 /* If that worked, we're done. */
9081 if (cp_parser_parse_definitely (parser))
9084 /* We're still not sure what the argument will be. */
9085 cp_parser_parse_tentatively (parser);
9086 /* Try a template. */
9087 argument = cp_parser_id_expression (parser,
9088 /*template_keyword_p=*/false,
9089 /*check_dependency_p=*/true,
9091 /*declarator_p=*/false);
9092 /* If the next token isn't a `,' or a `>', then this argument wasn't
9094 if (!cp_parser_next_token_ends_template_argument_p (parser))
9095 cp_parser_error (parser, "expected template-argument");
9096 if (!cp_parser_error_occurred (parser))
9098 /* Figure out what is being referred to. If the id-expression
9099 was for a class template specialization, then we will have a
9100 TYPE_DECL at this point. There is no need to do name lookup
9101 at this point in that case. */
9102 if (TREE_CODE (argument) != TYPE_DECL)
9103 argument = cp_parser_lookup_name (parser, argument,
9105 /*is_template=*/template_p,
9106 /*is_namespace=*/false,
9107 /*check_dependency=*/true,
9108 /*ambiguous_decls=*/NULL);
9109 if (TREE_CODE (argument) != TEMPLATE_DECL
9110 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9111 cp_parser_error (parser, "expected template-name");
9113 if (cp_parser_parse_definitely (parser))
9115 /* It must be a non-type argument. There permitted cases are given
9116 in [temp.arg.nontype]:
9118 -- an integral constant-expression of integral or enumeration
9121 -- the name of a non-type template-parameter; or
9123 -- the name of an object or function with external linkage...
9125 -- the address of an object or function with external linkage...
9127 -- a pointer to member... */
9128 /* Look for a non-type template parameter. */
9129 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9131 cp_parser_parse_tentatively (parser);
9132 argument = cp_parser_primary_expression (parser,
9135 /*template_arg_p=*/true,
9137 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9138 || !cp_parser_next_token_ends_template_argument_p (parser))
9139 cp_parser_simulate_error (parser);
9140 if (cp_parser_parse_definitely (parser))
9144 /* If the next token is "&", the argument must be the address of an
9145 object or function with external linkage. */
9146 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9148 cp_lexer_consume_token (parser->lexer);
9149 /* See if we might have an id-expression. */
9150 token = cp_lexer_peek_token (parser->lexer);
9151 if (token->type == CPP_NAME
9152 || token->keyword == RID_OPERATOR
9153 || token->type == CPP_SCOPE
9154 || token->type == CPP_TEMPLATE_ID
9155 || token->type == CPP_NESTED_NAME_SPECIFIER)
9157 cp_parser_parse_tentatively (parser);
9158 argument = cp_parser_primary_expression (parser,
9161 /*template_arg_p=*/true,
9163 if (cp_parser_error_occurred (parser)
9164 || !cp_parser_next_token_ends_template_argument_p (parser))
9165 cp_parser_abort_tentative_parse (parser);
9168 if (TREE_CODE (argument) == INDIRECT_REF)
9170 gcc_assert (REFERENCE_REF_P (argument));
9171 argument = TREE_OPERAND (argument, 0);
9174 if (TREE_CODE (argument) == BASELINK)
9175 /* We don't need the information about what class was used
9176 to name the overloaded functions. */
9177 argument = BASELINK_FUNCTIONS (argument);
9179 if (TREE_CODE (argument) == VAR_DECL)
9181 /* A variable without external linkage might still be a
9182 valid constant-expression, so no error is issued here
9183 if the external-linkage check fails. */
9184 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9185 cp_parser_simulate_error (parser);
9187 else if (is_overloaded_fn (argument))
9188 /* All overloaded functions are allowed; if the external
9189 linkage test does not pass, an error will be issued
9193 && (TREE_CODE (argument) == OFFSET_REF
9194 || TREE_CODE (argument) == SCOPE_REF))
9195 /* A pointer-to-member. */
9197 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9200 cp_parser_simulate_error (parser);
9202 if (cp_parser_parse_definitely (parser))
9205 argument = build_x_unary_op (ADDR_EXPR, argument);
9210 /* If the argument started with "&", there are no other valid
9211 alternatives at this point. */
9214 cp_parser_error (parser, "invalid non-type template argument");
9215 return error_mark_node;
9218 /* If the argument wasn't successfully parsed as a type-id followed
9219 by '>>', the argument can only be a constant expression now.
9220 Otherwise, we try parsing the constant-expression tentatively,
9221 because the argument could really be a type-id. */
9223 cp_parser_parse_tentatively (parser);
9224 argument = cp_parser_constant_expression (parser,
9225 /*allow_non_constant_p=*/false,
9226 /*non_constant_p=*/NULL);
9227 argument = fold_non_dependent_expr (argument);
9230 if (!cp_parser_next_token_ends_template_argument_p (parser))
9231 cp_parser_error (parser, "expected template-argument");
9232 if (cp_parser_parse_definitely (parser))
9234 /* We did our best to parse the argument as a non type-id, but that
9235 was the only alternative that matched (albeit with a '>' after
9236 it). We can assume it's just a typo from the user, and a
9237 diagnostic will then be issued. */
9238 return cp_parser_type_id (parser);
9241 /* Parse an explicit-instantiation.
9243 explicit-instantiation:
9244 template declaration
9246 Although the standard says `declaration', what it really means is:
9248 explicit-instantiation:
9249 template decl-specifier-seq [opt] declarator [opt] ;
9251 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9252 supposed to be allowed. A defect report has been filed about this
9257 explicit-instantiation:
9258 storage-class-specifier template
9259 decl-specifier-seq [opt] declarator [opt] ;
9260 function-specifier template
9261 decl-specifier-seq [opt] declarator [opt] ; */
9264 cp_parser_explicit_instantiation (cp_parser* parser)
9266 int declares_class_or_enum;
9267 cp_decl_specifier_seq decl_specifiers;
9268 tree extension_specifier = NULL_TREE;
9270 /* Look for an (optional) storage-class-specifier or
9271 function-specifier. */
9272 if (cp_parser_allow_gnu_extensions_p (parser))
9275 = cp_parser_storage_class_specifier_opt (parser);
9276 if (!extension_specifier)
9278 = cp_parser_function_specifier_opt (parser,
9279 /*decl_specs=*/NULL);
9282 /* Look for the `template' keyword. */
9283 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9284 /* Let the front end know that we are processing an explicit
9286 begin_explicit_instantiation ();
9287 /* [temp.explicit] says that we are supposed to ignore access
9288 control while processing explicit instantiation directives. */
9289 push_deferring_access_checks (dk_no_check);
9290 /* Parse a decl-specifier-seq. */
9291 cp_parser_decl_specifier_seq (parser,
9292 CP_PARSER_FLAGS_OPTIONAL,
9294 &declares_class_or_enum);
9295 /* If there was exactly one decl-specifier, and it declared a class,
9296 and there's no declarator, then we have an explicit type
9298 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9302 type = check_tag_decl (&decl_specifiers);
9303 /* Turn access control back on for names used during
9304 template instantiation. */
9305 pop_deferring_access_checks ();
9307 do_type_instantiation (type, extension_specifier,
9308 /*complain=*/tf_error);
9312 cp_declarator *declarator;
9315 /* Parse the declarator. */
9317 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9318 /*ctor_dtor_or_conv_p=*/NULL,
9319 /*parenthesized_p=*/NULL,
9320 /*member_p=*/false);
9321 if (declares_class_or_enum & 2)
9322 cp_parser_check_for_definition_in_return_type (declarator,
9323 decl_specifiers.type);
9324 if (declarator != cp_error_declarator)
9326 decl = grokdeclarator (declarator, &decl_specifiers,
9328 /* Turn access control back on for names used during
9329 template instantiation. */
9330 pop_deferring_access_checks ();
9331 /* Do the explicit instantiation. */
9332 do_decl_instantiation (decl, extension_specifier);
9336 pop_deferring_access_checks ();
9337 /* Skip the body of the explicit instantiation. */
9338 cp_parser_skip_to_end_of_statement (parser);
9341 /* We're done with the instantiation. */
9342 end_explicit_instantiation ();
9344 cp_parser_consume_semicolon_at_end_of_statement (parser);
9347 /* Parse an explicit-specialization.
9349 explicit-specialization:
9350 template < > declaration
9352 Although the standard says `declaration', what it really means is:
9354 explicit-specialization:
9355 template <> decl-specifier [opt] init-declarator [opt] ;
9356 template <> function-definition
9357 template <> explicit-specialization
9358 template <> template-declaration */
9361 cp_parser_explicit_specialization (cp_parser* parser)
9364 /* Look for the `template' keyword. */
9365 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9366 /* Look for the `<'. */
9367 cp_parser_require (parser, CPP_LESS, "`<'");
9368 /* Look for the `>'. */
9369 cp_parser_require (parser, CPP_GREATER, "`>'");
9370 /* We have processed another parameter list. */
9371 ++parser->num_template_parameter_lists;
9374 A template ... explicit specialization ... shall not have C
9376 if (current_lang_name == lang_name_c)
9378 error ("template specialization with C linkage");
9379 /* Give it C++ linkage to avoid confusing other parts of the
9381 push_lang_context (lang_name_cplusplus);
9382 need_lang_pop = true;
9385 need_lang_pop = false;
9386 /* Let the front end know that we are beginning a specialization. */
9387 begin_specialization ();
9388 /* If the next keyword is `template', we need to figure out whether
9389 or not we're looking a template-declaration. */
9390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9392 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9393 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9394 cp_parser_template_declaration_after_export (parser,
9395 /*member_p=*/false);
9397 cp_parser_explicit_specialization (parser);
9400 /* Parse the dependent declaration. */
9401 cp_parser_single_declaration (parser,
9404 /* We're done with the specialization. */
9405 end_specialization ();
9406 /* For the erroneous case of a template with C linkage, we pushed an
9407 implicit C++ linkage scope; exit that scope now. */
9409 pop_lang_context ();
9410 /* We're done with this parameter list. */
9411 --parser->num_template_parameter_lists;
9414 /* Parse a type-specifier.
9417 simple-type-specifier
9420 elaborated-type-specifier
9428 Returns a representation of the type-specifier. For a
9429 class-specifier, enum-specifier, or elaborated-type-specifier, a
9430 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9432 The parser flags FLAGS is used to control type-specifier parsing.
9434 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9435 in a decl-specifier-seq.
9437 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9438 class-specifier, enum-specifier, or elaborated-type-specifier, then
9439 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9440 if a type is declared; 2 if it is defined. Otherwise, it is set to
9443 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9444 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9448 cp_parser_type_specifier (cp_parser* parser,
9449 cp_parser_flags flags,
9450 cp_decl_specifier_seq *decl_specs,
9451 bool is_declaration,
9452 int* declares_class_or_enum,
9453 bool* is_cv_qualifier)
9455 tree type_spec = NULL_TREE;
9458 cp_decl_spec ds = ds_last;
9460 /* Assume this type-specifier does not declare a new type. */
9461 if (declares_class_or_enum)
9462 *declares_class_or_enum = 0;
9463 /* And that it does not specify a cv-qualifier. */
9464 if (is_cv_qualifier)
9465 *is_cv_qualifier = false;
9466 /* Peek at the next token. */
9467 token = cp_lexer_peek_token (parser->lexer);
9469 /* If we're looking at a keyword, we can use that to guide the
9470 production we choose. */
9471 keyword = token->keyword;
9475 /* 'enum' [identifier] '{' introduces an enum-specifier;
9476 'enum' <anything else> introduces an elaborated-type-specifier. */
9477 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9478 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9479 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9482 if (parser->num_template_parameter_lists)
9484 error ("template declaration of %qs", "enum");
9485 cp_parser_skip_to_end_of_block_or_statement (parser);
9486 type_spec = error_mark_node;
9489 type_spec = cp_parser_enum_specifier (parser);
9491 if (declares_class_or_enum)
9492 *declares_class_or_enum = 2;
9494 cp_parser_set_decl_spec_type (decl_specs,
9496 /*user_defined_p=*/true);
9500 goto elaborated_type_specifier;
9502 /* Any of these indicate either a class-specifier, or an
9503 elaborated-type-specifier. */
9507 /* Parse tentatively so that we can back up if we don't find a
9509 cp_parser_parse_tentatively (parser);
9510 /* Look for the class-specifier. */
9511 type_spec = cp_parser_class_specifier (parser);
9512 /* If that worked, we're done. */
9513 if (cp_parser_parse_definitely (parser))
9515 if (declares_class_or_enum)
9516 *declares_class_or_enum = 2;
9518 cp_parser_set_decl_spec_type (decl_specs,
9520 /*user_defined_p=*/true);
9525 elaborated_type_specifier:
9526 /* We're declaring (not defining) a class or enum. */
9527 if (declares_class_or_enum)
9528 *declares_class_or_enum = 1;
9532 /* Look for an elaborated-type-specifier. */
9534 = (cp_parser_elaborated_type_specifier
9536 decl_specs && decl_specs->specs[(int) ds_friend],
9539 cp_parser_set_decl_spec_type (decl_specs,
9541 /*user_defined_p=*/true);
9546 if (is_cv_qualifier)
9547 *is_cv_qualifier = true;
9552 if (is_cv_qualifier)
9553 *is_cv_qualifier = true;
9558 if (is_cv_qualifier)
9559 *is_cv_qualifier = true;
9563 /* The `__complex__' keyword is a GNU extension. */
9571 /* Handle simple keywords. */
9576 ++decl_specs->specs[(int)ds];
9577 decl_specs->any_specifiers_p = true;
9579 return cp_lexer_consume_token (parser->lexer)->value;
9582 /* If we do not already have a type-specifier, assume we are looking
9583 at a simple-type-specifier. */
9584 type_spec = cp_parser_simple_type_specifier (parser,
9588 /* If we didn't find a type-specifier, and a type-specifier was not
9589 optional in this context, issue an error message. */
9590 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9592 cp_parser_error (parser, "expected type specifier");
9593 return error_mark_node;
9599 /* Parse a simple-type-specifier.
9601 simple-type-specifier:
9602 :: [opt] nested-name-specifier [opt] type-name
9603 :: [opt] nested-name-specifier template template-id
9618 simple-type-specifier:
9619 __typeof__ unary-expression
9620 __typeof__ ( type-id )
9622 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9623 appropriately updated. */
9626 cp_parser_simple_type_specifier (cp_parser* parser,
9627 cp_decl_specifier_seq *decl_specs,
9628 cp_parser_flags flags)
9630 tree type = NULL_TREE;
9633 /* Peek at the next token. */
9634 token = cp_lexer_peek_token (parser->lexer);
9636 /* If we're looking at a keyword, things are easy. */
9637 switch (token->keyword)
9641 decl_specs->explicit_char_p = true;
9642 type = char_type_node;
9645 type = wchar_type_node;
9648 type = boolean_type_node;
9652 ++decl_specs->specs[(int) ds_short];
9653 type = short_integer_type_node;
9657 decl_specs->explicit_int_p = true;
9658 type = integer_type_node;
9662 ++decl_specs->specs[(int) ds_long];
9663 type = long_integer_type_node;
9667 ++decl_specs->specs[(int) ds_signed];
9668 type = integer_type_node;
9672 ++decl_specs->specs[(int) ds_unsigned];
9673 type = unsigned_type_node;
9676 type = float_type_node;
9679 type = double_type_node;
9682 type = void_type_node;
9686 /* Consume the `typeof' token. */
9687 cp_lexer_consume_token (parser->lexer);
9688 /* Parse the operand to `typeof'. */
9689 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9690 /* If it is not already a TYPE, take its type. */
9692 type = finish_typeof (type);
9695 cp_parser_set_decl_spec_type (decl_specs, type,
9696 /*user_defined_p=*/true);
9704 /* If the type-specifier was for a built-in type, we're done. */
9709 /* Record the type. */
9711 && (token->keyword != RID_SIGNED
9712 && token->keyword != RID_UNSIGNED
9713 && token->keyword != RID_SHORT
9714 && token->keyword != RID_LONG))
9715 cp_parser_set_decl_spec_type (decl_specs,
9717 /*user_defined=*/false);
9719 decl_specs->any_specifiers_p = true;
9721 /* Consume the token. */
9722 id = cp_lexer_consume_token (parser->lexer)->value;
9724 /* There is no valid C++ program where a non-template type is
9725 followed by a "<". That usually indicates that the user thought
9726 that the type was a template. */
9727 cp_parser_check_for_invalid_template_id (parser, type);
9729 return TYPE_NAME (type);
9732 /* The type-specifier must be a user-defined type. */
9733 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9738 /* Don't gobble tokens or issue error messages if this is an
9739 optional type-specifier. */
9740 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9741 cp_parser_parse_tentatively (parser);
9743 /* Look for the optional `::' operator. */
9745 = (cp_parser_global_scope_opt (parser,
9746 /*current_scope_valid_p=*/false)
9748 /* Look for the nested-name specifier. */
9750 = (cp_parser_nested_name_specifier_opt (parser,
9751 /*typename_keyword_p=*/false,
9752 /*check_dependency_p=*/true,
9754 /*is_declaration=*/false)
9756 /* If we have seen a nested-name-specifier, and the next token
9757 is `template', then we are using the template-id production. */
9759 && cp_parser_optional_template_keyword (parser))
9761 /* Look for the template-id. */
9762 type = cp_parser_template_id (parser,
9763 /*template_keyword_p=*/true,
9764 /*check_dependency_p=*/true,
9765 /*is_declaration=*/false);
9766 /* If the template-id did not name a type, we are out of
9768 if (TREE_CODE (type) != TYPE_DECL)
9770 cp_parser_error (parser, "expected template-id for type");
9774 /* Otherwise, look for a type-name. */
9776 type = cp_parser_type_name (parser);
9777 /* Keep track of all name-lookups performed in class scopes. */
9781 && TREE_CODE (type) == TYPE_DECL
9782 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9783 maybe_note_name_used_in_class (DECL_NAME (type), type);
9784 /* If it didn't work out, we don't have a TYPE. */
9785 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9786 && !cp_parser_parse_definitely (parser))
9788 if (type && decl_specs)
9789 cp_parser_set_decl_spec_type (decl_specs, type,
9790 /*user_defined=*/true);
9793 /* If we didn't get a type-name, issue an error message. */
9794 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9796 cp_parser_error (parser, "expected type-name");
9797 return error_mark_node;
9800 /* There is no valid C++ program where a non-template type is
9801 followed by a "<". That usually indicates that the user thought
9802 that the type was a template. */
9803 if (type && type != error_mark_node)
9805 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9806 If it is, then the '<'...'>' enclose protocol names rather than
9807 template arguments, and so everything is fine. */
9808 if (c_dialect_objc ()
9809 && (objc_is_id (type) || objc_is_class_name (type)))
9811 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9812 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9814 /* Clobber the "unqualified" type previously entered into
9815 DECL_SPECS with the new, improved protocol-qualified version. */
9817 decl_specs->type = qual_type;
9822 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9828 /* Parse a type-name.
9841 Returns a TYPE_DECL for the type. */
9844 cp_parser_type_name (cp_parser* parser)
9849 /* We can't know yet whether it is a class-name or not. */
9850 cp_parser_parse_tentatively (parser);
9851 /* Try a class-name. */
9852 type_decl = cp_parser_class_name (parser,
9853 /*typename_keyword_p=*/false,
9854 /*template_keyword_p=*/false,
9856 /*check_dependency_p=*/true,
9857 /*class_head_p=*/false,
9858 /*is_declaration=*/false);
9859 /* If it's not a class-name, keep looking. */
9860 if (!cp_parser_parse_definitely (parser))
9862 /* It must be a typedef-name or an enum-name. */
9863 identifier = cp_parser_identifier (parser);
9864 if (identifier == error_mark_node)
9865 return error_mark_node;
9867 /* Look up the type-name. */
9868 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9870 if (TREE_CODE (type_decl) != TYPE_DECL
9871 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9873 /* See if this is an Objective-C type. */
9874 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9875 tree type = objc_get_protocol_qualified_type (identifier, protos);
9877 type_decl = TYPE_NAME (type);
9880 /* Issue an error if we did not find a type-name. */
9881 if (TREE_CODE (type_decl) != TYPE_DECL)
9883 if (!cp_parser_simulate_error (parser))
9884 cp_parser_name_lookup_error (parser, identifier, type_decl,
9886 type_decl = error_mark_node;
9888 /* Remember that the name was used in the definition of the
9889 current class so that we can check later to see if the
9890 meaning would have been different after the class was
9891 entirely defined. */
9892 else if (type_decl != error_mark_node
9894 maybe_note_name_used_in_class (identifier, type_decl);
9901 /* Parse an elaborated-type-specifier. Note that the grammar given
9902 here incorporates the resolution to DR68.
9904 elaborated-type-specifier:
9905 class-key :: [opt] nested-name-specifier [opt] identifier
9906 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9907 enum :: [opt] nested-name-specifier [opt] identifier
9908 typename :: [opt] nested-name-specifier identifier
9909 typename :: [opt] nested-name-specifier template [opt]
9914 elaborated-type-specifier:
9915 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9916 class-key attributes :: [opt] nested-name-specifier [opt]
9917 template [opt] template-id
9918 enum attributes :: [opt] nested-name-specifier [opt] identifier
9920 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9921 declared `friend'. If IS_DECLARATION is TRUE, then this
9922 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9923 something is being declared.
9925 Returns the TYPE specified. */
9928 cp_parser_elaborated_type_specifier (cp_parser* parser,
9930 bool is_declaration)
9932 enum tag_types tag_type;
9934 tree type = NULL_TREE;
9935 tree attributes = NULL_TREE;
9937 /* See if we're looking at the `enum' keyword. */
9938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9940 /* Consume the `enum' token. */
9941 cp_lexer_consume_token (parser->lexer);
9942 /* Remember that it's an enumeration type. */
9943 tag_type = enum_type;
9944 /* Parse the attributes. */
9945 attributes = cp_parser_attributes_opt (parser);
9947 /* Or, it might be `typename'. */
9948 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9951 /* Consume the `typename' token. */
9952 cp_lexer_consume_token (parser->lexer);
9953 /* Remember that it's a `typename' type. */
9954 tag_type = typename_type;
9955 /* The `typename' keyword is only allowed in templates. */
9956 if (!processing_template_decl)
9957 pedwarn ("using %<typename%> outside of template");
9959 /* Otherwise it must be a class-key. */
9962 tag_type = cp_parser_class_key (parser);
9963 if (tag_type == none_type)
9964 return error_mark_node;
9965 /* Parse the attributes. */
9966 attributes = cp_parser_attributes_opt (parser);
9969 /* Look for the `::' operator. */
9970 cp_parser_global_scope_opt (parser,
9971 /*current_scope_valid_p=*/false);
9972 /* Look for the nested-name-specifier. */
9973 if (tag_type == typename_type)
9975 if (!cp_parser_nested_name_specifier (parser,
9976 /*typename_keyword_p=*/true,
9977 /*check_dependency_p=*/true,
9980 return error_mark_node;
9983 /* Even though `typename' is not present, the proposed resolution
9984 to Core Issue 180 says that in `class A<T>::B', `B' should be
9985 considered a type-name, even if `A<T>' is dependent. */
9986 cp_parser_nested_name_specifier_opt (parser,
9987 /*typename_keyword_p=*/true,
9988 /*check_dependency_p=*/true,
9991 /* For everything but enumeration types, consider a template-id. */
9992 if (tag_type != enum_type)
9994 bool template_p = false;
9997 /* Allow the `template' keyword. */
9998 template_p = cp_parser_optional_template_keyword (parser);
9999 /* If we didn't see `template', we don't know if there's a
10000 template-id or not. */
10002 cp_parser_parse_tentatively (parser);
10003 /* Parse the template-id. */
10004 decl = cp_parser_template_id (parser, template_p,
10005 /*check_dependency_p=*/true,
10007 /* If we didn't find a template-id, look for an ordinary
10009 if (!template_p && !cp_parser_parse_definitely (parser))
10011 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10012 in effect, then we must assume that, upon instantiation, the
10013 template will correspond to a class. */
10014 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10015 && tag_type == typename_type)
10016 type = make_typename_type (parser->scope, decl,
10018 /*complain=*/tf_error);
10020 type = TREE_TYPE (decl);
10023 /* For an enumeration type, consider only a plain identifier. */
10026 identifier = cp_parser_identifier (parser);
10028 if (identifier == error_mark_node)
10030 parser->scope = NULL_TREE;
10031 return error_mark_node;
10034 /* For a `typename', we needn't call xref_tag. */
10035 if (tag_type == typename_type
10036 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10037 return cp_parser_make_typename_type (parser, parser->scope,
10039 /* Look up a qualified name in the usual way. */
10044 decl = cp_parser_lookup_name (parser, identifier,
10046 /*is_template=*/false,
10047 /*is_namespace=*/false,
10048 /*check_dependency=*/true,
10049 /*ambiguous_decls=*/NULL);
10051 /* If we are parsing friend declaration, DECL may be a
10052 TEMPLATE_DECL tree node here. However, we need to check
10053 whether this TEMPLATE_DECL results in valid code. Consider
10054 the following example:
10057 template <class T> class C {};
10060 template <class T> friend class N::C; // #1, valid code
10062 template <class T> class Y {
10063 friend class N::C; // #2, invalid code
10066 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10067 name lookup of `N::C'. We see that friend declaration must
10068 be template for the code to be valid. Note that
10069 processing_template_decl does not work here since it is
10070 always 1 for the above two cases. */
10072 decl = (cp_parser_maybe_treat_template_as_class
10073 (decl, /*tag_name_p=*/is_friend
10074 && parser->num_template_parameter_lists));
10076 if (TREE_CODE (decl) != TYPE_DECL)
10078 cp_parser_diagnose_invalid_type_name (parser,
10081 return error_mark_node;
10084 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10085 check_elaborated_type_specifier
10087 (parser->num_template_parameter_lists
10088 || DECL_SELF_REFERENCE_P (decl)));
10090 type = TREE_TYPE (decl);
10094 /* An elaborated-type-specifier sometimes introduces a new type and
10095 sometimes names an existing type. Normally, the rule is that it
10096 introduces a new type only if there is not an existing type of
10097 the same name already in scope. For example, given:
10100 void f() { struct S s; }
10102 the `struct S' in the body of `f' is the same `struct S' as in
10103 the global scope; the existing definition is used. However, if
10104 there were no global declaration, this would introduce a new
10105 local class named `S'.
10107 An exception to this rule applies to the following code:
10109 namespace N { struct S; }
10111 Here, the elaborated-type-specifier names a new type
10112 unconditionally; even if there is already an `S' in the
10113 containing scope this declaration names a new type.
10114 This exception only applies if the elaborated-type-specifier
10115 forms the complete declaration:
10119 A declaration consisting solely of `class-key identifier ;' is
10120 either a redeclaration of the name in the current scope or a
10121 forward declaration of the identifier as a class name. It
10122 introduces the name into the current scope.
10124 We are in this situation precisely when the next token is a `;'.
10126 An exception to the exception is that a `friend' declaration does
10127 *not* name a new type; i.e., given:
10129 struct S { friend struct T; };
10131 `T' is not a new type in the scope of `S'.
10133 Also, `new struct S' or `sizeof (struct S)' never results in the
10134 definition of a new type; a new type can only be declared in a
10135 declaration context. */
10141 /* Friends have special name lookup rules. */
10142 ts = ts_within_enclosing_non_class;
10143 else if (is_declaration
10144 && cp_lexer_next_token_is (parser->lexer,
10146 /* This is a `class-key identifier ;' */
10151 /* Warn about attributes. They are ignored. */
10153 warning (OPT_Wattributes,
10154 "type attributes are honored only at type definition");
10157 (parser->num_template_parameter_lists
10158 && (cp_parser_next_token_starts_class_definition_p (parser)
10159 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10160 /* An unqualified name was used to reference this type, so
10161 there were no qualifying templates. */
10162 if (!cp_parser_check_template_parameters (parser,
10163 /*num_templates=*/0))
10164 return error_mark_node;
10165 type = xref_tag (tag_type, identifier, ts, template_p);
10168 if (tag_type != enum_type)
10169 cp_parser_check_class_key (tag_type, type);
10171 /* A "<" cannot follow an elaborated type specifier. If that
10172 happens, the user was probably trying to form a template-id. */
10173 cp_parser_check_for_invalid_template_id (parser, type);
10178 /* Parse an enum-specifier.
10181 enum identifier [opt] { enumerator-list [opt] }
10184 enum identifier [opt] { enumerator-list [opt] } attributes
10186 Returns an ENUM_TYPE representing the enumeration. */
10189 cp_parser_enum_specifier (cp_parser* parser)
10194 /* Caller guarantees that the current token is 'enum', an identifier
10195 possibly follows, and the token after that is an opening brace.
10196 If we don't have an identifier, fabricate an anonymous name for
10197 the enumeration being defined. */
10198 cp_lexer_consume_token (parser->lexer);
10200 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10201 identifier = cp_parser_identifier (parser);
10203 identifier = make_anon_name ();
10205 /* Issue an error message if type-definitions are forbidden here. */
10206 cp_parser_check_type_definition (parser);
10208 /* Create the new type. We do this before consuming the opening brace
10209 so the enum will be recorded as being on the line of its tag (or the
10210 'enum' keyword, if there is no tag). */
10211 type = start_enum (identifier);
10213 /* Consume the opening brace. */
10214 cp_lexer_consume_token (parser->lexer);
10216 /* If the next token is not '}', then there are some enumerators. */
10217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10218 cp_parser_enumerator_list (parser, type);
10220 /* Consume the final '}'. */
10221 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10223 /* Look for trailing attributes to apply to this enumeration, and
10224 apply them if appropriate. */
10225 if (cp_parser_allow_gnu_extensions_p (parser))
10227 tree trailing_attr = cp_parser_attributes_opt (parser);
10228 cplus_decl_attributes (&type,
10230 (int) ATTR_FLAG_TYPE_IN_PLACE);
10233 /* Finish up the enumeration. */
10234 finish_enum (type);
10239 /* Parse an enumerator-list. The enumerators all have the indicated
10243 enumerator-definition
10244 enumerator-list , enumerator-definition */
10247 cp_parser_enumerator_list (cp_parser* parser, tree type)
10251 /* Parse an enumerator-definition. */
10252 cp_parser_enumerator_definition (parser, type);
10254 /* If the next token is not a ',', we've reached the end of
10256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10258 /* Otherwise, consume the `,' and keep going. */
10259 cp_lexer_consume_token (parser->lexer);
10260 /* If the next token is a `}', there is a trailing comma. */
10261 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10263 if (pedantic && !in_system_header)
10264 pedwarn ("comma at end of enumerator list");
10270 /* Parse an enumerator-definition. The enumerator has the indicated
10273 enumerator-definition:
10275 enumerator = constant-expression
10281 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10286 /* Look for the identifier. */
10287 identifier = cp_parser_identifier (parser);
10288 if (identifier == error_mark_node)
10291 /* If the next token is an '=', then there is an explicit value. */
10292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10294 /* Consume the `=' token. */
10295 cp_lexer_consume_token (parser->lexer);
10296 /* Parse the value. */
10297 value = cp_parser_constant_expression (parser,
10298 /*allow_non_constant_p=*/false,
10304 /* Create the enumerator. */
10305 build_enumerator (identifier, value, type);
10308 /* Parse a namespace-name.
10311 original-namespace-name
10314 Returns the NAMESPACE_DECL for the namespace. */
10317 cp_parser_namespace_name (cp_parser* parser)
10320 tree namespace_decl;
10322 /* Get the name of the namespace. */
10323 identifier = cp_parser_identifier (parser);
10324 if (identifier == error_mark_node)
10325 return error_mark_node;
10327 /* Look up the identifier in the currently active scope. Look only
10328 for namespaces, due to:
10330 [basic.lookup.udir]
10332 When looking up a namespace-name in a using-directive or alias
10333 definition, only namespace names are considered.
10337 [basic.lookup.qual]
10339 During the lookup of a name preceding the :: scope resolution
10340 operator, object, function, and enumerator names are ignored.
10342 (Note that cp_parser_class_or_namespace_name only calls this
10343 function if the token after the name is the scope resolution
10345 namespace_decl = cp_parser_lookup_name (parser, identifier,
10347 /*is_template=*/false,
10348 /*is_namespace=*/true,
10349 /*check_dependency=*/true,
10350 /*ambiguous_decls=*/NULL);
10351 /* If it's not a namespace, issue an error. */
10352 if (namespace_decl == error_mark_node
10353 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10355 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10356 error ("%qD is not a namespace-name", identifier);
10357 cp_parser_error (parser, "expected namespace-name");
10358 namespace_decl = error_mark_node;
10361 return namespace_decl;
10364 /* Parse a namespace-definition.
10366 namespace-definition:
10367 named-namespace-definition
10368 unnamed-namespace-definition
10370 named-namespace-definition:
10371 original-namespace-definition
10372 extension-namespace-definition
10374 original-namespace-definition:
10375 namespace identifier { namespace-body }
10377 extension-namespace-definition:
10378 namespace original-namespace-name { namespace-body }
10380 unnamed-namespace-definition:
10381 namespace { namespace-body } */
10384 cp_parser_namespace_definition (cp_parser* parser)
10388 /* Look for the `namespace' keyword. */
10389 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10391 /* Get the name of the namespace. We do not attempt to distinguish
10392 between an original-namespace-definition and an
10393 extension-namespace-definition at this point. The semantic
10394 analysis routines are responsible for that. */
10395 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10396 identifier = cp_parser_identifier (parser);
10398 identifier = NULL_TREE;
10400 /* Look for the `{' to start the namespace. */
10401 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10402 /* Start the namespace. */
10403 push_namespace (identifier);
10404 /* Parse the body of the namespace. */
10405 cp_parser_namespace_body (parser);
10406 /* Finish the namespace. */
10408 /* Look for the final `}'. */
10409 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10412 /* Parse a namespace-body.
10415 declaration-seq [opt] */
10418 cp_parser_namespace_body (cp_parser* parser)
10420 cp_parser_declaration_seq_opt (parser);
10423 /* Parse a namespace-alias-definition.
10425 namespace-alias-definition:
10426 namespace identifier = qualified-namespace-specifier ; */
10429 cp_parser_namespace_alias_definition (cp_parser* parser)
10432 tree namespace_specifier;
10434 /* Look for the `namespace' keyword. */
10435 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10436 /* Look for the identifier. */
10437 identifier = cp_parser_identifier (parser);
10438 if (identifier == error_mark_node)
10440 /* Look for the `=' token. */
10441 cp_parser_require (parser, CPP_EQ, "`='");
10442 /* Look for the qualified-namespace-specifier. */
10443 namespace_specifier
10444 = cp_parser_qualified_namespace_specifier (parser);
10445 /* Look for the `;' token. */
10446 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10448 /* Register the alias in the symbol table. */
10449 do_namespace_alias (identifier, namespace_specifier);
10452 /* Parse a qualified-namespace-specifier.
10454 qualified-namespace-specifier:
10455 :: [opt] nested-name-specifier [opt] namespace-name
10457 Returns a NAMESPACE_DECL corresponding to the specified
10461 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10463 /* Look for the optional `::'. */
10464 cp_parser_global_scope_opt (parser,
10465 /*current_scope_valid_p=*/false);
10467 /* Look for the optional nested-name-specifier. */
10468 cp_parser_nested_name_specifier_opt (parser,
10469 /*typename_keyword_p=*/false,
10470 /*check_dependency_p=*/true,
10472 /*is_declaration=*/true);
10474 return cp_parser_namespace_name (parser);
10477 /* Parse a using-declaration.
10480 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10481 using :: unqualified-id ; */
10484 cp_parser_using_declaration (cp_parser* parser)
10487 bool typename_p = false;
10488 bool global_scope_p;
10493 /* Look for the `using' keyword. */
10494 cp_parser_require_keyword (parser, RID_USING, "`using'");
10496 /* Peek at the next token. */
10497 token = cp_lexer_peek_token (parser->lexer);
10498 /* See if it's `typename'. */
10499 if (token->keyword == RID_TYPENAME)
10501 /* Remember that we've seen it. */
10503 /* Consume the `typename' token. */
10504 cp_lexer_consume_token (parser->lexer);
10507 /* Look for the optional global scope qualification. */
10509 = (cp_parser_global_scope_opt (parser,
10510 /*current_scope_valid_p=*/false)
10513 /* If we saw `typename', or didn't see `::', then there must be a
10514 nested-name-specifier present. */
10515 if (typename_p || !global_scope_p)
10516 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10517 /*check_dependency_p=*/true,
10519 /*is_declaration=*/true);
10520 /* Otherwise, we could be in either of the two productions. In that
10521 case, treat the nested-name-specifier as optional. */
10523 qscope = cp_parser_nested_name_specifier_opt (parser,
10524 /*typename_keyword_p=*/false,
10525 /*check_dependency_p=*/true,
10527 /*is_declaration=*/true);
10529 qscope = global_namespace;
10531 /* Parse the unqualified-id. */
10532 identifier = cp_parser_unqualified_id (parser,
10533 /*template_keyword_p=*/false,
10534 /*check_dependency_p=*/true,
10535 /*declarator_p=*/true);
10537 /* The function we call to handle a using-declaration is different
10538 depending on what scope we are in. */
10539 if (qscope == error_mark_node || identifier == error_mark_node)
10541 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10542 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10543 /* [namespace.udecl]
10545 A using declaration shall not name a template-id. */
10546 error ("a template-id may not appear in a using-declaration");
10549 if (at_class_scope_p ())
10551 /* Create the USING_DECL. */
10552 decl = do_class_using_decl (parser->scope, identifier);
10553 /* Add it to the list of members in this class. */
10554 finish_member_declaration (decl);
10558 decl = cp_parser_lookup_name_simple (parser, identifier);
10559 if (decl == error_mark_node)
10560 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10561 else if (!at_namespace_scope_p ())
10562 do_local_using_decl (decl, qscope, identifier);
10564 do_toplevel_using_decl (decl, qscope, identifier);
10568 /* Look for the final `;'. */
10569 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10572 /* Parse a using-directive.
10575 using namespace :: [opt] nested-name-specifier [opt]
10576 namespace-name ; */
10579 cp_parser_using_directive (cp_parser* parser)
10581 tree namespace_decl;
10584 /* Look for the `using' keyword. */
10585 cp_parser_require_keyword (parser, RID_USING, "`using'");
10586 /* And the `namespace' keyword. */
10587 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10588 /* Look for the optional `::' operator. */
10589 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10590 /* And the optional nested-name-specifier. */
10591 cp_parser_nested_name_specifier_opt (parser,
10592 /*typename_keyword_p=*/false,
10593 /*check_dependency_p=*/true,
10595 /*is_declaration=*/true);
10596 /* Get the namespace being used. */
10597 namespace_decl = cp_parser_namespace_name (parser);
10598 /* And any specified attributes. */
10599 attribs = cp_parser_attributes_opt (parser);
10600 /* Update the symbol table. */
10601 parse_using_directive (namespace_decl, attribs);
10602 /* Look for the final `;'. */
10603 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10606 /* Parse an asm-definition.
10609 asm ( string-literal ) ;
10614 asm volatile [opt] ( string-literal ) ;
10615 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10616 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10617 : asm-operand-list [opt] ) ;
10618 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10619 : asm-operand-list [opt]
10620 : asm-operand-list [opt] ) ; */
10623 cp_parser_asm_definition (cp_parser* parser)
10626 tree outputs = NULL_TREE;
10627 tree inputs = NULL_TREE;
10628 tree clobbers = NULL_TREE;
10630 bool volatile_p = false;
10631 bool extended_p = false;
10633 /* Look for the `asm' keyword. */
10634 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10635 /* See if the next token is `volatile'. */
10636 if (cp_parser_allow_gnu_extensions_p (parser)
10637 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10639 /* Remember that we saw the `volatile' keyword. */
10641 /* Consume the token. */
10642 cp_lexer_consume_token (parser->lexer);
10644 /* Look for the opening `('. */
10645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10647 /* Look for the string. */
10648 string = cp_parser_string_literal (parser, false, false);
10649 if (string == error_mark_node)
10651 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10652 /*consume_paren=*/true);
10656 /* If we're allowing GNU extensions, check for the extended assembly
10657 syntax. Unfortunately, the `:' tokens need not be separated by
10658 a space in C, and so, for compatibility, we tolerate that here
10659 too. Doing that means that we have to treat the `::' operator as
10661 if (cp_parser_allow_gnu_extensions_p (parser)
10662 && at_function_scope_p ()
10663 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10664 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10666 bool inputs_p = false;
10667 bool clobbers_p = false;
10669 /* The extended syntax was used. */
10672 /* Look for outputs. */
10673 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10675 /* Consume the `:'. */
10676 cp_lexer_consume_token (parser->lexer);
10677 /* Parse the output-operands. */
10678 if (cp_lexer_next_token_is_not (parser->lexer,
10680 && cp_lexer_next_token_is_not (parser->lexer,
10682 && cp_lexer_next_token_is_not (parser->lexer,
10684 outputs = cp_parser_asm_operand_list (parser);
10686 /* If the next token is `::', there are no outputs, and the
10687 next token is the beginning of the inputs. */
10688 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10689 /* The inputs are coming next. */
10692 /* Look for inputs. */
10694 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10696 /* Consume the `:' or `::'. */
10697 cp_lexer_consume_token (parser->lexer);
10698 /* Parse the output-operands. */
10699 if (cp_lexer_next_token_is_not (parser->lexer,
10701 && cp_lexer_next_token_is_not (parser->lexer,
10703 inputs = cp_parser_asm_operand_list (parser);
10705 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10706 /* The clobbers are coming next. */
10709 /* Look for clobbers. */
10711 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10713 /* Consume the `:' or `::'. */
10714 cp_lexer_consume_token (parser->lexer);
10715 /* Parse the clobbers. */
10716 if (cp_lexer_next_token_is_not (parser->lexer,
10718 clobbers = cp_parser_asm_clobber_list (parser);
10721 /* Look for the closing `)'. */
10722 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10723 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10724 /*consume_paren=*/true);
10725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10727 /* Create the ASM_EXPR. */
10728 if (at_function_scope_p ())
10730 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10732 /* If the extended syntax was not used, mark the ASM_EXPR. */
10735 tree temp = asm_stmt;
10736 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10737 temp = TREE_OPERAND (temp, 0);
10739 ASM_INPUT_P (temp) = 1;
10743 assemble_asm (string);
10746 /* Declarators [gram.dcl.decl] */
10748 /* Parse an init-declarator.
10751 declarator initializer [opt]
10756 declarator asm-specification [opt] attributes [opt] initializer [opt]
10758 function-definition:
10759 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10761 decl-specifier-seq [opt] declarator function-try-block
10765 function-definition:
10766 __extension__ function-definition
10768 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10769 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10770 then this declarator appears in a class scope. The new DECL created
10771 by this declarator is returned.
10773 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10774 for a function-definition here as well. If the declarator is a
10775 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10776 be TRUE upon return. By that point, the function-definition will
10777 have been completely parsed.
10779 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10783 cp_parser_init_declarator (cp_parser* parser,
10784 cp_decl_specifier_seq *decl_specifiers,
10785 bool function_definition_allowed_p,
10787 int declares_class_or_enum,
10788 bool* function_definition_p)
10791 cp_declarator *declarator;
10792 tree prefix_attributes;
10794 tree asm_specification;
10796 tree decl = NULL_TREE;
10798 bool is_initialized;
10799 bool is_parenthesized_init;
10800 bool is_non_constant_init;
10801 int ctor_dtor_or_conv_p;
10803 tree pushed_scope = NULL;
10805 /* Gather the attributes that were provided with the
10806 decl-specifiers. */
10807 prefix_attributes = decl_specifiers->attributes;
10809 /* Assume that this is not the declarator for a function
10811 if (function_definition_p)
10812 *function_definition_p = false;
10814 /* Defer access checks while parsing the declarator; we cannot know
10815 what names are accessible until we know what is being
10817 resume_deferring_access_checks ();
10819 /* Parse the declarator. */
10821 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10822 &ctor_dtor_or_conv_p,
10823 /*parenthesized_p=*/NULL,
10824 /*member_p=*/false);
10825 /* Gather up the deferred checks. */
10826 stop_deferring_access_checks ();
10828 /* If the DECLARATOR was erroneous, there's no need to go
10830 if (declarator == cp_error_declarator)
10831 return error_mark_node;
10833 if (declares_class_or_enum & 2)
10834 cp_parser_check_for_definition_in_return_type (declarator,
10835 decl_specifiers->type);
10837 /* Figure out what scope the entity declared by the DECLARATOR is
10838 located in. `grokdeclarator' sometimes changes the scope, so
10839 we compute it now. */
10840 scope = get_scope_of_declarator (declarator);
10842 /* If we're allowing GNU extensions, look for an asm-specification
10844 if (cp_parser_allow_gnu_extensions_p (parser))
10846 /* Look for an asm-specification. */
10847 asm_specification = cp_parser_asm_specification_opt (parser);
10848 /* And attributes. */
10849 attributes = cp_parser_attributes_opt (parser);
10853 asm_specification = NULL_TREE;
10854 attributes = NULL_TREE;
10857 /* Peek at the next token. */
10858 token = cp_lexer_peek_token (parser->lexer);
10859 /* Check to see if the token indicates the start of a
10860 function-definition. */
10861 if (cp_parser_token_starts_function_definition_p (token))
10863 if (!function_definition_allowed_p)
10865 /* If a function-definition should not appear here, issue an
10867 cp_parser_error (parser,
10868 "a function-definition is not allowed here");
10869 return error_mark_node;
10873 /* Neither attributes nor an asm-specification are allowed
10874 on a function-definition. */
10875 if (asm_specification)
10876 error ("an asm-specification is not allowed on a function-definition");
10878 error ("attributes are not allowed on a function-definition");
10879 /* This is a function-definition. */
10880 *function_definition_p = true;
10882 /* Parse the function definition. */
10884 decl = cp_parser_save_member_function_body (parser,
10887 prefix_attributes);
10890 = (cp_parser_function_definition_from_specifiers_and_declarator
10891 (parser, decl_specifiers, prefix_attributes, declarator));
10899 Only in function declarations for constructors, destructors, and
10900 type conversions can the decl-specifier-seq be omitted.
10902 We explicitly postpone this check past the point where we handle
10903 function-definitions because we tolerate function-definitions
10904 that are missing their return types in some modes. */
10905 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10907 cp_parser_error (parser,
10908 "expected constructor, destructor, or type conversion");
10909 return error_mark_node;
10912 /* An `=' or an `(' indicates an initializer. */
10913 is_initialized = (token->type == CPP_EQ
10914 || token->type == CPP_OPEN_PAREN);
10915 /* If the init-declarator isn't initialized and isn't followed by a
10916 `,' or `;', it's not a valid init-declarator. */
10917 if (!is_initialized
10918 && token->type != CPP_COMMA
10919 && token->type != CPP_SEMICOLON)
10921 cp_parser_error (parser, "expected initializer");
10922 return error_mark_node;
10925 /* Because start_decl has side-effects, we should only call it if we
10926 know we're going ahead. By this point, we know that we cannot
10927 possibly be looking at any other construct. */
10928 cp_parser_commit_to_tentative_parse (parser);
10930 /* If the decl specifiers were bad, issue an error now that we're
10931 sure this was intended to be a declarator. Then continue
10932 declaring the variable(s), as int, to try to cut down on further
10934 if (decl_specifiers->any_specifiers_p
10935 && decl_specifiers->type == error_mark_node)
10937 cp_parser_error (parser, "invalid type in declaration");
10938 decl_specifiers->type = integer_type_node;
10941 /* Check to see whether or not this declaration is a friend. */
10942 friend_p = cp_parser_friend_p (decl_specifiers);
10944 /* Check that the number of template-parameter-lists is OK. */
10945 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10946 return error_mark_node;
10948 /* Enter the newly declared entry in the symbol table. If we're
10949 processing a declaration in a class-specifier, we wait until
10950 after processing the initializer. */
10953 if (parser->in_unbraced_linkage_specification_p)
10955 decl_specifiers->storage_class = sc_extern;
10956 have_extern_spec = false;
10958 decl = start_decl (declarator, decl_specifiers,
10959 is_initialized, attributes, prefix_attributes,
10963 /* Enter the SCOPE. That way unqualified names appearing in the
10964 initializer will be looked up in SCOPE. */
10965 pushed_scope = push_scope (scope);
10967 /* Perform deferred access control checks, now that we know in which
10968 SCOPE the declared entity resides. */
10969 if (!member_p && decl)
10971 tree saved_current_function_decl = NULL_TREE;
10973 /* If the entity being declared is a function, pretend that we
10974 are in its scope. If it is a `friend', it may have access to
10975 things that would not otherwise be accessible. */
10976 if (TREE_CODE (decl) == FUNCTION_DECL)
10978 saved_current_function_decl = current_function_decl;
10979 current_function_decl = decl;
10982 /* Perform the access control checks for the declarator and the
10983 the decl-specifiers. */
10984 perform_deferred_access_checks ();
10986 /* Restore the saved value. */
10987 if (TREE_CODE (decl) == FUNCTION_DECL)
10988 current_function_decl = saved_current_function_decl;
10991 /* Parse the initializer. */
10992 if (is_initialized)
10993 initializer = cp_parser_initializer (parser,
10994 &is_parenthesized_init,
10995 &is_non_constant_init);
10998 initializer = NULL_TREE;
10999 is_parenthesized_init = false;
11000 is_non_constant_init = true;
11003 /* The old parser allows attributes to appear after a parenthesized
11004 initializer. Mark Mitchell proposed removing this functionality
11005 on the GCC mailing lists on 2002-08-13. This parser accepts the
11006 attributes -- but ignores them. */
11007 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11008 if (cp_parser_attributes_opt (parser))
11009 warning (OPT_Wattributes,
11010 "attributes after parenthesized initializer ignored");
11012 /* For an in-class declaration, use `grokfield' to create the
11018 pop_scope (pushed_scope);
11019 pushed_scope = false;
11021 decl = grokfield (declarator, decl_specifiers,
11022 initializer, /*asmspec=*/NULL_TREE,
11023 prefix_attributes);
11024 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11025 cp_parser_save_default_args (parser, decl);
11028 /* Finish processing the declaration. But, skip friend
11030 if (!friend_p && decl && decl != error_mark_node)
11032 cp_finish_decl (decl,
11035 /* If the initializer is in parentheses, then this is
11036 a direct-initialization, which means that an
11037 `explicit' constructor is OK. Otherwise, an
11038 `explicit' constructor cannot be used. */
11039 ((is_parenthesized_init || !is_initialized)
11040 ? 0 : LOOKUP_ONLYCONVERTING));
11042 if (!friend_p && pushed_scope)
11043 pop_scope (pushed_scope);
11045 /* Remember whether or not variables were initialized by
11046 constant-expressions. */
11047 if (decl && TREE_CODE (decl) == VAR_DECL
11048 && is_initialized && !is_non_constant_init)
11049 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
11054 /* Parse a declarator.
11058 ptr-operator declarator
11060 abstract-declarator:
11061 ptr-operator abstract-declarator [opt]
11062 direct-abstract-declarator
11067 attributes [opt] direct-declarator
11068 attributes [opt] ptr-operator declarator
11070 abstract-declarator:
11071 attributes [opt] ptr-operator abstract-declarator [opt]
11072 attributes [opt] direct-abstract-declarator
11074 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11075 detect constructor, destructor or conversion operators. It is set
11076 to -1 if the declarator is a name, and +1 if it is a
11077 function. Otherwise it is set to zero. Usually you just want to
11078 test for >0, but internally the negative value is used.
11080 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11081 a decl-specifier-seq unless it declares a constructor, destructor,
11082 or conversion. It might seem that we could check this condition in
11083 semantic analysis, rather than parsing, but that makes it difficult
11084 to handle something like `f()'. We want to notice that there are
11085 no decl-specifiers, and therefore realize that this is an
11086 expression, not a declaration.)
11088 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11089 the declarator is a direct-declarator of the form "(...)".
11091 MEMBER_P is true iff this declarator is a member-declarator. */
11093 static cp_declarator *
11094 cp_parser_declarator (cp_parser* parser,
11095 cp_parser_declarator_kind dcl_kind,
11096 int* ctor_dtor_or_conv_p,
11097 bool* parenthesized_p,
11101 cp_declarator *declarator;
11102 enum tree_code code;
11103 cp_cv_quals cv_quals;
11105 tree attributes = NULL_TREE;
11107 /* Assume this is not a constructor, destructor, or type-conversion
11109 if (ctor_dtor_or_conv_p)
11110 *ctor_dtor_or_conv_p = 0;
11112 if (cp_parser_allow_gnu_extensions_p (parser))
11113 attributes = cp_parser_attributes_opt (parser);
11115 /* Peek at the next token. */
11116 token = cp_lexer_peek_token (parser->lexer);
11118 /* Check for the ptr-operator production. */
11119 cp_parser_parse_tentatively (parser);
11120 /* Parse the ptr-operator. */
11121 code = cp_parser_ptr_operator (parser,
11124 /* If that worked, then we have a ptr-operator. */
11125 if (cp_parser_parse_definitely (parser))
11127 /* If a ptr-operator was found, then this declarator was not
11129 if (parenthesized_p)
11130 *parenthesized_p = true;
11131 /* The dependent declarator is optional if we are parsing an
11132 abstract-declarator. */
11133 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11134 cp_parser_parse_tentatively (parser);
11136 /* Parse the dependent declarator. */
11137 declarator = cp_parser_declarator (parser, dcl_kind,
11138 /*ctor_dtor_or_conv_p=*/NULL,
11139 /*parenthesized_p=*/NULL,
11140 /*member_p=*/false);
11142 /* If we are parsing an abstract-declarator, we must handle the
11143 case where the dependent declarator is absent. */
11144 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11145 && !cp_parser_parse_definitely (parser))
11148 /* Build the representation of the ptr-operator. */
11150 declarator = make_ptrmem_declarator (cv_quals,
11153 else if (code == INDIRECT_REF)
11154 declarator = make_pointer_declarator (cv_quals, declarator);
11156 declarator = make_reference_declarator (cv_quals, declarator);
11158 /* Everything else is a direct-declarator. */
11161 if (parenthesized_p)
11162 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11164 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11165 ctor_dtor_or_conv_p,
11169 if (attributes && declarator != cp_error_declarator)
11170 declarator->attributes = attributes;
11175 /* Parse a direct-declarator or direct-abstract-declarator.
11179 direct-declarator ( parameter-declaration-clause )
11180 cv-qualifier-seq [opt]
11181 exception-specification [opt]
11182 direct-declarator [ constant-expression [opt] ]
11185 direct-abstract-declarator:
11186 direct-abstract-declarator [opt]
11187 ( parameter-declaration-clause )
11188 cv-qualifier-seq [opt]
11189 exception-specification [opt]
11190 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11191 ( abstract-declarator )
11193 Returns a representation of the declarator. DCL_KIND is
11194 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11195 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11196 we are parsing a direct-declarator. It is
11197 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11198 of ambiguity we prefer an abstract declarator, as per
11199 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11200 cp_parser_declarator. */
11202 static cp_declarator *
11203 cp_parser_direct_declarator (cp_parser* parser,
11204 cp_parser_declarator_kind dcl_kind,
11205 int* ctor_dtor_or_conv_p,
11209 cp_declarator *declarator = NULL;
11210 tree scope = NULL_TREE;
11211 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11212 bool saved_in_declarator_p = parser->in_declarator_p;
11214 tree pushed_scope = NULL_TREE;
11218 /* Peek at the next token. */
11219 token = cp_lexer_peek_token (parser->lexer);
11220 if (token->type == CPP_OPEN_PAREN)
11222 /* This is either a parameter-declaration-clause, or a
11223 parenthesized declarator. When we know we are parsing a
11224 named declarator, it must be a parenthesized declarator
11225 if FIRST is true. For instance, `(int)' is a
11226 parameter-declaration-clause, with an omitted
11227 direct-abstract-declarator. But `((*))', is a
11228 parenthesized abstract declarator. Finally, when T is a
11229 template parameter `(T)' is a
11230 parameter-declaration-clause, and not a parenthesized
11233 We first try and parse a parameter-declaration-clause,
11234 and then try a nested declarator (if FIRST is true).
11236 It is not an error for it not to be a
11237 parameter-declaration-clause, even when FIRST is
11243 The first is the declaration of a function while the
11244 second is a the definition of a variable, including its
11247 Having seen only the parenthesis, we cannot know which of
11248 these two alternatives should be selected. Even more
11249 complex are examples like:
11254 The former is a function-declaration; the latter is a
11255 variable initialization.
11257 Thus again, we try a parameter-declaration-clause, and if
11258 that fails, we back out and return. */
11260 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11262 cp_parameter_declarator *params;
11263 unsigned saved_num_template_parameter_lists;
11265 /* In a member-declarator, the only valid interpretation
11266 of a parenthesis is the start of a
11267 parameter-declaration-clause. (It is invalid to
11268 initialize a static data member with a parenthesized
11269 initializer; only the "=" form of initialization is
11272 cp_parser_parse_tentatively (parser);
11274 /* Consume the `('. */
11275 cp_lexer_consume_token (parser->lexer);
11278 /* If this is going to be an abstract declarator, we're
11279 in a declarator and we can't have default args. */
11280 parser->default_arg_ok_p = false;
11281 parser->in_declarator_p = true;
11284 /* Inside the function parameter list, surrounding
11285 template-parameter-lists do not apply. */
11286 saved_num_template_parameter_lists
11287 = parser->num_template_parameter_lists;
11288 parser->num_template_parameter_lists = 0;
11290 /* Parse the parameter-declaration-clause. */
11291 params = cp_parser_parameter_declaration_clause (parser);
11293 parser->num_template_parameter_lists
11294 = saved_num_template_parameter_lists;
11296 /* If all went well, parse the cv-qualifier-seq and the
11297 exception-specification. */
11298 if (member_p || cp_parser_parse_definitely (parser))
11300 cp_cv_quals cv_quals;
11301 tree exception_specification;
11303 if (ctor_dtor_or_conv_p)
11304 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11306 /* Consume the `)'. */
11307 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11309 /* Parse the cv-qualifier-seq. */
11310 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11311 /* And the exception-specification. */
11312 exception_specification
11313 = cp_parser_exception_specification_opt (parser);
11315 /* Create the function-declarator. */
11316 declarator = make_call_declarator (declarator,
11319 exception_specification);
11320 /* Any subsequent parameter lists are to do with
11321 return type, so are not those of the declared
11323 parser->default_arg_ok_p = false;
11325 /* Repeat the main loop. */
11330 /* If this is the first, we can try a parenthesized
11334 bool saved_in_type_id_in_expr_p;
11336 parser->default_arg_ok_p = saved_default_arg_ok_p;
11337 parser->in_declarator_p = saved_in_declarator_p;
11339 /* Consume the `('. */
11340 cp_lexer_consume_token (parser->lexer);
11341 /* Parse the nested declarator. */
11342 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11343 parser->in_type_id_in_expr_p = true;
11345 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11346 /*parenthesized_p=*/NULL,
11348 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11350 /* Expect a `)'. */
11351 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11352 declarator = cp_error_declarator;
11353 if (declarator == cp_error_declarator)
11356 goto handle_declarator;
11358 /* Otherwise, we must be done. */
11362 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11363 && token->type == CPP_OPEN_SQUARE)
11365 /* Parse an array-declarator. */
11368 if (ctor_dtor_or_conv_p)
11369 *ctor_dtor_or_conv_p = 0;
11372 parser->default_arg_ok_p = false;
11373 parser->in_declarator_p = true;
11374 /* Consume the `['. */
11375 cp_lexer_consume_token (parser->lexer);
11376 /* Peek at the next token. */
11377 token = cp_lexer_peek_token (parser->lexer);
11378 /* If the next token is `]', then there is no
11379 constant-expression. */
11380 if (token->type != CPP_CLOSE_SQUARE)
11382 bool non_constant_p;
11385 = cp_parser_constant_expression (parser,
11386 /*allow_non_constant=*/true,
11388 if (!non_constant_p)
11389 bounds = fold_non_dependent_expr (bounds);
11390 /* Normally, the array bound must be an integral constant
11391 expression. However, as an extension, we allow VLAs
11392 in function scopes. */
11393 else if (!at_function_scope_p ())
11395 error ("array bound is not an integer constant");
11396 bounds = error_mark_node;
11400 bounds = NULL_TREE;
11401 /* Look for the closing `]'. */
11402 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11404 declarator = cp_error_declarator;
11408 declarator = make_array_declarator (declarator, bounds);
11410 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11412 tree qualifying_scope;
11413 tree unqualified_name;
11414 special_function_kind sfk;
11416 /* Parse a declarator-id */
11417 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11418 cp_parser_parse_tentatively (parser);
11419 unqualified_name = cp_parser_declarator_id (parser);
11420 qualifying_scope = parser->scope;
11421 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11423 if (!cp_parser_parse_definitely (parser))
11424 unqualified_name = error_mark_node;
11425 else if (qualifying_scope
11426 || (TREE_CODE (unqualified_name)
11427 != IDENTIFIER_NODE))
11429 cp_parser_error (parser, "expected unqualified-id");
11430 unqualified_name = error_mark_node;
11434 if (unqualified_name == error_mark_node)
11436 declarator = cp_error_declarator;
11440 if (qualifying_scope && at_namespace_scope_p ()
11441 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11443 /* In the declaration of a member of a template class
11444 outside of the class itself, the SCOPE will sometimes
11445 be a TYPENAME_TYPE. For example, given:
11447 template <typename T>
11448 int S<T>::R::i = 3;
11450 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11451 this context, we must resolve S<T>::R to an ordinary
11452 type, rather than a typename type.
11454 The reason we normally avoid resolving TYPENAME_TYPEs
11455 is that a specialization of `S' might render
11456 `S<T>::R' not a type. However, if `S' is
11457 specialized, then this `i' will not be used, so there
11458 is no harm in resolving the types here. */
11461 /* Resolve the TYPENAME_TYPE. */
11462 type = resolve_typename_type (qualifying_scope,
11463 /*only_current_p=*/false);
11464 /* If that failed, the declarator is invalid. */
11465 if (type == error_mark_node)
11466 error ("%<%T::%D%> is not a type",
11467 TYPE_CONTEXT (qualifying_scope),
11468 TYPE_IDENTIFIER (qualifying_scope));
11469 qualifying_scope = type;
11473 if (unqualified_name)
11477 if (qualifying_scope
11478 && CLASS_TYPE_P (qualifying_scope))
11479 class_type = qualifying_scope;
11481 class_type = current_class_type;
11483 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11485 if (qualifying_scope
11486 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11488 error ("invalid use of constructor as a template");
11489 inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11490 "the constructor in a qualified name",
11492 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11493 class_type, class_type);
11494 declarator = cp_error_declarator;
11497 else if (class_type
11498 && same_type_p (TREE_TYPE (unqualified_name),
11500 unqualified_name = constructor_name (class_type);
11503 /* We do not attempt to print the declarator
11504 here because we do not have enough
11505 information about its original syntactic
11507 error ("invalid declarator");
11508 declarator = cp_error_declarator;
11515 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11516 sfk = sfk_destructor;
11517 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11518 sfk = sfk_conversion;
11519 else if (/* There's no way to declare a constructor
11520 for an anonymous type, even if the type
11521 got a name for linkage purposes. */
11522 !TYPE_WAS_ANONYMOUS (class_type)
11523 && constructor_name_p (unqualified_name,
11526 unqualified_name = constructor_name (class_type);
11527 sfk = sfk_constructor;
11530 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11531 *ctor_dtor_or_conv_p = -1;
11534 declarator = make_id_declarator (qualifying_scope,
11537 declarator->id_loc = token->location;
11539 handle_declarator:;
11540 scope = get_scope_of_declarator (declarator);
11542 /* Any names that appear after the declarator-id for a
11543 member are looked up in the containing scope. */
11544 pushed_scope = push_scope (scope);
11545 parser->in_declarator_p = true;
11546 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11547 || (declarator && declarator->kind == cdk_id))
11548 /* Default args are only allowed on function
11550 parser->default_arg_ok_p = saved_default_arg_ok_p;
11552 parser->default_arg_ok_p = false;
11561 /* For an abstract declarator, we might wind up with nothing at this
11562 point. That's an error; the declarator is not optional. */
11564 cp_parser_error (parser, "expected declarator");
11566 /* If we entered a scope, we must exit it now. */
11568 pop_scope (pushed_scope);
11570 parser->default_arg_ok_p = saved_default_arg_ok_p;
11571 parser->in_declarator_p = saved_in_declarator_p;
11576 /* Parse a ptr-operator.
11579 * cv-qualifier-seq [opt]
11581 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11586 & cv-qualifier-seq [opt]
11588 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11589 Returns ADDR_EXPR if a reference was used. In the case of a
11590 pointer-to-member, *TYPE is filled in with the TYPE containing the
11591 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11592 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11593 ERROR_MARK if an error occurred. */
11595 static enum tree_code
11596 cp_parser_ptr_operator (cp_parser* parser,
11598 cp_cv_quals *cv_quals)
11600 enum tree_code code = ERROR_MARK;
11603 /* Assume that it's not a pointer-to-member. */
11605 /* And that there are no cv-qualifiers. */
11606 *cv_quals = TYPE_UNQUALIFIED;
11608 /* Peek at the next token. */
11609 token = cp_lexer_peek_token (parser->lexer);
11610 /* If it's a `*' or `&' we have a pointer or reference. */
11611 if (token->type == CPP_MULT || token->type == CPP_AND)
11613 /* Remember which ptr-operator we were processing. */
11614 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11616 /* Consume the `*' or `&'. */
11617 cp_lexer_consume_token (parser->lexer);
11619 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11620 `&', if we are allowing GNU extensions. (The only qualifier
11621 that can legally appear after `&' is `restrict', but that is
11622 enforced during semantic analysis. */
11623 if (code == INDIRECT_REF
11624 || cp_parser_allow_gnu_extensions_p (parser))
11625 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11629 /* Try the pointer-to-member case. */
11630 cp_parser_parse_tentatively (parser);
11631 /* Look for the optional `::' operator. */
11632 cp_parser_global_scope_opt (parser,
11633 /*current_scope_valid_p=*/false);
11634 /* Look for the nested-name specifier. */
11635 cp_parser_nested_name_specifier (parser,
11636 /*typename_keyword_p=*/false,
11637 /*check_dependency_p=*/true,
11639 /*is_declaration=*/false);
11640 /* If we found it, and the next token is a `*', then we are
11641 indeed looking at a pointer-to-member operator. */
11642 if (!cp_parser_error_occurred (parser)
11643 && cp_parser_require (parser, CPP_MULT, "`*'"))
11645 /* The type of which the member is a member is given by the
11647 *type = parser->scope;
11648 /* The next name will not be qualified. */
11649 parser->scope = NULL_TREE;
11650 parser->qualifying_scope = NULL_TREE;
11651 parser->object_scope = NULL_TREE;
11652 /* Indicate that the `*' operator was used. */
11653 code = INDIRECT_REF;
11654 /* Look for the optional cv-qualifier-seq. */
11655 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11657 /* If that didn't work we don't have a ptr-operator. */
11658 if (!cp_parser_parse_definitely (parser))
11659 cp_parser_error (parser, "expected ptr-operator");
11665 /* Parse an (optional) cv-qualifier-seq.
11668 cv-qualifier cv-qualifier-seq [opt]
11679 Returns a bitmask representing the cv-qualifiers. */
11682 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11684 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11689 cp_cv_quals cv_qualifier;
11691 /* Peek at the next token. */
11692 token = cp_lexer_peek_token (parser->lexer);
11693 /* See if it's a cv-qualifier. */
11694 switch (token->keyword)
11697 cv_qualifier = TYPE_QUAL_CONST;
11701 cv_qualifier = TYPE_QUAL_VOLATILE;
11705 cv_qualifier = TYPE_QUAL_RESTRICT;
11709 cv_qualifier = TYPE_UNQUALIFIED;
11716 if (cv_quals & cv_qualifier)
11718 error ("duplicate cv-qualifier");
11719 cp_lexer_purge_token (parser->lexer);
11723 cp_lexer_consume_token (parser->lexer);
11724 cv_quals |= cv_qualifier;
11731 /* Parse a declarator-id.
11735 :: [opt] nested-name-specifier [opt] type-name
11737 In the `id-expression' case, the value returned is as for
11738 cp_parser_id_expression if the id-expression was an unqualified-id.
11739 If the id-expression was a qualified-id, then a SCOPE_REF is
11740 returned. The first operand is the scope (either a NAMESPACE_DECL
11741 or TREE_TYPE), but the second is still just a representation of an
11745 cp_parser_declarator_id (cp_parser* parser)
11748 /* The expression must be an id-expression. Assume that qualified
11749 names are the names of types so that:
11752 int S<T>::R::i = 3;
11754 will work; we must treat `S<T>::R' as the name of a type.
11755 Similarly, assume that qualified names are templates, where
11759 int S<T>::R<T>::i = 3;
11762 id = cp_parser_id_expression (parser,
11763 /*template_keyword_p=*/false,
11764 /*check_dependency_p=*/false,
11765 /*template_p=*/NULL,
11766 /*declarator_p=*/true);
11767 if (BASELINK_P (id))
11768 id = BASELINK_FUNCTIONS (id);
11772 /* Parse a type-id.
11775 type-specifier-seq abstract-declarator [opt]
11777 Returns the TYPE specified. */
11780 cp_parser_type_id (cp_parser* parser)
11782 cp_decl_specifier_seq type_specifier_seq;
11783 cp_declarator *abstract_declarator;
11785 /* Parse the type-specifier-seq. */
11786 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11787 &type_specifier_seq);
11788 if (type_specifier_seq.type == error_mark_node)
11789 return error_mark_node;
11791 /* There might or might not be an abstract declarator. */
11792 cp_parser_parse_tentatively (parser);
11793 /* Look for the declarator. */
11794 abstract_declarator
11795 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11796 /*parenthesized_p=*/NULL,
11797 /*member_p=*/false);
11798 /* Check to see if there really was a declarator. */
11799 if (!cp_parser_parse_definitely (parser))
11800 abstract_declarator = NULL;
11802 return groktypename (&type_specifier_seq, abstract_declarator);
11805 /* Parse a type-specifier-seq.
11807 type-specifier-seq:
11808 type-specifier type-specifier-seq [opt]
11812 type-specifier-seq:
11813 attributes type-specifier-seq [opt]
11815 If IS_CONDITION is true, we are at the start of a "condition",
11816 e.g., we've just seen "if (".
11818 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11821 cp_parser_type_specifier_seq (cp_parser* parser,
11823 cp_decl_specifier_seq *type_specifier_seq)
11825 bool seen_type_specifier = false;
11826 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11828 /* Clear the TYPE_SPECIFIER_SEQ. */
11829 clear_decl_specs (type_specifier_seq);
11831 /* Parse the type-specifiers and attributes. */
11834 tree type_specifier;
11835 bool is_cv_qualifier;
11837 /* Check for attributes first. */
11838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11840 type_specifier_seq->attributes =
11841 chainon (type_specifier_seq->attributes,
11842 cp_parser_attributes_opt (parser));
11846 /* Look for the type-specifier. */
11847 type_specifier = cp_parser_type_specifier (parser,
11849 type_specifier_seq,
11850 /*is_declaration=*/false,
11853 if (!type_specifier)
11855 /* If the first type-specifier could not be found, this is not a
11856 type-specifier-seq at all. */
11857 if (!seen_type_specifier)
11859 cp_parser_error (parser, "expected type-specifier");
11860 type_specifier_seq->type = error_mark_node;
11863 /* If subsequent type-specifiers could not be found, the
11864 type-specifier-seq is complete. */
11868 seen_type_specifier = true;
11869 /* The standard says that a condition can be:
11871 type-specifier-seq declarator = assignment-expression
11878 we should treat the "S" as a declarator, not as a
11879 type-specifier. The standard doesn't say that explicitly for
11880 type-specifier-seq, but it does say that for
11881 decl-specifier-seq in an ordinary declaration. Perhaps it
11882 would be clearer just to allow a decl-specifier-seq here, and
11883 then add a semantic restriction that if any decl-specifiers
11884 that are not type-specifiers appear, the program is invalid. */
11885 if (is_condition && !is_cv_qualifier)
11886 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11892 /* Parse a parameter-declaration-clause.
11894 parameter-declaration-clause:
11895 parameter-declaration-list [opt] ... [opt]
11896 parameter-declaration-list , ...
11898 Returns a representation for the parameter declarations. A return
11899 value of NULL indicates a parameter-declaration-clause consisting
11900 only of an ellipsis. */
11902 static cp_parameter_declarator *
11903 cp_parser_parameter_declaration_clause (cp_parser* parser)
11905 cp_parameter_declarator *parameters;
11910 /* Peek at the next token. */
11911 token = cp_lexer_peek_token (parser->lexer);
11912 /* Check for trivial parameter-declaration-clauses. */
11913 if (token->type == CPP_ELLIPSIS)
11915 /* Consume the `...' token. */
11916 cp_lexer_consume_token (parser->lexer);
11919 else if (token->type == CPP_CLOSE_PAREN)
11920 /* There are no parameters. */
11922 #ifndef NO_IMPLICIT_EXTERN_C
11923 if (in_system_header && current_class_type == NULL
11924 && current_lang_name == lang_name_c)
11928 return no_parameters;
11930 /* Check for `(void)', too, which is a special case. */
11931 else if (token->keyword == RID_VOID
11932 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11933 == CPP_CLOSE_PAREN))
11935 /* Consume the `void' token. */
11936 cp_lexer_consume_token (parser->lexer);
11937 /* There are no parameters. */
11938 return no_parameters;
11941 /* Parse the parameter-declaration-list. */
11942 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11943 /* If a parse error occurred while parsing the
11944 parameter-declaration-list, then the entire
11945 parameter-declaration-clause is erroneous. */
11949 /* Peek at the next token. */
11950 token = cp_lexer_peek_token (parser->lexer);
11951 /* If it's a `,', the clause should terminate with an ellipsis. */
11952 if (token->type == CPP_COMMA)
11954 /* Consume the `,'. */
11955 cp_lexer_consume_token (parser->lexer);
11956 /* Expect an ellipsis. */
11958 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11960 /* It might also be `...' if the optional trailing `,' was
11962 else if (token->type == CPP_ELLIPSIS)
11964 /* Consume the `...' token. */
11965 cp_lexer_consume_token (parser->lexer);
11966 /* And remember that we saw it. */
11970 ellipsis_p = false;
11972 /* Finish the parameter list. */
11973 if (parameters && ellipsis_p)
11974 parameters->ellipsis_p = true;
11979 /* Parse a parameter-declaration-list.
11981 parameter-declaration-list:
11982 parameter-declaration
11983 parameter-declaration-list , parameter-declaration
11985 Returns a representation of the parameter-declaration-list, as for
11986 cp_parser_parameter_declaration_clause. However, the
11987 `void_list_node' is never appended to the list. Upon return,
11988 *IS_ERROR will be true iff an error occurred. */
11990 static cp_parameter_declarator *
11991 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11993 cp_parameter_declarator *parameters = NULL;
11994 cp_parameter_declarator **tail = ¶meters;
11996 /* Assume all will go well. */
11999 /* Look for more parameters. */
12002 cp_parameter_declarator *parameter;
12003 bool parenthesized_p;
12004 /* Parse the parameter. */
12006 = cp_parser_parameter_declaration (parser,
12007 /*template_parm_p=*/false,
12010 /* If a parse error occurred parsing the parameter declaration,
12011 then the entire parameter-declaration-list is erroneous. */
12018 /* Add the new parameter to the list. */
12020 tail = ¶meter->next;
12022 /* Peek at the next token. */
12023 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12024 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12025 /* These are for Objective-C++ */
12026 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12027 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12028 /* The parameter-declaration-list is complete. */
12030 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12034 /* Peek at the next token. */
12035 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12036 /* If it's an ellipsis, then the list is complete. */
12037 if (token->type == CPP_ELLIPSIS)
12039 /* Otherwise, there must be more parameters. Consume the
12041 cp_lexer_consume_token (parser->lexer);
12042 /* When parsing something like:
12044 int i(float f, double d)
12046 we can tell after seeing the declaration for "f" that we
12047 are not looking at an initialization of a variable "i",
12048 but rather at the declaration of a function "i".
12050 Due to the fact that the parsing of template arguments
12051 (as specified to a template-id) requires backtracking we
12052 cannot use this technique when inside a template argument
12054 if (!parser->in_template_argument_list_p
12055 && !parser->in_type_id_in_expr_p
12056 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12057 /* However, a parameter-declaration of the form
12058 "foat(f)" (which is a valid declaration of a
12059 parameter "f") can also be interpreted as an
12060 expression (the conversion of "f" to "float"). */
12061 && !parenthesized_p)
12062 cp_parser_commit_to_tentative_parse (parser);
12066 cp_parser_error (parser, "expected %<,%> or %<...%>");
12067 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12068 cp_parser_skip_to_closing_parenthesis (parser,
12069 /*recovering=*/true,
12070 /*or_comma=*/false,
12071 /*consume_paren=*/false);
12079 /* Parse a parameter declaration.
12081 parameter-declaration:
12082 decl-specifier-seq declarator
12083 decl-specifier-seq declarator = assignment-expression
12084 decl-specifier-seq abstract-declarator [opt]
12085 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12087 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12088 declares a template parameter. (In that case, a non-nested `>'
12089 token encountered during the parsing of the assignment-expression
12090 is not interpreted as a greater-than operator.)
12092 Returns a representation of the parameter, or NULL if an error
12093 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12094 true iff the declarator is of the form "(p)". */
12096 static cp_parameter_declarator *
12097 cp_parser_parameter_declaration (cp_parser *parser,
12098 bool template_parm_p,
12099 bool *parenthesized_p)
12101 int declares_class_or_enum;
12102 bool greater_than_is_operator_p;
12103 cp_decl_specifier_seq decl_specifiers;
12104 cp_declarator *declarator;
12105 tree default_argument;
12107 const char *saved_message;
12109 /* In a template parameter, `>' is not an operator.
12113 When parsing a default template-argument for a non-type
12114 template-parameter, the first non-nested `>' is taken as the end
12115 of the template parameter-list rather than a greater-than
12117 greater_than_is_operator_p = !template_parm_p;
12119 /* Type definitions may not appear in parameter types. */
12120 saved_message = parser->type_definition_forbidden_message;
12121 parser->type_definition_forbidden_message
12122 = "types may not be defined in parameter types";
12124 /* Parse the declaration-specifiers. */
12125 cp_parser_decl_specifier_seq (parser,
12126 CP_PARSER_FLAGS_NONE,
12128 &declares_class_or_enum);
12129 /* If an error occurred, there's no reason to attempt to parse the
12130 rest of the declaration. */
12131 if (cp_parser_error_occurred (parser))
12133 parser->type_definition_forbidden_message = saved_message;
12137 /* Peek at the next token. */
12138 token = cp_lexer_peek_token (parser->lexer);
12139 /* If the next token is a `)', `,', `=', `>', or `...', then there
12140 is no declarator. */
12141 if (token->type == CPP_CLOSE_PAREN
12142 || token->type == CPP_COMMA
12143 || token->type == CPP_EQ
12144 || token->type == CPP_ELLIPSIS
12145 || token->type == CPP_GREATER)
12148 if (parenthesized_p)
12149 *parenthesized_p = false;
12151 /* Otherwise, there should be a declarator. */
12154 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12155 parser->default_arg_ok_p = false;
12157 /* After seeing a decl-specifier-seq, if the next token is not a
12158 "(", there is no possibility that the code is a valid
12159 expression. Therefore, if parsing tentatively, we commit at
12161 if (!parser->in_template_argument_list_p
12162 /* In an expression context, having seen:
12166 we cannot be sure whether we are looking at a
12167 function-type (taking a "char" as a parameter) or a cast
12168 of some object of type "char" to "int". */
12169 && !parser->in_type_id_in_expr_p
12170 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12171 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12172 cp_parser_commit_to_tentative_parse (parser);
12173 /* Parse the declarator. */
12174 declarator = cp_parser_declarator (parser,
12175 CP_PARSER_DECLARATOR_EITHER,
12176 /*ctor_dtor_or_conv_p=*/NULL,
12178 /*member_p=*/false);
12179 parser->default_arg_ok_p = saved_default_arg_ok_p;
12180 /* After the declarator, allow more attributes. */
12181 decl_specifiers.attributes
12182 = chainon (decl_specifiers.attributes,
12183 cp_parser_attributes_opt (parser));
12186 /* The restriction on defining new types applies only to the type
12187 of the parameter, not to the default argument. */
12188 parser->type_definition_forbidden_message = saved_message;
12190 /* If the next token is `=', then process a default argument. */
12191 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12193 bool saved_greater_than_is_operator_p;
12194 /* Consume the `='. */
12195 cp_lexer_consume_token (parser->lexer);
12197 /* If we are defining a class, then the tokens that make up the
12198 default argument must be saved and processed later. */
12199 if (!template_parm_p && at_class_scope_p ()
12200 && TYPE_BEING_DEFINED (current_class_type))
12202 unsigned depth = 0;
12203 cp_token *first_token;
12206 /* Add tokens until we have processed the entire default
12207 argument. We add the range [first_token, token). */
12208 first_token = cp_lexer_peek_token (parser->lexer);
12213 /* Peek at the next token. */
12214 token = cp_lexer_peek_token (parser->lexer);
12215 /* What we do depends on what token we have. */
12216 switch (token->type)
12218 /* In valid code, a default argument must be
12219 immediately followed by a `,' `)', or `...'. */
12221 case CPP_CLOSE_PAREN:
12223 /* If we run into a non-nested `;', `}', or `]',
12224 then the code is invalid -- but the default
12225 argument is certainly over. */
12226 case CPP_SEMICOLON:
12227 case CPP_CLOSE_BRACE:
12228 case CPP_CLOSE_SQUARE:
12231 /* Update DEPTH, if necessary. */
12232 else if (token->type == CPP_CLOSE_PAREN
12233 || token->type == CPP_CLOSE_BRACE
12234 || token->type == CPP_CLOSE_SQUARE)
12238 case CPP_OPEN_PAREN:
12239 case CPP_OPEN_SQUARE:
12240 case CPP_OPEN_BRACE:
12245 /* If we see a non-nested `>', and `>' is not an
12246 operator, then it marks the end of the default
12248 if (!depth && !greater_than_is_operator_p)
12252 /* If we run out of tokens, issue an error message. */
12254 case CPP_PRAGMA_EOL:
12255 error ("file ends in default argument");
12261 /* In these cases, we should look for template-ids.
12262 For example, if the default argument is
12263 `X<int, double>()', we need to do name lookup to
12264 figure out whether or not `X' is a template; if
12265 so, the `,' does not end the default argument.
12267 That is not yet done. */
12274 /* If we've reached the end, stop. */
12278 /* Add the token to the token block. */
12279 token = cp_lexer_consume_token (parser->lexer);
12282 /* Create a DEFAULT_ARG to represented the unparsed default
12284 default_argument = make_node (DEFAULT_ARG);
12285 DEFARG_TOKENS (default_argument)
12286 = cp_token_cache_new (first_token, token);
12287 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12289 /* Outside of a class definition, we can just parse the
12290 assignment-expression. */
12293 bool saved_local_variables_forbidden_p;
12295 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12297 saved_greater_than_is_operator_p
12298 = parser->greater_than_is_operator_p;
12299 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12300 /* Local variable names (and the `this' keyword) may not
12301 appear in a default argument. */
12302 saved_local_variables_forbidden_p
12303 = parser->local_variables_forbidden_p;
12304 parser->local_variables_forbidden_p = true;
12305 /* Parse the assignment-expression. */
12307 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12308 /* Restore saved state. */
12309 parser->greater_than_is_operator_p
12310 = saved_greater_than_is_operator_p;
12311 parser->local_variables_forbidden_p
12312 = saved_local_variables_forbidden_p;
12314 if (!parser->default_arg_ok_p)
12316 if (!flag_pedantic_errors)
12317 warning (0, "deprecated use of default argument for parameter of non-function");
12320 error ("default arguments are only permitted for function parameters");
12321 default_argument = NULL_TREE;
12326 default_argument = NULL_TREE;
12328 return make_parameter_declarator (&decl_specifiers,
12333 /* Parse a function-body.
12336 compound_statement */
12339 cp_parser_function_body (cp_parser *parser)
12341 cp_parser_compound_statement (parser, NULL, false);
12344 /* Parse a ctor-initializer-opt followed by a function-body. Return
12345 true if a ctor-initializer was present. */
12348 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12351 bool ctor_initializer_p;
12353 /* Begin the function body. */
12354 body = begin_function_body ();
12355 /* Parse the optional ctor-initializer. */
12356 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12357 /* Parse the function-body. */
12358 cp_parser_function_body (parser);
12359 /* Finish the function body. */
12360 finish_function_body (body);
12362 return ctor_initializer_p;
12365 /* Parse an initializer.
12368 = initializer-clause
12369 ( expression-list )
12371 Returns an expression representing the initializer. If no
12372 initializer is present, NULL_TREE is returned.
12374 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12375 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12376 set to FALSE if there is no initializer present. If there is an
12377 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12378 is set to true; otherwise it is set to false. */
12381 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12382 bool* non_constant_p)
12387 /* Peek at the next token. */
12388 token = cp_lexer_peek_token (parser->lexer);
12390 /* Let our caller know whether or not this initializer was
12392 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12393 /* Assume that the initializer is constant. */
12394 *non_constant_p = false;
12396 if (token->type == CPP_EQ)
12398 /* Consume the `='. */
12399 cp_lexer_consume_token (parser->lexer);
12400 /* Parse the initializer-clause. */
12401 init = cp_parser_initializer_clause (parser, non_constant_p);
12403 else if (token->type == CPP_OPEN_PAREN)
12404 init = cp_parser_parenthesized_expression_list (parser, false,
12409 /* Anything else is an error. */
12410 cp_parser_error (parser, "expected initializer");
12411 init = error_mark_node;
12417 /* Parse an initializer-clause.
12419 initializer-clause:
12420 assignment-expression
12421 { initializer-list , [opt] }
12424 Returns an expression representing the initializer.
12426 If the `assignment-expression' production is used the value
12427 returned is simply a representation for the expression.
12429 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12430 the elements of the initializer-list (or NULL, if the last
12431 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12432 NULL_TREE. There is no way to detect whether or not the optional
12433 trailing `,' was provided. NON_CONSTANT_P is as for
12434 cp_parser_initializer. */
12437 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12441 /* Assume the expression is constant. */
12442 *non_constant_p = false;
12444 /* If it is not a `{', then we are looking at an
12445 assignment-expression. */
12446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12449 = cp_parser_constant_expression (parser,
12450 /*allow_non_constant_p=*/true,
12452 if (!*non_constant_p)
12453 initializer = fold_non_dependent_expr (initializer);
12457 /* Consume the `{' token. */
12458 cp_lexer_consume_token (parser->lexer);
12459 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12460 initializer = make_node (CONSTRUCTOR);
12461 /* If it's not a `}', then there is a non-trivial initializer. */
12462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12464 /* Parse the initializer list. */
12465 CONSTRUCTOR_ELTS (initializer)
12466 = cp_parser_initializer_list (parser, non_constant_p);
12467 /* A trailing `,' token is allowed. */
12468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12469 cp_lexer_consume_token (parser->lexer);
12471 /* Now, there should be a trailing `}'. */
12472 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12475 return initializer;
12478 /* Parse an initializer-list.
12482 initializer-list , initializer-clause
12487 identifier : initializer-clause
12488 initializer-list, identifier : initializer-clause
12490 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12491 for the initializer. If the INDEX of the elt is non-NULL, it is the
12492 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12493 as for cp_parser_initializer. */
12495 static VEC(constructor_elt,gc) *
12496 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12498 VEC(constructor_elt,gc) *v = NULL;
12500 /* Assume all of the expressions are constant. */
12501 *non_constant_p = false;
12503 /* Parse the rest of the list. */
12509 bool clause_non_constant_p;
12511 /* If the next token is an identifier and the following one is a
12512 colon, we are looking at the GNU designated-initializer
12514 if (cp_parser_allow_gnu_extensions_p (parser)
12515 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12516 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12518 /* Consume the identifier. */
12519 identifier = cp_lexer_consume_token (parser->lexer)->value;
12520 /* Consume the `:'. */
12521 cp_lexer_consume_token (parser->lexer);
12524 identifier = NULL_TREE;
12526 /* Parse the initializer. */
12527 initializer = cp_parser_initializer_clause (parser,
12528 &clause_non_constant_p);
12529 /* If any clause is non-constant, so is the entire initializer. */
12530 if (clause_non_constant_p)
12531 *non_constant_p = true;
12533 /* Add it to the vector. */
12534 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12536 /* If the next token is not a comma, we have reached the end of
12538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12541 /* Peek at the next token. */
12542 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12543 /* If the next token is a `}', then we're still done. An
12544 initializer-clause can have a trailing `,' after the
12545 initializer-list and before the closing `}'. */
12546 if (token->type == CPP_CLOSE_BRACE)
12549 /* Consume the `,' token. */
12550 cp_lexer_consume_token (parser->lexer);
12556 /* Classes [gram.class] */
12558 /* Parse a class-name.
12564 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12565 to indicate that names looked up in dependent types should be
12566 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12567 keyword has been used to indicate that the name that appears next
12568 is a template. TAG_TYPE indicates the explicit tag given before
12569 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12570 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12571 is the class being defined in a class-head.
12573 Returns the TYPE_DECL representing the class. */
12576 cp_parser_class_name (cp_parser *parser,
12577 bool typename_keyword_p,
12578 bool template_keyword_p,
12579 enum tag_types tag_type,
12580 bool check_dependency_p,
12582 bool is_declaration)
12589 /* All class-names start with an identifier. */
12590 token = cp_lexer_peek_token (parser->lexer);
12591 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12593 cp_parser_error (parser, "expected class-name");
12594 return error_mark_node;
12597 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12598 to a template-id, so we save it here. */
12599 scope = parser->scope;
12600 if (scope == error_mark_node)
12601 return error_mark_node;
12603 /* Any name names a type if we're following the `typename' keyword
12604 in a qualified name where the enclosing scope is type-dependent. */
12605 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12606 && dependent_type_p (scope));
12607 /* Handle the common case (an identifier, but not a template-id)
12609 if (token->type == CPP_NAME
12610 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12612 cp_token *identifier_token;
12616 /* Look for the identifier. */
12617 identifier_token = cp_lexer_peek_token (parser->lexer);
12618 ambiguous_p = identifier_token->ambiguous_p;
12619 identifier = cp_parser_identifier (parser);
12620 /* If the next token isn't an identifier, we are certainly not
12621 looking at a class-name. */
12622 if (identifier == error_mark_node)
12623 decl = error_mark_node;
12624 /* If we know this is a type-name, there's no need to look it
12626 else if (typename_p)
12630 tree ambiguous_decls;
12631 /* If we already know that this lookup is ambiguous, then
12632 we've already issued an error message; there's no reason
12636 cp_parser_simulate_error (parser);
12637 return error_mark_node;
12639 /* If the next token is a `::', then the name must be a type
12642 [basic.lookup.qual]
12644 During the lookup for a name preceding the :: scope
12645 resolution operator, object, function, and enumerator
12646 names are ignored. */
12647 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12648 tag_type = typename_type;
12649 /* Look up the name. */
12650 decl = cp_parser_lookup_name (parser, identifier,
12652 /*is_template=*/false,
12653 /*is_namespace=*/false,
12654 check_dependency_p,
12656 if (ambiguous_decls)
12658 error ("reference to %qD is ambiguous", identifier);
12659 print_candidates (ambiguous_decls);
12660 if (cp_parser_parsing_tentatively (parser))
12662 identifier_token->ambiguous_p = true;
12663 cp_parser_simulate_error (parser);
12665 return error_mark_node;
12671 /* Try a template-id. */
12672 decl = cp_parser_template_id (parser, template_keyword_p,
12673 check_dependency_p,
12675 if (decl == error_mark_node)
12676 return error_mark_node;
12679 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12681 /* If this is a typename, create a TYPENAME_TYPE. */
12682 if (typename_p && decl != error_mark_node)
12684 decl = make_typename_type (scope, decl, typename_type,
12685 /*complain=*/tf_error);
12686 if (decl != error_mark_node)
12687 decl = TYPE_NAME (decl);
12690 /* Check to see that it is really the name of a class. */
12691 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12692 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12693 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12694 /* Situations like this:
12696 template <typename T> struct A {
12697 typename T::template X<int>::I i;
12700 are problematic. Is `T::template X<int>' a class-name? The
12701 standard does not seem to be definitive, but there is no other
12702 valid interpretation of the following `::'. Therefore, those
12703 names are considered class-names. */
12704 decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12705 else if (decl == error_mark_node
12706 || TREE_CODE (decl) != TYPE_DECL
12707 || TREE_TYPE (decl) == error_mark_node
12708 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12710 cp_parser_error (parser, "expected class-name");
12711 return error_mark_node;
12717 /* Parse a class-specifier.
12720 class-head { member-specification [opt] }
12722 Returns the TREE_TYPE representing the class. */
12725 cp_parser_class_specifier (cp_parser* parser)
12729 tree attributes = NULL_TREE;
12730 int has_trailing_semicolon;
12731 bool nested_name_specifier_p;
12732 unsigned saved_num_template_parameter_lists;
12733 tree old_scope = NULL_TREE;
12734 tree scope = NULL_TREE;
12736 push_deferring_access_checks (dk_no_deferred);
12738 /* Parse the class-head. */
12739 type = cp_parser_class_head (parser,
12740 &nested_name_specifier_p,
12742 /* If the class-head was a semantic disaster, skip the entire body
12746 cp_parser_skip_to_end_of_block_or_statement (parser);
12747 pop_deferring_access_checks ();
12748 return error_mark_node;
12751 /* Look for the `{'. */
12752 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12754 pop_deferring_access_checks ();
12755 return error_mark_node;
12758 /* Issue an error message if type-definitions are forbidden here. */
12759 cp_parser_check_type_definition (parser);
12760 /* Remember that we are defining one more class. */
12761 ++parser->num_classes_being_defined;
12762 /* Inside the class, surrounding template-parameter-lists do not
12764 saved_num_template_parameter_lists
12765 = parser->num_template_parameter_lists;
12766 parser->num_template_parameter_lists = 0;
12768 /* Start the class. */
12769 if (nested_name_specifier_p)
12771 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12772 old_scope = push_inner_scope (scope);
12774 type = begin_class_definition (type);
12776 if (type == error_mark_node)
12777 /* If the type is erroneous, skip the entire body of the class. */
12778 cp_parser_skip_to_closing_brace (parser);
12780 /* Parse the member-specification. */
12781 cp_parser_member_specification_opt (parser);
12783 /* Look for the trailing `}'. */
12784 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12785 /* We get better error messages by noticing a common problem: a
12786 missing trailing `;'. */
12787 token = cp_lexer_peek_token (parser->lexer);
12788 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12789 /* Look for trailing attributes to apply to this class. */
12790 if (cp_parser_allow_gnu_extensions_p (parser))
12792 tree sub_attr = cp_parser_attributes_opt (parser);
12793 attributes = chainon (attributes, sub_attr);
12795 if (type != error_mark_node)
12796 type = finish_struct (type, attributes);
12797 if (nested_name_specifier_p)
12798 pop_inner_scope (old_scope, scope);
12799 /* If this class is not itself within the scope of another class,
12800 then we need to parse the bodies of all of the queued function
12801 definitions. Note that the queued functions defined in a class
12802 are not always processed immediately following the
12803 class-specifier for that class. Consider:
12806 struct B { void f() { sizeof (A); } };
12809 If `f' were processed before the processing of `A' were
12810 completed, there would be no way to compute the size of `A'.
12811 Note that the nesting we are interested in here is lexical --
12812 not the semantic nesting given by TYPE_CONTEXT. In particular,
12815 struct A { struct B; };
12816 struct A::B { void f() { } };
12818 there is no need to delay the parsing of `A::B::f'. */
12819 if (--parser->num_classes_being_defined == 0)
12823 tree class_type = NULL_TREE;
12824 tree pushed_scope = NULL_TREE;
12826 /* In a first pass, parse default arguments to the functions.
12827 Then, in a second pass, parse the bodies of the functions.
12828 This two-phased approach handles cases like:
12836 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12837 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12838 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12839 TREE_PURPOSE (parser->unparsed_functions_queues)
12840 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12842 fn = TREE_VALUE (queue_entry);
12843 /* If there are default arguments that have not yet been processed,
12844 take care of them now. */
12845 if (class_type != TREE_PURPOSE (queue_entry))
12848 pop_scope (pushed_scope);
12849 class_type = TREE_PURPOSE (queue_entry);
12850 pushed_scope = push_scope (class_type);
12852 /* Make sure that any template parameters are in scope. */
12853 maybe_begin_member_template_processing (fn);
12854 /* Parse the default argument expressions. */
12855 cp_parser_late_parsing_default_args (parser, fn);
12856 /* Remove any template parameters from the symbol table. */
12857 maybe_end_member_template_processing ();
12860 pop_scope (pushed_scope);
12861 /* Now parse the body of the functions. */
12862 for (TREE_VALUE (parser->unparsed_functions_queues)
12863 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12864 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12865 TREE_VALUE (parser->unparsed_functions_queues)
12866 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12868 /* Figure out which function we need to process. */
12869 fn = TREE_VALUE (queue_entry);
12870 /* Parse the function. */
12871 cp_parser_late_parsing_for_member (parser, fn);
12875 /* Put back any saved access checks. */
12876 pop_deferring_access_checks ();
12878 /* Restore the count of active template-parameter-lists. */
12879 parser->num_template_parameter_lists
12880 = saved_num_template_parameter_lists;
12885 /* Parse a class-head.
12888 class-key identifier [opt] base-clause [opt]
12889 class-key nested-name-specifier identifier base-clause [opt]
12890 class-key nested-name-specifier [opt] template-id
12894 class-key attributes identifier [opt] base-clause [opt]
12895 class-key attributes nested-name-specifier identifier base-clause [opt]
12896 class-key attributes nested-name-specifier [opt] template-id
12899 Returns the TYPE of the indicated class. Sets
12900 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12901 involving a nested-name-specifier was used, and FALSE otherwise.
12903 Returns error_mark_node if this is not a class-head.
12905 Returns NULL_TREE if the class-head is syntactically valid, but
12906 semantically invalid in a way that means we should skip the entire
12907 body of the class. */
12910 cp_parser_class_head (cp_parser* parser,
12911 bool* nested_name_specifier_p,
12912 tree *attributes_p)
12914 tree nested_name_specifier;
12915 enum tag_types class_key;
12916 tree id = NULL_TREE;
12917 tree type = NULL_TREE;
12919 bool template_id_p = false;
12920 bool qualified_p = false;
12921 bool invalid_nested_name_p = false;
12922 bool invalid_explicit_specialization_p = false;
12923 tree pushed_scope = NULL_TREE;
12924 unsigned num_templates;
12927 /* Assume no nested-name-specifier will be present. */
12928 *nested_name_specifier_p = false;
12929 /* Assume no template parameter lists will be used in defining the
12933 /* Look for the class-key. */
12934 class_key = cp_parser_class_key (parser);
12935 if (class_key == none_type)
12936 return error_mark_node;
12938 /* Parse the attributes. */
12939 attributes = cp_parser_attributes_opt (parser);
12941 /* If the next token is `::', that is invalid -- but sometimes
12942 people do try to write:
12946 Handle this gracefully by accepting the extra qualifier, and then
12947 issuing an error about it later if this really is a
12948 class-head. If it turns out just to be an elaborated type
12949 specifier, remain silent. */
12950 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12951 qualified_p = true;
12953 push_deferring_access_checks (dk_no_check);
12955 /* Determine the name of the class. Begin by looking for an
12956 optional nested-name-specifier. */
12957 nested_name_specifier
12958 = cp_parser_nested_name_specifier_opt (parser,
12959 /*typename_keyword_p=*/false,
12960 /*check_dependency_p=*/false,
12962 /*is_declaration=*/false);
12963 /* If there was a nested-name-specifier, then there *must* be an
12965 if (nested_name_specifier)
12967 /* Although the grammar says `identifier', it really means
12968 `class-name' or `template-name'. You are only allowed to
12969 define a class that has already been declared with this
12972 The proposed resolution for Core Issue 180 says that whever
12973 you see `class T::X' you should treat `X' as a type-name.
12975 It is OK to define an inaccessible class; for example:
12977 class A { class B; };
12980 We do not know if we will see a class-name, or a
12981 template-name. We look for a class-name first, in case the
12982 class-name is a template-id; if we looked for the
12983 template-name first we would stop after the template-name. */
12984 cp_parser_parse_tentatively (parser);
12985 type = cp_parser_class_name (parser,
12986 /*typename_keyword_p=*/false,
12987 /*template_keyword_p=*/false,
12989 /*check_dependency_p=*/false,
12990 /*class_head_p=*/true,
12991 /*is_declaration=*/false);
12992 /* If that didn't work, ignore the nested-name-specifier. */
12993 if (!cp_parser_parse_definitely (parser))
12995 invalid_nested_name_p = true;
12996 id = cp_parser_identifier (parser);
12997 if (id == error_mark_node)
13000 /* If we could not find a corresponding TYPE, treat this
13001 declaration like an unqualified declaration. */
13002 if (type == error_mark_node)
13003 nested_name_specifier = NULL_TREE;
13004 /* Otherwise, count the number of templates used in TYPE and its
13005 containing scopes. */
13010 for (scope = TREE_TYPE (type);
13011 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13012 scope = (TYPE_P (scope)
13013 ? TYPE_CONTEXT (scope)
13014 : DECL_CONTEXT (scope)))
13016 && CLASS_TYPE_P (scope)
13017 && CLASSTYPE_TEMPLATE_INFO (scope)
13018 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13019 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13023 /* Otherwise, the identifier is optional. */
13026 /* We don't know whether what comes next is a template-id,
13027 an identifier, or nothing at all. */
13028 cp_parser_parse_tentatively (parser);
13029 /* Check for a template-id. */
13030 id = cp_parser_template_id (parser,
13031 /*template_keyword_p=*/false,
13032 /*check_dependency_p=*/true,
13033 /*is_declaration=*/true);
13034 /* If that didn't work, it could still be an identifier. */
13035 if (!cp_parser_parse_definitely (parser))
13037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13038 id = cp_parser_identifier (parser);
13044 template_id_p = true;
13049 pop_deferring_access_checks ();
13052 cp_parser_check_for_invalid_template_id (parser, id);
13054 /* If it's not a `:' or a `{' then we can't really be looking at a
13055 class-head, since a class-head only appears as part of a
13056 class-specifier. We have to detect this situation before calling
13057 xref_tag, since that has irreversible side-effects. */
13058 if (!cp_parser_next_token_starts_class_definition_p (parser))
13060 cp_parser_error (parser, "expected %<{%> or %<:%>");
13061 return error_mark_node;
13064 /* At this point, we're going ahead with the class-specifier, even
13065 if some other problem occurs. */
13066 cp_parser_commit_to_tentative_parse (parser);
13067 /* Issue the error about the overly-qualified name now. */
13069 cp_parser_error (parser,
13070 "global qualification of class name is invalid");
13071 else if (invalid_nested_name_p)
13072 cp_parser_error (parser,
13073 "qualified name does not name a class");
13074 else if (nested_name_specifier)
13078 /* Reject typedef-names in class heads. */
13079 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13081 error ("invalid class name in declaration of %qD", type);
13086 /* Figure out in what scope the declaration is being placed. */
13087 scope = current_scope ();
13088 /* If that scope does not contain the scope in which the
13089 class was originally declared, the program is invalid. */
13090 if (scope && !is_ancestor (scope, nested_name_specifier))
13092 error ("declaration of %qD in %qD which does not enclose %qD",
13093 type, scope, nested_name_specifier);
13099 A declarator-id shall not be qualified exception of the
13100 definition of a ... nested class outside of its class
13101 ... [or] a the definition or explicit instantiation of a
13102 class member of a namespace outside of its namespace. */
13103 if (scope == nested_name_specifier)
13105 pedwarn ("extra qualification ignored");
13106 nested_name_specifier = NULL_TREE;
13110 /* An explicit-specialization must be preceded by "template <>". If
13111 it is not, try to recover gracefully. */
13112 if (at_namespace_scope_p ()
13113 && parser->num_template_parameter_lists == 0
13116 error ("an explicit specialization must be preceded by %<template <>%>");
13117 invalid_explicit_specialization_p = true;
13118 /* Take the same action that would have been taken by
13119 cp_parser_explicit_specialization. */
13120 ++parser->num_template_parameter_lists;
13121 begin_specialization ();
13123 /* There must be no "return" statements between this point and the
13124 end of this function; set "type "to the correct return value and
13125 use "goto done;" to return. */
13126 /* Make sure that the right number of template parameters were
13128 if (!cp_parser_check_template_parameters (parser, num_templates))
13130 /* If something went wrong, there is no point in even trying to
13131 process the class-definition. */
13136 /* Look up the type. */
13139 type = TREE_TYPE (id);
13140 maybe_process_partial_specialization (type);
13141 if (nested_name_specifier)
13142 pushed_scope = push_scope (nested_name_specifier);
13144 else if (nested_name_specifier)
13150 template <typename T> struct S { struct T };
13151 template <typename T> struct S<T>::T { };
13153 we will get a TYPENAME_TYPE when processing the definition of
13154 `S::T'. We need to resolve it to the actual type before we
13155 try to define it. */
13156 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13158 class_type = resolve_typename_type (TREE_TYPE (type),
13159 /*only_current_p=*/false);
13160 if (class_type != error_mark_node)
13161 type = TYPE_NAME (class_type);
13164 cp_parser_error (parser, "could not resolve typename type");
13165 type = error_mark_node;
13169 maybe_process_partial_specialization (TREE_TYPE (type));
13170 class_type = current_class_type;
13171 /* Enter the scope indicated by the nested-name-specifier. */
13172 pushed_scope = push_scope (nested_name_specifier);
13173 /* Get the canonical version of this type. */
13174 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13175 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13176 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13178 type = push_template_decl (type);
13179 if (type == error_mark_node)
13186 type = TREE_TYPE (type);
13187 *nested_name_specifier_p = true;
13189 else /* The name is not a nested name. */
13191 /* If the class was unnamed, create a dummy name. */
13193 id = make_anon_name ();
13194 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13195 parser->num_template_parameter_lists);
13198 /* Indicate whether this class was declared as a `class' or as a
13200 if (TREE_CODE (type) == RECORD_TYPE)
13201 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13202 cp_parser_check_class_key (class_key, type);
13204 /* If this type was already complete, and we see another definition,
13205 that's an error. */
13206 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13208 error ("redefinition of %q#T", type);
13209 error ("previous definition of %q+#T", type);
13214 /* We will have entered the scope containing the class; the names of
13215 base classes should be looked up in that context. For example:
13217 struct A { struct B {}; struct C; };
13218 struct A::C : B {};
13223 /* Get the list of base-classes, if there is one. */
13224 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13225 bases = cp_parser_base_clause (parser);
13227 /* Process the base classes. */
13228 xref_basetypes (type, bases);
13231 /* Leave the scope given by the nested-name-specifier. We will
13232 enter the class scope itself while processing the members. */
13234 pop_scope (pushed_scope);
13236 if (invalid_explicit_specialization_p)
13238 end_specialization ();
13239 --parser->num_template_parameter_lists;
13241 *attributes_p = attributes;
13245 /* Parse a class-key.
13252 Returns the kind of class-key specified, or none_type to indicate
13255 static enum tag_types
13256 cp_parser_class_key (cp_parser* parser)
13259 enum tag_types tag_type;
13261 /* Look for the class-key. */
13262 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13266 /* Check to see if the TOKEN is a class-key. */
13267 tag_type = cp_parser_token_is_class_key (token);
13269 cp_parser_error (parser, "expected class-key");
13273 /* Parse an (optional) member-specification.
13275 member-specification:
13276 member-declaration member-specification [opt]
13277 access-specifier : member-specification [opt] */
13280 cp_parser_member_specification_opt (cp_parser* parser)
13287 /* Peek at the next token. */
13288 token = cp_lexer_peek_token (parser->lexer);
13289 /* If it's a `}', or EOF then we've seen all the members. */
13290 if (token->type == CPP_CLOSE_BRACE
13291 || token->type == CPP_EOF
13292 || token->type == CPP_PRAGMA_EOL)
13295 /* See if this token is a keyword. */
13296 keyword = token->keyword;
13300 case RID_PROTECTED:
13302 /* Consume the access-specifier. */
13303 cp_lexer_consume_token (parser->lexer);
13304 /* Remember which access-specifier is active. */
13305 current_access_specifier = token->value;
13306 /* Look for the `:'. */
13307 cp_parser_require (parser, CPP_COLON, "`:'");
13311 /* Accept #pragmas at class scope. */
13312 if (token->type == CPP_PRAGMA)
13314 cp_parser_pragma (parser, pragma_external);
13318 /* Otherwise, the next construction must be a
13319 member-declaration. */
13320 cp_parser_member_declaration (parser);
13325 /* Parse a member-declaration.
13327 member-declaration:
13328 decl-specifier-seq [opt] member-declarator-list [opt] ;
13329 function-definition ; [opt]
13330 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13332 template-declaration
13334 member-declarator-list:
13336 member-declarator-list , member-declarator
13339 declarator pure-specifier [opt]
13340 declarator constant-initializer [opt]
13341 identifier [opt] : constant-expression
13345 member-declaration:
13346 __extension__ member-declaration
13349 declarator attributes [opt] pure-specifier [opt]
13350 declarator attributes [opt] constant-initializer [opt]
13351 identifier [opt] attributes [opt] : constant-expression */
13354 cp_parser_member_declaration (cp_parser* parser)
13356 cp_decl_specifier_seq decl_specifiers;
13357 tree prefix_attributes;
13359 int declares_class_or_enum;
13362 int saved_pedantic;
13364 /* Check for the `__extension__' keyword. */
13365 if (cp_parser_extension_opt (parser, &saved_pedantic))
13368 cp_parser_member_declaration (parser);
13369 /* Restore the old value of the PEDANTIC flag. */
13370 pedantic = saved_pedantic;
13375 /* Check for a template-declaration. */
13376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13378 /* An explicit specialization here is an error condition, and we
13379 expect the specialization handler to detect and report this. */
13380 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13381 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13382 cp_parser_explicit_specialization (parser);
13384 cp_parser_template_declaration (parser, /*member_p=*/true);
13389 /* Check for a using-declaration. */
13390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13392 /* Parse the using-declaration. */
13393 cp_parser_using_declaration (parser);
13398 /* Check for @defs. */
13399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13402 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13403 ivar = ivar_chains;
13407 ivar = TREE_CHAIN (member);
13408 TREE_CHAIN (member) = NULL_TREE;
13409 finish_member_declaration (member);
13414 /* Parse the decl-specifier-seq. */
13415 cp_parser_decl_specifier_seq (parser,
13416 CP_PARSER_FLAGS_OPTIONAL,
13418 &declares_class_or_enum);
13419 prefix_attributes = decl_specifiers.attributes;
13420 decl_specifiers.attributes = NULL_TREE;
13421 /* Check for an invalid type-name. */
13422 if (!decl_specifiers.type
13423 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13425 /* If there is no declarator, then the decl-specifier-seq should
13427 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13429 /* If there was no decl-specifier-seq, and the next token is a
13430 `;', then we have something like:
13436 Each member-declaration shall declare at least one member
13437 name of the class. */
13438 if (!decl_specifiers.any_specifiers_p)
13440 cp_token *token = cp_lexer_peek_token (parser->lexer);
13441 if (pedantic && !token->in_system_header)
13442 pedwarn ("%Hextra %<;%>", &token->location);
13448 /* See if this declaration is a friend. */
13449 friend_p = cp_parser_friend_p (&decl_specifiers);
13450 /* If there were decl-specifiers, check to see if there was
13451 a class-declaration. */
13452 type = check_tag_decl (&decl_specifiers);
13453 /* Nested classes have already been added to the class, but
13454 a `friend' needs to be explicitly registered. */
13457 /* If the `friend' keyword was present, the friend must
13458 be introduced with a class-key. */
13459 if (!declares_class_or_enum)
13460 error ("a class-key must be used when declaring a friend");
13463 template <typename T> struct A {
13464 friend struct A<T>::B;
13467 A<T>::B will be represented by a TYPENAME_TYPE, and
13468 therefore not recognized by check_tag_decl. */
13470 && decl_specifiers.type
13471 && TYPE_P (decl_specifiers.type))
13472 type = decl_specifiers.type;
13473 if (!type || !TYPE_P (type))
13474 error ("friend declaration does not name a class or "
13477 make_friend_class (current_class_type, type,
13478 /*complain=*/true);
13480 /* If there is no TYPE, an error message will already have
13482 else if (!type || type == error_mark_node)
13484 /* An anonymous aggregate has to be handled specially; such
13485 a declaration really declares a data member (with a
13486 particular type), as opposed to a nested class. */
13487 else if (ANON_AGGR_TYPE_P (type))
13489 /* Remove constructors and such from TYPE, now that we
13490 know it is an anonymous aggregate. */
13491 fixup_anonymous_aggr (type);
13492 /* And make the corresponding data member. */
13493 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13494 /* Add it to the class. */
13495 finish_member_declaration (decl);
13498 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13503 /* See if these declarations will be friends. */
13504 friend_p = cp_parser_friend_p (&decl_specifiers);
13506 /* Keep going until we hit the `;' at the end of the
13508 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13510 tree attributes = NULL_TREE;
13511 tree first_attribute;
13513 /* Peek at the next token. */
13514 token = cp_lexer_peek_token (parser->lexer);
13516 /* Check for a bitfield declaration. */
13517 if (token->type == CPP_COLON
13518 || (token->type == CPP_NAME
13519 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13525 /* Get the name of the bitfield. Note that we cannot just
13526 check TOKEN here because it may have been invalidated by
13527 the call to cp_lexer_peek_nth_token above. */
13528 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13529 identifier = cp_parser_identifier (parser);
13531 identifier = NULL_TREE;
13533 /* Consume the `:' token. */
13534 cp_lexer_consume_token (parser->lexer);
13535 /* Get the width of the bitfield. */
13537 = cp_parser_constant_expression (parser,
13538 /*allow_non_constant=*/false,
13541 /* Look for attributes that apply to the bitfield. */
13542 attributes = cp_parser_attributes_opt (parser);
13543 /* Remember which attributes are prefix attributes and
13545 first_attribute = attributes;
13546 /* Combine the attributes. */
13547 attributes = chainon (prefix_attributes, attributes);
13549 /* Create the bitfield declaration. */
13550 decl = grokbitfield (identifier
13551 ? make_id_declarator (NULL_TREE,
13557 /* Apply the attributes. */
13558 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13562 cp_declarator *declarator;
13564 tree asm_specification;
13565 int ctor_dtor_or_conv_p;
13567 /* Parse the declarator. */
13569 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13570 &ctor_dtor_or_conv_p,
13571 /*parenthesized_p=*/NULL,
13572 /*member_p=*/true);
13574 /* If something went wrong parsing the declarator, make sure
13575 that we at least consume some tokens. */
13576 if (declarator == cp_error_declarator)
13578 /* Skip to the end of the statement. */
13579 cp_parser_skip_to_end_of_statement (parser);
13580 /* If the next token is not a semicolon, that is
13581 probably because we just skipped over the body of
13582 a function. So, we consume a semicolon if
13583 present, but do not issue an error message if it
13585 if (cp_lexer_next_token_is (parser->lexer,
13587 cp_lexer_consume_token (parser->lexer);
13591 if (declares_class_or_enum & 2)
13592 cp_parser_check_for_definition_in_return_type
13593 (declarator, decl_specifiers.type);
13595 /* Look for an asm-specification. */
13596 asm_specification = cp_parser_asm_specification_opt (parser);
13597 /* Look for attributes that apply to the declaration. */
13598 attributes = cp_parser_attributes_opt (parser);
13599 /* Remember which attributes are prefix attributes and
13601 first_attribute = attributes;
13602 /* Combine the attributes. */
13603 attributes = chainon (prefix_attributes, attributes);
13605 /* If it's an `=', then we have a constant-initializer or a
13606 pure-specifier. It is not correct to parse the
13607 initializer before registering the member declaration
13608 since the member declaration should be in scope while
13609 its initializer is processed. However, the rest of the
13610 front end does not yet provide an interface that allows
13611 us to handle this correctly. */
13612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13616 A pure-specifier shall be used only in the declaration of
13617 a virtual function.
13619 A member-declarator can contain a constant-initializer
13620 only if it declares a static member of integral or
13623 Therefore, if the DECLARATOR is for a function, we look
13624 for a pure-specifier; otherwise, we look for a
13625 constant-initializer. When we call `grokfield', it will
13626 perform more stringent semantics checks. */
13627 if (declarator->kind == cdk_function)
13628 initializer = cp_parser_pure_specifier (parser);
13630 /* Parse the initializer. */
13631 initializer = cp_parser_constant_initializer (parser);
13633 /* Otherwise, there is no initializer. */
13635 initializer = NULL_TREE;
13637 /* See if we are probably looking at a function
13638 definition. We are certainly not looking at a
13639 member-declarator. Calling `grokfield' has
13640 side-effects, so we must not do it unless we are sure
13641 that we are looking at a member-declarator. */
13642 if (cp_parser_token_starts_function_definition_p
13643 (cp_lexer_peek_token (parser->lexer)))
13645 /* The grammar does not allow a pure-specifier to be
13646 used when a member function is defined. (It is
13647 possible that this fact is an oversight in the
13648 standard, since a pure function may be defined
13649 outside of the class-specifier. */
13651 error ("pure-specifier on function-definition");
13652 decl = cp_parser_save_member_function_body (parser,
13656 /* If the member was not a friend, declare it here. */
13658 finish_member_declaration (decl);
13659 /* Peek at the next token. */
13660 token = cp_lexer_peek_token (parser->lexer);
13661 /* If the next token is a semicolon, consume it. */
13662 if (token->type == CPP_SEMICOLON)
13663 cp_lexer_consume_token (parser->lexer);
13668 /* Create the declaration. */
13669 decl = grokfield (declarator, &decl_specifiers,
13670 initializer, asm_specification,
13672 /* Any initialization must have been from a
13673 constant-expression. */
13674 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13675 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13679 /* Reset PREFIX_ATTRIBUTES. */
13680 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13681 attributes = TREE_CHAIN (attributes);
13683 TREE_CHAIN (attributes) = NULL_TREE;
13685 /* If there is any qualification still in effect, clear it
13686 now; we will be starting fresh with the next declarator. */
13687 parser->scope = NULL_TREE;
13688 parser->qualifying_scope = NULL_TREE;
13689 parser->object_scope = NULL_TREE;
13690 /* If it's a `,', then there are more declarators. */
13691 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13692 cp_lexer_consume_token (parser->lexer);
13693 /* If the next token isn't a `;', then we have a parse error. */
13694 else if (cp_lexer_next_token_is_not (parser->lexer,
13697 cp_parser_error (parser, "expected %<;%>");
13698 /* Skip tokens until we find a `;'. */
13699 cp_parser_skip_to_end_of_statement (parser);
13706 /* Add DECL to the list of members. */
13708 finish_member_declaration (decl);
13710 if (TREE_CODE (decl) == FUNCTION_DECL)
13711 cp_parser_save_default_args (parser, decl);
13716 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13719 /* Parse a pure-specifier.
13724 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13725 Otherwise, ERROR_MARK_NODE is returned. */
13728 cp_parser_pure_specifier (cp_parser* parser)
13732 /* Look for the `=' token. */
13733 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13734 return error_mark_node;
13735 /* Look for the `0' token. */
13736 token = cp_lexer_consume_token (parser->lexer);
13737 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13738 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13739 return integer_zero_node;
13741 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13742 cp_parser_skip_to_end_of_statement (parser);
13743 return error_mark_node;
13746 /* Parse a constant-initializer.
13748 constant-initializer:
13749 = constant-expression
13751 Returns a representation of the constant-expression. */
13754 cp_parser_constant_initializer (cp_parser* parser)
13756 /* Look for the `=' token. */
13757 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13758 return error_mark_node;
13760 /* It is invalid to write:
13762 struct S { static const int i = { 7 }; };
13765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13767 cp_parser_error (parser,
13768 "a brace-enclosed initializer is not allowed here");
13769 /* Consume the opening brace. */
13770 cp_lexer_consume_token (parser->lexer);
13771 /* Skip the initializer. */
13772 cp_parser_skip_to_closing_brace (parser);
13773 /* Look for the trailing `}'. */
13774 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13776 return error_mark_node;
13779 return cp_parser_constant_expression (parser,
13780 /*allow_non_constant=*/false,
13784 /* Derived classes [gram.class.derived] */
13786 /* Parse a base-clause.
13789 : base-specifier-list
13791 base-specifier-list:
13793 base-specifier-list , base-specifier
13795 Returns a TREE_LIST representing the base-classes, in the order in
13796 which they were declared. The representation of each node is as
13797 described by cp_parser_base_specifier.
13799 In the case that no bases are specified, this function will return
13800 NULL_TREE, not ERROR_MARK_NODE. */
13803 cp_parser_base_clause (cp_parser* parser)
13805 tree bases = NULL_TREE;
13807 /* Look for the `:' that begins the list. */
13808 cp_parser_require (parser, CPP_COLON, "`:'");
13810 /* Scan the base-specifier-list. */
13816 /* Look for the base-specifier. */
13817 base = cp_parser_base_specifier (parser);
13818 /* Add BASE to the front of the list. */
13819 if (base != error_mark_node)
13821 TREE_CHAIN (base) = bases;
13824 /* Peek at the next token. */
13825 token = cp_lexer_peek_token (parser->lexer);
13826 /* If it's not a comma, then the list is complete. */
13827 if (token->type != CPP_COMMA)
13829 /* Consume the `,'. */
13830 cp_lexer_consume_token (parser->lexer);
13833 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13834 base class had a qualified name. However, the next name that
13835 appears is certainly not qualified. */
13836 parser->scope = NULL_TREE;
13837 parser->qualifying_scope = NULL_TREE;
13838 parser->object_scope = NULL_TREE;
13840 return nreverse (bases);
13843 /* Parse a base-specifier.
13846 :: [opt] nested-name-specifier [opt] class-name
13847 virtual access-specifier [opt] :: [opt] nested-name-specifier
13849 access-specifier virtual [opt] :: [opt] nested-name-specifier
13852 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13853 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13854 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13855 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13858 cp_parser_base_specifier (cp_parser* parser)
13862 bool virtual_p = false;
13863 bool duplicate_virtual_error_issued_p = false;
13864 bool duplicate_access_error_issued_p = false;
13865 bool class_scope_p, template_p;
13866 tree access = access_default_node;
13869 /* Process the optional `virtual' and `access-specifier'. */
13872 /* Peek at the next token. */
13873 token = cp_lexer_peek_token (parser->lexer);
13874 /* Process `virtual'. */
13875 switch (token->keyword)
13878 /* If `virtual' appears more than once, issue an error. */
13879 if (virtual_p && !duplicate_virtual_error_issued_p)
13881 cp_parser_error (parser,
13882 "%<virtual%> specified more than once in base-specified");
13883 duplicate_virtual_error_issued_p = true;
13888 /* Consume the `virtual' token. */
13889 cp_lexer_consume_token (parser->lexer);
13894 case RID_PROTECTED:
13896 /* If more than one access specifier appears, issue an
13898 if (access != access_default_node
13899 && !duplicate_access_error_issued_p)
13901 cp_parser_error (parser,
13902 "more than one access specifier in base-specified");
13903 duplicate_access_error_issued_p = true;
13906 access = ridpointers[(int) token->keyword];
13908 /* Consume the access-specifier. */
13909 cp_lexer_consume_token (parser->lexer);
13918 /* It is not uncommon to see programs mechanically, erroneously, use
13919 the 'typename' keyword to denote (dependent) qualified types
13920 as base classes. */
13921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13923 if (!processing_template_decl)
13924 error ("keyword %<typename%> not allowed outside of templates");
13926 error ("keyword %<typename%> not allowed in this context "
13927 "(the base class is implicitly a type)");
13928 cp_lexer_consume_token (parser->lexer);
13931 /* Look for the optional `::' operator. */
13932 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13933 /* Look for the nested-name-specifier. The simplest way to
13938 The keyword `typename' is not permitted in a base-specifier or
13939 mem-initializer; in these contexts a qualified name that
13940 depends on a template-parameter is implicitly assumed to be a
13943 is to pretend that we have seen the `typename' keyword at this
13945 cp_parser_nested_name_specifier_opt (parser,
13946 /*typename_keyword_p=*/true,
13947 /*check_dependency_p=*/true,
13949 /*is_declaration=*/true);
13950 /* If the base class is given by a qualified name, assume that names
13951 we see are type names or templates, as appropriate. */
13952 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13953 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13955 /* Finally, look for the class-name. */
13956 type = cp_parser_class_name (parser,
13960 /*check_dependency_p=*/true,
13961 /*class_head_p=*/false,
13962 /*is_declaration=*/true);
13964 if (type == error_mark_node)
13965 return error_mark_node;
13967 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13970 /* Exception handling [gram.exception] */
13972 /* Parse an (optional) exception-specification.
13974 exception-specification:
13975 throw ( type-id-list [opt] )
13977 Returns a TREE_LIST representing the exception-specification. The
13978 TREE_VALUE of each node is a type. */
13981 cp_parser_exception_specification_opt (cp_parser* parser)
13986 /* Peek at the next token. */
13987 token = cp_lexer_peek_token (parser->lexer);
13988 /* If it's not `throw', then there's no exception-specification. */
13989 if (!cp_parser_is_keyword (token, RID_THROW))
13992 /* Consume the `throw'. */
13993 cp_lexer_consume_token (parser->lexer);
13995 /* Look for the `('. */
13996 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13998 /* Peek at the next token. */
13999 token = cp_lexer_peek_token (parser->lexer);
14000 /* If it's not a `)', then there is a type-id-list. */
14001 if (token->type != CPP_CLOSE_PAREN)
14003 const char *saved_message;
14005 /* Types may not be defined in an exception-specification. */
14006 saved_message = parser->type_definition_forbidden_message;
14007 parser->type_definition_forbidden_message
14008 = "types may not be defined in an exception-specification";
14009 /* Parse the type-id-list. */
14010 type_id_list = cp_parser_type_id_list (parser);
14011 /* Restore the saved message. */
14012 parser->type_definition_forbidden_message = saved_message;
14015 type_id_list = empty_except_spec;
14017 /* Look for the `)'. */
14018 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14020 return type_id_list;
14023 /* Parse an (optional) type-id-list.
14027 type-id-list , type-id
14029 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14030 in the order that the types were presented. */
14033 cp_parser_type_id_list (cp_parser* parser)
14035 tree types = NULL_TREE;
14042 /* Get the next type-id. */
14043 type = cp_parser_type_id (parser);
14044 /* Add it to the list. */
14045 types = add_exception_specifier (types, type, /*complain=*/1);
14046 /* Peek at the next token. */
14047 token = cp_lexer_peek_token (parser->lexer);
14048 /* If it is not a `,', we are done. */
14049 if (token->type != CPP_COMMA)
14051 /* Consume the `,'. */
14052 cp_lexer_consume_token (parser->lexer);
14055 return nreverse (types);
14058 /* Parse a try-block.
14061 try compound-statement handler-seq */
14064 cp_parser_try_block (cp_parser* parser)
14068 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14069 try_block = begin_try_block ();
14070 cp_parser_compound_statement (parser, NULL, true);
14071 finish_try_block (try_block);
14072 cp_parser_handler_seq (parser);
14073 finish_handler_sequence (try_block);
14078 /* Parse a function-try-block.
14080 function-try-block:
14081 try ctor-initializer [opt] function-body handler-seq */
14084 cp_parser_function_try_block (cp_parser* parser)
14087 bool ctor_initializer_p;
14089 /* Look for the `try' keyword. */
14090 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14092 /* Let the rest of the front-end know where we are. */
14093 try_block = begin_function_try_block ();
14094 /* Parse the function-body. */
14096 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14097 /* We're done with the `try' part. */
14098 finish_function_try_block (try_block);
14099 /* Parse the handlers. */
14100 cp_parser_handler_seq (parser);
14101 /* We're done with the handlers. */
14102 finish_function_handler_sequence (try_block);
14104 return ctor_initializer_p;
14107 /* Parse a handler-seq.
14110 handler handler-seq [opt] */
14113 cp_parser_handler_seq (cp_parser* parser)
14119 /* Parse the handler. */
14120 cp_parser_handler (parser);
14121 /* Peek at the next token. */
14122 token = cp_lexer_peek_token (parser->lexer);
14123 /* If it's not `catch' then there are no more handlers. */
14124 if (!cp_parser_is_keyword (token, RID_CATCH))
14129 /* Parse a handler.
14132 catch ( exception-declaration ) compound-statement */
14135 cp_parser_handler (cp_parser* parser)
14140 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14141 handler = begin_handler ();
14142 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14143 declaration = cp_parser_exception_declaration (parser);
14144 finish_handler_parms (declaration, handler);
14145 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14146 cp_parser_compound_statement (parser, NULL, false);
14147 finish_handler (handler);
14150 /* Parse an exception-declaration.
14152 exception-declaration:
14153 type-specifier-seq declarator
14154 type-specifier-seq abstract-declarator
14158 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14159 ellipsis variant is used. */
14162 cp_parser_exception_declaration (cp_parser* parser)
14165 cp_decl_specifier_seq type_specifiers;
14166 cp_declarator *declarator;
14167 const char *saved_message;
14169 /* If it's an ellipsis, it's easy to handle. */
14170 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14172 /* Consume the `...' token. */
14173 cp_lexer_consume_token (parser->lexer);
14177 /* Types may not be defined in exception-declarations. */
14178 saved_message = parser->type_definition_forbidden_message;
14179 parser->type_definition_forbidden_message
14180 = "types may not be defined in exception-declarations";
14182 /* Parse the type-specifier-seq. */
14183 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14185 /* If it's a `)', then there is no declarator. */
14186 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14189 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14190 /*ctor_dtor_or_conv_p=*/NULL,
14191 /*parenthesized_p=*/NULL,
14192 /*member_p=*/false);
14194 /* Restore the saved message. */
14195 parser->type_definition_forbidden_message = saved_message;
14197 if (type_specifiers.any_specifiers_p)
14199 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14200 if (decl == NULL_TREE)
14201 error ("invalid catch parameter");
14209 /* Parse a throw-expression.
14212 throw assignment-expression [opt]
14214 Returns a THROW_EXPR representing the throw-expression. */
14217 cp_parser_throw_expression (cp_parser* parser)
14222 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14223 token = cp_lexer_peek_token (parser->lexer);
14224 /* Figure out whether or not there is an assignment-expression
14225 following the "throw" keyword. */
14226 if (token->type == CPP_COMMA
14227 || token->type == CPP_SEMICOLON
14228 || token->type == CPP_CLOSE_PAREN
14229 || token->type == CPP_CLOSE_SQUARE
14230 || token->type == CPP_CLOSE_BRACE
14231 || token->type == CPP_COLON)
14232 expression = NULL_TREE;
14234 expression = cp_parser_assignment_expression (parser,
14237 return build_throw (expression);
14240 /* GNU Extensions */
14242 /* Parse an (optional) asm-specification.
14245 asm ( string-literal )
14247 If the asm-specification is present, returns a STRING_CST
14248 corresponding to the string-literal. Otherwise, returns
14252 cp_parser_asm_specification_opt (cp_parser* parser)
14255 tree asm_specification;
14257 /* Peek at the next token. */
14258 token = cp_lexer_peek_token (parser->lexer);
14259 /* If the next token isn't the `asm' keyword, then there's no
14260 asm-specification. */
14261 if (!cp_parser_is_keyword (token, RID_ASM))
14264 /* Consume the `asm' token. */
14265 cp_lexer_consume_token (parser->lexer);
14266 /* Look for the `('. */
14267 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14269 /* Look for the string-literal. */
14270 asm_specification = cp_parser_string_literal (parser, false, false);
14272 /* Look for the `)'. */
14273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14275 return asm_specification;
14278 /* Parse an asm-operand-list.
14282 asm-operand-list , asm-operand
14285 string-literal ( expression )
14286 [ string-literal ] string-literal ( expression )
14288 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14289 each node is the expression. The TREE_PURPOSE is itself a
14290 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14291 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14292 is a STRING_CST for the string literal before the parenthesis. */
14295 cp_parser_asm_operand_list (cp_parser* parser)
14297 tree asm_operands = NULL_TREE;
14301 tree string_literal;
14305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14307 /* Consume the `[' token. */
14308 cp_lexer_consume_token (parser->lexer);
14309 /* Read the operand name. */
14310 name = cp_parser_identifier (parser);
14311 if (name != error_mark_node)
14312 name = build_string (IDENTIFIER_LENGTH (name),
14313 IDENTIFIER_POINTER (name));
14314 /* Look for the closing `]'. */
14315 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14319 /* Look for the string-literal. */
14320 string_literal = cp_parser_string_literal (parser, false, false);
14322 /* Look for the `('. */
14323 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14324 /* Parse the expression. */
14325 expression = cp_parser_expression (parser, /*cast_p=*/false);
14326 /* Look for the `)'. */
14327 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14329 /* Add this operand to the list. */
14330 asm_operands = tree_cons (build_tree_list (name, string_literal),
14333 /* If the next token is not a `,', there are no more
14335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14337 /* Consume the `,'. */
14338 cp_lexer_consume_token (parser->lexer);
14341 return nreverse (asm_operands);
14344 /* Parse an asm-clobber-list.
14348 asm-clobber-list , string-literal
14350 Returns a TREE_LIST, indicating the clobbers in the order that they
14351 appeared. The TREE_VALUE of each node is a STRING_CST. */
14354 cp_parser_asm_clobber_list (cp_parser* parser)
14356 tree clobbers = NULL_TREE;
14360 tree string_literal;
14362 /* Look for the string literal. */
14363 string_literal = cp_parser_string_literal (parser, false, false);
14364 /* Add it to the list. */
14365 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14366 /* If the next token is not a `,', then the list is
14368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14370 /* Consume the `,' token. */
14371 cp_lexer_consume_token (parser->lexer);
14377 /* Parse an (optional) series of attributes.
14380 attributes attribute
14383 __attribute__ (( attribute-list [opt] ))
14385 The return value is as for cp_parser_attribute_list. */
14388 cp_parser_attributes_opt (cp_parser* parser)
14390 tree attributes = NULL_TREE;
14395 tree attribute_list;
14397 /* Peek at the next token. */
14398 token = cp_lexer_peek_token (parser->lexer);
14399 /* If it's not `__attribute__', then we're done. */
14400 if (token->keyword != RID_ATTRIBUTE)
14403 /* Consume the `__attribute__' keyword. */
14404 cp_lexer_consume_token (parser->lexer);
14405 /* Look for the two `(' tokens. */
14406 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14407 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14409 /* Peek at the next token. */
14410 token = cp_lexer_peek_token (parser->lexer);
14411 if (token->type != CPP_CLOSE_PAREN)
14412 /* Parse the attribute-list. */
14413 attribute_list = cp_parser_attribute_list (parser);
14415 /* If the next token is a `)', then there is no attribute
14417 attribute_list = NULL;
14419 /* Look for the two `)' tokens. */
14420 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14421 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14423 /* Add these new attributes to the list. */
14424 attributes = chainon (attributes, attribute_list);
14430 /* Parse an attribute-list.
14434 attribute-list , attribute
14438 identifier ( identifier )
14439 identifier ( identifier , expression-list )
14440 identifier ( expression-list )
14442 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14443 to an attribute. The TREE_PURPOSE of each node is the identifier
14444 indicating which attribute is in use. The TREE_VALUE represents
14445 the arguments, if any. */
14448 cp_parser_attribute_list (cp_parser* parser)
14450 tree attribute_list = NULL_TREE;
14451 bool save_translate_strings_p = parser->translate_strings_p;
14453 parser->translate_strings_p = false;
14460 /* Look for the identifier. We also allow keywords here; for
14461 example `__attribute__ ((const))' is legal. */
14462 token = cp_lexer_peek_token (parser->lexer);
14463 if (token->type == CPP_NAME
14464 || token->type == CPP_KEYWORD)
14466 /* Consume the token. */
14467 token = cp_lexer_consume_token (parser->lexer);
14469 /* Save away the identifier that indicates which attribute
14471 identifier = token->value;
14472 attribute = build_tree_list (identifier, NULL_TREE);
14474 /* Peek at the next token. */
14475 token = cp_lexer_peek_token (parser->lexer);
14476 /* If it's an `(', then parse the attribute arguments. */
14477 if (token->type == CPP_OPEN_PAREN)
14481 arguments = (cp_parser_parenthesized_expression_list
14482 (parser, true, /*cast_p=*/false,
14483 /*non_constant_p=*/NULL));
14484 /* Save the identifier and arguments away. */
14485 TREE_VALUE (attribute) = arguments;
14488 /* Add this attribute to the list. */
14489 TREE_CHAIN (attribute) = attribute_list;
14490 attribute_list = attribute;
14492 token = cp_lexer_peek_token (parser->lexer);
14494 /* Now, look for more attributes. If the next token isn't a
14495 `,', we're done. */
14496 if (token->type != CPP_COMMA)
14499 /* Consume the comma and keep going. */
14500 cp_lexer_consume_token (parser->lexer);
14502 parser->translate_strings_p = save_translate_strings_p;
14504 /* We built up the list in reverse order. */
14505 return nreverse (attribute_list);
14508 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14509 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14510 current value of the PEDANTIC flag, regardless of whether or not
14511 the `__extension__' keyword is present. The caller is responsible
14512 for restoring the value of the PEDANTIC flag. */
14515 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14517 /* Save the old value of the PEDANTIC flag. */
14518 *saved_pedantic = pedantic;
14520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14522 /* Consume the `__extension__' token. */
14523 cp_lexer_consume_token (parser->lexer);
14524 /* We're not being pedantic while the `__extension__' keyword is
14534 /* Parse a label declaration.
14537 __label__ label-declarator-seq ;
14539 label-declarator-seq:
14540 identifier , label-declarator-seq
14544 cp_parser_label_declaration (cp_parser* parser)
14546 /* Look for the `__label__' keyword. */
14547 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14553 /* Look for an identifier. */
14554 identifier = cp_parser_identifier (parser);
14555 /* If we failed, stop. */
14556 if (identifier == error_mark_node)
14558 /* Declare it as a label. */
14559 finish_label_decl (identifier);
14560 /* If the next token is a `;', stop. */
14561 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14563 /* Look for the `,' separating the label declarations. */
14564 cp_parser_require (parser, CPP_COMMA, "`,'");
14567 /* Look for the final `;'. */
14568 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14571 /* Support Functions */
14573 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14574 NAME should have one of the representations used for an
14575 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14576 is returned. If PARSER->SCOPE is a dependent type, then a
14577 SCOPE_REF is returned.
14579 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14580 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14581 was formed. Abstractly, such entities should not be passed to this
14582 function, because they do not need to be looked up, but it is
14583 simpler to check for this special case here, rather than at the
14586 In cases not explicitly covered above, this function returns a
14587 DECL, OVERLOAD, or baselink representing the result of the lookup.
14588 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14591 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14592 (e.g., "struct") that was used. In that case bindings that do not
14593 refer to types are ignored.
14595 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14598 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14601 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14604 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14605 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14606 NULL_TREE otherwise. */
14609 cp_parser_lookup_name (cp_parser *parser, tree name,
14610 enum tag_types tag_type,
14613 bool check_dependency,
14614 tree *ambiguous_decls)
14618 tree object_type = parser->context->object_type;
14620 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14621 flags |= LOOKUP_COMPLAIN;
14623 /* Assume that the lookup will be unambiguous. */
14624 if (ambiguous_decls)
14625 *ambiguous_decls = NULL_TREE;
14627 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14628 no longer valid. Note that if we are parsing tentatively, and
14629 the parse fails, OBJECT_TYPE will be automatically restored. */
14630 parser->context->object_type = NULL_TREE;
14632 if (name == error_mark_node)
14633 return error_mark_node;
14635 /* A template-id has already been resolved; there is no lookup to
14637 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14639 if (BASELINK_P (name))
14641 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14642 == TEMPLATE_ID_EXPR);
14646 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14647 it should already have been checked to make sure that the name
14648 used matches the type being destroyed. */
14649 if (TREE_CODE (name) == BIT_NOT_EXPR)
14653 /* Figure out to which type this destructor applies. */
14655 type = parser->scope;
14656 else if (object_type)
14657 type = object_type;
14659 type = current_class_type;
14660 /* If that's not a class type, there is no destructor. */
14661 if (!type || !CLASS_TYPE_P (type))
14662 return error_mark_node;
14663 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14664 lazily_declare_fn (sfk_destructor, type);
14665 if (!CLASSTYPE_DESTRUCTORS (type))
14666 return error_mark_node;
14667 /* If it was a class type, return the destructor. */
14668 return CLASSTYPE_DESTRUCTORS (type);
14671 /* By this point, the NAME should be an ordinary identifier. If
14672 the id-expression was a qualified name, the qualifying scope is
14673 stored in PARSER->SCOPE at this point. */
14674 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14676 /* Perform the lookup. */
14681 if (parser->scope == error_mark_node)
14682 return error_mark_node;
14684 /* If the SCOPE is dependent, the lookup must be deferred until
14685 the template is instantiated -- unless we are explicitly
14686 looking up names in uninstantiated templates. Even then, we
14687 cannot look up the name if the scope is not a class type; it
14688 might, for example, be a template type parameter. */
14689 dependent_p = (TYPE_P (parser->scope)
14690 && !(parser->in_declarator_p
14691 && currently_open_class (parser->scope))
14692 && dependent_type_p (parser->scope));
14693 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14700 /* The resolution to Core Issue 180 says that `struct
14701 A::B' should be considered a type-name, even if `A'
14703 type = make_typename_type (parser->scope, name, tag_type,
14704 /*complain=*/tf_error);
14705 decl = TYPE_NAME (type);
14707 else if (is_template
14708 && (cp_parser_next_token_ends_template_argument_p (parser)
14709 || cp_lexer_next_token_is (parser->lexer,
14711 decl = make_unbound_class_template (parser->scope,
14713 /*complain=*/tf_error);
14715 decl = build_qualified_name (/*type=*/NULL_TREE,
14716 parser->scope, name,
14721 tree pushed_scope = NULL_TREE;
14723 /* If PARSER->SCOPE is a dependent type, then it must be a
14724 class type, and we must not be checking dependencies;
14725 otherwise, we would have processed this lookup above. So
14726 that PARSER->SCOPE is not considered a dependent base by
14727 lookup_member, we must enter the scope here. */
14729 pushed_scope = push_scope (parser->scope);
14730 /* If the PARSER->SCOPE is a template specialization, it
14731 may be instantiated during name lookup. In that case,
14732 errors may be issued. Even if we rollback the current
14733 tentative parse, those errors are valid. */
14734 decl = lookup_qualified_name (parser->scope, name,
14735 tag_type != none_type,
14736 /*complain=*/true);
14738 pop_scope (pushed_scope);
14740 parser->qualifying_scope = parser->scope;
14741 parser->object_scope = NULL_TREE;
14743 else if (object_type)
14745 tree object_decl = NULL_TREE;
14746 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14747 OBJECT_TYPE is not a class. */
14748 if (CLASS_TYPE_P (object_type))
14749 /* If the OBJECT_TYPE is a template specialization, it may
14750 be instantiated during name lookup. In that case, errors
14751 may be issued. Even if we rollback the current tentative
14752 parse, those errors are valid. */
14753 object_decl = lookup_member (object_type,
14756 tag_type != none_type);
14757 /* Look it up in the enclosing context, too. */
14758 decl = lookup_name_real (name, tag_type != none_type,
14760 /*block_p=*/true, is_namespace, flags);
14761 parser->object_scope = object_type;
14762 parser->qualifying_scope = NULL_TREE;
14764 decl = object_decl;
14768 decl = lookup_name_real (name, tag_type != none_type,
14770 /*block_p=*/true, is_namespace, flags);
14771 parser->qualifying_scope = NULL_TREE;
14772 parser->object_scope = NULL_TREE;
14775 /* If the lookup failed, let our caller know. */
14776 if (!decl || decl == error_mark_node)
14777 return error_mark_node;
14779 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14780 if (TREE_CODE (decl) == TREE_LIST)
14782 if (ambiguous_decls)
14783 *ambiguous_decls = decl;
14784 /* The error message we have to print is too complicated for
14785 cp_parser_error, so we incorporate its actions directly. */
14786 if (!cp_parser_simulate_error (parser))
14788 error ("reference to %qD is ambiguous", name);
14789 print_candidates (decl);
14791 return error_mark_node;
14794 gcc_assert (DECL_P (decl)
14795 || TREE_CODE (decl) == OVERLOAD
14796 || TREE_CODE (decl) == SCOPE_REF
14797 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14798 || BASELINK_P (decl));
14800 /* If we have resolved the name of a member declaration, check to
14801 see if the declaration is accessible. When the name resolves to
14802 set of overloaded functions, accessibility is checked when
14803 overload resolution is done.
14805 During an explicit instantiation, access is not checked at all,
14806 as per [temp.explicit]. */
14808 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14813 /* Like cp_parser_lookup_name, but for use in the typical case where
14814 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14815 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14818 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14820 return cp_parser_lookup_name (parser, name,
14822 /*is_template=*/false,
14823 /*is_namespace=*/false,
14824 /*check_dependency=*/true,
14825 /*ambiguous_decls=*/NULL);
14828 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14829 the current context, return the TYPE_DECL. If TAG_NAME_P is
14830 true, the DECL indicates the class being defined in a class-head,
14831 or declared in an elaborated-type-specifier.
14833 Otherwise, return DECL. */
14836 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14838 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14839 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14842 template <typename T> struct B;
14845 template <typename T> struct A::B {};
14847 Similarly, in an elaborated-type-specifier:
14849 namespace N { struct X{}; }
14852 template <typename T> friend struct N::X;
14855 However, if the DECL refers to a class type, and we are in
14856 the scope of the class, then the name lookup automatically
14857 finds the TYPE_DECL created by build_self_reference rather
14858 than a TEMPLATE_DECL. For example, in:
14860 template <class T> struct S {
14864 there is no need to handle such case. */
14866 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14867 return DECL_TEMPLATE_RESULT (decl);
14872 /* If too many, or too few, template-parameter lists apply to the
14873 declarator, issue an error message. Returns TRUE if all went well,
14874 and FALSE otherwise. */
14877 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14878 cp_declarator *declarator)
14880 unsigned num_templates;
14882 /* We haven't seen any classes that involve template parameters yet. */
14885 switch (declarator->kind)
14888 if (declarator->u.id.qualifying_scope)
14893 scope = declarator->u.id.qualifying_scope;
14894 member = declarator->u.id.unqualified_name;
14896 while (scope && CLASS_TYPE_P (scope))
14898 /* You're supposed to have one `template <...>'
14899 for every template class, but you don't need one
14900 for a full specialization. For example:
14902 template <class T> struct S{};
14903 template <> struct S<int> { void f(); };
14904 void S<int>::f () {}
14906 is correct; there shouldn't be a `template <>' for
14907 the definition of `S<int>::f'. */
14908 if (CLASSTYPE_TEMPLATE_INFO (scope)
14909 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14910 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14911 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14914 scope = TYPE_CONTEXT (scope);
14917 else if (TREE_CODE (declarator->u.id.unqualified_name)
14918 == TEMPLATE_ID_EXPR)
14919 /* If the DECLARATOR has the form `X<y>' then it uses one
14920 additional level of template parameters. */
14923 return cp_parser_check_template_parameters (parser,
14929 case cdk_reference:
14931 return (cp_parser_check_declarator_template_parameters
14932 (parser, declarator->declarator));
14938 gcc_unreachable ();
14943 /* NUM_TEMPLATES were used in the current declaration. If that is
14944 invalid, return FALSE and issue an error messages. Otherwise,
14948 cp_parser_check_template_parameters (cp_parser* parser,
14949 unsigned num_templates)
14951 /* If there are more template classes than parameter lists, we have
14954 template <class T> void S<T>::R<T>::f (); */
14955 if (parser->num_template_parameter_lists < num_templates)
14957 error ("too few template-parameter-lists");
14960 /* If there are the same number of template classes and parameter
14961 lists, that's OK. */
14962 if (parser->num_template_parameter_lists == num_templates)
14964 /* If there are more, but only one more, then we are referring to a
14965 member template. That's OK too. */
14966 if (parser->num_template_parameter_lists == num_templates + 1)
14968 /* Otherwise, there are too many template parameter lists. We have
14971 template <class T> template <class U> void S::f(); */
14972 error ("too many template-parameter-lists");
14976 /* Parse an optional `::' token indicating that the following name is
14977 from the global namespace. If so, PARSER->SCOPE is set to the
14978 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14979 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14980 Returns the new value of PARSER->SCOPE, if the `::' token is
14981 present, and NULL_TREE otherwise. */
14984 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14988 /* Peek at the next token. */
14989 token = cp_lexer_peek_token (parser->lexer);
14990 /* If we're looking at a `::' token then we're starting from the
14991 global namespace, not our current location. */
14992 if (token->type == CPP_SCOPE)
14994 /* Consume the `::' token. */
14995 cp_lexer_consume_token (parser->lexer);
14996 /* Set the SCOPE so that we know where to start the lookup. */
14997 parser->scope = global_namespace;
14998 parser->qualifying_scope = global_namespace;
14999 parser->object_scope = NULL_TREE;
15001 return parser->scope;
15003 else if (!current_scope_valid_p)
15005 parser->scope = NULL_TREE;
15006 parser->qualifying_scope = NULL_TREE;
15007 parser->object_scope = NULL_TREE;
15013 /* Returns TRUE if the upcoming token sequence is the start of a
15014 constructor declarator. If FRIEND_P is true, the declarator is
15015 preceded by the `friend' specifier. */
15018 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15020 bool constructor_p;
15021 tree type_decl = NULL_TREE;
15022 bool nested_name_p;
15023 cp_token *next_token;
15025 /* The common case is that this is not a constructor declarator, so
15026 try to avoid doing lots of work if at all possible. It's not
15027 valid declare a constructor at function scope. */
15028 if (at_function_scope_p ())
15030 /* And only certain tokens can begin a constructor declarator. */
15031 next_token = cp_lexer_peek_token (parser->lexer);
15032 if (next_token->type != CPP_NAME
15033 && next_token->type != CPP_SCOPE
15034 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15035 && next_token->type != CPP_TEMPLATE_ID)
15038 /* Parse tentatively; we are going to roll back all of the tokens
15040 cp_parser_parse_tentatively (parser);
15041 /* Assume that we are looking at a constructor declarator. */
15042 constructor_p = true;
15044 /* Look for the optional `::' operator. */
15045 cp_parser_global_scope_opt (parser,
15046 /*current_scope_valid_p=*/false);
15047 /* Look for the nested-name-specifier. */
15049 = (cp_parser_nested_name_specifier_opt (parser,
15050 /*typename_keyword_p=*/false,
15051 /*check_dependency_p=*/false,
15053 /*is_declaration=*/false)
15055 /* Outside of a class-specifier, there must be a
15056 nested-name-specifier. */
15057 if (!nested_name_p &&
15058 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15060 constructor_p = false;
15061 /* If we still think that this might be a constructor-declarator,
15062 look for a class-name. */
15067 template <typename T> struct S { S(); };
15068 template <typename T> S<T>::S ();
15070 we must recognize that the nested `S' names a class.
15073 template <typename T> S<T>::S<T> ();
15075 we must recognize that the nested `S' names a template. */
15076 type_decl = cp_parser_class_name (parser,
15077 /*typename_keyword_p=*/false,
15078 /*template_keyword_p=*/false,
15080 /*check_dependency_p=*/false,
15081 /*class_head_p=*/false,
15082 /*is_declaration=*/false);
15083 /* If there was no class-name, then this is not a constructor. */
15084 constructor_p = !cp_parser_error_occurred (parser);
15087 /* If we're still considering a constructor, we have to see a `(',
15088 to begin the parameter-declaration-clause, followed by either a
15089 `)', an `...', or a decl-specifier. We need to check for a
15090 type-specifier to avoid being fooled into thinking that:
15094 is a constructor. (It is actually a function named `f' that
15095 takes one parameter (of type `int') and returns a value of type
15098 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15101 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15102 /* A parameter declaration begins with a decl-specifier,
15103 which is either the "attribute" keyword, a storage class
15104 specifier, or (usually) a type-specifier. */
15105 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15106 && !cp_parser_storage_class_specifier_opt (parser))
15109 tree pushed_scope = NULL_TREE;
15110 unsigned saved_num_template_parameter_lists;
15112 /* Names appearing in the type-specifier should be looked up
15113 in the scope of the class. */
15114 if (current_class_type)
15118 type = TREE_TYPE (type_decl);
15119 if (TREE_CODE (type) == TYPENAME_TYPE)
15121 type = resolve_typename_type (type,
15122 /*only_current_p=*/false);
15123 if (type == error_mark_node)
15125 cp_parser_abort_tentative_parse (parser);
15129 pushed_scope = push_scope (type);
15132 /* Inside the constructor parameter list, surrounding
15133 template-parameter-lists do not apply. */
15134 saved_num_template_parameter_lists
15135 = parser->num_template_parameter_lists;
15136 parser->num_template_parameter_lists = 0;
15138 /* Look for the type-specifier. */
15139 cp_parser_type_specifier (parser,
15140 CP_PARSER_FLAGS_NONE,
15141 /*decl_specs=*/NULL,
15142 /*is_declarator=*/true,
15143 /*declares_class_or_enum=*/NULL,
15144 /*is_cv_qualifier=*/NULL);
15146 parser->num_template_parameter_lists
15147 = saved_num_template_parameter_lists;
15149 /* Leave the scope of the class. */
15151 pop_scope (pushed_scope);
15153 constructor_p = !cp_parser_error_occurred (parser);
15157 constructor_p = false;
15158 /* We did not really want to consume any tokens. */
15159 cp_parser_abort_tentative_parse (parser);
15161 return constructor_p;
15164 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15165 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15166 they must be performed once we are in the scope of the function.
15168 Returns the function defined. */
15171 cp_parser_function_definition_from_specifiers_and_declarator
15172 (cp_parser* parser,
15173 cp_decl_specifier_seq *decl_specifiers,
15175 const cp_declarator *declarator)
15180 /* Begin the function-definition. */
15181 success_p = start_function (decl_specifiers, declarator, attributes);
15183 /* The things we're about to see are not directly qualified by any
15184 template headers we've seen thus far. */
15185 reset_specialization ();
15187 /* If there were names looked up in the decl-specifier-seq that we
15188 did not check, check them now. We must wait until we are in the
15189 scope of the function to perform the checks, since the function
15190 might be a friend. */
15191 perform_deferred_access_checks ();
15195 /* Skip the entire function. */
15196 error ("invalid function declaration");
15197 cp_parser_skip_to_end_of_block_or_statement (parser);
15198 fn = error_mark_node;
15201 fn = cp_parser_function_definition_after_declarator (parser,
15202 /*inline_p=*/false);
15207 /* Parse the part of a function-definition that follows the
15208 declarator. INLINE_P is TRUE iff this function is an inline
15209 function defined with a class-specifier.
15211 Returns the function defined. */
15214 cp_parser_function_definition_after_declarator (cp_parser* parser,
15218 bool ctor_initializer_p = false;
15219 bool saved_in_unbraced_linkage_specification_p;
15220 unsigned saved_num_template_parameter_lists;
15222 /* If the next token is `return', then the code may be trying to
15223 make use of the "named return value" extension that G++ used to
15225 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15227 /* Consume the `return' keyword. */
15228 cp_lexer_consume_token (parser->lexer);
15229 /* Look for the identifier that indicates what value is to be
15231 cp_parser_identifier (parser);
15232 /* Issue an error message. */
15233 error ("named return values are no longer supported");
15234 /* Skip tokens until we reach the start of the function body. */
15237 cp_token *token = cp_lexer_peek_token (parser->lexer);
15238 if (token->type == CPP_OPEN_BRACE
15239 || token->type == CPP_EOF
15240 || token->type == CPP_PRAGMA_EOL)
15242 cp_lexer_consume_token (parser->lexer);
15245 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15246 anything declared inside `f'. */
15247 saved_in_unbraced_linkage_specification_p
15248 = parser->in_unbraced_linkage_specification_p;
15249 parser->in_unbraced_linkage_specification_p = false;
15250 /* Inside the function, surrounding template-parameter-lists do not
15252 saved_num_template_parameter_lists
15253 = parser->num_template_parameter_lists;
15254 parser->num_template_parameter_lists = 0;
15255 /* If the next token is `try', then we are looking at a
15256 function-try-block. */
15257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15258 ctor_initializer_p = cp_parser_function_try_block (parser);
15259 /* A function-try-block includes the function-body, so we only do
15260 this next part if we're not processing a function-try-block. */
15263 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15265 /* Finish the function. */
15266 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15267 (inline_p ? 2 : 0));
15268 /* Generate code for it, if necessary. */
15269 expand_or_defer_fn (fn);
15270 /* Restore the saved values. */
15271 parser->in_unbraced_linkage_specification_p
15272 = saved_in_unbraced_linkage_specification_p;
15273 parser->num_template_parameter_lists
15274 = saved_num_template_parameter_lists;
15279 /* Parse a template-declaration, assuming that the `export' (and
15280 `extern') keywords, if present, has already been scanned. MEMBER_P
15281 is as for cp_parser_template_declaration. */
15284 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15286 tree decl = NULL_TREE;
15287 tree parameter_list;
15288 bool friend_p = false;
15289 bool need_lang_pop;
15291 /* Look for the `template' keyword. */
15292 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15296 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15300 A template ... shall not have C linkage. */
15301 if (current_lang_name == lang_name_c)
15303 error ("template with C linkage");
15304 /* Give it C++ linkage to avoid confusing other parts of the
15306 push_lang_context (lang_name_cplusplus);
15307 need_lang_pop = true;
15310 need_lang_pop = false;
15311 /* If the next token is `>', then we have an invalid
15312 specialization. Rather than complain about an invalid template
15313 parameter, issue an error message here. */
15314 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15316 cp_parser_error (parser, "invalid explicit specialization");
15317 begin_specialization ();
15318 parameter_list = NULL_TREE;
15321 /* Parse the template parameters. */
15322 parameter_list = cp_parser_template_parameter_list (parser);
15324 /* Look for the `>'. */
15325 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15326 /* We just processed one more parameter list. */
15327 ++parser->num_template_parameter_lists;
15328 /* If the next token is `template', there are more template
15330 if (cp_lexer_next_token_is_keyword (parser->lexer,
15332 cp_parser_template_declaration_after_export (parser, member_p);
15335 /* There are no access checks when parsing a template, as we do not
15336 know if a specialization will be a friend. */
15337 push_deferring_access_checks (dk_no_check);
15339 decl = cp_parser_single_declaration (parser,
15343 pop_deferring_access_checks ();
15345 /* If this is a member template declaration, let the front
15347 if (member_p && !friend_p && decl)
15349 if (TREE_CODE (decl) == TYPE_DECL)
15350 cp_parser_check_access_in_redeclaration (decl);
15352 decl = finish_member_template_decl (decl);
15354 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15355 make_friend_class (current_class_type, TREE_TYPE (decl),
15356 /*complain=*/true);
15358 /* We are done with the current parameter list. */
15359 --parser->num_template_parameter_lists;
15362 finish_template_decl (parameter_list);
15364 /* Register member declarations. */
15365 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15366 finish_member_declaration (decl);
15367 /* For the erroneous case of a template with C linkage, we pushed an
15368 implicit C++ linkage scope; exit that scope now. */
15370 pop_lang_context ();
15371 /* If DECL is a function template, we must return to parse it later.
15372 (Even though there is no definition, there might be default
15373 arguments that need handling.) */
15374 if (member_p && decl
15375 && (TREE_CODE (decl) == FUNCTION_DECL
15376 || DECL_FUNCTION_TEMPLATE_P (decl)))
15377 TREE_VALUE (parser->unparsed_functions_queues)
15378 = tree_cons (NULL_TREE, decl,
15379 TREE_VALUE (parser->unparsed_functions_queues));
15382 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15383 `function-definition' sequence. MEMBER_P is true, this declaration
15384 appears in a class scope.
15386 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15387 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15390 cp_parser_single_declaration (cp_parser* parser,
15394 int declares_class_or_enum;
15395 tree decl = NULL_TREE;
15396 cp_decl_specifier_seq decl_specifiers;
15397 bool function_definition_p = false;
15399 /* This function is only used when processing a template
15401 gcc_assert (innermost_scope_kind () == sk_template_parms
15402 || innermost_scope_kind () == sk_template_spec);
15404 /* Defer access checks until we know what is being declared. */
15405 push_deferring_access_checks (dk_deferred);
15407 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15409 cp_parser_decl_specifier_seq (parser,
15410 CP_PARSER_FLAGS_OPTIONAL,
15412 &declares_class_or_enum);
15414 *friend_p = cp_parser_friend_p (&decl_specifiers);
15416 /* There are no template typedefs. */
15417 if (decl_specifiers.specs[(int) ds_typedef])
15419 error ("template declaration of %qs", "typedef");
15420 decl = error_mark_node;
15423 /* Gather up the access checks that occurred the
15424 decl-specifier-seq. */
15425 stop_deferring_access_checks ();
15427 /* Check for the declaration of a template class. */
15428 if (declares_class_or_enum)
15430 if (cp_parser_declares_only_class_p (parser))
15432 decl = shadow_tag (&decl_specifiers);
15437 friend template <typename T> struct A<T>::B;
15440 A<T>::B will be represented by a TYPENAME_TYPE, and
15441 therefore not recognized by shadow_tag. */
15442 if (friend_p && *friend_p
15444 && decl_specifiers.type
15445 && TYPE_P (decl_specifiers.type))
15446 decl = decl_specifiers.type;
15448 if (decl && decl != error_mark_node)
15449 decl = TYPE_NAME (decl);
15451 decl = error_mark_node;
15454 /* If it's not a template class, try for a template function. If
15455 the next token is a `;', then this declaration does not declare
15456 anything. But, if there were errors in the decl-specifiers, then
15457 the error might well have come from an attempted class-specifier.
15458 In that case, there's no need to warn about a missing declarator. */
15460 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15461 || decl_specifiers.type != error_mark_node))
15462 decl = cp_parser_init_declarator (parser,
15464 /*function_definition_allowed_p=*/true,
15466 declares_class_or_enum,
15467 &function_definition_p);
15469 pop_deferring_access_checks ();
15471 /* Clear any current qualification; whatever comes next is the start
15472 of something new. */
15473 parser->scope = NULL_TREE;
15474 parser->qualifying_scope = NULL_TREE;
15475 parser->object_scope = NULL_TREE;
15476 /* Look for a trailing `;' after the declaration. */
15477 if (!function_definition_p
15478 && (decl == error_mark_node
15479 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15480 cp_parser_skip_to_end_of_block_or_statement (parser);
15485 /* Parse a cast-expression that is not the operand of a unary "&". */
15488 cp_parser_simple_cast_expression (cp_parser *parser)
15490 return cp_parser_cast_expression (parser, /*address_p=*/false,
15494 /* Parse a functional cast to TYPE. Returns an expression
15495 representing the cast. */
15498 cp_parser_functional_cast (cp_parser* parser, tree type)
15500 tree expression_list;
15504 = cp_parser_parenthesized_expression_list (parser, false,
15506 /*non_constant_p=*/NULL);
15508 cast = build_functional_cast (type, expression_list);
15509 /* [expr.const]/1: In an integral constant expression "only type
15510 conversions to integral or enumeration type can be used". */
15511 if (TREE_CODE (type) == TYPE_DECL)
15512 type = TREE_TYPE (type);
15513 if (cast != error_mark_node && !dependent_type_p (type)
15514 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15516 if (cp_parser_non_integral_constant_expression
15517 (parser, "a call to a constructor"))
15518 return error_mark_node;
15523 /* Save the tokens that make up the body of a member function defined
15524 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15525 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15526 specifiers applied to the declaration. Returns the FUNCTION_DECL
15527 for the member function. */
15530 cp_parser_save_member_function_body (cp_parser* parser,
15531 cp_decl_specifier_seq *decl_specifiers,
15532 cp_declarator *declarator,
15539 /* Create the function-declaration. */
15540 fn = start_method (decl_specifiers, declarator, attributes);
15541 /* If something went badly wrong, bail out now. */
15542 if (fn == error_mark_node)
15544 /* If there's a function-body, skip it. */
15545 if (cp_parser_token_starts_function_definition_p
15546 (cp_lexer_peek_token (parser->lexer)))
15547 cp_parser_skip_to_end_of_block_or_statement (parser);
15548 return error_mark_node;
15551 /* Remember it, if there default args to post process. */
15552 cp_parser_save_default_args (parser, fn);
15554 /* Save away the tokens that make up the body of the
15556 first = parser->lexer->next_token;
15557 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15558 /* Handle function try blocks. */
15559 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15560 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15561 last = parser->lexer->next_token;
15563 /* Save away the inline definition; we will process it when the
15564 class is complete. */
15565 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15566 DECL_PENDING_INLINE_P (fn) = 1;
15568 /* We need to know that this was defined in the class, so that
15569 friend templates are handled correctly. */
15570 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15572 /* We're done with the inline definition. */
15573 finish_method (fn);
15575 /* Add FN to the queue of functions to be parsed later. */
15576 TREE_VALUE (parser->unparsed_functions_queues)
15577 = tree_cons (NULL_TREE, fn,
15578 TREE_VALUE (parser->unparsed_functions_queues));
15583 /* Parse a template-argument-list, as well as the trailing ">" (but
15584 not the opening ">"). See cp_parser_template_argument_list for the
15588 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15592 tree saved_qualifying_scope;
15593 tree saved_object_scope;
15594 bool saved_greater_than_is_operator_p;
15595 bool saved_skip_evaluation;
15599 When parsing a template-id, the first non-nested `>' is taken as
15600 the end of the template-argument-list rather than a greater-than
15602 saved_greater_than_is_operator_p
15603 = parser->greater_than_is_operator_p;
15604 parser->greater_than_is_operator_p = false;
15605 /* Parsing the argument list may modify SCOPE, so we save it
15607 saved_scope = parser->scope;
15608 saved_qualifying_scope = parser->qualifying_scope;
15609 saved_object_scope = parser->object_scope;
15610 /* We need to evaluate the template arguments, even though this
15611 template-id may be nested within a "sizeof". */
15612 saved_skip_evaluation = skip_evaluation;
15613 skip_evaluation = false;
15614 /* Parse the template-argument-list itself. */
15615 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15616 arguments = NULL_TREE;
15618 arguments = cp_parser_template_argument_list (parser);
15619 /* Look for the `>' that ends the template-argument-list. If we find
15620 a '>>' instead, it's probably just a typo. */
15621 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15623 if (!saved_greater_than_is_operator_p)
15625 /* If we're in a nested template argument list, the '>>' has
15626 to be a typo for '> >'. We emit the error message, but we
15627 continue parsing and we push a '>' as next token, so that
15628 the argument list will be parsed correctly. Note that the
15629 global source location is still on the token before the
15630 '>>', so we need to say explicitly where we want it. */
15631 cp_token *token = cp_lexer_peek_token (parser->lexer);
15632 error ("%H%<>>%> should be %<> >%> "
15633 "within a nested template argument list",
15636 /* ??? Proper recovery should terminate two levels of
15637 template argument list here. */
15638 token->type = CPP_GREATER;
15642 /* If this is not a nested template argument list, the '>>'
15643 is a typo for '>'. Emit an error message and continue.
15644 Same deal about the token location, but here we can get it
15645 right by consuming the '>>' before issuing the diagnostic. */
15646 cp_lexer_consume_token (parser->lexer);
15647 error ("spurious %<>>%>, use %<>%> to terminate "
15648 "a template argument list");
15652 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15653 /* The `>' token might be a greater-than operator again now. */
15654 parser->greater_than_is_operator_p
15655 = saved_greater_than_is_operator_p;
15656 /* Restore the SAVED_SCOPE. */
15657 parser->scope = saved_scope;
15658 parser->qualifying_scope = saved_qualifying_scope;
15659 parser->object_scope = saved_object_scope;
15660 skip_evaluation = saved_skip_evaluation;
15665 /* MEMBER_FUNCTION is a member function, or a friend. If default
15666 arguments, or the body of the function have not yet been parsed,
15670 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15672 /* If this member is a template, get the underlying
15674 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15675 member_function = DECL_TEMPLATE_RESULT (member_function);
15677 /* There should not be any class definitions in progress at this
15678 point; the bodies of members are only parsed outside of all class
15680 gcc_assert (parser->num_classes_being_defined == 0);
15681 /* While we're parsing the member functions we might encounter more
15682 classes. We want to handle them right away, but we don't want
15683 them getting mixed up with functions that are currently in the
15685 parser->unparsed_functions_queues
15686 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15688 /* Make sure that any template parameters are in scope. */
15689 maybe_begin_member_template_processing (member_function);
15691 /* If the body of the function has not yet been parsed, parse it
15693 if (DECL_PENDING_INLINE_P (member_function))
15695 tree function_scope;
15696 cp_token_cache *tokens;
15698 /* The function is no longer pending; we are processing it. */
15699 tokens = DECL_PENDING_INLINE_INFO (member_function);
15700 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15701 DECL_PENDING_INLINE_P (member_function) = 0;
15703 /* If this is a local class, enter the scope of the containing
15705 function_scope = current_function_decl;
15706 if (function_scope)
15707 push_function_context_to (function_scope);
15710 /* Push the body of the function onto the lexer stack. */
15711 cp_parser_push_lexer_for_tokens (parser, tokens);
15713 /* Let the front end know that we going to be defining this
15715 start_preparsed_function (member_function, NULL_TREE,
15716 SF_PRE_PARSED | SF_INCLASS_INLINE);
15718 /* Don't do access checking if it is a templated function. */
15719 if (processing_template_decl)
15720 push_deferring_access_checks (dk_no_check);
15722 /* Now, parse the body of the function. */
15723 cp_parser_function_definition_after_declarator (parser,
15724 /*inline_p=*/true);
15726 if (processing_template_decl)
15727 pop_deferring_access_checks ();
15729 /* Leave the scope of the containing function. */
15730 if (function_scope)
15731 pop_function_context_from (function_scope);
15732 cp_parser_pop_lexer (parser);
15735 /* Remove any template parameters from the symbol table. */
15736 maybe_end_member_template_processing ();
15738 /* Restore the queue. */
15739 parser->unparsed_functions_queues
15740 = TREE_CHAIN (parser->unparsed_functions_queues);
15743 /* If DECL contains any default args, remember it on the unparsed
15744 functions queue. */
15747 cp_parser_save_default_args (cp_parser* parser, tree decl)
15751 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15753 probe = TREE_CHAIN (probe))
15754 if (TREE_PURPOSE (probe))
15756 TREE_PURPOSE (parser->unparsed_functions_queues)
15757 = tree_cons (current_class_type, decl,
15758 TREE_PURPOSE (parser->unparsed_functions_queues));
15764 /* FN is a FUNCTION_DECL which may contains a parameter with an
15765 unparsed DEFAULT_ARG. Parse the default args now. This function
15766 assumes that the current scope is the scope in which the default
15767 argument should be processed. */
15770 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15772 bool saved_local_variables_forbidden_p;
15775 /* While we're parsing the default args, we might (due to the
15776 statement expression extension) encounter more classes. We want
15777 to handle them right away, but we don't want them getting mixed
15778 up with default args that are currently in the queue. */
15779 parser->unparsed_functions_queues
15780 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15782 /* Local variable names (and the `this' keyword) may not appear
15783 in a default argument. */
15784 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15785 parser->local_variables_forbidden_p = true;
15787 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15789 parm = TREE_CHAIN (parm))
15791 cp_token_cache *tokens;
15792 tree default_arg = TREE_PURPOSE (parm);
15794 VEC(tree,gc) *insts;
15801 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15802 /* This can happen for a friend declaration for a function
15803 already declared with default arguments. */
15806 /* Push the saved tokens for the default argument onto the parser's
15808 tokens = DEFARG_TOKENS (default_arg);
15809 cp_parser_push_lexer_for_tokens (parser, tokens);
15811 /* Parse the assignment-expression. */
15812 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15814 if (!processing_template_decl)
15815 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
15817 TREE_PURPOSE (parm) = parsed_arg;
15819 /* Update any instantiations we've already created. */
15820 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
15821 VEC_iterate (tree, insts, ix, copy); ix++)
15822 TREE_PURPOSE (copy) = parsed_arg;
15824 /* If the token stream has not been completely used up, then
15825 there was extra junk after the end of the default
15827 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15828 cp_parser_error (parser, "expected %<,%>");
15830 /* Revert to the main lexer. */
15831 cp_parser_pop_lexer (parser);
15834 /* Restore the state of local_variables_forbidden_p. */
15835 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15837 /* Restore the queue. */
15838 parser->unparsed_functions_queues
15839 = TREE_CHAIN (parser->unparsed_functions_queues);
15842 /* Parse the operand of `sizeof' (or a similar operator). Returns
15843 either a TYPE or an expression, depending on the form of the
15844 input. The KEYWORD indicates which kind of expression we have
15848 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15850 static const char *format;
15851 tree expr = NULL_TREE;
15852 const char *saved_message;
15853 bool saved_integral_constant_expression_p;
15854 bool saved_non_integral_constant_expression_p;
15856 /* Initialize FORMAT the first time we get here. */
15858 format = "types may not be defined in '%s' expressions";
15860 /* Types cannot be defined in a `sizeof' expression. Save away the
15862 saved_message = parser->type_definition_forbidden_message;
15863 /* And create the new one. */
15864 parser->type_definition_forbidden_message
15865 = XNEWVEC (const char, strlen (format)
15866 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15868 sprintf ((char *) parser->type_definition_forbidden_message,
15869 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15871 /* The restrictions on constant-expressions do not apply inside
15872 sizeof expressions. */
15873 saved_integral_constant_expression_p
15874 = parser->integral_constant_expression_p;
15875 saved_non_integral_constant_expression_p
15876 = parser->non_integral_constant_expression_p;
15877 parser->integral_constant_expression_p = false;
15879 /* Do not actually evaluate the expression. */
15881 /* If it's a `(', then we might be looking at the type-id
15883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15886 bool saved_in_type_id_in_expr_p;
15888 /* We can't be sure yet whether we're looking at a type-id or an
15890 cp_parser_parse_tentatively (parser);
15891 /* Consume the `('. */
15892 cp_lexer_consume_token (parser->lexer);
15893 /* Parse the type-id. */
15894 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15895 parser->in_type_id_in_expr_p = true;
15896 type = cp_parser_type_id (parser);
15897 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15898 /* Now, look for the trailing `)'. */
15899 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15900 /* If all went well, then we're done. */
15901 if (cp_parser_parse_definitely (parser))
15903 cp_decl_specifier_seq decl_specs;
15905 /* Build a trivial decl-specifier-seq. */
15906 clear_decl_specs (&decl_specs);
15907 decl_specs.type = type;
15909 /* Call grokdeclarator to figure out what type this is. */
15910 expr = grokdeclarator (NULL,
15914 /*attrlist=*/NULL);
15918 /* If the type-id production did not work out, then we must be
15919 looking at the unary-expression production. */
15921 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15923 /* Go back to evaluating expressions. */
15926 /* Free the message we created. */
15927 free ((char *) parser->type_definition_forbidden_message);
15928 /* And restore the old one. */
15929 parser->type_definition_forbidden_message = saved_message;
15930 parser->integral_constant_expression_p
15931 = saved_integral_constant_expression_p;
15932 parser->non_integral_constant_expression_p
15933 = saved_non_integral_constant_expression_p;
15938 /* If the current declaration has no declarator, return true. */
15941 cp_parser_declares_only_class_p (cp_parser *parser)
15943 /* If the next token is a `;' or a `,' then there is no
15945 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15946 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15949 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15952 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15953 cp_storage_class storage_class)
15955 if (decl_specs->storage_class != sc_none)
15956 decl_specs->multiple_storage_classes_p = true;
15958 decl_specs->storage_class = storage_class;
15961 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15962 is true, the type is a user-defined type; otherwise it is a
15963 built-in type specified by a keyword. */
15966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15968 bool user_defined_p)
15970 decl_specs->any_specifiers_p = true;
15972 /* If the user tries to redeclare bool or wchar_t (with, for
15973 example, in "typedef int wchar_t;") we remember that this is what
15974 happened. In system headers, we ignore these declarations so
15975 that G++ can work with system headers that are not C++-safe. */
15976 if (decl_specs->specs[(int) ds_typedef]
15978 && (type_spec == boolean_type_node
15979 || type_spec == wchar_type_node)
15980 && (decl_specs->type
15981 || decl_specs->specs[(int) ds_long]
15982 || decl_specs->specs[(int) ds_short]
15983 || decl_specs->specs[(int) ds_unsigned]
15984 || decl_specs->specs[(int) ds_signed]))
15986 decl_specs->redefined_builtin_type = type_spec;
15987 if (!decl_specs->type)
15989 decl_specs->type = type_spec;
15990 decl_specs->user_defined_type_p = false;
15993 else if (decl_specs->type)
15994 decl_specs->multiple_types_p = true;
15997 decl_specs->type = type_spec;
15998 decl_specs->user_defined_type_p = user_defined_p;
15999 decl_specs->redefined_builtin_type = NULL_TREE;
16003 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16004 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16007 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16009 return decl_specifiers->specs[(int) ds_friend] != 0;
16012 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16013 issue an error message indicating that TOKEN_DESC was expected.
16015 Returns the token consumed, if the token had the appropriate type.
16016 Otherwise, returns NULL. */
16019 cp_parser_require (cp_parser* parser,
16020 enum cpp_ttype type,
16021 const char* token_desc)
16023 if (cp_lexer_next_token_is (parser->lexer, type))
16024 return cp_lexer_consume_token (parser->lexer);
16027 /* Output the MESSAGE -- unless we're parsing tentatively. */
16028 if (!cp_parser_simulate_error (parser))
16030 char *message = concat ("expected ", token_desc, NULL);
16031 cp_parser_error (parser, message);
16038 /* Like cp_parser_require, except that tokens will be skipped until
16039 the desired token is found. An error message is still produced if
16040 the next token is not as expected. */
16043 cp_parser_skip_until_found (cp_parser* parser,
16044 enum cpp_ttype type,
16045 const char* token_desc)
16048 unsigned nesting_depth = 0;
16050 if (cp_parser_require (parser, type, token_desc))
16053 /* Skip tokens until the desired token is found. */
16056 /* Peek at the next token. */
16057 token = cp_lexer_peek_token (parser->lexer);
16059 /* If we've reached the token we want, consume it and stop. */
16060 if (token->type == type && !nesting_depth)
16062 cp_lexer_consume_token (parser->lexer);
16066 switch (token->type)
16069 case CPP_PRAGMA_EOL:
16070 /* If we've run out of tokens, stop. */
16073 case CPP_OPEN_BRACE:
16074 case CPP_OPEN_PAREN:
16075 case CPP_OPEN_SQUARE:
16079 case CPP_CLOSE_BRACE:
16080 case CPP_CLOSE_PAREN:
16081 case CPP_CLOSE_SQUARE:
16082 if (nesting_depth-- == 0)
16090 /* Consume this token. */
16091 cp_lexer_consume_token (parser->lexer);
16095 /* If the next token is the indicated keyword, consume it. Otherwise,
16096 issue an error message indicating that TOKEN_DESC was expected.
16098 Returns the token consumed, if the token had the appropriate type.
16099 Otherwise, returns NULL. */
16102 cp_parser_require_keyword (cp_parser* parser,
16104 const char* token_desc)
16106 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16108 if (token && token->keyword != keyword)
16110 dyn_string_t error_msg;
16112 /* Format the error message. */
16113 error_msg = dyn_string_new (0);
16114 dyn_string_append_cstr (error_msg, "expected ");
16115 dyn_string_append_cstr (error_msg, token_desc);
16116 cp_parser_error (parser, error_msg->s);
16117 dyn_string_delete (error_msg);
16124 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16125 function-definition. */
16128 cp_parser_token_starts_function_definition_p (cp_token* token)
16130 return (/* An ordinary function-body begins with an `{'. */
16131 token->type == CPP_OPEN_BRACE
16132 /* A ctor-initializer begins with a `:'. */
16133 || token->type == CPP_COLON
16134 /* A function-try-block begins with `try'. */
16135 || token->keyword == RID_TRY
16136 /* The named return value extension begins with `return'. */
16137 || token->keyword == RID_RETURN);
16140 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16144 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16148 token = cp_lexer_peek_token (parser->lexer);
16149 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16152 /* Returns TRUE iff the next token is the "," or ">" ending a
16153 template-argument. */
16156 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16160 token = cp_lexer_peek_token (parser->lexer);
16161 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16164 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16165 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16168 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16173 token = cp_lexer_peek_nth_token (parser->lexer, n);
16174 if (token->type == CPP_LESS)
16176 /* Check for the sequence `<::' in the original code. It would be lexed as
16177 `[:', where `[' is a digraph, and there is no whitespace before
16179 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16182 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16183 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16189 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16190 or none_type otherwise. */
16192 static enum tag_types
16193 cp_parser_token_is_class_key (cp_token* token)
16195 switch (token->keyword)
16200 return record_type;
16209 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16212 cp_parser_check_class_key (enum tag_types class_key, tree type)
16214 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16215 pedwarn ("%qs tag used in naming %q#T",
16216 class_key == union_type ? "union"
16217 : class_key == record_type ? "struct" : "class",
16221 /* Issue an error message if DECL is redeclared with different
16222 access than its original declaration [class.access.spec/3].
16223 This applies to nested classes and nested class templates.
16227 cp_parser_check_access_in_redeclaration (tree decl)
16229 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16232 if ((TREE_PRIVATE (decl)
16233 != (current_access_specifier == access_private_node))
16234 || (TREE_PROTECTED (decl)
16235 != (current_access_specifier == access_protected_node)))
16236 error ("%qD redeclared with different access", decl);
16239 /* Look for the `template' keyword, as a syntactic disambiguator.
16240 Return TRUE iff it is present, in which case it will be
16244 cp_parser_optional_template_keyword (cp_parser *parser)
16246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16248 /* The `template' keyword can only be used within templates;
16249 outside templates the parser can always figure out what is a
16250 template and what is not. */
16251 if (!processing_template_decl)
16253 error ("%<template%> (as a disambiguator) is only allowed "
16254 "within templates");
16255 /* If this part of the token stream is rescanned, the same
16256 error message would be generated. So, we purge the token
16257 from the stream. */
16258 cp_lexer_purge_token (parser->lexer);
16263 /* Consume the `template' keyword. */
16264 cp_lexer_consume_token (parser->lexer);
16272 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16273 set PARSER->SCOPE, and perform other related actions. */
16276 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16281 /* Get the stored value. */
16282 value = cp_lexer_consume_token (parser->lexer)->value;
16283 /* Perform any access checks that were deferred. */
16284 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16285 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16286 /* Set the scope from the stored value. */
16287 parser->scope = TREE_VALUE (value);
16288 parser->qualifying_scope = TREE_TYPE (value);
16289 parser->object_scope = NULL_TREE;
16292 /* Consume tokens up through a non-nested END token. */
16295 cp_parser_cache_group (cp_parser *parser,
16296 enum cpp_ttype end,
16303 /* Abort a parenthesized expression if we encounter a brace. */
16304 if ((end == CPP_CLOSE_PAREN || depth == 0)
16305 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16307 /* If we've reached the end of the file, stop. */
16308 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16309 || (end != CPP_PRAGMA_EOL
16310 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16312 /* Consume the next token. */
16313 token = cp_lexer_consume_token (parser->lexer);
16314 /* See if it starts a new group. */
16315 if (token->type == CPP_OPEN_BRACE)
16317 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16321 else if (token->type == CPP_OPEN_PAREN)
16322 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16323 else if (token->type == CPP_PRAGMA)
16324 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16325 else if (token->type == end)
16330 /* Begin parsing tentatively. We always save tokens while parsing
16331 tentatively so that if the tentative parsing fails we can restore the
16335 cp_parser_parse_tentatively (cp_parser* parser)
16337 /* Enter a new parsing context. */
16338 parser->context = cp_parser_context_new (parser->context);
16339 /* Begin saving tokens. */
16340 cp_lexer_save_tokens (parser->lexer);
16341 /* In order to avoid repetitive access control error messages,
16342 access checks are queued up until we are no longer parsing
16344 push_deferring_access_checks (dk_deferred);
16347 /* Commit to the currently active tentative parse. */
16350 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16352 cp_parser_context *context;
16355 /* Mark all of the levels as committed. */
16356 lexer = parser->lexer;
16357 for (context = parser->context; context->next; context = context->next)
16359 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16361 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16362 while (!cp_lexer_saving_tokens (lexer))
16363 lexer = lexer->next;
16364 cp_lexer_commit_tokens (lexer);
16368 /* Abort the currently active tentative parse. All consumed tokens
16369 will be rolled back, and no diagnostics will be issued. */
16372 cp_parser_abort_tentative_parse (cp_parser* parser)
16374 cp_parser_simulate_error (parser);
16375 /* Now, pretend that we want to see if the construct was
16376 successfully parsed. */
16377 cp_parser_parse_definitely (parser);
16380 /* Stop parsing tentatively. If a parse error has occurred, restore the
16381 token stream. Otherwise, commit to the tokens we have consumed.
16382 Returns true if no error occurred; false otherwise. */
16385 cp_parser_parse_definitely (cp_parser* parser)
16387 bool error_occurred;
16388 cp_parser_context *context;
16390 /* Remember whether or not an error occurred, since we are about to
16391 destroy that information. */
16392 error_occurred = cp_parser_error_occurred (parser);
16393 /* Remove the topmost context from the stack. */
16394 context = parser->context;
16395 parser->context = context->next;
16396 /* If no parse errors occurred, commit to the tentative parse. */
16397 if (!error_occurred)
16399 /* Commit to the tokens read tentatively, unless that was
16401 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16402 cp_lexer_commit_tokens (parser->lexer);
16404 pop_to_parent_deferring_access_checks ();
16406 /* Otherwise, if errors occurred, roll back our state so that things
16407 are just as they were before we began the tentative parse. */
16410 cp_lexer_rollback_tokens (parser->lexer);
16411 pop_deferring_access_checks ();
16413 /* Add the context to the front of the free list. */
16414 context->next = cp_parser_context_free_list;
16415 cp_parser_context_free_list = context;
16417 return !error_occurred;
16420 /* Returns true if we are parsing tentatively and are not committed to
16421 this tentative parse. */
16424 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16426 return (cp_parser_parsing_tentatively (parser)
16427 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16430 /* Returns nonzero iff an error has occurred during the most recent
16431 tentative parse. */
16434 cp_parser_error_occurred (cp_parser* parser)
16436 return (cp_parser_parsing_tentatively (parser)
16437 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16440 /* Returns nonzero if GNU extensions are allowed. */
16443 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16445 return parser->allow_gnu_extensions_p;
16448 /* Objective-C++ Productions */
16451 /* Parse an Objective-C expression, which feeds into a primary-expression
16455 objc-message-expression
16456 objc-string-literal
16457 objc-encode-expression
16458 objc-protocol-expression
16459 objc-selector-expression
16461 Returns a tree representation of the expression. */
16464 cp_parser_objc_expression (cp_parser* parser)
16466 /* Try to figure out what kind of declaration is present. */
16467 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16471 case CPP_OPEN_SQUARE:
16472 return cp_parser_objc_message_expression (parser);
16474 case CPP_OBJC_STRING:
16475 kwd = cp_lexer_consume_token (parser->lexer);
16476 return objc_build_string_object (kwd->value);
16479 switch (kwd->keyword)
16481 case RID_AT_ENCODE:
16482 return cp_parser_objc_encode_expression (parser);
16484 case RID_AT_PROTOCOL:
16485 return cp_parser_objc_protocol_expression (parser);
16487 case RID_AT_SELECTOR:
16488 return cp_parser_objc_selector_expression (parser);
16494 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16495 cp_parser_skip_to_end_of_block_or_statement (parser);
16498 return error_mark_node;
16501 /* Parse an Objective-C message expression.
16503 objc-message-expression:
16504 [ objc-message-receiver objc-message-args ]
16506 Returns a representation of an Objective-C message. */
16509 cp_parser_objc_message_expression (cp_parser* parser)
16511 tree receiver, messageargs;
16513 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16514 receiver = cp_parser_objc_message_receiver (parser);
16515 messageargs = cp_parser_objc_message_args (parser);
16516 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16518 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16521 /* Parse an objc-message-receiver.
16523 objc-message-receiver:
16525 simple-type-specifier
16527 Returns a representation of the type or expression. */
16530 cp_parser_objc_message_receiver (cp_parser* parser)
16534 /* An Objective-C message receiver may be either (1) a type
16535 or (2) an expression. */
16536 cp_parser_parse_tentatively (parser);
16537 rcv = cp_parser_expression (parser, false);
16539 if (cp_parser_parse_definitely (parser))
16542 rcv = cp_parser_simple_type_specifier (parser,
16543 /*decl_specs=*/NULL,
16544 CP_PARSER_FLAGS_NONE);
16546 return objc_get_class_reference (rcv);
16549 /* Parse the arguments and selectors comprising an Objective-C message.
16554 objc-selector-args , objc-comma-args
16556 objc-selector-args:
16557 objc-selector [opt] : assignment-expression
16558 objc-selector-args objc-selector [opt] : assignment-expression
16561 assignment-expression
16562 objc-comma-args , assignment-expression
16564 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16565 selector arguments and TREE_VALUE containing a list of comma
16569 cp_parser_objc_message_args (cp_parser* parser)
16571 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16572 bool maybe_unary_selector_p = true;
16573 cp_token *token = cp_lexer_peek_token (parser->lexer);
16575 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16577 tree selector = NULL_TREE, arg;
16579 if (token->type != CPP_COLON)
16580 selector = cp_parser_objc_selector (parser);
16582 /* Detect if we have a unary selector. */
16583 if (maybe_unary_selector_p
16584 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16585 return build_tree_list (selector, NULL_TREE);
16587 maybe_unary_selector_p = false;
16588 cp_parser_require (parser, CPP_COLON, "`:'");
16589 arg = cp_parser_assignment_expression (parser, false);
16592 = chainon (sel_args,
16593 build_tree_list (selector, arg));
16595 token = cp_lexer_peek_token (parser->lexer);
16598 /* Handle non-selector arguments, if any. */
16599 while (token->type == CPP_COMMA)
16603 cp_lexer_consume_token (parser->lexer);
16604 arg = cp_parser_assignment_expression (parser, false);
16607 = chainon (addl_args,
16608 build_tree_list (NULL_TREE, arg));
16610 token = cp_lexer_peek_token (parser->lexer);
16613 return build_tree_list (sel_args, addl_args);
16616 /* Parse an Objective-C encode expression.
16618 objc-encode-expression:
16619 @encode objc-typename
16621 Returns an encoded representation of the type argument. */
16624 cp_parser_objc_encode_expression (cp_parser* parser)
16628 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16630 type = complete_type (cp_parser_type_id (parser));
16631 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16635 error ("%<@encode%> must specify a type as an argument");
16636 return error_mark_node;
16639 return objc_build_encode_expr (type);
16642 /* Parse an Objective-C @defs expression. */
16645 cp_parser_objc_defs_expression (cp_parser *parser)
16649 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16651 name = cp_parser_identifier (parser);
16652 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16654 return objc_get_class_ivars (name);
16657 /* Parse an Objective-C protocol expression.
16659 objc-protocol-expression:
16660 @protocol ( identifier )
16662 Returns a representation of the protocol expression. */
16665 cp_parser_objc_protocol_expression (cp_parser* parser)
16669 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16670 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16671 proto = cp_parser_identifier (parser);
16672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16674 return objc_build_protocol_expr (proto);
16677 /* Parse an Objective-C selector expression.
16679 objc-selector-expression:
16680 @selector ( objc-method-signature )
16682 objc-method-signature:
16688 objc-selector-seq objc-selector :
16690 Returns a representation of the method selector. */
16693 cp_parser_objc_selector_expression (cp_parser* parser)
16695 tree sel_seq = NULL_TREE;
16696 bool maybe_unary_selector_p = true;
16699 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16700 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16701 token = cp_lexer_peek_token (parser->lexer);
16703 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16704 || token->type == CPP_SCOPE)
16706 tree selector = NULL_TREE;
16708 if (token->type != CPP_COLON
16709 || token->type == CPP_SCOPE)
16710 selector = cp_parser_objc_selector (parser);
16712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16713 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16715 /* Detect if we have a unary selector. */
16716 if (maybe_unary_selector_p)
16718 sel_seq = selector;
16719 goto finish_selector;
16723 cp_parser_error (parser, "expected %<:%>");
16726 maybe_unary_selector_p = false;
16727 token = cp_lexer_consume_token (parser->lexer);
16729 if (token->type == CPP_SCOPE)
16732 = chainon (sel_seq,
16733 build_tree_list (selector, NULL_TREE));
16735 = chainon (sel_seq,
16736 build_tree_list (NULL_TREE, NULL_TREE));
16740 = chainon (sel_seq,
16741 build_tree_list (selector, NULL_TREE));
16743 token = cp_lexer_peek_token (parser->lexer);
16747 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16749 return objc_build_selector_expr (sel_seq);
16752 /* Parse a list of identifiers.
16754 objc-identifier-list:
16756 objc-identifier-list , identifier
16758 Returns a TREE_LIST of identifier nodes. */
16761 cp_parser_objc_identifier_list (cp_parser* parser)
16763 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16764 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16766 while (sep->type == CPP_COMMA)
16768 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16769 list = chainon (list,
16770 build_tree_list (NULL_TREE,
16771 cp_parser_identifier (parser)));
16772 sep = cp_lexer_peek_token (parser->lexer);
16778 /* Parse an Objective-C alias declaration.
16780 objc-alias-declaration:
16781 @compatibility_alias identifier identifier ;
16783 This function registers the alias mapping with the Objective-C front-end.
16784 It returns nothing. */
16787 cp_parser_objc_alias_declaration (cp_parser* parser)
16791 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16792 alias = cp_parser_identifier (parser);
16793 orig = cp_parser_identifier (parser);
16794 objc_declare_alias (alias, orig);
16795 cp_parser_consume_semicolon_at_end_of_statement (parser);
16798 /* Parse an Objective-C class forward-declaration.
16800 objc-class-declaration:
16801 @class objc-identifier-list ;
16803 The function registers the forward declarations with the Objective-C
16804 front-end. It returns nothing. */
16807 cp_parser_objc_class_declaration (cp_parser* parser)
16809 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16810 objc_declare_class (cp_parser_objc_identifier_list (parser));
16811 cp_parser_consume_semicolon_at_end_of_statement (parser);
16814 /* Parse a list of Objective-C protocol references.
16816 objc-protocol-refs-opt:
16817 objc-protocol-refs [opt]
16819 objc-protocol-refs:
16820 < objc-identifier-list >
16822 Returns a TREE_LIST of identifiers, if any. */
16825 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16827 tree protorefs = NULL_TREE;
16829 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16831 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
16832 protorefs = cp_parser_objc_identifier_list (parser);
16833 cp_parser_require (parser, CPP_GREATER, "`>'");
16839 /* Parse a Objective-C visibility specification. */
16842 cp_parser_objc_visibility_spec (cp_parser* parser)
16844 cp_token *vis = cp_lexer_peek_token (parser->lexer);
16846 switch (vis->keyword)
16848 case RID_AT_PRIVATE:
16849 objc_set_visibility (2);
16851 case RID_AT_PROTECTED:
16852 objc_set_visibility (0);
16854 case RID_AT_PUBLIC:
16855 objc_set_visibility (1);
16861 /* Eat '@private'/'@protected'/'@public'. */
16862 cp_lexer_consume_token (parser->lexer);
16865 /* Parse an Objective-C method type. */
16868 cp_parser_objc_method_type (cp_parser* parser)
16870 objc_set_method_type
16871 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16876 /* Parse an Objective-C protocol qualifier. */
16879 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16881 tree quals = NULL_TREE, node;
16882 cp_token *token = cp_lexer_peek_token (parser->lexer);
16884 node = token->value;
16886 while (node && TREE_CODE (node) == IDENTIFIER_NODE
16887 && (node == ridpointers [(int) RID_IN]
16888 || node == ridpointers [(int) RID_OUT]
16889 || node == ridpointers [(int) RID_INOUT]
16890 || node == ridpointers [(int) RID_BYCOPY]
16891 || node == ridpointers [(int) RID_BYREF]
16892 || node == ridpointers [(int) RID_ONEWAY]))
16894 quals = tree_cons (NULL_TREE, node, quals);
16895 cp_lexer_consume_token (parser->lexer);
16896 token = cp_lexer_peek_token (parser->lexer);
16897 node = token->value;
16903 /* Parse an Objective-C typename. */
16906 cp_parser_objc_typename (cp_parser* parser)
16908 tree typename = NULL_TREE;
16910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16912 tree proto_quals, cp_type = NULL_TREE;
16914 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
16915 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16917 /* An ObjC type name may consist of just protocol qualifiers, in which
16918 case the type shall default to 'id'. */
16919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16920 cp_type = cp_parser_type_id (parser);
16922 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16923 typename = build_tree_list (proto_quals, cp_type);
16929 /* Check to see if TYPE refers to an Objective-C selector name. */
16932 cp_parser_objc_selector_p (enum cpp_ttype type)
16934 return (type == CPP_NAME || type == CPP_KEYWORD
16935 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16936 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16937 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16938 || type == CPP_XOR || type == CPP_XOR_EQ);
16941 /* Parse an Objective-C selector. */
16944 cp_parser_objc_selector (cp_parser* parser)
16946 cp_token *token = cp_lexer_consume_token (parser->lexer);
16948 if (!cp_parser_objc_selector_p (token->type))
16950 error ("invalid Objective-C++ selector name");
16951 return error_mark_node;
16954 /* C++ operator names are allowed to appear in ObjC selectors. */
16955 switch (token->type)
16957 case CPP_AND_AND: return get_identifier ("and");
16958 case CPP_AND_EQ: return get_identifier ("and_eq");
16959 case CPP_AND: return get_identifier ("bitand");
16960 case CPP_OR: return get_identifier ("bitor");
16961 case CPP_COMPL: return get_identifier ("compl");
16962 case CPP_NOT: return get_identifier ("not");
16963 case CPP_NOT_EQ: return get_identifier ("not_eq");
16964 case CPP_OR_OR: return get_identifier ("or");
16965 case CPP_OR_EQ: return get_identifier ("or_eq");
16966 case CPP_XOR: return get_identifier ("xor");
16967 case CPP_XOR_EQ: return get_identifier ("xor_eq");
16968 default: return token->value;
16972 /* Parse an Objective-C params list. */
16975 cp_parser_objc_method_keyword_params (cp_parser* parser)
16977 tree params = NULL_TREE;
16978 bool maybe_unary_selector_p = true;
16979 cp_token *token = cp_lexer_peek_token (parser->lexer);
16981 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16983 tree selector = NULL_TREE, typename, identifier;
16985 if (token->type != CPP_COLON)
16986 selector = cp_parser_objc_selector (parser);
16988 /* Detect if we have a unary selector. */
16989 if (maybe_unary_selector_p
16990 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16993 maybe_unary_selector_p = false;
16994 cp_parser_require (parser, CPP_COLON, "`:'");
16995 typename = cp_parser_objc_typename (parser);
16996 identifier = cp_parser_identifier (parser);
17000 objc_build_keyword_decl (selector,
17004 token = cp_lexer_peek_token (parser->lexer);
17010 /* Parse the non-keyword Objective-C params. */
17013 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17015 tree params = make_node (TREE_LIST);
17016 cp_token *token = cp_lexer_peek_token (parser->lexer);
17017 *ellipsisp = false; /* Initially, assume no ellipsis. */
17019 while (token->type == CPP_COMMA)
17021 cp_parameter_declarator *parmdecl;
17024 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17025 token = cp_lexer_peek_token (parser->lexer);
17027 if (token->type == CPP_ELLIPSIS)
17029 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17034 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17035 parm = grokdeclarator (parmdecl->declarator,
17036 &parmdecl->decl_specifiers,
17037 PARM, /*initialized=*/0,
17038 /*attrlist=*/NULL);
17040 chainon (params, build_tree_list (NULL_TREE, parm));
17041 token = cp_lexer_peek_token (parser->lexer);
17047 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17050 cp_parser_objc_interstitial_code (cp_parser* parser)
17052 cp_token *token = cp_lexer_peek_token (parser->lexer);
17054 /* If the next token is `extern' and the following token is a string
17055 literal, then we have a linkage specification. */
17056 if (token->keyword == RID_EXTERN
17057 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17058 cp_parser_linkage_specification (parser);
17059 /* Handle #pragma, if any. */
17060 else if (token->type == CPP_PRAGMA)
17061 cp_parser_pragma (parser, pragma_external);
17062 /* Allow stray semicolons. */
17063 else if (token->type == CPP_SEMICOLON)
17064 cp_lexer_consume_token (parser->lexer);
17065 /* Finally, try to parse a block-declaration, or a function-definition. */
17067 cp_parser_block_declaration (parser, /*statement_p=*/false);
17070 /* Parse a method signature. */
17073 cp_parser_objc_method_signature (cp_parser* parser)
17075 tree rettype, kwdparms, optparms;
17076 bool ellipsis = false;
17078 cp_parser_objc_method_type (parser);
17079 rettype = cp_parser_objc_typename (parser);
17080 kwdparms = cp_parser_objc_method_keyword_params (parser);
17081 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17083 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17086 /* Pars an Objective-C method prototype list. */
17089 cp_parser_objc_method_prototype_list (cp_parser* parser)
17091 cp_token *token = cp_lexer_peek_token (parser->lexer);
17093 while (token->keyword != RID_AT_END)
17095 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17097 objc_add_method_declaration
17098 (cp_parser_objc_method_signature (parser));
17099 cp_parser_consume_semicolon_at_end_of_statement (parser);
17102 /* Allow for interspersed non-ObjC++ code. */
17103 cp_parser_objc_interstitial_code (parser);
17105 token = cp_lexer_peek_token (parser->lexer);
17108 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17109 objc_finish_interface ();
17112 /* Parse an Objective-C method definition list. */
17115 cp_parser_objc_method_definition_list (cp_parser* parser)
17117 cp_token *token = cp_lexer_peek_token (parser->lexer);
17119 while (token->keyword != RID_AT_END)
17123 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17125 push_deferring_access_checks (dk_deferred);
17126 objc_start_method_definition
17127 (cp_parser_objc_method_signature (parser));
17129 /* For historical reasons, we accept an optional semicolon. */
17130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17131 cp_lexer_consume_token (parser->lexer);
17133 perform_deferred_access_checks ();
17134 stop_deferring_access_checks ();
17135 meth = cp_parser_function_definition_after_declarator (parser,
17137 pop_deferring_access_checks ();
17138 objc_finish_method_definition (meth);
17141 /* Allow for interspersed non-ObjC++ code. */
17142 cp_parser_objc_interstitial_code (parser);
17144 token = cp_lexer_peek_token (parser->lexer);
17147 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17148 objc_finish_implementation ();
17151 /* Parse Objective-C ivars. */
17154 cp_parser_objc_class_ivars (cp_parser* parser)
17156 cp_token *token = cp_lexer_peek_token (parser->lexer);
17158 if (token->type != CPP_OPEN_BRACE)
17159 return; /* No ivars specified. */
17161 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17162 token = cp_lexer_peek_token (parser->lexer);
17164 while (token->type != CPP_CLOSE_BRACE)
17166 cp_decl_specifier_seq declspecs;
17167 int decl_class_or_enum_p;
17168 tree prefix_attributes;
17170 cp_parser_objc_visibility_spec (parser);
17172 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17175 cp_parser_decl_specifier_seq (parser,
17176 CP_PARSER_FLAGS_OPTIONAL,
17178 &decl_class_or_enum_p);
17179 prefix_attributes = declspecs.attributes;
17180 declspecs.attributes = NULL_TREE;
17182 /* Keep going until we hit the `;' at the end of the
17184 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17186 tree width = NULL_TREE, attributes, first_attribute, decl;
17187 cp_declarator *declarator = NULL;
17188 int ctor_dtor_or_conv_p;
17190 /* Check for a (possibly unnamed) bitfield declaration. */
17191 token = cp_lexer_peek_token (parser->lexer);
17192 if (token->type == CPP_COLON)
17195 if (token->type == CPP_NAME
17196 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17199 /* Get the name of the bitfield. */
17200 declarator = make_id_declarator (NULL_TREE,
17201 cp_parser_identifier (parser),
17205 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17206 /* Get the width of the bitfield. */
17208 = cp_parser_constant_expression (parser,
17209 /*allow_non_constant=*/false,
17214 /* Parse the declarator. */
17216 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17217 &ctor_dtor_or_conv_p,
17218 /*parenthesized_p=*/NULL,
17219 /*member_p=*/false);
17222 /* Look for attributes that apply to the ivar. */
17223 attributes = cp_parser_attributes_opt (parser);
17224 /* Remember which attributes are prefix attributes and
17226 first_attribute = attributes;
17227 /* Combine the attributes. */
17228 attributes = chainon (prefix_attributes, attributes);
17232 /* Create the bitfield declaration. */
17233 decl = grokbitfield (declarator, &declspecs, width);
17234 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17237 decl = grokfield (declarator, &declspecs, NULL_TREE,
17238 NULL_TREE, attributes);
17240 /* Add the instance variable. */
17241 objc_add_instance_variable (decl);
17243 /* Reset PREFIX_ATTRIBUTES. */
17244 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17245 attributes = TREE_CHAIN (attributes);
17247 TREE_CHAIN (attributes) = NULL_TREE;
17249 token = cp_lexer_peek_token (parser->lexer);
17251 if (token->type == CPP_COMMA)
17253 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17259 cp_parser_consume_semicolon_at_end_of_statement (parser);
17260 token = cp_lexer_peek_token (parser->lexer);
17263 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17264 /* For historical reasons, we accept an optional semicolon. */
17265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17266 cp_lexer_consume_token (parser->lexer);
17269 /* Parse an Objective-C protocol declaration. */
17272 cp_parser_objc_protocol_declaration (cp_parser* parser)
17274 tree proto, protorefs;
17277 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17280 error ("identifier expected after %<@protocol%>");
17284 /* See if we have a forward declaration or a definition. */
17285 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17287 /* Try a forward declaration first. */
17288 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17290 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17292 cp_parser_consume_semicolon_at_end_of_statement (parser);
17295 /* Ok, we got a full-fledged definition (or at least should). */
17298 proto = cp_parser_identifier (parser);
17299 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17300 objc_start_protocol (proto, protorefs);
17301 cp_parser_objc_method_prototype_list (parser);
17305 /* Parse an Objective-C superclass or category. */
17308 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17311 cp_token *next = cp_lexer_peek_token (parser->lexer);
17313 *super = *categ = NULL_TREE;
17314 if (next->type == CPP_COLON)
17316 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17317 *super = cp_parser_identifier (parser);
17319 else if (next->type == CPP_OPEN_PAREN)
17321 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17322 *categ = cp_parser_identifier (parser);
17323 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17327 /* Parse an Objective-C class interface. */
17330 cp_parser_objc_class_interface (cp_parser* parser)
17332 tree name, super, categ, protos;
17334 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17335 name = cp_parser_identifier (parser);
17336 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17337 protos = cp_parser_objc_protocol_refs_opt (parser);
17339 /* We have either a class or a category on our hands. */
17341 objc_start_category_interface (name, categ, protos);
17344 objc_start_class_interface (name, super, protos);
17345 /* Handle instance variable declarations, if any. */
17346 cp_parser_objc_class_ivars (parser);
17347 objc_continue_interface ();
17350 cp_parser_objc_method_prototype_list (parser);
17353 /* Parse an Objective-C class implementation. */
17356 cp_parser_objc_class_implementation (cp_parser* parser)
17358 tree name, super, categ;
17360 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17361 name = cp_parser_identifier (parser);
17362 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17364 /* We have either a class or a category on our hands. */
17366 objc_start_category_implementation (name, categ);
17369 objc_start_class_implementation (name, super);
17370 /* Handle instance variable declarations, if any. */
17371 cp_parser_objc_class_ivars (parser);
17372 objc_continue_implementation ();
17375 cp_parser_objc_method_definition_list (parser);
17378 /* Consume the @end token and finish off the implementation. */
17381 cp_parser_objc_end_implementation (cp_parser* parser)
17383 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17384 objc_finish_implementation ();
17387 /* Parse an Objective-C declaration. */
17390 cp_parser_objc_declaration (cp_parser* parser)
17392 /* Try to figure out what kind of declaration is present. */
17393 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17395 switch (kwd->keyword)
17398 cp_parser_objc_alias_declaration (parser);
17401 cp_parser_objc_class_declaration (parser);
17403 case RID_AT_PROTOCOL:
17404 cp_parser_objc_protocol_declaration (parser);
17406 case RID_AT_INTERFACE:
17407 cp_parser_objc_class_interface (parser);
17409 case RID_AT_IMPLEMENTATION:
17410 cp_parser_objc_class_implementation (parser);
17413 cp_parser_objc_end_implementation (parser);
17416 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17417 cp_parser_skip_to_end_of_block_or_statement (parser);
17421 /* Parse an Objective-C try-catch-finally statement.
17423 objc-try-catch-finally-stmt:
17424 @try compound-statement objc-catch-clause-seq [opt]
17425 objc-finally-clause [opt]
17427 objc-catch-clause-seq:
17428 objc-catch-clause objc-catch-clause-seq [opt]
17431 @catch ( exception-declaration ) compound-statement
17433 objc-finally-clause
17434 @finally compound-statement
17436 Returns NULL_TREE. */
17439 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17440 location_t location;
17443 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17444 location = cp_lexer_peek_token (parser->lexer)->location;
17445 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17446 node, lest it get absorbed into the surrounding block. */
17447 stmt = push_stmt_list ();
17448 cp_parser_compound_statement (parser, NULL, false);
17449 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17451 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17453 cp_parameter_declarator *parmdecl;
17456 cp_lexer_consume_token (parser->lexer);
17457 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17458 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17459 parm = grokdeclarator (parmdecl->declarator,
17460 &parmdecl->decl_specifiers,
17461 PARM, /*initialized=*/0,
17462 /*attrlist=*/NULL);
17463 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17464 objc_begin_catch_clause (parm);
17465 cp_parser_compound_statement (parser, NULL, false);
17466 objc_finish_catch_clause ();
17469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17471 cp_lexer_consume_token (parser->lexer);
17472 location = cp_lexer_peek_token (parser->lexer)->location;
17473 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17474 node, lest it get absorbed into the surrounding block. */
17475 stmt = push_stmt_list ();
17476 cp_parser_compound_statement (parser, NULL, false);
17477 objc_build_finally_clause (location, pop_stmt_list (stmt));
17480 return objc_finish_try_stmt ();
17483 /* Parse an Objective-C synchronized statement.
17485 objc-synchronized-stmt:
17486 @synchronized ( expression ) compound-statement
17488 Returns NULL_TREE. */
17491 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17492 location_t location;
17495 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17497 location = cp_lexer_peek_token (parser->lexer)->location;
17498 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17499 lock = cp_parser_expression (parser, false);
17500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17502 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17503 node, lest it get absorbed into the surrounding block. */
17504 stmt = push_stmt_list ();
17505 cp_parser_compound_statement (parser, NULL, false);
17507 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17510 /* Parse an Objective-C throw statement.
17513 @throw assignment-expression [opt] ;
17515 Returns a constructed '@throw' statement. */
17518 cp_parser_objc_throw_statement (cp_parser *parser) {
17519 tree expr = NULL_TREE;
17521 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17524 expr = cp_parser_assignment_expression (parser, false);
17526 cp_parser_consume_semicolon_at_end_of_statement (parser);
17528 return objc_build_throw_stmt (expr);
17531 /* Parse an Objective-C statement. */
17534 cp_parser_objc_statement (cp_parser * parser) {
17535 /* Try to figure out what kind of declaration is present. */
17536 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17538 switch (kwd->keyword)
17541 return cp_parser_objc_try_catch_finally_statement (parser);
17542 case RID_AT_SYNCHRONIZED:
17543 return cp_parser_objc_synchronized_statement (parser);
17545 return cp_parser_objc_throw_statement (parser);
17547 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17548 cp_parser_skip_to_end_of_block_or_statement (parser);
17551 return error_mark_node;
17555 static GTY (()) cp_parser *the_parser;
17558 /* Special handling for the first token or line in the file. The first
17559 thing in the file might be #pragma GCC pch_preprocess, which loads a
17560 PCH file, which is a GC collection point. So we need to handle this
17561 first pragma without benefit of an existing lexer structure.
17563 Always returns one token to the caller in *FIRST_TOKEN. This is
17564 either the true first token of the file, or the first token after
17565 the initial pragma. */
17568 cp_parser_initial_pragma (cp_token *first_token)
17572 cp_lexer_get_preprocessor_token (NULL, first_token);
17573 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
17576 cp_lexer_get_preprocessor_token (NULL, first_token);
17577 if (first_token->type == CPP_STRING)
17579 name = first_token->value;
17581 cp_lexer_get_preprocessor_token (NULL, first_token);
17582 if (first_token->type != CPP_PRAGMA_EOL)
17583 error ("junk at end of %<#pragma GCC pch_preprocess%>");
17586 error ("expected string literal");
17588 /* Skip to the end of the pragma. */
17589 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
17590 cp_lexer_get_preprocessor_token (NULL, first_token);
17592 /* Read one more token to return to our caller. */
17593 cp_lexer_get_preprocessor_token (NULL, first_token);
17595 /* Now actually load the PCH file. */
17597 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
17600 /* Normal parsing of a pragma token. Here we can (and must) use the
17604 cp_parser_pragma (cp_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
17606 cp_token *pragma_tok;
17609 pragma_tok = cp_lexer_consume_token (parser->lexer);
17610 gcc_assert (pragma_tok->type == CPP_PRAGMA);
17611 parser->lexer->in_pragma = true;
17613 id = pragma_tok->pragma_kind;
17616 case PRAGMA_GCC_PCH_PREPROCESS:
17617 error ("%<#pragma GCC pch_preprocess%> must be first");
17621 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
17622 c_invoke_pragma_handler (id);
17626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
17630 /* The interface the pragma parsers have to the lexer. */
17633 pragma_lex (tree *value)
17636 enum cpp_ttype ret;
17638 tok = cp_lexer_peek_token (the_parser->lexer);
17641 *value = tok->value;
17643 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
17645 else if (ret == CPP_STRING)
17646 *value = cp_parser_string_literal (the_parser, false, false);
17649 cp_lexer_consume_token (the_parser->lexer);
17650 if (ret == CPP_KEYWORD)
17658 /* External interface. */
17660 /* Parse one entire translation unit. */
17663 c_parse_file (void)
17665 bool error_occurred;
17666 static bool already_called = false;
17668 if (already_called)
17670 sorry ("inter-module optimizations not implemented for C++");
17673 already_called = true;
17675 the_parser = cp_parser_new ();
17676 push_deferring_access_checks (flag_access_control
17677 ? dk_no_deferred : dk_no_check);
17678 error_occurred = cp_parser_translation_unit (the_parser);
17682 /* This variable must be provided by every front end. */
17686 #include "gt-cp-parser.h"