2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
50 typedef struct cp_token GTY (())
52 /* The kind of token. */
53 ENUM_BITFIELD (cpp_ttype) type : 8;
54 /* If this token is a keyword, this value indicates which keyword.
55 Otherwise, this value is RID_MAX. */
56 ENUM_BITFIELD (rid) keyword : 8;
59 /* Identifier for the pragma. */
60 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
61 /* True if this token is from a system header. */
62 BOOL_BITFIELD in_system_header : 1;
63 /* True if this token is from a context where it is implicitly extern "C" */
64 BOOL_BITFIELD implicit_extern_c : 1;
65 /* True for a CPP_NAME token that is not a keyword (i.e., for which
66 KEYWORD is RID_MAX) iff this name was looked up and found to be
67 ambiguous. An error has already been reported. */
68 BOOL_BITFIELD ambiguous_p : 1;
69 /* The value associated with this token, if any. */
71 /* The location at which this token was found. */
75 /* We use a stack of token pointer for saving token sets. */
76 typedef struct cp_token *cp_token_position;
77 DEF_VEC_P (cp_token_position);
78 DEF_VEC_ALLOC_P (cp_token_position,heap);
80 static const cp_token eof_token =
82 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, NULL_TREE,
83 #if USE_MAPPED_LOCATION
90 /* The cp_lexer structure represents the C++ lexer. It is responsible
91 for managing the token stream from the preprocessor and supplying
92 it to the parser. Tokens are never added to the cp_lexer after
95 typedef struct cp_lexer GTY (())
97 /* The memory allocated for the buffer. NULL if this lexer does not
98 own the token buffer. */
99 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
100 /* If the lexer owns the buffer, this is the number of tokens in the
102 size_t buffer_length;
104 /* A pointer just past the last available token. The tokens
105 in this lexer are [buffer, last_token). */
106 cp_token_position GTY ((skip)) last_token;
108 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
109 no more available tokens. */
110 cp_token_position GTY ((skip)) next_token;
112 /* A stack indicating positions at which cp_lexer_save_tokens was
113 called. The top entry is the most recent position at which we
114 began saving tokens. If the stack is non-empty, we are saving
116 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
118 /* The next lexer in a linked list of lexers. */
119 struct cp_lexer *next;
121 /* True if we should output debugging information. */
124 /* True if we're in the context of parsing a pragma, and should not
125 increment past the end-of-line marker. */
129 /* cp_token_cache is a range of tokens. There is no need to represent
130 allocate heap memory for it, since tokens are never removed from the
131 lexer's array. There is also no need for the GC to walk through
132 a cp_token_cache, since everything in here is referenced through
135 typedef struct cp_token_cache GTY(())
137 /* The beginning of the token range. */
138 cp_token * GTY((skip)) first;
140 /* Points immediately after the last token in the range. */
141 cp_token * GTY ((skip)) last;
146 static cp_lexer *cp_lexer_new_main
148 static cp_lexer *cp_lexer_new_from_tokens
149 (cp_token_cache *tokens);
150 static void cp_lexer_destroy
152 static int cp_lexer_saving_tokens
154 static cp_token_position cp_lexer_token_position
156 static cp_token *cp_lexer_token_at
157 (cp_lexer *, cp_token_position);
158 static void cp_lexer_get_preprocessor_token
159 (cp_lexer *, cp_token *);
160 static inline cp_token *cp_lexer_peek_token
162 static cp_token *cp_lexer_peek_nth_token
163 (cp_lexer *, size_t);
164 static inline bool cp_lexer_next_token_is
165 (cp_lexer *, enum cpp_ttype);
166 static bool cp_lexer_next_token_is_not
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_keyword
169 (cp_lexer *, enum rid);
170 static cp_token *cp_lexer_consume_token
172 static void cp_lexer_purge_token
174 static void cp_lexer_purge_tokens_after
175 (cp_lexer *, cp_token_position);
176 static void cp_lexer_save_tokens
178 static void cp_lexer_commit_tokens
180 static void cp_lexer_rollback_tokens
182 #ifdef ENABLE_CHECKING
183 static void cp_lexer_print_token
184 (FILE *, cp_token *);
185 static inline bool cp_lexer_debugging_p
187 static void cp_lexer_start_debugging
188 (cp_lexer *) ATTRIBUTE_UNUSED;
189 static void cp_lexer_stop_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
192 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
193 about passing NULL to functions that require non-NULL arguments
194 (fputs, fprintf). It will never be used, so all we need is a value
195 of the right type that's guaranteed not to be NULL. */
196 #define cp_lexer_debug_stream stdout
197 #define cp_lexer_print_token(str, tok) (void) 0
198 #define cp_lexer_debugging_p(lexer) 0
199 #endif /* ENABLE_CHECKING */
201 static cp_token_cache *cp_token_cache_new
202 (cp_token *, cp_token *);
204 static void cp_parser_initial_pragma
207 /* Manifest constants. */
208 #define CP_LEXER_BUFFER_SIZE 10000
209 #define CP_SAVED_TOKEN_STACK 5
211 /* A token type for keywords, as opposed to ordinary identifiers. */
212 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
214 /* A token type for template-ids. If a template-id is processed while
215 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
216 the value of the CPP_TEMPLATE_ID is whatever was returned by
217 cp_parser_template_id. */
218 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
220 /* A token type for nested-name-specifiers. If a
221 nested-name-specifier is processed while parsing tentatively, it is
222 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
223 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
224 cp_parser_nested_name_specifier_opt. */
225 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
227 /* A token type for tokens that are not tokens at all; these are used
228 to represent slots in the array where there used to be a token
229 that has now been deleted. */
230 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
232 /* The number of token types, including C++-specific ones. */
233 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
237 #ifdef ENABLE_CHECKING
238 /* The stream to which debugging output should be written. */
239 static FILE *cp_lexer_debug_stream;
240 #endif /* ENABLE_CHECKING */
242 /* Create a new main C++ lexer, the lexer that gets tokens from the
246 cp_lexer_new_main (void)
248 cp_token first_token;
255 /* It's possible that parsing the first pragma will load a PCH file,
256 which is a GC collection point. So we have to do that before
257 allocating any memory. */
258 cp_parser_initial_pragma (&first_token);
260 /* Tell c_lex_with_flags not to merge string constants. */
261 c_lex_return_raw_strings = true;
263 c_common_no_more_pch ();
265 /* Allocate the memory. */
266 lexer = GGC_CNEW (cp_lexer);
268 #ifdef ENABLE_CHECKING
269 /* Initially we are not debugging. */
270 lexer->debugging_p = false;
271 #endif /* ENABLE_CHECKING */
272 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
273 CP_SAVED_TOKEN_STACK);
275 /* Create the buffer. */
276 alloc = CP_LEXER_BUFFER_SIZE;
277 buffer = GGC_NEWVEC (cp_token, alloc);
279 /* Put the first token in the buffer. */
284 /* Get the remaining tokens from the preprocessor. */
285 while (pos->type != CPP_EOF)
292 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
293 pos = buffer + space;
295 cp_lexer_get_preprocessor_token (lexer, pos);
297 lexer->buffer = buffer;
298 lexer->buffer_length = alloc - space;
299 lexer->last_token = pos;
300 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
302 /* Subsequent preprocessor diagnostics should use compiler
303 diagnostic functions to get the compiler source location. */
304 cpp_get_options (parse_in)->client_diagnostic = true;
305 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
307 gcc_assert (lexer->next_token->type != CPP_PURGED);
311 /* Create a new lexer whose token stream is primed with the tokens in
312 CACHE. When these tokens are exhausted, no new tokens will be read. */
315 cp_lexer_new_from_tokens (cp_token_cache *cache)
317 cp_token *first = cache->first;
318 cp_token *last = cache->last;
319 cp_lexer *lexer = GGC_CNEW (cp_lexer);
321 /* We do not own the buffer. */
322 lexer->buffer = NULL;
323 lexer->buffer_length = 0;
324 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
325 lexer->last_token = last;
327 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
328 CP_SAVED_TOKEN_STACK);
330 #ifdef ENABLE_CHECKING
331 /* Initially we are not debugging. */
332 lexer->debugging_p = false;
335 gcc_assert (lexer->next_token->type != CPP_PURGED);
339 /* Frees all resources associated with LEXER. */
342 cp_lexer_destroy (cp_lexer *lexer)
345 ggc_free (lexer->buffer);
346 VEC_free (cp_token_position, heap, lexer->saved_tokens);
350 /* Returns nonzero if debugging information should be output. */
352 #ifdef ENABLE_CHECKING
355 cp_lexer_debugging_p (cp_lexer *lexer)
357 return lexer->debugging_p;
360 #endif /* ENABLE_CHECKING */
362 static inline cp_token_position
363 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
365 gcc_assert (!previous_p || lexer->next_token != &eof_token);
367 return lexer->next_token - previous_p;
370 static inline cp_token *
371 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
376 /* nonzero if we are presently saving tokens. */
379 cp_lexer_saving_tokens (const cp_lexer* lexer)
381 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
384 /* Store the next token from the preprocessor in *TOKEN. Return true
388 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
391 static int is_extern_c = 0;
393 /* Get a new token from the preprocessor. */
395 = c_lex_with_flags (&token->value, &token->location, &token->flags);
396 token->keyword = RID_MAX;
397 token->pragma_kind = PRAGMA_NONE;
398 token->in_system_header = in_system_header;
400 /* On some systems, some header files are surrounded by an
401 implicit extern "C" block. Set a flag in the token if it
402 comes from such a header. */
403 is_extern_c += pending_lang_change;
404 pending_lang_change = 0;
405 token->implicit_extern_c = is_extern_c > 0;
407 /* Check to see if this token is a keyword. */
408 if (token->type == CPP_NAME)
410 if (C_IS_RESERVED_WORD (token->value))
412 /* Mark this token as a keyword. */
413 token->type = CPP_KEYWORD;
414 /* Record which keyword. */
415 token->keyword = C_RID_CODE (token->value);
416 /* Update the value. Some keywords are mapped to particular
417 entities, rather than simply having the value of the
418 corresponding IDENTIFIER_NODE. For example, `__const' is
419 mapped to `const'. */
420 token->value = ridpointers[token->keyword];
424 token->ambiguous_p = false;
425 token->keyword = RID_MAX;
428 /* Handle Objective-C++ keywords. */
429 else if (token->type == CPP_AT_NAME)
431 token->type = CPP_KEYWORD;
432 switch (C_RID_CODE (token->value))
434 /* Map 'class' to '@class', 'private' to '@private', etc. */
435 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
436 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
437 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
438 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
439 case RID_THROW: token->keyword = RID_AT_THROW; break;
440 case RID_TRY: token->keyword = RID_AT_TRY; break;
441 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
442 default: token->keyword = C_RID_CODE (token->value);
445 else if (token->type == CPP_PRAGMA)
447 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
448 token->pragma_kind = TREE_INT_CST_LOW (token->value);
453 /* Update the globals input_location and in_system_header from TOKEN. */
455 cp_lexer_set_source_position_from_token (cp_token *token)
457 if (token->type != CPP_EOF)
459 input_location = token->location;
460 in_system_header = token->in_system_header;
464 /* Return a pointer to the next token in the token stream, but do not
467 static inline cp_token *
468 cp_lexer_peek_token (cp_lexer *lexer)
470 if (cp_lexer_debugging_p (lexer))
472 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
473 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
474 putc ('\n', cp_lexer_debug_stream);
476 return lexer->next_token;
479 /* Return true if the next token has the indicated TYPE. */
482 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
484 return cp_lexer_peek_token (lexer)->type == type;
487 /* Return true if the next token does not have the indicated TYPE. */
490 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
492 return !cp_lexer_next_token_is (lexer, type);
495 /* Return true if the next token is the indicated KEYWORD. */
498 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
500 return cp_lexer_peek_token (lexer)->keyword == keyword;
503 /* Return a pointer to the Nth token in the token stream. If N is 1,
504 then this is precisely equivalent to cp_lexer_peek_token (except
505 that it is not inline). One would like to disallow that case, but
506 there is one case (cp_parser_nth_token_starts_template_id) where
507 the caller passes a variable for N and it might be 1. */
510 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
514 /* N is 1-based, not zero-based. */
517 if (cp_lexer_debugging_p (lexer))
518 fprintf (cp_lexer_debug_stream,
519 "cp_lexer: peeking ahead %ld at token: ", (long)n);
522 token = lexer->next_token;
523 gcc_assert (!n || token != &eof_token);
527 if (token == lexer->last_token)
529 token = (cp_token *)&eof_token;
533 if (token->type != CPP_PURGED)
537 if (cp_lexer_debugging_p (lexer))
539 cp_lexer_print_token (cp_lexer_debug_stream, token);
540 putc ('\n', cp_lexer_debug_stream);
546 /* Return the next token, and advance the lexer's next_token pointer
547 to point to the next non-purged token. */
550 cp_lexer_consume_token (cp_lexer* lexer)
552 cp_token *token = lexer->next_token;
554 gcc_assert (token != &eof_token);
555 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
560 if (lexer->next_token == lexer->last_token)
562 lexer->next_token = (cp_token *)&eof_token;
567 while (lexer->next_token->type == CPP_PURGED);
569 cp_lexer_set_source_position_from_token (token);
571 /* Provide debugging output. */
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, token);
576 putc ('\n', cp_lexer_debug_stream);
582 /* Permanently remove the next token from the token stream, and
583 advance the next_token pointer to refer to the next non-purged
587 cp_lexer_purge_token (cp_lexer *lexer)
589 cp_token *tok = lexer->next_token;
591 gcc_assert (tok != &eof_token);
592 tok->type = CPP_PURGED;
593 tok->location = UNKNOWN_LOCATION;
594 tok->value = NULL_TREE;
595 tok->keyword = RID_MAX;
600 if (tok == lexer->last_token)
602 tok = (cp_token *)&eof_token;
606 while (tok->type == CPP_PURGED);
607 lexer->next_token = tok;
610 /* Permanently remove all tokens after TOK, up to, but not
611 including, the token that will be returned next by
612 cp_lexer_peek_token. */
615 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
617 cp_token *peek = lexer->next_token;
619 if (peek == &eof_token)
620 peek = lexer->last_token;
622 gcc_assert (tok < peek);
624 for ( tok += 1; tok != peek; tok += 1)
626 tok->type = CPP_PURGED;
627 tok->location = UNKNOWN_LOCATION;
628 tok->value = NULL_TREE;
629 tok->keyword = RID_MAX;
633 /* Begin saving tokens. All tokens consumed after this point will be
637 cp_lexer_save_tokens (cp_lexer* lexer)
639 /* Provide debugging output. */
640 if (cp_lexer_debugging_p (lexer))
641 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
643 VEC_safe_push (cp_token_position, heap,
644 lexer->saved_tokens, lexer->next_token);
647 /* Commit to the portion of the token stream most recently saved. */
650 cp_lexer_commit_tokens (cp_lexer* lexer)
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
656 VEC_pop (cp_token_position, lexer->saved_tokens);
659 /* Return all tokens saved since the last call to cp_lexer_save_tokens
660 to the token stream. Stop saving tokens. */
663 cp_lexer_rollback_tokens (cp_lexer* lexer)
665 /* Provide debugging output. */
666 if (cp_lexer_debugging_p (lexer))
667 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
669 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 /* Print a representation of the TOKEN on the STREAM. */
674 #ifdef ENABLE_CHECKING
677 cp_lexer_print_token (FILE * stream, cp_token *token)
679 /* We don't use cpp_type2name here because the parser defines
680 a few tokens of its own. */
681 static const char *const token_names[] = {
682 /* cpplib-defined token types */
688 /* C++ parser token types - see "Manifest constants", above. */
691 "NESTED_NAME_SPECIFIER",
695 /* If we have a name for the token, print it out. Otherwise, we
696 simply give the numeric code. */
697 gcc_assert (token->type < ARRAY_SIZE(token_names));
698 fputs (token_names[token->type], stream);
700 /* For some tokens, print the associated data. */
704 /* Some keywords have a value that is not an IDENTIFIER_NODE.
705 For example, `struct' is mapped to an INTEGER_CST. */
706 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
708 /* else fall through */
710 fputs (IDENTIFIER_POINTER (token->value), stream);
715 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
723 /* Start emitting debugging information. */
726 cp_lexer_start_debugging (cp_lexer* lexer)
728 lexer->debugging_p = true;
731 /* Stop emitting debugging information. */
734 cp_lexer_stop_debugging (cp_lexer* lexer)
736 lexer->debugging_p = false;
739 #endif /* ENABLE_CHECKING */
741 /* Create a new cp_token_cache, representing a range of tokens. */
743 static cp_token_cache *
744 cp_token_cache_new (cp_token *first, cp_token *last)
746 cp_token_cache *cache = GGC_NEW (cp_token_cache);
747 cache->first = first;
753 /* Decl-specifiers. */
755 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
758 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
760 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
765 /* Nothing other than the parser should be creating declarators;
766 declarators are a semi-syntactic representation of C++ entities.
767 Other parts of the front end that need to create entities (like
768 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
770 static cp_declarator *make_call_declarator
771 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
772 static cp_declarator *make_array_declarator
773 (cp_declarator *, tree);
774 static cp_declarator *make_pointer_declarator
775 (cp_cv_quals, cp_declarator *);
776 static cp_declarator *make_reference_declarator
777 (cp_cv_quals, cp_declarator *);
778 static cp_parameter_declarator *make_parameter_declarator
779 (cp_decl_specifier_seq *, cp_declarator *, tree);
780 static cp_declarator *make_ptrmem_declarator
781 (cp_cv_quals, tree, cp_declarator *);
783 /* An erroneous declarator. */
784 static cp_declarator *cp_error_declarator;
786 /* The obstack on which declarators and related data structures are
788 static struct obstack declarator_obstack;
790 /* Alloc BYTES from the declarator memory pool. */
793 alloc_declarator (size_t bytes)
795 return obstack_alloc (&declarator_obstack, bytes);
798 /* Allocate a declarator of the indicated KIND. Clear fields that are
799 common to all declarators. */
801 static cp_declarator *
802 make_declarator (cp_declarator_kind kind)
804 cp_declarator *declarator;
806 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
807 declarator->kind = kind;
808 declarator->attributes = NULL_TREE;
809 declarator->declarator = NULL;
814 /* Make a declarator for a generalized identifier. If
815 QUALIFYING_SCOPE is non-NULL, the identifier is
816 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
817 UNQUALIFIED_NAME. SFK indicates the kind of special function this
820 static cp_declarator *
821 make_id_declarator (tree qualifying_scope, tree unqualified_name,
822 special_function_kind sfk)
824 cp_declarator *declarator;
826 /* It is valid to write:
828 class C { void f(); };
832 The standard is not clear about whether `typedef const C D' is
833 legal; as of 2002-09-15 the committee is considering that
834 question. EDG 3.0 allows that syntax. Therefore, we do as
836 if (qualifying_scope && TYPE_P (qualifying_scope))
837 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
839 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
840 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
841 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
843 declarator = make_declarator (cdk_id);
844 declarator->u.id.qualifying_scope = qualifying_scope;
845 declarator->u.id.unqualified_name = unqualified_name;
846 declarator->u.id.sfk = sfk;
851 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
852 of modifiers such as const or volatile to apply to the pointer
853 type, represented as identifiers. */
856 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
858 cp_declarator *declarator;
860 declarator = make_declarator (cdk_pointer);
861 declarator->declarator = target;
862 declarator->u.pointer.qualifiers = cv_qualifiers;
863 declarator->u.pointer.class_type = NULL_TREE;
868 /* Like make_pointer_declarator -- but for references. */
871 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
873 cp_declarator *declarator;
875 declarator = make_declarator (cdk_reference);
876 declarator->declarator = target;
877 declarator->u.pointer.qualifiers = cv_qualifiers;
878 declarator->u.pointer.class_type = NULL_TREE;
883 /* Like make_pointer_declarator -- but for a pointer to a non-static
884 member of CLASS_TYPE. */
887 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
888 cp_declarator *pointee)
890 cp_declarator *declarator;
892 declarator = make_declarator (cdk_ptrmem);
893 declarator->declarator = pointee;
894 declarator->u.pointer.qualifiers = cv_qualifiers;
895 declarator->u.pointer.class_type = class_type;
900 /* Make a declarator for the function given by TARGET, with the
901 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
902 "const"-qualified member function. The EXCEPTION_SPECIFICATION
903 indicates what exceptions can be thrown. */
906 make_call_declarator (cp_declarator *target,
907 cp_parameter_declarator *parms,
908 cp_cv_quals cv_qualifiers,
909 tree exception_specification)
911 cp_declarator *declarator;
913 declarator = make_declarator (cdk_function);
914 declarator->declarator = target;
915 declarator->u.function.parameters = parms;
916 declarator->u.function.qualifiers = cv_qualifiers;
917 declarator->u.function.exception_specification = exception_specification;
922 /* Make a declarator for an array of BOUNDS elements, each of which is
923 defined by ELEMENT. */
926 make_array_declarator (cp_declarator *element, tree bounds)
928 cp_declarator *declarator;
930 declarator = make_declarator (cdk_array);
931 declarator->declarator = element;
932 declarator->u.array.bounds = bounds;
937 cp_parameter_declarator *no_parameters;
939 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
940 DECLARATOR and DEFAULT_ARGUMENT. */
942 cp_parameter_declarator *
943 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
944 cp_declarator *declarator,
945 tree default_argument)
947 cp_parameter_declarator *parameter;
949 parameter = ((cp_parameter_declarator *)
950 alloc_declarator (sizeof (cp_parameter_declarator)));
951 parameter->next = NULL;
953 parameter->decl_specifiers = *decl_specifiers;
955 clear_decl_specs (¶meter->decl_specifiers);
956 parameter->declarator = declarator;
957 parameter->default_argument = default_argument;
958 parameter->ellipsis_p = false;
968 A cp_parser parses the token stream as specified by the C++
969 grammar. Its job is purely parsing, not semantic analysis. For
970 example, the parser breaks the token stream into declarators,
971 expressions, statements, and other similar syntactic constructs.
972 It does not check that the types of the expressions on either side
973 of an assignment-statement are compatible, or that a function is
974 not declared with a parameter of type `void'.
976 The parser invokes routines elsewhere in the compiler to perform
977 semantic analysis and to build up the abstract syntax tree for the
980 The parser (and the template instantiation code, which is, in a
981 way, a close relative of parsing) are the only parts of the
982 compiler that should be calling push_scope and pop_scope, or
983 related functions. The parser (and template instantiation code)
984 keeps track of what scope is presently active; everything else
985 should simply honor that. (The code that generates static
986 initializers may also need to set the scope, in order to check
987 access control correctly when emitting the initializers.)
992 The parser is of the standard recursive-descent variety. Upcoming
993 tokens in the token stream are examined in order to determine which
994 production to use when parsing a non-terminal. Some C++ constructs
995 require arbitrary look ahead to disambiguate. For example, it is
996 impossible, in the general case, to tell whether a statement is an
997 expression or declaration without scanning the entire statement.
998 Therefore, the parser is capable of "parsing tentatively." When the
999 parser is not sure what construct comes next, it enters this mode.
1000 Then, while we attempt to parse the construct, the parser queues up
1001 error messages, rather than issuing them immediately, and saves the
1002 tokens it consumes. If the construct is parsed successfully, the
1003 parser "commits", i.e., it issues any queued error messages and
1004 the tokens that were being preserved are permanently discarded.
1005 If, however, the construct is not parsed successfully, the parser
1006 rolls back its state completely so that it can resume parsing using
1007 a different alternative.
1012 The performance of the parser could probably be improved substantially.
1013 We could often eliminate the need to parse tentatively by looking ahead
1014 a little bit. In some places, this approach might not entirely eliminate
1015 the need to parse tentatively, but it might still speed up the average
1018 /* Flags that are passed to some parsing functions. These values can
1019 be bitwise-ored together. */
1021 typedef enum cp_parser_flags
1024 CP_PARSER_FLAGS_NONE = 0x0,
1025 /* The construct is optional. If it is not present, then no error
1026 should be issued. */
1027 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1028 /* When parsing a type-specifier, do not allow user-defined types. */
1029 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1032 /* The different kinds of declarators we want to parse. */
1034 typedef enum cp_parser_declarator_kind
1036 /* We want an abstract declarator. */
1037 CP_PARSER_DECLARATOR_ABSTRACT,
1038 /* We want a named declarator. */
1039 CP_PARSER_DECLARATOR_NAMED,
1040 /* We don't mind, but the name must be an unqualified-id. */
1041 CP_PARSER_DECLARATOR_EITHER
1042 } cp_parser_declarator_kind;
1044 /* The precedence values used to parse binary expressions. The minimum value
1045 of PREC must be 1, because zero is reserved to quickly discriminate
1046 binary operators from other tokens. */
1051 PREC_LOGICAL_OR_EXPRESSION,
1052 PREC_LOGICAL_AND_EXPRESSION,
1053 PREC_INCLUSIVE_OR_EXPRESSION,
1054 PREC_EXCLUSIVE_OR_EXPRESSION,
1055 PREC_AND_EXPRESSION,
1056 PREC_EQUALITY_EXPRESSION,
1057 PREC_RELATIONAL_EXPRESSION,
1058 PREC_SHIFT_EXPRESSION,
1059 PREC_ADDITIVE_EXPRESSION,
1060 PREC_MULTIPLICATIVE_EXPRESSION,
1062 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1065 /* A mapping from a token type to a corresponding tree node type, with a
1066 precedence value. */
1068 typedef struct cp_parser_binary_operations_map_node
1070 /* The token type. */
1071 enum cpp_ttype token_type;
1072 /* The corresponding tree code. */
1073 enum tree_code tree_type;
1074 /* The precedence of this operator. */
1075 enum cp_parser_prec prec;
1076 } cp_parser_binary_operations_map_node;
1078 /* The status of a tentative parse. */
1080 typedef enum cp_parser_status_kind
1082 /* No errors have occurred. */
1083 CP_PARSER_STATUS_KIND_NO_ERROR,
1084 /* An error has occurred. */
1085 CP_PARSER_STATUS_KIND_ERROR,
1086 /* We are committed to this tentative parse, whether or not an error
1088 CP_PARSER_STATUS_KIND_COMMITTED
1089 } cp_parser_status_kind;
1091 typedef struct cp_parser_expression_stack_entry
1094 enum tree_code tree_type;
1096 } cp_parser_expression_stack_entry;
1098 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1099 entries because precedence levels on the stack are monotonically
1101 typedef struct cp_parser_expression_stack_entry
1102 cp_parser_expression_stack[NUM_PREC_VALUES];
1104 /* Context that is saved and restored when parsing tentatively. */
1105 typedef struct cp_parser_context GTY (())
1107 /* If this is a tentative parsing context, the status of the
1109 enum cp_parser_status_kind status;
1110 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1111 that are looked up in this context must be looked up both in the
1112 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1113 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1122 /* Constructors and destructors. */
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1127 /* Class variables. */
1129 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1131 /* The operator-precedence table used by cp_parser_binary_expression.
1132 Transformed into an associative array (binops_by_token) by
1135 static const cp_parser_binary_operations_map_node binops[] = {
1136 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1137 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1139 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1141 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1143 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1144 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1146 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1147 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1149 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1150 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1151 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1152 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1153 { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1154 { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1159 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1161 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1163 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1165 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1167 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1170 /* The same as binops, but initialized by cp_parser_new so that
1171 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1173 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1175 /* Constructors and destructors. */
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1180 static cp_parser_context *
1181 cp_parser_context_new (cp_parser_context* next)
1183 cp_parser_context *context;
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list != NULL)
1188 /* Pull the first entry from the free list. */
1189 context = cp_parser_context_free_list;
1190 cp_parser_context_free_list = context->next;
1191 memset (context, 0, sizeof (*context));
1194 context = GGC_CNEW (cp_parser_context);
1196 /* No errors have occurred yet in this context. */
1197 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context->object_type = next->object_type;
1205 /* Thread the stack. */
1206 context->next = next;
1212 /* The cp_parser structure represents the C++ parser. */
1214 typedef struct cp_parser GTY(())
1216 /* The lexer from which we are obtaining tokens. */
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look. It can
1223 also be ERROR_MARK, when we've parsed a bogus scope.
1225 This value is not cleared automatically after a name is looked
1226 up, so we must be careful to clear it before starting a new look
1227 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1228 will look up `Z' in the scope of `X', rather than the current
1229 scope.) Unfortunately, it is difficult to tell when name lookup
1230 is complete, because we sometimes peek at a token, look it up,
1231 and then decide not to consume it. */
1234 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1235 last lookup took place. OBJECT_SCOPE is used if an expression
1236 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1237 respectively. QUALIFYING_SCOPE is used for an expression of the
1238 form "X::Y"; it refers to X. */
1240 tree qualifying_scope;
1242 /* A stack of parsing contexts. All but the bottom entry on the
1243 stack will be tentative contexts.
1245 We parse tentatively in order to determine which construct is in
1246 use in some situations. For example, in order to determine
1247 whether a statement is an expression-statement or a
1248 declaration-statement we parse it tentatively as a
1249 declaration-statement. If that fails, we then reparse the same
1250 token stream as an expression-statement. */
1251 cp_parser_context *context;
1253 /* True if we are parsing GNU C++. If this flag is not set, then
1254 GNU extensions are not recognized. */
1255 bool allow_gnu_extensions_p;
1257 /* TRUE if the `>' token should be interpreted as the greater-than
1258 operator. FALSE if it is the end of a template-id or
1259 template-parameter-list. */
1260 bool greater_than_is_operator_p;
1262 /* TRUE if default arguments are allowed within a parameter list
1263 that starts at this point. FALSE if only a gnu extension makes
1264 them permissible. */
1265 bool default_arg_ok_p;
1267 /* TRUE if we are parsing an integral constant-expression. See
1268 [expr.const] for a precise definition. */
1269 bool integral_constant_expression_p;
1271 /* TRUE if we are parsing an integral constant-expression -- but a
1272 non-constant expression should be permitted as well. This flag
1273 is used when parsing an array bound so that GNU variable-length
1274 arrays are tolerated. */
1275 bool allow_non_integral_constant_expression_p;
1277 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1278 been seen that makes the expression non-constant. */
1279 bool non_integral_constant_expression_p;
1281 /* TRUE if local variable names and `this' are forbidden in the
1283 bool local_variables_forbidden_p;
1285 /* TRUE if the declaration we are parsing is part of a
1286 linkage-specification of the form `extern string-literal
1288 bool in_unbraced_linkage_specification_p;
1290 /* TRUE if we are presently parsing a declarator, after the
1291 direct-declarator. */
1292 bool in_declarator_p;
1294 /* TRUE if we are presently parsing a template-argument-list. */
1295 bool in_template_argument_list_p;
1297 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1298 to IN_OMP_BLOCK if parsing OpenMP structured block and
1299 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1300 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1301 iteration-statement, OpenMP block or loop within that switch. */
1302 #define IN_SWITCH_STMT 1
1303 #define IN_ITERATION_STMT 2
1304 #define IN_OMP_BLOCK 4
1305 #define IN_OMP_FOR 8
1306 unsigned char in_statement;
1308 /* TRUE if we are presently parsing the body of a switch statement.
1309 Note that this doesn't quite overlap with in_statement above.
1310 The difference relates to giving the right sets of error messages:
1311 "case not in switch" vs "break statement used with OpenMP...". */
1312 bool in_switch_statement_p;
1314 /* TRUE if we are parsing a type-id in an expression context. In
1315 such a situation, both "type (expr)" and "type (type)" are valid
1317 bool in_type_id_in_expr_p;
1319 /* TRUE if we are currently in a header file where declarations are
1320 implicitly extern "C". */
1321 bool implicit_extern_c;
1323 /* TRUE if strings in expressions should be translated to the execution
1325 bool translate_strings_p;
1327 /* If non-NULL, then we are parsing a construct where new type
1328 definitions are not permitted. The string stored here will be
1329 issued as an error message if a type is defined. */
1330 const char *type_definition_forbidden_message;
1332 /* A list of lists. The outer list is a stack, used for member
1333 functions of local classes. At each level there are two sub-list,
1334 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1335 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1336 TREE_VALUE's. The functions are chained in reverse declaration
1339 The TREE_PURPOSE sublist contains those functions with default
1340 arguments that need post processing, and the TREE_VALUE sublist
1341 contains those functions with definitions that need post
1344 These lists can only be processed once the outermost class being
1345 defined is complete. */
1346 tree unparsed_functions_queues;
1348 /* The number of classes whose definitions are currently in
1350 unsigned num_classes_being_defined;
1352 /* The number of template parameter lists that apply directly to the
1353 current declaration. */
1354 unsigned num_template_parameter_lists;
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, bool);
1394 static tree cp_parser_unqualified_id
1395 (cp_parser *, bool, 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 *, tree, 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
1540 (cp_parser *, bool);
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 void cp_parser_perform_template_parameter_access_checks
1731 static tree cp_parser_single_declaration
1732 (cp_parser *, tree, bool, bool *);
1733 static tree cp_parser_functional_cast
1734 (cp_parser *, tree);
1735 static tree cp_parser_save_member_function_body
1736 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1737 static tree cp_parser_enclosed_template_argument_list
1739 static void cp_parser_save_default_args
1740 (cp_parser *, tree);
1741 static void cp_parser_late_parsing_for_member
1742 (cp_parser *, tree);
1743 static void cp_parser_late_parsing_default_args
1744 (cp_parser *, tree);
1745 static tree cp_parser_sizeof_operand
1746 (cp_parser *, enum rid);
1747 static bool cp_parser_declares_only_class_p
1749 static void cp_parser_set_storage_class
1750 (cp_decl_specifier_seq *, cp_storage_class);
1751 static void cp_parser_set_decl_spec_type
1752 (cp_decl_specifier_seq *, tree, bool);
1753 static bool cp_parser_friend_p
1754 (const cp_decl_specifier_seq *);
1755 static cp_token *cp_parser_require
1756 (cp_parser *, enum cpp_ttype, const char *);
1757 static cp_token *cp_parser_require_keyword
1758 (cp_parser *, enum rid, const char *);
1759 static bool cp_parser_token_starts_function_definition_p
1761 static bool cp_parser_next_token_starts_class_definition_p
1763 static bool cp_parser_next_token_ends_template_argument_p
1765 static bool cp_parser_nth_token_starts_template_argument_list_p
1766 (cp_parser *, size_t);
1767 static enum tag_types cp_parser_token_is_class_key
1769 static void cp_parser_check_class_key
1770 (enum tag_types, tree type);
1771 static void cp_parser_check_access_in_redeclaration
1773 static bool cp_parser_optional_template_keyword
1775 static void cp_parser_pre_parsed_nested_name_specifier
1777 static void cp_parser_cache_group
1778 (cp_parser *, enum cpp_ttype, unsigned);
1779 static void cp_parser_parse_tentatively
1781 static void cp_parser_commit_to_tentative_parse
1783 static void cp_parser_abort_tentative_parse
1785 static bool cp_parser_parse_definitely
1787 static inline bool cp_parser_parsing_tentatively
1789 static bool cp_parser_uncommitted_to_tentative_parse_p
1791 static void cp_parser_error
1792 (cp_parser *, const char *);
1793 static void cp_parser_name_lookup_error
1794 (cp_parser *, tree, tree, const char *);
1795 static bool cp_parser_simulate_error
1797 static void cp_parser_check_type_definition
1799 static void cp_parser_check_for_definition_in_return_type
1800 (cp_declarator *, tree);
1801 static void cp_parser_check_for_invalid_template_id
1802 (cp_parser *, tree);
1803 static bool cp_parser_non_integral_constant_expression
1804 (cp_parser *, const char *);
1805 static void cp_parser_diagnose_invalid_type_name
1806 (cp_parser *, tree, tree);
1807 static bool cp_parser_parse_and_diagnose_invalid_type_name
1809 static int cp_parser_skip_to_closing_parenthesis
1810 (cp_parser *, bool, bool, bool);
1811 static void cp_parser_skip_to_end_of_statement
1813 static void cp_parser_consume_semicolon_at_end_of_statement
1815 static void cp_parser_skip_to_end_of_block_or_statement
1817 static void cp_parser_skip_to_closing_brace
1819 static void cp_parser_skip_until_found
1820 (cp_parser *, enum cpp_ttype, const char *);
1821 static void cp_parser_skip_to_pragma_eol
1822 (cp_parser*, cp_token *);
1823 static bool cp_parser_error_occurred
1825 static bool cp_parser_allow_gnu_extensions_p
1827 static bool cp_parser_is_string_literal
1829 static bool cp_parser_is_keyword
1830 (cp_token *, enum rid);
1831 static tree cp_parser_make_typename_type
1832 (cp_parser *, tree, tree);
1834 /* Returns nonzero if we are parsing tentatively. */
1837 cp_parser_parsing_tentatively (cp_parser* parser)
1839 return parser->context->next != NULL;
1842 /* Returns nonzero if TOKEN is a string literal. */
1845 cp_parser_is_string_literal (cp_token* token)
1847 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1850 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1853 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1855 return token->keyword == keyword;
1858 /* A minimum or maximum operator has been seen. As these are
1859 deprecated, issue a warning. */
1862 cp_parser_warn_min_max (void)
1864 if (warn_deprecated && !in_system_header)
1865 warning (OPT_Wdeprecated, "minimum/maximum operators are deprecated");
1868 /* If not parsing tentatively, issue a diagnostic of the form
1869 FILE:LINE: MESSAGE before TOKEN
1870 where TOKEN is the next token in the input stream. MESSAGE
1871 (specified by the caller) is usually of the form "expected
1875 cp_parser_error (cp_parser* parser, const char* message)
1877 if (!cp_parser_simulate_error (parser))
1879 cp_token *token = cp_lexer_peek_token (parser->lexer);
1880 /* This diagnostic makes more sense if it is tagged to the line
1881 of the token we just peeked at. */
1882 cp_lexer_set_source_position_from_token (token);
1884 if (token->type == CPP_PRAGMA)
1886 error ("%<#pragma%> is not allowed here");
1887 cp_parser_skip_to_pragma_eol (parser, token);
1891 c_parse_error (message,
1892 /* Because c_parser_error does not understand
1893 CPP_KEYWORD, keywords are treated like
1895 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1900 /* Issue an error about name-lookup failing. NAME is the
1901 IDENTIFIER_NODE DECL is the result of
1902 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1903 the thing that we hoped to find. */
1906 cp_parser_name_lookup_error (cp_parser* parser,
1909 const char* desired)
1911 /* If name lookup completely failed, tell the user that NAME was not
1913 if (decl == error_mark_node)
1915 if (parser->scope && parser->scope != global_namespace)
1916 error ("%<%D::%D%> has not been declared",
1917 parser->scope, name);
1918 else if (parser->scope == global_namespace)
1919 error ("%<::%D%> has not been declared", name);
1920 else if (parser->object_scope
1921 && !CLASS_TYPE_P (parser->object_scope))
1922 error ("request for member %qD in non-class type %qT",
1923 name, parser->object_scope);
1924 else if (parser->object_scope)
1925 error ("%<%T::%D%> has not been declared",
1926 parser->object_scope, name);
1928 error ("%qD has not been declared", name);
1930 else if (parser->scope && parser->scope != global_namespace)
1931 error ("%<%D::%D%> %s", parser->scope, name, desired);
1932 else if (parser->scope == global_namespace)
1933 error ("%<::%D%> %s", name, desired);
1935 error ("%qD %s", name, desired);
1938 /* If we are parsing tentatively, remember that an error has occurred
1939 during this tentative parse. Returns true if the error was
1940 simulated; false if a message should be issued by the caller. */
1943 cp_parser_simulate_error (cp_parser* parser)
1945 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1947 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1953 /* This function is called when a type is defined. If type
1954 definitions are forbidden at this point, an error message is
1958 cp_parser_check_type_definition (cp_parser* parser)
1960 /* If types are forbidden here, issue a message. */
1961 if (parser->type_definition_forbidden_message)
1962 /* Use `%s' to print the string in case there are any escape
1963 characters in the message. */
1964 error ("%s", parser->type_definition_forbidden_message);
1967 /* This function is called when the DECLARATOR is processed. The TYPE
1968 was a type defined in the decl-specifiers. If it is invalid to
1969 define a type in the decl-specifiers for DECLARATOR, an error is
1973 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1976 /* [dcl.fct] forbids type definitions in return types.
1977 Unfortunately, it's not easy to know whether or not we are
1978 processing a return type until after the fact. */
1980 && (declarator->kind == cdk_pointer
1981 || declarator->kind == cdk_reference
1982 || declarator->kind == cdk_ptrmem))
1983 declarator = declarator->declarator;
1985 && declarator->kind == cdk_function)
1987 error ("new types may not be defined in a return type");
1988 inform ("(perhaps a semicolon is missing after the definition of %qT)",
1993 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1994 "<" in any valid C++ program. If the next token is indeed "<",
1995 issue a message warning the user about what appears to be an
1996 invalid attempt to form a template-id. */
1999 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2002 cp_token_position start = 0;
2004 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2007 error ("%qT is not a template", type);
2008 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2009 error ("%qE is not a template", type);
2011 error ("invalid template-id");
2012 /* Remember the location of the invalid "<". */
2013 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2014 start = cp_lexer_token_position (parser->lexer, true);
2015 /* Consume the "<". */
2016 cp_lexer_consume_token (parser->lexer);
2017 /* Parse the template arguments. */
2018 cp_parser_enclosed_template_argument_list (parser);
2019 /* Permanently remove the invalid template arguments so that
2020 this error message is not issued again. */
2022 cp_lexer_purge_tokens_after (parser->lexer, start);
2026 /* If parsing an integral constant-expression, issue an error message
2027 about the fact that THING appeared and return true. Otherwise,
2028 return false. In either case, set
2029 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2032 cp_parser_non_integral_constant_expression (cp_parser *parser,
2035 parser->non_integral_constant_expression_p = true;
2036 if (parser->integral_constant_expression_p)
2038 if (!parser->allow_non_integral_constant_expression_p)
2040 error ("%s cannot appear in a constant-expression", thing);
2047 /* Emit a diagnostic for an invalid type name. SCOPE is the
2048 qualifying scope (or NULL, if none) for ID. This function commits
2049 to the current active tentative parse, if any. (Otherwise, the
2050 problematic construct might be encountered again later, resulting
2051 in duplicate error messages.) */
2054 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2056 tree decl, old_scope;
2057 /* Try to lookup the identifier. */
2058 old_scope = parser->scope;
2059 parser->scope = scope;
2060 decl = cp_parser_lookup_name_simple (parser, id);
2061 parser->scope = old_scope;
2062 /* If the lookup found a template-name, it means that the user forgot
2063 to specify an argument list. Emit a useful error message. */
2064 if (TREE_CODE (decl) == TEMPLATE_DECL)
2065 error ("invalid use of template-name %qE without an argument list",
2067 else if (!parser->scope)
2069 /* Issue an error message. */
2070 error ("%qE does not name a type", id);
2071 /* If we're in a template class, it's possible that the user was
2072 referring to a type from a base class. For example:
2074 template <typename T> struct A { typedef T X; };
2075 template <typename T> struct B : public A<T> { X x; };
2077 The user should have said "typename A<T>::X". */
2078 if (processing_template_decl && current_class_type
2079 && TYPE_BINFO (current_class_type))
2083 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2087 tree base_type = BINFO_TYPE (b);
2088 if (CLASS_TYPE_P (base_type)
2089 && dependent_type_p (base_type))
2092 /* Go from a particular instantiation of the
2093 template (which will have an empty TYPE_FIELDs),
2094 to the main version. */
2095 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2096 for (field = TYPE_FIELDS (base_type);
2098 field = TREE_CHAIN (field))
2099 if (TREE_CODE (field) == TYPE_DECL
2100 && DECL_NAME (field) == id)
2102 inform ("(perhaps %<typename %T::%E%> was intended)",
2103 BINFO_TYPE (b), id);
2112 /* Here we diagnose qualified-ids where the scope is actually correct,
2113 but the identifier does not resolve to a valid type name. */
2114 else if (parser->scope != error_mark_node)
2116 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2117 error ("%qE in namespace %qE does not name a type",
2119 else if (TYPE_P (parser->scope))
2120 error ("%qE in class %qT does not name a type", id, parser->scope);
2124 cp_parser_commit_to_tentative_parse (parser);
2127 /* Check for a common situation where a type-name should be present,
2128 but is not, and issue a sensible error message. Returns true if an
2129 invalid type-name was detected.
2131 The situation handled by this function are variable declarations of the
2132 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2133 Usually, `ID' should name a type, but if we got here it means that it
2134 does not. We try to emit the best possible error message depending on
2135 how exactly the id-expression looks like.
2139 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2143 cp_parser_parse_tentatively (parser);
2144 id = cp_parser_id_expression (parser,
2145 /*template_keyword_p=*/false,
2146 /*check_dependency_p=*/true,
2147 /*template_p=*/NULL,
2148 /*declarator_p=*/true,
2149 /*optional_p=*/false);
2150 /* After the id-expression, there should be a plain identifier,
2151 otherwise this is not a simple variable declaration. Also, if
2152 the scope is dependent, we cannot do much. */
2153 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2154 || (parser->scope && TYPE_P (parser->scope)
2155 && dependent_type_p (parser->scope)))
2157 cp_parser_abort_tentative_parse (parser);
2160 if (!cp_parser_parse_definitely (parser)
2161 || TREE_CODE (id) != IDENTIFIER_NODE)
2164 /* Emit a diagnostic for the invalid type. */
2165 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2166 /* Skip to the end of the declaration; there's no point in
2167 trying to process it. */
2168 cp_parser_skip_to_end_of_block_or_statement (parser);
2172 /* Consume tokens up to, and including, the next non-nested closing `)'.
2173 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2174 are doing error recovery. Returns -1 if OR_COMMA is true and we
2175 found an unnested comma. */
2178 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2183 unsigned paren_depth = 0;
2184 unsigned brace_depth = 0;
2186 if (recovering && !or_comma
2187 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2192 cp_token * token = cp_lexer_peek_token (parser->lexer);
2194 switch (token->type)
2197 case CPP_PRAGMA_EOL:
2198 /* If we've run out of tokens, then there is no closing `)'. */
2202 /* This matches the processing in skip_to_end_of_statement. */
2207 case CPP_OPEN_BRACE:
2210 case CPP_CLOSE_BRACE:
2216 if (recovering && or_comma && !brace_depth && !paren_depth)
2220 case CPP_OPEN_PAREN:
2225 case CPP_CLOSE_PAREN:
2226 if (!brace_depth && !paren_depth--)
2229 cp_lexer_consume_token (parser->lexer);
2238 /* Consume the token. */
2239 cp_lexer_consume_token (parser->lexer);
2243 /* Consume tokens until we reach the end of the current statement.
2244 Normally, that will be just before consuming a `;'. However, if a
2245 non-nested `}' comes first, then we stop before consuming that. */
2248 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2250 unsigned nesting_depth = 0;
2254 cp_token *token = cp_lexer_peek_token (parser->lexer);
2256 switch (token->type)
2259 case CPP_PRAGMA_EOL:
2260 /* If we've run out of tokens, stop. */
2264 /* If the next token is a `;', we have reached the end of the
2270 case CPP_CLOSE_BRACE:
2271 /* If this is a non-nested '}', stop before consuming it.
2272 That way, when confronted with something like:
2276 we stop before consuming the closing '}', even though we
2277 have not yet reached a `;'. */
2278 if (nesting_depth == 0)
2281 /* If it is the closing '}' for a block that we have
2282 scanned, stop -- but only after consuming the token.
2288 we will stop after the body of the erroneously declared
2289 function, but before consuming the following `typedef'
2291 if (--nesting_depth == 0)
2293 cp_lexer_consume_token (parser->lexer);
2297 case CPP_OPEN_BRACE:
2305 /* Consume the token. */
2306 cp_lexer_consume_token (parser->lexer);
2310 /* This function is called at the end of a statement or declaration.
2311 If the next token is a semicolon, it is consumed; otherwise, error
2312 recovery is attempted. */
2315 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2317 /* Look for the trailing `;'. */
2318 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2320 /* If there is additional (erroneous) input, skip to the end of
2322 cp_parser_skip_to_end_of_statement (parser);
2323 /* If the next token is now a `;', consume it. */
2324 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2325 cp_lexer_consume_token (parser->lexer);
2329 /* Skip tokens until we have consumed an entire block, or until we
2330 have consumed a non-nested `;'. */
2333 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2335 int nesting_depth = 0;
2337 while (nesting_depth >= 0)
2339 cp_token *token = cp_lexer_peek_token (parser->lexer);
2341 switch (token->type)
2344 case CPP_PRAGMA_EOL:
2345 /* If we've run out of tokens, stop. */
2349 /* Stop if this is an unnested ';'. */
2354 case CPP_CLOSE_BRACE:
2355 /* Stop if this is an unnested '}', or closes the outermost
2362 case CPP_OPEN_BRACE:
2371 /* Consume the token. */
2372 cp_lexer_consume_token (parser->lexer);
2376 /* Skip tokens until a non-nested closing curly brace is the next
2380 cp_parser_skip_to_closing_brace (cp_parser *parser)
2382 unsigned nesting_depth = 0;
2386 cp_token *token = cp_lexer_peek_token (parser->lexer);
2388 switch (token->type)
2391 case CPP_PRAGMA_EOL:
2392 /* If we've run out of tokens, stop. */
2395 case CPP_CLOSE_BRACE:
2396 /* If the next token is a non-nested `}', then we have reached
2397 the end of the current block. */
2398 if (nesting_depth-- == 0)
2402 case CPP_OPEN_BRACE:
2403 /* If it the next token is a `{', then we are entering a new
2404 block. Consume the entire block. */
2412 /* Consume the token. */
2413 cp_lexer_consume_token (parser->lexer);
2417 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2418 parameter is the PRAGMA token, allowing us to purge the entire pragma
2422 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2426 parser->lexer->in_pragma = false;
2429 token = cp_lexer_consume_token (parser->lexer);
2430 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2432 /* Ensure that the pragma is not parsed again. */
2433 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2436 /* Require pragma end of line, resyncing with it as necessary. The
2437 arguments are as for cp_parser_skip_to_pragma_eol. */
2440 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2442 parser->lexer->in_pragma = false;
2443 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2444 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2447 /* This is a simple wrapper around make_typename_type. When the id is
2448 an unresolved identifier node, we can provide a superior diagnostic
2449 using cp_parser_diagnose_invalid_type_name. */
2452 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2455 if (TREE_CODE (id) == IDENTIFIER_NODE)
2457 result = make_typename_type (scope, id, typename_type,
2458 /*complain=*/tf_none);
2459 if (result == error_mark_node)
2460 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2463 return make_typename_type (scope, id, typename_type, tf_error);
2467 /* Create a new C++ parser. */
2470 cp_parser_new (void)
2476 /* cp_lexer_new_main is called before calling ggc_alloc because
2477 cp_lexer_new_main might load a PCH file. */
2478 lexer = cp_lexer_new_main ();
2480 /* Initialize the binops_by_token so that we can get the tree
2481 directly from the token. */
2482 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2483 binops_by_token[binops[i].token_type] = binops[i];
2485 parser = GGC_CNEW (cp_parser);
2486 parser->lexer = lexer;
2487 parser->context = cp_parser_context_new (NULL);
2489 /* For now, we always accept GNU extensions. */
2490 parser->allow_gnu_extensions_p = 1;
2492 /* The `>' token is a greater-than operator, not the end of a
2494 parser->greater_than_is_operator_p = true;
2496 parser->default_arg_ok_p = true;
2498 /* We are not parsing a constant-expression. */
2499 parser->integral_constant_expression_p = false;
2500 parser->allow_non_integral_constant_expression_p = false;
2501 parser->non_integral_constant_expression_p = false;
2503 /* Local variable names are not forbidden. */
2504 parser->local_variables_forbidden_p = false;
2506 /* We are not processing an `extern "C"' declaration. */
2507 parser->in_unbraced_linkage_specification_p = false;
2509 /* We are not processing a declarator. */
2510 parser->in_declarator_p = false;
2512 /* We are not processing a template-argument-list. */
2513 parser->in_template_argument_list_p = false;
2515 /* We are not in an iteration statement. */
2516 parser->in_statement = 0;
2518 /* We are not in a switch statement. */
2519 parser->in_switch_statement_p = false;
2521 /* We are not parsing a type-id inside an expression. */
2522 parser->in_type_id_in_expr_p = false;
2524 /* Declarations aren't implicitly extern "C". */
2525 parser->implicit_extern_c = false;
2527 /* String literals should be translated to the execution character set. */
2528 parser->translate_strings_p = true;
2530 /* The unparsed function queue is empty. */
2531 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2533 /* There are no classes being defined. */
2534 parser->num_classes_being_defined = 0;
2536 /* No template parameters apply. */
2537 parser->num_template_parameter_lists = 0;
2542 /* Create a cp_lexer structure which will emit the tokens in CACHE
2543 and push it onto the parser's lexer stack. This is used for delayed
2544 parsing of in-class method bodies and default arguments, and should
2545 not be confused with tentative parsing. */
2547 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2549 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2550 lexer->next = parser->lexer;
2551 parser->lexer = lexer;
2553 /* Move the current source position to that of the first token in the
2555 cp_lexer_set_source_position_from_token (lexer->next_token);
2558 /* Pop the top lexer off the parser stack. This is never used for the
2559 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2561 cp_parser_pop_lexer (cp_parser *parser)
2563 cp_lexer *lexer = parser->lexer;
2564 parser->lexer = lexer->next;
2565 cp_lexer_destroy (lexer);
2567 /* Put the current source position back where it was before this
2568 lexer was pushed. */
2569 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2572 /* Lexical conventions [gram.lex] */
2574 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2578 cp_parser_identifier (cp_parser* parser)
2582 /* Look for the identifier. */
2583 token = cp_parser_require (parser, CPP_NAME, "identifier");
2584 /* Return the value. */
2585 return token ? token->value : error_mark_node;
2588 /* Parse a sequence of adjacent string constants. Returns a
2589 TREE_STRING representing the combined, nul-terminated string
2590 constant. If TRANSLATE is true, translate the string to the
2591 execution character set. If WIDE_OK is true, a wide string is
2594 C++98 [lex.string] says that if a narrow string literal token is
2595 adjacent to a wide string literal token, the behavior is undefined.
2596 However, C99 6.4.5p4 says that this results in a wide string literal.
2597 We follow C99 here, for consistency with the C front end.
2599 This code is largely lifted from lex_string() in c-lex.c.
2601 FUTURE: ObjC++ will need to handle @-strings here. */
2603 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2608 struct obstack str_ob;
2609 cpp_string str, istr, *strs;
2612 tok = cp_lexer_peek_token (parser->lexer);
2613 if (!cp_parser_is_string_literal (tok))
2615 cp_parser_error (parser, "expected string-literal");
2616 return error_mark_node;
2619 /* Try to avoid the overhead of creating and destroying an obstack
2620 for the common case of just one string. */
2621 if (!cp_parser_is_string_literal
2622 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2624 cp_lexer_consume_token (parser->lexer);
2626 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2627 str.len = TREE_STRING_LENGTH (tok->value);
2629 if (tok->type == CPP_WSTRING)
2636 gcc_obstack_init (&str_ob);
2641 cp_lexer_consume_token (parser->lexer);
2643 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2644 str.len = TREE_STRING_LENGTH (tok->value);
2645 if (tok->type == CPP_WSTRING)
2648 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2650 tok = cp_lexer_peek_token (parser->lexer);
2652 while (cp_parser_is_string_literal (tok));
2654 strs = (cpp_string *) obstack_finish (&str_ob);
2657 if (wide && !wide_ok)
2659 cp_parser_error (parser, "a wide string is invalid in this context");
2663 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2664 (parse_in, strs, count, &istr, wide))
2666 value = build_string (istr.len, (char *)istr.text);
2667 free ((void *)istr.text);
2669 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2670 value = fix_string_type (value);
2673 /* cpp_interpret_string has issued an error. */
2674 value = error_mark_node;
2677 obstack_free (&str_ob, 0);
2683 /* Basic concepts [gram.basic] */
2685 /* Parse a translation-unit.
2688 declaration-seq [opt]
2690 Returns TRUE if all went well. */
2693 cp_parser_translation_unit (cp_parser* parser)
2695 /* The address of the first non-permanent object on the declarator
2697 static void *declarator_obstack_base;
2701 /* Create the declarator obstack, if necessary. */
2702 if (!cp_error_declarator)
2704 gcc_obstack_init (&declarator_obstack);
2705 /* Create the error declarator. */
2706 cp_error_declarator = make_declarator (cdk_error);
2707 /* Create the empty parameter list. */
2708 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2709 /* Remember where the base of the declarator obstack lies. */
2710 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2713 cp_parser_declaration_seq_opt (parser);
2715 /* If there are no tokens left then all went well. */
2716 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2718 /* Get rid of the token array; we don't need it any more. */
2719 cp_lexer_destroy (parser->lexer);
2720 parser->lexer = NULL;
2722 /* This file might have been a context that's implicitly extern
2723 "C". If so, pop the lang context. (Only relevant for PCH.) */
2724 if (parser->implicit_extern_c)
2726 pop_lang_context ();
2727 parser->implicit_extern_c = false;
2731 finish_translation_unit ();
2737 cp_parser_error (parser, "expected declaration");
2741 /* Make sure the declarator obstack was fully cleaned up. */
2742 gcc_assert (obstack_next_free (&declarator_obstack)
2743 == declarator_obstack_base);
2745 /* All went well. */
2749 /* Expressions [gram.expr] */
2751 /* Parse a primary-expression.
2762 ( compound-statement )
2763 __builtin_va_arg ( assignment-expression , type-id )
2764 __builtin_offsetof ( type-id , offsetof-expression )
2766 Objective-C++ Extension:
2774 ADDRESS_P is true iff this expression was immediately preceded by
2775 "&" and therefore might denote a pointer-to-member. CAST_P is true
2776 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2777 true iff this expression is a template argument.
2779 Returns a representation of the expression. Upon return, *IDK
2780 indicates what kind of id-expression (if any) was present. */
2783 cp_parser_primary_expression (cp_parser *parser,
2786 bool template_arg_p,
2791 /* Assume the primary expression is not an id-expression. */
2792 *idk = CP_ID_KIND_NONE;
2794 /* Peek at the next token. */
2795 token = cp_lexer_peek_token (parser->lexer);
2796 switch (token->type)
2807 token = cp_lexer_consume_token (parser->lexer);
2808 /* Floating-point literals are only allowed in an integral
2809 constant expression if they are cast to an integral or
2810 enumeration type. */
2811 if (TREE_CODE (token->value) == REAL_CST
2812 && parser->integral_constant_expression_p
2815 /* CAST_P will be set even in invalid code like "int(2.7 +
2816 ...)". Therefore, we have to check that the next token
2817 is sure to end the cast. */
2820 cp_token *next_token;
2822 next_token = cp_lexer_peek_token (parser->lexer);
2823 if (/* The comma at the end of an
2824 enumerator-definition. */
2825 next_token->type != CPP_COMMA
2826 /* The curly brace at the end of an enum-specifier. */
2827 && next_token->type != CPP_CLOSE_BRACE
2828 /* The end of a statement. */
2829 && next_token->type != CPP_SEMICOLON
2830 /* The end of the cast-expression. */
2831 && next_token->type != CPP_CLOSE_PAREN
2832 /* The end of an array bound. */
2833 && next_token->type != CPP_CLOSE_SQUARE
2834 /* The closing ">" in a template-argument-list. */
2835 && (next_token->type != CPP_GREATER
2836 || parser->greater_than_is_operator_p))
2840 /* If we are within a cast, then the constraint that the
2841 cast is to an integral or enumeration type will be
2842 checked at that point. If we are not within a cast, then
2843 this code is invalid. */
2845 cp_parser_non_integral_constant_expression
2846 (parser, "floating-point literal");
2848 return token->value;
2852 /* ??? Should wide strings be allowed when parser->translate_strings_p
2853 is false (i.e. in attributes)? If not, we can kill the third
2854 argument to cp_parser_string_literal. */
2855 return cp_parser_string_literal (parser,
2856 parser->translate_strings_p,
2859 case CPP_OPEN_PAREN:
2862 bool saved_greater_than_is_operator_p;
2864 /* Consume the `('. */
2865 cp_lexer_consume_token (parser->lexer);
2866 /* Within a parenthesized expression, a `>' token is always
2867 the greater-than operator. */
2868 saved_greater_than_is_operator_p
2869 = parser->greater_than_is_operator_p;
2870 parser->greater_than_is_operator_p = true;
2871 /* If we see `( { ' then we are looking at the beginning of
2872 a GNU statement-expression. */
2873 if (cp_parser_allow_gnu_extensions_p (parser)
2874 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2876 /* Statement-expressions are not allowed by the standard. */
2878 pedwarn ("ISO C++ forbids braced-groups within expressions");
2880 /* And they're not allowed outside of a function-body; you
2881 cannot, for example, write:
2883 int i = ({ int j = 3; j + 1; });
2885 at class or namespace scope. */
2886 if (!at_function_scope_p ())
2887 error ("statement-expressions are allowed only inside functions");
2888 /* Start the statement-expression. */
2889 expr = begin_stmt_expr ();
2890 /* Parse the compound-statement. */
2891 cp_parser_compound_statement (parser, expr, false);
2893 expr = finish_stmt_expr (expr, false);
2897 /* Parse the parenthesized expression. */
2898 expr = cp_parser_expression (parser, cast_p);
2899 /* Let the front end know that this expression was
2900 enclosed in parentheses. This matters in case, for
2901 example, the expression is of the form `A::B', since
2902 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2904 finish_parenthesized_expr (expr);
2906 /* The `>' token might be the end of a template-id or
2907 template-parameter-list now. */
2908 parser->greater_than_is_operator_p
2909 = saved_greater_than_is_operator_p;
2910 /* Consume the `)'. */
2911 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2912 cp_parser_skip_to_end_of_statement (parser);
2918 switch (token->keyword)
2920 /* These two are the boolean literals. */
2922 cp_lexer_consume_token (parser->lexer);
2923 return boolean_true_node;
2925 cp_lexer_consume_token (parser->lexer);
2926 return boolean_false_node;
2928 /* The `__null' literal. */
2930 cp_lexer_consume_token (parser->lexer);
2933 /* Recognize the `this' keyword. */
2935 cp_lexer_consume_token (parser->lexer);
2936 if (parser->local_variables_forbidden_p)
2938 error ("%<this%> may not be used in this context");
2939 return error_mark_node;
2941 /* Pointers cannot appear in constant-expressions. */
2942 if (cp_parser_non_integral_constant_expression (parser,
2944 return error_mark_node;
2945 return finish_this_expr ();
2947 /* The `operator' keyword can be the beginning of an
2952 case RID_FUNCTION_NAME:
2953 case RID_PRETTY_FUNCTION_NAME:
2954 case RID_C99_FUNCTION_NAME:
2955 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2956 __func__ are the names of variables -- but they are
2957 treated specially. Therefore, they are handled here,
2958 rather than relying on the generic id-expression logic
2959 below. Grammatically, these names are id-expressions.
2961 Consume the token. */
2962 token = cp_lexer_consume_token (parser->lexer);
2963 /* Look up the name. */
2964 return finish_fname (token->value);
2971 /* The `__builtin_va_arg' construct is used to handle
2972 `va_arg'. Consume the `__builtin_va_arg' token. */
2973 cp_lexer_consume_token (parser->lexer);
2974 /* Look for the opening `('. */
2975 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2976 /* Now, parse the assignment-expression. */
2977 expression = cp_parser_assignment_expression (parser,
2979 /* Look for the `,'. */
2980 cp_parser_require (parser, CPP_COMMA, "`,'");
2981 /* Parse the type-id. */
2982 type = cp_parser_type_id (parser);
2983 /* Look for the closing `)'. */
2984 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2985 /* Using `va_arg' in a constant-expression is not
2987 if (cp_parser_non_integral_constant_expression (parser,
2989 return error_mark_node;
2990 return build_x_va_arg (expression, type);
2994 return cp_parser_builtin_offsetof (parser);
2996 /* Objective-C++ expressions. */
2998 case RID_AT_PROTOCOL:
2999 case RID_AT_SELECTOR:
3000 return cp_parser_objc_expression (parser);
3003 cp_parser_error (parser, "expected primary-expression");
3004 return error_mark_node;
3007 /* An id-expression can start with either an identifier, a
3008 `::' as the beginning of a qualified-id, or the "operator"
3012 case CPP_TEMPLATE_ID:
3013 case CPP_NESTED_NAME_SPECIFIER:
3017 const char *error_msg;
3022 /* Parse the id-expression. */
3024 = cp_parser_id_expression (parser,
3025 /*template_keyword_p=*/false,
3026 /*check_dependency_p=*/true,
3028 /*declarator_p=*/false,
3029 /*optional_p=*/false);
3030 if (id_expression == error_mark_node)
3031 return error_mark_node;
3032 token = cp_lexer_peek_token (parser->lexer);
3033 done = (token->type != CPP_OPEN_SQUARE
3034 && token->type != CPP_OPEN_PAREN
3035 && token->type != CPP_DOT
3036 && token->type != CPP_DEREF
3037 && token->type != CPP_PLUS_PLUS
3038 && token->type != CPP_MINUS_MINUS);
3039 /* If we have a template-id, then no further lookup is
3040 required. If the template-id was for a template-class, we
3041 will sometimes have a TYPE_DECL at this point. */
3042 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3043 || TREE_CODE (id_expression) == TYPE_DECL)
3044 decl = id_expression;
3045 /* Look up the name. */
3048 tree ambiguous_decls;
3050 decl = cp_parser_lookup_name (parser, id_expression,
3053 /*is_namespace=*/false,
3054 /*check_dependency=*/true,
3056 /* If the lookup was ambiguous, an error will already have
3058 if (ambiguous_decls)
3059 return error_mark_node;
3061 /* In Objective-C++, an instance variable (ivar) may be preferred
3062 to whatever cp_parser_lookup_name() found. */
3063 decl = objc_lookup_ivar (decl, id_expression);
3065 /* If name lookup gives us a SCOPE_REF, then the
3066 qualifying scope was dependent. */
3067 if (TREE_CODE (decl) == SCOPE_REF)
3069 /* Check to see if DECL is a local variable in a context
3070 where that is forbidden. */
3071 if (parser->local_variables_forbidden_p
3072 && local_variable_p (decl))
3074 /* It might be that we only found DECL because we are
3075 trying to be generous with pre-ISO scoping rules.
3076 For example, consider:
3080 for (int i = 0; i < 10; ++i) {}
3081 extern void f(int j = i);
3084 Here, name look up will originally find the out
3085 of scope `i'. We need to issue a warning message,
3086 but then use the global `i'. */
3087 decl = check_for_out_of_scope_variable (decl);
3088 if (local_variable_p (decl))
3090 error ("local variable %qD may not appear in this context",
3092 return error_mark_node;
3097 decl = (finish_id_expression
3098 (id_expression, decl, parser->scope,
3100 parser->integral_constant_expression_p,
3101 parser->allow_non_integral_constant_expression_p,
3102 &parser->non_integral_constant_expression_p,
3103 template_p, done, address_p,
3107 cp_parser_error (parser, error_msg);
3111 /* Anything else is an error. */
3113 /* ...unless we have an Objective-C++ message or string literal, that is. */
3114 if (c_dialect_objc ()
3115 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3116 return cp_parser_objc_expression (parser);
3118 cp_parser_error (parser, "expected primary-expression");
3119 return error_mark_node;
3123 /* Parse an id-expression.
3130 :: [opt] nested-name-specifier template [opt] unqualified-id
3132 :: operator-function-id
3135 Return a representation of the unqualified portion of the
3136 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3137 a `::' or nested-name-specifier.
3139 Often, if the id-expression was a qualified-id, the caller will
3140 want to make a SCOPE_REF to represent the qualified-id. This
3141 function does not do this in order to avoid wastefully creating
3142 SCOPE_REFs when they are not required.
3144 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3147 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3148 uninstantiated templates.
3150 If *TEMPLATE_P is non-NULL, it is set to true iff the
3151 `template' keyword is used to explicitly indicate that the entity
3152 named is a template.
3154 If DECLARATOR_P is true, the id-expression is appearing as part of
3155 a declarator, rather than as part of an expression. */
3158 cp_parser_id_expression (cp_parser *parser,
3159 bool template_keyword_p,
3160 bool check_dependency_p,
3165 bool global_scope_p;
3166 bool nested_name_specifier_p;
3168 /* Assume the `template' keyword was not used. */
3170 *template_p = template_keyword_p;
3172 /* Look for the optional `::' operator. */
3174 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3176 /* Look for the optional nested-name-specifier. */
3177 nested_name_specifier_p
3178 = (cp_parser_nested_name_specifier_opt (parser,
3179 /*typename_keyword_p=*/false,
3184 /* If there is a nested-name-specifier, then we are looking at
3185 the first qualified-id production. */
3186 if (nested_name_specifier_p)
3189 tree saved_object_scope;
3190 tree saved_qualifying_scope;
3191 tree unqualified_id;
3194 /* See if the next token is the `template' keyword. */
3196 template_p = &is_template;
3197 *template_p = cp_parser_optional_template_keyword (parser);
3198 /* Name lookup we do during the processing of the
3199 unqualified-id might obliterate SCOPE. */
3200 saved_scope = parser->scope;
3201 saved_object_scope = parser->object_scope;
3202 saved_qualifying_scope = parser->qualifying_scope;
3203 /* Process the final unqualified-id. */
3204 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3207 /*optional_p=*/false);
3208 /* Restore the SAVED_SCOPE for our caller. */
3209 parser->scope = saved_scope;
3210 parser->object_scope = saved_object_scope;
3211 parser->qualifying_scope = saved_qualifying_scope;
3213 return unqualified_id;
3215 /* Otherwise, if we are in global scope, then we are looking at one
3216 of the other qualified-id productions. */
3217 else if (global_scope_p)
3222 /* Peek at the next token. */
3223 token = cp_lexer_peek_token (parser->lexer);
3225 /* If it's an identifier, and the next token is not a "<", then
3226 we can avoid the template-id case. This is an optimization
3227 for this common case. */
3228 if (token->type == CPP_NAME
3229 && !cp_parser_nth_token_starts_template_argument_list_p
3231 return cp_parser_identifier (parser);
3233 cp_parser_parse_tentatively (parser);
3234 /* Try a template-id. */
3235 id = cp_parser_template_id (parser,
3236 /*template_keyword_p=*/false,
3237 /*check_dependency_p=*/true,
3239 /* If that worked, we're done. */
3240 if (cp_parser_parse_definitely (parser))
3243 /* Peek at the next token. (Changes in the token buffer may
3244 have invalidated the pointer obtained above.) */
3245 token = cp_lexer_peek_token (parser->lexer);
3247 switch (token->type)
3250 return cp_parser_identifier (parser);
3253 if (token->keyword == RID_OPERATOR)
3254 return cp_parser_operator_function_id (parser);
3258 cp_parser_error (parser, "expected id-expression");
3259 return error_mark_node;
3263 return cp_parser_unqualified_id (parser, template_keyword_p,
3264 /*check_dependency_p=*/true,
3269 /* Parse an unqualified-id.
3273 operator-function-id
3274 conversion-function-id
3278 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3279 keyword, in a construct like `A::template ...'.
3281 Returns a representation of unqualified-id. For the `identifier'
3282 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3283 production a BIT_NOT_EXPR is returned; the operand of the
3284 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3285 other productions, see the documentation accompanying the
3286 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3287 names are looked up in uninstantiated templates. If DECLARATOR_P
3288 is true, the unqualified-id is appearing as part of a declarator,
3289 rather than as part of an expression. */
3292 cp_parser_unqualified_id (cp_parser* parser,
3293 bool template_keyword_p,
3294 bool check_dependency_p,
3300 /* Peek at the next token. */
3301 token = cp_lexer_peek_token (parser->lexer);
3303 switch (token->type)
3309 /* We don't know yet whether or not this will be a
3311 cp_parser_parse_tentatively (parser);
3312 /* Try a template-id. */
3313 id = cp_parser_template_id (parser, template_keyword_p,
3316 /* If it worked, we're done. */
3317 if (cp_parser_parse_definitely (parser))
3319 /* Otherwise, it's an ordinary identifier. */
3320 return cp_parser_identifier (parser);
3323 case CPP_TEMPLATE_ID:
3324 return cp_parser_template_id (parser, template_keyword_p,
3331 tree qualifying_scope;
3336 /* Consume the `~' token. */
3337 cp_lexer_consume_token (parser->lexer);
3338 /* Parse the class-name. The standard, as written, seems to
3341 template <typename T> struct S { ~S (); };
3342 template <typename T> S<T>::~S() {}
3344 is invalid, since `~' must be followed by a class-name, but
3345 `S<T>' is dependent, and so not known to be a class.
3346 That's not right; we need to look in uninstantiated
3347 templates. A further complication arises from:
3349 template <typename T> void f(T t) {
3353 Here, it is not possible to look up `T' in the scope of `T'
3354 itself. We must look in both the current scope, and the
3355 scope of the containing complete expression.
3357 Yet another issue is:
3366 The standard does not seem to say that the `S' in `~S'
3367 should refer to the type `S' and not the data member
3370 /* DR 244 says that we look up the name after the "~" in the
3371 same scope as we looked up the qualifying name. That idea
3372 isn't fully worked out; it's more complicated than that. */
3373 scope = parser->scope;
3374 object_scope = parser->object_scope;
3375 qualifying_scope = parser->qualifying_scope;
3377 /* If the name is of the form "X::~X" it's OK. */
3378 if (scope && TYPE_P (scope)
3379 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3380 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3382 && (cp_lexer_peek_token (parser->lexer)->value
3383 == TYPE_IDENTIFIER (scope)))
3385 cp_lexer_consume_token (parser->lexer);
3386 return build_nt (BIT_NOT_EXPR, scope);
3389 /* If there was an explicit qualification (S::~T), first look
3390 in the scope given by the qualification (i.e., S). */
3392 type_decl = NULL_TREE;
3395 cp_parser_parse_tentatively (parser);
3396 type_decl = cp_parser_class_name (parser,
3397 /*typename_keyword_p=*/false,
3398 /*template_keyword_p=*/false,
3400 /*check_dependency=*/false,
3401 /*class_head_p=*/false,
3403 if (cp_parser_parse_definitely (parser))
3406 /* In "N::S::~S", look in "N" as well. */
3407 if (!done && scope && qualifying_scope)
3409 cp_parser_parse_tentatively (parser);
3410 parser->scope = qualifying_scope;
3411 parser->object_scope = NULL_TREE;
3412 parser->qualifying_scope = NULL_TREE;
3414 = cp_parser_class_name (parser,
3415 /*typename_keyword_p=*/false,
3416 /*template_keyword_p=*/false,
3418 /*check_dependency=*/false,
3419 /*class_head_p=*/false,
3421 if (cp_parser_parse_definitely (parser))
3424 /* In "p->S::~T", look in the scope given by "*p" as well. */
3425 else if (!done && object_scope)
3427 cp_parser_parse_tentatively (parser);
3428 parser->scope = object_scope;
3429 parser->object_scope = NULL_TREE;
3430 parser->qualifying_scope = NULL_TREE;
3432 = cp_parser_class_name (parser,
3433 /*typename_keyword_p=*/false,
3434 /*template_keyword_p=*/false,
3436 /*check_dependency=*/false,
3437 /*class_head_p=*/false,
3439 if (cp_parser_parse_definitely (parser))
3442 /* Look in the surrounding context. */
3445 parser->scope = NULL_TREE;
3446 parser->object_scope = NULL_TREE;
3447 parser->qualifying_scope = NULL_TREE;
3449 = cp_parser_class_name (parser,
3450 /*typename_keyword_p=*/false,
3451 /*template_keyword_p=*/false,
3453 /*check_dependency=*/false,
3454 /*class_head_p=*/false,
3457 /* If an error occurred, assume that the name of the
3458 destructor is the same as the name of the qualifying
3459 class. That allows us to keep parsing after running
3460 into ill-formed destructor names. */
3461 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3462 return build_nt (BIT_NOT_EXPR, scope);
3463 else if (type_decl == error_mark_node)
3464 return error_mark_node;
3466 /* Check that destructor name and scope match. */
3467 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3469 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3470 error ("declaration of %<~%T%> as member of %qT",
3472 return error_mark_node;
3477 A typedef-name that names a class shall not be used as the
3478 identifier in the declarator for a destructor declaration. */
3480 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3481 && !DECL_SELF_REFERENCE_P (type_decl)
3482 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3483 error ("typedef-name %qD used as destructor declarator",
3486 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3490 if (token->keyword == RID_OPERATOR)
3494 /* This could be a template-id, so we try that first. */
3495 cp_parser_parse_tentatively (parser);
3496 /* Try a template-id. */
3497 id = cp_parser_template_id (parser, template_keyword_p,
3498 /*check_dependency_p=*/true,
3500 /* If that worked, we're done. */
3501 if (cp_parser_parse_definitely (parser))
3503 /* We still don't know whether we're looking at an
3504 operator-function-id or a conversion-function-id. */
3505 cp_parser_parse_tentatively (parser);
3506 /* Try an operator-function-id. */
3507 id = cp_parser_operator_function_id (parser);
3508 /* If that didn't work, try a conversion-function-id. */
3509 if (!cp_parser_parse_definitely (parser))
3510 id = cp_parser_conversion_function_id (parser);
3519 cp_parser_error (parser, "expected unqualified-id");
3520 return error_mark_node;
3524 /* Parse an (optional) nested-name-specifier.
3526 nested-name-specifier:
3527 class-or-namespace-name :: nested-name-specifier [opt]
3528 class-or-namespace-name :: template nested-name-specifier [opt]
3530 PARSER->SCOPE should be set appropriately before this function is
3531 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3532 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3535 Sets PARSER->SCOPE to the class (TYPE) or namespace
3536 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3537 it unchanged if there is no nested-name-specifier. Returns the new
3538 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3540 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3541 part of a declaration and/or decl-specifier. */
3544 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3545 bool typename_keyword_p,
3546 bool check_dependency_p,
3548 bool is_declaration)
3550 bool success = false;
3551 cp_token_position start = 0;
3554 /* If the next token corresponds to a nested name specifier, there
3555 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3556 false, it may have been true before, in which case something
3557 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3558 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3559 CHECK_DEPENDENCY_P is false, we have to fall through into the
3561 if (check_dependency_p
3562 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3564 cp_parser_pre_parsed_nested_name_specifier (parser);
3565 return parser->scope;
3568 /* Remember where the nested-name-specifier starts. */
3569 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3571 start = cp_lexer_token_position (parser->lexer, false);
3572 push_deferring_access_checks (dk_deferred);
3579 tree saved_qualifying_scope;
3580 bool template_keyword_p;
3582 /* Spot cases that cannot be the beginning of a
3583 nested-name-specifier. */
3584 token = cp_lexer_peek_token (parser->lexer);
3586 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3587 the already parsed nested-name-specifier. */
3588 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3590 /* Grab the nested-name-specifier and continue the loop. */
3591 cp_parser_pre_parsed_nested_name_specifier (parser);
3596 /* Spot cases that cannot be the beginning of a
3597 nested-name-specifier. On the second and subsequent times
3598 through the loop, we look for the `template' keyword. */
3599 if (success && token->keyword == RID_TEMPLATE)
3601 /* A template-id can start a nested-name-specifier. */
3602 else if (token->type == CPP_TEMPLATE_ID)
3606 /* If the next token is not an identifier, then it is
3607 definitely not a class-or-namespace-name. */
3608 if (token->type != CPP_NAME)
3610 /* If the following token is neither a `<' (to begin a
3611 template-id), nor a `::', then we are not looking at a
3612 nested-name-specifier. */
3613 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3614 if (token->type != CPP_SCOPE
3615 && !cp_parser_nth_token_starts_template_argument_list_p
3620 /* The nested-name-specifier is optional, so we parse
3622 cp_parser_parse_tentatively (parser);
3624 /* Look for the optional `template' keyword, if this isn't the
3625 first time through the loop. */
3627 template_keyword_p = cp_parser_optional_template_keyword (parser);
3629 template_keyword_p = false;
3631 /* Save the old scope since the name lookup we are about to do
3632 might destroy it. */
3633 old_scope = parser->scope;
3634 saved_qualifying_scope = parser->qualifying_scope;
3635 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3636 look up names in "X<T>::I" in order to determine that "Y" is
3637 a template. So, if we have a typename at this point, we make
3638 an effort to look through it. */
3640 && !typename_keyword_p
3642 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3643 parser->scope = resolve_typename_type (parser->scope,
3644 /*only_current_p=*/false);
3645 /* Parse the qualifying entity. */
3647 = cp_parser_class_or_namespace_name (parser,
3653 /* Look for the `::' token. */
3654 cp_parser_require (parser, CPP_SCOPE, "`::'");
3656 /* If we found what we wanted, we keep going; otherwise, we're
3658 if (!cp_parser_parse_definitely (parser))
3660 bool error_p = false;
3662 /* Restore the OLD_SCOPE since it was valid before the
3663 failed attempt at finding the last
3664 class-or-namespace-name. */
3665 parser->scope = old_scope;
3666 parser->qualifying_scope = saved_qualifying_scope;
3667 /* If the next token is an identifier, and the one after
3668 that is a `::', then any valid interpretation would have
3669 found a class-or-namespace-name. */
3670 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3671 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3673 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3676 token = cp_lexer_consume_token (parser->lexer);
3679 if (!token->ambiguous_p)
3682 tree ambiguous_decls;
3684 decl = cp_parser_lookup_name (parser, token->value,
3686 /*is_template=*/false,
3687 /*is_namespace=*/false,
3688 /*check_dependency=*/true,
3690 if (TREE_CODE (decl) == TEMPLATE_DECL)
3691 error ("%qD used without template parameters", decl);
3692 else if (ambiguous_decls)
3694 error ("reference to %qD is ambiguous",
3696 print_candidates (ambiguous_decls);
3697 decl = error_mark_node;
3700 cp_parser_name_lookup_error
3701 (parser, token->value, decl,
3702 "is not a class or namespace");
3704 parser->scope = error_mark_node;
3706 /* Treat this as a successful nested-name-specifier
3711 If the name found is not a class-name (clause
3712 _class_) or namespace-name (_namespace.def_), the
3713 program is ill-formed. */
3716 cp_lexer_consume_token (parser->lexer);
3720 /* We've found one valid nested-name-specifier. */
3722 /* Name lookup always gives us a DECL. */
3723 if (TREE_CODE (new_scope) == TYPE_DECL)
3724 new_scope = TREE_TYPE (new_scope);
3725 /* Uses of "template" must be followed by actual templates. */
3726 if (template_keyword_p
3727 && !(CLASS_TYPE_P (new_scope)
3728 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3729 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3730 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3731 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3732 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3733 == TEMPLATE_ID_EXPR)))
3734 pedwarn (TYPE_P (new_scope)
3735 ? "%qT is not a template"
3736 : "%qD is not a template",
3738 /* If it is a class scope, try to complete it; we are about to
3739 be looking up names inside the class. */
3740 if (TYPE_P (new_scope)
3741 /* Since checking types for dependency can be expensive,
3742 avoid doing it if the type is already complete. */
3743 && !COMPLETE_TYPE_P (new_scope)
3744 /* Do not try to complete dependent types. */
3745 && !dependent_type_p (new_scope))
3746 new_scope = complete_type (new_scope);
3747 /* Make sure we look in the right scope the next time through
3749 parser->scope = new_scope;
3752 /* If parsing tentatively, replace the sequence of tokens that makes
3753 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3754 token. That way, should we re-parse the token stream, we will
3755 not have to repeat the effort required to do the parse, nor will
3756 we issue duplicate error messages. */
3757 if (success && start)
3762 token = cp_lexer_token_at (parser->lexer, start);
3763 /* Reset the contents of the START token. */
3764 token->type = CPP_NESTED_NAME_SPECIFIER;
3765 /* Retrieve any deferred checks. Do not pop this access checks yet
3766 so the memory will not be reclaimed during token replacing below. */
3767 access_checks = get_deferred_access_checks ();
3768 token->value = build_tree_list (copy_list (access_checks),
3770 TREE_TYPE (token->value) = parser->qualifying_scope;
3771 token->keyword = RID_MAX;
3773 /* Purge all subsequent tokens. */
3774 cp_lexer_purge_tokens_after (parser->lexer, start);
3778 pop_to_parent_deferring_access_checks ();
3780 return success ? parser->scope : NULL_TREE;
3783 /* Parse a nested-name-specifier. See
3784 cp_parser_nested_name_specifier_opt for details. This function
3785 behaves identically, except that it will an issue an error if no
3786 nested-name-specifier is present. */
3789 cp_parser_nested_name_specifier (cp_parser *parser,
3790 bool typename_keyword_p,
3791 bool check_dependency_p,
3793 bool is_declaration)
3797 /* Look for the nested-name-specifier. */
3798 scope = cp_parser_nested_name_specifier_opt (parser,
3803 /* If it was not present, issue an error message. */
3806 cp_parser_error (parser, "expected nested-name-specifier");
3807 parser->scope = NULL_TREE;
3813 /* Parse a class-or-namespace-name.
3815 class-or-namespace-name:
3819 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3820 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3821 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3822 TYPE_P is TRUE iff the next name should be taken as a class-name,
3823 even the same name is declared to be another entity in the same
3826 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3827 specified by the class-or-namespace-name. If neither is found the
3828 ERROR_MARK_NODE is returned. */
3831 cp_parser_class_or_namespace_name (cp_parser *parser,
3832 bool typename_keyword_p,
3833 bool template_keyword_p,
3834 bool check_dependency_p,
3836 bool is_declaration)
3839 tree saved_qualifying_scope;
3840 tree saved_object_scope;
3844 /* Before we try to parse the class-name, we must save away the
3845 current PARSER->SCOPE since cp_parser_class_name will destroy
3847 saved_scope = parser->scope;
3848 saved_qualifying_scope = parser->qualifying_scope;
3849 saved_object_scope = parser->object_scope;
3850 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3851 there is no need to look for a namespace-name. */
3852 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3854 cp_parser_parse_tentatively (parser);
3855 scope = cp_parser_class_name (parser,
3858 type_p ? class_type : none_type,
3860 /*class_head_p=*/false,
3862 /* If that didn't work, try for a namespace-name. */
3863 if (!only_class_p && !cp_parser_parse_definitely (parser))
3865 /* Restore the saved scope. */
3866 parser->scope = saved_scope;
3867 parser->qualifying_scope = saved_qualifying_scope;
3868 parser->object_scope = saved_object_scope;
3869 /* If we are not looking at an identifier followed by the scope
3870 resolution operator, then this is not part of a
3871 nested-name-specifier. (Note that this function is only used
3872 to parse the components of a nested-name-specifier.) */
3873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3874 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3875 return error_mark_node;
3876 scope = cp_parser_namespace_name (parser);
3882 /* Parse a postfix-expression.
3886 postfix-expression [ expression ]
3887 postfix-expression ( expression-list [opt] )
3888 simple-type-specifier ( expression-list [opt] )
3889 typename :: [opt] nested-name-specifier identifier
3890 ( expression-list [opt] )
3891 typename :: [opt] nested-name-specifier template [opt] template-id
3892 ( expression-list [opt] )
3893 postfix-expression . template [opt] id-expression
3894 postfix-expression -> template [opt] id-expression
3895 postfix-expression . pseudo-destructor-name
3896 postfix-expression -> pseudo-destructor-name
3897 postfix-expression ++
3898 postfix-expression --
3899 dynamic_cast < type-id > ( expression )
3900 static_cast < type-id > ( expression )
3901 reinterpret_cast < type-id > ( expression )
3902 const_cast < type-id > ( expression )
3903 typeid ( expression )
3909 ( type-id ) { initializer-list , [opt] }
3911 This extension is a GNU version of the C99 compound-literal
3912 construct. (The C99 grammar uses `type-name' instead of `type-id',
3913 but they are essentially the same concept.)
3915 If ADDRESS_P is true, the postfix expression is the operand of the
3916 `&' operator. CAST_P is true if this expression is the target of a
3919 Returns a representation of the expression. */
3922 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3926 cp_id_kind idk = CP_ID_KIND_NONE;
3927 tree postfix_expression = NULL_TREE;
3929 /* Peek at the next token. */
3930 token = cp_lexer_peek_token (parser->lexer);
3931 /* Some of the productions are determined by keywords. */
3932 keyword = token->keyword;
3942 const char *saved_message;
3944 /* All of these can be handled in the same way from the point
3945 of view of parsing. Begin by consuming the token
3946 identifying the cast. */
3947 cp_lexer_consume_token (parser->lexer);
3949 /* New types cannot be defined in the cast. */
3950 saved_message = parser->type_definition_forbidden_message;
3951 parser->type_definition_forbidden_message
3952 = "types may not be defined in casts";
3954 /* Look for the opening `<'. */
3955 cp_parser_require (parser, CPP_LESS, "`<'");
3956 /* Parse the type to which we are casting. */
3957 type = cp_parser_type_id (parser);
3958 /* Look for the closing `>'. */
3959 cp_parser_require (parser, CPP_GREATER, "`>'");
3960 /* Restore the old message. */
3961 parser->type_definition_forbidden_message = saved_message;
3963 /* And the expression which is being cast. */
3964 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3965 expression = cp_parser_expression (parser, /*cast_p=*/true);
3966 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3968 /* Only type conversions to integral or enumeration types
3969 can be used in constant-expressions. */
3970 if (parser->integral_constant_expression_p
3971 && !dependent_type_p (type)
3972 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3973 && (cp_parser_non_integral_constant_expression
3975 "a cast to a type other than an integral or "
3976 "enumeration type")))
3977 return error_mark_node;
3983 = build_dynamic_cast (type, expression);
3987 = build_static_cast (type, expression);
3991 = build_reinterpret_cast (type, expression);
3995 = build_const_cast (type, expression);
4006 const char *saved_message;
4007 bool saved_in_type_id_in_expr_p;
4009 /* Consume the `typeid' token. */
4010 cp_lexer_consume_token (parser->lexer);
4011 /* Look for the `(' token. */
4012 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4013 /* Types cannot be defined in a `typeid' expression. */
4014 saved_message = parser->type_definition_forbidden_message;
4015 parser->type_definition_forbidden_message
4016 = "types may not be defined in a `typeid\' expression";
4017 /* We can't be sure yet whether we're looking at a type-id or an
4019 cp_parser_parse_tentatively (parser);
4020 /* Try a type-id first. */
4021 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4022 parser->in_type_id_in_expr_p = true;
4023 type = cp_parser_type_id (parser);
4024 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4025 /* Look for the `)' token. Otherwise, we can't be sure that
4026 we're not looking at an expression: consider `typeid (int
4027 (3))', for example. */
4028 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4029 /* If all went well, simply lookup the type-id. */
4030 if (cp_parser_parse_definitely (parser))
4031 postfix_expression = get_typeid (type);
4032 /* Otherwise, fall back to the expression variant. */
4037 /* Look for an expression. */
4038 expression = cp_parser_expression (parser, /*cast_p=*/false);
4039 /* Compute its typeid. */
4040 postfix_expression = build_typeid (expression);
4041 /* Look for the `)' token. */
4042 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4044 /* `typeid' may not appear in an integral constant expression. */
4045 if (cp_parser_non_integral_constant_expression(parser,
4046 "`typeid' operator"))
4047 return error_mark_node;
4048 /* Restore the saved message. */
4049 parser->type_definition_forbidden_message = saved_message;
4056 /* The syntax permitted here is the same permitted for an
4057 elaborated-type-specifier. */
4058 type = cp_parser_elaborated_type_specifier (parser,
4059 /*is_friend=*/false,
4060 /*is_declaration=*/false);
4061 postfix_expression = cp_parser_functional_cast (parser, type);
4069 /* If the next thing is a simple-type-specifier, we may be
4070 looking at a functional cast. We could also be looking at
4071 an id-expression. So, we try the functional cast, and if
4072 that doesn't work we fall back to the primary-expression. */
4073 cp_parser_parse_tentatively (parser);
4074 /* Look for the simple-type-specifier. */
4075 type = cp_parser_simple_type_specifier (parser,
4076 /*decl_specs=*/NULL,
4077 CP_PARSER_FLAGS_NONE);
4078 /* Parse the cast itself. */
4079 if (!cp_parser_error_occurred (parser))
4081 = cp_parser_functional_cast (parser, type);
4082 /* If that worked, we're done. */
4083 if (cp_parser_parse_definitely (parser))
4086 /* If the functional-cast didn't work out, try a
4087 compound-literal. */
4088 if (cp_parser_allow_gnu_extensions_p (parser)
4089 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4091 VEC(constructor_elt,gc) *initializer_list = NULL;
4092 bool saved_in_type_id_in_expr_p;
4094 cp_parser_parse_tentatively (parser);
4095 /* Consume the `('. */
4096 cp_lexer_consume_token (parser->lexer);
4097 /* Parse the type. */
4098 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4099 parser->in_type_id_in_expr_p = true;
4100 type = cp_parser_type_id (parser);
4101 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4102 /* Look for the `)'. */
4103 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4104 /* Look for the `{'. */
4105 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4106 /* If things aren't going well, there's no need to
4108 if (!cp_parser_error_occurred (parser))
4110 bool non_constant_p;
4111 /* Parse the initializer-list. */
4113 = cp_parser_initializer_list (parser, &non_constant_p);
4114 /* Allow a trailing `,'. */
4115 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4116 cp_lexer_consume_token (parser->lexer);
4117 /* Look for the final `}'. */
4118 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4120 /* If that worked, we're definitely looking at a
4121 compound-literal expression. */
4122 if (cp_parser_parse_definitely (parser))
4124 /* Warn the user that a compound literal is not
4125 allowed in standard C++. */
4127 pedwarn ("ISO C++ forbids compound-literals");
4128 /* Form the representation of the compound-literal. */
4130 = finish_compound_literal (type, initializer_list);
4135 /* It must be a primary-expression. */
4137 = cp_parser_primary_expression (parser, address_p, cast_p,
4138 /*template_arg_p=*/false,
4144 /* Keep looping until the postfix-expression is complete. */
4147 if (idk == CP_ID_KIND_UNQUALIFIED
4148 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4149 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4150 /* It is not a Koenig lookup function call. */
4152 = unqualified_name_lookup_error (postfix_expression);
4154 /* Peek at the next token. */
4155 token = cp_lexer_peek_token (parser->lexer);
4157 switch (token->type)
4159 case CPP_OPEN_SQUARE:
4161 = cp_parser_postfix_open_square_expression (parser,
4164 idk = CP_ID_KIND_NONE;
4167 case CPP_OPEN_PAREN:
4168 /* postfix-expression ( expression-list [opt] ) */
4171 bool is_builtin_constant_p;
4172 bool saved_integral_constant_expression_p = false;
4173 bool saved_non_integral_constant_expression_p = false;
4176 is_builtin_constant_p
4177 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4178 if (is_builtin_constant_p)
4180 /* The whole point of __builtin_constant_p is to allow
4181 non-constant expressions to appear as arguments. */
4182 saved_integral_constant_expression_p
4183 = parser->integral_constant_expression_p;
4184 saved_non_integral_constant_expression_p
4185 = parser->non_integral_constant_expression_p;
4186 parser->integral_constant_expression_p = false;
4188 args = (cp_parser_parenthesized_expression_list
4189 (parser, /*is_attribute_list=*/false,
4191 /*non_constant_p=*/NULL));
4192 if (is_builtin_constant_p)
4194 parser->integral_constant_expression_p
4195 = saved_integral_constant_expression_p;
4196 parser->non_integral_constant_expression_p
4197 = saved_non_integral_constant_expression_p;
4200 if (args == error_mark_node)
4202 postfix_expression = error_mark_node;
4206 /* Function calls are not permitted in
4207 constant-expressions. */
4208 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4209 && cp_parser_non_integral_constant_expression (parser,
4212 postfix_expression = error_mark_node;
4217 if (idk == CP_ID_KIND_UNQUALIFIED)
4219 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4225 = perform_koenig_lookup (postfix_expression, args);
4229 = unqualified_fn_lookup_error (postfix_expression);
4231 /* We do not perform argument-dependent lookup if
4232 normal lookup finds a non-function, in accordance
4233 with the expected resolution of DR 218. */
4234 else if (args && is_overloaded_fn (postfix_expression))
4236 tree fn = get_first_fn (postfix_expression);
4238 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4239 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4241 /* Only do argument dependent lookup if regular
4242 lookup does not find a set of member functions.
4243 [basic.lookup.koenig]/2a */
4244 if (!DECL_FUNCTION_MEMBER_P (fn))
4248 = perform_koenig_lookup (postfix_expression, args);
4253 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4255 tree instance = TREE_OPERAND (postfix_expression, 0);
4256 tree fn = TREE_OPERAND (postfix_expression, 1);
4258 if (processing_template_decl
4259 && (type_dependent_expression_p (instance)
4260 || (!BASELINK_P (fn)
4261 && TREE_CODE (fn) != FIELD_DECL)
4262 || type_dependent_expression_p (fn)
4263 || any_type_dependent_arguments_p (args)))
4266 = build_min_nt (CALL_EXPR, postfix_expression,
4271 if (BASELINK_P (fn))
4273 = (build_new_method_call
4274 (instance, fn, args, NULL_TREE,
4275 (idk == CP_ID_KIND_QUALIFIED
4276 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4280 = finish_call_expr (postfix_expression, args,
4281 /*disallow_virtual=*/false,
4282 /*koenig_p=*/false);
4284 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4285 || TREE_CODE (postfix_expression) == MEMBER_REF
4286 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4287 postfix_expression = (build_offset_ref_call_from_tree
4288 (postfix_expression, args));
4289 else if (idk == CP_ID_KIND_QUALIFIED)
4290 /* A call to a static class member, or a namespace-scope
4293 = finish_call_expr (postfix_expression, args,
4294 /*disallow_virtual=*/true,
4297 /* All other function calls. */
4299 = finish_call_expr (postfix_expression, args,
4300 /*disallow_virtual=*/false,
4303 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4304 idk = CP_ID_KIND_NONE;
4310 /* postfix-expression . template [opt] id-expression
4311 postfix-expression . pseudo-destructor-name
4312 postfix-expression -> template [opt] id-expression
4313 postfix-expression -> pseudo-destructor-name */
4315 /* Consume the `.' or `->' operator. */
4316 cp_lexer_consume_token (parser->lexer);
4319 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4325 /* postfix-expression ++ */
4326 /* Consume the `++' token. */
4327 cp_lexer_consume_token (parser->lexer);
4328 /* Generate a representation for the complete expression. */
4330 = finish_increment_expr (postfix_expression,
4331 POSTINCREMENT_EXPR);
4332 /* Increments may not appear in constant-expressions. */
4333 if (cp_parser_non_integral_constant_expression (parser,
4335 postfix_expression = error_mark_node;
4336 idk = CP_ID_KIND_NONE;
4339 case CPP_MINUS_MINUS:
4340 /* postfix-expression -- */
4341 /* Consume the `--' token. */
4342 cp_lexer_consume_token (parser->lexer);
4343 /* Generate a representation for the complete expression. */
4345 = finish_increment_expr (postfix_expression,
4346 POSTDECREMENT_EXPR);
4347 /* Decrements may not appear in constant-expressions. */
4348 if (cp_parser_non_integral_constant_expression (parser,
4350 postfix_expression = error_mark_node;
4351 idk = CP_ID_KIND_NONE;
4355 return postfix_expression;
4359 /* We should never get here. */
4361 return error_mark_node;
4364 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4365 by cp_parser_builtin_offsetof. We're looking for
4367 postfix-expression [ expression ]
4369 FOR_OFFSETOF is set if we're being called in that context, which
4370 changes how we deal with integer constant expressions. */
4373 cp_parser_postfix_open_square_expression (cp_parser *parser,
4374 tree postfix_expression,
4379 /* Consume the `[' token. */
4380 cp_lexer_consume_token (parser->lexer);
4382 /* Parse the index expression. */
4383 /* ??? For offsetof, there is a question of what to allow here. If
4384 offsetof is not being used in an integral constant expression context,
4385 then we *could* get the right answer by computing the value at runtime.
4386 If we are in an integral constant expression context, then we might
4387 could accept any constant expression; hard to say without analysis.
4388 Rather than open the barn door too wide right away, allow only integer
4389 constant expressions here. */
4391 index = cp_parser_constant_expression (parser, false, NULL);
4393 index = cp_parser_expression (parser, /*cast_p=*/false);
4395 /* Look for the closing `]'. */
4396 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4398 /* Build the ARRAY_REF. */
4399 postfix_expression = grok_array_decl (postfix_expression, index);
4401 /* When not doing offsetof, array references are not permitted in
4402 constant-expressions. */
4404 && (cp_parser_non_integral_constant_expression
4405 (parser, "an array reference")))
4406 postfix_expression = error_mark_node;
4408 return postfix_expression;
4411 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4412 by cp_parser_builtin_offsetof. We're looking for
4414 postfix-expression . template [opt] id-expression
4415 postfix-expression . pseudo-destructor-name
4416 postfix-expression -> template [opt] id-expression
4417 postfix-expression -> pseudo-destructor-name
4419 FOR_OFFSETOF is set if we're being called in that context. That sorta
4420 limits what of the above we'll actually accept, but nevermind.
4421 TOKEN_TYPE is the "." or "->" token, which will already have been
4422 removed from the stream. */
4425 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4426 enum cpp_ttype token_type,
4427 tree postfix_expression,
4428 bool for_offsetof, cp_id_kind *idk)
4432 bool pseudo_destructor_p;
4433 tree scope = NULL_TREE;
4435 /* If this is a `->' operator, dereference the pointer. */
4436 if (token_type == CPP_DEREF)
4437 postfix_expression = build_x_arrow (postfix_expression);
4438 /* Check to see whether or not the expression is type-dependent. */
4439 dependent_p = type_dependent_expression_p (postfix_expression);
4440 /* The identifier following the `->' or `.' is not qualified. */
4441 parser->scope = NULL_TREE;
4442 parser->qualifying_scope = NULL_TREE;
4443 parser->object_scope = NULL_TREE;
4444 *idk = CP_ID_KIND_NONE;
4445 /* Enter the scope corresponding to the type of the object
4446 given by the POSTFIX_EXPRESSION. */
4447 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4449 scope = TREE_TYPE (postfix_expression);
4450 /* According to the standard, no expression should ever have
4451 reference type. Unfortunately, we do not currently match
4452 the standard in this respect in that our internal representation
4453 of an expression may have reference type even when the standard
4454 says it does not. Therefore, we have to manually obtain the
4455 underlying type here. */
4456 scope = non_reference (scope);
4457 /* The type of the POSTFIX_EXPRESSION must be complete. */
4458 if (scope == unknown_type_node)
4460 error ("%qE does not have class type", postfix_expression);
4464 scope = complete_type_or_else (scope, NULL_TREE);
4465 /* Let the name lookup machinery know that we are processing a
4466 class member access expression. */
4467 parser->context->object_type = scope;
4468 /* If something went wrong, we want to be able to discern that case,
4469 as opposed to the case where there was no SCOPE due to the type
4470 of expression being dependent. */
4472 scope = error_mark_node;
4473 /* If the SCOPE was erroneous, make the various semantic analysis
4474 functions exit quickly -- and without issuing additional error
4476 if (scope == error_mark_node)
4477 postfix_expression = error_mark_node;
4480 /* Assume this expression is not a pseudo-destructor access. */
4481 pseudo_destructor_p = false;
4483 /* If the SCOPE is a scalar type, then, if this is a valid program,
4484 we must be looking at a pseudo-destructor-name. */
4485 if (scope && SCALAR_TYPE_P (scope))
4490 cp_parser_parse_tentatively (parser);
4491 /* Parse the pseudo-destructor-name. */
4493 cp_parser_pseudo_destructor_name (parser, &s, &type);
4494 if (cp_parser_parse_definitely (parser))
4496 pseudo_destructor_p = true;
4498 = finish_pseudo_destructor_expr (postfix_expression,
4499 s, TREE_TYPE (type));
4503 if (!pseudo_destructor_p)
4505 /* If the SCOPE is not a scalar type, we are looking at an
4506 ordinary class member access expression, rather than a
4507 pseudo-destructor-name. */
4509 /* Parse the id-expression. */
4510 name = (cp_parser_id_expression
4512 cp_parser_optional_template_keyword (parser),
4513 /*check_dependency_p=*/true,
4515 /*declarator_p=*/false,
4516 /*optional_p=*/false));
4517 /* In general, build a SCOPE_REF if the member name is qualified.
4518 However, if the name was not dependent and has already been
4519 resolved; there is no need to build the SCOPE_REF. For example;
4521 struct X { void f(); };
4522 template <typename T> void f(T* t) { t->X::f(); }
4524 Even though "t" is dependent, "X::f" is not and has been resolved
4525 to a BASELINK; there is no need to include scope information. */
4527 /* But we do need to remember that there was an explicit scope for
4528 virtual function calls. */
4530 *idk = CP_ID_KIND_QUALIFIED;
4532 /* If the name is a template-id that names a type, we will get a
4533 TYPE_DECL here. That is invalid code. */
4534 if (TREE_CODE (name) == TYPE_DECL)
4536 error ("invalid use of %qD", name);
4537 postfix_expression = error_mark_node;
4541 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4543 name = build_qualified_name (/*type=*/NULL_TREE,
4547 parser->scope = NULL_TREE;
4548 parser->qualifying_scope = NULL_TREE;
4549 parser->object_scope = NULL_TREE;
4551 if (scope && name && BASELINK_P (name))
4552 adjust_result_of_qualified_name_lookup
4553 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4555 = finish_class_member_access_expr (postfix_expression, name,
4560 /* We no longer need to look up names in the scope of the object on
4561 the left-hand side of the `.' or `->' operator. */
4562 parser->context->object_type = NULL_TREE;
4564 /* Outside of offsetof, these operators may not appear in
4565 constant-expressions. */
4567 && (cp_parser_non_integral_constant_expression
4568 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4569 postfix_expression = error_mark_node;
4571 return postfix_expression;
4574 /* Parse a parenthesized expression-list.
4577 assignment-expression
4578 expression-list, assignment-expression
4583 identifier, expression-list
4585 CAST_P is true if this expression is the target of a cast.
4587 Returns a TREE_LIST. The TREE_VALUE of each node is a
4588 representation of an assignment-expression. Note that a TREE_LIST
4589 is returned even if there is only a single expression in the list.
4590 error_mark_node is returned if the ( and or ) are
4591 missing. NULL_TREE is returned on no expressions. The parentheses
4592 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4593 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4594 indicates whether or not all of the expressions in the list were
4598 cp_parser_parenthesized_expression_list (cp_parser* parser,
4599 bool is_attribute_list,
4601 bool *non_constant_p)
4603 tree expression_list = NULL_TREE;
4604 bool fold_expr_p = is_attribute_list;
4605 tree identifier = NULL_TREE;
4607 /* Assume all the expressions will be constant. */
4609 *non_constant_p = false;
4611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4612 return error_mark_node;
4614 /* Consume expressions until there are no more. */
4615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4620 /* At the beginning of attribute lists, check to see if the
4621 next token is an identifier. */
4622 if (is_attribute_list
4623 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4627 /* Consume the identifier. */
4628 token = cp_lexer_consume_token (parser->lexer);
4629 /* Save the identifier. */
4630 identifier = token->value;
4634 /* Parse the next assignment-expression. */
4637 bool expr_non_constant_p;
4638 expr = (cp_parser_constant_expression
4639 (parser, /*allow_non_constant_p=*/true,
4640 &expr_non_constant_p));
4641 if (expr_non_constant_p)
4642 *non_constant_p = true;
4645 expr = cp_parser_assignment_expression (parser, cast_p);
4648 expr = fold_non_dependent_expr (expr);
4650 /* Add it to the list. We add error_mark_node
4651 expressions to the list, so that we can still tell if
4652 the correct form for a parenthesized expression-list
4653 is found. That gives better errors. */
4654 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4656 if (expr == error_mark_node)
4660 /* After the first item, attribute lists look the same as
4661 expression lists. */
4662 is_attribute_list = false;
4665 /* If the next token isn't a `,', then we are done. */
4666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4669 /* Otherwise, consume the `,' and keep going. */
4670 cp_lexer_consume_token (parser->lexer);
4673 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4678 /* We try and resync to an unnested comma, as that will give the
4679 user better diagnostics. */
4680 ending = cp_parser_skip_to_closing_parenthesis (parser,
4681 /*recovering=*/true,
4683 /*consume_paren=*/true);
4687 return error_mark_node;
4690 /* We built up the list in reverse order so we must reverse it now. */
4691 expression_list = nreverse (expression_list);
4693 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4695 return expression_list;
4698 /* Parse a pseudo-destructor-name.
4700 pseudo-destructor-name:
4701 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4702 :: [opt] nested-name-specifier template template-id :: ~ type-name
4703 :: [opt] nested-name-specifier [opt] ~ type-name
4705 If either of the first two productions is used, sets *SCOPE to the
4706 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4707 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4708 or ERROR_MARK_NODE if the parse fails. */
4711 cp_parser_pseudo_destructor_name (cp_parser* parser,
4715 bool nested_name_specifier_p;
4717 /* Assume that things will not work out. */
4718 *type = error_mark_node;
4720 /* Look for the optional `::' operator. */
4721 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4722 /* Look for the optional nested-name-specifier. */
4723 nested_name_specifier_p
4724 = (cp_parser_nested_name_specifier_opt (parser,
4725 /*typename_keyword_p=*/false,
4726 /*check_dependency_p=*/true,
4728 /*is_declaration=*/true)
4730 /* Now, if we saw a nested-name-specifier, we might be doing the
4731 second production. */
4732 if (nested_name_specifier_p
4733 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4735 /* Consume the `template' keyword. */
4736 cp_lexer_consume_token (parser->lexer);
4737 /* Parse the template-id. */
4738 cp_parser_template_id (parser,
4739 /*template_keyword_p=*/true,
4740 /*check_dependency_p=*/false,
4741 /*is_declaration=*/true);
4742 /* Look for the `::' token. */
4743 cp_parser_require (parser, CPP_SCOPE, "`::'");
4745 /* If the next token is not a `~', then there might be some
4746 additional qualification. */
4747 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4749 /* Look for the type-name. */
4750 *scope = TREE_TYPE (cp_parser_type_name (parser));
4752 if (*scope == error_mark_node)
4755 /* If we don't have ::~, then something has gone wrong. Since
4756 the only caller of this function is looking for something
4757 after `.' or `->' after a scalar type, most likely the
4758 program is trying to get a member of a non-aggregate
4760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4761 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4763 cp_parser_error (parser, "request for member of non-aggregate type");
4767 /* Look for the `::' token. */
4768 cp_parser_require (parser, CPP_SCOPE, "`::'");
4773 /* Look for the `~'. */
4774 cp_parser_require (parser, CPP_COMPL, "`~'");
4775 /* Look for the type-name again. We are not responsible for
4776 checking that it matches the first type-name. */
4777 *type = cp_parser_type_name (parser);
4780 /* Parse a unary-expression.
4786 unary-operator cast-expression
4787 sizeof unary-expression
4795 __extension__ cast-expression
4796 __alignof__ unary-expression
4797 __alignof__ ( type-id )
4798 __real__ cast-expression
4799 __imag__ cast-expression
4802 ADDRESS_P is true iff the unary-expression is appearing as the
4803 operand of the `&' operator. CAST_P is true if this expression is
4804 the target of a cast.
4806 Returns a representation of the expression. */
4809 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4812 enum tree_code unary_operator;
4814 /* Peek at the next token. */
4815 token = cp_lexer_peek_token (parser->lexer);
4816 /* Some keywords give away the kind of expression. */
4817 if (token->type == CPP_KEYWORD)
4819 enum rid keyword = token->keyword;
4829 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4830 /* Consume the token. */
4831 cp_lexer_consume_token (parser->lexer);
4832 /* Parse the operand. */
4833 operand = cp_parser_sizeof_operand (parser, keyword);
4835 if (TYPE_P (operand))
4836 return cxx_sizeof_or_alignof_type (operand, op, true);
4838 return cxx_sizeof_or_alignof_expr (operand, op);
4842 return cp_parser_new_expression (parser);
4845 return cp_parser_delete_expression (parser);
4849 /* The saved value of the PEDANTIC flag. */
4853 /* Save away the PEDANTIC flag. */
4854 cp_parser_extension_opt (parser, &saved_pedantic);
4855 /* Parse the cast-expression. */
4856 expr = cp_parser_simple_cast_expression (parser);
4857 /* Restore the PEDANTIC flag. */
4858 pedantic = saved_pedantic;
4868 /* Consume the `__real__' or `__imag__' token. */
4869 cp_lexer_consume_token (parser->lexer);
4870 /* Parse the cast-expression. */
4871 expression = cp_parser_simple_cast_expression (parser);
4872 /* Create the complete representation. */
4873 return build_x_unary_op ((keyword == RID_REALPART
4874 ? REALPART_EXPR : IMAGPART_EXPR),
4884 /* Look for the `:: new' and `:: delete', which also signal the
4885 beginning of a new-expression, or delete-expression,
4886 respectively. If the next token is `::', then it might be one of
4888 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4892 /* See if the token after the `::' is one of the keywords in
4893 which we're interested. */
4894 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4895 /* If it's `new', we have a new-expression. */
4896 if (keyword == RID_NEW)
4897 return cp_parser_new_expression (parser);
4898 /* Similarly, for `delete'. */
4899 else if (keyword == RID_DELETE)
4900 return cp_parser_delete_expression (parser);
4903 /* Look for a unary operator. */
4904 unary_operator = cp_parser_unary_operator (token);
4905 /* The `++' and `--' operators can be handled similarly, even though
4906 they are not technically unary-operators in the grammar. */
4907 if (unary_operator == ERROR_MARK)
4909 if (token->type == CPP_PLUS_PLUS)
4910 unary_operator = PREINCREMENT_EXPR;
4911 else if (token->type == CPP_MINUS_MINUS)
4912 unary_operator = PREDECREMENT_EXPR;
4913 /* Handle the GNU address-of-label extension. */
4914 else if (cp_parser_allow_gnu_extensions_p (parser)
4915 && token->type == CPP_AND_AND)
4919 /* Consume the '&&' token. */
4920 cp_lexer_consume_token (parser->lexer);
4921 /* Look for the identifier. */
4922 identifier = cp_parser_identifier (parser);
4923 /* Create an expression representing the address. */
4924 return finish_label_address_expr (identifier);
4927 if (unary_operator != ERROR_MARK)
4929 tree cast_expression;
4930 tree expression = error_mark_node;
4931 const char *non_constant_p = NULL;
4933 /* Consume the operator token. */
4934 token = cp_lexer_consume_token (parser->lexer);
4935 /* Parse the cast-expression. */
4937 = cp_parser_cast_expression (parser,
4938 unary_operator == ADDR_EXPR,
4940 /* Now, build an appropriate representation. */
4941 switch (unary_operator)
4944 non_constant_p = "`*'";
4945 expression = build_x_indirect_ref (cast_expression, "unary *");
4949 non_constant_p = "`&'";
4952 expression = build_x_unary_op (unary_operator, cast_expression);
4955 case PREINCREMENT_EXPR:
4956 case PREDECREMENT_EXPR:
4957 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4960 case UNARY_PLUS_EXPR:
4962 case TRUTH_NOT_EXPR:
4963 expression = finish_unary_op_expr (unary_operator, cast_expression);
4971 && cp_parser_non_integral_constant_expression (parser,
4973 expression = error_mark_node;
4978 return cp_parser_postfix_expression (parser, address_p, cast_p);
4981 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4982 unary-operator, the corresponding tree code is returned. */
4984 static enum tree_code
4985 cp_parser_unary_operator (cp_token* token)
4987 switch (token->type)
4990 return INDIRECT_REF;
4996 return UNARY_PLUS_EXPR;
5002 return TRUTH_NOT_EXPR;
5005 return BIT_NOT_EXPR;
5012 /* Parse a new-expression.
5015 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5016 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5018 Returns a representation of the expression. */
5021 cp_parser_new_expression (cp_parser* parser)
5023 bool global_scope_p;
5029 /* Look for the optional `::' operator. */
5031 = (cp_parser_global_scope_opt (parser,
5032 /*current_scope_valid_p=*/false)
5034 /* Look for the `new' operator. */
5035 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5036 /* There's no easy way to tell a new-placement from the
5037 `( type-id )' construct. */
5038 cp_parser_parse_tentatively (parser);
5039 /* Look for a new-placement. */
5040 placement = cp_parser_new_placement (parser);
5041 /* If that didn't work out, there's no new-placement. */
5042 if (!cp_parser_parse_definitely (parser))
5043 placement = NULL_TREE;
5045 /* If the next token is a `(', then we have a parenthesized
5047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5049 /* Consume the `('. */
5050 cp_lexer_consume_token (parser->lexer);
5051 /* Parse the type-id. */
5052 type = cp_parser_type_id (parser);
5053 /* Look for the closing `)'. */
5054 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5055 /* There should not be a direct-new-declarator in this production,
5056 but GCC used to allowed this, so we check and emit a sensible error
5057 message for this case. */
5058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5060 error ("array bound forbidden after parenthesized type-id");
5061 inform ("try removing the parentheses around the type-id");
5062 cp_parser_direct_new_declarator (parser);
5066 /* Otherwise, there must be a new-type-id. */
5068 type = cp_parser_new_type_id (parser, &nelts);
5070 /* If the next token is a `(', then we have a new-initializer. */
5071 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5072 initializer = cp_parser_new_initializer (parser);
5074 initializer = NULL_TREE;
5076 /* A new-expression may not appear in an integral constant
5078 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5079 return error_mark_node;
5081 /* Create a representation of the new-expression. */
5082 return build_new (placement, type, nelts, initializer, global_scope_p);
5085 /* Parse a new-placement.
5090 Returns the same representation as for an expression-list. */
5093 cp_parser_new_placement (cp_parser* parser)
5095 tree expression_list;
5097 /* Parse the expression-list. */
5098 expression_list = (cp_parser_parenthesized_expression_list
5099 (parser, false, /*cast_p=*/false,
5100 /*non_constant_p=*/NULL));
5102 return expression_list;
5105 /* Parse a new-type-id.
5108 type-specifier-seq new-declarator [opt]
5110 Returns the TYPE allocated. If the new-type-id indicates an array
5111 type, *NELTS is set to the number of elements in the last array
5112 bound; the TYPE will not include the last array bound. */
5115 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5117 cp_decl_specifier_seq type_specifier_seq;
5118 cp_declarator *new_declarator;
5119 cp_declarator *declarator;
5120 cp_declarator *outer_declarator;
5121 const char *saved_message;
5124 /* The type-specifier sequence must not contain type definitions.
5125 (It cannot contain declarations of new types either, but if they
5126 are not definitions we will catch that because they are not
5128 saved_message = parser->type_definition_forbidden_message;
5129 parser->type_definition_forbidden_message
5130 = "types may not be defined in a new-type-id";
5131 /* Parse the type-specifier-seq. */
5132 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5133 &type_specifier_seq);
5134 /* Restore the old message. */
5135 parser->type_definition_forbidden_message = saved_message;
5136 /* Parse the new-declarator. */
5137 new_declarator = cp_parser_new_declarator_opt (parser);
5139 /* Determine the number of elements in the last array dimension, if
5142 /* Skip down to the last array dimension. */
5143 declarator = new_declarator;
5144 outer_declarator = NULL;
5145 while (declarator && (declarator->kind == cdk_pointer
5146 || declarator->kind == cdk_ptrmem))
5148 outer_declarator = declarator;
5149 declarator = declarator->declarator;
5152 && declarator->kind == cdk_array
5153 && declarator->declarator
5154 && declarator->declarator->kind == cdk_array)
5156 outer_declarator = declarator;
5157 declarator = declarator->declarator;
5160 if (declarator && declarator->kind == cdk_array)
5162 *nelts = declarator->u.array.bounds;
5163 if (*nelts == error_mark_node)
5164 *nelts = integer_one_node;
5166 if (outer_declarator)
5167 outer_declarator->declarator = declarator->declarator;
5169 new_declarator = NULL;
5172 type = groktypename (&type_specifier_seq, new_declarator);
5173 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5175 *nelts = array_type_nelts_top (type);
5176 type = TREE_TYPE (type);
5181 /* Parse an (optional) new-declarator.
5184 ptr-operator new-declarator [opt]
5185 direct-new-declarator
5187 Returns the declarator. */
5189 static cp_declarator *
5190 cp_parser_new_declarator_opt (cp_parser* parser)
5192 enum tree_code code;
5194 cp_cv_quals cv_quals;
5196 /* We don't know if there's a ptr-operator next, or not. */
5197 cp_parser_parse_tentatively (parser);
5198 /* Look for a ptr-operator. */
5199 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5200 /* If that worked, look for more new-declarators. */
5201 if (cp_parser_parse_definitely (parser))
5203 cp_declarator *declarator;
5205 /* Parse another optional declarator. */
5206 declarator = cp_parser_new_declarator_opt (parser);
5208 /* Create the representation of the declarator. */
5210 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5211 else if (code == INDIRECT_REF)
5212 declarator = make_pointer_declarator (cv_quals, declarator);
5214 declarator = make_reference_declarator (cv_quals, declarator);
5219 /* If the next token is a `[', there is a direct-new-declarator. */
5220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5221 return cp_parser_direct_new_declarator (parser);
5226 /* Parse a direct-new-declarator.
5228 direct-new-declarator:
5230 direct-new-declarator [constant-expression]
5234 static cp_declarator *
5235 cp_parser_direct_new_declarator (cp_parser* parser)
5237 cp_declarator *declarator = NULL;
5243 /* Look for the opening `['. */
5244 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5245 /* The first expression is not required to be constant. */
5248 expression = cp_parser_expression (parser, /*cast_p=*/false);
5249 /* The standard requires that the expression have integral
5250 type. DR 74 adds enumeration types. We believe that the
5251 real intent is that these expressions be handled like the
5252 expression in a `switch' condition, which also allows
5253 classes with a single conversion to integral or
5254 enumeration type. */
5255 if (!processing_template_decl)
5258 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5263 error ("expression in new-declarator must have integral "
5264 "or enumeration type");
5265 expression = error_mark_node;
5269 /* But all the other expressions must be. */
5272 = cp_parser_constant_expression (parser,
5273 /*allow_non_constant=*/false,
5275 /* Look for the closing `]'. */
5276 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5278 /* Add this bound to the declarator. */
5279 declarator = make_array_declarator (declarator, expression);
5281 /* If the next token is not a `[', then there are no more
5283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5290 /* Parse a new-initializer.
5293 ( expression-list [opt] )
5295 Returns a representation of the expression-list. If there is no
5296 expression-list, VOID_ZERO_NODE is returned. */
5299 cp_parser_new_initializer (cp_parser* parser)
5301 tree expression_list;
5303 expression_list = (cp_parser_parenthesized_expression_list
5304 (parser, false, /*cast_p=*/false,
5305 /*non_constant_p=*/NULL));
5306 if (!expression_list)
5307 expression_list = void_zero_node;
5309 return expression_list;
5312 /* Parse a delete-expression.
5315 :: [opt] delete cast-expression
5316 :: [opt] delete [ ] cast-expression
5318 Returns a representation of the expression. */
5321 cp_parser_delete_expression (cp_parser* parser)
5323 bool global_scope_p;
5327 /* Look for the optional `::' operator. */
5329 = (cp_parser_global_scope_opt (parser,
5330 /*current_scope_valid_p=*/false)
5332 /* Look for the `delete' keyword. */
5333 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5334 /* See if the array syntax is in use. */
5335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5337 /* Consume the `[' token. */
5338 cp_lexer_consume_token (parser->lexer);
5339 /* Look for the `]' token. */
5340 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5341 /* Remember that this is the `[]' construct. */
5347 /* Parse the cast-expression. */
5348 expression = cp_parser_simple_cast_expression (parser);
5350 /* A delete-expression may not appear in an integral constant
5352 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5353 return error_mark_node;
5355 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5358 /* Parse a cast-expression.
5362 ( type-id ) cast-expression
5364 ADDRESS_P is true iff the unary-expression is appearing as the
5365 operand of the `&' operator. CAST_P is true if this expression is
5366 the target of a cast.
5368 Returns a representation of the expression. */
5371 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5373 /* If it's a `(', then we might be looking at a cast. */
5374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5376 tree type = NULL_TREE;
5377 tree expr = NULL_TREE;
5378 bool compound_literal_p;
5379 const char *saved_message;
5381 /* There's no way to know yet whether or not this is a cast.
5382 For example, `(int (3))' is a unary-expression, while `(int)
5383 3' is a cast. So, we resort to parsing tentatively. */
5384 cp_parser_parse_tentatively (parser);
5385 /* Types may not be defined in a cast. */
5386 saved_message = parser->type_definition_forbidden_message;
5387 parser->type_definition_forbidden_message
5388 = "types may not be defined in casts";
5389 /* Consume the `('. */
5390 cp_lexer_consume_token (parser->lexer);
5391 /* A very tricky bit is that `(struct S) { 3 }' is a
5392 compound-literal (which we permit in C++ as an extension).
5393 But, that construct is not a cast-expression -- it is a
5394 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5395 is legal; if the compound-literal were a cast-expression,
5396 you'd need an extra set of parentheses.) But, if we parse
5397 the type-id, and it happens to be a class-specifier, then we
5398 will commit to the parse at that point, because we cannot
5399 undo the action that is done when creating a new class. So,
5400 then we cannot back up and do a postfix-expression.
5402 Therefore, we scan ahead to the closing `)', and check to see
5403 if the token after the `)' is a `{'. If so, we are not
5404 looking at a cast-expression.
5406 Save tokens so that we can put them back. */
5407 cp_lexer_save_tokens (parser->lexer);
5408 /* Skip tokens until the next token is a closing parenthesis.
5409 If we find the closing `)', and the next token is a `{', then
5410 we are looking at a compound-literal. */
5412 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5413 /*consume_paren=*/true)
5414 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5415 /* Roll back the tokens we skipped. */
5416 cp_lexer_rollback_tokens (parser->lexer);
5417 /* If we were looking at a compound-literal, simulate an error
5418 so that the call to cp_parser_parse_definitely below will
5420 if (compound_literal_p)
5421 cp_parser_simulate_error (parser);
5424 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5425 parser->in_type_id_in_expr_p = true;
5426 /* Look for the type-id. */
5427 type = cp_parser_type_id (parser);
5428 /* Look for the closing `)'. */
5429 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5433 /* Restore the saved message. */
5434 parser->type_definition_forbidden_message = saved_message;
5436 /* If ok so far, parse the dependent expression. We cannot be
5437 sure it is a cast. Consider `(T ())'. It is a parenthesized
5438 ctor of T, but looks like a cast to function returning T
5439 without a dependent expression. */
5440 if (!cp_parser_error_occurred (parser))
5441 expr = cp_parser_cast_expression (parser,
5442 /*address_p=*/false,
5445 if (cp_parser_parse_definitely (parser))
5447 /* Warn about old-style casts, if so requested. */
5448 if (warn_old_style_cast
5449 && !in_system_header
5450 && !VOID_TYPE_P (type)
5451 && current_lang_name != lang_name_c)
5452 warning (OPT_Wold_style_cast, "use of old-style cast");
5454 /* Only type conversions to integral or enumeration types
5455 can be used in constant-expressions. */
5456 if (parser->integral_constant_expression_p
5457 && !dependent_type_p (type)
5458 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5459 && (cp_parser_non_integral_constant_expression
5461 "a cast to a type other than an integral or "
5462 "enumeration type")))
5463 return error_mark_node;
5465 /* Perform the cast. */
5466 expr = build_c_cast (type, expr);
5471 /* If we get here, then it's not a cast, so it must be a
5472 unary-expression. */
5473 return cp_parser_unary_expression (parser, address_p, cast_p);
5476 /* Parse a binary expression of the general form:
5480 pm-expression .* cast-expression
5481 pm-expression ->* cast-expression
5483 multiplicative-expression:
5485 multiplicative-expression * pm-expression
5486 multiplicative-expression / pm-expression
5487 multiplicative-expression % pm-expression
5489 additive-expression:
5490 multiplicative-expression
5491 additive-expression + multiplicative-expression
5492 additive-expression - multiplicative-expression
5496 shift-expression << additive-expression
5497 shift-expression >> additive-expression
5499 relational-expression:
5501 relational-expression < shift-expression
5502 relational-expression > shift-expression
5503 relational-expression <= shift-expression
5504 relational-expression >= shift-expression
5508 relational-expression:
5509 relational-expression <? shift-expression
5510 relational-expression >? shift-expression
5512 equality-expression:
5513 relational-expression
5514 equality-expression == relational-expression
5515 equality-expression != relational-expression
5519 and-expression & equality-expression
5521 exclusive-or-expression:
5523 exclusive-or-expression ^ and-expression
5525 inclusive-or-expression:
5526 exclusive-or-expression
5527 inclusive-or-expression | exclusive-or-expression
5529 logical-and-expression:
5530 inclusive-or-expression
5531 logical-and-expression && inclusive-or-expression
5533 logical-or-expression:
5534 logical-and-expression
5535 logical-or-expression || logical-and-expression
5537 All these are implemented with a single function like:
5540 simple-cast-expression
5541 binary-expression <token> binary-expression
5543 CAST_P is true if this expression is the target of a cast.
5545 The binops_by_token map is used to get the tree codes for each <token> type.
5546 binary-expressions are associated according to a precedence table. */
5548 #define TOKEN_PRECEDENCE(token) \
5549 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5550 ? PREC_NOT_OPERATOR \
5551 : binops_by_token[token->type].prec)
5554 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5556 cp_parser_expression_stack stack;
5557 cp_parser_expression_stack_entry *sp = &stack[0];
5560 enum tree_code tree_type;
5561 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5564 /* Parse the first expression. */
5565 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5569 /* Get an operator token. */
5570 token = cp_lexer_peek_token (parser->lexer);
5571 if (token->type == CPP_MIN || token->type == CPP_MAX)
5572 cp_parser_warn_min_max ();
5574 new_prec = TOKEN_PRECEDENCE (token);
5576 /* Popping an entry off the stack means we completed a subexpression:
5577 - either we found a token which is not an operator (`>' where it is not
5578 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5579 will happen repeatedly;
5580 - or, we found an operator which has lower priority. This is the case
5581 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5583 if (new_prec <= prec)
5592 tree_type = binops_by_token[token->type].tree_type;
5594 /* We used the operator token. */
5595 cp_lexer_consume_token (parser->lexer);
5597 /* Extract another operand. It may be the RHS of this expression
5598 or the LHS of a new, higher priority expression. */
5599 rhs = cp_parser_simple_cast_expression (parser);
5601 /* Get another operator token. Look up its precedence to avoid
5602 building a useless (immediately popped) stack entry for common
5603 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5604 token = cp_lexer_peek_token (parser->lexer);
5605 lookahead_prec = TOKEN_PRECEDENCE (token);
5606 if (lookahead_prec > new_prec)
5608 /* ... and prepare to parse the RHS of the new, higher priority
5609 expression. Since precedence levels on the stack are
5610 monotonically increasing, we do not have to care about
5613 sp->tree_type = tree_type;
5618 new_prec = lookahead_prec;
5622 /* If the stack is not empty, we have parsed into LHS the right side
5623 (`4' in the example above) of an expression we had suspended.
5624 We can use the information on the stack to recover the LHS (`3')
5625 from the stack together with the tree code (`MULT_EXPR'), and
5626 the precedence of the higher level subexpression
5627 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5628 which will be used to actually build the additive expression. */
5631 tree_type = sp->tree_type;
5636 overloaded_p = false;
5637 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5639 /* If the binary operator required the use of an overloaded operator,
5640 then this expression cannot be an integral constant-expression.
5641 An overloaded operator can be used even if both operands are
5642 otherwise permissible in an integral constant-expression if at
5643 least one of the operands is of enumeration type. */
5646 && (cp_parser_non_integral_constant_expression
5647 (parser, "calls to overloaded operators")))
5648 return error_mark_node;
5655 /* Parse the `? expression : assignment-expression' part of a
5656 conditional-expression. The LOGICAL_OR_EXPR is the
5657 logical-or-expression that started the conditional-expression.
5658 Returns a representation of the entire conditional-expression.
5660 This routine is used by cp_parser_assignment_expression.
5662 ? expression : assignment-expression
5666 ? : assignment-expression */
5669 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5672 tree assignment_expr;
5674 /* Consume the `?' token. */
5675 cp_lexer_consume_token (parser->lexer);
5676 if (cp_parser_allow_gnu_extensions_p (parser)
5677 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5678 /* Implicit true clause. */
5681 /* Parse the expression. */
5682 expr = cp_parser_expression (parser, /*cast_p=*/false);
5684 /* The next token should be a `:'. */
5685 cp_parser_require (parser, CPP_COLON, "`:'");
5686 /* Parse the assignment-expression. */
5687 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5689 /* Build the conditional-expression. */
5690 return build_x_conditional_expr (logical_or_expr,
5695 /* Parse an assignment-expression.
5697 assignment-expression:
5698 conditional-expression
5699 logical-or-expression assignment-operator assignment_expression
5702 CAST_P is true if this expression is the target of a cast.
5704 Returns a representation for the expression. */
5707 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5711 /* If the next token is the `throw' keyword, then we're looking at
5712 a throw-expression. */
5713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5714 expr = cp_parser_throw_expression (parser);
5715 /* Otherwise, it must be that we are looking at a
5716 logical-or-expression. */
5719 /* Parse the binary expressions (logical-or-expression). */
5720 expr = cp_parser_binary_expression (parser, cast_p);
5721 /* If the next token is a `?' then we're actually looking at a
5722 conditional-expression. */
5723 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5724 return cp_parser_question_colon_clause (parser, expr);
5727 enum tree_code assignment_operator;
5729 /* If it's an assignment-operator, we're using the second
5732 = cp_parser_assignment_operator_opt (parser);
5733 if (assignment_operator != ERROR_MARK)
5737 /* Parse the right-hand side of the assignment. */
5738 rhs = cp_parser_assignment_expression (parser, cast_p);
5739 /* An assignment may not appear in a
5740 constant-expression. */
5741 if (cp_parser_non_integral_constant_expression (parser,
5743 return error_mark_node;
5744 /* Build the assignment expression. */
5745 expr = build_x_modify_expr (expr,
5746 assignment_operator,
5755 /* Parse an (optional) assignment-operator.
5757 assignment-operator: one of
5758 = *= /= %= += -= >>= <<= &= ^= |=
5762 assignment-operator: one of
5765 If the next token is an assignment operator, the corresponding tree
5766 code is returned, and the token is consumed. For example, for
5767 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5768 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5769 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5770 operator, ERROR_MARK is returned. */
5772 static enum tree_code
5773 cp_parser_assignment_operator_opt (cp_parser* parser)
5778 /* Peek at the next toen. */
5779 token = cp_lexer_peek_token (parser->lexer);
5781 switch (token->type)
5792 op = TRUNC_DIV_EXPR;
5796 op = TRUNC_MOD_EXPR;
5829 cp_parser_warn_min_max ();
5834 cp_parser_warn_min_max ();
5838 /* Nothing else is an assignment operator. */
5842 /* If it was an assignment operator, consume it. */
5843 if (op != ERROR_MARK)
5844 cp_lexer_consume_token (parser->lexer);
5849 /* Parse an expression.
5852 assignment-expression
5853 expression , assignment-expression
5855 CAST_P is true if this expression is the target of a cast.
5857 Returns a representation of the expression. */
5860 cp_parser_expression (cp_parser* parser, bool cast_p)
5862 tree expression = NULL_TREE;
5866 tree assignment_expression;
5868 /* Parse the next assignment-expression. */
5869 assignment_expression
5870 = cp_parser_assignment_expression (parser, cast_p);
5871 /* If this is the first assignment-expression, we can just
5874 expression = assignment_expression;
5876 expression = build_x_compound_expr (expression,
5877 assignment_expression);
5878 /* If the next token is not a comma, then we are done with the
5880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5882 /* Consume the `,'. */
5883 cp_lexer_consume_token (parser->lexer);
5884 /* A comma operator cannot appear in a constant-expression. */
5885 if (cp_parser_non_integral_constant_expression (parser,
5886 "a comma operator"))
5887 expression = error_mark_node;
5893 /* Parse a constant-expression.
5895 constant-expression:
5896 conditional-expression
5898 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5899 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5900 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5901 is false, NON_CONSTANT_P should be NULL. */
5904 cp_parser_constant_expression (cp_parser* parser,
5905 bool allow_non_constant_p,
5906 bool *non_constant_p)
5908 bool saved_integral_constant_expression_p;
5909 bool saved_allow_non_integral_constant_expression_p;
5910 bool saved_non_integral_constant_expression_p;
5913 /* It might seem that we could simply parse the
5914 conditional-expression, and then check to see if it were
5915 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5916 one that the compiler can figure out is constant, possibly after
5917 doing some simplifications or optimizations. The standard has a
5918 precise definition of constant-expression, and we must honor
5919 that, even though it is somewhat more restrictive.
5925 is not a legal declaration, because `(2, 3)' is not a
5926 constant-expression. The `,' operator is forbidden in a
5927 constant-expression. However, GCC's constant-folding machinery
5928 will fold this operation to an INTEGER_CST for `3'. */
5930 /* Save the old settings. */
5931 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5932 saved_allow_non_integral_constant_expression_p
5933 = parser->allow_non_integral_constant_expression_p;
5934 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5935 /* We are now parsing a constant-expression. */
5936 parser->integral_constant_expression_p = true;
5937 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5938 parser->non_integral_constant_expression_p = false;
5939 /* Although the grammar says "conditional-expression", we parse an
5940 "assignment-expression", which also permits "throw-expression"
5941 and the use of assignment operators. In the case that
5942 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5943 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5944 actually essential that we look for an assignment-expression.
5945 For example, cp_parser_initializer_clauses uses this function to
5946 determine whether a particular assignment-expression is in fact
5948 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5949 /* Restore the old settings. */
5950 parser->integral_constant_expression_p
5951 = saved_integral_constant_expression_p;
5952 parser->allow_non_integral_constant_expression_p
5953 = saved_allow_non_integral_constant_expression_p;
5954 if (allow_non_constant_p)
5955 *non_constant_p = parser->non_integral_constant_expression_p;
5956 else if (parser->non_integral_constant_expression_p)
5957 expression = error_mark_node;
5958 parser->non_integral_constant_expression_p
5959 = saved_non_integral_constant_expression_p;
5964 /* Parse __builtin_offsetof.
5966 offsetof-expression:
5967 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5969 offsetof-member-designator:
5971 | offsetof-member-designator "." id-expression
5972 | offsetof-member-designator "[" expression "]"
5976 cp_parser_builtin_offsetof (cp_parser *parser)
5978 int save_ice_p, save_non_ice_p;
5982 /* We're about to accept non-integral-constant things, but will
5983 definitely yield an integral constant expression. Save and
5984 restore these values around our local parsing. */
5985 save_ice_p = parser->integral_constant_expression_p;
5986 save_non_ice_p = parser->non_integral_constant_expression_p;
5988 /* Consume the "__builtin_offsetof" token. */
5989 cp_lexer_consume_token (parser->lexer);
5990 /* Consume the opening `('. */
5991 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5992 /* Parse the type-id. */
5993 type = cp_parser_type_id (parser);
5994 /* Look for the `,'. */
5995 cp_parser_require (parser, CPP_COMMA, "`,'");
5997 /* Build the (type *)null that begins the traditional offsetof macro. */
5998 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6000 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6001 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6005 cp_token *token = cp_lexer_peek_token (parser->lexer);
6006 switch (token->type)
6008 case CPP_OPEN_SQUARE:
6009 /* offsetof-member-designator "[" expression "]" */
6010 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6014 /* offsetof-member-designator "." identifier */
6015 cp_lexer_consume_token (parser->lexer);
6016 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6020 case CPP_CLOSE_PAREN:
6021 /* Consume the ")" token. */
6022 cp_lexer_consume_token (parser->lexer);
6026 /* Error. We know the following require will fail, but
6027 that gives the proper error message. */
6028 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6029 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6030 expr = error_mark_node;
6036 /* If we're processing a template, we can't finish the semantics yet.
6037 Otherwise we can fold the entire expression now. */
6038 if (processing_template_decl)
6039 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6041 expr = fold_offsetof (expr);
6044 parser->integral_constant_expression_p = save_ice_p;
6045 parser->non_integral_constant_expression_p = save_non_ice_p;
6050 /* Statements [gram.stmt.stmt] */
6052 /* Parse a statement.
6056 expression-statement
6061 declaration-statement
6064 IN_COMPOUND is true when the statement is nested inside a
6065 cp_parser_compound_statement; this matters for certain pragmas. */
6068 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6073 location_t statement_location;
6076 /* There is no statement yet. */
6077 statement = NULL_TREE;
6078 /* Peek at the next token. */
6079 token = cp_lexer_peek_token (parser->lexer);
6080 /* Remember the location of the first token in the statement. */
6081 statement_location = token->location;
6082 /* If this is a keyword, then that will often determine what kind of
6083 statement we have. */
6084 if (token->type == CPP_KEYWORD)
6086 enum rid keyword = token->keyword;
6092 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6098 statement = cp_parser_selection_statement (parser);
6104 statement = cp_parser_iteration_statement (parser);
6111 statement = cp_parser_jump_statement (parser);
6114 /* Objective-C++ exception-handling constructs. */
6117 case RID_AT_FINALLY:
6118 case RID_AT_SYNCHRONIZED:
6120 statement = cp_parser_objc_statement (parser);
6124 statement = cp_parser_try_block (parser);
6128 /* It might be a keyword like `int' that can start a
6129 declaration-statement. */
6133 else if (token->type == CPP_NAME)
6135 /* If the next token is a `:', then we are looking at a
6136 labeled-statement. */
6137 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6138 if (token->type == CPP_COLON)
6139 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6142 /* Anything that starts with a `{' must be a compound-statement. */
6143 else if (token->type == CPP_OPEN_BRACE)
6144 statement = cp_parser_compound_statement (parser, NULL, false);
6145 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6146 a statement all its own. */
6147 else if (token->type == CPP_PRAGMA)
6149 /* Only certain OpenMP pragmas are attached to statements, and thus
6150 are considered statements themselves. All others are not. In
6151 the context of a compound, accept the pragma as a "statement" and
6152 return so that we can check for a close brace. Otherwise we
6153 require a real statement and must go back and read one. */
6155 cp_parser_pragma (parser, pragma_compound);
6156 else if (!cp_parser_pragma (parser, pragma_stmt))
6160 else if (token->type == CPP_EOF)
6162 cp_parser_error (parser, "expected statement");
6166 /* Everything else must be a declaration-statement or an
6167 expression-statement. Try for the declaration-statement
6168 first, unless we are looking at a `;', in which case we know that
6169 we have an expression-statement. */
6172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6174 cp_parser_parse_tentatively (parser);
6175 /* Try to parse the declaration-statement. */
6176 cp_parser_declaration_statement (parser);
6177 /* If that worked, we're done. */
6178 if (cp_parser_parse_definitely (parser))
6181 /* Look for an expression-statement instead. */
6182 statement = cp_parser_expression_statement (parser, in_statement_expr);
6185 /* Set the line number for the statement. */
6186 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6187 SET_EXPR_LOCATION (statement, statement_location);
6190 /* Parse a labeled-statement.
6193 identifier : statement
6194 case constant-expression : statement
6200 case constant-expression ... constant-expression : statement
6202 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6203 For an ordinary label, returns a LABEL_EXPR.
6205 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6206 inside a compound. */
6209 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6213 tree statement = error_mark_node;
6215 /* The next token should be an identifier. */
6216 token = cp_lexer_peek_token (parser->lexer);
6217 if (token->type != CPP_NAME
6218 && token->type != CPP_KEYWORD)
6220 cp_parser_error (parser, "expected labeled-statement");
6221 return error_mark_node;
6224 switch (token->keyword)
6231 /* Consume the `case' token. */
6232 cp_lexer_consume_token (parser->lexer);
6233 /* Parse the constant-expression. */
6234 expr = cp_parser_constant_expression (parser,
6235 /*allow_non_constant_p=*/false,
6238 ellipsis = cp_lexer_peek_token (parser->lexer);
6239 if (ellipsis->type == CPP_ELLIPSIS)
6241 /* Consume the `...' token. */
6242 cp_lexer_consume_token (parser->lexer);
6244 cp_parser_constant_expression (parser,
6245 /*allow_non_constant_p=*/false,
6247 /* We don't need to emit warnings here, as the common code
6248 will do this for us. */
6251 expr_hi = NULL_TREE;
6253 if (parser->in_switch_statement_p)
6254 statement = finish_case_label (expr, expr_hi);
6256 error ("case label %qE not within a switch statement", expr);
6261 /* Consume the `default' token. */
6262 cp_lexer_consume_token (parser->lexer);
6264 if (parser->in_switch_statement_p)
6265 statement = finish_case_label (NULL_TREE, NULL_TREE);
6267 error ("case label not within a switch statement");
6271 /* Anything else must be an ordinary label. */
6272 statement = finish_label_stmt (cp_parser_identifier (parser));
6276 /* Require the `:' token. */
6277 cp_parser_require (parser, CPP_COLON, "`:'");
6278 /* Parse the labeled statement. */
6279 cp_parser_statement (parser, in_statement_expr, in_compound);
6281 /* Return the label, in the case of a `case' or `default' label. */
6285 /* Parse an expression-statement.
6287 expression-statement:
6290 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6291 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6292 indicates whether this expression-statement is part of an
6293 expression statement. */
6296 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6298 tree statement = NULL_TREE;
6300 /* If the next token is a ';', then there is no expression
6302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6303 statement = cp_parser_expression (parser, /*cast_p=*/false);
6305 /* Consume the final `;'. */
6306 cp_parser_consume_semicolon_at_end_of_statement (parser);
6308 if (in_statement_expr
6309 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6310 /* This is the final expression statement of a statement
6312 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6314 statement = finish_expr_stmt (statement);
6321 /* Parse a compound-statement.
6324 { statement-seq [opt] }
6326 Returns a tree representing the statement. */
6329 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6334 /* Consume the `{'. */
6335 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6336 return error_mark_node;
6337 /* Begin the compound-statement. */
6338 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6339 /* Parse an (optional) statement-seq. */
6340 cp_parser_statement_seq_opt (parser, in_statement_expr);
6341 /* Finish the compound-statement. */
6342 finish_compound_stmt (compound_stmt);
6343 /* Consume the `}'. */
6344 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6346 return compound_stmt;
6349 /* Parse an (optional) statement-seq.
6353 statement-seq [opt] statement */
6356 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6358 /* Scan statements until there aren't any more. */
6361 cp_token *token = cp_lexer_peek_token (parser->lexer);
6363 /* If we're looking at a `}', then we've run out of statements. */
6364 if (token->type == CPP_CLOSE_BRACE
6365 || token->type == CPP_EOF
6366 || token->type == CPP_PRAGMA_EOL)
6369 /* Parse the statement. */
6370 cp_parser_statement (parser, in_statement_expr, true);
6374 /* Parse a selection-statement.
6376 selection-statement:
6377 if ( condition ) statement
6378 if ( condition ) statement else statement
6379 switch ( condition ) statement
6381 Returns the new IF_STMT or SWITCH_STMT. */
6384 cp_parser_selection_statement (cp_parser* parser)
6389 /* Peek at the next token. */
6390 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6392 /* See what kind of keyword it is. */
6393 keyword = token->keyword;
6402 /* Look for the `('. */
6403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6405 cp_parser_skip_to_end_of_statement (parser);
6406 return error_mark_node;
6409 /* Begin the selection-statement. */
6410 if (keyword == RID_IF)
6411 statement = begin_if_stmt ();
6413 statement = begin_switch_stmt ();
6415 /* Parse the condition. */
6416 condition = cp_parser_condition (parser);
6417 /* Look for the `)'. */
6418 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6419 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6420 /*consume_paren=*/true);
6422 if (keyword == RID_IF)
6424 /* Add the condition. */
6425 finish_if_stmt_cond (condition, statement);
6427 /* Parse the then-clause. */
6428 cp_parser_implicitly_scoped_statement (parser);
6429 finish_then_clause (statement);
6431 /* If the next token is `else', parse the else-clause. */
6432 if (cp_lexer_next_token_is_keyword (parser->lexer,
6435 /* Consume the `else' keyword. */
6436 cp_lexer_consume_token (parser->lexer);
6437 begin_else_clause (statement);
6438 /* Parse the else-clause. */
6439 cp_parser_implicitly_scoped_statement (parser);
6440 finish_else_clause (statement);
6443 /* Now we're all done with the if-statement. */
6444 finish_if_stmt (statement);
6448 bool in_switch_statement_p;
6449 unsigned char in_statement;
6451 /* Add the condition. */
6452 finish_switch_cond (condition, statement);
6454 /* Parse the body of the switch-statement. */
6455 in_switch_statement_p = parser->in_switch_statement_p;
6456 in_statement = parser->in_statement;
6457 parser->in_switch_statement_p = true;
6458 parser->in_statement |= IN_SWITCH_STMT;
6459 cp_parser_implicitly_scoped_statement (parser);
6460 parser->in_switch_statement_p = in_switch_statement_p;
6461 parser->in_statement = in_statement;
6463 /* Now we're all done with the switch-statement. */
6464 finish_switch_stmt (statement);
6472 cp_parser_error (parser, "expected selection-statement");
6473 return error_mark_node;
6477 /* Parse a condition.
6481 type-specifier-seq declarator = assignment-expression
6486 type-specifier-seq declarator asm-specification [opt]
6487 attributes [opt] = assignment-expression
6489 Returns the expression that should be tested. */
6492 cp_parser_condition (cp_parser* parser)
6494 cp_decl_specifier_seq type_specifiers;
6495 const char *saved_message;
6497 /* Try the declaration first. */
6498 cp_parser_parse_tentatively (parser);
6499 /* New types are not allowed in the type-specifier-seq for a
6501 saved_message = parser->type_definition_forbidden_message;
6502 parser->type_definition_forbidden_message
6503 = "types may not be defined in conditions";
6504 /* Parse the type-specifier-seq. */
6505 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6507 /* Restore the saved message. */
6508 parser->type_definition_forbidden_message = saved_message;
6509 /* If all is well, we might be looking at a declaration. */
6510 if (!cp_parser_error_occurred (parser))
6513 tree asm_specification;
6515 cp_declarator *declarator;
6516 tree initializer = NULL_TREE;
6518 /* Parse the declarator. */
6519 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6520 /*ctor_dtor_or_conv_p=*/NULL,
6521 /*parenthesized_p=*/NULL,
6522 /*member_p=*/false);
6523 /* Parse the attributes. */
6524 attributes = cp_parser_attributes_opt (parser);
6525 /* Parse the asm-specification. */
6526 asm_specification = cp_parser_asm_specification_opt (parser);
6527 /* If the next token is not an `=', then we might still be
6528 looking at an expression. For example:
6532 looks like a decl-specifier-seq and a declarator -- but then
6533 there is no `=', so this is an expression. */
6534 cp_parser_require (parser, CPP_EQ, "`='");
6535 /* If we did see an `=', then we are looking at a declaration
6537 if (cp_parser_parse_definitely (parser))
6540 bool non_constant_p;
6542 /* Create the declaration. */
6543 decl = start_decl (declarator, &type_specifiers,
6544 /*initialized_p=*/true,
6545 attributes, /*prefix_attributes=*/NULL_TREE,
6547 /* Parse the assignment-expression. */
6549 = cp_parser_constant_expression (parser,
6550 /*allow_non_constant_p=*/true,
6552 if (!non_constant_p)
6553 initializer = fold_non_dependent_expr (initializer);
6555 /* Process the initializer. */
6556 cp_finish_decl (decl,
6557 initializer, !non_constant_p,
6559 LOOKUP_ONLYCONVERTING);
6562 pop_scope (pushed_scope);
6564 return convert_from_reference (decl);
6567 /* If we didn't even get past the declarator successfully, we are
6568 definitely not looking at a declaration. */
6570 cp_parser_abort_tentative_parse (parser);
6572 /* Otherwise, we are looking at an expression. */
6573 return cp_parser_expression (parser, /*cast_p=*/false);
6576 /* Parse an iteration-statement.
6578 iteration-statement:
6579 while ( condition ) statement
6580 do statement while ( expression ) ;
6581 for ( for-init-statement condition [opt] ; expression [opt] )
6584 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6587 cp_parser_iteration_statement (cp_parser* parser)
6592 unsigned char in_statement;
6594 /* Peek at the next token. */
6595 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6597 return error_mark_node;
6599 /* Remember whether or not we are already within an iteration
6601 in_statement = parser->in_statement;
6603 /* See what kind of keyword it is. */
6604 keyword = token->keyword;
6611 /* Begin the while-statement. */
6612 statement = begin_while_stmt ();
6613 /* Look for the `('. */
6614 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6615 /* Parse the condition. */
6616 condition = cp_parser_condition (parser);
6617 finish_while_stmt_cond (condition, statement);
6618 /* Look for the `)'. */
6619 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6620 /* Parse the dependent statement. */
6621 parser->in_statement = IN_ITERATION_STMT;
6622 cp_parser_already_scoped_statement (parser);
6623 parser->in_statement = in_statement;
6624 /* We're done with the while-statement. */
6625 finish_while_stmt (statement);
6633 /* Begin the do-statement. */
6634 statement = begin_do_stmt ();
6635 /* Parse the body of the do-statement. */
6636 parser->in_statement = IN_ITERATION_STMT;
6637 cp_parser_implicitly_scoped_statement (parser);
6638 parser->in_statement = in_statement;
6639 finish_do_body (statement);
6640 /* Look for the `while' keyword. */
6641 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6642 /* Look for the `('. */
6643 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6644 /* Parse the expression. */
6645 expression = cp_parser_expression (parser, /*cast_p=*/false);
6646 /* We're done with the do-statement. */
6647 finish_do_stmt (expression, statement);
6648 /* Look for the `)'. */
6649 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6650 /* Look for the `;'. */
6651 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6657 tree condition = NULL_TREE;
6658 tree expression = NULL_TREE;
6660 /* Begin the for-statement. */
6661 statement = begin_for_stmt ();
6662 /* Look for the `('. */
6663 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6664 /* Parse the initialization. */
6665 cp_parser_for_init_statement (parser);
6666 finish_for_init_stmt (statement);
6668 /* If there's a condition, process it. */
6669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6670 condition = cp_parser_condition (parser);
6671 finish_for_cond (condition, statement);
6672 /* Look for the `;'. */
6673 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6675 /* If there's an expression, process it. */
6676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6677 expression = cp_parser_expression (parser, /*cast_p=*/false);
6678 finish_for_expr (expression, statement);
6679 /* Look for the `)'. */
6680 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6682 /* Parse the body of the for-statement. */
6683 parser->in_statement = IN_ITERATION_STMT;
6684 cp_parser_already_scoped_statement (parser);
6685 parser->in_statement = in_statement;
6687 /* We're done with the for-statement. */
6688 finish_for_stmt (statement);
6693 cp_parser_error (parser, "expected iteration-statement");
6694 statement = error_mark_node;
6701 /* Parse a for-init-statement.
6704 expression-statement
6705 simple-declaration */
6708 cp_parser_for_init_statement (cp_parser* parser)
6710 /* If the next token is a `;', then we have an empty
6711 expression-statement. Grammatically, this is also a
6712 simple-declaration, but an invalid one, because it does not
6713 declare anything. Therefore, if we did not handle this case
6714 specially, we would issue an error message about an invalid
6716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6718 /* We're going to speculatively look for a declaration, falling back
6719 to an expression, if necessary. */
6720 cp_parser_parse_tentatively (parser);
6721 /* Parse the declaration. */
6722 cp_parser_simple_declaration (parser,
6723 /*function_definition_allowed_p=*/false);
6724 /* If the tentative parse failed, then we shall need to look for an
6725 expression-statement. */
6726 if (cp_parser_parse_definitely (parser))
6730 cp_parser_expression_statement (parser, false);
6733 /* Parse a jump-statement.
6738 return expression [opt] ;
6746 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6749 cp_parser_jump_statement (cp_parser* parser)
6751 tree statement = error_mark_node;
6755 /* Peek at the next token. */
6756 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6758 return error_mark_node;
6760 /* See what kind of keyword it is. */
6761 keyword = token->keyword;
6765 switch (parser->in_statement)
6768 error ("break statement not within loop or switch");
6771 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6772 || parser->in_statement == IN_ITERATION_STMT);
6773 statement = finish_break_stmt ();
6776 error ("invalid exit from OpenMP structured block");
6779 error ("break statement used with OpenMP for loop");
6782 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6786 switch (parser->in_statement & ~IN_SWITCH_STMT)
6789 error ("continue statement not within a loop");
6791 case IN_ITERATION_STMT:
6793 statement = finish_continue_stmt ();
6796 error ("invalid exit from OpenMP structured block");
6801 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6808 /* If the next token is a `;', then there is no
6810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6811 expr = cp_parser_expression (parser, /*cast_p=*/false);
6814 /* Build the return-statement. */
6815 statement = finish_return_stmt (expr);
6816 /* Look for the final `;'. */
6817 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6822 /* Create the goto-statement. */
6823 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6825 /* Issue a warning about this use of a GNU extension. */
6827 pedwarn ("ISO C++ forbids computed gotos");
6828 /* Consume the '*' token. */
6829 cp_lexer_consume_token (parser->lexer);
6830 /* Parse the dependent expression. */
6831 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6834 finish_goto_stmt (cp_parser_identifier (parser));
6835 /* Look for the final `;'. */
6836 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6840 cp_parser_error (parser, "expected jump-statement");
6847 /* Parse a declaration-statement.
6849 declaration-statement:
6850 block-declaration */
6853 cp_parser_declaration_statement (cp_parser* parser)
6857 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6858 p = obstack_alloc (&declarator_obstack, 0);
6860 /* Parse the block-declaration. */
6861 cp_parser_block_declaration (parser, /*statement_p=*/true);
6863 /* Free any declarators allocated. */
6864 obstack_free (&declarator_obstack, p);
6866 /* Finish off the statement. */
6870 /* Some dependent statements (like `if (cond) statement'), are
6871 implicitly in their own scope. In other words, if the statement is
6872 a single statement (as opposed to a compound-statement), it is
6873 none-the-less treated as if it were enclosed in braces. Any
6874 declarations appearing in the dependent statement are out of scope
6875 after control passes that point. This function parses a statement,
6876 but ensures that is in its own scope, even if it is not a
6879 Returns the new statement. */
6882 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6886 /* Mark if () ; with a special NOP_EXPR. */
6887 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6889 cp_lexer_consume_token (parser->lexer);
6890 statement = add_stmt (build_empty_stmt ());
6892 /* if a compound is opened, we simply parse the statement directly. */
6893 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6894 statement = cp_parser_compound_statement (parser, NULL, false);
6895 /* If the token is not a `{', then we must take special action. */
6898 /* Create a compound-statement. */
6899 statement = begin_compound_stmt (0);
6900 /* Parse the dependent-statement. */
6901 cp_parser_statement (parser, NULL_TREE, false);
6902 /* Finish the dummy compound-statement. */
6903 finish_compound_stmt (statement);
6906 /* Return the statement. */
6910 /* For some dependent statements (like `while (cond) statement'), we
6911 have already created a scope. Therefore, even if the dependent
6912 statement is a compound-statement, we do not want to create another
6916 cp_parser_already_scoped_statement (cp_parser* parser)
6918 /* If the token is a `{', then we must take special action. */
6919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6920 cp_parser_statement (parser, NULL_TREE, false);
6923 /* Avoid calling cp_parser_compound_statement, so that we
6924 don't create a new scope. Do everything else by hand. */
6925 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6926 cp_parser_statement_seq_opt (parser, NULL_TREE);
6927 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6931 /* Declarations [gram.dcl.dcl] */
6933 /* Parse an optional declaration-sequence.
6937 declaration-seq declaration */
6940 cp_parser_declaration_seq_opt (cp_parser* parser)
6946 token = cp_lexer_peek_token (parser->lexer);
6948 if (token->type == CPP_CLOSE_BRACE
6949 || token->type == CPP_EOF
6950 || token->type == CPP_PRAGMA_EOL)
6953 if (token->type == CPP_SEMICOLON)
6955 /* A declaration consisting of a single semicolon is
6956 invalid. Allow it unless we're being pedantic. */
6957 cp_lexer_consume_token (parser->lexer);
6958 if (pedantic && !in_system_header)
6959 pedwarn ("extra %<;%>");
6963 /* If we're entering or exiting a region that's implicitly
6964 extern "C", modify the lang context appropriately. */
6965 if (!parser->implicit_extern_c && token->implicit_extern_c)
6967 push_lang_context (lang_name_c);
6968 parser->implicit_extern_c = true;
6970 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6972 pop_lang_context ();
6973 parser->implicit_extern_c = false;
6976 if (token->type == CPP_PRAGMA)
6978 /* A top-level declaration can consist solely of a #pragma.
6979 A nested declaration cannot, so this is done here and not
6980 in cp_parser_declaration. (A #pragma at block scope is
6981 handled in cp_parser_statement.) */
6982 cp_parser_pragma (parser, pragma_external);
6986 /* Parse the declaration itself. */
6987 cp_parser_declaration (parser);
6991 /* Parse a declaration.
6996 template-declaration
6997 explicit-instantiation
6998 explicit-specialization
6999 linkage-specification
7000 namespace-definition
7005 __extension__ declaration */
7008 cp_parser_declaration (cp_parser* parser)
7015 /* Check for the `__extension__' keyword. */
7016 if (cp_parser_extension_opt (parser, &saved_pedantic))
7018 /* Parse the qualified declaration. */
7019 cp_parser_declaration (parser);
7020 /* Restore the PEDANTIC flag. */
7021 pedantic = saved_pedantic;
7026 /* Try to figure out what kind of declaration is present. */
7027 token1 = *cp_lexer_peek_token (parser->lexer);
7029 if (token1.type != CPP_EOF)
7030 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7033 token2.type = CPP_EOF;
7034 token2.keyword = RID_MAX;
7037 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7038 p = obstack_alloc (&declarator_obstack, 0);
7040 /* If the next token is `extern' and the following token is a string
7041 literal, then we have a linkage specification. */
7042 if (token1.keyword == RID_EXTERN
7043 && cp_parser_is_string_literal (&token2))
7044 cp_parser_linkage_specification (parser);
7045 /* If the next token is `template', then we have either a template
7046 declaration, an explicit instantiation, or an explicit
7048 else if (token1.keyword == RID_TEMPLATE)
7050 /* `template <>' indicates a template specialization. */
7051 if (token2.type == CPP_LESS
7052 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7053 cp_parser_explicit_specialization (parser);
7054 /* `template <' indicates a template declaration. */
7055 else if (token2.type == CPP_LESS)
7056 cp_parser_template_declaration (parser, /*member_p=*/false);
7057 /* Anything else must be an explicit instantiation. */
7059 cp_parser_explicit_instantiation (parser);
7061 /* If the next token is `export', then we have a template
7063 else if (token1.keyword == RID_EXPORT)
7064 cp_parser_template_declaration (parser, /*member_p=*/false);
7065 /* If the next token is `extern', 'static' or 'inline' and the one
7066 after that is `template', we have a GNU extended explicit
7067 instantiation directive. */
7068 else if (cp_parser_allow_gnu_extensions_p (parser)
7069 && (token1.keyword == RID_EXTERN
7070 || token1.keyword == RID_STATIC
7071 || token1.keyword == RID_INLINE)
7072 && token2.keyword == RID_TEMPLATE)
7073 cp_parser_explicit_instantiation (parser);
7074 /* If the next token is `namespace', check for a named or unnamed
7075 namespace definition. */
7076 else if (token1.keyword == RID_NAMESPACE
7077 && (/* A named namespace definition. */
7078 (token2.type == CPP_NAME
7079 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7081 /* An unnamed namespace definition. */
7082 || token2.type == CPP_OPEN_BRACE
7083 || token2.keyword == RID_ATTRIBUTE))
7084 cp_parser_namespace_definition (parser);
7085 /* Objective-C++ declaration/definition. */
7086 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7087 cp_parser_objc_declaration (parser);
7088 /* We must have either a block declaration or a function
7091 /* Try to parse a block-declaration, or a function-definition. */
7092 cp_parser_block_declaration (parser, /*statement_p=*/false);
7094 /* Free any declarators allocated. */
7095 obstack_free (&declarator_obstack, p);
7098 /* Parse a block-declaration.
7103 namespace-alias-definition
7110 __extension__ block-declaration
7113 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7114 part of a declaration-statement. */
7117 cp_parser_block_declaration (cp_parser *parser,
7123 /* Check for the `__extension__' keyword. */
7124 if (cp_parser_extension_opt (parser, &saved_pedantic))
7126 /* Parse the qualified declaration. */
7127 cp_parser_block_declaration (parser, statement_p);
7128 /* Restore the PEDANTIC flag. */
7129 pedantic = saved_pedantic;
7134 /* Peek at the next token to figure out which kind of declaration is
7136 token1 = cp_lexer_peek_token (parser->lexer);
7138 /* If the next keyword is `asm', we have an asm-definition. */
7139 if (token1->keyword == RID_ASM)
7142 cp_parser_commit_to_tentative_parse (parser);
7143 cp_parser_asm_definition (parser);
7145 /* If the next keyword is `namespace', we have a
7146 namespace-alias-definition. */
7147 else if (token1->keyword == RID_NAMESPACE)
7148 cp_parser_namespace_alias_definition (parser);
7149 /* If the next keyword is `using', we have either a
7150 using-declaration or a using-directive. */
7151 else if (token1->keyword == RID_USING)
7156 cp_parser_commit_to_tentative_parse (parser);
7157 /* If the token after `using' is `namespace', then we have a
7159 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7160 if (token2->keyword == RID_NAMESPACE)
7161 cp_parser_using_directive (parser);
7162 /* Otherwise, it's a using-declaration. */
7164 cp_parser_using_declaration (parser);
7166 /* If the next keyword is `__label__' we have a label declaration. */
7167 else if (token1->keyword == RID_LABEL)
7170 cp_parser_commit_to_tentative_parse (parser);
7171 cp_parser_label_declaration (parser);
7173 /* Anything else must be a simple-declaration. */
7175 cp_parser_simple_declaration (parser, !statement_p);
7178 /* Parse a simple-declaration.
7181 decl-specifier-seq [opt] init-declarator-list [opt] ;
7183 init-declarator-list:
7185 init-declarator-list , init-declarator
7187 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7188 function-definition as a simple-declaration. */
7191 cp_parser_simple_declaration (cp_parser* parser,
7192 bool function_definition_allowed_p)
7194 cp_decl_specifier_seq decl_specifiers;
7195 int declares_class_or_enum;
7196 bool saw_declarator;
7198 /* Defer access checks until we know what is being declared; the
7199 checks for names appearing in the decl-specifier-seq should be
7200 done as if we were in the scope of the thing being declared. */
7201 push_deferring_access_checks (dk_deferred);
7203 /* Parse the decl-specifier-seq. We have to keep track of whether
7204 or not the decl-specifier-seq declares a named class or
7205 enumeration type, since that is the only case in which the
7206 init-declarator-list is allowed to be empty.
7210 In a simple-declaration, the optional init-declarator-list can be
7211 omitted only when declaring a class or enumeration, that is when
7212 the decl-specifier-seq contains either a class-specifier, an
7213 elaborated-type-specifier, or an enum-specifier. */
7214 cp_parser_decl_specifier_seq (parser,
7215 CP_PARSER_FLAGS_OPTIONAL,
7217 &declares_class_or_enum);
7218 /* We no longer need to defer access checks. */
7219 stop_deferring_access_checks ();
7221 /* In a block scope, a valid declaration must always have a
7222 decl-specifier-seq. By not trying to parse declarators, we can
7223 resolve the declaration/expression ambiguity more quickly. */
7224 if (!function_definition_allowed_p
7225 && !decl_specifiers.any_specifiers_p)
7227 cp_parser_error (parser, "expected declaration");
7231 /* If the next two tokens are both identifiers, the code is
7232 erroneous. The usual cause of this situation is code like:
7236 where "T" should name a type -- but does not. */
7237 if (!decl_specifiers.type
7238 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7240 /* If parsing tentatively, we should commit; we really are
7241 looking at a declaration. */
7242 cp_parser_commit_to_tentative_parse (parser);
7247 /* If we have seen at least one decl-specifier, and the next token
7248 is not a parenthesis, then we must be looking at a declaration.
7249 (After "int (" we might be looking at a functional cast.) */
7250 if (decl_specifiers.any_specifiers_p
7251 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7252 cp_parser_commit_to_tentative_parse (parser);
7254 /* Keep going until we hit the `;' at the end of the simple
7256 saw_declarator = false;
7257 while (cp_lexer_next_token_is_not (parser->lexer,
7261 bool function_definition_p;
7266 /* If we are processing next declarator, coma is expected */
7267 token = cp_lexer_peek_token (parser->lexer);
7268 gcc_assert (token->type == CPP_COMMA);
7269 cp_lexer_consume_token (parser->lexer);
7272 saw_declarator = true;
7274 /* Parse the init-declarator. */
7275 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7276 /*checks=*/NULL_TREE,
7277 function_definition_allowed_p,
7279 declares_class_or_enum,
7280 &function_definition_p);
7281 /* If an error occurred while parsing tentatively, exit quickly.
7282 (That usually happens when in the body of a function; each
7283 statement is treated as a declaration-statement until proven
7285 if (cp_parser_error_occurred (parser))
7287 /* Handle function definitions specially. */
7288 if (function_definition_p)
7290 /* If the next token is a `,', then we are probably
7291 processing something like:
7295 which is erroneous. */
7296 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7297 error ("mixing declarations and function-definitions is forbidden");
7298 /* Otherwise, we're done with the list of declarators. */
7301 pop_deferring_access_checks ();
7305 /* The next token should be either a `,' or a `;'. */
7306 token = cp_lexer_peek_token (parser->lexer);
7307 /* If it's a `,', there are more declarators to come. */
7308 if (token->type == CPP_COMMA)
7309 /* will be consumed next time around */;
7310 /* If it's a `;', we are done. */
7311 else if (token->type == CPP_SEMICOLON)
7313 /* Anything else is an error. */
7316 /* If we have already issued an error message we don't need
7317 to issue another one. */
7318 if (decl != error_mark_node
7319 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7320 cp_parser_error (parser, "expected %<,%> or %<;%>");
7321 /* Skip tokens until we reach the end of the statement. */
7322 cp_parser_skip_to_end_of_statement (parser);
7323 /* If the next token is now a `;', consume it. */
7324 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7325 cp_lexer_consume_token (parser->lexer);
7328 /* After the first time around, a function-definition is not
7329 allowed -- even if it was OK at first. For example:
7334 function_definition_allowed_p = false;
7337 /* Issue an error message if no declarators are present, and the
7338 decl-specifier-seq does not itself declare a class or
7340 if (!saw_declarator)
7342 if (cp_parser_declares_only_class_p (parser))
7343 shadow_tag (&decl_specifiers);
7344 /* Perform any deferred access checks. */
7345 perform_deferred_access_checks ();
7348 /* Consume the `;'. */
7349 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7352 pop_deferring_access_checks ();
7355 /* Parse a decl-specifier-seq.
7358 decl-specifier-seq [opt] decl-specifier
7361 storage-class-specifier
7372 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7374 The parser flags FLAGS is used to control type-specifier parsing.
7376 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7379 1: one of the decl-specifiers is an elaborated-type-specifier
7380 (i.e., a type declaration)
7381 2: one of the decl-specifiers is an enum-specifier or a
7382 class-specifier (i.e., a type definition)
7387 cp_parser_decl_specifier_seq (cp_parser* parser,
7388 cp_parser_flags flags,
7389 cp_decl_specifier_seq *decl_specs,
7390 int* declares_class_or_enum)
7392 bool constructor_possible_p = !parser->in_declarator_p;
7395 /* Clear DECL_SPECS. */
7396 clear_decl_specs (decl_specs);
7398 /* Assume no class or enumeration type is declared. */
7399 *declares_class_or_enum = 0;
7401 /* Keep reading specifiers until there are no more to read. */
7405 bool found_decl_spec;
7408 /* Peek at the next token. */
7409 token = cp_lexer_peek_token (parser->lexer);
7410 /* Handle attributes. */
7411 if (token->keyword == RID_ATTRIBUTE)
7413 /* Parse the attributes. */
7414 decl_specs->attributes
7415 = chainon (decl_specs->attributes,
7416 cp_parser_attributes_opt (parser));
7419 /* Assume we will find a decl-specifier keyword. */
7420 found_decl_spec = true;
7421 /* If the next token is an appropriate keyword, we can simply
7422 add it to the list. */
7423 switch (token->keyword)
7428 ++decl_specs->specs[(int) ds_friend];
7429 /* Consume the token. */
7430 cp_lexer_consume_token (parser->lexer);
7433 /* function-specifier:
7440 cp_parser_function_specifier_opt (parser, decl_specs);
7446 ++decl_specs->specs[(int) ds_typedef];
7447 /* Consume the token. */
7448 cp_lexer_consume_token (parser->lexer);
7449 /* A constructor declarator cannot appear in a typedef. */
7450 constructor_possible_p = false;
7451 /* The "typedef" keyword can only occur in a declaration; we
7452 may as well commit at this point. */
7453 cp_parser_commit_to_tentative_parse (parser);
7456 /* storage-class-specifier:
7466 /* Consume the token. */
7467 cp_lexer_consume_token (parser->lexer);
7468 cp_parser_set_storage_class (decl_specs, sc_auto);
7471 /* Consume the token. */
7472 cp_lexer_consume_token (parser->lexer);
7473 cp_parser_set_storage_class (decl_specs, sc_register);
7476 /* Consume the token. */
7477 cp_lexer_consume_token (parser->lexer);
7478 if (decl_specs->specs[(int) ds_thread])
7480 error ("%<__thread%> before %<static%>");
7481 decl_specs->specs[(int) ds_thread] = 0;
7483 cp_parser_set_storage_class (decl_specs, sc_static);
7486 /* Consume the token. */
7487 cp_lexer_consume_token (parser->lexer);
7488 if (decl_specs->specs[(int) ds_thread])
7490 error ("%<__thread%> before %<extern%>");
7491 decl_specs->specs[(int) ds_thread] = 0;
7493 cp_parser_set_storage_class (decl_specs, sc_extern);
7496 /* Consume the token. */
7497 cp_lexer_consume_token (parser->lexer);
7498 cp_parser_set_storage_class (decl_specs, sc_mutable);
7501 /* Consume the token. */
7502 cp_lexer_consume_token (parser->lexer);
7503 ++decl_specs->specs[(int) ds_thread];
7507 /* We did not yet find a decl-specifier yet. */
7508 found_decl_spec = false;
7512 /* Constructors are a special case. The `S' in `S()' is not a
7513 decl-specifier; it is the beginning of the declarator. */
7516 && constructor_possible_p
7517 && (cp_parser_constructor_declarator_p
7518 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7520 /* If we don't have a DECL_SPEC yet, then we must be looking at
7521 a type-specifier. */
7522 if (!found_decl_spec && !constructor_p)
7524 int decl_spec_declares_class_or_enum;
7525 bool is_cv_qualifier;
7529 = cp_parser_type_specifier (parser, flags,
7531 /*is_declaration=*/true,
7532 &decl_spec_declares_class_or_enum,
7535 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7537 /* If this type-specifier referenced a user-defined type
7538 (a typedef, class-name, etc.), then we can't allow any
7539 more such type-specifiers henceforth.
7543 The longest sequence of decl-specifiers that could
7544 possibly be a type name is taken as the
7545 decl-specifier-seq of a declaration. The sequence shall
7546 be self-consistent as described below.
7550 As a general rule, at most one type-specifier is allowed
7551 in the complete decl-specifier-seq of a declaration. The
7552 only exceptions are the following:
7554 -- const or volatile can be combined with any other
7557 -- signed or unsigned can be combined with char, long,
7565 void g (const int Pc);
7567 Here, Pc is *not* part of the decl-specifier seq; it's
7568 the declarator. Therefore, once we see a type-specifier
7569 (other than a cv-qualifier), we forbid any additional
7570 user-defined types. We *do* still allow things like `int
7571 int' to be considered a decl-specifier-seq, and issue the
7572 error message later. */
7573 if (type_spec && !is_cv_qualifier)
7574 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7575 /* A constructor declarator cannot follow a type-specifier. */
7578 constructor_possible_p = false;
7579 found_decl_spec = true;
7583 /* If we still do not have a DECL_SPEC, then there are no more
7585 if (!found_decl_spec)
7588 decl_specs->any_specifiers_p = true;
7589 /* After we see one decl-specifier, further decl-specifiers are
7591 flags |= CP_PARSER_FLAGS_OPTIONAL;
7594 /* Check for repeated decl-specifiers. */
7595 for (ds = ds_first; ds != ds_last; ++ds)
7597 unsigned count = decl_specs->specs[(int)ds];
7600 /* The "long" specifier is a special case because of "long long". */
7604 error ("%<long long long%> is too long for GCC");
7605 else if (pedantic && !in_system_header && warn_long_long)
7606 pedwarn ("ISO C++ does not support %<long long%>");
7610 static const char *const decl_spec_names[] = {
7626 error ("duplicate %qs", decl_spec_names[(int)ds]);
7630 /* Don't allow a friend specifier with a class definition. */
7631 if (decl_specs->specs[(int) ds_friend] != 0
7632 && (*declares_class_or_enum & 2))
7633 error ("class definition may not be declared a friend");
7636 /* Parse an (optional) storage-class-specifier.
7638 storage-class-specifier:
7647 storage-class-specifier:
7650 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7653 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7655 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7663 /* Consume the token. */
7664 return cp_lexer_consume_token (parser->lexer)->value;
7671 /* Parse an (optional) function-specifier.
7678 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7679 Updates DECL_SPECS, if it is non-NULL. */
7682 cp_parser_function_specifier_opt (cp_parser* parser,
7683 cp_decl_specifier_seq *decl_specs)
7685 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7689 ++decl_specs->specs[(int) ds_inline];
7694 ++decl_specs->specs[(int) ds_virtual];
7699 ++decl_specs->specs[(int) ds_explicit];
7706 /* Consume the token. */
7707 return cp_lexer_consume_token (parser->lexer)->value;
7710 /* Parse a linkage-specification.
7712 linkage-specification:
7713 extern string-literal { declaration-seq [opt] }
7714 extern string-literal declaration */
7717 cp_parser_linkage_specification (cp_parser* parser)
7721 /* Look for the `extern' keyword. */
7722 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7724 /* Look for the string-literal. */
7725 linkage = cp_parser_string_literal (parser, false, false);
7727 /* Transform the literal into an identifier. If the literal is a
7728 wide-character string, or contains embedded NULs, then we can't
7729 handle it as the user wants. */
7730 if (strlen (TREE_STRING_POINTER (linkage))
7731 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7733 cp_parser_error (parser, "invalid linkage-specification");
7734 /* Assume C++ linkage. */
7735 linkage = lang_name_cplusplus;
7738 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7740 /* We're now using the new linkage. */
7741 push_lang_context (linkage);
7743 /* If the next token is a `{', then we're using the first
7745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7747 /* Consume the `{' token. */
7748 cp_lexer_consume_token (parser->lexer);
7749 /* Parse the declarations. */
7750 cp_parser_declaration_seq_opt (parser);
7751 /* Look for the closing `}'. */
7752 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7754 /* Otherwise, there's just one declaration. */
7757 bool saved_in_unbraced_linkage_specification_p;
7759 saved_in_unbraced_linkage_specification_p
7760 = parser->in_unbraced_linkage_specification_p;
7761 parser->in_unbraced_linkage_specification_p = true;
7762 have_extern_spec = true;
7763 cp_parser_declaration (parser);
7764 have_extern_spec = false;
7765 parser->in_unbraced_linkage_specification_p
7766 = saved_in_unbraced_linkage_specification_p;
7769 /* We're done with the linkage-specification. */
7770 pop_lang_context ();
7773 /* Special member functions [gram.special] */
7775 /* Parse a conversion-function-id.
7777 conversion-function-id:
7778 operator conversion-type-id
7780 Returns an IDENTIFIER_NODE representing the operator. */
7783 cp_parser_conversion_function_id (cp_parser* parser)
7787 tree saved_qualifying_scope;
7788 tree saved_object_scope;
7789 tree pushed_scope = NULL_TREE;
7791 /* Look for the `operator' token. */
7792 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7793 return error_mark_node;
7794 /* When we parse the conversion-type-id, the current scope will be
7795 reset. However, we need that information in able to look up the
7796 conversion function later, so we save it here. */
7797 saved_scope = parser->scope;
7798 saved_qualifying_scope = parser->qualifying_scope;
7799 saved_object_scope = parser->object_scope;
7800 /* We must enter the scope of the class so that the names of
7801 entities declared within the class are available in the
7802 conversion-type-id. For example, consider:
7809 S::operator I() { ... }
7811 In order to see that `I' is a type-name in the definition, we
7812 must be in the scope of `S'. */
7814 pushed_scope = push_scope (saved_scope);
7815 /* Parse the conversion-type-id. */
7816 type = cp_parser_conversion_type_id (parser);
7817 /* Leave the scope of the class, if any. */
7819 pop_scope (pushed_scope);
7820 /* Restore the saved scope. */
7821 parser->scope = saved_scope;
7822 parser->qualifying_scope = saved_qualifying_scope;
7823 parser->object_scope = saved_object_scope;
7824 /* If the TYPE is invalid, indicate failure. */
7825 if (type == error_mark_node)
7826 return error_mark_node;
7827 return mangle_conv_op_name_for_type (type);
7830 /* Parse a conversion-type-id:
7833 type-specifier-seq conversion-declarator [opt]
7835 Returns the TYPE specified. */
7838 cp_parser_conversion_type_id (cp_parser* parser)
7841 cp_decl_specifier_seq type_specifiers;
7842 cp_declarator *declarator;
7843 tree type_specified;
7845 /* Parse the attributes. */
7846 attributes = cp_parser_attributes_opt (parser);
7847 /* Parse the type-specifiers. */
7848 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7850 /* If that didn't work, stop. */
7851 if (type_specifiers.type == error_mark_node)
7852 return error_mark_node;
7853 /* Parse the conversion-declarator. */
7854 declarator = cp_parser_conversion_declarator_opt (parser);
7856 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7857 /*initialized=*/0, &attributes);
7859 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7860 return type_specified;
7863 /* Parse an (optional) conversion-declarator.
7865 conversion-declarator:
7866 ptr-operator conversion-declarator [opt]
7870 static cp_declarator *
7871 cp_parser_conversion_declarator_opt (cp_parser* parser)
7873 enum tree_code code;
7875 cp_cv_quals cv_quals;
7877 /* We don't know if there's a ptr-operator next, or not. */
7878 cp_parser_parse_tentatively (parser);
7879 /* Try the ptr-operator. */
7880 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7881 /* If it worked, look for more conversion-declarators. */
7882 if (cp_parser_parse_definitely (parser))
7884 cp_declarator *declarator;
7886 /* Parse another optional declarator. */
7887 declarator = cp_parser_conversion_declarator_opt (parser);
7889 /* Create the representation of the declarator. */
7891 declarator = make_ptrmem_declarator (cv_quals, class_type,
7893 else if (code == INDIRECT_REF)
7894 declarator = make_pointer_declarator (cv_quals, declarator);
7896 declarator = make_reference_declarator (cv_quals, declarator);
7904 /* Parse an (optional) ctor-initializer.
7907 : mem-initializer-list
7909 Returns TRUE iff the ctor-initializer was actually present. */
7912 cp_parser_ctor_initializer_opt (cp_parser* parser)
7914 /* If the next token is not a `:', then there is no
7915 ctor-initializer. */
7916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7918 /* Do default initialization of any bases and members. */
7919 if (DECL_CONSTRUCTOR_P (current_function_decl))
7920 finish_mem_initializers (NULL_TREE);
7925 /* Consume the `:' token. */
7926 cp_lexer_consume_token (parser->lexer);
7927 /* And the mem-initializer-list. */
7928 cp_parser_mem_initializer_list (parser);
7933 /* Parse a mem-initializer-list.
7935 mem-initializer-list:
7937 mem-initializer , mem-initializer-list */
7940 cp_parser_mem_initializer_list (cp_parser* parser)
7942 tree mem_initializer_list = NULL_TREE;
7944 /* Let the semantic analysis code know that we are starting the
7945 mem-initializer-list. */
7946 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7947 error ("only constructors take base initializers");
7949 /* Loop through the list. */
7952 tree mem_initializer;
7954 /* Parse the mem-initializer. */
7955 mem_initializer = cp_parser_mem_initializer (parser);
7956 /* Add it to the list, unless it was erroneous. */
7957 if (mem_initializer != error_mark_node)
7959 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7960 mem_initializer_list = mem_initializer;
7962 /* If the next token is not a `,', we're done. */
7963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7965 /* Consume the `,' token. */
7966 cp_lexer_consume_token (parser->lexer);
7969 /* Perform semantic analysis. */
7970 if (DECL_CONSTRUCTOR_P (current_function_decl))
7971 finish_mem_initializers (mem_initializer_list);
7974 /* Parse a mem-initializer.
7977 mem-initializer-id ( expression-list [opt] )
7982 ( expression-list [opt] )
7984 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7985 class) or FIELD_DECL (for a non-static data member) to initialize;
7986 the TREE_VALUE is the expression-list. An empty initialization
7987 list is represented by void_list_node. */
7990 cp_parser_mem_initializer (cp_parser* parser)
7992 tree mem_initializer_id;
7993 tree expression_list;
7996 /* Find out what is being initialized. */
7997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7999 pedwarn ("anachronistic old-style base class initializer");
8000 mem_initializer_id = NULL_TREE;
8003 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8004 member = expand_member_init (mem_initializer_id);
8005 if (member && !DECL_P (member))
8006 in_base_initializer = 1;
8009 = cp_parser_parenthesized_expression_list (parser, false,
8011 /*non_constant_p=*/NULL);
8012 if (expression_list == error_mark_node)
8013 return error_mark_node;
8014 if (!expression_list)
8015 expression_list = void_type_node;
8017 in_base_initializer = 0;
8019 return member ? build_tree_list (member, expression_list) : error_mark_node;
8022 /* Parse a mem-initializer-id.
8025 :: [opt] nested-name-specifier [opt] class-name
8028 Returns a TYPE indicating the class to be initializer for the first
8029 production. Returns an IDENTIFIER_NODE indicating the data member
8030 to be initialized for the second production. */
8033 cp_parser_mem_initializer_id (cp_parser* parser)
8035 bool global_scope_p;
8036 bool nested_name_specifier_p;
8037 bool template_p = false;
8040 /* `typename' is not allowed in this context ([temp.res]). */
8041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8043 error ("keyword %<typename%> not allowed in this context (a qualified "
8044 "member initializer is implicitly a type)");
8045 cp_lexer_consume_token (parser->lexer);
8047 /* Look for the optional `::' operator. */
8049 = (cp_parser_global_scope_opt (parser,
8050 /*current_scope_valid_p=*/false)
8052 /* Look for the optional nested-name-specifier. The simplest way to
8057 The keyword `typename' is not permitted in a base-specifier or
8058 mem-initializer; in these contexts a qualified name that
8059 depends on a template-parameter is implicitly assumed to be a
8062 is to assume that we have seen the `typename' keyword at this
8064 nested_name_specifier_p
8065 = (cp_parser_nested_name_specifier_opt (parser,
8066 /*typename_keyword_p=*/true,
8067 /*check_dependency_p=*/true,
8069 /*is_declaration=*/true)
8071 if (nested_name_specifier_p)
8072 template_p = cp_parser_optional_template_keyword (parser);
8073 /* If there is a `::' operator or a nested-name-specifier, then we
8074 are definitely looking for a class-name. */
8075 if (global_scope_p || nested_name_specifier_p)
8076 return cp_parser_class_name (parser,
8077 /*typename_keyword_p=*/true,
8078 /*template_keyword_p=*/template_p,
8080 /*check_dependency_p=*/true,
8081 /*class_head_p=*/false,
8082 /*is_declaration=*/true);
8083 /* Otherwise, we could also be looking for an ordinary identifier. */
8084 cp_parser_parse_tentatively (parser);
8085 /* Try a class-name. */
8086 id = cp_parser_class_name (parser,
8087 /*typename_keyword_p=*/true,
8088 /*template_keyword_p=*/false,
8090 /*check_dependency_p=*/true,
8091 /*class_head_p=*/false,
8092 /*is_declaration=*/true);
8093 /* If we found one, we're done. */
8094 if (cp_parser_parse_definitely (parser))
8096 /* Otherwise, look for an ordinary identifier. */
8097 return cp_parser_identifier (parser);
8100 /* Overloading [gram.over] */
8102 /* Parse an operator-function-id.
8104 operator-function-id:
8107 Returns an IDENTIFIER_NODE for the operator which is a
8108 human-readable spelling of the identifier, e.g., `operator +'. */
8111 cp_parser_operator_function_id (cp_parser* parser)
8113 /* Look for the `operator' keyword. */
8114 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8115 return error_mark_node;
8116 /* And then the name of the operator itself. */
8117 return cp_parser_operator (parser);
8120 /* Parse an operator.
8123 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8124 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8125 || ++ -- , ->* -> () []
8132 Returns an IDENTIFIER_NODE for the operator which is a
8133 human-readable spelling of the identifier, e.g., `operator +'. */
8136 cp_parser_operator (cp_parser* parser)
8138 tree id = NULL_TREE;
8141 /* Peek at the next token. */
8142 token = cp_lexer_peek_token (parser->lexer);
8143 /* Figure out which operator we have. */
8144 switch (token->type)
8150 /* The keyword should be either `new' or `delete'. */
8151 if (token->keyword == RID_NEW)
8153 else if (token->keyword == RID_DELETE)
8158 /* Consume the `new' or `delete' token. */
8159 cp_lexer_consume_token (parser->lexer);
8161 /* Peek at the next token. */
8162 token = cp_lexer_peek_token (parser->lexer);
8163 /* If it's a `[' token then this is the array variant of the
8165 if (token->type == CPP_OPEN_SQUARE)
8167 /* Consume the `[' token. */
8168 cp_lexer_consume_token (parser->lexer);
8169 /* Look for the `]' token. */
8170 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8171 id = ansi_opname (op == NEW_EXPR
8172 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8174 /* Otherwise, we have the non-array variant. */
8176 id = ansi_opname (op);
8182 id = ansi_opname (PLUS_EXPR);
8186 id = ansi_opname (MINUS_EXPR);
8190 id = ansi_opname (MULT_EXPR);
8194 id = ansi_opname (TRUNC_DIV_EXPR);
8198 id = ansi_opname (TRUNC_MOD_EXPR);
8202 id = ansi_opname (BIT_XOR_EXPR);
8206 id = ansi_opname (BIT_AND_EXPR);
8210 id = ansi_opname (BIT_IOR_EXPR);
8214 id = ansi_opname (BIT_NOT_EXPR);
8218 id = ansi_opname (TRUTH_NOT_EXPR);
8222 id = ansi_assopname (NOP_EXPR);
8226 id = ansi_opname (LT_EXPR);
8230 id = ansi_opname (GT_EXPR);
8234 id = ansi_assopname (PLUS_EXPR);
8238 id = ansi_assopname (MINUS_EXPR);
8242 id = ansi_assopname (MULT_EXPR);
8246 id = ansi_assopname (TRUNC_DIV_EXPR);
8250 id = ansi_assopname (TRUNC_MOD_EXPR);
8254 id = ansi_assopname (BIT_XOR_EXPR);
8258 id = ansi_assopname (BIT_AND_EXPR);
8262 id = ansi_assopname (BIT_IOR_EXPR);
8266 id = ansi_opname (LSHIFT_EXPR);
8270 id = ansi_opname (RSHIFT_EXPR);
8274 id = ansi_assopname (LSHIFT_EXPR);
8278 id = ansi_assopname (RSHIFT_EXPR);
8282 id = ansi_opname (EQ_EXPR);
8286 id = ansi_opname (NE_EXPR);
8290 id = ansi_opname (LE_EXPR);
8293 case CPP_GREATER_EQ:
8294 id = ansi_opname (GE_EXPR);
8298 id = ansi_opname (TRUTH_ANDIF_EXPR);
8302 id = ansi_opname (TRUTH_ORIF_EXPR);
8306 id = ansi_opname (POSTINCREMENT_EXPR);
8309 case CPP_MINUS_MINUS:
8310 id = ansi_opname (PREDECREMENT_EXPR);
8314 id = ansi_opname (COMPOUND_EXPR);
8317 case CPP_DEREF_STAR:
8318 id = ansi_opname (MEMBER_REF);
8322 id = ansi_opname (COMPONENT_REF);
8325 case CPP_OPEN_PAREN:
8326 /* Consume the `('. */
8327 cp_lexer_consume_token (parser->lexer);
8328 /* Look for the matching `)'. */
8329 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8330 return ansi_opname (CALL_EXPR);
8332 case CPP_OPEN_SQUARE:
8333 /* Consume the `['. */
8334 cp_lexer_consume_token (parser->lexer);
8335 /* Look for the matching `]'. */
8336 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8337 return ansi_opname (ARRAY_REF);
8341 id = ansi_opname (MIN_EXPR);
8342 cp_parser_warn_min_max ();
8346 id = ansi_opname (MAX_EXPR);
8347 cp_parser_warn_min_max ();
8351 id = ansi_assopname (MIN_EXPR);
8352 cp_parser_warn_min_max ();
8356 id = ansi_assopname (MAX_EXPR);
8357 cp_parser_warn_min_max ();
8361 /* Anything else is an error. */
8365 /* If we have selected an identifier, we need to consume the
8368 cp_lexer_consume_token (parser->lexer);
8369 /* Otherwise, no valid operator name was present. */
8372 cp_parser_error (parser, "expected operator");
8373 id = error_mark_node;
8379 /* Parse a template-declaration.
8381 template-declaration:
8382 export [opt] template < template-parameter-list > declaration
8384 If MEMBER_P is TRUE, this template-declaration occurs within a
8387 The grammar rule given by the standard isn't correct. What
8390 template-declaration:
8391 export [opt] template-parameter-list-seq
8392 decl-specifier-seq [opt] init-declarator [opt] ;
8393 export [opt] template-parameter-list-seq
8396 template-parameter-list-seq:
8397 template-parameter-list-seq [opt]
8398 template < template-parameter-list > */
8401 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8403 /* Check for `export'. */
8404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8406 /* Consume the `export' token. */
8407 cp_lexer_consume_token (parser->lexer);
8408 /* Warn that we do not support `export'. */
8409 warning (0, "keyword %<export%> not implemented, and will be ignored");
8412 cp_parser_template_declaration_after_export (parser, member_p);
8415 /* Parse a template-parameter-list.
8417 template-parameter-list:
8419 template-parameter-list , template-parameter
8421 Returns a TREE_LIST. Each node represents a template parameter.
8422 The nodes are connected via their TREE_CHAINs. */
8425 cp_parser_template_parameter_list (cp_parser* parser)
8427 tree parameter_list = NULL_TREE;
8429 begin_template_parm_list ();
8436 /* Parse the template-parameter. */
8437 parameter = cp_parser_template_parameter (parser, &is_non_type);
8438 /* Add it to the list. */
8439 if (parameter != error_mark_node)
8440 parameter_list = process_template_parm (parameter_list,
8443 /* Peek at the next token. */
8444 token = cp_lexer_peek_token (parser->lexer);
8445 /* If it's not a `,', we're done. */
8446 if (token->type != CPP_COMMA)
8448 /* Otherwise, consume the `,' token. */
8449 cp_lexer_consume_token (parser->lexer);
8452 return end_template_parm_list (parameter_list);
8455 /* Parse a template-parameter.
8459 parameter-declaration
8461 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8462 the parameter. The TREE_PURPOSE is the default value, if any.
8463 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8464 iff this parameter is a non-type parameter. */
8467 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8470 cp_parameter_declarator *parameter_declarator;
8473 /* Assume it is a type parameter or a template parameter. */
8474 *is_non_type = false;
8475 /* Peek at the next token. */
8476 token = cp_lexer_peek_token (parser->lexer);
8477 /* If it is `class' or `template', we have a type-parameter. */
8478 if (token->keyword == RID_TEMPLATE)
8479 return cp_parser_type_parameter (parser);
8480 /* If it is `class' or `typename' we do not know yet whether it is a
8481 type parameter or a non-type parameter. Consider:
8483 template <typename T, typename T::X X> ...
8487 template <class C, class D*> ...
8489 Here, the first parameter is a type parameter, and the second is
8490 a non-type parameter. We can tell by looking at the token after
8491 the identifier -- if it is a `,', `=', or `>' then we have a type
8493 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8495 /* Peek at the token after `class' or `typename'. */
8496 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8497 /* If it's an identifier, skip it. */
8498 if (token->type == CPP_NAME)
8499 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8500 /* Now, see if the token looks like the end of a template
8502 if (token->type == CPP_COMMA
8503 || token->type == CPP_EQ
8504 || token->type == CPP_GREATER)
8505 return cp_parser_type_parameter (parser);
8508 /* Otherwise, it is a non-type parameter.
8512 When parsing a default template-argument for a non-type
8513 template-parameter, the first non-nested `>' is taken as the end
8514 of the template parameter-list rather than a greater-than
8516 *is_non_type = true;
8517 parameter_declarator
8518 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8519 /*parenthesized_p=*/NULL);
8520 parm = grokdeclarator (parameter_declarator->declarator,
8521 ¶meter_declarator->decl_specifiers,
8522 PARM, /*initialized=*/0,
8524 if (parm == error_mark_node)
8525 return error_mark_node;
8526 return build_tree_list (parameter_declarator->default_argument, parm);
8529 /* Parse a type-parameter.
8532 class identifier [opt]
8533 class identifier [opt] = type-id
8534 typename identifier [opt]
8535 typename identifier [opt] = type-id
8536 template < template-parameter-list > class identifier [opt]
8537 template < template-parameter-list > class identifier [opt]
8540 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8541 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8542 the declaration of the parameter. */
8545 cp_parser_type_parameter (cp_parser* parser)
8550 /* Look for a keyword to tell us what kind of parameter this is. */
8551 token = cp_parser_require (parser, CPP_KEYWORD,
8552 "`class', `typename', or `template'");
8554 return error_mark_node;
8556 switch (token->keyword)
8562 tree default_argument;
8564 /* If the next token is an identifier, then it names the
8566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8567 identifier = cp_parser_identifier (parser);
8569 identifier = NULL_TREE;
8571 /* Create the parameter. */
8572 parameter = finish_template_type_parm (class_type_node, identifier);
8574 /* If the next token is an `=', we have a default argument. */
8575 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8577 /* Consume the `=' token. */
8578 cp_lexer_consume_token (parser->lexer);
8579 /* Parse the default-argument. */
8580 push_deferring_access_checks (dk_no_deferred);
8581 default_argument = cp_parser_type_id (parser);
8582 pop_deferring_access_checks ();
8585 default_argument = NULL_TREE;
8587 /* Create the combined representation of the parameter and the
8588 default argument. */
8589 parameter = build_tree_list (default_argument, parameter);
8595 tree parameter_list;
8597 tree default_argument;
8599 /* Look for the `<'. */
8600 cp_parser_require (parser, CPP_LESS, "`<'");
8601 /* Parse the template-parameter-list. */
8602 parameter_list = cp_parser_template_parameter_list (parser);
8603 /* Look for the `>'. */
8604 cp_parser_require (parser, CPP_GREATER, "`>'");
8605 /* Look for the `class' keyword. */
8606 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8607 /* If the next token is an `=', then there is a
8608 default-argument. If the next token is a `>', we are at
8609 the end of the parameter-list. If the next token is a `,',
8610 then we are at the end of this parameter. */
8611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8612 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8613 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8615 identifier = cp_parser_identifier (parser);
8616 /* Treat invalid names as if the parameter were nameless. */
8617 if (identifier == error_mark_node)
8618 identifier = NULL_TREE;
8621 identifier = NULL_TREE;
8623 /* Create the template parameter. */
8624 parameter = finish_template_template_parm (class_type_node,
8627 /* If the next token is an `=', then there is a
8628 default-argument. */
8629 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8633 /* Consume the `='. */
8634 cp_lexer_consume_token (parser->lexer);
8635 /* Parse the id-expression. */
8636 push_deferring_access_checks (dk_no_deferred);
8638 = cp_parser_id_expression (parser,
8639 /*template_keyword_p=*/false,
8640 /*check_dependency_p=*/true,
8641 /*template_p=*/&is_template,
8642 /*declarator_p=*/false,
8643 /*optional_p=*/false);
8644 if (TREE_CODE (default_argument) == TYPE_DECL)
8645 /* If the id-expression was a template-id that refers to
8646 a template-class, we already have the declaration here,
8647 so no further lookup is needed. */
8650 /* Look up the name. */
8652 = cp_parser_lookup_name (parser, default_argument,
8654 /*is_template=*/is_template,
8655 /*is_namespace=*/false,
8656 /*check_dependency=*/true,
8657 /*ambiguous_decls=*/NULL);
8658 /* See if the default argument is valid. */
8660 = check_template_template_default_arg (default_argument);
8661 pop_deferring_access_checks ();
8664 default_argument = NULL_TREE;
8666 /* Create the combined representation of the parameter and the
8667 default argument. */
8668 parameter = build_tree_list (default_argument, parameter);
8680 /* Parse a template-id.
8683 template-name < template-argument-list [opt] >
8685 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8686 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8687 returned. Otherwise, if the template-name names a function, or set
8688 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8689 names a class, returns a TYPE_DECL for the specialization.
8691 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8692 uninstantiated templates. */
8695 cp_parser_template_id (cp_parser *parser,
8696 bool template_keyword_p,
8697 bool check_dependency_p,
8698 bool is_declaration)
8703 cp_token_position start_of_id = 0;
8704 tree access_check = NULL_TREE;
8705 cp_token *next_token, *next_token_2;
8708 /* If the next token corresponds to a template-id, there is no need
8710 next_token = cp_lexer_peek_token (parser->lexer);
8711 if (next_token->type == CPP_TEMPLATE_ID)
8716 /* Get the stored value. */
8717 value = cp_lexer_consume_token (parser->lexer)->value;
8718 /* Perform any access checks that were deferred. */
8719 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8720 perform_or_defer_access_check (TREE_PURPOSE (check),
8721 TREE_VALUE (check));
8722 /* Return the stored value. */
8723 return TREE_VALUE (value);
8726 /* Avoid performing name lookup if there is no possibility of
8727 finding a template-id. */
8728 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8729 || (next_token->type == CPP_NAME
8730 && !cp_parser_nth_token_starts_template_argument_list_p
8733 cp_parser_error (parser, "expected template-id");
8734 return error_mark_node;
8737 /* Remember where the template-id starts. */
8738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8739 start_of_id = cp_lexer_token_position (parser->lexer, false);
8741 push_deferring_access_checks (dk_deferred);
8743 /* Parse the template-name. */
8744 is_identifier = false;
8745 template = cp_parser_template_name (parser, template_keyword_p,
8749 if (template == error_mark_node || is_identifier)
8751 pop_deferring_access_checks ();
8755 /* If we find the sequence `[:' after a template-name, it's probably
8756 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8757 parse correctly the argument list. */
8758 next_token = cp_lexer_peek_token (parser->lexer);
8759 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8760 if (next_token->type == CPP_OPEN_SQUARE
8761 && next_token->flags & DIGRAPH
8762 && next_token_2->type == CPP_COLON
8763 && !(next_token_2->flags & PREV_WHITE))
8765 cp_parser_parse_tentatively (parser);
8766 /* Change `:' into `::'. */
8767 next_token_2->type = CPP_SCOPE;
8768 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8770 cp_lexer_consume_token (parser->lexer);
8771 /* Parse the arguments. */
8772 arguments = cp_parser_enclosed_template_argument_list (parser);
8773 if (!cp_parser_parse_definitely (parser))
8775 /* If we couldn't parse an argument list, then we revert our changes
8776 and return simply an error. Maybe this is not a template-id
8778 next_token_2->type = CPP_COLON;
8779 cp_parser_error (parser, "expected %<<%>");
8780 pop_deferring_access_checks ();
8781 return error_mark_node;
8783 /* Otherwise, emit an error about the invalid digraph, but continue
8784 parsing because we got our argument list. */
8785 pedwarn ("%<<::%> cannot begin a template-argument list");
8786 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8787 "between %<<%> and %<::%>");
8788 if (!flag_permissive)
8793 inform ("(if you use -fpermissive G++ will accept your code)");
8800 /* Look for the `<' that starts the template-argument-list. */
8801 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8803 pop_deferring_access_checks ();
8804 return error_mark_node;
8806 /* Parse the arguments. */
8807 arguments = cp_parser_enclosed_template_argument_list (parser);
8810 /* Build a representation of the specialization. */
8811 if (TREE_CODE (template) == IDENTIFIER_NODE)
8812 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8813 else if (DECL_CLASS_TEMPLATE_P (template)
8814 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8816 = finish_template_type (template, arguments,
8817 cp_lexer_next_token_is (parser->lexer,
8821 /* If it's not a class-template or a template-template, it should be
8822 a function-template. */
8823 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8824 || TREE_CODE (template) == OVERLOAD
8825 || BASELINK_P (template)));
8827 template_id = lookup_template_function (template, arguments);
8830 /* Retrieve any deferred checks. Do not pop this access checks yet
8831 so the memory will not be reclaimed during token replacing below. */
8832 access_check = get_deferred_access_checks ();
8834 /* If parsing tentatively, replace the sequence of tokens that makes
8835 up the template-id with a CPP_TEMPLATE_ID token. That way,
8836 should we re-parse the token stream, we will not have to repeat
8837 the effort required to do the parse, nor will we issue duplicate
8838 error messages about problems during instantiation of the
8842 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8844 /* Reset the contents of the START_OF_ID token. */
8845 token->type = CPP_TEMPLATE_ID;
8846 token->value = build_tree_list (access_check, template_id);
8847 token->keyword = RID_MAX;
8849 /* Purge all subsequent tokens. */
8850 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8852 /* ??? Can we actually assume that, if template_id ==
8853 error_mark_node, we will have issued a diagnostic to the
8854 user, as opposed to simply marking the tentative parse as
8856 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8857 error ("parse error in template argument list");
8860 pop_deferring_access_checks ();
8864 /* Parse a template-name.
8869 The standard should actually say:
8873 operator-function-id
8875 A defect report has been filed about this issue.
8877 A conversion-function-id cannot be a template name because they cannot
8878 be part of a template-id. In fact, looking at this code:
8882 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8883 It is impossible to call a templated conversion-function-id with an
8884 explicit argument list, since the only allowed template parameter is
8885 the type to which it is converting.
8887 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8888 `template' keyword, in a construction like:
8892 In that case `f' is taken to be a template-name, even though there
8893 is no way of knowing for sure.
8895 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8896 name refers to a set of overloaded functions, at least one of which
8897 is a template, or an IDENTIFIER_NODE with the name of the template,
8898 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8899 names are looked up inside uninstantiated templates. */
8902 cp_parser_template_name (cp_parser* parser,
8903 bool template_keyword_p,
8904 bool check_dependency_p,
8905 bool is_declaration,
8906 bool *is_identifier)
8912 /* If the next token is `operator', then we have either an
8913 operator-function-id or a conversion-function-id. */
8914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8916 /* We don't know whether we're looking at an
8917 operator-function-id or a conversion-function-id. */
8918 cp_parser_parse_tentatively (parser);
8919 /* Try an operator-function-id. */
8920 identifier = cp_parser_operator_function_id (parser);
8921 /* If that didn't work, try a conversion-function-id. */
8922 if (!cp_parser_parse_definitely (parser))
8924 cp_parser_error (parser, "expected template-name");
8925 return error_mark_node;
8928 /* Look for the identifier. */
8930 identifier = cp_parser_identifier (parser);
8932 /* If we didn't find an identifier, we don't have a template-id. */
8933 if (identifier == error_mark_node)
8934 return error_mark_node;
8936 /* If the name immediately followed the `template' keyword, then it
8937 is a template-name. However, if the next token is not `<', then
8938 we do not treat it as a template-name, since it is not being used
8939 as part of a template-id. This enables us to handle constructs
8942 template <typename T> struct S { S(); };
8943 template <typename T> S<T>::S();
8945 correctly. We would treat `S' as a template -- if it were `S<T>'
8946 -- but we do not if there is no `<'. */
8948 if (processing_template_decl
8949 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8951 /* In a declaration, in a dependent context, we pretend that the
8952 "template" keyword was present in order to improve error
8953 recovery. For example, given:
8955 template <typename T> void f(T::X<int>);
8957 we want to treat "X<int>" as a template-id. */
8959 && !template_keyword_p
8960 && parser->scope && TYPE_P (parser->scope)
8961 && check_dependency_p
8962 && dependent_type_p (parser->scope)
8963 /* Do not do this for dtors (or ctors), since they never
8964 need the template keyword before their name. */
8965 && !constructor_name_p (identifier, parser->scope))
8967 cp_token_position start = 0;
8969 /* Explain what went wrong. */
8970 error ("non-template %qD used as template", identifier);
8971 inform ("use %<%T::template %D%> to indicate that it is a template",
8972 parser->scope, identifier);
8973 /* If parsing tentatively, find the location of the "<" token. */
8974 if (cp_parser_simulate_error (parser))
8975 start = cp_lexer_token_position (parser->lexer, true);
8976 /* Parse the template arguments so that we can issue error
8977 messages about them. */
8978 cp_lexer_consume_token (parser->lexer);
8979 cp_parser_enclosed_template_argument_list (parser);
8980 /* Skip tokens until we find a good place from which to
8981 continue parsing. */
8982 cp_parser_skip_to_closing_parenthesis (parser,
8983 /*recovering=*/true,
8985 /*consume_paren=*/false);
8986 /* If parsing tentatively, permanently remove the
8987 template argument list. That will prevent duplicate
8988 error messages from being issued about the missing
8989 "template" keyword. */
8991 cp_lexer_purge_tokens_after (parser->lexer, start);
8993 *is_identifier = true;
8997 /* If the "template" keyword is present, then there is generally
8998 no point in doing name-lookup, so we just return IDENTIFIER.
8999 But, if the qualifying scope is non-dependent then we can
9000 (and must) do name-lookup normally. */
9001 if (template_keyword_p
9003 || (TYPE_P (parser->scope)
9004 && dependent_type_p (parser->scope))))
9008 /* Look up the name. */
9009 decl = cp_parser_lookup_name (parser, identifier,
9011 /*is_template=*/false,
9012 /*is_namespace=*/false,
9014 /*ambiguous_decls=*/NULL);
9015 decl = maybe_get_template_decl_from_type_decl (decl);
9017 /* If DECL is a template, then the name was a template-name. */
9018 if (TREE_CODE (decl) == TEMPLATE_DECL)
9022 tree fn = NULL_TREE;
9024 /* The standard does not explicitly indicate whether a name that
9025 names a set of overloaded declarations, some of which are
9026 templates, is a template-name. However, such a name should
9027 be a template-name; otherwise, there is no way to form a
9028 template-id for the overloaded templates. */
9029 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9030 if (TREE_CODE (fns) == OVERLOAD)
9031 for (fn = fns; fn; fn = OVL_NEXT (fn))
9032 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9037 /* The name does not name a template. */
9038 cp_parser_error (parser, "expected template-name");
9039 return error_mark_node;
9043 /* If DECL is dependent, and refers to a function, then just return
9044 its name; we will look it up again during template instantiation. */
9045 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9047 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9048 if (TYPE_P (scope) && dependent_type_p (scope))
9055 /* Parse a template-argument-list.
9057 template-argument-list:
9059 template-argument-list , template-argument
9061 Returns a TREE_VEC containing the arguments. */
9064 cp_parser_template_argument_list (cp_parser* parser)
9066 tree fixed_args[10];
9067 unsigned n_args = 0;
9068 unsigned alloced = 10;
9069 tree *arg_ary = fixed_args;
9071 bool saved_in_template_argument_list_p;
9073 bool saved_non_ice_p;
9075 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9076 parser->in_template_argument_list_p = true;
9077 /* Even if the template-id appears in an integral
9078 constant-expression, the contents of the argument list do
9080 saved_ice_p = parser->integral_constant_expression_p;
9081 parser->integral_constant_expression_p = false;
9082 saved_non_ice_p = parser->non_integral_constant_expression_p;
9083 parser->non_integral_constant_expression_p = false;
9084 /* Parse the arguments. */
9090 /* Consume the comma. */
9091 cp_lexer_consume_token (parser->lexer);
9093 /* Parse the template-argument. */
9094 argument = cp_parser_template_argument (parser);
9095 if (n_args == alloced)
9099 if (arg_ary == fixed_args)
9101 arg_ary = XNEWVEC (tree, alloced);
9102 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9105 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9107 arg_ary[n_args++] = argument;
9109 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9111 vec = make_tree_vec (n_args);
9114 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9116 if (arg_ary != fixed_args)
9118 parser->non_integral_constant_expression_p = saved_non_ice_p;
9119 parser->integral_constant_expression_p = saved_ice_p;
9120 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9124 /* Parse a template-argument.
9127 assignment-expression
9131 The representation is that of an assignment-expression, type-id, or
9132 id-expression -- except that the qualified id-expression is
9133 evaluated, so that the value returned is either a DECL or an
9136 Although the standard says "assignment-expression", it forbids
9137 throw-expressions or assignments in the template argument.
9138 Therefore, we use "conditional-expression" instead. */
9141 cp_parser_template_argument (cp_parser* parser)
9146 bool maybe_type_id = false;
9150 /* There's really no way to know what we're looking at, so we just
9151 try each alternative in order.
9155 In a template-argument, an ambiguity between a type-id and an
9156 expression is resolved to a type-id, regardless of the form of
9157 the corresponding template-parameter.
9159 Therefore, we try a type-id first. */
9160 cp_parser_parse_tentatively (parser);
9161 argument = cp_parser_type_id (parser);
9162 /* If there was no error parsing the type-id but the next token is a '>>',
9163 we probably found a typo for '> >'. But there are type-id which are
9164 also valid expressions. For instance:
9166 struct X { int operator >> (int); };
9167 template <int V> struct Foo {};
9170 Here 'X()' is a valid type-id of a function type, but the user just
9171 wanted to write the expression "X() >> 5". Thus, we remember that we
9172 found a valid type-id, but we still try to parse the argument as an
9173 expression to see what happens. */
9174 if (!cp_parser_error_occurred (parser)
9175 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9177 maybe_type_id = true;
9178 cp_parser_abort_tentative_parse (parser);
9182 /* If the next token isn't a `,' or a `>', then this argument wasn't
9183 really finished. This means that the argument is not a valid
9185 if (!cp_parser_next_token_ends_template_argument_p (parser))
9186 cp_parser_error (parser, "expected template-argument");
9187 /* If that worked, we're done. */
9188 if (cp_parser_parse_definitely (parser))
9191 /* We're still not sure what the argument will be. */
9192 cp_parser_parse_tentatively (parser);
9193 /* Try a template. */
9194 argument = cp_parser_id_expression (parser,
9195 /*template_keyword_p=*/false,
9196 /*check_dependency_p=*/true,
9198 /*declarator_p=*/false,
9199 /*optional_p=*/false);
9200 /* If the next token isn't a `,' or a `>', then this argument wasn't
9202 if (!cp_parser_next_token_ends_template_argument_p (parser))
9203 cp_parser_error (parser, "expected template-argument");
9204 if (!cp_parser_error_occurred (parser))
9206 /* Figure out what is being referred to. If the id-expression
9207 was for a class template specialization, then we will have a
9208 TYPE_DECL at this point. There is no need to do name lookup
9209 at this point in that case. */
9210 if (TREE_CODE (argument) != TYPE_DECL)
9211 argument = cp_parser_lookup_name (parser, argument,
9213 /*is_template=*/template_p,
9214 /*is_namespace=*/false,
9215 /*check_dependency=*/true,
9216 /*ambiguous_decls=*/NULL);
9217 if (TREE_CODE (argument) != TEMPLATE_DECL
9218 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9219 cp_parser_error (parser, "expected template-name");
9221 if (cp_parser_parse_definitely (parser))
9223 /* It must be a non-type argument. There permitted cases are given
9224 in [temp.arg.nontype]:
9226 -- an integral constant-expression of integral or enumeration
9229 -- the name of a non-type template-parameter; or
9231 -- the name of an object or function with external linkage...
9233 -- the address of an object or function with external linkage...
9235 -- a pointer to member... */
9236 /* Look for a non-type template parameter. */
9237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9239 cp_parser_parse_tentatively (parser);
9240 argument = cp_parser_primary_expression (parser,
9243 /*template_arg_p=*/true,
9245 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9246 || !cp_parser_next_token_ends_template_argument_p (parser))
9247 cp_parser_simulate_error (parser);
9248 if (cp_parser_parse_definitely (parser))
9252 /* If the next token is "&", the argument must be the address of an
9253 object or function with external linkage. */
9254 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9256 cp_lexer_consume_token (parser->lexer);
9257 /* See if we might have an id-expression. */
9258 token = cp_lexer_peek_token (parser->lexer);
9259 if (token->type == CPP_NAME
9260 || token->keyword == RID_OPERATOR
9261 || token->type == CPP_SCOPE
9262 || token->type == CPP_TEMPLATE_ID
9263 || token->type == CPP_NESTED_NAME_SPECIFIER)
9265 cp_parser_parse_tentatively (parser);
9266 argument = cp_parser_primary_expression (parser,
9269 /*template_arg_p=*/true,
9271 if (cp_parser_error_occurred (parser)
9272 || !cp_parser_next_token_ends_template_argument_p (parser))
9273 cp_parser_abort_tentative_parse (parser);
9276 if (TREE_CODE (argument) == INDIRECT_REF)
9278 gcc_assert (REFERENCE_REF_P (argument));
9279 argument = TREE_OPERAND (argument, 0);
9282 if (TREE_CODE (argument) == BASELINK)
9283 /* We don't need the information about what class was used
9284 to name the overloaded functions. */
9285 argument = BASELINK_FUNCTIONS (argument);
9287 if (TREE_CODE (argument) == VAR_DECL)
9289 /* A variable without external linkage might still be a
9290 valid constant-expression, so no error is issued here
9291 if the external-linkage check fails. */
9292 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9293 cp_parser_simulate_error (parser);
9295 else if (is_overloaded_fn (argument))
9296 /* All overloaded functions are allowed; if the external
9297 linkage test does not pass, an error will be issued
9301 && (TREE_CODE (argument) == OFFSET_REF
9302 || TREE_CODE (argument) == SCOPE_REF))
9303 /* A pointer-to-member. */
9305 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9308 cp_parser_simulate_error (parser);
9310 if (cp_parser_parse_definitely (parser))
9313 argument = build_x_unary_op (ADDR_EXPR, argument);
9318 /* If the argument started with "&", there are no other valid
9319 alternatives at this point. */
9322 cp_parser_error (parser, "invalid non-type template argument");
9323 return error_mark_node;
9326 /* If the argument wasn't successfully parsed as a type-id followed
9327 by '>>', the argument can only be a constant expression now.
9328 Otherwise, we try parsing the constant-expression tentatively,
9329 because the argument could really be a type-id. */
9331 cp_parser_parse_tentatively (parser);
9332 argument = cp_parser_constant_expression (parser,
9333 /*allow_non_constant_p=*/false,
9334 /*non_constant_p=*/NULL);
9335 argument = fold_non_dependent_expr (argument);
9338 if (!cp_parser_next_token_ends_template_argument_p (parser))
9339 cp_parser_error (parser, "expected template-argument");
9340 if (cp_parser_parse_definitely (parser))
9342 /* We did our best to parse the argument as a non type-id, but that
9343 was the only alternative that matched (albeit with a '>' after
9344 it). We can assume it's just a typo from the user, and a
9345 diagnostic will then be issued. */
9346 return cp_parser_type_id (parser);
9349 /* Parse an explicit-instantiation.
9351 explicit-instantiation:
9352 template declaration
9354 Although the standard says `declaration', what it really means is:
9356 explicit-instantiation:
9357 template decl-specifier-seq [opt] declarator [opt] ;
9359 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9360 supposed to be allowed. A defect report has been filed about this
9365 explicit-instantiation:
9366 storage-class-specifier template
9367 decl-specifier-seq [opt] declarator [opt] ;
9368 function-specifier template
9369 decl-specifier-seq [opt] declarator [opt] ; */
9372 cp_parser_explicit_instantiation (cp_parser* parser)
9374 int declares_class_or_enum;
9375 cp_decl_specifier_seq decl_specifiers;
9376 tree extension_specifier = NULL_TREE;
9378 /* Look for an (optional) storage-class-specifier or
9379 function-specifier. */
9380 if (cp_parser_allow_gnu_extensions_p (parser))
9383 = cp_parser_storage_class_specifier_opt (parser);
9384 if (!extension_specifier)
9386 = cp_parser_function_specifier_opt (parser,
9387 /*decl_specs=*/NULL);
9390 /* Look for the `template' keyword. */
9391 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9392 /* Let the front end know that we are processing an explicit
9394 begin_explicit_instantiation ();
9395 /* [temp.explicit] says that we are supposed to ignore access
9396 control while processing explicit instantiation directives. */
9397 push_deferring_access_checks (dk_no_check);
9398 /* Parse a decl-specifier-seq. */
9399 cp_parser_decl_specifier_seq (parser,
9400 CP_PARSER_FLAGS_OPTIONAL,
9402 &declares_class_or_enum);
9403 /* If there was exactly one decl-specifier, and it declared a class,
9404 and there's no declarator, then we have an explicit type
9406 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9410 type = check_tag_decl (&decl_specifiers);
9411 /* Turn access control back on for names used during
9412 template instantiation. */
9413 pop_deferring_access_checks ();
9415 do_type_instantiation (type, extension_specifier,
9416 /*complain=*/tf_error);
9420 cp_declarator *declarator;
9423 /* Parse the declarator. */
9425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9426 /*ctor_dtor_or_conv_p=*/NULL,
9427 /*parenthesized_p=*/NULL,
9428 /*member_p=*/false);
9429 if (declares_class_or_enum & 2)
9430 cp_parser_check_for_definition_in_return_type (declarator,
9431 decl_specifiers.type);
9432 if (declarator != cp_error_declarator)
9434 decl = grokdeclarator (declarator, &decl_specifiers,
9436 /* Turn access control back on for names used during
9437 template instantiation. */
9438 pop_deferring_access_checks ();
9439 /* Do the explicit instantiation. */
9440 do_decl_instantiation (decl, extension_specifier);
9444 pop_deferring_access_checks ();
9445 /* Skip the body of the explicit instantiation. */
9446 cp_parser_skip_to_end_of_statement (parser);
9449 /* We're done with the instantiation. */
9450 end_explicit_instantiation ();
9452 cp_parser_consume_semicolon_at_end_of_statement (parser);
9455 /* Parse an explicit-specialization.
9457 explicit-specialization:
9458 template < > declaration
9460 Although the standard says `declaration', what it really means is:
9462 explicit-specialization:
9463 template <> decl-specifier [opt] init-declarator [opt] ;
9464 template <> function-definition
9465 template <> explicit-specialization
9466 template <> template-declaration */
9469 cp_parser_explicit_specialization (cp_parser* parser)
9472 /* Look for the `template' keyword. */
9473 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9474 /* Look for the `<'. */
9475 cp_parser_require (parser, CPP_LESS, "`<'");
9476 /* Look for the `>'. */
9477 cp_parser_require (parser, CPP_GREATER, "`>'");
9478 /* We have processed another parameter list. */
9479 ++parser->num_template_parameter_lists;
9482 A template ... explicit specialization ... shall not have C
9484 if (current_lang_name == lang_name_c)
9486 error ("template specialization with C linkage");
9487 /* Give it C++ linkage to avoid confusing other parts of the
9489 push_lang_context (lang_name_cplusplus);
9490 need_lang_pop = true;
9493 need_lang_pop = false;
9494 /* Let the front end know that we are beginning a specialization. */
9495 begin_specialization ();
9496 /* If the next keyword is `template', we need to figure out whether
9497 or not we're looking a template-declaration. */
9498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9500 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9502 cp_parser_template_declaration_after_export (parser,
9503 /*member_p=*/false);
9505 cp_parser_explicit_specialization (parser);
9508 /* Parse the dependent declaration. */
9509 cp_parser_single_declaration (parser,
9510 /*checks=*/NULL_TREE,
9513 /* We're done with the specialization. */
9514 end_specialization ();
9515 /* For the erroneous case of a template with C linkage, we pushed an
9516 implicit C++ linkage scope; exit that scope now. */
9518 pop_lang_context ();
9519 /* We're done with this parameter list. */
9520 --parser->num_template_parameter_lists;
9523 /* Parse a type-specifier.
9526 simple-type-specifier
9529 elaborated-type-specifier
9537 Returns a representation of the type-specifier. For a
9538 class-specifier, enum-specifier, or elaborated-type-specifier, a
9539 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9541 The parser flags FLAGS is used to control type-specifier parsing.
9543 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9544 in a decl-specifier-seq.
9546 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9547 class-specifier, enum-specifier, or elaborated-type-specifier, then
9548 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9549 if a type is declared; 2 if it is defined. Otherwise, it is set to
9552 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9553 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9557 cp_parser_type_specifier (cp_parser* parser,
9558 cp_parser_flags flags,
9559 cp_decl_specifier_seq *decl_specs,
9560 bool is_declaration,
9561 int* declares_class_or_enum,
9562 bool* is_cv_qualifier)
9564 tree type_spec = NULL_TREE;
9567 cp_decl_spec ds = ds_last;
9569 /* Assume this type-specifier does not declare a new type. */
9570 if (declares_class_or_enum)
9571 *declares_class_or_enum = 0;
9572 /* And that it does not specify a cv-qualifier. */
9573 if (is_cv_qualifier)
9574 *is_cv_qualifier = false;
9575 /* Peek at the next token. */
9576 token = cp_lexer_peek_token (parser->lexer);
9578 /* If we're looking at a keyword, we can use that to guide the
9579 production we choose. */
9580 keyword = token->keyword;
9584 /* 'enum' [identifier] '{' introduces an enum-specifier;
9585 'enum' <anything else> introduces an elaborated-type-specifier. */
9586 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9587 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9591 if (parser->num_template_parameter_lists)
9593 error ("template declaration of %qs", "enum");
9594 cp_parser_skip_to_end_of_block_or_statement (parser);
9595 type_spec = error_mark_node;
9598 type_spec = cp_parser_enum_specifier (parser);
9600 if (declares_class_or_enum)
9601 *declares_class_or_enum = 2;
9603 cp_parser_set_decl_spec_type (decl_specs,
9605 /*user_defined_p=*/true);
9609 goto elaborated_type_specifier;
9611 /* Any of these indicate either a class-specifier, or an
9612 elaborated-type-specifier. */
9616 /* Parse tentatively so that we can back up if we don't find a
9618 cp_parser_parse_tentatively (parser);
9619 /* Look for the class-specifier. */
9620 type_spec = cp_parser_class_specifier (parser);
9621 /* If that worked, we're done. */
9622 if (cp_parser_parse_definitely (parser))
9624 if (declares_class_or_enum)
9625 *declares_class_or_enum = 2;
9627 cp_parser_set_decl_spec_type (decl_specs,
9629 /*user_defined_p=*/true);
9634 elaborated_type_specifier:
9635 /* We're declaring (not defining) a class or enum. */
9636 if (declares_class_or_enum)
9637 *declares_class_or_enum = 1;
9641 /* Look for an elaborated-type-specifier. */
9643 = (cp_parser_elaborated_type_specifier
9645 decl_specs && decl_specs->specs[(int) ds_friend],
9648 cp_parser_set_decl_spec_type (decl_specs,
9650 /*user_defined_p=*/true);
9655 if (is_cv_qualifier)
9656 *is_cv_qualifier = true;
9661 if (is_cv_qualifier)
9662 *is_cv_qualifier = true;
9667 if (is_cv_qualifier)
9668 *is_cv_qualifier = true;
9672 /* The `__complex__' keyword is a GNU extension. */
9680 /* Handle simple keywords. */
9685 ++decl_specs->specs[(int)ds];
9686 decl_specs->any_specifiers_p = true;
9688 return cp_lexer_consume_token (parser->lexer)->value;
9691 /* If we do not already have a type-specifier, assume we are looking
9692 at a simple-type-specifier. */
9693 type_spec = cp_parser_simple_type_specifier (parser,
9697 /* If we didn't find a type-specifier, and a type-specifier was not
9698 optional in this context, issue an error message. */
9699 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9701 cp_parser_error (parser, "expected type specifier");
9702 return error_mark_node;
9708 /* Parse a simple-type-specifier.
9710 simple-type-specifier:
9711 :: [opt] nested-name-specifier [opt] type-name
9712 :: [opt] nested-name-specifier template template-id
9727 simple-type-specifier:
9728 __typeof__ unary-expression
9729 __typeof__ ( type-id )
9731 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9732 appropriately updated. */
9735 cp_parser_simple_type_specifier (cp_parser* parser,
9736 cp_decl_specifier_seq *decl_specs,
9737 cp_parser_flags flags)
9739 tree type = NULL_TREE;
9742 /* Peek at the next token. */
9743 token = cp_lexer_peek_token (parser->lexer);
9745 /* If we're looking at a keyword, things are easy. */
9746 switch (token->keyword)
9750 decl_specs->explicit_char_p = true;
9751 type = char_type_node;
9754 type = wchar_type_node;
9757 type = boolean_type_node;
9761 ++decl_specs->specs[(int) ds_short];
9762 type = short_integer_type_node;
9766 decl_specs->explicit_int_p = true;
9767 type = integer_type_node;
9771 ++decl_specs->specs[(int) ds_long];
9772 type = long_integer_type_node;
9776 ++decl_specs->specs[(int) ds_signed];
9777 type = integer_type_node;
9781 ++decl_specs->specs[(int) ds_unsigned];
9782 type = unsigned_type_node;
9785 type = float_type_node;
9788 type = double_type_node;
9791 type = void_type_node;
9795 /* Consume the `typeof' token. */
9796 cp_lexer_consume_token (parser->lexer);
9797 /* Parse the operand to `typeof'. */
9798 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9799 /* If it is not already a TYPE, take its type. */
9801 type = finish_typeof (type);
9804 cp_parser_set_decl_spec_type (decl_specs, type,
9805 /*user_defined_p=*/true);
9813 /* If the type-specifier was for a built-in type, we're done. */
9818 /* Record the type. */
9820 && (token->keyword != RID_SIGNED
9821 && token->keyword != RID_UNSIGNED
9822 && token->keyword != RID_SHORT
9823 && token->keyword != RID_LONG))
9824 cp_parser_set_decl_spec_type (decl_specs,
9826 /*user_defined=*/false);
9828 decl_specs->any_specifiers_p = true;
9830 /* Consume the token. */
9831 id = cp_lexer_consume_token (parser->lexer)->value;
9833 /* There is no valid C++ program where a non-template type is
9834 followed by a "<". That usually indicates that the user thought
9835 that the type was a template. */
9836 cp_parser_check_for_invalid_template_id (parser, type);
9838 return TYPE_NAME (type);
9841 /* The type-specifier must be a user-defined type. */
9842 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9847 /* Don't gobble tokens or issue error messages if this is an
9848 optional type-specifier. */
9849 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9850 cp_parser_parse_tentatively (parser);
9852 /* Look for the optional `::' operator. */
9854 = (cp_parser_global_scope_opt (parser,
9855 /*current_scope_valid_p=*/false)
9857 /* Look for the nested-name specifier. */
9859 = (cp_parser_nested_name_specifier_opt (parser,
9860 /*typename_keyword_p=*/false,
9861 /*check_dependency_p=*/true,
9863 /*is_declaration=*/false)
9865 /* If we have seen a nested-name-specifier, and the next token
9866 is `template', then we are using the template-id production. */
9868 && cp_parser_optional_template_keyword (parser))
9870 /* Look for the template-id. */
9871 type = cp_parser_template_id (parser,
9872 /*template_keyword_p=*/true,
9873 /*check_dependency_p=*/true,
9874 /*is_declaration=*/false);
9875 /* If the template-id did not name a type, we are out of
9877 if (TREE_CODE (type) != TYPE_DECL)
9879 cp_parser_error (parser, "expected template-id for type");
9883 /* Otherwise, look for a type-name. */
9885 type = cp_parser_type_name (parser);
9886 /* Keep track of all name-lookups performed in class scopes. */
9890 && TREE_CODE (type) == TYPE_DECL
9891 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9892 maybe_note_name_used_in_class (DECL_NAME (type), type);
9893 /* If it didn't work out, we don't have a TYPE. */
9894 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9895 && !cp_parser_parse_definitely (parser))
9897 if (type && decl_specs)
9898 cp_parser_set_decl_spec_type (decl_specs, type,
9899 /*user_defined=*/true);
9902 /* If we didn't get a type-name, issue an error message. */
9903 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9905 cp_parser_error (parser, "expected type-name");
9906 return error_mark_node;
9909 /* There is no valid C++ program where a non-template type is
9910 followed by a "<". That usually indicates that the user thought
9911 that the type was a template. */
9912 if (type && type != error_mark_node)
9914 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9915 If it is, then the '<'...'>' enclose protocol names rather than
9916 template arguments, and so everything is fine. */
9917 if (c_dialect_objc ()
9918 && (objc_is_id (type) || objc_is_class_name (type)))
9920 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9921 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9923 /* Clobber the "unqualified" type previously entered into
9924 DECL_SPECS with the new, improved protocol-qualified version. */
9926 decl_specs->type = qual_type;
9931 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9937 /* Parse a type-name.
9950 Returns a TYPE_DECL for the type. */
9953 cp_parser_type_name (cp_parser* parser)
9958 /* We can't know yet whether it is a class-name or not. */
9959 cp_parser_parse_tentatively (parser);
9960 /* Try a class-name. */
9961 type_decl = cp_parser_class_name (parser,
9962 /*typename_keyword_p=*/false,
9963 /*template_keyword_p=*/false,
9965 /*check_dependency_p=*/true,
9966 /*class_head_p=*/false,
9967 /*is_declaration=*/false);
9968 /* If it's not a class-name, keep looking. */
9969 if (!cp_parser_parse_definitely (parser))
9971 /* It must be a typedef-name or an enum-name. */
9972 identifier = cp_parser_identifier (parser);
9973 if (identifier == error_mark_node)
9974 return error_mark_node;
9976 /* Look up the type-name. */
9977 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9979 if (TREE_CODE (type_decl) != TYPE_DECL
9980 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9982 /* See if this is an Objective-C type. */
9983 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9984 tree type = objc_get_protocol_qualified_type (identifier, protos);
9986 type_decl = TYPE_NAME (type);
9989 /* Issue an error if we did not find a type-name. */
9990 if (TREE_CODE (type_decl) != TYPE_DECL)
9992 if (!cp_parser_simulate_error (parser))
9993 cp_parser_name_lookup_error (parser, identifier, type_decl,
9995 type_decl = error_mark_node;
9997 /* Remember that the name was used in the definition of the
9998 current class so that we can check later to see if the
9999 meaning would have been different after the class was
10000 entirely defined. */
10001 else if (type_decl != error_mark_node
10003 maybe_note_name_used_in_class (identifier, type_decl);
10010 /* Parse an elaborated-type-specifier. Note that the grammar given
10011 here incorporates the resolution to DR68.
10013 elaborated-type-specifier:
10014 class-key :: [opt] nested-name-specifier [opt] identifier
10015 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10016 enum :: [opt] nested-name-specifier [opt] identifier
10017 typename :: [opt] nested-name-specifier identifier
10018 typename :: [opt] nested-name-specifier template [opt]
10023 elaborated-type-specifier:
10024 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10025 class-key attributes :: [opt] nested-name-specifier [opt]
10026 template [opt] template-id
10027 enum attributes :: [opt] nested-name-specifier [opt] identifier
10029 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10030 declared `friend'. If IS_DECLARATION is TRUE, then this
10031 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10032 something is being declared.
10034 Returns the TYPE specified. */
10037 cp_parser_elaborated_type_specifier (cp_parser* parser,
10039 bool is_declaration)
10041 enum tag_types tag_type;
10043 tree type = NULL_TREE;
10044 tree attributes = NULL_TREE;
10046 /* See if we're looking at the `enum' keyword. */
10047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10049 /* Consume the `enum' token. */
10050 cp_lexer_consume_token (parser->lexer);
10051 /* Remember that it's an enumeration type. */
10052 tag_type = enum_type;
10053 /* Parse the attributes. */
10054 attributes = cp_parser_attributes_opt (parser);
10056 /* Or, it might be `typename'. */
10057 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10060 /* Consume the `typename' token. */
10061 cp_lexer_consume_token (parser->lexer);
10062 /* Remember that it's a `typename' type. */
10063 tag_type = typename_type;
10064 /* The `typename' keyword is only allowed in templates. */
10065 if (!processing_template_decl)
10066 pedwarn ("using %<typename%> outside of template");
10068 /* Otherwise it must be a class-key. */
10071 tag_type = cp_parser_class_key (parser);
10072 if (tag_type == none_type)
10073 return error_mark_node;
10074 /* Parse the attributes. */
10075 attributes = cp_parser_attributes_opt (parser);
10078 /* Look for the `::' operator. */
10079 cp_parser_global_scope_opt (parser,
10080 /*current_scope_valid_p=*/false);
10081 /* Look for the nested-name-specifier. */
10082 if (tag_type == typename_type)
10084 if (!cp_parser_nested_name_specifier (parser,
10085 /*typename_keyword_p=*/true,
10086 /*check_dependency_p=*/true,
10089 return error_mark_node;
10092 /* Even though `typename' is not present, the proposed resolution
10093 to Core Issue 180 says that in `class A<T>::B', `B' should be
10094 considered a type-name, even if `A<T>' is dependent. */
10095 cp_parser_nested_name_specifier_opt (parser,
10096 /*typename_keyword_p=*/true,
10097 /*check_dependency_p=*/true,
10100 /* For everything but enumeration types, consider a template-id. */
10101 if (tag_type != enum_type)
10103 bool template_p = false;
10106 /* Allow the `template' keyword. */
10107 template_p = cp_parser_optional_template_keyword (parser);
10108 /* If we didn't see `template', we don't know if there's a
10109 template-id or not. */
10111 cp_parser_parse_tentatively (parser);
10112 /* Parse the template-id. */
10113 decl = cp_parser_template_id (parser, template_p,
10114 /*check_dependency_p=*/true,
10116 /* If we didn't find a template-id, look for an ordinary
10118 if (!template_p && !cp_parser_parse_definitely (parser))
10120 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10121 in effect, then we must assume that, upon instantiation, the
10122 template will correspond to a class. */
10123 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10124 && tag_type == typename_type)
10125 type = make_typename_type (parser->scope, decl,
10127 /*complain=*/tf_error);
10129 type = TREE_TYPE (decl);
10132 /* For an enumeration type, consider only a plain identifier. */
10135 identifier = cp_parser_identifier (parser);
10137 if (identifier == error_mark_node)
10139 parser->scope = NULL_TREE;
10140 return error_mark_node;
10143 /* For a `typename', we needn't call xref_tag. */
10144 if (tag_type == typename_type
10145 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10146 return cp_parser_make_typename_type (parser, parser->scope,
10148 /* Look up a qualified name in the usual way. */
10153 decl = cp_parser_lookup_name (parser, identifier,
10155 /*is_template=*/false,
10156 /*is_namespace=*/false,
10157 /*check_dependency=*/true,
10158 /*ambiguous_decls=*/NULL);
10160 /* If we are parsing friend declaration, DECL may be a
10161 TEMPLATE_DECL tree node here. However, we need to check
10162 whether this TEMPLATE_DECL results in valid code. Consider
10163 the following example:
10166 template <class T> class C {};
10169 template <class T> friend class N::C; // #1, valid code
10171 template <class T> class Y {
10172 friend class N::C; // #2, invalid code
10175 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10176 name lookup of `N::C'. We see that friend declaration must
10177 be template for the code to be valid. Note that
10178 processing_template_decl does not work here since it is
10179 always 1 for the above two cases. */
10181 decl = (cp_parser_maybe_treat_template_as_class
10182 (decl, /*tag_name_p=*/is_friend
10183 && parser->num_template_parameter_lists));
10185 if (TREE_CODE (decl) != TYPE_DECL)
10187 cp_parser_diagnose_invalid_type_name (parser,
10190 return error_mark_node;
10193 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10194 check_elaborated_type_specifier
10196 (parser->num_template_parameter_lists
10197 || DECL_SELF_REFERENCE_P (decl)));
10199 type = TREE_TYPE (decl);
10203 /* An elaborated-type-specifier sometimes introduces a new type and
10204 sometimes names an existing type. Normally, the rule is that it
10205 introduces a new type only if there is not an existing type of
10206 the same name already in scope. For example, given:
10209 void f() { struct S s; }
10211 the `struct S' in the body of `f' is the same `struct S' as in
10212 the global scope; the existing definition is used. However, if
10213 there were no global declaration, this would introduce a new
10214 local class named `S'.
10216 An exception to this rule applies to the following code:
10218 namespace N { struct S; }
10220 Here, the elaborated-type-specifier names a new type
10221 unconditionally; even if there is already an `S' in the
10222 containing scope this declaration names a new type.
10223 This exception only applies if the elaborated-type-specifier
10224 forms the complete declaration:
10228 A declaration consisting solely of `class-key identifier ;' is
10229 either a redeclaration of the name in the current scope or a
10230 forward declaration of the identifier as a class name. It
10231 introduces the name into the current scope.
10233 We are in this situation precisely when the next token is a `;'.
10235 An exception to the exception is that a `friend' declaration does
10236 *not* name a new type; i.e., given:
10238 struct S { friend struct T; };
10240 `T' is not a new type in the scope of `S'.
10242 Also, `new struct S' or `sizeof (struct S)' never results in the
10243 definition of a new type; a new type can only be declared in a
10244 declaration context. */
10250 /* Friends have special name lookup rules. */
10251 ts = ts_within_enclosing_non_class;
10252 else if (is_declaration
10253 && cp_lexer_next_token_is (parser->lexer,
10255 /* This is a `class-key identifier ;' */
10260 /* Warn about attributes. They are ignored. */
10262 warning (OPT_Wattributes,
10263 "type attributes are honored only at type definition");
10266 (parser->num_template_parameter_lists
10267 && (cp_parser_next_token_starts_class_definition_p (parser)
10268 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10269 /* An unqualified name was used to reference this type, so
10270 there were no qualifying templates. */
10271 if (!cp_parser_check_template_parameters (parser,
10272 /*num_templates=*/0))
10273 return error_mark_node;
10274 type = xref_tag (tag_type, identifier, ts, template_p);
10277 if (tag_type != enum_type)
10278 cp_parser_check_class_key (tag_type, type);
10280 /* A "<" cannot follow an elaborated type specifier. If that
10281 happens, the user was probably trying to form a template-id. */
10282 cp_parser_check_for_invalid_template_id (parser, type);
10287 /* Parse an enum-specifier.
10290 enum identifier [opt] { enumerator-list [opt] }
10293 enum identifier [opt] { enumerator-list [opt] } attributes
10295 Returns an ENUM_TYPE representing the enumeration. */
10298 cp_parser_enum_specifier (cp_parser* parser)
10303 /* Caller guarantees that the current token is 'enum', an identifier
10304 possibly follows, and the token after that is an opening brace.
10305 If we don't have an identifier, fabricate an anonymous name for
10306 the enumeration being defined. */
10307 cp_lexer_consume_token (parser->lexer);
10309 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10310 identifier = cp_parser_identifier (parser);
10312 identifier = make_anon_name ();
10314 /* Issue an error message if type-definitions are forbidden here. */
10315 cp_parser_check_type_definition (parser);
10317 /* Create the new type. We do this before consuming the opening brace
10318 so the enum will be recorded as being on the line of its tag (or the
10319 'enum' keyword, if there is no tag). */
10320 type = start_enum (identifier);
10322 /* Consume the opening brace. */
10323 cp_lexer_consume_token (parser->lexer);
10325 /* If the next token is not '}', then there are some enumerators. */
10326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10327 cp_parser_enumerator_list (parser, type);
10329 /* Consume the final '}'. */
10330 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10332 /* Look for trailing attributes to apply to this enumeration, and
10333 apply them if appropriate. */
10334 if (cp_parser_allow_gnu_extensions_p (parser))
10336 tree trailing_attr = cp_parser_attributes_opt (parser);
10337 cplus_decl_attributes (&type,
10339 (int) ATTR_FLAG_TYPE_IN_PLACE);
10342 /* Finish up the enumeration. */
10343 finish_enum (type);
10348 /* Parse an enumerator-list. The enumerators all have the indicated
10352 enumerator-definition
10353 enumerator-list , enumerator-definition */
10356 cp_parser_enumerator_list (cp_parser* parser, tree type)
10360 /* Parse an enumerator-definition. */
10361 cp_parser_enumerator_definition (parser, type);
10363 /* If the next token is not a ',', we've reached the end of
10365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10367 /* Otherwise, consume the `,' and keep going. */
10368 cp_lexer_consume_token (parser->lexer);
10369 /* If the next token is a `}', there is a trailing comma. */
10370 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10372 if (pedantic && !in_system_header)
10373 pedwarn ("comma at end of enumerator list");
10379 /* Parse an enumerator-definition. The enumerator has the indicated
10382 enumerator-definition:
10384 enumerator = constant-expression
10390 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10395 /* Look for the identifier. */
10396 identifier = cp_parser_identifier (parser);
10397 if (identifier == error_mark_node)
10400 /* If the next token is an '=', then there is an explicit value. */
10401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10403 /* Consume the `=' token. */
10404 cp_lexer_consume_token (parser->lexer);
10405 /* Parse the value. */
10406 value = cp_parser_constant_expression (parser,
10407 /*allow_non_constant_p=*/false,
10413 /* Create the enumerator. */
10414 build_enumerator (identifier, value, type);
10417 /* Parse a namespace-name.
10420 original-namespace-name
10423 Returns the NAMESPACE_DECL for the namespace. */
10426 cp_parser_namespace_name (cp_parser* parser)
10429 tree namespace_decl;
10431 /* Get the name of the namespace. */
10432 identifier = cp_parser_identifier (parser);
10433 if (identifier == error_mark_node)
10434 return error_mark_node;
10436 /* Look up the identifier in the currently active scope. Look only
10437 for namespaces, due to:
10439 [basic.lookup.udir]
10441 When looking up a namespace-name in a using-directive or alias
10442 definition, only namespace names are considered.
10446 [basic.lookup.qual]
10448 During the lookup of a name preceding the :: scope resolution
10449 operator, object, function, and enumerator names are ignored.
10451 (Note that cp_parser_class_or_namespace_name only calls this
10452 function if the token after the name is the scope resolution
10454 namespace_decl = cp_parser_lookup_name (parser, identifier,
10456 /*is_template=*/false,
10457 /*is_namespace=*/true,
10458 /*check_dependency=*/true,
10459 /*ambiguous_decls=*/NULL);
10460 /* If it's not a namespace, issue an error. */
10461 if (namespace_decl == error_mark_node
10462 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10464 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10465 error ("%qD is not a namespace-name", identifier);
10466 cp_parser_error (parser, "expected namespace-name");
10467 namespace_decl = error_mark_node;
10470 return namespace_decl;
10473 /* Parse a namespace-definition.
10475 namespace-definition:
10476 named-namespace-definition
10477 unnamed-namespace-definition
10479 named-namespace-definition:
10480 original-namespace-definition
10481 extension-namespace-definition
10483 original-namespace-definition:
10484 namespace identifier { namespace-body }
10486 extension-namespace-definition:
10487 namespace original-namespace-name { namespace-body }
10489 unnamed-namespace-definition:
10490 namespace { namespace-body } */
10493 cp_parser_namespace_definition (cp_parser* parser)
10495 tree identifier, attribs;
10497 /* Look for the `namespace' keyword. */
10498 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10500 /* Get the name of the namespace. We do not attempt to distinguish
10501 between an original-namespace-definition and an
10502 extension-namespace-definition at this point. The semantic
10503 analysis routines are responsible for that. */
10504 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10505 identifier = cp_parser_identifier (parser);
10507 identifier = NULL_TREE;
10509 /* Parse any specified attributes. */
10510 attribs = cp_parser_attributes_opt (parser);
10512 /* Look for the `{' to start the namespace. */
10513 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10514 /* Start the namespace. */
10515 push_namespace_with_attribs (identifier, attribs);
10516 /* Parse the body of the namespace. */
10517 cp_parser_namespace_body (parser);
10518 /* Finish the namespace. */
10520 /* Look for the final `}'. */
10521 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10524 /* Parse a namespace-body.
10527 declaration-seq [opt] */
10530 cp_parser_namespace_body (cp_parser* parser)
10532 cp_parser_declaration_seq_opt (parser);
10535 /* Parse a namespace-alias-definition.
10537 namespace-alias-definition:
10538 namespace identifier = qualified-namespace-specifier ; */
10541 cp_parser_namespace_alias_definition (cp_parser* parser)
10544 tree namespace_specifier;
10546 /* Look for the `namespace' keyword. */
10547 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10548 /* Look for the identifier. */
10549 identifier = cp_parser_identifier (parser);
10550 if (identifier == error_mark_node)
10552 /* Look for the `=' token. */
10553 cp_parser_require (parser, CPP_EQ, "`='");
10554 /* Look for the qualified-namespace-specifier. */
10555 namespace_specifier
10556 = cp_parser_qualified_namespace_specifier (parser);
10557 /* Look for the `;' token. */
10558 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10560 /* Register the alias in the symbol table. */
10561 do_namespace_alias (identifier, namespace_specifier);
10564 /* Parse a qualified-namespace-specifier.
10566 qualified-namespace-specifier:
10567 :: [opt] nested-name-specifier [opt] namespace-name
10569 Returns a NAMESPACE_DECL corresponding to the specified
10573 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10575 /* Look for the optional `::'. */
10576 cp_parser_global_scope_opt (parser,
10577 /*current_scope_valid_p=*/false);
10579 /* Look for the optional nested-name-specifier. */
10580 cp_parser_nested_name_specifier_opt (parser,
10581 /*typename_keyword_p=*/false,
10582 /*check_dependency_p=*/true,
10584 /*is_declaration=*/true);
10586 return cp_parser_namespace_name (parser);
10589 /* Parse a using-declaration.
10592 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10593 using :: unqualified-id ; */
10596 cp_parser_using_declaration (cp_parser* parser)
10599 bool typename_p = false;
10600 bool global_scope_p;
10605 /* Look for the `using' keyword. */
10606 cp_parser_require_keyword (parser, RID_USING, "`using'");
10608 /* Peek at the next token. */
10609 token = cp_lexer_peek_token (parser->lexer);
10610 /* See if it's `typename'. */
10611 if (token->keyword == RID_TYPENAME)
10613 /* Remember that we've seen it. */
10615 /* Consume the `typename' token. */
10616 cp_lexer_consume_token (parser->lexer);
10619 /* Look for the optional global scope qualification. */
10621 = (cp_parser_global_scope_opt (parser,
10622 /*current_scope_valid_p=*/false)
10625 /* If we saw `typename', or didn't see `::', then there must be a
10626 nested-name-specifier present. */
10627 if (typename_p || !global_scope_p)
10628 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10629 /*check_dependency_p=*/true,
10631 /*is_declaration=*/true);
10632 /* Otherwise, we could be in either of the two productions. In that
10633 case, treat the nested-name-specifier as optional. */
10635 qscope = cp_parser_nested_name_specifier_opt (parser,
10636 /*typename_keyword_p=*/false,
10637 /*check_dependency_p=*/true,
10639 /*is_declaration=*/true);
10641 qscope = global_namespace;
10643 /* Parse the unqualified-id. */
10644 identifier = cp_parser_unqualified_id (parser,
10645 /*template_keyword_p=*/false,
10646 /*check_dependency_p=*/true,
10647 /*declarator_p=*/true,
10648 /*optional_p=*/false);
10650 /* The function we call to handle a using-declaration is different
10651 depending on what scope we are in. */
10652 if (qscope == error_mark_node || identifier == error_mark_node)
10654 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10655 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10656 /* [namespace.udecl]
10658 A using declaration shall not name a template-id. */
10659 error ("a template-id may not appear in a using-declaration");
10662 if (at_class_scope_p ())
10664 /* Create the USING_DECL. */
10665 decl = do_class_using_decl (parser->scope, identifier);
10666 /* Add it to the list of members in this class. */
10667 finish_member_declaration (decl);
10671 decl = cp_parser_lookup_name_simple (parser, identifier);
10672 if (decl == error_mark_node)
10673 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10674 else if (!at_namespace_scope_p ())
10675 do_local_using_decl (decl, qscope, identifier);
10677 do_toplevel_using_decl (decl, qscope, identifier);
10681 /* Look for the final `;'. */
10682 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10685 /* Parse a using-directive.
10688 using namespace :: [opt] nested-name-specifier [opt]
10689 namespace-name ; */
10692 cp_parser_using_directive (cp_parser* parser)
10694 tree namespace_decl;
10697 /* Look for the `using' keyword. */
10698 cp_parser_require_keyword (parser, RID_USING, "`using'");
10699 /* And the `namespace' keyword. */
10700 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10701 /* Look for the optional `::' operator. */
10702 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10703 /* And the optional nested-name-specifier. */
10704 cp_parser_nested_name_specifier_opt (parser,
10705 /*typename_keyword_p=*/false,
10706 /*check_dependency_p=*/true,
10708 /*is_declaration=*/true);
10709 /* Get the namespace being used. */
10710 namespace_decl = cp_parser_namespace_name (parser);
10711 /* And any specified attributes. */
10712 attribs = cp_parser_attributes_opt (parser);
10713 /* Update the symbol table. */
10714 parse_using_directive (namespace_decl, attribs);
10715 /* Look for the final `;'. */
10716 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10719 /* Parse an asm-definition.
10722 asm ( string-literal ) ;
10727 asm volatile [opt] ( string-literal ) ;
10728 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10729 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10730 : asm-operand-list [opt] ) ;
10731 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10732 : asm-operand-list [opt]
10733 : asm-operand-list [opt] ) ; */
10736 cp_parser_asm_definition (cp_parser* parser)
10739 tree outputs = NULL_TREE;
10740 tree inputs = NULL_TREE;
10741 tree clobbers = NULL_TREE;
10743 bool volatile_p = false;
10744 bool extended_p = false;
10746 /* Look for the `asm' keyword. */
10747 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10748 /* See if the next token is `volatile'. */
10749 if (cp_parser_allow_gnu_extensions_p (parser)
10750 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10752 /* Remember that we saw the `volatile' keyword. */
10754 /* Consume the token. */
10755 cp_lexer_consume_token (parser->lexer);
10757 /* Look for the opening `('. */
10758 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10760 /* Look for the string. */
10761 string = cp_parser_string_literal (parser, false, false);
10762 if (string == error_mark_node)
10764 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10765 /*consume_paren=*/true);
10769 /* If we're allowing GNU extensions, check for the extended assembly
10770 syntax. Unfortunately, the `:' tokens need not be separated by
10771 a space in C, and so, for compatibility, we tolerate that here
10772 too. Doing that means that we have to treat the `::' operator as
10774 if (cp_parser_allow_gnu_extensions_p (parser)
10775 && at_function_scope_p ()
10776 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10777 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10779 bool inputs_p = false;
10780 bool clobbers_p = false;
10782 /* The extended syntax was used. */
10785 /* Look for outputs. */
10786 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10788 /* Consume the `:'. */
10789 cp_lexer_consume_token (parser->lexer);
10790 /* Parse the output-operands. */
10791 if (cp_lexer_next_token_is_not (parser->lexer,
10793 && cp_lexer_next_token_is_not (parser->lexer,
10795 && cp_lexer_next_token_is_not (parser->lexer,
10797 outputs = cp_parser_asm_operand_list (parser);
10799 /* If the next token is `::', there are no outputs, and the
10800 next token is the beginning of the inputs. */
10801 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10802 /* The inputs are coming next. */
10805 /* Look for inputs. */
10807 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10809 /* Consume the `:' or `::'. */
10810 cp_lexer_consume_token (parser->lexer);
10811 /* Parse the output-operands. */
10812 if (cp_lexer_next_token_is_not (parser->lexer,
10814 && cp_lexer_next_token_is_not (parser->lexer,
10816 inputs = cp_parser_asm_operand_list (parser);
10818 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10819 /* The clobbers are coming next. */
10822 /* Look for clobbers. */
10824 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10826 /* Consume the `:' or `::'. */
10827 cp_lexer_consume_token (parser->lexer);
10828 /* Parse the clobbers. */
10829 if (cp_lexer_next_token_is_not (parser->lexer,
10831 clobbers = cp_parser_asm_clobber_list (parser);
10834 /* Look for the closing `)'. */
10835 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10836 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10837 /*consume_paren=*/true);
10838 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10840 /* Create the ASM_EXPR. */
10841 if (at_function_scope_p ())
10843 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10845 /* If the extended syntax was not used, mark the ASM_EXPR. */
10848 tree temp = asm_stmt;
10849 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10850 temp = TREE_OPERAND (temp, 0);
10852 ASM_INPUT_P (temp) = 1;
10856 cgraph_add_asm_node (string);
10859 /* Declarators [gram.dcl.decl] */
10861 /* Parse an init-declarator.
10864 declarator initializer [opt]
10869 declarator asm-specification [opt] attributes [opt] initializer [opt]
10871 function-definition:
10872 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10874 decl-specifier-seq [opt] declarator function-try-block
10878 function-definition:
10879 __extension__ function-definition
10881 The DECL_SPECIFIERS apply to this declarator. Returns a
10882 representation of the entity declared. If MEMBER_P is TRUE, then
10883 this declarator appears in a class scope. The new DECL created by
10884 this declarator is returned.
10886 The CHECKS are access checks that should be performed once we know
10887 what entity is being declared (and, therefore, what classes have
10890 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10891 for a function-definition here as well. If the declarator is a
10892 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10893 be TRUE upon return. By that point, the function-definition will
10894 have been completely parsed.
10896 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10900 cp_parser_init_declarator (cp_parser* parser,
10901 cp_decl_specifier_seq *decl_specifiers,
10903 bool function_definition_allowed_p,
10905 int declares_class_or_enum,
10906 bool* function_definition_p)
10909 cp_declarator *declarator;
10910 tree prefix_attributes;
10912 tree asm_specification;
10914 tree decl = NULL_TREE;
10916 bool is_initialized;
10917 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10918 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10920 enum cpp_ttype initialization_kind;
10921 bool is_parenthesized_init = false;
10922 bool is_non_constant_init;
10923 int ctor_dtor_or_conv_p;
10925 tree pushed_scope = NULL;
10927 /* Gather the attributes that were provided with the
10928 decl-specifiers. */
10929 prefix_attributes = decl_specifiers->attributes;
10931 /* Assume that this is not the declarator for a function
10933 if (function_definition_p)
10934 *function_definition_p = false;
10936 /* Defer access checks while parsing the declarator; we cannot know
10937 what names are accessible until we know what is being
10939 resume_deferring_access_checks ();
10941 /* Parse the declarator. */
10943 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10944 &ctor_dtor_or_conv_p,
10945 /*parenthesized_p=*/NULL,
10946 /*member_p=*/false);
10947 /* Gather up the deferred checks. */
10948 stop_deferring_access_checks ();
10950 /* If the DECLARATOR was erroneous, there's no need to go
10952 if (declarator == cp_error_declarator)
10953 return error_mark_node;
10955 if (declares_class_or_enum & 2)
10956 cp_parser_check_for_definition_in_return_type (declarator,
10957 decl_specifiers->type);
10959 /* Figure out what scope the entity declared by the DECLARATOR is
10960 located in. `grokdeclarator' sometimes changes the scope, so
10961 we compute it now. */
10962 scope = get_scope_of_declarator (declarator);
10964 /* If we're allowing GNU extensions, look for an asm-specification
10966 if (cp_parser_allow_gnu_extensions_p (parser))
10968 /* Look for an asm-specification. */
10969 asm_specification = cp_parser_asm_specification_opt (parser);
10970 /* And attributes. */
10971 attributes = cp_parser_attributes_opt (parser);
10975 asm_specification = NULL_TREE;
10976 attributes = NULL_TREE;
10979 /* Peek at the next token. */
10980 token = cp_lexer_peek_token (parser->lexer);
10981 /* Check to see if the token indicates the start of a
10982 function-definition. */
10983 if (cp_parser_token_starts_function_definition_p (token))
10985 if (!function_definition_allowed_p)
10987 /* If a function-definition should not appear here, issue an
10989 cp_parser_error (parser,
10990 "a function-definition is not allowed here");
10991 return error_mark_node;
10995 /* Neither attributes nor an asm-specification are allowed
10996 on a function-definition. */
10997 if (asm_specification)
10998 error ("an asm-specification is not allowed on a function-definition");
11000 error ("attributes are not allowed on a function-definition");
11001 /* This is a function-definition. */
11002 *function_definition_p = true;
11004 /* Parse the function definition. */
11006 decl = cp_parser_save_member_function_body (parser,
11009 prefix_attributes);
11012 = (cp_parser_function_definition_from_specifiers_and_declarator
11013 (parser, decl_specifiers, prefix_attributes, declarator));
11021 Only in function declarations for constructors, destructors, and
11022 type conversions can the decl-specifier-seq be omitted.
11024 We explicitly postpone this check past the point where we handle
11025 function-definitions because we tolerate function-definitions
11026 that are missing their return types in some modes. */
11027 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11029 cp_parser_error (parser,
11030 "expected constructor, destructor, or type conversion");
11031 return error_mark_node;
11034 /* An `=' or an `(' indicates an initializer. */
11035 if (token->type == CPP_EQ
11036 || token->type == CPP_OPEN_PAREN)
11038 is_initialized = true;
11039 initialization_kind = token->type;
11043 /* If the init-declarator isn't initialized and isn't followed by a
11044 `,' or `;', it's not a valid init-declarator. */
11045 if (token->type != CPP_COMMA
11046 && token->type != CPP_SEMICOLON)
11048 cp_parser_error (parser, "expected initializer");
11049 return error_mark_node;
11051 is_initialized = false;
11052 initialization_kind = CPP_EOF;
11055 /* Because start_decl has side-effects, we should only call it if we
11056 know we're going ahead. By this point, we know that we cannot
11057 possibly be looking at any other construct. */
11058 cp_parser_commit_to_tentative_parse (parser);
11060 /* If the decl specifiers were bad, issue an error now that we're
11061 sure this was intended to be a declarator. Then continue
11062 declaring the variable(s), as int, to try to cut down on further
11064 if (decl_specifiers->any_specifiers_p
11065 && decl_specifiers->type == error_mark_node)
11067 cp_parser_error (parser, "invalid type in declaration");
11068 decl_specifiers->type = integer_type_node;
11071 /* Check to see whether or not this declaration is a friend. */
11072 friend_p = cp_parser_friend_p (decl_specifiers);
11074 /* Check that the number of template-parameter-lists is OK. */
11075 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11076 return error_mark_node;
11078 /* Enter the newly declared entry in the symbol table. If we're
11079 processing a declaration in a class-specifier, we wait until
11080 after processing the initializer. */
11083 if (parser->in_unbraced_linkage_specification_p)
11085 decl_specifiers->storage_class = sc_extern;
11086 have_extern_spec = false;
11088 decl = start_decl (declarator, decl_specifiers,
11089 is_initialized, attributes, prefix_attributes,
11093 /* Enter the SCOPE. That way unqualified names appearing in the
11094 initializer will be looked up in SCOPE. */
11095 pushed_scope = push_scope (scope);
11097 /* Perform deferred access control checks, now that we know in which
11098 SCOPE the declared entity resides. */
11099 if (!member_p && decl)
11101 tree saved_current_function_decl = NULL_TREE;
11103 /* If the entity being declared is a function, pretend that we
11104 are in its scope. If it is a `friend', it may have access to
11105 things that would not otherwise be accessible. */
11106 if (TREE_CODE (decl) == FUNCTION_DECL)
11108 saved_current_function_decl = current_function_decl;
11109 current_function_decl = decl;
11112 /* Perform access checks for template parameters. */
11113 cp_parser_perform_template_parameter_access_checks (checks);
11115 /* Perform the access control checks for the declarator and the
11116 the decl-specifiers. */
11117 perform_deferred_access_checks ();
11119 /* Restore the saved value. */
11120 if (TREE_CODE (decl) == FUNCTION_DECL)
11121 current_function_decl = saved_current_function_decl;
11124 /* Parse the initializer. */
11125 initializer = NULL_TREE;
11126 is_parenthesized_init = false;
11127 is_non_constant_init = true;
11128 if (is_initialized)
11130 if (declarator->kind == cdk_function
11131 && declarator->declarator->kind == cdk_id
11132 && initialization_kind == CPP_EQ)
11133 initializer = cp_parser_pure_specifier (parser);
11135 initializer = cp_parser_initializer (parser,
11136 &is_parenthesized_init,
11137 &is_non_constant_init);
11140 /* The old parser allows attributes to appear after a parenthesized
11141 initializer. Mark Mitchell proposed removing this functionality
11142 on the GCC mailing lists on 2002-08-13. This parser accepts the
11143 attributes -- but ignores them. */
11144 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11145 if (cp_parser_attributes_opt (parser))
11146 warning (OPT_Wattributes,
11147 "attributes after parenthesized initializer ignored");
11149 /* For an in-class declaration, use `grokfield' to create the
11155 pop_scope (pushed_scope);
11156 pushed_scope = false;
11158 decl = grokfield (declarator, decl_specifiers,
11159 initializer, !is_non_constant_init,
11160 /*asmspec=*/NULL_TREE,
11161 prefix_attributes);
11162 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11163 cp_parser_save_default_args (parser, decl);
11166 /* Finish processing the declaration. But, skip friend
11168 if (!friend_p && decl && decl != error_mark_node)
11170 cp_finish_decl (decl,
11171 initializer, !is_non_constant_init,
11173 /* If the initializer is in parentheses, then this is
11174 a direct-initialization, which means that an
11175 `explicit' constructor is OK. Otherwise, an
11176 `explicit' constructor cannot be used. */
11177 ((is_parenthesized_init || !is_initialized)
11178 ? 0 : LOOKUP_ONLYCONVERTING));
11180 if (!friend_p && pushed_scope)
11181 pop_scope (pushed_scope);
11186 /* Parse a declarator.
11190 ptr-operator declarator
11192 abstract-declarator:
11193 ptr-operator abstract-declarator [opt]
11194 direct-abstract-declarator
11199 attributes [opt] direct-declarator
11200 attributes [opt] ptr-operator declarator
11202 abstract-declarator:
11203 attributes [opt] ptr-operator abstract-declarator [opt]
11204 attributes [opt] direct-abstract-declarator
11206 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11207 detect constructor, destructor or conversion operators. It is set
11208 to -1 if the declarator is a name, and +1 if it is a
11209 function. Otherwise it is set to zero. Usually you just want to
11210 test for >0, but internally the negative value is used.
11212 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11213 a decl-specifier-seq unless it declares a constructor, destructor,
11214 or conversion. It might seem that we could check this condition in
11215 semantic analysis, rather than parsing, but that makes it difficult
11216 to handle something like `f()'. We want to notice that there are
11217 no decl-specifiers, and therefore realize that this is an
11218 expression, not a declaration.)
11220 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11221 the declarator is a direct-declarator of the form "(...)".
11223 MEMBER_P is true iff this declarator is a member-declarator. */
11225 static cp_declarator *
11226 cp_parser_declarator (cp_parser* parser,
11227 cp_parser_declarator_kind dcl_kind,
11228 int* ctor_dtor_or_conv_p,
11229 bool* parenthesized_p,
11233 cp_declarator *declarator;
11234 enum tree_code code;
11235 cp_cv_quals cv_quals;
11237 tree attributes = NULL_TREE;
11239 /* Assume this is not a constructor, destructor, or type-conversion
11241 if (ctor_dtor_or_conv_p)
11242 *ctor_dtor_or_conv_p = 0;
11244 if (cp_parser_allow_gnu_extensions_p (parser))
11245 attributes = cp_parser_attributes_opt (parser);
11247 /* Peek at the next token. */
11248 token = cp_lexer_peek_token (parser->lexer);
11250 /* Check for the ptr-operator production. */
11251 cp_parser_parse_tentatively (parser);
11252 /* Parse the ptr-operator. */
11253 code = cp_parser_ptr_operator (parser,
11256 /* If that worked, then we have a ptr-operator. */
11257 if (cp_parser_parse_definitely (parser))
11259 /* If a ptr-operator was found, then this declarator was not
11261 if (parenthesized_p)
11262 *parenthesized_p = true;
11263 /* The dependent declarator is optional if we are parsing an
11264 abstract-declarator. */
11265 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11266 cp_parser_parse_tentatively (parser);
11268 /* Parse the dependent declarator. */
11269 declarator = cp_parser_declarator (parser, dcl_kind,
11270 /*ctor_dtor_or_conv_p=*/NULL,
11271 /*parenthesized_p=*/NULL,
11272 /*member_p=*/false);
11274 /* If we are parsing an abstract-declarator, we must handle the
11275 case where the dependent declarator is absent. */
11276 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11277 && !cp_parser_parse_definitely (parser))
11280 /* Build the representation of the ptr-operator. */
11282 declarator = make_ptrmem_declarator (cv_quals,
11285 else if (code == INDIRECT_REF)
11286 declarator = make_pointer_declarator (cv_quals, declarator);
11288 declarator = make_reference_declarator (cv_quals, declarator);
11290 /* Everything else is a direct-declarator. */
11293 if (parenthesized_p)
11294 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11296 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11297 ctor_dtor_or_conv_p,
11301 if (attributes && declarator != cp_error_declarator)
11302 declarator->attributes = attributes;
11307 /* Parse a direct-declarator or direct-abstract-declarator.
11311 direct-declarator ( parameter-declaration-clause )
11312 cv-qualifier-seq [opt]
11313 exception-specification [opt]
11314 direct-declarator [ constant-expression [opt] ]
11317 direct-abstract-declarator:
11318 direct-abstract-declarator [opt]
11319 ( parameter-declaration-clause )
11320 cv-qualifier-seq [opt]
11321 exception-specification [opt]
11322 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11323 ( abstract-declarator )
11325 Returns a representation of the declarator. DCL_KIND is
11326 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11327 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11328 we are parsing a direct-declarator. It is
11329 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11330 of ambiguity we prefer an abstract declarator, as per
11331 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11332 cp_parser_declarator. */
11334 static cp_declarator *
11335 cp_parser_direct_declarator (cp_parser* parser,
11336 cp_parser_declarator_kind dcl_kind,
11337 int* ctor_dtor_or_conv_p,
11341 cp_declarator *declarator = NULL;
11342 tree scope = NULL_TREE;
11343 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11344 bool saved_in_declarator_p = parser->in_declarator_p;
11346 tree pushed_scope = NULL_TREE;
11350 /* Peek at the next token. */
11351 token = cp_lexer_peek_token (parser->lexer);
11352 if (token->type == CPP_OPEN_PAREN)
11354 /* This is either a parameter-declaration-clause, or a
11355 parenthesized declarator. When we know we are parsing a
11356 named declarator, it must be a parenthesized declarator
11357 if FIRST is true. For instance, `(int)' is a
11358 parameter-declaration-clause, with an omitted
11359 direct-abstract-declarator. But `((*))', is a
11360 parenthesized abstract declarator. Finally, when T is a
11361 template parameter `(T)' is a
11362 parameter-declaration-clause, and not a parenthesized
11365 We first try and parse a parameter-declaration-clause,
11366 and then try a nested declarator (if FIRST is true).
11368 It is not an error for it not to be a
11369 parameter-declaration-clause, even when FIRST is
11375 The first is the declaration of a function while the
11376 second is a the definition of a variable, including its
11379 Having seen only the parenthesis, we cannot know which of
11380 these two alternatives should be selected. Even more
11381 complex are examples like:
11386 The former is a function-declaration; the latter is a
11387 variable initialization.
11389 Thus again, we try a parameter-declaration-clause, and if
11390 that fails, we back out and return. */
11392 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11394 cp_parameter_declarator *params;
11395 unsigned saved_num_template_parameter_lists;
11397 /* In a member-declarator, the only valid interpretation
11398 of a parenthesis is the start of a
11399 parameter-declaration-clause. (It is invalid to
11400 initialize a static data member with a parenthesized
11401 initializer; only the "=" form of initialization is
11404 cp_parser_parse_tentatively (parser);
11406 /* Consume the `('. */
11407 cp_lexer_consume_token (parser->lexer);
11410 /* If this is going to be an abstract declarator, we're
11411 in a declarator and we can't have default args. */
11412 parser->default_arg_ok_p = false;
11413 parser->in_declarator_p = true;
11416 /* Inside the function parameter list, surrounding
11417 template-parameter-lists do not apply. */
11418 saved_num_template_parameter_lists
11419 = parser->num_template_parameter_lists;
11420 parser->num_template_parameter_lists = 0;
11422 /* Parse the parameter-declaration-clause. */
11423 params = cp_parser_parameter_declaration_clause (parser);
11425 parser->num_template_parameter_lists
11426 = saved_num_template_parameter_lists;
11428 /* If all went well, parse the cv-qualifier-seq and the
11429 exception-specification. */
11430 if (member_p || cp_parser_parse_definitely (parser))
11432 cp_cv_quals cv_quals;
11433 tree exception_specification;
11435 if (ctor_dtor_or_conv_p)
11436 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11438 /* Consume the `)'. */
11439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11441 /* Parse the cv-qualifier-seq. */
11442 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11443 /* And the exception-specification. */
11444 exception_specification
11445 = cp_parser_exception_specification_opt (parser);
11447 /* Create the function-declarator. */
11448 declarator = make_call_declarator (declarator,
11451 exception_specification);
11452 /* Any subsequent parameter lists are to do with
11453 return type, so are not those of the declared
11455 parser->default_arg_ok_p = false;
11457 /* Repeat the main loop. */
11462 /* If this is the first, we can try a parenthesized
11466 bool saved_in_type_id_in_expr_p;
11468 parser->default_arg_ok_p = saved_default_arg_ok_p;
11469 parser->in_declarator_p = saved_in_declarator_p;
11471 /* Consume the `('. */
11472 cp_lexer_consume_token (parser->lexer);
11473 /* Parse the nested declarator. */
11474 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11475 parser->in_type_id_in_expr_p = true;
11477 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11478 /*parenthesized_p=*/NULL,
11480 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11482 /* Expect a `)'. */
11483 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11484 declarator = cp_error_declarator;
11485 if (declarator == cp_error_declarator)
11488 goto handle_declarator;
11490 /* Otherwise, we must be done. */
11494 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11495 && token->type == CPP_OPEN_SQUARE)
11497 /* Parse an array-declarator. */
11500 if (ctor_dtor_or_conv_p)
11501 *ctor_dtor_or_conv_p = 0;
11504 parser->default_arg_ok_p = false;
11505 parser->in_declarator_p = true;
11506 /* Consume the `['. */
11507 cp_lexer_consume_token (parser->lexer);
11508 /* Peek at the next token. */
11509 token = cp_lexer_peek_token (parser->lexer);
11510 /* If the next token is `]', then there is no
11511 constant-expression. */
11512 if (token->type != CPP_CLOSE_SQUARE)
11514 bool non_constant_p;
11517 = cp_parser_constant_expression (parser,
11518 /*allow_non_constant=*/true,
11520 if (!non_constant_p)
11521 bounds = fold_non_dependent_expr (bounds);
11522 /* Normally, the array bound must be an integral constant
11523 expression. However, as an extension, we allow VLAs
11524 in function scopes. */
11525 else if (!at_function_scope_p ())
11527 error ("array bound is not an integer constant");
11528 bounds = error_mark_node;
11532 bounds = NULL_TREE;
11533 /* Look for the closing `]'. */
11534 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11536 declarator = cp_error_declarator;
11540 declarator = make_array_declarator (declarator, bounds);
11542 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11544 tree qualifying_scope;
11545 tree unqualified_name;
11546 special_function_kind sfk;
11549 /* Parse a declarator-id */
11550 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11552 cp_parser_parse_tentatively (parser);
11554 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11555 qualifying_scope = parser->scope;
11558 if (!cp_parser_parse_definitely (parser))
11559 unqualified_name = error_mark_node;
11560 else if (unqualified_name
11561 && (qualifying_scope
11562 || (TREE_CODE (unqualified_name)
11563 != IDENTIFIER_NODE)))
11565 cp_parser_error (parser, "expected unqualified-id");
11566 unqualified_name = error_mark_node;
11570 if (!unqualified_name)
11572 if (unqualified_name == error_mark_node)
11574 declarator = cp_error_declarator;
11578 if (qualifying_scope && at_namespace_scope_p ()
11579 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11581 /* In the declaration of a member of a template class
11582 outside of the class itself, the SCOPE will sometimes
11583 be a TYPENAME_TYPE. For example, given:
11585 template <typename T>
11586 int S<T>::R::i = 3;
11588 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11589 this context, we must resolve S<T>::R to an ordinary
11590 type, rather than a typename type.
11592 The reason we normally avoid resolving TYPENAME_TYPEs
11593 is that a specialization of `S' might render
11594 `S<T>::R' not a type. However, if `S' is
11595 specialized, then this `i' will not be used, so there
11596 is no harm in resolving the types here. */
11599 /* Resolve the TYPENAME_TYPE. */
11600 type = resolve_typename_type (qualifying_scope,
11601 /*only_current_p=*/false);
11602 /* If that failed, the declarator is invalid. */
11603 if (type == error_mark_node)
11604 error ("%<%T::%D%> is not a type",
11605 TYPE_CONTEXT (qualifying_scope),
11606 TYPE_IDENTIFIER (qualifying_scope));
11607 qualifying_scope = type;
11611 if (unqualified_name)
11615 if (qualifying_scope
11616 && CLASS_TYPE_P (qualifying_scope))
11617 class_type = qualifying_scope;
11619 class_type = current_class_type;
11621 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11623 tree name_type = TREE_TYPE (unqualified_name);
11624 if (class_type && same_type_p (name_type, class_type))
11626 if (qualifying_scope
11627 && CLASSTYPE_USE_TEMPLATE (name_type))
11629 error ("invalid use of constructor as a template");
11630 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11631 "name the constructor in a qualified name",
11633 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11634 class_type, name_type);
11635 declarator = cp_error_declarator;
11639 unqualified_name = constructor_name (class_type);
11643 /* We do not attempt to print the declarator
11644 here because we do not have enough
11645 information about its original syntactic
11647 cp_parser_error (parser, "invalid declarator");
11648 declarator = cp_error_declarator;
11655 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11656 sfk = sfk_destructor;
11657 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11658 sfk = sfk_conversion;
11659 else if (/* There's no way to declare a constructor
11660 for an anonymous type, even if the type
11661 got a name for linkage purposes. */
11662 !TYPE_WAS_ANONYMOUS (class_type)
11663 && constructor_name_p (unqualified_name,
11666 unqualified_name = constructor_name (class_type);
11667 sfk = sfk_constructor;
11670 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11671 *ctor_dtor_or_conv_p = -1;
11674 declarator = make_id_declarator (qualifying_scope,
11677 declarator->id_loc = token->location;
11679 handle_declarator:;
11680 scope = get_scope_of_declarator (declarator);
11682 /* Any names that appear after the declarator-id for a
11683 member are looked up in the containing scope. */
11684 pushed_scope = push_scope (scope);
11685 parser->in_declarator_p = true;
11686 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11687 || (declarator && declarator->kind == cdk_id))
11688 /* Default args are only allowed on function
11690 parser->default_arg_ok_p = saved_default_arg_ok_p;
11692 parser->default_arg_ok_p = false;
11701 /* For an abstract declarator, we might wind up with nothing at this
11702 point. That's an error; the declarator is not optional. */
11704 cp_parser_error (parser, "expected declarator");
11706 /* If we entered a scope, we must exit it now. */
11708 pop_scope (pushed_scope);
11710 parser->default_arg_ok_p = saved_default_arg_ok_p;
11711 parser->in_declarator_p = saved_in_declarator_p;
11716 /* Parse a ptr-operator.
11719 * cv-qualifier-seq [opt]
11721 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11726 & cv-qualifier-seq [opt]
11728 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11729 Returns ADDR_EXPR if a reference was used. In the case of a
11730 pointer-to-member, *TYPE is filled in with the TYPE containing the
11731 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11732 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11733 ERROR_MARK if an error occurred. */
11735 static enum tree_code
11736 cp_parser_ptr_operator (cp_parser* parser,
11738 cp_cv_quals *cv_quals)
11740 enum tree_code code = ERROR_MARK;
11743 /* Assume that it's not a pointer-to-member. */
11745 /* And that there are no cv-qualifiers. */
11746 *cv_quals = TYPE_UNQUALIFIED;
11748 /* Peek at the next token. */
11749 token = cp_lexer_peek_token (parser->lexer);
11750 /* If it's a `*' or `&' we have a pointer or reference. */
11751 if (token->type == CPP_MULT || token->type == CPP_AND)
11753 /* Remember which ptr-operator we were processing. */
11754 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11756 /* Consume the `*' or `&'. */
11757 cp_lexer_consume_token (parser->lexer);
11759 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11760 `&', if we are allowing GNU extensions. (The only qualifier
11761 that can legally appear after `&' is `restrict', but that is
11762 enforced during semantic analysis. */
11763 if (code == INDIRECT_REF
11764 || cp_parser_allow_gnu_extensions_p (parser))
11765 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11769 /* Try the pointer-to-member case. */
11770 cp_parser_parse_tentatively (parser);
11771 /* Look for the optional `::' operator. */
11772 cp_parser_global_scope_opt (parser,
11773 /*current_scope_valid_p=*/false);
11774 /* Look for the nested-name specifier. */
11775 cp_parser_nested_name_specifier (parser,
11776 /*typename_keyword_p=*/false,
11777 /*check_dependency_p=*/true,
11779 /*is_declaration=*/false);
11780 /* If we found it, and the next token is a `*', then we are
11781 indeed looking at a pointer-to-member operator. */
11782 if (!cp_parser_error_occurred (parser)
11783 && cp_parser_require (parser, CPP_MULT, "`*'"))
11785 /* Indicate that the `*' operator was used. */
11786 code = INDIRECT_REF;
11788 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11789 error ("%qD is a namespace", parser->scope);
11792 /* The type of which the member is a member is given by the
11794 *type = parser->scope;
11795 /* The next name will not be qualified. */
11796 parser->scope = NULL_TREE;
11797 parser->qualifying_scope = NULL_TREE;
11798 parser->object_scope = NULL_TREE;
11799 /* Look for the optional cv-qualifier-seq. */
11800 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11803 /* If that didn't work we don't have a ptr-operator. */
11804 if (!cp_parser_parse_definitely (parser))
11805 cp_parser_error (parser, "expected ptr-operator");
11811 /* Parse an (optional) cv-qualifier-seq.
11814 cv-qualifier cv-qualifier-seq [opt]
11825 Returns a bitmask representing the cv-qualifiers. */
11828 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11830 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11835 cp_cv_quals cv_qualifier;
11837 /* Peek at the next token. */
11838 token = cp_lexer_peek_token (parser->lexer);
11839 /* See if it's a cv-qualifier. */
11840 switch (token->keyword)
11843 cv_qualifier = TYPE_QUAL_CONST;
11847 cv_qualifier = TYPE_QUAL_VOLATILE;
11851 cv_qualifier = TYPE_QUAL_RESTRICT;
11855 cv_qualifier = TYPE_UNQUALIFIED;
11862 if (cv_quals & cv_qualifier)
11864 error ("duplicate cv-qualifier");
11865 cp_lexer_purge_token (parser->lexer);
11869 cp_lexer_consume_token (parser->lexer);
11870 cv_quals |= cv_qualifier;
11877 /* Parse a declarator-id.
11881 :: [opt] nested-name-specifier [opt] type-name
11883 In the `id-expression' case, the value returned is as for
11884 cp_parser_id_expression if the id-expression was an unqualified-id.
11885 If the id-expression was a qualified-id, then a SCOPE_REF is
11886 returned. The first operand is the scope (either a NAMESPACE_DECL
11887 or TREE_TYPE), but the second is still just a representation of an
11891 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11894 /* The expression must be an id-expression. Assume that qualified
11895 names are the names of types so that:
11898 int S<T>::R::i = 3;
11900 will work; we must treat `S<T>::R' as the name of a type.
11901 Similarly, assume that qualified names are templates, where
11905 int S<T>::R<T>::i = 3;
11908 id = cp_parser_id_expression (parser,
11909 /*template_keyword_p=*/false,
11910 /*check_dependency_p=*/false,
11911 /*template_p=*/NULL,
11912 /*declarator_p=*/true,
11914 if (id && BASELINK_P (id))
11915 id = BASELINK_FUNCTIONS (id);
11919 /* Parse a type-id.
11922 type-specifier-seq abstract-declarator [opt]
11924 Returns the TYPE specified. */
11927 cp_parser_type_id (cp_parser* parser)
11929 cp_decl_specifier_seq type_specifier_seq;
11930 cp_declarator *abstract_declarator;
11932 /* Parse the type-specifier-seq. */
11933 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11934 &type_specifier_seq);
11935 if (type_specifier_seq.type == error_mark_node)
11936 return error_mark_node;
11938 /* There might or might not be an abstract declarator. */
11939 cp_parser_parse_tentatively (parser);
11940 /* Look for the declarator. */
11941 abstract_declarator
11942 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11943 /*parenthesized_p=*/NULL,
11944 /*member_p=*/false);
11945 /* Check to see if there really was a declarator. */
11946 if (!cp_parser_parse_definitely (parser))
11947 abstract_declarator = NULL;
11949 return groktypename (&type_specifier_seq, abstract_declarator);
11952 /* Parse a type-specifier-seq.
11954 type-specifier-seq:
11955 type-specifier type-specifier-seq [opt]
11959 type-specifier-seq:
11960 attributes type-specifier-seq [opt]
11962 If IS_CONDITION is true, we are at the start of a "condition",
11963 e.g., we've just seen "if (".
11965 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11968 cp_parser_type_specifier_seq (cp_parser* parser,
11970 cp_decl_specifier_seq *type_specifier_seq)
11972 bool seen_type_specifier = false;
11973 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11975 /* Clear the TYPE_SPECIFIER_SEQ. */
11976 clear_decl_specs (type_specifier_seq);
11978 /* Parse the type-specifiers and attributes. */
11981 tree type_specifier;
11982 bool is_cv_qualifier;
11984 /* Check for attributes first. */
11985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11987 type_specifier_seq->attributes =
11988 chainon (type_specifier_seq->attributes,
11989 cp_parser_attributes_opt (parser));
11993 /* Look for the type-specifier. */
11994 type_specifier = cp_parser_type_specifier (parser,
11996 type_specifier_seq,
11997 /*is_declaration=*/false,
12000 if (!type_specifier)
12002 /* If the first type-specifier could not be found, this is not a
12003 type-specifier-seq at all. */
12004 if (!seen_type_specifier)
12006 cp_parser_error (parser, "expected type-specifier");
12007 type_specifier_seq->type = error_mark_node;
12010 /* If subsequent type-specifiers could not be found, the
12011 type-specifier-seq is complete. */
12015 seen_type_specifier = true;
12016 /* The standard says that a condition can be:
12018 type-specifier-seq declarator = assignment-expression
12025 we should treat the "S" as a declarator, not as a
12026 type-specifier. The standard doesn't say that explicitly for
12027 type-specifier-seq, but it does say that for
12028 decl-specifier-seq in an ordinary declaration. Perhaps it
12029 would be clearer just to allow a decl-specifier-seq here, and
12030 then add a semantic restriction that if any decl-specifiers
12031 that are not type-specifiers appear, the program is invalid. */
12032 if (is_condition && !is_cv_qualifier)
12033 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12037 /* Parse a parameter-declaration-clause.
12039 parameter-declaration-clause:
12040 parameter-declaration-list [opt] ... [opt]
12041 parameter-declaration-list , ...
12043 Returns a representation for the parameter declarations. A return
12044 value of NULL indicates a parameter-declaration-clause consisting
12045 only of an ellipsis. */
12047 static cp_parameter_declarator *
12048 cp_parser_parameter_declaration_clause (cp_parser* parser)
12050 cp_parameter_declarator *parameters;
12055 /* Peek at the next token. */
12056 token = cp_lexer_peek_token (parser->lexer);
12057 /* Check for trivial parameter-declaration-clauses. */
12058 if (token->type == CPP_ELLIPSIS)
12060 /* Consume the `...' token. */
12061 cp_lexer_consume_token (parser->lexer);
12064 else if (token->type == CPP_CLOSE_PAREN)
12065 /* There are no parameters. */
12067 #ifndef NO_IMPLICIT_EXTERN_C
12068 if (in_system_header && current_class_type == NULL
12069 && current_lang_name == lang_name_c)
12073 return no_parameters;
12075 /* Check for `(void)', too, which is a special case. */
12076 else if (token->keyword == RID_VOID
12077 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12078 == CPP_CLOSE_PAREN))
12080 /* Consume the `void' token. */
12081 cp_lexer_consume_token (parser->lexer);
12082 /* There are no parameters. */
12083 return no_parameters;
12086 /* Parse the parameter-declaration-list. */
12087 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12088 /* If a parse error occurred while parsing the
12089 parameter-declaration-list, then the entire
12090 parameter-declaration-clause is erroneous. */
12094 /* Peek at the next token. */
12095 token = cp_lexer_peek_token (parser->lexer);
12096 /* If it's a `,', the clause should terminate with an ellipsis. */
12097 if (token->type == CPP_COMMA)
12099 /* Consume the `,'. */
12100 cp_lexer_consume_token (parser->lexer);
12101 /* Expect an ellipsis. */
12103 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12105 /* It might also be `...' if the optional trailing `,' was
12107 else if (token->type == CPP_ELLIPSIS)
12109 /* Consume the `...' token. */
12110 cp_lexer_consume_token (parser->lexer);
12111 /* And remember that we saw it. */
12115 ellipsis_p = false;
12117 /* Finish the parameter list. */
12118 if (parameters && ellipsis_p)
12119 parameters->ellipsis_p = true;
12124 /* Parse a parameter-declaration-list.
12126 parameter-declaration-list:
12127 parameter-declaration
12128 parameter-declaration-list , parameter-declaration
12130 Returns a representation of the parameter-declaration-list, as for
12131 cp_parser_parameter_declaration_clause. However, the
12132 `void_list_node' is never appended to the list. Upon return,
12133 *IS_ERROR will be true iff an error occurred. */
12135 static cp_parameter_declarator *
12136 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12138 cp_parameter_declarator *parameters = NULL;
12139 cp_parameter_declarator **tail = ¶meters;
12141 /* Assume all will go well. */
12144 /* Look for more parameters. */
12147 cp_parameter_declarator *parameter;
12148 bool parenthesized_p;
12149 /* Parse the parameter. */
12151 = cp_parser_parameter_declaration (parser,
12152 /*template_parm_p=*/false,
12155 /* If a parse error occurred parsing the parameter declaration,
12156 then the entire parameter-declaration-list is erroneous. */
12163 /* Add the new parameter to the list. */
12165 tail = ¶meter->next;
12167 /* Peek at the next token. */
12168 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12169 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12170 /* These are for Objective-C++ */
12171 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12172 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12173 /* The parameter-declaration-list is complete. */
12175 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12179 /* Peek at the next token. */
12180 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12181 /* If it's an ellipsis, then the list is complete. */
12182 if (token->type == CPP_ELLIPSIS)
12184 /* Otherwise, there must be more parameters. Consume the
12186 cp_lexer_consume_token (parser->lexer);
12187 /* When parsing something like:
12189 int i(float f, double d)
12191 we can tell after seeing the declaration for "f" that we
12192 are not looking at an initialization of a variable "i",
12193 but rather at the declaration of a function "i".
12195 Due to the fact that the parsing of template arguments
12196 (as specified to a template-id) requires backtracking we
12197 cannot use this technique when inside a template argument
12199 if (!parser->in_template_argument_list_p
12200 && !parser->in_type_id_in_expr_p
12201 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12202 /* However, a parameter-declaration of the form
12203 "foat(f)" (which is a valid declaration of a
12204 parameter "f") can also be interpreted as an
12205 expression (the conversion of "f" to "float"). */
12206 && !parenthesized_p)
12207 cp_parser_commit_to_tentative_parse (parser);
12211 cp_parser_error (parser, "expected %<,%> or %<...%>");
12212 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12213 cp_parser_skip_to_closing_parenthesis (parser,
12214 /*recovering=*/true,
12215 /*or_comma=*/false,
12216 /*consume_paren=*/false);
12224 /* Parse a parameter declaration.
12226 parameter-declaration:
12227 decl-specifier-seq declarator
12228 decl-specifier-seq declarator = assignment-expression
12229 decl-specifier-seq abstract-declarator [opt]
12230 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12232 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12233 declares a template parameter. (In that case, a non-nested `>'
12234 token encountered during the parsing of the assignment-expression
12235 is not interpreted as a greater-than operator.)
12237 Returns a representation of the parameter, or NULL if an error
12238 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12239 true iff the declarator is of the form "(p)". */
12241 static cp_parameter_declarator *
12242 cp_parser_parameter_declaration (cp_parser *parser,
12243 bool template_parm_p,
12244 bool *parenthesized_p)
12246 int declares_class_or_enum;
12247 bool greater_than_is_operator_p;
12248 cp_decl_specifier_seq decl_specifiers;
12249 cp_declarator *declarator;
12250 tree default_argument;
12252 const char *saved_message;
12254 /* In a template parameter, `>' is not an operator.
12258 When parsing a default template-argument for a non-type
12259 template-parameter, the first non-nested `>' is taken as the end
12260 of the template parameter-list rather than a greater-than
12262 greater_than_is_operator_p = !template_parm_p;
12264 /* Type definitions may not appear in parameter types. */
12265 saved_message = parser->type_definition_forbidden_message;
12266 parser->type_definition_forbidden_message
12267 = "types may not be defined in parameter types";
12269 /* Parse the declaration-specifiers. */
12270 cp_parser_decl_specifier_seq (parser,
12271 CP_PARSER_FLAGS_NONE,
12273 &declares_class_or_enum);
12274 /* If an error occurred, there's no reason to attempt to parse the
12275 rest of the declaration. */
12276 if (cp_parser_error_occurred (parser))
12278 parser->type_definition_forbidden_message = saved_message;
12282 /* Peek at the next token. */
12283 token = cp_lexer_peek_token (parser->lexer);
12284 /* If the next token is a `)', `,', `=', `>', or `...', then there
12285 is no declarator. */
12286 if (token->type == CPP_CLOSE_PAREN
12287 || token->type == CPP_COMMA
12288 || token->type == CPP_EQ
12289 || token->type == CPP_ELLIPSIS
12290 || token->type == CPP_GREATER)
12293 if (parenthesized_p)
12294 *parenthesized_p = false;
12296 /* Otherwise, there should be a declarator. */
12299 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12300 parser->default_arg_ok_p = false;
12302 /* After seeing a decl-specifier-seq, if the next token is not a
12303 "(", there is no possibility that the code is a valid
12304 expression. Therefore, if parsing tentatively, we commit at
12306 if (!parser->in_template_argument_list_p
12307 /* In an expression context, having seen:
12311 we cannot be sure whether we are looking at a
12312 function-type (taking a "char" as a parameter) or a cast
12313 of some object of type "char" to "int". */
12314 && !parser->in_type_id_in_expr_p
12315 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12316 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12317 cp_parser_commit_to_tentative_parse (parser);
12318 /* Parse the declarator. */
12319 declarator = cp_parser_declarator (parser,
12320 CP_PARSER_DECLARATOR_EITHER,
12321 /*ctor_dtor_or_conv_p=*/NULL,
12323 /*member_p=*/false);
12324 parser->default_arg_ok_p = saved_default_arg_ok_p;
12325 /* After the declarator, allow more attributes. */
12326 decl_specifiers.attributes
12327 = chainon (decl_specifiers.attributes,
12328 cp_parser_attributes_opt (parser));
12331 /* The restriction on defining new types applies only to the type
12332 of the parameter, not to the default argument. */
12333 parser->type_definition_forbidden_message = saved_message;
12335 /* If the next token is `=', then process a default argument. */
12336 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12338 bool saved_greater_than_is_operator_p;
12339 /* Consume the `='. */
12340 cp_lexer_consume_token (parser->lexer);
12342 /* If we are defining a class, then the tokens that make up the
12343 default argument must be saved and processed later. */
12344 if (!template_parm_p && at_class_scope_p ()
12345 && TYPE_BEING_DEFINED (current_class_type))
12347 unsigned depth = 0;
12348 cp_token *first_token;
12351 /* Add tokens until we have processed the entire default
12352 argument. We add the range [first_token, token). */
12353 first_token = cp_lexer_peek_token (parser->lexer);
12358 /* Peek at the next token. */
12359 token = cp_lexer_peek_token (parser->lexer);
12360 /* What we do depends on what token we have. */
12361 switch (token->type)
12363 /* In valid code, a default argument must be
12364 immediately followed by a `,' `)', or `...'. */
12366 case CPP_CLOSE_PAREN:
12368 /* If we run into a non-nested `;', `}', or `]',
12369 then the code is invalid -- but the default
12370 argument is certainly over. */
12371 case CPP_SEMICOLON:
12372 case CPP_CLOSE_BRACE:
12373 case CPP_CLOSE_SQUARE:
12376 /* Update DEPTH, if necessary. */
12377 else if (token->type == CPP_CLOSE_PAREN
12378 || token->type == CPP_CLOSE_BRACE
12379 || token->type == CPP_CLOSE_SQUARE)
12383 case CPP_OPEN_PAREN:
12384 case CPP_OPEN_SQUARE:
12385 case CPP_OPEN_BRACE:
12390 /* If we see a non-nested `>', and `>' is not an
12391 operator, then it marks the end of the default
12393 if (!depth && !greater_than_is_operator_p)
12397 /* If we run out of tokens, issue an error message. */
12399 case CPP_PRAGMA_EOL:
12400 error ("file ends in default argument");
12406 /* In these cases, we should look for template-ids.
12407 For example, if the default argument is
12408 `X<int, double>()', we need to do name lookup to
12409 figure out whether or not `X' is a template; if
12410 so, the `,' does not end the default argument.
12412 That is not yet done. */
12419 /* If we've reached the end, stop. */
12423 /* Add the token to the token block. */
12424 token = cp_lexer_consume_token (parser->lexer);
12427 /* Create a DEFAULT_ARG to represented the unparsed default
12429 default_argument = make_node (DEFAULT_ARG);
12430 DEFARG_TOKENS (default_argument)
12431 = cp_token_cache_new (first_token, token);
12432 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12434 /* Outside of a class definition, we can just parse the
12435 assignment-expression. */
12438 bool saved_local_variables_forbidden_p;
12440 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12442 saved_greater_than_is_operator_p
12443 = parser->greater_than_is_operator_p;
12444 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12445 /* Local variable names (and the `this' keyword) may not
12446 appear in a default argument. */
12447 saved_local_variables_forbidden_p
12448 = parser->local_variables_forbidden_p;
12449 parser->local_variables_forbidden_p = true;
12450 /* The default argument expression may cause implicitly
12451 defined member functions to be synthesized, which will
12452 result in garbage collection. We must treat this
12453 situation as if we were within the body of function so as
12454 to avoid collecting live data on the stack. */
12456 /* Parse the assignment-expression. */
12457 if (template_parm_p)
12458 push_deferring_access_checks (dk_no_deferred);
12460 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12461 if (template_parm_p)
12462 pop_deferring_access_checks ();
12463 /* Restore saved state. */
12465 parser->greater_than_is_operator_p
12466 = saved_greater_than_is_operator_p;
12467 parser->local_variables_forbidden_p
12468 = saved_local_variables_forbidden_p;
12470 if (!parser->default_arg_ok_p)
12472 if (!flag_pedantic_errors)
12473 warning (0, "deprecated use of default argument for parameter of non-function");
12476 error ("default arguments are only permitted for function parameters");
12477 default_argument = NULL_TREE;
12482 default_argument = NULL_TREE;
12484 return make_parameter_declarator (&decl_specifiers,
12489 /* Parse a function-body.
12492 compound_statement */
12495 cp_parser_function_body (cp_parser *parser)
12497 cp_parser_compound_statement (parser, NULL, false);
12500 /* Parse a ctor-initializer-opt followed by a function-body. Return
12501 true if a ctor-initializer was present. */
12504 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12507 bool ctor_initializer_p;
12509 /* Begin the function body. */
12510 body = begin_function_body ();
12511 /* Parse the optional ctor-initializer. */
12512 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12513 /* Parse the function-body. */
12514 cp_parser_function_body (parser);
12515 /* Finish the function body. */
12516 finish_function_body (body);
12518 return ctor_initializer_p;
12521 /* Parse an initializer.
12524 = initializer-clause
12525 ( expression-list )
12527 Returns an expression representing the initializer. If no
12528 initializer is present, NULL_TREE is returned.
12530 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12531 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12532 set to FALSE if there is no initializer present. If there is an
12533 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12534 is set to true; otherwise it is set to false. */
12537 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12538 bool* non_constant_p)
12543 /* Peek at the next token. */
12544 token = cp_lexer_peek_token (parser->lexer);
12546 /* Let our caller know whether or not this initializer was
12548 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12549 /* Assume that the initializer is constant. */
12550 *non_constant_p = false;
12552 if (token->type == CPP_EQ)
12554 /* Consume the `='. */
12555 cp_lexer_consume_token (parser->lexer);
12556 /* Parse the initializer-clause. */
12557 init = cp_parser_initializer_clause (parser, non_constant_p);
12559 else if (token->type == CPP_OPEN_PAREN)
12560 init = cp_parser_parenthesized_expression_list (parser, false,
12565 /* Anything else is an error. */
12566 cp_parser_error (parser, "expected initializer");
12567 init = error_mark_node;
12573 /* Parse an initializer-clause.
12575 initializer-clause:
12576 assignment-expression
12577 { initializer-list , [opt] }
12580 Returns an expression representing the initializer.
12582 If the `assignment-expression' production is used the value
12583 returned is simply a representation for the expression.
12585 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12586 the elements of the initializer-list (or NULL, if the last
12587 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12588 NULL_TREE. There is no way to detect whether or not the optional
12589 trailing `,' was provided. NON_CONSTANT_P is as for
12590 cp_parser_initializer. */
12593 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12597 /* Assume the expression is constant. */
12598 *non_constant_p = false;
12600 /* If it is not a `{', then we are looking at an
12601 assignment-expression. */
12602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12605 = cp_parser_constant_expression (parser,
12606 /*allow_non_constant_p=*/true,
12608 if (!*non_constant_p)
12609 initializer = fold_non_dependent_expr (initializer);
12613 /* Consume the `{' token. */
12614 cp_lexer_consume_token (parser->lexer);
12615 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12616 initializer = make_node (CONSTRUCTOR);
12617 /* If it's not a `}', then there is a non-trivial initializer. */
12618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12620 /* Parse the initializer list. */
12621 CONSTRUCTOR_ELTS (initializer)
12622 = cp_parser_initializer_list (parser, non_constant_p);
12623 /* A trailing `,' token is allowed. */
12624 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12625 cp_lexer_consume_token (parser->lexer);
12627 /* Now, there should be a trailing `}'. */
12628 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12631 return initializer;
12634 /* Parse an initializer-list.
12638 initializer-list , initializer-clause
12643 identifier : initializer-clause
12644 initializer-list, identifier : initializer-clause
12646 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12647 for the initializer. If the INDEX of the elt is non-NULL, it is the
12648 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12649 as for cp_parser_initializer. */
12651 static VEC(constructor_elt,gc) *
12652 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12654 VEC(constructor_elt,gc) *v = NULL;
12656 /* Assume all of the expressions are constant. */
12657 *non_constant_p = false;
12659 /* Parse the rest of the list. */
12665 bool clause_non_constant_p;
12667 /* If the next token is an identifier and the following one is a
12668 colon, we are looking at the GNU designated-initializer
12670 if (cp_parser_allow_gnu_extensions_p (parser)
12671 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12672 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12674 /* Consume the identifier. */
12675 identifier = cp_lexer_consume_token (parser->lexer)->value;
12676 /* Consume the `:'. */
12677 cp_lexer_consume_token (parser->lexer);
12680 identifier = NULL_TREE;
12682 /* Parse the initializer. */
12683 initializer = cp_parser_initializer_clause (parser,
12684 &clause_non_constant_p);
12685 /* If any clause is non-constant, so is the entire initializer. */
12686 if (clause_non_constant_p)
12687 *non_constant_p = true;
12689 /* Add it to the vector. */
12690 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12692 /* If the next token is not a comma, we have reached the end of
12694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12697 /* Peek at the next token. */
12698 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12699 /* If the next token is a `}', then we're still done. An
12700 initializer-clause can have a trailing `,' after the
12701 initializer-list and before the closing `}'. */
12702 if (token->type == CPP_CLOSE_BRACE)
12705 /* Consume the `,' token. */
12706 cp_lexer_consume_token (parser->lexer);
12712 /* Classes [gram.class] */
12714 /* Parse a class-name.
12720 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12721 to indicate that names looked up in dependent types should be
12722 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12723 keyword has been used to indicate that the name that appears next
12724 is a template. TAG_TYPE indicates the explicit tag given before
12725 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12726 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12727 is the class being defined in a class-head.
12729 Returns the TYPE_DECL representing the class. */
12732 cp_parser_class_name (cp_parser *parser,
12733 bool typename_keyword_p,
12734 bool template_keyword_p,
12735 enum tag_types tag_type,
12736 bool check_dependency_p,
12738 bool is_declaration)
12745 /* All class-names start with an identifier. */
12746 token = cp_lexer_peek_token (parser->lexer);
12747 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12749 cp_parser_error (parser, "expected class-name");
12750 return error_mark_node;
12753 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12754 to a template-id, so we save it here. */
12755 scope = parser->scope;
12756 if (scope == error_mark_node)
12757 return error_mark_node;
12759 /* Any name names a type if we're following the `typename' keyword
12760 in a qualified name where the enclosing scope is type-dependent. */
12761 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12762 && dependent_type_p (scope));
12763 /* Handle the common case (an identifier, but not a template-id)
12765 if (token->type == CPP_NAME
12766 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12768 cp_token *identifier_token;
12772 /* Look for the identifier. */
12773 identifier_token = cp_lexer_peek_token (parser->lexer);
12774 ambiguous_p = identifier_token->ambiguous_p;
12775 identifier = cp_parser_identifier (parser);
12776 /* If the next token isn't an identifier, we are certainly not
12777 looking at a class-name. */
12778 if (identifier == error_mark_node)
12779 decl = error_mark_node;
12780 /* If we know this is a type-name, there's no need to look it
12782 else if (typename_p)
12786 tree ambiguous_decls;
12787 /* If we already know that this lookup is ambiguous, then
12788 we've already issued an error message; there's no reason
12792 cp_parser_simulate_error (parser);
12793 return error_mark_node;
12795 /* If the next token is a `::', then the name must be a type
12798 [basic.lookup.qual]
12800 During the lookup for a name preceding the :: scope
12801 resolution operator, object, function, and enumerator
12802 names are ignored. */
12803 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12804 tag_type = typename_type;
12805 /* Look up the name. */
12806 decl = cp_parser_lookup_name (parser, identifier,
12808 /*is_template=*/false,
12809 /*is_namespace=*/false,
12810 check_dependency_p,
12812 if (ambiguous_decls)
12814 error ("reference to %qD is ambiguous", identifier);
12815 print_candidates (ambiguous_decls);
12816 if (cp_parser_parsing_tentatively (parser))
12818 identifier_token->ambiguous_p = true;
12819 cp_parser_simulate_error (parser);
12821 return error_mark_node;
12827 /* Try a template-id. */
12828 decl = cp_parser_template_id (parser, template_keyword_p,
12829 check_dependency_p,
12831 if (decl == error_mark_node)
12832 return error_mark_node;
12835 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12837 /* If this is a typename, create a TYPENAME_TYPE. */
12838 if (typename_p && decl != error_mark_node)
12840 decl = make_typename_type (scope, decl, typename_type,
12841 /*complain=*/tf_error);
12842 if (decl != error_mark_node)
12843 decl = TYPE_NAME (decl);
12846 /* Check to see that it is really the name of a class. */
12847 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12848 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12849 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12850 /* Situations like this:
12852 template <typename T> struct A {
12853 typename T::template X<int>::I i;
12856 are problematic. Is `T::template X<int>' a class-name? The
12857 standard does not seem to be definitive, but there is no other
12858 valid interpretation of the following `::'. Therefore, those
12859 names are considered class-names. */
12861 decl = make_typename_type (scope, decl, tag_type, tf_error);
12862 if (decl != error_mark_node)
12863 decl = TYPE_NAME (decl);
12865 else if (TREE_CODE (decl) != TYPE_DECL
12866 || TREE_TYPE (decl) == error_mark_node
12867 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12868 decl = error_mark_node;
12870 if (decl == error_mark_node)
12871 cp_parser_error (parser, "expected class-name");
12876 /* Parse a class-specifier.
12879 class-head { member-specification [opt] }
12881 Returns the TREE_TYPE representing the class. */
12884 cp_parser_class_specifier (cp_parser* parser)
12888 tree attributes = NULL_TREE;
12889 int has_trailing_semicolon;
12890 bool nested_name_specifier_p;
12891 unsigned saved_num_template_parameter_lists;
12892 tree old_scope = NULL_TREE;
12893 tree scope = NULL_TREE;
12895 push_deferring_access_checks (dk_no_deferred);
12897 /* Parse the class-head. */
12898 type = cp_parser_class_head (parser,
12899 &nested_name_specifier_p,
12901 /* If the class-head was a semantic disaster, skip the entire body
12905 cp_parser_skip_to_end_of_block_or_statement (parser);
12906 pop_deferring_access_checks ();
12907 return error_mark_node;
12910 /* Look for the `{'. */
12911 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12913 pop_deferring_access_checks ();
12914 return error_mark_node;
12917 /* Issue an error message if type-definitions are forbidden here. */
12918 cp_parser_check_type_definition (parser);
12919 /* Remember that we are defining one more class. */
12920 ++parser->num_classes_being_defined;
12921 /* Inside the class, surrounding template-parameter-lists do not
12923 saved_num_template_parameter_lists
12924 = parser->num_template_parameter_lists;
12925 parser->num_template_parameter_lists = 0;
12927 /* Start the class. */
12928 if (nested_name_specifier_p)
12930 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12931 old_scope = push_inner_scope (scope);
12933 type = begin_class_definition (type);
12935 if (type == error_mark_node)
12936 /* If the type is erroneous, skip the entire body of the class. */
12937 cp_parser_skip_to_closing_brace (parser);
12939 /* Parse the member-specification. */
12940 cp_parser_member_specification_opt (parser);
12942 /* Look for the trailing `}'. */
12943 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12944 /* We get better error messages by noticing a common problem: a
12945 missing trailing `;'. */
12946 token = cp_lexer_peek_token (parser->lexer);
12947 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12948 /* Look for trailing attributes to apply to this class. */
12949 if (cp_parser_allow_gnu_extensions_p (parser))
12951 tree sub_attr = cp_parser_attributes_opt (parser);
12952 attributes = chainon (attributes, sub_attr);
12954 if (type != error_mark_node)
12955 type = finish_struct (type, attributes);
12956 if (nested_name_specifier_p)
12957 pop_inner_scope (old_scope, scope);
12958 /* If this class is not itself within the scope of another class,
12959 then we need to parse the bodies of all of the queued function
12960 definitions. Note that the queued functions defined in a class
12961 are not always processed immediately following the
12962 class-specifier for that class. Consider:
12965 struct B { void f() { sizeof (A); } };
12968 If `f' were processed before the processing of `A' were
12969 completed, there would be no way to compute the size of `A'.
12970 Note that the nesting we are interested in here is lexical --
12971 not the semantic nesting given by TYPE_CONTEXT. In particular,
12974 struct A { struct B; };
12975 struct A::B { void f() { } };
12977 there is no need to delay the parsing of `A::B::f'. */
12978 if (--parser->num_classes_being_defined == 0)
12982 tree class_type = NULL_TREE;
12983 tree pushed_scope = NULL_TREE;
12985 /* In a first pass, parse default arguments to the functions.
12986 Then, in a second pass, parse the bodies of the functions.
12987 This two-phased approach handles cases like:
12995 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12996 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12997 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12998 TREE_PURPOSE (parser->unparsed_functions_queues)
12999 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13001 fn = TREE_VALUE (queue_entry);
13002 /* If there are default arguments that have not yet been processed,
13003 take care of them now. */
13004 if (class_type != TREE_PURPOSE (queue_entry))
13007 pop_scope (pushed_scope);
13008 class_type = TREE_PURPOSE (queue_entry);
13009 pushed_scope = push_scope (class_type);
13011 /* Make sure that any template parameters are in scope. */
13012 maybe_begin_member_template_processing (fn);
13013 /* Parse the default argument expressions. */
13014 cp_parser_late_parsing_default_args (parser, fn);
13015 /* Remove any template parameters from the symbol table. */
13016 maybe_end_member_template_processing ();
13019 pop_scope (pushed_scope);
13020 /* Now parse the body of the functions. */
13021 for (TREE_VALUE (parser->unparsed_functions_queues)
13022 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13023 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13024 TREE_VALUE (parser->unparsed_functions_queues)
13025 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13027 /* Figure out which function we need to process. */
13028 fn = TREE_VALUE (queue_entry);
13029 /* Parse the function. */
13030 cp_parser_late_parsing_for_member (parser, fn);
13034 /* Put back any saved access checks. */
13035 pop_deferring_access_checks ();
13037 /* Restore the count of active template-parameter-lists. */
13038 parser->num_template_parameter_lists
13039 = saved_num_template_parameter_lists;
13044 /* Parse a class-head.
13047 class-key identifier [opt] base-clause [opt]
13048 class-key nested-name-specifier identifier base-clause [opt]
13049 class-key nested-name-specifier [opt] template-id
13053 class-key attributes identifier [opt] base-clause [opt]
13054 class-key attributes nested-name-specifier identifier base-clause [opt]
13055 class-key attributes nested-name-specifier [opt] template-id
13058 Returns the TYPE of the indicated class. Sets
13059 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13060 involving a nested-name-specifier was used, and FALSE otherwise.
13062 Returns error_mark_node if this is not a class-head.
13064 Returns NULL_TREE if the class-head is syntactically valid, but
13065 semantically invalid in a way that means we should skip the entire
13066 body of the class. */
13069 cp_parser_class_head (cp_parser* parser,
13070 bool* nested_name_specifier_p,
13071 tree *attributes_p)
13073 tree nested_name_specifier;
13074 enum tag_types class_key;
13075 tree id = NULL_TREE;
13076 tree type = NULL_TREE;
13078 bool template_id_p = false;
13079 bool qualified_p = false;
13080 bool invalid_nested_name_p = false;
13081 bool invalid_explicit_specialization_p = false;
13082 tree pushed_scope = NULL_TREE;
13083 unsigned num_templates;
13086 /* Assume no nested-name-specifier will be present. */
13087 *nested_name_specifier_p = false;
13088 /* Assume no template parameter lists will be used in defining the
13092 /* Look for the class-key. */
13093 class_key = cp_parser_class_key (parser);
13094 if (class_key == none_type)
13095 return error_mark_node;
13097 /* Parse the attributes. */
13098 attributes = cp_parser_attributes_opt (parser);
13100 /* If the next token is `::', that is invalid -- but sometimes
13101 people do try to write:
13105 Handle this gracefully by accepting the extra qualifier, and then
13106 issuing an error about it later if this really is a
13107 class-head. If it turns out just to be an elaborated type
13108 specifier, remain silent. */
13109 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13110 qualified_p = true;
13112 push_deferring_access_checks (dk_no_check);
13114 /* Determine the name of the class. Begin by looking for an
13115 optional nested-name-specifier. */
13116 nested_name_specifier
13117 = cp_parser_nested_name_specifier_opt (parser,
13118 /*typename_keyword_p=*/false,
13119 /*check_dependency_p=*/false,
13121 /*is_declaration=*/false);
13122 /* If there was a nested-name-specifier, then there *must* be an
13124 if (nested_name_specifier)
13126 /* Although the grammar says `identifier', it really means
13127 `class-name' or `template-name'. You are only allowed to
13128 define a class that has already been declared with this
13131 The proposed resolution for Core Issue 180 says that whever
13132 you see `class T::X' you should treat `X' as a type-name.
13134 It is OK to define an inaccessible class; for example:
13136 class A { class B; };
13139 We do not know if we will see a class-name, or a
13140 template-name. We look for a class-name first, in case the
13141 class-name is a template-id; if we looked for the
13142 template-name first we would stop after the template-name. */
13143 cp_parser_parse_tentatively (parser);
13144 type = cp_parser_class_name (parser,
13145 /*typename_keyword_p=*/false,
13146 /*template_keyword_p=*/false,
13148 /*check_dependency_p=*/false,
13149 /*class_head_p=*/true,
13150 /*is_declaration=*/false);
13151 /* If that didn't work, ignore the nested-name-specifier. */
13152 if (!cp_parser_parse_definitely (parser))
13154 invalid_nested_name_p = true;
13155 id = cp_parser_identifier (parser);
13156 if (id == error_mark_node)
13159 /* If we could not find a corresponding TYPE, treat this
13160 declaration like an unqualified declaration. */
13161 if (type == error_mark_node)
13162 nested_name_specifier = NULL_TREE;
13163 /* Otherwise, count the number of templates used in TYPE and its
13164 containing scopes. */
13169 for (scope = TREE_TYPE (type);
13170 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13171 scope = (TYPE_P (scope)
13172 ? TYPE_CONTEXT (scope)
13173 : DECL_CONTEXT (scope)))
13175 && CLASS_TYPE_P (scope)
13176 && CLASSTYPE_TEMPLATE_INFO (scope)
13177 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13178 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13182 /* Otherwise, the identifier is optional. */
13185 /* We don't know whether what comes next is a template-id,
13186 an identifier, or nothing at all. */
13187 cp_parser_parse_tentatively (parser);
13188 /* Check for a template-id. */
13189 id = cp_parser_template_id (parser,
13190 /*template_keyword_p=*/false,
13191 /*check_dependency_p=*/true,
13192 /*is_declaration=*/true);
13193 /* If that didn't work, it could still be an identifier. */
13194 if (!cp_parser_parse_definitely (parser))
13196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13197 id = cp_parser_identifier (parser);
13203 template_id_p = true;
13208 pop_deferring_access_checks ();
13211 cp_parser_check_for_invalid_template_id (parser, id);
13213 /* If it's not a `:' or a `{' then we can't really be looking at a
13214 class-head, since a class-head only appears as part of a
13215 class-specifier. We have to detect this situation before calling
13216 xref_tag, since that has irreversible side-effects. */
13217 if (!cp_parser_next_token_starts_class_definition_p (parser))
13219 cp_parser_error (parser, "expected %<{%> or %<:%>");
13220 return error_mark_node;
13223 /* At this point, we're going ahead with the class-specifier, even
13224 if some other problem occurs. */
13225 cp_parser_commit_to_tentative_parse (parser);
13226 /* Issue the error about the overly-qualified name now. */
13228 cp_parser_error (parser,
13229 "global qualification of class name is invalid");
13230 else if (invalid_nested_name_p)
13231 cp_parser_error (parser,
13232 "qualified name does not name a class");
13233 else if (nested_name_specifier)
13237 /* Reject typedef-names in class heads. */
13238 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13240 error ("invalid class name in declaration of %qD", type);
13245 /* Figure out in what scope the declaration is being placed. */
13246 scope = current_scope ();
13247 /* If that scope does not contain the scope in which the
13248 class was originally declared, the program is invalid. */
13249 if (scope && !is_ancestor (scope, nested_name_specifier))
13251 error ("declaration of %qD in %qD which does not enclose %qD",
13252 type, scope, nested_name_specifier);
13258 A declarator-id shall not be qualified exception of the
13259 definition of a ... nested class outside of its class
13260 ... [or] a the definition or explicit instantiation of a
13261 class member of a namespace outside of its namespace. */
13262 if (scope == nested_name_specifier)
13264 pedwarn ("extra qualification ignored");
13265 nested_name_specifier = NULL_TREE;
13269 /* An explicit-specialization must be preceded by "template <>". If
13270 it is not, try to recover gracefully. */
13271 if (at_namespace_scope_p ()
13272 && parser->num_template_parameter_lists == 0
13275 error ("an explicit specialization must be preceded by %<template <>%>");
13276 invalid_explicit_specialization_p = true;
13277 /* Take the same action that would have been taken by
13278 cp_parser_explicit_specialization. */
13279 ++parser->num_template_parameter_lists;
13280 begin_specialization ();
13282 /* There must be no "return" statements between this point and the
13283 end of this function; set "type "to the correct return value and
13284 use "goto done;" to return. */
13285 /* Make sure that the right number of template parameters were
13287 if (!cp_parser_check_template_parameters (parser, num_templates))
13289 /* If something went wrong, there is no point in even trying to
13290 process the class-definition. */
13295 /* Look up the type. */
13298 type = TREE_TYPE (id);
13299 maybe_process_partial_specialization (type);
13300 if (nested_name_specifier)
13301 pushed_scope = push_scope (nested_name_specifier);
13303 else if (nested_name_specifier)
13309 template <typename T> struct S { struct T };
13310 template <typename T> struct S<T>::T { };
13312 we will get a TYPENAME_TYPE when processing the definition of
13313 `S::T'. We need to resolve it to the actual type before we
13314 try to define it. */
13315 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13317 class_type = resolve_typename_type (TREE_TYPE (type),
13318 /*only_current_p=*/false);
13319 if (class_type != error_mark_node)
13320 type = TYPE_NAME (class_type);
13323 cp_parser_error (parser, "could not resolve typename type");
13324 type = error_mark_node;
13328 maybe_process_partial_specialization (TREE_TYPE (type));
13329 class_type = current_class_type;
13330 /* Enter the scope indicated by the nested-name-specifier. */
13331 pushed_scope = push_scope (nested_name_specifier);
13332 /* Get the canonical version of this type. */
13333 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13334 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13335 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13337 type = push_template_decl (type);
13338 if (type == error_mark_node)
13345 type = TREE_TYPE (type);
13346 *nested_name_specifier_p = true;
13348 else /* The name is not a nested name. */
13350 /* If the class was unnamed, create a dummy name. */
13352 id = make_anon_name ();
13353 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13354 parser->num_template_parameter_lists);
13357 /* Indicate whether this class was declared as a `class' or as a
13359 if (TREE_CODE (type) == RECORD_TYPE)
13360 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13361 cp_parser_check_class_key (class_key, type);
13363 /* If this type was already complete, and we see another definition,
13364 that's an error. */
13365 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13367 error ("redefinition of %q#T", type);
13368 error ("previous definition of %q+#T", type);
13373 /* We will have entered the scope containing the class; the names of
13374 base classes should be looked up in that context. For example:
13376 struct A { struct B {}; struct C; };
13377 struct A::C : B {};
13382 /* Get the list of base-classes, if there is one. */
13383 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13384 bases = cp_parser_base_clause (parser);
13386 /* Process the base classes. */
13387 xref_basetypes (type, bases);
13390 /* Leave the scope given by the nested-name-specifier. We will
13391 enter the class scope itself while processing the members. */
13393 pop_scope (pushed_scope);
13395 if (invalid_explicit_specialization_p)
13397 end_specialization ();
13398 --parser->num_template_parameter_lists;
13400 *attributes_p = attributes;
13404 /* Parse a class-key.
13411 Returns the kind of class-key specified, or none_type to indicate
13414 static enum tag_types
13415 cp_parser_class_key (cp_parser* parser)
13418 enum tag_types tag_type;
13420 /* Look for the class-key. */
13421 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13425 /* Check to see if the TOKEN is a class-key. */
13426 tag_type = cp_parser_token_is_class_key (token);
13428 cp_parser_error (parser, "expected class-key");
13432 /* Parse an (optional) member-specification.
13434 member-specification:
13435 member-declaration member-specification [opt]
13436 access-specifier : member-specification [opt] */
13439 cp_parser_member_specification_opt (cp_parser* parser)
13446 /* Peek at the next token. */
13447 token = cp_lexer_peek_token (parser->lexer);
13448 /* If it's a `}', or EOF then we've seen all the members. */
13449 if (token->type == CPP_CLOSE_BRACE
13450 || token->type == CPP_EOF
13451 || token->type == CPP_PRAGMA_EOL)
13454 /* See if this token is a keyword. */
13455 keyword = token->keyword;
13459 case RID_PROTECTED:
13461 /* Consume the access-specifier. */
13462 cp_lexer_consume_token (parser->lexer);
13463 /* Remember which access-specifier is active. */
13464 current_access_specifier = token->value;
13465 /* Look for the `:'. */
13466 cp_parser_require (parser, CPP_COLON, "`:'");
13470 /* Accept #pragmas at class scope. */
13471 if (token->type == CPP_PRAGMA)
13473 cp_parser_pragma (parser, pragma_external);
13477 /* Otherwise, the next construction must be a
13478 member-declaration. */
13479 cp_parser_member_declaration (parser);
13484 /* Parse a member-declaration.
13486 member-declaration:
13487 decl-specifier-seq [opt] member-declarator-list [opt] ;
13488 function-definition ; [opt]
13489 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13491 template-declaration
13493 member-declarator-list:
13495 member-declarator-list , member-declarator
13498 declarator pure-specifier [opt]
13499 declarator constant-initializer [opt]
13500 identifier [opt] : constant-expression
13504 member-declaration:
13505 __extension__ member-declaration
13508 declarator attributes [opt] pure-specifier [opt]
13509 declarator attributes [opt] constant-initializer [opt]
13510 identifier [opt] attributes [opt] : constant-expression */
13513 cp_parser_member_declaration (cp_parser* parser)
13515 cp_decl_specifier_seq decl_specifiers;
13516 tree prefix_attributes;
13518 int declares_class_or_enum;
13521 int saved_pedantic;
13523 /* Check for the `__extension__' keyword. */
13524 if (cp_parser_extension_opt (parser, &saved_pedantic))
13527 cp_parser_member_declaration (parser);
13528 /* Restore the old value of the PEDANTIC flag. */
13529 pedantic = saved_pedantic;
13534 /* Check for a template-declaration. */
13535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13537 /* An explicit specialization here is an error condition, and we
13538 expect the specialization handler to detect and report this. */
13539 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13540 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13541 cp_parser_explicit_specialization (parser);
13543 cp_parser_template_declaration (parser, /*member_p=*/true);
13548 /* Check for a using-declaration. */
13549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13551 /* Parse the using-declaration. */
13552 cp_parser_using_declaration (parser);
13557 /* Check for @defs. */
13558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13561 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13562 ivar = ivar_chains;
13566 ivar = TREE_CHAIN (member);
13567 TREE_CHAIN (member) = NULL_TREE;
13568 finish_member_declaration (member);
13573 /* Parse the decl-specifier-seq. */
13574 cp_parser_decl_specifier_seq (parser,
13575 CP_PARSER_FLAGS_OPTIONAL,
13577 &declares_class_or_enum);
13578 prefix_attributes = decl_specifiers.attributes;
13579 decl_specifiers.attributes = NULL_TREE;
13580 /* Check for an invalid type-name. */
13581 if (!decl_specifiers.type
13582 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13584 /* If there is no declarator, then the decl-specifier-seq should
13586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13588 /* If there was no decl-specifier-seq, and the next token is a
13589 `;', then we have something like:
13595 Each member-declaration shall declare at least one member
13596 name of the class. */
13597 if (!decl_specifiers.any_specifiers_p)
13599 cp_token *token = cp_lexer_peek_token (parser->lexer);
13600 if (pedantic && !token->in_system_header)
13601 pedwarn ("%Hextra %<;%>", &token->location);
13607 /* See if this declaration is a friend. */
13608 friend_p = cp_parser_friend_p (&decl_specifiers);
13609 /* If there were decl-specifiers, check to see if there was
13610 a class-declaration. */
13611 type = check_tag_decl (&decl_specifiers);
13612 /* Nested classes have already been added to the class, but
13613 a `friend' needs to be explicitly registered. */
13616 /* If the `friend' keyword was present, the friend must
13617 be introduced with a class-key. */
13618 if (!declares_class_or_enum)
13619 error ("a class-key must be used when declaring a friend");
13622 template <typename T> struct A {
13623 friend struct A<T>::B;
13626 A<T>::B will be represented by a TYPENAME_TYPE, and
13627 therefore not recognized by check_tag_decl. */
13629 && decl_specifiers.type
13630 && TYPE_P (decl_specifiers.type))
13631 type = decl_specifiers.type;
13632 if (!type || !TYPE_P (type))
13633 error ("friend declaration does not name a class or "
13636 make_friend_class (current_class_type, type,
13637 /*complain=*/true);
13639 /* If there is no TYPE, an error message will already have
13641 else if (!type || type == error_mark_node)
13643 /* An anonymous aggregate has to be handled specially; such
13644 a declaration really declares a data member (with a
13645 particular type), as opposed to a nested class. */
13646 else if (ANON_AGGR_TYPE_P (type))
13648 /* Remove constructors and such from TYPE, now that we
13649 know it is an anonymous aggregate. */
13650 fixup_anonymous_aggr (type);
13651 /* And make the corresponding data member. */
13652 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13653 /* Add it to the class. */
13654 finish_member_declaration (decl);
13657 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13662 /* See if these declarations will be friends. */
13663 friend_p = cp_parser_friend_p (&decl_specifiers);
13665 /* Keep going until we hit the `;' at the end of the
13667 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13669 tree attributes = NULL_TREE;
13670 tree first_attribute;
13672 /* Peek at the next token. */
13673 token = cp_lexer_peek_token (parser->lexer);
13675 /* Check for a bitfield declaration. */
13676 if (token->type == CPP_COLON
13677 || (token->type == CPP_NAME
13678 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13684 /* Get the name of the bitfield. Note that we cannot just
13685 check TOKEN here because it may have been invalidated by
13686 the call to cp_lexer_peek_nth_token above. */
13687 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13688 identifier = cp_parser_identifier (parser);
13690 identifier = NULL_TREE;
13692 /* Consume the `:' token. */
13693 cp_lexer_consume_token (parser->lexer);
13694 /* Get the width of the bitfield. */
13696 = cp_parser_constant_expression (parser,
13697 /*allow_non_constant=*/false,
13700 /* Look for attributes that apply to the bitfield. */
13701 attributes = cp_parser_attributes_opt (parser);
13702 /* Remember which attributes are prefix attributes and
13704 first_attribute = attributes;
13705 /* Combine the attributes. */
13706 attributes = chainon (prefix_attributes, attributes);
13708 /* Create the bitfield declaration. */
13709 decl = grokbitfield (identifier
13710 ? make_id_declarator (NULL_TREE,
13716 /* Apply the attributes. */
13717 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13721 cp_declarator *declarator;
13723 tree asm_specification;
13724 int ctor_dtor_or_conv_p;
13726 /* Parse the declarator. */
13728 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13729 &ctor_dtor_or_conv_p,
13730 /*parenthesized_p=*/NULL,
13731 /*member_p=*/true);
13733 /* If something went wrong parsing the declarator, make sure
13734 that we at least consume some tokens. */
13735 if (declarator == cp_error_declarator)
13737 /* Skip to the end of the statement. */
13738 cp_parser_skip_to_end_of_statement (parser);
13739 /* If the next token is not a semicolon, that is
13740 probably because we just skipped over the body of
13741 a function. So, we consume a semicolon if
13742 present, but do not issue an error message if it
13744 if (cp_lexer_next_token_is (parser->lexer,
13746 cp_lexer_consume_token (parser->lexer);
13750 if (declares_class_or_enum & 2)
13751 cp_parser_check_for_definition_in_return_type
13752 (declarator, decl_specifiers.type);
13754 /* Look for an asm-specification. */
13755 asm_specification = cp_parser_asm_specification_opt (parser);
13756 /* Look for attributes that apply to the declaration. */
13757 attributes = cp_parser_attributes_opt (parser);
13758 /* Remember which attributes are prefix attributes and
13760 first_attribute = attributes;
13761 /* Combine the attributes. */
13762 attributes = chainon (prefix_attributes, attributes);
13764 /* If it's an `=', then we have a constant-initializer or a
13765 pure-specifier. It is not correct to parse the
13766 initializer before registering the member declaration
13767 since the member declaration should be in scope while
13768 its initializer is processed. However, the rest of the
13769 front end does not yet provide an interface that allows
13770 us to handle this correctly. */
13771 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13775 A pure-specifier shall be used only in the declaration of
13776 a virtual function.
13778 A member-declarator can contain a constant-initializer
13779 only if it declares a static member of integral or
13782 Therefore, if the DECLARATOR is for a function, we look
13783 for a pure-specifier; otherwise, we look for a
13784 constant-initializer. When we call `grokfield', it will
13785 perform more stringent semantics checks. */
13786 if (declarator->kind == cdk_function
13787 && declarator->declarator->kind == cdk_id)
13788 initializer = cp_parser_pure_specifier (parser);
13790 /* Parse the initializer. */
13791 initializer = cp_parser_constant_initializer (parser);
13793 /* Otherwise, there is no initializer. */
13795 initializer = NULL_TREE;
13797 /* See if we are probably looking at a function
13798 definition. We are certainly not looking at a
13799 member-declarator. Calling `grokfield' has
13800 side-effects, so we must not do it unless we are sure
13801 that we are looking at a member-declarator. */
13802 if (cp_parser_token_starts_function_definition_p
13803 (cp_lexer_peek_token (parser->lexer)))
13805 /* The grammar does not allow a pure-specifier to be
13806 used when a member function is defined. (It is
13807 possible that this fact is an oversight in the
13808 standard, since a pure function may be defined
13809 outside of the class-specifier. */
13811 error ("pure-specifier on function-definition");
13812 decl = cp_parser_save_member_function_body (parser,
13816 /* If the member was not a friend, declare it here. */
13818 finish_member_declaration (decl);
13819 /* Peek at the next token. */
13820 token = cp_lexer_peek_token (parser->lexer);
13821 /* If the next token is a semicolon, consume it. */
13822 if (token->type == CPP_SEMICOLON)
13823 cp_lexer_consume_token (parser->lexer);
13827 /* Create the declaration. */
13828 decl = grokfield (declarator, &decl_specifiers,
13829 initializer, /*init_const_expr_p=*/true,
13834 /* Reset PREFIX_ATTRIBUTES. */
13835 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13836 attributes = TREE_CHAIN (attributes);
13838 TREE_CHAIN (attributes) = NULL_TREE;
13840 /* If there is any qualification still in effect, clear it
13841 now; we will be starting fresh with the next declarator. */
13842 parser->scope = NULL_TREE;
13843 parser->qualifying_scope = NULL_TREE;
13844 parser->object_scope = NULL_TREE;
13845 /* If it's a `,', then there are more declarators. */
13846 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13847 cp_lexer_consume_token (parser->lexer);
13848 /* If the next token isn't a `;', then we have a parse error. */
13849 else if (cp_lexer_next_token_is_not (parser->lexer,
13852 cp_parser_error (parser, "expected %<;%>");
13853 /* Skip tokens until we find a `;'. */
13854 cp_parser_skip_to_end_of_statement (parser);
13861 /* Add DECL to the list of members. */
13863 finish_member_declaration (decl);
13865 if (TREE_CODE (decl) == FUNCTION_DECL)
13866 cp_parser_save_default_args (parser, decl);
13871 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13874 /* Parse a pure-specifier.
13879 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13880 Otherwise, ERROR_MARK_NODE is returned. */
13883 cp_parser_pure_specifier (cp_parser* parser)
13887 /* Look for the `=' token. */
13888 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13889 return error_mark_node;
13890 /* Look for the `0' token. */
13891 token = cp_lexer_consume_token (parser->lexer);
13892 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13893 if (token->type == CPP_NUMBER && (token->flags & PURE_ZERO))
13894 return integer_zero_node;
13896 cp_parser_error (parser, "invalid pure specifier (only `= 0' is allowed)");
13897 cp_parser_skip_to_end_of_statement (parser);
13898 return error_mark_node;
13901 /* Parse a constant-initializer.
13903 constant-initializer:
13904 = constant-expression
13906 Returns a representation of the constant-expression. */
13909 cp_parser_constant_initializer (cp_parser* parser)
13911 /* Look for the `=' token. */
13912 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13913 return error_mark_node;
13915 /* It is invalid to write:
13917 struct S { static const int i = { 7 }; };
13920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13922 cp_parser_error (parser,
13923 "a brace-enclosed initializer is not allowed here");
13924 /* Consume the opening brace. */
13925 cp_lexer_consume_token (parser->lexer);
13926 /* Skip the initializer. */
13927 cp_parser_skip_to_closing_brace (parser);
13928 /* Look for the trailing `}'. */
13929 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13931 return error_mark_node;
13934 return cp_parser_constant_expression (parser,
13935 /*allow_non_constant=*/false,
13939 /* Derived classes [gram.class.derived] */
13941 /* Parse a base-clause.
13944 : base-specifier-list
13946 base-specifier-list:
13948 base-specifier-list , base-specifier
13950 Returns a TREE_LIST representing the base-classes, in the order in
13951 which they were declared. The representation of each node is as
13952 described by cp_parser_base_specifier.
13954 In the case that no bases are specified, this function will return
13955 NULL_TREE, not ERROR_MARK_NODE. */
13958 cp_parser_base_clause (cp_parser* parser)
13960 tree bases = NULL_TREE;
13962 /* Look for the `:' that begins the list. */
13963 cp_parser_require (parser, CPP_COLON, "`:'");
13965 /* Scan the base-specifier-list. */
13971 /* Look for the base-specifier. */
13972 base = cp_parser_base_specifier (parser);
13973 /* Add BASE to the front of the list. */
13974 if (base != error_mark_node)
13976 TREE_CHAIN (base) = bases;
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981 /* If it's not a comma, then the list is complete. */
13982 if (token->type != CPP_COMMA)
13984 /* Consume the `,'. */
13985 cp_lexer_consume_token (parser->lexer);
13988 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13989 base class had a qualified name. However, the next name that
13990 appears is certainly not qualified. */
13991 parser->scope = NULL_TREE;
13992 parser->qualifying_scope = NULL_TREE;
13993 parser->object_scope = NULL_TREE;
13995 return nreverse (bases);
13998 /* Parse a base-specifier.
14001 :: [opt] nested-name-specifier [opt] class-name
14002 virtual access-specifier [opt] :: [opt] nested-name-specifier
14004 access-specifier virtual [opt] :: [opt] nested-name-specifier
14007 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14008 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14009 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14010 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14013 cp_parser_base_specifier (cp_parser* parser)
14017 bool virtual_p = false;
14018 bool duplicate_virtual_error_issued_p = false;
14019 bool duplicate_access_error_issued_p = false;
14020 bool class_scope_p, template_p;
14021 tree access = access_default_node;
14024 /* Process the optional `virtual' and `access-specifier'. */
14027 /* Peek at the next token. */
14028 token = cp_lexer_peek_token (parser->lexer);
14029 /* Process `virtual'. */
14030 switch (token->keyword)
14033 /* If `virtual' appears more than once, issue an error. */
14034 if (virtual_p && !duplicate_virtual_error_issued_p)
14036 cp_parser_error (parser,
14037 "%<virtual%> specified more than once in base-specified");
14038 duplicate_virtual_error_issued_p = true;
14043 /* Consume the `virtual' token. */
14044 cp_lexer_consume_token (parser->lexer);
14049 case RID_PROTECTED:
14051 /* If more than one access specifier appears, issue an
14053 if (access != access_default_node
14054 && !duplicate_access_error_issued_p)
14056 cp_parser_error (parser,
14057 "more than one access specifier in base-specified");
14058 duplicate_access_error_issued_p = true;
14061 access = ridpointers[(int) token->keyword];
14063 /* Consume the access-specifier. */
14064 cp_lexer_consume_token (parser->lexer);
14073 /* It is not uncommon to see programs mechanically, erroneously, use
14074 the 'typename' keyword to denote (dependent) qualified types
14075 as base classes. */
14076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14078 if (!processing_template_decl)
14079 error ("keyword %<typename%> not allowed outside of templates");
14081 error ("keyword %<typename%> not allowed in this context "
14082 "(the base class is implicitly a type)");
14083 cp_lexer_consume_token (parser->lexer);
14086 /* Look for the optional `::' operator. */
14087 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14088 /* Look for the nested-name-specifier. The simplest way to
14093 The keyword `typename' is not permitted in a base-specifier or
14094 mem-initializer; in these contexts a qualified name that
14095 depends on a template-parameter is implicitly assumed to be a
14098 is to pretend that we have seen the `typename' keyword at this
14100 cp_parser_nested_name_specifier_opt (parser,
14101 /*typename_keyword_p=*/true,
14102 /*check_dependency_p=*/true,
14104 /*is_declaration=*/true);
14105 /* If the base class is given by a qualified name, assume that names
14106 we see are type names or templates, as appropriate. */
14107 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14108 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14110 /* Finally, look for the class-name. */
14111 type = cp_parser_class_name (parser,
14115 /*check_dependency_p=*/true,
14116 /*class_head_p=*/false,
14117 /*is_declaration=*/true);
14119 if (type == error_mark_node)
14120 return error_mark_node;
14122 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14125 /* Exception handling [gram.exception] */
14127 /* Parse an (optional) exception-specification.
14129 exception-specification:
14130 throw ( type-id-list [opt] )
14132 Returns a TREE_LIST representing the exception-specification. The
14133 TREE_VALUE of each node is a type. */
14136 cp_parser_exception_specification_opt (cp_parser* parser)
14141 /* Peek at the next token. */
14142 token = cp_lexer_peek_token (parser->lexer);
14143 /* If it's not `throw', then there's no exception-specification. */
14144 if (!cp_parser_is_keyword (token, RID_THROW))
14147 /* Consume the `throw'. */
14148 cp_lexer_consume_token (parser->lexer);
14150 /* Look for the `('. */
14151 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14153 /* Peek at the next token. */
14154 token = cp_lexer_peek_token (parser->lexer);
14155 /* If it's not a `)', then there is a type-id-list. */
14156 if (token->type != CPP_CLOSE_PAREN)
14158 const char *saved_message;
14160 /* Types may not be defined in an exception-specification. */
14161 saved_message = parser->type_definition_forbidden_message;
14162 parser->type_definition_forbidden_message
14163 = "types may not be defined in an exception-specification";
14164 /* Parse the type-id-list. */
14165 type_id_list = cp_parser_type_id_list (parser);
14166 /* Restore the saved message. */
14167 parser->type_definition_forbidden_message = saved_message;
14170 type_id_list = empty_except_spec;
14172 /* Look for the `)'. */
14173 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14175 return type_id_list;
14178 /* Parse an (optional) type-id-list.
14182 type-id-list , type-id
14184 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14185 in the order that the types were presented. */
14188 cp_parser_type_id_list (cp_parser* parser)
14190 tree types = NULL_TREE;
14197 /* Get the next type-id. */
14198 type = cp_parser_type_id (parser);
14199 /* Add it to the list. */
14200 types = add_exception_specifier (types, type, /*complain=*/1);
14201 /* Peek at the next token. */
14202 token = cp_lexer_peek_token (parser->lexer);
14203 /* If it is not a `,', we are done. */
14204 if (token->type != CPP_COMMA)
14206 /* Consume the `,'. */
14207 cp_lexer_consume_token (parser->lexer);
14210 return nreverse (types);
14213 /* Parse a try-block.
14216 try compound-statement handler-seq */
14219 cp_parser_try_block (cp_parser* parser)
14223 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14224 try_block = begin_try_block ();
14225 cp_parser_compound_statement (parser, NULL, true);
14226 finish_try_block (try_block);
14227 cp_parser_handler_seq (parser);
14228 finish_handler_sequence (try_block);
14233 /* Parse a function-try-block.
14235 function-try-block:
14236 try ctor-initializer [opt] function-body handler-seq */
14239 cp_parser_function_try_block (cp_parser* parser)
14242 bool ctor_initializer_p;
14244 /* Look for the `try' keyword. */
14245 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14247 /* Let the rest of the front-end know where we are. */
14248 try_block = begin_function_try_block ();
14249 /* Parse the function-body. */
14251 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14252 /* We're done with the `try' part. */
14253 finish_function_try_block (try_block);
14254 /* Parse the handlers. */
14255 cp_parser_handler_seq (parser);
14256 /* We're done with the handlers. */
14257 finish_function_handler_sequence (try_block);
14259 return ctor_initializer_p;
14262 /* Parse a handler-seq.
14265 handler handler-seq [opt] */
14268 cp_parser_handler_seq (cp_parser* parser)
14274 /* Parse the handler. */
14275 cp_parser_handler (parser);
14276 /* Peek at the next token. */
14277 token = cp_lexer_peek_token (parser->lexer);
14278 /* If it's not `catch' then there are no more handlers. */
14279 if (!cp_parser_is_keyword (token, RID_CATCH))
14284 /* Parse a handler.
14287 catch ( exception-declaration ) compound-statement */
14290 cp_parser_handler (cp_parser* parser)
14295 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14296 handler = begin_handler ();
14297 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14298 declaration = cp_parser_exception_declaration (parser);
14299 finish_handler_parms (declaration, handler);
14300 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14301 cp_parser_compound_statement (parser, NULL, false);
14302 finish_handler (handler);
14305 /* Parse an exception-declaration.
14307 exception-declaration:
14308 type-specifier-seq declarator
14309 type-specifier-seq abstract-declarator
14313 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14314 ellipsis variant is used. */
14317 cp_parser_exception_declaration (cp_parser* parser)
14320 cp_decl_specifier_seq type_specifiers;
14321 cp_declarator *declarator;
14322 const char *saved_message;
14324 /* If it's an ellipsis, it's easy to handle. */
14325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14327 /* Consume the `...' token. */
14328 cp_lexer_consume_token (parser->lexer);
14332 /* Types may not be defined in exception-declarations. */
14333 saved_message = parser->type_definition_forbidden_message;
14334 parser->type_definition_forbidden_message
14335 = "types may not be defined in exception-declarations";
14337 /* Parse the type-specifier-seq. */
14338 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14340 /* If it's a `)', then there is no declarator. */
14341 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14344 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14345 /*ctor_dtor_or_conv_p=*/NULL,
14346 /*parenthesized_p=*/NULL,
14347 /*member_p=*/false);
14349 /* Restore the saved message. */
14350 parser->type_definition_forbidden_message = saved_message;
14352 if (type_specifiers.any_specifiers_p)
14354 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14355 if (decl == NULL_TREE)
14356 error ("invalid catch parameter");
14364 /* Parse a throw-expression.
14367 throw assignment-expression [opt]
14369 Returns a THROW_EXPR representing the throw-expression. */
14372 cp_parser_throw_expression (cp_parser* parser)
14377 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14378 token = cp_lexer_peek_token (parser->lexer);
14379 /* Figure out whether or not there is an assignment-expression
14380 following the "throw" keyword. */
14381 if (token->type == CPP_COMMA
14382 || token->type == CPP_SEMICOLON
14383 || token->type == CPP_CLOSE_PAREN
14384 || token->type == CPP_CLOSE_SQUARE
14385 || token->type == CPP_CLOSE_BRACE
14386 || token->type == CPP_COLON)
14387 expression = NULL_TREE;
14389 expression = cp_parser_assignment_expression (parser,
14392 return build_throw (expression);
14395 /* GNU Extensions */
14397 /* Parse an (optional) asm-specification.
14400 asm ( string-literal )
14402 If the asm-specification is present, returns a STRING_CST
14403 corresponding to the string-literal. Otherwise, returns
14407 cp_parser_asm_specification_opt (cp_parser* parser)
14410 tree asm_specification;
14412 /* Peek at the next token. */
14413 token = cp_lexer_peek_token (parser->lexer);
14414 /* If the next token isn't the `asm' keyword, then there's no
14415 asm-specification. */
14416 if (!cp_parser_is_keyword (token, RID_ASM))
14419 /* Consume the `asm' token. */
14420 cp_lexer_consume_token (parser->lexer);
14421 /* Look for the `('. */
14422 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14424 /* Look for the string-literal. */
14425 asm_specification = cp_parser_string_literal (parser, false, false);
14427 /* Look for the `)'. */
14428 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14430 return asm_specification;
14433 /* Parse an asm-operand-list.
14437 asm-operand-list , asm-operand
14440 string-literal ( expression )
14441 [ string-literal ] string-literal ( expression )
14443 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14444 each node is the expression. The TREE_PURPOSE is itself a
14445 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14446 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14447 is a STRING_CST for the string literal before the parenthesis. */
14450 cp_parser_asm_operand_list (cp_parser* parser)
14452 tree asm_operands = NULL_TREE;
14456 tree string_literal;
14460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14462 /* Consume the `[' token. */
14463 cp_lexer_consume_token (parser->lexer);
14464 /* Read the operand name. */
14465 name = cp_parser_identifier (parser);
14466 if (name != error_mark_node)
14467 name = build_string (IDENTIFIER_LENGTH (name),
14468 IDENTIFIER_POINTER (name));
14469 /* Look for the closing `]'. */
14470 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14474 /* Look for the string-literal. */
14475 string_literal = cp_parser_string_literal (parser, false, false);
14477 /* Look for the `('. */
14478 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14479 /* Parse the expression. */
14480 expression = cp_parser_expression (parser, /*cast_p=*/false);
14481 /* Look for the `)'. */
14482 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14484 /* Add this operand to the list. */
14485 asm_operands = tree_cons (build_tree_list (name, string_literal),
14488 /* If the next token is not a `,', there are no more
14490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14492 /* Consume the `,'. */
14493 cp_lexer_consume_token (parser->lexer);
14496 return nreverse (asm_operands);
14499 /* Parse an asm-clobber-list.
14503 asm-clobber-list , string-literal
14505 Returns a TREE_LIST, indicating the clobbers in the order that they
14506 appeared. The TREE_VALUE of each node is a STRING_CST. */
14509 cp_parser_asm_clobber_list (cp_parser* parser)
14511 tree clobbers = NULL_TREE;
14515 tree string_literal;
14517 /* Look for the string literal. */
14518 string_literal = cp_parser_string_literal (parser, false, false);
14519 /* Add it to the list. */
14520 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14521 /* If the next token is not a `,', then the list is
14523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14525 /* Consume the `,' token. */
14526 cp_lexer_consume_token (parser->lexer);
14532 /* Parse an (optional) series of attributes.
14535 attributes attribute
14538 __attribute__ (( attribute-list [opt] ))
14540 The return value is as for cp_parser_attribute_list. */
14543 cp_parser_attributes_opt (cp_parser* parser)
14545 tree attributes = NULL_TREE;
14550 tree attribute_list;
14552 /* Peek at the next token. */
14553 token = cp_lexer_peek_token (parser->lexer);
14554 /* If it's not `__attribute__', then we're done. */
14555 if (token->keyword != RID_ATTRIBUTE)
14558 /* Consume the `__attribute__' keyword. */
14559 cp_lexer_consume_token (parser->lexer);
14560 /* Look for the two `(' tokens. */
14561 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14562 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14564 /* Peek at the next token. */
14565 token = cp_lexer_peek_token (parser->lexer);
14566 if (token->type != CPP_CLOSE_PAREN)
14567 /* Parse the attribute-list. */
14568 attribute_list = cp_parser_attribute_list (parser);
14570 /* If the next token is a `)', then there is no attribute
14572 attribute_list = NULL;
14574 /* Look for the two `)' tokens. */
14575 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14576 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14578 /* Add these new attributes to the list. */
14579 attributes = chainon (attributes, attribute_list);
14585 /* Parse an attribute-list.
14589 attribute-list , attribute
14593 identifier ( identifier )
14594 identifier ( identifier , expression-list )
14595 identifier ( expression-list )
14597 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14598 to an attribute. The TREE_PURPOSE of each node is the identifier
14599 indicating which attribute is in use. The TREE_VALUE represents
14600 the arguments, if any. */
14603 cp_parser_attribute_list (cp_parser* parser)
14605 tree attribute_list = NULL_TREE;
14606 bool save_translate_strings_p = parser->translate_strings_p;
14608 parser->translate_strings_p = false;
14615 /* Look for the identifier. We also allow keywords here; for
14616 example `__attribute__ ((const))' is legal. */
14617 token = cp_lexer_peek_token (parser->lexer);
14618 if (token->type == CPP_NAME
14619 || token->type == CPP_KEYWORD)
14621 /* Consume the token. */
14622 token = cp_lexer_consume_token (parser->lexer);
14624 /* Save away the identifier that indicates which attribute
14626 identifier = token->value;
14627 attribute = build_tree_list (identifier, NULL_TREE);
14629 /* Peek at the next token. */
14630 token = cp_lexer_peek_token (parser->lexer);
14631 /* If it's an `(', then parse the attribute arguments. */
14632 if (token->type == CPP_OPEN_PAREN)
14636 arguments = (cp_parser_parenthesized_expression_list
14637 (parser, true, /*cast_p=*/false,
14638 /*non_constant_p=*/NULL));
14639 /* Save the identifier and arguments away. */
14640 TREE_VALUE (attribute) = arguments;
14643 /* Add this attribute to the list. */
14644 TREE_CHAIN (attribute) = attribute_list;
14645 attribute_list = attribute;
14647 token = cp_lexer_peek_token (parser->lexer);
14649 /* Now, look for more attributes. If the next token isn't a
14650 `,', we're done. */
14651 if (token->type != CPP_COMMA)
14654 /* Consume the comma and keep going. */
14655 cp_lexer_consume_token (parser->lexer);
14657 parser->translate_strings_p = save_translate_strings_p;
14659 /* We built up the list in reverse order. */
14660 return nreverse (attribute_list);
14663 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14664 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14665 current value of the PEDANTIC flag, regardless of whether or not
14666 the `__extension__' keyword is present. The caller is responsible
14667 for restoring the value of the PEDANTIC flag. */
14670 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14672 /* Save the old value of the PEDANTIC flag. */
14673 *saved_pedantic = pedantic;
14675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14677 /* Consume the `__extension__' token. */
14678 cp_lexer_consume_token (parser->lexer);
14679 /* We're not being pedantic while the `__extension__' keyword is
14689 /* Parse a label declaration.
14692 __label__ label-declarator-seq ;
14694 label-declarator-seq:
14695 identifier , label-declarator-seq
14699 cp_parser_label_declaration (cp_parser* parser)
14701 /* Look for the `__label__' keyword. */
14702 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14708 /* Look for an identifier. */
14709 identifier = cp_parser_identifier (parser);
14710 /* If we failed, stop. */
14711 if (identifier == error_mark_node)
14713 /* Declare it as a label. */
14714 finish_label_decl (identifier);
14715 /* If the next token is a `;', stop. */
14716 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14718 /* Look for the `,' separating the label declarations. */
14719 cp_parser_require (parser, CPP_COMMA, "`,'");
14722 /* Look for the final `;'. */
14723 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14726 /* Support Functions */
14728 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14729 NAME should have one of the representations used for an
14730 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14731 is returned. If PARSER->SCOPE is a dependent type, then a
14732 SCOPE_REF is returned.
14734 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14735 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14736 was formed. Abstractly, such entities should not be passed to this
14737 function, because they do not need to be looked up, but it is
14738 simpler to check for this special case here, rather than at the
14741 In cases not explicitly covered above, this function returns a
14742 DECL, OVERLOAD, or baselink representing the result of the lookup.
14743 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14746 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14747 (e.g., "struct") that was used. In that case bindings that do not
14748 refer to types are ignored.
14750 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14753 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14756 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14759 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14760 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14761 NULL_TREE otherwise. */
14764 cp_parser_lookup_name (cp_parser *parser, tree name,
14765 enum tag_types tag_type,
14768 bool check_dependency,
14769 tree *ambiguous_decls)
14773 tree object_type = parser->context->object_type;
14775 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14776 flags |= LOOKUP_COMPLAIN;
14778 /* Assume that the lookup will be unambiguous. */
14779 if (ambiguous_decls)
14780 *ambiguous_decls = NULL_TREE;
14782 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14783 no longer valid. Note that if we are parsing tentatively, and
14784 the parse fails, OBJECT_TYPE will be automatically restored. */
14785 parser->context->object_type = NULL_TREE;
14787 if (name == error_mark_node)
14788 return error_mark_node;
14790 /* A template-id has already been resolved; there is no lookup to
14792 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14794 if (BASELINK_P (name))
14796 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14797 == TEMPLATE_ID_EXPR);
14801 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14802 it should already have been checked to make sure that the name
14803 used matches the type being destroyed. */
14804 if (TREE_CODE (name) == BIT_NOT_EXPR)
14808 /* Figure out to which type this destructor applies. */
14810 type = parser->scope;
14811 else if (object_type)
14812 type = object_type;
14814 type = current_class_type;
14815 /* If that's not a class type, there is no destructor. */
14816 if (!type || !CLASS_TYPE_P (type))
14817 return error_mark_node;
14818 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14819 lazily_declare_fn (sfk_destructor, type);
14820 if (!CLASSTYPE_DESTRUCTORS (type))
14821 return error_mark_node;
14822 /* If it was a class type, return the destructor. */
14823 return CLASSTYPE_DESTRUCTORS (type);
14826 /* By this point, the NAME should be an ordinary identifier. If
14827 the id-expression was a qualified name, the qualifying scope is
14828 stored in PARSER->SCOPE at this point. */
14829 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14831 /* Perform the lookup. */
14836 if (parser->scope == error_mark_node)
14837 return error_mark_node;
14839 /* If the SCOPE is dependent, the lookup must be deferred until
14840 the template is instantiated -- unless we are explicitly
14841 looking up names in uninstantiated templates. Even then, we
14842 cannot look up the name if the scope is not a class type; it
14843 might, for example, be a template type parameter. */
14844 dependent_p = (TYPE_P (parser->scope)
14845 && !(parser->in_declarator_p
14846 && currently_open_class (parser->scope))
14847 && dependent_type_p (parser->scope));
14848 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14855 /* The resolution to Core Issue 180 says that `struct
14856 A::B' should be considered a type-name, even if `A'
14858 type = make_typename_type (parser->scope, name, tag_type,
14859 /*complain=*/tf_error);
14860 decl = TYPE_NAME (type);
14862 else if (is_template
14863 && (cp_parser_next_token_ends_template_argument_p (parser)
14864 || cp_lexer_next_token_is (parser->lexer,
14866 decl = make_unbound_class_template (parser->scope,
14868 /*complain=*/tf_error);
14870 decl = build_qualified_name (/*type=*/NULL_TREE,
14871 parser->scope, name,
14876 tree pushed_scope = NULL_TREE;
14878 /* If PARSER->SCOPE is a dependent type, then it must be a
14879 class type, and we must not be checking dependencies;
14880 otherwise, we would have processed this lookup above. So
14881 that PARSER->SCOPE is not considered a dependent base by
14882 lookup_member, we must enter the scope here. */
14884 pushed_scope = push_scope (parser->scope);
14885 /* If the PARSER->SCOPE is a template specialization, it
14886 may be instantiated during name lookup. In that case,
14887 errors may be issued. Even if we rollback the current
14888 tentative parse, those errors are valid. */
14889 decl = lookup_qualified_name (parser->scope, name,
14890 tag_type != none_type,
14891 /*complain=*/true);
14893 pop_scope (pushed_scope);
14895 parser->qualifying_scope = parser->scope;
14896 parser->object_scope = NULL_TREE;
14898 else if (object_type)
14900 tree object_decl = NULL_TREE;
14901 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14902 OBJECT_TYPE is not a class. */
14903 if (CLASS_TYPE_P (object_type))
14904 /* If the OBJECT_TYPE is a template specialization, it may
14905 be instantiated during name lookup. In that case, errors
14906 may be issued. Even if we rollback the current tentative
14907 parse, those errors are valid. */
14908 object_decl = lookup_member (object_type,
14911 tag_type != none_type);
14912 /* Look it up in the enclosing context, too. */
14913 decl = lookup_name_real (name, tag_type != none_type,
14915 /*block_p=*/true, is_namespace, flags);
14916 parser->object_scope = object_type;
14917 parser->qualifying_scope = NULL_TREE;
14919 decl = object_decl;
14923 decl = lookup_name_real (name, tag_type != none_type,
14925 /*block_p=*/true, is_namespace, flags);
14926 parser->qualifying_scope = NULL_TREE;
14927 parser->object_scope = NULL_TREE;
14930 /* If the lookup failed, let our caller know. */
14931 if (!decl || decl == error_mark_node)
14932 return error_mark_node;
14934 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14935 if (TREE_CODE (decl) == TREE_LIST)
14937 if (ambiguous_decls)
14938 *ambiguous_decls = decl;
14939 /* The error message we have to print is too complicated for
14940 cp_parser_error, so we incorporate its actions directly. */
14941 if (!cp_parser_simulate_error (parser))
14943 error ("reference to %qD is ambiguous", name);
14944 print_candidates (decl);
14946 return error_mark_node;
14949 gcc_assert (DECL_P (decl)
14950 || TREE_CODE (decl) == OVERLOAD
14951 || TREE_CODE (decl) == SCOPE_REF
14952 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14953 || BASELINK_P (decl));
14955 /* If we have resolved the name of a member declaration, check to
14956 see if the declaration is accessible. When the name resolves to
14957 set of overloaded functions, accessibility is checked when
14958 overload resolution is done.
14960 During an explicit instantiation, access is not checked at all,
14961 as per [temp.explicit]. */
14963 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14968 /* Like cp_parser_lookup_name, but for use in the typical case where
14969 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14970 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14973 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14975 return cp_parser_lookup_name (parser, name,
14977 /*is_template=*/false,
14978 /*is_namespace=*/false,
14979 /*check_dependency=*/true,
14980 /*ambiguous_decls=*/NULL);
14983 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14984 the current context, return the TYPE_DECL. If TAG_NAME_P is
14985 true, the DECL indicates the class being defined in a class-head,
14986 or declared in an elaborated-type-specifier.
14988 Otherwise, return DECL. */
14991 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14993 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14994 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14997 template <typename T> struct B;
15000 template <typename T> struct A::B {};
15002 Similarly, in an elaborated-type-specifier:
15004 namespace N { struct X{}; }
15007 template <typename T> friend struct N::X;
15010 However, if the DECL refers to a class type, and we are in
15011 the scope of the class, then the name lookup automatically
15012 finds the TYPE_DECL created by build_self_reference rather
15013 than a TEMPLATE_DECL. For example, in:
15015 template <class T> struct S {
15019 there is no need to handle such case. */
15021 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15022 return DECL_TEMPLATE_RESULT (decl);
15027 /* If too many, or too few, template-parameter lists apply to the
15028 declarator, issue an error message. Returns TRUE if all went well,
15029 and FALSE otherwise. */
15032 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15033 cp_declarator *declarator)
15035 unsigned num_templates;
15037 /* We haven't seen any classes that involve template parameters yet. */
15040 switch (declarator->kind)
15043 if (declarator->u.id.qualifying_scope)
15048 scope = declarator->u.id.qualifying_scope;
15049 member = declarator->u.id.unqualified_name;
15051 while (scope && CLASS_TYPE_P (scope))
15053 /* You're supposed to have one `template <...>'
15054 for every template class, but you don't need one
15055 for a full specialization. For example:
15057 template <class T> struct S{};
15058 template <> struct S<int> { void f(); };
15059 void S<int>::f () {}
15061 is correct; there shouldn't be a `template <>' for
15062 the definition of `S<int>::f'. */
15063 if (CLASSTYPE_TEMPLATE_INFO (scope)
15064 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15065 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15066 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15069 scope = TYPE_CONTEXT (scope);
15072 else if (TREE_CODE (declarator->u.id.unqualified_name)
15073 == TEMPLATE_ID_EXPR)
15074 /* If the DECLARATOR has the form `X<y>' then it uses one
15075 additional level of template parameters. */
15078 return cp_parser_check_template_parameters (parser,
15084 case cdk_reference:
15086 return (cp_parser_check_declarator_template_parameters
15087 (parser, declarator->declarator));
15093 gcc_unreachable ();
15098 /* NUM_TEMPLATES were used in the current declaration. If that is
15099 invalid, return FALSE and issue an error messages. Otherwise,
15103 cp_parser_check_template_parameters (cp_parser* parser,
15104 unsigned num_templates)
15106 /* If there are more template classes than parameter lists, we have
15109 template <class T> void S<T>::R<T>::f (); */
15110 if (parser->num_template_parameter_lists < num_templates)
15112 error ("too few template-parameter-lists");
15115 /* If there are the same number of template classes and parameter
15116 lists, that's OK. */
15117 if (parser->num_template_parameter_lists == num_templates)
15119 /* If there are more, but only one more, then we are referring to a
15120 member template. That's OK too. */
15121 if (parser->num_template_parameter_lists == num_templates + 1)
15123 /* Otherwise, there are too many template parameter lists. We have
15126 template <class T> template <class U> void S::f(); */
15127 error ("too many template-parameter-lists");
15131 /* Parse an optional `::' token indicating that the following name is
15132 from the global namespace. If so, PARSER->SCOPE is set to the
15133 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15134 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15135 Returns the new value of PARSER->SCOPE, if the `::' token is
15136 present, and NULL_TREE otherwise. */
15139 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15143 /* Peek at the next token. */
15144 token = cp_lexer_peek_token (parser->lexer);
15145 /* If we're looking at a `::' token then we're starting from the
15146 global namespace, not our current location. */
15147 if (token->type == CPP_SCOPE)
15149 /* Consume the `::' token. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Set the SCOPE so that we know where to start the lookup. */
15152 parser->scope = global_namespace;
15153 parser->qualifying_scope = global_namespace;
15154 parser->object_scope = NULL_TREE;
15156 return parser->scope;
15158 else if (!current_scope_valid_p)
15160 parser->scope = NULL_TREE;
15161 parser->qualifying_scope = NULL_TREE;
15162 parser->object_scope = NULL_TREE;
15168 /* Returns TRUE if the upcoming token sequence is the start of a
15169 constructor declarator. If FRIEND_P is true, the declarator is
15170 preceded by the `friend' specifier. */
15173 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15175 bool constructor_p;
15176 tree type_decl = NULL_TREE;
15177 bool nested_name_p;
15178 cp_token *next_token;
15180 /* The common case is that this is not a constructor declarator, so
15181 try to avoid doing lots of work if at all possible. It's not
15182 valid declare a constructor at function scope. */
15183 if (at_function_scope_p ())
15185 /* And only certain tokens can begin a constructor declarator. */
15186 next_token = cp_lexer_peek_token (parser->lexer);
15187 if (next_token->type != CPP_NAME
15188 && next_token->type != CPP_SCOPE
15189 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15190 && next_token->type != CPP_TEMPLATE_ID)
15193 /* Parse tentatively; we are going to roll back all of the tokens
15195 cp_parser_parse_tentatively (parser);
15196 /* Assume that we are looking at a constructor declarator. */
15197 constructor_p = true;
15199 /* Look for the optional `::' operator. */
15200 cp_parser_global_scope_opt (parser,
15201 /*current_scope_valid_p=*/false);
15202 /* Look for the nested-name-specifier. */
15204 = (cp_parser_nested_name_specifier_opt (parser,
15205 /*typename_keyword_p=*/false,
15206 /*check_dependency_p=*/false,
15208 /*is_declaration=*/false)
15210 /* Outside of a class-specifier, there must be a
15211 nested-name-specifier. */
15212 if (!nested_name_p &&
15213 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15215 constructor_p = false;
15216 /* If we still think that this might be a constructor-declarator,
15217 look for a class-name. */
15222 template <typename T> struct S { S(); };
15223 template <typename T> S<T>::S ();
15225 we must recognize that the nested `S' names a class.
15228 template <typename T> S<T>::S<T> ();
15230 we must recognize that the nested `S' names a template. */
15231 type_decl = cp_parser_class_name (parser,
15232 /*typename_keyword_p=*/false,
15233 /*template_keyword_p=*/false,
15235 /*check_dependency_p=*/false,
15236 /*class_head_p=*/false,
15237 /*is_declaration=*/false);
15238 /* If there was no class-name, then this is not a constructor. */
15239 constructor_p = !cp_parser_error_occurred (parser);
15242 /* If we're still considering a constructor, we have to see a `(',
15243 to begin the parameter-declaration-clause, followed by either a
15244 `)', an `...', or a decl-specifier. We need to check for a
15245 type-specifier to avoid being fooled into thinking that:
15249 is a constructor. (It is actually a function named `f' that
15250 takes one parameter (of type `int') and returns a value of type
15253 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15256 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15257 /* A parameter declaration begins with a decl-specifier,
15258 which is either the "attribute" keyword, a storage class
15259 specifier, or (usually) a type-specifier. */
15260 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15261 && !cp_parser_storage_class_specifier_opt (parser))
15264 tree pushed_scope = NULL_TREE;
15265 unsigned saved_num_template_parameter_lists;
15267 /* Names appearing in the type-specifier should be looked up
15268 in the scope of the class. */
15269 if (current_class_type)
15273 type = TREE_TYPE (type_decl);
15274 if (TREE_CODE (type) == TYPENAME_TYPE)
15276 type = resolve_typename_type (type,
15277 /*only_current_p=*/false);
15278 if (type == error_mark_node)
15280 cp_parser_abort_tentative_parse (parser);
15284 pushed_scope = push_scope (type);
15287 /* Inside the constructor parameter list, surrounding
15288 template-parameter-lists do not apply. */
15289 saved_num_template_parameter_lists
15290 = parser->num_template_parameter_lists;
15291 parser->num_template_parameter_lists = 0;
15293 /* Look for the type-specifier. */
15294 cp_parser_type_specifier (parser,
15295 CP_PARSER_FLAGS_NONE,
15296 /*decl_specs=*/NULL,
15297 /*is_declarator=*/true,
15298 /*declares_class_or_enum=*/NULL,
15299 /*is_cv_qualifier=*/NULL);
15301 parser->num_template_parameter_lists
15302 = saved_num_template_parameter_lists;
15304 /* Leave the scope of the class. */
15306 pop_scope (pushed_scope);
15308 constructor_p = !cp_parser_error_occurred (parser);
15312 constructor_p = false;
15313 /* We did not really want to consume any tokens. */
15314 cp_parser_abort_tentative_parse (parser);
15316 return constructor_p;
15319 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15320 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15321 they must be performed once we are in the scope of the function.
15323 Returns the function defined. */
15326 cp_parser_function_definition_from_specifiers_and_declarator
15327 (cp_parser* parser,
15328 cp_decl_specifier_seq *decl_specifiers,
15330 const cp_declarator *declarator)
15335 /* Begin the function-definition. */
15336 success_p = start_function (decl_specifiers, declarator, attributes);
15338 /* The things we're about to see are not directly qualified by any
15339 template headers we've seen thus far. */
15340 reset_specialization ();
15342 /* If there were names looked up in the decl-specifier-seq that we
15343 did not check, check them now. We must wait until we are in the
15344 scope of the function to perform the checks, since the function
15345 might be a friend. */
15346 perform_deferred_access_checks ();
15350 /* Skip the entire function. */
15351 cp_parser_skip_to_end_of_block_or_statement (parser);
15352 fn = error_mark_node;
15355 fn = cp_parser_function_definition_after_declarator (parser,
15356 /*inline_p=*/false);
15361 /* Parse the part of a function-definition that follows the
15362 declarator. INLINE_P is TRUE iff this function is an inline
15363 function defined with a class-specifier.
15365 Returns the function defined. */
15368 cp_parser_function_definition_after_declarator (cp_parser* parser,
15372 bool ctor_initializer_p = false;
15373 bool saved_in_unbraced_linkage_specification_p;
15374 unsigned saved_num_template_parameter_lists;
15376 /* If the next token is `return', then the code may be trying to
15377 make use of the "named return value" extension that G++ used to
15379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15381 /* Consume the `return' keyword. */
15382 cp_lexer_consume_token (parser->lexer);
15383 /* Look for the identifier that indicates what value is to be
15385 cp_parser_identifier (parser);
15386 /* Issue an error message. */
15387 error ("named return values are no longer supported");
15388 /* Skip tokens until we reach the start of the function body. */
15391 cp_token *token = cp_lexer_peek_token (parser->lexer);
15392 if (token->type == CPP_OPEN_BRACE
15393 || token->type == CPP_EOF
15394 || token->type == CPP_PRAGMA_EOL)
15396 cp_lexer_consume_token (parser->lexer);
15399 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15400 anything declared inside `f'. */
15401 saved_in_unbraced_linkage_specification_p
15402 = parser->in_unbraced_linkage_specification_p;
15403 parser->in_unbraced_linkage_specification_p = false;
15404 /* Inside the function, surrounding template-parameter-lists do not
15406 saved_num_template_parameter_lists
15407 = parser->num_template_parameter_lists;
15408 parser->num_template_parameter_lists = 0;
15409 /* If the next token is `try', then we are looking at a
15410 function-try-block. */
15411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15412 ctor_initializer_p = cp_parser_function_try_block (parser);
15413 /* A function-try-block includes the function-body, so we only do
15414 this next part if we're not processing a function-try-block. */
15417 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15419 /* Finish the function. */
15420 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15421 (inline_p ? 2 : 0));
15422 /* Generate code for it, if necessary. */
15423 expand_or_defer_fn (fn);
15424 /* Restore the saved values. */
15425 parser->in_unbraced_linkage_specification_p
15426 = saved_in_unbraced_linkage_specification_p;
15427 parser->num_template_parameter_lists
15428 = saved_num_template_parameter_lists;
15433 /* Parse a template-declaration, assuming that the `export' (and
15434 `extern') keywords, if present, has already been scanned. MEMBER_P
15435 is as for cp_parser_template_declaration. */
15438 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15440 tree decl = NULL_TREE;
15442 tree parameter_list;
15443 bool friend_p = false;
15444 bool need_lang_pop;
15446 /* Look for the `template' keyword. */
15447 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15451 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15455 A template ... shall not have C linkage. */
15456 if (current_lang_name == lang_name_c)
15458 error ("template with C linkage");
15459 /* Give it C++ linkage to avoid confusing other parts of the
15461 push_lang_context (lang_name_cplusplus);
15462 need_lang_pop = true;
15465 need_lang_pop = false;
15467 /* We cannot perform access checks on the template parameter
15468 declarations until we know what is being declared, just as we
15469 cannot check the decl-specifier list. */
15470 push_deferring_access_checks (dk_deferred);
15472 /* If the next token is `>', then we have an invalid
15473 specialization. Rather than complain about an invalid template
15474 parameter, issue an error message here. */
15475 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15477 cp_parser_error (parser, "invalid explicit specialization");
15478 begin_specialization ();
15479 parameter_list = NULL_TREE;
15482 /* Parse the template parameters. */
15483 parameter_list = cp_parser_template_parameter_list (parser);
15485 /* Get the deferred access checks from the parameter list. These
15486 will be checked once we know what is being declared, as for a
15487 member template the checks must be performed in the scope of the
15488 class containing the member. */
15489 checks = get_deferred_access_checks ();
15491 /* Look for the `>'. */
15492 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15493 /* We just processed one more parameter list. */
15494 ++parser->num_template_parameter_lists;
15495 /* If the next token is `template', there are more template
15497 if (cp_lexer_next_token_is_keyword (parser->lexer,
15499 cp_parser_template_declaration_after_export (parser, member_p);
15502 /* There are no access checks when parsing a template, as we do not
15503 know if a specialization will be a friend. */
15504 push_deferring_access_checks (dk_no_check);
15505 decl = cp_parser_single_declaration (parser,
15509 pop_deferring_access_checks ();
15511 /* If this is a member template declaration, let the front
15513 if (member_p && !friend_p && decl)
15515 if (TREE_CODE (decl) == TYPE_DECL)
15516 cp_parser_check_access_in_redeclaration (decl);
15518 decl = finish_member_template_decl (decl);
15520 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15521 make_friend_class (current_class_type, TREE_TYPE (decl),
15522 /*complain=*/true);
15524 /* We are done with the current parameter list. */
15525 --parser->num_template_parameter_lists;
15527 pop_deferring_access_checks ();
15530 finish_template_decl (parameter_list);
15532 /* Register member declarations. */
15533 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15534 finish_member_declaration (decl);
15535 /* For the erroneous case of a template with C linkage, we pushed an
15536 implicit C++ linkage scope; exit that scope now. */
15538 pop_lang_context ();
15539 /* If DECL is a function template, we must return to parse it later.
15540 (Even though there is no definition, there might be default
15541 arguments that need handling.) */
15542 if (member_p && decl
15543 && (TREE_CODE (decl) == FUNCTION_DECL
15544 || DECL_FUNCTION_TEMPLATE_P (decl)))
15545 TREE_VALUE (parser->unparsed_functions_queues)
15546 = tree_cons (NULL_TREE, decl,
15547 TREE_VALUE (parser->unparsed_functions_queues));
15550 /* Perform the deferred access checks from a template-parameter-list.
15551 CHECKS is a TREE_LIST of access checks, as returned by
15552 get_deferred_access_checks. */
15555 cp_parser_perform_template_parameter_access_checks (tree checks)
15557 ++processing_template_parmlist;
15558 perform_access_checks (checks);
15559 --processing_template_parmlist;
15562 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15563 `function-definition' sequence. MEMBER_P is true, this declaration
15564 appears in a class scope.
15566 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15567 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15570 cp_parser_single_declaration (cp_parser* parser,
15575 int declares_class_or_enum;
15576 tree decl = NULL_TREE;
15577 cp_decl_specifier_seq decl_specifiers;
15578 bool function_definition_p = false;
15580 /* This function is only used when processing a template
15582 gcc_assert (innermost_scope_kind () == sk_template_parms
15583 || innermost_scope_kind () == sk_template_spec);
15585 /* Defer access checks until we know what is being declared. */
15586 push_deferring_access_checks (dk_deferred);
15588 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15590 cp_parser_decl_specifier_seq (parser,
15591 CP_PARSER_FLAGS_OPTIONAL,
15593 &declares_class_or_enum);
15595 *friend_p = cp_parser_friend_p (&decl_specifiers);
15597 /* There are no template typedefs. */
15598 if (decl_specifiers.specs[(int) ds_typedef])
15600 error ("template declaration of %qs", "typedef");
15601 decl = error_mark_node;
15604 /* Gather up the access checks that occurred the
15605 decl-specifier-seq. */
15606 stop_deferring_access_checks ();
15608 /* Check for the declaration of a template class. */
15609 if (declares_class_or_enum)
15611 if (cp_parser_declares_only_class_p (parser))
15613 decl = shadow_tag (&decl_specifiers);
15618 friend template <typename T> struct A<T>::B;
15621 A<T>::B will be represented by a TYPENAME_TYPE, and
15622 therefore not recognized by shadow_tag. */
15623 if (friend_p && *friend_p
15625 && decl_specifiers.type
15626 && TYPE_P (decl_specifiers.type))
15627 decl = decl_specifiers.type;
15629 if (decl && decl != error_mark_node)
15630 decl = TYPE_NAME (decl);
15632 decl = error_mark_node;
15634 /* Perform access checks for template parameters. */
15635 cp_parser_perform_template_parameter_access_checks (checks);
15638 /* If it's not a template class, try for a template function. If
15639 the next token is a `;', then this declaration does not declare
15640 anything. But, if there were errors in the decl-specifiers, then
15641 the error might well have come from an attempted class-specifier.
15642 In that case, there's no need to warn about a missing declarator. */
15644 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15645 || decl_specifiers.type != error_mark_node))
15646 decl = cp_parser_init_declarator (parser,
15649 /*function_definition_allowed_p=*/true,
15651 declares_class_or_enum,
15652 &function_definition_p);
15654 pop_deferring_access_checks ();
15656 /* Clear any current qualification; whatever comes next is the start
15657 of something new. */
15658 parser->scope = NULL_TREE;
15659 parser->qualifying_scope = NULL_TREE;
15660 parser->object_scope = NULL_TREE;
15661 /* Look for a trailing `;' after the declaration. */
15662 if (!function_definition_p
15663 && (decl == error_mark_node
15664 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15665 cp_parser_skip_to_end_of_block_or_statement (parser);
15670 /* Parse a cast-expression that is not the operand of a unary "&". */
15673 cp_parser_simple_cast_expression (cp_parser *parser)
15675 return cp_parser_cast_expression (parser, /*address_p=*/false,
15679 /* Parse a functional cast to TYPE. Returns an expression
15680 representing the cast. */
15683 cp_parser_functional_cast (cp_parser* parser, tree type)
15685 tree expression_list;
15689 = cp_parser_parenthesized_expression_list (parser, false,
15691 /*non_constant_p=*/NULL);
15693 cast = build_functional_cast (type, expression_list);
15694 /* [expr.const]/1: In an integral constant expression "only type
15695 conversions to integral or enumeration type can be used". */
15696 if (TREE_CODE (type) == TYPE_DECL)
15697 type = TREE_TYPE (type);
15698 if (cast != error_mark_node && !dependent_type_p (type)
15699 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
15701 if (cp_parser_non_integral_constant_expression
15702 (parser, "a call to a constructor"))
15703 return error_mark_node;
15708 /* Save the tokens that make up the body of a member function defined
15709 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15710 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15711 specifiers applied to the declaration. Returns the FUNCTION_DECL
15712 for the member function. */
15715 cp_parser_save_member_function_body (cp_parser* parser,
15716 cp_decl_specifier_seq *decl_specifiers,
15717 cp_declarator *declarator,
15724 /* Create the function-declaration. */
15725 fn = start_method (decl_specifiers, declarator, attributes);
15726 /* If something went badly wrong, bail out now. */
15727 if (fn == error_mark_node)
15729 /* If there's a function-body, skip it. */
15730 if (cp_parser_token_starts_function_definition_p
15731 (cp_lexer_peek_token (parser->lexer)))
15732 cp_parser_skip_to_end_of_block_or_statement (parser);
15733 return error_mark_node;
15736 /* Remember it, if there default args to post process. */
15737 cp_parser_save_default_args (parser, fn);
15739 /* Save away the tokens that make up the body of the
15741 first = parser->lexer->next_token;
15742 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15743 /* Handle function try blocks. */
15744 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15745 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15746 last = parser->lexer->next_token;
15748 /* Save away the inline definition; we will process it when the
15749 class is complete. */
15750 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15751 DECL_PENDING_INLINE_P (fn) = 1;
15753 /* We need to know that this was defined in the class, so that
15754 friend templates are handled correctly. */
15755 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15757 /* We're done with the inline definition. */
15758 finish_method (fn);
15760 /* Add FN to the queue of functions to be parsed later. */
15761 TREE_VALUE (parser->unparsed_functions_queues)
15762 = tree_cons (NULL_TREE, fn,
15763 TREE_VALUE (parser->unparsed_functions_queues));
15768 /* Parse a template-argument-list, as well as the trailing ">" (but
15769 not the opening ">"). See cp_parser_template_argument_list for the
15773 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15777 tree saved_qualifying_scope;
15778 tree saved_object_scope;
15779 bool saved_greater_than_is_operator_p;
15780 bool saved_skip_evaluation;
15784 When parsing a template-id, the first non-nested `>' is taken as
15785 the end of the template-argument-list rather than a greater-than
15787 saved_greater_than_is_operator_p
15788 = parser->greater_than_is_operator_p;
15789 parser->greater_than_is_operator_p = false;
15790 /* Parsing the argument list may modify SCOPE, so we save it
15792 saved_scope = parser->scope;
15793 saved_qualifying_scope = parser->qualifying_scope;
15794 saved_object_scope = parser->object_scope;
15795 /* We need to evaluate the template arguments, even though this
15796 template-id may be nested within a "sizeof". */
15797 saved_skip_evaluation = skip_evaluation;
15798 skip_evaluation = false;
15799 /* Parse the template-argument-list itself. */
15800 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15801 arguments = NULL_TREE;
15803 arguments = cp_parser_template_argument_list (parser);
15804 /* Look for the `>' that ends the template-argument-list. If we find
15805 a '>>' instead, it's probably just a typo. */
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15808 if (!saved_greater_than_is_operator_p)
15810 /* If we're in a nested template argument list, the '>>' has
15811 to be a typo for '> >'. We emit the error message, but we
15812 continue parsing and we push a '>' as next token, so that
15813 the argument list will be parsed correctly. Note that the
15814 global source location is still on the token before the
15815 '>>', so we need to say explicitly where we want it. */
15816 cp_token *token = cp_lexer_peek_token (parser->lexer);
15817 error ("%H%<>>%> should be %<> >%> "
15818 "within a nested template argument list",
15821 /* ??? Proper recovery should terminate two levels of
15822 template argument list here. */
15823 token->type = CPP_GREATER;
15827 /* If this is not a nested template argument list, the '>>'
15828 is a typo for '>'. Emit an error message and continue.
15829 Same deal about the token location, but here we can get it
15830 right by consuming the '>>' before issuing the diagnostic. */
15831 cp_lexer_consume_token (parser->lexer);
15832 error ("spurious %<>>%>, use %<>%> to terminate "
15833 "a template argument list");
15837 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15838 /* The `>' token might be a greater-than operator again now. */
15839 parser->greater_than_is_operator_p
15840 = saved_greater_than_is_operator_p;
15841 /* Restore the SAVED_SCOPE. */
15842 parser->scope = saved_scope;
15843 parser->qualifying_scope = saved_qualifying_scope;
15844 parser->object_scope = saved_object_scope;
15845 skip_evaluation = saved_skip_evaluation;
15850 /* MEMBER_FUNCTION is a member function, or a friend. If default
15851 arguments, or the body of the function have not yet been parsed,
15855 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15857 /* If this member is a template, get the underlying
15859 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15860 member_function = DECL_TEMPLATE_RESULT (member_function);
15862 /* There should not be any class definitions in progress at this
15863 point; the bodies of members are only parsed outside of all class
15865 gcc_assert (parser->num_classes_being_defined == 0);
15866 /* While we're parsing the member functions we might encounter more
15867 classes. We want to handle them right away, but we don't want
15868 them getting mixed up with functions that are currently in the
15870 parser->unparsed_functions_queues
15871 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15873 /* Make sure that any template parameters are in scope. */
15874 maybe_begin_member_template_processing (member_function);
15876 /* If the body of the function has not yet been parsed, parse it
15878 if (DECL_PENDING_INLINE_P (member_function))
15880 tree function_scope;
15881 cp_token_cache *tokens;
15883 /* The function is no longer pending; we are processing it. */
15884 tokens = DECL_PENDING_INLINE_INFO (member_function);
15885 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15886 DECL_PENDING_INLINE_P (member_function) = 0;
15888 /* If this is a local class, enter the scope of the containing
15890 function_scope = current_function_decl;
15891 if (function_scope)
15892 push_function_context_to (function_scope);
15895 /* Push the body of the function onto the lexer stack. */
15896 cp_parser_push_lexer_for_tokens (parser, tokens);
15898 /* Let the front end know that we going to be defining this
15900 start_preparsed_function (member_function, NULL_TREE,
15901 SF_PRE_PARSED | SF_INCLASS_INLINE);
15903 /* Don't do access checking if it is a templated function. */
15904 if (processing_template_decl)
15905 push_deferring_access_checks (dk_no_check);
15907 /* Now, parse the body of the function. */
15908 cp_parser_function_definition_after_declarator (parser,
15909 /*inline_p=*/true);
15911 if (processing_template_decl)
15912 pop_deferring_access_checks ();
15914 /* Leave the scope of the containing function. */
15915 if (function_scope)
15916 pop_function_context_from (function_scope);
15917 cp_parser_pop_lexer (parser);
15920 /* Remove any template parameters from the symbol table. */
15921 maybe_end_member_template_processing ();
15923 /* Restore the queue. */
15924 parser->unparsed_functions_queues
15925 = TREE_CHAIN (parser->unparsed_functions_queues);
15928 /* If DECL contains any default args, remember it on the unparsed
15929 functions queue. */
15932 cp_parser_save_default_args (cp_parser* parser, tree decl)
15936 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15938 probe = TREE_CHAIN (probe))
15939 if (TREE_PURPOSE (probe))
15941 TREE_PURPOSE (parser->unparsed_functions_queues)
15942 = tree_cons (current_class_type, decl,
15943 TREE_PURPOSE (parser->unparsed_functions_queues));
15948 /* FN is a FUNCTION_DECL which may contains a parameter with an
15949 unparsed DEFAULT_ARG. Parse the default args now. This function
15950 assumes that the current scope is the scope in which the default
15951 argument should be processed. */
15954 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15956 bool saved_local_variables_forbidden_p;
15959 /* While we're parsing the default args, we might (due to the
15960 statement expression extension) encounter more classes. We want
15961 to handle them right away, but we don't want them getting mixed
15962 up with default args that are currently in the queue. */
15963 parser->unparsed_functions_queues
15964 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15966 /* Local variable names (and the `this' keyword) may not appear
15967 in a default argument. */
15968 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15969 parser->local_variables_forbidden_p = true;
15971 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15973 parm = TREE_CHAIN (parm))
15975 cp_token_cache *tokens;
15976 tree default_arg = TREE_PURPOSE (parm);
15978 VEC(tree,gc) *insts;
15985 if (TREE_CODE (default_arg) != DEFAULT_ARG)
15986 /* This can happen for a friend declaration for a function
15987 already declared with default arguments. */
15990 /* Push the saved tokens for the default argument onto the parser's
15992 tokens = DEFARG_TOKENS (default_arg);
15993 cp_parser_push_lexer_for_tokens (parser, tokens);
15995 /* Parse the assignment-expression. */
15996 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
15998 if (!processing_template_decl)
15999 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16001 TREE_PURPOSE (parm) = parsed_arg;
16003 /* Update any instantiations we've already created. */
16004 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16005 VEC_iterate (tree, insts, ix, copy); ix++)
16006 TREE_PURPOSE (copy) = parsed_arg;
16008 /* If the token stream has not been completely used up, then
16009 there was extra junk after the end of the default
16011 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16012 cp_parser_error (parser, "expected %<,%>");
16014 /* Revert to the main lexer. */
16015 cp_parser_pop_lexer (parser);
16018 /* Make sure no default arg is missing. */
16019 check_default_args (fn);
16021 /* Restore the state of local_variables_forbidden_p. */
16022 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16024 /* Restore the queue. */
16025 parser->unparsed_functions_queues
16026 = TREE_CHAIN (parser->unparsed_functions_queues);
16029 /* Parse the operand of `sizeof' (or a similar operator). Returns
16030 either a TYPE or an expression, depending on the form of the
16031 input. The KEYWORD indicates which kind of expression we have
16035 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16037 static const char *format;
16038 tree expr = NULL_TREE;
16039 const char *saved_message;
16040 bool saved_integral_constant_expression_p;
16041 bool saved_non_integral_constant_expression_p;
16043 /* Initialize FORMAT the first time we get here. */
16045 format = "types may not be defined in '%s' expressions";
16047 /* Types cannot be defined in a `sizeof' expression. Save away the
16049 saved_message = parser->type_definition_forbidden_message;
16050 /* And create the new one. */
16051 parser->type_definition_forbidden_message
16052 = XNEWVEC (const char, strlen (format)
16053 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16055 sprintf ((char *) parser->type_definition_forbidden_message,
16056 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16058 /* The restrictions on constant-expressions do not apply inside
16059 sizeof expressions. */
16060 saved_integral_constant_expression_p
16061 = parser->integral_constant_expression_p;
16062 saved_non_integral_constant_expression_p
16063 = parser->non_integral_constant_expression_p;
16064 parser->integral_constant_expression_p = false;
16066 /* Do not actually evaluate the expression. */
16068 /* If it's a `(', then we might be looking at the type-id
16070 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16073 bool saved_in_type_id_in_expr_p;
16075 /* We can't be sure yet whether we're looking at a type-id or an
16077 cp_parser_parse_tentatively (parser);
16078 /* Consume the `('. */
16079 cp_lexer_consume_token (parser->lexer);
16080 /* Parse the type-id. */
16081 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16082 parser->in_type_id_in_expr_p = true;
16083 type = cp_parser_type_id (parser);
16084 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16085 /* Now, look for the trailing `)'. */
16086 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16087 /* If all went well, then we're done. */
16088 if (cp_parser_parse_definitely (parser))
16090 cp_decl_specifier_seq decl_specs;
16092 /* Build a trivial decl-specifier-seq. */
16093 clear_decl_specs (&decl_specs);
16094 decl_specs.type = type;
16096 /* Call grokdeclarator to figure out what type this is. */
16097 expr = grokdeclarator (NULL,
16101 /*attrlist=*/NULL);
16105 /* If the type-id production did not work out, then we must be
16106 looking at the unary-expression production. */
16108 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16110 /* Go back to evaluating expressions. */
16113 /* Free the message we created. */
16114 free ((char *) parser->type_definition_forbidden_message);
16115 /* And restore the old one. */
16116 parser->type_definition_forbidden_message = saved_message;
16117 parser->integral_constant_expression_p
16118 = saved_integral_constant_expression_p;
16119 parser->non_integral_constant_expression_p
16120 = saved_non_integral_constant_expression_p;
16125 /* If the current declaration has no declarator, return true. */
16128 cp_parser_declares_only_class_p (cp_parser *parser)
16130 /* If the next token is a `;' or a `,' then there is no
16132 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16133 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16136 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
16139 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
16140 cp_storage_class storage_class)
16142 if (decl_specs->storage_class != sc_none)
16143 decl_specs->multiple_storage_classes_p = true;
16145 decl_specs->storage_class = storage_class;
16148 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16149 is true, the type is a user-defined type; otherwise it is a
16150 built-in type specified by a keyword. */
16153 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16155 bool user_defined_p)
16157 decl_specs->any_specifiers_p = true;
16159 /* If the user tries to redeclare bool or wchar_t (with, for
16160 example, in "typedef int wchar_t;") we remember that this is what
16161 happened. In system headers, we ignore these declarations so
16162 that G++ can work with system headers that are not C++-safe. */
16163 if (decl_specs->specs[(int) ds_typedef]
16165 && (type_spec == boolean_type_node
16166 || type_spec == wchar_type_node)
16167 && (decl_specs->type
16168 || decl_specs->specs[(int) ds_long]
16169 || decl_specs->specs[(int) ds_short]
16170 || decl_specs->specs[(int) ds_unsigned]
16171 || decl_specs->specs[(int) ds_signed]))
16173 decl_specs->redefined_builtin_type = type_spec;
16174 if (!decl_specs->type)
16176 decl_specs->type = type_spec;
16177 decl_specs->user_defined_type_p = false;
16180 else if (decl_specs->type)
16181 decl_specs->multiple_types_p = true;
16184 decl_specs->type = type_spec;
16185 decl_specs->user_defined_type_p = user_defined_p;
16186 decl_specs->redefined_builtin_type = NULL_TREE;
16190 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16191 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16194 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16196 return decl_specifiers->specs[(int) ds_friend] != 0;
16199 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16200 issue an error message indicating that TOKEN_DESC was expected.
16202 Returns the token consumed, if the token had the appropriate type.
16203 Otherwise, returns NULL. */
16206 cp_parser_require (cp_parser* parser,
16207 enum cpp_ttype type,
16208 const char* token_desc)
16210 if (cp_lexer_next_token_is (parser->lexer, type))
16211 return cp_lexer_consume_token (parser->lexer);
16214 /* Output the MESSAGE -- unless we're parsing tentatively. */
16215 if (!cp_parser_simulate_error (parser))
16217 char *message = concat ("expected ", token_desc, NULL);
16218 cp_parser_error (parser, message);
16225 /* Like cp_parser_require, except that tokens will be skipped until
16226 the desired token is found. An error message is still produced if
16227 the next token is not as expected. */
16230 cp_parser_skip_until_found (cp_parser* parser,
16231 enum cpp_ttype type,
16232 const char* token_desc)
16235 unsigned nesting_depth = 0;
16237 if (cp_parser_require (parser, type, token_desc))
16240 /* Skip tokens until the desired token is found. */
16243 /* Peek at the next token. */
16244 token = cp_lexer_peek_token (parser->lexer);
16246 /* If we've reached the token we want, consume it and stop. */
16247 if (token->type == type && !nesting_depth)
16249 cp_lexer_consume_token (parser->lexer);
16253 switch (token->type)
16256 case CPP_PRAGMA_EOL:
16257 /* If we've run out of tokens, stop. */
16260 case CPP_OPEN_BRACE:
16261 case CPP_OPEN_PAREN:
16262 case CPP_OPEN_SQUARE:
16266 case CPP_CLOSE_BRACE:
16267 case CPP_CLOSE_PAREN:
16268 case CPP_CLOSE_SQUARE:
16269 if (nesting_depth-- == 0)
16277 /* Consume this token. */
16278 cp_lexer_consume_token (parser->lexer);
16282 /* If the next token is the indicated keyword, consume it. Otherwise,
16283 issue an error message indicating that TOKEN_DESC was expected.
16285 Returns the token consumed, if the token had the appropriate type.
16286 Otherwise, returns NULL. */
16289 cp_parser_require_keyword (cp_parser* parser,
16291 const char* token_desc)
16293 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16295 if (token && token->keyword != keyword)
16297 dyn_string_t error_msg;
16299 /* Format the error message. */
16300 error_msg = dyn_string_new (0);
16301 dyn_string_append_cstr (error_msg, "expected ");
16302 dyn_string_append_cstr (error_msg, token_desc);
16303 cp_parser_error (parser, error_msg->s);
16304 dyn_string_delete (error_msg);
16311 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16312 function-definition. */
16315 cp_parser_token_starts_function_definition_p (cp_token* token)
16317 return (/* An ordinary function-body begins with an `{'. */
16318 token->type == CPP_OPEN_BRACE
16319 /* A ctor-initializer begins with a `:'. */
16320 || token->type == CPP_COLON
16321 /* A function-try-block begins with `try'. */
16322 || token->keyword == RID_TRY
16323 /* The named return value extension begins with `return'. */
16324 || token->keyword == RID_RETURN);
16327 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16331 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16335 token = cp_lexer_peek_token (parser->lexer);
16336 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16339 /* Returns TRUE iff the next token is the "," or ">" ending a
16340 template-argument. */
16343 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16347 token = cp_lexer_peek_token (parser->lexer);
16348 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16351 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16352 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16355 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16360 token = cp_lexer_peek_nth_token (parser->lexer, n);
16361 if (token->type == CPP_LESS)
16363 /* Check for the sequence `<::' in the original code. It would be lexed as
16364 `[:', where `[' is a digraph, and there is no whitespace before
16366 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16369 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16370 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16376 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16377 or none_type otherwise. */
16379 static enum tag_types
16380 cp_parser_token_is_class_key (cp_token* token)
16382 switch (token->keyword)
16387 return record_type;
16396 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16399 cp_parser_check_class_key (enum tag_types class_key, tree type)
16401 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16402 pedwarn ("%qs tag used in naming %q#T",
16403 class_key == union_type ? "union"
16404 : class_key == record_type ? "struct" : "class",
16408 /* Issue an error message if DECL is redeclared with different
16409 access than its original declaration [class.access.spec/3].
16410 This applies to nested classes and nested class templates.
16414 cp_parser_check_access_in_redeclaration (tree decl)
16416 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16419 if ((TREE_PRIVATE (decl)
16420 != (current_access_specifier == access_private_node))
16421 || (TREE_PROTECTED (decl)
16422 != (current_access_specifier == access_protected_node)))
16423 error ("%qD redeclared with different access", decl);
16426 /* Look for the `template' keyword, as a syntactic disambiguator.
16427 Return TRUE iff it is present, in which case it will be
16431 cp_parser_optional_template_keyword (cp_parser *parser)
16433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16435 /* The `template' keyword can only be used within templates;
16436 outside templates the parser can always figure out what is a
16437 template and what is not. */
16438 if (!processing_template_decl)
16440 error ("%<template%> (as a disambiguator) is only allowed "
16441 "within templates");
16442 /* If this part of the token stream is rescanned, the same
16443 error message would be generated. So, we purge the token
16444 from the stream. */
16445 cp_lexer_purge_token (parser->lexer);
16450 /* Consume the `template' keyword. */
16451 cp_lexer_consume_token (parser->lexer);
16459 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16460 set PARSER->SCOPE, and perform other related actions. */
16463 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16468 /* Get the stored value. */
16469 value = cp_lexer_consume_token (parser->lexer)->value;
16470 /* Perform any access checks that were deferred. */
16471 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16472 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16473 /* Set the scope from the stored value. */
16474 parser->scope = TREE_VALUE (value);
16475 parser->qualifying_scope = TREE_TYPE (value);
16476 parser->object_scope = NULL_TREE;
16479 /* Consume tokens up through a non-nested END token. */
16482 cp_parser_cache_group (cp_parser *parser,
16483 enum cpp_ttype end,
16490 /* Abort a parenthesized expression if we encounter a brace. */
16491 if ((end == CPP_CLOSE_PAREN || depth == 0)
16492 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16494 /* If we've reached the end of the file, stop. */
16495 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16496 || (end != CPP_PRAGMA_EOL
16497 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16499 /* Consume the next token. */
16500 token = cp_lexer_consume_token (parser->lexer);
16501 /* See if it starts a new group. */
16502 if (token->type == CPP_OPEN_BRACE)
16504 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16508 else if (token->type == CPP_OPEN_PAREN)
16509 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16510 else if (token->type == CPP_PRAGMA)
16511 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16512 else if (token->type == end)
16517 /* Begin parsing tentatively. We always save tokens while parsing
16518 tentatively so that if the tentative parsing fails we can restore the
16522 cp_parser_parse_tentatively (cp_parser* parser)
16524 /* Enter a new parsing context. */
16525 parser->context = cp_parser_context_new (parser->context);
16526 /* Begin saving tokens. */
16527 cp_lexer_save_tokens (parser->lexer);
16528 /* In order to avoid repetitive access control error messages,
16529 access checks are queued up until we are no longer parsing
16531 push_deferring_access_checks (dk_deferred);
16534 /* Commit to the currently active tentative parse. */
16537 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16539 cp_parser_context *context;
16542 /* Mark all of the levels as committed. */
16543 lexer = parser->lexer;
16544 for (context = parser->context; context->next; context = context->next)
16546 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16548 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16549 while (!cp_lexer_saving_tokens (lexer))
16550 lexer = lexer->next;
16551 cp_lexer_commit_tokens (lexer);
16555 /* Abort the currently active tentative parse. All consumed tokens
16556 will be rolled back, and no diagnostics will be issued. */
16559 cp_parser_abort_tentative_parse (cp_parser* parser)
16561 cp_parser_simulate_error (parser);
16562 /* Now, pretend that we want to see if the construct was
16563 successfully parsed. */
16564 cp_parser_parse_definitely (parser);
16567 /* Stop parsing tentatively. If a parse error has occurred, restore the
16568 token stream. Otherwise, commit to the tokens we have consumed.
16569 Returns true if no error occurred; false otherwise. */
16572 cp_parser_parse_definitely (cp_parser* parser)
16574 bool error_occurred;
16575 cp_parser_context *context;
16577 /* Remember whether or not an error occurred, since we are about to
16578 destroy that information. */
16579 error_occurred = cp_parser_error_occurred (parser);
16580 /* Remove the topmost context from the stack. */
16581 context = parser->context;
16582 parser->context = context->next;
16583 /* If no parse errors occurred, commit to the tentative parse. */
16584 if (!error_occurred)
16586 /* Commit to the tokens read tentatively, unless that was
16588 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16589 cp_lexer_commit_tokens (parser->lexer);
16591 pop_to_parent_deferring_access_checks ();
16593 /* Otherwise, if errors occurred, roll back our state so that things
16594 are just as they were before we began the tentative parse. */
16597 cp_lexer_rollback_tokens (parser->lexer);
16598 pop_deferring_access_checks ();
16600 /* Add the context to the front of the free list. */
16601 context->next = cp_parser_context_free_list;
16602 cp_parser_context_free_list = context;
16604 return !error_occurred;
16607 /* Returns true if we are parsing tentatively and are not committed to
16608 this tentative parse. */
16611 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16613 return (cp_parser_parsing_tentatively (parser)
16614 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16617 /* Returns nonzero iff an error has occurred during the most recent
16618 tentative parse. */
16621 cp_parser_error_occurred (cp_parser* parser)
16623 return (cp_parser_parsing_tentatively (parser)
16624 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16627 /* Returns nonzero if GNU extensions are allowed. */
16630 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16632 return parser->allow_gnu_extensions_p;
16635 /* Objective-C++ Productions */
16638 /* Parse an Objective-C expression, which feeds into a primary-expression
16642 objc-message-expression
16643 objc-string-literal
16644 objc-encode-expression
16645 objc-protocol-expression
16646 objc-selector-expression
16648 Returns a tree representation of the expression. */
16651 cp_parser_objc_expression (cp_parser* parser)
16653 /* Try to figure out what kind of declaration is present. */
16654 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16658 case CPP_OPEN_SQUARE:
16659 return cp_parser_objc_message_expression (parser);
16661 case CPP_OBJC_STRING:
16662 kwd = cp_lexer_consume_token (parser->lexer);
16663 return objc_build_string_object (kwd->value);
16666 switch (kwd->keyword)
16668 case RID_AT_ENCODE:
16669 return cp_parser_objc_encode_expression (parser);
16671 case RID_AT_PROTOCOL:
16672 return cp_parser_objc_protocol_expression (parser);
16674 case RID_AT_SELECTOR:
16675 return cp_parser_objc_selector_expression (parser);
16681 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16682 cp_parser_skip_to_end_of_block_or_statement (parser);
16685 return error_mark_node;
16688 /* Parse an Objective-C message expression.
16690 objc-message-expression:
16691 [ objc-message-receiver objc-message-args ]
16693 Returns a representation of an Objective-C message. */
16696 cp_parser_objc_message_expression (cp_parser* parser)
16698 tree receiver, messageargs;
16700 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16701 receiver = cp_parser_objc_message_receiver (parser);
16702 messageargs = cp_parser_objc_message_args (parser);
16703 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16705 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16708 /* Parse an objc-message-receiver.
16710 objc-message-receiver:
16712 simple-type-specifier
16714 Returns a representation of the type or expression. */
16717 cp_parser_objc_message_receiver (cp_parser* parser)
16721 /* An Objective-C message receiver may be either (1) a type
16722 or (2) an expression. */
16723 cp_parser_parse_tentatively (parser);
16724 rcv = cp_parser_expression (parser, false);
16726 if (cp_parser_parse_definitely (parser))
16729 rcv = cp_parser_simple_type_specifier (parser,
16730 /*decl_specs=*/NULL,
16731 CP_PARSER_FLAGS_NONE);
16733 return objc_get_class_reference (rcv);
16736 /* Parse the arguments and selectors comprising an Objective-C message.
16741 objc-selector-args , objc-comma-args
16743 objc-selector-args:
16744 objc-selector [opt] : assignment-expression
16745 objc-selector-args objc-selector [opt] : assignment-expression
16748 assignment-expression
16749 objc-comma-args , assignment-expression
16751 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16752 selector arguments and TREE_VALUE containing a list of comma
16756 cp_parser_objc_message_args (cp_parser* parser)
16758 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16759 bool maybe_unary_selector_p = true;
16760 cp_token *token = cp_lexer_peek_token (parser->lexer);
16762 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16764 tree selector = NULL_TREE, arg;
16766 if (token->type != CPP_COLON)
16767 selector = cp_parser_objc_selector (parser);
16769 /* Detect if we have a unary selector. */
16770 if (maybe_unary_selector_p
16771 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16772 return build_tree_list (selector, NULL_TREE);
16774 maybe_unary_selector_p = false;
16775 cp_parser_require (parser, CPP_COLON, "`:'");
16776 arg = cp_parser_assignment_expression (parser, false);
16779 = chainon (sel_args,
16780 build_tree_list (selector, arg));
16782 token = cp_lexer_peek_token (parser->lexer);
16785 /* Handle non-selector arguments, if any. */
16786 while (token->type == CPP_COMMA)
16790 cp_lexer_consume_token (parser->lexer);
16791 arg = cp_parser_assignment_expression (parser, false);
16794 = chainon (addl_args,
16795 build_tree_list (NULL_TREE, arg));
16797 token = cp_lexer_peek_token (parser->lexer);
16800 return build_tree_list (sel_args, addl_args);
16803 /* Parse an Objective-C encode expression.
16805 objc-encode-expression:
16806 @encode objc-typename
16808 Returns an encoded representation of the type argument. */
16811 cp_parser_objc_encode_expression (cp_parser* parser)
16815 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16816 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16817 type = complete_type (cp_parser_type_id (parser));
16818 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16822 error ("%<@encode%> must specify a type as an argument");
16823 return error_mark_node;
16826 return objc_build_encode_expr (type);
16829 /* Parse an Objective-C @defs expression. */
16832 cp_parser_objc_defs_expression (cp_parser *parser)
16836 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16837 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16838 name = cp_parser_identifier (parser);
16839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16841 return objc_get_class_ivars (name);
16844 /* Parse an Objective-C protocol expression.
16846 objc-protocol-expression:
16847 @protocol ( identifier )
16849 Returns a representation of the protocol expression. */
16852 cp_parser_objc_protocol_expression (cp_parser* parser)
16856 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16857 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16858 proto = cp_parser_identifier (parser);
16859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16861 return objc_build_protocol_expr (proto);
16864 /* Parse an Objective-C selector expression.
16866 objc-selector-expression:
16867 @selector ( objc-method-signature )
16869 objc-method-signature:
16875 objc-selector-seq objc-selector :
16877 Returns a representation of the method selector. */
16880 cp_parser_objc_selector_expression (cp_parser* parser)
16882 tree sel_seq = NULL_TREE;
16883 bool maybe_unary_selector_p = true;
16886 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16887 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16888 token = cp_lexer_peek_token (parser->lexer);
16890 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16891 || token->type == CPP_SCOPE)
16893 tree selector = NULL_TREE;
16895 if (token->type != CPP_COLON
16896 || token->type == CPP_SCOPE)
16897 selector = cp_parser_objc_selector (parser);
16899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16900 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16902 /* Detect if we have a unary selector. */
16903 if (maybe_unary_selector_p)
16905 sel_seq = selector;
16906 goto finish_selector;
16910 cp_parser_error (parser, "expected %<:%>");
16913 maybe_unary_selector_p = false;
16914 token = cp_lexer_consume_token (parser->lexer);
16916 if (token->type == CPP_SCOPE)
16919 = chainon (sel_seq,
16920 build_tree_list (selector, NULL_TREE));
16922 = chainon (sel_seq,
16923 build_tree_list (NULL_TREE, NULL_TREE));
16927 = chainon (sel_seq,
16928 build_tree_list (selector, NULL_TREE));
16930 token = cp_lexer_peek_token (parser->lexer);
16934 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16936 return objc_build_selector_expr (sel_seq);
16939 /* Parse a list of identifiers.
16941 objc-identifier-list:
16943 objc-identifier-list , identifier
16945 Returns a TREE_LIST of identifier nodes. */
16948 cp_parser_objc_identifier_list (cp_parser* parser)
16950 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16951 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16953 while (sep->type == CPP_COMMA)
16955 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16956 list = chainon (list,
16957 build_tree_list (NULL_TREE,
16958 cp_parser_identifier (parser)));
16959 sep = cp_lexer_peek_token (parser->lexer);
16965 /* Parse an Objective-C alias declaration.
16967 objc-alias-declaration:
16968 @compatibility_alias identifier identifier ;
16970 This function registers the alias mapping with the Objective-C front-end.
16971 It returns nothing. */
16974 cp_parser_objc_alias_declaration (cp_parser* parser)
16978 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
16979 alias = cp_parser_identifier (parser);
16980 orig = cp_parser_identifier (parser);
16981 objc_declare_alias (alias, orig);
16982 cp_parser_consume_semicolon_at_end_of_statement (parser);
16985 /* Parse an Objective-C class forward-declaration.
16987 objc-class-declaration:
16988 @class objc-identifier-list ;
16990 The function registers the forward declarations with the Objective-C
16991 front-end. It returns nothing. */
16994 cp_parser_objc_class_declaration (cp_parser* parser)
16996 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
16997 objc_declare_class (cp_parser_objc_identifier_list (parser));
16998 cp_parser_consume_semicolon_at_end_of_statement (parser);
17001 /* Parse a list of Objective-C protocol references.
17003 objc-protocol-refs-opt:
17004 objc-protocol-refs [opt]
17006 objc-protocol-refs:
17007 < objc-identifier-list >
17009 Returns a TREE_LIST of identifiers, if any. */
17012 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17014 tree protorefs = NULL_TREE;
17016 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17018 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17019 protorefs = cp_parser_objc_identifier_list (parser);
17020 cp_parser_require (parser, CPP_GREATER, "`>'");
17026 /* Parse a Objective-C visibility specification. */
17029 cp_parser_objc_visibility_spec (cp_parser* parser)
17031 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17033 switch (vis->keyword)
17035 case RID_AT_PRIVATE:
17036 objc_set_visibility (2);
17038 case RID_AT_PROTECTED:
17039 objc_set_visibility (0);
17041 case RID_AT_PUBLIC:
17042 objc_set_visibility (1);
17048 /* Eat '@private'/'@protected'/'@public'. */
17049 cp_lexer_consume_token (parser->lexer);
17052 /* Parse an Objective-C method type. */
17055 cp_parser_objc_method_type (cp_parser* parser)
17057 objc_set_method_type
17058 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17063 /* Parse an Objective-C protocol qualifier. */
17066 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17068 tree quals = NULL_TREE, node;
17069 cp_token *token = cp_lexer_peek_token (parser->lexer);
17071 node = token->value;
17073 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17074 && (node == ridpointers [(int) RID_IN]
17075 || node == ridpointers [(int) RID_OUT]
17076 || node == ridpointers [(int) RID_INOUT]
17077 || node == ridpointers [(int) RID_BYCOPY]
17078 || node == ridpointers [(int) RID_BYREF]
17079 || node == ridpointers [(int) RID_ONEWAY]))
17081 quals = tree_cons (NULL_TREE, node, quals);
17082 cp_lexer_consume_token (parser->lexer);
17083 token = cp_lexer_peek_token (parser->lexer);
17084 node = token->value;
17090 /* Parse an Objective-C typename. */
17093 cp_parser_objc_typename (cp_parser* parser)
17095 tree typename = NULL_TREE;
17097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17099 tree proto_quals, cp_type = NULL_TREE;
17101 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17102 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17104 /* An ObjC type name may consist of just protocol qualifiers, in which
17105 case the type shall default to 'id'. */
17106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17107 cp_type = cp_parser_type_id (parser);
17109 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17110 typename = build_tree_list (proto_quals, cp_type);
17116 /* Check to see if TYPE refers to an Objective-C selector name. */
17119 cp_parser_objc_selector_p (enum cpp_ttype type)
17121 return (type == CPP_NAME || type == CPP_KEYWORD
17122 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17123 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17124 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17125 || type == CPP_XOR || type == CPP_XOR_EQ);
17128 /* Parse an Objective-C selector. */
17131 cp_parser_objc_selector (cp_parser* parser)
17133 cp_token *token = cp_lexer_consume_token (parser->lexer);
17135 if (!cp_parser_objc_selector_p (token->type))
17137 error ("invalid Objective-C++ selector name");
17138 return error_mark_node;
17141 /* C++ operator names are allowed to appear in ObjC selectors. */
17142 switch (token->type)
17144 case CPP_AND_AND: return get_identifier ("and");
17145 case CPP_AND_EQ: return get_identifier ("and_eq");
17146 case CPP_AND: return get_identifier ("bitand");
17147 case CPP_OR: return get_identifier ("bitor");
17148 case CPP_COMPL: return get_identifier ("compl");
17149 case CPP_NOT: return get_identifier ("not");
17150 case CPP_NOT_EQ: return get_identifier ("not_eq");
17151 case CPP_OR_OR: return get_identifier ("or");
17152 case CPP_OR_EQ: return get_identifier ("or_eq");
17153 case CPP_XOR: return get_identifier ("xor");
17154 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17155 default: return token->value;
17159 /* Parse an Objective-C params list. */
17162 cp_parser_objc_method_keyword_params (cp_parser* parser)
17164 tree params = NULL_TREE;
17165 bool maybe_unary_selector_p = true;
17166 cp_token *token = cp_lexer_peek_token (parser->lexer);
17168 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17170 tree selector = NULL_TREE, typename, identifier;
17172 if (token->type != CPP_COLON)
17173 selector = cp_parser_objc_selector (parser);
17175 /* Detect if we have a unary selector. */
17176 if (maybe_unary_selector_p
17177 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17180 maybe_unary_selector_p = false;
17181 cp_parser_require (parser, CPP_COLON, "`:'");
17182 typename = cp_parser_objc_typename (parser);
17183 identifier = cp_parser_identifier (parser);
17187 objc_build_keyword_decl (selector,
17191 token = cp_lexer_peek_token (parser->lexer);
17197 /* Parse the non-keyword Objective-C params. */
17200 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17202 tree params = make_node (TREE_LIST);
17203 cp_token *token = cp_lexer_peek_token (parser->lexer);
17204 *ellipsisp = false; /* Initially, assume no ellipsis. */
17206 while (token->type == CPP_COMMA)
17208 cp_parameter_declarator *parmdecl;
17211 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17212 token = cp_lexer_peek_token (parser->lexer);
17214 if (token->type == CPP_ELLIPSIS)
17216 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17221 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17222 parm = grokdeclarator (parmdecl->declarator,
17223 &parmdecl->decl_specifiers,
17224 PARM, /*initialized=*/0,
17225 /*attrlist=*/NULL);
17227 chainon (params, build_tree_list (NULL_TREE, parm));
17228 token = cp_lexer_peek_token (parser->lexer);
17234 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17237 cp_parser_objc_interstitial_code (cp_parser* parser)
17239 cp_token *token = cp_lexer_peek_token (parser->lexer);
17241 /* If the next token is `extern' and the following token is a string
17242 literal, then we have a linkage specification. */
17243 if (token->keyword == RID_EXTERN
17244 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17245 cp_parser_linkage_specification (parser);
17246 /* Handle #pragma, if any. */
17247 else if (token->type == CPP_PRAGMA)
17248 cp_parser_pragma (parser, pragma_external);
17249 /* Allow stray semicolons. */
17250 else if (token->type == CPP_SEMICOLON)
17251 cp_lexer_consume_token (parser->lexer);
17252 /* Finally, try to parse a block-declaration, or a function-definition. */
17254 cp_parser_block_declaration (parser, /*statement_p=*/false);
17257 /* Parse a method signature. */
17260 cp_parser_objc_method_signature (cp_parser* parser)
17262 tree rettype, kwdparms, optparms;
17263 bool ellipsis = false;
17265 cp_parser_objc_method_type (parser);
17266 rettype = cp_parser_objc_typename (parser);
17267 kwdparms = cp_parser_objc_method_keyword_params (parser);
17268 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17270 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17273 /* Pars an Objective-C method prototype list. */
17276 cp_parser_objc_method_prototype_list (cp_parser* parser)
17278 cp_token *token = cp_lexer_peek_token (parser->lexer);
17280 while (token->keyword != RID_AT_END)
17282 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17284 objc_add_method_declaration
17285 (cp_parser_objc_method_signature (parser));
17286 cp_parser_consume_semicolon_at_end_of_statement (parser);
17289 /* Allow for interspersed non-ObjC++ code. */
17290 cp_parser_objc_interstitial_code (parser);
17292 token = cp_lexer_peek_token (parser->lexer);
17295 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17296 objc_finish_interface ();
17299 /* Parse an Objective-C method definition list. */
17302 cp_parser_objc_method_definition_list (cp_parser* parser)
17304 cp_token *token = cp_lexer_peek_token (parser->lexer);
17306 while (token->keyword != RID_AT_END)
17310 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17312 push_deferring_access_checks (dk_deferred);
17313 objc_start_method_definition
17314 (cp_parser_objc_method_signature (parser));
17316 /* For historical reasons, we accept an optional semicolon. */
17317 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17318 cp_lexer_consume_token (parser->lexer);
17320 perform_deferred_access_checks ();
17321 stop_deferring_access_checks ();
17322 meth = cp_parser_function_definition_after_declarator (parser,
17324 pop_deferring_access_checks ();
17325 objc_finish_method_definition (meth);
17328 /* Allow for interspersed non-ObjC++ code. */
17329 cp_parser_objc_interstitial_code (parser);
17331 token = cp_lexer_peek_token (parser->lexer);
17334 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17335 objc_finish_implementation ();
17338 /* Parse Objective-C ivars. */
17341 cp_parser_objc_class_ivars (cp_parser* parser)
17343 cp_token *token = cp_lexer_peek_token (parser->lexer);
17345 if (token->type != CPP_OPEN_BRACE)
17346 return; /* No ivars specified. */
17348 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17349 token = cp_lexer_peek_token (parser->lexer);
17351 while (token->type != CPP_CLOSE_BRACE)
17353 cp_decl_specifier_seq declspecs;
17354 int decl_class_or_enum_p;
17355 tree prefix_attributes;
17357 cp_parser_objc_visibility_spec (parser);
17359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17362 cp_parser_decl_specifier_seq (parser,
17363 CP_PARSER_FLAGS_OPTIONAL,
17365 &decl_class_or_enum_p);
17366 prefix_attributes = declspecs.attributes;
17367 declspecs.attributes = NULL_TREE;
17369 /* Keep going until we hit the `;' at the end of the
17371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17373 tree width = NULL_TREE, attributes, first_attribute, decl;
17374 cp_declarator *declarator = NULL;
17375 int ctor_dtor_or_conv_p;
17377 /* Check for a (possibly unnamed) bitfield declaration. */
17378 token = cp_lexer_peek_token (parser->lexer);
17379 if (token->type == CPP_COLON)
17382 if (token->type == CPP_NAME
17383 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17386 /* Get the name of the bitfield. */
17387 declarator = make_id_declarator (NULL_TREE,
17388 cp_parser_identifier (parser),
17392 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17393 /* Get the width of the bitfield. */
17395 = cp_parser_constant_expression (parser,
17396 /*allow_non_constant=*/false,
17401 /* Parse the declarator. */
17403 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17404 &ctor_dtor_or_conv_p,
17405 /*parenthesized_p=*/NULL,
17406 /*member_p=*/false);
17409 /* Look for attributes that apply to the ivar. */
17410 attributes = cp_parser_attributes_opt (parser);
17411 /* Remember which attributes are prefix attributes and
17413 first_attribute = attributes;
17414 /* Combine the attributes. */
17415 attributes = chainon (prefix_attributes, attributes);
17419 /* Create the bitfield declaration. */
17420 decl = grokbitfield (declarator, &declspecs, width);
17421 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17424 decl = grokfield (declarator, &declspecs,
17425 NULL_TREE, /*init_const_expr_p=*/false,
17426 NULL_TREE, attributes);
17428 /* Add the instance variable. */
17429 objc_add_instance_variable (decl);
17431 /* Reset PREFIX_ATTRIBUTES. */
17432 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17433 attributes = TREE_CHAIN (attributes);
17435 TREE_CHAIN (attributes) = NULL_TREE;
17437 token = cp_lexer_peek_token (parser->lexer);
17439 if (token->type == CPP_COMMA)
17441 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17447 cp_parser_consume_semicolon_at_end_of_statement (parser);
17448 token = cp_lexer_peek_token (parser->lexer);
17451 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17452 /* For historical reasons, we accept an optional semicolon. */
17453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17454 cp_lexer_consume_token (parser->lexer);
17457 /* Parse an Objective-C protocol declaration. */
17460 cp_parser_objc_protocol_declaration (cp_parser* parser)
17462 tree proto, protorefs;
17465 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17468 error ("identifier expected after %<@protocol%>");
17472 /* See if we have a forward declaration or a definition. */
17473 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17475 /* Try a forward declaration first. */
17476 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17478 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17480 cp_parser_consume_semicolon_at_end_of_statement (parser);
17483 /* Ok, we got a full-fledged definition (or at least should). */
17486 proto = cp_parser_identifier (parser);
17487 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17488 objc_start_protocol (proto, protorefs);
17489 cp_parser_objc_method_prototype_list (parser);
17493 /* Parse an Objective-C superclass or category. */
17496 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17499 cp_token *next = cp_lexer_peek_token (parser->lexer);
17501 *super = *categ = NULL_TREE;
17502 if (next->type == CPP_COLON)
17504 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17505 *super = cp_parser_identifier (parser);
17507 else if (next->type == CPP_OPEN_PAREN)
17509 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17510 *categ = cp_parser_identifier (parser);
17511 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17515 /* Parse an Objective-C class interface. */
17518 cp_parser_objc_class_interface (cp_parser* parser)
17520 tree name, super, categ, protos;
17522 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17523 name = cp_parser_identifier (parser);
17524 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17525 protos = cp_parser_objc_protocol_refs_opt (parser);
17527 /* We have either a class or a category on our hands. */
17529 objc_start_category_interface (name, categ, protos);
17532 objc_start_class_interface (name, super, protos);
17533 /* Handle instance variable declarations, if any. */
17534 cp_parser_objc_class_ivars (parser);
17535 objc_continue_interface ();
17538 cp_parser_objc_method_prototype_list (parser);
17541 /* Parse an Objective-C class implementation. */
17544 cp_parser_objc_class_implementation (cp_parser* parser)
17546 tree name, super, categ;
17548 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17549 name = cp_parser_identifier (parser);
17550 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17552 /* We have either a class or a category on our hands. */
17554 objc_start_category_implementation (name, categ);
17557 objc_start_class_implementation (name, super);
17558 /* Handle instance variable declarations, if any. */
17559 cp_parser_objc_class_ivars (parser);
17560 objc_continue_implementation ();
17563 cp_parser_objc_method_definition_list (parser);
17566 /* Consume the @end token and finish off the implementation. */
17569 cp_parser_objc_end_implementation (cp_parser* parser)
17571 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17572 objc_finish_implementation ();
17575 /* Parse an Objective-C declaration. */
17578 cp_parser_objc_declaration (cp_parser* parser)
17580 /* Try to figure out what kind of declaration is present. */
17581 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17583 switch (kwd->keyword)
17586 cp_parser_objc_alias_declaration (parser);
17589 cp_parser_objc_class_declaration (parser);
17591 case RID_AT_PROTOCOL:
17592 cp_parser_objc_protocol_declaration (parser);
17594 case RID_AT_INTERFACE:
17595 cp_parser_objc_class_interface (parser);
17597 case RID_AT_IMPLEMENTATION:
17598 cp_parser_objc_class_implementation (parser);
17601 cp_parser_objc_end_implementation (parser);
17604 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17605 cp_parser_skip_to_end_of_block_or_statement (parser);
17609 /* Parse an Objective-C try-catch-finally statement.
17611 objc-try-catch-finally-stmt:
17612 @try compound-statement objc-catch-clause-seq [opt]
17613 objc-finally-clause [opt]
17615 objc-catch-clause-seq:
17616 objc-catch-clause objc-catch-clause-seq [opt]
17619 @catch ( exception-declaration ) compound-statement
17621 objc-finally-clause
17622 @finally compound-statement
17624 Returns NULL_TREE. */
17627 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17628 location_t location;
17631 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17632 location = cp_lexer_peek_token (parser->lexer)->location;
17633 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17634 node, lest it get absorbed into the surrounding block. */
17635 stmt = push_stmt_list ();
17636 cp_parser_compound_statement (parser, NULL, false);
17637 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17639 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17641 cp_parameter_declarator *parmdecl;
17644 cp_lexer_consume_token (parser->lexer);
17645 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17646 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17647 parm = grokdeclarator (parmdecl->declarator,
17648 &parmdecl->decl_specifiers,
17649 PARM, /*initialized=*/0,
17650 /*attrlist=*/NULL);
17651 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17652 objc_begin_catch_clause (parm);
17653 cp_parser_compound_statement (parser, NULL, false);
17654 objc_finish_catch_clause ();
17657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17659 cp_lexer_consume_token (parser->lexer);
17660 location = cp_lexer_peek_token (parser->lexer)->location;
17661 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17662 node, lest it get absorbed into the surrounding block. */
17663 stmt = push_stmt_list ();
17664 cp_parser_compound_statement (parser, NULL, false);
17665 objc_build_finally_clause (location, pop_stmt_list (stmt));
17668 return objc_finish_try_stmt ();
17671 /* Parse an Objective-C synchronized statement.
17673 objc-synchronized-stmt:
17674 @synchronized ( expression ) compound-statement
17676 Returns NULL_TREE. */
17679 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17680 location_t location;
17683 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17685 location = cp_lexer_peek_token (parser->lexer)->location;
17686 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17687 lock = cp_parser_expression (parser, false);
17688 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17690 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17691 node, lest it get absorbed into the surrounding block. */
17692 stmt = push_stmt_list ();
17693 cp_parser_compound_statement (parser, NULL, false);
17695 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17698 /* Parse an Objective-C throw statement.
17701 @throw assignment-expression [opt] ;
17703 Returns a constructed '@throw' statement. */
17706 cp_parser_objc_throw_statement (cp_parser *parser) {
17707 tree expr = NULL_TREE;
17709 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17712 expr = cp_parser_assignment_expression (parser, false);
17714 cp_parser_consume_semicolon_at_end_of_statement (parser);
17716 return objc_build_throw_stmt (expr);
17719 /* Parse an Objective-C statement. */
17722 cp_parser_objc_statement (cp_parser * parser) {
17723 /* Try to figure out what kind of declaration is present. */
17724 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17726 switch (kwd->keyword)
17729 return cp_parser_objc_try_catch_finally_statement (parser);
17730 case RID_AT_SYNCHRONIZED:
17731 return cp_parser_objc_synchronized_statement (parser);
17733 return cp_parser_objc_throw_statement (parser);
17735 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17736 cp_parser_skip_to_end_of_block_or_statement (parser);
17739 return error_mark_node;
17742 /* OpenMP 2.5 parsing routines. */
17744 /* All OpenMP clauses. OpenMP 2.5. */
17745 typedef enum pragma_omp_clause {
17746 PRAGMA_OMP_CLAUSE_NONE = 0,
17748 PRAGMA_OMP_CLAUSE_COPYIN,
17749 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17750 PRAGMA_OMP_CLAUSE_DEFAULT,
17751 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17752 PRAGMA_OMP_CLAUSE_IF,
17753 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17754 PRAGMA_OMP_CLAUSE_NOWAIT,
17755 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17756 PRAGMA_OMP_CLAUSE_ORDERED,
17757 PRAGMA_OMP_CLAUSE_PRIVATE,
17758 PRAGMA_OMP_CLAUSE_REDUCTION,
17759 PRAGMA_OMP_CLAUSE_SCHEDULE,
17760 PRAGMA_OMP_CLAUSE_SHARED
17761 } pragma_omp_clause;
17763 /* Returns name of the next clause.
17764 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17765 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17766 returned and the token is consumed. */
17768 static pragma_omp_clause
17769 cp_parser_omp_clause_name (cp_parser *parser)
17771 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17774 result = PRAGMA_OMP_CLAUSE_IF;
17775 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17776 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17777 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17778 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17779 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17781 tree id = cp_lexer_peek_token (parser->lexer)->value;
17782 const char *p = IDENTIFIER_POINTER (id);
17787 if (!strcmp ("copyin", p))
17788 result = PRAGMA_OMP_CLAUSE_COPYIN;
17789 else if (!strcmp ("copyprivate", p))
17790 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17793 if (!strcmp ("firstprivate", p))
17794 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17797 if (!strcmp ("lastprivate", p))
17798 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17801 if (!strcmp ("nowait", p))
17802 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17803 else if (!strcmp ("num_threads", p))
17804 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17807 if (!strcmp ("ordered", p))
17808 result = PRAGMA_OMP_CLAUSE_ORDERED;
17811 if (!strcmp ("reduction", p))
17812 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17815 if (!strcmp ("schedule", p))
17816 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17817 else if (!strcmp ("shared", p))
17818 result = PRAGMA_OMP_CLAUSE_SHARED;
17823 if (result != PRAGMA_OMP_CLAUSE_NONE)
17824 cp_lexer_consume_token (parser->lexer);
17829 /* Validate that a clause of the given type does not already exist. */
17832 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17836 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17837 if (OMP_CLAUSE_CODE (c) == code)
17839 error ("too many %qs clauses", name);
17847 variable-list , identifier
17849 In addition, we match a closing parenthesis. An opening parenthesis
17850 will have been consumed by the caller.
17852 If KIND is nonzero, create the appropriate node and install the decl
17853 in OMP_CLAUSE_DECL and add the node to the head of the list.
17855 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17856 return the list created. */
17859 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17866 name = cp_parser_id_expression (parser, /*template_p=*/false,
17867 /*check_dependency_p=*/true,
17868 /*template_p=*/NULL,
17869 /*declarator_p=*/false,
17870 /*optional_p=*/false);
17871 if (name == error_mark_node)
17874 decl = cp_parser_lookup_name_simple (parser, name);
17875 if (decl == error_mark_node)
17876 cp_parser_name_lookup_error (parser, name, decl, NULL);
17877 else if (kind != 0)
17879 tree u = build_omp_clause (kind);
17880 OMP_CLAUSE_DECL (u) = decl;
17881 OMP_CLAUSE_CHAIN (u) = list;
17885 list = tree_cons (decl, NULL_TREE, list);
17888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17890 cp_lexer_consume_token (parser->lexer);
17893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17897 /* Try to resync to an unnested comma. Copied from
17898 cp_parser_parenthesized_expression_list. */
17900 ending = cp_parser_skip_to_closing_parenthesis (parser,
17901 /*recovering=*/true,
17903 /*consume_paren=*/true);
17911 /* Similarly, but expect leading and trailing parenthesis. This is a very
17912 common case for omp clauses. */
17915 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17917 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17918 return cp_parser_omp_var_list_no_open (parser, kind, list);
17923 default ( shared | none ) */
17926 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17928 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17933 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17935 tree id = cp_lexer_peek_token (parser->lexer)->value;
17936 const char *p = IDENTIFIER_POINTER (id);
17941 if (strcmp ("none", p) != 0)
17943 kind = OMP_CLAUSE_DEFAULT_NONE;
17947 if (strcmp ("shared", p) != 0)
17949 kind = OMP_CLAUSE_DEFAULT_SHARED;
17956 cp_lexer_consume_token (parser->lexer);
17961 cp_parser_error (parser, "expected %<none%> or %<shared%>");
17964 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17966 /*or_comma=*/false,
17967 /*consume_paren=*/true);
17969 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
17972 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
17973 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
17974 OMP_CLAUSE_CHAIN (c) = list;
17975 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
17981 if ( expression ) */
17984 cp_parser_omp_clause_if (cp_parser *parser, tree list)
17988 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17991 t = cp_parser_condition (parser);
17993 if (t == error_mark_node
17994 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
17996 /*or_comma=*/false,
17997 /*consume_paren=*/true);
17999 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18001 c = build_omp_clause (OMP_CLAUSE_IF);
18002 OMP_CLAUSE_IF_EXPR (c) = t;
18003 OMP_CLAUSE_CHAIN (c) = list;
18012 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18016 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18018 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18019 OMP_CLAUSE_CHAIN (c) = list;
18024 num_threads ( expression ) */
18027 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18031 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18034 t = cp_parser_expression (parser, false);
18036 if (t == error_mark_node
18037 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18039 /*or_comma=*/false,
18040 /*consume_paren=*/true);
18042 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18044 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18045 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18046 OMP_CLAUSE_CHAIN (c) = list;
18055 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18059 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18061 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18062 OMP_CLAUSE_CHAIN (c) = list;
18067 reduction ( reduction-operator : variable-list )
18069 reduction-operator:
18070 One of: + * - & ^ | && || */
18073 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18075 enum tree_code code;
18078 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18081 switch (cp_lexer_peek_token (parser->lexer)->type)
18093 code = BIT_AND_EXPR;
18096 code = BIT_XOR_EXPR;
18099 code = BIT_IOR_EXPR;
18102 code = TRUTH_ANDIF_EXPR;
18105 code = TRUTH_ORIF_EXPR;
18108 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18111 /*or_comma=*/false,
18112 /*consume_paren=*/true);
18115 cp_lexer_consume_token (parser->lexer);
18117 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18120 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18121 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18122 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18128 schedule ( schedule-kind )
18129 schedule ( schedule-kind , expression )
18132 static | dynamic | guided | runtime
18136 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18140 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18143 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18145 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18147 tree id = cp_lexer_peek_token (parser->lexer)->value;
18148 const char *p = IDENTIFIER_POINTER (id);
18153 if (strcmp ("dynamic", p) != 0)
18155 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18159 if (strcmp ("guided", p) != 0)
18161 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18165 if (strcmp ("runtime", p) != 0)
18167 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18174 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18175 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18178 cp_lexer_consume_token (parser->lexer);
18180 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18182 cp_lexer_consume_token (parser->lexer);
18184 t = cp_parser_assignment_expression (parser, false);
18186 if (t == error_mark_node)
18188 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18189 error ("schedule %<runtime%> does not take "
18190 "a %<chunk_size%> parameter");
18192 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18197 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18200 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18201 OMP_CLAUSE_CHAIN (c) = list;
18205 cp_parser_error (parser, "invalid schedule kind");
18207 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18208 /*or_comma=*/false,
18209 /*consume_paren=*/true);
18213 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18214 is a bitmask in MASK. Return the list of clauses found; the result
18215 of clause default goes in *pdefault. */
18218 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18219 const char *where, cp_token *pragma_tok)
18221 tree clauses = NULL;
18223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18225 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18226 const char *c_name;
18227 tree prev = clauses;
18231 case PRAGMA_OMP_CLAUSE_COPYIN:
18232 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18235 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18236 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18238 c_name = "copyprivate";
18240 case PRAGMA_OMP_CLAUSE_DEFAULT:
18241 clauses = cp_parser_omp_clause_default (parser, clauses);
18242 c_name = "default";
18244 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18245 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18247 c_name = "firstprivate";
18249 case PRAGMA_OMP_CLAUSE_IF:
18250 clauses = cp_parser_omp_clause_if (parser, clauses);
18253 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18254 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18256 c_name = "lastprivate";
18258 case PRAGMA_OMP_CLAUSE_NOWAIT:
18259 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18262 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18263 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18264 c_name = "num_threads";
18266 case PRAGMA_OMP_CLAUSE_ORDERED:
18267 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18268 c_name = "ordered";
18270 case PRAGMA_OMP_CLAUSE_PRIVATE:
18271 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18273 c_name = "private";
18275 case PRAGMA_OMP_CLAUSE_REDUCTION:
18276 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18277 c_name = "reduction";
18279 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18280 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18281 c_name = "schedule";
18283 case PRAGMA_OMP_CLAUSE_SHARED:
18284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18289 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18293 if (((mask >> c_kind) & 1) == 0)
18295 /* Remove the invalid clause(s) from the list to avoid
18296 confusing the rest of the compiler. */
18298 error ("%qs is not valid for %qs", c_name, where);
18302 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18303 return finish_omp_clauses (clauses);
18310 In practice, we're also interested in adding the statement to an
18311 outer node. So it is convenient if we work around the fact that
18312 cp_parser_statement calls add_stmt. */
18315 cp_parser_begin_omp_structured_block (cp_parser *parser)
18317 unsigned save = parser->in_statement;
18319 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18320 This preserves the "not within loop or switch" style error messages
18321 for nonsense cases like
18327 if (parser->in_statement)
18328 parser->in_statement = IN_OMP_BLOCK;
18334 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18336 parser->in_statement = save;
18340 cp_parser_omp_structured_block (cp_parser *parser)
18342 tree stmt = begin_omp_structured_block ();
18343 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18345 cp_parser_statement (parser, NULL_TREE, false);
18347 cp_parser_end_omp_structured_block (parser, save);
18348 return finish_omp_structured_block (stmt);
18352 # pragma omp atomic new-line
18356 x binop= expr | x++ | ++x | x-- | --x
18358 +, *, -, /, &, ^, |, <<, >>
18360 where x is an lvalue expression with scalar type. */
18363 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18366 enum tree_code code;
18368 cp_parser_require_pragma_eol (parser, pragma_tok);
18370 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18372 switch (TREE_CODE (lhs))
18377 case PREINCREMENT_EXPR:
18378 case POSTINCREMENT_EXPR:
18379 lhs = TREE_OPERAND (lhs, 0);
18381 rhs = integer_one_node;
18384 case PREDECREMENT_EXPR:
18385 case POSTDECREMENT_EXPR:
18386 lhs = TREE_OPERAND (lhs, 0);
18388 rhs = integer_one_node;
18392 switch (cp_lexer_peek_token (parser->lexer)->type)
18398 code = TRUNC_DIV_EXPR;
18406 case CPP_LSHIFT_EQ:
18407 code = LSHIFT_EXPR;
18409 case CPP_RSHIFT_EQ:
18410 code = RSHIFT_EXPR;
18413 code = BIT_AND_EXPR;
18416 code = BIT_IOR_EXPR;
18419 code = BIT_XOR_EXPR;
18422 cp_parser_error (parser,
18423 "invalid operator for %<#pragma omp atomic%>");
18426 cp_lexer_consume_token (parser->lexer);
18428 rhs = cp_parser_expression (parser, false);
18429 if (rhs == error_mark_node)
18433 finish_omp_atomic (code, lhs, rhs);
18434 cp_parser_consume_semicolon_at_end_of_statement (parser);
18438 cp_parser_skip_to_end_of_block_or_statement (parser);
18443 # pragma omp barrier new-line
18447 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18449 cp_parser_require_pragma_eol (parser, pragma_tok);
18450 finish_omp_barrier ();
18454 # pragma omp critical [(name)] new-line
18459 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18461 tree stmt, name = NULL;
18463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18465 cp_lexer_consume_token (parser->lexer);
18467 name = cp_parser_identifier (parser);
18469 if (name == error_mark_node
18470 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18472 /*or_comma=*/false,
18473 /*consume_paren=*/true);
18474 if (name == error_mark_node)
18477 cp_parser_require_pragma_eol (parser, pragma_tok);
18479 stmt = cp_parser_omp_structured_block (parser);
18480 return c_finish_omp_critical (stmt, name);
18484 # pragma omp flush flush-vars[opt] new-line
18487 ( variable-list ) */
18490 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18493 (void) cp_parser_omp_var_list (parser, 0, NULL);
18494 cp_parser_require_pragma_eol (parser, pragma_tok);
18496 finish_omp_flush ();
18499 /* Parse the restricted form of the for statment allowed by OpenMP. */
18502 cp_parser_omp_for_loop (cp_parser *parser)
18504 tree init, cond, incr, body, decl, pre_body;
18507 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18509 cp_parser_error (parser, "for statement expected");
18512 loc = cp_lexer_consume_token (parser->lexer)->location;
18513 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18516 init = decl = NULL;
18517 pre_body = push_stmt_list ();
18518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18520 cp_decl_specifier_seq type_specifiers;
18522 /* First, try to parse as an initialized declaration. See
18523 cp_parser_condition, from whence the bulk of this is copied. */
18525 cp_parser_parse_tentatively (parser);
18526 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18528 if (!cp_parser_error_occurred (parser))
18530 tree asm_specification, attributes;
18531 cp_declarator *declarator;
18533 declarator = cp_parser_declarator (parser,
18534 CP_PARSER_DECLARATOR_NAMED,
18535 /*ctor_dtor_or_conv_p=*/NULL,
18536 /*parenthesized_p=*/NULL,
18537 /*member_p=*/false);
18538 attributes = cp_parser_attributes_opt (parser);
18539 asm_specification = cp_parser_asm_specification_opt (parser);
18541 cp_parser_require (parser, CPP_EQ, "`='");
18542 if (cp_parser_parse_definitely (parser))
18546 decl = start_decl (declarator, &type_specifiers,
18547 /*initialized_p=*/false, attributes,
18548 /*prefix_attributes=*/NULL_TREE,
18551 init = cp_parser_assignment_expression (parser, false);
18553 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18554 asm_specification, LOOKUP_ONLYCONVERTING);
18557 pop_scope (pushed_scope);
18561 cp_parser_abort_tentative_parse (parser);
18563 /* If parsing as an initialized declaration failed, try again as
18564 a simple expression. */
18566 init = cp_parser_expression (parser, false);
18568 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18569 pre_body = pop_stmt_list (pre_body);
18572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18573 cond = cp_parser_condition (parser);
18574 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18578 incr = cp_parser_expression (parser, false);
18580 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18581 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18582 /*or_comma=*/false,
18583 /*consume_paren=*/true);
18585 /* Note that we saved the original contents of this flag when we entered
18586 the structured block, and so we don't need to re-save it here. */
18587 parser->in_statement = IN_OMP_FOR;
18589 /* Note that the grammar doesn't call for a structured block here,
18590 though the loop as a whole is a structured block. */
18591 body = push_stmt_list ();
18592 cp_parser_statement (parser, NULL_TREE, false);
18593 body = pop_stmt_list (body);
18595 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18599 #pragma omp for for-clause[optseq] new-line
18603 #define OMP_FOR_CLAUSE_MASK \
18604 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18605 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18606 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18607 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18608 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18609 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18610 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18613 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18615 tree clauses, sb, ret;
18618 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18619 "#pragma omp for", pragma_tok);
18621 sb = begin_omp_structured_block ();
18622 save = cp_parser_begin_omp_structured_block (parser);
18624 ret = cp_parser_omp_for_loop (parser);
18626 OMP_FOR_CLAUSES (ret) = clauses;
18628 cp_parser_end_omp_structured_block (parser, save);
18629 add_stmt (finish_omp_structured_block (sb));
18635 # pragma omp master new-line
18640 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18642 cp_parser_require_pragma_eol (parser, pragma_tok);
18643 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18647 # pragma omp ordered new-line
18652 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18654 cp_parser_require_pragma_eol (parser, pragma_tok);
18655 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18661 { section-sequence }
18664 section-directive[opt] structured-block
18665 section-sequence section-directive structured-block */
18668 cp_parser_omp_sections_scope (cp_parser *parser)
18670 tree stmt, substmt;
18671 bool error_suppress = false;
18674 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18677 stmt = push_stmt_list ();
18679 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18683 substmt = begin_omp_structured_block ();
18684 save = cp_parser_begin_omp_structured_block (parser);
18688 cp_parser_statement (parser, NULL_TREE, false);
18690 tok = cp_lexer_peek_token (parser->lexer);
18691 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18693 if (tok->type == CPP_CLOSE_BRACE)
18695 if (tok->type == CPP_EOF)
18699 cp_parser_end_omp_structured_block (parser, save);
18700 substmt = finish_omp_structured_block (substmt);
18701 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18702 add_stmt (substmt);
18707 tok = cp_lexer_peek_token (parser->lexer);
18708 if (tok->type == CPP_CLOSE_BRACE)
18710 if (tok->type == CPP_EOF)
18713 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18715 cp_lexer_consume_token (parser->lexer);
18716 cp_parser_require_pragma_eol (parser, tok);
18717 error_suppress = false;
18719 else if (!error_suppress)
18721 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18722 error_suppress = true;
18725 substmt = cp_parser_omp_structured_block (parser);
18726 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18727 add_stmt (substmt);
18729 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18731 substmt = pop_stmt_list (stmt);
18733 stmt = make_node (OMP_SECTIONS);
18734 TREE_TYPE (stmt) = void_type_node;
18735 OMP_SECTIONS_BODY (stmt) = substmt;
18742 # pragma omp sections sections-clause[optseq] newline
18746 #define OMP_SECTIONS_CLAUSE_MASK \
18747 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18748 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18749 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18750 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18751 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18754 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18758 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18759 "#pragma omp sections", pragma_tok);
18761 ret = cp_parser_omp_sections_scope (parser);
18763 OMP_SECTIONS_CLAUSES (ret) = clauses;
18769 # pragma parallel parallel-clause new-line
18770 # pragma parallel for parallel-for-clause new-line
18771 # pragma parallel sections parallel-sections-clause new-line
18774 #define OMP_PARALLEL_CLAUSE_MASK \
18775 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18776 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18777 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18778 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18779 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18780 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18781 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18782 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18785 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18787 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18788 const char *p_name = "#pragma omp parallel";
18789 tree stmt, clauses, par_clause, ws_clause, block;
18790 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18795 cp_lexer_consume_token (parser->lexer);
18796 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18797 p_name = "#pragma omp parallel for";
18798 mask |= OMP_FOR_CLAUSE_MASK;
18799 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18801 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18803 tree id = cp_lexer_peek_token (parser->lexer)->value;
18804 const char *p = IDENTIFIER_POINTER (id);
18805 if (strcmp (p, "sections") == 0)
18807 cp_lexer_consume_token (parser->lexer);
18808 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18809 p_name = "#pragma omp parallel sections";
18810 mask |= OMP_SECTIONS_CLAUSE_MASK;
18811 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18815 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18816 block = begin_omp_parallel ();
18817 save = cp_parser_begin_omp_structured_block (parser);
18821 case PRAGMA_OMP_PARALLEL:
18822 cp_parser_already_scoped_statement (parser);
18823 par_clause = clauses;
18826 case PRAGMA_OMP_PARALLEL_FOR:
18827 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18828 stmt = cp_parser_omp_for_loop (parser);
18830 OMP_FOR_CLAUSES (stmt) = ws_clause;
18833 case PRAGMA_OMP_PARALLEL_SECTIONS:
18834 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18835 stmt = cp_parser_omp_sections_scope (parser);
18837 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18841 gcc_unreachable ();
18844 cp_parser_end_omp_structured_block (parser, save);
18845 return finish_omp_parallel (par_clause, block);
18849 # pragma omp single single-clause[optseq] new-line
18853 #define OMP_SINGLE_CLAUSE_MASK \
18854 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18855 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18856 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18857 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18860 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18862 tree stmt = make_node (OMP_SINGLE);
18863 TREE_TYPE (stmt) = void_type_node;
18865 OMP_SINGLE_CLAUSES (stmt)
18866 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18867 "#pragma omp single", pragma_tok);
18868 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18870 return add_stmt (stmt);
18874 # pragma omp threadprivate (variable-list) */
18877 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18881 vars = cp_parser_omp_var_list (parser, 0, NULL);
18882 cp_parser_require_pragma_eol (parser, pragma_tok);
18884 if (!targetm.have_tls)
18885 sorry ("threadprivate variables not supported in this target");
18887 finish_omp_threadprivate (vars);
18890 /* Main entry point to OpenMP statement pragmas. */
18893 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18897 switch (pragma_tok->pragma_kind)
18899 case PRAGMA_OMP_ATOMIC:
18900 cp_parser_omp_atomic (parser, pragma_tok);
18902 case PRAGMA_OMP_CRITICAL:
18903 stmt = cp_parser_omp_critical (parser, pragma_tok);
18905 case PRAGMA_OMP_FOR:
18906 stmt = cp_parser_omp_for (parser, pragma_tok);
18908 case PRAGMA_OMP_MASTER:
18909 stmt = cp_parser_omp_master (parser, pragma_tok);
18911 case PRAGMA_OMP_ORDERED:
18912 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18914 case PRAGMA_OMP_PARALLEL:
18915 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18917 case PRAGMA_OMP_SECTIONS:
18918 stmt = cp_parser_omp_sections (parser, pragma_tok);
18920 case PRAGMA_OMP_SINGLE:
18921 stmt = cp_parser_omp_single (parser, pragma_tok);
18924 gcc_unreachable ();
18928 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18933 static GTY (()) cp_parser *the_parser;
18936 /* Special handling for the first token or line in the file. The first
18937 thing in the file might be #pragma GCC pch_preprocess, which loads a
18938 PCH file, which is a GC collection point. So we need to handle this
18939 first pragma without benefit of an existing lexer structure.
18941 Always returns one token to the caller in *FIRST_TOKEN. This is
18942 either the true first token of the file, or the first token after
18943 the initial pragma. */
18946 cp_parser_initial_pragma (cp_token *first_token)
18950 cp_lexer_get_preprocessor_token (NULL, first_token);
18951 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18954 cp_lexer_get_preprocessor_token (NULL, first_token);
18955 if (first_token->type == CPP_STRING)
18957 name = first_token->value;
18959 cp_lexer_get_preprocessor_token (NULL, first_token);
18960 if (first_token->type != CPP_PRAGMA_EOL)
18961 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18964 error ("expected string literal");
18966 /* Skip to the end of the pragma. */
18967 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
18968 cp_lexer_get_preprocessor_token (NULL, first_token);
18970 /* Read one more token to return to our caller. */
18971 cp_lexer_get_preprocessor_token (NULL, first_token);
18973 /* Now actually load the PCH file. */
18975 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
18978 /* Normal parsing of a pragma token. Here we can (and must) use the
18982 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
18984 cp_token *pragma_tok;
18987 pragma_tok = cp_lexer_consume_token (parser->lexer);
18988 gcc_assert (pragma_tok->type == CPP_PRAGMA);
18989 parser->lexer->in_pragma = true;
18991 id = pragma_tok->pragma_kind;
18994 case PRAGMA_GCC_PCH_PREPROCESS:
18995 error ("%<#pragma GCC pch_preprocess%> must be first");
18998 case PRAGMA_OMP_BARRIER:
19001 case pragma_compound:
19002 cp_parser_omp_barrier (parser, pragma_tok);
19005 error ("%<#pragma omp barrier%> may only be "
19006 "used in compound statements");
19013 case PRAGMA_OMP_FLUSH:
19016 case pragma_compound:
19017 cp_parser_omp_flush (parser, pragma_tok);
19020 error ("%<#pragma omp flush%> may only be "
19021 "used in compound statements");
19028 case PRAGMA_OMP_THREADPRIVATE:
19029 cp_parser_omp_threadprivate (parser, pragma_tok);
19032 case PRAGMA_OMP_ATOMIC:
19033 case PRAGMA_OMP_CRITICAL:
19034 case PRAGMA_OMP_FOR:
19035 case PRAGMA_OMP_MASTER:
19036 case PRAGMA_OMP_ORDERED:
19037 case PRAGMA_OMP_PARALLEL:
19038 case PRAGMA_OMP_SECTIONS:
19039 case PRAGMA_OMP_SINGLE:
19040 if (context == pragma_external)
19042 cp_parser_omp_construct (parser, pragma_tok);
19045 case PRAGMA_OMP_SECTION:
19046 error ("%<#pragma omp section%> may only be used in "
19047 "%<#pragma omp sections%> construct");
19051 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19052 c_invoke_pragma_handler (id);
19056 cp_parser_error (parser, "expected declaration specifiers");
19060 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19064 /* The interface the pragma parsers have to the lexer. */
19067 pragma_lex (tree *value)
19070 enum cpp_ttype ret;
19072 tok = cp_lexer_peek_token (the_parser->lexer);
19075 *value = tok->value;
19077 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19079 else if (ret == CPP_STRING)
19080 *value = cp_parser_string_literal (the_parser, false, false);
19083 cp_lexer_consume_token (the_parser->lexer);
19084 if (ret == CPP_KEYWORD)
19092 /* External interface. */
19094 /* Parse one entire translation unit. */
19097 c_parse_file (void)
19099 bool error_occurred;
19100 static bool already_called = false;
19102 if (already_called)
19104 sorry ("inter-module optimizations not implemented for C++");
19107 already_called = true;
19109 the_parser = cp_parser_new ();
19110 push_deferring_access_checks (flag_access_control
19111 ? dk_no_deferred : dk_no_check);
19112 error_occurred = cp_parser_translation_unit (the_parser);
19116 /* This variable must be provided by every front end. */
19120 #include "gt-cp-parser.h"