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 ((256 * 1024) / sizeof (cp_token))
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 },
1154 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1157 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1159 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1161 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1163 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1165 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1168 /* The same as binops, but initialized by cp_parser_new so that
1169 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1173 /* Constructors and destructors. */
1175 /* Construct a new context. The context below this one on the stack
1176 is given by NEXT. */
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1181 cp_parser_context *context;
1183 /* Allocate the storage. */
1184 if (cp_parser_context_free_list != NULL)
1186 /* Pull the first entry from the free list. */
1187 context = cp_parser_context_free_list;
1188 cp_parser_context_free_list = context->next;
1189 memset (context, 0, sizeof (*context));
1192 context = GGC_CNEW (cp_parser_context);
1194 /* No errors have occurred yet in this context. */
1195 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196 /* If this is not the bottomost context, copy information that we
1197 need from the previous context. */
1200 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201 expression, then we are parsing one in this context, too. */
1202 context->object_type = next->object_type;
1203 /* Thread the stack. */
1204 context->next = next;
1210 /* The cp_parser structure represents the C++ parser. */
1212 typedef struct cp_parser GTY(())
1214 /* The lexer from which we are obtaining tokens. */
1217 /* The scope in which names should be looked up. If NULL_TREE, then
1218 we look up names in the scope that is currently open in the
1219 source program. If non-NULL, this is either a TYPE or
1220 NAMESPACE_DECL for the scope in which we should look. It can
1221 also be ERROR_MARK, when we've parsed a bogus scope.
1223 This value is not cleared automatically after a name is looked
1224 up, so we must be careful to clear it before starting a new look
1225 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1226 will look up `Z' in the scope of `X', rather than the current
1227 scope.) Unfortunately, it is difficult to tell when name lookup
1228 is complete, because we sometimes peek at a token, look it up,
1229 and then decide not to consume it. */
1232 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1233 last lookup took place. OBJECT_SCOPE is used if an expression
1234 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1235 respectively. QUALIFYING_SCOPE is used for an expression of the
1236 form "X::Y"; it refers to X. */
1238 tree qualifying_scope;
1240 /* A stack of parsing contexts. All but the bottom entry on the
1241 stack will be tentative contexts.
1243 We parse tentatively in order to determine which construct is in
1244 use in some situations. For example, in order to determine
1245 whether a statement is an expression-statement or a
1246 declaration-statement we parse it tentatively as a
1247 declaration-statement. If that fails, we then reparse the same
1248 token stream as an expression-statement. */
1249 cp_parser_context *context;
1251 /* True if we are parsing GNU C++. If this flag is not set, then
1252 GNU extensions are not recognized. */
1253 bool allow_gnu_extensions_p;
1255 /* TRUE if the `>' token should be interpreted as the greater-than
1256 operator. FALSE if it is the end of a template-id or
1257 template-parameter-list. */
1258 bool greater_than_is_operator_p;
1260 /* TRUE if default arguments are allowed within a parameter list
1261 that starts at this point. FALSE if only a gnu extension makes
1262 them permissible. */
1263 bool default_arg_ok_p;
1265 /* TRUE if we are parsing an integral constant-expression. See
1266 [expr.const] for a precise definition. */
1267 bool integral_constant_expression_p;
1269 /* TRUE if we are parsing an integral constant-expression -- but a
1270 non-constant expression should be permitted as well. This flag
1271 is used when parsing an array bound so that GNU variable-length
1272 arrays are tolerated. */
1273 bool allow_non_integral_constant_expression_p;
1275 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1276 been seen that makes the expression non-constant. */
1277 bool non_integral_constant_expression_p;
1279 /* TRUE if local variable names and `this' are forbidden in the
1281 bool local_variables_forbidden_p;
1283 /* TRUE if the declaration we are parsing is part of a
1284 linkage-specification of the form `extern string-literal
1286 bool in_unbraced_linkage_specification_p;
1288 /* TRUE if we are presently parsing a declarator, after the
1289 direct-declarator. */
1290 bool in_declarator_p;
1292 /* TRUE if we are presently parsing a template-argument-list. */
1293 bool in_template_argument_list_p;
1295 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1296 to IN_OMP_BLOCK if parsing OpenMP structured block and
1297 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1298 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1299 iteration-statement, OpenMP block or loop within that switch. */
1300 #define IN_SWITCH_STMT 1
1301 #define IN_ITERATION_STMT 2
1302 #define IN_OMP_BLOCK 4
1303 #define IN_OMP_FOR 8
1304 unsigned char in_statement;
1306 /* TRUE if we are presently parsing the body of a switch statement.
1307 Note that this doesn't quite overlap with in_statement above.
1308 The difference relates to giving the right sets of error messages:
1309 "case not in switch" vs "break statement used with OpenMP...". */
1310 bool in_switch_statement_p;
1312 /* TRUE if we are parsing a type-id in an expression context. In
1313 such a situation, both "type (expr)" and "type (type)" are valid
1315 bool in_type_id_in_expr_p;
1317 /* TRUE if we are currently in a header file where declarations are
1318 implicitly extern "C". */
1319 bool implicit_extern_c;
1321 /* TRUE if strings in expressions should be translated to the execution
1323 bool translate_strings_p;
1325 /* If non-NULL, then we are parsing a construct where new type
1326 definitions are not permitted. The string stored here will be
1327 issued as an error message if a type is defined. */
1328 const char *type_definition_forbidden_message;
1330 /* A list of lists. The outer list is a stack, used for member
1331 functions of local classes. At each level there are two sub-list,
1332 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1333 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1334 TREE_VALUE's. The functions are chained in reverse declaration
1337 The TREE_PURPOSE sublist contains those functions with default
1338 arguments that need post processing, and the TREE_VALUE sublist
1339 contains those functions with definitions that need post
1342 These lists can only be processed once the outermost class being
1343 defined is complete. */
1344 tree unparsed_functions_queues;
1346 /* The number of classes whose definitions are currently in
1348 unsigned num_classes_being_defined;
1350 /* The number of template parameter lists that apply directly to the
1351 current declaration. */
1352 unsigned num_template_parameter_lists;
1357 /* Constructors and destructors. */
1359 static cp_parser *cp_parser_new
1362 /* Routines to parse various constructs.
1364 Those that return `tree' will return the error_mark_node (rather
1365 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1366 Sometimes, they will return an ordinary node if error-recovery was
1367 attempted, even though a parse error occurred. So, to check
1368 whether or not a parse error occurred, you should always use
1369 cp_parser_error_occurred. If the construct is optional (indicated
1370 either by an `_opt' in the name of the function that does the
1371 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1372 the construct is not present. */
1374 /* Lexical conventions [gram.lex] */
1376 static tree cp_parser_identifier
1378 static tree cp_parser_string_literal
1379 (cp_parser *, bool, bool);
1381 /* Basic concepts [gram.basic] */
1383 static bool cp_parser_translation_unit
1386 /* Expressions [gram.expr] */
1388 static tree cp_parser_primary_expression
1389 (cp_parser *, bool, bool, bool, cp_id_kind *);
1390 static tree cp_parser_id_expression
1391 (cp_parser *, bool, bool, bool *, bool, bool);
1392 static tree cp_parser_unqualified_id
1393 (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_nested_name_specifier_opt
1395 (cp_parser *, bool, bool, bool, bool);
1396 static tree cp_parser_nested_name_specifier
1397 (cp_parser *, bool, bool, bool, bool);
1398 static tree cp_parser_class_or_namespace_name
1399 (cp_parser *, bool, bool, bool, bool, bool);
1400 static tree cp_parser_postfix_expression
1401 (cp_parser *, bool, bool);
1402 static tree cp_parser_postfix_open_square_expression
1403 (cp_parser *, tree, bool);
1404 static tree cp_parser_postfix_dot_deref_expression
1405 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1406 static tree cp_parser_parenthesized_expression_list
1407 (cp_parser *, bool, bool, bool *);
1408 static void cp_parser_pseudo_destructor_name
1409 (cp_parser *, tree *, tree *);
1410 static tree cp_parser_unary_expression
1411 (cp_parser *, bool, bool);
1412 static enum tree_code cp_parser_unary_operator
1414 static tree cp_parser_new_expression
1416 static tree cp_parser_new_placement
1418 static tree cp_parser_new_type_id
1419 (cp_parser *, tree *);
1420 static cp_declarator *cp_parser_new_declarator_opt
1422 static cp_declarator *cp_parser_direct_new_declarator
1424 static tree cp_parser_new_initializer
1426 static tree cp_parser_delete_expression
1428 static tree cp_parser_cast_expression
1429 (cp_parser *, bool, bool);
1430 static tree cp_parser_binary_expression
1431 (cp_parser *, bool);
1432 static tree cp_parser_question_colon_clause
1433 (cp_parser *, tree);
1434 static tree cp_parser_assignment_expression
1435 (cp_parser *, bool);
1436 static enum tree_code cp_parser_assignment_operator_opt
1438 static tree cp_parser_expression
1439 (cp_parser *, bool);
1440 static tree cp_parser_constant_expression
1441 (cp_parser *, bool, bool *);
1442 static tree cp_parser_builtin_offsetof
1445 /* Statements [gram.stmt.stmt] */
1447 static void cp_parser_statement
1448 (cp_parser *, tree, bool);
1449 static tree cp_parser_labeled_statement
1450 (cp_parser *, tree, bool);
1451 static tree cp_parser_expression_statement
1452 (cp_parser *, tree);
1453 static tree cp_parser_compound_statement
1454 (cp_parser *, tree, bool);
1455 static void cp_parser_statement_seq_opt
1456 (cp_parser *, tree);
1457 static tree cp_parser_selection_statement
1459 static tree cp_parser_condition
1461 static tree cp_parser_iteration_statement
1463 static void cp_parser_for_init_statement
1465 static tree cp_parser_jump_statement
1467 static void cp_parser_declaration_statement
1470 static tree cp_parser_implicitly_scoped_statement
1472 static void cp_parser_already_scoped_statement
1475 /* Declarations [gram.dcl.dcl] */
1477 static void cp_parser_declaration_seq_opt
1479 static void cp_parser_declaration
1481 static void cp_parser_block_declaration
1482 (cp_parser *, bool);
1483 static void cp_parser_simple_declaration
1484 (cp_parser *, bool);
1485 static void cp_parser_decl_specifier_seq
1486 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1487 static tree cp_parser_storage_class_specifier_opt
1489 static tree cp_parser_function_specifier_opt
1490 (cp_parser *, cp_decl_specifier_seq *);
1491 static tree cp_parser_type_specifier
1492 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1494 static tree cp_parser_simple_type_specifier
1495 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1496 static tree cp_parser_type_name
1498 static tree cp_parser_elaborated_type_specifier
1499 (cp_parser *, bool, bool);
1500 static tree cp_parser_enum_specifier
1502 static void cp_parser_enumerator_list
1503 (cp_parser *, tree);
1504 static void cp_parser_enumerator_definition
1505 (cp_parser *, tree);
1506 static tree cp_parser_namespace_name
1508 static void cp_parser_namespace_definition
1510 static void cp_parser_namespace_body
1512 static tree cp_parser_qualified_namespace_specifier
1514 static void cp_parser_namespace_alias_definition
1516 static void cp_parser_using_declaration
1518 static void cp_parser_using_directive
1520 static void cp_parser_asm_definition
1522 static void cp_parser_linkage_specification
1525 /* Declarators [gram.dcl.decl] */
1527 static tree cp_parser_init_declarator
1528 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1529 static cp_declarator *cp_parser_declarator
1530 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1531 static cp_declarator *cp_parser_direct_declarator
1532 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1533 static enum tree_code cp_parser_ptr_operator
1534 (cp_parser *, tree *, cp_cv_quals *);
1535 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1537 static tree cp_parser_declarator_id
1538 (cp_parser *, bool);
1539 static tree cp_parser_type_id
1541 static void cp_parser_type_specifier_seq
1542 (cp_parser *, bool, cp_decl_specifier_seq *);
1543 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1545 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1546 (cp_parser *, bool *);
1547 static cp_parameter_declarator *cp_parser_parameter_declaration
1548 (cp_parser *, bool, bool *);
1549 static void cp_parser_function_body
1551 static tree cp_parser_initializer
1552 (cp_parser *, bool *, bool *);
1553 static tree cp_parser_initializer_clause
1554 (cp_parser *, bool *);
1555 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1556 (cp_parser *, bool *);
1558 static bool cp_parser_ctor_initializer_opt_and_function_body
1561 /* Classes [gram.class] */
1563 static tree cp_parser_class_name
1564 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1565 static tree cp_parser_class_specifier
1567 static tree cp_parser_class_head
1568 (cp_parser *, bool *, tree *);
1569 static enum tag_types cp_parser_class_key
1571 static void cp_parser_member_specification_opt
1573 static void cp_parser_member_declaration
1575 static tree cp_parser_pure_specifier
1577 static tree cp_parser_constant_initializer
1580 /* Derived classes [gram.class.derived] */
1582 static tree cp_parser_base_clause
1584 static tree cp_parser_base_specifier
1587 /* Special member functions [gram.special] */
1589 static tree cp_parser_conversion_function_id
1591 static tree cp_parser_conversion_type_id
1593 static cp_declarator *cp_parser_conversion_declarator_opt
1595 static bool cp_parser_ctor_initializer_opt
1597 static void cp_parser_mem_initializer_list
1599 static tree cp_parser_mem_initializer
1601 static tree cp_parser_mem_initializer_id
1604 /* Overloading [gram.over] */
1606 static tree cp_parser_operator_function_id
1608 static tree cp_parser_operator
1611 /* Templates [gram.temp] */
1613 static void cp_parser_template_declaration
1614 (cp_parser *, bool);
1615 static tree cp_parser_template_parameter_list
1617 static tree cp_parser_template_parameter
1618 (cp_parser *, bool *);
1619 static tree cp_parser_type_parameter
1621 static tree cp_parser_template_id
1622 (cp_parser *, bool, bool, bool);
1623 static tree cp_parser_template_name
1624 (cp_parser *, bool, bool, bool, bool *);
1625 static tree cp_parser_template_argument_list
1627 static tree cp_parser_template_argument
1629 static void cp_parser_explicit_instantiation
1631 static void cp_parser_explicit_specialization
1634 /* Exception handling [gram.exception] */
1636 static tree cp_parser_try_block
1638 static bool cp_parser_function_try_block
1640 static void cp_parser_handler_seq
1642 static void cp_parser_handler
1644 static tree cp_parser_exception_declaration
1646 static tree cp_parser_throw_expression
1648 static tree cp_parser_exception_specification_opt
1650 static tree cp_parser_type_id_list
1653 /* GNU Extensions */
1655 static tree cp_parser_asm_specification_opt
1657 static tree cp_parser_asm_operand_list
1659 static tree cp_parser_asm_clobber_list
1661 static tree cp_parser_attributes_opt
1663 static tree cp_parser_attribute_list
1665 static bool cp_parser_extension_opt
1666 (cp_parser *, int *);
1667 static void cp_parser_label_declaration
1670 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1671 static bool cp_parser_pragma
1672 (cp_parser *, enum pragma_context);
1674 /* Objective-C++ Productions */
1676 static tree cp_parser_objc_message_receiver
1678 static tree cp_parser_objc_message_args
1680 static tree cp_parser_objc_message_expression
1682 static tree cp_parser_objc_encode_expression
1684 static tree cp_parser_objc_defs_expression
1686 static tree cp_parser_objc_protocol_expression
1688 static tree cp_parser_objc_selector_expression
1690 static tree cp_parser_objc_expression
1692 static bool cp_parser_objc_selector_p
1694 static tree cp_parser_objc_selector
1696 static tree cp_parser_objc_protocol_refs_opt
1698 static void cp_parser_objc_declaration
1700 static tree cp_parser_objc_statement
1703 /* Utility Routines */
1705 static tree cp_parser_lookup_name
1706 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1707 static tree cp_parser_lookup_name_simple
1708 (cp_parser *, tree);
1709 static tree cp_parser_maybe_treat_template_as_class
1711 static bool cp_parser_check_declarator_template_parameters
1712 (cp_parser *, cp_declarator *);
1713 static bool cp_parser_check_template_parameters
1714 (cp_parser *, unsigned);
1715 static tree cp_parser_simple_cast_expression
1717 static tree cp_parser_global_scope_opt
1718 (cp_parser *, bool);
1719 static bool cp_parser_constructor_declarator_p
1720 (cp_parser *, bool);
1721 static tree cp_parser_function_definition_from_specifiers_and_declarator
1722 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1723 static tree cp_parser_function_definition_after_declarator
1724 (cp_parser *, bool);
1725 static void cp_parser_template_declaration_after_export
1726 (cp_parser *, bool);
1727 static void cp_parser_perform_template_parameter_access_checks
1729 static tree cp_parser_single_declaration
1730 (cp_parser *, tree, bool, bool *);
1731 static tree cp_parser_functional_cast
1732 (cp_parser *, tree);
1733 static tree cp_parser_save_member_function_body
1734 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1735 static tree cp_parser_enclosed_template_argument_list
1737 static void cp_parser_save_default_args
1738 (cp_parser *, tree);
1739 static void cp_parser_late_parsing_for_member
1740 (cp_parser *, tree);
1741 static void cp_parser_late_parsing_default_args
1742 (cp_parser *, tree);
1743 static tree cp_parser_sizeof_operand
1744 (cp_parser *, enum rid);
1745 static bool cp_parser_declares_only_class_p
1747 static void cp_parser_set_storage_class
1748 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1749 static void cp_parser_set_decl_spec_type
1750 (cp_decl_specifier_seq *, tree, bool);
1751 static bool cp_parser_friend_p
1752 (const cp_decl_specifier_seq *);
1753 static cp_token *cp_parser_require
1754 (cp_parser *, enum cpp_ttype, const char *);
1755 static cp_token *cp_parser_require_keyword
1756 (cp_parser *, enum rid, const char *);
1757 static bool cp_parser_token_starts_function_definition_p
1759 static bool cp_parser_next_token_starts_class_definition_p
1761 static bool cp_parser_next_token_ends_template_argument_p
1763 static bool cp_parser_nth_token_starts_template_argument_list_p
1764 (cp_parser *, size_t);
1765 static enum tag_types cp_parser_token_is_class_key
1767 static void cp_parser_check_class_key
1768 (enum tag_types, tree type);
1769 static void cp_parser_check_access_in_redeclaration
1771 static bool cp_parser_optional_template_keyword
1773 static void cp_parser_pre_parsed_nested_name_specifier
1775 static void cp_parser_cache_group
1776 (cp_parser *, enum cpp_ttype, unsigned);
1777 static void cp_parser_parse_tentatively
1779 static void cp_parser_commit_to_tentative_parse
1781 static void cp_parser_abort_tentative_parse
1783 static bool cp_parser_parse_definitely
1785 static inline bool cp_parser_parsing_tentatively
1787 static bool cp_parser_uncommitted_to_tentative_parse_p
1789 static void cp_parser_error
1790 (cp_parser *, const char *);
1791 static void cp_parser_name_lookup_error
1792 (cp_parser *, tree, tree, const char *);
1793 static bool cp_parser_simulate_error
1795 static void cp_parser_check_type_definition
1797 static void cp_parser_check_for_definition_in_return_type
1798 (cp_declarator *, tree);
1799 static void cp_parser_check_for_invalid_template_id
1800 (cp_parser *, tree);
1801 static bool cp_parser_non_integral_constant_expression
1802 (cp_parser *, const char *);
1803 static void cp_parser_diagnose_invalid_type_name
1804 (cp_parser *, tree, tree);
1805 static bool cp_parser_parse_and_diagnose_invalid_type_name
1807 static int cp_parser_skip_to_closing_parenthesis
1808 (cp_parser *, bool, bool, bool);
1809 static void cp_parser_skip_to_end_of_statement
1811 static void cp_parser_consume_semicolon_at_end_of_statement
1813 static void cp_parser_skip_to_end_of_block_or_statement
1815 static void cp_parser_skip_to_closing_brace
1817 static void cp_parser_skip_until_found
1818 (cp_parser *, enum cpp_ttype, const char *);
1819 static void cp_parser_skip_to_pragma_eol
1820 (cp_parser*, cp_token *);
1821 static bool cp_parser_error_occurred
1823 static bool cp_parser_allow_gnu_extensions_p
1825 static bool cp_parser_is_string_literal
1827 static bool cp_parser_is_keyword
1828 (cp_token *, enum rid);
1829 static tree cp_parser_make_typename_type
1830 (cp_parser *, tree, tree);
1832 /* Returns nonzero if we are parsing tentatively. */
1835 cp_parser_parsing_tentatively (cp_parser* parser)
1837 return parser->context->next != NULL;
1840 /* Returns nonzero if TOKEN is a string literal. */
1843 cp_parser_is_string_literal (cp_token* token)
1845 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1851 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1853 return token->keyword == keyword;
1856 /* If not parsing tentatively, issue a diagnostic of the form
1857 FILE:LINE: MESSAGE before TOKEN
1858 where TOKEN is the next token in the input stream. MESSAGE
1859 (specified by the caller) is usually of the form "expected
1863 cp_parser_error (cp_parser* parser, const char* message)
1865 if (!cp_parser_simulate_error (parser))
1867 cp_token *token = cp_lexer_peek_token (parser->lexer);
1868 /* This diagnostic makes more sense if it is tagged to the line
1869 of the token we just peeked at. */
1870 cp_lexer_set_source_position_from_token (token);
1872 if (token->type == CPP_PRAGMA)
1874 error ("%<#pragma%> is not allowed here");
1875 cp_parser_skip_to_pragma_eol (parser, token);
1879 c_parse_error (message,
1880 /* Because c_parser_error does not understand
1881 CPP_KEYWORD, keywords are treated like
1883 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1888 /* Issue an error about name-lookup failing. NAME is the
1889 IDENTIFIER_NODE DECL is the result of
1890 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1891 the thing that we hoped to find. */
1894 cp_parser_name_lookup_error (cp_parser* parser,
1897 const char* desired)
1899 /* If name lookup completely failed, tell the user that NAME was not
1901 if (decl == error_mark_node)
1903 if (parser->scope && parser->scope != global_namespace)
1904 error ("%<%D::%D%> has not been declared",
1905 parser->scope, name);
1906 else if (parser->scope == global_namespace)
1907 error ("%<::%D%> has not been declared", name);
1908 else if (parser->object_scope
1909 && !CLASS_TYPE_P (parser->object_scope))
1910 error ("request for member %qD in non-class type %qT",
1911 name, parser->object_scope);
1912 else if (parser->object_scope)
1913 error ("%<%T::%D%> has not been declared",
1914 parser->object_scope, name);
1916 error ("%qD has not been declared", name);
1918 else if (parser->scope && parser->scope != global_namespace)
1919 error ("%<%D::%D%> %s", parser->scope, name, desired);
1920 else if (parser->scope == global_namespace)
1921 error ("%<::%D%> %s", name, desired);
1923 error ("%qD %s", name, desired);
1926 /* If we are parsing tentatively, remember that an error has occurred
1927 during this tentative parse. Returns true if the error was
1928 simulated; false if a message should be issued by the caller. */
1931 cp_parser_simulate_error (cp_parser* parser)
1933 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1935 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1941 /* Check for repeated decl-specifiers. */
1944 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1948 for (ds = ds_first; ds != ds_last; ++ds)
1950 unsigned count = decl_specs->specs[(int)ds];
1953 /* The "long" specifier is a special case because of "long long". */
1957 error ("%<long long long%> is too long for GCC");
1958 else if (pedantic && !in_system_header && warn_long_long)
1959 pedwarn ("ISO C++ does not support %<long long%>");
1963 static const char *const decl_spec_names[] = {
1979 error ("duplicate %qs", decl_spec_names[(int)ds]);
1984 /* This function is called when a type is defined. If type
1985 definitions are forbidden at this point, an error message is
1989 cp_parser_check_type_definition (cp_parser* parser)
1991 /* If types are forbidden here, issue a message. */
1992 if (parser->type_definition_forbidden_message)
1993 /* Use `%s' to print the string in case there are any escape
1994 characters in the message. */
1995 error ("%s", parser->type_definition_forbidden_message);
1998 /* This function is called when the DECLARATOR is processed. The TYPE
1999 was a type defined in the decl-specifiers. If it is invalid to
2000 define a type in the decl-specifiers for DECLARATOR, an error is
2004 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2007 /* [dcl.fct] forbids type definitions in return types.
2008 Unfortunately, it's not easy to know whether or not we are
2009 processing a return type until after the fact. */
2011 && (declarator->kind == cdk_pointer
2012 || declarator->kind == cdk_reference
2013 || declarator->kind == cdk_ptrmem))
2014 declarator = declarator->declarator;
2016 && declarator->kind == cdk_function)
2018 error ("new types may not be defined in a return type");
2019 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2024 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2025 "<" in any valid C++ program. If the next token is indeed "<",
2026 issue a message warning the user about what appears to be an
2027 invalid attempt to form a template-id. */
2030 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2033 cp_token_position start = 0;
2035 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2038 error ("%qT is not a template", type);
2039 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2040 error ("%qE is not a template", type);
2042 error ("invalid template-id");
2043 /* Remember the location of the invalid "<". */
2044 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2045 start = cp_lexer_token_position (parser->lexer, true);
2046 /* Consume the "<". */
2047 cp_lexer_consume_token (parser->lexer);
2048 /* Parse the template arguments. */
2049 cp_parser_enclosed_template_argument_list (parser);
2050 /* Permanently remove the invalid template arguments so that
2051 this error message is not issued again. */
2053 cp_lexer_purge_tokens_after (parser->lexer, start);
2057 /* If parsing an integral constant-expression, issue an error message
2058 about the fact that THING appeared and return true. Otherwise,
2059 return false. In either case, set
2060 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2063 cp_parser_non_integral_constant_expression (cp_parser *parser,
2066 parser->non_integral_constant_expression_p = true;
2067 if (parser->integral_constant_expression_p)
2069 if (!parser->allow_non_integral_constant_expression_p)
2071 error ("%s cannot appear in a constant-expression", thing);
2078 /* Emit a diagnostic for an invalid type name. SCOPE is the
2079 qualifying scope (or NULL, if none) for ID. This function commits
2080 to the current active tentative parse, if any. (Otherwise, the
2081 problematic construct might be encountered again later, resulting
2082 in duplicate error messages.) */
2085 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2087 tree decl, old_scope;
2088 /* Try to lookup the identifier. */
2089 old_scope = parser->scope;
2090 parser->scope = scope;
2091 decl = cp_parser_lookup_name_simple (parser, id);
2092 parser->scope = old_scope;
2093 /* If the lookup found a template-name, it means that the user forgot
2094 to specify an argument list. Emit a useful error message. */
2095 if (TREE_CODE (decl) == TEMPLATE_DECL)
2096 error ("invalid use of template-name %qE without an argument list", decl);
2097 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2098 error ("invalid use of destructor %qD as a type", id);
2099 else if (!parser->scope)
2101 /* Issue an error message. */
2102 error ("%qE does not name a type", id);
2103 /* If we're in a template class, it's possible that the user was
2104 referring to a type from a base class. For example:
2106 template <typename T> struct A { typedef T X; };
2107 template <typename T> struct B : public A<T> { X x; };
2109 The user should have said "typename A<T>::X". */
2110 if (processing_template_decl && current_class_type
2111 && TYPE_BINFO (current_class_type))
2115 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2119 tree base_type = BINFO_TYPE (b);
2120 if (CLASS_TYPE_P (base_type)
2121 && dependent_type_p (base_type))
2124 /* Go from a particular instantiation of the
2125 template (which will have an empty TYPE_FIELDs),
2126 to the main version. */
2127 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2128 for (field = TYPE_FIELDS (base_type);
2130 field = TREE_CHAIN (field))
2131 if (TREE_CODE (field) == TYPE_DECL
2132 && DECL_NAME (field) == id)
2134 inform ("(perhaps %<typename %T::%E%> was intended)",
2135 BINFO_TYPE (b), id);
2144 /* Here we diagnose qualified-ids where the scope is actually correct,
2145 but the identifier does not resolve to a valid type name. */
2146 else if (parser->scope != error_mark_node)
2148 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2149 error ("%qE in namespace %qE does not name a type",
2151 else if (TYPE_P (parser->scope))
2152 error ("%qE in class %qT does not name a type", id, parser->scope);
2156 cp_parser_commit_to_tentative_parse (parser);
2159 /* Check for a common situation where a type-name should be present,
2160 but is not, and issue a sensible error message. Returns true if an
2161 invalid type-name was detected.
2163 The situation handled by this function are variable declarations of the
2164 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2165 Usually, `ID' should name a type, but if we got here it means that it
2166 does not. We try to emit the best possible error message depending on
2167 how exactly the id-expression looks like. */
2170 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2174 cp_parser_parse_tentatively (parser);
2175 id = cp_parser_id_expression (parser,
2176 /*template_keyword_p=*/false,
2177 /*check_dependency_p=*/true,
2178 /*template_p=*/NULL,
2179 /*declarator_p=*/true,
2180 /*optional_p=*/false);
2181 /* After the id-expression, there should be a plain identifier,
2182 otherwise this is not a simple variable declaration. Also, if
2183 the scope is dependent, we cannot do much. */
2184 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2185 || (parser->scope && TYPE_P (parser->scope)
2186 && dependent_type_p (parser->scope)))
2188 cp_parser_abort_tentative_parse (parser);
2191 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2194 /* Emit a diagnostic for the invalid type. */
2195 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2196 /* Skip to the end of the declaration; there's no point in
2197 trying to process it. */
2198 cp_parser_skip_to_end_of_block_or_statement (parser);
2202 /* Consume tokens up to, and including, the next non-nested closing `)'.
2203 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2204 are doing error recovery. Returns -1 if OR_COMMA is true and we
2205 found an unnested comma. */
2208 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2213 unsigned paren_depth = 0;
2214 unsigned brace_depth = 0;
2216 if (recovering && !or_comma
2217 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2222 cp_token * token = cp_lexer_peek_token (parser->lexer);
2224 switch (token->type)
2227 case CPP_PRAGMA_EOL:
2228 /* If we've run out of tokens, then there is no closing `)'. */
2232 /* This matches the processing in skip_to_end_of_statement. */
2237 case CPP_OPEN_BRACE:
2240 case CPP_CLOSE_BRACE:
2246 if (recovering && or_comma && !brace_depth && !paren_depth)
2250 case CPP_OPEN_PAREN:
2255 case CPP_CLOSE_PAREN:
2256 if (!brace_depth && !paren_depth--)
2259 cp_lexer_consume_token (parser->lexer);
2268 /* Consume the token. */
2269 cp_lexer_consume_token (parser->lexer);
2273 /* Consume tokens until we reach the end of the current statement.
2274 Normally, that will be just before consuming a `;'. However, if a
2275 non-nested `}' comes first, then we stop before consuming that. */
2278 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2280 unsigned nesting_depth = 0;
2284 cp_token *token = cp_lexer_peek_token (parser->lexer);
2286 switch (token->type)
2289 case CPP_PRAGMA_EOL:
2290 /* If we've run out of tokens, stop. */
2294 /* If the next token is a `;', we have reached the end of the
2300 case CPP_CLOSE_BRACE:
2301 /* If this is a non-nested '}', stop before consuming it.
2302 That way, when confronted with something like:
2306 we stop before consuming the closing '}', even though we
2307 have not yet reached a `;'. */
2308 if (nesting_depth == 0)
2311 /* If it is the closing '}' for a block that we have
2312 scanned, stop -- but only after consuming the token.
2318 we will stop after the body of the erroneously declared
2319 function, but before consuming the following `typedef'
2321 if (--nesting_depth == 0)
2323 cp_lexer_consume_token (parser->lexer);
2327 case CPP_OPEN_BRACE:
2335 /* Consume the token. */
2336 cp_lexer_consume_token (parser->lexer);
2340 /* This function is called at the end of a statement or declaration.
2341 If the next token is a semicolon, it is consumed; otherwise, error
2342 recovery is attempted. */
2345 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2347 /* Look for the trailing `;'. */
2348 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2350 /* If there is additional (erroneous) input, skip to the end of
2352 cp_parser_skip_to_end_of_statement (parser);
2353 /* If the next token is now a `;', consume it. */
2354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2355 cp_lexer_consume_token (parser->lexer);
2359 /* Skip tokens until we have consumed an entire block, or until we
2360 have consumed a non-nested `;'. */
2363 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2365 int nesting_depth = 0;
2367 while (nesting_depth >= 0)
2369 cp_token *token = cp_lexer_peek_token (parser->lexer);
2371 switch (token->type)
2374 case CPP_PRAGMA_EOL:
2375 /* If we've run out of tokens, stop. */
2379 /* Stop if this is an unnested ';'. */
2384 case CPP_CLOSE_BRACE:
2385 /* Stop if this is an unnested '}', or closes the outermost
2392 case CPP_OPEN_BRACE:
2401 /* Consume the token. */
2402 cp_lexer_consume_token (parser->lexer);
2406 /* Skip tokens until a non-nested closing curly brace is the next
2410 cp_parser_skip_to_closing_brace (cp_parser *parser)
2412 unsigned nesting_depth = 0;
2416 cp_token *token = cp_lexer_peek_token (parser->lexer);
2418 switch (token->type)
2421 case CPP_PRAGMA_EOL:
2422 /* If we've run out of tokens, stop. */
2425 case CPP_CLOSE_BRACE:
2426 /* If the next token is a non-nested `}', then we have reached
2427 the end of the current block. */
2428 if (nesting_depth-- == 0)
2432 case CPP_OPEN_BRACE:
2433 /* If it the next token is a `{', then we are entering a new
2434 block. Consume the entire block. */
2442 /* Consume the token. */
2443 cp_lexer_consume_token (parser->lexer);
2447 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2448 parameter is the PRAGMA token, allowing us to purge the entire pragma
2452 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2456 parser->lexer->in_pragma = false;
2459 token = cp_lexer_consume_token (parser->lexer);
2460 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2462 /* Ensure that the pragma is not parsed again. */
2463 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2466 /* Require pragma end of line, resyncing with it as necessary. The
2467 arguments are as for cp_parser_skip_to_pragma_eol. */
2470 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2472 parser->lexer->in_pragma = false;
2473 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2477 /* This is a simple wrapper around make_typename_type. When the id is
2478 an unresolved identifier node, we can provide a superior diagnostic
2479 using cp_parser_diagnose_invalid_type_name. */
2482 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2485 if (TREE_CODE (id) == IDENTIFIER_NODE)
2487 result = make_typename_type (scope, id, typename_type,
2488 /*complain=*/tf_none);
2489 if (result == error_mark_node)
2490 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2493 return make_typename_type (scope, id, typename_type, tf_error);
2497 /* Create a new C++ parser. */
2500 cp_parser_new (void)
2506 /* cp_lexer_new_main is called before calling ggc_alloc because
2507 cp_lexer_new_main might load a PCH file. */
2508 lexer = cp_lexer_new_main ();
2510 /* Initialize the binops_by_token so that we can get the tree
2511 directly from the token. */
2512 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2513 binops_by_token[binops[i].token_type] = binops[i];
2515 parser = GGC_CNEW (cp_parser);
2516 parser->lexer = lexer;
2517 parser->context = cp_parser_context_new (NULL);
2519 /* For now, we always accept GNU extensions. */
2520 parser->allow_gnu_extensions_p = 1;
2522 /* The `>' token is a greater-than operator, not the end of a
2524 parser->greater_than_is_operator_p = true;
2526 parser->default_arg_ok_p = true;
2528 /* We are not parsing a constant-expression. */
2529 parser->integral_constant_expression_p = false;
2530 parser->allow_non_integral_constant_expression_p = false;
2531 parser->non_integral_constant_expression_p = false;
2533 /* Local variable names are not forbidden. */
2534 parser->local_variables_forbidden_p = false;
2536 /* We are not processing an `extern "C"' declaration. */
2537 parser->in_unbraced_linkage_specification_p = false;
2539 /* We are not processing a declarator. */
2540 parser->in_declarator_p = false;
2542 /* We are not processing a template-argument-list. */
2543 parser->in_template_argument_list_p = false;
2545 /* We are not in an iteration statement. */
2546 parser->in_statement = 0;
2548 /* We are not in a switch statement. */
2549 parser->in_switch_statement_p = false;
2551 /* We are not parsing a type-id inside an expression. */
2552 parser->in_type_id_in_expr_p = false;
2554 /* Declarations aren't implicitly extern "C". */
2555 parser->implicit_extern_c = false;
2557 /* String literals should be translated to the execution character set. */
2558 parser->translate_strings_p = true;
2560 /* The unparsed function queue is empty. */
2561 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2563 /* There are no classes being defined. */
2564 parser->num_classes_being_defined = 0;
2566 /* No template parameters apply. */
2567 parser->num_template_parameter_lists = 0;
2572 /* Create a cp_lexer structure which will emit the tokens in CACHE
2573 and push it onto the parser's lexer stack. This is used for delayed
2574 parsing of in-class method bodies and default arguments, and should
2575 not be confused with tentative parsing. */
2577 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2579 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2580 lexer->next = parser->lexer;
2581 parser->lexer = lexer;
2583 /* Move the current source position to that of the first token in the
2585 cp_lexer_set_source_position_from_token (lexer->next_token);
2588 /* Pop the top lexer off the parser stack. This is never used for the
2589 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2591 cp_parser_pop_lexer (cp_parser *parser)
2593 cp_lexer *lexer = parser->lexer;
2594 parser->lexer = lexer->next;
2595 cp_lexer_destroy (lexer);
2597 /* Put the current source position back where it was before this
2598 lexer was pushed. */
2599 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2602 /* Lexical conventions [gram.lex] */
2604 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2608 cp_parser_identifier (cp_parser* parser)
2612 /* Look for the identifier. */
2613 token = cp_parser_require (parser, CPP_NAME, "identifier");
2614 /* Return the value. */
2615 return token ? token->value : error_mark_node;
2618 /* Parse a sequence of adjacent string constants. Returns a
2619 TREE_STRING representing the combined, nul-terminated string
2620 constant. If TRANSLATE is true, translate the string to the
2621 execution character set. If WIDE_OK is true, a wide string is
2624 C++98 [lex.string] says that if a narrow string literal token is
2625 adjacent to a wide string literal token, the behavior is undefined.
2626 However, C99 6.4.5p4 says that this results in a wide string literal.
2627 We follow C99 here, for consistency with the C front end.
2629 This code is largely lifted from lex_string() in c-lex.c.
2631 FUTURE: ObjC++ will need to handle @-strings here. */
2633 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2638 struct obstack str_ob;
2639 cpp_string str, istr, *strs;
2642 tok = cp_lexer_peek_token (parser->lexer);
2643 if (!cp_parser_is_string_literal (tok))
2645 cp_parser_error (parser, "expected string-literal");
2646 return error_mark_node;
2649 /* Try to avoid the overhead of creating and destroying an obstack
2650 for the common case of just one string. */
2651 if (!cp_parser_is_string_literal
2652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2654 cp_lexer_consume_token (parser->lexer);
2656 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2657 str.len = TREE_STRING_LENGTH (tok->value);
2659 if (tok->type == CPP_WSTRING)
2666 gcc_obstack_init (&str_ob);
2671 cp_lexer_consume_token (parser->lexer);
2673 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2674 str.len = TREE_STRING_LENGTH (tok->value);
2675 if (tok->type == CPP_WSTRING)
2678 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2680 tok = cp_lexer_peek_token (parser->lexer);
2682 while (cp_parser_is_string_literal (tok));
2684 strs = (cpp_string *) obstack_finish (&str_ob);
2687 if (wide && !wide_ok)
2689 cp_parser_error (parser, "a wide string is invalid in this context");
2693 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2694 (parse_in, strs, count, &istr, wide))
2696 value = build_string (istr.len, (char *)istr.text);
2697 free ((void *)istr.text);
2699 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2700 value = fix_string_type (value);
2703 /* cpp_interpret_string has issued an error. */
2704 value = error_mark_node;
2707 obstack_free (&str_ob, 0);
2713 /* Basic concepts [gram.basic] */
2715 /* Parse a translation-unit.
2718 declaration-seq [opt]
2720 Returns TRUE if all went well. */
2723 cp_parser_translation_unit (cp_parser* parser)
2725 /* The address of the first non-permanent object on the declarator
2727 static void *declarator_obstack_base;
2731 /* Create the declarator obstack, if necessary. */
2732 if (!cp_error_declarator)
2734 gcc_obstack_init (&declarator_obstack);
2735 /* Create the error declarator. */
2736 cp_error_declarator = make_declarator (cdk_error);
2737 /* Create the empty parameter list. */
2738 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2739 /* Remember where the base of the declarator obstack lies. */
2740 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2743 cp_parser_declaration_seq_opt (parser);
2745 /* If there are no tokens left then all went well. */
2746 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2748 /* Get rid of the token array; we don't need it any more. */
2749 cp_lexer_destroy (parser->lexer);
2750 parser->lexer = NULL;
2752 /* This file might have been a context that's implicitly extern
2753 "C". If so, pop the lang context. (Only relevant for PCH.) */
2754 if (parser->implicit_extern_c)
2756 pop_lang_context ();
2757 parser->implicit_extern_c = false;
2761 finish_translation_unit ();
2767 cp_parser_error (parser, "expected declaration");
2771 /* Make sure the declarator obstack was fully cleaned up. */
2772 gcc_assert (obstack_next_free (&declarator_obstack)
2773 == declarator_obstack_base);
2775 /* All went well. */
2779 /* Expressions [gram.expr] */
2781 /* Parse a primary-expression.
2792 ( compound-statement )
2793 __builtin_va_arg ( assignment-expression , type-id )
2794 __builtin_offsetof ( type-id , offsetof-expression )
2796 Objective-C++ Extension:
2804 ADDRESS_P is true iff this expression was immediately preceded by
2805 "&" and therefore might denote a pointer-to-member. CAST_P is true
2806 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2807 true iff this expression is a template argument.
2809 Returns a representation of the expression. Upon return, *IDK
2810 indicates what kind of id-expression (if any) was present. */
2813 cp_parser_primary_expression (cp_parser *parser,
2816 bool template_arg_p,
2821 /* Assume the primary expression is not an id-expression. */
2822 *idk = CP_ID_KIND_NONE;
2824 /* Peek at the next token. */
2825 token = cp_lexer_peek_token (parser->lexer);
2826 switch (token->type)
2837 token = cp_lexer_consume_token (parser->lexer);
2838 /* Floating-point literals are only allowed in an integral
2839 constant expression if they are cast to an integral or
2840 enumeration type. */
2841 if (TREE_CODE (token->value) == REAL_CST
2842 && parser->integral_constant_expression_p
2845 /* CAST_P will be set even in invalid code like "int(2.7 +
2846 ...)". Therefore, we have to check that the next token
2847 is sure to end the cast. */
2850 cp_token *next_token;
2852 next_token = cp_lexer_peek_token (parser->lexer);
2853 if (/* The comma at the end of an
2854 enumerator-definition. */
2855 next_token->type != CPP_COMMA
2856 /* The curly brace at the end of an enum-specifier. */
2857 && next_token->type != CPP_CLOSE_BRACE
2858 /* The end of a statement. */
2859 && next_token->type != CPP_SEMICOLON
2860 /* The end of the cast-expression. */
2861 && next_token->type != CPP_CLOSE_PAREN
2862 /* The end of an array bound. */
2863 && next_token->type != CPP_CLOSE_SQUARE
2864 /* The closing ">" in a template-argument-list. */
2865 && (next_token->type != CPP_GREATER
2866 || parser->greater_than_is_operator_p))
2870 /* If we are within a cast, then the constraint that the
2871 cast is to an integral or enumeration type will be
2872 checked at that point. If we are not within a cast, then
2873 this code is invalid. */
2875 cp_parser_non_integral_constant_expression
2876 (parser, "floating-point literal");
2878 return token->value;
2882 /* ??? Should wide strings be allowed when parser->translate_strings_p
2883 is false (i.e. in attributes)? If not, we can kill the third
2884 argument to cp_parser_string_literal. */
2885 return cp_parser_string_literal (parser,
2886 parser->translate_strings_p,
2889 case CPP_OPEN_PAREN:
2892 bool saved_greater_than_is_operator_p;
2894 /* Consume the `('. */
2895 cp_lexer_consume_token (parser->lexer);
2896 /* Within a parenthesized expression, a `>' token is always
2897 the greater-than operator. */
2898 saved_greater_than_is_operator_p
2899 = parser->greater_than_is_operator_p;
2900 parser->greater_than_is_operator_p = true;
2901 /* If we see `( { ' then we are looking at the beginning of
2902 a GNU statement-expression. */
2903 if (cp_parser_allow_gnu_extensions_p (parser)
2904 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2906 /* Statement-expressions are not allowed by the standard. */
2908 pedwarn ("ISO C++ forbids braced-groups within expressions");
2910 /* And they're not allowed outside of a function-body; you
2911 cannot, for example, write:
2913 int i = ({ int j = 3; j + 1; });
2915 at class or namespace scope. */
2916 if (!at_function_scope_p ())
2917 error ("statement-expressions are allowed only inside functions");
2918 /* Start the statement-expression. */
2919 expr = begin_stmt_expr ();
2920 /* Parse the compound-statement. */
2921 cp_parser_compound_statement (parser, expr, false);
2923 expr = finish_stmt_expr (expr, false);
2927 /* Parse the parenthesized expression. */
2928 expr = cp_parser_expression (parser, cast_p);
2929 /* Let the front end know that this expression was
2930 enclosed in parentheses. This matters in case, for
2931 example, the expression is of the form `A::B', since
2932 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2934 finish_parenthesized_expr (expr);
2936 /* The `>' token might be the end of a template-id or
2937 template-parameter-list now. */
2938 parser->greater_than_is_operator_p
2939 = saved_greater_than_is_operator_p;
2940 /* Consume the `)'. */
2941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2942 cp_parser_skip_to_end_of_statement (parser);
2948 switch (token->keyword)
2950 /* These two are the boolean literals. */
2952 cp_lexer_consume_token (parser->lexer);
2953 return boolean_true_node;
2955 cp_lexer_consume_token (parser->lexer);
2956 return boolean_false_node;
2958 /* The `__null' literal. */
2960 cp_lexer_consume_token (parser->lexer);
2963 /* Recognize the `this' keyword. */
2965 cp_lexer_consume_token (parser->lexer);
2966 if (parser->local_variables_forbidden_p)
2968 error ("%<this%> may not be used in this context");
2969 return error_mark_node;
2971 /* Pointers cannot appear in constant-expressions. */
2972 if (cp_parser_non_integral_constant_expression (parser,
2974 return error_mark_node;
2975 return finish_this_expr ();
2977 /* The `operator' keyword can be the beginning of an
2982 case RID_FUNCTION_NAME:
2983 case RID_PRETTY_FUNCTION_NAME:
2984 case RID_C99_FUNCTION_NAME:
2985 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2986 __func__ are the names of variables -- but they are
2987 treated specially. Therefore, they are handled here,
2988 rather than relying on the generic id-expression logic
2989 below. Grammatically, these names are id-expressions.
2991 Consume the token. */
2992 token = cp_lexer_consume_token (parser->lexer);
2993 /* Look up the name. */
2994 return finish_fname (token->value);
3001 /* The `__builtin_va_arg' construct is used to handle
3002 `va_arg'. Consume the `__builtin_va_arg' token. */
3003 cp_lexer_consume_token (parser->lexer);
3004 /* Look for the opening `('. */
3005 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3006 /* Now, parse the assignment-expression. */
3007 expression = cp_parser_assignment_expression (parser,
3009 /* Look for the `,'. */
3010 cp_parser_require (parser, CPP_COMMA, "`,'");
3011 /* Parse the type-id. */
3012 type = cp_parser_type_id (parser);
3013 /* Look for the closing `)'. */
3014 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3015 /* Using `va_arg' in a constant-expression is not
3017 if (cp_parser_non_integral_constant_expression (parser,
3019 return error_mark_node;
3020 return build_x_va_arg (expression, type);
3024 return cp_parser_builtin_offsetof (parser);
3026 /* Objective-C++ expressions. */
3028 case RID_AT_PROTOCOL:
3029 case RID_AT_SELECTOR:
3030 return cp_parser_objc_expression (parser);
3033 cp_parser_error (parser, "expected primary-expression");
3034 return error_mark_node;
3037 /* An id-expression can start with either an identifier, a
3038 `::' as the beginning of a qualified-id, or the "operator"
3042 case CPP_TEMPLATE_ID:
3043 case CPP_NESTED_NAME_SPECIFIER:
3047 const char *error_msg;
3052 /* Parse the id-expression. */
3054 = cp_parser_id_expression (parser,
3055 /*template_keyword_p=*/false,
3056 /*check_dependency_p=*/true,
3058 /*declarator_p=*/false,
3059 /*optional_p=*/false);
3060 if (id_expression == error_mark_node)
3061 return error_mark_node;
3062 token = cp_lexer_peek_token (parser->lexer);
3063 done = (token->type != CPP_OPEN_SQUARE
3064 && token->type != CPP_OPEN_PAREN
3065 && token->type != CPP_DOT
3066 && token->type != CPP_DEREF
3067 && token->type != CPP_PLUS_PLUS
3068 && token->type != CPP_MINUS_MINUS);
3069 /* If we have a template-id, then no further lookup is
3070 required. If the template-id was for a template-class, we
3071 will sometimes have a TYPE_DECL at this point. */
3072 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3073 || TREE_CODE (id_expression) == TYPE_DECL)
3074 decl = id_expression;
3075 /* Look up the name. */
3078 tree ambiguous_decls;
3080 decl = cp_parser_lookup_name (parser, id_expression,
3083 /*is_namespace=*/false,
3084 /*check_dependency=*/true,
3086 /* If the lookup was ambiguous, an error will already have
3088 if (ambiguous_decls)
3089 return error_mark_node;
3091 /* In Objective-C++, an instance variable (ivar) may be preferred
3092 to whatever cp_parser_lookup_name() found. */
3093 decl = objc_lookup_ivar (decl, id_expression);
3095 /* If name lookup gives us a SCOPE_REF, then the
3096 qualifying scope was dependent. */
3097 if (TREE_CODE (decl) == SCOPE_REF)
3099 /* Check to see if DECL is a local variable in a context
3100 where that is forbidden. */
3101 if (parser->local_variables_forbidden_p
3102 && local_variable_p (decl))
3104 /* It might be that we only found DECL because we are
3105 trying to be generous with pre-ISO scoping rules.
3106 For example, consider:
3110 for (int i = 0; i < 10; ++i) {}
3111 extern void f(int j = i);
3114 Here, name look up will originally find the out
3115 of scope `i'. We need to issue a warning message,
3116 but then use the global `i'. */
3117 decl = check_for_out_of_scope_variable (decl);
3118 if (local_variable_p (decl))
3120 error ("local variable %qD may not appear in this context",
3122 return error_mark_node;
3127 decl = (finish_id_expression
3128 (id_expression, decl, parser->scope,
3130 parser->integral_constant_expression_p,
3131 parser->allow_non_integral_constant_expression_p,
3132 &parser->non_integral_constant_expression_p,
3133 template_p, done, address_p,
3137 cp_parser_error (parser, error_msg);
3141 /* Anything else is an error. */
3143 /* ...unless we have an Objective-C++ message or string literal, that is. */
3144 if (c_dialect_objc ()
3145 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3146 return cp_parser_objc_expression (parser);
3148 cp_parser_error (parser, "expected primary-expression");
3149 return error_mark_node;
3153 /* Parse an id-expression.
3160 :: [opt] nested-name-specifier template [opt] unqualified-id
3162 :: operator-function-id
3165 Return a representation of the unqualified portion of the
3166 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3167 a `::' or nested-name-specifier.
3169 Often, if the id-expression was a qualified-id, the caller will
3170 want to make a SCOPE_REF to represent the qualified-id. This
3171 function does not do this in order to avoid wastefully creating
3172 SCOPE_REFs when they are not required.
3174 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3177 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3178 uninstantiated templates.
3180 If *TEMPLATE_P is non-NULL, it is set to true iff the
3181 `template' keyword is used to explicitly indicate that the entity
3182 named is a template.
3184 If DECLARATOR_P is true, the id-expression is appearing as part of
3185 a declarator, rather than as part of an expression. */
3188 cp_parser_id_expression (cp_parser *parser,
3189 bool template_keyword_p,
3190 bool check_dependency_p,
3195 bool global_scope_p;
3196 bool nested_name_specifier_p;
3198 /* Assume the `template' keyword was not used. */
3200 *template_p = template_keyword_p;
3202 /* Look for the optional `::' operator. */
3204 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3206 /* Look for the optional nested-name-specifier. */
3207 nested_name_specifier_p
3208 = (cp_parser_nested_name_specifier_opt (parser,
3209 /*typename_keyword_p=*/false,
3214 /* If there is a nested-name-specifier, then we are looking at
3215 the first qualified-id production. */
3216 if (nested_name_specifier_p)
3219 tree saved_object_scope;
3220 tree saved_qualifying_scope;
3221 tree unqualified_id;
3224 /* See if the next token is the `template' keyword. */
3226 template_p = &is_template;
3227 *template_p = cp_parser_optional_template_keyword (parser);
3228 /* Name lookup we do during the processing of the
3229 unqualified-id might obliterate SCOPE. */
3230 saved_scope = parser->scope;
3231 saved_object_scope = parser->object_scope;
3232 saved_qualifying_scope = parser->qualifying_scope;
3233 /* Process the final unqualified-id. */
3234 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3237 /*optional_p=*/false);
3238 /* Restore the SAVED_SCOPE for our caller. */
3239 parser->scope = saved_scope;
3240 parser->object_scope = saved_object_scope;
3241 parser->qualifying_scope = saved_qualifying_scope;
3243 return unqualified_id;
3245 /* Otherwise, if we are in global scope, then we are looking at one
3246 of the other qualified-id productions. */
3247 else if (global_scope_p)
3252 /* Peek at the next token. */
3253 token = cp_lexer_peek_token (parser->lexer);
3255 /* If it's an identifier, and the next token is not a "<", then
3256 we can avoid the template-id case. This is an optimization
3257 for this common case. */
3258 if (token->type == CPP_NAME
3259 && !cp_parser_nth_token_starts_template_argument_list_p
3261 return cp_parser_identifier (parser);
3263 cp_parser_parse_tentatively (parser);
3264 /* Try a template-id. */
3265 id = cp_parser_template_id (parser,
3266 /*template_keyword_p=*/false,
3267 /*check_dependency_p=*/true,
3269 /* If that worked, we're done. */
3270 if (cp_parser_parse_definitely (parser))
3273 /* Peek at the next token. (Changes in the token buffer may
3274 have invalidated the pointer obtained above.) */
3275 token = cp_lexer_peek_token (parser->lexer);
3277 switch (token->type)
3280 return cp_parser_identifier (parser);
3283 if (token->keyword == RID_OPERATOR)
3284 return cp_parser_operator_function_id (parser);
3288 cp_parser_error (parser, "expected id-expression");
3289 return error_mark_node;
3293 return cp_parser_unqualified_id (parser, template_keyword_p,
3294 /*check_dependency_p=*/true,
3299 /* Parse an unqualified-id.
3303 operator-function-id
3304 conversion-function-id
3308 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3309 keyword, in a construct like `A::template ...'.
3311 Returns a representation of unqualified-id. For the `identifier'
3312 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3313 production a BIT_NOT_EXPR is returned; the operand of the
3314 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3315 other productions, see the documentation accompanying the
3316 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3317 names are looked up in uninstantiated templates. If DECLARATOR_P
3318 is true, the unqualified-id is appearing as part of a declarator,
3319 rather than as part of an expression. */
3322 cp_parser_unqualified_id (cp_parser* parser,
3323 bool template_keyword_p,
3324 bool check_dependency_p,
3330 /* Peek at the next token. */
3331 token = cp_lexer_peek_token (parser->lexer);
3333 switch (token->type)
3339 /* We don't know yet whether or not this will be a
3341 cp_parser_parse_tentatively (parser);
3342 /* Try a template-id. */
3343 id = cp_parser_template_id (parser, template_keyword_p,
3346 /* If it worked, we're done. */
3347 if (cp_parser_parse_definitely (parser))
3349 /* Otherwise, it's an ordinary identifier. */
3350 return cp_parser_identifier (parser);
3353 case CPP_TEMPLATE_ID:
3354 return cp_parser_template_id (parser, template_keyword_p,
3361 tree qualifying_scope;
3366 /* Consume the `~' token. */
3367 cp_lexer_consume_token (parser->lexer);
3368 /* Parse the class-name. The standard, as written, seems to
3371 template <typename T> struct S { ~S (); };
3372 template <typename T> S<T>::~S() {}
3374 is invalid, since `~' must be followed by a class-name, but
3375 `S<T>' is dependent, and so not known to be a class.
3376 That's not right; we need to look in uninstantiated
3377 templates. A further complication arises from:
3379 template <typename T> void f(T t) {
3383 Here, it is not possible to look up `T' in the scope of `T'
3384 itself. We must look in both the current scope, and the
3385 scope of the containing complete expression.
3387 Yet another issue is:
3396 The standard does not seem to say that the `S' in `~S'
3397 should refer to the type `S' and not the data member
3400 /* DR 244 says that we look up the name after the "~" in the
3401 same scope as we looked up the qualifying name. That idea
3402 isn't fully worked out; it's more complicated than that. */
3403 scope = parser->scope;
3404 object_scope = parser->object_scope;
3405 qualifying_scope = parser->qualifying_scope;
3407 /* Check for invalid scopes. */
3408 if (scope == error_mark_node)
3410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3411 cp_lexer_consume_token (parser->lexer);
3412 return error_mark_node;
3414 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3416 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3417 error ("scope %qT before %<~%> is not a class-name", scope);
3418 cp_parser_simulate_error (parser);
3419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3420 cp_lexer_consume_token (parser->lexer);
3421 return error_mark_node;
3423 gcc_assert (!scope || TYPE_P (scope));
3425 /* If the name is of the form "X::~X" it's OK. */
3426 token = cp_lexer_peek_token (parser->lexer);
3428 && token->type == CPP_NAME
3429 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3431 && constructor_name_p (token->value, scope))
3433 cp_lexer_consume_token (parser->lexer);
3434 return build_nt (BIT_NOT_EXPR, scope);
3437 /* If there was an explicit qualification (S::~T), first look
3438 in the scope given by the qualification (i.e., S). */
3440 type_decl = NULL_TREE;
3443 cp_parser_parse_tentatively (parser);
3444 type_decl = cp_parser_class_name (parser,
3445 /*typename_keyword_p=*/false,
3446 /*template_keyword_p=*/false,
3448 /*check_dependency=*/false,
3449 /*class_head_p=*/false,
3451 if (cp_parser_parse_definitely (parser))
3454 /* In "N::S::~S", look in "N" as well. */
3455 if (!done && scope && qualifying_scope)
3457 cp_parser_parse_tentatively (parser);
3458 parser->scope = qualifying_scope;
3459 parser->object_scope = NULL_TREE;
3460 parser->qualifying_scope = NULL_TREE;
3462 = cp_parser_class_name (parser,
3463 /*typename_keyword_p=*/false,
3464 /*template_keyword_p=*/false,
3466 /*check_dependency=*/false,
3467 /*class_head_p=*/false,
3469 if (cp_parser_parse_definitely (parser))
3472 /* In "p->S::~T", look in the scope given by "*p" as well. */
3473 else if (!done && object_scope)
3475 cp_parser_parse_tentatively (parser);
3476 parser->scope = object_scope;
3477 parser->object_scope = NULL_TREE;
3478 parser->qualifying_scope = NULL_TREE;
3480 = cp_parser_class_name (parser,
3481 /*typename_keyword_p=*/false,
3482 /*template_keyword_p=*/false,
3484 /*check_dependency=*/false,
3485 /*class_head_p=*/false,
3487 if (cp_parser_parse_definitely (parser))
3490 /* Look in the surrounding context. */
3493 parser->scope = NULL_TREE;
3494 parser->object_scope = NULL_TREE;
3495 parser->qualifying_scope = NULL_TREE;
3497 = cp_parser_class_name (parser,
3498 /*typename_keyword_p=*/false,
3499 /*template_keyword_p=*/false,
3501 /*check_dependency=*/false,
3502 /*class_head_p=*/false,
3505 /* If an error occurred, assume that the name of the
3506 destructor is the same as the name of the qualifying
3507 class. That allows us to keep parsing after running
3508 into ill-formed destructor names. */
3509 if (type_decl == error_mark_node && scope)
3510 return build_nt (BIT_NOT_EXPR, scope);
3511 else if (type_decl == error_mark_node)
3512 return error_mark_node;
3514 /* Check that destructor name and scope match. */
3515 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3517 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3518 error ("declaration of %<~%T%> as member of %qT",
3520 cp_parser_simulate_error (parser);
3521 return error_mark_node;
3526 A typedef-name that names a class shall not be used as the
3527 identifier in the declarator for a destructor declaration. */
3529 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3530 && !DECL_SELF_REFERENCE_P (type_decl)
3531 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3532 error ("typedef-name %qD used as destructor declarator",
3535 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3539 if (token->keyword == RID_OPERATOR)
3543 /* This could be a template-id, so we try that first. */
3544 cp_parser_parse_tentatively (parser);
3545 /* Try a template-id. */
3546 id = cp_parser_template_id (parser, template_keyword_p,
3547 /*check_dependency_p=*/true,
3549 /* If that worked, we're done. */
3550 if (cp_parser_parse_definitely (parser))
3552 /* We still don't know whether we're looking at an
3553 operator-function-id or a conversion-function-id. */
3554 cp_parser_parse_tentatively (parser);
3555 /* Try an operator-function-id. */
3556 id = cp_parser_operator_function_id (parser);
3557 /* If that didn't work, try a conversion-function-id. */
3558 if (!cp_parser_parse_definitely (parser))
3559 id = cp_parser_conversion_function_id (parser);
3568 cp_parser_error (parser, "expected unqualified-id");
3569 return error_mark_node;
3573 /* Parse an (optional) nested-name-specifier.
3575 nested-name-specifier:
3576 class-or-namespace-name :: nested-name-specifier [opt]
3577 class-or-namespace-name :: template nested-name-specifier [opt]
3579 PARSER->SCOPE should be set appropriately before this function is
3580 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3581 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3584 Sets PARSER->SCOPE to the class (TYPE) or namespace
3585 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3586 it unchanged if there is no nested-name-specifier. Returns the new
3587 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3589 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3590 part of a declaration and/or decl-specifier. */
3593 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3594 bool typename_keyword_p,
3595 bool check_dependency_p,
3597 bool is_declaration)
3599 bool success = false;
3600 cp_token_position start = 0;
3603 /* Remember where the nested-name-specifier starts. */
3604 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3606 start = cp_lexer_token_position (parser->lexer, false);
3607 push_deferring_access_checks (dk_deferred);
3614 tree saved_qualifying_scope;
3615 bool template_keyword_p;
3617 /* Spot cases that cannot be the beginning of a
3618 nested-name-specifier. */
3619 token = cp_lexer_peek_token (parser->lexer);
3621 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3622 the already parsed nested-name-specifier. */
3623 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3625 /* Grab the nested-name-specifier and continue the loop. */
3626 cp_parser_pre_parsed_nested_name_specifier (parser);
3631 /* Spot cases that cannot be the beginning of a
3632 nested-name-specifier. On the second and subsequent times
3633 through the loop, we look for the `template' keyword. */
3634 if (success && token->keyword == RID_TEMPLATE)
3636 /* A template-id can start a nested-name-specifier. */
3637 else if (token->type == CPP_TEMPLATE_ID)
3641 /* If the next token is not an identifier, then it is
3642 definitely not a class-or-namespace-name. */
3643 if (token->type != CPP_NAME)
3645 /* If the following token is neither a `<' (to begin a
3646 template-id), nor a `::', then we are not looking at a
3647 nested-name-specifier. */
3648 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3649 if (token->type != CPP_SCOPE
3650 && !cp_parser_nth_token_starts_template_argument_list_p
3655 /* The nested-name-specifier is optional, so we parse
3657 cp_parser_parse_tentatively (parser);
3659 /* Look for the optional `template' keyword, if this isn't the
3660 first time through the loop. */
3662 template_keyword_p = cp_parser_optional_template_keyword (parser);
3664 template_keyword_p = false;
3666 /* Save the old scope since the name lookup we are about to do
3667 might destroy it. */
3668 old_scope = parser->scope;
3669 saved_qualifying_scope = parser->qualifying_scope;
3670 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3671 look up names in "X<T>::I" in order to determine that "Y" is
3672 a template. So, if we have a typename at this point, we make
3673 an effort to look through it. */
3675 && !typename_keyword_p
3677 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3678 parser->scope = resolve_typename_type (parser->scope,
3679 /*only_current_p=*/false);
3680 /* Parse the qualifying entity. */
3682 = cp_parser_class_or_namespace_name (parser,
3688 /* Look for the `::' token. */
3689 cp_parser_require (parser, CPP_SCOPE, "`::'");
3691 /* If we found what we wanted, we keep going; otherwise, we're
3693 if (!cp_parser_parse_definitely (parser))
3695 bool error_p = false;
3697 /* Restore the OLD_SCOPE since it was valid before the
3698 failed attempt at finding the last
3699 class-or-namespace-name. */
3700 parser->scope = old_scope;
3701 parser->qualifying_scope = saved_qualifying_scope;
3702 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3704 /* If the next token is an identifier, and the one after
3705 that is a `::', then any valid interpretation would have
3706 found a class-or-namespace-name. */
3707 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3708 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3710 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3713 token = cp_lexer_consume_token (parser->lexer);
3716 if (!token->ambiguous_p)
3719 tree ambiguous_decls;
3721 decl = cp_parser_lookup_name (parser, token->value,
3723 /*is_template=*/false,
3724 /*is_namespace=*/false,
3725 /*check_dependency=*/true,
3727 if (TREE_CODE (decl) == TEMPLATE_DECL)
3728 error ("%qD used without template parameters", decl);
3729 else if (ambiguous_decls)
3731 error ("reference to %qD is ambiguous",
3733 print_candidates (ambiguous_decls);
3734 decl = error_mark_node;
3737 cp_parser_name_lookup_error
3738 (parser, token->value, decl,
3739 "is not a class or namespace");
3741 parser->scope = error_mark_node;
3743 /* Treat this as a successful nested-name-specifier
3748 If the name found is not a class-name (clause
3749 _class_) or namespace-name (_namespace.def_), the
3750 program is ill-formed. */
3753 cp_lexer_consume_token (parser->lexer);
3757 /* We've found one valid nested-name-specifier. */
3759 /* Name lookup always gives us a DECL. */
3760 if (TREE_CODE (new_scope) == TYPE_DECL)
3761 new_scope = TREE_TYPE (new_scope);
3762 /* Uses of "template" must be followed by actual templates. */
3763 if (template_keyword_p
3764 && !(CLASS_TYPE_P (new_scope)
3765 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3766 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3767 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3768 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3769 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3770 == TEMPLATE_ID_EXPR)))
3771 pedwarn (TYPE_P (new_scope)
3772 ? "%qT is not a template"
3773 : "%qD is not a template",
3775 /* If it is a class scope, try to complete it; we are about to
3776 be looking up names inside the class. */
3777 if (TYPE_P (new_scope)
3778 /* Since checking types for dependency can be expensive,
3779 avoid doing it if the type is already complete. */
3780 && !COMPLETE_TYPE_P (new_scope)
3781 /* Do not try to complete dependent types. */
3782 && !dependent_type_p (new_scope))
3783 new_scope = complete_type (new_scope);
3784 /* Make sure we look in the right scope the next time through
3786 parser->scope = new_scope;
3789 /* If parsing tentatively, replace the sequence of tokens that makes
3790 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3791 token. That way, should we re-parse the token stream, we will
3792 not have to repeat the effort required to do the parse, nor will
3793 we issue duplicate error messages. */
3794 if (success && start)
3799 token = cp_lexer_token_at (parser->lexer, start);
3800 /* Reset the contents of the START token. */
3801 token->type = CPP_NESTED_NAME_SPECIFIER;
3802 /* Retrieve any deferred checks. Do not pop this access checks yet
3803 so the memory will not be reclaimed during token replacing below. */
3804 access_checks = get_deferred_access_checks ();
3805 token->value = build_tree_list (copy_list (access_checks),
3807 TREE_TYPE (token->value) = parser->qualifying_scope;
3808 token->keyword = RID_MAX;
3810 /* Purge all subsequent tokens. */
3811 cp_lexer_purge_tokens_after (parser->lexer, start);
3815 pop_to_parent_deferring_access_checks ();
3817 return success ? parser->scope : NULL_TREE;
3820 /* Parse a nested-name-specifier. See
3821 cp_parser_nested_name_specifier_opt for details. This function
3822 behaves identically, except that it will an issue an error if no
3823 nested-name-specifier is present. */
3826 cp_parser_nested_name_specifier (cp_parser *parser,
3827 bool typename_keyword_p,
3828 bool check_dependency_p,
3830 bool is_declaration)
3834 /* Look for the nested-name-specifier. */
3835 scope = cp_parser_nested_name_specifier_opt (parser,
3840 /* If it was not present, issue an error message. */
3843 cp_parser_error (parser, "expected nested-name-specifier");
3844 parser->scope = NULL_TREE;
3850 /* Parse a class-or-namespace-name.
3852 class-or-namespace-name:
3856 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3857 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3858 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3859 TYPE_P is TRUE iff the next name should be taken as a class-name,
3860 even the same name is declared to be another entity in the same
3863 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3864 specified by the class-or-namespace-name. If neither is found the
3865 ERROR_MARK_NODE is returned. */
3868 cp_parser_class_or_namespace_name (cp_parser *parser,
3869 bool typename_keyword_p,
3870 bool template_keyword_p,
3871 bool check_dependency_p,
3873 bool is_declaration)
3876 tree saved_qualifying_scope;
3877 tree saved_object_scope;
3881 /* Before we try to parse the class-name, we must save away the
3882 current PARSER->SCOPE since cp_parser_class_name will destroy
3884 saved_scope = parser->scope;
3885 saved_qualifying_scope = parser->qualifying_scope;
3886 saved_object_scope = parser->object_scope;
3887 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3888 there is no need to look for a namespace-name. */
3889 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3891 cp_parser_parse_tentatively (parser);
3892 scope = cp_parser_class_name (parser,
3895 type_p ? class_type : none_type,
3897 /*class_head_p=*/false,
3899 /* If that didn't work, try for a namespace-name. */
3900 if (!only_class_p && !cp_parser_parse_definitely (parser))
3902 /* Restore the saved scope. */
3903 parser->scope = saved_scope;
3904 parser->qualifying_scope = saved_qualifying_scope;
3905 parser->object_scope = saved_object_scope;
3906 /* If we are not looking at an identifier followed by the scope
3907 resolution operator, then this is not part of a
3908 nested-name-specifier. (Note that this function is only used
3909 to parse the components of a nested-name-specifier.) */
3910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3911 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3912 return error_mark_node;
3913 scope = cp_parser_namespace_name (parser);
3919 /* Parse a postfix-expression.
3923 postfix-expression [ expression ]
3924 postfix-expression ( expression-list [opt] )
3925 simple-type-specifier ( expression-list [opt] )
3926 typename :: [opt] nested-name-specifier identifier
3927 ( expression-list [opt] )
3928 typename :: [opt] nested-name-specifier template [opt] template-id
3929 ( expression-list [opt] )
3930 postfix-expression . template [opt] id-expression
3931 postfix-expression -> template [opt] id-expression
3932 postfix-expression . pseudo-destructor-name
3933 postfix-expression -> pseudo-destructor-name
3934 postfix-expression ++
3935 postfix-expression --
3936 dynamic_cast < type-id > ( expression )
3937 static_cast < type-id > ( expression )
3938 reinterpret_cast < type-id > ( expression )
3939 const_cast < type-id > ( expression )
3940 typeid ( expression )
3946 ( type-id ) { initializer-list , [opt] }
3948 This extension is a GNU version of the C99 compound-literal
3949 construct. (The C99 grammar uses `type-name' instead of `type-id',
3950 but they are essentially the same concept.)
3952 If ADDRESS_P is true, the postfix expression is the operand of the
3953 `&' operator. CAST_P is true if this expression is the target of a
3956 Returns a representation of the expression. */
3959 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3963 cp_id_kind idk = CP_ID_KIND_NONE;
3964 tree postfix_expression = NULL_TREE;
3966 /* Peek at the next token. */
3967 token = cp_lexer_peek_token (parser->lexer);
3968 /* Some of the productions are determined by keywords. */
3969 keyword = token->keyword;
3979 const char *saved_message;
3981 /* All of these can be handled in the same way from the point
3982 of view of parsing. Begin by consuming the token
3983 identifying the cast. */
3984 cp_lexer_consume_token (parser->lexer);
3986 /* New types cannot be defined in the cast. */
3987 saved_message = parser->type_definition_forbidden_message;
3988 parser->type_definition_forbidden_message
3989 = "types may not be defined in casts";
3991 /* Look for the opening `<'. */
3992 cp_parser_require (parser, CPP_LESS, "`<'");
3993 /* Parse the type to which we are casting. */
3994 type = cp_parser_type_id (parser);
3995 /* Look for the closing `>'. */
3996 cp_parser_require (parser, CPP_GREATER, "`>'");
3997 /* Restore the old message. */
3998 parser->type_definition_forbidden_message = saved_message;
4000 /* And the expression which is being cast. */
4001 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4002 expression = cp_parser_expression (parser, /*cast_p=*/true);
4003 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4005 /* Only type conversions to integral or enumeration types
4006 can be used in constant-expressions. */
4007 if (parser->integral_constant_expression_p
4008 && !dependent_type_p (type)
4009 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4010 && (cp_parser_non_integral_constant_expression
4012 "a cast to a type other than an integral or "
4013 "enumeration type")))
4014 return error_mark_node;
4020 = build_dynamic_cast (type, expression);
4024 = build_static_cast (type, expression);
4028 = build_reinterpret_cast (type, expression);
4032 = build_const_cast (type, expression);
4043 const char *saved_message;
4044 bool saved_in_type_id_in_expr_p;
4046 /* Consume the `typeid' token. */
4047 cp_lexer_consume_token (parser->lexer);
4048 /* Look for the `(' token. */
4049 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4050 /* Types cannot be defined in a `typeid' expression. */
4051 saved_message = parser->type_definition_forbidden_message;
4052 parser->type_definition_forbidden_message
4053 = "types may not be defined in a `typeid\' expression";
4054 /* We can't be sure yet whether we're looking at a type-id or an
4056 cp_parser_parse_tentatively (parser);
4057 /* Try a type-id first. */
4058 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4059 parser->in_type_id_in_expr_p = true;
4060 type = cp_parser_type_id (parser);
4061 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4062 /* Look for the `)' token. Otherwise, we can't be sure that
4063 we're not looking at an expression: consider `typeid (int
4064 (3))', for example. */
4065 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4066 /* If all went well, simply lookup the type-id. */
4067 if (cp_parser_parse_definitely (parser))
4068 postfix_expression = get_typeid (type);
4069 /* Otherwise, fall back to the expression variant. */
4074 /* Look for an expression. */
4075 expression = cp_parser_expression (parser, /*cast_p=*/false);
4076 /* Compute its typeid. */
4077 postfix_expression = build_typeid (expression);
4078 /* Look for the `)' token. */
4079 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4081 /* `typeid' may not appear in an integral constant expression. */
4082 if (cp_parser_non_integral_constant_expression(parser,
4083 "`typeid' operator"))
4084 return error_mark_node;
4085 /* Restore the saved message. */
4086 parser->type_definition_forbidden_message = saved_message;
4093 /* The syntax permitted here is the same permitted for an
4094 elaborated-type-specifier. */
4095 type = cp_parser_elaborated_type_specifier (parser,
4096 /*is_friend=*/false,
4097 /*is_declaration=*/false);
4098 postfix_expression = cp_parser_functional_cast (parser, type);
4106 /* If the next thing is a simple-type-specifier, we may be
4107 looking at a functional cast. We could also be looking at
4108 an id-expression. So, we try the functional cast, and if
4109 that doesn't work we fall back to the primary-expression. */
4110 cp_parser_parse_tentatively (parser);
4111 /* Look for the simple-type-specifier. */
4112 type = cp_parser_simple_type_specifier (parser,
4113 /*decl_specs=*/NULL,
4114 CP_PARSER_FLAGS_NONE);
4115 /* Parse the cast itself. */
4116 if (!cp_parser_error_occurred (parser))
4118 = cp_parser_functional_cast (parser, type);
4119 /* If that worked, we're done. */
4120 if (cp_parser_parse_definitely (parser))
4123 /* If the functional-cast didn't work out, try a
4124 compound-literal. */
4125 if (cp_parser_allow_gnu_extensions_p (parser)
4126 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4128 VEC(constructor_elt,gc) *initializer_list = NULL;
4129 bool saved_in_type_id_in_expr_p;
4131 cp_parser_parse_tentatively (parser);
4132 /* Consume the `('. */
4133 cp_lexer_consume_token (parser->lexer);
4134 /* Parse the type. */
4135 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4136 parser->in_type_id_in_expr_p = true;
4137 type = cp_parser_type_id (parser);
4138 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4139 /* Look for the `)'. */
4140 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4141 /* Look for the `{'. */
4142 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4143 /* If things aren't going well, there's no need to
4145 if (!cp_parser_error_occurred (parser))
4147 bool non_constant_p;
4148 /* Parse the initializer-list. */
4150 = cp_parser_initializer_list (parser, &non_constant_p);
4151 /* Allow a trailing `,'. */
4152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4153 cp_lexer_consume_token (parser->lexer);
4154 /* Look for the final `}'. */
4155 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4157 /* If that worked, we're definitely looking at a
4158 compound-literal expression. */
4159 if (cp_parser_parse_definitely (parser))
4161 /* Warn the user that a compound literal is not
4162 allowed in standard C++. */
4164 pedwarn ("ISO C++ forbids compound-literals");
4165 /* Form the representation of the compound-literal. */
4167 = finish_compound_literal (type, initializer_list);
4172 /* It must be a primary-expression. */
4174 = cp_parser_primary_expression (parser, address_p, cast_p,
4175 /*template_arg_p=*/false,
4181 /* Keep looping until the postfix-expression is complete. */
4184 if (idk == CP_ID_KIND_UNQUALIFIED
4185 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4186 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4187 /* It is not a Koenig lookup function call. */
4189 = unqualified_name_lookup_error (postfix_expression);
4191 /* Peek at the next token. */
4192 token = cp_lexer_peek_token (parser->lexer);
4194 switch (token->type)
4196 case CPP_OPEN_SQUARE:
4198 = cp_parser_postfix_open_square_expression (parser,
4201 idk = CP_ID_KIND_NONE;
4204 case CPP_OPEN_PAREN:
4205 /* postfix-expression ( expression-list [opt] ) */
4208 bool is_builtin_constant_p;
4209 bool saved_integral_constant_expression_p = false;
4210 bool saved_non_integral_constant_expression_p = false;
4213 is_builtin_constant_p
4214 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4215 if (is_builtin_constant_p)
4217 /* The whole point of __builtin_constant_p is to allow
4218 non-constant expressions to appear as arguments. */
4219 saved_integral_constant_expression_p
4220 = parser->integral_constant_expression_p;
4221 saved_non_integral_constant_expression_p
4222 = parser->non_integral_constant_expression_p;
4223 parser->integral_constant_expression_p = false;
4225 args = (cp_parser_parenthesized_expression_list
4226 (parser, /*is_attribute_list=*/false,
4228 /*non_constant_p=*/NULL));
4229 if (is_builtin_constant_p)
4231 parser->integral_constant_expression_p
4232 = saved_integral_constant_expression_p;
4233 parser->non_integral_constant_expression_p
4234 = saved_non_integral_constant_expression_p;
4237 if (args == error_mark_node)
4239 postfix_expression = error_mark_node;
4243 /* Function calls are not permitted in
4244 constant-expressions. */
4245 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4246 && cp_parser_non_integral_constant_expression (parser,
4249 postfix_expression = error_mark_node;
4254 if (idk == CP_ID_KIND_UNQUALIFIED)
4256 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4262 = perform_koenig_lookup (postfix_expression, args);
4266 = unqualified_fn_lookup_error (postfix_expression);
4268 /* We do not perform argument-dependent lookup if
4269 normal lookup finds a non-function, in accordance
4270 with the expected resolution of DR 218. */
4271 else if (args && is_overloaded_fn (postfix_expression))
4273 tree fn = get_first_fn (postfix_expression);
4275 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4276 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4278 /* Only do argument dependent lookup if regular
4279 lookup does not find a set of member functions.
4280 [basic.lookup.koenig]/2a */
4281 if (!DECL_FUNCTION_MEMBER_P (fn))
4285 = perform_koenig_lookup (postfix_expression, args);
4290 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4292 tree instance = TREE_OPERAND (postfix_expression, 0);
4293 tree fn = TREE_OPERAND (postfix_expression, 1);
4295 if (processing_template_decl
4296 && (type_dependent_expression_p (instance)
4297 || (!BASELINK_P (fn)
4298 && TREE_CODE (fn) != FIELD_DECL)
4299 || type_dependent_expression_p (fn)
4300 || any_type_dependent_arguments_p (args)))
4303 = build_min_nt (CALL_EXPR, postfix_expression,
4308 if (BASELINK_P (fn))
4310 = (build_new_method_call
4311 (instance, fn, args, NULL_TREE,
4312 (idk == CP_ID_KIND_QUALIFIED
4313 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4317 = finish_call_expr (postfix_expression, args,
4318 /*disallow_virtual=*/false,
4319 /*koenig_p=*/false);
4321 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4322 || TREE_CODE (postfix_expression) == MEMBER_REF
4323 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4324 postfix_expression = (build_offset_ref_call_from_tree
4325 (postfix_expression, args));
4326 else if (idk == CP_ID_KIND_QUALIFIED)
4327 /* A call to a static class member, or a namespace-scope
4330 = finish_call_expr (postfix_expression, args,
4331 /*disallow_virtual=*/true,
4334 /* All other function calls. */
4336 = finish_call_expr (postfix_expression, args,
4337 /*disallow_virtual=*/false,
4340 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4341 idk = CP_ID_KIND_NONE;
4347 /* postfix-expression . template [opt] id-expression
4348 postfix-expression . pseudo-destructor-name
4349 postfix-expression -> template [opt] id-expression
4350 postfix-expression -> pseudo-destructor-name */
4352 /* Consume the `.' or `->' operator. */
4353 cp_lexer_consume_token (parser->lexer);
4356 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4362 /* postfix-expression ++ */
4363 /* Consume the `++' token. */
4364 cp_lexer_consume_token (parser->lexer);
4365 /* Generate a representation for the complete expression. */
4367 = finish_increment_expr (postfix_expression,
4368 POSTINCREMENT_EXPR);
4369 /* Increments may not appear in constant-expressions. */
4370 if (cp_parser_non_integral_constant_expression (parser,
4372 postfix_expression = error_mark_node;
4373 idk = CP_ID_KIND_NONE;
4376 case CPP_MINUS_MINUS:
4377 /* postfix-expression -- */
4378 /* Consume the `--' token. */
4379 cp_lexer_consume_token (parser->lexer);
4380 /* Generate a representation for the complete expression. */
4382 = finish_increment_expr (postfix_expression,
4383 POSTDECREMENT_EXPR);
4384 /* Decrements may not appear in constant-expressions. */
4385 if (cp_parser_non_integral_constant_expression (parser,
4387 postfix_expression = error_mark_node;
4388 idk = CP_ID_KIND_NONE;
4392 return postfix_expression;
4396 /* We should never get here. */
4398 return error_mark_node;
4401 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4402 by cp_parser_builtin_offsetof. We're looking for
4404 postfix-expression [ expression ]
4406 FOR_OFFSETOF is set if we're being called in that context, which
4407 changes how we deal with integer constant expressions. */
4410 cp_parser_postfix_open_square_expression (cp_parser *parser,
4411 tree postfix_expression,
4416 /* Consume the `[' token. */
4417 cp_lexer_consume_token (parser->lexer);
4419 /* Parse the index expression. */
4420 /* ??? For offsetof, there is a question of what to allow here. If
4421 offsetof is not being used in an integral constant expression context,
4422 then we *could* get the right answer by computing the value at runtime.
4423 If we are in an integral constant expression context, then we might
4424 could accept any constant expression; hard to say without analysis.
4425 Rather than open the barn door too wide right away, allow only integer
4426 constant expressions here. */
4428 index = cp_parser_constant_expression (parser, false, NULL);
4430 index = cp_parser_expression (parser, /*cast_p=*/false);
4432 /* Look for the closing `]'. */
4433 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4435 /* Build the ARRAY_REF. */
4436 postfix_expression = grok_array_decl (postfix_expression, index);
4438 /* When not doing offsetof, array references are not permitted in
4439 constant-expressions. */
4441 && (cp_parser_non_integral_constant_expression
4442 (parser, "an array reference")))
4443 postfix_expression = error_mark_node;
4445 return postfix_expression;
4448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4449 by cp_parser_builtin_offsetof. We're looking for
4451 postfix-expression . template [opt] id-expression
4452 postfix-expression . pseudo-destructor-name
4453 postfix-expression -> template [opt] id-expression
4454 postfix-expression -> pseudo-destructor-name
4456 FOR_OFFSETOF is set if we're being called in that context. That sorta
4457 limits what of the above we'll actually accept, but nevermind.
4458 TOKEN_TYPE is the "." or "->" token, which will already have been
4459 removed from the stream. */
4462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4463 enum cpp_ttype token_type,
4464 tree postfix_expression,
4465 bool for_offsetof, cp_id_kind *idk)
4469 bool pseudo_destructor_p;
4470 tree scope = NULL_TREE;
4472 /* If this is a `->' operator, dereference the pointer. */
4473 if (token_type == CPP_DEREF)
4474 postfix_expression = build_x_arrow (postfix_expression);
4475 /* Check to see whether or not the expression is type-dependent. */
4476 dependent_p = type_dependent_expression_p (postfix_expression);
4477 /* The identifier following the `->' or `.' is not qualified. */
4478 parser->scope = NULL_TREE;
4479 parser->qualifying_scope = NULL_TREE;
4480 parser->object_scope = NULL_TREE;
4481 *idk = CP_ID_KIND_NONE;
4482 /* Enter the scope corresponding to the type of the object
4483 given by the POSTFIX_EXPRESSION. */
4484 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4486 scope = TREE_TYPE (postfix_expression);
4487 /* According to the standard, no expression should ever have
4488 reference type. Unfortunately, we do not currently match
4489 the standard in this respect in that our internal representation
4490 of an expression may have reference type even when the standard
4491 says it does not. Therefore, we have to manually obtain the
4492 underlying type here. */
4493 scope = non_reference (scope);
4494 /* The type of the POSTFIX_EXPRESSION must be complete. */
4495 if (scope == unknown_type_node)
4497 error ("%qE does not have class type", postfix_expression);
4501 scope = complete_type_or_else (scope, NULL_TREE);
4502 /* Let the name lookup machinery know that we are processing a
4503 class member access expression. */
4504 parser->context->object_type = scope;
4505 /* If something went wrong, we want to be able to discern that case,
4506 as opposed to the case where there was no SCOPE due to the type
4507 of expression being dependent. */
4509 scope = error_mark_node;
4510 /* If the SCOPE was erroneous, make the various semantic analysis
4511 functions exit quickly -- and without issuing additional error
4513 if (scope == error_mark_node)
4514 postfix_expression = error_mark_node;
4517 /* Assume this expression is not a pseudo-destructor access. */
4518 pseudo_destructor_p = false;
4520 /* If the SCOPE is a scalar type, then, if this is a valid program,
4521 we must be looking at a pseudo-destructor-name. */
4522 if (scope && SCALAR_TYPE_P (scope))
4527 cp_parser_parse_tentatively (parser);
4528 /* Parse the pseudo-destructor-name. */
4530 cp_parser_pseudo_destructor_name (parser, &s, &type);
4531 if (cp_parser_parse_definitely (parser))
4533 pseudo_destructor_p = true;
4535 = finish_pseudo_destructor_expr (postfix_expression,
4536 s, TREE_TYPE (type));
4540 if (!pseudo_destructor_p)
4542 /* If the SCOPE is not a scalar type, we are looking at an
4543 ordinary class member access expression, rather than a
4544 pseudo-destructor-name. */
4546 /* Parse the id-expression. */
4547 name = (cp_parser_id_expression
4549 cp_parser_optional_template_keyword (parser),
4550 /*check_dependency_p=*/true,
4552 /*declarator_p=*/false,
4553 /*optional_p=*/false));
4554 /* In general, build a SCOPE_REF if the member name is qualified.
4555 However, if the name was not dependent and has already been
4556 resolved; there is no need to build the SCOPE_REF. For example;
4558 struct X { void f(); };
4559 template <typename T> void f(T* t) { t->X::f(); }
4561 Even though "t" is dependent, "X::f" is not and has been resolved
4562 to a BASELINK; there is no need to include scope information. */
4564 /* But we do need to remember that there was an explicit scope for
4565 virtual function calls. */
4567 *idk = CP_ID_KIND_QUALIFIED;
4569 /* If the name is a template-id that names a type, we will get a
4570 TYPE_DECL here. That is invalid code. */
4571 if (TREE_CODE (name) == TYPE_DECL)
4573 error ("invalid use of %qD", name);
4574 postfix_expression = error_mark_node;
4578 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4580 name = build_qualified_name (/*type=*/NULL_TREE,
4584 parser->scope = NULL_TREE;
4585 parser->qualifying_scope = NULL_TREE;
4586 parser->object_scope = NULL_TREE;
4588 if (scope && name && BASELINK_P (name))
4589 adjust_result_of_qualified_name_lookup
4590 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4592 = finish_class_member_access_expr (postfix_expression, name,
4597 /* We no longer need to look up names in the scope of the object on
4598 the left-hand side of the `.' or `->' operator. */
4599 parser->context->object_type = NULL_TREE;
4601 /* Outside of offsetof, these operators may not appear in
4602 constant-expressions. */
4604 && (cp_parser_non_integral_constant_expression
4605 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4606 postfix_expression = error_mark_node;
4608 return postfix_expression;
4611 /* Parse a parenthesized expression-list.
4614 assignment-expression
4615 expression-list, assignment-expression
4620 identifier, expression-list
4622 CAST_P is true if this expression is the target of a cast.
4624 Returns a TREE_LIST. The TREE_VALUE of each node is a
4625 representation of an assignment-expression. Note that a TREE_LIST
4626 is returned even if there is only a single expression in the list.
4627 error_mark_node is returned if the ( and or ) are
4628 missing. NULL_TREE is returned on no expressions. The parentheses
4629 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4630 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4631 indicates whether or not all of the expressions in the list were
4635 cp_parser_parenthesized_expression_list (cp_parser* parser,
4636 bool is_attribute_list,
4638 bool *non_constant_p)
4640 tree expression_list = NULL_TREE;
4641 bool fold_expr_p = is_attribute_list;
4642 tree identifier = NULL_TREE;
4644 /* Assume all the expressions will be constant. */
4646 *non_constant_p = false;
4648 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4649 return error_mark_node;
4651 /* Consume expressions until there are no more. */
4652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4657 /* At the beginning of attribute lists, check to see if the
4658 next token is an identifier. */
4659 if (is_attribute_list
4660 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4664 /* Consume the identifier. */
4665 token = cp_lexer_consume_token (parser->lexer);
4666 /* Save the identifier. */
4667 identifier = token->value;
4671 /* Parse the next assignment-expression. */
4674 bool expr_non_constant_p;
4675 expr = (cp_parser_constant_expression
4676 (parser, /*allow_non_constant_p=*/true,
4677 &expr_non_constant_p));
4678 if (expr_non_constant_p)
4679 *non_constant_p = true;
4682 expr = cp_parser_assignment_expression (parser, cast_p);
4685 expr = fold_non_dependent_expr (expr);
4687 /* Add it to the list. We add error_mark_node
4688 expressions to the list, so that we can still tell if
4689 the correct form for a parenthesized expression-list
4690 is found. That gives better errors. */
4691 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4693 if (expr == error_mark_node)
4697 /* After the first item, attribute lists look the same as
4698 expression lists. */
4699 is_attribute_list = false;
4702 /* If the next token isn't a `,', then we are done. */
4703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4706 /* Otherwise, consume the `,' and keep going. */
4707 cp_lexer_consume_token (parser->lexer);
4710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4715 /* We try and resync to an unnested comma, as that will give the
4716 user better diagnostics. */
4717 ending = cp_parser_skip_to_closing_parenthesis (parser,
4718 /*recovering=*/true,
4720 /*consume_paren=*/true);
4724 return error_mark_node;
4727 /* We built up the list in reverse order so we must reverse it now. */
4728 expression_list = nreverse (expression_list);
4730 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4732 return expression_list;
4735 /* Parse a pseudo-destructor-name.
4737 pseudo-destructor-name:
4738 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4739 :: [opt] nested-name-specifier template template-id :: ~ type-name
4740 :: [opt] nested-name-specifier [opt] ~ type-name
4742 If either of the first two productions is used, sets *SCOPE to the
4743 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4744 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4745 or ERROR_MARK_NODE if the parse fails. */
4748 cp_parser_pseudo_destructor_name (cp_parser* parser,
4752 bool nested_name_specifier_p;
4754 /* Assume that things will not work out. */
4755 *type = error_mark_node;
4757 /* Look for the optional `::' operator. */
4758 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4759 /* Look for the optional nested-name-specifier. */
4760 nested_name_specifier_p
4761 = (cp_parser_nested_name_specifier_opt (parser,
4762 /*typename_keyword_p=*/false,
4763 /*check_dependency_p=*/true,
4765 /*is_declaration=*/true)
4767 /* Now, if we saw a nested-name-specifier, we might be doing the
4768 second production. */
4769 if (nested_name_specifier_p
4770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4772 /* Consume the `template' keyword. */
4773 cp_lexer_consume_token (parser->lexer);
4774 /* Parse the template-id. */
4775 cp_parser_template_id (parser,
4776 /*template_keyword_p=*/true,
4777 /*check_dependency_p=*/false,
4778 /*is_declaration=*/true);
4779 /* Look for the `::' token. */
4780 cp_parser_require (parser, CPP_SCOPE, "`::'");
4782 /* If the next token is not a `~', then there might be some
4783 additional qualification. */
4784 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4786 /* Look for the type-name. */
4787 *scope = TREE_TYPE (cp_parser_type_name (parser));
4789 if (*scope == error_mark_node)
4792 /* If we don't have ::~, then something has gone wrong. Since
4793 the only caller of this function is looking for something
4794 after `.' or `->' after a scalar type, most likely the
4795 program is trying to get a member of a non-aggregate
4797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4798 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4800 cp_parser_error (parser, "request for member of non-aggregate type");
4804 /* Look for the `::' token. */
4805 cp_parser_require (parser, CPP_SCOPE, "`::'");
4810 /* Look for the `~'. */
4811 cp_parser_require (parser, CPP_COMPL, "`~'");
4812 /* Look for the type-name again. We are not responsible for
4813 checking that it matches the first type-name. */
4814 *type = cp_parser_type_name (parser);
4817 /* Parse a unary-expression.
4823 unary-operator cast-expression
4824 sizeof unary-expression
4832 __extension__ cast-expression
4833 __alignof__ unary-expression
4834 __alignof__ ( type-id )
4835 __real__ cast-expression
4836 __imag__ cast-expression
4839 ADDRESS_P is true iff the unary-expression is appearing as the
4840 operand of the `&' operator. CAST_P is true if this expression is
4841 the target of a cast.
4843 Returns a representation of the expression. */
4846 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4849 enum tree_code unary_operator;
4851 /* Peek at the next token. */
4852 token = cp_lexer_peek_token (parser->lexer);
4853 /* Some keywords give away the kind of expression. */
4854 if (token->type == CPP_KEYWORD)
4856 enum rid keyword = token->keyword;
4866 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4867 /* Consume the token. */
4868 cp_lexer_consume_token (parser->lexer);
4869 /* Parse the operand. */
4870 operand = cp_parser_sizeof_operand (parser, keyword);
4872 if (TYPE_P (operand))
4873 return cxx_sizeof_or_alignof_type (operand, op, true);
4875 return cxx_sizeof_or_alignof_expr (operand, op);
4879 return cp_parser_new_expression (parser);
4882 return cp_parser_delete_expression (parser);
4886 /* The saved value of the PEDANTIC flag. */
4890 /* Save away the PEDANTIC flag. */
4891 cp_parser_extension_opt (parser, &saved_pedantic);
4892 /* Parse the cast-expression. */
4893 expr = cp_parser_simple_cast_expression (parser);
4894 /* Restore the PEDANTIC flag. */
4895 pedantic = saved_pedantic;
4905 /* Consume the `__real__' or `__imag__' token. */
4906 cp_lexer_consume_token (parser->lexer);
4907 /* Parse the cast-expression. */
4908 expression = cp_parser_simple_cast_expression (parser);
4909 /* Create the complete representation. */
4910 return build_x_unary_op ((keyword == RID_REALPART
4911 ? REALPART_EXPR : IMAGPART_EXPR),
4921 /* Look for the `:: new' and `:: delete', which also signal the
4922 beginning of a new-expression, or delete-expression,
4923 respectively. If the next token is `::', then it might be one of
4925 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4929 /* See if the token after the `::' is one of the keywords in
4930 which we're interested. */
4931 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4932 /* If it's `new', we have a new-expression. */
4933 if (keyword == RID_NEW)
4934 return cp_parser_new_expression (parser);
4935 /* Similarly, for `delete'. */
4936 else if (keyword == RID_DELETE)
4937 return cp_parser_delete_expression (parser);
4940 /* Look for a unary operator. */
4941 unary_operator = cp_parser_unary_operator (token);
4942 /* The `++' and `--' operators can be handled similarly, even though
4943 they are not technically unary-operators in the grammar. */
4944 if (unary_operator == ERROR_MARK)
4946 if (token->type == CPP_PLUS_PLUS)
4947 unary_operator = PREINCREMENT_EXPR;
4948 else if (token->type == CPP_MINUS_MINUS)
4949 unary_operator = PREDECREMENT_EXPR;
4950 /* Handle the GNU address-of-label extension. */
4951 else if (cp_parser_allow_gnu_extensions_p (parser)
4952 && token->type == CPP_AND_AND)
4956 /* Consume the '&&' token. */
4957 cp_lexer_consume_token (parser->lexer);
4958 /* Look for the identifier. */
4959 identifier = cp_parser_identifier (parser);
4960 /* Create an expression representing the address. */
4961 return finish_label_address_expr (identifier);
4964 if (unary_operator != ERROR_MARK)
4966 tree cast_expression;
4967 tree expression = error_mark_node;
4968 const char *non_constant_p = NULL;
4970 /* Consume the operator token. */
4971 token = cp_lexer_consume_token (parser->lexer);
4972 /* Parse the cast-expression. */
4974 = cp_parser_cast_expression (parser,
4975 unary_operator == ADDR_EXPR,
4977 /* Now, build an appropriate representation. */
4978 switch (unary_operator)
4981 non_constant_p = "`*'";
4982 expression = build_x_indirect_ref (cast_expression, "unary *");
4986 non_constant_p = "`&'";
4989 expression = build_x_unary_op (unary_operator, cast_expression);
4992 case PREINCREMENT_EXPR:
4993 case PREDECREMENT_EXPR:
4994 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4997 case UNARY_PLUS_EXPR:
4999 case TRUTH_NOT_EXPR:
5000 expression = finish_unary_op_expr (unary_operator, cast_expression);
5008 && cp_parser_non_integral_constant_expression (parser,
5010 expression = error_mark_node;
5015 return cp_parser_postfix_expression (parser, address_p, cast_p);
5018 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5019 unary-operator, the corresponding tree code is returned. */
5021 static enum tree_code
5022 cp_parser_unary_operator (cp_token* token)
5024 switch (token->type)
5027 return INDIRECT_REF;
5033 return UNARY_PLUS_EXPR;
5039 return TRUTH_NOT_EXPR;
5042 return BIT_NOT_EXPR;
5049 /* Parse a new-expression.
5052 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5053 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5055 Returns a representation of the expression. */
5058 cp_parser_new_expression (cp_parser* parser)
5060 bool global_scope_p;
5066 /* Look for the optional `::' operator. */
5068 = (cp_parser_global_scope_opt (parser,
5069 /*current_scope_valid_p=*/false)
5071 /* Look for the `new' operator. */
5072 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5073 /* There's no easy way to tell a new-placement from the
5074 `( type-id )' construct. */
5075 cp_parser_parse_tentatively (parser);
5076 /* Look for a new-placement. */
5077 placement = cp_parser_new_placement (parser);
5078 /* If that didn't work out, there's no new-placement. */
5079 if (!cp_parser_parse_definitely (parser))
5080 placement = NULL_TREE;
5082 /* If the next token is a `(', then we have a parenthesized
5084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5086 /* Consume the `('. */
5087 cp_lexer_consume_token (parser->lexer);
5088 /* Parse the type-id. */
5089 type = cp_parser_type_id (parser);
5090 /* Look for the closing `)'. */
5091 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5092 /* There should not be a direct-new-declarator in this production,
5093 but GCC used to allowed this, so we check and emit a sensible error
5094 message for this case. */
5095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5097 error ("array bound forbidden after parenthesized type-id");
5098 inform ("try removing the parentheses around the type-id");
5099 cp_parser_direct_new_declarator (parser);
5103 /* Otherwise, there must be a new-type-id. */
5105 type = cp_parser_new_type_id (parser, &nelts);
5107 /* If the next token is a `(', then we have a new-initializer. */
5108 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5109 initializer = cp_parser_new_initializer (parser);
5111 initializer = NULL_TREE;
5113 /* A new-expression may not appear in an integral constant
5115 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5116 return error_mark_node;
5118 /* Create a representation of the new-expression. */
5119 return build_new (placement, type, nelts, initializer, global_scope_p);
5122 /* Parse a new-placement.
5127 Returns the same representation as for an expression-list. */
5130 cp_parser_new_placement (cp_parser* parser)
5132 tree expression_list;
5134 /* Parse the expression-list. */
5135 expression_list = (cp_parser_parenthesized_expression_list
5136 (parser, false, /*cast_p=*/false,
5137 /*non_constant_p=*/NULL));
5139 return expression_list;
5142 /* Parse a new-type-id.
5145 type-specifier-seq new-declarator [opt]
5147 Returns the TYPE allocated. If the new-type-id indicates an array
5148 type, *NELTS is set to the number of elements in the last array
5149 bound; the TYPE will not include the last array bound. */
5152 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5154 cp_decl_specifier_seq type_specifier_seq;
5155 cp_declarator *new_declarator;
5156 cp_declarator *declarator;
5157 cp_declarator *outer_declarator;
5158 const char *saved_message;
5161 /* The type-specifier sequence must not contain type definitions.
5162 (It cannot contain declarations of new types either, but if they
5163 are not definitions we will catch that because they are not
5165 saved_message = parser->type_definition_forbidden_message;
5166 parser->type_definition_forbidden_message
5167 = "types may not be defined in a new-type-id";
5168 /* Parse the type-specifier-seq. */
5169 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5170 &type_specifier_seq);
5171 /* Restore the old message. */
5172 parser->type_definition_forbidden_message = saved_message;
5173 /* Parse the new-declarator. */
5174 new_declarator = cp_parser_new_declarator_opt (parser);
5176 /* Determine the number of elements in the last array dimension, if
5179 /* Skip down to the last array dimension. */
5180 declarator = new_declarator;
5181 outer_declarator = NULL;
5182 while (declarator && (declarator->kind == cdk_pointer
5183 || declarator->kind == cdk_ptrmem))
5185 outer_declarator = declarator;
5186 declarator = declarator->declarator;
5189 && declarator->kind == cdk_array
5190 && declarator->declarator
5191 && declarator->declarator->kind == cdk_array)
5193 outer_declarator = declarator;
5194 declarator = declarator->declarator;
5197 if (declarator && declarator->kind == cdk_array)
5199 *nelts = declarator->u.array.bounds;
5200 if (*nelts == error_mark_node)
5201 *nelts = integer_one_node;
5203 if (outer_declarator)
5204 outer_declarator->declarator = declarator->declarator;
5206 new_declarator = NULL;
5209 type = groktypename (&type_specifier_seq, new_declarator);
5210 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5212 *nelts = array_type_nelts_top (type);
5213 type = TREE_TYPE (type);
5218 /* Parse an (optional) new-declarator.
5221 ptr-operator new-declarator [opt]
5222 direct-new-declarator
5224 Returns the declarator. */
5226 static cp_declarator *
5227 cp_parser_new_declarator_opt (cp_parser* parser)
5229 enum tree_code code;
5231 cp_cv_quals cv_quals;
5233 /* We don't know if there's a ptr-operator next, or not. */
5234 cp_parser_parse_tentatively (parser);
5235 /* Look for a ptr-operator. */
5236 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5237 /* If that worked, look for more new-declarators. */
5238 if (cp_parser_parse_definitely (parser))
5240 cp_declarator *declarator;
5242 /* Parse another optional declarator. */
5243 declarator = cp_parser_new_declarator_opt (parser);
5245 /* Create the representation of the declarator. */
5247 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5248 else if (code == INDIRECT_REF)
5249 declarator = make_pointer_declarator (cv_quals, declarator);
5251 declarator = make_reference_declarator (cv_quals, declarator);
5256 /* If the next token is a `[', there is a direct-new-declarator. */
5257 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5258 return cp_parser_direct_new_declarator (parser);
5263 /* Parse a direct-new-declarator.
5265 direct-new-declarator:
5267 direct-new-declarator [constant-expression]
5271 static cp_declarator *
5272 cp_parser_direct_new_declarator (cp_parser* parser)
5274 cp_declarator *declarator = NULL;
5280 /* Look for the opening `['. */
5281 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5282 /* The first expression is not required to be constant. */
5285 expression = cp_parser_expression (parser, /*cast_p=*/false);
5286 /* The standard requires that the expression have integral
5287 type. DR 74 adds enumeration types. We believe that the
5288 real intent is that these expressions be handled like the
5289 expression in a `switch' condition, which also allows
5290 classes with a single conversion to integral or
5291 enumeration type. */
5292 if (!processing_template_decl)
5295 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5300 error ("expression in new-declarator must have integral "
5301 "or enumeration type");
5302 expression = error_mark_node;
5306 /* But all the other expressions must be. */
5309 = cp_parser_constant_expression (parser,
5310 /*allow_non_constant=*/false,
5312 /* Look for the closing `]'. */
5313 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5315 /* Add this bound to the declarator. */
5316 declarator = make_array_declarator (declarator, expression);
5318 /* If the next token is not a `[', then there are no more
5320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5327 /* Parse a new-initializer.
5330 ( expression-list [opt] )
5332 Returns a representation of the expression-list. If there is no
5333 expression-list, VOID_ZERO_NODE is returned. */
5336 cp_parser_new_initializer (cp_parser* parser)
5338 tree expression_list;
5340 expression_list = (cp_parser_parenthesized_expression_list
5341 (parser, false, /*cast_p=*/false,
5342 /*non_constant_p=*/NULL));
5343 if (!expression_list)
5344 expression_list = void_zero_node;
5346 return expression_list;
5349 /* Parse a delete-expression.
5352 :: [opt] delete cast-expression
5353 :: [opt] delete [ ] cast-expression
5355 Returns a representation of the expression. */
5358 cp_parser_delete_expression (cp_parser* parser)
5360 bool global_scope_p;
5364 /* Look for the optional `::' operator. */
5366 = (cp_parser_global_scope_opt (parser,
5367 /*current_scope_valid_p=*/false)
5369 /* Look for the `delete' keyword. */
5370 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5371 /* See if the array syntax is in use. */
5372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5374 /* Consume the `[' token. */
5375 cp_lexer_consume_token (parser->lexer);
5376 /* Look for the `]' token. */
5377 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5378 /* Remember that this is the `[]' construct. */
5384 /* Parse the cast-expression. */
5385 expression = cp_parser_simple_cast_expression (parser);
5387 /* A delete-expression may not appear in an integral constant
5389 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5390 return error_mark_node;
5392 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5395 /* Parse a cast-expression.
5399 ( type-id ) cast-expression
5401 ADDRESS_P is true iff the unary-expression is appearing as the
5402 operand of the `&' operator. CAST_P is true if this expression is
5403 the target of a cast.
5405 Returns a representation of the expression. */
5408 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5410 /* If it's a `(', then we might be looking at a cast. */
5411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5413 tree type = NULL_TREE;
5414 tree expr = NULL_TREE;
5415 bool compound_literal_p;
5416 const char *saved_message;
5418 /* There's no way to know yet whether or not this is a cast.
5419 For example, `(int (3))' is a unary-expression, while `(int)
5420 3' is a cast. So, we resort to parsing tentatively. */
5421 cp_parser_parse_tentatively (parser);
5422 /* Types may not be defined in a cast. */
5423 saved_message = parser->type_definition_forbidden_message;
5424 parser->type_definition_forbidden_message
5425 = "types may not be defined in casts";
5426 /* Consume the `('. */
5427 cp_lexer_consume_token (parser->lexer);
5428 /* A very tricky bit is that `(struct S) { 3 }' is a
5429 compound-literal (which we permit in C++ as an extension).
5430 But, that construct is not a cast-expression -- it is a
5431 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5432 is legal; if the compound-literal were a cast-expression,
5433 you'd need an extra set of parentheses.) But, if we parse
5434 the type-id, and it happens to be a class-specifier, then we
5435 will commit to the parse at that point, because we cannot
5436 undo the action that is done when creating a new class. So,
5437 then we cannot back up and do a postfix-expression.
5439 Therefore, we scan ahead to the closing `)', and check to see
5440 if the token after the `)' is a `{'. If so, we are not
5441 looking at a cast-expression.
5443 Save tokens so that we can put them back. */
5444 cp_lexer_save_tokens (parser->lexer);
5445 /* Skip tokens until the next token is a closing parenthesis.
5446 If we find the closing `)', and the next token is a `{', then
5447 we are looking at a compound-literal. */
5449 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5450 /*consume_paren=*/true)
5451 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5452 /* Roll back the tokens we skipped. */
5453 cp_lexer_rollback_tokens (parser->lexer);
5454 /* If we were looking at a compound-literal, simulate an error
5455 so that the call to cp_parser_parse_definitely below will
5457 if (compound_literal_p)
5458 cp_parser_simulate_error (parser);
5461 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5462 parser->in_type_id_in_expr_p = true;
5463 /* Look for the type-id. */
5464 type = cp_parser_type_id (parser);
5465 /* Look for the closing `)'. */
5466 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5467 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5470 /* Restore the saved message. */
5471 parser->type_definition_forbidden_message = saved_message;
5473 /* If ok so far, parse the dependent expression. We cannot be
5474 sure it is a cast. Consider `(T ())'. It is a parenthesized
5475 ctor of T, but looks like a cast to function returning T
5476 without a dependent expression. */
5477 if (!cp_parser_error_occurred (parser))
5478 expr = cp_parser_cast_expression (parser,
5479 /*address_p=*/false,
5482 if (cp_parser_parse_definitely (parser))
5484 /* Warn about old-style casts, if so requested. */
5485 if (warn_old_style_cast
5486 && !in_system_header
5487 && !VOID_TYPE_P (type)
5488 && current_lang_name != lang_name_c)
5489 warning (OPT_Wold_style_cast, "use of old-style cast");
5491 /* Only type conversions to integral or enumeration types
5492 can be used in constant-expressions. */
5493 if (parser->integral_constant_expression_p
5494 && !dependent_type_p (type)
5495 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5496 && (cp_parser_non_integral_constant_expression
5498 "a cast to a type other than an integral or "
5499 "enumeration type")))
5500 return error_mark_node;
5502 /* Perform the cast. */
5503 expr = build_c_cast (type, expr);
5508 /* If we get here, then it's not a cast, so it must be a
5509 unary-expression. */
5510 return cp_parser_unary_expression (parser, address_p, cast_p);
5513 /* Parse a binary expression of the general form:
5517 pm-expression .* cast-expression
5518 pm-expression ->* cast-expression
5520 multiplicative-expression:
5522 multiplicative-expression * pm-expression
5523 multiplicative-expression / pm-expression
5524 multiplicative-expression % pm-expression
5526 additive-expression:
5527 multiplicative-expression
5528 additive-expression + multiplicative-expression
5529 additive-expression - multiplicative-expression
5533 shift-expression << additive-expression
5534 shift-expression >> additive-expression
5536 relational-expression:
5538 relational-expression < shift-expression
5539 relational-expression > shift-expression
5540 relational-expression <= shift-expression
5541 relational-expression >= shift-expression
5545 relational-expression:
5546 relational-expression <? shift-expression
5547 relational-expression >? shift-expression
5549 equality-expression:
5550 relational-expression
5551 equality-expression == relational-expression
5552 equality-expression != relational-expression
5556 and-expression & equality-expression
5558 exclusive-or-expression:
5560 exclusive-or-expression ^ and-expression
5562 inclusive-or-expression:
5563 exclusive-or-expression
5564 inclusive-or-expression | exclusive-or-expression
5566 logical-and-expression:
5567 inclusive-or-expression
5568 logical-and-expression && inclusive-or-expression
5570 logical-or-expression:
5571 logical-and-expression
5572 logical-or-expression || logical-and-expression
5574 All these are implemented with a single function like:
5577 simple-cast-expression
5578 binary-expression <token> binary-expression
5580 CAST_P is true if this expression is the target of a cast.
5582 The binops_by_token map is used to get the tree codes for each <token> type.
5583 binary-expressions are associated according to a precedence table. */
5585 #define TOKEN_PRECEDENCE(token) \
5586 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5587 ? PREC_NOT_OPERATOR \
5588 : binops_by_token[token->type].prec)
5591 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5593 cp_parser_expression_stack stack;
5594 cp_parser_expression_stack_entry *sp = &stack[0];
5597 enum tree_code tree_type;
5598 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5601 /* Parse the first expression. */
5602 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5606 /* Get an operator token. */
5607 token = cp_lexer_peek_token (parser->lexer);
5609 new_prec = TOKEN_PRECEDENCE (token);
5611 /* Popping an entry off the stack means we completed a subexpression:
5612 - either we found a token which is not an operator (`>' where it is not
5613 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5614 will happen repeatedly;
5615 - or, we found an operator which has lower priority. This is the case
5616 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5618 if (new_prec <= prec)
5627 tree_type = binops_by_token[token->type].tree_type;
5629 /* We used the operator token. */
5630 cp_lexer_consume_token (parser->lexer);
5632 /* Extract another operand. It may be the RHS of this expression
5633 or the LHS of a new, higher priority expression. */
5634 rhs = cp_parser_simple_cast_expression (parser);
5636 /* Get another operator token. Look up its precedence to avoid
5637 building a useless (immediately popped) stack entry for common
5638 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5639 token = cp_lexer_peek_token (parser->lexer);
5640 lookahead_prec = TOKEN_PRECEDENCE (token);
5641 if (lookahead_prec > new_prec)
5643 /* ... and prepare to parse the RHS of the new, higher priority
5644 expression. Since precedence levels on the stack are
5645 monotonically increasing, we do not have to care about
5648 sp->tree_type = tree_type;
5653 new_prec = lookahead_prec;
5657 /* If the stack is not empty, we have parsed into LHS the right side
5658 (`4' in the example above) of an expression we had suspended.
5659 We can use the information on the stack to recover the LHS (`3')
5660 from the stack together with the tree code (`MULT_EXPR'), and
5661 the precedence of the higher level subexpression
5662 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5663 which will be used to actually build the additive expression. */
5666 tree_type = sp->tree_type;
5671 overloaded_p = false;
5672 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5674 /* If the binary operator required the use of an overloaded operator,
5675 then this expression cannot be an integral constant-expression.
5676 An overloaded operator can be used even if both operands are
5677 otherwise permissible in an integral constant-expression if at
5678 least one of the operands is of enumeration type. */
5681 && (cp_parser_non_integral_constant_expression
5682 (parser, "calls to overloaded operators")))
5683 return error_mark_node;
5690 /* Parse the `? expression : assignment-expression' part of a
5691 conditional-expression. The LOGICAL_OR_EXPR is the
5692 logical-or-expression that started the conditional-expression.
5693 Returns a representation of the entire conditional-expression.
5695 This routine is used by cp_parser_assignment_expression.
5697 ? expression : assignment-expression
5701 ? : assignment-expression */
5704 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5707 tree assignment_expr;
5709 /* Consume the `?' token. */
5710 cp_lexer_consume_token (parser->lexer);
5711 if (cp_parser_allow_gnu_extensions_p (parser)
5712 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5713 /* Implicit true clause. */
5716 /* Parse the expression. */
5717 expr = cp_parser_expression (parser, /*cast_p=*/false);
5719 /* The next token should be a `:'. */
5720 cp_parser_require (parser, CPP_COLON, "`:'");
5721 /* Parse the assignment-expression. */
5722 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5724 /* Build the conditional-expression. */
5725 return build_x_conditional_expr (logical_or_expr,
5730 /* Parse an assignment-expression.
5732 assignment-expression:
5733 conditional-expression
5734 logical-or-expression assignment-operator assignment_expression
5737 CAST_P is true if this expression is the target of a cast.
5739 Returns a representation for the expression. */
5742 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5746 /* If the next token is the `throw' keyword, then we're looking at
5747 a throw-expression. */
5748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5749 expr = cp_parser_throw_expression (parser);
5750 /* Otherwise, it must be that we are looking at a
5751 logical-or-expression. */
5754 /* Parse the binary expressions (logical-or-expression). */
5755 expr = cp_parser_binary_expression (parser, cast_p);
5756 /* If the next token is a `?' then we're actually looking at a
5757 conditional-expression. */
5758 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5759 return cp_parser_question_colon_clause (parser, expr);
5762 enum tree_code assignment_operator;
5764 /* If it's an assignment-operator, we're using the second
5767 = cp_parser_assignment_operator_opt (parser);
5768 if (assignment_operator != ERROR_MARK)
5772 /* Parse the right-hand side of the assignment. */
5773 rhs = cp_parser_assignment_expression (parser, cast_p);
5774 /* An assignment may not appear in a
5775 constant-expression. */
5776 if (cp_parser_non_integral_constant_expression (parser,
5778 return error_mark_node;
5779 /* Build the assignment expression. */
5780 expr = build_x_modify_expr (expr,
5781 assignment_operator,
5790 /* Parse an (optional) assignment-operator.
5792 assignment-operator: one of
5793 = *= /= %= += -= >>= <<= &= ^= |=
5797 assignment-operator: one of
5800 If the next token is an assignment operator, the corresponding tree
5801 code is returned, and the token is consumed. For example, for
5802 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5803 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5804 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5805 operator, ERROR_MARK is returned. */
5807 static enum tree_code
5808 cp_parser_assignment_operator_opt (cp_parser* parser)
5813 /* Peek at the next toen. */
5814 token = cp_lexer_peek_token (parser->lexer);
5816 switch (token->type)
5827 op = TRUNC_DIV_EXPR;
5831 op = TRUNC_MOD_EXPR;
5863 /* Nothing else is an assignment operator. */
5867 /* If it was an assignment operator, consume it. */
5868 if (op != ERROR_MARK)
5869 cp_lexer_consume_token (parser->lexer);
5874 /* Parse an expression.
5877 assignment-expression
5878 expression , assignment-expression
5880 CAST_P is true if this expression is the target of a cast.
5882 Returns a representation of the expression. */
5885 cp_parser_expression (cp_parser* parser, bool cast_p)
5887 tree expression = NULL_TREE;
5891 tree assignment_expression;
5893 /* Parse the next assignment-expression. */
5894 assignment_expression
5895 = cp_parser_assignment_expression (parser, cast_p);
5896 /* If this is the first assignment-expression, we can just
5899 expression = assignment_expression;
5901 expression = build_x_compound_expr (expression,
5902 assignment_expression);
5903 /* If the next token is not a comma, then we are done with the
5905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5907 /* Consume the `,'. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* A comma operator cannot appear in a constant-expression. */
5910 if (cp_parser_non_integral_constant_expression (parser,
5911 "a comma operator"))
5912 expression = error_mark_node;
5918 /* Parse a constant-expression.
5920 constant-expression:
5921 conditional-expression
5923 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5924 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5925 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5926 is false, NON_CONSTANT_P should be NULL. */
5929 cp_parser_constant_expression (cp_parser* parser,
5930 bool allow_non_constant_p,
5931 bool *non_constant_p)
5933 bool saved_integral_constant_expression_p;
5934 bool saved_allow_non_integral_constant_expression_p;
5935 bool saved_non_integral_constant_expression_p;
5938 /* It might seem that we could simply parse the
5939 conditional-expression, and then check to see if it were
5940 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5941 one that the compiler can figure out is constant, possibly after
5942 doing some simplifications or optimizations. The standard has a
5943 precise definition of constant-expression, and we must honor
5944 that, even though it is somewhat more restrictive.
5950 is not a legal declaration, because `(2, 3)' is not a
5951 constant-expression. The `,' operator is forbidden in a
5952 constant-expression. However, GCC's constant-folding machinery
5953 will fold this operation to an INTEGER_CST for `3'. */
5955 /* Save the old settings. */
5956 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5957 saved_allow_non_integral_constant_expression_p
5958 = parser->allow_non_integral_constant_expression_p;
5959 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5960 /* We are now parsing a constant-expression. */
5961 parser->integral_constant_expression_p = true;
5962 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5963 parser->non_integral_constant_expression_p = false;
5964 /* Although the grammar says "conditional-expression", we parse an
5965 "assignment-expression", which also permits "throw-expression"
5966 and the use of assignment operators. In the case that
5967 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5968 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5969 actually essential that we look for an assignment-expression.
5970 For example, cp_parser_initializer_clauses uses this function to
5971 determine whether a particular assignment-expression is in fact
5973 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5974 /* Restore the old settings. */
5975 parser->integral_constant_expression_p
5976 = saved_integral_constant_expression_p;
5977 parser->allow_non_integral_constant_expression_p
5978 = saved_allow_non_integral_constant_expression_p;
5979 if (allow_non_constant_p)
5980 *non_constant_p = parser->non_integral_constant_expression_p;
5981 else if (parser->non_integral_constant_expression_p)
5982 expression = error_mark_node;
5983 parser->non_integral_constant_expression_p
5984 = saved_non_integral_constant_expression_p;
5989 /* Parse __builtin_offsetof.
5991 offsetof-expression:
5992 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5994 offsetof-member-designator:
5996 | offsetof-member-designator "." id-expression
5997 | offsetof-member-designator "[" expression "]" */
6000 cp_parser_builtin_offsetof (cp_parser *parser)
6002 int save_ice_p, save_non_ice_p;
6006 /* We're about to accept non-integral-constant things, but will
6007 definitely yield an integral constant expression. Save and
6008 restore these values around our local parsing. */
6009 save_ice_p = parser->integral_constant_expression_p;
6010 save_non_ice_p = parser->non_integral_constant_expression_p;
6012 /* Consume the "__builtin_offsetof" token. */
6013 cp_lexer_consume_token (parser->lexer);
6014 /* Consume the opening `('. */
6015 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6016 /* Parse the type-id. */
6017 type = cp_parser_type_id (parser);
6018 /* Look for the `,'. */
6019 cp_parser_require (parser, CPP_COMMA, "`,'");
6021 /* Build the (type *)null that begins the traditional offsetof macro. */
6022 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6024 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6025 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6029 cp_token *token = cp_lexer_peek_token (parser->lexer);
6030 switch (token->type)
6032 case CPP_OPEN_SQUARE:
6033 /* offsetof-member-designator "[" expression "]" */
6034 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6038 /* offsetof-member-designator "." identifier */
6039 cp_lexer_consume_token (parser->lexer);
6040 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6044 case CPP_CLOSE_PAREN:
6045 /* Consume the ")" token. */
6046 cp_lexer_consume_token (parser->lexer);
6050 /* Error. We know the following require will fail, but
6051 that gives the proper error message. */
6052 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6053 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6054 expr = error_mark_node;
6060 /* If we're processing a template, we can't finish the semantics yet.
6061 Otherwise we can fold the entire expression now. */
6062 if (processing_template_decl)
6063 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6065 expr = finish_offsetof (expr);
6068 parser->integral_constant_expression_p = save_ice_p;
6069 parser->non_integral_constant_expression_p = save_non_ice_p;
6074 /* Statements [gram.stmt.stmt] */
6076 /* Parse a statement.
6080 expression-statement
6085 declaration-statement
6088 IN_COMPOUND is true when the statement is nested inside a
6089 cp_parser_compound_statement; this matters for certain pragmas. */
6092 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6097 location_t statement_location;
6100 /* There is no statement yet. */
6101 statement = NULL_TREE;
6102 /* Peek at the next token. */
6103 token = cp_lexer_peek_token (parser->lexer);
6104 /* Remember the location of the first token in the statement. */
6105 statement_location = token->location;
6106 /* If this is a keyword, then that will often determine what kind of
6107 statement we have. */
6108 if (token->type == CPP_KEYWORD)
6110 enum rid keyword = token->keyword;
6116 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6122 statement = cp_parser_selection_statement (parser);
6128 statement = cp_parser_iteration_statement (parser);
6135 statement = cp_parser_jump_statement (parser);
6138 /* Objective-C++ exception-handling constructs. */
6141 case RID_AT_FINALLY:
6142 case RID_AT_SYNCHRONIZED:
6144 statement = cp_parser_objc_statement (parser);
6148 statement = cp_parser_try_block (parser);
6152 /* It might be a keyword like `int' that can start a
6153 declaration-statement. */
6157 else if (token->type == CPP_NAME)
6159 /* If the next token is a `:', then we are looking at a
6160 labeled-statement. */
6161 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6162 if (token->type == CPP_COLON)
6163 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6166 /* Anything that starts with a `{' must be a compound-statement. */
6167 else if (token->type == CPP_OPEN_BRACE)
6168 statement = cp_parser_compound_statement (parser, NULL, false);
6169 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6170 a statement all its own. */
6171 else if (token->type == CPP_PRAGMA)
6173 /* Only certain OpenMP pragmas are attached to statements, and thus
6174 are considered statements themselves. All others are not. In
6175 the context of a compound, accept the pragma as a "statement" and
6176 return so that we can check for a close brace. Otherwise we
6177 require a real statement and must go back and read one. */
6179 cp_parser_pragma (parser, pragma_compound);
6180 else if (!cp_parser_pragma (parser, pragma_stmt))
6184 else if (token->type == CPP_EOF)
6186 cp_parser_error (parser, "expected statement");
6190 /* Everything else must be a declaration-statement or an
6191 expression-statement. Try for the declaration-statement
6192 first, unless we are looking at a `;', in which case we know that
6193 we have an expression-statement. */
6196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6198 cp_parser_parse_tentatively (parser);
6199 /* Try to parse the declaration-statement. */
6200 cp_parser_declaration_statement (parser);
6201 /* If that worked, we're done. */
6202 if (cp_parser_parse_definitely (parser))
6205 /* Look for an expression-statement instead. */
6206 statement = cp_parser_expression_statement (parser, in_statement_expr);
6209 /* Set the line number for the statement. */
6210 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6211 SET_EXPR_LOCATION (statement, statement_location);
6214 /* Parse a labeled-statement.
6217 identifier : statement
6218 case constant-expression : statement
6224 case constant-expression ... constant-expression : statement
6226 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6227 For an ordinary label, returns a LABEL_EXPR.
6229 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6230 inside a compound. */
6233 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6237 tree statement = error_mark_node;
6239 /* The next token should be an identifier. */
6240 token = cp_lexer_peek_token (parser->lexer);
6241 if (token->type != CPP_NAME
6242 && token->type != CPP_KEYWORD)
6244 cp_parser_error (parser, "expected labeled-statement");
6245 return error_mark_node;
6248 switch (token->keyword)
6255 /* Consume the `case' token. */
6256 cp_lexer_consume_token (parser->lexer);
6257 /* Parse the constant-expression. */
6258 expr = cp_parser_constant_expression (parser,
6259 /*allow_non_constant_p=*/false,
6262 ellipsis = cp_lexer_peek_token (parser->lexer);
6263 if (ellipsis->type == CPP_ELLIPSIS)
6265 /* Consume the `...' token. */
6266 cp_lexer_consume_token (parser->lexer);
6268 cp_parser_constant_expression (parser,
6269 /*allow_non_constant_p=*/false,
6271 /* We don't need to emit warnings here, as the common code
6272 will do this for us. */
6275 expr_hi = NULL_TREE;
6277 if (parser->in_switch_statement_p)
6278 statement = finish_case_label (expr, expr_hi);
6280 error ("case label %qE not within a switch statement", expr);
6285 /* Consume the `default' token. */
6286 cp_lexer_consume_token (parser->lexer);
6288 if (parser->in_switch_statement_p)
6289 statement = finish_case_label (NULL_TREE, NULL_TREE);
6291 error ("case label not within a switch statement");
6295 /* Anything else must be an ordinary label. */
6296 statement = finish_label_stmt (cp_parser_identifier (parser));
6300 /* Require the `:' token. */
6301 cp_parser_require (parser, CPP_COLON, "`:'");
6302 /* Parse the labeled statement. */
6303 cp_parser_statement (parser, in_statement_expr, in_compound);
6305 /* Return the label, in the case of a `case' or `default' label. */
6309 /* Parse an expression-statement.
6311 expression-statement:
6314 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6315 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6316 indicates whether this expression-statement is part of an
6317 expression statement. */
6320 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6322 tree statement = NULL_TREE;
6324 /* If the next token is a ';', then there is no expression
6326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6327 statement = cp_parser_expression (parser, /*cast_p=*/false);
6329 /* Consume the final `;'. */
6330 cp_parser_consume_semicolon_at_end_of_statement (parser);
6332 if (in_statement_expr
6333 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6334 /* This is the final expression statement of a statement
6336 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6338 statement = finish_expr_stmt (statement);
6345 /* Parse a compound-statement.
6348 { statement-seq [opt] }
6350 Returns a tree representing the statement. */
6353 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6358 /* Consume the `{'. */
6359 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6360 return error_mark_node;
6361 /* Begin the compound-statement. */
6362 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6363 /* Parse an (optional) statement-seq. */
6364 cp_parser_statement_seq_opt (parser, in_statement_expr);
6365 /* Finish the compound-statement. */
6366 finish_compound_stmt (compound_stmt);
6367 /* Consume the `}'. */
6368 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6370 return compound_stmt;
6373 /* Parse an (optional) statement-seq.
6377 statement-seq [opt] statement */
6380 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6382 /* Scan statements until there aren't any more. */
6385 cp_token *token = cp_lexer_peek_token (parser->lexer);
6387 /* If we're looking at a `}', then we've run out of statements. */
6388 if (token->type == CPP_CLOSE_BRACE
6389 || token->type == CPP_EOF
6390 || token->type == CPP_PRAGMA_EOL)
6393 /* Parse the statement. */
6394 cp_parser_statement (parser, in_statement_expr, true);
6398 /* Parse a selection-statement.
6400 selection-statement:
6401 if ( condition ) statement
6402 if ( condition ) statement else statement
6403 switch ( condition ) statement
6405 Returns the new IF_STMT or SWITCH_STMT. */
6408 cp_parser_selection_statement (cp_parser* parser)
6413 /* Peek at the next token. */
6414 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6416 /* See what kind of keyword it is. */
6417 keyword = token->keyword;
6426 /* Look for the `('. */
6427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6429 cp_parser_skip_to_end_of_statement (parser);
6430 return error_mark_node;
6433 /* Begin the selection-statement. */
6434 if (keyword == RID_IF)
6435 statement = begin_if_stmt ();
6437 statement = begin_switch_stmt ();
6439 /* Parse the condition. */
6440 condition = cp_parser_condition (parser);
6441 /* Look for the `)'. */
6442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6443 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6444 /*consume_paren=*/true);
6446 if (keyword == RID_IF)
6448 /* Add the condition. */
6449 finish_if_stmt_cond (condition, statement);
6451 /* Parse the then-clause. */
6452 cp_parser_implicitly_scoped_statement (parser);
6453 finish_then_clause (statement);
6455 /* If the next token is `else', parse the else-clause. */
6456 if (cp_lexer_next_token_is_keyword (parser->lexer,
6459 /* Consume the `else' keyword. */
6460 cp_lexer_consume_token (parser->lexer);
6461 begin_else_clause (statement);
6462 /* Parse the else-clause. */
6463 cp_parser_implicitly_scoped_statement (parser);
6464 finish_else_clause (statement);
6467 /* Now we're all done with the if-statement. */
6468 finish_if_stmt (statement);
6472 bool in_switch_statement_p;
6473 unsigned char in_statement;
6475 /* Add the condition. */
6476 finish_switch_cond (condition, statement);
6478 /* Parse the body of the switch-statement. */
6479 in_switch_statement_p = parser->in_switch_statement_p;
6480 in_statement = parser->in_statement;
6481 parser->in_switch_statement_p = true;
6482 parser->in_statement |= IN_SWITCH_STMT;
6483 cp_parser_implicitly_scoped_statement (parser);
6484 parser->in_switch_statement_p = in_switch_statement_p;
6485 parser->in_statement = in_statement;
6487 /* Now we're all done with the switch-statement. */
6488 finish_switch_stmt (statement);
6496 cp_parser_error (parser, "expected selection-statement");
6497 return error_mark_node;
6501 /* Parse a condition.
6505 type-specifier-seq declarator = assignment-expression
6510 type-specifier-seq declarator asm-specification [opt]
6511 attributes [opt] = assignment-expression
6513 Returns the expression that should be tested. */
6516 cp_parser_condition (cp_parser* parser)
6518 cp_decl_specifier_seq type_specifiers;
6519 const char *saved_message;
6521 /* Try the declaration first. */
6522 cp_parser_parse_tentatively (parser);
6523 /* New types are not allowed in the type-specifier-seq for a
6525 saved_message = parser->type_definition_forbidden_message;
6526 parser->type_definition_forbidden_message
6527 = "types may not be defined in conditions";
6528 /* Parse the type-specifier-seq. */
6529 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6531 /* Restore the saved message. */
6532 parser->type_definition_forbidden_message = saved_message;
6533 /* If all is well, we might be looking at a declaration. */
6534 if (!cp_parser_error_occurred (parser))
6537 tree asm_specification;
6539 cp_declarator *declarator;
6540 tree initializer = NULL_TREE;
6542 /* Parse the declarator. */
6543 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6544 /*ctor_dtor_or_conv_p=*/NULL,
6545 /*parenthesized_p=*/NULL,
6546 /*member_p=*/false);
6547 /* Parse the attributes. */
6548 attributes = cp_parser_attributes_opt (parser);
6549 /* Parse the asm-specification. */
6550 asm_specification = cp_parser_asm_specification_opt (parser);
6551 /* If the next token is not an `=', then we might still be
6552 looking at an expression. For example:
6556 looks like a decl-specifier-seq and a declarator -- but then
6557 there is no `=', so this is an expression. */
6558 cp_parser_require (parser, CPP_EQ, "`='");
6559 /* If we did see an `=', then we are looking at a declaration
6561 if (cp_parser_parse_definitely (parser))
6564 bool non_constant_p;
6566 /* Create the declaration. */
6567 decl = start_decl (declarator, &type_specifiers,
6568 /*initialized_p=*/true,
6569 attributes, /*prefix_attributes=*/NULL_TREE,
6571 /* Parse the assignment-expression. */
6573 = cp_parser_constant_expression (parser,
6574 /*allow_non_constant_p=*/true,
6576 if (!non_constant_p)
6577 initializer = fold_non_dependent_expr (initializer);
6579 /* Process the initializer. */
6580 cp_finish_decl (decl,
6581 initializer, !non_constant_p,
6583 LOOKUP_ONLYCONVERTING);
6586 pop_scope (pushed_scope);
6588 return convert_from_reference (decl);
6591 /* If we didn't even get past the declarator successfully, we are
6592 definitely not looking at a declaration. */
6594 cp_parser_abort_tentative_parse (parser);
6596 /* Otherwise, we are looking at an expression. */
6597 return cp_parser_expression (parser, /*cast_p=*/false);
6600 /* Parse an iteration-statement.
6602 iteration-statement:
6603 while ( condition ) statement
6604 do statement while ( expression ) ;
6605 for ( for-init-statement condition [opt] ; expression [opt] )
6608 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6611 cp_parser_iteration_statement (cp_parser* parser)
6616 unsigned char in_statement;
6618 /* Peek at the next token. */
6619 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6621 return error_mark_node;
6623 /* Remember whether or not we are already within an iteration
6625 in_statement = parser->in_statement;
6627 /* See what kind of keyword it is. */
6628 keyword = token->keyword;
6635 /* Begin the while-statement. */
6636 statement = begin_while_stmt ();
6637 /* Look for the `('. */
6638 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6639 /* Parse the condition. */
6640 condition = cp_parser_condition (parser);
6641 finish_while_stmt_cond (condition, statement);
6642 /* Look for the `)'. */
6643 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6644 /* Parse the dependent statement. */
6645 parser->in_statement = IN_ITERATION_STMT;
6646 cp_parser_already_scoped_statement (parser);
6647 parser->in_statement = in_statement;
6648 /* We're done with the while-statement. */
6649 finish_while_stmt (statement);
6657 /* Begin the do-statement. */
6658 statement = begin_do_stmt ();
6659 /* Parse the body of the do-statement. */
6660 parser->in_statement = IN_ITERATION_STMT;
6661 cp_parser_implicitly_scoped_statement (parser);
6662 parser->in_statement = in_statement;
6663 finish_do_body (statement);
6664 /* Look for the `while' keyword. */
6665 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6666 /* Look for the `('. */
6667 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6668 /* Parse the expression. */
6669 expression = cp_parser_expression (parser, /*cast_p=*/false);
6670 /* We're done with the do-statement. */
6671 finish_do_stmt (expression, statement);
6672 /* Look for the `)'. */
6673 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6674 /* Look for the `;'. */
6675 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6681 tree condition = NULL_TREE;
6682 tree expression = NULL_TREE;
6684 /* Begin the for-statement. */
6685 statement = begin_for_stmt ();
6686 /* Look for the `('. */
6687 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6688 /* Parse the initialization. */
6689 cp_parser_for_init_statement (parser);
6690 finish_for_init_stmt (statement);
6692 /* If there's a condition, process it. */
6693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6694 condition = cp_parser_condition (parser);
6695 finish_for_cond (condition, statement);
6696 /* Look for the `;'. */
6697 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6699 /* If there's an expression, process it. */
6700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6701 expression = cp_parser_expression (parser, /*cast_p=*/false);
6702 finish_for_expr (expression, statement);
6703 /* Look for the `)'. */
6704 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6706 /* Parse the body of the for-statement. */
6707 parser->in_statement = IN_ITERATION_STMT;
6708 cp_parser_already_scoped_statement (parser);
6709 parser->in_statement = in_statement;
6711 /* We're done with the for-statement. */
6712 finish_for_stmt (statement);
6717 cp_parser_error (parser, "expected iteration-statement");
6718 statement = error_mark_node;
6725 /* Parse a for-init-statement.
6728 expression-statement
6729 simple-declaration */
6732 cp_parser_for_init_statement (cp_parser* parser)
6734 /* If the next token is a `;', then we have an empty
6735 expression-statement. Grammatically, this is also a
6736 simple-declaration, but an invalid one, because it does not
6737 declare anything. Therefore, if we did not handle this case
6738 specially, we would issue an error message about an invalid
6740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6742 /* We're going to speculatively look for a declaration, falling back
6743 to an expression, if necessary. */
6744 cp_parser_parse_tentatively (parser);
6745 /* Parse the declaration. */
6746 cp_parser_simple_declaration (parser,
6747 /*function_definition_allowed_p=*/false);
6748 /* If the tentative parse failed, then we shall need to look for an
6749 expression-statement. */
6750 if (cp_parser_parse_definitely (parser))
6754 cp_parser_expression_statement (parser, false);
6757 /* Parse a jump-statement.
6762 return expression [opt] ;
6770 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6773 cp_parser_jump_statement (cp_parser* parser)
6775 tree statement = error_mark_node;
6779 /* Peek at the next token. */
6780 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6782 return error_mark_node;
6784 /* See what kind of keyword it is. */
6785 keyword = token->keyword;
6789 switch (parser->in_statement)
6792 error ("break statement not within loop or switch");
6795 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6796 || parser->in_statement == IN_ITERATION_STMT);
6797 statement = finish_break_stmt ();
6800 error ("invalid exit from OpenMP structured block");
6803 error ("break statement used with OpenMP for loop");
6806 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6810 switch (parser->in_statement & ~IN_SWITCH_STMT)
6813 error ("continue statement not within a loop");
6815 case IN_ITERATION_STMT:
6817 statement = finish_continue_stmt ();
6820 error ("invalid exit from OpenMP structured block");
6825 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6832 /* If the next token is a `;', then there is no
6834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6835 expr = cp_parser_expression (parser, /*cast_p=*/false);
6838 /* Build the return-statement. */
6839 statement = finish_return_stmt (expr);
6840 /* Look for the final `;'. */
6841 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6846 /* Create the goto-statement. */
6847 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6849 /* Issue a warning about this use of a GNU extension. */
6851 pedwarn ("ISO C++ forbids computed gotos");
6852 /* Consume the '*' token. */
6853 cp_lexer_consume_token (parser->lexer);
6854 /* Parse the dependent expression. */
6855 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6858 finish_goto_stmt (cp_parser_identifier (parser));
6859 /* Look for the final `;'. */
6860 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6864 cp_parser_error (parser, "expected jump-statement");
6871 /* Parse a declaration-statement.
6873 declaration-statement:
6874 block-declaration */
6877 cp_parser_declaration_statement (cp_parser* parser)
6881 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6882 p = obstack_alloc (&declarator_obstack, 0);
6884 /* Parse the block-declaration. */
6885 cp_parser_block_declaration (parser, /*statement_p=*/true);
6887 /* Free any declarators allocated. */
6888 obstack_free (&declarator_obstack, p);
6890 /* Finish off the statement. */
6894 /* Some dependent statements (like `if (cond) statement'), are
6895 implicitly in their own scope. In other words, if the statement is
6896 a single statement (as opposed to a compound-statement), it is
6897 none-the-less treated as if it were enclosed in braces. Any
6898 declarations appearing in the dependent statement are out of scope
6899 after control passes that point. This function parses a statement,
6900 but ensures that is in its own scope, even if it is not a
6903 Returns the new statement. */
6906 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6910 /* Mark if () ; with a special NOP_EXPR. */
6911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6913 cp_lexer_consume_token (parser->lexer);
6914 statement = add_stmt (build_empty_stmt ());
6916 /* if a compound is opened, we simply parse the statement directly. */
6917 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6918 statement = cp_parser_compound_statement (parser, NULL, false);
6919 /* If the token is not a `{', then we must take special action. */
6922 /* Create a compound-statement. */
6923 statement = begin_compound_stmt (0);
6924 /* Parse the dependent-statement. */
6925 cp_parser_statement (parser, NULL_TREE, false);
6926 /* Finish the dummy compound-statement. */
6927 finish_compound_stmt (statement);
6930 /* Return the statement. */
6934 /* For some dependent statements (like `while (cond) statement'), we
6935 have already created a scope. Therefore, even if the dependent
6936 statement is a compound-statement, we do not want to create another
6940 cp_parser_already_scoped_statement (cp_parser* parser)
6942 /* If the token is a `{', then we must take special action. */
6943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6944 cp_parser_statement (parser, NULL_TREE, false);
6947 /* Avoid calling cp_parser_compound_statement, so that we
6948 don't create a new scope. Do everything else by hand. */
6949 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6950 cp_parser_statement_seq_opt (parser, NULL_TREE);
6951 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6955 /* Declarations [gram.dcl.dcl] */
6957 /* Parse an optional declaration-sequence.
6961 declaration-seq declaration */
6964 cp_parser_declaration_seq_opt (cp_parser* parser)
6970 token = cp_lexer_peek_token (parser->lexer);
6972 if (token->type == CPP_CLOSE_BRACE
6973 || token->type == CPP_EOF
6974 || token->type == CPP_PRAGMA_EOL)
6977 if (token->type == CPP_SEMICOLON)
6979 /* A declaration consisting of a single semicolon is
6980 invalid. Allow it unless we're being pedantic. */
6981 cp_lexer_consume_token (parser->lexer);
6982 if (pedantic && !in_system_header)
6983 pedwarn ("extra %<;%>");
6987 /* If we're entering or exiting a region that's implicitly
6988 extern "C", modify the lang context appropriately. */
6989 if (!parser->implicit_extern_c && token->implicit_extern_c)
6991 push_lang_context (lang_name_c);
6992 parser->implicit_extern_c = true;
6994 else if (parser->implicit_extern_c && !token->implicit_extern_c)
6996 pop_lang_context ();
6997 parser->implicit_extern_c = false;
7000 if (token->type == CPP_PRAGMA)
7002 /* A top-level declaration can consist solely of a #pragma.
7003 A nested declaration cannot, so this is done here and not
7004 in cp_parser_declaration. (A #pragma at block scope is
7005 handled in cp_parser_statement.) */
7006 cp_parser_pragma (parser, pragma_external);
7010 /* Parse the declaration itself. */
7011 cp_parser_declaration (parser);
7015 /* Parse a declaration.
7020 template-declaration
7021 explicit-instantiation
7022 explicit-specialization
7023 linkage-specification
7024 namespace-definition
7029 __extension__ declaration */
7032 cp_parser_declaration (cp_parser* parser)
7039 /* Check for the `__extension__' keyword. */
7040 if (cp_parser_extension_opt (parser, &saved_pedantic))
7042 /* Parse the qualified declaration. */
7043 cp_parser_declaration (parser);
7044 /* Restore the PEDANTIC flag. */
7045 pedantic = saved_pedantic;
7050 /* Try to figure out what kind of declaration is present. */
7051 token1 = *cp_lexer_peek_token (parser->lexer);
7053 if (token1.type != CPP_EOF)
7054 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7057 token2.type = CPP_EOF;
7058 token2.keyword = RID_MAX;
7061 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7062 p = obstack_alloc (&declarator_obstack, 0);
7064 /* If the next token is `extern' and the following token is a string
7065 literal, then we have a linkage specification. */
7066 if (token1.keyword == RID_EXTERN
7067 && cp_parser_is_string_literal (&token2))
7068 cp_parser_linkage_specification (parser);
7069 /* If the next token is `template', then we have either a template
7070 declaration, an explicit instantiation, or an explicit
7072 else if (token1.keyword == RID_TEMPLATE)
7074 /* `template <>' indicates a template specialization. */
7075 if (token2.type == CPP_LESS
7076 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7077 cp_parser_explicit_specialization (parser);
7078 /* `template <' indicates a template declaration. */
7079 else if (token2.type == CPP_LESS)
7080 cp_parser_template_declaration (parser, /*member_p=*/false);
7081 /* Anything else must be an explicit instantiation. */
7083 cp_parser_explicit_instantiation (parser);
7085 /* If the next token is `export', then we have a template
7087 else if (token1.keyword == RID_EXPORT)
7088 cp_parser_template_declaration (parser, /*member_p=*/false);
7089 /* If the next token is `extern', 'static' or 'inline' and the one
7090 after that is `template', we have a GNU extended explicit
7091 instantiation directive. */
7092 else if (cp_parser_allow_gnu_extensions_p (parser)
7093 && (token1.keyword == RID_EXTERN
7094 || token1.keyword == RID_STATIC
7095 || token1.keyword == RID_INLINE)
7096 && token2.keyword == RID_TEMPLATE)
7097 cp_parser_explicit_instantiation (parser);
7098 /* If the next token is `namespace', check for a named or unnamed
7099 namespace definition. */
7100 else if (token1.keyword == RID_NAMESPACE
7101 && (/* A named namespace definition. */
7102 (token2.type == CPP_NAME
7103 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7105 /* An unnamed namespace definition. */
7106 || token2.type == CPP_OPEN_BRACE
7107 || token2.keyword == RID_ATTRIBUTE))
7108 cp_parser_namespace_definition (parser);
7109 /* Objective-C++ declaration/definition. */
7110 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7111 cp_parser_objc_declaration (parser);
7112 /* We must have either a block declaration or a function
7115 /* Try to parse a block-declaration, or a function-definition. */
7116 cp_parser_block_declaration (parser, /*statement_p=*/false);
7118 /* Free any declarators allocated. */
7119 obstack_free (&declarator_obstack, p);
7122 /* Parse a block-declaration.
7127 namespace-alias-definition
7134 __extension__ block-declaration
7137 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7138 part of a declaration-statement. */
7141 cp_parser_block_declaration (cp_parser *parser,
7147 /* Check for the `__extension__' keyword. */
7148 if (cp_parser_extension_opt (parser, &saved_pedantic))
7150 /* Parse the qualified declaration. */
7151 cp_parser_block_declaration (parser, statement_p);
7152 /* Restore the PEDANTIC flag. */
7153 pedantic = saved_pedantic;
7158 /* Peek at the next token to figure out which kind of declaration is
7160 token1 = cp_lexer_peek_token (parser->lexer);
7162 /* If the next keyword is `asm', we have an asm-definition. */
7163 if (token1->keyword == RID_ASM)
7166 cp_parser_commit_to_tentative_parse (parser);
7167 cp_parser_asm_definition (parser);
7169 /* If the next keyword is `namespace', we have a
7170 namespace-alias-definition. */
7171 else if (token1->keyword == RID_NAMESPACE)
7172 cp_parser_namespace_alias_definition (parser);
7173 /* If the next keyword is `using', we have either a
7174 using-declaration or a using-directive. */
7175 else if (token1->keyword == RID_USING)
7180 cp_parser_commit_to_tentative_parse (parser);
7181 /* If the token after `using' is `namespace', then we have a
7183 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7184 if (token2->keyword == RID_NAMESPACE)
7185 cp_parser_using_directive (parser);
7186 /* Otherwise, it's a using-declaration. */
7188 cp_parser_using_declaration (parser);
7190 /* If the next keyword is `__label__' we have a label declaration. */
7191 else if (token1->keyword == RID_LABEL)
7194 cp_parser_commit_to_tentative_parse (parser);
7195 cp_parser_label_declaration (parser);
7197 /* Anything else must be a simple-declaration. */
7199 cp_parser_simple_declaration (parser, !statement_p);
7202 /* Parse a simple-declaration.
7205 decl-specifier-seq [opt] init-declarator-list [opt] ;
7207 init-declarator-list:
7209 init-declarator-list , init-declarator
7211 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7212 function-definition as a simple-declaration. */
7215 cp_parser_simple_declaration (cp_parser* parser,
7216 bool function_definition_allowed_p)
7218 cp_decl_specifier_seq decl_specifiers;
7219 int declares_class_or_enum;
7220 bool saw_declarator;
7222 /* Defer access checks until we know what is being declared; the
7223 checks for names appearing in the decl-specifier-seq should be
7224 done as if we were in the scope of the thing being declared. */
7225 push_deferring_access_checks (dk_deferred);
7227 /* Parse the decl-specifier-seq. We have to keep track of whether
7228 or not the decl-specifier-seq declares a named class or
7229 enumeration type, since that is the only case in which the
7230 init-declarator-list is allowed to be empty.
7234 In a simple-declaration, the optional init-declarator-list can be
7235 omitted only when declaring a class or enumeration, that is when
7236 the decl-specifier-seq contains either a class-specifier, an
7237 elaborated-type-specifier, or an enum-specifier. */
7238 cp_parser_decl_specifier_seq (parser,
7239 CP_PARSER_FLAGS_OPTIONAL,
7241 &declares_class_or_enum);
7242 /* We no longer need to defer access checks. */
7243 stop_deferring_access_checks ();
7245 /* In a block scope, a valid declaration must always have a
7246 decl-specifier-seq. By not trying to parse declarators, we can
7247 resolve the declaration/expression ambiguity more quickly. */
7248 if (!function_definition_allowed_p
7249 && !decl_specifiers.any_specifiers_p)
7251 cp_parser_error (parser, "expected declaration");
7255 /* If the next two tokens are both identifiers, the code is
7256 erroneous. The usual cause of this situation is code like:
7260 where "T" should name a type -- but does not. */
7261 if (!decl_specifiers.type
7262 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7264 /* If parsing tentatively, we should commit; we really are
7265 looking at a declaration. */
7266 cp_parser_commit_to_tentative_parse (parser);
7271 /* If we have seen at least one decl-specifier, and the next token
7272 is not a parenthesis, then we must be looking at a declaration.
7273 (After "int (" we might be looking at a functional cast.) */
7274 if (decl_specifiers.any_specifiers_p
7275 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7276 cp_parser_commit_to_tentative_parse (parser);
7278 /* Keep going until we hit the `;' at the end of the simple
7280 saw_declarator = false;
7281 while (cp_lexer_next_token_is_not (parser->lexer,
7285 bool function_definition_p;
7290 /* If we are processing next declarator, coma is expected */
7291 token = cp_lexer_peek_token (parser->lexer);
7292 gcc_assert (token->type == CPP_COMMA);
7293 cp_lexer_consume_token (parser->lexer);
7296 saw_declarator = true;
7298 /* Parse the init-declarator. */
7299 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7300 /*checks=*/NULL_TREE,
7301 function_definition_allowed_p,
7303 declares_class_or_enum,
7304 &function_definition_p);
7305 /* If an error occurred while parsing tentatively, exit quickly.
7306 (That usually happens when in the body of a function; each
7307 statement is treated as a declaration-statement until proven
7309 if (cp_parser_error_occurred (parser))
7311 /* Handle function definitions specially. */
7312 if (function_definition_p)
7314 /* If the next token is a `,', then we are probably
7315 processing something like:
7319 which is erroneous. */
7320 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7321 error ("mixing declarations and function-definitions is forbidden");
7322 /* Otherwise, we're done with the list of declarators. */
7325 pop_deferring_access_checks ();
7329 /* The next token should be either a `,' or a `;'. */
7330 token = cp_lexer_peek_token (parser->lexer);
7331 /* If it's a `,', there are more declarators to come. */
7332 if (token->type == CPP_COMMA)
7333 /* will be consumed next time around */;
7334 /* If it's a `;', we are done. */
7335 else if (token->type == CPP_SEMICOLON)
7337 /* Anything else is an error. */
7340 /* If we have already issued an error message we don't need
7341 to issue another one. */
7342 if (decl != error_mark_node
7343 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7344 cp_parser_error (parser, "expected %<,%> or %<;%>");
7345 /* Skip tokens until we reach the end of the statement. */
7346 cp_parser_skip_to_end_of_statement (parser);
7347 /* If the next token is now a `;', consume it. */
7348 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7349 cp_lexer_consume_token (parser->lexer);
7352 /* After the first time around, a function-definition is not
7353 allowed -- even if it was OK at first. For example:
7358 function_definition_allowed_p = false;
7361 /* Issue an error message if no declarators are present, and the
7362 decl-specifier-seq does not itself declare a class or
7364 if (!saw_declarator)
7366 if (cp_parser_declares_only_class_p (parser))
7367 shadow_tag (&decl_specifiers);
7368 /* Perform any deferred access checks. */
7369 perform_deferred_access_checks ();
7372 /* Consume the `;'. */
7373 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7376 pop_deferring_access_checks ();
7379 /* Parse a decl-specifier-seq.
7382 decl-specifier-seq [opt] decl-specifier
7385 storage-class-specifier
7396 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7398 The parser flags FLAGS is used to control type-specifier parsing.
7400 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7403 1: one of the decl-specifiers is an elaborated-type-specifier
7404 (i.e., a type declaration)
7405 2: one of the decl-specifiers is an enum-specifier or a
7406 class-specifier (i.e., a type definition)
7411 cp_parser_decl_specifier_seq (cp_parser* parser,
7412 cp_parser_flags flags,
7413 cp_decl_specifier_seq *decl_specs,
7414 int* declares_class_or_enum)
7416 bool constructor_possible_p = !parser->in_declarator_p;
7418 /* Clear DECL_SPECS. */
7419 clear_decl_specs (decl_specs);
7421 /* Assume no class or enumeration type is declared. */
7422 *declares_class_or_enum = 0;
7424 /* Keep reading specifiers until there are no more to read. */
7428 bool found_decl_spec;
7431 /* Peek at the next token. */
7432 token = cp_lexer_peek_token (parser->lexer);
7433 /* Handle attributes. */
7434 if (token->keyword == RID_ATTRIBUTE)
7436 /* Parse the attributes. */
7437 decl_specs->attributes
7438 = chainon (decl_specs->attributes,
7439 cp_parser_attributes_opt (parser));
7442 /* Assume we will find a decl-specifier keyword. */
7443 found_decl_spec = true;
7444 /* If the next token is an appropriate keyword, we can simply
7445 add it to the list. */
7446 switch (token->keyword)
7451 if (!at_class_scope_p ())
7453 error ("%<friend%> used outside of class");
7454 cp_lexer_purge_token (parser->lexer);
7458 ++decl_specs->specs[(int) ds_friend];
7459 /* Consume the token. */
7460 cp_lexer_consume_token (parser->lexer);
7464 /* function-specifier:
7471 cp_parser_function_specifier_opt (parser, decl_specs);
7477 ++decl_specs->specs[(int) ds_typedef];
7478 /* Consume the token. */
7479 cp_lexer_consume_token (parser->lexer);
7480 /* A constructor declarator cannot appear in a typedef. */
7481 constructor_possible_p = false;
7482 /* The "typedef" keyword can only occur in a declaration; we
7483 may as well commit at this point. */
7484 cp_parser_commit_to_tentative_parse (parser);
7487 /* storage-class-specifier:
7501 /* Consume the token. */
7502 cp_lexer_consume_token (parser->lexer);
7503 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7506 /* Consume the token. */
7507 cp_lexer_consume_token (parser->lexer);
7508 ++decl_specs->specs[(int) ds_thread];
7512 /* We did not yet find a decl-specifier yet. */
7513 found_decl_spec = false;
7517 /* Constructors are a special case. The `S' in `S()' is not a
7518 decl-specifier; it is the beginning of the declarator. */
7521 && constructor_possible_p
7522 && (cp_parser_constructor_declarator_p
7523 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7525 /* If we don't have a DECL_SPEC yet, then we must be looking at
7526 a type-specifier. */
7527 if (!found_decl_spec && !constructor_p)
7529 int decl_spec_declares_class_or_enum;
7530 bool is_cv_qualifier;
7534 = cp_parser_type_specifier (parser, flags,
7536 /*is_declaration=*/true,
7537 &decl_spec_declares_class_or_enum,
7540 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7542 /* If this type-specifier referenced a user-defined type
7543 (a typedef, class-name, etc.), then we can't allow any
7544 more such type-specifiers henceforth.
7548 The longest sequence of decl-specifiers that could
7549 possibly be a type name is taken as the
7550 decl-specifier-seq of a declaration. The sequence shall
7551 be self-consistent as described below.
7555 As a general rule, at most one type-specifier is allowed
7556 in the complete decl-specifier-seq of a declaration. The
7557 only exceptions are the following:
7559 -- const or volatile can be combined with any other
7562 -- signed or unsigned can be combined with char, long,
7570 void g (const int Pc);
7572 Here, Pc is *not* part of the decl-specifier seq; it's
7573 the declarator. Therefore, once we see a type-specifier
7574 (other than a cv-qualifier), we forbid any additional
7575 user-defined types. We *do* still allow things like `int
7576 int' to be considered a decl-specifier-seq, and issue the
7577 error message later. */
7578 if (type_spec && !is_cv_qualifier)
7579 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7580 /* A constructor declarator cannot follow a type-specifier. */
7583 constructor_possible_p = false;
7584 found_decl_spec = true;
7588 /* If we still do not have a DECL_SPEC, then there are no more
7590 if (!found_decl_spec)
7593 decl_specs->any_specifiers_p = true;
7594 /* After we see one decl-specifier, further decl-specifiers are
7596 flags |= CP_PARSER_FLAGS_OPTIONAL;
7599 cp_parser_check_decl_spec (decl_specs);
7601 /* Don't allow a friend specifier with a class definition. */
7602 if (decl_specs->specs[(int) ds_friend] != 0
7603 && (*declares_class_or_enum & 2))
7604 error ("class definition may not be declared a friend");
7607 /* Parse an (optional) storage-class-specifier.
7609 storage-class-specifier:
7618 storage-class-specifier:
7621 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7624 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7626 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7634 /* Consume the token. */
7635 return cp_lexer_consume_token (parser->lexer)->value;
7642 /* Parse an (optional) function-specifier.
7649 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7650 Updates DECL_SPECS, if it is non-NULL. */
7653 cp_parser_function_specifier_opt (cp_parser* parser,
7654 cp_decl_specifier_seq *decl_specs)
7656 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7660 ++decl_specs->specs[(int) ds_inline];
7664 /* 14.5.2.3 [temp.mem]
7666 A member function template shall not be virtual. */
7667 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7668 error ("templates may not be %<virtual%>");
7669 else if (decl_specs)
7670 ++decl_specs->specs[(int) ds_virtual];
7675 ++decl_specs->specs[(int) ds_explicit];
7682 /* Consume the token. */
7683 return cp_lexer_consume_token (parser->lexer)->value;
7686 /* Parse a linkage-specification.
7688 linkage-specification:
7689 extern string-literal { declaration-seq [opt] }
7690 extern string-literal declaration */
7693 cp_parser_linkage_specification (cp_parser* parser)
7697 /* Look for the `extern' keyword. */
7698 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7700 /* Look for the string-literal. */
7701 linkage = cp_parser_string_literal (parser, false, false);
7703 /* Transform the literal into an identifier. If the literal is a
7704 wide-character string, or contains embedded NULs, then we can't
7705 handle it as the user wants. */
7706 if (strlen (TREE_STRING_POINTER (linkage))
7707 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7709 cp_parser_error (parser, "invalid linkage-specification");
7710 /* Assume C++ linkage. */
7711 linkage = lang_name_cplusplus;
7714 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7716 /* We're now using the new linkage. */
7717 push_lang_context (linkage);
7719 /* If the next token is a `{', then we're using the first
7721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7723 /* Consume the `{' token. */
7724 cp_lexer_consume_token (parser->lexer);
7725 /* Parse the declarations. */
7726 cp_parser_declaration_seq_opt (parser);
7727 /* Look for the closing `}'. */
7728 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7730 /* Otherwise, there's just one declaration. */
7733 bool saved_in_unbraced_linkage_specification_p;
7735 saved_in_unbraced_linkage_specification_p
7736 = parser->in_unbraced_linkage_specification_p;
7737 parser->in_unbraced_linkage_specification_p = true;
7738 cp_parser_declaration (parser);
7739 parser->in_unbraced_linkage_specification_p
7740 = saved_in_unbraced_linkage_specification_p;
7743 /* We're done with the linkage-specification. */
7744 pop_lang_context ();
7747 /* Special member functions [gram.special] */
7749 /* Parse a conversion-function-id.
7751 conversion-function-id:
7752 operator conversion-type-id
7754 Returns an IDENTIFIER_NODE representing the operator. */
7757 cp_parser_conversion_function_id (cp_parser* parser)
7761 tree saved_qualifying_scope;
7762 tree saved_object_scope;
7763 tree pushed_scope = NULL_TREE;
7765 /* Look for the `operator' token. */
7766 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7767 return error_mark_node;
7768 /* When we parse the conversion-type-id, the current scope will be
7769 reset. However, we need that information in able to look up the
7770 conversion function later, so we save it here. */
7771 saved_scope = parser->scope;
7772 saved_qualifying_scope = parser->qualifying_scope;
7773 saved_object_scope = parser->object_scope;
7774 /* We must enter the scope of the class so that the names of
7775 entities declared within the class are available in the
7776 conversion-type-id. For example, consider:
7783 S::operator I() { ... }
7785 In order to see that `I' is a type-name in the definition, we
7786 must be in the scope of `S'. */
7788 pushed_scope = push_scope (saved_scope);
7789 /* Parse the conversion-type-id. */
7790 type = cp_parser_conversion_type_id (parser);
7791 /* Leave the scope of the class, if any. */
7793 pop_scope (pushed_scope);
7794 /* Restore the saved scope. */
7795 parser->scope = saved_scope;
7796 parser->qualifying_scope = saved_qualifying_scope;
7797 parser->object_scope = saved_object_scope;
7798 /* If the TYPE is invalid, indicate failure. */
7799 if (type == error_mark_node)
7800 return error_mark_node;
7801 return mangle_conv_op_name_for_type (type);
7804 /* Parse a conversion-type-id:
7807 type-specifier-seq conversion-declarator [opt]
7809 Returns the TYPE specified. */
7812 cp_parser_conversion_type_id (cp_parser* parser)
7815 cp_decl_specifier_seq type_specifiers;
7816 cp_declarator *declarator;
7817 tree type_specified;
7819 /* Parse the attributes. */
7820 attributes = cp_parser_attributes_opt (parser);
7821 /* Parse the type-specifiers. */
7822 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7824 /* If that didn't work, stop. */
7825 if (type_specifiers.type == error_mark_node)
7826 return error_mark_node;
7827 /* Parse the conversion-declarator. */
7828 declarator = cp_parser_conversion_declarator_opt (parser);
7830 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7831 /*initialized=*/0, &attributes);
7833 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7834 return type_specified;
7837 /* Parse an (optional) conversion-declarator.
7839 conversion-declarator:
7840 ptr-operator conversion-declarator [opt]
7844 static cp_declarator *
7845 cp_parser_conversion_declarator_opt (cp_parser* parser)
7847 enum tree_code code;
7849 cp_cv_quals cv_quals;
7851 /* We don't know if there's a ptr-operator next, or not. */
7852 cp_parser_parse_tentatively (parser);
7853 /* Try the ptr-operator. */
7854 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7855 /* If it worked, look for more conversion-declarators. */
7856 if (cp_parser_parse_definitely (parser))
7858 cp_declarator *declarator;
7860 /* Parse another optional declarator. */
7861 declarator = cp_parser_conversion_declarator_opt (parser);
7863 /* Create the representation of the declarator. */
7865 declarator = make_ptrmem_declarator (cv_quals, class_type,
7867 else if (code == INDIRECT_REF)
7868 declarator = make_pointer_declarator (cv_quals, declarator);
7870 declarator = make_reference_declarator (cv_quals, declarator);
7878 /* Parse an (optional) ctor-initializer.
7881 : mem-initializer-list
7883 Returns TRUE iff the ctor-initializer was actually present. */
7886 cp_parser_ctor_initializer_opt (cp_parser* parser)
7888 /* If the next token is not a `:', then there is no
7889 ctor-initializer. */
7890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7892 /* Do default initialization of any bases and members. */
7893 if (DECL_CONSTRUCTOR_P (current_function_decl))
7894 finish_mem_initializers (NULL_TREE);
7899 /* Consume the `:' token. */
7900 cp_lexer_consume_token (parser->lexer);
7901 /* And the mem-initializer-list. */
7902 cp_parser_mem_initializer_list (parser);
7907 /* Parse a mem-initializer-list.
7909 mem-initializer-list:
7911 mem-initializer , mem-initializer-list */
7914 cp_parser_mem_initializer_list (cp_parser* parser)
7916 tree mem_initializer_list = NULL_TREE;
7918 /* Let the semantic analysis code know that we are starting the
7919 mem-initializer-list. */
7920 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7921 error ("only constructors take base initializers");
7923 /* Loop through the list. */
7926 tree mem_initializer;
7928 /* Parse the mem-initializer. */
7929 mem_initializer = cp_parser_mem_initializer (parser);
7930 /* Add it to the list, unless it was erroneous. */
7931 if (mem_initializer != error_mark_node)
7933 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7934 mem_initializer_list = mem_initializer;
7936 /* If the next token is not a `,', we're done. */
7937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7939 /* Consume the `,' token. */
7940 cp_lexer_consume_token (parser->lexer);
7943 /* Perform semantic analysis. */
7944 if (DECL_CONSTRUCTOR_P (current_function_decl))
7945 finish_mem_initializers (mem_initializer_list);
7948 /* Parse a mem-initializer.
7951 mem-initializer-id ( expression-list [opt] )
7956 ( expression-list [opt] )
7958 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7959 class) or FIELD_DECL (for a non-static data member) to initialize;
7960 the TREE_VALUE is the expression-list. An empty initialization
7961 list is represented by void_list_node. */
7964 cp_parser_mem_initializer (cp_parser* parser)
7966 tree mem_initializer_id;
7967 tree expression_list;
7970 /* Find out what is being initialized. */
7971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7973 pedwarn ("anachronistic old-style base class initializer");
7974 mem_initializer_id = NULL_TREE;
7977 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7978 member = expand_member_init (mem_initializer_id);
7979 if (member && !DECL_P (member))
7980 in_base_initializer = 1;
7983 = cp_parser_parenthesized_expression_list (parser, false,
7985 /*non_constant_p=*/NULL);
7986 if (expression_list == error_mark_node)
7987 return error_mark_node;
7988 if (!expression_list)
7989 expression_list = void_type_node;
7991 in_base_initializer = 0;
7993 return member ? build_tree_list (member, expression_list) : error_mark_node;
7996 /* Parse a mem-initializer-id.
7999 :: [opt] nested-name-specifier [opt] class-name
8002 Returns a TYPE indicating the class to be initializer for the first
8003 production. Returns an IDENTIFIER_NODE indicating the data member
8004 to be initialized for the second production. */
8007 cp_parser_mem_initializer_id (cp_parser* parser)
8009 bool global_scope_p;
8010 bool nested_name_specifier_p;
8011 bool template_p = false;
8014 /* `typename' is not allowed in this context ([temp.res]). */
8015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8017 error ("keyword %<typename%> not allowed in this context (a qualified "
8018 "member initializer is implicitly a type)");
8019 cp_lexer_consume_token (parser->lexer);
8021 /* Look for the optional `::' operator. */
8023 = (cp_parser_global_scope_opt (parser,
8024 /*current_scope_valid_p=*/false)
8026 /* Look for the optional nested-name-specifier. The simplest way to
8031 The keyword `typename' is not permitted in a base-specifier or
8032 mem-initializer; in these contexts a qualified name that
8033 depends on a template-parameter is implicitly assumed to be a
8036 is to assume that we have seen the `typename' keyword at this
8038 nested_name_specifier_p
8039 = (cp_parser_nested_name_specifier_opt (parser,
8040 /*typename_keyword_p=*/true,
8041 /*check_dependency_p=*/true,
8043 /*is_declaration=*/true)
8045 if (nested_name_specifier_p)
8046 template_p = cp_parser_optional_template_keyword (parser);
8047 /* If there is a `::' operator or a nested-name-specifier, then we
8048 are definitely looking for a class-name. */
8049 if (global_scope_p || nested_name_specifier_p)
8050 return cp_parser_class_name (parser,
8051 /*typename_keyword_p=*/true,
8052 /*template_keyword_p=*/template_p,
8054 /*check_dependency_p=*/true,
8055 /*class_head_p=*/false,
8056 /*is_declaration=*/true);
8057 /* Otherwise, we could also be looking for an ordinary identifier. */
8058 cp_parser_parse_tentatively (parser);
8059 /* Try a class-name. */
8060 id = cp_parser_class_name (parser,
8061 /*typename_keyword_p=*/true,
8062 /*template_keyword_p=*/false,
8064 /*check_dependency_p=*/true,
8065 /*class_head_p=*/false,
8066 /*is_declaration=*/true);
8067 /* If we found one, we're done. */
8068 if (cp_parser_parse_definitely (parser))
8070 /* Otherwise, look for an ordinary identifier. */
8071 return cp_parser_identifier (parser);
8074 /* Overloading [gram.over] */
8076 /* Parse an operator-function-id.
8078 operator-function-id:
8081 Returns an IDENTIFIER_NODE for the operator which is a
8082 human-readable spelling of the identifier, e.g., `operator +'. */
8085 cp_parser_operator_function_id (cp_parser* parser)
8087 /* Look for the `operator' keyword. */
8088 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8089 return error_mark_node;
8090 /* And then the name of the operator itself. */
8091 return cp_parser_operator (parser);
8094 /* Parse an operator.
8097 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8098 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8099 || ++ -- , ->* -> () []
8106 Returns an IDENTIFIER_NODE for the operator which is a
8107 human-readable spelling of the identifier, e.g., `operator +'. */
8110 cp_parser_operator (cp_parser* parser)
8112 tree id = NULL_TREE;
8115 /* Peek at the next token. */
8116 token = cp_lexer_peek_token (parser->lexer);
8117 /* Figure out which operator we have. */
8118 switch (token->type)
8124 /* The keyword should be either `new' or `delete'. */
8125 if (token->keyword == RID_NEW)
8127 else if (token->keyword == RID_DELETE)
8132 /* Consume the `new' or `delete' token. */
8133 cp_lexer_consume_token (parser->lexer);
8135 /* Peek at the next token. */
8136 token = cp_lexer_peek_token (parser->lexer);
8137 /* If it's a `[' token then this is the array variant of the
8139 if (token->type == CPP_OPEN_SQUARE)
8141 /* Consume the `[' token. */
8142 cp_lexer_consume_token (parser->lexer);
8143 /* Look for the `]' token. */
8144 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8145 id = ansi_opname (op == NEW_EXPR
8146 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8148 /* Otherwise, we have the non-array variant. */
8150 id = ansi_opname (op);
8156 id = ansi_opname (PLUS_EXPR);
8160 id = ansi_opname (MINUS_EXPR);
8164 id = ansi_opname (MULT_EXPR);
8168 id = ansi_opname (TRUNC_DIV_EXPR);
8172 id = ansi_opname (TRUNC_MOD_EXPR);
8176 id = ansi_opname (BIT_XOR_EXPR);
8180 id = ansi_opname (BIT_AND_EXPR);
8184 id = ansi_opname (BIT_IOR_EXPR);
8188 id = ansi_opname (BIT_NOT_EXPR);
8192 id = ansi_opname (TRUTH_NOT_EXPR);
8196 id = ansi_assopname (NOP_EXPR);
8200 id = ansi_opname (LT_EXPR);
8204 id = ansi_opname (GT_EXPR);
8208 id = ansi_assopname (PLUS_EXPR);
8212 id = ansi_assopname (MINUS_EXPR);
8216 id = ansi_assopname (MULT_EXPR);
8220 id = ansi_assopname (TRUNC_DIV_EXPR);
8224 id = ansi_assopname (TRUNC_MOD_EXPR);
8228 id = ansi_assopname (BIT_XOR_EXPR);
8232 id = ansi_assopname (BIT_AND_EXPR);
8236 id = ansi_assopname (BIT_IOR_EXPR);
8240 id = ansi_opname (LSHIFT_EXPR);
8244 id = ansi_opname (RSHIFT_EXPR);
8248 id = ansi_assopname (LSHIFT_EXPR);
8252 id = ansi_assopname (RSHIFT_EXPR);
8256 id = ansi_opname (EQ_EXPR);
8260 id = ansi_opname (NE_EXPR);
8264 id = ansi_opname (LE_EXPR);
8267 case CPP_GREATER_EQ:
8268 id = ansi_opname (GE_EXPR);
8272 id = ansi_opname (TRUTH_ANDIF_EXPR);
8276 id = ansi_opname (TRUTH_ORIF_EXPR);
8280 id = ansi_opname (POSTINCREMENT_EXPR);
8283 case CPP_MINUS_MINUS:
8284 id = ansi_opname (PREDECREMENT_EXPR);
8288 id = ansi_opname (COMPOUND_EXPR);
8291 case CPP_DEREF_STAR:
8292 id = ansi_opname (MEMBER_REF);
8296 id = ansi_opname (COMPONENT_REF);
8299 case CPP_OPEN_PAREN:
8300 /* Consume the `('. */
8301 cp_lexer_consume_token (parser->lexer);
8302 /* Look for the matching `)'. */
8303 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8304 return ansi_opname (CALL_EXPR);
8306 case CPP_OPEN_SQUARE:
8307 /* Consume the `['. */
8308 cp_lexer_consume_token (parser->lexer);
8309 /* Look for the matching `]'. */
8310 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8311 return ansi_opname (ARRAY_REF);
8314 /* Anything else is an error. */
8318 /* If we have selected an identifier, we need to consume the
8321 cp_lexer_consume_token (parser->lexer);
8322 /* Otherwise, no valid operator name was present. */
8325 cp_parser_error (parser, "expected operator");
8326 id = error_mark_node;
8332 /* Parse a template-declaration.
8334 template-declaration:
8335 export [opt] template < template-parameter-list > declaration
8337 If MEMBER_P is TRUE, this template-declaration occurs within a
8340 The grammar rule given by the standard isn't correct. What
8343 template-declaration:
8344 export [opt] template-parameter-list-seq
8345 decl-specifier-seq [opt] init-declarator [opt] ;
8346 export [opt] template-parameter-list-seq
8349 template-parameter-list-seq:
8350 template-parameter-list-seq [opt]
8351 template < template-parameter-list > */
8354 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8356 /* Check for `export'. */
8357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8359 /* Consume the `export' token. */
8360 cp_lexer_consume_token (parser->lexer);
8361 /* Warn that we do not support `export'. */
8362 warning (0, "keyword %<export%> not implemented, and will be ignored");
8365 cp_parser_template_declaration_after_export (parser, member_p);
8368 /* Parse a template-parameter-list.
8370 template-parameter-list:
8372 template-parameter-list , template-parameter
8374 Returns a TREE_LIST. Each node represents a template parameter.
8375 The nodes are connected via their TREE_CHAINs. */
8378 cp_parser_template_parameter_list (cp_parser* parser)
8380 tree parameter_list = NULL_TREE;
8382 begin_template_parm_list ();
8389 /* Parse the template-parameter. */
8390 parameter = cp_parser_template_parameter (parser, &is_non_type);
8391 /* Add it to the list. */
8392 if (parameter != error_mark_node)
8393 parameter_list = process_template_parm (parameter_list,
8396 /* Peek at the next token. */
8397 token = cp_lexer_peek_token (parser->lexer);
8398 /* If it's not a `,', we're done. */
8399 if (token->type != CPP_COMMA)
8401 /* Otherwise, consume the `,' token. */
8402 cp_lexer_consume_token (parser->lexer);
8405 return end_template_parm_list (parameter_list);
8408 /* Parse a template-parameter.
8412 parameter-declaration
8414 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8415 the parameter. The TREE_PURPOSE is the default value, if any.
8416 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8417 iff this parameter is a non-type parameter. */
8420 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8423 cp_parameter_declarator *parameter_declarator;
8426 /* Assume it is a type parameter or a template parameter. */
8427 *is_non_type = false;
8428 /* Peek at the next token. */
8429 token = cp_lexer_peek_token (parser->lexer);
8430 /* If it is `class' or `template', we have a type-parameter. */
8431 if (token->keyword == RID_TEMPLATE)
8432 return cp_parser_type_parameter (parser);
8433 /* If it is `class' or `typename' we do not know yet whether it is a
8434 type parameter or a non-type parameter. Consider:
8436 template <typename T, typename T::X X> ...
8440 template <class C, class D*> ...
8442 Here, the first parameter is a type parameter, and the second is
8443 a non-type parameter. We can tell by looking at the token after
8444 the identifier -- if it is a `,', `=', or `>' then we have a type
8446 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8448 /* Peek at the token after `class' or `typename'. */
8449 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8450 /* If it's an identifier, skip it. */
8451 if (token->type == CPP_NAME)
8452 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8453 /* Now, see if the token looks like the end of a template
8455 if (token->type == CPP_COMMA
8456 || token->type == CPP_EQ
8457 || token->type == CPP_GREATER)
8458 return cp_parser_type_parameter (parser);
8461 /* Otherwise, it is a non-type parameter.
8465 When parsing a default template-argument for a non-type
8466 template-parameter, the first non-nested `>' is taken as the end
8467 of the template parameter-list rather than a greater-than
8469 *is_non_type = true;
8470 parameter_declarator
8471 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8472 /*parenthesized_p=*/NULL);
8473 parm = grokdeclarator (parameter_declarator->declarator,
8474 ¶meter_declarator->decl_specifiers,
8475 PARM, /*initialized=*/0,
8477 if (parm == error_mark_node)
8478 return error_mark_node;
8479 return build_tree_list (parameter_declarator->default_argument, parm);
8482 /* Parse a type-parameter.
8485 class identifier [opt]
8486 class identifier [opt] = type-id
8487 typename identifier [opt]
8488 typename identifier [opt] = type-id
8489 template < template-parameter-list > class identifier [opt]
8490 template < template-parameter-list > class identifier [opt]
8493 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8494 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8495 the declaration of the parameter. */
8498 cp_parser_type_parameter (cp_parser* parser)
8503 /* Look for a keyword to tell us what kind of parameter this is. */
8504 token = cp_parser_require (parser, CPP_KEYWORD,
8505 "`class', `typename', or `template'");
8507 return error_mark_node;
8509 switch (token->keyword)
8515 tree default_argument;
8517 /* If the next token is an identifier, then it names the
8519 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8520 identifier = cp_parser_identifier (parser);
8522 identifier = NULL_TREE;
8524 /* Create the parameter. */
8525 parameter = finish_template_type_parm (class_type_node, identifier);
8527 /* If the next token is an `=', we have a default argument. */
8528 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8530 /* Consume the `=' token. */
8531 cp_lexer_consume_token (parser->lexer);
8532 /* Parse the default-argument. */
8533 push_deferring_access_checks (dk_no_deferred);
8534 default_argument = cp_parser_type_id (parser);
8535 pop_deferring_access_checks ();
8538 default_argument = NULL_TREE;
8540 /* Create the combined representation of the parameter and the
8541 default argument. */
8542 parameter = build_tree_list (default_argument, parameter);
8548 tree parameter_list;
8550 tree default_argument;
8552 /* Look for the `<'. */
8553 cp_parser_require (parser, CPP_LESS, "`<'");
8554 /* Parse the template-parameter-list. */
8555 parameter_list = cp_parser_template_parameter_list (parser);
8556 /* Look for the `>'. */
8557 cp_parser_require (parser, CPP_GREATER, "`>'");
8558 /* Look for the `class' keyword. */
8559 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8560 /* If the next token is an `=', then there is a
8561 default-argument. If the next token is a `>', we are at
8562 the end of the parameter-list. If the next token is a `,',
8563 then we are at the end of this parameter. */
8564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8565 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8566 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8568 identifier = cp_parser_identifier (parser);
8569 /* Treat invalid names as if the parameter were nameless. */
8570 if (identifier == error_mark_node)
8571 identifier = NULL_TREE;
8574 identifier = NULL_TREE;
8576 /* Create the template parameter. */
8577 parameter = finish_template_template_parm (class_type_node,
8580 /* If the next token is an `=', then there is a
8581 default-argument. */
8582 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8586 /* Consume the `='. */
8587 cp_lexer_consume_token (parser->lexer);
8588 /* Parse the id-expression. */
8589 push_deferring_access_checks (dk_no_deferred);
8591 = cp_parser_id_expression (parser,
8592 /*template_keyword_p=*/false,
8593 /*check_dependency_p=*/true,
8594 /*template_p=*/&is_template,
8595 /*declarator_p=*/false,
8596 /*optional_p=*/false);
8597 if (TREE_CODE (default_argument) == TYPE_DECL)
8598 /* If the id-expression was a template-id that refers to
8599 a template-class, we already have the declaration here,
8600 so no further lookup is needed. */
8603 /* Look up the name. */
8605 = cp_parser_lookup_name (parser, default_argument,
8607 /*is_template=*/is_template,
8608 /*is_namespace=*/false,
8609 /*check_dependency=*/true,
8610 /*ambiguous_decls=*/NULL);
8611 /* See if the default argument is valid. */
8613 = check_template_template_default_arg (default_argument);
8614 pop_deferring_access_checks ();
8617 default_argument = NULL_TREE;
8619 /* Create the combined representation of the parameter and the
8620 default argument. */
8621 parameter = build_tree_list (default_argument, parameter);
8633 /* Parse a template-id.
8636 template-name < template-argument-list [opt] >
8638 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8639 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8640 returned. Otherwise, if the template-name names a function, or set
8641 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8642 names a class, returns a TYPE_DECL for the specialization.
8644 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8645 uninstantiated templates. */
8648 cp_parser_template_id (cp_parser *parser,
8649 bool template_keyword_p,
8650 bool check_dependency_p,
8651 bool is_declaration)
8656 cp_token_position start_of_id = 0;
8657 tree access_check = NULL_TREE;
8658 cp_token *next_token, *next_token_2;
8661 /* If the next token corresponds to a template-id, there is no need
8663 next_token = cp_lexer_peek_token (parser->lexer);
8664 if (next_token->type == CPP_TEMPLATE_ID)
8669 /* Get the stored value. */
8670 value = cp_lexer_consume_token (parser->lexer)->value;
8671 /* Perform any access checks that were deferred. */
8672 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8673 perform_or_defer_access_check (TREE_PURPOSE (check),
8674 TREE_VALUE (check));
8675 /* Return the stored value. */
8676 return TREE_VALUE (value);
8679 /* Avoid performing name lookup if there is no possibility of
8680 finding a template-id. */
8681 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8682 || (next_token->type == CPP_NAME
8683 && !cp_parser_nth_token_starts_template_argument_list_p
8686 cp_parser_error (parser, "expected template-id");
8687 return error_mark_node;
8690 /* Remember where the template-id starts. */
8691 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8692 start_of_id = cp_lexer_token_position (parser->lexer, false);
8694 push_deferring_access_checks (dk_deferred);
8696 /* Parse the template-name. */
8697 is_identifier = false;
8698 template = cp_parser_template_name (parser, template_keyword_p,
8702 if (template == error_mark_node || is_identifier)
8704 pop_deferring_access_checks ();
8708 /* If we find the sequence `[:' after a template-name, it's probably
8709 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8710 parse correctly the argument list. */
8711 next_token = cp_lexer_peek_token (parser->lexer);
8712 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8713 if (next_token->type == CPP_OPEN_SQUARE
8714 && next_token->flags & DIGRAPH
8715 && next_token_2->type == CPP_COLON
8716 && !(next_token_2->flags & PREV_WHITE))
8718 cp_parser_parse_tentatively (parser);
8719 /* Change `:' into `::'. */
8720 next_token_2->type = CPP_SCOPE;
8721 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8723 cp_lexer_consume_token (parser->lexer);
8724 /* Parse the arguments. */
8725 arguments = cp_parser_enclosed_template_argument_list (parser);
8726 if (!cp_parser_parse_definitely (parser))
8728 /* If we couldn't parse an argument list, then we revert our changes
8729 and return simply an error. Maybe this is not a template-id
8731 next_token_2->type = CPP_COLON;
8732 cp_parser_error (parser, "expected %<<%>");
8733 pop_deferring_access_checks ();
8734 return error_mark_node;
8736 /* Otherwise, emit an error about the invalid digraph, but continue
8737 parsing because we got our argument list. */
8738 pedwarn ("%<<::%> cannot begin a template-argument list");
8739 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8740 "between %<<%> and %<::%>");
8741 if (!flag_permissive)
8746 inform ("(if you use -fpermissive G++ will accept your code)");
8753 /* Look for the `<' that starts the template-argument-list. */
8754 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8756 pop_deferring_access_checks ();
8757 return error_mark_node;
8759 /* Parse the arguments. */
8760 arguments = cp_parser_enclosed_template_argument_list (parser);
8763 /* Build a representation of the specialization. */
8764 if (TREE_CODE (template) == IDENTIFIER_NODE)
8765 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8766 else if (DECL_CLASS_TEMPLATE_P (template)
8767 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8769 bool entering_scope;
8770 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8771 template (rather than some instantiation thereof) only if
8772 is not nested within some other construct. For example, in
8773 "template <typename T> void f(T) { A<T>::", A<T> is just an
8774 instantiation of A. */
8775 entering_scope = (template_parm_scope_p ()
8776 && cp_lexer_next_token_is (parser->lexer,
8779 = finish_template_type (template, arguments, entering_scope);
8783 /* If it's not a class-template or a template-template, it should be
8784 a function-template. */
8785 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8786 || TREE_CODE (template) == OVERLOAD
8787 || BASELINK_P (template)));
8789 template_id = lookup_template_function (template, arguments);
8792 /* Retrieve any deferred checks. Do not pop this access checks yet
8793 so the memory will not be reclaimed during token replacing below. */
8794 access_check = get_deferred_access_checks ();
8796 /* If parsing tentatively, replace the sequence of tokens that makes
8797 up the template-id with a CPP_TEMPLATE_ID token. That way,
8798 should we re-parse the token stream, we will not have to repeat
8799 the effort required to do the parse, nor will we issue duplicate
8800 error messages about problems during instantiation of the
8804 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8806 /* Reset the contents of the START_OF_ID token. */
8807 token->type = CPP_TEMPLATE_ID;
8808 token->value = build_tree_list (access_check, template_id);
8809 token->keyword = RID_MAX;
8811 /* Purge all subsequent tokens. */
8812 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8814 /* ??? Can we actually assume that, if template_id ==
8815 error_mark_node, we will have issued a diagnostic to the
8816 user, as opposed to simply marking the tentative parse as
8818 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8819 error ("parse error in template argument list");
8822 pop_deferring_access_checks ();
8826 /* Parse a template-name.
8831 The standard should actually say:
8835 operator-function-id
8837 A defect report has been filed about this issue.
8839 A conversion-function-id cannot be a template name because they cannot
8840 be part of a template-id. In fact, looking at this code:
8844 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8845 It is impossible to call a templated conversion-function-id with an
8846 explicit argument list, since the only allowed template parameter is
8847 the type to which it is converting.
8849 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8850 `template' keyword, in a construction like:
8854 In that case `f' is taken to be a template-name, even though there
8855 is no way of knowing for sure.
8857 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8858 name refers to a set of overloaded functions, at least one of which
8859 is a template, or an IDENTIFIER_NODE with the name of the template,
8860 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8861 names are looked up inside uninstantiated templates. */
8864 cp_parser_template_name (cp_parser* parser,
8865 bool template_keyword_p,
8866 bool check_dependency_p,
8867 bool is_declaration,
8868 bool *is_identifier)
8874 /* If the next token is `operator', then we have either an
8875 operator-function-id or a conversion-function-id. */
8876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8878 /* We don't know whether we're looking at an
8879 operator-function-id or a conversion-function-id. */
8880 cp_parser_parse_tentatively (parser);
8881 /* Try an operator-function-id. */
8882 identifier = cp_parser_operator_function_id (parser);
8883 /* If that didn't work, try a conversion-function-id. */
8884 if (!cp_parser_parse_definitely (parser))
8886 cp_parser_error (parser, "expected template-name");
8887 return error_mark_node;
8890 /* Look for the identifier. */
8892 identifier = cp_parser_identifier (parser);
8894 /* If we didn't find an identifier, we don't have a template-id. */
8895 if (identifier == error_mark_node)
8896 return error_mark_node;
8898 /* If the name immediately followed the `template' keyword, then it
8899 is a template-name. However, if the next token is not `<', then
8900 we do not treat it as a template-name, since it is not being used
8901 as part of a template-id. This enables us to handle constructs
8904 template <typename T> struct S { S(); };
8905 template <typename T> S<T>::S();
8907 correctly. We would treat `S' as a template -- if it were `S<T>'
8908 -- but we do not if there is no `<'. */
8910 if (processing_template_decl
8911 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8913 /* In a declaration, in a dependent context, we pretend that the
8914 "template" keyword was present in order to improve error
8915 recovery. For example, given:
8917 template <typename T> void f(T::X<int>);
8919 we want to treat "X<int>" as a template-id. */
8921 && !template_keyword_p
8922 && parser->scope && TYPE_P (parser->scope)
8923 && check_dependency_p
8924 && dependent_type_p (parser->scope)
8925 /* Do not do this for dtors (or ctors), since they never
8926 need the template keyword before their name. */
8927 && !constructor_name_p (identifier, parser->scope))
8929 cp_token_position start = 0;
8931 /* Explain what went wrong. */
8932 error ("non-template %qD used as template", identifier);
8933 inform ("use %<%T::template %D%> to indicate that it is a template",
8934 parser->scope, identifier);
8935 /* If parsing tentatively, find the location of the "<" token. */
8936 if (cp_parser_simulate_error (parser))
8937 start = cp_lexer_token_position (parser->lexer, true);
8938 /* Parse the template arguments so that we can issue error
8939 messages about them. */
8940 cp_lexer_consume_token (parser->lexer);
8941 cp_parser_enclosed_template_argument_list (parser);
8942 /* Skip tokens until we find a good place from which to
8943 continue parsing. */
8944 cp_parser_skip_to_closing_parenthesis (parser,
8945 /*recovering=*/true,
8947 /*consume_paren=*/false);
8948 /* If parsing tentatively, permanently remove the
8949 template argument list. That will prevent duplicate
8950 error messages from being issued about the missing
8951 "template" keyword. */
8953 cp_lexer_purge_tokens_after (parser->lexer, start);
8955 *is_identifier = true;
8959 /* If the "template" keyword is present, then there is generally
8960 no point in doing name-lookup, so we just return IDENTIFIER.
8961 But, if the qualifying scope is non-dependent then we can
8962 (and must) do name-lookup normally. */
8963 if (template_keyword_p
8965 || (TYPE_P (parser->scope)
8966 && dependent_type_p (parser->scope))))
8970 /* Look up the name. */
8971 decl = cp_parser_lookup_name (parser, identifier,
8973 /*is_template=*/false,
8974 /*is_namespace=*/false,
8976 /*ambiguous_decls=*/NULL);
8977 decl = maybe_get_template_decl_from_type_decl (decl);
8979 /* If DECL is a template, then the name was a template-name. */
8980 if (TREE_CODE (decl) == TEMPLATE_DECL)
8984 tree fn = NULL_TREE;
8986 /* The standard does not explicitly indicate whether a name that
8987 names a set of overloaded declarations, some of which are
8988 templates, is a template-name. However, such a name should
8989 be a template-name; otherwise, there is no way to form a
8990 template-id for the overloaded templates. */
8991 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8992 if (TREE_CODE (fns) == OVERLOAD)
8993 for (fn = fns; fn; fn = OVL_NEXT (fn))
8994 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8999 /* The name does not name a template. */
9000 cp_parser_error (parser, "expected template-name");
9001 return error_mark_node;
9005 /* If DECL is dependent, and refers to a function, then just return
9006 its name; we will look it up again during template instantiation. */
9007 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9009 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9010 if (TYPE_P (scope) && dependent_type_p (scope))
9017 /* Parse a template-argument-list.
9019 template-argument-list:
9021 template-argument-list , template-argument
9023 Returns a TREE_VEC containing the arguments. */
9026 cp_parser_template_argument_list (cp_parser* parser)
9028 tree fixed_args[10];
9029 unsigned n_args = 0;
9030 unsigned alloced = 10;
9031 tree *arg_ary = fixed_args;
9033 bool saved_in_template_argument_list_p;
9035 bool saved_non_ice_p;
9037 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9038 parser->in_template_argument_list_p = true;
9039 /* Even if the template-id appears in an integral
9040 constant-expression, the contents of the argument list do
9042 saved_ice_p = parser->integral_constant_expression_p;
9043 parser->integral_constant_expression_p = false;
9044 saved_non_ice_p = parser->non_integral_constant_expression_p;
9045 parser->non_integral_constant_expression_p = false;
9046 /* Parse the arguments. */
9052 /* Consume the comma. */
9053 cp_lexer_consume_token (parser->lexer);
9055 /* Parse the template-argument. */
9056 argument = cp_parser_template_argument (parser);
9057 if (n_args == alloced)
9061 if (arg_ary == fixed_args)
9063 arg_ary = XNEWVEC (tree, alloced);
9064 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9067 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9069 arg_ary[n_args++] = argument;
9071 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9073 vec = make_tree_vec (n_args);
9076 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9078 if (arg_ary != fixed_args)
9080 parser->non_integral_constant_expression_p = saved_non_ice_p;
9081 parser->integral_constant_expression_p = saved_ice_p;
9082 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9086 /* Parse a template-argument.
9089 assignment-expression
9093 The representation is that of an assignment-expression, type-id, or
9094 id-expression -- except that the qualified id-expression is
9095 evaluated, so that the value returned is either a DECL or an
9098 Although the standard says "assignment-expression", it forbids
9099 throw-expressions or assignments in the template argument.
9100 Therefore, we use "conditional-expression" instead. */
9103 cp_parser_template_argument (cp_parser* parser)
9108 bool maybe_type_id = false;
9112 /* There's really no way to know what we're looking at, so we just
9113 try each alternative in order.
9117 In a template-argument, an ambiguity between a type-id and an
9118 expression is resolved to a type-id, regardless of the form of
9119 the corresponding template-parameter.
9121 Therefore, we try a type-id first. */
9122 cp_parser_parse_tentatively (parser);
9123 argument = cp_parser_type_id (parser);
9124 /* If there was no error parsing the type-id but the next token is a '>>',
9125 we probably found a typo for '> >'. But there are type-id which are
9126 also valid expressions. For instance:
9128 struct X { int operator >> (int); };
9129 template <int V> struct Foo {};
9132 Here 'X()' is a valid type-id of a function type, but the user just
9133 wanted to write the expression "X() >> 5". Thus, we remember that we
9134 found a valid type-id, but we still try to parse the argument as an
9135 expression to see what happens. */
9136 if (!cp_parser_error_occurred (parser)
9137 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9139 maybe_type_id = true;
9140 cp_parser_abort_tentative_parse (parser);
9144 /* If the next token isn't a `,' or a `>', then this argument wasn't
9145 really finished. This means that the argument is not a valid
9147 if (!cp_parser_next_token_ends_template_argument_p (parser))
9148 cp_parser_error (parser, "expected template-argument");
9149 /* If that worked, we're done. */
9150 if (cp_parser_parse_definitely (parser))
9153 /* We're still not sure what the argument will be. */
9154 cp_parser_parse_tentatively (parser);
9155 /* Try a template. */
9156 argument = cp_parser_id_expression (parser,
9157 /*template_keyword_p=*/false,
9158 /*check_dependency_p=*/true,
9160 /*declarator_p=*/false,
9161 /*optional_p=*/false);
9162 /* If the next token isn't a `,' or a `>', then this argument wasn't
9164 if (!cp_parser_next_token_ends_template_argument_p (parser))
9165 cp_parser_error (parser, "expected template-argument");
9166 if (!cp_parser_error_occurred (parser))
9168 /* Figure out what is being referred to. If the id-expression
9169 was for a class template specialization, then we will have a
9170 TYPE_DECL at this point. There is no need to do name lookup
9171 at this point in that case. */
9172 if (TREE_CODE (argument) != TYPE_DECL)
9173 argument = cp_parser_lookup_name (parser, argument,
9175 /*is_template=*/template_p,
9176 /*is_namespace=*/false,
9177 /*check_dependency=*/true,
9178 /*ambiguous_decls=*/NULL);
9179 if (TREE_CODE (argument) != TEMPLATE_DECL
9180 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9181 cp_parser_error (parser, "expected template-name");
9183 if (cp_parser_parse_definitely (parser))
9185 /* It must be a non-type argument. There permitted cases are given
9186 in [temp.arg.nontype]:
9188 -- an integral constant-expression of integral or enumeration
9191 -- the name of a non-type template-parameter; or
9193 -- the name of an object or function with external linkage...
9195 -- the address of an object or function with external linkage...
9197 -- a pointer to member... */
9198 /* Look for a non-type template parameter. */
9199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9201 cp_parser_parse_tentatively (parser);
9202 argument = cp_parser_primary_expression (parser,
9205 /*template_arg_p=*/true,
9207 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9208 || !cp_parser_next_token_ends_template_argument_p (parser))
9209 cp_parser_simulate_error (parser);
9210 if (cp_parser_parse_definitely (parser))
9214 /* If the next token is "&", the argument must be the address of an
9215 object or function with external linkage. */
9216 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9218 cp_lexer_consume_token (parser->lexer);
9219 /* See if we might have an id-expression. */
9220 token = cp_lexer_peek_token (parser->lexer);
9221 if (token->type == CPP_NAME
9222 || token->keyword == RID_OPERATOR
9223 || token->type == CPP_SCOPE
9224 || token->type == CPP_TEMPLATE_ID
9225 || token->type == CPP_NESTED_NAME_SPECIFIER)
9227 cp_parser_parse_tentatively (parser);
9228 argument = cp_parser_primary_expression (parser,
9231 /*template_arg_p=*/true,
9233 if (cp_parser_error_occurred (parser)
9234 || !cp_parser_next_token_ends_template_argument_p (parser))
9235 cp_parser_abort_tentative_parse (parser);
9238 if (TREE_CODE (argument) == INDIRECT_REF)
9240 gcc_assert (REFERENCE_REF_P (argument));
9241 argument = TREE_OPERAND (argument, 0);
9244 if (TREE_CODE (argument) == BASELINK)
9245 /* We don't need the information about what class was used
9246 to name the overloaded functions. */
9247 argument = BASELINK_FUNCTIONS (argument);
9249 if (TREE_CODE (argument) == VAR_DECL)
9251 /* A variable without external linkage might still be a
9252 valid constant-expression, so no error is issued here
9253 if the external-linkage check fails. */
9254 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9255 cp_parser_simulate_error (parser);
9257 else if (is_overloaded_fn (argument))
9258 /* All overloaded functions are allowed; if the external
9259 linkage test does not pass, an error will be issued
9263 && (TREE_CODE (argument) == OFFSET_REF
9264 || TREE_CODE (argument) == SCOPE_REF))
9265 /* A pointer-to-member. */
9267 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9270 cp_parser_simulate_error (parser);
9272 if (cp_parser_parse_definitely (parser))
9275 argument = build_x_unary_op (ADDR_EXPR, argument);
9280 /* If the argument started with "&", there are no other valid
9281 alternatives at this point. */
9284 cp_parser_error (parser, "invalid non-type template argument");
9285 return error_mark_node;
9288 /* If the argument wasn't successfully parsed as a type-id followed
9289 by '>>', the argument can only be a constant expression now.
9290 Otherwise, we try parsing the constant-expression tentatively,
9291 because the argument could really be a type-id. */
9293 cp_parser_parse_tentatively (parser);
9294 argument = cp_parser_constant_expression (parser,
9295 /*allow_non_constant_p=*/false,
9296 /*non_constant_p=*/NULL);
9297 argument = fold_non_dependent_expr (argument);
9300 if (!cp_parser_next_token_ends_template_argument_p (parser))
9301 cp_parser_error (parser, "expected template-argument");
9302 if (cp_parser_parse_definitely (parser))
9304 /* We did our best to parse the argument as a non type-id, but that
9305 was the only alternative that matched (albeit with a '>' after
9306 it). We can assume it's just a typo from the user, and a
9307 diagnostic will then be issued. */
9308 return cp_parser_type_id (parser);
9311 /* Parse an explicit-instantiation.
9313 explicit-instantiation:
9314 template declaration
9316 Although the standard says `declaration', what it really means is:
9318 explicit-instantiation:
9319 template decl-specifier-seq [opt] declarator [opt] ;
9321 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9322 supposed to be allowed. A defect report has been filed about this
9327 explicit-instantiation:
9328 storage-class-specifier template
9329 decl-specifier-seq [opt] declarator [opt] ;
9330 function-specifier template
9331 decl-specifier-seq [opt] declarator [opt] ; */
9334 cp_parser_explicit_instantiation (cp_parser* parser)
9336 int declares_class_or_enum;
9337 cp_decl_specifier_seq decl_specifiers;
9338 tree extension_specifier = NULL_TREE;
9340 /* Look for an (optional) storage-class-specifier or
9341 function-specifier. */
9342 if (cp_parser_allow_gnu_extensions_p (parser))
9345 = cp_parser_storage_class_specifier_opt (parser);
9346 if (!extension_specifier)
9348 = cp_parser_function_specifier_opt (parser,
9349 /*decl_specs=*/NULL);
9352 /* Look for the `template' keyword. */
9353 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9354 /* Let the front end know that we are processing an explicit
9356 begin_explicit_instantiation ();
9357 /* [temp.explicit] says that we are supposed to ignore access
9358 control while processing explicit instantiation directives. */
9359 push_deferring_access_checks (dk_no_check);
9360 /* Parse a decl-specifier-seq. */
9361 cp_parser_decl_specifier_seq (parser,
9362 CP_PARSER_FLAGS_OPTIONAL,
9364 &declares_class_or_enum);
9365 /* If there was exactly one decl-specifier, and it declared a class,
9366 and there's no declarator, then we have an explicit type
9368 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9372 type = check_tag_decl (&decl_specifiers);
9373 /* Turn access control back on for names used during
9374 template instantiation. */
9375 pop_deferring_access_checks ();
9377 do_type_instantiation (type, extension_specifier,
9378 /*complain=*/tf_error);
9382 cp_declarator *declarator;
9385 /* Parse the declarator. */
9387 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9388 /*ctor_dtor_or_conv_p=*/NULL,
9389 /*parenthesized_p=*/NULL,
9390 /*member_p=*/false);
9391 if (declares_class_or_enum & 2)
9392 cp_parser_check_for_definition_in_return_type (declarator,
9393 decl_specifiers.type);
9394 if (declarator != cp_error_declarator)
9396 decl = grokdeclarator (declarator, &decl_specifiers,
9397 NORMAL, 0, &decl_specifiers.attributes);
9398 /* Turn access control back on for names used during
9399 template instantiation. */
9400 pop_deferring_access_checks ();
9401 /* Do the explicit instantiation. */
9402 do_decl_instantiation (decl, extension_specifier);
9406 pop_deferring_access_checks ();
9407 /* Skip the body of the explicit instantiation. */
9408 cp_parser_skip_to_end_of_statement (parser);
9411 /* We're done with the instantiation. */
9412 end_explicit_instantiation ();
9414 cp_parser_consume_semicolon_at_end_of_statement (parser);
9417 /* Parse an explicit-specialization.
9419 explicit-specialization:
9420 template < > declaration
9422 Although the standard says `declaration', what it really means is:
9424 explicit-specialization:
9425 template <> decl-specifier [opt] init-declarator [opt] ;
9426 template <> function-definition
9427 template <> explicit-specialization
9428 template <> template-declaration */
9431 cp_parser_explicit_specialization (cp_parser* parser)
9434 /* Look for the `template' keyword. */
9435 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9436 /* Look for the `<'. */
9437 cp_parser_require (parser, CPP_LESS, "`<'");
9438 /* Look for the `>'. */
9439 cp_parser_require (parser, CPP_GREATER, "`>'");
9440 /* We have processed another parameter list. */
9441 ++parser->num_template_parameter_lists;
9444 A template ... explicit specialization ... shall not have C
9446 if (current_lang_name == lang_name_c)
9448 error ("template specialization with C linkage");
9449 /* Give it C++ linkage to avoid confusing other parts of the
9451 push_lang_context (lang_name_cplusplus);
9452 need_lang_pop = true;
9455 need_lang_pop = false;
9456 /* Let the front end know that we are beginning a specialization. */
9457 begin_specialization ();
9458 /* If the next keyword is `template', we need to figure out whether
9459 or not we're looking a template-declaration. */
9460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9462 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9464 cp_parser_template_declaration_after_export (parser,
9465 /*member_p=*/false);
9467 cp_parser_explicit_specialization (parser);
9470 /* Parse the dependent declaration. */
9471 cp_parser_single_declaration (parser,
9472 /*checks=*/NULL_TREE,
9475 /* We're done with the specialization. */
9476 end_specialization ();
9477 /* For the erroneous case of a template with C linkage, we pushed an
9478 implicit C++ linkage scope; exit that scope now. */
9480 pop_lang_context ();
9481 /* We're done with this parameter list. */
9482 --parser->num_template_parameter_lists;
9485 /* Parse a type-specifier.
9488 simple-type-specifier
9491 elaborated-type-specifier
9499 Returns a representation of the type-specifier. For a
9500 class-specifier, enum-specifier, or elaborated-type-specifier, a
9501 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9503 The parser flags FLAGS is used to control type-specifier parsing.
9505 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9506 in a decl-specifier-seq.
9508 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9509 class-specifier, enum-specifier, or elaborated-type-specifier, then
9510 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9511 if a type is declared; 2 if it is defined. Otherwise, it is set to
9514 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9515 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9519 cp_parser_type_specifier (cp_parser* parser,
9520 cp_parser_flags flags,
9521 cp_decl_specifier_seq *decl_specs,
9522 bool is_declaration,
9523 int* declares_class_or_enum,
9524 bool* is_cv_qualifier)
9526 tree type_spec = NULL_TREE;
9529 cp_decl_spec ds = ds_last;
9531 /* Assume this type-specifier does not declare a new type. */
9532 if (declares_class_or_enum)
9533 *declares_class_or_enum = 0;
9534 /* And that it does not specify a cv-qualifier. */
9535 if (is_cv_qualifier)
9536 *is_cv_qualifier = false;
9537 /* Peek at the next token. */
9538 token = cp_lexer_peek_token (parser->lexer);
9540 /* If we're looking at a keyword, we can use that to guide the
9541 production we choose. */
9542 keyword = token->keyword;
9546 /* Look for the enum-specifier. */
9547 type_spec = cp_parser_enum_specifier (parser);
9548 /* If that worked, we're done. */
9551 if (declares_class_or_enum)
9552 *declares_class_or_enum = 2;
9554 cp_parser_set_decl_spec_type (decl_specs,
9556 /*user_defined_p=*/true);
9560 goto elaborated_type_specifier;
9562 /* Any of these indicate either a class-specifier, or an
9563 elaborated-type-specifier. */
9567 /* Parse tentatively so that we can back up if we don't find a
9569 cp_parser_parse_tentatively (parser);
9570 /* Look for the class-specifier. */
9571 type_spec = cp_parser_class_specifier (parser);
9572 /* If that worked, we're done. */
9573 if (cp_parser_parse_definitely (parser))
9575 if (declares_class_or_enum)
9576 *declares_class_or_enum = 2;
9578 cp_parser_set_decl_spec_type (decl_specs,
9580 /*user_defined_p=*/true);
9585 elaborated_type_specifier:
9586 /* We're declaring (not defining) a class or enum. */
9587 if (declares_class_or_enum)
9588 *declares_class_or_enum = 1;
9592 /* Look for an elaborated-type-specifier. */
9594 = (cp_parser_elaborated_type_specifier
9596 decl_specs && decl_specs->specs[(int) ds_friend],
9599 cp_parser_set_decl_spec_type (decl_specs,
9601 /*user_defined_p=*/true);
9606 if (is_cv_qualifier)
9607 *is_cv_qualifier = true;
9612 if (is_cv_qualifier)
9613 *is_cv_qualifier = true;
9618 if (is_cv_qualifier)
9619 *is_cv_qualifier = true;
9623 /* The `__complex__' keyword is a GNU extension. */
9631 /* Handle simple keywords. */
9636 ++decl_specs->specs[(int)ds];
9637 decl_specs->any_specifiers_p = true;
9639 return cp_lexer_consume_token (parser->lexer)->value;
9642 /* If we do not already have a type-specifier, assume we are looking
9643 at a simple-type-specifier. */
9644 type_spec = cp_parser_simple_type_specifier (parser,
9648 /* If we didn't find a type-specifier, and a type-specifier was not
9649 optional in this context, issue an error message. */
9650 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9652 cp_parser_error (parser, "expected type specifier");
9653 return error_mark_node;
9659 /* Parse a simple-type-specifier.
9661 simple-type-specifier:
9662 :: [opt] nested-name-specifier [opt] type-name
9663 :: [opt] nested-name-specifier template template-id
9678 simple-type-specifier:
9679 __typeof__ unary-expression
9680 __typeof__ ( type-id )
9682 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9683 appropriately updated. */
9686 cp_parser_simple_type_specifier (cp_parser* parser,
9687 cp_decl_specifier_seq *decl_specs,
9688 cp_parser_flags flags)
9690 tree type = NULL_TREE;
9693 /* Peek at the next token. */
9694 token = cp_lexer_peek_token (parser->lexer);
9696 /* If we're looking at a keyword, things are easy. */
9697 switch (token->keyword)
9701 decl_specs->explicit_char_p = true;
9702 type = char_type_node;
9705 type = wchar_type_node;
9708 type = boolean_type_node;
9712 ++decl_specs->specs[(int) ds_short];
9713 type = short_integer_type_node;
9717 decl_specs->explicit_int_p = true;
9718 type = integer_type_node;
9722 ++decl_specs->specs[(int) ds_long];
9723 type = long_integer_type_node;
9727 ++decl_specs->specs[(int) ds_signed];
9728 type = integer_type_node;
9732 ++decl_specs->specs[(int) ds_unsigned];
9733 type = unsigned_type_node;
9736 type = float_type_node;
9739 type = double_type_node;
9742 type = void_type_node;
9746 /* Consume the `typeof' token. */
9747 cp_lexer_consume_token (parser->lexer);
9748 /* Parse the operand to `typeof'. */
9749 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9750 /* If it is not already a TYPE, take its type. */
9752 type = finish_typeof (type);
9755 cp_parser_set_decl_spec_type (decl_specs, type,
9756 /*user_defined_p=*/true);
9764 /* If the type-specifier was for a built-in type, we're done. */
9769 /* Record the type. */
9771 && (token->keyword != RID_SIGNED
9772 && token->keyword != RID_UNSIGNED
9773 && token->keyword != RID_SHORT
9774 && token->keyword != RID_LONG))
9775 cp_parser_set_decl_spec_type (decl_specs,
9777 /*user_defined=*/false);
9779 decl_specs->any_specifiers_p = true;
9781 /* Consume the token. */
9782 id = cp_lexer_consume_token (parser->lexer)->value;
9784 /* There is no valid C++ program where a non-template type is
9785 followed by a "<". That usually indicates that the user thought
9786 that the type was a template. */
9787 cp_parser_check_for_invalid_template_id (parser, type);
9789 return TYPE_NAME (type);
9792 /* The type-specifier must be a user-defined type. */
9793 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9798 /* Don't gobble tokens or issue error messages if this is an
9799 optional type-specifier. */
9800 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9801 cp_parser_parse_tentatively (parser);
9803 /* Look for the optional `::' operator. */
9805 = (cp_parser_global_scope_opt (parser,
9806 /*current_scope_valid_p=*/false)
9808 /* Look for the nested-name specifier. */
9810 = (cp_parser_nested_name_specifier_opt (parser,
9811 /*typename_keyword_p=*/false,
9812 /*check_dependency_p=*/true,
9814 /*is_declaration=*/false)
9816 /* If we have seen a nested-name-specifier, and the next token
9817 is `template', then we are using the template-id production. */
9819 && cp_parser_optional_template_keyword (parser))
9821 /* Look for the template-id. */
9822 type = cp_parser_template_id (parser,
9823 /*template_keyword_p=*/true,
9824 /*check_dependency_p=*/true,
9825 /*is_declaration=*/false);
9826 /* If the template-id did not name a type, we are out of
9828 if (TREE_CODE (type) != TYPE_DECL)
9830 cp_parser_error (parser, "expected template-id for type");
9834 /* Otherwise, look for a type-name. */
9836 type = cp_parser_type_name (parser);
9837 /* Keep track of all name-lookups performed in class scopes. */
9841 && TREE_CODE (type) == TYPE_DECL
9842 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9843 maybe_note_name_used_in_class (DECL_NAME (type), type);
9844 /* If it didn't work out, we don't have a TYPE. */
9845 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9846 && !cp_parser_parse_definitely (parser))
9848 if (type && decl_specs)
9849 cp_parser_set_decl_spec_type (decl_specs, type,
9850 /*user_defined=*/true);
9853 /* If we didn't get a type-name, issue an error message. */
9854 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9856 cp_parser_error (parser, "expected type-name");
9857 return error_mark_node;
9860 /* There is no valid C++ program where a non-template type is
9861 followed by a "<". That usually indicates that the user thought
9862 that the type was a template. */
9863 if (type && type != error_mark_node)
9865 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9866 If it is, then the '<'...'>' enclose protocol names rather than
9867 template arguments, and so everything is fine. */
9868 if (c_dialect_objc ()
9869 && (objc_is_id (type) || objc_is_class_name (type)))
9871 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9872 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9874 /* Clobber the "unqualified" type previously entered into
9875 DECL_SPECS with the new, improved protocol-qualified version. */
9877 decl_specs->type = qual_type;
9882 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9888 /* Parse a type-name.
9901 Returns a TYPE_DECL for the type. */
9904 cp_parser_type_name (cp_parser* parser)
9909 /* We can't know yet whether it is a class-name or not. */
9910 cp_parser_parse_tentatively (parser);
9911 /* Try a class-name. */
9912 type_decl = cp_parser_class_name (parser,
9913 /*typename_keyword_p=*/false,
9914 /*template_keyword_p=*/false,
9916 /*check_dependency_p=*/true,
9917 /*class_head_p=*/false,
9918 /*is_declaration=*/false);
9919 /* If it's not a class-name, keep looking. */
9920 if (!cp_parser_parse_definitely (parser))
9922 /* It must be a typedef-name or an enum-name. */
9923 identifier = cp_parser_identifier (parser);
9924 if (identifier == error_mark_node)
9925 return error_mark_node;
9927 /* Look up the type-name. */
9928 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9930 if (TREE_CODE (type_decl) != TYPE_DECL
9931 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9933 /* See if this is an Objective-C type. */
9934 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9935 tree type = objc_get_protocol_qualified_type (identifier, protos);
9937 type_decl = TYPE_NAME (type);
9940 /* Issue an error if we did not find a type-name. */
9941 if (TREE_CODE (type_decl) != TYPE_DECL)
9943 if (!cp_parser_simulate_error (parser))
9944 cp_parser_name_lookup_error (parser, identifier, type_decl,
9946 type_decl = error_mark_node;
9948 /* Remember that the name was used in the definition of the
9949 current class so that we can check later to see if the
9950 meaning would have been different after the class was
9951 entirely defined. */
9952 else if (type_decl != error_mark_node
9954 maybe_note_name_used_in_class (identifier, type_decl);
9961 /* Parse an elaborated-type-specifier. Note that the grammar given
9962 here incorporates the resolution to DR68.
9964 elaborated-type-specifier:
9965 class-key :: [opt] nested-name-specifier [opt] identifier
9966 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9967 enum :: [opt] nested-name-specifier [opt] identifier
9968 typename :: [opt] nested-name-specifier identifier
9969 typename :: [opt] nested-name-specifier template [opt]
9974 elaborated-type-specifier:
9975 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9976 class-key attributes :: [opt] nested-name-specifier [opt]
9977 template [opt] template-id
9978 enum attributes :: [opt] nested-name-specifier [opt] identifier
9980 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9981 declared `friend'. If IS_DECLARATION is TRUE, then this
9982 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9983 something is being declared.
9985 Returns the TYPE specified. */
9988 cp_parser_elaborated_type_specifier (cp_parser* parser,
9990 bool is_declaration)
9992 enum tag_types tag_type;
9994 tree type = NULL_TREE;
9995 tree attributes = NULL_TREE;
9997 /* See if we're looking at the `enum' keyword. */
9998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10000 /* Consume the `enum' token. */
10001 cp_lexer_consume_token (parser->lexer);
10002 /* Remember that it's an enumeration type. */
10003 tag_type = enum_type;
10004 /* Parse the attributes. */
10005 attributes = cp_parser_attributes_opt (parser);
10007 /* Or, it might be `typename'. */
10008 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10011 /* Consume the `typename' token. */
10012 cp_lexer_consume_token (parser->lexer);
10013 /* Remember that it's a `typename' type. */
10014 tag_type = typename_type;
10015 /* The `typename' keyword is only allowed in templates. */
10016 if (!processing_template_decl)
10017 pedwarn ("using %<typename%> outside of template");
10019 /* Otherwise it must be a class-key. */
10022 tag_type = cp_parser_class_key (parser);
10023 if (tag_type == none_type)
10024 return error_mark_node;
10025 /* Parse the attributes. */
10026 attributes = cp_parser_attributes_opt (parser);
10029 /* Look for the `::' operator. */
10030 cp_parser_global_scope_opt (parser,
10031 /*current_scope_valid_p=*/false);
10032 /* Look for the nested-name-specifier. */
10033 if (tag_type == typename_type)
10035 if (!cp_parser_nested_name_specifier (parser,
10036 /*typename_keyword_p=*/true,
10037 /*check_dependency_p=*/true,
10040 return error_mark_node;
10043 /* Even though `typename' is not present, the proposed resolution
10044 to Core Issue 180 says that in `class A<T>::B', `B' should be
10045 considered a type-name, even if `A<T>' is dependent. */
10046 cp_parser_nested_name_specifier_opt (parser,
10047 /*typename_keyword_p=*/true,
10048 /*check_dependency_p=*/true,
10051 /* For everything but enumeration types, consider a template-id. */
10052 /* For an enumeration type, consider only a plain identifier. */
10053 if (tag_type != enum_type)
10055 bool template_p = false;
10058 /* Allow the `template' keyword. */
10059 template_p = cp_parser_optional_template_keyword (parser);
10060 /* If we didn't see `template', we don't know if there's a
10061 template-id or not. */
10063 cp_parser_parse_tentatively (parser);
10064 /* Parse the template-id. */
10065 decl = cp_parser_template_id (parser, template_p,
10066 /*check_dependency_p=*/true,
10068 /* If we didn't find a template-id, look for an ordinary
10070 if (!template_p && !cp_parser_parse_definitely (parser))
10072 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10073 in effect, then we must assume that, upon instantiation, the
10074 template will correspond to a class. */
10075 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10076 && tag_type == typename_type)
10077 type = make_typename_type (parser->scope, decl,
10079 /*complain=*/tf_error);
10081 type = TREE_TYPE (decl);
10086 identifier = cp_parser_identifier (parser);
10088 if (identifier == error_mark_node)
10090 parser->scope = NULL_TREE;
10091 return error_mark_node;
10094 /* For a `typename', we needn't call xref_tag. */
10095 if (tag_type == typename_type
10096 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10097 return cp_parser_make_typename_type (parser, parser->scope,
10099 /* Look up a qualified name in the usual way. */
10104 decl = cp_parser_lookup_name (parser, identifier,
10106 /*is_template=*/false,
10107 /*is_namespace=*/false,
10108 /*check_dependency=*/true,
10109 /*ambiguous_decls=*/NULL);
10111 /* If we are parsing friend declaration, DECL may be a
10112 TEMPLATE_DECL tree node here. However, we need to check
10113 whether this TEMPLATE_DECL results in valid code. Consider
10114 the following example:
10117 template <class T> class C {};
10120 template <class T> friend class N::C; // #1, valid code
10122 template <class T> class Y {
10123 friend class N::C; // #2, invalid code
10126 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10127 name lookup of `N::C'. We see that friend declaration must
10128 be template for the code to be valid. Note that
10129 processing_template_decl does not work here since it is
10130 always 1 for the above two cases. */
10132 decl = (cp_parser_maybe_treat_template_as_class
10133 (decl, /*tag_name_p=*/is_friend
10134 && parser->num_template_parameter_lists));
10136 if (TREE_CODE (decl) != TYPE_DECL)
10138 cp_parser_diagnose_invalid_type_name (parser,
10141 return error_mark_node;
10144 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10145 check_elaborated_type_specifier
10147 (parser->num_template_parameter_lists
10148 || DECL_SELF_REFERENCE_P (decl)));
10150 type = TREE_TYPE (decl);
10154 /* An elaborated-type-specifier sometimes introduces a new type and
10155 sometimes names an existing type. Normally, the rule is that it
10156 introduces a new type only if there is not an existing type of
10157 the same name already in scope. For example, given:
10160 void f() { struct S s; }
10162 the `struct S' in the body of `f' is the same `struct S' as in
10163 the global scope; the existing definition is used. However, if
10164 there were no global declaration, this would introduce a new
10165 local class named `S'.
10167 An exception to this rule applies to the following code:
10169 namespace N { struct S; }
10171 Here, the elaborated-type-specifier names a new type
10172 unconditionally; even if there is already an `S' in the
10173 containing scope this declaration names a new type.
10174 This exception only applies if the elaborated-type-specifier
10175 forms the complete declaration:
10179 A declaration consisting solely of `class-key identifier ;' is
10180 either a redeclaration of the name in the current scope or a
10181 forward declaration of the identifier as a class name. It
10182 introduces the name into the current scope.
10184 We are in this situation precisely when the next token is a `;'.
10186 An exception to the exception is that a `friend' declaration does
10187 *not* name a new type; i.e., given:
10189 struct S { friend struct T; };
10191 `T' is not a new type in the scope of `S'.
10193 Also, `new struct S' or `sizeof (struct S)' never results in the
10194 definition of a new type; a new type can only be declared in a
10195 declaration context. */
10201 /* Friends have special name lookup rules. */
10202 ts = ts_within_enclosing_non_class;
10203 else if (is_declaration
10204 && cp_lexer_next_token_is (parser->lexer,
10206 /* This is a `class-key identifier ;' */
10212 (parser->num_template_parameter_lists
10213 && (cp_parser_next_token_starts_class_definition_p (parser)
10214 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10215 /* An unqualified name was used to reference this type, so
10216 there were no qualifying templates. */
10217 if (!cp_parser_check_template_parameters (parser,
10218 /*num_templates=*/0))
10219 return error_mark_node;
10220 type = xref_tag (tag_type, identifier, ts, template_p);
10224 if (type == error_mark_node)
10225 return error_mark_node;
10227 /* Allow attributes on forward declarations of classes. */
10230 if (TREE_CODE (type) == TYPENAME_TYPE)
10231 warning (OPT_Wattributes,
10232 "attributes ignored on uninstantiated type");
10233 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10234 && ! processing_explicit_instantiation)
10235 warning (OPT_Wattributes,
10236 "attributes ignored on template instantiation");
10237 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10238 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10240 warning (OPT_Wattributes,
10241 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10244 if (tag_type != enum_type)
10245 cp_parser_check_class_key (tag_type, type);
10247 /* A "<" cannot follow an elaborated type specifier. If that
10248 happens, the user was probably trying to form a template-id. */
10249 cp_parser_check_for_invalid_template_id (parser, type);
10254 /* Parse an enum-specifier.
10257 enum identifier [opt] { enumerator-list [opt] }
10260 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10263 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10264 if the token stream isn't an enum-specifier after all. */
10267 cp_parser_enum_specifier (cp_parser* parser)
10273 /* Parse tentatively so that we can back up if we don't find a
10275 cp_parser_parse_tentatively (parser);
10277 /* Caller guarantees that the current token is 'enum', an identifier
10278 possibly follows, and the token after that is an opening brace.
10279 If we don't have an identifier, fabricate an anonymous name for
10280 the enumeration being defined. */
10281 cp_lexer_consume_token (parser->lexer);
10283 attributes = cp_parser_attributes_opt (parser);
10285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10286 identifier = cp_parser_identifier (parser);
10288 identifier = make_anon_name ();
10290 /* Look for the `{' but don't consume it yet. */
10291 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10292 cp_parser_simulate_error (parser);
10294 if (!cp_parser_parse_definitely (parser))
10297 /* Issue an error message if type-definitions are forbidden here. */
10298 cp_parser_check_type_definition (parser);
10300 /* Create the new type. We do this before consuming the opening brace
10301 so the enum will be recorded as being on the line of its tag (or the
10302 'enum' keyword, if there is no tag). */
10303 type = start_enum (identifier);
10305 /* Consume the opening brace. */
10306 cp_lexer_consume_token (parser->lexer);
10308 if (type == error_mark_node)
10310 cp_parser_skip_to_end_of_block_or_statement (parser);
10311 return error_mark_node;
10314 /* If the next token is not '}', then there are some enumerators. */
10315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10316 cp_parser_enumerator_list (parser, type);
10318 /* Consume the final '}'. */
10319 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10321 /* Look for trailing attributes to apply to this enumeration, and
10322 apply them if appropriate. */
10323 if (cp_parser_allow_gnu_extensions_p (parser))
10325 tree trailing_attr = cp_parser_attributes_opt (parser);
10326 cplus_decl_attributes (&type,
10328 (int) ATTR_FLAG_TYPE_IN_PLACE);
10331 /* Finish up the enumeration. */
10332 finish_enum (type);
10337 /* Parse an enumerator-list. The enumerators all have the indicated
10341 enumerator-definition
10342 enumerator-list , enumerator-definition */
10345 cp_parser_enumerator_list (cp_parser* parser, tree type)
10349 /* Parse an enumerator-definition. */
10350 cp_parser_enumerator_definition (parser, type);
10352 /* If the next token is not a ',', we've reached the end of
10354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10356 /* Otherwise, consume the `,' and keep going. */
10357 cp_lexer_consume_token (parser->lexer);
10358 /* If the next token is a `}', there is a trailing comma. */
10359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10361 if (pedantic && !in_system_header)
10362 pedwarn ("comma at end of enumerator list");
10368 /* Parse an enumerator-definition. The enumerator has the indicated
10371 enumerator-definition:
10373 enumerator = constant-expression
10379 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10384 /* Look for the identifier. */
10385 identifier = cp_parser_identifier (parser);
10386 if (identifier == error_mark_node)
10389 /* If the next token is an '=', then there is an explicit value. */
10390 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10392 /* Consume the `=' token. */
10393 cp_lexer_consume_token (parser->lexer);
10394 /* Parse the value. */
10395 value = cp_parser_constant_expression (parser,
10396 /*allow_non_constant_p=*/false,
10402 /* Create the enumerator. */
10403 build_enumerator (identifier, value, type);
10406 /* Parse a namespace-name.
10409 original-namespace-name
10412 Returns the NAMESPACE_DECL for the namespace. */
10415 cp_parser_namespace_name (cp_parser* parser)
10418 tree namespace_decl;
10420 /* Get the name of the namespace. */
10421 identifier = cp_parser_identifier (parser);
10422 if (identifier == error_mark_node)
10423 return error_mark_node;
10425 /* Look up the identifier in the currently active scope. Look only
10426 for namespaces, due to:
10428 [basic.lookup.udir]
10430 When looking up a namespace-name in a using-directive or alias
10431 definition, only namespace names are considered.
10435 [basic.lookup.qual]
10437 During the lookup of a name preceding the :: scope resolution
10438 operator, object, function, and enumerator names are ignored.
10440 (Note that cp_parser_class_or_namespace_name only calls this
10441 function if the token after the name is the scope resolution
10443 namespace_decl = cp_parser_lookup_name (parser, identifier,
10445 /*is_template=*/false,
10446 /*is_namespace=*/true,
10447 /*check_dependency=*/true,
10448 /*ambiguous_decls=*/NULL);
10449 /* If it's not a namespace, issue an error. */
10450 if (namespace_decl == error_mark_node
10451 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10453 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10454 error ("%qD is not a namespace-name", identifier);
10455 cp_parser_error (parser, "expected namespace-name");
10456 namespace_decl = error_mark_node;
10459 return namespace_decl;
10462 /* Parse a namespace-definition.
10464 namespace-definition:
10465 named-namespace-definition
10466 unnamed-namespace-definition
10468 named-namespace-definition:
10469 original-namespace-definition
10470 extension-namespace-definition
10472 original-namespace-definition:
10473 namespace identifier { namespace-body }
10475 extension-namespace-definition:
10476 namespace original-namespace-name { namespace-body }
10478 unnamed-namespace-definition:
10479 namespace { namespace-body } */
10482 cp_parser_namespace_definition (cp_parser* parser)
10484 tree identifier, attribs;
10486 /* Look for the `namespace' keyword. */
10487 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10489 /* Get the name of the namespace. We do not attempt to distinguish
10490 between an original-namespace-definition and an
10491 extension-namespace-definition at this point. The semantic
10492 analysis routines are responsible for that. */
10493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10494 identifier = cp_parser_identifier (parser);
10496 identifier = NULL_TREE;
10498 /* Parse any specified attributes. */
10499 attribs = cp_parser_attributes_opt (parser);
10501 /* Look for the `{' to start the namespace. */
10502 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10503 /* Start the namespace. */
10504 push_namespace_with_attribs (identifier, attribs);
10505 /* Parse the body of the namespace. */
10506 cp_parser_namespace_body (parser);
10507 /* Finish the namespace. */
10509 /* Look for the final `}'. */
10510 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10513 /* Parse a namespace-body.
10516 declaration-seq [opt] */
10519 cp_parser_namespace_body (cp_parser* parser)
10521 cp_parser_declaration_seq_opt (parser);
10524 /* Parse a namespace-alias-definition.
10526 namespace-alias-definition:
10527 namespace identifier = qualified-namespace-specifier ; */
10530 cp_parser_namespace_alias_definition (cp_parser* parser)
10533 tree namespace_specifier;
10535 /* Look for the `namespace' keyword. */
10536 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10537 /* Look for the identifier. */
10538 identifier = cp_parser_identifier (parser);
10539 if (identifier == error_mark_node)
10541 /* Look for the `=' token. */
10542 cp_parser_require (parser, CPP_EQ, "`='");
10543 /* Look for the qualified-namespace-specifier. */
10544 namespace_specifier
10545 = cp_parser_qualified_namespace_specifier (parser);
10546 /* Look for the `;' token. */
10547 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10549 /* Register the alias in the symbol table. */
10550 do_namespace_alias (identifier, namespace_specifier);
10553 /* Parse a qualified-namespace-specifier.
10555 qualified-namespace-specifier:
10556 :: [opt] nested-name-specifier [opt] namespace-name
10558 Returns a NAMESPACE_DECL corresponding to the specified
10562 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10564 /* Look for the optional `::'. */
10565 cp_parser_global_scope_opt (parser,
10566 /*current_scope_valid_p=*/false);
10568 /* Look for the optional nested-name-specifier. */
10569 cp_parser_nested_name_specifier_opt (parser,
10570 /*typename_keyword_p=*/false,
10571 /*check_dependency_p=*/true,
10573 /*is_declaration=*/true);
10575 return cp_parser_namespace_name (parser);
10578 /* Parse a using-declaration.
10581 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10582 using :: unqualified-id ; */
10585 cp_parser_using_declaration (cp_parser* parser)
10588 bool typename_p = false;
10589 bool global_scope_p;
10594 /* Look for the `using' keyword. */
10595 cp_parser_require_keyword (parser, RID_USING, "`using'");
10597 /* Peek at the next token. */
10598 token = cp_lexer_peek_token (parser->lexer);
10599 /* See if it's `typename'. */
10600 if (token->keyword == RID_TYPENAME)
10602 /* Remember that we've seen it. */
10604 /* Consume the `typename' token. */
10605 cp_lexer_consume_token (parser->lexer);
10608 /* Look for the optional global scope qualification. */
10610 = (cp_parser_global_scope_opt (parser,
10611 /*current_scope_valid_p=*/false)
10614 /* If we saw `typename', or didn't see `::', then there must be a
10615 nested-name-specifier present. */
10616 if (typename_p || !global_scope_p)
10617 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10618 /*check_dependency_p=*/true,
10620 /*is_declaration=*/true);
10621 /* Otherwise, we could be in either of the two productions. In that
10622 case, treat the nested-name-specifier as optional. */
10624 qscope = cp_parser_nested_name_specifier_opt (parser,
10625 /*typename_keyword_p=*/false,
10626 /*check_dependency_p=*/true,
10628 /*is_declaration=*/true);
10630 qscope = global_namespace;
10632 /* Parse the unqualified-id. */
10633 identifier = cp_parser_unqualified_id (parser,
10634 /*template_keyword_p=*/false,
10635 /*check_dependency_p=*/true,
10636 /*declarator_p=*/true,
10637 /*optional_p=*/false);
10639 /* The function we call to handle a using-declaration is different
10640 depending on what scope we are in. */
10641 if (qscope == error_mark_node || identifier == error_mark_node)
10643 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10644 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10645 /* [namespace.udecl]
10647 A using declaration shall not name a template-id. */
10648 error ("a template-id may not appear in a using-declaration");
10651 if (at_class_scope_p ())
10653 /* Create the USING_DECL. */
10654 decl = do_class_using_decl (parser->scope, identifier);
10655 /* Add it to the list of members in this class. */
10656 finish_member_declaration (decl);
10660 decl = cp_parser_lookup_name_simple (parser, identifier);
10661 if (decl == error_mark_node)
10662 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10663 else if (!at_namespace_scope_p ())
10664 do_local_using_decl (decl, qscope, identifier);
10666 do_toplevel_using_decl (decl, qscope, identifier);
10670 /* Look for the final `;'. */
10671 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10674 /* Parse a using-directive.
10677 using namespace :: [opt] nested-name-specifier [opt]
10678 namespace-name ; */
10681 cp_parser_using_directive (cp_parser* parser)
10683 tree namespace_decl;
10686 /* Look for the `using' keyword. */
10687 cp_parser_require_keyword (parser, RID_USING, "`using'");
10688 /* And the `namespace' keyword. */
10689 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10690 /* Look for the optional `::' operator. */
10691 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10692 /* And the optional nested-name-specifier. */
10693 cp_parser_nested_name_specifier_opt (parser,
10694 /*typename_keyword_p=*/false,
10695 /*check_dependency_p=*/true,
10697 /*is_declaration=*/true);
10698 /* Get the namespace being used. */
10699 namespace_decl = cp_parser_namespace_name (parser);
10700 /* And any specified attributes. */
10701 attribs = cp_parser_attributes_opt (parser);
10702 /* Update the symbol table. */
10703 parse_using_directive (namespace_decl, attribs);
10704 /* Look for the final `;'. */
10705 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10708 /* Parse an asm-definition.
10711 asm ( string-literal ) ;
10716 asm volatile [opt] ( string-literal ) ;
10717 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10718 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10719 : asm-operand-list [opt] ) ;
10720 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10721 : asm-operand-list [opt]
10722 : asm-operand-list [opt] ) ; */
10725 cp_parser_asm_definition (cp_parser* parser)
10728 tree outputs = NULL_TREE;
10729 tree inputs = NULL_TREE;
10730 tree clobbers = NULL_TREE;
10732 bool volatile_p = false;
10733 bool extended_p = false;
10735 /* Look for the `asm' keyword. */
10736 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10737 /* See if the next token is `volatile'. */
10738 if (cp_parser_allow_gnu_extensions_p (parser)
10739 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10741 /* Remember that we saw the `volatile' keyword. */
10743 /* Consume the token. */
10744 cp_lexer_consume_token (parser->lexer);
10746 /* Look for the opening `('. */
10747 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10749 /* Look for the string. */
10750 string = cp_parser_string_literal (parser, false, false);
10751 if (string == error_mark_node)
10753 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10754 /*consume_paren=*/true);
10758 /* If we're allowing GNU extensions, check for the extended assembly
10759 syntax. Unfortunately, the `:' tokens need not be separated by
10760 a space in C, and so, for compatibility, we tolerate that here
10761 too. Doing that means that we have to treat the `::' operator as
10763 if (cp_parser_allow_gnu_extensions_p (parser)
10764 && at_function_scope_p ()
10765 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10766 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10768 bool inputs_p = false;
10769 bool clobbers_p = false;
10771 /* The extended syntax was used. */
10774 /* Look for outputs. */
10775 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10777 /* Consume the `:'. */
10778 cp_lexer_consume_token (parser->lexer);
10779 /* Parse the output-operands. */
10780 if (cp_lexer_next_token_is_not (parser->lexer,
10782 && cp_lexer_next_token_is_not (parser->lexer,
10784 && cp_lexer_next_token_is_not (parser->lexer,
10786 outputs = cp_parser_asm_operand_list (parser);
10788 /* If the next token is `::', there are no outputs, and the
10789 next token is the beginning of the inputs. */
10790 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10791 /* The inputs are coming next. */
10794 /* Look for inputs. */
10796 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10798 /* Consume the `:' or `::'. */
10799 cp_lexer_consume_token (parser->lexer);
10800 /* Parse the output-operands. */
10801 if (cp_lexer_next_token_is_not (parser->lexer,
10803 && cp_lexer_next_token_is_not (parser->lexer,
10805 inputs = cp_parser_asm_operand_list (parser);
10807 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10808 /* The clobbers are coming next. */
10811 /* Look for clobbers. */
10813 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10815 /* Consume the `:' or `::'. */
10816 cp_lexer_consume_token (parser->lexer);
10817 /* Parse the clobbers. */
10818 if (cp_lexer_next_token_is_not (parser->lexer,
10820 clobbers = cp_parser_asm_clobber_list (parser);
10823 /* Look for the closing `)'. */
10824 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10825 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10826 /*consume_paren=*/true);
10827 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10829 /* Create the ASM_EXPR. */
10830 if (at_function_scope_p ())
10832 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10834 /* If the extended syntax was not used, mark the ASM_EXPR. */
10837 tree temp = asm_stmt;
10838 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10839 temp = TREE_OPERAND (temp, 0);
10841 ASM_INPUT_P (temp) = 1;
10845 cgraph_add_asm_node (string);
10848 /* Declarators [gram.dcl.decl] */
10850 /* Parse an init-declarator.
10853 declarator initializer [opt]
10858 declarator asm-specification [opt] attributes [opt] initializer [opt]
10860 function-definition:
10861 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10863 decl-specifier-seq [opt] declarator function-try-block
10867 function-definition:
10868 __extension__ function-definition
10870 The DECL_SPECIFIERS apply to this declarator. Returns a
10871 representation of the entity declared. If MEMBER_P is TRUE, then
10872 this declarator appears in a class scope. The new DECL created by
10873 this declarator is returned.
10875 The CHECKS are access checks that should be performed once we know
10876 what entity is being declared (and, therefore, what classes have
10879 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10880 for a function-definition here as well. If the declarator is a
10881 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10882 be TRUE upon return. By that point, the function-definition will
10883 have been completely parsed.
10885 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10889 cp_parser_init_declarator (cp_parser* parser,
10890 cp_decl_specifier_seq *decl_specifiers,
10892 bool function_definition_allowed_p,
10894 int declares_class_or_enum,
10895 bool* function_definition_p)
10898 cp_declarator *declarator;
10899 tree prefix_attributes;
10901 tree asm_specification;
10903 tree decl = NULL_TREE;
10905 bool is_initialized;
10906 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10907 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10909 enum cpp_ttype initialization_kind;
10910 bool is_parenthesized_init = false;
10911 bool is_non_constant_init;
10912 int ctor_dtor_or_conv_p;
10914 tree pushed_scope = NULL;
10916 /* Gather the attributes that were provided with the
10917 decl-specifiers. */
10918 prefix_attributes = decl_specifiers->attributes;
10920 /* Assume that this is not the declarator for a function
10922 if (function_definition_p)
10923 *function_definition_p = false;
10925 /* Defer access checks while parsing the declarator; we cannot know
10926 what names are accessible until we know what is being
10928 resume_deferring_access_checks ();
10930 /* Parse the declarator. */
10932 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10933 &ctor_dtor_or_conv_p,
10934 /*parenthesized_p=*/NULL,
10935 /*member_p=*/false);
10936 /* Gather up the deferred checks. */
10937 stop_deferring_access_checks ();
10939 /* If the DECLARATOR was erroneous, there's no need to go
10941 if (declarator == cp_error_declarator)
10942 return error_mark_node;
10944 if (declares_class_or_enum & 2)
10945 cp_parser_check_for_definition_in_return_type (declarator,
10946 decl_specifiers->type);
10948 /* Figure out what scope the entity declared by the DECLARATOR is
10949 located in. `grokdeclarator' sometimes changes the scope, so
10950 we compute it now. */
10951 scope = get_scope_of_declarator (declarator);
10953 /* If we're allowing GNU extensions, look for an asm-specification
10955 if (cp_parser_allow_gnu_extensions_p (parser))
10957 /* Look for an asm-specification. */
10958 asm_specification = cp_parser_asm_specification_opt (parser);
10959 /* And attributes. */
10960 attributes = cp_parser_attributes_opt (parser);
10964 asm_specification = NULL_TREE;
10965 attributes = NULL_TREE;
10968 /* Peek at the next token. */
10969 token = cp_lexer_peek_token (parser->lexer);
10970 /* Check to see if the token indicates the start of a
10971 function-definition. */
10972 if (cp_parser_token_starts_function_definition_p (token))
10974 if (!function_definition_allowed_p)
10976 /* If a function-definition should not appear here, issue an
10978 cp_parser_error (parser,
10979 "a function-definition is not allowed here");
10980 return error_mark_node;
10984 /* Neither attributes nor an asm-specification are allowed
10985 on a function-definition. */
10986 if (asm_specification)
10987 error ("an asm-specification is not allowed on a function-definition");
10989 error ("attributes are not allowed on a function-definition");
10990 /* This is a function-definition. */
10991 *function_definition_p = true;
10993 /* Parse the function definition. */
10995 decl = cp_parser_save_member_function_body (parser,
10998 prefix_attributes);
11001 = (cp_parser_function_definition_from_specifiers_and_declarator
11002 (parser, decl_specifiers, prefix_attributes, declarator));
11010 Only in function declarations for constructors, destructors, and
11011 type conversions can the decl-specifier-seq be omitted.
11013 We explicitly postpone this check past the point where we handle
11014 function-definitions because we tolerate function-definitions
11015 that are missing their return types in some modes. */
11016 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11018 cp_parser_error (parser,
11019 "expected constructor, destructor, or type conversion");
11020 return error_mark_node;
11023 /* An `=' or an `(' indicates an initializer. */
11024 if (token->type == CPP_EQ
11025 || token->type == CPP_OPEN_PAREN)
11027 is_initialized = true;
11028 initialization_kind = token->type;
11032 /* If the init-declarator isn't initialized and isn't followed by a
11033 `,' or `;', it's not a valid init-declarator. */
11034 if (token->type != CPP_COMMA
11035 && token->type != CPP_SEMICOLON)
11037 cp_parser_error (parser, "expected initializer");
11038 return error_mark_node;
11040 is_initialized = false;
11041 initialization_kind = CPP_EOF;
11044 /* Because start_decl has side-effects, we should only call it if we
11045 know we're going ahead. By this point, we know that we cannot
11046 possibly be looking at any other construct. */
11047 cp_parser_commit_to_tentative_parse (parser);
11049 /* If the decl specifiers were bad, issue an error now that we're
11050 sure this was intended to be a declarator. Then continue
11051 declaring the variable(s), as int, to try to cut down on further
11053 if (decl_specifiers->any_specifiers_p
11054 && decl_specifiers->type == error_mark_node)
11056 cp_parser_error (parser, "invalid type in declaration");
11057 decl_specifiers->type = integer_type_node;
11060 /* Check to see whether or not this declaration is a friend. */
11061 friend_p = cp_parser_friend_p (decl_specifiers);
11063 /* Check that the number of template-parameter-lists is OK. */
11064 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11065 return error_mark_node;
11067 /* Enter the newly declared entry in the symbol table. If we're
11068 processing a declaration in a class-specifier, we wait until
11069 after processing the initializer. */
11072 if (parser->in_unbraced_linkage_specification_p)
11073 decl_specifiers->storage_class = sc_extern;
11074 decl = start_decl (declarator, decl_specifiers,
11075 is_initialized, attributes, prefix_attributes,
11079 /* Enter the SCOPE. That way unqualified names appearing in the
11080 initializer will be looked up in SCOPE. */
11081 pushed_scope = push_scope (scope);
11083 /* Perform deferred access control checks, now that we know in which
11084 SCOPE the declared entity resides. */
11085 if (!member_p && decl)
11087 tree saved_current_function_decl = NULL_TREE;
11089 /* If the entity being declared is a function, pretend that we
11090 are in its scope. If it is a `friend', it may have access to
11091 things that would not otherwise be accessible. */
11092 if (TREE_CODE (decl) == FUNCTION_DECL)
11094 saved_current_function_decl = current_function_decl;
11095 current_function_decl = decl;
11098 /* Perform access checks for template parameters. */
11099 cp_parser_perform_template_parameter_access_checks (checks);
11101 /* Perform the access control checks for the declarator and the
11102 the decl-specifiers. */
11103 perform_deferred_access_checks ();
11105 /* Restore the saved value. */
11106 if (TREE_CODE (decl) == FUNCTION_DECL)
11107 current_function_decl = saved_current_function_decl;
11110 /* Parse the initializer. */
11111 initializer = NULL_TREE;
11112 is_parenthesized_init = false;
11113 is_non_constant_init = true;
11114 if (is_initialized)
11116 if (declarator->kind == cdk_function
11117 && declarator->declarator->kind == cdk_id
11118 && initialization_kind == CPP_EQ)
11119 initializer = cp_parser_pure_specifier (parser);
11121 initializer = cp_parser_initializer (parser,
11122 &is_parenthesized_init,
11123 &is_non_constant_init);
11126 /* The old parser allows attributes to appear after a parenthesized
11127 initializer. Mark Mitchell proposed removing this functionality
11128 on the GCC mailing lists on 2002-08-13. This parser accepts the
11129 attributes -- but ignores them. */
11130 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11131 if (cp_parser_attributes_opt (parser))
11132 warning (OPT_Wattributes,
11133 "attributes after parenthesized initializer ignored");
11135 /* For an in-class declaration, use `grokfield' to create the
11141 pop_scope (pushed_scope);
11142 pushed_scope = false;
11144 decl = grokfield (declarator, decl_specifiers,
11145 initializer, !is_non_constant_init,
11146 /*asmspec=*/NULL_TREE,
11147 prefix_attributes);
11148 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11149 cp_parser_save_default_args (parser, decl);
11152 /* Finish processing the declaration. But, skip friend
11154 if (!friend_p && decl && decl != error_mark_node)
11156 cp_finish_decl (decl,
11157 initializer, !is_non_constant_init,
11159 /* If the initializer is in parentheses, then this is
11160 a direct-initialization, which means that an
11161 `explicit' constructor is OK. Otherwise, an
11162 `explicit' constructor cannot be used. */
11163 ((is_parenthesized_init || !is_initialized)
11164 ? 0 : LOOKUP_ONLYCONVERTING));
11166 if (!friend_p && pushed_scope)
11167 pop_scope (pushed_scope);
11172 /* Parse a declarator.
11176 ptr-operator declarator
11178 abstract-declarator:
11179 ptr-operator abstract-declarator [opt]
11180 direct-abstract-declarator
11185 attributes [opt] direct-declarator
11186 attributes [opt] ptr-operator declarator
11188 abstract-declarator:
11189 attributes [opt] ptr-operator abstract-declarator [opt]
11190 attributes [opt] direct-abstract-declarator
11192 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11193 detect constructor, destructor or conversion operators. It is set
11194 to -1 if the declarator is a name, and +1 if it is a
11195 function. Otherwise it is set to zero. Usually you just want to
11196 test for >0, but internally the negative value is used.
11198 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11199 a decl-specifier-seq unless it declares a constructor, destructor,
11200 or conversion. It might seem that we could check this condition in
11201 semantic analysis, rather than parsing, but that makes it difficult
11202 to handle something like `f()'. We want to notice that there are
11203 no decl-specifiers, and therefore realize that this is an
11204 expression, not a declaration.)
11206 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11207 the declarator is a direct-declarator of the form "(...)".
11209 MEMBER_P is true iff this declarator is a member-declarator. */
11211 static cp_declarator *
11212 cp_parser_declarator (cp_parser* parser,
11213 cp_parser_declarator_kind dcl_kind,
11214 int* ctor_dtor_or_conv_p,
11215 bool* parenthesized_p,
11219 cp_declarator *declarator;
11220 enum tree_code code;
11221 cp_cv_quals cv_quals;
11223 tree attributes = NULL_TREE;
11225 /* Assume this is not a constructor, destructor, or type-conversion
11227 if (ctor_dtor_or_conv_p)
11228 *ctor_dtor_or_conv_p = 0;
11230 if (cp_parser_allow_gnu_extensions_p (parser))
11231 attributes = cp_parser_attributes_opt (parser);
11233 /* Peek at the next token. */
11234 token = cp_lexer_peek_token (parser->lexer);
11236 /* Check for the ptr-operator production. */
11237 cp_parser_parse_tentatively (parser);
11238 /* Parse the ptr-operator. */
11239 code = cp_parser_ptr_operator (parser,
11242 /* If that worked, then we have a ptr-operator. */
11243 if (cp_parser_parse_definitely (parser))
11245 /* If a ptr-operator was found, then this declarator was not
11247 if (parenthesized_p)
11248 *parenthesized_p = true;
11249 /* The dependent declarator is optional if we are parsing an
11250 abstract-declarator. */
11251 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11252 cp_parser_parse_tentatively (parser);
11254 /* Parse the dependent declarator. */
11255 declarator = cp_parser_declarator (parser, dcl_kind,
11256 /*ctor_dtor_or_conv_p=*/NULL,
11257 /*parenthesized_p=*/NULL,
11258 /*member_p=*/false);
11260 /* If we are parsing an abstract-declarator, we must handle the
11261 case where the dependent declarator is absent. */
11262 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11263 && !cp_parser_parse_definitely (parser))
11266 /* Build the representation of the ptr-operator. */
11268 declarator = make_ptrmem_declarator (cv_quals,
11271 else if (code == INDIRECT_REF)
11272 declarator = make_pointer_declarator (cv_quals, declarator);
11274 declarator = make_reference_declarator (cv_quals, declarator);
11276 /* Everything else is a direct-declarator. */
11279 if (parenthesized_p)
11280 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11282 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11283 ctor_dtor_or_conv_p,
11287 if (attributes && declarator && declarator != cp_error_declarator)
11288 declarator->attributes = attributes;
11293 /* Parse a direct-declarator or direct-abstract-declarator.
11297 direct-declarator ( parameter-declaration-clause )
11298 cv-qualifier-seq [opt]
11299 exception-specification [opt]
11300 direct-declarator [ constant-expression [opt] ]
11303 direct-abstract-declarator:
11304 direct-abstract-declarator [opt]
11305 ( parameter-declaration-clause )
11306 cv-qualifier-seq [opt]
11307 exception-specification [opt]
11308 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11309 ( abstract-declarator )
11311 Returns a representation of the declarator. DCL_KIND is
11312 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11313 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11314 we are parsing a direct-declarator. It is
11315 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11316 of ambiguity we prefer an abstract declarator, as per
11317 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11318 cp_parser_declarator. */
11320 static cp_declarator *
11321 cp_parser_direct_declarator (cp_parser* parser,
11322 cp_parser_declarator_kind dcl_kind,
11323 int* ctor_dtor_or_conv_p,
11327 cp_declarator *declarator = NULL;
11328 tree scope = NULL_TREE;
11329 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11330 bool saved_in_declarator_p = parser->in_declarator_p;
11332 tree pushed_scope = NULL_TREE;
11336 /* Peek at the next token. */
11337 token = cp_lexer_peek_token (parser->lexer);
11338 if (token->type == CPP_OPEN_PAREN)
11340 /* This is either a parameter-declaration-clause, or a
11341 parenthesized declarator. When we know we are parsing a
11342 named declarator, it must be a parenthesized declarator
11343 if FIRST is true. For instance, `(int)' is a
11344 parameter-declaration-clause, with an omitted
11345 direct-abstract-declarator. But `((*))', is a
11346 parenthesized abstract declarator. Finally, when T is a
11347 template parameter `(T)' is a
11348 parameter-declaration-clause, and not a parenthesized
11351 We first try and parse a parameter-declaration-clause,
11352 and then try a nested declarator (if FIRST is true).
11354 It is not an error for it not to be a
11355 parameter-declaration-clause, even when FIRST is
11361 The first is the declaration of a function while the
11362 second is a the definition of a variable, including its
11365 Having seen only the parenthesis, we cannot know which of
11366 these two alternatives should be selected. Even more
11367 complex are examples like:
11372 The former is a function-declaration; the latter is a
11373 variable initialization.
11375 Thus again, we try a parameter-declaration-clause, and if
11376 that fails, we back out and return. */
11378 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11380 cp_parameter_declarator *params;
11381 unsigned saved_num_template_parameter_lists;
11383 /* In a member-declarator, the only valid interpretation
11384 of a parenthesis is the start of a
11385 parameter-declaration-clause. (It is invalid to
11386 initialize a static data member with a parenthesized
11387 initializer; only the "=" form of initialization is
11390 cp_parser_parse_tentatively (parser);
11392 /* Consume the `('. */
11393 cp_lexer_consume_token (parser->lexer);
11396 /* If this is going to be an abstract declarator, we're
11397 in a declarator and we can't have default args. */
11398 parser->default_arg_ok_p = false;
11399 parser->in_declarator_p = true;
11402 /* Inside the function parameter list, surrounding
11403 template-parameter-lists do not apply. */
11404 saved_num_template_parameter_lists
11405 = parser->num_template_parameter_lists;
11406 parser->num_template_parameter_lists = 0;
11408 /* Parse the parameter-declaration-clause. */
11409 params = cp_parser_parameter_declaration_clause (parser);
11411 parser->num_template_parameter_lists
11412 = saved_num_template_parameter_lists;
11414 /* If all went well, parse the cv-qualifier-seq and the
11415 exception-specification. */
11416 if (member_p || cp_parser_parse_definitely (parser))
11418 cp_cv_quals cv_quals;
11419 tree exception_specification;
11421 if (ctor_dtor_or_conv_p)
11422 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11424 /* Consume the `)'. */
11425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11427 /* Parse the cv-qualifier-seq. */
11428 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11429 /* And the exception-specification. */
11430 exception_specification
11431 = cp_parser_exception_specification_opt (parser);
11433 /* Create the function-declarator. */
11434 declarator = make_call_declarator (declarator,
11437 exception_specification);
11438 /* Any subsequent parameter lists are to do with
11439 return type, so are not those of the declared
11441 parser->default_arg_ok_p = false;
11443 /* Repeat the main loop. */
11448 /* If this is the first, we can try a parenthesized
11452 bool saved_in_type_id_in_expr_p;
11454 parser->default_arg_ok_p = saved_default_arg_ok_p;
11455 parser->in_declarator_p = saved_in_declarator_p;
11457 /* Consume the `('. */
11458 cp_lexer_consume_token (parser->lexer);
11459 /* Parse the nested declarator. */
11460 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11461 parser->in_type_id_in_expr_p = true;
11463 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11464 /*parenthesized_p=*/NULL,
11466 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11468 /* Expect a `)'. */
11469 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11470 declarator = cp_error_declarator;
11471 if (declarator == cp_error_declarator)
11474 goto handle_declarator;
11476 /* Otherwise, we must be done. */
11480 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11481 && token->type == CPP_OPEN_SQUARE)
11483 /* Parse an array-declarator. */
11486 if (ctor_dtor_or_conv_p)
11487 *ctor_dtor_or_conv_p = 0;
11490 parser->default_arg_ok_p = false;
11491 parser->in_declarator_p = true;
11492 /* Consume the `['. */
11493 cp_lexer_consume_token (parser->lexer);
11494 /* Peek at the next token. */
11495 token = cp_lexer_peek_token (parser->lexer);
11496 /* If the next token is `]', then there is no
11497 constant-expression. */
11498 if (token->type != CPP_CLOSE_SQUARE)
11500 bool non_constant_p;
11503 = cp_parser_constant_expression (parser,
11504 /*allow_non_constant=*/true,
11506 if (!non_constant_p)
11507 bounds = fold_non_dependent_expr (bounds);
11508 /* Normally, the array bound must be an integral constant
11509 expression. However, as an extension, we allow VLAs
11510 in function scopes. */
11511 else if (!at_function_scope_p ())
11513 error ("array bound is not an integer constant");
11514 bounds = error_mark_node;
11518 bounds = NULL_TREE;
11519 /* Look for the closing `]'. */
11520 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11522 declarator = cp_error_declarator;
11526 declarator = make_array_declarator (declarator, bounds);
11528 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11530 tree qualifying_scope;
11531 tree unqualified_name;
11532 special_function_kind sfk;
11535 /* Parse a declarator-id */
11536 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11538 cp_parser_parse_tentatively (parser);
11540 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11541 qualifying_scope = parser->scope;
11544 if (!cp_parser_parse_definitely (parser))
11545 unqualified_name = error_mark_node;
11546 else if (unqualified_name
11547 && (qualifying_scope
11548 || (TREE_CODE (unqualified_name)
11549 != IDENTIFIER_NODE)))
11551 cp_parser_error (parser, "expected unqualified-id");
11552 unqualified_name = error_mark_node;
11556 if (!unqualified_name)
11558 if (unqualified_name == error_mark_node)
11560 declarator = cp_error_declarator;
11564 if (qualifying_scope && at_namespace_scope_p ()
11565 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11567 /* In the declaration of a member of a template class
11568 outside of the class itself, the SCOPE will sometimes
11569 be a TYPENAME_TYPE. For example, given:
11571 template <typename T>
11572 int S<T>::R::i = 3;
11574 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11575 this context, we must resolve S<T>::R to an ordinary
11576 type, rather than a typename type.
11578 The reason we normally avoid resolving TYPENAME_TYPEs
11579 is that a specialization of `S' might render
11580 `S<T>::R' not a type. However, if `S' is
11581 specialized, then this `i' will not be used, so there
11582 is no harm in resolving the types here. */
11585 /* Resolve the TYPENAME_TYPE. */
11586 type = resolve_typename_type (qualifying_scope,
11587 /*only_current_p=*/false);
11588 /* If that failed, the declarator is invalid. */
11589 if (type == error_mark_node)
11590 error ("%<%T::%D%> is not a type",
11591 TYPE_CONTEXT (qualifying_scope),
11592 TYPE_IDENTIFIER (qualifying_scope));
11593 qualifying_scope = type;
11597 if (unqualified_name)
11601 if (qualifying_scope
11602 && CLASS_TYPE_P (qualifying_scope))
11603 class_type = qualifying_scope;
11605 class_type = current_class_type;
11607 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11609 tree name_type = TREE_TYPE (unqualified_name);
11610 if (class_type && same_type_p (name_type, class_type))
11612 if (qualifying_scope
11613 && CLASSTYPE_USE_TEMPLATE (name_type))
11615 error ("invalid use of constructor as a template");
11616 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11617 "name the constructor in a qualified name",
11619 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11620 class_type, name_type);
11621 declarator = cp_error_declarator;
11625 unqualified_name = constructor_name (class_type);
11629 /* We do not attempt to print the declarator
11630 here because we do not have enough
11631 information about its original syntactic
11633 cp_parser_error (parser, "invalid declarator");
11634 declarator = cp_error_declarator;
11641 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11642 sfk = sfk_destructor;
11643 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11644 sfk = sfk_conversion;
11645 else if (/* There's no way to declare a constructor
11646 for an anonymous type, even if the type
11647 got a name for linkage purposes. */
11648 !TYPE_WAS_ANONYMOUS (class_type)
11649 && constructor_name_p (unqualified_name,
11652 unqualified_name = constructor_name (class_type);
11653 sfk = sfk_constructor;
11656 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11657 *ctor_dtor_or_conv_p = -1;
11660 declarator = make_id_declarator (qualifying_scope,
11663 declarator->id_loc = token->location;
11665 handle_declarator:;
11666 scope = get_scope_of_declarator (declarator);
11668 /* Any names that appear after the declarator-id for a
11669 member are looked up in the containing scope. */
11670 pushed_scope = push_scope (scope);
11671 parser->in_declarator_p = true;
11672 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11673 || (declarator && declarator->kind == cdk_id))
11674 /* Default args are only allowed on function
11676 parser->default_arg_ok_p = saved_default_arg_ok_p;
11678 parser->default_arg_ok_p = false;
11687 /* For an abstract declarator, we might wind up with nothing at this
11688 point. That's an error; the declarator is not optional. */
11690 cp_parser_error (parser, "expected declarator");
11692 /* If we entered a scope, we must exit it now. */
11694 pop_scope (pushed_scope);
11696 parser->default_arg_ok_p = saved_default_arg_ok_p;
11697 parser->in_declarator_p = saved_in_declarator_p;
11702 /* Parse a ptr-operator.
11705 * cv-qualifier-seq [opt]
11707 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11712 & cv-qualifier-seq [opt]
11714 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11715 Returns ADDR_EXPR if a reference was used. In the case of a
11716 pointer-to-member, *TYPE is filled in with the TYPE containing the
11717 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11718 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11719 ERROR_MARK if an error occurred. */
11721 static enum tree_code
11722 cp_parser_ptr_operator (cp_parser* parser,
11724 cp_cv_quals *cv_quals)
11726 enum tree_code code = ERROR_MARK;
11729 /* Assume that it's not a pointer-to-member. */
11731 /* And that there are no cv-qualifiers. */
11732 *cv_quals = TYPE_UNQUALIFIED;
11734 /* Peek at the next token. */
11735 token = cp_lexer_peek_token (parser->lexer);
11736 /* If it's a `*' or `&' we have a pointer or reference. */
11737 if (token->type == CPP_MULT || token->type == CPP_AND)
11739 /* Remember which ptr-operator we were processing. */
11740 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11742 /* Consume the `*' or `&'. */
11743 cp_lexer_consume_token (parser->lexer);
11745 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11746 `&', if we are allowing GNU extensions. (The only qualifier
11747 that can legally appear after `&' is `restrict', but that is
11748 enforced during semantic analysis. */
11749 if (code == INDIRECT_REF
11750 || cp_parser_allow_gnu_extensions_p (parser))
11751 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11755 /* Try the pointer-to-member case. */
11756 cp_parser_parse_tentatively (parser);
11757 /* Look for the optional `::' operator. */
11758 cp_parser_global_scope_opt (parser,
11759 /*current_scope_valid_p=*/false);
11760 /* Look for the nested-name specifier. */
11761 cp_parser_nested_name_specifier (parser,
11762 /*typename_keyword_p=*/false,
11763 /*check_dependency_p=*/true,
11765 /*is_declaration=*/false);
11766 /* If we found it, and the next token is a `*', then we are
11767 indeed looking at a pointer-to-member operator. */
11768 if (!cp_parser_error_occurred (parser)
11769 && cp_parser_require (parser, CPP_MULT, "`*'"))
11771 /* Indicate that the `*' operator was used. */
11772 code = INDIRECT_REF;
11774 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11775 error ("%qD is a namespace", parser->scope);
11778 /* The type of which the member is a member is given by the
11780 *type = parser->scope;
11781 /* The next name will not be qualified. */
11782 parser->scope = NULL_TREE;
11783 parser->qualifying_scope = NULL_TREE;
11784 parser->object_scope = NULL_TREE;
11785 /* Look for the optional cv-qualifier-seq. */
11786 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11789 /* If that didn't work we don't have a ptr-operator. */
11790 if (!cp_parser_parse_definitely (parser))
11791 cp_parser_error (parser, "expected ptr-operator");
11797 /* Parse an (optional) cv-qualifier-seq.
11800 cv-qualifier cv-qualifier-seq [opt]
11811 Returns a bitmask representing the cv-qualifiers. */
11814 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11816 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11821 cp_cv_quals cv_qualifier;
11823 /* Peek at the next token. */
11824 token = cp_lexer_peek_token (parser->lexer);
11825 /* See if it's a cv-qualifier. */
11826 switch (token->keyword)
11829 cv_qualifier = TYPE_QUAL_CONST;
11833 cv_qualifier = TYPE_QUAL_VOLATILE;
11837 cv_qualifier = TYPE_QUAL_RESTRICT;
11841 cv_qualifier = TYPE_UNQUALIFIED;
11848 if (cv_quals & cv_qualifier)
11850 error ("duplicate cv-qualifier");
11851 cp_lexer_purge_token (parser->lexer);
11855 cp_lexer_consume_token (parser->lexer);
11856 cv_quals |= cv_qualifier;
11863 /* Parse a declarator-id.
11867 :: [opt] nested-name-specifier [opt] type-name
11869 In the `id-expression' case, the value returned is as for
11870 cp_parser_id_expression if the id-expression was an unqualified-id.
11871 If the id-expression was a qualified-id, then a SCOPE_REF is
11872 returned. The first operand is the scope (either a NAMESPACE_DECL
11873 or TREE_TYPE), but the second is still just a representation of an
11877 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11880 /* The expression must be an id-expression. Assume that qualified
11881 names are the names of types so that:
11884 int S<T>::R::i = 3;
11886 will work; we must treat `S<T>::R' as the name of a type.
11887 Similarly, assume that qualified names are templates, where
11891 int S<T>::R<T>::i = 3;
11894 id = cp_parser_id_expression (parser,
11895 /*template_keyword_p=*/false,
11896 /*check_dependency_p=*/false,
11897 /*template_p=*/NULL,
11898 /*declarator_p=*/true,
11900 if (id && BASELINK_P (id))
11901 id = BASELINK_FUNCTIONS (id);
11905 /* Parse a type-id.
11908 type-specifier-seq abstract-declarator [opt]
11910 Returns the TYPE specified. */
11913 cp_parser_type_id (cp_parser* parser)
11915 cp_decl_specifier_seq type_specifier_seq;
11916 cp_declarator *abstract_declarator;
11918 /* Parse the type-specifier-seq. */
11919 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11920 &type_specifier_seq);
11921 if (type_specifier_seq.type == error_mark_node)
11922 return error_mark_node;
11924 /* There might or might not be an abstract declarator. */
11925 cp_parser_parse_tentatively (parser);
11926 /* Look for the declarator. */
11927 abstract_declarator
11928 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11929 /*parenthesized_p=*/NULL,
11930 /*member_p=*/false);
11931 /* Check to see if there really was a declarator. */
11932 if (!cp_parser_parse_definitely (parser))
11933 abstract_declarator = NULL;
11935 return groktypename (&type_specifier_seq, abstract_declarator);
11938 /* Parse a type-specifier-seq.
11940 type-specifier-seq:
11941 type-specifier type-specifier-seq [opt]
11945 type-specifier-seq:
11946 attributes type-specifier-seq [opt]
11948 If IS_CONDITION is true, we are at the start of a "condition",
11949 e.g., we've just seen "if (".
11951 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11954 cp_parser_type_specifier_seq (cp_parser* parser,
11956 cp_decl_specifier_seq *type_specifier_seq)
11958 bool seen_type_specifier = false;
11959 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11961 /* Clear the TYPE_SPECIFIER_SEQ. */
11962 clear_decl_specs (type_specifier_seq);
11964 /* Parse the type-specifiers and attributes. */
11967 tree type_specifier;
11968 bool is_cv_qualifier;
11970 /* Check for attributes first. */
11971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11973 type_specifier_seq->attributes =
11974 chainon (type_specifier_seq->attributes,
11975 cp_parser_attributes_opt (parser));
11979 /* Look for the type-specifier. */
11980 type_specifier = cp_parser_type_specifier (parser,
11982 type_specifier_seq,
11983 /*is_declaration=*/false,
11986 if (!type_specifier)
11988 /* If the first type-specifier could not be found, this is not a
11989 type-specifier-seq at all. */
11990 if (!seen_type_specifier)
11992 cp_parser_error (parser, "expected type-specifier");
11993 type_specifier_seq->type = error_mark_node;
11996 /* If subsequent type-specifiers could not be found, the
11997 type-specifier-seq is complete. */
12001 seen_type_specifier = true;
12002 /* The standard says that a condition can be:
12004 type-specifier-seq declarator = assignment-expression
12011 we should treat the "S" as a declarator, not as a
12012 type-specifier. The standard doesn't say that explicitly for
12013 type-specifier-seq, but it does say that for
12014 decl-specifier-seq in an ordinary declaration. Perhaps it
12015 would be clearer just to allow a decl-specifier-seq here, and
12016 then add a semantic restriction that if any decl-specifiers
12017 that are not type-specifiers appear, the program is invalid. */
12018 if (is_condition && !is_cv_qualifier)
12019 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12022 cp_parser_check_decl_spec (type_specifier_seq);
12025 /* Parse a parameter-declaration-clause.
12027 parameter-declaration-clause:
12028 parameter-declaration-list [opt] ... [opt]
12029 parameter-declaration-list , ...
12031 Returns a representation for the parameter declarations. A return
12032 value of NULL indicates a parameter-declaration-clause consisting
12033 only of an ellipsis. */
12035 static cp_parameter_declarator *
12036 cp_parser_parameter_declaration_clause (cp_parser* parser)
12038 cp_parameter_declarator *parameters;
12043 /* Peek at the next token. */
12044 token = cp_lexer_peek_token (parser->lexer);
12045 /* Check for trivial parameter-declaration-clauses. */
12046 if (token->type == CPP_ELLIPSIS)
12048 /* Consume the `...' token. */
12049 cp_lexer_consume_token (parser->lexer);
12052 else if (token->type == CPP_CLOSE_PAREN)
12053 /* There are no parameters. */
12055 #ifndef NO_IMPLICIT_EXTERN_C
12056 if (in_system_header && current_class_type == NULL
12057 && current_lang_name == lang_name_c)
12061 return no_parameters;
12063 /* Check for `(void)', too, which is a special case. */
12064 else if (token->keyword == RID_VOID
12065 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12066 == CPP_CLOSE_PAREN))
12068 /* Consume the `void' token. */
12069 cp_lexer_consume_token (parser->lexer);
12070 /* There are no parameters. */
12071 return no_parameters;
12074 /* Parse the parameter-declaration-list. */
12075 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12076 /* If a parse error occurred while parsing the
12077 parameter-declaration-list, then the entire
12078 parameter-declaration-clause is erroneous. */
12082 /* Peek at the next token. */
12083 token = cp_lexer_peek_token (parser->lexer);
12084 /* If it's a `,', the clause should terminate with an ellipsis. */
12085 if (token->type == CPP_COMMA)
12087 /* Consume the `,'. */
12088 cp_lexer_consume_token (parser->lexer);
12089 /* Expect an ellipsis. */
12091 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12093 /* It might also be `...' if the optional trailing `,' was
12095 else if (token->type == CPP_ELLIPSIS)
12097 /* Consume the `...' token. */
12098 cp_lexer_consume_token (parser->lexer);
12099 /* And remember that we saw it. */
12103 ellipsis_p = false;
12105 /* Finish the parameter list. */
12106 if (parameters && ellipsis_p)
12107 parameters->ellipsis_p = true;
12112 /* Parse a parameter-declaration-list.
12114 parameter-declaration-list:
12115 parameter-declaration
12116 parameter-declaration-list , parameter-declaration
12118 Returns a representation of the parameter-declaration-list, as for
12119 cp_parser_parameter_declaration_clause. However, the
12120 `void_list_node' is never appended to the list. Upon return,
12121 *IS_ERROR will be true iff an error occurred. */
12123 static cp_parameter_declarator *
12124 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12126 cp_parameter_declarator *parameters = NULL;
12127 cp_parameter_declarator **tail = ¶meters;
12128 bool saved_in_unbraced_linkage_specification_p;
12130 /* Assume all will go well. */
12132 /* The special considerations that apply to a function within an
12133 unbraced linkage specifications do not apply to the parameters
12134 to the function. */
12135 saved_in_unbraced_linkage_specification_p
12136 = parser->in_unbraced_linkage_specification_p;
12137 parser->in_unbraced_linkage_specification_p = false;
12139 /* Look for more parameters. */
12142 cp_parameter_declarator *parameter;
12143 bool parenthesized_p;
12144 /* Parse the parameter. */
12146 = cp_parser_parameter_declaration (parser,
12147 /*template_parm_p=*/false,
12150 /* If a parse error occurred parsing the parameter declaration,
12151 then the entire parameter-declaration-list is erroneous. */
12158 /* Add the new parameter to the list. */
12160 tail = ¶meter->next;
12162 /* Peek at the next token. */
12163 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12164 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12165 /* These are for Objective-C++ */
12166 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12167 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12168 /* The parameter-declaration-list is complete. */
12170 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12174 /* Peek at the next token. */
12175 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12176 /* If it's an ellipsis, then the list is complete. */
12177 if (token->type == CPP_ELLIPSIS)
12179 /* Otherwise, there must be more parameters. Consume the
12181 cp_lexer_consume_token (parser->lexer);
12182 /* When parsing something like:
12184 int i(float f, double d)
12186 we can tell after seeing the declaration for "f" that we
12187 are not looking at an initialization of a variable "i",
12188 but rather at the declaration of a function "i".
12190 Due to the fact that the parsing of template arguments
12191 (as specified to a template-id) requires backtracking we
12192 cannot use this technique when inside a template argument
12194 if (!parser->in_template_argument_list_p
12195 && !parser->in_type_id_in_expr_p
12196 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12197 /* However, a parameter-declaration of the form
12198 "foat(f)" (which is a valid declaration of a
12199 parameter "f") can also be interpreted as an
12200 expression (the conversion of "f" to "float"). */
12201 && !parenthesized_p)
12202 cp_parser_commit_to_tentative_parse (parser);
12206 cp_parser_error (parser, "expected %<,%> or %<...%>");
12207 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12208 cp_parser_skip_to_closing_parenthesis (parser,
12209 /*recovering=*/true,
12210 /*or_comma=*/false,
12211 /*consume_paren=*/false);
12216 parser->in_unbraced_linkage_specification_p
12217 = saved_in_unbraced_linkage_specification_p;
12222 /* Parse a parameter declaration.
12224 parameter-declaration:
12225 decl-specifier-seq declarator
12226 decl-specifier-seq declarator = assignment-expression
12227 decl-specifier-seq abstract-declarator [opt]
12228 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12230 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12231 declares a template parameter. (In that case, a non-nested `>'
12232 token encountered during the parsing of the assignment-expression
12233 is not interpreted as a greater-than operator.)
12235 Returns a representation of the parameter, or NULL if an error
12236 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12237 true iff the declarator is of the form "(p)". */
12239 static cp_parameter_declarator *
12240 cp_parser_parameter_declaration (cp_parser *parser,
12241 bool template_parm_p,
12242 bool *parenthesized_p)
12244 int declares_class_or_enum;
12245 bool greater_than_is_operator_p;
12246 cp_decl_specifier_seq decl_specifiers;
12247 cp_declarator *declarator;
12248 tree default_argument;
12250 const char *saved_message;
12252 /* In a template parameter, `>' is not an operator.
12256 When parsing a default template-argument for a non-type
12257 template-parameter, the first non-nested `>' is taken as the end
12258 of the template parameter-list rather than a greater-than
12260 greater_than_is_operator_p = !template_parm_p;
12262 /* Type definitions may not appear in parameter types. */
12263 saved_message = parser->type_definition_forbidden_message;
12264 parser->type_definition_forbidden_message
12265 = "types may not be defined in parameter types";
12267 /* Parse the declaration-specifiers. */
12268 cp_parser_decl_specifier_seq (parser,
12269 CP_PARSER_FLAGS_NONE,
12271 &declares_class_or_enum);
12272 /* If an error occurred, there's no reason to attempt to parse the
12273 rest of the declaration. */
12274 if (cp_parser_error_occurred (parser))
12276 parser->type_definition_forbidden_message = saved_message;
12280 /* Peek at the next token. */
12281 token = cp_lexer_peek_token (parser->lexer);
12282 /* If the next token is a `)', `,', `=', `>', or `...', then there
12283 is no declarator. */
12284 if (token->type == CPP_CLOSE_PAREN
12285 || token->type == CPP_COMMA
12286 || token->type == CPP_EQ
12287 || token->type == CPP_ELLIPSIS
12288 || token->type == CPP_GREATER)
12291 if (parenthesized_p)
12292 *parenthesized_p = false;
12294 /* Otherwise, there should be a declarator. */
12297 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12298 parser->default_arg_ok_p = false;
12300 /* After seeing a decl-specifier-seq, if the next token is not a
12301 "(", there is no possibility that the code is a valid
12302 expression. Therefore, if parsing tentatively, we commit at
12304 if (!parser->in_template_argument_list_p
12305 /* In an expression context, having seen:
12309 we cannot be sure whether we are looking at a
12310 function-type (taking a "char" as a parameter) or a cast
12311 of some object of type "char" to "int". */
12312 && !parser->in_type_id_in_expr_p
12313 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12314 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12315 cp_parser_commit_to_tentative_parse (parser);
12316 /* Parse the declarator. */
12317 declarator = cp_parser_declarator (parser,
12318 CP_PARSER_DECLARATOR_EITHER,
12319 /*ctor_dtor_or_conv_p=*/NULL,
12321 /*member_p=*/false);
12322 parser->default_arg_ok_p = saved_default_arg_ok_p;
12323 /* After the declarator, allow more attributes. */
12324 decl_specifiers.attributes
12325 = chainon (decl_specifiers.attributes,
12326 cp_parser_attributes_opt (parser));
12329 /* The restriction on defining new types applies only to the type
12330 of the parameter, not to the default argument. */
12331 parser->type_definition_forbidden_message = saved_message;
12333 /* If the next token is `=', then process a default argument. */
12334 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12336 bool saved_greater_than_is_operator_p;
12337 /* Consume the `='. */
12338 cp_lexer_consume_token (parser->lexer);
12340 /* If we are defining a class, then the tokens that make up the
12341 default argument must be saved and processed later. */
12342 if (!template_parm_p && at_class_scope_p ()
12343 && TYPE_BEING_DEFINED (current_class_type))
12345 unsigned depth = 0;
12346 cp_token *first_token;
12349 /* Add tokens until we have processed the entire default
12350 argument. We add the range [first_token, token). */
12351 first_token = cp_lexer_peek_token (parser->lexer);
12356 /* Peek at the next token. */
12357 token = cp_lexer_peek_token (parser->lexer);
12358 /* What we do depends on what token we have. */
12359 switch (token->type)
12361 /* In valid code, a default argument must be
12362 immediately followed by a `,' `)', or `...'. */
12364 case CPP_CLOSE_PAREN:
12366 /* If we run into a non-nested `;', `}', or `]',
12367 then the code is invalid -- but the default
12368 argument is certainly over. */
12369 case CPP_SEMICOLON:
12370 case CPP_CLOSE_BRACE:
12371 case CPP_CLOSE_SQUARE:
12374 /* Update DEPTH, if necessary. */
12375 else if (token->type == CPP_CLOSE_PAREN
12376 || token->type == CPP_CLOSE_BRACE
12377 || token->type == CPP_CLOSE_SQUARE)
12381 case CPP_OPEN_PAREN:
12382 case CPP_OPEN_SQUARE:
12383 case CPP_OPEN_BRACE:
12388 /* If we see a non-nested `>', and `>' is not an
12389 operator, then it marks the end of the default
12391 if (!depth && !greater_than_is_operator_p)
12395 /* If we run out of tokens, issue an error message. */
12397 case CPP_PRAGMA_EOL:
12398 error ("file ends in default argument");
12404 /* In these cases, we should look for template-ids.
12405 For example, if the default argument is
12406 `X<int, double>()', we need to do name lookup to
12407 figure out whether or not `X' is a template; if
12408 so, the `,' does not end the default argument.
12410 That is not yet done. */
12417 /* If we've reached the end, stop. */
12421 /* Add the token to the token block. */
12422 token = cp_lexer_consume_token (parser->lexer);
12425 /* Create a DEFAULT_ARG to represented the unparsed default
12427 default_argument = make_node (DEFAULT_ARG);
12428 DEFARG_TOKENS (default_argument)
12429 = cp_token_cache_new (first_token, token);
12430 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12432 /* Outside of a class definition, we can just parse the
12433 assignment-expression. */
12436 bool saved_local_variables_forbidden_p;
12438 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12440 saved_greater_than_is_operator_p
12441 = parser->greater_than_is_operator_p;
12442 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12443 /* Local variable names (and the `this' keyword) may not
12444 appear in a default argument. */
12445 saved_local_variables_forbidden_p
12446 = parser->local_variables_forbidden_p;
12447 parser->local_variables_forbidden_p = true;
12448 /* The default argument expression may cause implicitly
12449 defined member functions to be synthesized, which will
12450 result in garbage collection. We must treat this
12451 situation as if we were within the body of function so as
12452 to avoid collecting live data on the stack. */
12454 /* Parse the assignment-expression. */
12455 if (template_parm_p)
12456 push_deferring_access_checks (dk_no_deferred);
12458 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12459 if (template_parm_p)
12460 pop_deferring_access_checks ();
12461 /* Restore saved state. */
12463 parser->greater_than_is_operator_p
12464 = saved_greater_than_is_operator_p;
12465 parser->local_variables_forbidden_p
12466 = saved_local_variables_forbidden_p;
12468 if (!parser->default_arg_ok_p)
12470 if (!flag_pedantic_errors)
12471 warning (0, "deprecated use of default argument for parameter of non-function");
12474 error ("default arguments are only permitted for function parameters");
12475 default_argument = NULL_TREE;
12480 default_argument = NULL_TREE;
12482 return make_parameter_declarator (&decl_specifiers,
12487 /* Parse a function-body.
12490 compound_statement */
12493 cp_parser_function_body (cp_parser *parser)
12495 cp_parser_compound_statement (parser, NULL, false);
12498 /* Parse a ctor-initializer-opt followed by a function-body. Return
12499 true if a ctor-initializer was present. */
12502 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12505 bool ctor_initializer_p;
12507 /* Begin the function body. */
12508 body = begin_function_body ();
12509 /* Parse the optional ctor-initializer. */
12510 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12511 /* Parse the function-body. */
12512 cp_parser_function_body (parser);
12513 /* Finish the function body. */
12514 finish_function_body (body);
12516 return ctor_initializer_p;
12519 /* Parse an initializer.
12522 = initializer-clause
12523 ( expression-list )
12525 Returns an expression representing the initializer. If no
12526 initializer is present, NULL_TREE is returned.
12528 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12529 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12530 set to FALSE if there is no initializer present. If there is an
12531 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12532 is set to true; otherwise it is set to false. */
12535 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12536 bool* non_constant_p)
12541 /* Peek at the next token. */
12542 token = cp_lexer_peek_token (parser->lexer);
12544 /* Let our caller know whether or not this initializer was
12546 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12547 /* Assume that the initializer is constant. */
12548 *non_constant_p = false;
12550 if (token->type == CPP_EQ)
12552 /* Consume the `='. */
12553 cp_lexer_consume_token (parser->lexer);
12554 /* Parse the initializer-clause. */
12555 init = cp_parser_initializer_clause (parser, non_constant_p);
12557 else if (token->type == CPP_OPEN_PAREN)
12558 init = cp_parser_parenthesized_expression_list (parser, false,
12563 /* Anything else is an error. */
12564 cp_parser_error (parser, "expected initializer");
12565 init = error_mark_node;
12571 /* Parse an initializer-clause.
12573 initializer-clause:
12574 assignment-expression
12575 { initializer-list , [opt] }
12578 Returns an expression representing the initializer.
12580 If the `assignment-expression' production is used the value
12581 returned is simply a representation for the expression.
12583 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12584 the elements of the initializer-list (or NULL, if the last
12585 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12586 NULL_TREE. There is no way to detect whether or not the optional
12587 trailing `,' was provided. NON_CONSTANT_P is as for
12588 cp_parser_initializer. */
12591 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12595 /* Assume the expression is constant. */
12596 *non_constant_p = false;
12598 /* If it is not a `{', then we are looking at an
12599 assignment-expression. */
12600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12603 = cp_parser_constant_expression (parser,
12604 /*allow_non_constant_p=*/true,
12606 if (!*non_constant_p)
12607 initializer = fold_non_dependent_expr (initializer);
12611 /* Consume the `{' token. */
12612 cp_lexer_consume_token (parser->lexer);
12613 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12614 initializer = make_node (CONSTRUCTOR);
12615 /* If it's not a `}', then there is a non-trivial initializer. */
12616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12618 /* Parse the initializer list. */
12619 CONSTRUCTOR_ELTS (initializer)
12620 = cp_parser_initializer_list (parser, non_constant_p);
12621 /* A trailing `,' token is allowed. */
12622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12623 cp_lexer_consume_token (parser->lexer);
12625 /* Now, there should be a trailing `}'. */
12626 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12629 return initializer;
12632 /* Parse an initializer-list.
12636 initializer-list , initializer-clause
12641 identifier : initializer-clause
12642 initializer-list, identifier : initializer-clause
12644 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12645 for the initializer. If the INDEX of the elt is non-NULL, it is the
12646 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12647 as for cp_parser_initializer. */
12649 static VEC(constructor_elt,gc) *
12650 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12652 VEC(constructor_elt,gc) *v = NULL;
12654 /* Assume all of the expressions are constant. */
12655 *non_constant_p = false;
12657 /* Parse the rest of the list. */
12663 bool clause_non_constant_p;
12665 /* If the next token is an identifier and the following one is a
12666 colon, we are looking at the GNU designated-initializer
12668 if (cp_parser_allow_gnu_extensions_p (parser)
12669 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12670 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12672 /* Consume the identifier. */
12673 identifier = cp_lexer_consume_token (parser->lexer)->value;
12674 /* Consume the `:'. */
12675 cp_lexer_consume_token (parser->lexer);
12678 identifier = NULL_TREE;
12680 /* Parse the initializer. */
12681 initializer = cp_parser_initializer_clause (parser,
12682 &clause_non_constant_p);
12683 /* If any clause is non-constant, so is the entire initializer. */
12684 if (clause_non_constant_p)
12685 *non_constant_p = true;
12687 /* Add it to the vector. */
12688 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12690 /* If the next token is not a comma, we have reached the end of
12692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12695 /* Peek at the next token. */
12696 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12697 /* If the next token is a `}', then we're still done. An
12698 initializer-clause can have a trailing `,' after the
12699 initializer-list and before the closing `}'. */
12700 if (token->type == CPP_CLOSE_BRACE)
12703 /* Consume the `,' token. */
12704 cp_lexer_consume_token (parser->lexer);
12710 /* Classes [gram.class] */
12712 /* Parse a class-name.
12718 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12719 to indicate that names looked up in dependent types should be
12720 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12721 keyword has been used to indicate that the name that appears next
12722 is a template. TAG_TYPE indicates the explicit tag given before
12723 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12724 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12725 is the class being defined in a class-head.
12727 Returns the TYPE_DECL representing the class. */
12730 cp_parser_class_name (cp_parser *parser,
12731 bool typename_keyword_p,
12732 bool template_keyword_p,
12733 enum tag_types tag_type,
12734 bool check_dependency_p,
12736 bool is_declaration)
12743 /* All class-names start with an identifier. */
12744 token = cp_lexer_peek_token (parser->lexer);
12745 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12747 cp_parser_error (parser, "expected class-name");
12748 return error_mark_node;
12751 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12752 to a template-id, so we save it here. */
12753 scope = parser->scope;
12754 if (scope == error_mark_node)
12755 return error_mark_node;
12757 /* Any name names a type if we're following the `typename' keyword
12758 in a qualified name where the enclosing scope is type-dependent. */
12759 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12760 && dependent_type_p (scope));
12761 /* Handle the common case (an identifier, but not a template-id)
12763 if (token->type == CPP_NAME
12764 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12766 cp_token *identifier_token;
12770 /* Look for the identifier. */
12771 identifier_token = cp_lexer_peek_token (parser->lexer);
12772 ambiguous_p = identifier_token->ambiguous_p;
12773 identifier = cp_parser_identifier (parser);
12774 /* If the next token isn't an identifier, we are certainly not
12775 looking at a class-name. */
12776 if (identifier == error_mark_node)
12777 decl = error_mark_node;
12778 /* If we know this is a type-name, there's no need to look it
12780 else if (typename_p)
12784 tree ambiguous_decls;
12785 /* If we already know that this lookup is ambiguous, then
12786 we've already issued an error message; there's no reason
12790 cp_parser_simulate_error (parser);
12791 return error_mark_node;
12793 /* If the next token is a `::', then the name must be a type
12796 [basic.lookup.qual]
12798 During the lookup for a name preceding the :: scope
12799 resolution operator, object, function, and enumerator
12800 names are ignored. */
12801 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12802 tag_type = typename_type;
12803 /* Look up the name. */
12804 decl = cp_parser_lookup_name (parser, identifier,
12806 /*is_template=*/false,
12807 /*is_namespace=*/false,
12808 check_dependency_p,
12810 if (ambiguous_decls)
12812 error ("reference to %qD is ambiguous", identifier);
12813 print_candidates (ambiguous_decls);
12814 if (cp_parser_parsing_tentatively (parser))
12816 identifier_token->ambiguous_p = true;
12817 cp_parser_simulate_error (parser);
12819 return error_mark_node;
12825 /* Try a template-id. */
12826 decl = cp_parser_template_id (parser, template_keyword_p,
12827 check_dependency_p,
12829 if (decl == error_mark_node)
12830 return error_mark_node;
12833 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12835 /* If this is a typename, create a TYPENAME_TYPE. */
12836 if (typename_p && decl != error_mark_node)
12838 decl = make_typename_type (scope, decl, typename_type,
12839 /*complain=*/tf_error);
12840 if (decl != error_mark_node)
12841 decl = TYPE_NAME (decl);
12844 /* Check to see that it is really the name of a class. */
12845 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12846 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12847 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12848 /* Situations like this:
12850 template <typename T> struct A {
12851 typename T::template X<int>::I i;
12854 are problematic. Is `T::template X<int>' a class-name? The
12855 standard does not seem to be definitive, but there is no other
12856 valid interpretation of the following `::'. Therefore, those
12857 names are considered class-names. */
12859 decl = make_typename_type (scope, decl, tag_type, tf_error);
12860 if (decl != error_mark_node)
12861 decl = TYPE_NAME (decl);
12863 else if (TREE_CODE (decl) != TYPE_DECL
12864 || TREE_TYPE (decl) == error_mark_node
12865 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12866 decl = error_mark_node;
12868 if (decl == error_mark_node)
12869 cp_parser_error (parser, "expected class-name");
12874 /* Parse a class-specifier.
12877 class-head { member-specification [opt] }
12879 Returns the TREE_TYPE representing the class. */
12882 cp_parser_class_specifier (cp_parser* parser)
12886 tree attributes = NULL_TREE;
12887 int has_trailing_semicolon;
12888 bool nested_name_specifier_p;
12889 unsigned saved_num_template_parameter_lists;
12890 tree old_scope = NULL_TREE;
12891 tree scope = NULL_TREE;
12893 push_deferring_access_checks (dk_no_deferred);
12895 /* Parse the class-head. */
12896 type = cp_parser_class_head (parser,
12897 &nested_name_specifier_p,
12899 /* If the class-head was a semantic disaster, skip the entire body
12903 cp_parser_skip_to_end_of_block_or_statement (parser);
12904 pop_deferring_access_checks ();
12905 return error_mark_node;
12908 /* Look for the `{'. */
12909 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12911 pop_deferring_access_checks ();
12912 return error_mark_node;
12915 /* Issue an error message if type-definitions are forbidden here. */
12916 cp_parser_check_type_definition (parser);
12917 /* Remember that we are defining one more class. */
12918 ++parser->num_classes_being_defined;
12919 /* Inside the class, surrounding template-parameter-lists do not
12921 saved_num_template_parameter_lists
12922 = parser->num_template_parameter_lists;
12923 parser->num_template_parameter_lists = 0;
12925 /* Start the class. */
12926 if (nested_name_specifier_p)
12928 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12929 old_scope = push_inner_scope (scope);
12931 type = begin_class_definition (type, attributes);
12933 if (type == error_mark_node)
12934 /* If the type is erroneous, skip the entire body of the class. */
12935 cp_parser_skip_to_closing_brace (parser);
12937 /* Parse the member-specification. */
12938 cp_parser_member_specification_opt (parser);
12940 /* Look for the trailing `}'. */
12941 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12942 /* We get better error messages by noticing a common problem: a
12943 missing trailing `;'. */
12944 token = cp_lexer_peek_token (parser->lexer);
12945 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12946 /* Look for trailing attributes to apply to this class. */
12947 if (cp_parser_allow_gnu_extensions_p (parser))
12948 attributes = cp_parser_attributes_opt (parser);
12949 if (type != error_mark_node)
12950 type = finish_struct (type, attributes);
12951 if (nested_name_specifier_p)
12952 pop_inner_scope (old_scope, scope);
12953 /* If this class is not itself within the scope of another class,
12954 then we need to parse the bodies of all of the queued function
12955 definitions. Note that the queued functions defined in a class
12956 are not always processed immediately following the
12957 class-specifier for that class. Consider:
12960 struct B { void f() { sizeof (A); } };
12963 If `f' were processed before the processing of `A' were
12964 completed, there would be no way to compute the size of `A'.
12965 Note that the nesting we are interested in here is lexical --
12966 not the semantic nesting given by TYPE_CONTEXT. In particular,
12969 struct A { struct B; };
12970 struct A::B { void f() { } };
12972 there is no need to delay the parsing of `A::B::f'. */
12973 if (--parser->num_classes_being_defined == 0)
12977 tree class_type = NULL_TREE;
12978 tree pushed_scope = NULL_TREE;
12980 /* In a first pass, parse default arguments to the functions.
12981 Then, in a second pass, parse the bodies of the functions.
12982 This two-phased approach handles cases like:
12990 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12991 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12992 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12993 TREE_PURPOSE (parser->unparsed_functions_queues)
12994 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12996 fn = TREE_VALUE (queue_entry);
12997 /* If there are default arguments that have not yet been processed,
12998 take care of them now. */
12999 if (class_type != TREE_PURPOSE (queue_entry))
13002 pop_scope (pushed_scope);
13003 class_type = TREE_PURPOSE (queue_entry);
13004 pushed_scope = push_scope (class_type);
13006 /* Make sure that any template parameters are in scope. */
13007 maybe_begin_member_template_processing (fn);
13008 /* Parse the default argument expressions. */
13009 cp_parser_late_parsing_default_args (parser, fn);
13010 /* Remove any template parameters from the symbol table. */
13011 maybe_end_member_template_processing ();
13014 pop_scope (pushed_scope);
13015 /* Now parse the body of the functions. */
13016 for (TREE_VALUE (parser->unparsed_functions_queues)
13017 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13018 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13019 TREE_VALUE (parser->unparsed_functions_queues)
13020 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13022 /* Figure out which function we need to process. */
13023 fn = TREE_VALUE (queue_entry);
13024 /* Parse the function. */
13025 cp_parser_late_parsing_for_member (parser, fn);
13029 /* Put back any saved access checks. */
13030 pop_deferring_access_checks ();
13032 /* Restore the count of active template-parameter-lists. */
13033 parser->num_template_parameter_lists
13034 = saved_num_template_parameter_lists;
13039 /* Parse a class-head.
13042 class-key identifier [opt] base-clause [opt]
13043 class-key nested-name-specifier identifier base-clause [opt]
13044 class-key nested-name-specifier [opt] template-id
13048 class-key attributes identifier [opt] base-clause [opt]
13049 class-key attributes nested-name-specifier identifier base-clause [opt]
13050 class-key attributes nested-name-specifier [opt] template-id
13053 Returns the TYPE of the indicated class. Sets
13054 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13055 involving a nested-name-specifier was used, and FALSE otherwise.
13057 Returns error_mark_node if this is not a class-head.
13059 Returns NULL_TREE if the class-head is syntactically valid, but
13060 semantically invalid in a way that means we should skip the entire
13061 body of the class. */
13064 cp_parser_class_head (cp_parser* parser,
13065 bool* nested_name_specifier_p,
13066 tree *attributes_p)
13068 tree nested_name_specifier;
13069 enum tag_types class_key;
13070 tree id = NULL_TREE;
13071 tree type = NULL_TREE;
13073 bool template_id_p = false;
13074 bool qualified_p = false;
13075 bool invalid_nested_name_p = false;
13076 bool invalid_explicit_specialization_p = false;
13077 tree pushed_scope = NULL_TREE;
13078 unsigned num_templates;
13081 /* Assume no nested-name-specifier will be present. */
13082 *nested_name_specifier_p = false;
13083 /* Assume no template parameter lists will be used in defining the
13087 /* Look for the class-key. */
13088 class_key = cp_parser_class_key (parser);
13089 if (class_key == none_type)
13090 return error_mark_node;
13092 /* Parse the attributes. */
13093 attributes = cp_parser_attributes_opt (parser);
13095 /* If the next token is `::', that is invalid -- but sometimes
13096 people do try to write:
13100 Handle this gracefully by accepting the extra qualifier, and then
13101 issuing an error about it later if this really is a
13102 class-head. If it turns out just to be an elaborated type
13103 specifier, remain silent. */
13104 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13105 qualified_p = true;
13107 push_deferring_access_checks (dk_no_check);
13109 /* Determine the name of the class. Begin by looking for an
13110 optional nested-name-specifier. */
13111 nested_name_specifier
13112 = cp_parser_nested_name_specifier_opt (parser,
13113 /*typename_keyword_p=*/false,
13114 /*check_dependency_p=*/false,
13116 /*is_declaration=*/false);
13117 /* If there was a nested-name-specifier, then there *must* be an
13119 if (nested_name_specifier)
13121 /* Although the grammar says `identifier', it really means
13122 `class-name' or `template-name'. You are only allowed to
13123 define a class that has already been declared with this
13126 The proposed resolution for Core Issue 180 says that wherever
13127 you see `class T::X' you should treat `X' as a type-name.
13129 It is OK to define an inaccessible class; for example:
13131 class A { class B; };
13134 We do not know if we will see a class-name, or a
13135 template-name. We look for a class-name first, in case the
13136 class-name is a template-id; if we looked for the
13137 template-name first we would stop after the template-name. */
13138 cp_parser_parse_tentatively (parser);
13139 type = cp_parser_class_name (parser,
13140 /*typename_keyword_p=*/false,
13141 /*template_keyword_p=*/false,
13143 /*check_dependency_p=*/false,
13144 /*class_head_p=*/true,
13145 /*is_declaration=*/false);
13146 /* If that didn't work, ignore the nested-name-specifier. */
13147 if (!cp_parser_parse_definitely (parser))
13149 invalid_nested_name_p = true;
13150 id = cp_parser_identifier (parser);
13151 if (id == error_mark_node)
13154 /* If we could not find a corresponding TYPE, treat this
13155 declaration like an unqualified declaration. */
13156 if (type == error_mark_node)
13157 nested_name_specifier = NULL_TREE;
13158 /* Otherwise, count the number of templates used in TYPE and its
13159 containing scopes. */
13164 for (scope = TREE_TYPE (type);
13165 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13166 scope = (TYPE_P (scope)
13167 ? TYPE_CONTEXT (scope)
13168 : DECL_CONTEXT (scope)))
13170 && CLASS_TYPE_P (scope)
13171 && CLASSTYPE_TEMPLATE_INFO (scope)
13172 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13173 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13177 /* Otherwise, the identifier is optional. */
13180 /* We don't know whether what comes next is a template-id,
13181 an identifier, or nothing at all. */
13182 cp_parser_parse_tentatively (parser);
13183 /* Check for a template-id. */
13184 id = cp_parser_template_id (parser,
13185 /*template_keyword_p=*/false,
13186 /*check_dependency_p=*/true,
13187 /*is_declaration=*/true);
13188 /* If that didn't work, it could still be an identifier. */
13189 if (!cp_parser_parse_definitely (parser))
13191 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13192 id = cp_parser_identifier (parser);
13198 template_id_p = true;
13203 pop_deferring_access_checks ();
13206 cp_parser_check_for_invalid_template_id (parser, id);
13208 /* If it's not a `:' or a `{' then we can't really be looking at a
13209 class-head, since a class-head only appears as part of a
13210 class-specifier. We have to detect this situation before calling
13211 xref_tag, since that has irreversible side-effects. */
13212 if (!cp_parser_next_token_starts_class_definition_p (parser))
13214 cp_parser_error (parser, "expected %<{%> or %<:%>");
13215 return error_mark_node;
13218 /* At this point, we're going ahead with the class-specifier, even
13219 if some other problem occurs. */
13220 cp_parser_commit_to_tentative_parse (parser);
13221 /* Issue the error about the overly-qualified name now. */
13223 cp_parser_error (parser,
13224 "global qualification of class name is invalid");
13225 else if (invalid_nested_name_p)
13226 cp_parser_error (parser,
13227 "qualified name does not name a class");
13228 else if (nested_name_specifier)
13232 /* Reject typedef-names in class heads. */
13233 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13235 error ("invalid class name in declaration of %qD", type);
13240 /* Figure out in what scope the declaration is being placed. */
13241 scope = current_scope ();
13242 /* If that scope does not contain the scope in which the
13243 class was originally declared, the program is invalid. */
13244 if (scope && !is_ancestor (scope, nested_name_specifier))
13246 error ("declaration of %qD in %qD which does not enclose %qD",
13247 type, scope, nested_name_specifier);
13253 A declarator-id shall not be qualified exception of the
13254 definition of a ... nested class outside of its class
13255 ... [or] a the definition or explicit instantiation of a
13256 class member of a namespace outside of its namespace. */
13257 if (scope == nested_name_specifier)
13259 pedwarn ("extra qualification ignored");
13260 nested_name_specifier = NULL_TREE;
13264 /* An explicit-specialization must be preceded by "template <>". If
13265 it is not, try to recover gracefully. */
13266 if (at_namespace_scope_p ()
13267 && parser->num_template_parameter_lists == 0
13270 error ("an explicit specialization must be preceded by %<template <>%>");
13271 invalid_explicit_specialization_p = true;
13272 /* Take the same action that would have been taken by
13273 cp_parser_explicit_specialization. */
13274 ++parser->num_template_parameter_lists;
13275 begin_specialization ();
13277 /* There must be no "return" statements between this point and the
13278 end of this function; set "type "to the correct return value and
13279 use "goto done;" to return. */
13280 /* Make sure that the right number of template parameters were
13282 if (!cp_parser_check_template_parameters (parser, num_templates))
13284 /* If something went wrong, there is no point in even trying to
13285 process the class-definition. */
13290 /* Look up the type. */
13293 type = TREE_TYPE (id);
13294 maybe_process_partial_specialization (type);
13295 if (nested_name_specifier)
13296 pushed_scope = push_scope (nested_name_specifier);
13298 else if (nested_name_specifier)
13304 template <typename T> struct S { struct T };
13305 template <typename T> struct S<T>::T { };
13307 we will get a TYPENAME_TYPE when processing the definition of
13308 `S::T'. We need to resolve it to the actual type before we
13309 try to define it. */
13310 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13312 class_type = resolve_typename_type (TREE_TYPE (type),
13313 /*only_current_p=*/false);
13314 if (class_type != error_mark_node)
13315 type = TYPE_NAME (class_type);
13318 cp_parser_error (parser, "could not resolve typename type");
13319 type = error_mark_node;
13323 maybe_process_partial_specialization (TREE_TYPE (type));
13324 class_type = current_class_type;
13325 /* Enter the scope indicated by the nested-name-specifier. */
13326 pushed_scope = push_scope (nested_name_specifier);
13327 /* Get the canonical version of this type. */
13328 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13329 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13330 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13332 type = push_template_decl (type);
13333 if (type == error_mark_node)
13340 type = TREE_TYPE (type);
13341 *nested_name_specifier_p = true;
13343 else /* The name is not a nested name. */
13345 /* If the class was unnamed, create a dummy name. */
13347 id = make_anon_name ();
13348 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13349 parser->num_template_parameter_lists);
13352 /* Indicate whether this class was declared as a `class' or as a
13354 if (TREE_CODE (type) == RECORD_TYPE)
13355 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13356 cp_parser_check_class_key (class_key, type);
13358 /* If this type was already complete, and we see another definition,
13359 that's an error. */
13360 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13362 error ("redefinition of %q#T", type);
13363 error ("previous definition of %q+#T", type);
13368 /* We will have entered the scope containing the class; the names of
13369 base classes should be looked up in that context. For example:
13371 struct A { struct B {}; struct C; };
13372 struct A::C : B {};
13377 /* Get the list of base-classes, if there is one. */
13378 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13379 bases = cp_parser_base_clause (parser);
13381 /* Process the base classes. */
13382 xref_basetypes (type, bases);
13385 /* Leave the scope given by the nested-name-specifier. We will
13386 enter the class scope itself while processing the members. */
13388 pop_scope (pushed_scope);
13390 if (invalid_explicit_specialization_p)
13392 end_specialization ();
13393 --parser->num_template_parameter_lists;
13395 *attributes_p = attributes;
13399 /* Parse a class-key.
13406 Returns the kind of class-key specified, or none_type to indicate
13409 static enum tag_types
13410 cp_parser_class_key (cp_parser* parser)
13413 enum tag_types tag_type;
13415 /* Look for the class-key. */
13416 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13420 /* Check to see if the TOKEN is a class-key. */
13421 tag_type = cp_parser_token_is_class_key (token);
13423 cp_parser_error (parser, "expected class-key");
13427 /* Parse an (optional) member-specification.
13429 member-specification:
13430 member-declaration member-specification [opt]
13431 access-specifier : member-specification [opt] */
13434 cp_parser_member_specification_opt (cp_parser* parser)
13441 /* Peek at the next token. */
13442 token = cp_lexer_peek_token (parser->lexer);
13443 /* If it's a `}', or EOF then we've seen all the members. */
13444 if (token->type == CPP_CLOSE_BRACE
13445 || token->type == CPP_EOF
13446 || token->type == CPP_PRAGMA_EOL)
13449 /* See if this token is a keyword. */
13450 keyword = token->keyword;
13454 case RID_PROTECTED:
13456 /* Consume the access-specifier. */
13457 cp_lexer_consume_token (parser->lexer);
13458 /* Remember which access-specifier is active. */
13459 current_access_specifier = token->value;
13460 /* Look for the `:'. */
13461 cp_parser_require (parser, CPP_COLON, "`:'");
13465 /* Accept #pragmas at class scope. */
13466 if (token->type == CPP_PRAGMA)
13468 cp_parser_pragma (parser, pragma_external);
13472 /* Otherwise, the next construction must be a
13473 member-declaration. */
13474 cp_parser_member_declaration (parser);
13479 /* Parse a member-declaration.
13481 member-declaration:
13482 decl-specifier-seq [opt] member-declarator-list [opt] ;
13483 function-definition ; [opt]
13484 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13486 template-declaration
13488 member-declarator-list:
13490 member-declarator-list , member-declarator
13493 declarator pure-specifier [opt]
13494 declarator constant-initializer [opt]
13495 identifier [opt] : constant-expression
13499 member-declaration:
13500 __extension__ member-declaration
13503 declarator attributes [opt] pure-specifier [opt]
13504 declarator attributes [opt] constant-initializer [opt]
13505 identifier [opt] attributes [opt] : constant-expression */
13508 cp_parser_member_declaration (cp_parser* parser)
13510 cp_decl_specifier_seq decl_specifiers;
13511 tree prefix_attributes;
13513 int declares_class_or_enum;
13516 int saved_pedantic;
13518 /* Check for the `__extension__' keyword. */
13519 if (cp_parser_extension_opt (parser, &saved_pedantic))
13522 cp_parser_member_declaration (parser);
13523 /* Restore the old value of the PEDANTIC flag. */
13524 pedantic = saved_pedantic;
13529 /* Check for a template-declaration. */
13530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13532 /* An explicit specialization here is an error condition, and we
13533 expect the specialization handler to detect and report this. */
13534 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13535 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13536 cp_parser_explicit_specialization (parser);
13538 cp_parser_template_declaration (parser, /*member_p=*/true);
13543 /* Check for a using-declaration. */
13544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13546 /* Parse the using-declaration. */
13547 cp_parser_using_declaration (parser);
13552 /* Check for @defs. */
13553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13556 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13557 ivar = ivar_chains;
13561 ivar = TREE_CHAIN (member);
13562 TREE_CHAIN (member) = NULL_TREE;
13563 finish_member_declaration (member);
13568 /* Parse the decl-specifier-seq. */
13569 cp_parser_decl_specifier_seq (parser,
13570 CP_PARSER_FLAGS_OPTIONAL,
13572 &declares_class_or_enum);
13573 prefix_attributes = decl_specifiers.attributes;
13574 decl_specifiers.attributes = NULL_TREE;
13575 /* Check for an invalid type-name. */
13576 if (!decl_specifiers.type
13577 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13579 /* If there is no declarator, then the decl-specifier-seq should
13581 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13583 /* If there was no decl-specifier-seq, and the next token is a
13584 `;', then we have something like:
13590 Each member-declaration shall declare at least one member
13591 name of the class. */
13592 if (!decl_specifiers.any_specifiers_p)
13594 cp_token *token = cp_lexer_peek_token (parser->lexer);
13595 if (pedantic && !token->in_system_header)
13596 pedwarn ("%Hextra %<;%>", &token->location);
13602 /* See if this declaration is a friend. */
13603 friend_p = cp_parser_friend_p (&decl_specifiers);
13604 /* If there were decl-specifiers, check to see if there was
13605 a class-declaration. */
13606 type = check_tag_decl (&decl_specifiers);
13607 /* Nested classes have already been added to the class, but
13608 a `friend' needs to be explicitly registered. */
13611 /* If the `friend' keyword was present, the friend must
13612 be introduced with a class-key. */
13613 if (!declares_class_or_enum)
13614 error ("a class-key must be used when declaring a friend");
13617 template <typename T> struct A {
13618 friend struct A<T>::B;
13621 A<T>::B will be represented by a TYPENAME_TYPE, and
13622 therefore not recognized by check_tag_decl. */
13624 && decl_specifiers.type
13625 && TYPE_P (decl_specifiers.type))
13626 type = decl_specifiers.type;
13627 if (!type || !TYPE_P (type))
13628 error ("friend declaration does not name a class or "
13631 make_friend_class (current_class_type, type,
13632 /*complain=*/true);
13634 /* If there is no TYPE, an error message will already have
13636 else if (!type || type == error_mark_node)
13638 /* An anonymous aggregate has to be handled specially; such
13639 a declaration really declares a data member (with a
13640 particular type), as opposed to a nested class. */
13641 else if (ANON_AGGR_TYPE_P (type))
13643 /* Remove constructors and such from TYPE, now that we
13644 know it is an anonymous aggregate. */
13645 fixup_anonymous_aggr (type);
13646 /* And make the corresponding data member. */
13647 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13648 /* Add it to the class. */
13649 finish_member_declaration (decl);
13652 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13657 /* See if these declarations will be friends. */
13658 friend_p = cp_parser_friend_p (&decl_specifiers);
13660 /* Keep going until we hit the `;' at the end of the
13662 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13664 tree attributes = NULL_TREE;
13665 tree first_attribute;
13667 /* Peek at the next token. */
13668 token = cp_lexer_peek_token (parser->lexer);
13670 /* Check for a bitfield declaration. */
13671 if (token->type == CPP_COLON
13672 || (token->type == CPP_NAME
13673 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13679 /* Get the name of the bitfield. Note that we cannot just
13680 check TOKEN here because it may have been invalidated by
13681 the call to cp_lexer_peek_nth_token above. */
13682 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13683 identifier = cp_parser_identifier (parser);
13685 identifier = NULL_TREE;
13687 /* Consume the `:' token. */
13688 cp_lexer_consume_token (parser->lexer);
13689 /* Get the width of the bitfield. */
13691 = cp_parser_constant_expression (parser,
13692 /*allow_non_constant=*/false,
13695 /* Look for attributes that apply to the bitfield. */
13696 attributes = cp_parser_attributes_opt (parser);
13697 /* Remember which attributes are prefix attributes and
13699 first_attribute = attributes;
13700 /* Combine the attributes. */
13701 attributes = chainon (prefix_attributes, attributes);
13703 /* Create the bitfield declaration. */
13704 decl = grokbitfield (identifier
13705 ? make_id_declarator (NULL_TREE,
13711 /* Apply the attributes. */
13712 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13716 cp_declarator *declarator;
13718 tree asm_specification;
13719 int ctor_dtor_or_conv_p;
13721 /* Parse the declarator. */
13723 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13724 &ctor_dtor_or_conv_p,
13725 /*parenthesized_p=*/NULL,
13726 /*member_p=*/true);
13728 /* If something went wrong parsing the declarator, make sure
13729 that we at least consume some tokens. */
13730 if (declarator == cp_error_declarator)
13732 /* Skip to the end of the statement. */
13733 cp_parser_skip_to_end_of_statement (parser);
13734 /* If the next token is not a semicolon, that is
13735 probably because we just skipped over the body of
13736 a function. So, we consume a semicolon if
13737 present, but do not issue an error message if it
13739 if (cp_lexer_next_token_is (parser->lexer,
13741 cp_lexer_consume_token (parser->lexer);
13745 if (declares_class_or_enum & 2)
13746 cp_parser_check_for_definition_in_return_type
13747 (declarator, decl_specifiers.type);
13749 /* Look for an asm-specification. */
13750 asm_specification = cp_parser_asm_specification_opt (parser);
13751 /* Look for attributes that apply to the declaration. */
13752 attributes = cp_parser_attributes_opt (parser);
13753 /* Remember which attributes are prefix attributes and
13755 first_attribute = attributes;
13756 /* Combine the attributes. */
13757 attributes = chainon (prefix_attributes, attributes);
13759 /* If it's an `=', then we have a constant-initializer or a
13760 pure-specifier. It is not correct to parse the
13761 initializer before registering the member declaration
13762 since the member declaration should be in scope while
13763 its initializer is processed. However, the rest of the
13764 front end does not yet provide an interface that allows
13765 us to handle this correctly. */
13766 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13770 A pure-specifier shall be used only in the declaration of
13771 a virtual function.
13773 A member-declarator can contain a constant-initializer
13774 only if it declares a static member of integral or
13777 Therefore, if the DECLARATOR is for a function, we look
13778 for a pure-specifier; otherwise, we look for a
13779 constant-initializer. When we call `grokfield', it will
13780 perform more stringent semantics checks. */
13781 if (declarator->kind == cdk_function
13782 && declarator->declarator->kind == cdk_id)
13783 initializer = cp_parser_pure_specifier (parser);
13785 /* Parse the initializer. */
13786 initializer = cp_parser_constant_initializer (parser);
13788 /* Otherwise, there is no initializer. */
13790 initializer = NULL_TREE;
13792 /* See if we are probably looking at a function
13793 definition. We are certainly not looking at a
13794 member-declarator. Calling `grokfield' has
13795 side-effects, so we must not do it unless we are sure
13796 that we are looking at a member-declarator. */
13797 if (cp_parser_token_starts_function_definition_p
13798 (cp_lexer_peek_token (parser->lexer)))
13800 /* The grammar does not allow a pure-specifier to be
13801 used when a member function is defined. (It is
13802 possible that this fact is an oversight in the
13803 standard, since a pure function may be defined
13804 outside of the class-specifier. */
13806 error ("pure-specifier on function-definition");
13807 decl = cp_parser_save_member_function_body (parser,
13811 /* If the member was not a friend, declare it here. */
13813 finish_member_declaration (decl);
13814 /* Peek at the next token. */
13815 token = cp_lexer_peek_token (parser->lexer);
13816 /* If the next token is a semicolon, consume it. */
13817 if (token->type == CPP_SEMICOLON)
13818 cp_lexer_consume_token (parser->lexer);
13822 /* Create the declaration. */
13823 decl = grokfield (declarator, &decl_specifiers,
13824 initializer, /*init_const_expr_p=*/true,
13829 /* Reset PREFIX_ATTRIBUTES. */
13830 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13831 attributes = TREE_CHAIN (attributes);
13833 TREE_CHAIN (attributes) = NULL_TREE;
13835 /* If there is any qualification still in effect, clear it
13836 now; we will be starting fresh with the next declarator. */
13837 parser->scope = NULL_TREE;
13838 parser->qualifying_scope = NULL_TREE;
13839 parser->object_scope = NULL_TREE;
13840 /* If it's a `,', then there are more declarators. */
13841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13842 cp_lexer_consume_token (parser->lexer);
13843 /* If the next token isn't a `;', then we have a parse error. */
13844 else if (cp_lexer_next_token_is_not (parser->lexer,
13847 cp_parser_error (parser, "expected %<;%>");
13848 /* Skip tokens until we find a `;'. */
13849 cp_parser_skip_to_end_of_statement (parser);
13856 /* Add DECL to the list of members. */
13858 finish_member_declaration (decl);
13860 if (TREE_CODE (decl) == FUNCTION_DECL)
13861 cp_parser_save_default_args (parser, decl);
13866 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13869 /* Parse a pure-specifier.
13874 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13875 Otherwise, ERROR_MARK_NODE is returned. */
13878 cp_parser_pure_specifier (cp_parser* parser)
13882 /* Look for the `=' token. */
13883 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13884 return error_mark_node;
13885 /* Look for the `0' token. */
13886 token = cp_lexer_consume_token (parser->lexer);
13887 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13888 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13890 cp_parser_error (parser,
13891 "invalid pure specifier (only `= 0' is allowed)");
13892 cp_parser_skip_to_end_of_statement (parser);
13893 return error_mark_node;
13895 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13897 error ("templates may not be %<virtual%>");
13898 return error_mark_node;
13901 return integer_zero_node;
13904 /* Parse a constant-initializer.
13906 constant-initializer:
13907 = constant-expression
13909 Returns a representation of the constant-expression. */
13912 cp_parser_constant_initializer (cp_parser* parser)
13914 /* Look for the `=' token. */
13915 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13916 return error_mark_node;
13918 /* It is invalid to write:
13920 struct S { static const int i = { 7 }; };
13923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13925 cp_parser_error (parser,
13926 "a brace-enclosed initializer is not allowed here");
13927 /* Consume the opening brace. */
13928 cp_lexer_consume_token (parser->lexer);
13929 /* Skip the initializer. */
13930 cp_parser_skip_to_closing_brace (parser);
13931 /* Look for the trailing `}'. */
13932 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13934 return error_mark_node;
13937 return cp_parser_constant_expression (parser,
13938 /*allow_non_constant=*/false,
13942 /* Derived classes [gram.class.derived] */
13944 /* Parse a base-clause.
13947 : base-specifier-list
13949 base-specifier-list:
13951 base-specifier-list , base-specifier
13953 Returns a TREE_LIST representing the base-classes, in the order in
13954 which they were declared. The representation of each node is as
13955 described by cp_parser_base_specifier.
13957 In the case that no bases are specified, this function will return
13958 NULL_TREE, not ERROR_MARK_NODE. */
13961 cp_parser_base_clause (cp_parser* parser)
13963 tree bases = NULL_TREE;
13965 /* Look for the `:' that begins the list. */
13966 cp_parser_require (parser, CPP_COLON, "`:'");
13968 /* Scan the base-specifier-list. */
13974 /* Look for the base-specifier. */
13975 base = cp_parser_base_specifier (parser);
13976 /* Add BASE to the front of the list. */
13977 if (base != error_mark_node)
13979 TREE_CHAIN (base) = bases;
13982 /* Peek at the next token. */
13983 token = cp_lexer_peek_token (parser->lexer);
13984 /* If it's not a comma, then the list is complete. */
13985 if (token->type != CPP_COMMA)
13987 /* Consume the `,'. */
13988 cp_lexer_consume_token (parser->lexer);
13991 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13992 base class had a qualified name. However, the next name that
13993 appears is certainly not qualified. */
13994 parser->scope = NULL_TREE;
13995 parser->qualifying_scope = NULL_TREE;
13996 parser->object_scope = NULL_TREE;
13998 return nreverse (bases);
14001 /* Parse a base-specifier.
14004 :: [opt] nested-name-specifier [opt] class-name
14005 virtual access-specifier [opt] :: [opt] nested-name-specifier
14007 access-specifier virtual [opt] :: [opt] nested-name-specifier
14010 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14011 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14012 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14013 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14016 cp_parser_base_specifier (cp_parser* parser)
14020 bool virtual_p = false;
14021 bool duplicate_virtual_error_issued_p = false;
14022 bool duplicate_access_error_issued_p = false;
14023 bool class_scope_p, template_p;
14024 tree access = access_default_node;
14027 /* Process the optional `virtual' and `access-specifier'. */
14030 /* Peek at the next token. */
14031 token = cp_lexer_peek_token (parser->lexer);
14032 /* Process `virtual'. */
14033 switch (token->keyword)
14036 /* If `virtual' appears more than once, issue an error. */
14037 if (virtual_p && !duplicate_virtual_error_issued_p)
14039 cp_parser_error (parser,
14040 "%<virtual%> specified more than once in base-specified");
14041 duplicate_virtual_error_issued_p = true;
14046 /* Consume the `virtual' token. */
14047 cp_lexer_consume_token (parser->lexer);
14052 case RID_PROTECTED:
14054 /* If more than one access specifier appears, issue an
14056 if (access != access_default_node
14057 && !duplicate_access_error_issued_p)
14059 cp_parser_error (parser,
14060 "more than one access specifier in base-specified");
14061 duplicate_access_error_issued_p = true;
14064 access = ridpointers[(int) token->keyword];
14066 /* Consume the access-specifier. */
14067 cp_lexer_consume_token (parser->lexer);
14076 /* It is not uncommon to see programs mechanically, erroneously, use
14077 the 'typename' keyword to denote (dependent) qualified types
14078 as base classes. */
14079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14081 if (!processing_template_decl)
14082 error ("keyword %<typename%> not allowed outside of templates");
14084 error ("keyword %<typename%> not allowed in this context "
14085 "(the base class is implicitly a type)");
14086 cp_lexer_consume_token (parser->lexer);
14089 /* Look for the optional `::' operator. */
14090 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14091 /* Look for the nested-name-specifier. The simplest way to
14096 The keyword `typename' is not permitted in a base-specifier or
14097 mem-initializer; in these contexts a qualified name that
14098 depends on a template-parameter is implicitly assumed to be a
14101 is to pretend that we have seen the `typename' keyword at this
14103 cp_parser_nested_name_specifier_opt (parser,
14104 /*typename_keyword_p=*/true,
14105 /*check_dependency_p=*/true,
14107 /*is_declaration=*/true);
14108 /* If the base class is given by a qualified name, assume that names
14109 we see are type names or templates, as appropriate. */
14110 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14111 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14113 /* Finally, look for the class-name. */
14114 type = cp_parser_class_name (parser,
14118 /*check_dependency_p=*/true,
14119 /*class_head_p=*/false,
14120 /*is_declaration=*/true);
14122 if (type == error_mark_node)
14123 return error_mark_node;
14125 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14128 /* Exception handling [gram.exception] */
14130 /* Parse an (optional) exception-specification.
14132 exception-specification:
14133 throw ( type-id-list [opt] )
14135 Returns a TREE_LIST representing the exception-specification. The
14136 TREE_VALUE of each node is a type. */
14139 cp_parser_exception_specification_opt (cp_parser* parser)
14144 /* Peek at the next token. */
14145 token = cp_lexer_peek_token (parser->lexer);
14146 /* If it's not `throw', then there's no exception-specification. */
14147 if (!cp_parser_is_keyword (token, RID_THROW))
14150 /* Consume the `throw'. */
14151 cp_lexer_consume_token (parser->lexer);
14153 /* Look for the `('. */
14154 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14156 /* Peek at the next token. */
14157 token = cp_lexer_peek_token (parser->lexer);
14158 /* If it's not a `)', then there is a type-id-list. */
14159 if (token->type != CPP_CLOSE_PAREN)
14161 const char *saved_message;
14163 /* Types may not be defined in an exception-specification. */
14164 saved_message = parser->type_definition_forbidden_message;
14165 parser->type_definition_forbidden_message
14166 = "types may not be defined in an exception-specification";
14167 /* Parse the type-id-list. */
14168 type_id_list = cp_parser_type_id_list (parser);
14169 /* Restore the saved message. */
14170 parser->type_definition_forbidden_message = saved_message;
14173 type_id_list = empty_except_spec;
14175 /* Look for the `)'. */
14176 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14178 return type_id_list;
14181 /* Parse an (optional) type-id-list.
14185 type-id-list , type-id
14187 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14188 in the order that the types were presented. */
14191 cp_parser_type_id_list (cp_parser* parser)
14193 tree types = NULL_TREE;
14200 /* Get the next type-id. */
14201 type = cp_parser_type_id (parser);
14202 /* Add it to the list. */
14203 types = add_exception_specifier (types, type, /*complain=*/1);
14204 /* Peek at the next token. */
14205 token = cp_lexer_peek_token (parser->lexer);
14206 /* If it is not a `,', we are done. */
14207 if (token->type != CPP_COMMA)
14209 /* Consume the `,'. */
14210 cp_lexer_consume_token (parser->lexer);
14213 return nreverse (types);
14216 /* Parse a try-block.
14219 try compound-statement handler-seq */
14222 cp_parser_try_block (cp_parser* parser)
14226 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14227 try_block = begin_try_block ();
14228 cp_parser_compound_statement (parser, NULL, true);
14229 finish_try_block (try_block);
14230 cp_parser_handler_seq (parser);
14231 finish_handler_sequence (try_block);
14236 /* Parse a function-try-block.
14238 function-try-block:
14239 try ctor-initializer [opt] function-body handler-seq */
14242 cp_parser_function_try_block (cp_parser* parser)
14244 tree compound_stmt;
14246 bool ctor_initializer_p;
14248 /* Look for the `try' keyword. */
14249 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14251 /* Let the rest of the front-end know where we are. */
14252 try_block = begin_function_try_block (&compound_stmt);
14253 /* Parse the function-body. */
14255 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14256 /* We're done with the `try' part. */
14257 finish_function_try_block (try_block);
14258 /* Parse the handlers. */
14259 cp_parser_handler_seq (parser);
14260 /* We're done with the handlers. */
14261 finish_function_handler_sequence (try_block, compound_stmt);
14263 return ctor_initializer_p;
14266 /* Parse a handler-seq.
14269 handler handler-seq [opt] */
14272 cp_parser_handler_seq (cp_parser* parser)
14278 /* Parse the handler. */
14279 cp_parser_handler (parser);
14280 /* Peek at the next token. */
14281 token = cp_lexer_peek_token (parser->lexer);
14282 /* If it's not `catch' then there are no more handlers. */
14283 if (!cp_parser_is_keyword (token, RID_CATCH))
14288 /* Parse a handler.
14291 catch ( exception-declaration ) compound-statement */
14294 cp_parser_handler (cp_parser* parser)
14299 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14300 handler = begin_handler ();
14301 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14302 declaration = cp_parser_exception_declaration (parser);
14303 finish_handler_parms (declaration, handler);
14304 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14305 cp_parser_compound_statement (parser, NULL, false);
14306 finish_handler (handler);
14309 /* Parse an exception-declaration.
14311 exception-declaration:
14312 type-specifier-seq declarator
14313 type-specifier-seq abstract-declarator
14317 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14318 ellipsis variant is used. */
14321 cp_parser_exception_declaration (cp_parser* parser)
14323 cp_decl_specifier_seq type_specifiers;
14324 cp_declarator *declarator;
14325 const char *saved_message;
14327 /* If it's an ellipsis, it's easy to handle. */
14328 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14330 /* Consume the `...' token. */
14331 cp_lexer_consume_token (parser->lexer);
14335 /* Types may not be defined in exception-declarations. */
14336 saved_message = parser->type_definition_forbidden_message;
14337 parser->type_definition_forbidden_message
14338 = "types may not be defined in exception-declarations";
14340 /* Parse the type-specifier-seq. */
14341 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14343 /* If it's a `)', then there is no declarator. */
14344 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14347 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14348 /*ctor_dtor_or_conv_p=*/NULL,
14349 /*parenthesized_p=*/NULL,
14350 /*member_p=*/false);
14352 /* Restore the saved message. */
14353 parser->type_definition_forbidden_message = saved_message;
14355 if (!type_specifiers.any_specifiers_p)
14356 return error_mark_node;
14358 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14361 /* Parse a throw-expression.
14364 throw assignment-expression [opt]
14366 Returns a THROW_EXPR representing the throw-expression. */
14369 cp_parser_throw_expression (cp_parser* parser)
14374 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14375 token = cp_lexer_peek_token (parser->lexer);
14376 /* Figure out whether or not there is an assignment-expression
14377 following the "throw" keyword. */
14378 if (token->type == CPP_COMMA
14379 || token->type == CPP_SEMICOLON
14380 || token->type == CPP_CLOSE_PAREN
14381 || token->type == CPP_CLOSE_SQUARE
14382 || token->type == CPP_CLOSE_BRACE
14383 || token->type == CPP_COLON)
14384 expression = NULL_TREE;
14386 expression = cp_parser_assignment_expression (parser,
14389 return build_throw (expression);
14392 /* GNU Extensions */
14394 /* Parse an (optional) asm-specification.
14397 asm ( string-literal )
14399 If the asm-specification is present, returns a STRING_CST
14400 corresponding to the string-literal. Otherwise, returns
14404 cp_parser_asm_specification_opt (cp_parser* parser)
14407 tree asm_specification;
14409 /* Peek at the next token. */
14410 token = cp_lexer_peek_token (parser->lexer);
14411 /* If the next token isn't the `asm' keyword, then there's no
14412 asm-specification. */
14413 if (!cp_parser_is_keyword (token, RID_ASM))
14416 /* Consume the `asm' token. */
14417 cp_lexer_consume_token (parser->lexer);
14418 /* Look for the `('. */
14419 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14421 /* Look for the string-literal. */
14422 asm_specification = cp_parser_string_literal (parser, false, false);
14424 /* Look for the `)'. */
14425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14427 return asm_specification;
14430 /* Parse an asm-operand-list.
14434 asm-operand-list , asm-operand
14437 string-literal ( expression )
14438 [ string-literal ] string-literal ( expression )
14440 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14441 each node is the expression. The TREE_PURPOSE is itself a
14442 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14443 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14444 is a STRING_CST for the string literal before the parenthesis. */
14447 cp_parser_asm_operand_list (cp_parser* parser)
14449 tree asm_operands = NULL_TREE;
14453 tree string_literal;
14457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14459 /* Consume the `[' token. */
14460 cp_lexer_consume_token (parser->lexer);
14461 /* Read the operand name. */
14462 name = cp_parser_identifier (parser);
14463 if (name != error_mark_node)
14464 name = build_string (IDENTIFIER_LENGTH (name),
14465 IDENTIFIER_POINTER (name));
14466 /* Look for the closing `]'. */
14467 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14471 /* Look for the string-literal. */
14472 string_literal = cp_parser_string_literal (parser, false, false);
14474 /* Look for the `('. */
14475 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14476 /* Parse the expression. */
14477 expression = cp_parser_expression (parser, /*cast_p=*/false);
14478 /* Look for the `)'. */
14479 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14481 /* Add this operand to the list. */
14482 asm_operands = tree_cons (build_tree_list (name, string_literal),
14485 /* If the next token is not a `,', there are no more
14487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14489 /* Consume the `,'. */
14490 cp_lexer_consume_token (parser->lexer);
14493 return nreverse (asm_operands);
14496 /* Parse an asm-clobber-list.
14500 asm-clobber-list , string-literal
14502 Returns a TREE_LIST, indicating the clobbers in the order that they
14503 appeared. The TREE_VALUE of each node is a STRING_CST. */
14506 cp_parser_asm_clobber_list (cp_parser* parser)
14508 tree clobbers = NULL_TREE;
14512 tree string_literal;
14514 /* Look for the string literal. */
14515 string_literal = cp_parser_string_literal (parser, false, false);
14516 /* Add it to the list. */
14517 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14518 /* If the next token is not a `,', then the list is
14520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14522 /* Consume the `,' token. */
14523 cp_lexer_consume_token (parser->lexer);
14529 /* Parse an (optional) series of attributes.
14532 attributes attribute
14535 __attribute__ (( attribute-list [opt] ))
14537 The return value is as for cp_parser_attribute_list. */
14540 cp_parser_attributes_opt (cp_parser* parser)
14542 tree attributes = NULL_TREE;
14547 tree attribute_list;
14549 /* Peek at the next token. */
14550 token = cp_lexer_peek_token (parser->lexer);
14551 /* If it's not `__attribute__', then we're done. */
14552 if (token->keyword != RID_ATTRIBUTE)
14555 /* Consume the `__attribute__' keyword. */
14556 cp_lexer_consume_token (parser->lexer);
14557 /* Look for the two `(' tokens. */
14558 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14559 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14561 /* Peek at the next token. */
14562 token = cp_lexer_peek_token (parser->lexer);
14563 if (token->type != CPP_CLOSE_PAREN)
14564 /* Parse the attribute-list. */
14565 attribute_list = cp_parser_attribute_list (parser);
14567 /* If the next token is a `)', then there is no attribute
14569 attribute_list = NULL;
14571 /* Look for the two `)' tokens. */
14572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14573 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14575 /* Add these new attributes to the list. */
14576 attributes = chainon (attributes, attribute_list);
14582 /* Parse an attribute-list.
14586 attribute-list , attribute
14590 identifier ( identifier )
14591 identifier ( identifier , expression-list )
14592 identifier ( expression-list )
14594 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14595 to an attribute. The TREE_PURPOSE of each node is the identifier
14596 indicating which attribute is in use. The TREE_VALUE represents
14597 the arguments, if any. */
14600 cp_parser_attribute_list (cp_parser* parser)
14602 tree attribute_list = NULL_TREE;
14603 bool save_translate_strings_p = parser->translate_strings_p;
14605 parser->translate_strings_p = false;
14612 /* Look for the identifier. We also allow keywords here; for
14613 example `__attribute__ ((const))' is legal. */
14614 token = cp_lexer_peek_token (parser->lexer);
14615 if (token->type == CPP_NAME
14616 || token->type == CPP_KEYWORD)
14618 tree arguments = NULL_TREE;
14620 /* Consume the token. */
14621 token = cp_lexer_consume_token (parser->lexer);
14623 /* Save away the identifier that indicates which attribute
14625 identifier = token->value;
14626 attribute = build_tree_list (identifier, NULL_TREE);
14628 /* Peek at the next token. */
14629 token = cp_lexer_peek_token (parser->lexer);
14630 /* If it's an `(', then parse the attribute arguments. */
14631 if (token->type == CPP_OPEN_PAREN)
14633 arguments = cp_parser_parenthesized_expression_list
14634 (parser, true, /*cast_p=*/false,
14635 /*non_constant_p=*/NULL);
14636 /* Save the arguments away. */
14637 TREE_VALUE (attribute) = arguments;
14640 if (arguments != error_mark_node)
14642 /* Add this attribute to the list. */
14643 TREE_CHAIN (attribute) = attribute_list;
14644 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 indicated by
16140 cp_parser_set_storage_class (cp_parser *parser,
16141 cp_decl_specifier_seq *decl_specs,
16144 cp_storage_class storage_class;
16146 if (parser->in_unbraced_linkage_specification_p)
16148 error ("invalid use of %qD in linkage specification",
16149 ridpointers[keyword]);
16152 else if (decl_specs->storage_class != sc_none)
16154 decl_specs->multiple_storage_classes_p = true;
16158 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16159 && decl_specs->specs[(int) ds_thread])
16161 error ("%<__thread%> before %qD", ridpointers[keyword]);
16162 decl_specs->specs[(int) ds_thread] = 0;
16168 storage_class = sc_auto;
16171 storage_class = sc_register;
16174 storage_class = sc_static;
16177 storage_class = sc_extern;
16180 storage_class = sc_mutable;
16183 gcc_unreachable ();
16185 decl_specs->storage_class = storage_class;
16188 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16189 is true, the type is a user-defined type; otherwise it is a
16190 built-in type specified by a keyword. */
16193 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16195 bool user_defined_p)
16197 decl_specs->any_specifiers_p = true;
16199 /* If the user tries to redeclare bool or wchar_t (with, for
16200 example, in "typedef int wchar_t;") we remember that this is what
16201 happened. In system headers, we ignore these declarations so
16202 that G++ can work with system headers that are not C++-safe. */
16203 if (decl_specs->specs[(int) ds_typedef]
16205 && (type_spec == boolean_type_node
16206 || type_spec == wchar_type_node)
16207 && (decl_specs->type
16208 || decl_specs->specs[(int) ds_long]
16209 || decl_specs->specs[(int) ds_short]
16210 || decl_specs->specs[(int) ds_unsigned]
16211 || decl_specs->specs[(int) ds_signed]))
16213 decl_specs->redefined_builtin_type = type_spec;
16214 if (!decl_specs->type)
16216 decl_specs->type = type_spec;
16217 decl_specs->user_defined_type_p = false;
16220 else if (decl_specs->type)
16221 decl_specs->multiple_types_p = true;
16224 decl_specs->type = type_spec;
16225 decl_specs->user_defined_type_p = user_defined_p;
16226 decl_specs->redefined_builtin_type = NULL_TREE;
16230 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16231 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16234 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16236 return decl_specifiers->specs[(int) ds_friend] != 0;
16239 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16240 issue an error message indicating that TOKEN_DESC was expected.
16242 Returns the token consumed, if the token had the appropriate type.
16243 Otherwise, returns NULL. */
16246 cp_parser_require (cp_parser* parser,
16247 enum cpp_ttype type,
16248 const char* token_desc)
16250 if (cp_lexer_next_token_is (parser->lexer, type))
16251 return cp_lexer_consume_token (parser->lexer);
16254 /* Output the MESSAGE -- unless we're parsing tentatively. */
16255 if (!cp_parser_simulate_error (parser))
16257 char *message = concat ("expected ", token_desc, NULL);
16258 cp_parser_error (parser, message);
16265 /* Like cp_parser_require, except that tokens will be skipped until
16266 the desired token is found. An error message is still produced if
16267 the next token is not as expected. */
16270 cp_parser_skip_until_found (cp_parser* parser,
16271 enum cpp_ttype type,
16272 const char* token_desc)
16275 unsigned nesting_depth = 0;
16277 if (cp_parser_require (parser, type, token_desc))
16280 /* Skip tokens until the desired token is found. */
16283 /* Peek at the next token. */
16284 token = cp_lexer_peek_token (parser->lexer);
16286 /* If we've reached the token we want, consume it and stop. */
16287 if (token->type == type && !nesting_depth)
16289 cp_lexer_consume_token (parser->lexer);
16293 switch (token->type)
16296 case CPP_PRAGMA_EOL:
16297 /* If we've run out of tokens, stop. */
16300 case CPP_OPEN_BRACE:
16301 case CPP_OPEN_PAREN:
16302 case CPP_OPEN_SQUARE:
16306 case CPP_CLOSE_BRACE:
16307 case CPP_CLOSE_PAREN:
16308 case CPP_CLOSE_SQUARE:
16309 if (nesting_depth-- == 0)
16317 /* Consume this token. */
16318 cp_lexer_consume_token (parser->lexer);
16322 /* If the next token is the indicated keyword, consume it. Otherwise,
16323 issue an error message indicating that TOKEN_DESC was expected.
16325 Returns the token consumed, if the token had the appropriate type.
16326 Otherwise, returns NULL. */
16329 cp_parser_require_keyword (cp_parser* parser,
16331 const char* token_desc)
16333 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16335 if (token && token->keyword != keyword)
16337 dyn_string_t error_msg;
16339 /* Format the error message. */
16340 error_msg = dyn_string_new (0);
16341 dyn_string_append_cstr (error_msg, "expected ");
16342 dyn_string_append_cstr (error_msg, token_desc);
16343 cp_parser_error (parser, error_msg->s);
16344 dyn_string_delete (error_msg);
16351 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16352 function-definition. */
16355 cp_parser_token_starts_function_definition_p (cp_token* token)
16357 return (/* An ordinary function-body begins with an `{'. */
16358 token->type == CPP_OPEN_BRACE
16359 /* A ctor-initializer begins with a `:'. */
16360 || token->type == CPP_COLON
16361 /* A function-try-block begins with `try'. */
16362 || token->keyword == RID_TRY
16363 /* The named return value extension begins with `return'. */
16364 || token->keyword == RID_RETURN);
16367 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16371 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16375 token = cp_lexer_peek_token (parser->lexer);
16376 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16379 /* Returns TRUE iff the next token is the "," or ">" ending a
16380 template-argument. */
16383 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16387 token = cp_lexer_peek_token (parser->lexer);
16388 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16391 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16392 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16395 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16400 token = cp_lexer_peek_nth_token (parser->lexer, n);
16401 if (token->type == CPP_LESS)
16403 /* Check for the sequence `<::' in the original code. It would be lexed as
16404 `[:', where `[' is a digraph, and there is no whitespace before
16406 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16409 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16410 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16416 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16417 or none_type otherwise. */
16419 static enum tag_types
16420 cp_parser_token_is_class_key (cp_token* token)
16422 switch (token->keyword)
16427 return record_type;
16436 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16439 cp_parser_check_class_key (enum tag_types class_key, tree type)
16441 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16442 pedwarn ("%qs tag used in naming %q#T",
16443 class_key == union_type ? "union"
16444 : class_key == record_type ? "struct" : "class",
16448 /* Issue an error message if DECL is redeclared with different
16449 access than its original declaration [class.access.spec/3].
16450 This applies to nested classes and nested class templates.
16454 cp_parser_check_access_in_redeclaration (tree decl)
16456 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16459 if ((TREE_PRIVATE (decl)
16460 != (current_access_specifier == access_private_node))
16461 || (TREE_PROTECTED (decl)
16462 != (current_access_specifier == access_protected_node)))
16463 error ("%qD redeclared with different access", decl);
16466 /* Look for the `template' keyword, as a syntactic disambiguator.
16467 Return TRUE iff it is present, in which case it will be
16471 cp_parser_optional_template_keyword (cp_parser *parser)
16473 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16475 /* The `template' keyword can only be used within templates;
16476 outside templates the parser can always figure out what is a
16477 template and what is not. */
16478 if (!processing_template_decl)
16480 error ("%<template%> (as a disambiguator) is only allowed "
16481 "within templates");
16482 /* If this part of the token stream is rescanned, the same
16483 error message would be generated. So, we purge the token
16484 from the stream. */
16485 cp_lexer_purge_token (parser->lexer);
16490 /* Consume the `template' keyword. */
16491 cp_lexer_consume_token (parser->lexer);
16499 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16500 set PARSER->SCOPE, and perform other related actions. */
16503 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16508 /* Get the stored value. */
16509 value = cp_lexer_consume_token (parser->lexer)->value;
16510 /* Perform any access checks that were deferred. */
16511 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16512 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16513 /* Set the scope from the stored value. */
16514 parser->scope = TREE_VALUE (value);
16515 parser->qualifying_scope = TREE_TYPE (value);
16516 parser->object_scope = NULL_TREE;
16519 /* Consume tokens up through a non-nested END token. */
16522 cp_parser_cache_group (cp_parser *parser,
16523 enum cpp_ttype end,
16530 /* Abort a parenthesized expression if we encounter a brace. */
16531 if ((end == CPP_CLOSE_PAREN || depth == 0)
16532 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16534 /* If we've reached the end of the file, stop. */
16535 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16536 || (end != CPP_PRAGMA_EOL
16537 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16539 /* Consume the next token. */
16540 token = cp_lexer_consume_token (parser->lexer);
16541 /* See if it starts a new group. */
16542 if (token->type == CPP_OPEN_BRACE)
16544 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16548 else if (token->type == CPP_OPEN_PAREN)
16549 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16550 else if (token->type == CPP_PRAGMA)
16551 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16552 else if (token->type == end)
16557 /* Begin parsing tentatively. We always save tokens while parsing
16558 tentatively so that if the tentative parsing fails we can restore the
16562 cp_parser_parse_tentatively (cp_parser* parser)
16564 /* Enter a new parsing context. */
16565 parser->context = cp_parser_context_new (parser->context);
16566 /* Begin saving tokens. */
16567 cp_lexer_save_tokens (parser->lexer);
16568 /* In order to avoid repetitive access control error messages,
16569 access checks are queued up until we are no longer parsing
16571 push_deferring_access_checks (dk_deferred);
16574 /* Commit to the currently active tentative parse. */
16577 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16579 cp_parser_context *context;
16582 /* Mark all of the levels as committed. */
16583 lexer = parser->lexer;
16584 for (context = parser->context; context->next; context = context->next)
16586 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16588 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16589 while (!cp_lexer_saving_tokens (lexer))
16590 lexer = lexer->next;
16591 cp_lexer_commit_tokens (lexer);
16595 /* Abort the currently active tentative parse. All consumed tokens
16596 will be rolled back, and no diagnostics will be issued. */
16599 cp_parser_abort_tentative_parse (cp_parser* parser)
16601 cp_parser_simulate_error (parser);
16602 /* Now, pretend that we want to see if the construct was
16603 successfully parsed. */
16604 cp_parser_parse_definitely (parser);
16607 /* Stop parsing tentatively. If a parse error has occurred, restore the
16608 token stream. Otherwise, commit to the tokens we have consumed.
16609 Returns true if no error occurred; false otherwise. */
16612 cp_parser_parse_definitely (cp_parser* parser)
16614 bool error_occurred;
16615 cp_parser_context *context;
16617 /* Remember whether or not an error occurred, since we are about to
16618 destroy that information. */
16619 error_occurred = cp_parser_error_occurred (parser);
16620 /* Remove the topmost context from the stack. */
16621 context = parser->context;
16622 parser->context = context->next;
16623 /* If no parse errors occurred, commit to the tentative parse. */
16624 if (!error_occurred)
16626 /* Commit to the tokens read tentatively, unless that was
16628 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16629 cp_lexer_commit_tokens (parser->lexer);
16631 pop_to_parent_deferring_access_checks ();
16633 /* Otherwise, if errors occurred, roll back our state so that things
16634 are just as they were before we began the tentative parse. */
16637 cp_lexer_rollback_tokens (parser->lexer);
16638 pop_deferring_access_checks ();
16640 /* Add the context to the front of the free list. */
16641 context->next = cp_parser_context_free_list;
16642 cp_parser_context_free_list = context;
16644 return !error_occurred;
16647 /* Returns true if we are parsing tentatively and are not committed to
16648 this tentative parse. */
16651 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16653 return (cp_parser_parsing_tentatively (parser)
16654 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16657 /* Returns nonzero iff an error has occurred during the most recent
16658 tentative parse. */
16661 cp_parser_error_occurred (cp_parser* parser)
16663 return (cp_parser_parsing_tentatively (parser)
16664 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16667 /* Returns nonzero if GNU extensions are allowed. */
16670 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16672 return parser->allow_gnu_extensions_p;
16675 /* Objective-C++ Productions */
16678 /* Parse an Objective-C expression, which feeds into a primary-expression
16682 objc-message-expression
16683 objc-string-literal
16684 objc-encode-expression
16685 objc-protocol-expression
16686 objc-selector-expression
16688 Returns a tree representation of the expression. */
16691 cp_parser_objc_expression (cp_parser* parser)
16693 /* Try to figure out what kind of declaration is present. */
16694 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16698 case CPP_OPEN_SQUARE:
16699 return cp_parser_objc_message_expression (parser);
16701 case CPP_OBJC_STRING:
16702 kwd = cp_lexer_consume_token (parser->lexer);
16703 return objc_build_string_object (kwd->value);
16706 switch (kwd->keyword)
16708 case RID_AT_ENCODE:
16709 return cp_parser_objc_encode_expression (parser);
16711 case RID_AT_PROTOCOL:
16712 return cp_parser_objc_protocol_expression (parser);
16714 case RID_AT_SELECTOR:
16715 return cp_parser_objc_selector_expression (parser);
16721 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16722 cp_parser_skip_to_end_of_block_or_statement (parser);
16725 return error_mark_node;
16728 /* Parse an Objective-C message expression.
16730 objc-message-expression:
16731 [ objc-message-receiver objc-message-args ]
16733 Returns a representation of an Objective-C message. */
16736 cp_parser_objc_message_expression (cp_parser* parser)
16738 tree receiver, messageargs;
16740 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16741 receiver = cp_parser_objc_message_receiver (parser);
16742 messageargs = cp_parser_objc_message_args (parser);
16743 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16745 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16748 /* Parse an objc-message-receiver.
16750 objc-message-receiver:
16752 simple-type-specifier
16754 Returns a representation of the type or expression. */
16757 cp_parser_objc_message_receiver (cp_parser* parser)
16761 /* An Objective-C message receiver may be either (1) a type
16762 or (2) an expression. */
16763 cp_parser_parse_tentatively (parser);
16764 rcv = cp_parser_expression (parser, false);
16766 if (cp_parser_parse_definitely (parser))
16769 rcv = cp_parser_simple_type_specifier (parser,
16770 /*decl_specs=*/NULL,
16771 CP_PARSER_FLAGS_NONE);
16773 return objc_get_class_reference (rcv);
16776 /* Parse the arguments and selectors comprising an Objective-C message.
16781 objc-selector-args , objc-comma-args
16783 objc-selector-args:
16784 objc-selector [opt] : assignment-expression
16785 objc-selector-args objc-selector [opt] : assignment-expression
16788 assignment-expression
16789 objc-comma-args , assignment-expression
16791 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16792 selector arguments and TREE_VALUE containing a list of comma
16796 cp_parser_objc_message_args (cp_parser* parser)
16798 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16799 bool maybe_unary_selector_p = true;
16800 cp_token *token = cp_lexer_peek_token (parser->lexer);
16802 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16804 tree selector = NULL_TREE, arg;
16806 if (token->type != CPP_COLON)
16807 selector = cp_parser_objc_selector (parser);
16809 /* Detect if we have a unary selector. */
16810 if (maybe_unary_selector_p
16811 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16812 return build_tree_list (selector, NULL_TREE);
16814 maybe_unary_selector_p = false;
16815 cp_parser_require (parser, CPP_COLON, "`:'");
16816 arg = cp_parser_assignment_expression (parser, false);
16819 = chainon (sel_args,
16820 build_tree_list (selector, arg));
16822 token = cp_lexer_peek_token (parser->lexer);
16825 /* Handle non-selector arguments, if any. */
16826 while (token->type == CPP_COMMA)
16830 cp_lexer_consume_token (parser->lexer);
16831 arg = cp_parser_assignment_expression (parser, false);
16834 = chainon (addl_args,
16835 build_tree_list (NULL_TREE, arg));
16837 token = cp_lexer_peek_token (parser->lexer);
16840 return build_tree_list (sel_args, addl_args);
16843 /* Parse an Objective-C encode expression.
16845 objc-encode-expression:
16846 @encode objc-typename
16848 Returns an encoded representation of the type argument. */
16851 cp_parser_objc_encode_expression (cp_parser* parser)
16855 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16856 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16857 type = complete_type (cp_parser_type_id (parser));
16858 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16862 error ("%<@encode%> must specify a type as an argument");
16863 return error_mark_node;
16866 return objc_build_encode_expr (type);
16869 /* Parse an Objective-C @defs expression. */
16872 cp_parser_objc_defs_expression (cp_parser *parser)
16876 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16877 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16878 name = cp_parser_identifier (parser);
16879 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16881 return objc_get_class_ivars (name);
16884 /* Parse an Objective-C protocol expression.
16886 objc-protocol-expression:
16887 @protocol ( identifier )
16889 Returns a representation of the protocol expression. */
16892 cp_parser_objc_protocol_expression (cp_parser* parser)
16896 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16897 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16898 proto = cp_parser_identifier (parser);
16899 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16901 return objc_build_protocol_expr (proto);
16904 /* Parse an Objective-C selector expression.
16906 objc-selector-expression:
16907 @selector ( objc-method-signature )
16909 objc-method-signature:
16915 objc-selector-seq objc-selector :
16917 Returns a representation of the method selector. */
16920 cp_parser_objc_selector_expression (cp_parser* parser)
16922 tree sel_seq = NULL_TREE;
16923 bool maybe_unary_selector_p = true;
16926 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16927 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16928 token = cp_lexer_peek_token (parser->lexer);
16930 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16931 || token->type == CPP_SCOPE)
16933 tree selector = NULL_TREE;
16935 if (token->type != CPP_COLON
16936 || token->type == CPP_SCOPE)
16937 selector = cp_parser_objc_selector (parser);
16939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
16940 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
16942 /* Detect if we have a unary selector. */
16943 if (maybe_unary_selector_p)
16945 sel_seq = selector;
16946 goto finish_selector;
16950 cp_parser_error (parser, "expected %<:%>");
16953 maybe_unary_selector_p = false;
16954 token = cp_lexer_consume_token (parser->lexer);
16956 if (token->type == CPP_SCOPE)
16959 = chainon (sel_seq,
16960 build_tree_list (selector, NULL_TREE));
16962 = chainon (sel_seq,
16963 build_tree_list (NULL_TREE, NULL_TREE));
16967 = chainon (sel_seq,
16968 build_tree_list (selector, NULL_TREE));
16970 token = cp_lexer_peek_token (parser->lexer);
16974 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16976 return objc_build_selector_expr (sel_seq);
16979 /* Parse a list of identifiers.
16981 objc-identifier-list:
16983 objc-identifier-list , identifier
16985 Returns a TREE_LIST of identifier nodes. */
16988 cp_parser_objc_identifier_list (cp_parser* parser)
16990 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16991 cp_token *sep = cp_lexer_peek_token (parser->lexer);
16993 while (sep->type == CPP_COMMA)
16995 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
16996 list = chainon (list,
16997 build_tree_list (NULL_TREE,
16998 cp_parser_identifier (parser)));
16999 sep = cp_lexer_peek_token (parser->lexer);
17005 /* Parse an Objective-C alias declaration.
17007 objc-alias-declaration:
17008 @compatibility_alias identifier identifier ;
17010 This function registers the alias mapping with the Objective-C front-end.
17011 It returns nothing. */
17014 cp_parser_objc_alias_declaration (cp_parser* parser)
17018 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17019 alias = cp_parser_identifier (parser);
17020 orig = cp_parser_identifier (parser);
17021 objc_declare_alias (alias, orig);
17022 cp_parser_consume_semicolon_at_end_of_statement (parser);
17025 /* Parse an Objective-C class forward-declaration.
17027 objc-class-declaration:
17028 @class objc-identifier-list ;
17030 The function registers the forward declarations with the Objective-C
17031 front-end. It returns nothing. */
17034 cp_parser_objc_class_declaration (cp_parser* parser)
17036 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17037 objc_declare_class (cp_parser_objc_identifier_list (parser));
17038 cp_parser_consume_semicolon_at_end_of_statement (parser);
17041 /* Parse a list of Objective-C protocol references.
17043 objc-protocol-refs-opt:
17044 objc-protocol-refs [opt]
17046 objc-protocol-refs:
17047 < objc-identifier-list >
17049 Returns a TREE_LIST of identifiers, if any. */
17052 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17054 tree protorefs = NULL_TREE;
17056 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17058 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17059 protorefs = cp_parser_objc_identifier_list (parser);
17060 cp_parser_require (parser, CPP_GREATER, "`>'");
17066 /* Parse a Objective-C visibility specification. */
17069 cp_parser_objc_visibility_spec (cp_parser* parser)
17071 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17073 switch (vis->keyword)
17075 case RID_AT_PRIVATE:
17076 objc_set_visibility (2);
17078 case RID_AT_PROTECTED:
17079 objc_set_visibility (0);
17081 case RID_AT_PUBLIC:
17082 objc_set_visibility (1);
17088 /* Eat '@private'/'@protected'/'@public'. */
17089 cp_lexer_consume_token (parser->lexer);
17092 /* Parse an Objective-C method type. */
17095 cp_parser_objc_method_type (cp_parser* parser)
17097 objc_set_method_type
17098 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17103 /* Parse an Objective-C protocol qualifier. */
17106 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17108 tree quals = NULL_TREE, node;
17109 cp_token *token = cp_lexer_peek_token (parser->lexer);
17111 node = token->value;
17113 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17114 && (node == ridpointers [(int) RID_IN]
17115 || node == ridpointers [(int) RID_OUT]
17116 || node == ridpointers [(int) RID_INOUT]
17117 || node == ridpointers [(int) RID_BYCOPY]
17118 || node == ridpointers [(int) RID_BYREF]
17119 || node == ridpointers [(int) RID_ONEWAY]))
17121 quals = tree_cons (NULL_TREE, node, quals);
17122 cp_lexer_consume_token (parser->lexer);
17123 token = cp_lexer_peek_token (parser->lexer);
17124 node = token->value;
17130 /* Parse an Objective-C typename. */
17133 cp_parser_objc_typename (cp_parser* parser)
17135 tree typename = NULL_TREE;
17137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17139 tree proto_quals, cp_type = NULL_TREE;
17141 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17142 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17144 /* An ObjC type name may consist of just protocol qualifiers, in which
17145 case the type shall default to 'id'. */
17146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17147 cp_type = cp_parser_type_id (parser);
17149 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17150 typename = build_tree_list (proto_quals, cp_type);
17156 /* Check to see if TYPE refers to an Objective-C selector name. */
17159 cp_parser_objc_selector_p (enum cpp_ttype type)
17161 return (type == CPP_NAME || type == CPP_KEYWORD
17162 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17163 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17164 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17165 || type == CPP_XOR || type == CPP_XOR_EQ);
17168 /* Parse an Objective-C selector. */
17171 cp_parser_objc_selector (cp_parser* parser)
17173 cp_token *token = cp_lexer_consume_token (parser->lexer);
17175 if (!cp_parser_objc_selector_p (token->type))
17177 error ("invalid Objective-C++ selector name");
17178 return error_mark_node;
17181 /* C++ operator names are allowed to appear in ObjC selectors. */
17182 switch (token->type)
17184 case CPP_AND_AND: return get_identifier ("and");
17185 case CPP_AND_EQ: return get_identifier ("and_eq");
17186 case CPP_AND: return get_identifier ("bitand");
17187 case CPP_OR: return get_identifier ("bitor");
17188 case CPP_COMPL: return get_identifier ("compl");
17189 case CPP_NOT: return get_identifier ("not");
17190 case CPP_NOT_EQ: return get_identifier ("not_eq");
17191 case CPP_OR_OR: return get_identifier ("or");
17192 case CPP_OR_EQ: return get_identifier ("or_eq");
17193 case CPP_XOR: return get_identifier ("xor");
17194 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17195 default: return token->value;
17199 /* Parse an Objective-C params list. */
17202 cp_parser_objc_method_keyword_params (cp_parser* parser)
17204 tree params = NULL_TREE;
17205 bool maybe_unary_selector_p = true;
17206 cp_token *token = cp_lexer_peek_token (parser->lexer);
17208 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17210 tree selector = NULL_TREE, typename, identifier;
17212 if (token->type != CPP_COLON)
17213 selector = cp_parser_objc_selector (parser);
17215 /* Detect if we have a unary selector. */
17216 if (maybe_unary_selector_p
17217 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17220 maybe_unary_selector_p = false;
17221 cp_parser_require (parser, CPP_COLON, "`:'");
17222 typename = cp_parser_objc_typename (parser);
17223 identifier = cp_parser_identifier (parser);
17227 objc_build_keyword_decl (selector,
17231 token = cp_lexer_peek_token (parser->lexer);
17237 /* Parse the non-keyword Objective-C params. */
17240 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17242 tree params = make_node (TREE_LIST);
17243 cp_token *token = cp_lexer_peek_token (parser->lexer);
17244 *ellipsisp = false; /* Initially, assume no ellipsis. */
17246 while (token->type == CPP_COMMA)
17248 cp_parameter_declarator *parmdecl;
17251 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17252 token = cp_lexer_peek_token (parser->lexer);
17254 if (token->type == CPP_ELLIPSIS)
17256 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17261 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17262 parm = grokdeclarator (parmdecl->declarator,
17263 &parmdecl->decl_specifiers,
17264 PARM, /*initialized=*/0,
17265 /*attrlist=*/NULL);
17267 chainon (params, build_tree_list (NULL_TREE, parm));
17268 token = cp_lexer_peek_token (parser->lexer);
17274 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17277 cp_parser_objc_interstitial_code (cp_parser* parser)
17279 cp_token *token = cp_lexer_peek_token (parser->lexer);
17281 /* If the next token is `extern' and the following token is a string
17282 literal, then we have a linkage specification. */
17283 if (token->keyword == RID_EXTERN
17284 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17285 cp_parser_linkage_specification (parser);
17286 /* Handle #pragma, if any. */
17287 else if (token->type == CPP_PRAGMA)
17288 cp_parser_pragma (parser, pragma_external);
17289 /* Allow stray semicolons. */
17290 else if (token->type == CPP_SEMICOLON)
17291 cp_lexer_consume_token (parser->lexer);
17292 /* Finally, try to parse a block-declaration, or a function-definition. */
17294 cp_parser_block_declaration (parser, /*statement_p=*/false);
17297 /* Parse a method signature. */
17300 cp_parser_objc_method_signature (cp_parser* parser)
17302 tree rettype, kwdparms, optparms;
17303 bool ellipsis = false;
17305 cp_parser_objc_method_type (parser);
17306 rettype = cp_parser_objc_typename (parser);
17307 kwdparms = cp_parser_objc_method_keyword_params (parser);
17308 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17310 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17313 /* Pars an Objective-C method prototype list. */
17316 cp_parser_objc_method_prototype_list (cp_parser* parser)
17318 cp_token *token = cp_lexer_peek_token (parser->lexer);
17320 while (token->keyword != RID_AT_END)
17322 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17324 objc_add_method_declaration
17325 (cp_parser_objc_method_signature (parser));
17326 cp_parser_consume_semicolon_at_end_of_statement (parser);
17329 /* Allow for interspersed non-ObjC++ code. */
17330 cp_parser_objc_interstitial_code (parser);
17332 token = cp_lexer_peek_token (parser->lexer);
17335 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17336 objc_finish_interface ();
17339 /* Parse an Objective-C method definition list. */
17342 cp_parser_objc_method_definition_list (cp_parser* parser)
17344 cp_token *token = cp_lexer_peek_token (parser->lexer);
17346 while (token->keyword != RID_AT_END)
17350 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17352 push_deferring_access_checks (dk_deferred);
17353 objc_start_method_definition
17354 (cp_parser_objc_method_signature (parser));
17356 /* For historical reasons, we accept an optional semicolon. */
17357 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17358 cp_lexer_consume_token (parser->lexer);
17360 perform_deferred_access_checks ();
17361 stop_deferring_access_checks ();
17362 meth = cp_parser_function_definition_after_declarator (parser,
17364 pop_deferring_access_checks ();
17365 objc_finish_method_definition (meth);
17368 /* Allow for interspersed non-ObjC++ code. */
17369 cp_parser_objc_interstitial_code (parser);
17371 token = cp_lexer_peek_token (parser->lexer);
17374 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17375 objc_finish_implementation ();
17378 /* Parse Objective-C ivars. */
17381 cp_parser_objc_class_ivars (cp_parser* parser)
17383 cp_token *token = cp_lexer_peek_token (parser->lexer);
17385 if (token->type != CPP_OPEN_BRACE)
17386 return; /* No ivars specified. */
17388 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17389 token = cp_lexer_peek_token (parser->lexer);
17391 while (token->type != CPP_CLOSE_BRACE)
17393 cp_decl_specifier_seq declspecs;
17394 int decl_class_or_enum_p;
17395 tree prefix_attributes;
17397 cp_parser_objc_visibility_spec (parser);
17399 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17402 cp_parser_decl_specifier_seq (parser,
17403 CP_PARSER_FLAGS_OPTIONAL,
17405 &decl_class_or_enum_p);
17406 prefix_attributes = declspecs.attributes;
17407 declspecs.attributes = NULL_TREE;
17409 /* Keep going until we hit the `;' at the end of the
17411 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17413 tree width = NULL_TREE, attributes, first_attribute, decl;
17414 cp_declarator *declarator = NULL;
17415 int ctor_dtor_or_conv_p;
17417 /* Check for a (possibly unnamed) bitfield declaration. */
17418 token = cp_lexer_peek_token (parser->lexer);
17419 if (token->type == CPP_COLON)
17422 if (token->type == CPP_NAME
17423 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17426 /* Get the name of the bitfield. */
17427 declarator = make_id_declarator (NULL_TREE,
17428 cp_parser_identifier (parser),
17432 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17433 /* Get the width of the bitfield. */
17435 = cp_parser_constant_expression (parser,
17436 /*allow_non_constant=*/false,
17441 /* Parse the declarator. */
17443 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17444 &ctor_dtor_or_conv_p,
17445 /*parenthesized_p=*/NULL,
17446 /*member_p=*/false);
17449 /* Look for attributes that apply to the ivar. */
17450 attributes = cp_parser_attributes_opt (parser);
17451 /* Remember which attributes are prefix attributes and
17453 first_attribute = attributes;
17454 /* Combine the attributes. */
17455 attributes = chainon (prefix_attributes, attributes);
17459 /* Create the bitfield declaration. */
17460 decl = grokbitfield (declarator, &declspecs, width);
17461 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17464 decl = grokfield (declarator, &declspecs,
17465 NULL_TREE, /*init_const_expr_p=*/false,
17466 NULL_TREE, attributes);
17468 /* Add the instance variable. */
17469 objc_add_instance_variable (decl);
17471 /* Reset PREFIX_ATTRIBUTES. */
17472 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17473 attributes = TREE_CHAIN (attributes);
17475 TREE_CHAIN (attributes) = NULL_TREE;
17477 token = cp_lexer_peek_token (parser->lexer);
17479 if (token->type == CPP_COMMA)
17481 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17487 cp_parser_consume_semicolon_at_end_of_statement (parser);
17488 token = cp_lexer_peek_token (parser->lexer);
17491 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17492 /* For historical reasons, we accept an optional semicolon. */
17493 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17494 cp_lexer_consume_token (parser->lexer);
17497 /* Parse an Objective-C protocol declaration. */
17500 cp_parser_objc_protocol_declaration (cp_parser* parser)
17502 tree proto, protorefs;
17505 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17508 error ("identifier expected after %<@protocol%>");
17512 /* See if we have a forward declaration or a definition. */
17513 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17515 /* Try a forward declaration first. */
17516 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17518 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17520 cp_parser_consume_semicolon_at_end_of_statement (parser);
17523 /* Ok, we got a full-fledged definition (or at least should). */
17526 proto = cp_parser_identifier (parser);
17527 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17528 objc_start_protocol (proto, protorefs);
17529 cp_parser_objc_method_prototype_list (parser);
17533 /* Parse an Objective-C superclass or category. */
17536 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17539 cp_token *next = cp_lexer_peek_token (parser->lexer);
17541 *super = *categ = NULL_TREE;
17542 if (next->type == CPP_COLON)
17544 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17545 *super = cp_parser_identifier (parser);
17547 else if (next->type == CPP_OPEN_PAREN)
17549 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17550 *categ = cp_parser_identifier (parser);
17551 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17555 /* Parse an Objective-C class interface. */
17558 cp_parser_objc_class_interface (cp_parser* parser)
17560 tree name, super, categ, protos;
17562 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17563 name = cp_parser_identifier (parser);
17564 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17565 protos = cp_parser_objc_protocol_refs_opt (parser);
17567 /* We have either a class or a category on our hands. */
17569 objc_start_category_interface (name, categ, protos);
17572 objc_start_class_interface (name, super, protos);
17573 /* Handle instance variable declarations, if any. */
17574 cp_parser_objc_class_ivars (parser);
17575 objc_continue_interface ();
17578 cp_parser_objc_method_prototype_list (parser);
17581 /* Parse an Objective-C class implementation. */
17584 cp_parser_objc_class_implementation (cp_parser* parser)
17586 tree name, super, categ;
17588 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17589 name = cp_parser_identifier (parser);
17590 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17592 /* We have either a class or a category on our hands. */
17594 objc_start_category_implementation (name, categ);
17597 objc_start_class_implementation (name, super);
17598 /* Handle instance variable declarations, if any. */
17599 cp_parser_objc_class_ivars (parser);
17600 objc_continue_implementation ();
17603 cp_parser_objc_method_definition_list (parser);
17606 /* Consume the @end token and finish off the implementation. */
17609 cp_parser_objc_end_implementation (cp_parser* parser)
17611 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17612 objc_finish_implementation ();
17615 /* Parse an Objective-C declaration. */
17618 cp_parser_objc_declaration (cp_parser* parser)
17620 /* Try to figure out what kind of declaration is present. */
17621 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17623 switch (kwd->keyword)
17626 cp_parser_objc_alias_declaration (parser);
17629 cp_parser_objc_class_declaration (parser);
17631 case RID_AT_PROTOCOL:
17632 cp_parser_objc_protocol_declaration (parser);
17634 case RID_AT_INTERFACE:
17635 cp_parser_objc_class_interface (parser);
17637 case RID_AT_IMPLEMENTATION:
17638 cp_parser_objc_class_implementation (parser);
17641 cp_parser_objc_end_implementation (parser);
17644 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17645 cp_parser_skip_to_end_of_block_or_statement (parser);
17649 /* Parse an Objective-C try-catch-finally statement.
17651 objc-try-catch-finally-stmt:
17652 @try compound-statement objc-catch-clause-seq [opt]
17653 objc-finally-clause [opt]
17655 objc-catch-clause-seq:
17656 objc-catch-clause objc-catch-clause-seq [opt]
17659 @catch ( exception-declaration ) compound-statement
17661 objc-finally-clause
17662 @finally compound-statement
17664 Returns NULL_TREE. */
17667 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17668 location_t location;
17671 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17672 location = cp_lexer_peek_token (parser->lexer)->location;
17673 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17674 node, lest it get absorbed into the surrounding block. */
17675 stmt = push_stmt_list ();
17676 cp_parser_compound_statement (parser, NULL, false);
17677 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17679 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17681 cp_parameter_declarator *parmdecl;
17684 cp_lexer_consume_token (parser->lexer);
17685 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17686 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17687 parm = grokdeclarator (parmdecl->declarator,
17688 &parmdecl->decl_specifiers,
17689 PARM, /*initialized=*/0,
17690 /*attrlist=*/NULL);
17691 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17692 objc_begin_catch_clause (parm);
17693 cp_parser_compound_statement (parser, NULL, false);
17694 objc_finish_catch_clause ();
17697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17699 cp_lexer_consume_token (parser->lexer);
17700 location = cp_lexer_peek_token (parser->lexer)->location;
17701 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17702 node, lest it get absorbed into the surrounding block. */
17703 stmt = push_stmt_list ();
17704 cp_parser_compound_statement (parser, NULL, false);
17705 objc_build_finally_clause (location, pop_stmt_list (stmt));
17708 return objc_finish_try_stmt ();
17711 /* Parse an Objective-C synchronized statement.
17713 objc-synchronized-stmt:
17714 @synchronized ( expression ) compound-statement
17716 Returns NULL_TREE. */
17719 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17720 location_t location;
17723 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17725 location = cp_lexer_peek_token (parser->lexer)->location;
17726 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17727 lock = cp_parser_expression (parser, false);
17728 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17730 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17731 node, lest it get absorbed into the surrounding block. */
17732 stmt = push_stmt_list ();
17733 cp_parser_compound_statement (parser, NULL, false);
17735 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17738 /* Parse an Objective-C throw statement.
17741 @throw assignment-expression [opt] ;
17743 Returns a constructed '@throw' statement. */
17746 cp_parser_objc_throw_statement (cp_parser *parser) {
17747 tree expr = NULL_TREE;
17749 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17752 expr = cp_parser_assignment_expression (parser, false);
17754 cp_parser_consume_semicolon_at_end_of_statement (parser);
17756 return objc_build_throw_stmt (expr);
17759 /* Parse an Objective-C statement. */
17762 cp_parser_objc_statement (cp_parser * parser) {
17763 /* Try to figure out what kind of declaration is present. */
17764 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17766 switch (kwd->keyword)
17769 return cp_parser_objc_try_catch_finally_statement (parser);
17770 case RID_AT_SYNCHRONIZED:
17771 return cp_parser_objc_synchronized_statement (parser);
17773 return cp_parser_objc_throw_statement (parser);
17775 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17776 cp_parser_skip_to_end_of_block_or_statement (parser);
17779 return error_mark_node;
17782 /* OpenMP 2.5 parsing routines. */
17784 /* All OpenMP clauses. OpenMP 2.5. */
17785 typedef enum pragma_omp_clause {
17786 PRAGMA_OMP_CLAUSE_NONE = 0,
17788 PRAGMA_OMP_CLAUSE_COPYIN,
17789 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17790 PRAGMA_OMP_CLAUSE_DEFAULT,
17791 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17792 PRAGMA_OMP_CLAUSE_IF,
17793 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17794 PRAGMA_OMP_CLAUSE_NOWAIT,
17795 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17796 PRAGMA_OMP_CLAUSE_ORDERED,
17797 PRAGMA_OMP_CLAUSE_PRIVATE,
17798 PRAGMA_OMP_CLAUSE_REDUCTION,
17799 PRAGMA_OMP_CLAUSE_SCHEDULE,
17800 PRAGMA_OMP_CLAUSE_SHARED
17801 } pragma_omp_clause;
17803 /* Returns name of the next clause.
17804 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17805 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17806 returned and the token is consumed. */
17808 static pragma_omp_clause
17809 cp_parser_omp_clause_name (cp_parser *parser)
17811 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17814 result = PRAGMA_OMP_CLAUSE_IF;
17815 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17816 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17817 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17818 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17819 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17821 tree id = cp_lexer_peek_token (parser->lexer)->value;
17822 const char *p = IDENTIFIER_POINTER (id);
17827 if (!strcmp ("copyin", p))
17828 result = PRAGMA_OMP_CLAUSE_COPYIN;
17829 else if (!strcmp ("copyprivate", p))
17830 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17833 if (!strcmp ("firstprivate", p))
17834 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17837 if (!strcmp ("lastprivate", p))
17838 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17841 if (!strcmp ("nowait", p))
17842 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17843 else if (!strcmp ("num_threads", p))
17844 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17847 if (!strcmp ("ordered", p))
17848 result = PRAGMA_OMP_CLAUSE_ORDERED;
17851 if (!strcmp ("reduction", p))
17852 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17855 if (!strcmp ("schedule", p))
17856 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17857 else if (!strcmp ("shared", p))
17858 result = PRAGMA_OMP_CLAUSE_SHARED;
17863 if (result != PRAGMA_OMP_CLAUSE_NONE)
17864 cp_lexer_consume_token (parser->lexer);
17869 /* Validate that a clause of the given type does not already exist. */
17872 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17876 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17877 if (OMP_CLAUSE_CODE (c) == code)
17879 error ("too many %qs clauses", name);
17887 variable-list , identifier
17889 In addition, we match a closing parenthesis. An opening parenthesis
17890 will have been consumed by the caller.
17892 If KIND is nonzero, create the appropriate node and install the decl
17893 in OMP_CLAUSE_DECL and add the node to the head of the list.
17895 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17896 return the list created. */
17899 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17906 name = cp_parser_id_expression (parser, /*template_p=*/false,
17907 /*check_dependency_p=*/true,
17908 /*template_p=*/NULL,
17909 /*declarator_p=*/false,
17910 /*optional_p=*/false);
17911 if (name == error_mark_node)
17914 decl = cp_parser_lookup_name_simple (parser, name);
17915 if (decl == error_mark_node)
17916 cp_parser_name_lookup_error (parser, name, decl, NULL);
17917 else if (kind != 0)
17919 tree u = build_omp_clause (kind);
17920 OMP_CLAUSE_DECL (u) = decl;
17921 OMP_CLAUSE_CHAIN (u) = list;
17925 list = tree_cons (decl, NULL_TREE, list);
17928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17930 cp_lexer_consume_token (parser->lexer);
17933 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
17937 /* Try to resync to an unnested comma. Copied from
17938 cp_parser_parenthesized_expression_list. */
17940 ending = cp_parser_skip_to_closing_parenthesis (parser,
17941 /*recovering=*/true,
17943 /*consume_paren=*/true);
17951 /* Similarly, but expect leading and trailing parenthesis. This is a very
17952 common case for omp clauses. */
17955 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
17957 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17958 return cp_parser_omp_var_list_no_open (parser, kind, list);
17963 default ( shared | none ) */
17966 cp_parser_omp_clause_default (cp_parser *parser, tree list)
17968 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
17971 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
17973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17975 tree id = cp_lexer_peek_token (parser->lexer)->value;
17976 const char *p = IDENTIFIER_POINTER (id);
17981 if (strcmp ("none", p) != 0)
17983 kind = OMP_CLAUSE_DEFAULT_NONE;
17987 if (strcmp ("shared", p) != 0)
17989 kind = OMP_CLAUSE_DEFAULT_SHARED;
17996 cp_lexer_consume_token (parser->lexer);
18001 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18004 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18006 /*or_comma=*/false,
18007 /*consume_paren=*/true);
18009 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18012 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18013 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18014 OMP_CLAUSE_CHAIN (c) = list;
18015 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18021 if ( expression ) */
18024 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18028 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18031 t = cp_parser_condition (parser);
18033 if (t == error_mark_node
18034 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18036 /*or_comma=*/false,
18037 /*consume_paren=*/true);
18039 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18041 c = build_omp_clause (OMP_CLAUSE_IF);
18042 OMP_CLAUSE_IF_EXPR (c) = t;
18043 OMP_CLAUSE_CHAIN (c) = list;
18052 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18056 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18058 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18059 OMP_CLAUSE_CHAIN (c) = list;
18064 num_threads ( expression ) */
18067 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18071 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18074 t = cp_parser_expression (parser, false);
18076 if (t == error_mark_node
18077 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18079 /*or_comma=*/false,
18080 /*consume_paren=*/true);
18082 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18084 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18085 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18086 OMP_CLAUSE_CHAIN (c) = list;
18095 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18099 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18101 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18102 OMP_CLAUSE_CHAIN (c) = list;
18107 reduction ( reduction-operator : variable-list )
18109 reduction-operator:
18110 One of: + * - & ^ | && || */
18113 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18115 enum tree_code code;
18118 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18121 switch (cp_lexer_peek_token (parser->lexer)->type)
18133 code = BIT_AND_EXPR;
18136 code = BIT_XOR_EXPR;
18139 code = BIT_IOR_EXPR;
18142 code = TRUTH_ANDIF_EXPR;
18145 code = TRUTH_ORIF_EXPR;
18148 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18151 /*or_comma=*/false,
18152 /*consume_paren=*/true);
18155 cp_lexer_consume_token (parser->lexer);
18157 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18160 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18161 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18162 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18168 schedule ( schedule-kind )
18169 schedule ( schedule-kind , expression )
18172 static | dynamic | guided | runtime */
18175 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18182 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18186 tree id = cp_lexer_peek_token (parser->lexer)->value;
18187 const char *p = IDENTIFIER_POINTER (id);
18192 if (strcmp ("dynamic", p) != 0)
18194 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18198 if (strcmp ("guided", p) != 0)
18200 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18204 if (strcmp ("runtime", p) != 0)
18206 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18213 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18214 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18217 cp_lexer_consume_token (parser->lexer);
18219 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18221 cp_lexer_consume_token (parser->lexer);
18223 t = cp_parser_assignment_expression (parser, false);
18225 if (t == error_mark_node)
18227 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18228 error ("schedule %<runtime%> does not take "
18229 "a %<chunk_size%> parameter");
18231 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18233 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18236 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18239 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18240 OMP_CLAUSE_CHAIN (c) = list;
18244 cp_parser_error (parser, "invalid schedule kind");
18246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18247 /*or_comma=*/false,
18248 /*consume_paren=*/true);
18252 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18253 is a bitmask in MASK. Return the list of clauses found; the result
18254 of clause default goes in *pdefault. */
18257 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18258 const char *where, cp_token *pragma_tok)
18260 tree clauses = NULL;
18262 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18264 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18265 const char *c_name;
18266 tree prev = clauses;
18270 case PRAGMA_OMP_CLAUSE_COPYIN:
18271 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18274 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18277 c_name = "copyprivate";
18279 case PRAGMA_OMP_CLAUSE_DEFAULT:
18280 clauses = cp_parser_omp_clause_default (parser, clauses);
18281 c_name = "default";
18283 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18286 c_name = "firstprivate";
18288 case PRAGMA_OMP_CLAUSE_IF:
18289 clauses = cp_parser_omp_clause_if (parser, clauses);
18292 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18293 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18295 c_name = "lastprivate";
18297 case PRAGMA_OMP_CLAUSE_NOWAIT:
18298 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18301 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18302 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18303 c_name = "num_threads";
18305 case PRAGMA_OMP_CLAUSE_ORDERED:
18306 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18307 c_name = "ordered";
18309 case PRAGMA_OMP_CLAUSE_PRIVATE:
18310 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18312 c_name = "private";
18314 case PRAGMA_OMP_CLAUSE_REDUCTION:
18315 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18316 c_name = "reduction";
18318 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18319 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18320 c_name = "schedule";
18322 case PRAGMA_OMP_CLAUSE_SHARED:
18323 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18328 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18332 if (((mask >> c_kind) & 1) == 0)
18334 /* Remove the invalid clause(s) from the list to avoid
18335 confusing the rest of the compiler. */
18337 error ("%qs is not valid for %qs", c_name, where);
18341 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18342 return finish_omp_clauses (clauses);
18349 In practice, we're also interested in adding the statement to an
18350 outer node. So it is convenient if we work around the fact that
18351 cp_parser_statement calls add_stmt. */
18354 cp_parser_begin_omp_structured_block (cp_parser *parser)
18356 unsigned save = parser->in_statement;
18358 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18359 This preserves the "not within loop or switch" style error messages
18360 for nonsense cases like
18366 if (parser->in_statement)
18367 parser->in_statement = IN_OMP_BLOCK;
18373 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18375 parser->in_statement = save;
18379 cp_parser_omp_structured_block (cp_parser *parser)
18381 tree stmt = begin_omp_structured_block ();
18382 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18384 cp_parser_statement (parser, NULL_TREE, false);
18386 cp_parser_end_omp_structured_block (parser, save);
18387 return finish_omp_structured_block (stmt);
18391 # pragma omp atomic new-line
18395 x binop= expr | x++ | ++x | x-- | --x
18397 +, *, -, /, &, ^, |, <<, >>
18399 where x is an lvalue expression with scalar type. */
18402 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18405 enum tree_code code;
18407 cp_parser_require_pragma_eol (parser, pragma_tok);
18409 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18411 switch (TREE_CODE (lhs))
18416 case PREINCREMENT_EXPR:
18417 case POSTINCREMENT_EXPR:
18418 lhs = TREE_OPERAND (lhs, 0);
18420 rhs = integer_one_node;
18423 case PREDECREMENT_EXPR:
18424 case POSTDECREMENT_EXPR:
18425 lhs = TREE_OPERAND (lhs, 0);
18427 rhs = integer_one_node;
18431 switch (cp_lexer_peek_token (parser->lexer)->type)
18437 code = TRUNC_DIV_EXPR;
18445 case CPP_LSHIFT_EQ:
18446 code = LSHIFT_EXPR;
18448 case CPP_RSHIFT_EQ:
18449 code = RSHIFT_EXPR;
18452 code = BIT_AND_EXPR;
18455 code = BIT_IOR_EXPR;
18458 code = BIT_XOR_EXPR;
18461 cp_parser_error (parser,
18462 "invalid operator for %<#pragma omp atomic%>");
18465 cp_lexer_consume_token (parser->lexer);
18467 rhs = cp_parser_expression (parser, false);
18468 if (rhs == error_mark_node)
18472 finish_omp_atomic (code, lhs, rhs);
18473 cp_parser_consume_semicolon_at_end_of_statement (parser);
18477 cp_parser_skip_to_end_of_block_or_statement (parser);
18482 # pragma omp barrier new-line */
18485 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18487 cp_parser_require_pragma_eol (parser, pragma_tok);
18488 finish_omp_barrier ();
18492 # pragma omp critical [(name)] new-line
18493 structured-block */
18496 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18498 tree stmt, name = NULL;
18500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18502 cp_lexer_consume_token (parser->lexer);
18504 name = cp_parser_identifier (parser);
18506 if (name == error_mark_node
18507 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18509 /*or_comma=*/false,
18510 /*consume_paren=*/true);
18511 if (name == error_mark_node)
18514 cp_parser_require_pragma_eol (parser, pragma_tok);
18516 stmt = cp_parser_omp_structured_block (parser);
18517 return c_finish_omp_critical (stmt, name);
18521 # pragma omp flush flush-vars[opt] new-line
18524 ( variable-list ) */
18527 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18530 (void) cp_parser_omp_var_list (parser, 0, NULL);
18531 cp_parser_require_pragma_eol (parser, pragma_tok);
18533 finish_omp_flush ();
18536 /* Parse the restricted form of the for statment allowed by OpenMP. */
18539 cp_parser_omp_for_loop (cp_parser *parser)
18541 tree init, cond, incr, body, decl, pre_body;
18544 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18546 cp_parser_error (parser, "for statement expected");
18549 loc = cp_lexer_consume_token (parser->lexer)->location;
18550 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18553 init = decl = NULL;
18554 pre_body = push_stmt_list ();
18555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18557 cp_decl_specifier_seq type_specifiers;
18559 /* First, try to parse as an initialized declaration. See
18560 cp_parser_condition, from whence the bulk of this is copied. */
18562 cp_parser_parse_tentatively (parser);
18563 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18565 if (!cp_parser_error_occurred (parser))
18567 tree asm_specification, attributes;
18568 cp_declarator *declarator;
18570 declarator = cp_parser_declarator (parser,
18571 CP_PARSER_DECLARATOR_NAMED,
18572 /*ctor_dtor_or_conv_p=*/NULL,
18573 /*parenthesized_p=*/NULL,
18574 /*member_p=*/false);
18575 attributes = cp_parser_attributes_opt (parser);
18576 asm_specification = cp_parser_asm_specification_opt (parser);
18578 cp_parser_require (parser, CPP_EQ, "`='");
18579 if (cp_parser_parse_definitely (parser))
18583 decl = start_decl (declarator, &type_specifiers,
18584 /*initialized_p=*/false, attributes,
18585 /*prefix_attributes=*/NULL_TREE,
18588 init = cp_parser_assignment_expression (parser, false);
18590 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18591 asm_specification, LOOKUP_ONLYCONVERTING);
18594 pop_scope (pushed_scope);
18598 cp_parser_abort_tentative_parse (parser);
18600 /* If parsing as an initialized declaration failed, try again as
18601 a simple expression. */
18603 init = cp_parser_expression (parser, false);
18605 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18606 pre_body = pop_stmt_list (pre_body);
18609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18610 cond = cp_parser_condition (parser);
18611 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18615 incr = cp_parser_expression (parser, false);
18617 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18619 /*or_comma=*/false,
18620 /*consume_paren=*/true);
18622 /* Note that we saved the original contents of this flag when we entered
18623 the structured block, and so we don't need to re-save it here. */
18624 parser->in_statement = IN_OMP_FOR;
18626 /* Note that the grammar doesn't call for a structured block here,
18627 though the loop as a whole is a structured block. */
18628 body = push_stmt_list ();
18629 cp_parser_statement (parser, NULL_TREE, false);
18630 body = pop_stmt_list (body);
18632 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18636 #pragma omp for for-clause[optseq] new-line
18639 #define OMP_FOR_CLAUSE_MASK \
18640 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18641 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18642 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18643 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18644 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18645 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18646 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18649 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18651 tree clauses, sb, ret;
18654 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18655 "#pragma omp for", pragma_tok);
18657 sb = begin_omp_structured_block ();
18658 save = cp_parser_begin_omp_structured_block (parser);
18660 ret = cp_parser_omp_for_loop (parser);
18662 OMP_FOR_CLAUSES (ret) = clauses;
18664 cp_parser_end_omp_structured_block (parser, save);
18665 add_stmt (finish_omp_structured_block (sb));
18671 # pragma omp master new-line
18672 structured-block */
18675 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18677 cp_parser_require_pragma_eol (parser, pragma_tok);
18678 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18682 # pragma omp ordered new-line
18683 structured-block */
18686 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18688 cp_parser_require_pragma_eol (parser, pragma_tok);
18689 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18695 { section-sequence }
18698 section-directive[opt] structured-block
18699 section-sequence section-directive structured-block */
18702 cp_parser_omp_sections_scope (cp_parser *parser)
18704 tree stmt, substmt;
18705 bool error_suppress = false;
18708 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18711 stmt = push_stmt_list ();
18713 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18717 substmt = begin_omp_structured_block ();
18718 save = cp_parser_begin_omp_structured_block (parser);
18722 cp_parser_statement (parser, NULL_TREE, false);
18724 tok = cp_lexer_peek_token (parser->lexer);
18725 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18727 if (tok->type == CPP_CLOSE_BRACE)
18729 if (tok->type == CPP_EOF)
18733 cp_parser_end_omp_structured_block (parser, save);
18734 substmt = finish_omp_structured_block (substmt);
18735 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18736 add_stmt (substmt);
18741 tok = cp_lexer_peek_token (parser->lexer);
18742 if (tok->type == CPP_CLOSE_BRACE)
18744 if (tok->type == CPP_EOF)
18747 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18749 cp_lexer_consume_token (parser->lexer);
18750 cp_parser_require_pragma_eol (parser, tok);
18751 error_suppress = false;
18753 else if (!error_suppress)
18755 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18756 error_suppress = true;
18759 substmt = cp_parser_omp_structured_block (parser);
18760 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18761 add_stmt (substmt);
18763 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18765 substmt = pop_stmt_list (stmt);
18767 stmt = make_node (OMP_SECTIONS);
18768 TREE_TYPE (stmt) = void_type_node;
18769 OMP_SECTIONS_BODY (stmt) = substmt;
18776 # pragma omp sections sections-clause[optseq] newline
18779 #define OMP_SECTIONS_CLAUSE_MASK \
18780 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18781 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18782 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18783 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18784 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18787 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18791 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18792 "#pragma omp sections", pragma_tok);
18794 ret = cp_parser_omp_sections_scope (parser);
18796 OMP_SECTIONS_CLAUSES (ret) = clauses;
18802 # pragma parallel parallel-clause new-line
18803 # pragma parallel for parallel-for-clause new-line
18804 # pragma parallel sections parallel-sections-clause new-line */
18806 #define OMP_PARALLEL_CLAUSE_MASK \
18807 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18808 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18809 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18810 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18811 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18812 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18813 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18814 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18817 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18819 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18820 const char *p_name = "#pragma omp parallel";
18821 tree stmt, clauses, par_clause, ws_clause, block;
18822 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18827 cp_lexer_consume_token (parser->lexer);
18828 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18829 p_name = "#pragma omp parallel for";
18830 mask |= OMP_FOR_CLAUSE_MASK;
18831 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18833 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18835 tree id = cp_lexer_peek_token (parser->lexer)->value;
18836 const char *p = IDENTIFIER_POINTER (id);
18837 if (strcmp (p, "sections") == 0)
18839 cp_lexer_consume_token (parser->lexer);
18840 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18841 p_name = "#pragma omp parallel sections";
18842 mask |= OMP_SECTIONS_CLAUSE_MASK;
18843 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18847 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18848 block = begin_omp_parallel ();
18849 save = cp_parser_begin_omp_structured_block (parser);
18853 case PRAGMA_OMP_PARALLEL:
18854 cp_parser_already_scoped_statement (parser);
18855 par_clause = clauses;
18858 case PRAGMA_OMP_PARALLEL_FOR:
18859 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18860 stmt = cp_parser_omp_for_loop (parser);
18862 OMP_FOR_CLAUSES (stmt) = ws_clause;
18865 case PRAGMA_OMP_PARALLEL_SECTIONS:
18866 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18867 stmt = cp_parser_omp_sections_scope (parser);
18869 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18873 gcc_unreachable ();
18876 cp_parser_end_omp_structured_block (parser, save);
18877 stmt = finish_omp_parallel (par_clause, block);
18878 if (p_kind != PRAGMA_OMP_PARALLEL)
18879 OMP_PARALLEL_COMBINED (stmt) = 1;
18884 # pragma omp single single-clause[optseq] new-line
18885 structured-block */
18887 #define OMP_SINGLE_CLAUSE_MASK \
18888 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18889 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18890 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18891 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18894 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18896 tree stmt = make_node (OMP_SINGLE);
18897 TREE_TYPE (stmt) = void_type_node;
18899 OMP_SINGLE_CLAUSES (stmt)
18900 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18901 "#pragma omp single", pragma_tok);
18902 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18904 return add_stmt (stmt);
18908 # pragma omp threadprivate (variable-list) */
18911 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18915 vars = cp_parser_omp_var_list (parser, 0, NULL);
18916 cp_parser_require_pragma_eol (parser, pragma_tok);
18918 if (!targetm.have_tls)
18919 sorry ("threadprivate variables not supported in this target");
18921 finish_omp_threadprivate (vars);
18924 /* Main entry point to OpenMP statement pragmas. */
18927 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18931 switch (pragma_tok->pragma_kind)
18933 case PRAGMA_OMP_ATOMIC:
18934 cp_parser_omp_atomic (parser, pragma_tok);
18936 case PRAGMA_OMP_CRITICAL:
18937 stmt = cp_parser_omp_critical (parser, pragma_tok);
18939 case PRAGMA_OMP_FOR:
18940 stmt = cp_parser_omp_for (parser, pragma_tok);
18942 case PRAGMA_OMP_MASTER:
18943 stmt = cp_parser_omp_master (parser, pragma_tok);
18945 case PRAGMA_OMP_ORDERED:
18946 stmt = cp_parser_omp_ordered (parser, pragma_tok);
18948 case PRAGMA_OMP_PARALLEL:
18949 stmt = cp_parser_omp_parallel (parser, pragma_tok);
18951 case PRAGMA_OMP_SECTIONS:
18952 stmt = cp_parser_omp_sections (parser, pragma_tok);
18954 case PRAGMA_OMP_SINGLE:
18955 stmt = cp_parser_omp_single (parser, pragma_tok);
18958 gcc_unreachable ();
18962 SET_EXPR_LOCATION (stmt, pragma_tok->location);
18967 static GTY (()) cp_parser *the_parser;
18970 /* Special handling for the first token or line in the file. The first
18971 thing in the file might be #pragma GCC pch_preprocess, which loads a
18972 PCH file, which is a GC collection point. So we need to handle this
18973 first pragma without benefit of an existing lexer structure.
18975 Always returns one token to the caller in *FIRST_TOKEN. This is
18976 either the true first token of the file, or the first token after
18977 the initial pragma. */
18980 cp_parser_initial_pragma (cp_token *first_token)
18984 cp_lexer_get_preprocessor_token (NULL, first_token);
18985 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
18988 cp_lexer_get_preprocessor_token (NULL, first_token);
18989 if (first_token->type == CPP_STRING)
18991 name = first_token->value;
18993 cp_lexer_get_preprocessor_token (NULL, first_token);
18994 if (first_token->type != CPP_PRAGMA_EOL)
18995 error ("junk at end of %<#pragma GCC pch_preprocess%>");
18998 error ("expected string literal");
19000 /* Skip to the end of the pragma. */
19001 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19002 cp_lexer_get_preprocessor_token (NULL, first_token);
19004 /* Now actually load the PCH file. */
19006 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19008 /* Read one more token to return to our caller. We have to do this
19009 after reading the PCH file in, since its pointers have to be
19011 cp_lexer_get_preprocessor_token (NULL, first_token);
19014 /* Normal parsing of a pragma token. Here we can (and must) use the
19018 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19020 cp_token *pragma_tok;
19023 pragma_tok = cp_lexer_consume_token (parser->lexer);
19024 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19025 parser->lexer->in_pragma = true;
19027 id = pragma_tok->pragma_kind;
19030 case PRAGMA_GCC_PCH_PREPROCESS:
19031 error ("%<#pragma GCC pch_preprocess%> must be first");
19034 case PRAGMA_OMP_BARRIER:
19037 case pragma_compound:
19038 cp_parser_omp_barrier (parser, pragma_tok);
19041 error ("%<#pragma omp barrier%> may only be "
19042 "used in compound statements");
19049 case PRAGMA_OMP_FLUSH:
19052 case pragma_compound:
19053 cp_parser_omp_flush (parser, pragma_tok);
19056 error ("%<#pragma omp flush%> may only be "
19057 "used in compound statements");
19064 case PRAGMA_OMP_THREADPRIVATE:
19065 cp_parser_omp_threadprivate (parser, pragma_tok);
19068 case PRAGMA_OMP_ATOMIC:
19069 case PRAGMA_OMP_CRITICAL:
19070 case PRAGMA_OMP_FOR:
19071 case PRAGMA_OMP_MASTER:
19072 case PRAGMA_OMP_ORDERED:
19073 case PRAGMA_OMP_PARALLEL:
19074 case PRAGMA_OMP_SECTIONS:
19075 case PRAGMA_OMP_SINGLE:
19076 if (context == pragma_external)
19078 cp_parser_omp_construct (parser, pragma_tok);
19081 case PRAGMA_OMP_SECTION:
19082 error ("%<#pragma omp section%> may only be used in "
19083 "%<#pragma omp sections%> construct");
19087 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19088 c_invoke_pragma_handler (id);
19092 cp_parser_error (parser, "expected declaration specifiers");
19096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19100 /* The interface the pragma parsers have to the lexer. */
19103 pragma_lex (tree *value)
19106 enum cpp_ttype ret;
19108 tok = cp_lexer_peek_token (the_parser->lexer);
19111 *value = tok->value;
19113 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19115 else if (ret == CPP_STRING)
19116 *value = cp_parser_string_literal (the_parser, false, false);
19119 cp_lexer_consume_token (the_parser->lexer);
19120 if (ret == CPP_KEYWORD)
19128 /* External interface. */
19130 /* Parse one entire translation unit. */
19133 c_parse_file (void)
19135 bool error_occurred;
19136 static bool already_called = false;
19138 if (already_called)
19140 sorry ("inter-module optimizations not implemented for C++");
19143 already_called = true;
19145 the_parser = cp_parser_new ();
19146 push_deferring_access_checks (flag_access_control
19147 ? dk_no_deferred : dk_no_check);
19148 error_occurred = cp_parser_translation_unit (the_parser);
19152 /* This variable must be provided by every front end. */
19156 #include "gt-cp-parser.h"