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 input file stack index at which this token was found. */
70 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
71 /* The value associated with this token, if any. */
73 /* The location at which this token was found. */
77 /* We use a stack of token pointer for saving token sets. */
78 typedef struct cp_token *cp_token_position;
79 DEF_VEC_P (cp_token_position);
80 DEF_VEC_ALLOC_P (cp_token_position,heap);
82 static const cp_token eof_token =
84 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, NULL_TREE,
85 #if USE_MAPPED_LOCATION
92 /* The cp_lexer structure represents the C++ lexer. It is responsible
93 for managing the token stream from the preprocessor and supplying
94 it to the parser. Tokens are never added to the cp_lexer after
97 typedef struct cp_lexer GTY (())
99 /* The memory allocated for the buffer. NULL if this lexer does not
100 own the token buffer. */
101 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
102 /* If the lexer owns the buffer, this is the number of tokens in the
104 size_t buffer_length;
106 /* A pointer just past the last available token. The tokens
107 in this lexer are [buffer, last_token). */
108 cp_token_position GTY ((skip)) last_token;
110 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
111 no more available tokens. */
112 cp_token_position GTY ((skip)) next_token;
114 /* A stack indicating positions at which cp_lexer_save_tokens was
115 called. The top entry is the most recent position at which we
116 began saving tokens. If the stack is non-empty, we are saving
118 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
120 /* The next lexer in a linked list of lexers. */
121 struct cp_lexer *next;
123 /* True if we should output debugging information. */
126 /* True if we're in the context of parsing a pragma, and should not
127 increment past the end-of-line marker. */
131 /* cp_token_cache is a range of tokens. There is no need to represent
132 allocate heap memory for it, since tokens are never removed from the
133 lexer's array. There is also no need for the GC to walk through
134 a cp_token_cache, since everything in here is referenced through
137 typedef struct cp_token_cache GTY(())
139 /* The beginning of the token range. */
140 cp_token * GTY((skip)) first;
142 /* Points immediately after the last token in the range. */
143 cp_token * GTY ((skip)) last;
148 static cp_lexer *cp_lexer_new_main
150 static cp_lexer *cp_lexer_new_from_tokens
151 (cp_token_cache *tokens);
152 static void cp_lexer_destroy
154 static int cp_lexer_saving_tokens
156 static cp_token_position cp_lexer_token_position
158 static cp_token *cp_lexer_token_at
159 (cp_lexer *, cp_token_position);
160 static void cp_lexer_get_preprocessor_token
161 (cp_lexer *, cp_token *);
162 static inline cp_token *cp_lexer_peek_token
164 static cp_token *cp_lexer_peek_nth_token
165 (cp_lexer *, size_t);
166 static inline bool cp_lexer_next_token_is
167 (cp_lexer *, enum cpp_ttype);
168 static bool cp_lexer_next_token_is_not
169 (cp_lexer *, enum cpp_ttype);
170 static bool cp_lexer_next_token_is_keyword
171 (cp_lexer *, enum rid);
172 static cp_token *cp_lexer_consume_token
174 static void cp_lexer_purge_token
176 static void cp_lexer_purge_tokens_after
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_save_tokens
180 static void cp_lexer_commit_tokens
182 static void cp_lexer_rollback_tokens
184 #ifdef ENABLE_CHECKING
185 static void cp_lexer_print_token
186 (FILE *, cp_token *);
187 static inline bool cp_lexer_debugging_p
189 static void cp_lexer_start_debugging
190 (cp_lexer *) ATTRIBUTE_UNUSED;
191 static void cp_lexer_stop_debugging
192 (cp_lexer *) ATTRIBUTE_UNUSED;
194 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
195 about passing NULL to functions that require non-NULL arguments
196 (fputs, fprintf). It will never be used, so all we need is a value
197 of the right type that's guaranteed not to be NULL. */
198 #define cp_lexer_debug_stream stdout
199 #define cp_lexer_print_token(str, tok) (void) 0
200 #define cp_lexer_debugging_p(lexer) 0
201 #endif /* ENABLE_CHECKING */
203 static cp_token_cache *cp_token_cache_new
204 (cp_token *, cp_token *);
206 static void cp_parser_initial_pragma
209 /* Manifest constants. */
210 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
211 #define CP_SAVED_TOKEN_STACK 5
213 /* A token type for keywords, as opposed to ordinary identifiers. */
214 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
216 /* A token type for template-ids. If a template-id is processed while
217 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
218 the value of the CPP_TEMPLATE_ID is whatever was returned by
219 cp_parser_template_id. */
220 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
222 /* A token type for nested-name-specifiers. If a
223 nested-name-specifier is processed while parsing tentatively, it is
224 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
225 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
226 cp_parser_nested_name_specifier_opt. */
227 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
229 /* A token type for tokens that are not tokens at all; these are used
230 to represent slots in the array where there used to be a token
231 that has now been deleted. */
232 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
234 /* The number of token types, including C++-specific ones. */
235 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
239 #ifdef ENABLE_CHECKING
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
242 #endif /* ENABLE_CHECKING */
244 /* Create a new main C++ lexer, the lexer that gets tokens from the
248 cp_lexer_new_main (void)
250 cp_token first_token;
257 /* It's possible that parsing the first pragma will load a PCH file,
258 which is a GC collection point. So we have to do that before
259 allocating any memory. */
260 cp_parser_initial_pragma (&first_token);
262 /* Tell c_lex_with_flags not to merge string constants. */
263 c_lex_return_raw_strings = true;
265 c_common_no_more_pch ();
267 /* Allocate the memory. */
268 lexer = GGC_CNEW (cp_lexer);
270 #ifdef ENABLE_CHECKING
271 /* Initially we are not debugging. */
272 lexer->debugging_p = false;
273 #endif /* ENABLE_CHECKING */
274 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
275 CP_SAVED_TOKEN_STACK);
277 /* Create the buffer. */
278 alloc = CP_LEXER_BUFFER_SIZE;
279 buffer = GGC_NEWVEC (cp_token, alloc);
281 /* Put the first token in the buffer. */
286 /* Get the remaining tokens from the preprocessor. */
287 while (pos->type != CPP_EOF)
294 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
295 pos = buffer + space;
297 cp_lexer_get_preprocessor_token (lexer, pos);
299 lexer->buffer = buffer;
300 lexer->buffer_length = alloc - space;
301 lexer->last_token = pos;
302 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
304 /* Subsequent preprocessor diagnostics should use compiler
305 diagnostic functions to get the compiler source location. */
306 cpp_get_options (parse_in)->client_diagnostic = true;
307 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
309 gcc_assert (lexer->next_token->type != CPP_PURGED);
313 /* Create a new lexer whose token stream is primed with the tokens in
314 CACHE. When these tokens are exhausted, no new tokens will be read. */
317 cp_lexer_new_from_tokens (cp_token_cache *cache)
319 cp_token *first = cache->first;
320 cp_token *last = cache->last;
321 cp_lexer *lexer = GGC_CNEW (cp_lexer);
323 /* We do not own the buffer. */
324 lexer->buffer = NULL;
325 lexer->buffer_length = 0;
326 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
327 lexer->last_token = last;
329 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
330 CP_SAVED_TOKEN_STACK);
332 #ifdef ENABLE_CHECKING
333 /* Initially we are not debugging. */
334 lexer->debugging_p = false;
337 gcc_assert (lexer->next_token->type != CPP_PURGED);
341 /* Frees all resources associated with LEXER. */
344 cp_lexer_destroy (cp_lexer *lexer)
347 ggc_free (lexer->buffer);
348 VEC_free (cp_token_position, heap, lexer->saved_tokens);
352 /* Returns nonzero if debugging information should be output. */
354 #ifdef ENABLE_CHECKING
357 cp_lexer_debugging_p (cp_lexer *lexer)
359 return lexer->debugging_p;
362 #endif /* ENABLE_CHECKING */
364 static inline cp_token_position
365 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
367 gcc_assert (!previous_p || lexer->next_token != &eof_token);
369 return lexer->next_token - previous_p;
372 static inline cp_token *
373 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
378 /* nonzero if we are presently saving tokens. */
381 cp_lexer_saving_tokens (const cp_lexer* lexer)
383 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
386 /* Store the next token from the preprocessor in *TOKEN. Return true
390 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
393 static int is_extern_c = 0;
395 /* Get a new token from the preprocessor. */
397 = c_lex_with_flags (&token->value, &token->location, &token->flags);
398 token->input_file_stack_index = input_file_stack_tick;
399 token->keyword = RID_MAX;
400 token->pragma_kind = PRAGMA_NONE;
401 token->in_system_header = in_system_header;
403 /* On some systems, some header files are surrounded by an
404 implicit extern "C" block. Set a flag in the token if it
405 comes from such a header. */
406 is_extern_c += pending_lang_change;
407 pending_lang_change = 0;
408 token->implicit_extern_c = is_extern_c > 0;
410 /* Check to see if this token is a keyword. */
411 if (token->type == CPP_NAME)
413 if (C_IS_RESERVED_WORD (token->value))
415 /* Mark this token as a keyword. */
416 token->type = CPP_KEYWORD;
417 /* Record which keyword. */
418 token->keyword = C_RID_CODE (token->value);
419 /* Update the value. Some keywords are mapped to particular
420 entities, rather than simply having the value of the
421 corresponding IDENTIFIER_NODE. For example, `__const' is
422 mapped to `const'. */
423 token->value = ridpointers[token->keyword];
427 token->ambiguous_p = false;
428 token->keyword = RID_MAX;
431 /* Handle Objective-C++ keywords. */
432 else if (token->type == CPP_AT_NAME)
434 token->type = CPP_KEYWORD;
435 switch (C_RID_CODE (token->value))
437 /* Map 'class' to '@class', 'private' to '@private', etc. */
438 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
439 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
440 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
441 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
442 case RID_THROW: token->keyword = RID_AT_THROW; break;
443 case RID_TRY: token->keyword = RID_AT_TRY; break;
444 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
445 default: token->keyword = C_RID_CODE (token->value);
448 else if (token->type == CPP_PRAGMA)
450 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
451 token->pragma_kind = TREE_INT_CST_LOW (token->value);
456 /* Update the globals input_location and in_system_header and the
457 input file stack from TOKEN. */
459 cp_lexer_set_source_position_from_token (cp_token *token)
461 if (token->type != CPP_EOF)
463 input_location = token->location;
464 in_system_header = token->in_system_header;
465 restore_input_file_stack (token->input_file_stack_index);
469 /* Return a pointer to the next token in the token stream, but do not
472 static inline cp_token *
473 cp_lexer_peek_token (cp_lexer *lexer)
475 if (cp_lexer_debugging_p (lexer))
477 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
478 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
479 putc ('\n', cp_lexer_debug_stream);
481 return lexer->next_token;
484 /* Return true if the next token has the indicated TYPE. */
487 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
489 return cp_lexer_peek_token (lexer)->type == type;
492 /* Return true if the next token does not have the indicated TYPE. */
495 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
497 return !cp_lexer_next_token_is (lexer, type);
500 /* Return true if the next token is the indicated KEYWORD. */
503 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
505 return cp_lexer_peek_token (lexer)->keyword == keyword;
508 /* Return a pointer to the Nth token in the token stream. If N is 1,
509 then this is precisely equivalent to cp_lexer_peek_token (except
510 that it is not inline). One would like to disallow that case, but
511 there is one case (cp_parser_nth_token_starts_template_id) where
512 the caller passes a variable for N and it might be 1. */
515 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
519 /* N is 1-based, not zero-based. */
522 if (cp_lexer_debugging_p (lexer))
523 fprintf (cp_lexer_debug_stream,
524 "cp_lexer: peeking ahead %ld at token: ", (long)n);
527 token = lexer->next_token;
528 gcc_assert (!n || token != &eof_token);
532 if (token == lexer->last_token)
534 token = (cp_token *)&eof_token;
538 if (token->type != CPP_PURGED)
542 if (cp_lexer_debugging_p (lexer))
544 cp_lexer_print_token (cp_lexer_debug_stream, token);
545 putc ('\n', cp_lexer_debug_stream);
551 /* Return the next token, and advance the lexer's next_token pointer
552 to point to the next non-purged token. */
555 cp_lexer_consume_token (cp_lexer* lexer)
557 cp_token *token = lexer->next_token;
559 gcc_assert (token != &eof_token);
560 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
565 if (lexer->next_token == lexer->last_token)
567 lexer->next_token = (cp_token *)&eof_token;
572 while (lexer->next_token->type == CPP_PURGED);
574 cp_lexer_set_source_position_from_token (token);
576 /* Provide debugging output. */
577 if (cp_lexer_debugging_p (lexer))
579 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
580 cp_lexer_print_token (cp_lexer_debug_stream, token);
581 putc ('\n', cp_lexer_debug_stream);
587 /* Permanently remove the next token from the token stream, and
588 advance the next_token pointer to refer to the next non-purged
592 cp_lexer_purge_token (cp_lexer *lexer)
594 cp_token *tok = lexer->next_token;
596 gcc_assert (tok != &eof_token);
597 tok->type = CPP_PURGED;
598 tok->location = UNKNOWN_LOCATION;
599 tok->value = NULL_TREE;
600 tok->keyword = RID_MAX;
605 if (tok == lexer->last_token)
607 tok = (cp_token *)&eof_token;
611 while (tok->type == CPP_PURGED);
612 lexer->next_token = tok;
615 /* Permanently remove all tokens after TOK, up to, but not
616 including, the token that will be returned next by
617 cp_lexer_peek_token. */
620 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
622 cp_token *peek = lexer->next_token;
624 if (peek == &eof_token)
625 peek = lexer->last_token;
627 gcc_assert (tok < peek);
629 for ( tok += 1; tok != peek; tok += 1)
631 tok->type = CPP_PURGED;
632 tok->location = UNKNOWN_LOCATION;
633 tok->value = NULL_TREE;
634 tok->keyword = RID_MAX;
638 /* Begin saving tokens. All tokens consumed after this point will be
642 cp_lexer_save_tokens (cp_lexer* lexer)
644 /* Provide debugging output. */
645 if (cp_lexer_debugging_p (lexer))
646 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
648 VEC_safe_push (cp_token_position, heap,
649 lexer->saved_tokens, lexer->next_token);
652 /* Commit to the portion of the token stream most recently saved. */
655 cp_lexer_commit_tokens (cp_lexer* lexer)
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
661 VEC_pop (cp_token_position, lexer->saved_tokens);
664 /* Return all tokens saved since the last call to cp_lexer_save_tokens
665 to the token stream. Stop saving tokens. */
668 cp_lexer_rollback_tokens (cp_lexer* lexer)
670 /* Provide debugging output. */
671 if (cp_lexer_debugging_p (lexer))
672 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
674 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
677 /* Print a representation of the TOKEN on the STREAM. */
679 #ifdef ENABLE_CHECKING
682 cp_lexer_print_token (FILE * stream, cp_token *token)
684 /* We don't use cpp_type2name here because the parser defines
685 a few tokens of its own. */
686 static const char *const token_names[] = {
687 /* cpplib-defined token types */
693 /* C++ parser token types - see "Manifest constants", above. */
696 "NESTED_NAME_SPECIFIER",
700 /* If we have a name for the token, print it out. Otherwise, we
701 simply give the numeric code. */
702 gcc_assert (token->type < ARRAY_SIZE(token_names));
703 fputs (token_names[token->type], stream);
705 /* For some tokens, print the associated data. */
709 /* Some keywords have a value that is not an IDENTIFIER_NODE.
710 For example, `struct' is mapped to an INTEGER_CST. */
711 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
713 /* else fall through */
715 fputs (IDENTIFIER_POINTER (token->value), stream);
720 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
728 /* Start emitting debugging information. */
731 cp_lexer_start_debugging (cp_lexer* lexer)
733 lexer->debugging_p = true;
736 /* Stop emitting debugging information. */
739 cp_lexer_stop_debugging (cp_lexer* lexer)
741 lexer->debugging_p = false;
744 #endif /* ENABLE_CHECKING */
746 /* Create a new cp_token_cache, representing a range of tokens. */
748 static cp_token_cache *
749 cp_token_cache_new (cp_token *first, cp_token *last)
751 cp_token_cache *cache = GGC_NEW (cp_token_cache);
752 cache->first = first;
758 /* Decl-specifiers. */
760 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
763 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
770 /* Nothing other than the parser should be creating declarators;
771 declarators are a semi-syntactic representation of C++ entities.
772 Other parts of the front end that need to create entities (like
773 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
775 static cp_declarator *make_call_declarator
776 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
777 static cp_declarator *make_array_declarator
778 (cp_declarator *, tree);
779 static cp_declarator *make_pointer_declarator
780 (cp_cv_quals, cp_declarator *);
781 static cp_declarator *make_reference_declarator
782 (cp_cv_quals, cp_declarator *);
783 static cp_parameter_declarator *make_parameter_declarator
784 (cp_decl_specifier_seq *, cp_declarator *, tree);
785 static cp_declarator *make_ptrmem_declarator
786 (cp_cv_quals, tree, cp_declarator *);
788 /* An erroneous declarator. */
789 static cp_declarator *cp_error_declarator;
791 /* The obstack on which declarators and related data structures are
793 static struct obstack declarator_obstack;
795 /* Alloc BYTES from the declarator memory pool. */
798 alloc_declarator (size_t bytes)
800 return obstack_alloc (&declarator_obstack, bytes);
803 /* Allocate a declarator of the indicated KIND. Clear fields that are
804 common to all declarators. */
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
809 cp_declarator *declarator;
811 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812 declarator->kind = kind;
813 declarator->attributes = NULL_TREE;
814 declarator->declarator = NULL;
819 /* Make a declarator for a generalized identifier. If
820 QUALIFYING_SCOPE is non-NULL, the identifier is
821 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
822 UNQUALIFIED_NAME. SFK indicates the kind of special function this
825 static cp_declarator *
826 make_id_declarator (tree qualifying_scope, tree unqualified_name,
827 special_function_kind sfk)
829 cp_declarator *declarator;
831 /* It is valid to write:
833 class C { void f(); };
837 The standard is not clear about whether `typedef const C D' is
838 legal; as of 2002-09-15 the committee is considering that
839 question. EDG 3.0 allows that syntax. Therefore, we do as
841 if (qualifying_scope && TYPE_P (qualifying_scope))
842 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
844 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
845 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
846 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
848 declarator = make_declarator (cdk_id);
849 declarator->u.id.qualifying_scope = qualifying_scope;
850 declarator->u.id.unqualified_name = unqualified_name;
851 declarator->u.id.sfk = sfk;
856 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
857 of modifiers such as const or volatile to apply to the pointer
858 type, represented as identifiers. */
861 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
863 cp_declarator *declarator;
865 declarator = make_declarator (cdk_pointer);
866 declarator->declarator = target;
867 declarator->u.pointer.qualifiers = cv_qualifiers;
868 declarator->u.pointer.class_type = NULL_TREE;
873 /* Like make_pointer_declarator -- but for references. */
876 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
878 cp_declarator *declarator;
880 declarator = make_declarator (cdk_reference);
881 declarator->declarator = target;
882 declarator->u.pointer.qualifiers = cv_qualifiers;
883 declarator->u.pointer.class_type = NULL_TREE;
888 /* Like make_pointer_declarator -- but for a pointer to a non-static
889 member of CLASS_TYPE. */
892 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
893 cp_declarator *pointee)
895 cp_declarator *declarator;
897 declarator = make_declarator (cdk_ptrmem);
898 declarator->declarator = pointee;
899 declarator->u.pointer.qualifiers = cv_qualifiers;
900 declarator->u.pointer.class_type = class_type;
905 /* Make a declarator for the function given by TARGET, with the
906 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
907 "const"-qualified member function. The EXCEPTION_SPECIFICATION
908 indicates what exceptions can be thrown. */
911 make_call_declarator (cp_declarator *target,
912 cp_parameter_declarator *parms,
913 cp_cv_quals cv_qualifiers,
914 tree exception_specification)
916 cp_declarator *declarator;
918 declarator = make_declarator (cdk_function);
919 declarator->declarator = target;
920 declarator->u.function.parameters = parms;
921 declarator->u.function.qualifiers = cv_qualifiers;
922 declarator->u.function.exception_specification = exception_specification;
927 /* Make a declarator for an array of BOUNDS elements, each of which is
928 defined by ELEMENT. */
931 make_array_declarator (cp_declarator *element, tree bounds)
933 cp_declarator *declarator;
935 declarator = make_declarator (cdk_array);
936 declarator->declarator = element;
937 declarator->u.array.bounds = bounds;
942 cp_parameter_declarator *no_parameters;
944 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
945 DECLARATOR and DEFAULT_ARGUMENT. */
947 cp_parameter_declarator *
948 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
949 cp_declarator *declarator,
950 tree default_argument)
952 cp_parameter_declarator *parameter;
954 parameter = ((cp_parameter_declarator *)
955 alloc_declarator (sizeof (cp_parameter_declarator)));
956 parameter->next = NULL;
958 parameter->decl_specifiers = *decl_specifiers;
960 clear_decl_specs (¶meter->decl_specifiers);
961 parameter->declarator = declarator;
962 parameter->default_argument = default_argument;
963 parameter->ellipsis_p = false;
973 A cp_parser parses the token stream as specified by the C++
974 grammar. Its job is purely parsing, not semantic analysis. For
975 example, the parser breaks the token stream into declarators,
976 expressions, statements, and other similar syntactic constructs.
977 It does not check that the types of the expressions on either side
978 of an assignment-statement are compatible, or that a function is
979 not declared with a parameter of type `void'.
981 The parser invokes routines elsewhere in the compiler to perform
982 semantic analysis and to build up the abstract syntax tree for the
985 The parser (and the template instantiation code, which is, in a
986 way, a close relative of parsing) are the only parts of the
987 compiler that should be calling push_scope and pop_scope, or
988 related functions. The parser (and template instantiation code)
989 keeps track of what scope is presently active; everything else
990 should simply honor that. (The code that generates static
991 initializers may also need to set the scope, in order to check
992 access control correctly when emitting the initializers.)
997 The parser is of the standard recursive-descent variety. Upcoming
998 tokens in the token stream are examined in order to determine which
999 production to use when parsing a non-terminal. Some C++ constructs
1000 require arbitrary look ahead to disambiguate. For example, it is
1001 impossible, in the general case, to tell whether a statement is an
1002 expression or declaration without scanning the entire statement.
1003 Therefore, the parser is capable of "parsing tentatively." When the
1004 parser is not sure what construct comes next, it enters this mode.
1005 Then, while we attempt to parse the construct, the parser queues up
1006 error messages, rather than issuing them immediately, and saves the
1007 tokens it consumes. If the construct is parsed successfully, the
1008 parser "commits", i.e., it issues any queued error messages and
1009 the tokens that were being preserved are permanently discarded.
1010 If, however, the construct is not parsed successfully, the parser
1011 rolls back its state completely so that it can resume parsing using
1012 a different alternative.
1017 The performance of the parser could probably be improved substantially.
1018 We could often eliminate the need to parse tentatively by looking ahead
1019 a little bit. In some places, this approach might not entirely eliminate
1020 the need to parse tentatively, but it might still speed up the average
1023 /* Flags that are passed to some parsing functions. These values can
1024 be bitwise-ored together. */
1026 typedef enum cp_parser_flags
1029 CP_PARSER_FLAGS_NONE = 0x0,
1030 /* The construct is optional. If it is not present, then no error
1031 should be issued. */
1032 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1033 /* When parsing a type-specifier, do not allow user-defined types. */
1034 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1037 /* The different kinds of declarators we want to parse. */
1039 typedef enum cp_parser_declarator_kind
1041 /* We want an abstract declarator. */
1042 CP_PARSER_DECLARATOR_ABSTRACT,
1043 /* We want a named declarator. */
1044 CP_PARSER_DECLARATOR_NAMED,
1045 /* We don't mind, but the name must be an unqualified-id. */
1046 CP_PARSER_DECLARATOR_EITHER
1047 } cp_parser_declarator_kind;
1049 /* The precedence values used to parse binary expressions. The minimum value
1050 of PREC must be 1, because zero is reserved to quickly discriminate
1051 binary operators from other tokens. */
1056 PREC_LOGICAL_OR_EXPRESSION,
1057 PREC_LOGICAL_AND_EXPRESSION,
1058 PREC_INCLUSIVE_OR_EXPRESSION,
1059 PREC_EXCLUSIVE_OR_EXPRESSION,
1060 PREC_AND_EXPRESSION,
1061 PREC_EQUALITY_EXPRESSION,
1062 PREC_RELATIONAL_EXPRESSION,
1063 PREC_SHIFT_EXPRESSION,
1064 PREC_ADDITIVE_EXPRESSION,
1065 PREC_MULTIPLICATIVE_EXPRESSION,
1067 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1070 /* A mapping from a token type to a corresponding tree node type, with a
1071 precedence value. */
1073 typedef struct cp_parser_binary_operations_map_node
1075 /* The token type. */
1076 enum cpp_ttype token_type;
1077 /* The corresponding tree code. */
1078 enum tree_code tree_type;
1079 /* The precedence of this operator. */
1080 enum cp_parser_prec prec;
1081 } cp_parser_binary_operations_map_node;
1083 /* The status of a tentative parse. */
1085 typedef enum cp_parser_status_kind
1087 /* No errors have occurred. */
1088 CP_PARSER_STATUS_KIND_NO_ERROR,
1089 /* An error has occurred. */
1090 CP_PARSER_STATUS_KIND_ERROR,
1091 /* We are committed to this tentative parse, whether or not an error
1093 CP_PARSER_STATUS_KIND_COMMITTED
1094 } cp_parser_status_kind;
1096 typedef struct cp_parser_expression_stack_entry
1099 enum tree_code tree_type;
1101 } cp_parser_expression_stack_entry;
1103 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1104 entries because precedence levels on the stack are monotonically
1106 typedef struct cp_parser_expression_stack_entry
1107 cp_parser_expression_stack[NUM_PREC_VALUES];
1109 /* Context that is saved and restored when parsing tentatively. */
1110 typedef struct cp_parser_context GTY (())
1112 /* If this is a tentative parsing context, the status of the
1114 enum cp_parser_status_kind status;
1115 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1116 that are looked up in this context must be looked up both in the
1117 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1118 the context of the containing expression. */
1121 /* The next parsing context in the stack. */
1122 struct cp_parser_context *next;
1123 } cp_parser_context;
1127 /* Constructors and destructors. */
1129 static cp_parser_context *cp_parser_context_new
1130 (cp_parser_context *);
1132 /* Class variables. */
1134 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1136 /* The operator-precedence table used by cp_parser_binary_expression.
1137 Transformed into an associative array (binops_by_token) by
1140 static const cp_parser_binary_operations_map_node binops[] = {
1141 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1142 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1144 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1145 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1146 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1148 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1149 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1151 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1152 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1154 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1155 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1156 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1157 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1159 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1160 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1162 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1164 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1166 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1168 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1170 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1173 /* The same as binops, but initialized by cp_parser_new so that
1174 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1176 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1178 /* Constructors and destructors. */
1180 /* Construct a new context. The context below this one on the stack
1181 is given by NEXT. */
1183 static cp_parser_context *
1184 cp_parser_context_new (cp_parser_context* next)
1186 cp_parser_context *context;
1188 /* Allocate the storage. */
1189 if (cp_parser_context_free_list != NULL)
1191 /* Pull the first entry from the free list. */
1192 context = cp_parser_context_free_list;
1193 cp_parser_context_free_list = context->next;
1194 memset (context, 0, sizeof (*context));
1197 context = GGC_CNEW (cp_parser_context);
1199 /* No errors have occurred yet in this context. */
1200 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1201 /* If this is not the bottomost context, copy information that we
1202 need from the previous context. */
1205 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1206 expression, then we are parsing one in this context, too. */
1207 context->object_type = next->object_type;
1208 /* Thread the stack. */
1209 context->next = next;
1215 /* The cp_parser structure represents the C++ parser. */
1217 typedef struct cp_parser GTY(())
1219 /* The lexer from which we are obtaining tokens. */
1222 /* The scope in which names should be looked up. If NULL_TREE, then
1223 we look up names in the scope that is currently open in the
1224 source program. If non-NULL, this is either a TYPE or
1225 NAMESPACE_DECL for the scope in which we should look. It can
1226 also be ERROR_MARK, when we've parsed a bogus scope.
1228 This value is not cleared automatically after a name is looked
1229 up, so we must be careful to clear it before starting a new look
1230 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1231 will look up `Z' in the scope of `X', rather than the current
1232 scope.) Unfortunately, it is difficult to tell when name lookup
1233 is complete, because we sometimes peek at a token, look it up,
1234 and then decide not to consume it. */
1237 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1238 last lookup took place. OBJECT_SCOPE is used if an expression
1239 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1240 respectively. QUALIFYING_SCOPE is used for an expression of the
1241 form "X::Y"; it refers to X. */
1243 tree qualifying_scope;
1245 /* A stack of parsing contexts. All but the bottom entry on the
1246 stack will be tentative contexts.
1248 We parse tentatively in order to determine which construct is in
1249 use in some situations. For example, in order to determine
1250 whether a statement is an expression-statement or a
1251 declaration-statement we parse it tentatively as a
1252 declaration-statement. If that fails, we then reparse the same
1253 token stream as an expression-statement. */
1254 cp_parser_context *context;
1256 /* True if we are parsing GNU C++. If this flag is not set, then
1257 GNU extensions are not recognized. */
1258 bool allow_gnu_extensions_p;
1260 /* TRUE if the `>' token should be interpreted as the greater-than
1261 operator. FALSE if it is the end of a template-id or
1262 template-parameter-list. */
1263 bool greater_than_is_operator_p;
1265 /* TRUE if default arguments are allowed within a parameter list
1266 that starts at this point. FALSE if only a gnu extension makes
1267 them permissible. */
1268 bool default_arg_ok_p;
1270 /* TRUE if we are parsing an integral constant-expression. See
1271 [expr.const] for a precise definition. */
1272 bool integral_constant_expression_p;
1274 /* TRUE if we are parsing an integral constant-expression -- but a
1275 non-constant expression should be permitted as well. This flag
1276 is used when parsing an array bound so that GNU variable-length
1277 arrays are tolerated. */
1278 bool allow_non_integral_constant_expression_p;
1280 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1281 been seen that makes the expression non-constant. */
1282 bool non_integral_constant_expression_p;
1284 /* TRUE if local variable names and `this' are forbidden in the
1286 bool local_variables_forbidden_p;
1288 /* TRUE if the declaration we are parsing is part of a
1289 linkage-specification of the form `extern string-literal
1291 bool in_unbraced_linkage_specification_p;
1293 /* TRUE if we are presently parsing a declarator, after the
1294 direct-declarator. */
1295 bool in_declarator_p;
1297 /* TRUE if we are presently parsing a template-argument-list. */
1298 bool in_template_argument_list_p;
1300 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1301 to IN_OMP_BLOCK if parsing OpenMP structured block and
1302 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1303 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1304 iteration-statement, OpenMP block or loop within that switch. */
1305 #define IN_SWITCH_STMT 1
1306 #define IN_ITERATION_STMT 2
1307 #define IN_OMP_BLOCK 4
1308 #define IN_OMP_FOR 8
1309 unsigned char in_statement;
1311 /* TRUE if we are presently parsing the body of a switch statement.
1312 Note that this doesn't quite overlap with in_statement above.
1313 The difference relates to giving the right sets of error messages:
1314 "case not in switch" vs "break statement used with OpenMP...". */
1315 bool in_switch_statement_p;
1317 /* TRUE if we are parsing a type-id in an expression context. In
1318 such a situation, both "type (expr)" and "type (type)" are valid
1320 bool in_type_id_in_expr_p;
1322 /* TRUE if we are currently in a header file where declarations are
1323 implicitly extern "C". */
1324 bool implicit_extern_c;
1326 /* TRUE if strings in expressions should be translated to the execution
1328 bool translate_strings_p;
1330 /* If non-NULL, then we are parsing a construct where new type
1331 definitions are not permitted. The string stored here will be
1332 issued as an error message if a type is defined. */
1333 const char *type_definition_forbidden_message;
1335 /* A list of lists. The outer list is a stack, used for member
1336 functions of local classes. At each level there are two sub-list,
1337 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1338 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1339 TREE_VALUE's. The functions are chained in reverse declaration
1342 The TREE_PURPOSE sublist contains those functions with default
1343 arguments that need post processing, and the TREE_VALUE sublist
1344 contains those functions with definitions that need post
1347 These lists can only be processed once the outermost class being
1348 defined is complete. */
1349 tree unparsed_functions_queues;
1351 /* The number of classes whose definitions are currently in
1353 unsigned num_classes_being_defined;
1355 /* The number of template parameter lists that apply directly to the
1356 current declaration. */
1357 unsigned num_template_parameter_lists;
1362 /* Constructors and destructors. */
1364 static cp_parser *cp_parser_new
1367 /* Routines to parse various constructs.
1369 Those that return `tree' will return the error_mark_node (rather
1370 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1371 Sometimes, they will return an ordinary node if error-recovery was
1372 attempted, even though a parse error occurred. So, to check
1373 whether or not a parse error occurred, you should always use
1374 cp_parser_error_occurred. If the construct is optional (indicated
1375 either by an `_opt' in the name of the function that does the
1376 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1377 the construct is not present. */
1379 /* Lexical conventions [gram.lex] */
1381 static tree cp_parser_identifier
1383 static tree cp_parser_string_literal
1384 (cp_parser *, bool, bool);
1386 /* Basic concepts [gram.basic] */
1388 static bool cp_parser_translation_unit
1391 /* Expressions [gram.expr] */
1393 static tree cp_parser_primary_expression
1394 (cp_parser *, bool, bool, bool, cp_id_kind *);
1395 static tree cp_parser_id_expression
1396 (cp_parser *, bool, bool, bool *, bool, bool, bool);
1397 static tree cp_parser_unqualified_id
1398 (cp_parser *, bool, bool, bool, bool, bool);
1399 static tree cp_parser_nested_name_specifier_opt
1400 (cp_parser *, bool, bool, bool, bool);
1401 static tree cp_parser_nested_name_specifier
1402 (cp_parser *, bool, bool, bool, bool);
1403 static tree cp_parser_class_or_namespace_name
1404 (cp_parser *, bool, bool, bool, bool, bool);
1405 static tree cp_parser_postfix_expression
1406 (cp_parser *, bool, bool);
1407 static tree cp_parser_postfix_open_square_expression
1408 (cp_parser *, tree, bool);
1409 static tree cp_parser_postfix_dot_deref_expression
1410 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1411 static tree cp_parser_parenthesized_expression_list
1412 (cp_parser *, bool, bool, bool *);
1413 static void cp_parser_pseudo_destructor_name
1414 (cp_parser *, tree *, tree *);
1415 static tree cp_parser_unary_expression
1416 (cp_parser *, bool, bool);
1417 static enum tree_code cp_parser_unary_operator
1419 static tree cp_parser_new_expression
1421 static tree cp_parser_new_placement
1423 static tree cp_parser_new_type_id
1424 (cp_parser *, tree *);
1425 static cp_declarator *cp_parser_new_declarator_opt
1427 static cp_declarator *cp_parser_direct_new_declarator
1429 static tree cp_parser_new_initializer
1431 static tree cp_parser_delete_expression
1433 static tree cp_parser_cast_expression
1434 (cp_parser *, bool, bool);
1435 static tree cp_parser_binary_expression
1436 (cp_parser *, bool);
1437 static tree cp_parser_question_colon_clause
1438 (cp_parser *, tree);
1439 static tree cp_parser_assignment_expression
1440 (cp_parser *, bool);
1441 static enum tree_code cp_parser_assignment_operator_opt
1443 static tree cp_parser_expression
1444 (cp_parser *, bool);
1445 static tree cp_parser_constant_expression
1446 (cp_parser *, bool, bool *);
1447 static tree cp_parser_builtin_offsetof
1450 /* Statements [gram.stmt.stmt] */
1452 static void cp_parser_statement
1453 (cp_parser *, tree, bool);
1454 static tree cp_parser_labeled_statement
1455 (cp_parser *, tree, bool);
1456 static tree cp_parser_expression_statement
1457 (cp_parser *, tree);
1458 static tree cp_parser_compound_statement
1459 (cp_parser *, tree, bool);
1460 static void cp_parser_statement_seq_opt
1461 (cp_parser *, tree);
1462 static tree cp_parser_selection_statement
1464 static tree cp_parser_condition
1466 static tree cp_parser_iteration_statement
1468 static void cp_parser_for_init_statement
1470 static tree cp_parser_jump_statement
1472 static void cp_parser_declaration_statement
1475 static tree cp_parser_implicitly_scoped_statement
1477 static void cp_parser_already_scoped_statement
1480 /* Declarations [gram.dcl.dcl] */
1482 static void cp_parser_declaration_seq_opt
1484 static void cp_parser_declaration
1486 static void cp_parser_block_declaration
1487 (cp_parser *, bool);
1488 static void cp_parser_simple_declaration
1489 (cp_parser *, bool);
1490 static void cp_parser_decl_specifier_seq
1491 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1492 static tree cp_parser_storage_class_specifier_opt
1494 static tree cp_parser_function_specifier_opt
1495 (cp_parser *, cp_decl_specifier_seq *);
1496 static tree cp_parser_type_specifier
1497 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1499 static tree cp_parser_simple_type_specifier
1500 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1501 static tree cp_parser_type_name
1503 static tree cp_parser_elaborated_type_specifier
1504 (cp_parser *, bool, bool);
1505 static tree cp_parser_enum_specifier
1507 static void cp_parser_enumerator_list
1508 (cp_parser *, tree);
1509 static void cp_parser_enumerator_definition
1510 (cp_parser *, tree);
1511 static tree cp_parser_namespace_name
1513 static void cp_parser_namespace_definition
1515 static void cp_parser_namespace_body
1517 static tree cp_parser_qualified_namespace_specifier
1519 static void cp_parser_namespace_alias_definition
1521 static void cp_parser_using_declaration
1523 static void cp_parser_using_directive
1525 static void cp_parser_asm_definition
1527 static void cp_parser_linkage_specification
1530 /* Declarators [gram.dcl.decl] */
1532 static tree cp_parser_init_declarator
1533 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1534 static cp_declarator *cp_parser_declarator
1535 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1536 static cp_declarator *cp_parser_direct_declarator
1537 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1538 static enum tree_code cp_parser_ptr_operator
1539 (cp_parser *, tree *, cp_cv_quals *);
1540 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1542 static tree cp_parser_declarator_id
1543 (cp_parser *, bool);
1544 static tree cp_parser_type_id
1546 static void cp_parser_type_specifier_seq
1547 (cp_parser *, bool, cp_decl_specifier_seq *);
1548 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1550 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1551 (cp_parser *, bool *);
1552 static cp_parameter_declarator *cp_parser_parameter_declaration
1553 (cp_parser *, bool, bool *);
1554 static void cp_parser_function_body
1556 static tree cp_parser_initializer
1557 (cp_parser *, bool *, bool *);
1558 static tree cp_parser_initializer_clause
1559 (cp_parser *, bool *);
1560 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1561 (cp_parser *, bool *);
1563 static bool cp_parser_ctor_initializer_opt_and_function_body
1566 /* Classes [gram.class] */
1568 static tree cp_parser_class_name
1569 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1570 static tree cp_parser_class_specifier
1572 static tree cp_parser_class_head
1573 (cp_parser *, bool *, tree *);
1574 static enum tag_types cp_parser_class_key
1576 static void cp_parser_member_specification_opt
1578 static void cp_parser_member_declaration
1580 static tree cp_parser_pure_specifier
1582 static tree cp_parser_constant_initializer
1585 /* Derived classes [gram.class.derived] */
1587 static tree cp_parser_base_clause
1589 static tree cp_parser_base_specifier
1592 /* Special member functions [gram.special] */
1594 static tree cp_parser_conversion_function_id
1596 static tree cp_parser_conversion_type_id
1598 static cp_declarator *cp_parser_conversion_declarator_opt
1600 static bool cp_parser_ctor_initializer_opt
1602 static void cp_parser_mem_initializer_list
1604 static tree cp_parser_mem_initializer
1606 static tree cp_parser_mem_initializer_id
1609 /* Overloading [gram.over] */
1611 static tree cp_parser_operator_function_id
1613 static tree cp_parser_operator
1616 /* Templates [gram.temp] */
1618 static void cp_parser_template_declaration
1619 (cp_parser *, bool);
1620 static tree cp_parser_template_parameter_list
1622 static tree cp_parser_template_parameter
1623 (cp_parser *, bool *);
1624 static tree cp_parser_type_parameter
1626 static tree cp_parser_template_id
1627 (cp_parser *, bool, bool, bool);
1628 static tree cp_parser_template_name
1629 (cp_parser *, bool, bool, bool, bool *);
1630 static tree cp_parser_template_argument_list
1632 static tree cp_parser_template_argument
1634 static void cp_parser_explicit_instantiation
1636 static void cp_parser_explicit_specialization
1639 /* Exception handling [gram.exception] */
1641 static tree cp_parser_try_block
1643 static bool cp_parser_function_try_block
1645 static void cp_parser_handler_seq
1647 static void cp_parser_handler
1649 static tree cp_parser_exception_declaration
1651 static tree cp_parser_throw_expression
1653 static tree cp_parser_exception_specification_opt
1655 static tree cp_parser_type_id_list
1658 /* GNU Extensions */
1660 static tree cp_parser_asm_specification_opt
1662 static tree cp_parser_asm_operand_list
1664 static tree cp_parser_asm_clobber_list
1666 static tree cp_parser_attributes_opt
1668 static tree cp_parser_attribute_list
1670 static bool cp_parser_extension_opt
1671 (cp_parser *, int *);
1672 static void cp_parser_label_declaration
1675 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1676 static bool cp_parser_pragma
1677 (cp_parser *, enum pragma_context);
1679 /* Objective-C++ Productions */
1681 static tree cp_parser_objc_message_receiver
1683 static tree cp_parser_objc_message_args
1685 static tree cp_parser_objc_message_expression
1687 static tree cp_parser_objc_encode_expression
1689 static tree cp_parser_objc_defs_expression
1691 static tree cp_parser_objc_protocol_expression
1693 static tree cp_parser_objc_selector_expression
1695 static tree cp_parser_objc_expression
1697 static bool cp_parser_objc_selector_p
1699 static tree cp_parser_objc_selector
1701 static tree cp_parser_objc_protocol_refs_opt
1703 static void cp_parser_objc_declaration
1705 static tree cp_parser_objc_statement
1708 /* Utility Routines */
1710 static tree cp_parser_lookup_name
1711 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1712 static tree cp_parser_lookup_name_simple
1713 (cp_parser *, tree);
1714 static tree cp_parser_maybe_treat_template_as_class
1716 static bool cp_parser_check_declarator_template_parameters
1717 (cp_parser *, cp_declarator *);
1718 static bool cp_parser_check_template_parameters
1719 (cp_parser *, unsigned);
1720 static tree cp_parser_simple_cast_expression
1722 static tree cp_parser_global_scope_opt
1723 (cp_parser *, bool, bool);
1724 static bool cp_parser_constructor_declarator_p
1725 (cp_parser *, bool);
1726 static tree cp_parser_function_definition_from_specifiers_and_declarator
1727 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1728 static tree cp_parser_function_definition_after_declarator
1729 (cp_parser *, bool);
1730 static void cp_parser_template_declaration_after_export
1731 (cp_parser *, bool);
1732 static void cp_parser_perform_template_parameter_access_checks
1734 static tree cp_parser_single_declaration
1735 (cp_parser *, tree, bool, bool *);
1736 static tree cp_parser_functional_cast
1737 (cp_parser *, tree);
1738 static tree cp_parser_save_member_function_body
1739 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1740 static tree cp_parser_enclosed_template_argument_list
1742 static void cp_parser_save_default_args
1743 (cp_parser *, tree);
1744 static void cp_parser_late_parsing_for_member
1745 (cp_parser *, tree);
1746 static void cp_parser_late_parsing_default_args
1747 (cp_parser *, tree);
1748 static tree cp_parser_sizeof_operand
1749 (cp_parser *, enum rid);
1750 static bool cp_parser_declares_only_class_p
1752 static void cp_parser_set_storage_class
1753 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1754 static void cp_parser_set_decl_spec_type
1755 (cp_decl_specifier_seq *, tree, bool);
1756 static bool cp_parser_friend_p
1757 (const cp_decl_specifier_seq *);
1758 static cp_token *cp_parser_require
1759 (cp_parser *, enum cpp_ttype, const char *);
1760 static cp_token *cp_parser_require_keyword
1761 (cp_parser *, enum rid, const char *);
1762 static bool cp_parser_token_starts_function_definition_p
1764 static bool cp_parser_next_token_starts_class_definition_p
1766 static bool cp_parser_next_token_ends_template_argument_p
1768 static bool cp_parser_nth_token_starts_template_argument_list_p
1769 (cp_parser *, size_t);
1770 static enum tag_types cp_parser_token_is_class_key
1772 static void cp_parser_check_class_key
1773 (enum tag_types, tree type);
1774 static void cp_parser_check_access_in_redeclaration
1776 static bool cp_parser_optional_template_keyword
1778 static void cp_parser_pre_parsed_nested_name_specifier
1780 static void cp_parser_cache_group
1781 (cp_parser *, enum cpp_ttype, unsigned);
1782 static void cp_parser_parse_tentatively
1784 static void cp_parser_commit_to_tentative_parse
1786 static void cp_parser_abort_tentative_parse
1788 static bool cp_parser_parse_definitely
1790 static inline bool cp_parser_parsing_tentatively
1792 static bool cp_parser_uncommitted_to_tentative_parse_p
1794 static void cp_parser_error
1795 (cp_parser *, const char *);
1796 static void cp_parser_name_lookup_error
1797 (cp_parser *, tree, tree, const char *);
1798 static bool cp_parser_simulate_error
1800 static void cp_parser_check_type_definition
1802 static void cp_parser_check_for_definition_in_return_type
1803 (cp_declarator *, tree);
1804 static void cp_parser_check_for_invalid_template_id
1805 (cp_parser *, tree);
1806 static bool cp_parser_non_integral_constant_expression
1807 (cp_parser *, const char *);
1808 static void cp_parser_diagnose_invalid_type_name
1809 (cp_parser *, tree, tree);
1810 static bool cp_parser_parse_and_diagnose_invalid_type_name
1812 static int cp_parser_skip_to_closing_parenthesis
1813 (cp_parser *, bool, bool, bool);
1814 static void cp_parser_skip_to_end_of_statement
1816 static void cp_parser_consume_semicolon_at_end_of_statement
1818 static void cp_parser_skip_to_end_of_block_or_statement
1820 static void cp_parser_skip_to_closing_brace
1822 static void cp_parser_skip_until_found
1823 (cp_parser *, enum cpp_ttype, const char *);
1824 static void cp_parser_skip_to_pragma_eol
1825 (cp_parser*, cp_token *);
1826 static bool cp_parser_error_occurred
1828 static bool cp_parser_allow_gnu_extensions_p
1830 static bool cp_parser_is_string_literal
1832 static bool cp_parser_is_keyword
1833 (cp_token *, enum rid);
1834 static tree cp_parser_make_typename_type
1835 (cp_parser *, tree, tree);
1837 /* Returns nonzero if we are parsing tentatively. */
1840 cp_parser_parsing_tentatively (cp_parser* parser)
1842 return parser->context->next != NULL;
1845 /* Returns nonzero if TOKEN is a string literal. */
1848 cp_parser_is_string_literal (cp_token* token)
1850 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1853 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1856 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1858 return token->keyword == keyword;
1861 /* If not parsing tentatively, issue a diagnostic of the form
1862 FILE:LINE: MESSAGE before TOKEN
1863 where TOKEN is the next token in the input stream. MESSAGE
1864 (specified by the caller) is usually of the form "expected
1868 cp_parser_error (cp_parser* parser, const char* message)
1870 if (!cp_parser_simulate_error (parser))
1872 cp_token *token = cp_lexer_peek_token (parser->lexer);
1873 /* This diagnostic makes more sense if it is tagged to the line
1874 of the token we just peeked at. */
1875 cp_lexer_set_source_position_from_token (token);
1877 if (token->type == CPP_PRAGMA)
1879 error ("%<#pragma%> is not allowed here");
1880 cp_parser_skip_to_pragma_eol (parser, token);
1884 c_parse_error (message,
1885 /* Because c_parser_error does not understand
1886 CPP_KEYWORD, keywords are treated like
1888 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1893 /* Issue an error about name-lookup failing. NAME is the
1894 IDENTIFIER_NODE DECL is the result of
1895 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1896 the thing that we hoped to find. */
1899 cp_parser_name_lookup_error (cp_parser* parser,
1902 const char* desired)
1904 /* If name lookup completely failed, tell the user that NAME was not
1906 if (decl == error_mark_node)
1908 if (parser->scope && parser->scope != global_namespace)
1909 error ("%<%D::%D%> has not been declared",
1910 parser->scope, name);
1911 else if (parser->scope == global_namespace)
1912 error ("%<::%D%> has not been declared", name);
1913 else if (parser->object_scope
1914 && !CLASS_TYPE_P (parser->object_scope))
1915 error ("request for member %qD in non-class type %qT",
1916 name, parser->object_scope);
1917 else if (parser->object_scope)
1918 error ("%<%T::%D%> has not been declared",
1919 parser->object_scope, name);
1921 error ("%qD has not been declared", name);
1923 else if (parser->scope && parser->scope != global_namespace)
1924 error ("%<%D::%D%> %s", parser->scope, name, desired);
1925 else if (parser->scope == global_namespace)
1926 error ("%<::%D%> %s", name, desired);
1928 error ("%qD %s", name, desired);
1931 /* If we are parsing tentatively, remember that an error has occurred
1932 during this tentative parse. Returns true if the error was
1933 simulated; false if a message should be issued by the caller. */
1936 cp_parser_simulate_error (cp_parser* parser)
1938 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1940 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1946 /* Check for repeated decl-specifiers. */
1949 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
1953 for (ds = ds_first; ds != ds_last; ++ds)
1955 unsigned count = decl_specs->specs[(int)ds];
1958 /* The "long" specifier is a special case because of "long long". */
1962 error ("%<long long long%> is too long for GCC");
1963 else if (pedantic && !in_system_header && warn_long_long)
1964 pedwarn ("ISO C++ does not support %<long long%>");
1968 static const char *const decl_spec_names[] = {
1984 error ("duplicate %qs", decl_spec_names[(int)ds]);
1989 /* This function is called when a type is defined. If type
1990 definitions are forbidden at this point, an error message is
1994 cp_parser_check_type_definition (cp_parser* parser)
1996 /* If types are forbidden here, issue a message. */
1997 if (parser->type_definition_forbidden_message)
1998 /* Use `%s' to print the string in case there are any escape
1999 characters in the message. */
2000 error ("%s", parser->type_definition_forbidden_message);
2003 /* This function is called when the DECLARATOR is processed. The TYPE
2004 was a type defined in the decl-specifiers. If it is invalid to
2005 define a type in the decl-specifiers for DECLARATOR, an error is
2009 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2012 /* [dcl.fct] forbids type definitions in return types.
2013 Unfortunately, it's not easy to know whether or not we are
2014 processing a return type until after the fact. */
2016 && (declarator->kind == cdk_pointer
2017 || declarator->kind == cdk_reference
2018 || declarator->kind == cdk_ptrmem))
2019 declarator = declarator->declarator;
2021 && declarator->kind == cdk_function)
2023 error ("new types may not be defined in a return type");
2024 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2029 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2030 "<" in any valid C++ program. If the next token is indeed "<",
2031 issue a message warning the user about what appears to be an
2032 invalid attempt to form a template-id. */
2035 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2038 cp_token_position start = 0;
2040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2043 error ("%qT is not a template", type);
2044 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2045 error ("%qE is not a template", type);
2047 error ("invalid template-id");
2048 /* Remember the location of the invalid "<". */
2049 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2050 start = cp_lexer_token_position (parser->lexer, true);
2051 /* Consume the "<". */
2052 cp_lexer_consume_token (parser->lexer);
2053 /* Parse the template arguments. */
2054 cp_parser_enclosed_template_argument_list (parser);
2055 /* Permanently remove the invalid template arguments so that
2056 this error message is not issued again. */
2058 cp_lexer_purge_tokens_after (parser->lexer, start);
2062 /* If parsing an integral constant-expression, issue an error message
2063 about the fact that THING appeared and return true. Otherwise,
2064 return false. In either case, set
2065 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2068 cp_parser_non_integral_constant_expression (cp_parser *parser,
2071 parser->non_integral_constant_expression_p = true;
2072 if (parser->integral_constant_expression_p)
2074 if (!parser->allow_non_integral_constant_expression_p)
2076 error ("%s cannot appear in a constant-expression", thing);
2083 /* Emit a diagnostic for an invalid type name. SCOPE is the
2084 qualifying scope (or NULL, if none) for ID. This function commits
2085 to the current active tentative parse, if any. (Otherwise, the
2086 problematic construct might be encountered again later, resulting
2087 in duplicate error messages.) */
2090 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2092 tree decl, old_scope;
2093 /* Try to lookup the identifier. */
2094 old_scope = parser->scope;
2095 parser->scope = scope;
2096 decl = cp_parser_lookup_name_simple (parser, id);
2097 parser->scope = old_scope;
2098 /* If the lookup found a template-name, it means that the user forgot
2099 to specify an argument list. Emit a useful error message. */
2100 if (TREE_CODE (decl) == TEMPLATE_DECL)
2101 error ("invalid use of template-name %qE without an argument list", decl);
2102 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2103 error ("invalid use of destructor %qD as a type", id);
2104 else if (!parser->scope)
2106 /* Issue an error message. */
2107 error ("%qE does not name a type", id);
2108 /* If we're in a template class, it's possible that the user was
2109 referring to a type from a base class. For example:
2111 template <typename T> struct A { typedef T X; };
2112 template <typename T> struct B : public A<T> { X x; };
2114 The user should have said "typename A<T>::X". */
2115 if (processing_template_decl && current_class_type
2116 && TYPE_BINFO (current_class_type))
2120 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2124 tree base_type = BINFO_TYPE (b);
2125 if (CLASS_TYPE_P (base_type)
2126 && dependent_type_p (base_type))
2129 /* Go from a particular instantiation of the
2130 template (which will have an empty TYPE_FIELDs),
2131 to the main version. */
2132 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2133 for (field = TYPE_FIELDS (base_type);
2135 field = TREE_CHAIN (field))
2136 if (TREE_CODE (field) == TYPE_DECL
2137 && DECL_NAME (field) == id)
2139 inform ("(perhaps %<typename %T::%E%> was intended)",
2140 BINFO_TYPE (b), id);
2149 /* Here we diagnose qualified-ids where the scope is actually correct,
2150 but the identifier does not resolve to a valid type name. */
2151 else if (parser->scope != error_mark_node)
2153 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2154 error ("%qE in namespace %qE does not name a type",
2156 else if (TYPE_P (parser->scope))
2157 error ("%qE in class %qT does not name a type", id, parser->scope);
2161 cp_parser_commit_to_tentative_parse (parser);
2164 /* Check for a common situation where a type-name should be present,
2165 but is not, and issue a sensible error message. Returns true if an
2166 invalid type-name was detected.
2168 The situation handled by this function are variable declarations of the
2169 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2170 Usually, `ID' should name a type, but if we got here it means that it
2171 does not. We try to emit the best possible error message depending on
2172 how exactly the id-expression looks like. */
2175 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2179 cp_parser_parse_tentatively (parser);
2180 id = cp_parser_id_expression (parser,
2181 /*template_keyword_p=*/false,
2182 /*check_dependency_p=*/true,
2183 /*template_p=*/NULL,
2184 /*declarator_p=*/true,
2185 /*optional_p=*/false,
2186 /*member_p=*/false);
2187 /* After the id-expression, there should be a plain identifier,
2188 otherwise this is not a simple variable declaration. Also, if
2189 the scope is dependent, we cannot do much. */
2190 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2191 || (parser->scope && TYPE_P (parser->scope)
2192 && dependent_type_p (parser->scope)))
2194 cp_parser_abort_tentative_parse (parser);
2197 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2200 /* Emit a diagnostic for the invalid type. */
2201 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2202 /* Skip to the end of the declaration; there's no point in
2203 trying to process it. */
2204 cp_parser_skip_to_end_of_block_or_statement (parser);
2208 /* Consume tokens up to, and including, the next non-nested closing `)'.
2209 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2210 are doing error recovery. Returns -1 if OR_COMMA is true and we
2211 found an unnested comma. */
2214 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2219 unsigned paren_depth = 0;
2220 unsigned brace_depth = 0;
2222 if (recovering && !or_comma
2223 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2228 cp_token * token = cp_lexer_peek_token (parser->lexer);
2230 switch (token->type)
2233 case CPP_PRAGMA_EOL:
2234 /* If we've run out of tokens, then there is no closing `)'. */
2238 /* This matches the processing in skip_to_end_of_statement. */
2243 case CPP_OPEN_BRACE:
2246 case CPP_CLOSE_BRACE:
2252 if (recovering && or_comma && !brace_depth && !paren_depth)
2256 case CPP_OPEN_PAREN:
2261 case CPP_CLOSE_PAREN:
2262 if (!brace_depth && !paren_depth--)
2265 cp_lexer_consume_token (parser->lexer);
2274 /* Consume the token. */
2275 cp_lexer_consume_token (parser->lexer);
2279 /* Consume tokens until we reach the end of the current statement.
2280 Normally, that will be just before consuming a `;'. However, if a
2281 non-nested `}' comes first, then we stop before consuming that. */
2284 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2286 unsigned nesting_depth = 0;
2290 cp_token *token = cp_lexer_peek_token (parser->lexer);
2292 switch (token->type)
2295 case CPP_PRAGMA_EOL:
2296 /* If we've run out of tokens, stop. */
2300 /* If the next token is a `;', we have reached the end of the
2306 case CPP_CLOSE_BRACE:
2307 /* If this is a non-nested '}', stop before consuming it.
2308 That way, when confronted with something like:
2312 we stop before consuming the closing '}', even though we
2313 have not yet reached a `;'. */
2314 if (nesting_depth == 0)
2317 /* If it is the closing '}' for a block that we have
2318 scanned, stop -- but only after consuming the token.
2324 we will stop after the body of the erroneously declared
2325 function, but before consuming the following `typedef'
2327 if (--nesting_depth == 0)
2329 cp_lexer_consume_token (parser->lexer);
2333 case CPP_OPEN_BRACE:
2341 /* Consume the token. */
2342 cp_lexer_consume_token (parser->lexer);
2346 /* This function is called at the end of a statement or declaration.
2347 If the next token is a semicolon, it is consumed; otherwise, error
2348 recovery is attempted. */
2351 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2353 /* Look for the trailing `;'. */
2354 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2356 /* If there is additional (erroneous) input, skip to the end of
2358 cp_parser_skip_to_end_of_statement (parser);
2359 /* If the next token is now a `;', consume it. */
2360 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2361 cp_lexer_consume_token (parser->lexer);
2365 /* Skip tokens until we have consumed an entire block, or until we
2366 have consumed a non-nested `;'. */
2369 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2371 int nesting_depth = 0;
2373 while (nesting_depth >= 0)
2375 cp_token *token = cp_lexer_peek_token (parser->lexer);
2377 switch (token->type)
2380 case CPP_PRAGMA_EOL:
2381 /* If we've run out of tokens, stop. */
2385 /* Stop if this is an unnested ';'. */
2390 case CPP_CLOSE_BRACE:
2391 /* Stop if this is an unnested '}', or closes the outermost
2398 case CPP_OPEN_BRACE:
2407 /* Consume the token. */
2408 cp_lexer_consume_token (parser->lexer);
2412 /* Skip tokens until a non-nested closing curly brace is the next
2416 cp_parser_skip_to_closing_brace (cp_parser *parser)
2418 unsigned nesting_depth = 0;
2422 cp_token *token = cp_lexer_peek_token (parser->lexer);
2424 switch (token->type)
2427 case CPP_PRAGMA_EOL:
2428 /* If we've run out of tokens, stop. */
2431 case CPP_CLOSE_BRACE:
2432 /* If the next token is a non-nested `}', then we have reached
2433 the end of the current block. */
2434 if (nesting_depth-- == 0)
2438 case CPP_OPEN_BRACE:
2439 /* If it the next token is a `{', then we are entering a new
2440 block. Consume the entire block. */
2448 /* Consume the token. */
2449 cp_lexer_consume_token (parser->lexer);
2453 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2454 parameter is the PRAGMA token, allowing us to purge the entire pragma
2458 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2462 parser->lexer->in_pragma = false;
2465 token = cp_lexer_consume_token (parser->lexer);
2466 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2468 /* Ensure that the pragma is not parsed again. */
2469 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2472 /* Require pragma end of line, resyncing with it as necessary. The
2473 arguments are as for cp_parser_skip_to_pragma_eol. */
2476 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2478 parser->lexer->in_pragma = false;
2479 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2483 /* This is a simple wrapper around make_typename_type. When the id is
2484 an unresolved identifier node, we can provide a superior diagnostic
2485 using cp_parser_diagnose_invalid_type_name. */
2488 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2491 if (TREE_CODE (id) == IDENTIFIER_NODE)
2493 result = make_typename_type (scope, id, typename_type,
2494 /*complain=*/tf_none);
2495 if (result == error_mark_node)
2496 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2499 return make_typename_type (scope, id, typename_type, tf_error);
2503 /* Create a new C++ parser. */
2506 cp_parser_new (void)
2512 /* cp_lexer_new_main is called before calling ggc_alloc because
2513 cp_lexer_new_main might load a PCH file. */
2514 lexer = cp_lexer_new_main ();
2516 /* Initialize the binops_by_token so that we can get the tree
2517 directly from the token. */
2518 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2519 binops_by_token[binops[i].token_type] = binops[i];
2521 parser = GGC_CNEW (cp_parser);
2522 parser->lexer = lexer;
2523 parser->context = cp_parser_context_new (NULL);
2525 /* For now, we always accept GNU extensions. */
2526 parser->allow_gnu_extensions_p = 1;
2528 /* The `>' token is a greater-than operator, not the end of a
2530 parser->greater_than_is_operator_p = true;
2532 parser->default_arg_ok_p = true;
2534 /* We are not parsing a constant-expression. */
2535 parser->integral_constant_expression_p = false;
2536 parser->allow_non_integral_constant_expression_p = false;
2537 parser->non_integral_constant_expression_p = false;
2539 /* Local variable names are not forbidden. */
2540 parser->local_variables_forbidden_p = false;
2542 /* We are not processing an `extern "C"' declaration. */
2543 parser->in_unbraced_linkage_specification_p = false;
2545 /* We are not processing a declarator. */
2546 parser->in_declarator_p = false;
2548 /* We are not processing a template-argument-list. */
2549 parser->in_template_argument_list_p = false;
2551 /* We are not in an iteration statement. */
2552 parser->in_statement = 0;
2554 /* We are not in a switch statement. */
2555 parser->in_switch_statement_p = false;
2557 /* We are not parsing a type-id inside an expression. */
2558 parser->in_type_id_in_expr_p = false;
2560 /* Declarations aren't implicitly extern "C". */
2561 parser->implicit_extern_c = false;
2563 /* String literals should be translated to the execution character set. */
2564 parser->translate_strings_p = true;
2566 /* The unparsed function queue is empty. */
2567 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2569 /* There are no classes being defined. */
2570 parser->num_classes_being_defined = 0;
2572 /* No template parameters apply. */
2573 parser->num_template_parameter_lists = 0;
2578 /* Create a cp_lexer structure which will emit the tokens in CACHE
2579 and push it onto the parser's lexer stack. This is used for delayed
2580 parsing of in-class method bodies and default arguments, and should
2581 not be confused with tentative parsing. */
2583 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2585 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2586 lexer->next = parser->lexer;
2587 parser->lexer = lexer;
2589 /* Move the current source position to that of the first token in the
2591 cp_lexer_set_source_position_from_token (lexer->next_token);
2594 /* Pop the top lexer off the parser stack. This is never used for the
2595 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2597 cp_parser_pop_lexer (cp_parser *parser)
2599 cp_lexer *lexer = parser->lexer;
2600 parser->lexer = lexer->next;
2601 cp_lexer_destroy (lexer);
2603 /* Put the current source position back where it was before this
2604 lexer was pushed. */
2605 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2608 /* Lexical conventions [gram.lex] */
2610 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2614 cp_parser_identifier (cp_parser* parser)
2618 /* Look for the identifier. */
2619 token = cp_parser_require (parser, CPP_NAME, "identifier");
2620 /* Return the value. */
2621 return token ? token->value : error_mark_node;
2624 /* Parse a sequence of adjacent string constants. Returns a
2625 TREE_STRING representing the combined, nul-terminated string
2626 constant. If TRANSLATE is true, translate the string to the
2627 execution character set. If WIDE_OK is true, a wide string is
2630 C++98 [lex.string] says that if a narrow string literal token is
2631 adjacent to a wide string literal token, the behavior is undefined.
2632 However, C99 6.4.5p4 says that this results in a wide string literal.
2633 We follow C99 here, for consistency with the C front end.
2635 This code is largely lifted from lex_string() in c-lex.c.
2637 FUTURE: ObjC++ will need to handle @-strings here. */
2639 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2644 struct obstack str_ob;
2645 cpp_string str, istr, *strs;
2648 tok = cp_lexer_peek_token (parser->lexer);
2649 if (!cp_parser_is_string_literal (tok))
2651 cp_parser_error (parser, "expected string-literal");
2652 return error_mark_node;
2655 /* Try to avoid the overhead of creating and destroying an obstack
2656 for the common case of just one string. */
2657 if (!cp_parser_is_string_literal
2658 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2660 cp_lexer_consume_token (parser->lexer);
2662 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2663 str.len = TREE_STRING_LENGTH (tok->value);
2665 if (tok->type == CPP_WSTRING)
2672 gcc_obstack_init (&str_ob);
2677 cp_lexer_consume_token (parser->lexer);
2679 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2680 str.len = TREE_STRING_LENGTH (tok->value);
2681 if (tok->type == CPP_WSTRING)
2684 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2686 tok = cp_lexer_peek_token (parser->lexer);
2688 while (cp_parser_is_string_literal (tok));
2690 strs = (cpp_string *) obstack_finish (&str_ob);
2693 if (wide && !wide_ok)
2695 cp_parser_error (parser, "a wide string is invalid in this context");
2699 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2700 (parse_in, strs, count, &istr, wide))
2702 value = build_string (istr.len, (char *)istr.text);
2703 free ((void *)istr.text);
2705 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2706 value = fix_string_type (value);
2709 /* cpp_interpret_string has issued an error. */
2710 value = error_mark_node;
2713 obstack_free (&str_ob, 0);
2719 /* Basic concepts [gram.basic] */
2721 /* Parse a translation-unit.
2724 declaration-seq [opt]
2726 Returns TRUE if all went well. */
2729 cp_parser_translation_unit (cp_parser* parser)
2731 /* The address of the first non-permanent object on the declarator
2733 static void *declarator_obstack_base;
2737 /* Create the declarator obstack, if necessary. */
2738 if (!cp_error_declarator)
2740 gcc_obstack_init (&declarator_obstack);
2741 /* Create the error declarator. */
2742 cp_error_declarator = make_declarator (cdk_error);
2743 /* Create the empty parameter list. */
2744 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2745 /* Remember where the base of the declarator obstack lies. */
2746 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2749 cp_parser_declaration_seq_opt (parser);
2751 /* If there are no tokens left then all went well. */
2752 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2754 /* Get rid of the token array; we don't need it any more. */
2755 cp_lexer_destroy (parser->lexer);
2756 parser->lexer = NULL;
2758 /* This file might have been a context that's implicitly extern
2759 "C". If so, pop the lang context. (Only relevant for PCH.) */
2760 if (parser->implicit_extern_c)
2762 pop_lang_context ();
2763 parser->implicit_extern_c = false;
2767 finish_translation_unit ();
2773 cp_parser_error (parser, "expected declaration");
2777 /* Make sure the declarator obstack was fully cleaned up. */
2778 gcc_assert (obstack_next_free (&declarator_obstack)
2779 == declarator_obstack_base);
2781 /* All went well. */
2785 /* Expressions [gram.expr] */
2787 /* Parse a primary-expression.
2798 ( compound-statement )
2799 __builtin_va_arg ( assignment-expression , type-id )
2800 __builtin_offsetof ( type-id , offsetof-expression )
2802 Objective-C++ Extension:
2810 ADDRESS_P is true iff this expression was immediately preceded by
2811 "&" and therefore might denote a pointer-to-member. CAST_P is true
2812 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2813 true iff this expression is a template argument.
2815 Returns a representation of the expression. Upon return, *IDK
2816 indicates what kind of id-expression (if any) was present. */
2819 cp_parser_primary_expression (cp_parser *parser,
2822 bool template_arg_p,
2827 /* Assume the primary expression is not an id-expression. */
2828 *idk = CP_ID_KIND_NONE;
2830 /* Peek at the next token. */
2831 token = cp_lexer_peek_token (parser->lexer);
2832 switch (token->type)
2843 token = cp_lexer_consume_token (parser->lexer);
2844 /* Floating-point literals are only allowed in an integral
2845 constant expression if they are cast to an integral or
2846 enumeration type. */
2847 if (TREE_CODE (token->value) == REAL_CST
2848 && parser->integral_constant_expression_p
2851 /* CAST_P will be set even in invalid code like "int(2.7 +
2852 ...)". Therefore, we have to check that the next token
2853 is sure to end the cast. */
2856 cp_token *next_token;
2858 next_token = cp_lexer_peek_token (parser->lexer);
2859 if (/* The comma at the end of an
2860 enumerator-definition. */
2861 next_token->type != CPP_COMMA
2862 /* The curly brace at the end of an enum-specifier. */
2863 && next_token->type != CPP_CLOSE_BRACE
2864 /* The end of a statement. */
2865 && next_token->type != CPP_SEMICOLON
2866 /* The end of the cast-expression. */
2867 && next_token->type != CPP_CLOSE_PAREN
2868 /* The end of an array bound. */
2869 && next_token->type != CPP_CLOSE_SQUARE
2870 /* The closing ">" in a template-argument-list. */
2871 && (next_token->type != CPP_GREATER
2872 || parser->greater_than_is_operator_p))
2876 /* If we are within a cast, then the constraint that the
2877 cast is to an integral or enumeration type will be
2878 checked at that point. If we are not within a cast, then
2879 this code is invalid. */
2881 cp_parser_non_integral_constant_expression
2882 (parser, "floating-point literal");
2884 return token->value;
2888 /* ??? Should wide strings be allowed when parser->translate_strings_p
2889 is false (i.e. in attributes)? If not, we can kill the third
2890 argument to cp_parser_string_literal. */
2891 return cp_parser_string_literal (parser,
2892 parser->translate_strings_p,
2895 case CPP_OPEN_PAREN:
2898 bool saved_greater_than_is_operator_p;
2900 /* Consume the `('. */
2901 cp_lexer_consume_token (parser->lexer);
2902 /* Within a parenthesized expression, a `>' token is always
2903 the greater-than operator. */
2904 saved_greater_than_is_operator_p
2905 = parser->greater_than_is_operator_p;
2906 parser->greater_than_is_operator_p = true;
2907 /* If we see `( { ' then we are looking at the beginning of
2908 a GNU statement-expression. */
2909 if (cp_parser_allow_gnu_extensions_p (parser)
2910 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2912 /* Statement-expressions are not allowed by the standard. */
2914 pedwarn ("ISO C++ forbids braced-groups within expressions");
2916 /* And they're not allowed outside of a function-body; you
2917 cannot, for example, write:
2919 int i = ({ int j = 3; j + 1; });
2921 at class or namespace scope. */
2922 if (!at_function_scope_p ())
2923 error ("statement-expressions are allowed only inside functions");
2924 /* Start the statement-expression. */
2925 expr = begin_stmt_expr ();
2926 /* Parse the compound-statement. */
2927 cp_parser_compound_statement (parser, expr, false);
2929 expr = finish_stmt_expr (expr, false);
2933 /* Parse the parenthesized expression. */
2934 expr = cp_parser_expression (parser, cast_p);
2935 /* Let the front end know that this expression was
2936 enclosed in parentheses. This matters in case, for
2937 example, the expression is of the form `A::B', since
2938 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2940 finish_parenthesized_expr (expr);
2942 /* The `>' token might be the end of a template-id or
2943 template-parameter-list now. */
2944 parser->greater_than_is_operator_p
2945 = saved_greater_than_is_operator_p;
2946 /* Consume the `)'. */
2947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2948 cp_parser_skip_to_end_of_statement (parser);
2954 switch (token->keyword)
2956 /* These two are the boolean literals. */
2958 cp_lexer_consume_token (parser->lexer);
2959 return boolean_true_node;
2961 cp_lexer_consume_token (parser->lexer);
2962 return boolean_false_node;
2964 /* The `__null' literal. */
2966 cp_lexer_consume_token (parser->lexer);
2969 /* Recognize the `this' keyword. */
2971 cp_lexer_consume_token (parser->lexer);
2972 if (parser->local_variables_forbidden_p)
2974 error ("%<this%> may not be used in this context");
2975 return error_mark_node;
2977 /* Pointers cannot appear in constant-expressions. */
2978 if (cp_parser_non_integral_constant_expression (parser,
2980 return error_mark_node;
2981 return finish_this_expr ();
2983 /* The `operator' keyword can be the beginning of an
2988 case RID_FUNCTION_NAME:
2989 case RID_PRETTY_FUNCTION_NAME:
2990 case RID_C99_FUNCTION_NAME:
2991 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2992 __func__ are the names of variables -- but they are
2993 treated specially. Therefore, they are handled here,
2994 rather than relying on the generic id-expression logic
2995 below. Grammatically, these names are id-expressions.
2997 Consume the token. */
2998 token = cp_lexer_consume_token (parser->lexer);
2999 /* Look up the name. */
3000 return finish_fname (token->value);
3007 /* The `__builtin_va_arg' construct is used to handle
3008 `va_arg'. Consume the `__builtin_va_arg' token. */
3009 cp_lexer_consume_token (parser->lexer);
3010 /* Look for the opening `('. */
3011 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3012 /* Now, parse the assignment-expression. */
3013 expression = cp_parser_assignment_expression (parser,
3015 /* Look for the `,'. */
3016 cp_parser_require (parser, CPP_COMMA, "`,'");
3017 /* Parse the type-id. */
3018 type = cp_parser_type_id (parser);
3019 /* Look for the closing `)'. */
3020 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3021 /* Using `va_arg' in a constant-expression is not
3023 if (cp_parser_non_integral_constant_expression (parser,
3025 return error_mark_node;
3026 return build_x_va_arg (expression, type);
3030 return cp_parser_builtin_offsetof (parser);
3032 /* Objective-C++ expressions. */
3034 case RID_AT_PROTOCOL:
3035 case RID_AT_SELECTOR:
3036 return cp_parser_objc_expression (parser);
3039 cp_parser_error (parser, "expected primary-expression");
3040 return error_mark_node;
3043 /* An id-expression can start with either an identifier, a
3044 `::' as the beginning of a qualified-id, or the "operator"
3048 case CPP_TEMPLATE_ID:
3049 case CPP_NESTED_NAME_SPECIFIER:
3053 const char *error_msg;
3058 /* Parse the id-expression. */
3060 = cp_parser_id_expression (parser,
3061 /*template_keyword_p=*/false,
3062 /*check_dependency_p=*/true,
3064 /*declarator_p=*/false,
3065 /*optional_p=*/false,
3066 /*member_p=*/false);
3067 if (id_expression == error_mark_node)
3068 return error_mark_node;
3069 token = cp_lexer_peek_token (parser->lexer);
3070 done = (token->type != CPP_OPEN_SQUARE
3071 && token->type != CPP_OPEN_PAREN
3072 && token->type != CPP_DOT
3073 && token->type != CPP_DEREF
3074 && token->type != CPP_PLUS_PLUS
3075 && token->type != CPP_MINUS_MINUS);
3076 /* If we have a template-id, then no further lookup is
3077 required. If the template-id was for a template-class, we
3078 will sometimes have a TYPE_DECL at this point. */
3079 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3080 || TREE_CODE (id_expression) == TYPE_DECL)
3081 decl = id_expression;
3082 /* Look up the name. */
3085 tree ambiguous_decls;
3087 decl = cp_parser_lookup_name (parser, id_expression,
3090 /*is_namespace=*/false,
3091 /*check_dependency=*/true,
3093 /* If the lookup was ambiguous, an error will already have
3095 if (ambiguous_decls)
3096 return error_mark_node;
3098 /* In Objective-C++, an instance variable (ivar) may be preferred
3099 to whatever cp_parser_lookup_name() found. */
3100 decl = objc_lookup_ivar (decl, id_expression);
3102 /* If name lookup gives us a SCOPE_REF, then the
3103 qualifying scope was dependent. */
3104 if (TREE_CODE (decl) == SCOPE_REF)
3106 /* Check to see if DECL is a local variable in a context
3107 where that is forbidden. */
3108 if (parser->local_variables_forbidden_p
3109 && local_variable_p (decl))
3111 /* It might be that we only found DECL because we are
3112 trying to be generous with pre-ISO scoping rules.
3113 For example, consider:
3117 for (int i = 0; i < 10; ++i) {}
3118 extern void f(int j = i);
3121 Here, name look up will originally find the out
3122 of scope `i'. We need to issue a warning message,
3123 but then use the global `i'. */
3124 decl = check_for_out_of_scope_variable (decl);
3125 if (local_variable_p (decl))
3127 error ("local variable %qD may not appear in this context",
3129 return error_mark_node;
3134 decl = (finish_id_expression
3135 (id_expression, decl, parser->scope,
3137 parser->integral_constant_expression_p,
3138 parser->allow_non_integral_constant_expression_p,
3139 &parser->non_integral_constant_expression_p,
3140 template_p, done, address_p,
3144 cp_parser_error (parser, error_msg);
3148 /* Anything else is an error. */
3150 /* ...unless we have an Objective-C++ message or string literal, that is. */
3151 if (c_dialect_objc ()
3152 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3153 return cp_parser_objc_expression (parser);
3155 cp_parser_error (parser, "expected primary-expression");
3156 return error_mark_node;
3160 /* Parse an id-expression.
3167 :: [opt] nested-name-specifier template [opt] unqualified-id
3169 :: operator-function-id
3172 Return a representation of the unqualified portion of the
3173 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3174 a `::' or nested-name-specifier.
3176 Often, if the id-expression was a qualified-id, the caller will
3177 want to make a SCOPE_REF to represent the qualified-id. This
3178 function does not do this in order to avoid wastefully creating
3179 SCOPE_REFs when they are not required.
3181 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3184 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3185 uninstantiated templates.
3187 If *TEMPLATE_P is non-NULL, it is set to true iff the
3188 `template' keyword is used to explicitly indicate that the entity
3189 named is a template.
3191 If DECLARATOR_P is true, the id-expression is appearing as part of
3192 a declarator, rather than as part of an expression. */
3195 cp_parser_id_expression (cp_parser *parser,
3196 bool template_keyword_p,
3197 bool check_dependency_p,
3203 bool global_scope_p;
3204 bool nested_name_specifier_p;
3206 /* Assume the `template' keyword was not used. */
3208 *template_p = template_keyword_p;
3210 /* Look for the optional `::' operator. */
3212 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
3213 /*object_scope_valid_p=*/member_p)
3216 /* Look for the optional nested-name-specifier. */
3217 nested_name_specifier_p
3218 = (cp_parser_nested_name_specifier_opt (parser,
3219 /*typename_keyword_p=*/false,
3224 /* If there is a nested-name-specifier, then we are looking at
3225 the first qualified-id production. */
3226 if (nested_name_specifier_p)
3229 tree saved_object_scope;
3230 tree saved_qualifying_scope;
3231 tree unqualified_id;
3234 /* See if the next token is the `template' keyword. */
3236 template_p = &is_template;
3237 *template_p = cp_parser_optional_template_keyword (parser);
3238 /* Name lookup we do during the processing of the
3239 unqualified-id might obliterate SCOPE. */
3240 saved_scope = parser->scope;
3241 saved_object_scope = parser->object_scope;
3242 saved_qualifying_scope = parser->qualifying_scope;
3243 /* Process the final unqualified-id. */
3244 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3247 /*optional_p=*/false,
3248 /*member_p=*/false);
3249 /* Restore the SAVED_SCOPE for our caller. */
3250 parser->scope = saved_scope;
3251 parser->object_scope = saved_object_scope;
3252 parser->qualifying_scope = saved_qualifying_scope;
3254 return unqualified_id;
3256 /* Otherwise, if we are in global scope, then we are looking at one
3257 of the other qualified-id productions. */
3258 else if (global_scope_p)
3263 /* Peek at the next token. */
3264 token = cp_lexer_peek_token (parser->lexer);
3266 /* If it's an identifier, and the next token is not a "<", then
3267 we can avoid the template-id case. This is an optimization
3268 for this common case. */
3269 if (token->type == CPP_NAME
3270 && !cp_parser_nth_token_starts_template_argument_list_p
3272 return cp_parser_identifier (parser);
3274 cp_parser_parse_tentatively (parser);
3275 /* Try a template-id. */
3276 id = cp_parser_template_id (parser,
3277 /*template_keyword_p=*/false,
3278 /*check_dependency_p=*/true,
3280 /* If that worked, we're done. */
3281 if (cp_parser_parse_definitely (parser))
3284 /* Peek at the next token. (Changes in the token buffer may
3285 have invalidated the pointer obtained above.) */
3286 token = cp_lexer_peek_token (parser->lexer);
3288 switch (token->type)
3291 return cp_parser_identifier (parser);
3294 if (token->keyword == RID_OPERATOR)
3295 return cp_parser_operator_function_id (parser);
3299 cp_parser_error (parser, "expected id-expression");
3300 return error_mark_node;
3304 return cp_parser_unqualified_id (parser, template_keyword_p,
3305 /*check_dependency_p=*/true,
3306 declarator_p, optional_p, member_p);
3309 /* Parse an unqualified-id.
3313 operator-function-id
3314 conversion-function-id
3318 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3319 keyword, in a construct like `A::template ...'.
3321 Returns a representation of unqualified-id. For the `identifier'
3322 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3323 production a BIT_NOT_EXPR is returned; the operand of the
3324 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3325 other productions, see the documentation accompanying the
3326 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3327 names are looked up in uninstantiated templates. If DECLARATOR_P
3328 is true, the unqualified-id is appearing as part of a declarator,
3329 rather than as part of an expression. */
3332 cp_parser_unqualified_id (cp_parser* parser,
3333 bool template_keyword_p,
3334 bool check_dependency_p,
3341 /* Peek at the next token. */
3342 token = cp_lexer_peek_token (parser->lexer);
3344 switch (token->type)
3350 /* We don't know yet whether or not this will be a
3352 cp_parser_parse_tentatively (parser);
3353 /* Try a template-id. */
3354 id = cp_parser_template_id (parser, template_keyword_p,
3357 /* If it worked, we're done. */
3358 if (cp_parser_parse_definitely (parser))
3360 /* Otherwise, it's an ordinary identifier. */
3361 return cp_parser_identifier (parser);
3364 case CPP_TEMPLATE_ID:
3365 return cp_parser_template_id (parser, template_keyword_p,
3372 tree qualifying_scope;
3377 /* Consume the `~' token. */
3378 cp_lexer_consume_token (parser->lexer);
3379 /* Parse the class-name. The standard, as written, seems to
3382 template <typename T> struct S { ~S (); };
3383 template <typename T> S<T>::~S() {}
3385 is invalid, since `~' must be followed by a class-name, but
3386 `S<T>' is dependent, and so not known to be a class.
3387 That's not right; we need to look in uninstantiated
3388 templates. A further complication arises from:
3390 template <typename T> void f(T t) {
3394 Here, it is not possible to look up `T' in the scope of `T'
3395 itself. We must look in both the current scope, and the
3396 scope of the containing complete expression.
3398 Yet another issue is:
3407 The standard does not seem to say that the `S' in `~S'
3408 should refer to the type `S' and not the data member
3411 /* DR 244 says that we look up the name after the "~" in the
3412 same scope as we looked up the qualifying name. That idea
3413 isn't fully worked out; it's more complicated than that. */
3414 scope = parser->scope;
3415 object_scope = parser->object_scope;
3416 qualifying_scope = parser->qualifying_scope;
3418 /* Check for invalid scopes. */
3419 if (scope == error_mark_node)
3421 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3422 cp_lexer_consume_token (parser->lexer);
3423 return error_mark_node;
3425 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3427 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3428 error ("scope %qT before %<~%> is not a class-name", scope);
3429 cp_parser_simulate_error (parser);
3430 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3431 cp_lexer_consume_token (parser->lexer);
3432 return error_mark_node;
3434 gcc_assert (!scope || TYPE_P (scope));
3436 /* If the name is of the form "X::~X" it's OK. */
3437 token = cp_lexer_peek_token (parser->lexer);
3439 && token->type == CPP_NAME
3440 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3442 && constructor_name_p (token->value, scope))
3444 cp_lexer_consume_token (parser->lexer);
3445 return build_nt (BIT_NOT_EXPR, scope);
3448 /* If there was an explicit qualification (S::~T), first look
3449 in the scope given by the qualification (i.e., S). */
3451 type_decl = NULL_TREE;
3454 cp_parser_parse_tentatively (parser);
3455 type_decl = cp_parser_class_name (parser,
3456 /*typename_keyword_p=*/false,
3457 /*template_keyword_p=*/false,
3459 /*check_dependency=*/false,
3460 /*class_head_p=*/false,
3462 if (cp_parser_parse_definitely (parser))
3466 /* In "N::S::~S", look in "N" as well. */
3467 if (!done && scope && qualifying_scope)
3469 cp_parser_parse_tentatively (parser);
3470 parser->scope = qualifying_scope;
3471 parser->object_scope = NULL_TREE;
3472 parser->qualifying_scope = NULL_TREE;
3474 = cp_parser_class_name (parser,
3475 /*typename_keyword_p=*/false,
3476 /*template_keyword_p=*/false,
3478 /*check_dependency=*/false,
3479 /*class_head_p=*/false,
3481 if (cp_parser_parse_definitely (parser))
3484 /* In "p->~T", look in the scope given by "*p" as well. */
3485 else if (!done && member_p)
3489 /* It's a dependent expression, so just parse the
3493 if (template_keyword_p)
3494 /* It's a template-id. */
3495 id = cp_parser_template_id (parser, true,
3500 /* Otherwise, it's an ordinary identifier. */
3501 id = cp_parser_identifier (parser);
3502 /* If ID is a template type parm, then use that
3505 && TREE_CODE (TREE_TYPE (id)) == TEMPLATE_TYPE_PARM)
3506 id = TREE_TYPE (id);
3509 if (id != error_mark_node)
3510 id = build_nt (BIT_NOT_EXPR, id);
3514 cp_parser_parse_tentatively (parser);
3515 parser->scope = object_scope;
3516 parser->object_scope = NULL_TREE;
3517 parser->qualifying_scope = NULL_TREE;
3519 = cp_parser_class_name (parser,
3520 /*typename_keyword_p=*/false,
3521 /*template_keyword_p=*/false,
3523 /*check_dependency=*/false,
3524 /*class_head_p=*/false,
3526 /* The name is not qualified, so reset the parser scopes
3527 so our callers do not get confused. */
3528 parser->object_scope = object_scope;
3529 parser->scope = NULL_TREE;
3530 if (cp_parser_parse_definitely (parser))
3534 /* Look in the surrounding context. */
3537 parser->scope = NULL_TREE;
3538 parser->object_scope = NULL_TREE;
3539 parser->qualifying_scope = NULL_TREE;
3541 = cp_parser_class_name (parser,
3542 /*typename_keyword_p=*/false,
3543 /*template_keyword_p=*/false,
3545 /*check_dependency=*/false,
3546 /*class_head_p=*/false,
3549 /* If an error occurred, assume that the name of the
3550 destructor is the same as the name of the qualifying
3551 class. That allows us to keep parsing after running
3552 into ill-formed destructor names. */
3553 if (type_decl == error_mark_node && scope)
3554 return build_nt (BIT_NOT_EXPR, scope);
3555 else if (type_decl == error_mark_node)
3556 return error_mark_node;
3558 /* Check that destructor name and scope match. */
3559 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3561 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3562 error ("declaration of %<~%T%> as member of %qT",
3564 cp_parser_simulate_error (parser);
3565 return error_mark_node;
3570 A typedef-name that names a class shall not be used as the
3571 identifier in the declarator for a destructor declaration. */
3573 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3574 && !DECL_SELF_REFERENCE_P (type_decl)
3575 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3576 error ("typedef-name %qD used as destructor declarator",
3579 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3583 if (token->keyword == RID_OPERATOR)
3587 /* This could be a template-id, so we try that first. */
3588 cp_parser_parse_tentatively (parser);
3589 /* Try a template-id. */
3590 id = cp_parser_template_id (parser, template_keyword_p,
3591 /*check_dependency_p=*/true,
3593 /* If that worked, we're done. */
3594 if (cp_parser_parse_definitely (parser))
3596 /* We still don't know whether we're looking at an
3597 operator-function-id or a conversion-function-id. */
3598 cp_parser_parse_tentatively (parser);
3599 /* Try an operator-function-id. */
3600 id = cp_parser_operator_function_id (parser);
3601 /* If that didn't work, try a conversion-function-id. */
3602 if (!cp_parser_parse_definitely (parser))
3603 id = cp_parser_conversion_function_id (parser);
3612 cp_parser_error (parser, "expected unqualified-id");
3613 return error_mark_node;
3617 /* Parse an (optional) nested-name-specifier.
3619 nested-name-specifier:
3620 class-or-namespace-name :: nested-name-specifier [opt]
3621 class-or-namespace-name :: template nested-name-specifier [opt]
3623 PARSER->SCOPE should be set appropriately before this function is
3624 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3625 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3628 Sets PARSER->SCOPE to the class (TYPE) or namespace
3629 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3630 it unchanged if there is no nested-name-specifier. Returns the new
3631 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3633 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3634 part of a declaration and/or decl-specifier. */
3637 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3638 bool typename_keyword_p,
3639 bool check_dependency_p,
3641 bool is_declaration)
3643 bool success = false;
3644 cp_token_position start = 0;
3647 /* Remember where the nested-name-specifier starts. */
3648 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3650 start = cp_lexer_token_position (parser->lexer, false);
3651 push_deferring_access_checks (dk_deferred);
3658 tree saved_qualifying_scope;
3659 bool template_keyword_p;
3661 /* Spot cases that cannot be the beginning of a
3662 nested-name-specifier. */
3663 token = cp_lexer_peek_token (parser->lexer);
3665 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3666 the already parsed nested-name-specifier. */
3667 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3669 /* Grab the nested-name-specifier and continue the loop. */
3670 cp_parser_pre_parsed_nested_name_specifier (parser);
3675 /* Spot cases that cannot be the beginning of a
3676 nested-name-specifier. On the second and subsequent times
3677 through the loop, we look for the `template' keyword. */
3678 if (success && token->keyword == RID_TEMPLATE)
3680 /* A template-id can start a nested-name-specifier. */
3681 else if (token->type == CPP_TEMPLATE_ID)
3685 /* If the next token is not an identifier, then it is
3686 definitely not a class-or-namespace-name. */
3687 if (token->type != CPP_NAME)
3689 /* If the following token is neither a `<' (to begin a
3690 template-id), nor a `::', then we are not looking at a
3691 nested-name-specifier. */
3692 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3693 if (token->type != CPP_SCOPE
3694 && !cp_parser_nth_token_starts_template_argument_list_p
3699 /* The nested-name-specifier is optional, so we parse
3701 cp_parser_parse_tentatively (parser);
3703 /* Look for the optional `template' keyword, if this isn't the
3704 first time through the loop. */
3706 template_keyword_p = cp_parser_optional_template_keyword (parser);
3708 template_keyword_p = false;
3710 /* Save the old scope since the name lookup we are about to do
3711 might destroy it. */
3712 old_scope = parser->scope;
3713 saved_qualifying_scope = parser->qualifying_scope;
3714 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3715 look up names in "X<T>::I" in order to determine that "Y" is
3716 a template. So, if we have a typename at this point, we make
3717 an effort to look through it. */
3719 && !typename_keyword_p
3721 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3722 parser->scope = resolve_typename_type (parser->scope,
3723 /*only_current_p=*/false);
3724 /* Parse the qualifying entity. */
3726 = cp_parser_class_or_namespace_name (parser,
3732 /* Look for the `::' token. */
3733 cp_parser_require (parser, CPP_SCOPE, "`::'");
3735 /* If we found what we wanted, we keep going; otherwise, we're
3737 if (!cp_parser_parse_definitely (parser))
3739 bool error_p = false;
3741 /* Restore the OLD_SCOPE since it was valid before the
3742 failed attempt at finding the last
3743 class-or-namespace-name. */
3744 parser->scope = old_scope;
3745 parser->qualifying_scope = saved_qualifying_scope;
3746 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3748 /* If the next token is an identifier, and the one after
3749 that is a `::', then any valid interpretation would have
3750 found a class-or-namespace-name. */
3751 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3752 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3754 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3757 token = cp_lexer_consume_token (parser->lexer);
3760 if (!token->ambiguous_p)
3763 tree ambiguous_decls;
3765 decl = cp_parser_lookup_name (parser, token->value,
3767 /*is_template=*/false,
3768 /*is_namespace=*/false,
3769 /*check_dependency=*/true,
3771 if (TREE_CODE (decl) == TEMPLATE_DECL)
3772 error ("%qD used without template parameters", decl);
3773 else if (ambiguous_decls)
3775 error ("reference to %qD is ambiguous",
3777 print_candidates (ambiguous_decls);
3778 decl = error_mark_node;
3781 cp_parser_name_lookup_error
3782 (parser, token->value, decl,
3783 "is not a class or namespace");
3785 parser->scope = error_mark_node;
3787 /* Treat this as a successful nested-name-specifier
3792 If the name found is not a class-name (clause
3793 _class_) or namespace-name (_namespace.def_), the
3794 program is ill-formed. */
3797 cp_lexer_consume_token (parser->lexer);
3801 /* We've found one valid nested-name-specifier. */
3803 /* Name lookup always gives us a DECL. */
3804 if (TREE_CODE (new_scope) == TYPE_DECL)
3805 new_scope = TREE_TYPE (new_scope);
3806 /* Uses of "template" must be followed by actual templates. */
3807 if (template_keyword_p
3808 && !(CLASS_TYPE_P (new_scope)
3809 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3810 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3811 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3812 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3813 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3814 == TEMPLATE_ID_EXPR)))
3815 pedwarn (TYPE_P (new_scope)
3816 ? "%qT is not a template"
3817 : "%qD is not a template",
3819 /* If it is a class scope, try to complete it; we are about to
3820 be looking up names inside the class. */
3821 if (TYPE_P (new_scope)
3822 /* Since checking types for dependency can be expensive,
3823 avoid doing it if the type is already complete. */
3824 && !COMPLETE_TYPE_P (new_scope)
3825 /* Do not try to complete dependent types. */
3826 && !dependent_type_p (new_scope))
3827 new_scope = complete_type (new_scope);
3828 /* Make sure we look in the right scope the next time through
3830 parser->scope = new_scope;
3833 /* If parsing tentatively, replace the sequence of tokens that makes
3834 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3835 token. That way, should we re-parse the token stream, we will
3836 not have to repeat the effort required to do the parse, nor will
3837 we issue duplicate error messages. */
3838 if (success && start)
3843 token = cp_lexer_token_at (parser->lexer, start);
3844 /* Reset the contents of the START token. */
3845 token->type = CPP_NESTED_NAME_SPECIFIER;
3846 /* Retrieve any deferred checks. Do not pop this access checks yet
3847 so the memory will not be reclaimed during token replacing below. */
3848 access_checks = get_deferred_access_checks ();
3849 token->value = build_tree_list (copy_list (access_checks),
3851 TREE_TYPE (token->value) = parser->qualifying_scope;
3852 token->keyword = RID_MAX;
3854 /* Purge all subsequent tokens. */
3855 cp_lexer_purge_tokens_after (parser->lexer, start);
3859 pop_to_parent_deferring_access_checks ();
3861 return success ? parser->scope : NULL_TREE;
3864 /* Parse a nested-name-specifier. See
3865 cp_parser_nested_name_specifier_opt for details. This function
3866 behaves identically, except that it will an issue an error if no
3867 nested-name-specifier is present. */
3870 cp_parser_nested_name_specifier (cp_parser *parser,
3871 bool typename_keyword_p,
3872 bool check_dependency_p,
3874 bool is_declaration)
3878 /* Look for the nested-name-specifier. */
3879 scope = cp_parser_nested_name_specifier_opt (parser,
3884 /* If it was not present, issue an error message. */
3887 cp_parser_error (parser, "expected nested-name-specifier");
3888 parser->scope = NULL_TREE;
3894 /* Parse a class-or-namespace-name.
3896 class-or-namespace-name:
3900 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3901 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3902 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3903 TYPE_P is TRUE iff the next name should be taken as a class-name,
3904 even the same name is declared to be another entity in the same
3907 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3908 specified by the class-or-namespace-name. If neither is found the
3909 ERROR_MARK_NODE is returned. */
3912 cp_parser_class_or_namespace_name (cp_parser *parser,
3913 bool typename_keyword_p,
3914 bool template_keyword_p,
3915 bool check_dependency_p,
3917 bool is_declaration)
3920 tree saved_qualifying_scope;
3921 tree saved_object_scope;
3925 /* Before we try to parse the class-name, we must save away the
3926 current PARSER->SCOPE since cp_parser_class_name will destroy
3928 saved_scope = parser->scope;
3929 saved_qualifying_scope = parser->qualifying_scope;
3930 saved_object_scope = parser->object_scope;
3931 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3932 there is no need to look for a namespace-name. */
3933 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3935 cp_parser_parse_tentatively (parser);
3936 scope = cp_parser_class_name (parser,
3939 type_p ? class_type : none_type,
3941 /*class_head_p=*/false,
3943 /* If that didn't work, try for a namespace-name. */
3944 if (!only_class_p && !cp_parser_parse_definitely (parser))
3946 /* Restore the saved scope. */
3947 parser->scope = saved_scope;
3948 parser->qualifying_scope = saved_qualifying_scope;
3949 parser->object_scope = saved_object_scope;
3950 /* If we are not looking at an identifier followed by the scope
3951 resolution operator, then this is not part of a
3952 nested-name-specifier. (Note that this function is only used
3953 to parse the components of a nested-name-specifier.) */
3954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3955 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3956 return error_mark_node;
3957 scope = cp_parser_namespace_name (parser);
3963 /* Parse a postfix-expression.
3967 postfix-expression [ expression ]
3968 postfix-expression ( expression-list [opt] )
3969 simple-type-specifier ( expression-list [opt] )
3970 typename :: [opt] nested-name-specifier identifier
3971 ( expression-list [opt] )
3972 typename :: [opt] nested-name-specifier template [opt] template-id
3973 ( expression-list [opt] )
3974 postfix-expression . template [opt] id-expression
3975 postfix-expression -> template [opt] id-expression
3976 postfix-expression . pseudo-destructor-name
3977 postfix-expression -> pseudo-destructor-name
3978 postfix-expression ++
3979 postfix-expression --
3980 dynamic_cast < type-id > ( expression )
3981 static_cast < type-id > ( expression )
3982 reinterpret_cast < type-id > ( expression )
3983 const_cast < type-id > ( expression )
3984 typeid ( expression )
3990 ( type-id ) { initializer-list , [opt] }
3992 This extension is a GNU version of the C99 compound-literal
3993 construct. (The C99 grammar uses `type-name' instead of `type-id',
3994 but they are essentially the same concept.)
3996 If ADDRESS_P is true, the postfix expression is the operand of the
3997 `&' operator. CAST_P is true if this expression is the target of a
4000 Returns a representation of the expression. */
4003 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4007 cp_id_kind idk = CP_ID_KIND_NONE;
4008 tree postfix_expression = NULL_TREE;
4010 /* Peek at the next token. */
4011 token = cp_lexer_peek_token (parser->lexer);
4012 /* Some of the productions are determined by keywords. */
4013 keyword = token->keyword;
4023 const char *saved_message;
4025 /* All of these can be handled in the same way from the point
4026 of view of parsing. Begin by consuming the token
4027 identifying the cast. */
4028 cp_lexer_consume_token (parser->lexer);
4030 /* New types cannot be defined in the cast. */
4031 saved_message = parser->type_definition_forbidden_message;
4032 parser->type_definition_forbidden_message
4033 = "types may not be defined in casts";
4035 /* Look for the opening `<'. */
4036 cp_parser_require (parser, CPP_LESS, "`<'");
4037 /* Parse the type to which we are casting. */
4038 type = cp_parser_type_id (parser);
4039 /* Look for the closing `>'. */
4040 cp_parser_require (parser, CPP_GREATER, "`>'");
4041 /* Restore the old message. */
4042 parser->type_definition_forbidden_message = saved_message;
4044 /* And the expression which is being cast. */
4045 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4046 expression = cp_parser_expression (parser, /*cast_p=*/true);
4047 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4049 /* Only type conversions to integral or enumeration types
4050 can be used in constant-expressions. */
4051 if (!cast_valid_in_integral_constant_expression_p (type)
4052 && (cp_parser_non_integral_constant_expression
4054 "a cast to a type other than an integral or "
4055 "enumeration type")))
4056 return error_mark_node;
4062 = build_dynamic_cast (type, expression);
4066 = build_static_cast (type, expression);
4070 = build_reinterpret_cast (type, expression);
4074 = build_const_cast (type, expression);
4085 const char *saved_message;
4086 bool saved_in_type_id_in_expr_p;
4088 /* Consume the `typeid' token. */
4089 cp_lexer_consume_token (parser->lexer);
4090 /* Look for the `(' token. */
4091 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4092 /* Types cannot be defined in a `typeid' expression. */
4093 saved_message = parser->type_definition_forbidden_message;
4094 parser->type_definition_forbidden_message
4095 = "types may not be defined in a `typeid\' expression";
4096 /* We can't be sure yet whether we're looking at a type-id or an
4098 cp_parser_parse_tentatively (parser);
4099 /* Try a type-id first. */
4100 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4101 parser->in_type_id_in_expr_p = true;
4102 type = cp_parser_type_id (parser);
4103 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4104 /* Look for the `)' token. Otherwise, we can't be sure that
4105 we're not looking at an expression: consider `typeid (int
4106 (3))', for example. */
4107 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4108 /* If all went well, simply lookup the type-id. */
4109 if (cp_parser_parse_definitely (parser))
4110 postfix_expression = get_typeid (type);
4111 /* Otherwise, fall back to the expression variant. */
4116 /* Look for an expression. */
4117 expression = cp_parser_expression (parser, /*cast_p=*/false);
4118 /* Compute its typeid. */
4119 postfix_expression = build_typeid (expression);
4120 /* Look for the `)' token. */
4121 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4123 /* Restore the saved message. */
4124 parser->type_definition_forbidden_message = saved_message;
4125 /* `typeid' may not appear in an integral constant expression. */
4126 if (cp_parser_non_integral_constant_expression(parser,
4127 "`typeid' operator"))
4128 return error_mark_node;
4135 /* The syntax permitted here is the same permitted for an
4136 elaborated-type-specifier. */
4137 type = cp_parser_elaborated_type_specifier (parser,
4138 /*is_friend=*/false,
4139 /*is_declaration=*/false);
4140 postfix_expression = cp_parser_functional_cast (parser, type);
4148 /* If the next thing is a simple-type-specifier, we may be
4149 looking at a functional cast. We could also be looking at
4150 an id-expression. So, we try the functional cast, and if
4151 that doesn't work we fall back to the primary-expression. */
4152 cp_parser_parse_tentatively (parser);
4153 /* Look for the simple-type-specifier. */
4154 type = cp_parser_simple_type_specifier (parser,
4155 /*decl_specs=*/NULL,
4156 CP_PARSER_FLAGS_NONE);
4157 /* Parse the cast itself. */
4158 if (!cp_parser_error_occurred (parser))
4160 = cp_parser_functional_cast (parser, type);
4161 /* If that worked, we're done. */
4162 if (cp_parser_parse_definitely (parser))
4165 /* If the functional-cast didn't work out, try a
4166 compound-literal. */
4167 if (cp_parser_allow_gnu_extensions_p (parser)
4168 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4170 VEC(constructor_elt,gc) *initializer_list = NULL;
4171 bool saved_in_type_id_in_expr_p;
4173 cp_parser_parse_tentatively (parser);
4174 /* Consume the `('. */
4175 cp_lexer_consume_token (parser->lexer);
4176 /* Parse the type. */
4177 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4178 parser->in_type_id_in_expr_p = true;
4179 type = cp_parser_type_id (parser);
4180 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4181 /* Look for the `)'. */
4182 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4183 /* Look for the `{'. */
4184 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4185 /* If things aren't going well, there's no need to
4187 if (!cp_parser_error_occurred (parser))
4189 bool non_constant_p;
4190 /* Parse the initializer-list. */
4192 = cp_parser_initializer_list (parser, &non_constant_p);
4193 /* Allow a trailing `,'. */
4194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Look for the final `}'. */
4197 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4199 /* If that worked, we're definitely looking at a
4200 compound-literal expression. */
4201 if (cp_parser_parse_definitely (parser))
4203 /* Warn the user that a compound literal is not
4204 allowed in standard C++. */
4206 pedwarn ("ISO C++ forbids compound-literals");
4207 /* Form the representation of the compound-literal. */
4209 = finish_compound_literal (type, initializer_list);
4214 /* It must be a primary-expression. */
4216 = cp_parser_primary_expression (parser, address_p, cast_p,
4217 /*template_arg_p=*/false,
4223 /* Keep looping until the postfix-expression is complete. */
4226 if (idk == CP_ID_KIND_UNQUALIFIED
4227 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4228 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4229 /* It is not a Koenig lookup function call. */
4231 = unqualified_name_lookup_error (postfix_expression);
4233 /* Peek at the next token. */
4234 token = cp_lexer_peek_token (parser->lexer);
4236 switch (token->type)
4238 case CPP_OPEN_SQUARE:
4240 = cp_parser_postfix_open_square_expression (parser,
4243 idk = CP_ID_KIND_NONE;
4246 case CPP_OPEN_PAREN:
4247 /* postfix-expression ( expression-list [opt] ) */
4250 bool is_builtin_constant_p;
4251 bool saved_integral_constant_expression_p = false;
4252 bool saved_non_integral_constant_expression_p = false;
4255 is_builtin_constant_p
4256 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4257 if (is_builtin_constant_p)
4259 /* The whole point of __builtin_constant_p is to allow
4260 non-constant expressions to appear as arguments. */
4261 saved_integral_constant_expression_p
4262 = parser->integral_constant_expression_p;
4263 saved_non_integral_constant_expression_p
4264 = parser->non_integral_constant_expression_p;
4265 parser->integral_constant_expression_p = false;
4267 args = (cp_parser_parenthesized_expression_list
4268 (parser, /*is_attribute_list=*/false,
4270 /*non_constant_p=*/NULL));
4271 if (is_builtin_constant_p)
4273 parser->integral_constant_expression_p
4274 = saved_integral_constant_expression_p;
4275 parser->non_integral_constant_expression_p
4276 = saved_non_integral_constant_expression_p;
4279 if (args == error_mark_node)
4281 postfix_expression = error_mark_node;
4285 /* Function calls are not permitted in
4286 constant-expressions. */
4287 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4288 && cp_parser_non_integral_constant_expression (parser,
4291 postfix_expression = error_mark_node;
4296 if (idk == CP_ID_KIND_UNQUALIFIED)
4298 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4304 = perform_koenig_lookup (postfix_expression, args);
4308 = unqualified_fn_lookup_error (postfix_expression);
4310 /* We do not perform argument-dependent lookup if
4311 normal lookup finds a non-function, in accordance
4312 with the expected resolution of DR 218. */
4313 else if (args && is_overloaded_fn (postfix_expression))
4315 tree fn = get_first_fn (postfix_expression);
4317 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4318 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4320 /* Only do argument dependent lookup if regular
4321 lookup does not find a set of member functions.
4322 [basic.lookup.koenig]/2a */
4323 if (!DECL_FUNCTION_MEMBER_P (fn))
4327 = perform_koenig_lookup (postfix_expression, args);
4332 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4334 tree instance = TREE_OPERAND (postfix_expression, 0);
4335 tree fn = TREE_OPERAND (postfix_expression, 1);
4337 if (processing_template_decl
4338 && (type_dependent_expression_p (instance)
4339 || (!BASELINK_P (fn)
4340 && TREE_CODE (fn) != FIELD_DECL)
4341 || type_dependent_expression_p (fn)
4342 || any_type_dependent_arguments_p (args)))
4345 = build_min_nt (CALL_EXPR, postfix_expression,
4350 if (BASELINK_P (fn))
4352 = (build_new_method_call
4353 (instance, fn, args, NULL_TREE,
4354 (idk == CP_ID_KIND_QUALIFIED
4355 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4359 = finish_call_expr (postfix_expression, args,
4360 /*disallow_virtual=*/false,
4361 /*koenig_p=*/false);
4363 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4364 || TREE_CODE (postfix_expression) == MEMBER_REF
4365 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4366 postfix_expression = (build_offset_ref_call_from_tree
4367 (postfix_expression, args));
4368 else if (idk == CP_ID_KIND_QUALIFIED)
4369 /* A call to a static class member, or a namespace-scope
4372 = finish_call_expr (postfix_expression, args,
4373 /*disallow_virtual=*/true,
4376 /* All other function calls. */
4378 = finish_call_expr (postfix_expression, args,
4379 /*disallow_virtual=*/false,
4382 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4383 idk = CP_ID_KIND_NONE;
4389 /* postfix-expression . template [opt] id-expression
4390 postfix-expression . pseudo-destructor-name
4391 postfix-expression -> template [opt] id-expression
4392 postfix-expression -> pseudo-destructor-name */
4394 /* Consume the `.' or `->' operator. */
4395 cp_lexer_consume_token (parser->lexer);
4398 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4404 /* postfix-expression ++ */
4405 /* Consume the `++' token. */
4406 cp_lexer_consume_token (parser->lexer);
4407 /* Generate a representation for the complete expression. */
4409 = finish_increment_expr (postfix_expression,
4410 POSTINCREMENT_EXPR);
4411 /* Increments may not appear in constant-expressions. */
4412 if (cp_parser_non_integral_constant_expression (parser,
4414 postfix_expression = error_mark_node;
4415 idk = CP_ID_KIND_NONE;
4418 case CPP_MINUS_MINUS:
4419 /* postfix-expression -- */
4420 /* Consume the `--' token. */
4421 cp_lexer_consume_token (parser->lexer);
4422 /* Generate a representation for the complete expression. */
4424 = finish_increment_expr (postfix_expression,
4425 POSTDECREMENT_EXPR);
4426 /* Decrements may not appear in constant-expressions. */
4427 if (cp_parser_non_integral_constant_expression (parser,
4429 postfix_expression = error_mark_node;
4430 idk = CP_ID_KIND_NONE;
4434 return postfix_expression;
4438 /* We should never get here. */
4440 return error_mark_node;
4443 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4444 by cp_parser_builtin_offsetof. We're looking for
4446 postfix-expression [ expression ]
4448 FOR_OFFSETOF is set if we're being called in that context, which
4449 changes how we deal with integer constant expressions. */
4452 cp_parser_postfix_open_square_expression (cp_parser *parser,
4453 tree postfix_expression,
4458 /* Consume the `[' token. */
4459 cp_lexer_consume_token (parser->lexer);
4461 /* Parse the index expression. */
4462 /* ??? For offsetof, there is a question of what to allow here. If
4463 offsetof is not being used in an integral constant expression context,
4464 then we *could* get the right answer by computing the value at runtime.
4465 If we are in an integral constant expression context, then we might
4466 could accept any constant expression; hard to say without analysis.
4467 Rather than open the barn door too wide right away, allow only integer
4468 constant expressions here. */
4470 index = cp_parser_constant_expression (parser, false, NULL);
4472 index = cp_parser_expression (parser, /*cast_p=*/false);
4474 /* Look for the closing `]'. */
4475 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4477 /* Build the ARRAY_REF. */
4478 postfix_expression = grok_array_decl (postfix_expression, index);
4480 /* When not doing offsetof, array references are not permitted in
4481 constant-expressions. */
4483 && (cp_parser_non_integral_constant_expression
4484 (parser, "an array reference")))
4485 postfix_expression = error_mark_node;
4487 return postfix_expression;
4490 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4491 by cp_parser_builtin_offsetof. We're looking for
4493 postfix-expression . template [opt] id-expression
4494 postfix-expression . pseudo-destructor-name
4495 postfix-expression -> template [opt] id-expression
4496 postfix-expression -> pseudo-destructor-name
4498 FOR_OFFSETOF is set if we're being called in that context. That sorta
4499 limits what of the above we'll actually accept, but nevermind.
4500 TOKEN_TYPE is the "." or "->" token, which will already have been
4501 removed from the stream. */
4504 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4505 enum cpp_ttype token_type,
4506 tree postfix_expression,
4507 bool for_offsetof, cp_id_kind *idk)
4511 bool pseudo_destructor_p;
4512 tree scope = NULL_TREE;
4514 /* If this is a `->' operator, dereference the pointer. */
4515 if (token_type == CPP_DEREF)
4516 postfix_expression = build_x_arrow (postfix_expression);
4517 /* Check to see whether or not the expression is type-dependent. */
4518 dependent_p = type_dependent_expression_p (postfix_expression);
4519 /* The identifier following the `->' or `.' is not qualified. */
4520 parser->scope = NULL_TREE;
4521 parser->qualifying_scope = NULL_TREE;
4522 parser->object_scope = NULL_TREE;
4523 *idk = CP_ID_KIND_NONE;
4525 /* Enter the scope corresponding to the type of the object
4526 given by the POSTFIX_EXPRESSION. */
4527 scope = TREE_TYPE (postfix_expression);
4528 if (!dependent_p && scope)
4530 /* According to the standard, no expression should ever have
4531 reference type. Unfortunately, we do not currently match
4532 the standard in this respect in that our internal representation
4533 of an expression may have reference type even when the standard
4534 says it does not. Therefore, we have to manually obtain the
4535 underlying type here. */
4536 scope = non_reference (scope);
4537 /* The type of the POSTFIX_EXPRESSION must be complete. */
4538 if (scope == unknown_type_node)
4540 error ("%qE does not have class type", postfix_expression);
4543 else if (!dependent_p)
4544 scope = complete_type_or_else (scope, NULL_TREE);
4545 /* If something went wrong, we want to be able to discern that case,
4546 as opposed to the case where there was no SCOPE due to the type
4547 of expression being dependent. */
4549 scope = error_mark_node;
4550 /* If the SCOPE was erroneous, make the various semantic analysis
4551 functions exit quickly -- and without issuing additional error
4553 if (scope == error_mark_node)
4554 postfix_expression = error_mark_node;
4556 /* Let the name lookup machinery know that we are processing a class
4557 member access expression. */
4558 parser->context->object_type = scope;
4559 parser->object_scope = scope;
4561 /* Assume this expression is not a pseudo-destructor access. */
4562 pseudo_destructor_p = false;
4564 /* If the SCOPE is a scalar type, then, if this is a valid program,
4565 we must be looking at a pseudo-destructor-name. */
4566 if (scope && SCALAR_TYPE_P (scope))
4571 cp_parser_parse_tentatively (parser);
4572 /* Parse the pseudo-destructor-name. */
4574 cp_parser_pseudo_destructor_name (parser, &s, &type);
4575 if (cp_parser_parse_definitely (parser))
4577 pseudo_destructor_p = true;
4579 = finish_pseudo_destructor_expr (postfix_expression,
4580 s, TREE_TYPE (type));
4584 if (!pseudo_destructor_p)
4586 /* If the SCOPE is not a scalar type, we are looking at an
4587 ordinary class member access expression, rather than a
4588 pseudo-destructor-name. */
4590 /* Parse the id-expression. */
4591 name = (cp_parser_id_expression
4593 cp_parser_optional_template_keyword (parser),
4594 /*check_dependency_p=*/true,
4596 /*declarator_p=*/false,
4597 /*optional_p=*/false,
4598 /*member_p=*/true));
4599 /* In general, build a SCOPE_REF if the member name is qualified.
4600 However, if the name was not dependent and has already been
4601 resolved; there is no need to build the SCOPE_REF. For example;
4603 struct X { void f(); };
4604 template <typename T> void f(T* t) { t->X::f(); }
4606 Even though "t" is dependent, "X::f" is not and has been resolved
4607 to a BASELINK; there is no need to include scope information. */
4609 /* But we do need to remember that there was an explicit scope for
4610 virtual function calls. */
4612 *idk = CP_ID_KIND_QUALIFIED;
4614 /* If the name is a template-id that names a type, we will get a
4615 TYPE_DECL here. That is invalid code. */
4616 if (TREE_CODE (name) == TYPE_DECL)
4618 error ("invalid use of %qD", name);
4619 postfix_expression = error_mark_node;
4623 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4625 name = build_qualified_name (/*type=*/NULL_TREE,
4629 parser->scope = NULL_TREE;
4630 parser->qualifying_scope = NULL_TREE;
4631 parser->object_scope = NULL_TREE;
4633 if (scope && name && BASELINK_P (name))
4634 adjust_result_of_qualified_name_lookup
4635 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4637 = finish_class_member_access_expr (postfix_expression, name,
4642 /* We no longer need to look up names in the scope of the object on
4643 the left-hand side of the `.' or `->' operator. */
4644 parser->context->object_type = NULL_TREE;
4646 /* Outside of offsetof, these operators may not appear in
4647 constant-expressions. */
4649 && (cp_parser_non_integral_constant_expression
4650 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4651 postfix_expression = error_mark_node;
4653 return postfix_expression;
4656 /* Parse a parenthesized expression-list.
4659 assignment-expression
4660 expression-list, assignment-expression
4665 identifier, expression-list
4667 CAST_P is true if this expression is the target of a cast.
4669 Returns a TREE_LIST. The TREE_VALUE of each node is a
4670 representation of an assignment-expression. Note that a TREE_LIST
4671 is returned even if there is only a single expression in the list.
4672 error_mark_node is returned if the ( and or ) are
4673 missing. NULL_TREE is returned on no expressions. The parentheses
4674 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4675 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4676 indicates whether or not all of the expressions in the list were
4680 cp_parser_parenthesized_expression_list (cp_parser* parser,
4681 bool is_attribute_list,
4683 bool *non_constant_p)
4685 tree expression_list = NULL_TREE;
4686 bool fold_expr_p = is_attribute_list;
4687 tree identifier = NULL_TREE;
4689 /* Assume all the expressions will be constant. */
4691 *non_constant_p = false;
4693 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4694 return error_mark_node;
4696 /* Consume expressions until there are no more. */
4697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4702 /* At the beginning of attribute lists, check to see if the
4703 next token is an identifier. */
4704 if (is_attribute_list
4705 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4709 /* Consume the identifier. */
4710 token = cp_lexer_consume_token (parser->lexer);
4711 /* Save the identifier. */
4712 identifier = token->value;
4716 /* Parse the next assignment-expression. */
4719 bool expr_non_constant_p;
4720 expr = (cp_parser_constant_expression
4721 (parser, /*allow_non_constant_p=*/true,
4722 &expr_non_constant_p));
4723 if (expr_non_constant_p)
4724 *non_constant_p = true;
4727 expr = cp_parser_assignment_expression (parser, cast_p);
4730 expr = fold_non_dependent_expr (expr);
4732 /* Add it to the list. We add error_mark_node
4733 expressions to the list, so that we can still tell if
4734 the correct form for a parenthesized expression-list
4735 is found. That gives better errors. */
4736 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4738 if (expr == error_mark_node)
4742 /* After the first item, attribute lists look the same as
4743 expression lists. */
4744 is_attribute_list = false;
4747 /* If the next token isn't a `,', then we are done. */
4748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4751 /* Otherwise, consume the `,' and keep going. */
4752 cp_lexer_consume_token (parser->lexer);
4755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4760 /* We try and resync to an unnested comma, as that will give the
4761 user better diagnostics. */
4762 ending = cp_parser_skip_to_closing_parenthesis (parser,
4763 /*recovering=*/true,
4765 /*consume_paren=*/true);
4769 return error_mark_node;
4772 /* We built up the list in reverse order so we must reverse it now. */
4773 expression_list = nreverse (expression_list);
4775 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4777 return expression_list;
4780 /* Parse a pseudo-destructor-name.
4782 pseudo-destructor-name:
4783 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4784 :: [opt] nested-name-specifier template template-id :: ~ type-name
4785 :: [opt] nested-name-specifier [opt] ~ type-name
4787 If either of the first two productions is used, sets *SCOPE to the
4788 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4789 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4790 or ERROR_MARK_NODE if the parse fails. */
4793 cp_parser_pseudo_destructor_name (cp_parser* parser,
4797 bool nested_name_specifier_p;
4799 /* Assume that things will not work out. */
4800 *type = error_mark_node;
4802 /* Look for the optional `::' operator. */
4803 cp_parser_global_scope_opt (parser,
4804 /*current_scope_valid_p=*/true,
4805 /*object_scop_valid_p=*/true);
4806 /* Look for the optional nested-name-specifier. */
4807 nested_name_specifier_p
4808 = (cp_parser_nested_name_specifier_opt (parser,
4809 /*typename_keyword_p=*/false,
4810 /*check_dependency_p=*/true,
4812 /*is_declaration=*/true)
4814 /* Now, if we saw a nested-name-specifier, we might be doing the
4815 second production. */
4816 if (nested_name_specifier_p
4817 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4819 /* Consume the `template' keyword. */
4820 cp_lexer_consume_token (parser->lexer);
4821 /* Parse the template-id. */
4822 cp_parser_template_id (parser,
4823 /*template_keyword_p=*/true,
4824 /*check_dependency_p=*/false,
4825 /*is_declaration=*/true);
4826 /* Look for the `::' token. */
4827 cp_parser_require (parser, CPP_SCOPE, "`::'");
4829 /* If the next token is not a `~', then there might be some
4830 additional qualification. */
4831 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4833 /* Look for the type-name. */
4834 *scope = TREE_TYPE (cp_parser_type_name (parser));
4836 if (*scope == error_mark_node)
4839 /* If we don't have ::~, then something has gone wrong. Since
4840 the only caller of this function is looking for something
4841 after `.' or `->' after a scalar type, most likely the
4842 program is trying to get a member of a non-aggregate
4844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4845 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4847 cp_parser_error (parser, "request for member of non-aggregate type");
4851 /* Look for the `::' token. */
4852 cp_parser_require (parser, CPP_SCOPE, "`::'");
4857 /* Look for the `~'. */
4858 cp_parser_require (parser, CPP_COMPL, "`~'");
4859 /* Look for the type-name again. We are not responsible for
4860 checking that it matches the first type-name. */
4861 *type = cp_parser_type_name (parser);
4864 /* Parse a unary-expression.
4870 unary-operator cast-expression
4871 sizeof unary-expression
4879 __extension__ cast-expression
4880 __alignof__ unary-expression
4881 __alignof__ ( type-id )
4882 __real__ cast-expression
4883 __imag__ cast-expression
4886 ADDRESS_P is true iff the unary-expression is appearing as the
4887 operand of the `&' operator. CAST_P is true if this expression is
4888 the target of a cast.
4890 Returns a representation of the expression. */
4893 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4896 enum tree_code unary_operator;
4898 /* Peek at the next token. */
4899 token = cp_lexer_peek_token (parser->lexer);
4900 /* Some keywords give away the kind of expression. */
4901 if (token->type == CPP_KEYWORD)
4903 enum rid keyword = token->keyword;
4913 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4914 /* Consume the token. */
4915 cp_lexer_consume_token (parser->lexer);
4916 /* Parse the operand. */
4917 operand = cp_parser_sizeof_operand (parser, keyword);
4919 if (TYPE_P (operand))
4920 return cxx_sizeof_or_alignof_type (operand, op, true);
4922 return cxx_sizeof_or_alignof_expr (operand, op);
4926 return cp_parser_new_expression (parser);
4929 return cp_parser_delete_expression (parser);
4933 /* The saved value of the PEDANTIC flag. */
4937 /* Save away the PEDANTIC flag. */
4938 cp_parser_extension_opt (parser, &saved_pedantic);
4939 /* Parse the cast-expression. */
4940 expr = cp_parser_simple_cast_expression (parser);
4941 /* Restore the PEDANTIC flag. */
4942 pedantic = saved_pedantic;
4952 /* Consume the `__real__' or `__imag__' token. */
4953 cp_lexer_consume_token (parser->lexer);
4954 /* Parse the cast-expression. */
4955 expression = cp_parser_simple_cast_expression (parser);
4956 /* Create the complete representation. */
4957 return build_x_unary_op ((keyword == RID_REALPART
4958 ? REALPART_EXPR : IMAGPART_EXPR),
4968 /* Look for the `:: new' and `:: delete', which also signal the
4969 beginning of a new-expression, or delete-expression,
4970 respectively. If the next token is `::', then it might be one of
4972 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4976 /* See if the token after the `::' is one of the keywords in
4977 which we're interested. */
4978 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4979 /* If it's `new', we have a new-expression. */
4980 if (keyword == RID_NEW)
4981 return cp_parser_new_expression (parser);
4982 /* Similarly, for `delete'. */
4983 else if (keyword == RID_DELETE)
4984 return cp_parser_delete_expression (parser);
4987 /* Look for a unary operator. */
4988 unary_operator = cp_parser_unary_operator (token);
4989 /* The `++' and `--' operators can be handled similarly, even though
4990 they are not technically unary-operators in the grammar. */
4991 if (unary_operator == ERROR_MARK)
4993 if (token->type == CPP_PLUS_PLUS)
4994 unary_operator = PREINCREMENT_EXPR;
4995 else if (token->type == CPP_MINUS_MINUS)
4996 unary_operator = PREDECREMENT_EXPR;
4997 /* Handle the GNU address-of-label extension. */
4998 else if (cp_parser_allow_gnu_extensions_p (parser)
4999 && token->type == CPP_AND_AND)
5003 /* Consume the '&&' token. */
5004 cp_lexer_consume_token (parser->lexer);
5005 /* Look for the identifier. */
5006 identifier = cp_parser_identifier (parser);
5007 /* Create an expression representing the address. */
5008 return finish_label_address_expr (identifier);
5011 if (unary_operator != ERROR_MARK)
5013 tree cast_expression;
5014 tree expression = error_mark_node;
5015 const char *non_constant_p = NULL;
5017 /* Consume the operator token. */
5018 token = cp_lexer_consume_token (parser->lexer);
5019 /* Parse the cast-expression. */
5021 = cp_parser_cast_expression (parser,
5022 unary_operator == ADDR_EXPR,
5024 /* Now, build an appropriate representation. */
5025 switch (unary_operator)
5028 non_constant_p = "`*'";
5029 expression = build_x_indirect_ref (cast_expression, "unary *");
5033 non_constant_p = "`&'";
5036 expression = build_x_unary_op (unary_operator, cast_expression);
5039 case PREINCREMENT_EXPR:
5040 case PREDECREMENT_EXPR:
5041 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5044 case UNARY_PLUS_EXPR:
5046 case TRUTH_NOT_EXPR:
5047 expression = finish_unary_op_expr (unary_operator, cast_expression);
5055 && cp_parser_non_integral_constant_expression (parser,
5057 expression = error_mark_node;
5062 return cp_parser_postfix_expression (parser, address_p, cast_p);
5065 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5066 unary-operator, the corresponding tree code is returned. */
5068 static enum tree_code
5069 cp_parser_unary_operator (cp_token* token)
5071 switch (token->type)
5074 return INDIRECT_REF;
5080 return UNARY_PLUS_EXPR;
5086 return TRUTH_NOT_EXPR;
5089 return BIT_NOT_EXPR;
5096 /* Parse a new-expression.
5099 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5100 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5102 Returns a representation of the expression. */
5105 cp_parser_new_expression (cp_parser* parser)
5107 bool global_scope_p;
5113 /* Look for the optional `::' operator. */
5115 = (cp_parser_global_scope_opt (parser,
5116 /*current_scope_valid_p=*/false,
5117 /*object_scope_valid_p=*/false)
5119 /* Look for the `new' operator. */
5120 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5121 /* There's no easy way to tell a new-placement from the
5122 `( type-id )' construct. */
5123 cp_parser_parse_tentatively (parser);
5124 /* Look for a new-placement. */
5125 placement = cp_parser_new_placement (parser);
5126 /* If that didn't work out, there's no new-placement. */
5127 if (!cp_parser_parse_definitely (parser))
5128 placement = NULL_TREE;
5130 /* If the next token is a `(', then we have a parenthesized
5132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5134 /* Consume the `('. */
5135 cp_lexer_consume_token (parser->lexer);
5136 /* Parse the type-id. */
5137 type = cp_parser_type_id (parser);
5138 /* Look for the closing `)'. */
5139 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5140 /* There should not be a direct-new-declarator in this production,
5141 but GCC used to allowed this, so we check and emit a sensible error
5142 message for this case. */
5143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5145 error ("array bound forbidden after parenthesized type-id");
5146 inform ("try removing the parentheses around the type-id");
5147 cp_parser_direct_new_declarator (parser);
5151 /* Otherwise, there must be a new-type-id. */
5153 type = cp_parser_new_type_id (parser, &nelts);
5155 /* If the next token is a `(', then we have a new-initializer. */
5156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5157 initializer = cp_parser_new_initializer (parser);
5159 initializer = NULL_TREE;
5161 /* A new-expression may not appear in an integral constant
5163 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5164 return error_mark_node;
5166 /* Create a representation of the new-expression. */
5167 return build_new (placement, type, nelts, initializer, global_scope_p);
5170 /* Parse a new-placement.
5175 Returns the same representation as for an expression-list. */
5178 cp_parser_new_placement (cp_parser* parser)
5180 tree expression_list;
5182 /* Parse the expression-list. */
5183 expression_list = (cp_parser_parenthesized_expression_list
5184 (parser, false, /*cast_p=*/false,
5185 /*non_constant_p=*/NULL));
5187 return expression_list;
5190 /* Parse a new-type-id.
5193 type-specifier-seq new-declarator [opt]
5195 Returns the TYPE allocated. If the new-type-id indicates an array
5196 type, *NELTS is set to the number of elements in the last array
5197 bound; the TYPE will not include the last array bound. */
5200 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5202 cp_decl_specifier_seq type_specifier_seq;
5203 cp_declarator *new_declarator;
5204 cp_declarator *declarator;
5205 cp_declarator *outer_declarator;
5206 const char *saved_message;
5209 /* The type-specifier sequence must not contain type definitions.
5210 (It cannot contain declarations of new types either, but if they
5211 are not definitions we will catch that because they are not
5213 saved_message = parser->type_definition_forbidden_message;
5214 parser->type_definition_forbidden_message
5215 = "types may not be defined in a new-type-id";
5216 /* Parse the type-specifier-seq. */
5217 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5218 &type_specifier_seq);
5219 /* Restore the old message. */
5220 parser->type_definition_forbidden_message = saved_message;
5221 /* Parse the new-declarator. */
5222 new_declarator = cp_parser_new_declarator_opt (parser);
5224 /* Determine the number of elements in the last array dimension, if
5227 /* Skip down to the last array dimension. */
5228 declarator = new_declarator;
5229 outer_declarator = NULL;
5230 while (declarator && (declarator->kind == cdk_pointer
5231 || declarator->kind == cdk_ptrmem))
5233 outer_declarator = declarator;
5234 declarator = declarator->declarator;
5237 && declarator->kind == cdk_array
5238 && declarator->declarator
5239 && declarator->declarator->kind == cdk_array)
5241 outer_declarator = declarator;
5242 declarator = declarator->declarator;
5245 if (declarator && declarator->kind == cdk_array)
5247 *nelts = declarator->u.array.bounds;
5248 if (*nelts == error_mark_node)
5249 *nelts = integer_one_node;
5251 if (outer_declarator)
5252 outer_declarator->declarator = declarator->declarator;
5254 new_declarator = NULL;
5257 type = groktypename (&type_specifier_seq, new_declarator);
5258 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5260 *nelts = array_type_nelts_top (type);
5261 type = TREE_TYPE (type);
5266 /* Parse an (optional) new-declarator.
5269 ptr-operator new-declarator [opt]
5270 direct-new-declarator
5272 Returns the declarator. */
5274 static cp_declarator *
5275 cp_parser_new_declarator_opt (cp_parser* parser)
5277 enum tree_code code;
5279 cp_cv_quals cv_quals;
5281 /* We don't know if there's a ptr-operator next, or not. */
5282 cp_parser_parse_tentatively (parser);
5283 /* Look for a ptr-operator. */
5284 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5285 /* If that worked, look for more new-declarators. */
5286 if (cp_parser_parse_definitely (parser))
5288 cp_declarator *declarator;
5290 /* Parse another optional declarator. */
5291 declarator = cp_parser_new_declarator_opt (parser);
5293 /* Create the representation of the declarator. */
5295 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5296 else if (code == INDIRECT_REF)
5297 declarator = make_pointer_declarator (cv_quals, declarator);
5299 declarator = make_reference_declarator (cv_quals, declarator);
5304 /* If the next token is a `[', there is a direct-new-declarator. */
5305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5306 return cp_parser_direct_new_declarator (parser);
5311 /* Parse a direct-new-declarator.
5313 direct-new-declarator:
5315 direct-new-declarator [constant-expression]
5319 static cp_declarator *
5320 cp_parser_direct_new_declarator (cp_parser* parser)
5322 cp_declarator *declarator = NULL;
5328 /* Look for the opening `['. */
5329 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5330 /* The first expression is not required to be constant. */
5333 expression = cp_parser_expression (parser, /*cast_p=*/false);
5334 /* The standard requires that the expression have integral
5335 type. DR 74 adds enumeration types. We believe that the
5336 real intent is that these expressions be handled like the
5337 expression in a `switch' condition, which also allows
5338 classes with a single conversion to integral or
5339 enumeration type. */
5340 if (!processing_template_decl)
5343 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5348 error ("expression in new-declarator must have integral "
5349 "or enumeration type");
5350 expression = error_mark_node;
5354 /* But all the other expressions must be. */
5357 = cp_parser_constant_expression (parser,
5358 /*allow_non_constant=*/false,
5360 /* Look for the closing `]'. */
5361 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5363 /* Add this bound to the declarator. */
5364 declarator = make_array_declarator (declarator, expression);
5366 /* If the next token is not a `[', then there are no more
5368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5375 /* Parse a new-initializer.
5378 ( expression-list [opt] )
5380 Returns a representation of the expression-list. If there is no
5381 expression-list, VOID_ZERO_NODE is returned. */
5384 cp_parser_new_initializer (cp_parser* parser)
5386 tree expression_list;
5388 expression_list = (cp_parser_parenthesized_expression_list
5389 (parser, false, /*cast_p=*/false,
5390 /*non_constant_p=*/NULL));
5391 if (!expression_list)
5392 expression_list = void_zero_node;
5394 return expression_list;
5397 /* Parse a delete-expression.
5400 :: [opt] delete cast-expression
5401 :: [opt] delete [ ] cast-expression
5403 Returns a representation of the expression. */
5406 cp_parser_delete_expression (cp_parser* parser)
5408 bool global_scope_p;
5412 /* Look for the optional `::' operator. */
5414 = (cp_parser_global_scope_opt (parser,
5415 /*current_scope_valid_p=*/false,
5416 /*object_scope_valid_p=*/false)
5418 /* Look for the `delete' keyword. */
5419 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5420 /* See if the array syntax is in use. */
5421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5423 /* Consume the `[' token. */
5424 cp_lexer_consume_token (parser->lexer);
5425 /* Look for the `]' token. */
5426 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5427 /* Remember that this is the `[]' construct. */
5433 /* Parse the cast-expression. */
5434 expression = cp_parser_simple_cast_expression (parser);
5436 /* A delete-expression may not appear in an integral constant
5438 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5439 return error_mark_node;
5441 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5444 /* Parse a cast-expression.
5448 ( type-id ) cast-expression
5450 ADDRESS_P is true iff the unary-expression is appearing as the
5451 operand of the `&' operator. CAST_P is true if this expression is
5452 the target of a cast.
5454 Returns a representation of the expression. */
5457 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5459 /* If it's a `(', then we might be looking at a cast. */
5460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5462 tree type = NULL_TREE;
5463 tree expr = NULL_TREE;
5464 bool compound_literal_p;
5465 const char *saved_message;
5467 /* There's no way to know yet whether or not this is a cast.
5468 For example, `(int (3))' is a unary-expression, while `(int)
5469 3' is a cast. So, we resort to parsing tentatively. */
5470 cp_parser_parse_tentatively (parser);
5471 /* Types may not be defined in a cast. */
5472 saved_message = parser->type_definition_forbidden_message;
5473 parser->type_definition_forbidden_message
5474 = "types may not be defined in casts";
5475 /* Consume the `('. */
5476 cp_lexer_consume_token (parser->lexer);
5477 /* A very tricky bit is that `(struct S) { 3 }' is a
5478 compound-literal (which we permit in C++ as an extension).
5479 But, that construct is not a cast-expression -- it is a
5480 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5481 is legal; if the compound-literal were a cast-expression,
5482 you'd need an extra set of parentheses.) But, if we parse
5483 the type-id, and it happens to be a class-specifier, then we
5484 will commit to the parse at that point, because we cannot
5485 undo the action that is done when creating a new class. So,
5486 then we cannot back up and do a postfix-expression.
5488 Therefore, we scan ahead to the closing `)', and check to see
5489 if the token after the `)' is a `{'. If so, we are not
5490 looking at a cast-expression.
5492 Save tokens so that we can put them back. */
5493 cp_lexer_save_tokens (parser->lexer);
5494 /* Skip tokens until the next token is a closing parenthesis.
5495 If we find the closing `)', and the next token is a `{', then
5496 we are looking at a compound-literal. */
5498 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5499 /*consume_paren=*/true)
5500 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5501 /* Roll back the tokens we skipped. */
5502 cp_lexer_rollback_tokens (parser->lexer);
5503 /* If we were looking at a compound-literal, simulate an error
5504 so that the call to cp_parser_parse_definitely below will
5506 if (compound_literal_p)
5507 cp_parser_simulate_error (parser);
5510 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5511 parser->in_type_id_in_expr_p = true;
5512 /* Look for the type-id. */
5513 type = cp_parser_type_id (parser);
5514 /* Look for the closing `)'. */
5515 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5516 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5519 /* Restore the saved message. */
5520 parser->type_definition_forbidden_message = saved_message;
5522 /* If ok so far, parse the dependent expression. We cannot be
5523 sure it is a cast. Consider `(T ())'. It is a parenthesized
5524 ctor of T, but looks like a cast to function returning T
5525 without a dependent expression. */
5526 if (!cp_parser_error_occurred (parser))
5527 expr = cp_parser_cast_expression (parser,
5528 /*address_p=*/false,
5531 if (cp_parser_parse_definitely (parser))
5533 /* Warn about old-style casts, if so requested. */
5534 if (warn_old_style_cast
5535 && !in_system_header
5536 && !VOID_TYPE_P (type)
5537 && current_lang_name != lang_name_c)
5538 warning (OPT_Wold_style_cast, "use of old-style cast");
5540 /* Only type conversions to integral or enumeration types
5541 can be used in constant-expressions. */
5542 if (!cast_valid_in_integral_constant_expression_p (type)
5543 && (cp_parser_non_integral_constant_expression
5545 "a cast to a type other than an integral or "
5546 "enumeration type")))
5547 return error_mark_node;
5549 /* Perform the cast. */
5550 expr = build_c_cast (type, expr);
5555 /* If we get here, then it's not a cast, so it must be a
5556 unary-expression. */
5557 return cp_parser_unary_expression (parser, address_p, cast_p);
5560 /* Parse a binary expression of the general form:
5564 pm-expression .* cast-expression
5565 pm-expression ->* cast-expression
5567 multiplicative-expression:
5569 multiplicative-expression * pm-expression
5570 multiplicative-expression / pm-expression
5571 multiplicative-expression % pm-expression
5573 additive-expression:
5574 multiplicative-expression
5575 additive-expression + multiplicative-expression
5576 additive-expression - multiplicative-expression
5580 shift-expression << additive-expression
5581 shift-expression >> additive-expression
5583 relational-expression:
5585 relational-expression < shift-expression
5586 relational-expression > shift-expression
5587 relational-expression <= shift-expression
5588 relational-expression >= shift-expression
5592 relational-expression:
5593 relational-expression <? shift-expression
5594 relational-expression >? shift-expression
5596 equality-expression:
5597 relational-expression
5598 equality-expression == relational-expression
5599 equality-expression != relational-expression
5603 and-expression & equality-expression
5605 exclusive-or-expression:
5607 exclusive-or-expression ^ and-expression
5609 inclusive-or-expression:
5610 exclusive-or-expression
5611 inclusive-or-expression | exclusive-or-expression
5613 logical-and-expression:
5614 inclusive-or-expression
5615 logical-and-expression && inclusive-or-expression
5617 logical-or-expression:
5618 logical-and-expression
5619 logical-or-expression || logical-and-expression
5621 All these are implemented with a single function like:
5624 simple-cast-expression
5625 binary-expression <token> binary-expression
5627 CAST_P is true if this expression is the target of a cast.
5629 The binops_by_token map is used to get the tree codes for each <token> type.
5630 binary-expressions are associated according to a precedence table. */
5632 #define TOKEN_PRECEDENCE(token) \
5633 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5634 ? PREC_NOT_OPERATOR \
5635 : binops_by_token[token->type].prec)
5638 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5640 cp_parser_expression_stack stack;
5641 cp_parser_expression_stack_entry *sp = &stack[0];
5644 enum tree_code tree_type;
5645 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5648 /* Parse the first expression. */
5649 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5653 /* Get an operator token. */
5654 token = cp_lexer_peek_token (parser->lexer);
5656 new_prec = TOKEN_PRECEDENCE (token);
5658 /* Popping an entry off the stack means we completed a subexpression:
5659 - either we found a token which is not an operator (`>' where it is not
5660 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5661 will happen repeatedly;
5662 - or, we found an operator which has lower priority. This is the case
5663 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5665 if (new_prec <= prec)
5674 tree_type = binops_by_token[token->type].tree_type;
5676 /* We used the operator token. */
5677 cp_lexer_consume_token (parser->lexer);
5679 /* Extract another operand. It may be the RHS of this expression
5680 or the LHS of a new, higher priority expression. */
5681 rhs = cp_parser_simple_cast_expression (parser);
5683 /* Get another operator token. Look up its precedence to avoid
5684 building a useless (immediately popped) stack entry for common
5685 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5686 token = cp_lexer_peek_token (parser->lexer);
5687 lookahead_prec = TOKEN_PRECEDENCE (token);
5688 if (lookahead_prec > new_prec)
5690 /* ... and prepare to parse the RHS of the new, higher priority
5691 expression. Since precedence levels on the stack are
5692 monotonically increasing, we do not have to care about
5695 sp->tree_type = tree_type;
5700 new_prec = lookahead_prec;
5704 /* If the stack is not empty, we have parsed into LHS the right side
5705 (`4' in the example above) of an expression we had suspended.
5706 We can use the information on the stack to recover the LHS (`3')
5707 from the stack together with the tree code (`MULT_EXPR'), and
5708 the precedence of the higher level subexpression
5709 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5710 which will be used to actually build the additive expression. */
5713 tree_type = sp->tree_type;
5718 overloaded_p = false;
5719 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5721 /* If the binary operator required the use of an overloaded operator,
5722 then this expression cannot be an integral constant-expression.
5723 An overloaded operator can be used even if both operands are
5724 otherwise permissible in an integral constant-expression if at
5725 least one of the operands is of enumeration type. */
5728 && (cp_parser_non_integral_constant_expression
5729 (parser, "calls to overloaded operators")))
5730 return error_mark_node;
5737 /* Parse the `? expression : assignment-expression' part of a
5738 conditional-expression. The LOGICAL_OR_EXPR is the
5739 logical-or-expression that started the conditional-expression.
5740 Returns a representation of the entire conditional-expression.
5742 This routine is used by cp_parser_assignment_expression.
5744 ? expression : assignment-expression
5748 ? : assignment-expression */
5751 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5754 tree assignment_expr;
5756 /* Consume the `?' token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 if (cp_parser_allow_gnu_extensions_p (parser)
5759 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5760 /* Implicit true clause. */
5763 /* Parse the expression. */
5764 expr = cp_parser_expression (parser, /*cast_p=*/false);
5766 /* The next token should be a `:'. */
5767 cp_parser_require (parser, CPP_COLON, "`:'");
5768 /* Parse the assignment-expression. */
5769 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5771 /* Build the conditional-expression. */
5772 return build_x_conditional_expr (logical_or_expr,
5777 /* Parse an assignment-expression.
5779 assignment-expression:
5780 conditional-expression
5781 logical-or-expression assignment-operator assignment_expression
5784 CAST_P is true if this expression is the target of a cast.
5786 Returns a representation for the expression. */
5789 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5793 /* If the next token is the `throw' keyword, then we're looking at
5794 a throw-expression. */
5795 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5796 expr = cp_parser_throw_expression (parser);
5797 /* Otherwise, it must be that we are looking at a
5798 logical-or-expression. */
5801 /* Parse the binary expressions (logical-or-expression). */
5802 expr = cp_parser_binary_expression (parser, cast_p);
5803 /* If the next token is a `?' then we're actually looking at a
5804 conditional-expression. */
5805 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5806 return cp_parser_question_colon_clause (parser, expr);
5809 enum tree_code assignment_operator;
5811 /* If it's an assignment-operator, we're using the second
5814 = cp_parser_assignment_operator_opt (parser);
5815 if (assignment_operator != ERROR_MARK)
5819 /* Parse the right-hand side of the assignment. */
5820 rhs = cp_parser_assignment_expression (parser, cast_p);
5821 /* An assignment may not appear in a
5822 constant-expression. */
5823 if (cp_parser_non_integral_constant_expression (parser,
5825 return error_mark_node;
5826 /* Build the assignment expression. */
5827 expr = build_x_modify_expr (expr,
5828 assignment_operator,
5837 /* Parse an (optional) assignment-operator.
5839 assignment-operator: one of
5840 = *= /= %= += -= >>= <<= &= ^= |=
5844 assignment-operator: one of
5847 If the next token is an assignment operator, the corresponding tree
5848 code is returned, and the token is consumed. For example, for
5849 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5850 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5851 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5852 operator, ERROR_MARK is returned. */
5854 static enum tree_code
5855 cp_parser_assignment_operator_opt (cp_parser* parser)
5860 /* Peek at the next toen. */
5861 token = cp_lexer_peek_token (parser->lexer);
5863 switch (token->type)
5874 op = TRUNC_DIV_EXPR;
5878 op = TRUNC_MOD_EXPR;
5910 /* Nothing else is an assignment operator. */
5914 /* If it was an assignment operator, consume it. */
5915 if (op != ERROR_MARK)
5916 cp_lexer_consume_token (parser->lexer);
5921 /* Parse an expression.
5924 assignment-expression
5925 expression , assignment-expression
5927 CAST_P is true if this expression is the target of a cast.
5929 Returns a representation of the expression. */
5932 cp_parser_expression (cp_parser* parser, bool cast_p)
5934 tree expression = NULL_TREE;
5938 tree assignment_expression;
5940 /* Parse the next assignment-expression. */
5941 assignment_expression
5942 = cp_parser_assignment_expression (parser, cast_p);
5943 /* If this is the first assignment-expression, we can just
5946 expression = assignment_expression;
5948 expression = build_x_compound_expr (expression,
5949 assignment_expression);
5950 /* If the next token is not a comma, then we are done with the
5952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5954 /* Consume the `,'. */
5955 cp_lexer_consume_token (parser->lexer);
5956 /* A comma operator cannot appear in a constant-expression. */
5957 if (cp_parser_non_integral_constant_expression (parser,
5958 "a comma operator"))
5959 expression = error_mark_node;
5965 /* Parse a constant-expression.
5967 constant-expression:
5968 conditional-expression
5970 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5971 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5972 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5973 is false, NON_CONSTANT_P should be NULL. */
5976 cp_parser_constant_expression (cp_parser* parser,
5977 bool allow_non_constant_p,
5978 bool *non_constant_p)
5980 bool saved_integral_constant_expression_p;
5981 bool saved_allow_non_integral_constant_expression_p;
5982 bool saved_non_integral_constant_expression_p;
5985 /* It might seem that we could simply parse the
5986 conditional-expression, and then check to see if it were
5987 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5988 one that the compiler can figure out is constant, possibly after
5989 doing some simplifications or optimizations. The standard has a
5990 precise definition of constant-expression, and we must honor
5991 that, even though it is somewhat more restrictive.
5997 is not a legal declaration, because `(2, 3)' is not a
5998 constant-expression. The `,' operator is forbidden in a
5999 constant-expression. However, GCC's constant-folding machinery
6000 will fold this operation to an INTEGER_CST for `3'. */
6002 /* Save the old settings. */
6003 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6004 saved_allow_non_integral_constant_expression_p
6005 = parser->allow_non_integral_constant_expression_p;
6006 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6007 /* We are now parsing a constant-expression. */
6008 parser->integral_constant_expression_p = true;
6009 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6010 parser->non_integral_constant_expression_p = false;
6011 /* Although the grammar says "conditional-expression", we parse an
6012 "assignment-expression", which also permits "throw-expression"
6013 and the use of assignment operators. In the case that
6014 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6015 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6016 actually essential that we look for an assignment-expression.
6017 For example, cp_parser_initializer_clauses uses this function to
6018 determine whether a particular assignment-expression is in fact
6020 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6021 /* Restore the old settings. */
6022 parser->integral_constant_expression_p
6023 = saved_integral_constant_expression_p;
6024 parser->allow_non_integral_constant_expression_p
6025 = saved_allow_non_integral_constant_expression_p;
6026 if (allow_non_constant_p)
6027 *non_constant_p = parser->non_integral_constant_expression_p;
6028 else if (parser->non_integral_constant_expression_p)
6029 expression = error_mark_node;
6030 parser->non_integral_constant_expression_p
6031 = saved_non_integral_constant_expression_p;
6036 /* Parse __builtin_offsetof.
6038 offsetof-expression:
6039 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6041 offsetof-member-designator:
6043 | offsetof-member-designator "." id-expression
6044 | offsetof-member-designator "[" expression "]" */
6047 cp_parser_builtin_offsetof (cp_parser *parser)
6049 int save_ice_p, save_non_ice_p;
6053 /* We're about to accept non-integral-constant things, but will
6054 definitely yield an integral constant expression. Save and
6055 restore these values around our local parsing. */
6056 save_ice_p = parser->integral_constant_expression_p;
6057 save_non_ice_p = parser->non_integral_constant_expression_p;
6059 /* Consume the "__builtin_offsetof" token. */
6060 cp_lexer_consume_token (parser->lexer);
6061 /* Consume the opening `('. */
6062 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6063 /* Parse the type-id. */
6064 type = cp_parser_type_id (parser);
6065 /* Look for the `,'. */
6066 cp_parser_require (parser, CPP_COMMA, "`,'");
6068 /* Build the (type *)null that begins the traditional offsetof macro. */
6069 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6071 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6072 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6076 cp_token *token = cp_lexer_peek_token (parser->lexer);
6077 switch (token->type)
6079 case CPP_OPEN_SQUARE:
6080 /* offsetof-member-designator "[" expression "]" */
6081 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6085 /* offsetof-member-designator "." identifier */
6086 cp_lexer_consume_token (parser->lexer);
6087 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6091 case CPP_CLOSE_PAREN:
6092 /* Consume the ")" token. */
6093 cp_lexer_consume_token (parser->lexer);
6097 /* Error. We know the following require will fail, but
6098 that gives the proper error message. */
6099 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6100 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6101 expr = error_mark_node;
6107 /* If we're processing a template, we can't finish the semantics yet.
6108 Otherwise we can fold the entire expression now. */
6109 if (processing_template_decl)
6110 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6112 expr = finish_offsetof (expr);
6115 parser->integral_constant_expression_p = save_ice_p;
6116 parser->non_integral_constant_expression_p = save_non_ice_p;
6121 /* Statements [gram.stmt.stmt] */
6123 /* Parse a statement.
6127 expression-statement
6132 declaration-statement
6135 IN_COMPOUND is true when the statement is nested inside a
6136 cp_parser_compound_statement; this matters for certain pragmas. */
6139 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6144 location_t statement_location;
6147 /* There is no statement yet. */
6148 statement = NULL_TREE;
6149 /* Peek at the next token. */
6150 token = cp_lexer_peek_token (parser->lexer);
6151 /* Remember the location of the first token in the statement. */
6152 statement_location = token->location;
6153 /* If this is a keyword, then that will often determine what kind of
6154 statement we have. */
6155 if (token->type == CPP_KEYWORD)
6157 enum rid keyword = token->keyword;
6163 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6169 statement = cp_parser_selection_statement (parser);
6175 statement = cp_parser_iteration_statement (parser);
6182 statement = cp_parser_jump_statement (parser);
6185 /* Objective-C++ exception-handling constructs. */
6188 case RID_AT_FINALLY:
6189 case RID_AT_SYNCHRONIZED:
6191 statement = cp_parser_objc_statement (parser);
6195 statement = cp_parser_try_block (parser);
6199 /* It might be a keyword like `int' that can start a
6200 declaration-statement. */
6204 else if (token->type == CPP_NAME)
6206 /* If the next token is a `:', then we are looking at a
6207 labeled-statement. */
6208 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6209 if (token->type == CPP_COLON)
6210 statement = cp_parser_labeled_statement (parser, in_statement_expr,
6213 /* Anything that starts with a `{' must be a compound-statement. */
6214 else if (token->type == CPP_OPEN_BRACE)
6215 statement = cp_parser_compound_statement (parser, NULL, false);
6216 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6217 a statement all its own. */
6218 else if (token->type == CPP_PRAGMA)
6220 /* Only certain OpenMP pragmas are attached to statements, and thus
6221 are considered statements themselves. All others are not. In
6222 the context of a compound, accept the pragma as a "statement" and
6223 return so that we can check for a close brace. Otherwise we
6224 require a real statement and must go back and read one. */
6226 cp_parser_pragma (parser, pragma_compound);
6227 else if (!cp_parser_pragma (parser, pragma_stmt))
6231 else if (token->type == CPP_EOF)
6233 cp_parser_error (parser, "expected statement");
6237 /* Everything else must be a declaration-statement or an
6238 expression-statement. Try for the declaration-statement
6239 first, unless we are looking at a `;', in which case we know that
6240 we have an expression-statement. */
6243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6245 cp_parser_parse_tentatively (parser);
6246 /* Try to parse the declaration-statement. */
6247 cp_parser_declaration_statement (parser);
6248 /* If that worked, we're done. */
6249 if (cp_parser_parse_definitely (parser))
6252 /* Look for an expression-statement instead. */
6253 statement = cp_parser_expression_statement (parser, in_statement_expr);
6256 /* Set the line number for the statement. */
6257 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6258 SET_EXPR_LOCATION (statement, statement_location);
6261 /* Parse a labeled-statement.
6264 identifier : statement
6265 case constant-expression : statement
6271 case constant-expression ... constant-expression : statement
6273 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6274 For an ordinary label, returns a LABEL_EXPR.
6276 IN_COMPOUND is as for cp_parser_statement: true when we're nested
6277 inside a compound. */
6280 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr,
6284 tree statement = error_mark_node;
6286 /* The next token should be an identifier. */
6287 token = cp_lexer_peek_token (parser->lexer);
6288 if (token->type != CPP_NAME
6289 && token->type != CPP_KEYWORD)
6291 cp_parser_error (parser, "expected labeled-statement");
6292 return error_mark_node;
6295 switch (token->keyword)
6302 /* Consume the `case' token. */
6303 cp_lexer_consume_token (parser->lexer);
6304 /* Parse the constant-expression. */
6305 expr = cp_parser_constant_expression (parser,
6306 /*allow_non_constant_p=*/false,
6309 ellipsis = cp_lexer_peek_token (parser->lexer);
6310 if (ellipsis->type == CPP_ELLIPSIS)
6312 /* Consume the `...' token. */
6313 cp_lexer_consume_token (parser->lexer);
6315 cp_parser_constant_expression (parser,
6316 /*allow_non_constant_p=*/false,
6318 /* We don't need to emit warnings here, as the common code
6319 will do this for us. */
6322 expr_hi = NULL_TREE;
6324 if (parser->in_switch_statement_p)
6325 statement = finish_case_label (expr, expr_hi);
6327 error ("case label %qE not within a switch statement", expr);
6332 /* Consume the `default' token. */
6333 cp_lexer_consume_token (parser->lexer);
6335 if (parser->in_switch_statement_p)
6336 statement = finish_case_label (NULL_TREE, NULL_TREE);
6338 error ("case label not within a switch statement");
6342 /* Anything else must be an ordinary label. */
6343 statement = finish_label_stmt (cp_parser_identifier (parser));
6347 /* Require the `:' token. */
6348 cp_parser_require (parser, CPP_COLON, "`:'");
6349 /* Parse the labeled statement. */
6350 cp_parser_statement (parser, in_statement_expr, in_compound);
6352 /* Return the label, in the case of a `case' or `default' label. */
6356 /* Parse an expression-statement.
6358 expression-statement:
6361 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6362 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6363 indicates whether this expression-statement is part of an
6364 expression statement. */
6367 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6369 tree statement = NULL_TREE;
6371 /* If the next token is a ';', then there is no expression
6373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6374 statement = cp_parser_expression (parser, /*cast_p=*/false);
6376 /* Consume the final `;'. */
6377 cp_parser_consume_semicolon_at_end_of_statement (parser);
6379 if (in_statement_expr
6380 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6381 /* This is the final expression statement of a statement
6383 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6385 statement = finish_expr_stmt (statement);
6392 /* Parse a compound-statement.
6395 { statement-seq [opt] }
6397 Returns a tree representing the statement. */
6400 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6405 /* Consume the `{'. */
6406 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6407 return error_mark_node;
6408 /* Begin the compound-statement. */
6409 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6410 /* Parse an (optional) statement-seq. */
6411 cp_parser_statement_seq_opt (parser, in_statement_expr);
6412 /* Finish the compound-statement. */
6413 finish_compound_stmt (compound_stmt);
6414 /* Consume the `}'. */
6415 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6417 return compound_stmt;
6420 /* Parse an (optional) statement-seq.
6424 statement-seq [opt] statement */
6427 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6429 /* Scan statements until there aren't any more. */
6432 cp_token *token = cp_lexer_peek_token (parser->lexer);
6434 /* If we're looking at a `}', then we've run out of statements. */
6435 if (token->type == CPP_CLOSE_BRACE
6436 || token->type == CPP_EOF
6437 || token->type == CPP_PRAGMA_EOL)
6440 /* Parse the statement. */
6441 cp_parser_statement (parser, in_statement_expr, true);
6445 /* Parse a selection-statement.
6447 selection-statement:
6448 if ( condition ) statement
6449 if ( condition ) statement else statement
6450 switch ( condition ) statement
6452 Returns the new IF_STMT or SWITCH_STMT. */
6455 cp_parser_selection_statement (cp_parser* parser)
6460 /* Peek at the next token. */
6461 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6463 /* See what kind of keyword it is. */
6464 keyword = token->keyword;
6473 /* Look for the `('. */
6474 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6476 cp_parser_skip_to_end_of_statement (parser);
6477 return error_mark_node;
6480 /* Begin the selection-statement. */
6481 if (keyword == RID_IF)
6482 statement = begin_if_stmt ();
6484 statement = begin_switch_stmt ();
6486 /* Parse the condition. */
6487 condition = cp_parser_condition (parser);
6488 /* Look for the `)'. */
6489 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6490 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6491 /*consume_paren=*/true);
6493 if (keyword == RID_IF)
6495 /* Add the condition. */
6496 finish_if_stmt_cond (condition, statement);
6498 /* Parse the then-clause. */
6499 cp_parser_implicitly_scoped_statement (parser);
6500 finish_then_clause (statement);
6502 /* If the next token is `else', parse the else-clause. */
6503 if (cp_lexer_next_token_is_keyword (parser->lexer,
6506 /* Consume the `else' keyword. */
6507 cp_lexer_consume_token (parser->lexer);
6508 begin_else_clause (statement);
6509 /* Parse the else-clause. */
6510 cp_parser_implicitly_scoped_statement (parser);
6511 finish_else_clause (statement);
6514 /* Now we're all done with the if-statement. */
6515 finish_if_stmt (statement);
6519 bool in_switch_statement_p;
6520 unsigned char in_statement;
6522 /* Add the condition. */
6523 finish_switch_cond (condition, statement);
6525 /* Parse the body of the switch-statement. */
6526 in_switch_statement_p = parser->in_switch_statement_p;
6527 in_statement = parser->in_statement;
6528 parser->in_switch_statement_p = true;
6529 parser->in_statement |= IN_SWITCH_STMT;
6530 cp_parser_implicitly_scoped_statement (parser);
6531 parser->in_switch_statement_p = in_switch_statement_p;
6532 parser->in_statement = in_statement;
6534 /* Now we're all done with the switch-statement. */
6535 finish_switch_stmt (statement);
6543 cp_parser_error (parser, "expected selection-statement");
6544 return error_mark_node;
6548 /* Parse a condition.
6552 type-specifier-seq declarator = assignment-expression
6557 type-specifier-seq declarator asm-specification [opt]
6558 attributes [opt] = assignment-expression
6560 Returns the expression that should be tested. */
6563 cp_parser_condition (cp_parser* parser)
6565 cp_decl_specifier_seq type_specifiers;
6566 const char *saved_message;
6568 /* Try the declaration first. */
6569 cp_parser_parse_tentatively (parser);
6570 /* New types are not allowed in the type-specifier-seq for a
6572 saved_message = parser->type_definition_forbidden_message;
6573 parser->type_definition_forbidden_message
6574 = "types may not be defined in conditions";
6575 /* Parse the type-specifier-seq. */
6576 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6578 /* Restore the saved message. */
6579 parser->type_definition_forbidden_message = saved_message;
6580 /* If all is well, we might be looking at a declaration. */
6581 if (!cp_parser_error_occurred (parser))
6584 tree asm_specification;
6586 cp_declarator *declarator;
6587 tree initializer = NULL_TREE;
6589 /* Parse the declarator. */
6590 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6591 /*ctor_dtor_or_conv_p=*/NULL,
6592 /*parenthesized_p=*/NULL,
6593 /*member_p=*/false);
6594 /* Parse the attributes. */
6595 attributes = cp_parser_attributes_opt (parser);
6596 /* Parse the asm-specification. */
6597 asm_specification = cp_parser_asm_specification_opt (parser);
6598 /* If the next token is not an `=', then we might still be
6599 looking at an expression. For example:
6603 looks like a decl-specifier-seq and a declarator -- but then
6604 there is no `=', so this is an expression. */
6605 cp_parser_require (parser, CPP_EQ, "`='");
6606 /* If we did see an `=', then we are looking at a declaration
6608 if (cp_parser_parse_definitely (parser))
6611 bool non_constant_p;
6613 /* Create the declaration. */
6614 decl = start_decl (declarator, &type_specifiers,
6615 /*initialized_p=*/true,
6616 attributes, /*prefix_attributes=*/NULL_TREE,
6618 /* Parse the assignment-expression. */
6620 = cp_parser_constant_expression (parser,
6621 /*allow_non_constant_p=*/true,
6623 if (!non_constant_p)
6624 initializer = fold_non_dependent_expr (initializer);
6626 /* Process the initializer. */
6627 cp_finish_decl (decl,
6628 initializer, !non_constant_p,
6630 LOOKUP_ONLYCONVERTING);
6633 pop_scope (pushed_scope);
6635 return convert_from_reference (decl);
6638 /* If we didn't even get past the declarator successfully, we are
6639 definitely not looking at a declaration. */
6641 cp_parser_abort_tentative_parse (parser);
6643 /* Otherwise, we are looking at an expression. */
6644 return cp_parser_expression (parser, /*cast_p=*/false);
6647 /* Parse an iteration-statement.
6649 iteration-statement:
6650 while ( condition ) statement
6651 do statement while ( expression ) ;
6652 for ( for-init-statement condition [opt] ; expression [opt] )
6655 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6658 cp_parser_iteration_statement (cp_parser* parser)
6663 unsigned char in_statement;
6665 /* Peek at the next token. */
6666 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6668 return error_mark_node;
6670 /* Remember whether or not we are already within an iteration
6672 in_statement = parser->in_statement;
6674 /* See what kind of keyword it is. */
6675 keyword = token->keyword;
6682 /* Begin the while-statement. */
6683 statement = begin_while_stmt ();
6684 /* Look for the `('. */
6685 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6686 /* Parse the condition. */
6687 condition = cp_parser_condition (parser);
6688 finish_while_stmt_cond (condition, statement);
6689 /* Look for the `)'. */
6690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6691 /* Parse the dependent statement. */
6692 parser->in_statement = IN_ITERATION_STMT;
6693 cp_parser_already_scoped_statement (parser);
6694 parser->in_statement = in_statement;
6695 /* We're done with the while-statement. */
6696 finish_while_stmt (statement);
6704 /* Begin the do-statement. */
6705 statement = begin_do_stmt ();
6706 /* Parse the body of the do-statement. */
6707 parser->in_statement = IN_ITERATION_STMT;
6708 cp_parser_implicitly_scoped_statement (parser);
6709 parser->in_statement = in_statement;
6710 finish_do_body (statement);
6711 /* Look for the `while' keyword. */
6712 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6713 /* Look for the `('. */
6714 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6715 /* Parse the expression. */
6716 expression = cp_parser_expression (parser, /*cast_p=*/false);
6717 /* We're done with the do-statement. */
6718 finish_do_stmt (expression, statement);
6719 /* Look for the `)'. */
6720 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6721 /* Look for the `;'. */
6722 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6728 tree condition = NULL_TREE;
6729 tree expression = NULL_TREE;
6731 /* Begin the for-statement. */
6732 statement = begin_for_stmt ();
6733 /* Look for the `('. */
6734 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6735 /* Parse the initialization. */
6736 cp_parser_for_init_statement (parser);
6737 finish_for_init_stmt (statement);
6739 /* If there's a condition, process it. */
6740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6741 condition = cp_parser_condition (parser);
6742 finish_for_cond (condition, statement);
6743 /* Look for the `;'. */
6744 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6746 /* If there's an expression, process it. */
6747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6748 expression = cp_parser_expression (parser, /*cast_p=*/false);
6749 finish_for_expr (expression, statement);
6750 /* Look for the `)'. */
6751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6753 /* Parse the body of the for-statement. */
6754 parser->in_statement = IN_ITERATION_STMT;
6755 cp_parser_already_scoped_statement (parser);
6756 parser->in_statement = in_statement;
6758 /* We're done with the for-statement. */
6759 finish_for_stmt (statement);
6764 cp_parser_error (parser, "expected iteration-statement");
6765 statement = error_mark_node;
6772 /* Parse a for-init-statement.
6775 expression-statement
6776 simple-declaration */
6779 cp_parser_for_init_statement (cp_parser* parser)
6781 /* If the next token is a `;', then we have an empty
6782 expression-statement. Grammatically, this is also a
6783 simple-declaration, but an invalid one, because it does not
6784 declare anything. Therefore, if we did not handle this case
6785 specially, we would issue an error message about an invalid
6787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6789 /* We're going to speculatively look for a declaration, falling back
6790 to an expression, if necessary. */
6791 cp_parser_parse_tentatively (parser);
6792 /* Parse the declaration. */
6793 cp_parser_simple_declaration (parser,
6794 /*function_definition_allowed_p=*/false);
6795 /* If the tentative parse failed, then we shall need to look for an
6796 expression-statement. */
6797 if (cp_parser_parse_definitely (parser))
6801 cp_parser_expression_statement (parser, false);
6804 /* Parse a jump-statement.
6809 return expression [opt] ;
6817 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6820 cp_parser_jump_statement (cp_parser* parser)
6822 tree statement = error_mark_node;
6826 /* Peek at the next token. */
6827 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6829 return error_mark_node;
6831 /* See what kind of keyword it is. */
6832 keyword = token->keyword;
6836 switch (parser->in_statement)
6839 error ("break statement not within loop or switch");
6842 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6843 || parser->in_statement == IN_ITERATION_STMT);
6844 statement = finish_break_stmt ();
6847 error ("invalid exit from OpenMP structured block");
6850 error ("break statement used with OpenMP for loop");
6853 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6857 switch (parser->in_statement & ~IN_SWITCH_STMT)
6860 error ("continue statement not within a loop");
6862 case IN_ITERATION_STMT:
6864 statement = finish_continue_stmt ();
6867 error ("invalid exit from OpenMP structured block");
6872 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6879 /* If the next token is a `;', then there is no
6881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6882 expr = cp_parser_expression (parser, /*cast_p=*/false);
6885 /* Build the return-statement. */
6886 statement = finish_return_stmt (expr);
6887 /* Look for the final `;'. */
6888 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6893 /* Create the goto-statement. */
6894 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6896 /* Issue a warning about this use of a GNU extension. */
6898 pedwarn ("ISO C++ forbids computed gotos");
6899 /* Consume the '*' token. */
6900 cp_lexer_consume_token (parser->lexer);
6901 /* Parse the dependent expression. */
6902 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6905 finish_goto_stmt (cp_parser_identifier (parser));
6906 /* Look for the final `;'. */
6907 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6911 cp_parser_error (parser, "expected jump-statement");
6918 /* Parse a declaration-statement.
6920 declaration-statement:
6921 block-declaration */
6924 cp_parser_declaration_statement (cp_parser* parser)
6928 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6929 p = obstack_alloc (&declarator_obstack, 0);
6931 /* Parse the block-declaration. */
6932 cp_parser_block_declaration (parser, /*statement_p=*/true);
6934 /* Free any declarators allocated. */
6935 obstack_free (&declarator_obstack, p);
6937 /* Finish off the statement. */
6941 /* Some dependent statements (like `if (cond) statement'), are
6942 implicitly in their own scope. In other words, if the statement is
6943 a single statement (as opposed to a compound-statement), it is
6944 none-the-less treated as if it were enclosed in braces. Any
6945 declarations appearing in the dependent statement are out of scope
6946 after control passes that point. This function parses a statement,
6947 but ensures that is in its own scope, even if it is not a
6950 Returns the new statement. */
6953 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6957 /* Mark if () ; with a special NOP_EXPR. */
6958 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6960 cp_lexer_consume_token (parser->lexer);
6961 statement = add_stmt (build_empty_stmt ());
6963 /* if a compound is opened, we simply parse the statement directly. */
6964 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6965 statement = cp_parser_compound_statement (parser, NULL, false);
6966 /* If the token is not a `{', then we must take special action. */
6969 /* Create a compound-statement. */
6970 statement = begin_compound_stmt (0);
6971 /* Parse the dependent-statement. */
6972 cp_parser_statement (parser, NULL_TREE, false);
6973 /* Finish the dummy compound-statement. */
6974 finish_compound_stmt (statement);
6977 /* Return the statement. */
6981 /* For some dependent statements (like `while (cond) statement'), we
6982 have already created a scope. Therefore, even if the dependent
6983 statement is a compound-statement, we do not want to create another
6987 cp_parser_already_scoped_statement (cp_parser* parser)
6989 /* If the token is a `{', then we must take special action. */
6990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6991 cp_parser_statement (parser, NULL_TREE, false);
6994 /* Avoid calling cp_parser_compound_statement, so that we
6995 don't create a new scope. Do everything else by hand. */
6996 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6997 cp_parser_statement_seq_opt (parser, NULL_TREE);
6998 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7002 /* Declarations [gram.dcl.dcl] */
7004 /* Parse an optional declaration-sequence.
7008 declaration-seq declaration */
7011 cp_parser_declaration_seq_opt (cp_parser* parser)
7017 token = cp_lexer_peek_token (parser->lexer);
7019 if (token->type == CPP_CLOSE_BRACE
7020 || token->type == CPP_EOF
7021 || token->type == CPP_PRAGMA_EOL)
7024 if (token->type == CPP_SEMICOLON)
7026 /* A declaration consisting of a single semicolon is
7027 invalid. Allow it unless we're being pedantic. */
7028 cp_lexer_consume_token (parser->lexer);
7029 if (pedantic && !in_system_header)
7030 pedwarn ("extra %<;%>");
7034 /* If we're entering or exiting a region that's implicitly
7035 extern "C", modify the lang context appropriately. */
7036 if (!parser->implicit_extern_c && token->implicit_extern_c)
7038 push_lang_context (lang_name_c);
7039 parser->implicit_extern_c = true;
7041 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7043 pop_lang_context ();
7044 parser->implicit_extern_c = false;
7047 if (token->type == CPP_PRAGMA)
7049 /* A top-level declaration can consist solely of a #pragma.
7050 A nested declaration cannot, so this is done here and not
7051 in cp_parser_declaration. (A #pragma at block scope is
7052 handled in cp_parser_statement.) */
7053 cp_parser_pragma (parser, pragma_external);
7057 /* Parse the declaration itself. */
7058 cp_parser_declaration (parser);
7062 /* Parse a declaration.
7067 template-declaration
7068 explicit-instantiation
7069 explicit-specialization
7070 linkage-specification
7071 namespace-definition
7076 __extension__ declaration */
7079 cp_parser_declaration (cp_parser* parser)
7086 /* Check for the `__extension__' keyword. */
7087 if (cp_parser_extension_opt (parser, &saved_pedantic))
7089 /* Parse the qualified declaration. */
7090 cp_parser_declaration (parser);
7091 /* Restore the PEDANTIC flag. */
7092 pedantic = saved_pedantic;
7097 /* Try to figure out what kind of declaration is present. */
7098 token1 = *cp_lexer_peek_token (parser->lexer);
7100 if (token1.type != CPP_EOF)
7101 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7104 token2.type = CPP_EOF;
7105 token2.keyword = RID_MAX;
7108 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7109 p = obstack_alloc (&declarator_obstack, 0);
7111 /* If the next token is `extern' and the following token is a string
7112 literal, then we have a linkage specification. */
7113 if (token1.keyword == RID_EXTERN
7114 && cp_parser_is_string_literal (&token2))
7115 cp_parser_linkage_specification (parser);
7116 /* If the next token is `template', then we have either a template
7117 declaration, an explicit instantiation, or an explicit
7119 else if (token1.keyword == RID_TEMPLATE)
7121 /* `template <>' indicates a template specialization. */
7122 if (token2.type == CPP_LESS
7123 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7124 cp_parser_explicit_specialization (parser);
7125 /* `template <' indicates a template declaration. */
7126 else if (token2.type == CPP_LESS)
7127 cp_parser_template_declaration (parser, /*member_p=*/false);
7128 /* Anything else must be an explicit instantiation. */
7130 cp_parser_explicit_instantiation (parser);
7132 /* If the next token is `export', then we have a template
7134 else if (token1.keyword == RID_EXPORT)
7135 cp_parser_template_declaration (parser, /*member_p=*/false);
7136 /* If the next token is `extern', 'static' or 'inline' and the one
7137 after that is `template', we have a GNU extended explicit
7138 instantiation directive. */
7139 else if (cp_parser_allow_gnu_extensions_p (parser)
7140 && (token1.keyword == RID_EXTERN
7141 || token1.keyword == RID_STATIC
7142 || token1.keyword == RID_INLINE)
7143 && token2.keyword == RID_TEMPLATE)
7144 cp_parser_explicit_instantiation (parser);
7145 /* If the next token is `namespace', check for a named or unnamed
7146 namespace definition. */
7147 else if (token1.keyword == RID_NAMESPACE
7148 && (/* A named namespace definition. */
7149 (token2.type == CPP_NAME
7150 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7152 /* An unnamed namespace definition. */
7153 || token2.type == CPP_OPEN_BRACE
7154 || token2.keyword == RID_ATTRIBUTE))
7155 cp_parser_namespace_definition (parser);
7156 /* Objective-C++ declaration/definition. */
7157 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7158 cp_parser_objc_declaration (parser);
7159 /* We must have either a block declaration or a function
7162 /* Try to parse a block-declaration, or a function-definition. */
7163 cp_parser_block_declaration (parser, /*statement_p=*/false);
7165 /* Free any declarators allocated. */
7166 obstack_free (&declarator_obstack, p);
7169 /* Parse a block-declaration.
7174 namespace-alias-definition
7181 __extension__ block-declaration
7184 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7185 part of a declaration-statement. */
7188 cp_parser_block_declaration (cp_parser *parser,
7194 /* Check for the `__extension__' keyword. */
7195 if (cp_parser_extension_opt (parser, &saved_pedantic))
7197 /* Parse the qualified declaration. */
7198 cp_parser_block_declaration (parser, statement_p);
7199 /* Restore the PEDANTIC flag. */
7200 pedantic = saved_pedantic;
7205 /* Peek at the next token to figure out which kind of declaration is
7207 token1 = cp_lexer_peek_token (parser->lexer);
7209 /* If the next keyword is `asm', we have an asm-definition. */
7210 if (token1->keyword == RID_ASM)
7213 cp_parser_commit_to_tentative_parse (parser);
7214 cp_parser_asm_definition (parser);
7216 /* If the next keyword is `namespace', we have a
7217 namespace-alias-definition. */
7218 else if (token1->keyword == RID_NAMESPACE)
7219 cp_parser_namespace_alias_definition (parser);
7220 /* If the next keyword is `using', we have either a
7221 using-declaration or a using-directive. */
7222 else if (token1->keyword == RID_USING)
7227 cp_parser_commit_to_tentative_parse (parser);
7228 /* If the token after `using' is `namespace', then we have a
7230 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7231 if (token2->keyword == RID_NAMESPACE)
7232 cp_parser_using_directive (parser);
7233 /* Otherwise, it's a using-declaration. */
7235 cp_parser_using_declaration (parser);
7237 /* If the next keyword is `__label__' we have a label declaration. */
7238 else if (token1->keyword == RID_LABEL)
7241 cp_parser_commit_to_tentative_parse (parser);
7242 cp_parser_label_declaration (parser);
7244 /* Anything else must be a simple-declaration. */
7246 cp_parser_simple_declaration (parser, !statement_p);
7249 /* Parse a simple-declaration.
7252 decl-specifier-seq [opt] init-declarator-list [opt] ;
7254 init-declarator-list:
7256 init-declarator-list , init-declarator
7258 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7259 function-definition as a simple-declaration. */
7262 cp_parser_simple_declaration (cp_parser* parser,
7263 bool function_definition_allowed_p)
7265 cp_decl_specifier_seq decl_specifiers;
7266 int declares_class_or_enum;
7267 bool saw_declarator;
7269 /* Defer access checks until we know what is being declared; the
7270 checks for names appearing in the decl-specifier-seq should be
7271 done as if we were in the scope of the thing being declared. */
7272 push_deferring_access_checks (dk_deferred);
7274 /* Parse the decl-specifier-seq. We have to keep track of whether
7275 or not the decl-specifier-seq declares a named class or
7276 enumeration type, since that is the only case in which the
7277 init-declarator-list is allowed to be empty.
7281 In a simple-declaration, the optional init-declarator-list can be
7282 omitted only when declaring a class or enumeration, that is when
7283 the decl-specifier-seq contains either a class-specifier, an
7284 elaborated-type-specifier, or an enum-specifier. */
7285 cp_parser_decl_specifier_seq (parser,
7286 CP_PARSER_FLAGS_OPTIONAL,
7288 &declares_class_or_enum);
7289 /* We no longer need to defer access checks. */
7290 stop_deferring_access_checks ();
7292 /* In a block scope, a valid declaration must always have a
7293 decl-specifier-seq. By not trying to parse declarators, we can
7294 resolve the declaration/expression ambiguity more quickly. */
7295 if (!function_definition_allowed_p
7296 && !decl_specifiers.any_specifiers_p)
7298 cp_parser_error (parser, "expected declaration");
7302 /* If the next two tokens are both identifiers, the code is
7303 erroneous. The usual cause of this situation is code like:
7307 where "T" should name a type -- but does not. */
7308 if (!decl_specifiers.type
7309 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7311 /* If parsing tentatively, we should commit; we really are
7312 looking at a declaration. */
7313 cp_parser_commit_to_tentative_parse (parser);
7318 /* If we have seen at least one decl-specifier, and the next token
7319 is not a parenthesis, then we must be looking at a declaration.
7320 (After "int (" we might be looking at a functional cast.) */
7321 if (decl_specifiers.any_specifiers_p
7322 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7323 cp_parser_commit_to_tentative_parse (parser);
7325 /* Keep going until we hit the `;' at the end of the simple
7327 saw_declarator = false;
7328 while (cp_lexer_next_token_is_not (parser->lexer,
7332 bool function_definition_p;
7337 /* If we are processing next declarator, coma is expected */
7338 token = cp_lexer_peek_token (parser->lexer);
7339 gcc_assert (token->type == CPP_COMMA);
7340 cp_lexer_consume_token (parser->lexer);
7343 saw_declarator = true;
7345 /* Parse the init-declarator. */
7346 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7347 /*checks=*/NULL_TREE,
7348 function_definition_allowed_p,
7350 declares_class_or_enum,
7351 &function_definition_p);
7352 /* If an error occurred while parsing tentatively, exit quickly.
7353 (That usually happens when in the body of a function; each
7354 statement is treated as a declaration-statement until proven
7356 if (cp_parser_error_occurred (parser))
7358 /* Handle function definitions specially. */
7359 if (function_definition_p)
7361 /* If the next token is a `,', then we are probably
7362 processing something like:
7366 which is erroneous. */
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7368 error ("mixing declarations and function-definitions is forbidden");
7369 /* Otherwise, we're done with the list of declarators. */
7372 pop_deferring_access_checks ();
7376 /* The next token should be either a `,' or a `;'. */
7377 token = cp_lexer_peek_token (parser->lexer);
7378 /* If it's a `,', there are more declarators to come. */
7379 if (token->type == CPP_COMMA)
7380 /* will be consumed next time around */;
7381 /* If it's a `;', we are done. */
7382 else if (token->type == CPP_SEMICOLON)
7384 /* Anything else is an error. */
7387 /* If we have already issued an error message we don't need
7388 to issue another one. */
7389 if (decl != error_mark_node
7390 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7391 cp_parser_error (parser, "expected %<,%> or %<;%>");
7392 /* Skip tokens until we reach the end of the statement. */
7393 cp_parser_skip_to_end_of_statement (parser);
7394 /* If the next token is now a `;', consume it. */
7395 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7396 cp_lexer_consume_token (parser->lexer);
7399 /* After the first time around, a function-definition is not
7400 allowed -- even if it was OK at first. For example:
7405 function_definition_allowed_p = false;
7408 /* Issue an error message if no declarators are present, and the
7409 decl-specifier-seq does not itself declare a class or
7411 if (!saw_declarator)
7413 if (cp_parser_declares_only_class_p (parser))
7414 shadow_tag (&decl_specifiers);
7415 /* Perform any deferred access checks. */
7416 perform_deferred_access_checks ();
7419 /* Consume the `;'. */
7420 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7423 pop_deferring_access_checks ();
7426 /* Parse a decl-specifier-seq.
7429 decl-specifier-seq [opt] decl-specifier
7432 storage-class-specifier
7443 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7445 The parser flags FLAGS is used to control type-specifier parsing.
7447 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7450 1: one of the decl-specifiers is an elaborated-type-specifier
7451 (i.e., a type declaration)
7452 2: one of the decl-specifiers is an enum-specifier or a
7453 class-specifier (i.e., a type definition)
7458 cp_parser_decl_specifier_seq (cp_parser* parser,
7459 cp_parser_flags flags,
7460 cp_decl_specifier_seq *decl_specs,
7461 int* declares_class_or_enum)
7463 bool constructor_possible_p = !parser->in_declarator_p;
7465 /* Clear DECL_SPECS. */
7466 clear_decl_specs (decl_specs);
7468 /* Assume no class or enumeration type is declared. */
7469 *declares_class_or_enum = 0;
7471 /* Keep reading specifiers until there are no more to read. */
7475 bool found_decl_spec;
7478 /* Peek at the next token. */
7479 token = cp_lexer_peek_token (parser->lexer);
7480 /* Handle attributes. */
7481 if (token->keyword == RID_ATTRIBUTE)
7483 /* Parse the attributes. */
7484 decl_specs->attributes
7485 = chainon (decl_specs->attributes,
7486 cp_parser_attributes_opt (parser));
7489 /* Assume we will find a decl-specifier keyword. */
7490 found_decl_spec = true;
7491 /* If the next token is an appropriate keyword, we can simply
7492 add it to the list. */
7493 switch (token->keyword)
7498 if (!at_class_scope_p ())
7500 error ("%<friend%> used outside of class");
7501 cp_lexer_purge_token (parser->lexer);
7505 ++decl_specs->specs[(int) ds_friend];
7506 /* Consume the token. */
7507 cp_lexer_consume_token (parser->lexer);
7511 /* function-specifier:
7518 cp_parser_function_specifier_opt (parser, decl_specs);
7524 ++decl_specs->specs[(int) ds_typedef];
7525 /* Consume the token. */
7526 cp_lexer_consume_token (parser->lexer);
7527 /* A constructor declarator cannot appear in a typedef. */
7528 constructor_possible_p = false;
7529 /* The "typedef" keyword can only occur in a declaration; we
7530 may as well commit at this point. */
7531 cp_parser_commit_to_tentative_parse (parser);
7534 /* storage-class-specifier:
7548 /* Consume the token. */
7549 cp_lexer_consume_token (parser->lexer);
7550 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7553 /* Consume the token. */
7554 cp_lexer_consume_token (parser->lexer);
7555 ++decl_specs->specs[(int) ds_thread];
7559 /* We did not yet find a decl-specifier yet. */
7560 found_decl_spec = false;
7564 /* Constructors are a special case. The `S' in `S()' is not a
7565 decl-specifier; it is the beginning of the declarator. */
7568 && constructor_possible_p
7569 && (cp_parser_constructor_declarator_p
7570 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7572 /* If we don't have a DECL_SPEC yet, then we must be looking at
7573 a type-specifier. */
7574 if (!found_decl_spec && !constructor_p)
7576 int decl_spec_declares_class_or_enum;
7577 bool is_cv_qualifier;
7581 = cp_parser_type_specifier (parser, flags,
7583 /*is_declaration=*/true,
7584 &decl_spec_declares_class_or_enum,
7587 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7589 /* If this type-specifier referenced a user-defined type
7590 (a typedef, class-name, etc.), then we can't allow any
7591 more such type-specifiers henceforth.
7595 The longest sequence of decl-specifiers that could
7596 possibly be a type name is taken as the
7597 decl-specifier-seq of a declaration. The sequence shall
7598 be self-consistent as described below.
7602 As a general rule, at most one type-specifier is allowed
7603 in the complete decl-specifier-seq of a declaration. The
7604 only exceptions are the following:
7606 -- const or volatile can be combined with any other
7609 -- signed or unsigned can be combined with char, long,
7617 void g (const int Pc);
7619 Here, Pc is *not* part of the decl-specifier seq; it's
7620 the declarator. Therefore, once we see a type-specifier
7621 (other than a cv-qualifier), we forbid any additional
7622 user-defined types. We *do* still allow things like `int
7623 int' to be considered a decl-specifier-seq, and issue the
7624 error message later. */
7625 if (type_spec && !is_cv_qualifier)
7626 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7627 /* A constructor declarator cannot follow a type-specifier. */
7630 constructor_possible_p = false;
7631 found_decl_spec = true;
7635 /* If we still do not have a DECL_SPEC, then there are no more
7637 if (!found_decl_spec)
7640 decl_specs->any_specifiers_p = true;
7641 /* After we see one decl-specifier, further decl-specifiers are
7643 flags |= CP_PARSER_FLAGS_OPTIONAL;
7646 cp_parser_check_decl_spec (decl_specs);
7648 /* Don't allow a friend specifier with a class definition. */
7649 if (decl_specs->specs[(int) ds_friend] != 0
7650 && (*declares_class_or_enum & 2))
7651 error ("class definition may not be declared a friend");
7654 /* Parse an (optional) storage-class-specifier.
7656 storage-class-specifier:
7665 storage-class-specifier:
7668 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7671 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7673 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7681 /* Consume the token. */
7682 return cp_lexer_consume_token (parser->lexer)->value;
7689 /* Parse an (optional) function-specifier.
7696 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7697 Updates DECL_SPECS, if it is non-NULL. */
7700 cp_parser_function_specifier_opt (cp_parser* parser,
7701 cp_decl_specifier_seq *decl_specs)
7703 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7707 ++decl_specs->specs[(int) ds_inline];
7711 /* 14.5.2.3 [temp.mem]
7713 A member function template shall not be virtual. */
7714 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7715 error ("templates may not be %<virtual%>");
7716 else if (decl_specs)
7717 ++decl_specs->specs[(int) ds_virtual];
7722 ++decl_specs->specs[(int) ds_explicit];
7729 /* Consume the token. */
7730 return cp_lexer_consume_token (parser->lexer)->value;
7733 /* Parse a linkage-specification.
7735 linkage-specification:
7736 extern string-literal { declaration-seq [opt] }
7737 extern string-literal declaration */
7740 cp_parser_linkage_specification (cp_parser* parser)
7744 /* Look for the `extern' keyword. */
7745 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7747 /* Look for the string-literal. */
7748 linkage = cp_parser_string_literal (parser, false, false);
7750 /* Transform the literal into an identifier. If the literal is a
7751 wide-character string, or contains embedded NULs, then we can't
7752 handle it as the user wants. */
7753 if (strlen (TREE_STRING_POINTER (linkage))
7754 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7756 cp_parser_error (parser, "invalid linkage-specification");
7757 /* Assume C++ linkage. */
7758 linkage = lang_name_cplusplus;
7761 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7763 /* We're now using the new linkage. */
7764 push_lang_context (linkage);
7766 /* If the next token is a `{', then we're using the first
7768 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7770 /* Consume the `{' token. */
7771 cp_lexer_consume_token (parser->lexer);
7772 /* Parse the declarations. */
7773 cp_parser_declaration_seq_opt (parser);
7774 /* Look for the closing `}'. */
7775 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7777 /* Otherwise, there's just one declaration. */
7780 bool saved_in_unbraced_linkage_specification_p;
7782 saved_in_unbraced_linkage_specification_p
7783 = parser->in_unbraced_linkage_specification_p;
7784 parser->in_unbraced_linkage_specification_p = true;
7785 cp_parser_declaration (parser);
7786 parser->in_unbraced_linkage_specification_p
7787 = saved_in_unbraced_linkage_specification_p;
7790 /* We're done with the linkage-specification. */
7791 pop_lang_context ();
7794 /* Special member functions [gram.special] */
7796 /* Parse a conversion-function-id.
7798 conversion-function-id:
7799 operator conversion-type-id
7801 Returns an IDENTIFIER_NODE representing the operator. */
7804 cp_parser_conversion_function_id (cp_parser* parser)
7808 tree saved_qualifying_scope;
7809 tree saved_object_scope;
7810 tree pushed_scope = NULL_TREE;
7812 /* Look for the `operator' token. */
7813 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7814 return error_mark_node;
7815 /* When we parse the conversion-type-id, the current scope will be
7816 reset. However, we need that information in able to look up the
7817 conversion function later, so we save it here. */
7818 saved_scope = parser->scope;
7819 saved_qualifying_scope = parser->qualifying_scope;
7820 saved_object_scope = parser->object_scope;
7821 /* We must enter the scope of the class so that the names of
7822 entities declared within the class are available in the
7823 conversion-type-id. For example, consider:
7830 S::operator I() { ... }
7832 In order to see that `I' is a type-name in the definition, we
7833 must be in the scope of `S'. */
7835 pushed_scope = push_scope (saved_scope);
7836 /* Parse the conversion-type-id. */
7837 type = cp_parser_conversion_type_id (parser);
7838 /* Leave the scope of the class, if any. */
7840 pop_scope (pushed_scope);
7841 /* Restore the saved scope. */
7842 parser->scope = saved_scope;
7843 parser->qualifying_scope = saved_qualifying_scope;
7844 parser->object_scope = saved_object_scope;
7845 /* If the TYPE is invalid, indicate failure. */
7846 if (type == error_mark_node)
7847 return error_mark_node;
7848 return mangle_conv_op_name_for_type (type);
7851 /* Parse a conversion-type-id:
7854 type-specifier-seq conversion-declarator [opt]
7856 Returns the TYPE specified. */
7859 cp_parser_conversion_type_id (cp_parser* parser)
7862 cp_decl_specifier_seq type_specifiers;
7863 cp_declarator *declarator;
7864 tree type_specified;
7866 /* Parse the attributes. */
7867 attributes = cp_parser_attributes_opt (parser);
7868 /* Parse the type-specifiers. */
7869 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7871 /* If that didn't work, stop. */
7872 if (type_specifiers.type == error_mark_node)
7873 return error_mark_node;
7874 /* Parse the conversion-declarator. */
7875 declarator = cp_parser_conversion_declarator_opt (parser);
7877 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7878 /*initialized=*/0, &attributes);
7880 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7881 return type_specified;
7884 /* Parse an (optional) conversion-declarator.
7886 conversion-declarator:
7887 ptr-operator conversion-declarator [opt]
7891 static cp_declarator *
7892 cp_parser_conversion_declarator_opt (cp_parser* parser)
7894 enum tree_code code;
7896 cp_cv_quals cv_quals;
7898 /* We don't know if there's a ptr-operator next, or not. */
7899 cp_parser_parse_tentatively (parser);
7900 /* Try the ptr-operator. */
7901 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7902 /* If it worked, look for more conversion-declarators. */
7903 if (cp_parser_parse_definitely (parser))
7905 cp_declarator *declarator;
7907 /* Parse another optional declarator. */
7908 declarator = cp_parser_conversion_declarator_opt (parser);
7910 /* Create the representation of the declarator. */
7912 declarator = make_ptrmem_declarator (cv_quals, class_type,
7914 else if (code == INDIRECT_REF)
7915 declarator = make_pointer_declarator (cv_quals, declarator);
7917 declarator = make_reference_declarator (cv_quals, declarator);
7925 /* Parse an (optional) ctor-initializer.
7928 : mem-initializer-list
7930 Returns TRUE iff the ctor-initializer was actually present. */
7933 cp_parser_ctor_initializer_opt (cp_parser* parser)
7935 /* If the next token is not a `:', then there is no
7936 ctor-initializer. */
7937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7939 /* Do default initialization of any bases and members. */
7940 if (DECL_CONSTRUCTOR_P (current_function_decl))
7941 finish_mem_initializers (NULL_TREE);
7946 /* Consume the `:' token. */
7947 cp_lexer_consume_token (parser->lexer);
7948 /* And the mem-initializer-list. */
7949 cp_parser_mem_initializer_list (parser);
7954 /* Parse a mem-initializer-list.
7956 mem-initializer-list:
7958 mem-initializer , mem-initializer-list */
7961 cp_parser_mem_initializer_list (cp_parser* parser)
7963 tree mem_initializer_list = NULL_TREE;
7965 /* Let the semantic analysis code know that we are starting the
7966 mem-initializer-list. */
7967 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7968 error ("only constructors take base initializers");
7970 /* Loop through the list. */
7973 tree mem_initializer;
7975 /* Parse the mem-initializer. */
7976 mem_initializer = cp_parser_mem_initializer (parser);
7977 /* Add it to the list, unless it was erroneous. */
7978 if (mem_initializer != error_mark_node)
7980 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7981 mem_initializer_list = mem_initializer;
7983 /* If the next token is not a `,', we're done. */
7984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7986 /* Consume the `,' token. */
7987 cp_lexer_consume_token (parser->lexer);
7990 /* Perform semantic analysis. */
7991 if (DECL_CONSTRUCTOR_P (current_function_decl))
7992 finish_mem_initializers (mem_initializer_list);
7995 /* Parse a mem-initializer.
7998 mem-initializer-id ( expression-list [opt] )
8003 ( expression-list [opt] )
8005 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8006 class) or FIELD_DECL (for a non-static data member) to initialize;
8007 the TREE_VALUE is the expression-list. An empty initialization
8008 list is represented by void_list_node. */
8011 cp_parser_mem_initializer (cp_parser* parser)
8013 tree mem_initializer_id;
8014 tree expression_list;
8017 /* Find out what is being initialized. */
8018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8020 pedwarn ("anachronistic old-style base class initializer");
8021 mem_initializer_id = NULL_TREE;
8024 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8025 member = expand_member_init (mem_initializer_id);
8026 if (member && !DECL_P (member))
8027 in_base_initializer = 1;
8030 = cp_parser_parenthesized_expression_list (parser, false,
8032 /*non_constant_p=*/NULL);
8033 if (expression_list == error_mark_node)
8034 return error_mark_node;
8035 if (!expression_list)
8036 expression_list = void_type_node;
8038 in_base_initializer = 0;
8040 return member ? build_tree_list (member, expression_list) : error_mark_node;
8043 /* Parse a mem-initializer-id.
8046 :: [opt] nested-name-specifier [opt] class-name
8049 Returns a TYPE indicating the class to be initializer for the first
8050 production. Returns an IDENTIFIER_NODE indicating the data member
8051 to be initialized for the second production. */
8054 cp_parser_mem_initializer_id (cp_parser* parser)
8056 bool global_scope_p;
8057 bool nested_name_specifier_p;
8058 bool template_p = false;
8061 /* `typename' is not allowed in this context ([temp.res]). */
8062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8064 error ("keyword %<typename%> not allowed in this context (a qualified "
8065 "member initializer is implicitly a type)");
8066 cp_lexer_consume_token (parser->lexer);
8068 /* Look for the optional `::' operator. */
8070 = (cp_parser_global_scope_opt (parser,
8071 /*current_scope_valid_p=*/false,
8072 /*object_scope_valid_p=*/false)
8074 /* Look for the optional nested-name-specifier. The simplest way to
8079 The keyword `typename' is not permitted in a base-specifier or
8080 mem-initializer; in these contexts a qualified name that
8081 depends on a template-parameter is implicitly assumed to be a
8084 is to assume that we have seen the `typename' keyword at this
8086 nested_name_specifier_p
8087 = (cp_parser_nested_name_specifier_opt (parser,
8088 /*typename_keyword_p=*/true,
8089 /*check_dependency_p=*/true,
8091 /*is_declaration=*/true)
8093 if (nested_name_specifier_p)
8094 template_p = cp_parser_optional_template_keyword (parser);
8095 /* If there is a `::' operator or a nested-name-specifier, then we
8096 are definitely looking for a class-name. */
8097 if (global_scope_p || nested_name_specifier_p)
8098 return cp_parser_class_name (parser,
8099 /*typename_keyword_p=*/true,
8100 /*template_keyword_p=*/template_p,
8102 /*check_dependency_p=*/true,
8103 /*class_head_p=*/false,
8104 /*is_declaration=*/true);
8105 /* Otherwise, we could also be looking for an ordinary identifier. */
8106 cp_parser_parse_tentatively (parser);
8107 /* Try a class-name. */
8108 id = cp_parser_class_name (parser,
8109 /*typename_keyword_p=*/true,
8110 /*template_keyword_p=*/false,
8112 /*check_dependency_p=*/true,
8113 /*class_head_p=*/false,
8114 /*is_declaration=*/true);
8115 /* If we found one, we're done. */
8116 if (cp_parser_parse_definitely (parser))
8118 /* Otherwise, look for an ordinary identifier. */
8119 return cp_parser_identifier (parser);
8122 /* Overloading [gram.over] */
8124 /* Parse an operator-function-id.
8126 operator-function-id:
8129 Returns an IDENTIFIER_NODE for the operator which is a
8130 human-readable spelling of the identifier, e.g., `operator +'. */
8133 cp_parser_operator_function_id (cp_parser* parser)
8135 /* Look for the `operator' keyword. */
8136 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8137 return error_mark_node;
8138 /* And then the name of the operator itself. */
8139 return cp_parser_operator (parser);
8142 /* Parse an operator.
8145 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8146 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8147 || ++ -- , ->* -> () []
8154 Returns an IDENTIFIER_NODE for the operator which is a
8155 human-readable spelling of the identifier, e.g., `operator +'. */
8158 cp_parser_operator (cp_parser* parser)
8160 tree id = NULL_TREE;
8163 /* Peek at the next token. */
8164 token = cp_lexer_peek_token (parser->lexer);
8165 /* Figure out which operator we have. */
8166 switch (token->type)
8172 /* The keyword should be either `new' or `delete'. */
8173 if (token->keyword == RID_NEW)
8175 else if (token->keyword == RID_DELETE)
8180 /* Consume the `new' or `delete' token. */
8181 cp_lexer_consume_token (parser->lexer);
8183 /* Peek at the next token. */
8184 token = cp_lexer_peek_token (parser->lexer);
8185 /* If it's a `[' token then this is the array variant of the
8187 if (token->type == CPP_OPEN_SQUARE)
8189 /* Consume the `[' token. */
8190 cp_lexer_consume_token (parser->lexer);
8191 /* Look for the `]' token. */
8192 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8193 id = ansi_opname (op == NEW_EXPR
8194 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8196 /* Otherwise, we have the non-array variant. */
8198 id = ansi_opname (op);
8204 id = ansi_opname (PLUS_EXPR);
8208 id = ansi_opname (MINUS_EXPR);
8212 id = ansi_opname (MULT_EXPR);
8216 id = ansi_opname (TRUNC_DIV_EXPR);
8220 id = ansi_opname (TRUNC_MOD_EXPR);
8224 id = ansi_opname (BIT_XOR_EXPR);
8228 id = ansi_opname (BIT_AND_EXPR);
8232 id = ansi_opname (BIT_IOR_EXPR);
8236 id = ansi_opname (BIT_NOT_EXPR);
8240 id = ansi_opname (TRUTH_NOT_EXPR);
8244 id = ansi_assopname (NOP_EXPR);
8248 id = ansi_opname (LT_EXPR);
8252 id = ansi_opname (GT_EXPR);
8256 id = ansi_assopname (PLUS_EXPR);
8260 id = ansi_assopname (MINUS_EXPR);
8264 id = ansi_assopname (MULT_EXPR);
8268 id = ansi_assopname (TRUNC_DIV_EXPR);
8272 id = ansi_assopname (TRUNC_MOD_EXPR);
8276 id = ansi_assopname (BIT_XOR_EXPR);
8280 id = ansi_assopname (BIT_AND_EXPR);
8284 id = ansi_assopname (BIT_IOR_EXPR);
8288 id = ansi_opname (LSHIFT_EXPR);
8292 id = ansi_opname (RSHIFT_EXPR);
8296 id = ansi_assopname (LSHIFT_EXPR);
8300 id = ansi_assopname (RSHIFT_EXPR);
8304 id = ansi_opname (EQ_EXPR);
8308 id = ansi_opname (NE_EXPR);
8312 id = ansi_opname (LE_EXPR);
8315 case CPP_GREATER_EQ:
8316 id = ansi_opname (GE_EXPR);
8320 id = ansi_opname (TRUTH_ANDIF_EXPR);
8324 id = ansi_opname (TRUTH_ORIF_EXPR);
8328 id = ansi_opname (POSTINCREMENT_EXPR);
8331 case CPP_MINUS_MINUS:
8332 id = ansi_opname (PREDECREMENT_EXPR);
8336 id = ansi_opname (COMPOUND_EXPR);
8339 case CPP_DEREF_STAR:
8340 id = ansi_opname (MEMBER_REF);
8344 id = ansi_opname (COMPONENT_REF);
8347 case CPP_OPEN_PAREN:
8348 /* Consume the `('. */
8349 cp_lexer_consume_token (parser->lexer);
8350 /* Look for the matching `)'. */
8351 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8352 return ansi_opname (CALL_EXPR);
8354 case CPP_OPEN_SQUARE:
8355 /* Consume the `['. */
8356 cp_lexer_consume_token (parser->lexer);
8357 /* Look for the matching `]'. */
8358 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8359 return ansi_opname (ARRAY_REF);
8362 /* Anything else is an error. */
8366 /* If we have selected an identifier, we need to consume the
8369 cp_lexer_consume_token (parser->lexer);
8370 /* Otherwise, no valid operator name was present. */
8373 cp_parser_error (parser, "expected operator");
8374 id = error_mark_node;
8380 /* Parse a template-declaration.
8382 template-declaration:
8383 export [opt] template < template-parameter-list > declaration
8385 If MEMBER_P is TRUE, this template-declaration occurs within a
8388 The grammar rule given by the standard isn't correct. What
8391 template-declaration:
8392 export [opt] template-parameter-list-seq
8393 decl-specifier-seq [opt] init-declarator [opt] ;
8394 export [opt] template-parameter-list-seq
8397 template-parameter-list-seq:
8398 template-parameter-list-seq [opt]
8399 template < template-parameter-list > */
8402 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8404 /* Check for `export'. */
8405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8407 /* Consume the `export' token. */
8408 cp_lexer_consume_token (parser->lexer);
8409 /* Warn that we do not support `export'. */
8410 warning (0, "keyword %<export%> not implemented, and will be ignored");
8413 cp_parser_template_declaration_after_export (parser, member_p);
8416 /* Parse a template-parameter-list.
8418 template-parameter-list:
8420 template-parameter-list , template-parameter
8422 Returns a TREE_LIST. Each node represents a template parameter.
8423 The nodes are connected via their TREE_CHAINs. */
8426 cp_parser_template_parameter_list (cp_parser* parser)
8428 tree parameter_list = NULL_TREE;
8430 begin_template_parm_list ();
8437 /* Parse the template-parameter. */
8438 parameter = cp_parser_template_parameter (parser, &is_non_type);
8439 /* Add it to the list. */
8440 if (parameter != error_mark_node)
8441 parameter_list = process_template_parm (parameter_list,
8446 tree err_parm = build_tree_list (parameter, parameter);
8447 TREE_VALUE (err_parm) = error_mark_node;
8448 parameter_list = chainon (parameter_list, err_parm);
8451 /* Peek at the next token. */
8452 token = cp_lexer_peek_token (parser->lexer);
8453 /* If it's not a `,', we're done. */
8454 if (token->type != CPP_COMMA)
8456 /* Otherwise, consume the `,' token. */
8457 cp_lexer_consume_token (parser->lexer);
8460 return end_template_parm_list (parameter_list);
8463 /* Parse a template-parameter.
8467 parameter-declaration
8469 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8470 the parameter. The TREE_PURPOSE is the default value, if any.
8471 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8472 iff this parameter is a non-type parameter. */
8475 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8478 cp_parameter_declarator *parameter_declarator;
8481 /* Assume it is a type parameter or a template parameter. */
8482 *is_non_type = false;
8483 /* Peek at the next token. */
8484 token = cp_lexer_peek_token (parser->lexer);
8485 /* If it is `class' or `template', we have a type-parameter. */
8486 if (token->keyword == RID_TEMPLATE)
8487 return cp_parser_type_parameter (parser);
8488 /* If it is `class' or `typename' we do not know yet whether it is a
8489 type parameter or a non-type parameter. Consider:
8491 template <typename T, typename T::X X> ...
8495 template <class C, class D*> ...
8497 Here, the first parameter is a type parameter, and the second is
8498 a non-type parameter. We can tell by looking at the token after
8499 the identifier -- if it is a `,', `=', or `>' then we have a type
8501 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8503 /* Peek at the token after `class' or `typename'. */
8504 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8505 /* If it's an identifier, skip it. */
8506 if (token->type == CPP_NAME)
8507 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8508 /* Now, see if the token looks like the end of a template
8510 if (token->type == CPP_COMMA
8511 || token->type == CPP_EQ
8512 || token->type == CPP_GREATER)
8513 return cp_parser_type_parameter (parser);
8516 /* Otherwise, it is a non-type parameter.
8520 When parsing a default template-argument for a non-type
8521 template-parameter, the first non-nested `>' is taken as the end
8522 of the template parameter-list rather than a greater-than
8524 *is_non_type = true;
8525 parameter_declarator
8526 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8527 /*parenthesized_p=*/NULL);
8528 parm = grokdeclarator (parameter_declarator->declarator,
8529 ¶meter_declarator->decl_specifiers,
8530 PARM, /*initialized=*/0,
8532 if (parm == error_mark_node)
8533 return error_mark_node;
8534 return build_tree_list (parameter_declarator->default_argument, parm);
8537 /* Parse a type-parameter.
8540 class identifier [opt]
8541 class identifier [opt] = type-id
8542 typename identifier [opt]
8543 typename identifier [opt] = type-id
8544 template < template-parameter-list > class identifier [opt]
8545 template < template-parameter-list > class identifier [opt]
8548 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8549 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8550 the declaration of the parameter. */
8553 cp_parser_type_parameter (cp_parser* parser)
8558 /* Look for a keyword to tell us what kind of parameter this is. */
8559 token = cp_parser_require (parser, CPP_KEYWORD,
8560 "`class', `typename', or `template'");
8562 return error_mark_node;
8564 switch (token->keyword)
8570 tree default_argument;
8572 /* If the next token is an identifier, then it names the
8574 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8575 identifier = cp_parser_identifier (parser);
8577 identifier = NULL_TREE;
8579 /* Create the parameter. */
8580 parameter = finish_template_type_parm (class_type_node, identifier);
8582 /* If the next token is an `=', we have a default argument. */
8583 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8585 /* Consume the `=' token. */
8586 cp_lexer_consume_token (parser->lexer);
8587 /* Parse the default-argument. */
8588 push_deferring_access_checks (dk_no_deferred);
8589 default_argument = cp_parser_type_id (parser);
8590 pop_deferring_access_checks ();
8593 default_argument = NULL_TREE;
8595 /* Create the combined representation of the parameter and the
8596 default argument. */
8597 parameter = build_tree_list (default_argument, parameter);
8603 tree parameter_list;
8605 tree default_argument;
8607 /* Look for the `<'. */
8608 cp_parser_require (parser, CPP_LESS, "`<'");
8609 /* Parse the template-parameter-list. */
8610 parameter_list = cp_parser_template_parameter_list (parser);
8611 /* Look for the `>'. */
8612 cp_parser_require (parser, CPP_GREATER, "`>'");
8613 /* Look for the `class' keyword. */
8614 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8615 /* If the next token is an `=', then there is a
8616 default-argument. If the next token is a `>', we are at
8617 the end of the parameter-list. If the next token is a `,',
8618 then we are at the end of this parameter. */
8619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8620 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8621 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8623 identifier = cp_parser_identifier (parser);
8624 /* Treat invalid names as if the parameter were nameless. */
8625 if (identifier == error_mark_node)
8626 identifier = NULL_TREE;
8629 identifier = NULL_TREE;
8631 /* Create the template parameter. */
8632 parameter = finish_template_template_parm (class_type_node,
8635 /* If the next token is an `=', then there is a
8636 default-argument. */
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8641 /* Consume the `='. */
8642 cp_lexer_consume_token (parser->lexer);
8643 /* Parse the id-expression. */
8644 push_deferring_access_checks (dk_no_deferred);
8646 = cp_parser_id_expression (parser,
8647 /*template_keyword_p=*/false,
8648 /*check_dependency_p=*/true,
8649 /*template_p=*/&is_template,
8650 /*declarator_p=*/false,
8651 /*optional_p=*/false,
8652 /*member_p=*/false);
8653 if (TREE_CODE (default_argument) == TYPE_DECL)
8654 /* If the id-expression was a template-id that refers to
8655 a template-class, we already have the declaration here,
8656 so no further lookup is needed. */
8659 /* Look up the name. */
8661 = cp_parser_lookup_name (parser, default_argument,
8663 /*is_template=*/is_template,
8664 /*is_namespace=*/false,
8665 /*check_dependency=*/true,
8666 /*ambiguous_decls=*/NULL);
8667 /* See if the default argument is valid. */
8669 = check_template_template_default_arg (default_argument);
8670 pop_deferring_access_checks ();
8673 default_argument = NULL_TREE;
8675 /* Create the combined representation of the parameter and the
8676 default argument. */
8677 parameter = build_tree_list (default_argument, parameter);
8689 /* Parse a template-id.
8692 template-name < template-argument-list [opt] >
8694 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8695 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8696 returned. Otherwise, if the template-name names a function, or set
8697 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8698 names a class, returns a TYPE_DECL for the specialization.
8700 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8701 uninstantiated templates. */
8704 cp_parser_template_id (cp_parser *parser,
8705 bool template_keyword_p,
8706 bool check_dependency_p,
8707 bool is_declaration)
8712 cp_token_position start_of_id = 0;
8713 tree access_check = NULL_TREE;
8714 cp_token *next_token, *next_token_2;
8717 /* If the next token corresponds to a template-id, there is no need
8719 next_token = cp_lexer_peek_token (parser->lexer);
8720 if (next_token->type == CPP_TEMPLATE_ID)
8725 /* Get the stored value. */
8726 value = cp_lexer_consume_token (parser->lexer)->value;
8727 /* Perform any access checks that were deferred. */
8728 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8729 perform_or_defer_access_check (TREE_PURPOSE (check),
8730 TREE_VALUE (check));
8731 /* Return the stored value. */
8732 return TREE_VALUE (value);
8735 /* Avoid performing name lookup if there is no possibility of
8736 finding a template-id. */
8737 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8738 || (next_token->type == CPP_NAME
8739 && !cp_parser_nth_token_starts_template_argument_list_p
8742 cp_parser_error (parser, "expected template-id");
8743 return error_mark_node;
8746 /* Remember where the template-id starts. */
8747 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8748 start_of_id = cp_lexer_token_position (parser->lexer, false);
8750 push_deferring_access_checks (dk_deferred);
8752 /* Parse the template-name. */
8753 is_identifier = false;
8754 template = cp_parser_template_name (parser, template_keyword_p,
8758 if (template == error_mark_node || is_identifier)
8760 pop_deferring_access_checks ();
8764 /* If we find the sequence `[:' after a template-name, it's probably
8765 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8766 parse correctly the argument list. */
8767 next_token = cp_lexer_peek_token (parser->lexer);
8768 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8769 if (next_token->type == CPP_OPEN_SQUARE
8770 && next_token->flags & DIGRAPH
8771 && next_token_2->type == CPP_COLON
8772 && !(next_token_2->flags & PREV_WHITE))
8774 cp_parser_parse_tentatively (parser);
8775 /* Change `:' into `::'. */
8776 next_token_2->type = CPP_SCOPE;
8777 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8779 cp_lexer_consume_token (parser->lexer);
8780 /* Parse the arguments. */
8781 arguments = cp_parser_enclosed_template_argument_list (parser);
8782 if (!cp_parser_parse_definitely (parser))
8784 /* If we couldn't parse an argument list, then we revert our changes
8785 and return simply an error. Maybe this is not a template-id
8787 next_token_2->type = CPP_COLON;
8788 cp_parser_error (parser, "expected %<<%>");
8789 pop_deferring_access_checks ();
8790 return error_mark_node;
8792 /* Otherwise, emit an error about the invalid digraph, but continue
8793 parsing because we got our argument list. */
8794 pedwarn ("%<<::%> cannot begin a template-argument list");
8795 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8796 "between %<<%> and %<::%>");
8797 if (!flag_permissive)
8802 inform ("(if you use -fpermissive G++ will accept your code)");
8809 /* Look for the `<' that starts the template-argument-list. */
8810 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8812 pop_deferring_access_checks ();
8813 return error_mark_node;
8815 /* Parse the arguments. */
8816 arguments = cp_parser_enclosed_template_argument_list (parser);
8819 /* Build a representation of the specialization. */
8820 if (TREE_CODE (template) == IDENTIFIER_NODE)
8821 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8822 else if (DECL_CLASS_TEMPLATE_P (template)
8823 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8825 bool entering_scope;
8826 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8827 template (rather than some instantiation thereof) only if
8828 is not nested within some other construct. For example, in
8829 "template <typename T> void f(T) { A<T>::", A<T> is just an
8830 instantiation of A. */
8831 entering_scope = (template_parm_scope_p ()
8832 && cp_lexer_next_token_is (parser->lexer,
8835 = finish_template_type (template, arguments, entering_scope);
8839 /* If it's not a class-template or a template-template, it should be
8840 a function-template. */
8841 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8842 || TREE_CODE (template) == OVERLOAD
8843 || BASELINK_P (template)));
8845 template_id = lookup_template_function (template, arguments);
8848 /* Retrieve any deferred checks. Do not pop this access checks yet
8849 so the memory will not be reclaimed during token replacing below. */
8850 access_check = get_deferred_access_checks ();
8852 /* If parsing tentatively, replace the sequence of tokens that makes
8853 up the template-id with a CPP_TEMPLATE_ID token. That way,
8854 should we re-parse the token stream, we will not have to repeat
8855 the effort required to do the parse, nor will we issue duplicate
8856 error messages about problems during instantiation of the
8860 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8862 /* Reset the contents of the START_OF_ID token. */
8863 token->type = CPP_TEMPLATE_ID;
8864 token->value = build_tree_list (access_check, template_id);
8865 token->keyword = RID_MAX;
8867 /* Purge all subsequent tokens. */
8868 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8870 /* ??? Can we actually assume that, if template_id ==
8871 error_mark_node, we will have issued a diagnostic to the
8872 user, as opposed to simply marking the tentative parse as
8874 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8875 error ("parse error in template argument list");
8878 pop_deferring_access_checks ();
8882 /* Parse a template-name.
8887 The standard should actually say:
8891 operator-function-id
8893 A defect report has been filed about this issue.
8895 A conversion-function-id cannot be a template name because they cannot
8896 be part of a template-id. In fact, looking at this code:
8900 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8901 It is impossible to call a templated conversion-function-id with an
8902 explicit argument list, since the only allowed template parameter is
8903 the type to which it is converting.
8905 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8906 `template' keyword, in a construction like:
8910 In that case `f' is taken to be a template-name, even though there
8911 is no way of knowing for sure.
8913 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8914 name refers to a set of overloaded functions, at least one of which
8915 is a template, or an IDENTIFIER_NODE with the name of the template,
8916 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8917 names are looked up inside uninstantiated templates. */
8920 cp_parser_template_name (cp_parser* parser,
8921 bool template_keyword_p,
8922 bool check_dependency_p,
8923 bool is_declaration,
8924 bool *is_identifier)
8930 /* If the next token is `operator', then we have either an
8931 operator-function-id or a conversion-function-id. */
8932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8934 /* We don't know whether we're looking at an
8935 operator-function-id or a conversion-function-id. */
8936 cp_parser_parse_tentatively (parser);
8937 /* Try an operator-function-id. */
8938 identifier = cp_parser_operator_function_id (parser);
8939 /* If that didn't work, try a conversion-function-id. */
8940 if (!cp_parser_parse_definitely (parser))
8942 cp_parser_error (parser, "expected template-name");
8943 return error_mark_node;
8946 /* Look for the identifier. */
8948 identifier = cp_parser_identifier (parser);
8950 /* If we didn't find an identifier, we don't have a template-id. */
8951 if (identifier == error_mark_node)
8952 return error_mark_node;
8954 /* If the name immediately followed the `template' keyword, then it
8955 is a template-name. However, if the next token is not `<', then
8956 we do not treat it as a template-name, since it is not being used
8957 as part of a template-id. This enables us to handle constructs
8960 template <typename T> struct S { S(); };
8961 template <typename T> S<T>::S();
8963 correctly. We would treat `S' as a template -- if it were `S<T>'
8964 -- but we do not if there is no `<'. */
8966 if (processing_template_decl
8967 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8969 /* In a declaration, in a dependent context, we pretend that the
8970 "template" keyword was present in order to improve error
8971 recovery. For example, given:
8973 template <typename T> void f(T::X<int>);
8975 we want to treat "X<int>" as a template-id. */
8977 && !template_keyword_p
8978 && parser->scope && TYPE_P (parser->scope)
8979 && check_dependency_p
8980 && dependent_type_p (parser->scope)
8981 /* Do not do this for dtors (or ctors), since they never
8982 need the template keyword before their name. */
8983 && !constructor_name_p (identifier, parser->scope))
8985 cp_token_position start = 0;
8987 /* Explain what went wrong. */
8988 error ("non-template %qD used as template", identifier);
8989 inform ("use %<%T::template %D%> to indicate that it is a template",
8990 parser->scope, identifier);
8991 /* If parsing tentatively, find the location of the "<" token. */
8992 if (cp_parser_simulate_error (parser))
8993 start = cp_lexer_token_position (parser->lexer, true);
8994 /* Parse the template arguments so that we can issue error
8995 messages about them. */
8996 cp_lexer_consume_token (parser->lexer);
8997 cp_parser_enclosed_template_argument_list (parser);
8998 /* Skip tokens until we find a good place from which to
8999 continue parsing. */
9000 cp_parser_skip_to_closing_parenthesis (parser,
9001 /*recovering=*/true,
9003 /*consume_paren=*/false);
9004 /* If parsing tentatively, permanently remove the
9005 template argument list. That will prevent duplicate
9006 error messages from being issued about the missing
9007 "template" keyword. */
9009 cp_lexer_purge_tokens_after (parser->lexer, start);
9011 *is_identifier = true;
9015 /* If the "template" keyword is present, then there is generally
9016 no point in doing name-lookup, so we just return IDENTIFIER.
9017 But, if the qualifying scope is non-dependent then we can
9018 (and must) do name-lookup normally. */
9019 if (template_keyword_p
9021 || (TYPE_P (parser->scope)
9022 && dependent_type_p (parser->scope))))
9026 /* Look up the name. */
9027 decl = cp_parser_lookup_name (parser, identifier,
9029 /*is_template=*/false,
9030 /*is_namespace=*/false,
9032 /*ambiguous_decls=*/NULL);
9033 decl = maybe_get_template_decl_from_type_decl (decl);
9035 /* If DECL is a template, then the name was a template-name. */
9036 if (TREE_CODE (decl) == TEMPLATE_DECL)
9040 tree fn = NULL_TREE;
9042 /* The standard does not explicitly indicate whether a name that
9043 names a set of overloaded declarations, some of which are
9044 templates, is a template-name. However, such a name should
9045 be a template-name; otherwise, there is no way to form a
9046 template-id for the overloaded templates. */
9047 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9048 if (TREE_CODE (fns) == OVERLOAD)
9049 for (fn = fns; fn; fn = OVL_NEXT (fn))
9050 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9055 /* The name does not name a template. */
9056 cp_parser_error (parser, "expected template-name");
9057 return error_mark_node;
9061 /* If DECL is dependent, and refers to a function, then just return
9062 its name; we will look it up again during template instantiation. */
9063 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9065 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9066 if (TYPE_P (scope) && dependent_type_p (scope))
9073 /* Parse a template-argument-list.
9075 template-argument-list:
9077 template-argument-list , template-argument
9079 Returns a TREE_VEC containing the arguments. */
9082 cp_parser_template_argument_list (cp_parser* parser)
9084 tree fixed_args[10];
9085 unsigned n_args = 0;
9086 unsigned alloced = 10;
9087 tree *arg_ary = fixed_args;
9089 bool saved_in_template_argument_list_p;
9091 bool saved_non_ice_p;
9093 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9094 parser->in_template_argument_list_p = true;
9095 /* Even if the template-id appears in an integral
9096 constant-expression, the contents of the argument list do
9098 saved_ice_p = parser->integral_constant_expression_p;
9099 parser->integral_constant_expression_p = false;
9100 saved_non_ice_p = parser->non_integral_constant_expression_p;
9101 parser->non_integral_constant_expression_p = false;
9102 /* Parse the arguments. */
9108 /* Consume the comma. */
9109 cp_lexer_consume_token (parser->lexer);
9111 /* Parse the template-argument. */
9112 argument = cp_parser_template_argument (parser);
9113 if (n_args == alloced)
9117 if (arg_ary == fixed_args)
9119 arg_ary = XNEWVEC (tree, alloced);
9120 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9123 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9125 arg_ary[n_args++] = argument;
9127 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9129 vec = make_tree_vec (n_args);
9132 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9134 if (arg_ary != fixed_args)
9136 parser->non_integral_constant_expression_p = saved_non_ice_p;
9137 parser->integral_constant_expression_p = saved_ice_p;
9138 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9142 /* Parse a template-argument.
9145 assignment-expression
9149 The representation is that of an assignment-expression, type-id, or
9150 id-expression -- except that the qualified id-expression is
9151 evaluated, so that the value returned is either a DECL or an
9154 Although the standard says "assignment-expression", it forbids
9155 throw-expressions or assignments in the template argument.
9156 Therefore, we use "conditional-expression" instead. */
9159 cp_parser_template_argument (cp_parser* parser)
9164 bool maybe_type_id = false;
9168 /* There's really no way to know what we're looking at, so we just
9169 try each alternative in order.
9173 In a template-argument, an ambiguity between a type-id and an
9174 expression is resolved to a type-id, regardless of the form of
9175 the corresponding template-parameter.
9177 Therefore, we try a type-id first. */
9178 cp_parser_parse_tentatively (parser);
9179 argument = cp_parser_type_id (parser);
9180 /* If there was no error parsing the type-id but the next token is a '>>',
9181 we probably found a typo for '> >'. But there are type-id which are
9182 also valid expressions. For instance:
9184 struct X { int operator >> (int); };
9185 template <int V> struct Foo {};
9188 Here 'X()' is a valid type-id of a function type, but the user just
9189 wanted to write the expression "X() >> 5". Thus, we remember that we
9190 found a valid type-id, but we still try to parse the argument as an
9191 expression to see what happens. */
9192 if (!cp_parser_error_occurred (parser)
9193 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9195 maybe_type_id = true;
9196 cp_parser_abort_tentative_parse (parser);
9200 /* If the next token isn't a `,' or a `>', then this argument wasn't
9201 really finished. This means that the argument is not a valid
9203 if (!cp_parser_next_token_ends_template_argument_p (parser))
9204 cp_parser_error (parser, "expected template-argument");
9205 /* If that worked, we're done. */
9206 if (cp_parser_parse_definitely (parser))
9209 /* We're still not sure what the argument will be. */
9210 cp_parser_parse_tentatively (parser);
9211 /* Try a template. */
9212 argument = cp_parser_id_expression (parser,
9213 /*template_keyword_p=*/false,
9214 /*check_dependency_p=*/true,
9216 /*declarator_p=*/false,
9217 /*optional_p=*/false,
9218 /*member_p=*/false);
9219 /* If the next token isn't a `,' or a `>', then this argument wasn't
9221 if (!cp_parser_next_token_ends_template_argument_p (parser))
9222 cp_parser_error (parser, "expected template-argument");
9223 if (!cp_parser_error_occurred (parser))
9225 /* Figure out what is being referred to. If the id-expression
9226 was for a class template specialization, then we will have a
9227 TYPE_DECL at this point. There is no need to do name lookup
9228 at this point in that case. */
9229 if (TREE_CODE (argument) != TYPE_DECL)
9230 argument = cp_parser_lookup_name (parser, argument,
9232 /*is_template=*/template_p,
9233 /*is_namespace=*/false,
9234 /*check_dependency=*/true,
9235 /*ambiguous_decls=*/NULL);
9236 if (TREE_CODE (argument) != TEMPLATE_DECL
9237 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9238 cp_parser_error (parser, "expected template-name");
9240 if (cp_parser_parse_definitely (parser))
9242 /* It must be a non-type argument. There permitted cases are given
9243 in [temp.arg.nontype]:
9245 -- an integral constant-expression of integral or enumeration
9248 -- the name of a non-type template-parameter; or
9250 -- the name of an object or function with external linkage...
9252 -- the address of an object or function with external linkage...
9254 -- a pointer to member... */
9255 /* Look for a non-type template parameter. */
9256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9258 cp_parser_parse_tentatively (parser);
9259 argument = cp_parser_primary_expression (parser,
9262 /*template_arg_p=*/true,
9264 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9265 || !cp_parser_next_token_ends_template_argument_p (parser))
9266 cp_parser_simulate_error (parser);
9267 if (cp_parser_parse_definitely (parser))
9271 /* If the next token is "&", the argument must be the address of an
9272 object or function with external linkage. */
9273 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9275 cp_lexer_consume_token (parser->lexer);
9276 /* See if we might have an id-expression. */
9277 token = cp_lexer_peek_token (parser->lexer);
9278 if (token->type == CPP_NAME
9279 || token->keyword == RID_OPERATOR
9280 || token->type == CPP_SCOPE
9281 || token->type == CPP_TEMPLATE_ID
9282 || token->type == CPP_NESTED_NAME_SPECIFIER)
9284 cp_parser_parse_tentatively (parser);
9285 argument = cp_parser_primary_expression (parser,
9288 /*template_arg_p=*/true,
9290 if (cp_parser_error_occurred (parser)
9291 || !cp_parser_next_token_ends_template_argument_p (parser))
9292 cp_parser_abort_tentative_parse (parser);
9295 if (TREE_CODE (argument) == INDIRECT_REF)
9297 gcc_assert (REFERENCE_REF_P (argument));
9298 argument = TREE_OPERAND (argument, 0);
9301 if (TREE_CODE (argument) == VAR_DECL)
9303 /* A variable without external linkage might still be a
9304 valid constant-expression, so no error is issued here
9305 if the external-linkage check fails. */
9306 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9307 cp_parser_simulate_error (parser);
9309 else if (is_overloaded_fn (argument))
9310 /* All overloaded functions are allowed; if the external
9311 linkage test does not pass, an error will be issued
9315 && (TREE_CODE (argument) == OFFSET_REF
9316 || TREE_CODE (argument) == SCOPE_REF))
9317 /* A pointer-to-member. */
9319 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9322 cp_parser_simulate_error (parser);
9324 if (cp_parser_parse_definitely (parser))
9327 argument = build_x_unary_op (ADDR_EXPR, argument);
9332 /* If the argument started with "&", there are no other valid
9333 alternatives at this point. */
9336 cp_parser_error (parser, "invalid non-type template argument");
9337 return error_mark_node;
9340 /* If the argument wasn't successfully parsed as a type-id followed
9341 by '>>', the argument can only be a constant expression now.
9342 Otherwise, we try parsing the constant-expression tentatively,
9343 because the argument could really be a type-id. */
9345 cp_parser_parse_tentatively (parser);
9346 argument = cp_parser_constant_expression (parser,
9347 /*allow_non_constant_p=*/false,
9348 /*non_constant_p=*/NULL);
9349 argument = fold_non_dependent_expr (argument);
9352 if (!cp_parser_next_token_ends_template_argument_p (parser))
9353 cp_parser_error (parser, "expected template-argument");
9354 if (cp_parser_parse_definitely (parser))
9356 /* We did our best to parse the argument as a non type-id, but that
9357 was the only alternative that matched (albeit with a '>' after
9358 it). We can assume it's just a typo from the user, and a
9359 diagnostic will then be issued. */
9360 return cp_parser_type_id (parser);
9363 /* Parse an explicit-instantiation.
9365 explicit-instantiation:
9366 template declaration
9368 Although the standard says `declaration', what it really means is:
9370 explicit-instantiation:
9371 template decl-specifier-seq [opt] declarator [opt] ;
9373 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9374 supposed to be allowed. A defect report has been filed about this
9379 explicit-instantiation:
9380 storage-class-specifier template
9381 decl-specifier-seq [opt] declarator [opt] ;
9382 function-specifier template
9383 decl-specifier-seq [opt] declarator [opt] ; */
9386 cp_parser_explicit_instantiation (cp_parser* parser)
9388 int declares_class_or_enum;
9389 cp_decl_specifier_seq decl_specifiers;
9390 tree extension_specifier = NULL_TREE;
9392 /* Look for an (optional) storage-class-specifier or
9393 function-specifier. */
9394 if (cp_parser_allow_gnu_extensions_p (parser))
9397 = cp_parser_storage_class_specifier_opt (parser);
9398 if (!extension_specifier)
9400 = cp_parser_function_specifier_opt (parser,
9401 /*decl_specs=*/NULL);
9404 /* Look for the `template' keyword. */
9405 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9406 /* Let the front end know that we are processing an explicit
9408 begin_explicit_instantiation ();
9409 /* [temp.explicit] says that we are supposed to ignore access
9410 control while processing explicit instantiation directives. */
9411 push_deferring_access_checks (dk_no_check);
9412 /* Parse a decl-specifier-seq. */
9413 cp_parser_decl_specifier_seq (parser,
9414 CP_PARSER_FLAGS_OPTIONAL,
9416 &declares_class_or_enum);
9417 /* If there was exactly one decl-specifier, and it declared a class,
9418 and there's no declarator, then we have an explicit type
9420 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9424 type = check_tag_decl (&decl_specifiers);
9425 /* Turn access control back on for names used during
9426 template instantiation. */
9427 pop_deferring_access_checks ();
9429 do_type_instantiation (type, extension_specifier,
9430 /*complain=*/tf_error);
9434 cp_declarator *declarator;
9437 /* Parse the declarator. */
9439 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9440 /*ctor_dtor_or_conv_p=*/NULL,
9441 /*parenthesized_p=*/NULL,
9442 /*member_p=*/false);
9443 if (declares_class_or_enum & 2)
9444 cp_parser_check_for_definition_in_return_type (declarator,
9445 decl_specifiers.type);
9446 if (declarator != cp_error_declarator)
9448 decl = grokdeclarator (declarator, &decl_specifiers,
9449 NORMAL, 0, &decl_specifiers.attributes);
9450 /* Turn access control back on for names used during
9451 template instantiation. */
9452 pop_deferring_access_checks ();
9453 /* Do the explicit instantiation. */
9454 do_decl_instantiation (decl, extension_specifier);
9458 pop_deferring_access_checks ();
9459 /* Skip the body of the explicit instantiation. */
9460 cp_parser_skip_to_end_of_statement (parser);
9463 /* We're done with the instantiation. */
9464 end_explicit_instantiation ();
9466 cp_parser_consume_semicolon_at_end_of_statement (parser);
9469 /* Parse an explicit-specialization.
9471 explicit-specialization:
9472 template < > declaration
9474 Although the standard says `declaration', what it really means is:
9476 explicit-specialization:
9477 template <> decl-specifier [opt] init-declarator [opt] ;
9478 template <> function-definition
9479 template <> explicit-specialization
9480 template <> template-declaration */
9483 cp_parser_explicit_specialization (cp_parser* parser)
9486 /* Look for the `template' keyword. */
9487 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9488 /* Look for the `<'. */
9489 cp_parser_require (parser, CPP_LESS, "`<'");
9490 /* Look for the `>'. */
9491 cp_parser_require (parser, CPP_GREATER, "`>'");
9492 /* We have processed another parameter list. */
9493 ++parser->num_template_parameter_lists;
9496 A template ... explicit specialization ... shall not have C
9498 if (current_lang_name == lang_name_c)
9500 error ("template specialization with C linkage");
9501 /* Give it C++ linkage to avoid confusing other parts of the
9503 push_lang_context (lang_name_cplusplus);
9504 need_lang_pop = true;
9507 need_lang_pop = false;
9508 /* Let the front end know that we are beginning a specialization. */
9509 begin_specialization ();
9510 /* If the next keyword is `template', we need to figure out whether
9511 or not we're looking a template-declaration. */
9512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9514 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9515 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9516 cp_parser_template_declaration_after_export (parser,
9517 /*member_p=*/false);
9519 cp_parser_explicit_specialization (parser);
9522 /* Parse the dependent declaration. */
9523 cp_parser_single_declaration (parser,
9524 /*checks=*/NULL_TREE,
9527 /* We're done with the specialization. */
9528 end_specialization ();
9529 /* For the erroneous case of a template with C linkage, we pushed an
9530 implicit C++ linkage scope; exit that scope now. */
9532 pop_lang_context ();
9533 /* We're done with this parameter list. */
9534 --parser->num_template_parameter_lists;
9537 /* Parse a type-specifier.
9540 simple-type-specifier
9543 elaborated-type-specifier
9551 Returns a representation of the type-specifier. For a
9552 class-specifier, enum-specifier, or elaborated-type-specifier, a
9553 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9555 The parser flags FLAGS is used to control type-specifier parsing.
9557 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9558 in a decl-specifier-seq.
9560 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9561 class-specifier, enum-specifier, or elaborated-type-specifier, then
9562 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9563 if a type is declared; 2 if it is defined. Otherwise, it is set to
9566 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9567 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9571 cp_parser_type_specifier (cp_parser* parser,
9572 cp_parser_flags flags,
9573 cp_decl_specifier_seq *decl_specs,
9574 bool is_declaration,
9575 int* declares_class_or_enum,
9576 bool* is_cv_qualifier)
9578 tree type_spec = NULL_TREE;
9581 cp_decl_spec ds = ds_last;
9583 /* Assume this type-specifier does not declare a new type. */
9584 if (declares_class_or_enum)
9585 *declares_class_or_enum = 0;
9586 /* And that it does not specify a cv-qualifier. */
9587 if (is_cv_qualifier)
9588 *is_cv_qualifier = false;
9589 /* Peek at the next token. */
9590 token = cp_lexer_peek_token (parser->lexer);
9592 /* If we're looking at a keyword, we can use that to guide the
9593 production we choose. */
9594 keyword = token->keyword;
9598 /* Look for the enum-specifier. */
9599 type_spec = cp_parser_enum_specifier (parser);
9600 /* If that worked, we're done. */
9603 if (declares_class_or_enum)
9604 *declares_class_or_enum = 2;
9606 cp_parser_set_decl_spec_type (decl_specs,
9608 /*user_defined_p=*/true);
9612 goto elaborated_type_specifier;
9614 /* Any of these indicate either a class-specifier, or an
9615 elaborated-type-specifier. */
9619 /* Parse tentatively so that we can back up if we don't find a
9621 cp_parser_parse_tentatively (parser);
9622 /* Look for the class-specifier. */
9623 type_spec = cp_parser_class_specifier (parser);
9624 /* If that worked, we're done. */
9625 if (cp_parser_parse_definitely (parser))
9627 if (declares_class_or_enum)
9628 *declares_class_or_enum = 2;
9630 cp_parser_set_decl_spec_type (decl_specs,
9632 /*user_defined_p=*/true);
9637 elaborated_type_specifier:
9638 /* We're declaring (not defining) a class or enum. */
9639 if (declares_class_or_enum)
9640 *declares_class_or_enum = 1;
9644 /* Look for an elaborated-type-specifier. */
9646 = (cp_parser_elaborated_type_specifier
9648 decl_specs && decl_specs->specs[(int) ds_friend],
9651 cp_parser_set_decl_spec_type (decl_specs,
9653 /*user_defined_p=*/true);
9658 if (is_cv_qualifier)
9659 *is_cv_qualifier = true;
9664 if (is_cv_qualifier)
9665 *is_cv_qualifier = true;
9670 if (is_cv_qualifier)
9671 *is_cv_qualifier = true;
9675 /* The `__complex__' keyword is a GNU extension. */
9683 /* Handle simple keywords. */
9688 ++decl_specs->specs[(int)ds];
9689 decl_specs->any_specifiers_p = true;
9691 return cp_lexer_consume_token (parser->lexer)->value;
9694 /* If we do not already have a type-specifier, assume we are looking
9695 at a simple-type-specifier. */
9696 type_spec = cp_parser_simple_type_specifier (parser,
9700 /* If we didn't find a type-specifier, and a type-specifier was not
9701 optional in this context, issue an error message. */
9702 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9704 cp_parser_error (parser, "expected type specifier");
9705 return error_mark_node;
9711 /* Parse a simple-type-specifier.
9713 simple-type-specifier:
9714 :: [opt] nested-name-specifier [opt] type-name
9715 :: [opt] nested-name-specifier template template-id
9730 simple-type-specifier:
9731 __typeof__ unary-expression
9732 __typeof__ ( type-id )
9734 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9735 appropriately updated. */
9738 cp_parser_simple_type_specifier (cp_parser* parser,
9739 cp_decl_specifier_seq *decl_specs,
9740 cp_parser_flags flags)
9742 tree type = NULL_TREE;
9745 /* Peek at the next token. */
9746 token = cp_lexer_peek_token (parser->lexer);
9748 /* If we're looking at a keyword, things are easy. */
9749 switch (token->keyword)
9753 decl_specs->explicit_char_p = true;
9754 type = char_type_node;
9757 type = wchar_type_node;
9760 type = boolean_type_node;
9764 ++decl_specs->specs[(int) ds_short];
9765 type = short_integer_type_node;
9769 decl_specs->explicit_int_p = true;
9770 type = integer_type_node;
9774 ++decl_specs->specs[(int) ds_long];
9775 type = long_integer_type_node;
9779 ++decl_specs->specs[(int) ds_signed];
9780 type = integer_type_node;
9784 ++decl_specs->specs[(int) ds_unsigned];
9785 type = unsigned_type_node;
9788 type = float_type_node;
9791 type = double_type_node;
9794 type = void_type_node;
9798 /* Consume the `typeof' token. */
9799 cp_lexer_consume_token (parser->lexer);
9800 /* Parse the operand to `typeof'. */
9801 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9802 /* If it is not already a TYPE, take its type. */
9804 type = finish_typeof (type);
9807 cp_parser_set_decl_spec_type (decl_specs, type,
9808 /*user_defined_p=*/true);
9816 /* If the type-specifier was for a built-in type, we're done. */
9821 /* Record the type. */
9823 && (token->keyword != RID_SIGNED
9824 && token->keyword != RID_UNSIGNED
9825 && token->keyword != RID_SHORT
9826 && token->keyword != RID_LONG))
9827 cp_parser_set_decl_spec_type (decl_specs,
9829 /*user_defined=*/false);
9831 decl_specs->any_specifiers_p = true;
9833 /* Consume the token. */
9834 id = cp_lexer_consume_token (parser->lexer)->value;
9836 /* There is no valid C++ program where a non-template type is
9837 followed by a "<". That usually indicates that the user thought
9838 that the type was a template. */
9839 cp_parser_check_for_invalid_template_id (parser, type);
9841 return TYPE_NAME (type);
9844 /* The type-specifier must be a user-defined type. */
9845 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9850 /* Don't gobble tokens or issue error messages if this is an
9851 optional type-specifier. */
9852 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9853 cp_parser_parse_tentatively (parser);
9855 /* Look for the optional `::' operator. */
9857 = (cp_parser_global_scope_opt (parser,
9858 /*current_scope_valid_p=*/false,
9859 /*object_scope_valid_p=*/false)
9861 /* Look for the nested-name specifier. */
9863 = (cp_parser_nested_name_specifier_opt (parser,
9864 /*typename_keyword_p=*/false,
9865 /*check_dependency_p=*/true,
9867 /*is_declaration=*/false)
9869 /* If we have seen a nested-name-specifier, and the next token
9870 is `template', then we are using the template-id production. */
9872 && cp_parser_optional_template_keyword (parser))
9874 /* Look for the template-id. */
9875 type = cp_parser_template_id (parser,
9876 /*template_keyword_p=*/true,
9877 /*check_dependency_p=*/true,
9878 /*is_declaration=*/false);
9879 /* If the template-id did not name a type, we are out of
9881 if (TREE_CODE (type) != TYPE_DECL)
9883 cp_parser_error (parser, "expected template-id for type");
9887 /* Otherwise, look for a type-name. */
9889 type = cp_parser_type_name (parser);
9890 /* Keep track of all name-lookups performed in class scopes. */
9894 && TREE_CODE (type) == TYPE_DECL
9895 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9896 maybe_note_name_used_in_class (DECL_NAME (type), type);
9897 /* If it didn't work out, we don't have a TYPE. */
9898 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9899 && !cp_parser_parse_definitely (parser))
9901 if (type && decl_specs)
9902 cp_parser_set_decl_spec_type (decl_specs, type,
9903 /*user_defined=*/true);
9906 /* If we didn't get a type-name, issue an error message. */
9907 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9909 cp_parser_error (parser, "expected type-name");
9910 return error_mark_node;
9913 /* There is no valid C++ program where a non-template type is
9914 followed by a "<". That usually indicates that the user thought
9915 that the type was a template. */
9916 if (type && type != error_mark_node)
9918 /* As a last-ditch effort, see if TYPE is an Objective-C type.
9919 If it is, then the '<'...'>' enclose protocol names rather than
9920 template arguments, and so everything is fine. */
9921 if (c_dialect_objc ()
9922 && (objc_is_id (type) || objc_is_class_name (type)))
9924 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9925 tree qual_type = objc_get_protocol_qualified_type (type, protos);
9927 /* Clobber the "unqualified" type previously entered into
9928 DECL_SPECS with the new, improved protocol-qualified version. */
9930 decl_specs->type = qual_type;
9935 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9941 /* Parse a type-name.
9954 Returns a TYPE_DECL for the type. */
9957 cp_parser_type_name (cp_parser* parser)
9962 /* We can't know yet whether it is a class-name or not. */
9963 cp_parser_parse_tentatively (parser);
9964 /* Try a class-name. */
9965 type_decl = cp_parser_class_name (parser,
9966 /*typename_keyword_p=*/false,
9967 /*template_keyword_p=*/false,
9969 /*check_dependency_p=*/true,
9970 /*class_head_p=*/false,
9971 /*is_declaration=*/false);
9972 /* If it's not a class-name, keep looking. */
9973 if (!cp_parser_parse_definitely (parser))
9975 /* It must be a typedef-name or an enum-name. */
9976 identifier = cp_parser_identifier (parser);
9977 if (identifier == error_mark_node)
9978 return error_mark_node;
9980 /* Look up the type-name. */
9981 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9983 if (TREE_CODE (type_decl) != TYPE_DECL
9984 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9986 /* See if this is an Objective-C type. */
9987 tree protos = cp_parser_objc_protocol_refs_opt (parser);
9988 tree type = objc_get_protocol_qualified_type (identifier, protos);
9990 type_decl = TYPE_NAME (type);
9993 /* Issue an error if we did not find a type-name. */
9994 if (TREE_CODE (type_decl) != TYPE_DECL)
9996 if (!cp_parser_simulate_error (parser))
9997 cp_parser_name_lookup_error (parser, identifier, type_decl,
9999 type_decl = error_mark_node;
10001 /* Remember that the name was used in the definition of the
10002 current class so that we can check later to see if the
10003 meaning would have been different after the class was
10004 entirely defined. */
10005 else if (type_decl != error_mark_node
10007 maybe_note_name_used_in_class (identifier, type_decl);
10014 /* Parse an elaborated-type-specifier. Note that the grammar given
10015 here incorporates the resolution to DR68.
10017 elaborated-type-specifier:
10018 class-key :: [opt] nested-name-specifier [opt] identifier
10019 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10020 enum :: [opt] nested-name-specifier [opt] identifier
10021 typename :: [opt] nested-name-specifier identifier
10022 typename :: [opt] nested-name-specifier template [opt]
10027 elaborated-type-specifier:
10028 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10029 class-key attributes :: [opt] nested-name-specifier [opt]
10030 template [opt] template-id
10031 enum attributes :: [opt] nested-name-specifier [opt] identifier
10033 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10034 declared `friend'. If IS_DECLARATION is TRUE, then this
10035 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10036 something is being declared.
10038 Returns the TYPE specified. */
10041 cp_parser_elaborated_type_specifier (cp_parser* parser,
10043 bool is_declaration)
10045 enum tag_types tag_type;
10047 tree type = NULL_TREE;
10048 tree attributes = NULL_TREE;
10050 /* See if we're looking at the `enum' keyword. */
10051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10053 /* Consume the `enum' token. */
10054 cp_lexer_consume_token (parser->lexer);
10055 /* Remember that it's an enumeration type. */
10056 tag_type = enum_type;
10057 /* Parse the attributes. */
10058 attributes = cp_parser_attributes_opt (parser);
10060 /* Or, it might be `typename'. */
10061 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10064 /* Consume the `typename' token. */
10065 cp_lexer_consume_token (parser->lexer);
10066 /* Remember that it's a `typename' type. */
10067 tag_type = typename_type;
10068 /* The `typename' keyword is only allowed in templates. */
10069 if (!processing_template_decl)
10070 pedwarn ("using %<typename%> outside of template");
10072 /* Otherwise it must be a class-key. */
10075 tag_type = cp_parser_class_key (parser);
10076 if (tag_type == none_type)
10077 return error_mark_node;
10078 /* Parse the attributes. */
10079 attributes = cp_parser_attributes_opt (parser);
10082 /* Look for the `::' operator. */
10083 cp_parser_global_scope_opt (parser,
10084 /*current_scope_valid_p=*/false,
10085 /*object_scope_valid_p=*/false);
10086 /* Look for the nested-name-specifier. */
10087 if (tag_type == typename_type)
10089 if (!cp_parser_nested_name_specifier (parser,
10090 /*typename_keyword_p=*/true,
10091 /*check_dependency_p=*/true,
10094 return error_mark_node;
10097 /* Even though `typename' is not present, the proposed resolution
10098 to Core Issue 180 says that in `class A<T>::B', `B' should be
10099 considered a type-name, even if `A<T>' is dependent. */
10100 cp_parser_nested_name_specifier_opt (parser,
10101 /*typename_keyword_p=*/true,
10102 /*check_dependency_p=*/true,
10105 /* For everything but enumeration types, consider a template-id. */
10106 /* For an enumeration type, consider only a plain identifier. */
10107 if (tag_type != enum_type)
10109 bool template_p = false;
10112 /* Allow the `template' keyword. */
10113 template_p = cp_parser_optional_template_keyword (parser);
10114 /* If we didn't see `template', we don't know if there's a
10115 template-id or not. */
10117 cp_parser_parse_tentatively (parser);
10118 /* Parse the template-id. */
10119 decl = cp_parser_template_id (parser, template_p,
10120 /*check_dependency_p=*/true,
10122 /* If we didn't find a template-id, look for an ordinary
10124 if (!template_p && !cp_parser_parse_definitely (parser))
10126 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10127 in effect, then we must assume that, upon instantiation, the
10128 template will correspond to a class. */
10129 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10130 && tag_type == typename_type)
10131 type = make_typename_type (parser->scope, decl,
10133 /*complain=*/tf_error);
10135 type = TREE_TYPE (decl);
10140 identifier = cp_parser_identifier (parser);
10142 if (identifier == error_mark_node)
10144 parser->scope = NULL_TREE;
10145 return error_mark_node;
10148 /* For a `typename', we needn't call xref_tag. */
10149 if (tag_type == typename_type
10150 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10151 return cp_parser_make_typename_type (parser, parser->scope,
10153 /* Look up a qualified name in the usual way. */
10158 decl = cp_parser_lookup_name (parser, identifier,
10160 /*is_template=*/false,
10161 /*is_namespace=*/false,
10162 /*check_dependency=*/true,
10163 /*ambiguous_decls=*/NULL);
10165 /* If we are parsing friend declaration, DECL may be a
10166 TEMPLATE_DECL tree node here. However, we need to check
10167 whether this TEMPLATE_DECL results in valid code. Consider
10168 the following example:
10171 template <class T> class C {};
10174 template <class T> friend class N::C; // #1, valid code
10176 template <class T> class Y {
10177 friend class N::C; // #2, invalid code
10180 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10181 name lookup of `N::C'. We see that friend declaration must
10182 be template for the code to be valid. Note that
10183 processing_template_decl does not work here since it is
10184 always 1 for the above two cases. */
10186 decl = (cp_parser_maybe_treat_template_as_class
10187 (decl, /*tag_name_p=*/is_friend
10188 && parser->num_template_parameter_lists));
10190 if (TREE_CODE (decl) != TYPE_DECL)
10192 cp_parser_diagnose_invalid_type_name (parser,
10195 return error_mark_node;
10198 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10199 check_elaborated_type_specifier
10201 (parser->num_template_parameter_lists
10202 || DECL_SELF_REFERENCE_P (decl)));
10204 type = TREE_TYPE (decl);
10208 /* An elaborated-type-specifier sometimes introduces a new type and
10209 sometimes names an existing type. Normally, the rule is that it
10210 introduces a new type only if there is not an existing type of
10211 the same name already in scope. For example, given:
10214 void f() { struct S s; }
10216 the `struct S' in the body of `f' is the same `struct S' as in
10217 the global scope; the existing definition is used. However, if
10218 there were no global declaration, this would introduce a new
10219 local class named `S'.
10221 An exception to this rule applies to the following code:
10223 namespace N { struct S; }
10225 Here, the elaborated-type-specifier names a new type
10226 unconditionally; even if there is already an `S' in the
10227 containing scope this declaration names a new type.
10228 This exception only applies if the elaborated-type-specifier
10229 forms the complete declaration:
10233 A declaration consisting solely of `class-key identifier ;' is
10234 either a redeclaration of the name in the current scope or a
10235 forward declaration of the identifier as a class name. It
10236 introduces the name into the current scope.
10238 We are in this situation precisely when the next token is a `;'.
10240 An exception to the exception is that a `friend' declaration does
10241 *not* name a new type; i.e., given:
10243 struct S { friend struct T; };
10245 `T' is not a new type in the scope of `S'.
10247 Also, `new struct S' or `sizeof (struct S)' never results in the
10248 definition of a new type; a new type can only be declared in a
10249 declaration context. */
10255 /* Friends have special name lookup rules. */
10256 ts = ts_within_enclosing_non_class;
10257 else if (is_declaration
10258 && cp_lexer_next_token_is (parser->lexer,
10260 /* This is a `class-key identifier ;' */
10266 (parser->num_template_parameter_lists
10267 && (cp_parser_next_token_starts_class_definition_p (parser)
10268 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10269 /* An unqualified name was used to reference this type, so
10270 there were no qualifying templates. */
10271 if (!cp_parser_check_template_parameters (parser,
10272 /*num_templates=*/0))
10273 return error_mark_node;
10274 type = xref_tag (tag_type, identifier, ts, template_p);
10278 if (type == error_mark_node)
10279 return error_mark_node;
10281 /* Allow attributes on forward declarations of classes. */
10284 if (TREE_CODE (type) == TYPENAME_TYPE)
10285 warning (OPT_Wattributes,
10286 "attributes ignored on uninstantiated type");
10287 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10288 && ! processing_explicit_instantiation)
10289 warning (OPT_Wattributes,
10290 "attributes ignored on template instantiation");
10291 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10292 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10294 warning (OPT_Wattributes,
10295 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10298 if (tag_type != enum_type)
10299 cp_parser_check_class_key (tag_type, type);
10301 /* A "<" cannot follow an elaborated type specifier. If that
10302 happens, the user was probably trying to form a template-id. */
10303 cp_parser_check_for_invalid_template_id (parser, type);
10308 /* Parse an enum-specifier.
10311 enum identifier [opt] { enumerator-list [opt] }
10314 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10317 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10318 if the token stream isn't an enum-specifier after all. */
10321 cp_parser_enum_specifier (cp_parser* parser)
10327 /* Parse tentatively so that we can back up if we don't find a
10329 cp_parser_parse_tentatively (parser);
10331 /* Caller guarantees that the current token is 'enum', an identifier
10332 possibly follows, and the token after that is an opening brace.
10333 If we don't have an identifier, fabricate an anonymous name for
10334 the enumeration being defined. */
10335 cp_lexer_consume_token (parser->lexer);
10337 attributes = cp_parser_attributes_opt (parser);
10339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10340 identifier = cp_parser_identifier (parser);
10342 identifier = make_anon_name ();
10344 /* Look for the `{' but don't consume it yet. */
10345 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10346 cp_parser_simulate_error (parser);
10348 if (!cp_parser_parse_definitely (parser))
10351 /* Issue an error message if type-definitions are forbidden here. */
10352 cp_parser_check_type_definition (parser);
10354 /* Create the new type. We do this before consuming the opening brace
10355 so the enum will be recorded as being on the line of its tag (or the
10356 'enum' keyword, if there is no tag). */
10357 type = start_enum (identifier);
10359 /* Consume the opening brace. */
10360 cp_lexer_consume_token (parser->lexer);
10362 if (type == error_mark_node)
10364 cp_parser_skip_to_end_of_block_or_statement (parser);
10365 return error_mark_node;
10368 /* If the next token is not '}', then there are some enumerators. */
10369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10370 cp_parser_enumerator_list (parser, type);
10372 /* Consume the final '}'. */
10373 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10375 /* Look for trailing attributes to apply to this enumeration, and
10376 apply them if appropriate. */
10377 if (cp_parser_allow_gnu_extensions_p (parser))
10379 tree trailing_attr = cp_parser_attributes_opt (parser);
10380 cplus_decl_attributes (&type,
10382 (int) ATTR_FLAG_TYPE_IN_PLACE);
10385 /* Finish up the enumeration. */
10386 finish_enum (type);
10391 /* Parse an enumerator-list. The enumerators all have the indicated
10395 enumerator-definition
10396 enumerator-list , enumerator-definition */
10399 cp_parser_enumerator_list (cp_parser* parser, tree type)
10403 /* Parse an enumerator-definition. */
10404 cp_parser_enumerator_definition (parser, type);
10406 /* If the next token is not a ',', we've reached the end of
10408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10410 /* Otherwise, consume the `,' and keep going. */
10411 cp_lexer_consume_token (parser->lexer);
10412 /* If the next token is a `}', there is a trailing comma. */
10413 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10415 if (pedantic && !in_system_header)
10416 pedwarn ("comma at end of enumerator list");
10422 /* Parse an enumerator-definition. The enumerator has the indicated
10425 enumerator-definition:
10427 enumerator = constant-expression
10433 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10438 /* Look for the identifier. */
10439 identifier = cp_parser_identifier (parser);
10440 if (identifier == error_mark_node)
10443 /* If the next token is an '=', then there is an explicit value. */
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10446 /* Consume the `=' token. */
10447 cp_lexer_consume_token (parser->lexer);
10448 /* Parse the value. */
10449 value = cp_parser_constant_expression (parser,
10450 /*allow_non_constant_p=*/false,
10456 /* Create the enumerator. */
10457 build_enumerator (identifier, value, type);
10460 /* Parse a namespace-name.
10463 original-namespace-name
10466 Returns the NAMESPACE_DECL for the namespace. */
10469 cp_parser_namespace_name (cp_parser* parser)
10472 tree namespace_decl;
10474 /* Get the name of the namespace. */
10475 identifier = cp_parser_identifier (parser);
10476 if (identifier == error_mark_node)
10477 return error_mark_node;
10479 /* Look up the identifier in the currently active scope. Look only
10480 for namespaces, due to:
10482 [basic.lookup.udir]
10484 When looking up a namespace-name in a using-directive or alias
10485 definition, only namespace names are considered.
10489 [basic.lookup.qual]
10491 During the lookup of a name preceding the :: scope resolution
10492 operator, object, function, and enumerator names are ignored.
10494 (Note that cp_parser_class_or_namespace_name only calls this
10495 function if the token after the name is the scope resolution
10497 namespace_decl = cp_parser_lookup_name (parser, identifier,
10499 /*is_template=*/false,
10500 /*is_namespace=*/true,
10501 /*check_dependency=*/true,
10502 /*ambiguous_decls=*/NULL);
10503 /* If it's not a namespace, issue an error. */
10504 if (namespace_decl == error_mark_node
10505 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10507 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10508 error ("%qD is not a namespace-name", identifier);
10509 cp_parser_error (parser, "expected namespace-name");
10510 namespace_decl = error_mark_node;
10513 return namespace_decl;
10516 /* Parse a namespace-definition.
10518 namespace-definition:
10519 named-namespace-definition
10520 unnamed-namespace-definition
10522 named-namespace-definition:
10523 original-namespace-definition
10524 extension-namespace-definition
10526 original-namespace-definition:
10527 namespace identifier { namespace-body }
10529 extension-namespace-definition:
10530 namespace original-namespace-name { namespace-body }
10532 unnamed-namespace-definition:
10533 namespace { namespace-body } */
10536 cp_parser_namespace_definition (cp_parser* parser)
10538 tree identifier, attribs;
10540 /* Look for the `namespace' keyword. */
10541 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10543 /* Get the name of the namespace. We do not attempt to distinguish
10544 between an original-namespace-definition and an
10545 extension-namespace-definition at this point. The semantic
10546 analysis routines are responsible for that. */
10547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10548 identifier = cp_parser_identifier (parser);
10550 identifier = NULL_TREE;
10552 /* Parse any specified attributes. */
10553 attribs = cp_parser_attributes_opt (parser);
10555 /* Look for the `{' to start the namespace. */
10556 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10557 /* Start the namespace. */
10558 push_namespace_with_attribs (identifier, attribs);
10559 /* Parse the body of the namespace. */
10560 cp_parser_namespace_body (parser);
10561 /* Finish the namespace. */
10563 /* Look for the final `}'. */
10564 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10567 /* Parse a namespace-body.
10570 declaration-seq [opt] */
10573 cp_parser_namespace_body (cp_parser* parser)
10575 cp_parser_declaration_seq_opt (parser);
10578 /* Parse a namespace-alias-definition.
10580 namespace-alias-definition:
10581 namespace identifier = qualified-namespace-specifier ; */
10584 cp_parser_namespace_alias_definition (cp_parser* parser)
10587 tree namespace_specifier;
10589 /* Look for the `namespace' keyword. */
10590 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10591 /* Look for the identifier. */
10592 identifier = cp_parser_identifier (parser);
10593 if (identifier == error_mark_node)
10595 /* Look for the `=' token. */
10596 cp_parser_require (parser, CPP_EQ, "`='");
10597 /* Look for the qualified-namespace-specifier. */
10598 namespace_specifier
10599 = cp_parser_qualified_namespace_specifier (parser);
10600 /* Look for the `;' token. */
10601 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10603 /* Register the alias in the symbol table. */
10604 do_namespace_alias (identifier, namespace_specifier);
10607 /* Parse a qualified-namespace-specifier.
10609 qualified-namespace-specifier:
10610 :: [opt] nested-name-specifier [opt] namespace-name
10612 Returns a NAMESPACE_DECL corresponding to the specified
10616 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10618 /* Look for the optional `::'. */
10619 cp_parser_global_scope_opt (parser,
10620 /*current_scope_valid_p=*/false,
10621 /*object_scope_valid_p=*/false);
10623 /* Look for the optional nested-name-specifier. */
10624 cp_parser_nested_name_specifier_opt (parser,
10625 /*typename_keyword_p=*/false,
10626 /*check_dependency_p=*/true,
10628 /*is_declaration=*/true);
10630 return cp_parser_namespace_name (parser);
10633 /* Parse a using-declaration.
10636 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10637 using :: unqualified-id ; */
10640 cp_parser_using_declaration (cp_parser* parser)
10643 bool typename_p = false;
10644 bool global_scope_p;
10649 /* Look for the `using' keyword. */
10650 cp_parser_require_keyword (parser, RID_USING, "`using'");
10652 /* Peek at the next token. */
10653 token = cp_lexer_peek_token (parser->lexer);
10654 /* See if it's `typename'. */
10655 if (token->keyword == RID_TYPENAME)
10657 /* Remember that we've seen it. */
10659 /* Consume the `typename' token. */
10660 cp_lexer_consume_token (parser->lexer);
10663 /* Look for the optional global scope qualification. */
10665 = (cp_parser_global_scope_opt (parser,
10666 /*current_scope_valid_p=*/false,
10667 /*object_scope_valid_p=*/false)
10670 /* If we saw `typename', or didn't see `::', then there must be a
10671 nested-name-specifier present. */
10672 if (typename_p || !global_scope_p)
10673 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10674 /*check_dependency_p=*/true,
10676 /*is_declaration=*/true);
10677 /* Otherwise, we could be in either of the two productions. In that
10678 case, treat the nested-name-specifier as optional. */
10680 qscope = cp_parser_nested_name_specifier_opt (parser,
10681 /*typename_keyword_p=*/false,
10682 /*check_dependency_p=*/true,
10684 /*is_declaration=*/true);
10686 qscope = global_namespace;
10688 /* Parse the unqualified-id. */
10689 identifier = cp_parser_unqualified_id (parser,
10690 /*template_keyword_p=*/false,
10691 /*check_dependency_p=*/true,
10692 /*declarator_p=*/true,
10693 /*optional_p=*/false,
10694 /*member_p=*/false);
10696 /* The function we call to handle a using-declaration is different
10697 depending on what scope we are in. */
10698 if (qscope == error_mark_node || identifier == error_mark_node)
10700 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10701 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10702 /* [namespace.udecl]
10704 A using declaration shall not name a template-id. */
10705 error ("a template-id may not appear in a using-declaration");
10708 if (at_class_scope_p ())
10710 /* Create the USING_DECL. */
10711 decl = do_class_using_decl (parser->scope, identifier);
10712 /* Add it to the list of members in this class. */
10713 finish_member_declaration (decl);
10717 decl = cp_parser_lookup_name_simple (parser, identifier);
10718 if (decl == error_mark_node)
10719 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10720 else if (!at_namespace_scope_p ())
10721 do_local_using_decl (decl, qscope, identifier);
10723 do_toplevel_using_decl (decl, qscope, identifier);
10727 /* Look for the final `;'. */
10728 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10731 /* Parse a using-directive.
10734 using namespace :: [opt] nested-name-specifier [opt]
10735 namespace-name ; */
10738 cp_parser_using_directive (cp_parser* parser)
10740 tree namespace_decl;
10743 /* Look for the `using' keyword. */
10744 cp_parser_require_keyword (parser, RID_USING, "`using'");
10745 /* And the `namespace' keyword. */
10746 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10747 /* Look for the optional `::' operator. */
10748 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
10749 /*object_scope_valid_p=*/false);
10750 /* And the optional nested-name-specifier. */
10751 cp_parser_nested_name_specifier_opt (parser,
10752 /*typename_keyword_p=*/false,
10753 /*check_dependency_p=*/true,
10755 /*is_declaration=*/true);
10756 /* Get the namespace being used. */
10757 namespace_decl = cp_parser_namespace_name (parser);
10758 /* And any specified attributes. */
10759 attribs = cp_parser_attributes_opt (parser);
10760 /* Update the symbol table. */
10761 parse_using_directive (namespace_decl, attribs);
10762 /* Look for the final `;'. */
10763 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10766 /* Parse an asm-definition.
10769 asm ( string-literal ) ;
10774 asm volatile [opt] ( string-literal ) ;
10775 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10776 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10777 : asm-operand-list [opt] ) ;
10778 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10779 : asm-operand-list [opt]
10780 : asm-operand-list [opt] ) ; */
10783 cp_parser_asm_definition (cp_parser* parser)
10786 tree outputs = NULL_TREE;
10787 tree inputs = NULL_TREE;
10788 tree clobbers = NULL_TREE;
10790 bool volatile_p = false;
10791 bool extended_p = false;
10793 /* Look for the `asm' keyword. */
10794 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10795 /* See if the next token is `volatile'. */
10796 if (cp_parser_allow_gnu_extensions_p (parser)
10797 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10799 /* Remember that we saw the `volatile' keyword. */
10801 /* Consume the token. */
10802 cp_lexer_consume_token (parser->lexer);
10804 /* Look for the opening `('. */
10805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10807 /* Look for the string. */
10808 string = cp_parser_string_literal (parser, false, false);
10809 if (string == error_mark_node)
10811 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10812 /*consume_paren=*/true);
10816 /* If we're allowing GNU extensions, check for the extended assembly
10817 syntax. Unfortunately, the `:' tokens need not be separated by
10818 a space in C, and so, for compatibility, we tolerate that here
10819 too. Doing that means that we have to treat the `::' operator as
10821 if (cp_parser_allow_gnu_extensions_p (parser)
10822 && at_function_scope_p ()
10823 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10824 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10826 bool inputs_p = false;
10827 bool clobbers_p = false;
10829 /* The extended syntax was used. */
10832 /* Look for outputs. */
10833 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10835 /* Consume the `:'. */
10836 cp_lexer_consume_token (parser->lexer);
10837 /* Parse the output-operands. */
10838 if (cp_lexer_next_token_is_not (parser->lexer,
10840 && cp_lexer_next_token_is_not (parser->lexer,
10842 && cp_lexer_next_token_is_not (parser->lexer,
10844 outputs = cp_parser_asm_operand_list (parser);
10846 /* If the next token is `::', there are no outputs, and the
10847 next token is the beginning of the inputs. */
10848 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10849 /* The inputs are coming next. */
10852 /* Look for inputs. */
10854 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10856 /* Consume the `:' or `::'. */
10857 cp_lexer_consume_token (parser->lexer);
10858 /* Parse the output-operands. */
10859 if (cp_lexer_next_token_is_not (parser->lexer,
10861 && cp_lexer_next_token_is_not (parser->lexer,
10863 inputs = cp_parser_asm_operand_list (parser);
10865 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10866 /* The clobbers are coming next. */
10869 /* Look for clobbers. */
10871 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10873 /* Consume the `:' or `::'. */
10874 cp_lexer_consume_token (parser->lexer);
10875 /* Parse the clobbers. */
10876 if (cp_lexer_next_token_is_not (parser->lexer,
10878 clobbers = cp_parser_asm_clobber_list (parser);
10881 /* Look for the closing `)'. */
10882 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10883 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10884 /*consume_paren=*/true);
10885 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10887 /* Create the ASM_EXPR. */
10888 if (at_function_scope_p ())
10890 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10892 /* If the extended syntax was not used, mark the ASM_EXPR. */
10895 tree temp = asm_stmt;
10896 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10897 temp = TREE_OPERAND (temp, 0);
10899 ASM_INPUT_P (temp) = 1;
10903 cgraph_add_asm_node (string);
10906 /* Declarators [gram.dcl.decl] */
10908 /* Parse an init-declarator.
10911 declarator initializer [opt]
10916 declarator asm-specification [opt] attributes [opt] initializer [opt]
10918 function-definition:
10919 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10921 decl-specifier-seq [opt] declarator function-try-block
10925 function-definition:
10926 __extension__ function-definition
10928 The DECL_SPECIFIERS apply to this declarator. Returns a
10929 representation of the entity declared. If MEMBER_P is TRUE, then
10930 this declarator appears in a class scope. The new DECL created by
10931 this declarator is returned.
10933 The CHECKS are access checks that should be performed once we know
10934 what entity is being declared (and, therefore, what classes have
10937 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10938 for a function-definition here as well. If the declarator is a
10939 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10940 be TRUE upon return. By that point, the function-definition will
10941 have been completely parsed.
10943 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10947 cp_parser_init_declarator (cp_parser* parser,
10948 cp_decl_specifier_seq *decl_specifiers,
10950 bool function_definition_allowed_p,
10952 int declares_class_or_enum,
10953 bool* function_definition_p)
10956 cp_declarator *declarator;
10957 tree prefix_attributes;
10959 tree asm_specification;
10961 tree decl = NULL_TREE;
10963 bool is_initialized;
10964 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
10965 initialized with "= ..", CPP_OPEN_PAREN if initialized with
10967 enum cpp_ttype initialization_kind;
10968 bool is_parenthesized_init = false;
10969 bool is_non_constant_init;
10970 int ctor_dtor_or_conv_p;
10972 tree pushed_scope = NULL;
10974 /* Gather the attributes that were provided with the
10975 decl-specifiers. */
10976 prefix_attributes = decl_specifiers->attributes;
10978 /* Assume that this is not the declarator for a function
10980 if (function_definition_p)
10981 *function_definition_p = false;
10983 /* Defer access checks while parsing the declarator; we cannot know
10984 what names are accessible until we know what is being
10986 resume_deferring_access_checks ();
10988 /* Parse the declarator. */
10990 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10991 &ctor_dtor_or_conv_p,
10992 /*parenthesized_p=*/NULL,
10993 /*member_p=*/false);
10994 /* Gather up the deferred checks. */
10995 stop_deferring_access_checks ();
10997 /* If the DECLARATOR was erroneous, there's no need to go
10999 if (declarator == cp_error_declarator)
11000 return error_mark_node;
11002 if (declares_class_or_enum & 2)
11003 cp_parser_check_for_definition_in_return_type (declarator,
11004 decl_specifiers->type);
11006 /* Figure out what scope the entity declared by the DECLARATOR is
11007 located in. `grokdeclarator' sometimes changes the scope, so
11008 we compute it now. */
11009 scope = get_scope_of_declarator (declarator);
11011 /* If we're allowing GNU extensions, look for an asm-specification
11013 if (cp_parser_allow_gnu_extensions_p (parser))
11015 /* Look for an asm-specification. */
11016 asm_specification = cp_parser_asm_specification_opt (parser);
11017 /* And attributes. */
11018 attributes = cp_parser_attributes_opt (parser);
11022 asm_specification = NULL_TREE;
11023 attributes = NULL_TREE;
11026 /* Peek at the next token. */
11027 token = cp_lexer_peek_token (parser->lexer);
11028 /* Check to see if the token indicates the start of a
11029 function-definition. */
11030 if (cp_parser_token_starts_function_definition_p (token))
11032 if (!function_definition_allowed_p)
11034 /* If a function-definition should not appear here, issue an
11036 cp_parser_error (parser,
11037 "a function-definition is not allowed here");
11038 return error_mark_node;
11042 /* Neither attributes nor an asm-specification are allowed
11043 on a function-definition. */
11044 if (asm_specification)
11045 error ("an asm-specification is not allowed on a function-definition");
11047 error ("attributes are not allowed on a function-definition");
11048 /* This is a function-definition. */
11049 *function_definition_p = true;
11051 /* Parse the function definition. */
11053 decl = cp_parser_save_member_function_body (parser,
11056 prefix_attributes);
11059 = (cp_parser_function_definition_from_specifiers_and_declarator
11060 (parser, decl_specifiers, prefix_attributes, declarator));
11068 Only in function declarations for constructors, destructors, and
11069 type conversions can the decl-specifier-seq be omitted.
11071 We explicitly postpone this check past the point where we handle
11072 function-definitions because we tolerate function-definitions
11073 that are missing their return types in some modes. */
11074 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11076 cp_parser_error (parser,
11077 "expected constructor, destructor, or type conversion");
11078 return error_mark_node;
11081 /* An `=' or an `(' indicates an initializer. */
11082 if (token->type == CPP_EQ
11083 || token->type == CPP_OPEN_PAREN)
11085 is_initialized = true;
11086 initialization_kind = token->type;
11090 /* If the init-declarator isn't initialized and isn't followed by a
11091 `,' or `;', it's not a valid init-declarator. */
11092 if (token->type != CPP_COMMA
11093 && token->type != CPP_SEMICOLON)
11095 cp_parser_error (parser, "expected initializer");
11096 return error_mark_node;
11098 is_initialized = false;
11099 initialization_kind = CPP_EOF;
11102 /* Because start_decl has side-effects, we should only call it if we
11103 know we're going ahead. By this point, we know that we cannot
11104 possibly be looking at any other construct. */
11105 cp_parser_commit_to_tentative_parse (parser);
11107 /* If the decl specifiers were bad, issue an error now that we're
11108 sure this was intended to be a declarator. Then continue
11109 declaring the variable(s), as int, to try to cut down on further
11111 if (decl_specifiers->any_specifiers_p
11112 && decl_specifiers->type == error_mark_node)
11114 cp_parser_error (parser, "invalid type in declaration");
11115 decl_specifiers->type = integer_type_node;
11118 /* Check to see whether or not this declaration is a friend. */
11119 friend_p = cp_parser_friend_p (decl_specifiers);
11121 /* Check that the number of template-parameter-lists is OK. */
11122 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11123 return error_mark_node;
11125 /* Enter the newly declared entry in the symbol table. If we're
11126 processing a declaration in a class-specifier, we wait until
11127 after processing the initializer. */
11130 if (parser->in_unbraced_linkage_specification_p)
11131 decl_specifiers->storage_class = sc_extern;
11132 decl = start_decl (declarator, decl_specifiers,
11133 is_initialized, attributes, prefix_attributes,
11137 /* Enter the SCOPE. That way unqualified names appearing in the
11138 initializer will be looked up in SCOPE. */
11139 pushed_scope = push_scope (scope);
11141 /* Perform deferred access control checks, now that we know in which
11142 SCOPE the declared entity resides. */
11143 if (!member_p && decl)
11145 tree saved_current_function_decl = NULL_TREE;
11147 /* If the entity being declared is a function, pretend that we
11148 are in its scope. If it is a `friend', it may have access to
11149 things that would not otherwise be accessible. */
11150 if (TREE_CODE (decl) == FUNCTION_DECL)
11152 saved_current_function_decl = current_function_decl;
11153 current_function_decl = decl;
11156 /* Perform access checks for template parameters. */
11157 cp_parser_perform_template_parameter_access_checks (checks);
11159 /* Perform the access control checks for the declarator and the
11160 the decl-specifiers. */
11161 perform_deferred_access_checks ();
11163 /* Restore the saved value. */
11164 if (TREE_CODE (decl) == FUNCTION_DECL)
11165 current_function_decl = saved_current_function_decl;
11168 /* Parse the initializer. */
11169 initializer = NULL_TREE;
11170 is_parenthesized_init = false;
11171 is_non_constant_init = true;
11172 if (is_initialized)
11174 if (declarator->kind == cdk_function
11175 && declarator->declarator->kind == cdk_id
11176 && initialization_kind == CPP_EQ)
11177 initializer = cp_parser_pure_specifier (parser);
11179 initializer = cp_parser_initializer (parser,
11180 &is_parenthesized_init,
11181 &is_non_constant_init);
11184 /* The old parser allows attributes to appear after a parenthesized
11185 initializer. Mark Mitchell proposed removing this functionality
11186 on the GCC mailing lists on 2002-08-13. This parser accepts the
11187 attributes -- but ignores them. */
11188 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11189 if (cp_parser_attributes_opt (parser))
11190 warning (OPT_Wattributes,
11191 "attributes after parenthesized initializer ignored");
11193 /* For an in-class declaration, use `grokfield' to create the
11199 pop_scope (pushed_scope);
11200 pushed_scope = false;
11202 decl = grokfield (declarator, decl_specifiers,
11203 initializer, !is_non_constant_init,
11204 /*asmspec=*/NULL_TREE,
11205 prefix_attributes);
11206 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11207 cp_parser_save_default_args (parser, decl);
11210 /* Finish processing the declaration. But, skip friend
11212 if (!friend_p && decl && decl != error_mark_node)
11214 cp_finish_decl (decl,
11215 initializer, !is_non_constant_init,
11217 /* If the initializer is in parentheses, then this is
11218 a direct-initialization, which means that an
11219 `explicit' constructor is OK. Otherwise, an
11220 `explicit' constructor cannot be used. */
11221 ((is_parenthesized_init || !is_initialized)
11222 ? 0 : LOOKUP_ONLYCONVERTING));
11224 if (!friend_p && pushed_scope)
11225 pop_scope (pushed_scope);
11230 /* Parse a declarator.
11234 ptr-operator declarator
11236 abstract-declarator:
11237 ptr-operator abstract-declarator [opt]
11238 direct-abstract-declarator
11243 attributes [opt] direct-declarator
11244 attributes [opt] ptr-operator declarator
11246 abstract-declarator:
11247 attributes [opt] ptr-operator abstract-declarator [opt]
11248 attributes [opt] direct-abstract-declarator
11250 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11251 detect constructor, destructor or conversion operators. It is set
11252 to -1 if the declarator is a name, and +1 if it is a
11253 function. Otherwise it is set to zero. Usually you just want to
11254 test for >0, but internally the negative value is used.
11256 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11257 a decl-specifier-seq unless it declares a constructor, destructor,
11258 or conversion. It might seem that we could check this condition in
11259 semantic analysis, rather than parsing, but that makes it difficult
11260 to handle something like `f()'. We want to notice that there are
11261 no decl-specifiers, and therefore realize that this is an
11262 expression, not a declaration.)
11264 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11265 the declarator is a direct-declarator of the form "(...)".
11267 MEMBER_P is true iff this declarator is a member-declarator. */
11269 static cp_declarator *
11270 cp_parser_declarator (cp_parser* parser,
11271 cp_parser_declarator_kind dcl_kind,
11272 int* ctor_dtor_or_conv_p,
11273 bool* parenthesized_p,
11277 cp_declarator *declarator;
11278 enum tree_code code;
11279 cp_cv_quals cv_quals;
11281 tree attributes = NULL_TREE;
11283 /* Assume this is not a constructor, destructor, or type-conversion
11285 if (ctor_dtor_or_conv_p)
11286 *ctor_dtor_or_conv_p = 0;
11288 if (cp_parser_allow_gnu_extensions_p (parser))
11289 attributes = cp_parser_attributes_opt (parser);
11291 /* Peek at the next token. */
11292 token = cp_lexer_peek_token (parser->lexer);
11294 /* Check for the ptr-operator production. */
11295 cp_parser_parse_tentatively (parser);
11296 /* Parse the ptr-operator. */
11297 code = cp_parser_ptr_operator (parser,
11300 /* If that worked, then we have a ptr-operator. */
11301 if (cp_parser_parse_definitely (parser))
11303 /* If a ptr-operator was found, then this declarator was not
11305 if (parenthesized_p)
11306 *parenthesized_p = true;
11307 /* The dependent declarator is optional if we are parsing an
11308 abstract-declarator. */
11309 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11310 cp_parser_parse_tentatively (parser);
11312 /* Parse the dependent declarator. */
11313 declarator = cp_parser_declarator (parser, dcl_kind,
11314 /*ctor_dtor_or_conv_p=*/NULL,
11315 /*parenthesized_p=*/NULL,
11316 /*member_p=*/false);
11318 /* If we are parsing an abstract-declarator, we must handle the
11319 case where the dependent declarator is absent. */
11320 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11321 && !cp_parser_parse_definitely (parser))
11324 /* Build the representation of the ptr-operator. */
11326 declarator = make_ptrmem_declarator (cv_quals,
11329 else if (code == INDIRECT_REF)
11330 declarator = make_pointer_declarator (cv_quals, declarator);
11332 declarator = make_reference_declarator (cv_quals, declarator);
11334 /* Everything else is a direct-declarator. */
11337 if (parenthesized_p)
11338 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11340 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11341 ctor_dtor_or_conv_p,
11345 if (attributes && declarator && declarator != cp_error_declarator)
11346 declarator->attributes = attributes;
11351 /* Parse a direct-declarator or direct-abstract-declarator.
11355 direct-declarator ( parameter-declaration-clause )
11356 cv-qualifier-seq [opt]
11357 exception-specification [opt]
11358 direct-declarator [ constant-expression [opt] ]
11361 direct-abstract-declarator:
11362 direct-abstract-declarator [opt]
11363 ( parameter-declaration-clause )
11364 cv-qualifier-seq [opt]
11365 exception-specification [opt]
11366 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11367 ( abstract-declarator )
11369 Returns a representation of the declarator. DCL_KIND is
11370 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11371 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11372 we are parsing a direct-declarator. It is
11373 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11374 of ambiguity we prefer an abstract declarator, as per
11375 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11376 cp_parser_declarator. */
11378 static cp_declarator *
11379 cp_parser_direct_declarator (cp_parser* parser,
11380 cp_parser_declarator_kind dcl_kind,
11381 int* ctor_dtor_or_conv_p,
11385 cp_declarator *declarator = NULL;
11386 tree scope = NULL_TREE;
11387 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11388 bool saved_in_declarator_p = parser->in_declarator_p;
11390 tree pushed_scope = NULL_TREE;
11394 /* Peek at the next token. */
11395 token = cp_lexer_peek_token (parser->lexer);
11396 if (token->type == CPP_OPEN_PAREN)
11398 /* This is either a parameter-declaration-clause, or a
11399 parenthesized declarator. When we know we are parsing a
11400 named declarator, it must be a parenthesized declarator
11401 if FIRST is true. For instance, `(int)' is a
11402 parameter-declaration-clause, with an omitted
11403 direct-abstract-declarator. But `((*))', is a
11404 parenthesized abstract declarator. Finally, when T is a
11405 template parameter `(T)' is a
11406 parameter-declaration-clause, and not a parenthesized
11409 We first try and parse a parameter-declaration-clause,
11410 and then try a nested declarator (if FIRST is true).
11412 It is not an error for it not to be a
11413 parameter-declaration-clause, even when FIRST is
11419 The first is the declaration of a function while the
11420 second is a the definition of a variable, including its
11423 Having seen only the parenthesis, we cannot know which of
11424 these two alternatives should be selected. Even more
11425 complex are examples like:
11430 The former is a function-declaration; the latter is a
11431 variable initialization.
11433 Thus again, we try a parameter-declaration-clause, and if
11434 that fails, we back out and return. */
11436 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11438 cp_parameter_declarator *params;
11439 unsigned saved_num_template_parameter_lists;
11441 /* In a member-declarator, the only valid interpretation
11442 of a parenthesis is the start of a
11443 parameter-declaration-clause. (It is invalid to
11444 initialize a static data member with a parenthesized
11445 initializer; only the "=" form of initialization is
11448 cp_parser_parse_tentatively (parser);
11450 /* Consume the `('. */
11451 cp_lexer_consume_token (parser->lexer);
11454 /* If this is going to be an abstract declarator, we're
11455 in a declarator and we can't have default args. */
11456 parser->default_arg_ok_p = false;
11457 parser->in_declarator_p = true;
11460 /* Inside the function parameter list, surrounding
11461 template-parameter-lists do not apply. */
11462 saved_num_template_parameter_lists
11463 = parser->num_template_parameter_lists;
11464 parser->num_template_parameter_lists = 0;
11466 /* Parse the parameter-declaration-clause. */
11467 params = cp_parser_parameter_declaration_clause (parser);
11469 parser->num_template_parameter_lists
11470 = saved_num_template_parameter_lists;
11472 /* If all went well, parse the cv-qualifier-seq and the
11473 exception-specification. */
11474 if (member_p || cp_parser_parse_definitely (parser))
11476 cp_cv_quals cv_quals;
11477 tree exception_specification;
11479 if (ctor_dtor_or_conv_p)
11480 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11482 /* Consume the `)'. */
11483 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11485 /* Parse the cv-qualifier-seq. */
11486 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11487 /* And the exception-specification. */
11488 exception_specification
11489 = cp_parser_exception_specification_opt (parser);
11491 /* Create the function-declarator. */
11492 declarator = make_call_declarator (declarator,
11495 exception_specification);
11496 /* Any subsequent parameter lists are to do with
11497 return type, so are not those of the declared
11499 parser->default_arg_ok_p = false;
11501 /* Repeat the main loop. */
11506 /* If this is the first, we can try a parenthesized
11510 bool saved_in_type_id_in_expr_p;
11512 parser->default_arg_ok_p = saved_default_arg_ok_p;
11513 parser->in_declarator_p = saved_in_declarator_p;
11515 /* Consume the `('. */
11516 cp_lexer_consume_token (parser->lexer);
11517 /* Parse the nested declarator. */
11518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11519 parser->in_type_id_in_expr_p = true;
11521 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11522 /*parenthesized_p=*/NULL,
11524 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11526 /* Expect a `)'. */
11527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11528 declarator = cp_error_declarator;
11529 if (declarator == cp_error_declarator)
11532 goto handle_declarator;
11534 /* Otherwise, we must be done. */
11538 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11539 && token->type == CPP_OPEN_SQUARE)
11541 /* Parse an array-declarator. */
11544 if (ctor_dtor_or_conv_p)
11545 *ctor_dtor_or_conv_p = 0;
11548 parser->default_arg_ok_p = false;
11549 parser->in_declarator_p = true;
11550 /* Consume the `['. */
11551 cp_lexer_consume_token (parser->lexer);
11552 /* Peek at the next token. */
11553 token = cp_lexer_peek_token (parser->lexer);
11554 /* If the next token is `]', then there is no
11555 constant-expression. */
11556 if (token->type != CPP_CLOSE_SQUARE)
11558 bool non_constant_p;
11561 = cp_parser_constant_expression (parser,
11562 /*allow_non_constant=*/true,
11564 if (!non_constant_p)
11565 bounds = fold_non_dependent_expr (bounds);
11566 /* Normally, the array bound must be an integral constant
11567 expression. However, as an extension, we allow VLAs
11568 in function scopes. */
11569 else if (!at_function_scope_p ())
11571 error ("array bound is not an integer constant");
11572 bounds = error_mark_node;
11576 bounds = NULL_TREE;
11577 /* Look for the closing `]'. */
11578 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11580 declarator = cp_error_declarator;
11584 declarator = make_array_declarator (declarator, bounds);
11586 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11588 tree qualifying_scope;
11589 tree unqualified_name;
11590 special_function_kind sfk;
11593 /* Parse a declarator-id */
11594 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11596 cp_parser_parse_tentatively (parser);
11598 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11599 qualifying_scope = parser->scope;
11602 if (!cp_parser_parse_definitely (parser))
11603 unqualified_name = error_mark_node;
11604 else if (unqualified_name
11605 && (qualifying_scope
11606 || (TREE_CODE (unqualified_name)
11607 != IDENTIFIER_NODE)))
11609 cp_parser_error (parser, "expected unqualified-id");
11610 unqualified_name = error_mark_node;
11614 if (!unqualified_name)
11616 if (unqualified_name == error_mark_node)
11618 declarator = cp_error_declarator;
11622 if (qualifying_scope && at_namespace_scope_p ()
11623 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11625 /* In the declaration of a member of a template class
11626 outside of the class itself, the SCOPE will sometimes
11627 be a TYPENAME_TYPE. For example, given:
11629 template <typename T>
11630 int S<T>::R::i = 3;
11632 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11633 this context, we must resolve S<T>::R to an ordinary
11634 type, rather than a typename type.
11636 The reason we normally avoid resolving TYPENAME_TYPEs
11637 is that a specialization of `S' might render
11638 `S<T>::R' not a type. However, if `S' is
11639 specialized, then this `i' will not be used, so there
11640 is no harm in resolving the types here. */
11643 /* Resolve the TYPENAME_TYPE. */
11644 type = resolve_typename_type (qualifying_scope,
11645 /*only_current_p=*/false);
11646 /* If that failed, the declarator is invalid. */
11647 if (type == error_mark_node)
11648 error ("%<%T::%D%> is not a type",
11649 TYPE_CONTEXT (qualifying_scope),
11650 TYPE_IDENTIFIER (qualifying_scope));
11651 qualifying_scope = type;
11655 if (unqualified_name)
11659 if (qualifying_scope
11660 && CLASS_TYPE_P (qualifying_scope))
11661 class_type = qualifying_scope;
11663 class_type = current_class_type;
11665 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11667 tree name_type = TREE_TYPE (unqualified_name);
11668 if (class_type && same_type_p (name_type, class_type))
11670 if (qualifying_scope
11671 && CLASSTYPE_USE_TEMPLATE (name_type))
11673 error ("invalid use of constructor as a template");
11674 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11675 "name the constructor in a qualified name",
11677 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11678 class_type, name_type);
11679 declarator = cp_error_declarator;
11683 unqualified_name = constructor_name (class_type);
11687 /* We do not attempt to print the declarator
11688 here because we do not have enough
11689 information about its original syntactic
11691 cp_parser_error (parser, "invalid declarator");
11692 declarator = cp_error_declarator;
11699 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11700 sfk = sfk_destructor;
11701 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11702 sfk = sfk_conversion;
11703 else if (/* There's no way to declare a constructor
11704 for an anonymous type, even if the type
11705 got a name for linkage purposes. */
11706 !TYPE_WAS_ANONYMOUS (class_type)
11707 && constructor_name_p (unqualified_name,
11710 unqualified_name = constructor_name (class_type);
11711 sfk = sfk_constructor;
11714 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11715 *ctor_dtor_or_conv_p = -1;
11718 declarator = make_id_declarator (qualifying_scope,
11721 declarator->id_loc = token->location;
11723 handle_declarator:;
11724 scope = get_scope_of_declarator (declarator);
11726 /* Any names that appear after the declarator-id for a
11727 member are looked up in the containing scope. */
11728 pushed_scope = push_scope (scope);
11729 parser->in_declarator_p = true;
11730 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11731 || (declarator && declarator->kind == cdk_id))
11732 /* Default args are only allowed on function
11734 parser->default_arg_ok_p = saved_default_arg_ok_p;
11736 parser->default_arg_ok_p = false;
11745 /* For an abstract declarator, we might wind up with nothing at this
11746 point. That's an error; the declarator is not optional. */
11748 cp_parser_error (parser, "expected declarator");
11750 /* If we entered a scope, we must exit it now. */
11752 pop_scope (pushed_scope);
11754 parser->default_arg_ok_p = saved_default_arg_ok_p;
11755 parser->in_declarator_p = saved_in_declarator_p;
11760 /* Parse a ptr-operator.
11763 * cv-qualifier-seq [opt]
11765 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11770 & cv-qualifier-seq [opt]
11772 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11773 Returns ADDR_EXPR if a reference was used. In the case of a
11774 pointer-to-member, *TYPE is filled in with the TYPE containing the
11775 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11776 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11777 ERROR_MARK if an error occurred. */
11779 static enum tree_code
11780 cp_parser_ptr_operator (cp_parser* parser,
11782 cp_cv_quals *cv_quals)
11784 enum tree_code code = ERROR_MARK;
11787 /* Assume that it's not a pointer-to-member. */
11789 /* And that there are no cv-qualifiers. */
11790 *cv_quals = TYPE_UNQUALIFIED;
11792 /* Peek at the next token. */
11793 token = cp_lexer_peek_token (parser->lexer);
11794 /* If it's a `*' or `&' we have a pointer or reference. */
11795 if (token->type == CPP_MULT || token->type == CPP_AND)
11797 /* Remember which ptr-operator we were processing. */
11798 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11800 /* Consume the `*' or `&'. */
11801 cp_lexer_consume_token (parser->lexer);
11803 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11804 `&', if we are allowing GNU extensions. (The only qualifier
11805 that can legally appear after `&' is `restrict', but that is
11806 enforced during semantic analysis. */
11807 if (code == INDIRECT_REF
11808 || cp_parser_allow_gnu_extensions_p (parser))
11809 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11813 /* Try the pointer-to-member case. */
11814 cp_parser_parse_tentatively (parser);
11815 /* Look for the optional `::' operator. */
11816 cp_parser_global_scope_opt (parser,
11817 /*current_scope_valid_p=*/false,
11818 /*object_scope_valid_p=*/false);
11819 /* Look for the nested-name specifier. */
11820 cp_parser_nested_name_specifier (parser,
11821 /*typename_keyword_p=*/false,
11822 /*check_dependency_p=*/true,
11824 /*is_declaration=*/false);
11825 /* If we found it, and the next token is a `*', then we are
11826 indeed looking at a pointer-to-member operator. */
11827 if (!cp_parser_error_occurred (parser)
11828 && cp_parser_require (parser, CPP_MULT, "`*'"))
11830 /* Indicate that the `*' operator was used. */
11831 code = INDIRECT_REF;
11833 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11834 error ("%qD is a namespace", parser->scope);
11837 /* The type of which the member is a member is given by the
11839 *type = parser->scope;
11840 /* The next name will not be qualified. */
11841 parser->scope = NULL_TREE;
11842 parser->qualifying_scope = NULL_TREE;
11843 parser->object_scope = NULL_TREE;
11844 /* Look for the optional cv-qualifier-seq. */
11845 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11848 /* If that didn't work we don't have a ptr-operator. */
11849 if (!cp_parser_parse_definitely (parser))
11850 cp_parser_error (parser, "expected ptr-operator");
11856 /* Parse an (optional) cv-qualifier-seq.
11859 cv-qualifier cv-qualifier-seq [opt]
11870 Returns a bitmask representing the cv-qualifiers. */
11873 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11875 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11880 cp_cv_quals cv_qualifier;
11882 /* Peek at the next token. */
11883 token = cp_lexer_peek_token (parser->lexer);
11884 /* See if it's a cv-qualifier. */
11885 switch (token->keyword)
11888 cv_qualifier = TYPE_QUAL_CONST;
11892 cv_qualifier = TYPE_QUAL_VOLATILE;
11896 cv_qualifier = TYPE_QUAL_RESTRICT;
11900 cv_qualifier = TYPE_UNQUALIFIED;
11907 if (cv_quals & cv_qualifier)
11909 error ("duplicate cv-qualifier");
11910 cp_lexer_purge_token (parser->lexer);
11914 cp_lexer_consume_token (parser->lexer);
11915 cv_quals |= cv_qualifier;
11922 /* Parse a declarator-id.
11926 :: [opt] nested-name-specifier [opt] type-name
11928 In the `id-expression' case, the value returned is as for
11929 cp_parser_id_expression if the id-expression was an unqualified-id.
11930 If the id-expression was a qualified-id, then a SCOPE_REF is
11931 returned. The first operand is the scope (either a NAMESPACE_DECL
11932 or TREE_TYPE), but the second is still just a representation of an
11936 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
11939 /* The expression must be an id-expression. Assume that qualified
11940 names are the names of types so that:
11943 int S<T>::R::i = 3;
11945 will work; we must treat `S<T>::R' as the name of a type.
11946 Similarly, assume that qualified names are templates, where
11950 int S<T>::R<T>::i = 3;
11953 id = cp_parser_id_expression (parser,
11954 /*template_keyword_p=*/false,
11955 /*check_dependency_p=*/false,
11956 /*template_p=*/NULL,
11957 /*declarator_p=*/true,
11959 /*member_p=*/false);
11960 if (id && BASELINK_P (id))
11961 id = BASELINK_FUNCTIONS (id);
11965 /* Parse a type-id.
11968 type-specifier-seq abstract-declarator [opt]
11970 Returns the TYPE specified. */
11973 cp_parser_type_id (cp_parser* parser)
11975 cp_decl_specifier_seq type_specifier_seq;
11976 cp_declarator *abstract_declarator;
11978 /* Parse the type-specifier-seq. */
11979 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11980 &type_specifier_seq);
11981 if (type_specifier_seq.type == error_mark_node)
11982 return error_mark_node;
11984 /* There might or might not be an abstract declarator. */
11985 cp_parser_parse_tentatively (parser);
11986 /* Look for the declarator. */
11987 abstract_declarator
11988 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11989 /*parenthesized_p=*/NULL,
11990 /*member_p=*/false);
11991 /* Check to see if there really was a declarator. */
11992 if (!cp_parser_parse_definitely (parser))
11993 abstract_declarator = NULL;
11995 return groktypename (&type_specifier_seq, abstract_declarator);
11998 /* Parse a type-specifier-seq.
12000 type-specifier-seq:
12001 type-specifier type-specifier-seq [opt]
12005 type-specifier-seq:
12006 attributes type-specifier-seq [opt]
12008 If IS_CONDITION is true, we are at the start of a "condition",
12009 e.g., we've just seen "if (".
12011 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12014 cp_parser_type_specifier_seq (cp_parser* parser,
12016 cp_decl_specifier_seq *type_specifier_seq)
12018 bool seen_type_specifier = false;
12019 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12021 /* Clear the TYPE_SPECIFIER_SEQ. */
12022 clear_decl_specs (type_specifier_seq);
12024 /* Parse the type-specifiers and attributes. */
12027 tree type_specifier;
12028 bool is_cv_qualifier;
12030 /* Check for attributes first. */
12031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12033 type_specifier_seq->attributes =
12034 chainon (type_specifier_seq->attributes,
12035 cp_parser_attributes_opt (parser));
12039 /* Look for the type-specifier. */
12040 type_specifier = cp_parser_type_specifier (parser,
12042 type_specifier_seq,
12043 /*is_declaration=*/false,
12046 if (!type_specifier)
12048 /* If the first type-specifier could not be found, this is not a
12049 type-specifier-seq at all. */
12050 if (!seen_type_specifier)
12052 cp_parser_error (parser, "expected type-specifier");
12053 type_specifier_seq->type = error_mark_node;
12056 /* If subsequent type-specifiers could not be found, the
12057 type-specifier-seq is complete. */
12061 seen_type_specifier = true;
12062 /* The standard says that a condition can be:
12064 type-specifier-seq declarator = assignment-expression
12071 we should treat the "S" as a declarator, not as a
12072 type-specifier. The standard doesn't say that explicitly for
12073 type-specifier-seq, but it does say that for
12074 decl-specifier-seq in an ordinary declaration. Perhaps it
12075 would be clearer just to allow a decl-specifier-seq here, and
12076 then add a semantic restriction that if any decl-specifiers
12077 that are not type-specifiers appear, the program is invalid. */
12078 if (is_condition && !is_cv_qualifier)
12079 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12082 cp_parser_check_decl_spec (type_specifier_seq);
12085 /* Parse a parameter-declaration-clause.
12087 parameter-declaration-clause:
12088 parameter-declaration-list [opt] ... [opt]
12089 parameter-declaration-list , ...
12091 Returns a representation for the parameter declarations. A return
12092 value of NULL indicates a parameter-declaration-clause consisting
12093 only of an ellipsis. */
12095 static cp_parameter_declarator *
12096 cp_parser_parameter_declaration_clause (cp_parser* parser)
12098 cp_parameter_declarator *parameters;
12103 /* Peek at the next token. */
12104 token = cp_lexer_peek_token (parser->lexer);
12105 /* Check for trivial parameter-declaration-clauses. */
12106 if (token->type == CPP_ELLIPSIS)
12108 /* Consume the `...' token. */
12109 cp_lexer_consume_token (parser->lexer);
12112 else if (token->type == CPP_CLOSE_PAREN)
12113 /* There are no parameters. */
12115 #ifndef NO_IMPLICIT_EXTERN_C
12116 if (in_system_header && current_class_type == NULL
12117 && current_lang_name == lang_name_c)
12121 return no_parameters;
12123 /* Check for `(void)', too, which is a special case. */
12124 else if (token->keyword == RID_VOID
12125 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12126 == CPP_CLOSE_PAREN))
12128 /* Consume the `void' token. */
12129 cp_lexer_consume_token (parser->lexer);
12130 /* There are no parameters. */
12131 return no_parameters;
12134 /* Parse the parameter-declaration-list. */
12135 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12136 /* If a parse error occurred while parsing the
12137 parameter-declaration-list, then the entire
12138 parameter-declaration-clause is erroneous. */
12142 /* Peek at the next token. */
12143 token = cp_lexer_peek_token (parser->lexer);
12144 /* If it's a `,', the clause should terminate with an ellipsis. */
12145 if (token->type == CPP_COMMA)
12147 /* Consume the `,'. */
12148 cp_lexer_consume_token (parser->lexer);
12149 /* Expect an ellipsis. */
12151 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12153 /* It might also be `...' if the optional trailing `,' was
12155 else if (token->type == CPP_ELLIPSIS)
12157 /* Consume the `...' token. */
12158 cp_lexer_consume_token (parser->lexer);
12159 /* And remember that we saw it. */
12163 ellipsis_p = false;
12165 /* Finish the parameter list. */
12166 if (parameters && ellipsis_p)
12167 parameters->ellipsis_p = true;
12172 /* Parse a parameter-declaration-list.
12174 parameter-declaration-list:
12175 parameter-declaration
12176 parameter-declaration-list , parameter-declaration
12178 Returns a representation of the parameter-declaration-list, as for
12179 cp_parser_parameter_declaration_clause. However, the
12180 `void_list_node' is never appended to the list. Upon return,
12181 *IS_ERROR will be true iff an error occurred. */
12183 static cp_parameter_declarator *
12184 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12186 cp_parameter_declarator *parameters = NULL;
12187 cp_parameter_declarator **tail = ¶meters;
12188 bool saved_in_unbraced_linkage_specification_p;
12190 /* Assume all will go well. */
12192 /* The special considerations that apply to a function within an
12193 unbraced linkage specifications do not apply to the parameters
12194 to the function. */
12195 saved_in_unbraced_linkage_specification_p
12196 = parser->in_unbraced_linkage_specification_p;
12197 parser->in_unbraced_linkage_specification_p = false;
12199 /* Look for more parameters. */
12202 cp_parameter_declarator *parameter;
12203 bool parenthesized_p;
12204 /* Parse the parameter. */
12206 = cp_parser_parameter_declaration (parser,
12207 /*template_parm_p=*/false,
12210 /* If a parse error occurred parsing the parameter declaration,
12211 then the entire parameter-declaration-list is erroneous. */
12218 /* Add the new parameter to the list. */
12220 tail = ¶meter->next;
12222 /* Peek at the next token. */
12223 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12224 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12225 /* These are for Objective-C++ */
12226 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12227 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12228 /* The parameter-declaration-list is complete. */
12230 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12234 /* Peek at the next token. */
12235 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12236 /* If it's an ellipsis, then the list is complete. */
12237 if (token->type == CPP_ELLIPSIS)
12239 /* Otherwise, there must be more parameters. Consume the
12241 cp_lexer_consume_token (parser->lexer);
12242 /* When parsing something like:
12244 int i(float f, double d)
12246 we can tell after seeing the declaration for "f" that we
12247 are not looking at an initialization of a variable "i",
12248 but rather at the declaration of a function "i".
12250 Due to the fact that the parsing of template arguments
12251 (as specified to a template-id) requires backtracking we
12252 cannot use this technique when inside a template argument
12254 if (!parser->in_template_argument_list_p
12255 && !parser->in_type_id_in_expr_p
12256 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12257 /* However, a parameter-declaration of the form
12258 "foat(f)" (which is a valid declaration of a
12259 parameter "f") can also be interpreted as an
12260 expression (the conversion of "f" to "float"). */
12261 && !parenthesized_p)
12262 cp_parser_commit_to_tentative_parse (parser);
12266 cp_parser_error (parser, "expected %<,%> or %<...%>");
12267 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12268 cp_parser_skip_to_closing_parenthesis (parser,
12269 /*recovering=*/true,
12270 /*or_comma=*/false,
12271 /*consume_paren=*/false);
12276 parser->in_unbraced_linkage_specification_p
12277 = saved_in_unbraced_linkage_specification_p;
12282 /* Parse a parameter declaration.
12284 parameter-declaration:
12285 decl-specifier-seq declarator
12286 decl-specifier-seq declarator = assignment-expression
12287 decl-specifier-seq abstract-declarator [opt]
12288 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12290 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12291 declares a template parameter. (In that case, a non-nested `>'
12292 token encountered during the parsing of the assignment-expression
12293 is not interpreted as a greater-than operator.)
12295 Returns a representation of the parameter, or NULL if an error
12296 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12297 true iff the declarator is of the form "(p)". */
12299 static cp_parameter_declarator *
12300 cp_parser_parameter_declaration (cp_parser *parser,
12301 bool template_parm_p,
12302 bool *parenthesized_p)
12304 int declares_class_or_enum;
12305 bool greater_than_is_operator_p;
12306 cp_decl_specifier_seq decl_specifiers;
12307 cp_declarator *declarator;
12308 tree default_argument;
12310 const char *saved_message;
12312 /* In a template parameter, `>' is not an operator.
12316 When parsing a default template-argument for a non-type
12317 template-parameter, the first non-nested `>' is taken as the end
12318 of the template parameter-list rather than a greater-than
12320 greater_than_is_operator_p = !template_parm_p;
12322 /* Type definitions may not appear in parameter types. */
12323 saved_message = parser->type_definition_forbidden_message;
12324 parser->type_definition_forbidden_message
12325 = "types may not be defined in parameter types";
12327 /* Parse the declaration-specifiers. */
12328 cp_parser_decl_specifier_seq (parser,
12329 CP_PARSER_FLAGS_NONE,
12331 &declares_class_or_enum);
12332 /* If an error occurred, there's no reason to attempt to parse the
12333 rest of the declaration. */
12334 if (cp_parser_error_occurred (parser))
12336 parser->type_definition_forbidden_message = saved_message;
12340 /* Peek at the next token. */
12341 token = cp_lexer_peek_token (parser->lexer);
12342 /* If the next token is a `)', `,', `=', `>', or `...', then there
12343 is no declarator. */
12344 if (token->type == CPP_CLOSE_PAREN
12345 || token->type == CPP_COMMA
12346 || token->type == CPP_EQ
12347 || token->type == CPP_ELLIPSIS
12348 || token->type == CPP_GREATER)
12351 if (parenthesized_p)
12352 *parenthesized_p = false;
12354 /* Otherwise, there should be a declarator. */
12357 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12358 parser->default_arg_ok_p = false;
12360 /* After seeing a decl-specifier-seq, if the next token is not a
12361 "(", there is no possibility that the code is a valid
12362 expression. Therefore, if parsing tentatively, we commit at
12364 if (!parser->in_template_argument_list_p
12365 /* In an expression context, having seen:
12369 we cannot be sure whether we are looking at a
12370 function-type (taking a "char" as a parameter) or a cast
12371 of some object of type "char" to "int". */
12372 && !parser->in_type_id_in_expr_p
12373 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12374 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12375 cp_parser_commit_to_tentative_parse (parser);
12376 /* Parse the declarator. */
12377 declarator = cp_parser_declarator (parser,
12378 CP_PARSER_DECLARATOR_EITHER,
12379 /*ctor_dtor_or_conv_p=*/NULL,
12381 /*member_p=*/false);
12382 parser->default_arg_ok_p = saved_default_arg_ok_p;
12383 /* After the declarator, allow more attributes. */
12384 decl_specifiers.attributes
12385 = chainon (decl_specifiers.attributes,
12386 cp_parser_attributes_opt (parser));
12389 /* The restriction on defining new types applies only to the type
12390 of the parameter, not to the default argument. */
12391 parser->type_definition_forbidden_message = saved_message;
12393 /* If the next token is `=', then process a default argument. */
12394 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12396 bool saved_greater_than_is_operator_p;
12397 /* Consume the `='. */
12398 cp_lexer_consume_token (parser->lexer);
12400 /* If we are defining a class, then the tokens that make up the
12401 default argument must be saved and processed later. */
12402 if (!template_parm_p && at_class_scope_p ()
12403 && TYPE_BEING_DEFINED (current_class_type))
12405 unsigned depth = 0;
12406 cp_token *first_token;
12409 /* Add tokens until we have processed the entire default
12410 argument. We add the range [first_token, token). */
12411 first_token = cp_lexer_peek_token (parser->lexer);
12416 /* Peek at the next token. */
12417 token = cp_lexer_peek_token (parser->lexer);
12418 /* What we do depends on what token we have. */
12419 switch (token->type)
12421 /* In valid code, a default argument must be
12422 immediately followed by a `,' `)', or `...'. */
12424 case CPP_CLOSE_PAREN:
12426 /* If we run into a non-nested `;', `}', or `]',
12427 then the code is invalid -- but the default
12428 argument is certainly over. */
12429 case CPP_SEMICOLON:
12430 case CPP_CLOSE_BRACE:
12431 case CPP_CLOSE_SQUARE:
12434 /* Update DEPTH, if necessary. */
12435 else if (token->type == CPP_CLOSE_PAREN
12436 || token->type == CPP_CLOSE_BRACE
12437 || token->type == CPP_CLOSE_SQUARE)
12441 case CPP_OPEN_PAREN:
12442 case CPP_OPEN_SQUARE:
12443 case CPP_OPEN_BRACE:
12448 /* If we see a non-nested `>', and `>' is not an
12449 operator, then it marks the end of the default
12451 if (!depth && !greater_than_is_operator_p)
12455 /* If we run out of tokens, issue an error message. */
12457 case CPP_PRAGMA_EOL:
12458 error ("file ends in default argument");
12464 /* In these cases, we should look for template-ids.
12465 For example, if the default argument is
12466 `X<int, double>()', we need to do name lookup to
12467 figure out whether or not `X' is a template; if
12468 so, the `,' does not end the default argument.
12470 That is not yet done. */
12477 /* If we've reached the end, stop. */
12481 /* Add the token to the token block. */
12482 token = cp_lexer_consume_token (parser->lexer);
12485 /* Create a DEFAULT_ARG to represented the unparsed default
12487 default_argument = make_node (DEFAULT_ARG);
12488 DEFARG_TOKENS (default_argument)
12489 = cp_token_cache_new (first_token, token);
12490 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12492 /* Outside of a class definition, we can just parse the
12493 assignment-expression. */
12496 bool saved_local_variables_forbidden_p;
12498 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12500 saved_greater_than_is_operator_p
12501 = parser->greater_than_is_operator_p;
12502 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12503 /* Local variable names (and the `this' keyword) may not
12504 appear in a default argument. */
12505 saved_local_variables_forbidden_p
12506 = parser->local_variables_forbidden_p;
12507 parser->local_variables_forbidden_p = true;
12508 /* The default argument expression may cause implicitly
12509 defined member functions to be synthesized, which will
12510 result in garbage collection. We must treat this
12511 situation as if we were within the body of function so as
12512 to avoid collecting live data on the stack. */
12514 /* Parse the assignment-expression. */
12515 if (template_parm_p)
12516 push_deferring_access_checks (dk_no_deferred);
12518 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12519 if (template_parm_p)
12520 pop_deferring_access_checks ();
12521 /* Restore saved state. */
12523 parser->greater_than_is_operator_p
12524 = saved_greater_than_is_operator_p;
12525 parser->local_variables_forbidden_p
12526 = saved_local_variables_forbidden_p;
12528 if (!parser->default_arg_ok_p)
12530 if (!flag_pedantic_errors)
12531 warning (0, "deprecated use of default argument for parameter of non-function");
12534 error ("default arguments are only permitted for function parameters");
12535 default_argument = NULL_TREE;
12540 default_argument = NULL_TREE;
12542 return make_parameter_declarator (&decl_specifiers,
12547 /* Parse a function-body.
12550 compound_statement */
12553 cp_parser_function_body (cp_parser *parser)
12555 cp_parser_compound_statement (parser, NULL, false);
12558 /* Parse a ctor-initializer-opt followed by a function-body. Return
12559 true if a ctor-initializer was present. */
12562 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12565 bool ctor_initializer_p;
12567 /* Begin the function body. */
12568 body = begin_function_body ();
12569 /* Parse the optional ctor-initializer. */
12570 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12571 /* Parse the function-body. */
12572 cp_parser_function_body (parser);
12573 /* Finish the function body. */
12574 finish_function_body (body);
12576 return ctor_initializer_p;
12579 /* Parse an initializer.
12582 = initializer-clause
12583 ( expression-list )
12585 Returns an expression representing the initializer. If no
12586 initializer is present, NULL_TREE is returned.
12588 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12589 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12590 set to FALSE if there is no initializer present. If there is an
12591 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12592 is set to true; otherwise it is set to false. */
12595 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12596 bool* non_constant_p)
12601 /* Peek at the next token. */
12602 token = cp_lexer_peek_token (parser->lexer);
12604 /* Let our caller know whether or not this initializer was
12606 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12607 /* Assume that the initializer is constant. */
12608 *non_constant_p = false;
12610 if (token->type == CPP_EQ)
12612 /* Consume the `='. */
12613 cp_lexer_consume_token (parser->lexer);
12614 /* Parse the initializer-clause. */
12615 init = cp_parser_initializer_clause (parser, non_constant_p);
12617 else if (token->type == CPP_OPEN_PAREN)
12618 init = cp_parser_parenthesized_expression_list (parser, false,
12623 /* Anything else is an error. */
12624 cp_parser_error (parser, "expected initializer");
12625 init = error_mark_node;
12631 /* Parse an initializer-clause.
12633 initializer-clause:
12634 assignment-expression
12635 { initializer-list , [opt] }
12638 Returns an expression representing the initializer.
12640 If the `assignment-expression' production is used the value
12641 returned is simply a representation for the expression.
12643 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12644 the elements of the initializer-list (or NULL, if the last
12645 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12646 NULL_TREE. There is no way to detect whether or not the optional
12647 trailing `,' was provided. NON_CONSTANT_P is as for
12648 cp_parser_initializer. */
12651 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12655 /* Assume the expression is constant. */
12656 *non_constant_p = false;
12658 /* If it is not a `{', then we are looking at an
12659 assignment-expression. */
12660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12663 = cp_parser_constant_expression (parser,
12664 /*allow_non_constant_p=*/true,
12666 if (!*non_constant_p)
12667 initializer = fold_non_dependent_expr (initializer);
12671 /* Consume the `{' token. */
12672 cp_lexer_consume_token (parser->lexer);
12673 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12674 initializer = make_node (CONSTRUCTOR);
12675 /* If it's not a `}', then there is a non-trivial initializer. */
12676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12678 /* Parse the initializer list. */
12679 CONSTRUCTOR_ELTS (initializer)
12680 = cp_parser_initializer_list (parser, non_constant_p);
12681 /* A trailing `,' token is allowed. */
12682 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12683 cp_lexer_consume_token (parser->lexer);
12685 /* Now, there should be a trailing `}'. */
12686 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12689 return initializer;
12692 /* Parse an initializer-list.
12696 initializer-list , initializer-clause
12701 identifier : initializer-clause
12702 initializer-list, identifier : initializer-clause
12704 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12705 for the initializer. If the INDEX of the elt is non-NULL, it is the
12706 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12707 as for cp_parser_initializer. */
12709 static VEC(constructor_elt,gc) *
12710 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12712 VEC(constructor_elt,gc) *v = NULL;
12714 /* Assume all of the expressions are constant. */
12715 *non_constant_p = false;
12717 /* Parse the rest of the list. */
12723 bool clause_non_constant_p;
12725 /* If the next token is an identifier and the following one is a
12726 colon, we are looking at the GNU designated-initializer
12728 if (cp_parser_allow_gnu_extensions_p (parser)
12729 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12730 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12732 /* Consume the identifier. */
12733 identifier = cp_lexer_consume_token (parser->lexer)->value;
12734 /* Consume the `:'. */
12735 cp_lexer_consume_token (parser->lexer);
12738 identifier = NULL_TREE;
12740 /* Parse the initializer. */
12741 initializer = cp_parser_initializer_clause (parser,
12742 &clause_non_constant_p);
12743 /* If any clause is non-constant, so is the entire initializer. */
12744 if (clause_non_constant_p)
12745 *non_constant_p = true;
12747 /* Add it to the vector. */
12748 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12750 /* If the next token is not a comma, we have reached the end of
12752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12755 /* Peek at the next token. */
12756 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12757 /* If the next token is a `}', then we're still done. An
12758 initializer-clause can have a trailing `,' after the
12759 initializer-list and before the closing `}'. */
12760 if (token->type == CPP_CLOSE_BRACE)
12763 /* Consume the `,' token. */
12764 cp_lexer_consume_token (parser->lexer);
12770 /* Classes [gram.class] */
12772 /* Parse a class-name.
12778 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12779 to indicate that names looked up in dependent types should be
12780 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12781 keyword has been used to indicate that the name that appears next
12782 is a template. TAG_TYPE indicates the explicit tag given before
12783 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12784 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12785 is the class being defined in a class-head.
12787 Returns the TYPE_DECL representing the class. */
12790 cp_parser_class_name (cp_parser *parser,
12791 bool typename_keyword_p,
12792 bool template_keyword_p,
12793 enum tag_types tag_type,
12794 bool check_dependency_p,
12796 bool is_declaration)
12803 /* All class-names start with an identifier. */
12804 token = cp_lexer_peek_token (parser->lexer);
12805 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12807 cp_parser_error (parser, "expected class-name");
12808 return error_mark_node;
12811 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12812 to a template-id, so we save it here. */
12813 scope = parser->scope;
12814 if (scope == error_mark_node)
12815 return error_mark_node;
12817 /* Any name names a type if we're following the `typename' keyword
12818 in a qualified name where the enclosing scope is type-dependent. */
12819 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12820 && dependent_type_p (scope));
12821 /* Handle the common case (an identifier, but not a template-id)
12823 if (token->type == CPP_NAME
12824 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12826 cp_token *identifier_token;
12830 /* Look for the identifier. */
12831 identifier_token = cp_lexer_peek_token (parser->lexer);
12832 ambiguous_p = identifier_token->ambiguous_p;
12833 identifier = cp_parser_identifier (parser);
12834 /* If the next token isn't an identifier, we are certainly not
12835 looking at a class-name. */
12836 if (identifier == error_mark_node)
12837 decl = error_mark_node;
12838 /* If we know this is a type-name, there's no need to look it
12840 else if (typename_p)
12844 tree ambiguous_decls;
12845 /* If we already know that this lookup is ambiguous, then
12846 we've already issued an error message; there's no reason
12850 cp_parser_simulate_error (parser);
12851 return error_mark_node;
12853 /* If the next token is a `::', then the name must be a type
12856 [basic.lookup.qual]
12858 During the lookup for a name preceding the :: scope
12859 resolution operator, object, function, and enumerator
12860 names are ignored. */
12861 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12862 tag_type = typename_type;
12863 /* Look up the name. */
12864 decl = cp_parser_lookup_name (parser, identifier,
12866 /*is_template=*/false,
12867 /*is_namespace=*/false,
12868 check_dependency_p,
12870 if (ambiguous_decls)
12872 error ("reference to %qD is ambiguous", identifier);
12873 print_candidates (ambiguous_decls);
12874 if (cp_parser_parsing_tentatively (parser))
12876 identifier_token->ambiguous_p = true;
12877 cp_parser_simulate_error (parser);
12879 return error_mark_node;
12885 /* Try a template-id. */
12886 decl = cp_parser_template_id (parser, template_keyword_p,
12887 check_dependency_p,
12889 if (decl == error_mark_node)
12890 return error_mark_node;
12893 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12895 /* If this is a typename, create a TYPENAME_TYPE. */
12896 if (typename_p && decl != error_mark_node)
12898 decl = make_typename_type (scope, decl, typename_type,
12899 /*complain=*/tf_error);
12900 if (decl != error_mark_node)
12901 decl = TYPE_NAME (decl);
12904 /* Check to see that it is really the name of a class. */
12905 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12906 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12907 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12908 /* Situations like this:
12910 template <typename T> struct A {
12911 typename T::template X<int>::I i;
12914 are problematic. Is `T::template X<int>' a class-name? The
12915 standard does not seem to be definitive, but there is no other
12916 valid interpretation of the following `::'. Therefore, those
12917 names are considered class-names. */
12919 decl = make_typename_type (scope, decl, tag_type, tf_error);
12920 if (decl != error_mark_node)
12921 decl = TYPE_NAME (decl);
12923 else if (TREE_CODE (decl) != TYPE_DECL
12924 || TREE_TYPE (decl) == error_mark_node
12925 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12926 decl = error_mark_node;
12928 if (decl == error_mark_node)
12929 cp_parser_error (parser, "expected class-name");
12934 /* Parse a class-specifier.
12937 class-head { member-specification [opt] }
12939 Returns the TREE_TYPE representing the class. */
12942 cp_parser_class_specifier (cp_parser* parser)
12946 tree attributes = NULL_TREE;
12947 int has_trailing_semicolon;
12948 bool nested_name_specifier_p;
12949 unsigned saved_num_template_parameter_lists;
12950 tree old_scope = NULL_TREE;
12951 tree scope = NULL_TREE;
12953 push_deferring_access_checks (dk_no_deferred);
12955 /* Parse the class-head. */
12956 type = cp_parser_class_head (parser,
12957 &nested_name_specifier_p,
12959 /* If the class-head was a semantic disaster, skip the entire body
12963 cp_parser_skip_to_end_of_block_or_statement (parser);
12964 pop_deferring_access_checks ();
12965 return error_mark_node;
12968 /* Look for the `{'. */
12969 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12971 pop_deferring_access_checks ();
12972 return error_mark_node;
12975 /* Issue an error message if type-definitions are forbidden here. */
12976 cp_parser_check_type_definition (parser);
12977 /* Remember that we are defining one more class. */
12978 ++parser->num_classes_being_defined;
12979 /* Inside the class, surrounding template-parameter-lists do not
12981 saved_num_template_parameter_lists
12982 = parser->num_template_parameter_lists;
12983 parser->num_template_parameter_lists = 0;
12985 /* Start the class. */
12986 if (nested_name_specifier_p)
12988 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12989 old_scope = push_inner_scope (scope);
12991 type = begin_class_definition (type, attributes);
12993 if (type == error_mark_node)
12994 /* If the type is erroneous, skip the entire body of the class. */
12995 cp_parser_skip_to_closing_brace (parser);
12997 /* Parse the member-specification. */
12998 cp_parser_member_specification_opt (parser);
13000 /* Look for the trailing `}'. */
13001 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13002 /* We get better error messages by noticing a common problem: a
13003 missing trailing `;'. */
13004 token = cp_lexer_peek_token (parser->lexer);
13005 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13006 /* Look for trailing attributes to apply to this class. */
13007 if (cp_parser_allow_gnu_extensions_p (parser))
13008 attributes = cp_parser_attributes_opt (parser);
13009 if (type != error_mark_node)
13010 type = finish_struct (type, attributes);
13011 if (nested_name_specifier_p)
13012 pop_inner_scope (old_scope, scope);
13013 /* If this class is not itself within the scope of another class,
13014 then we need to parse the bodies of all of the queued function
13015 definitions. Note that the queued functions defined in a class
13016 are not always processed immediately following the
13017 class-specifier for that class. Consider:
13020 struct B { void f() { sizeof (A); } };
13023 If `f' were processed before the processing of `A' were
13024 completed, there would be no way to compute the size of `A'.
13025 Note that the nesting we are interested in here is lexical --
13026 not the semantic nesting given by TYPE_CONTEXT. In particular,
13029 struct A { struct B; };
13030 struct A::B { void f() { } };
13032 there is no need to delay the parsing of `A::B::f'. */
13033 if (--parser->num_classes_being_defined == 0)
13037 tree class_type = NULL_TREE;
13038 tree pushed_scope = NULL_TREE;
13040 /* In a first pass, parse default arguments to the functions.
13041 Then, in a second pass, parse the bodies of the functions.
13042 This two-phased approach handles cases like:
13050 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13051 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13052 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13053 TREE_PURPOSE (parser->unparsed_functions_queues)
13054 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13056 fn = TREE_VALUE (queue_entry);
13057 /* If there are default arguments that have not yet been processed,
13058 take care of them now. */
13059 if (class_type != TREE_PURPOSE (queue_entry))
13062 pop_scope (pushed_scope);
13063 class_type = TREE_PURPOSE (queue_entry);
13064 pushed_scope = push_scope (class_type);
13066 /* Make sure that any template parameters are in scope. */
13067 maybe_begin_member_template_processing (fn);
13068 /* Parse the default argument expressions. */
13069 cp_parser_late_parsing_default_args (parser, fn);
13070 /* Remove any template parameters from the symbol table. */
13071 maybe_end_member_template_processing ();
13074 pop_scope (pushed_scope);
13075 /* Now parse the body of the functions. */
13076 for (TREE_VALUE (parser->unparsed_functions_queues)
13077 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13078 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13079 TREE_VALUE (parser->unparsed_functions_queues)
13080 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13082 /* Figure out which function we need to process. */
13083 fn = TREE_VALUE (queue_entry);
13084 /* Parse the function. */
13085 cp_parser_late_parsing_for_member (parser, fn);
13089 /* Put back any saved access checks. */
13090 pop_deferring_access_checks ();
13092 /* Restore the count of active template-parameter-lists. */
13093 parser->num_template_parameter_lists
13094 = saved_num_template_parameter_lists;
13099 /* Parse a class-head.
13102 class-key identifier [opt] base-clause [opt]
13103 class-key nested-name-specifier identifier base-clause [opt]
13104 class-key nested-name-specifier [opt] template-id
13108 class-key attributes identifier [opt] base-clause [opt]
13109 class-key attributes nested-name-specifier identifier base-clause [opt]
13110 class-key attributes nested-name-specifier [opt] template-id
13113 Returns the TYPE of the indicated class. Sets
13114 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13115 involving a nested-name-specifier was used, and FALSE otherwise.
13117 Returns error_mark_node if this is not a class-head.
13119 Returns NULL_TREE if the class-head is syntactically valid, but
13120 semantically invalid in a way that means we should skip the entire
13121 body of the class. */
13124 cp_parser_class_head (cp_parser* parser,
13125 bool* nested_name_specifier_p,
13126 tree *attributes_p)
13128 tree nested_name_specifier;
13129 enum tag_types class_key;
13130 tree id = NULL_TREE;
13131 tree type = NULL_TREE;
13133 bool template_id_p = false;
13134 bool qualified_p = false;
13135 bool invalid_nested_name_p = false;
13136 bool invalid_explicit_specialization_p = false;
13137 tree pushed_scope = NULL_TREE;
13138 unsigned num_templates;
13141 /* Assume no nested-name-specifier will be present. */
13142 *nested_name_specifier_p = false;
13143 /* Assume no template parameter lists will be used in defining the
13147 /* Look for the class-key. */
13148 class_key = cp_parser_class_key (parser);
13149 if (class_key == none_type)
13150 return error_mark_node;
13152 /* Parse the attributes. */
13153 attributes = cp_parser_attributes_opt (parser);
13155 /* If the next token is `::', that is invalid -- but sometimes
13156 people do try to write:
13160 Handle this gracefully by accepting the extra qualifier, and then
13161 issuing an error about it later if this really is a
13162 class-head. If it turns out just to be an elaborated type
13163 specifier, remain silent. */
13164 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
13165 /*object_scope_valid_p=*/false))
13166 qualified_p = true;
13168 push_deferring_access_checks (dk_no_check);
13170 /* Determine the name of the class. Begin by looking for an
13171 optional nested-name-specifier. */
13172 nested_name_specifier
13173 = cp_parser_nested_name_specifier_opt (parser,
13174 /*typename_keyword_p=*/false,
13175 /*check_dependency_p=*/false,
13177 /*is_declaration=*/false);
13178 /* If there was a nested-name-specifier, then there *must* be an
13180 if (nested_name_specifier)
13182 /* Although the grammar says `identifier', it really means
13183 `class-name' or `template-name'. You are only allowed to
13184 define a class that has already been declared with this
13187 The proposed resolution for Core Issue 180 says that wherever
13188 you see `class T::X' you should treat `X' as a type-name.
13190 It is OK to define an inaccessible class; for example:
13192 class A { class B; };
13195 We do not know if we will see a class-name, or a
13196 template-name. We look for a class-name first, in case the
13197 class-name is a template-id; if we looked for the
13198 template-name first we would stop after the template-name. */
13199 cp_parser_parse_tentatively (parser);
13200 type = cp_parser_class_name (parser,
13201 /*typename_keyword_p=*/false,
13202 /*template_keyword_p=*/false,
13204 /*check_dependency_p=*/false,
13205 /*class_head_p=*/true,
13206 /*is_declaration=*/false);
13207 /* If that didn't work, ignore the nested-name-specifier. */
13208 if (!cp_parser_parse_definitely (parser))
13210 invalid_nested_name_p = true;
13211 id = cp_parser_identifier (parser);
13212 if (id == error_mark_node)
13215 /* If we could not find a corresponding TYPE, treat this
13216 declaration like an unqualified declaration. */
13217 if (type == error_mark_node)
13218 nested_name_specifier = NULL_TREE;
13219 /* Otherwise, count the number of templates used in TYPE and its
13220 containing scopes. */
13225 for (scope = TREE_TYPE (type);
13226 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13227 scope = (TYPE_P (scope)
13228 ? TYPE_CONTEXT (scope)
13229 : DECL_CONTEXT (scope)))
13231 && CLASS_TYPE_P (scope)
13232 && CLASSTYPE_TEMPLATE_INFO (scope)
13233 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13234 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13238 /* Otherwise, the identifier is optional. */
13241 /* We don't know whether what comes next is a template-id,
13242 an identifier, or nothing at all. */
13243 cp_parser_parse_tentatively (parser);
13244 /* Check for a template-id. */
13245 id = cp_parser_template_id (parser,
13246 /*template_keyword_p=*/false,
13247 /*check_dependency_p=*/true,
13248 /*is_declaration=*/true);
13249 /* If that didn't work, it could still be an identifier. */
13250 if (!cp_parser_parse_definitely (parser))
13252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13253 id = cp_parser_identifier (parser);
13259 template_id_p = true;
13264 pop_deferring_access_checks ();
13267 cp_parser_check_for_invalid_template_id (parser, id);
13269 /* If it's not a `:' or a `{' then we can't really be looking at a
13270 class-head, since a class-head only appears as part of a
13271 class-specifier. We have to detect this situation before calling
13272 xref_tag, since that has irreversible side-effects. */
13273 if (!cp_parser_next_token_starts_class_definition_p (parser))
13275 cp_parser_error (parser, "expected %<{%> or %<:%>");
13276 return error_mark_node;
13279 /* At this point, we're going ahead with the class-specifier, even
13280 if some other problem occurs. */
13281 cp_parser_commit_to_tentative_parse (parser);
13282 /* Issue the error about the overly-qualified name now. */
13284 cp_parser_error (parser,
13285 "global qualification of class name is invalid");
13286 else if (invalid_nested_name_p)
13287 cp_parser_error (parser,
13288 "qualified name does not name a class");
13289 else if (nested_name_specifier)
13293 /* Reject typedef-names in class heads. */
13294 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13296 error ("invalid class name in declaration of %qD", type);
13301 /* Figure out in what scope the declaration is being placed. */
13302 scope = current_scope ();
13303 /* If that scope does not contain the scope in which the
13304 class was originally declared, the program is invalid. */
13305 if (scope && !is_ancestor (scope, nested_name_specifier))
13307 error ("declaration of %qD in %qD which does not enclose %qD",
13308 type, scope, nested_name_specifier);
13314 A declarator-id shall not be qualified exception of the
13315 definition of a ... nested class outside of its class
13316 ... [or] a the definition or explicit instantiation of a
13317 class member of a namespace outside of its namespace. */
13318 if (scope == nested_name_specifier)
13320 pedwarn ("extra qualification ignored");
13321 nested_name_specifier = NULL_TREE;
13325 /* An explicit-specialization must be preceded by "template <>". If
13326 it is not, try to recover gracefully. */
13327 if (at_namespace_scope_p ()
13328 && parser->num_template_parameter_lists == 0
13331 error ("an explicit specialization must be preceded by %<template <>%>");
13332 invalid_explicit_specialization_p = true;
13333 /* Take the same action that would have been taken by
13334 cp_parser_explicit_specialization. */
13335 ++parser->num_template_parameter_lists;
13336 begin_specialization ();
13338 /* There must be no "return" statements between this point and the
13339 end of this function; set "type "to the correct return value and
13340 use "goto done;" to return. */
13341 /* Make sure that the right number of template parameters were
13343 if (!cp_parser_check_template_parameters (parser, num_templates))
13345 /* If something went wrong, there is no point in even trying to
13346 process the class-definition. */
13351 /* Look up the type. */
13354 type = TREE_TYPE (id);
13355 type = maybe_process_partial_specialization (type);
13356 if (nested_name_specifier)
13357 pushed_scope = push_scope (nested_name_specifier);
13359 else if (nested_name_specifier)
13365 template <typename T> struct S { struct T };
13366 template <typename T> struct S<T>::T { };
13368 we will get a TYPENAME_TYPE when processing the definition of
13369 `S::T'. We need to resolve it to the actual type before we
13370 try to define it. */
13371 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13373 class_type = resolve_typename_type (TREE_TYPE (type),
13374 /*only_current_p=*/false);
13375 if (class_type != error_mark_node)
13376 type = TYPE_NAME (class_type);
13379 cp_parser_error (parser, "could not resolve typename type");
13380 type = error_mark_node;
13384 maybe_process_partial_specialization (TREE_TYPE (type));
13385 class_type = current_class_type;
13386 /* Enter the scope indicated by the nested-name-specifier. */
13387 pushed_scope = push_scope (nested_name_specifier);
13388 /* Get the canonical version of this type. */
13389 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13390 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13391 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13393 type = push_template_decl (type);
13394 if (type == error_mark_node)
13401 type = TREE_TYPE (type);
13402 *nested_name_specifier_p = true;
13404 else /* The name is not a nested name. */
13406 /* If the class was unnamed, create a dummy name. */
13408 id = make_anon_name ();
13409 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13410 parser->num_template_parameter_lists);
13413 /* Indicate whether this class was declared as a `class' or as a
13415 if (TREE_CODE (type) == RECORD_TYPE)
13416 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13417 cp_parser_check_class_key (class_key, type);
13419 /* If this type was already complete, and we see another definition,
13420 that's an error. */
13421 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13423 error ("redefinition of %q#T", type);
13424 error ("previous definition of %q+#T", type);
13429 /* We will have entered the scope containing the class; the names of
13430 base classes should be looked up in that context. For example:
13432 struct A { struct B {}; struct C; };
13433 struct A::C : B {};
13438 /* Get the list of base-classes, if there is one. */
13439 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13440 bases = cp_parser_base_clause (parser);
13442 /* Process the base classes. */
13443 xref_basetypes (type, bases);
13446 /* Leave the scope given by the nested-name-specifier. We will
13447 enter the class scope itself while processing the members. */
13449 pop_scope (pushed_scope);
13451 if (invalid_explicit_specialization_p)
13453 end_specialization ();
13454 --parser->num_template_parameter_lists;
13456 *attributes_p = attributes;
13460 /* Parse a class-key.
13467 Returns the kind of class-key specified, or none_type to indicate
13470 static enum tag_types
13471 cp_parser_class_key (cp_parser* parser)
13474 enum tag_types tag_type;
13476 /* Look for the class-key. */
13477 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13481 /* Check to see if the TOKEN is a class-key. */
13482 tag_type = cp_parser_token_is_class_key (token);
13484 cp_parser_error (parser, "expected class-key");
13488 /* Parse an (optional) member-specification.
13490 member-specification:
13491 member-declaration member-specification [opt]
13492 access-specifier : member-specification [opt] */
13495 cp_parser_member_specification_opt (cp_parser* parser)
13502 /* Peek at the next token. */
13503 token = cp_lexer_peek_token (parser->lexer);
13504 /* If it's a `}', or EOF then we've seen all the members. */
13505 if (token->type == CPP_CLOSE_BRACE
13506 || token->type == CPP_EOF
13507 || token->type == CPP_PRAGMA_EOL)
13510 /* See if this token is a keyword. */
13511 keyword = token->keyword;
13515 case RID_PROTECTED:
13517 /* Consume the access-specifier. */
13518 cp_lexer_consume_token (parser->lexer);
13519 /* Remember which access-specifier is active. */
13520 current_access_specifier = token->value;
13521 /* Look for the `:'. */
13522 cp_parser_require (parser, CPP_COLON, "`:'");
13526 /* Accept #pragmas at class scope. */
13527 if (token->type == CPP_PRAGMA)
13529 cp_parser_pragma (parser, pragma_external);
13533 /* Otherwise, the next construction must be a
13534 member-declaration. */
13535 cp_parser_member_declaration (parser);
13540 /* Parse a member-declaration.
13542 member-declaration:
13543 decl-specifier-seq [opt] member-declarator-list [opt] ;
13544 function-definition ; [opt]
13545 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13547 template-declaration
13549 member-declarator-list:
13551 member-declarator-list , member-declarator
13554 declarator pure-specifier [opt]
13555 declarator constant-initializer [opt]
13556 identifier [opt] : constant-expression
13560 member-declaration:
13561 __extension__ member-declaration
13564 declarator attributes [opt] pure-specifier [opt]
13565 declarator attributes [opt] constant-initializer [opt]
13566 identifier [opt] attributes [opt] : constant-expression */
13569 cp_parser_member_declaration (cp_parser* parser)
13571 cp_decl_specifier_seq decl_specifiers;
13572 tree prefix_attributes;
13574 int declares_class_or_enum;
13577 int saved_pedantic;
13579 /* Check for the `__extension__' keyword. */
13580 if (cp_parser_extension_opt (parser, &saved_pedantic))
13583 cp_parser_member_declaration (parser);
13584 /* Restore the old value of the PEDANTIC flag. */
13585 pedantic = saved_pedantic;
13590 /* Check for a template-declaration. */
13591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13593 /* An explicit specialization here is an error condition, and we
13594 expect the specialization handler to detect and report this. */
13595 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13596 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13597 cp_parser_explicit_specialization (parser);
13599 cp_parser_template_declaration (parser, /*member_p=*/true);
13604 /* Check for a using-declaration. */
13605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13607 /* Parse the using-declaration. */
13608 cp_parser_using_declaration (parser);
13613 /* Check for @defs. */
13614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13617 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13618 ivar = ivar_chains;
13622 ivar = TREE_CHAIN (member);
13623 TREE_CHAIN (member) = NULL_TREE;
13624 finish_member_declaration (member);
13629 /* Parse the decl-specifier-seq. */
13630 cp_parser_decl_specifier_seq (parser,
13631 CP_PARSER_FLAGS_OPTIONAL,
13633 &declares_class_or_enum);
13634 prefix_attributes = decl_specifiers.attributes;
13635 decl_specifiers.attributes = NULL_TREE;
13636 /* Check for an invalid type-name. */
13637 if (!decl_specifiers.type
13638 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13640 /* If there is no declarator, then the decl-specifier-seq should
13642 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13644 /* If there was no decl-specifier-seq, and the next token is a
13645 `;', then we have something like:
13651 Each member-declaration shall declare at least one member
13652 name of the class. */
13653 if (!decl_specifiers.any_specifiers_p)
13655 cp_token *token = cp_lexer_peek_token (parser->lexer);
13656 if (pedantic && !token->in_system_header)
13657 pedwarn ("%Hextra %<;%>", &token->location);
13663 /* See if this declaration is a friend. */
13664 friend_p = cp_parser_friend_p (&decl_specifiers);
13665 /* If there were decl-specifiers, check to see if there was
13666 a class-declaration. */
13667 type = check_tag_decl (&decl_specifiers);
13668 /* Nested classes have already been added to the class, but
13669 a `friend' needs to be explicitly registered. */
13672 /* If the `friend' keyword was present, the friend must
13673 be introduced with a class-key. */
13674 if (!declares_class_or_enum)
13675 error ("a class-key must be used when declaring a friend");
13678 template <typename T> struct A {
13679 friend struct A<T>::B;
13682 A<T>::B will be represented by a TYPENAME_TYPE, and
13683 therefore not recognized by check_tag_decl. */
13685 && decl_specifiers.type
13686 && TYPE_P (decl_specifiers.type))
13687 type = decl_specifiers.type;
13688 if (!type || !TYPE_P (type))
13689 error ("friend declaration does not name a class or "
13692 make_friend_class (current_class_type, type,
13693 /*complain=*/true);
13695 /* If there is no TYPE, an error message will already have
13697 else if (!type || type == error_mark_node)
13699 /* An anonymous aggregate has to be handled specially; such
13700 a declaration really declares a data member (with a
13701 particular type), as opposed to a nested class. */
13702 else if (ANON_AGGR_TYPE_P (type))
13704 /* Remove constructors and such from TYPE, now that we
13705 know it is an anonymous aggregate. */
13706 fixup_anonymous_aggr (type);
13707 /* And make the corresponding data member. */
13708 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13709 /* Add it to the class. */
13710 finish_member_declaration (decl);
13713 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13718 /* See if these declarations will be friends. */
13719 friend_p = cp_parser_friend_p (&decl_specifiers);
13721 /* Keep going until we hit the `;' at the end of the
13723 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13725 tree attributes = NULL_TREE;
13726 tree first_attribute;
13728 /* Peek at the next token. */
13729 token = cp_lexer_peek_token (parser->lexer);
13731 /* Check for a bitfield declaration. */
13732 if (token->type == CPP_COLON
13733 || (token->type == CPP_NAME
13734 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13740 /* Get the name of the bitfield. Note that we cannot just
13741 check TOKEN here because it may have been invalidated by
13742 the call to cp_lexer_peek_nth_token above. */
13743 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13744 identifier = cp_parser_identifier (parser);
13746 identifier = NULL_TREE;
13748 /* Consume the `:' token. */
13749 cp_lexer_consume_token (parser->lexer);
13750 /* Get the width of the bitfield. */
13752 = cp_parser_constant_expression (parser,
13753 /*allow_non_constant=*/false,
13756 /* Look for attributes that apply to the bitfield. */
13757 attributes = cp_parser_attributes_opt (parser);
13758 /* Remember which attributes are prefix attributes and
13760 first_attribute = attributes;
13761 /* Combine the attributes. */
13762 attributes = chainon (prefix_attributes, attributes);
13764 /* Create the bitfield declaration. */
13765 decl = grokbitfield (identifier
13766 ? make_id_declarator (NULL_TREE,
13772 /* Apply the attributes. */
13773 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13777 cp_declarator *declarator;
13779 tree asm_specification;
13780 int ctor_dtor_or_conv_p;
13782 /* Parse the declarator. */
13784 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13785 &ctor_dtor_or_conv_p,
13786 /*parenthesized_p=*/NULL,
13787 /*member_p=*/true);
13789 /* If something went wrong parsing the declarator, make sure
13790 that we at least consume some tokens. */
13791 if (declarator == cp_error_declarator)
13793 /* Skip to the end of the statement. */
13794 cp_parser_skip_to_end_of_statement (parser);
13795 /* If the next token is not a semicolon, that is
13796 probably because we just skipped over the body of
13797 a function. So, we consume a semicolon if
13798 present, but do not issue an error message if it
13800 if (cp_lexer_next_token_is (parser->lexer,
13802 cp_lexer_consume_token (parser->lexer);
13806 if (declares_class_or_enum & 2)
13807 cp_parser_check_for_definition_in_return_type
13808 (declarator, decl_specifiers.type);
13810 /* Look for an asm-specification. */
13811 asm_specification = cp_parser_asm_specification_opt (parser);
13812 /* Look for attributes that apply to the declaration. */
13813 attributes = cp_parser_attributes_opt (parser);
13814 /* Remember which attributes are prefix attributes and
13816 first_attribute = attributes;
13817 /* Combine the attributes. */
13818 attributes = chainon (prefix_attributes, attributes);
13820 /* If it's an `=', then we have a constant-initializer or a
13821 pure-specifier. It is not correct to parse the
13822 initializer before registering the member declaration
13823 since the member declaration should be in scope while
13824 its initializer is processed. However, the rest of the
13825 front end does not yet provide an interface that allows
13826 us to handle this correctly. */
13827 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13831 A pure-specifier shall be used only in the declaration of
13832 a virtual function.
13834 A member-declarator can contain a constant-initializer
13835 only if it declares a static member of integral or
13838 Therefore, if the DECLARATOR is for a function, we look
13839 for a pure-specifier; otherwise, we look for a
13840 constant-initializer. When we call `grokfield', it will
13841 perform more stringent semantics checks. */
13842 if (declarator->kind == cdk_function
13843 && declarator->declarator->kind == cdk_id)
13844 initializer = cp_parser_pure_specifier (parser);
13846 /* Parse the initializer. */
13847 initializer = cp_parser_constant_initializer (parser);
13849 /* Otherwise, there is no initializer. */
13851 initializer = NULL_TREE;
13853 /* See if we are probably looking at a function
13854 definition. We are certainly not looking at a
13855 member-declarator. Calling `grokfield' has
13856 side-effects, so we must not do it unless we are sure
13857 that we are looking at a member-declarator. */
13858 if (cp_parser_token_starts_function_definition_p
13859 (cp_lexer_peek_token (parser->lexer)))
13861 /* The grammar does not allow a pure-specifier to be
13862 used when a member function is defined. (It is
13863 possible that this fact is an oversight in the
13864 standard, since a pure function may be defined
13865 outside of the class-specifier. */
13867 error ("pure-specifier on function-definition");
13868 decl = cp_parser_save_member_function_body (parser,
13872 /* If the member was not a friend, declare it here. */
13874 finish_member_declaration (decl);
13875 /* Peek at the next token. */
13876 token = cp_lexer_peek_token (parser->lexer);
13877 /* If the next token is a semicolon, consume it. */
13878 if (token->type == CPP_SEMICOLON)
13879 cp_lexer_consume_token (parser->lexer);
13883 /* Create the declaration. */
13884 decl = grokfield (declarator, &decl_specifiers,
13885 initializer, /*init_const_expr_p=*/true,
13890 /* Reset PREFIX_ATTRIBUTES. */
13891 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13892 attributes = TREE_CHAIN (attributes);
13894 TREE_CHAIN (attributes) = NULL_TREE;
13896 /* If there is any qualification still in effect, clear it
13897 now; we will be starting fresh with the next declarator. */
13898 parser->scope = NULL_TREE;
13899 parser->qualifying_scope = NULL_TREE;
13900 parser->object_scope = NULL_TREE;
13901 /* If it's a `,', then there are more declarators. */
13902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13903 cp_lexer_consume_token (parser->lexer);
13904 /* If the next token isn't a `;', then we have a parse error. */
13905 else if (cp_lexer_next_token_is_not (parser->lexer,
13908 cp_parser_error (parser, "expected %<;%>");
13909 /* Skip tokens until we find a `;'. */
13910 cp_parser_skip_to_end_of_statement (parser);
13917 /* Add DECL to the list of members. */
13919 finish_member_declaration (decl);
13921 if (TREE_CODE (decl) == FUNCTION_DECL)
13922 cp_parser_save_default_args (parser, decl);
13927 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13930 /* Parse a pure-specifier.
13935 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13936 Otherwise, ERROR_MARK_NODE is returned. */
13939 cp_parser_pure_specifier (cp_parser* parser)
13943 /* Look for the `=' token. */
13944 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13945 return error_mark_node;
13946 /* Look for the `0' token. */
13947 token = cp_lexer_consume_token (parser->lexer);
13948 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
13949 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
13951 cp_parser_error (parser,
13952 "invalid pure specifier (only `= 0' is allowed)");
13953 cp_parser_skip_to_end_of_statement (parser);
13954 return error_mark_node;
13956 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
13958 error ("templates may not be %<virtual%>");
13959 return error_mark_node;
13962 return integer_zero_node;
13965 /* Parse a constant-initializer.
13967 constant-initializer:
13968 = constant-expression
13970 Returns a representation of the constant-expression. */
13973 cp_parser_constant_initializer (cp_parser* parser)
13975 /* Look for the `=' token. */
13976 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13977 return error_mark_node;
13979 /* It is invalid to write:
13981 struct S { static const int i = { 7 }; };
13984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13986 cp_parser_error (parser,
13987 "a brace-enclosed initializer is not allowed here");
13988 /* Consume the opening brace. */
13989 cp_lexer_consume_token (parser->lexer);
13990 /* Skip the initializer. */
13991 cp_parser_skip_to_closing_brace (parser);
13992 /* Look for the trailing `}'. */
13993 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13995 return error_mark_node;
13998 return cp_parser_constant_expression (parser,
13999 /*allow_non_constant=*/false,
14003 /* Derived classes [gram.class.derived] */
14005 /* Parse a base-clause.
14008 : base-specifier-list
14010 base-specifier-list:
14012 base-specifier-list , base-specifier
14014 Returns a TREE_LIST representing the base-classes, in the order in
14015 which they were declared. The representation of each node is as
14016 described by cp_parser_base_specifier.
14018 In the case that no bases are specified, this function will return
14019 NULL_TREE, not ERROR_MARK_NODE. */
14022 cp_parser_base_clause (cp_parser* parser)
14024 tree bases = NULL_TREE;
14026 /* Look for the `:' that begins the list. */
14027 cp_parser_require (parser, CPP_COLON, "`:'");
14029 /* Scan the base-specifier-list. */
14035 /* Look for the base-specifier. */
14036 base = cp_parser_base_specifier (parser);
14037 /* Add BASE to the front of the list. */
14038 if (base != error_mark_node)
14040 TREE_CHAIN (base) = bases;
14043 /* Peek at the next token. */
14044 token = cp_lexer_peek_token (parser->lexer);
14045 /* If it's not a comma, then the list is complete. */
14046 if (token->type != CPP_COMMA)
14048 /* Consume the `,'. */
14049 cp_lexer_consume_token (parser->lexer);
14052 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14053 base class had a qualified name. However, the next name that
14054 appears is certainly not qualified. */
14055 parser->scope = NULL_TREE;
14056 parser->qualifying_scope = NULL_TREE;
14057 parser->object_scope = NULL_TREE;
14059 return nreverse (bases);
14062 /* Parse a base-specifier.
14065 :: [opt] nested-name-specifier [opt] class-name
14066 virtual access-specifier [opt] :: [opt] nested-name-specifier
14068 access-specifier virtual [opt] :: [opt] nested-name-specifier
14071 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14072 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14073 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14074 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14077 cp_parser_base_specifier (cp_parser* parser)
14081 bool virtual_p = false;
14082 bool duplicate_virtual_error_issued_p = false;
14083 bool duplicate_access_error_issued_p = false;
14084 bool class_scope_p, template_p;
14085 tree access = access_default_node;
14088 /* Process the optional `virtual' and `access-specifier'. */
14091 /* Peek at the next token. */
14092 token = cp_lexer_peek_token (parser->lexer);
14093 /* Process `virtual'. */
14094 switch (token->keyword)
14097 /* If `virtual' appears more than once, issue an error. */
14098 if (virtual_p && !duplicate_virtual_error_issued_p)
14100 cp_parser_error (parser,
14101 "%<virtual%> specified more than once in base-specified");
14102 duplicate_virtual_error_issued_p = true;
14107 /* Consume the `virtual' token. */
14108 cp_lexer_consume_token (parser->lexer);
14113 case RID_PROTECTED:
14115 /* If more than one access specifier appears, issue an
14117 if (access != access_default_node
14118 && !duplicate_access_error_issued_p)
14120 cp_parser_error (parser,
14121 "more than one access specifier in base-specified");
14122 duplicate_access_error_issued_p = true;
14125 access = ridpointers[(int) token->keyword];
14127 /* Consume the access-specifier. */
14128 cp_lexer_consume_token (parser->lexer);
14137 /* It is not uncommon to see programs mechanically, erroneously, use
14138 the 'typename' keyword to denote (dependent) qualified types
14139 as base classes. */
14140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14142 if (!processing_template_decl)
14143 error ("keyword %<typename%> not allowed outside of templates");
14145 error ("keyword %<typename%> not allowed in this context "
14146 "(the base class is implicitly a type)");
14147 cp_lexer_consume_token (parser->lexer);
14150 /* Look for the optional `::' operator. */
14151 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false,
14152 /*object_scope_valid_p=*/false);
14153 /* Look for the nested-name-specifier. The simplest way to
14158 The keyword `typename' is not permitted in a base-specifier or
14159 mem-initializer; in these contexts a qualified name that
14160 depends on a template-parameter is implicitly assumed to be a
14163 is to pretend that we have seen the `typename' keyword at this
14165 cp_parser_nested_name_specifier_opt (parser,
14166 /*typename_keyword_p=*/true,
14167 /*check_dependency_p=*/true,
14169 /*is_declaration=*/true);
14170 /* If the base class is given by a qualified name, assume that names
14171 we see are type names or templates, as appropriate. */
14172 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14173 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14175 /* Finally, look for the class-name. */
14176 type = cp_parser_class_name (parser,
14180 /*check_dependency_p=*/true,
14181 /*class_head_p=*/false,
14182 /*is_declaration=*/true);
14184 if (type == error_mark_node)
14185 return error_mark_node;
14187 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14190 /* Exception handling [gram.exception] */
14192 /* Parse an (optional) exception-specification.
14194 exception-specification:
14195 throw ( type-id-list [opt] )
14197 Returns a TREE_LIST representing the exception-specification. The
14198 TREE_VALUE of each node is a type. */
14201 cp_parser_exception_specification_opt (cp_parser* parser)
14206 /* Peek at the next token. */
14207 token = cp_lexer_peek_token (parser->lexer);
14208 /* If it's not `throw', then there's no exception-specification. */
14209 if (!cp_parser_is_keyword (token, RID_THROW))
14212 /* Consume the `throw'. */
14213 cp_lexer_consume_token (parser->lexer);
14215 /* Look for the `('. */
14216 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14218 /* Peek at the next token. */
14219 token = cp_lexer_peek_token (parser->lexer);
14220 /* If it's not a `)', then there is a type-id-list. */
14221 if (token->type != CPP_CLOSE_PAREN)
14223 const char *saved_message;
14225 /* Types may not be defined in an exception-specification. */
14226 saved_message = parser->type_definition_forbidden_message;
14227 parser->type_definition_forbidden_message
14228 = "types may not be defined in an exception-specification";
14229 /* Parse the type-id-list. */
14230 type_id_list = cp_parser_type_id_list (parser);
14231 /* Restore the saved message. */
14232 parser->type_definition_forbidden_message = saved_message;
14235 type_id_list = empty_except_spec;
14237 /* Look for the `)'. */
14238 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14240 return type_id_list;
14243 /* Parse an (optional) type-id-list.
14247 type-id-list , type-id
14249 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14250 in the order that the types were presented. */
14253 cp_parser_type_id_list (cp_parser* parser)
14255 tree types = NULL_TREE;
14262 /* Get the next type-id. */
14263 type = cp_parser_type_id (parser);
14264 /* Add it to the list. */
14265 types = add_exception_specifier (types, type, /*complain=*/1);
14266 /* Peek at the next token. */
14267 token = cp_lexer_peek_token (parser->lexer);
14268 /* If it is not a `,', we are done. */
14269 if (token->type != CPP_COMMA)
14271 /* Consume the `,'. */
14272 cp_lexer_consume_token (parser->lexer);
14275 return nreverse (types);
14278 /* Parse a try-block.
14281 try compound-statement handler-seq */
14284 cp_parser_try_block (cp_parser* parser)
14288 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14289 try_block = begin_try_block ();
14290 cp_parser_compound_statement (parser, NULL, true);
14291 finish_try_block (try_block);
14292 cp_parser_handler_seq (parser);
14293 finish_handler_sequence (try_block);
14298 /* Parse a function-try-block.
14300 function-try-block:
14301 try ctor-initializer [opt] function-body handler-seq */
14304 cp_parser_function_try_block (cp_parser* parser)
14306 tree compound_stmt;
14308 bool ctor_initializer_p;
14310 /* Look for the `try' keyword. */
14311 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14313 /* Let the rest of the front-end know where we are. */
14314 try_block = begin_function_try_block (&compound_stmt);
14315 /* Parse the function-body. */
14317 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14318 /* We're done with the `try' part. */
14319 finish_function_try_block (try_block);
14320 /* Parse the handlers. */
14321 cp_parser_handler_seq (parser);
14322 /* We're done with the handlers. */
14323 finish_function_handler_sequence (try_block, compound_stmt);
14325 return ctor_initializer_p;
14328 /* Parse a handler-seq.
14331 handler handler-seq [opt] */
14334 cp_parser_handler_seq (cp_parser* parser)
14340 /* Parse the handler. */
14341 cp_parser_handler (parser);
14342 /* Peek at the next token. */
14343 token = cp_lexer_peek_token (parser->lexer);
14344 /* If it's not `catch' then there are no more handlers. */
14345 if (!cp_parser_is_keyword (token, RID_CATCH))
14350 /* Parse a handler.
14353 catch ( exception-declaration ) compound-statement */
14356 cp_parser_handler (cp_parser* parser)
14361 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14362 handler = begin_handler ();
14363 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14364 declaration = cp_parser_exception_declaration (parser);
14365 finish_handler_parms (declaration, handler);
14366 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14367 cp_parser_compound_statement (parser, NULL, false);
14368 finish_handler (handler);
14371 /* Parse an exception-declaration.
14373 exception-declaration:
14374 type-specifier-seq declarator
14375 type-specifier-seq abstract-declarator
14379 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14380 ellipsis variant is used. */
14383 cp_parser_exception_declaration (cp_parser* parser)
14385 cp_decl_specifier_seq type_specifiers;
14386 cp_declarator *declarator;
14387 const char *saved_message;
14389 /* If it's an ellipsis, it's easy to handle. */
14390 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14392 /* Consume the `...' token. */
14393 cp_lexer_consume_token (parser->lexer);
14397 /* Types may not be defined in exception-declarations. */
14398 saved_message = parser->type_definition_forbidden_message;
14399 parser->type_definition_forbidden_message
14400 = "types may not be defined in exception-declarations";
14402 /* Parse the type-specifier-seq. */
14403 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14405 /* If it's a `)', then there is no declarator. */
14406 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14409 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14410 /*ctor_dtor_or_conv_p=*/NULL,
14411 /*parenthesized_p=*/NULL,
14412 /*member_p=*/false);
14414 /* Restore the saved message. */
14415 parser->type_definition_forbidden_message = saved_message;
14417 if (!type_specifiers.any_specifiers_p)
14418 return error_mark_node;
14420 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14423 /* Parse a throw-expression.
14426 throw assignment-expression [opt]
14428 Returns a THROW_EXPR representing the throw-expression. */
14431 cp_parser_throw_expression (cp_parser* parser)
14436 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14437 token = cp_lexer_peek_token (parser->lexer);
14438 /* Figure out whether or not there is an assignment-expression
14439 following the "throw" keyword. */
14440 if (token->type == CPP_COMMA
14441 || token->type == CPP_SEMICOLON
14442 || token->type == CPP_CLOSE_PAREN
14443 || token->type == CPP_CLOSE_SQUARE
14444 || token->type == CPP_CLOSE_BRACE
14445 || token->type == CPP_COLON)
14446 expression = NULL_TREE;
14448 expression = cp_parser_assignment_expression (parser,
14451 return build_throw (expression);
14454 /* GNU Extensions */
14456 /* Parse an (optional) asm-specification.
14459 asm ( string-literal )
14461 If the asm-specification is present, returns a STRING_CST
14462 corresponding to the string-literal. Otherwise, returns
14466 cp_parser_asm_specification_opt (cp_parser* parser)
14469 tree asm_specification;
14471 /* Peek at the next token. */
14472 token = cp_lexer_peek_token (parser->lexer);
14473 /* If the next token isn't the `asm' keyword, then there's no
14474 asm-specification. */
14475 if (!cp_parser_is_keyword (token, RID_ASM))
14478 /* Consume the `asm' token. */
14479 cp_lexer_consume_token (parser->lexer);
14480 /* Look for the `('. */
14481 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14483 /* Look for the string-literal. */
14484 asm_specification = cp_parser_string_literal (parser, false, false);
14486 /* Look for the `)'. */
14487 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14489 return asm_specification;
14492 /* Parse an asm-operand-list.
14496 asm-operand-list , asm-operand
14499 string-literal ( expression )
14500 [ string-literal ] string-literal ( expression )
14502 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14503 each node is the expression. The TREE_PURPOSE is itself a
14504 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14505 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14506 is a STRING_CST for the string literal before the parenthesis. */
14509 cp_parser_asm_operand_list (cp_parser* parser)
14511 tree asm_operands = NULL_TREE;
14515 tree string_literal;
14519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14521 /* Consume the `[' token. */
14522 cp_lexer_consume_token (parser->lexer);
14523 /* Read the operand name. */
14524 name = cp_parser_identifier (parser);
14525 if (name != error_mark_node)
14526 name = build_string (IDENTIFIER_LENGTH (name),
14527 IDENTIFIER_POINTER (name));
14528 /* Look for the closing `]'. */
14529 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14533 /* Look for the string-literal. */
14534 string_literal = cp_parser_string_literal (parser, false, false);
14536 /* Look for the `('. */
14537 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14538 /* Parse the expression. */
14539 expression = cp_parser_expression (parser, /*cast_p=*/false);
14540 /* Look for the `)'. */
14541 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14543 /* Add this operand to the list. */
14544 asm_operands = tree_cons (build_tree_list (name, string_literal),
14547 /* If the next token is not a `,', there are no more
14549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14551 /* Consume the `,'. */
14552 cp_lexer_consume_token (parser->lexer);
14555 return nreverse (asm_operands);
14558 /* Parse an asm-clobber-list.
14562 asm-clobber-list , string-literal
14564 Returns a TREE_LIST, indicating the clobbers in the order that they
14565 appeared. The TREE_VALUE of each node is a STRING_CST. */
14568 cp_parser_asm_clobber_list (cp_parser* parser)
14570 tree clobbers = NULL_TREE;
14574 tree string_literal;
14576 /* Look for the string literal. */
14577 string_literal = cp_parser_string_literal (parser, false, false);
14578 /* Add it to the list. */
14579 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14580 /* If the next token is not a `,', then the list is
14582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14584 /* Consume the `,' token. */
14585 cp_lexer_consume_token (parser->lexer);
14591 /* Parse an (optional) series of attributes.
14594 attributes attribute
14597 __attribute__ (( attribute-list [opt] ))
14599 The return value is as for cp_parser_attribute_list. */
14602 cp_parser_attributes_opt (cp_parser* parser)
14604 tree attributes = NULL_TREE;
14609 tree attribute_list;
14611 /* Peek at the next token. */
14612 token = cp_lexer_peek_token (parser->lexer);
14613 /* If it's not `__attribute__', then we're done. */
14614 if (token->keyword != RID_ATTRIBUTE)
14617 /* Consume the `__attribute__' keyword. */
14618 cp_lexer_consume_token (parser->lexer);
14619 /* Look for the two `(' tokens. */
14620 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14621 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14623 /* Peek at the next token. */
14624 token = cp_lexer_peek_token (parser->lexer);
14625 if (token->type != CPP_CLOSE_PAREN)
14626 /* Parse the attribute-list. */
14627 attribute_list = cp_parser_attribute_list (parser);
14629 /* If the next token is a `)', then there is no attribute
14631 attribute_list = NULL;
14633 /* Look for the two `)' tokens. */
14634 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14637 /* Add these new attributes to the list. */
14638 attributes = chainon (attributes, attribute_list);
14644 /* Parse an attribute-list.
14648 attribute-list , attribute
14652 identifier ( identifier )
14653 identifier ( identifier , expression-list )
14654 identifier ( expression-list )
14656 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14657 to an attribute. The TREE_PURPOSE of each node is the identifier
14658 indicating which attribute is in use. The TREE_VALUE represents
14659 the arguments, if any. */
14662 cp_parser_attribute_list (cp_parser* parser)
14664 tree attribute_list = NULL_TREE;
14665 bool save_translate_strings_p = parser->translate_strings_p;
14667 parser->translate_strings_p = false;
14674 /* Look for the identifier. We also allow keywords here; for
14675 example `__attribute__ ((const))' is legal. */
14676 token = cp_lexer_peek_token (parser->lexer);
14677 if (token->type == CPP_NAME
14678 || token->type == CPP_KEYWORD)
14680 tree arguments = NULL_TREE;
14682 /* Consume the token. */
14683 token = cp_lexer_consume_token (parser->lexer);
14685 /* Save away the identifier that indicates which attribute
14687 identifier = token->value;
14688 attribute = build_tree_list (identifier, NULL_TREE);
14690 /* Peek at the next token. */
14691 token = cp_lexer_peek_token (parser->lexer);
14692 /* If it's an `(', then parse the attribute arguments. */
14693 if (token->type == CPP_OPEN_PAREN)
14695 arguments = cp_parser_parenthesized_expression_list
14696 (parser, true, /*cast_p=*/false,
14697 /*non_constant_p=*/NULL);
14698 /* Save the arguments away. */
14699 TREE_VALUE (attribute) = arguments;
14702 if (arguments != error_mark_node)
14704 /* Add this attribute to the list. */
14705 TREE_CHAIN (attribute) = attribute_list;
14706 attribute_list = attribute;
14709 token = cp_lexer_peek_token (parser->lexer);
14711 /* Now, look for more attributes. If the next token isn't a
14712 `,', we're done. */
14713 if (token->type != CPP_COMMA)
14716 /* Consume the comma and keep going. */
14717 cp_lexer_consume_token (parser->lexer);
14719 parser->translate_strings_p = save_translate_strings_p;
14721 /* We built up the list in reverse order. */
14722 return nreverse (attribute_list);
14725 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14726 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14727 current value of the PEDANTIC flag, regardless of whether or not
14728 the `__extension__' keyword is present. The caller is responsible
14729 for restoring the value of the PEDANTIC flag. */
14732 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14734 /* Save the old value of the PEDANTIC flag. */
14735 *saved_pedantic = pedantic;
14737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14739 /* Consume the `__extension__' token. */
14740 cp_lexer_consume_token (parser->lexer);
14741 /* We're not being pedantic while the `__extension__' keyword is
14751 /* Parse a label declaration.
14754 __label__ label-declarator-seq ;
14756 label-declarator-seq:
14757 identifier , label-declarator-seq
14761 cp_parser_label_declaration (cp_parser* parser)
14763 /* Look for the `__label__' keyword. */
14764 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14770 /* Look for an identifier. */
14771 identifier = cp_parser_identifier (parser);
14772 /* If we failed, stop. */
14773 if (identifier == error_mark_node)
14775 /* Declare it as a label. */
14776 finish_label_decl (identifier);
14777 /* If the next token is a `;', stop. */
14778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14780 /* Look for the `,' separating the label declarations. */
14781 cp_parser_require (parser, CPP_COMMA, "`,'");
14784 /* Look for the final `;'. */
14785 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14788 /* Support Functions */
14790 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14791 NAME should have one of the representations used for an
14792 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14793 is returned. If PARSER->SCOPE is a dependent type, then a
14794 SCOPE_REF is returned.
14796 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14797 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14798 was formed. Abstractly, such entities should not be passed to this
14799 function, because they do not need to be looked up, but it is
14800 simpler to check for this special case here, rather than at the
14803 In cases not explicitly covered above, this function returns a
14804 DECL, OVERLOAD, or baselink representing the result of the lookup.
14805 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14808 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14809 (e.g., "struct") that was used. In that case bindings that do not
14810 refer to types are ignored.
14812 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14815 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14818 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14821 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14822 TREE_LIST of candidates if name-lookup results in an ambiguity, and
14823 NULL_TREE otherwise. */
14826 cp_parser_lookup_name (cp_parser *parser, tree name,
14827 enum tag_types tag_type,
14830 bool check_dependency,
14831 tree *ambiguous_decls)
14835 tree object_type = parser->context->object_type;
14837 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14838 flags |= LOOKUP_COMPLAIN;
14840 /* Assume that the lookup will be unambiguous. */
14841 if (ambiguous_decls)
14842 *ambiguous_decls = NULL_TREE;
14844 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14845 no longer valid. Note that if we are parsing tentatively, and
14846 the parse fails, OBJECT_TYPE will be automatically restored. */
14847 parser->context->object_type = NULL_TREE;
14849 if (name == error_mark_node)
14850 return error_mark_node;
14852 /* A template-id has already been resolved; there is no lookup to
14854 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14856 if (BASELINK_P (name))
14858 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14859 == TEMPLATE_ID_EXPR);
14863 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14864 it should already have been checked to make sure that the name
14865 used matches the type being destroyed. */
14866 if (TREE_CODE (name) == BIT_NOT_EXPR)
14870 /* Figure out to which type this destructor applies. */
14872 type = parser->scope;
14873 else if (object_type)
14874 type = object_type;
14876 type = current_class_type;
14877 /* If that's not a class type, there is no destructor. */
14878 if (!type || !CLASS_TYPE_P (type))
14879 return error_mark_node;
14880 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14881 lazily_declare_fn (sfk_destructor, type);
14882 if (!CLASSTYPE_DESTRUCTORS (type))
14883 return error_mark_node;
14884 /* If it was a class type, return the destructor. */
14885 return CLASSTYPE_DESTRUCTORS (type);
14888 /* By this point, the NAME should be an ordinary identifier. If
14889 the id-expression was a qualified name, the qualifying scope is
14890 stored in PARSER->SCOPE at this point. */
14891 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14893 /* Perform the lookup. */
14898 if (parser->scope == error_mark_node)
14899 return error_mark_node;
14901 /* If the SCOPE is dependent, the lookup must be deferred until
14902 the template is instantiated -- unless we are explicitly
14903 looking up names in uninstantiated templates. Even then, we
14904 cannot look up the name if the scope is not a class type; it
14905 might, for example, be a template type parameter. */
14906 dependent_p = (TYPE_P (parser->scope)
14907 && !(parser->in_declarator_p
14908 && currently_open_class (parser->scope))
14909 && dependent_type_p (parser->scope));
14910 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14917 /* The resolution to Core Issue 180 says that `struct
14918 A::B' should be considered a type-name, even if `A'
14920 type = make_typename_type (parser->scope, name, tag_type,
14921 /*complain=*/tf_error);
14922 decl = TYPE_NAME (type);
14924 else if (is_template
14925 && (cp_parser_next_token_ends_template_argument_p (parser)
14926 || cp_lexer_next_token_is (parser->lexer,
14928 decl = make_unbound_class_template (parser->scope,
14930 /*complain=*/tf_error);
14932 decl = build_qualified_name (/*type=*/NULL_TREE,
14933 parser->scope, name,
14938 tree pushed_scope = NULL_TREE;
14940 /* If PARSER->SCOPE is a dependent type, then it must be a
14941 class type, and we must not be checking dependencies;
14942 otherwise, we would have processed this lookup above. So
14943 that PARSER->SCOPE is not considered a dependent base by
14944 lookup_member, we must enter the scope here. */
14946 pushed_scope = push_scope (parser->scope);
14947 /* If the PARSER->SCOPE is a template specialization, it
14948 may be instantiated during name lookup. In that case,
14949 errors may be issued. Even if we rollback the current
14950 tentative parse, those errors are valid. */
14951 decl = lookup_qualified_name (parser->scope, name,
14952 tag_type != none_type,
14953 /*complain=*/true);
14955 pop_scope (pushed_scope);
14957 parser->qualifying_scope = parser->scope;
14958 parser->object_scope = NULL_TREE;
14960 else if (object_type)
14962 tree object_decl = NULL_TREE;
14963 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14964 OBJECT_TYPE is not a class. */
14965 if (CLASS_TYPE_P (object_type))
14966 /* If the OBJECT_TYPE is a template specialization, it may
14967 be instantiated during name lookup. In that case, errors
14968 may be issued. Even if we rollback the current tentative
14969 parse, those errors are valid. */
14970 object_decl = lookup_member (object_type,
14973 tag_type != none_type);
14974 /* Look it up in the enclosing context, too. */
14975 decl = lookup_name_real (name, tag_type != none_type,
14977 /*block_p=*/true, is_namespace, flags);
14978 parser->object_scope = object_type;
14979 parser->qualifying_scope = NULL_TREE;
14981 decl = object_decl;
14985 decl = lookup_name_real (name, tag_type != none_type,
14987 /*block_p=*/true, is_namespace, flags);
14988 parser->qualifying_scope = NULL_TREE;
14989 parser->object_scope = NULL_TREE;
14992 /* If the lookup failed, let our caller know. */
14993 if (!decl || decl == error_mark_node)
14994 return error_mark_node;
14996 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14997 if (TREE_CODE (decl) == TREE_LIST)
14999 if (ambiguous_decls)
15000 *ambiguous_decls = decl;
15001 /* The error message we have to print is too complicated for
15002 cp_parser_error, so we incorporate its actions directly. */
15003 if (!cp_parser_simulate_error (parser))
15005 error ("reference to %qD is ambiguous", name);
15006 print_candidates (decl);
15008 return error_mark_node;
15011 gcc_assert (DECL_P (decl)
15012 || TREE_CODE (decl) == OVERLOAD
15013 || TREE_CODE (decl) == SCOPE_REF
15014 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15015 || BASELINK_P (decl));
15017 /* If we have resolved the name of a member declaration, check to
15018 see if the declaration is accessible. When the name resolves to
15019 set of overloaded functions, accessibility is checked when
15020 overload resolution is done.
15022 During an explicit instantiation, access is not checked at all,
15023 as per [temp.explicit]. */
15025 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15030 /* Like cp_parser_lookup_name, but for use in the typical case where
15031 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15032 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15035 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15037 return cp_parser_lookup_name (parser, name,
15039 /*is_template=*/false,
15040 /*is_namespace=*/false,
15041 /*check_dependency=*/true,
15042 /*ambiguous_decls=*/NULL);
15045 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15046 the current context, return the TYPE_DECL. If TAG_NAME_P is
15047 true, the DECL indicates the class being defined in a class-head,
15048 or declared in an elaborated-type-specifier.
15050 Otherwise, return DECL. */
15053 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15055 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15056 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15059 template <typename T> struct B;
15062 template <typename T> struct A::B {};
15064 Similarly, in an elaborated-type-specifier:
15066 namespace N { struct X{}; }
15069 template <typename T> friend struct N::X;
15072 However, if the DECL refers to a class type, and we are in
15073 the scope of the class, then the name lookup automatically
15074 finds the TYPE_DECL created by build_self_reference rather
15075 than a TEMPLATE_DECL. For example, in:
15077 template <class T> struct S {
15081 there is no need to handle such case. */
15083 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15084 return DECL_TEMPLATE_RESULT (decl);
15089 /* If too many, or too few, template-parameter lists apply to the
15090 declarator, issue an error message. Returns TRUE if all went well,
15091 and FALSE otherwise. */
15094 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15095 cp_declarator *declarator)
15097 unsigned num_templates;
15099 /* We haven't seen any classes that involve template parameters yet. */
15102 switch (declarator->kind)
15105 if (declarator->u.id.qualifying_scope)
15110 scope = declarator->u.id.qualifying_scope;
15111 member = declarator->u.id.unqualified_name;
15113 while (scope && CLASS_TYPE_P (scope))
15115 /* You're supposed to have one `template <...>'
15116 for every template class, but you don't need one
15117 for a full specialization. For example:
15119 template <class T> struct S{};
15120 template <> struct S<int> { void f(); };
15121 void S<int>::f () {}
15123 is correct; there shouldn't be a `template <>' for
15124 the definition of `S<int>::f'. */
15125 if (CLASSTYPE_TEMPLATE_INFO (scope)
15126 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
15127 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
15128 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15131 scope = TYPE_CONTEXT (scope);
15134 else if (TREE_CODE (declarator->u.id.unqualified_name)
15135 == TEMPLATE_ID_EXPR)
15136 /* If the DECLARATOR has the form `X<y>' then it uses one
15137 additional level of template parameters. */
15140 return cp_parser_check_template_parameters (parser,
15146 case cdk_reference:
15148 return (cp_parser_check_declarator_template_parameters
15149 (parser, declarator->declarator));
15155 gcc_unreachable ();
15160 /* NUM_TEMPLATES were used in the current declaration. If that is
15161 invalid, return FALSE and issue an error messages. Otherwise,
15165 cp_parser_check_template_parameters (cp_parser* parser,
15166 unsigned num_templates)
15168 /* If there are more template classes than parameter lists, we have
15171 template <class T> void S<T>::R<T>::f (); */
15172 if (parser->num_template_parameter_lists < num_templates)
15174 error ("too few template-parameter-lists");
15177 /* If there are the same number of template classes and parameter
15178 lists, that's OK. */
15179 if (parser->num_template_parameter_lists == num_templates)
15181 /* If there are more, but only one more, then we are referring to a
15182 member template. That's OK too. */
15183 if (parser->num_template_parameter_lists == num_templates + 1)
15185 /* Otherwise, there are too many template parameter lists. We have
15188 template <class T> template <class U> void S::f(); */
15189 error ("too many template-parameter-lists");
15193 /* Parse an optional `::' token indicating that the following name is
15194 from the global namespace. If so, PARSER->SCOPE is set to the
15195 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15196 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15197 Returns the new value of PARSER->SCOPE, if the `::' token is
15198 present, and NULL_TREE otherwise. */
15201 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p,
15202 bool object_scope_valid_p)
15206 /* Peek at the next token. */
15207 token = cp_lexer_peek_token (parser->lexer);
15208 /* If we're looking at a `::' token then we're starting from the
15209 global namespace, not our current location. */
15210 if (token->type == CPP_SCOPE)
15212 /* Consume the `::' token. */
15213 cp_lexer_consume_token (parser->lexer);
15214 /* Set the SCOPE so that we know where to start the lookup. */
15215 parser->scope = global_namespace;
15216 parser->qualifying_scope = global_namespace;
15217 parser->object_scope = NULL_TREE;
15219 return parser->scope;
15222 if (!current_scope_valid_p)
15224 parser->scope = NULL_TREE;
15225 parser->qualifying_scope = NULL_TREE;
15228 if (!object_scope_valid_p)
15229 parser->object_scope = NULL_TREE;
15234 /* Returns TRUE if the upcoming token sequence is the start of a
15235 constructor declarator. If FRIEND_P is true, the declarator is
15236 preceded by the `friend' specifier. */
15239 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15241 bool constructor_p;
15242 tree type_decl = NULL_TREE;
15243 bool nested_name_p;
15244 cp_token *next_token;
15246 /* The common case is that this is not a constructor declarator, so
15247 try to avoid doing lots of work if at all possible. It's not
15248 valid declare a constructor at function scope. */
15249 if (at_function_scope_p ())
15251 /* And only certain tokens can begin a constructor declarator. */
15252 next_token = cp_lexer_peek_token (parser->lexer);
15253 if (next_token->type != CPP_NAME
15254 && next_token->type != CPP_SCOPE
15255 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15256 && next_token->type != CPP_TEMPLATE_ID)
15259 /* Parse tentatively; we are going to roll back all of the tokens
15261 cp_parser_parse_tentatively (parser);
15262 /* Assume that we are looking at a constructor declarator. */
15263 constructor_p = true;
15265 /* Look for the optional `::' operator. */
15266 cp_parser_global_scope_opt (parser,
15267 /*current_scope_valid_p=*/false,
15268 /*object_scope_valid_p=*/false);
15269 /* Look for the nested-name-specifier. */
15271 = (cp_parser_nested_name_specifier_opt (parser,
15272 /*typename_keyword_p=*/false,
15273 /*check_dependency_p=*/false,
15275 /*is_declaration=*/false)
15277 /* Outside of a class-specifier, there must be a
15278 nested-name-specifier. */
15279 if (!nested_name_p &&
15280 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15282 constructor_p = false;
15283 /* If we still think that this might be a constructor-declarator,
15284 look for a class-name. */
15289 template <typename T> struct S { S(); };
15290 template <typename T> S<T>::S ();
15292 we must recognize that the nested `S' names a class.
15295 template <typename T> S<T>::S<T> ();
15297 we must recognize that the nested `S' names a template. */
15298 type_decl = cp_parser_class_name (parser,
15299 /*typename_keyword_p=*/false,
15300 /*template_keyword_p=*/false,
15302 /*check_dependency_p=*/false,
15303 /*class_head_p=*/false,
15304 /*is_declaration=*/false);
15305 /* If there was no class-name, then this is not a constructor. */
15306 constructor_p = !cp_parser_error_occurred (parser);
15309 /* If we're still considering a constructor, we have to see a `(',
15310 to begin the parameter-declaration-clause, followed by either a
15311 `)', an `...', or a decl-specifier. We need to check for a
15312 type-specifier to avoid being fooled into thinking that:
15316 is a constructor. (It is actually a function named `f' that
15317 takes one parameter (of type `int') and returns a value of type
15320 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15323 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15324 /* A parameter declaration begins with a decl-specifier,
15325 which is either the "attribute" keyword, a storage class
15326 specifier, or (usually) a type-specifier. */
15327 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
15328 && !cp_parser_storage_class_specifier_opt (parser))
15331 tree pushed_scope = NULL_TREE;
15332 unsigned saved_num_template_parameter_lists;
15334 /* Names appearing in the type-specifier should be looked up
15335 in the scope of the class. */
15336 if (current_class_type)
15340 type = TREE_TYPE (type_decl);
15341 if (TREE_CODE (type) == TYPENAME_TYPE)
15343 type = resolve_typename_type (type,
15344 /*only_current_p=*/false);
15345 if (type == error_mark_node)
15347 cp_parser_abort_tentative_parse (parser);
15351 pushed_scope = push_scope (type);
15354 /* Inside the constructor parameter list, surrounding
15355 template-parameter-lists do not apply. */
15356 saved_num_template_parameter_lists
15357 = parser->num_template_parameter_lists;
15358 parser->num_template_parameter_lists = 0;
15360 /* Look for the type-specifier. */
15361 cp_parser_type_specifier (parser,
15362 CP_PARSER_FLAGS_NONE,
15363 /*decl_specs=*/NULL,
15364 /*is_declarator=*/true,
15365 /*declares_class_or_enum=*/NULL,
15366 /*is_cv_qualifier=*/NULL);
15368 parser->num_template_parameter_lists
15369 = saved_num_template_parameter_lists;
15371 /* Leave the scope of the class. */
15373 pop_scope (pushed_scope);
15375 constructor_p = !cp_parser_error_occurred (parser);
15379 constructor_p = false;
15380 /* We did not really want to consume any tokens. */
15381 cp_parser_abort_tentative_parse (parser);
15383 return constructor_p;
15386 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15387 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15388 they must be performed once we are in the scope of the function.
15390 Returns the function defined. */
15393 cp_parser_function_definition_from_specifiers_and_declarator
15394 (cp_parser* parser,
15395 cp_decl_specifier_seq *decl_specifiers,
15397 const cp_declarator *declarator)
15402 /* Begin the function-definition. */
15403 success_p = start_function (decl_specifiers, declarator, attributes);
15405 /* The things we're about to see are not directly qualified by any
15406 template headers we've seen thus far. */
15407 reset_specialization ();
15409 /* If there were names looked up in the decl-specifier-seq that we
15410 did not check, check them now. We must wait until we are in the
15411 scope of the function to perform the checks, since the function
15412 might be a friend. */
15413 perform_deferred_access_checks ();
15417 /* Skip the entire function. */
15418 cp_parser_skip_to_end_of_block_or_statement (parser);
15419 fn = error_mark_node;
15422 fn = cp_parser_function_definition_after_declarator (parser,
15423 /*inline_p=*/false);
15428 /* Parse the part of a function-definition that follows the
15429 declarator. INLINE_P is TRUE iff this function is an inline
15430 function defined with a class-specifier.
15432 Returns the function defined. */
15435 cp_parser_function_definition_after_declarator (cp_parser* parser,
15439 bool ctor_initializer_p = false;
15440 bool saved_in_unbraced_linkage_specification_p;
15441 unsigned saved_num_template_parameter_lists;
15443 /* If the next token is `return', then the code may be trying to
15444 make use of the "named return value" extension that G++ used to
15446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15448 /* Consume the `return' keyword. */
15449 cp_lexer_consume_token (parser->lexer);
15450 /* Look for the identifier that indicates what value is to be
15452 cp_parser_identifier (parser);
15453 /* Issue an error message. */
15454 error ("named return values are no longer supported");
15455 /* Skip tokens until we reach the start of the function body. */
15458 cp_token *token = cp_lexer_peek_token (parser->lexer);
15459 if (token->type == CPP_OPEN_BRACE
15460 || token->type == CPP_EOF
15461 || token->type == CPP_PRAGMA_EOL)
15463 cp_lexer_consume_token (parser->lexer);
15466 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15467 anything declared inside `f'. */
15468 saved_in_unbraced_linkage_specification_p
15469 = parser->in_unbraced_linkage_specification_p;
15470 parser->in_unbraced_linkage_specification_p = false;
15471 /* Inside the function, surrounding template-parameter-lists do not
15473 saved_num_template_parameter_lists
15474 = parser->num_template_parameter_lists;
15475 parser->num_template_parameter_lists = 0;
15476 /* If the next token is `try', then we are looking at a
15477 function-try-block. */
15478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15479 ctor_initializer_p = cp_parser_function_try_block (parser);
15480 /* A function-try-block includes the function-body, so we only do
15481 this next part if we're not processing a function-try-block. */
15484 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15486 /* Finish the function. */
15487 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15488 (inline_p ? 2 : 0));
15489 /* Generate code for it, if necessary. */
15490 expand_or_defer_fn (fn);
15491 /* Restore the saved values. */
15492 parser->in_unbraced_linkage_specification_p
15493 = saved_in_unbraced_linkage_specification_p;
15494 parser->num_template_parameter_lists
15495 = saved_num_template_parameter_lists;
15500 /* Parse a template-declaration, assuming that the `export' (and
15501 `extern') keywords, if present, has already been scanned. MEMBER_P
15502 is as for cp_parser_template_declaration. */
15505 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15507 tree decl = NULL_TREE;
15509 tree parameter_list;
15510 bool friend_p = false;
15511 bool need_lang_pop;
15513 /* Look for the `template' keyword. */
15514 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15518 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15522 A template ... shall not have C linkage. */
15523 if (current_lang_name == lang_name_c)
15525 error ("template with C linkage");
15526 /* Give it C++ linkage to avoid confusing other parts of the
15528 push_lang_context (lang_name_cplusplus);
15529 need_lang_pop = true;
15532 need_lang_pop = false;
15534 /* We cannot perform access checks on the template parameter
15535 declarations until we know what is being declared, just as we
15536 cannot check the decl-specifier list. */
15537 push_deferring_access_checks (dk_deferred);
15539 /* If the next token is `>', then we have an invalid
15540 specialization. Rather than complain about an invalid template
15541 parameter, issue an error message here. */
15542 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15544 cp_parser_error (parser, "invalid explicit specialization");
15545 begin_specialization ();
15546 parameter_list = NULL_TREE;
15549 /* Parse the template parameters. */
15550 parameter_list = cp_parser_template_parameter_list (parser);
15552 /* Get the deferred access checks from the parameter list. These
15553 will be checked once we know what is being declared, as for a
15554 member template the checks must be performed in the scope of the
15555 class containing the member. */
15556 checks = get_deferred_access_checks ();
15558 /* Look for the `>'. */
15559 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15560 /* We just processed one more parameter list. */
15561 ++parser->num_template_parameter_lists;
15562 /* If the next token is `template', there are more template
15564 if (cp_lexer_next_token_is_keyword (parser->lexer,
15566 cp_parser_template_declaration_after_export (parser, member_p);
15569 /* There are no access checks when parsing a template, as we do not
15570 know if a specialization will be a friend. */
15571 push_deferring_access_checks (dk_no_check);
15572 decl = cp_parser_single_declaration (parser,
15576 pop_deferring_access_checks ();
15578 /* If this is a member template declaration, let the front
15580 if (member_p && !friend_p && decl)
15582 if (TREE_CODE (decl) == TYPE_DECL)
15583 cp_parser_check_access_in_redeclaration (decl);
15585 decl = finish_member_template_decl (decl);
15587 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15588 make_friend_class (current_class_type, TREE_TYPE (decl),
15589 /*complain=*/true);
15591 /* We are done with the current parameter list. */
15592 --parser->num_template_parameter_lists;
15594 pop_deferring_access_checks ();
15597 finish_template_decl (parameter_list);
15599 /* Register member declarations. */
15600 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15601 finish_member_declaration (decl);
15602 /* For the erroneous case of a template with C linkage, we pushed an
15603 implicit C++ linkage scope; exit that scope now. */
15605 pop_lang_context ();
15606 /* If DECL is a function template, we must return to parse it later.
15607 (Even though there is no definition, there might be default
15608 arguments that need handling.) */
15609 if (member_p && decl
15610 && (TREE_CODE (decl) == FUNCTION_DECL
15611 || DECL_FUNCTION_TEMPLATE_P (decl)))
15612 TREE_VALUE (parser->unparsed_functions_queues)
15613 = tree_cons (NULL_TREE, decl,
15614 TREE_VALUE (parser->unparsed_functions_queues));
15617 /* Perform the deferred access checks from a template-parameter-list.
15618 CHECKS is a TREE_LIST of access checks, as returned by
15619 get_deferred_access_checks. */
15622 cp_parser_perform_template_parameter_access_checks (tree checks)
15624 ++processing_template_parmlist;
15625 perform_access_checks (checks);
15626 --processing_template_parmlist;
15629 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15630 `function-definition' sequence. MEMBER_P is true, this declaration
15631 appears in a class scope.
15633 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15634 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15637 cp_parser_single_declaration (cp_parser* parser,
15642 int declares_class_or_enum;
15643 tree decl = NULL_TREE;
15644 cp_decl_specifier_seq decl_specifiers;
15645 bool function_definition_p = false;
15647 /* This function is only used when processing a template
15649 gcc_assert (innermost_scope_kind () == sk_template_parms
15650 || innermost_scope_kind () == sk_template_spec);
15652 /* Defer access checks until we know what is being declared. */
15653 push_deferring_access_checks (dk_deferred);
15655 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15657 cp_parser_decl_specifier_seq (parser,
15658 CP_PARSER_FLAGS_OPTIONAL,
15660 &declares_class_or_enum);
15662 *friend_p = cp_parser_friend_p (&decl_specifiers);
15664 /* There are no template typedefs. */
15665 if (decl_specifiers.specs[(int) ds_typedef])
15667 error ("template declaration of %qs", "typedef");
15668 decl = error_mark_node;
15671 /* Gather up the access checks that occurred the
15672 decl-specifier-seq. */
15673 stop_deferring_access_checks ();
15675 /* Check for the declaration of a template class. */
15676 if (declares_class_or_enum)
15678 if (cp_parser_declares_only_class_p (parser))
15680 decl = shadow_tag (&decl_specifiers);
15685 friend template <typename T> struct A<T>::B;
15688 A<T>::B will be represented by a TYPENAME_TYPE, and
15689 therefore not recognized by shadow_tag. */
15690 if (friend_p && *friend_p
15692 && decl_specifiers.type
15693 && TYPE_P (decl_specifiers.type))
15694 decl = decl_specifiers.type;
15696 if (decl && decl != error_mark_node)
15697 decl = TYPE_NAME (decl);
15699 decl = error_mark_node;
15701 /* Perform access checks for template parameters. */
15702 cp_parser_perform_template_parameter_access_checks (checks);
15705 /* If it's not a template class, try for a template function. If
15706 the next token is a `;', then this declaration does not declare
15707 anything. But, if there were errors in the decl-specifiers, then
15708 the error might well have come from an attempted class-specifier.
15709 In that case, there's no need to warn about a missing declarator. */
15711 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15712 || decl_specifiers.type != error_mark_node))
15713 decl = cp_parser_init_declarator (parser,
15716 /*function_definition_allowed_p=*/true,
15718 declares_class_or_enum,
15719 &function_definition_p);
15721 pop_deferring_access_checks ();
15723 /* Clear any current qualification; whatever comes next is the start
15724 of something new. */
15725 parser->scope = NULL_TREE;
15726 parser->qualifying_scope = NULL_TREE;
15727 parser->object_scope = NULL_TREE;
15728 /* Look for a trailing `;' after the declaration. */
15729 if (!function_definition_p
15730 && (decl == error_mark_node
15731 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15732 cp_parser_skip_to_end_of_block_or_statement (parser);
15737 /* Parse a cast-expression that is not the operand of a unary "&". */
15740 cp_parser_simple_cast_expression (cp_parser *parser)
15742 return cp_parser_cast_expression (parser, /*address_p=*/false,
15746 /* Parse a functional cast to TYPE. Returns an expression
15747 representing the cast. */
15750 cp_parser_functional_cast (cp_parser* parser, tree type)
15752 tree expression_list;
15756 = cp_parser_parenthesized_expression_list (parser, false,
15758 /*non_constant_p=*/NULL);
15760 cast = build_functional_cast (type, expression_list);
15761 /* [expr.const]/1: In an integral constant expression "only type
15762 conversions to integral or enumeration type can be used". */
15763 if (TREE_CODE (type) == TYPE_DECL)
15764 type = TREE_TYPE (type);
15765 if (cast != error_mark_node
15766 && !cast_valid_in_integral_constant_expression_p (type)
15767 && (cp_parser_non_integral_constant_expression
15768 (parser, "a call to a constructor")))
15769 return error_mark_node;
15773 /* Save the tokens that make up the body of a member function defined
15774 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15775 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15776 specifiers applied to the declaration. Returns the FUNCTION_DECL
15777 for the member function. */
15780 cp_parser_save_member_function_body (cp_parser* parser,
15781 cp_decl_specifier_seq *decl_specifiers,
15782 cp_declarator *declarator,
15789 /* Create the function-declaration. */
15790 fn = start_method (decl_specifiers, declarator, attributes);
15791 /* If something went badly wrong, bail out now. */
15792 if (fn == error_mark_node)
15794 /* If there's a function-body, skip it. */
15795 if (cp_parser_token_starts_function_definition_p
15796 (cp_lexer_peek_token (parser->lexer)))
15797 cp_parser_skip_to_end_of_block_or_statement (parser);
15798 return error_mark_node;
15801 /* Remember it, if there default args to post process. */
15802 cp_parser_save_default_args (parser, fn);
15804 /* Save away the tokens that make up the body of the
15806 first = parser->lexer->next_token;
15807 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15808 /* Handle function try blocks. */
15809 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15810 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15811 last = parser->lexer->next_token;
15813 /* Save away the inline definition; we will process it when the
15814 class is complete. */
15815 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15816 DECL_PENDING_INLINE_P (fn) = 1;
15818 /* We need to know that this was defined in the class, so that
15819 friend templates are handled correctly. */
15820 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15822 /* We're done with the inline definition. */
15823 finish_method (fn);
15825 /* Add FN to the queue of functions to be parsed later. */
15826 TREE_VALUE (parser->unparsed_functions_queues)
15827 = tree_cons (NULL_TREE, fn,
15828 TREE_VALUE (parser->unparsed_functions_queues));
15833 /* Parse a template-argument-list, as well as the trailing ">" (but
15834 not the opening ">"). See cp_parser_template_argument_list for the
15838 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15842 tree saved_qualifying_scope;
15843 tree saved_object_scope;
15844 bool saved_greater_than_is_operator_p;
15845 bool saved_skip_evaluation;
15849 When parsing a template-id, the first non-nested `>' is taken as
15850 the end of the template-argument-list rather than a greater-than
15852 saved_greater_than_is_operator_p
15853 = parser->greater_than_is_operator_p;
15854 parser->greater_than_is_operator_p = false;
15855 /* Parsing the argument list may modify SCOPE, so we save it
15857 saved_scope = parser->scope;
15858 saved_qualifying_scope = parser->qualifying_scope;
15859 saved_object_scope = parser->object_scope;
15860 /* We need to evaluate the template arguments, even though this
15861 template-id may be nested within a "sizeof". */
15862 saved_skip_evaluation = skip_evaluation;
15863 skip_evaluation = false;
15864 /* Parse the template-argument-list itself. */
15865 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15866 arguments = NULL_TREE;
15868 arguments = cp_parser_template_argument_list (parser);
15869 /* Look for the `>' that ends the template-argument-list. If we find
15870 a '>>' instead, it's probably just a typo. */
15871 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15873 if (!saved_greater_than_is_operator_p)
15875 /* If we're in a nested template argument list, the '>>' has
15876 to be a typo for '> >'. We emit the error message, but we
15877 continue parsing and we push a '>' as next token, so that
15878 the argument list will be parsed correctly. Note that the
15879 global source location is still on the token before the
15880 '>>', so we need to say explicitly where we want it. */
15881 cp_token *token = cp_lexer_peek_token (parser->lexer);
15882 error ("%H%<>>%> should be %<> >%> "
15883 "within a nested template argument list",
15886 /* ??? Proper recovery should terminate two levels of
15887 template argument list here. */
15888 token->type = CPP_GREATER;
15892 /* If this is not a nested template argument list, the '>>'
15893 is a typo for '>'. Emit an error message and continue.
15894 Same deal about the token location, but here we can get it
15895 right by consuming the '>>' before issuing the diagnostic. */
15896 cp_lexer_consume_token (parser->lexer);
15897 error ("spurious %<>>%>, use %<>%> to terminate "
15898 "a template argument list");
15902 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15903 /* The `>' token might be a greater-than operator again now. */
15904 parser->greater_than_is_operator_p
15905 = saved_greater_than_is_operator_p;
15906 /* Restore the SAVED_SCOPE. */
15907 parser->scope = saved_scope;
15908 parser->qualifying_scope = saved_qualifying_scope;
15909 parser->object_scope = saved_object_scope;
15910 skip_evaluation = saved_skip_evaluation;
15915 /* MEMBER_FUNCTION is a member function, or a friend. If default
15916 arguments, or the body of the function have not yet been parsed,
15920 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15922 /* If this member is a template, get the underlying
15924 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15925 member_function = DECL_TEMPLATE_RESULT (member_function);
15927 /* There should not be any class definitions in progress at this
15928 point; the bodies of members are only parsed outside of all class
15930 gcc_assert (parser->num_classes_being_defined == 0);
15931 /* While we're parsing the member functions we might encounter more
15932 classes. We want to handle them right away, but we don't want
15933 them getting mixed up with functions that are currently in the
15935 parser->unparsed_functions_queues
15936 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15938 /* Make sure that any template parameters are in scope. */
15939 maybe_begin_member_template_processing (member_function);
15941 /* If the body of the function has not yet been parsed, parse it
15943 if (DECL_PENDING_INLINE_P (member_function))
15945 tree function_scope;
15946 cp_token_cache *tokens;
15948 /* The function is no longer pending; we are processing it. */
15949 tokens = DECL_PENDING_INLINE_INFO (member_function);
15950 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15951 DECL_PENDING_INLINE_P (member_function) = 0;
15953 /* If this is a local class, enter the scope of the containing
15955 function_scope = current_function_decl;
15956 if (function_scope)
15957 push_function_context_to (function_scope);
15960 /* Push the body of the function onto the lexer stack. */
15961 cp_parser_push_lexer_for_tokens (parser, tokens);
15963 /* Let the front end know that we going to be defining this
15965 start_preparsed_function (member_function, NULL_TREE,
15966 SF_PRE_PARSED | SF_INCLASS_INLINE);
15968 /* Don't do access checking if it is a templated function. */
15969 if (processing_template_decl)
15970 push_deferring_access_checks (dk_no_check);
15972 /* Now, parse the body of the function. */
15973 cp_parser_function_definition_after_declarator (parser,
15974 /*inline_p=*/true);
15976 if (processing_template_decl)
15977 pop_deferring_access_checks ();
15979 /* Leave the scope of the containing function. */
15980 if (function_scope)
15981 pop_function_context_from (function_scope);
15982 cp_parser_pop_lexer (parser);
15985 /* Remove any template parameters from the symbol table. */
15986 maybe_end_member_template_processing ();
15988 /* Restore the queue. */
15989 parser->unparsed_functions_queues
15990 = TREE_CHAIN (parser->unparsed_functions_queues);
15993 /* If DECL contains any default args, remember it on the unparsed
15994 functions queue. */
15997 cp_parser_save_default_args (cp_parser* parser, tree decl)
16001 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16003 probe = TREE_CHAIN (probe))
16004 if (TREE_PURPOSE (probe))
16006 TREE_PURPOSE (parser->unparsed_functions_queues)
16007 = tree_cons (current_class_type, decl,
16008 TREE_PURPOSE (parser->unparsed_functions_queues));
16013 /* FN is a FUNCTION_DECL which may contains a parameter with an
16014 unparsed DEFAULT_ARG. Parse the default args now. This function
16015 assumes that the current scope is the scope in which the default
16016 argument should be processed. */
16019 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16021 bool saved_local_variables_forbidden_p;
16024 /* While we're parsing the default args, we might (due to the
16025 statement expression extension) encounter more classes. We want
16026 to handle them right away, but we don't want them getting mixed
16027 up with default args that are currently in the queue. */
16028 parser->unparsed_functions_queues
16029 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16031 /* Local variable names (and the `this' keyword) may not appear
16032 in a default argument. */
16033 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16034 parser->local_variables_forbidden_p = true;
16036 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16038 parm = TREE_CHAIN (parm))
16040 cp_token_cache *tokens;
16041 tree default_arg = TREE_PURPOSE (parm);
16043 VEC(tree,gc) *insts;
16050 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16051 /* This can happen for a friend declaration for a function
16052 already declared with default arguments. */
16055 /* Push the saved tokens for the default argument onto the parser's
16057 tokens = DEFARG_TOKENS (default_arg);
16058 cp_parser_push_lexer_for_tokens (parser, tokens);
16060 /* Parse the assignment-expression. */
16061 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16063 if (!processing_template_decl)
16064 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16066 TREE_PURPOSE (parm) = parsed_arg;
16068 /* Update any instantiations we've already created. */
16069 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16070 VEC_iterate (tree, insts, ix, copy); ix++)
16071 TREE_PURPOSE (copy) = parsed_arg;
16073 /* If the token stream has not been completely used up, then
16074 there was extra junk after the end of the default
16076 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16077 cp_parser_error (parser, "expected %<,%>");
16079 /* Revert to the main lexer. */
16080 cp_parser_pop_lexer (parser);
16083 /* Make sure no default arg is missing. */
16084 check_default_args (fn);
16086 /* Restore the state of local_variables_forbidden_p. */
16087 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16089 /* Restore the queue. */
16090 parser->unparsed_functions_queues
16091 = TREE_CHAIN (parser->unparsed_functions_queues);
16094 /* Parse the operand of `sizeof' (or a similar operator). Returns
16095 either a TYPE or an expression, depending on the form of the
16096 input. The KEYWORD indicates which kind of expression we have
16100 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16102 static const char *format;
16103 tree expr = NULL_TREE;
16104 const char *saved_message;
16105 bool saved_integral_constant_expression_p;
16106 bool saved_non_integral_constant_expression_p;
16108 /* Initialize FORMAT the first time we get here. */
16110 format = "types may not be defined in '%s' expressions";
16112 /* Types cannot be defined in a `sizeof' expression. Save away the
16114 saved_message = parser->type_definition_forbidden_message;
16115 /* And create the new one. */
16116 parser->type_definition_forbidden_message
16117 = XNEWVEC (const char, strlen (format)
16118 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16120 sprintf ((char *) parser->type_definition_forbidden_message,
16121 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16123 /* The restrictions on constant-expressions do not apply inside
16124 sizeof expressions. */
16125 saved_integral_constant_expression_p
16126 = parser->integral_constant_expression_p;
16127 saved_non_integral_constant_expression_p
16128 = parser->non_integral_constant_expression_p;
16129 parser->integral_constant_expression_p = false;
16131 /* Do not actually evaluate the expression. */
16133 /* If it's a `(', then we might be looking at the type-id
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16138 bool saved_in_type_id_in_expr_p;
16140 /* We can't be sure yet whether we're looking at a type-id or an
16142 cp_parser_parse_tentatively (parser);
16143 /* Consume the `('. */
16144 cp_lexer_consume_token (parser->lexer);
16145 /* Parse the type-id. */
16146 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16147 parser->in_type_id_in_expr_p = true;
16148 type = cp_parser_type_id (parser);
16149 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16150 /* Now, look for the trailing `)'. */
16151 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16152 /* If all went well, then we're done. */
16153 if (cp_parser_parse_definitely (parser))
16155 cp_decl_specifier_seq decl_specs;
16157 /* Build a trivial decl-specifier-seq. */
16158 clear_decl_specs (&decl_specs);
16159 decl_specs.type = type;
16161 /* Call grokdeclarator to figure out what type this is. */
16162 expr = grokdeclarator (NULL,
16166 /*attrlist=*/NULL);
16170 /* If the type-id production did not work out, then we must be
16171 looking at the unary-expression production. */
16173 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16175 /* Go back to evaluating expressions. */
16178 /* Free the message we created. */
16179 free ((char *) parser->type_definition_forbidden_message);
16180 /* And restore the old one. */
16181 parser->type_definition_forbidden_message = saved_message;
16182 parser->integral_constant_expression_p
16183 = saved_integral_constant_expression_p;
16184 parser->non_integral_constant_expression_p
16185 = saved_non_integral_constant_expression_p;
16190 /* If the current declaration has no declarator, return true. */
16193 cp_parser_declares_only_class_p (cp_parser *parser)
16195 /* If the next token is a `;' or a `,' then there is no
16197 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16198 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16201 /* Update the DECL_SPECS to reflect the storage class indicated by
16205 cp_parser_set_storage_class (cp_parser *parser,
16206 cp_decl_specifier_seq *decl_specs,
16209 cp_storage_class storage_class;
16211 if (parser->in_unbraced_linkage_specification_p)
16213 error ("invalid use of %qD in linkage specification",
16214 ridpointers[keyword]);
16217 else if (decl_specs->storage_class != sc_none)
16219 decl_specs->multiple_storage_classes_p = true;
16223 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16224 && decl_specs->specs[(int) ds_thread])
16226 error ("%<__thread%> before %qD", ridpointers[keyword]);
16227 decl_specs->specs[(int) ds_thread] = 0;
16233 storage_class = sc_auto;
16236 storage_class = sc_register;
16239 storage_class = sc_static;
16242 storage_class = sc_extern;
16245 storage_class = sc_mutable;
16248 gcc_unreachable ();
16250 decl_specs->storage_class = storage_class;
16253 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16254 is true, the type is a user-defined type; otherwise it is a
16255 built-in type specified by a keyword. */
16258 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16260 bool user_defined_p)
16262 decl_specs->any_specifiers_p = true;
16264 /* If the user tries to redeclare bool or wchar_t (with, for
16265 example, in "typedef int wchar_t;") we remember that this is what
16266 happened. In system headers, we ignore these declarations so
16267 that G++ can work with system headers that are not C++-safe. */
16268 if (decl_specs->specs[(int) ds_typedef]
16270 && (type_spec == boolean_type_node
16271 || type_spec == wchar_type_node)
16272 && (decl_specs->type
16273 || decl_specs->specs[(int) ds_long]
16274 || decl_specs->specs[(int) ds_short]
16275 || decl_specs->specs[(int) ds_unsigned]
16276 || decl_specs->specs[(int) ds_signed]))
16278 decl_specs->redefined_builtin_type = type_spec;
16279 if (!decl_specs->type)
16281 decl_specs->type = type_spec;
16282 decl_specs->user_defined_type_p = false;
16285 else if (decl_specs->type)
16286 decl_specs->multiple_types_p = true;
16289 decl_specs->type = type_spec;
16290 decl_specs->user_defined_type_p = user_defined_p;
16291 decl_specs->redefined_builtin_type = NULL_TREE;
16295 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16296 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16299 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16301 return decl_specifiers->specs[(int) ds_friend] != 0;
16304 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16305 issue an error message indicating that TOKEN_DESC was expected.
16307 Returns the token consumed, if the token had the appropriate type.
16308 Otherwise, returns NULL. */
16311 cp_parser_require (cp_parser* parser,
16312 enum cpp_ttype type,
16313 const char* token_desc)
16315 if (cp_lexer_next_token_is (parser->lexer, type))
16316 return cp_lexer_consume_token (parser->lexer);
16319 /* Output the MESSAGE -- unless we're parsing tentatively. */
16320 if (!cp_parser_simulate_error (parser))
16322 char *message = concat ("expected ", token_desc, NULL);
16323 cp_parser_error (parser, message);
16330 /* Like cp_parser_require, except that tokens will be skipped until
16331 the desired token is found. An error message is still produced if
16332 the next token is not as expected. */
16335 cp_parser_skip_until_found (cp_parser* parser,
16336 enum cpp_ttype type,
16337 const char* token_desc)
16340 unsigned nesting_depth = 0;
16342 if (cp_parser_require (parser, type, token_desc))
16345 /* Skip tokens until the desired token is found. */
16348 /* Peek at the next token. */
16349 token = cp_lexer_peek_token (parser->lexer);
16351 /* If we've reached the token we want, consume it and stop. */
16352 if (token->type == type && !nesting_depth)
16354 cp_lexer_consume_token (parser->lexer);
16358 switch (token->type)
16361 case CPP_PRAGMA_EOL:
16362 /* If we've run out of tokens, stop. */
16365 case CPP_OPEN_BRACE:
16366 case CPP_OPEN_PAREN:
16367 case CPP_OPEN_SQUARE:
16371 case CPP_CLOSE_BRACE:
16372 case CPP_CLOSE_PAREN:
16373 case CPP_CLOSE_SQUARE:
16374 if (nesting_depth-- == 0)
16382 /* Consume this token. */
16383 cp_lexer_consume_token (parser->lexer);
16387 /* If the next token is the indicated keyword, consume it. Otherwise,
16388 issue an error message indicating that TOKEN_DESC was expected.
16390 Returns the token consumed, if the token had the appropriate type.
16391 Otherwise, returns NULL. */
16394 cp_parser_require_keyword (cp_parser* parser,
16396 const char* token_desc)
16398 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16400 if (token && token->keyword != keyword)
16402 dyn_string_t error_msg;
16404 /* Format the error message. */
16405 error_msg = dyn_string_new (0);
16406 dyn_string_append_cstr (error_msg, "expected ");
16407 dyn_string_append_cstr (error_msg, token_desc);
16408 cp_parser_error (parser, error_msg->s);
16409 dyn_string_delete (error_msg);
16416 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16417 function-definition. */
16420 cp_parser_token_starts_function_definition_p (cp_token* token)
16422 return (/* An ordinary function-body begins with an `{'. */
16423 token->type == CPP_OPEN_BRACE
16424 /* A ctor-initializer begins with a `:'. */
16425 || token->type == CPP_COLON
16426 /* A function-try-block begins with `try'. */
16427 || token->keyword == RID_TRY
16428 /* The named return value extension begins with `return'. */
16429 || token->keyword == RID_RETURN);
16432 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16436 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16440 token = cp_lexer_peek_token (parser->lexer);
16441 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16444 /* Returns TRUE iff the next token is the "," or ">" ending a
16445 template-argument. */
16448 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16452 token = cp_lexer_peek_token (parser->lexer);
16453 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16456 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16457 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16460 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16465 token = cp_lexer_peek_nth_token (parser->lexer, n);
16466 if (token->type == CPP_LESS)
16468 /* Check for the sequence `<::' in the original code. It would be lexed as
16469 `[:', where `[' is a digraph, and there is no whitespace before
16471 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16474 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16475 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16481 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16482 or none_type otherwise. */
16484 static enum tag_types
16485 cp_parser_token_is_class_key (cp_token* token)
16487 switch (token->keyword)
16492 return record_type;
16501 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16504 cp_parser_check_class_key (enum tag_types class_key, tree type)
16506 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16507 pedwarn ("%qs tag used in naming %q#T",
16508 class_key == union_type ? "union"
16509 : class_key == record_type ? "struct" : "class",
16513 /* Issue an error message if DECL is redeclared with different
16514 access than its original declaration [class.access.spec/3].
16515 This applies to nested classes and nested class templates.
16519 cp_parser_check_access_in_redeclaration (tree decl)
16521 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16524 if ((TREE_PRIVATE (decl)
16525 != (current_access_specifier == access_private_node))
16526 || (TREE_PROTECTED (decl)
16527 != (current_access_specifier == access_protected_node)))
16528 error ("%qD redeclared with different access", decl);
16531 /* Look for the `template' keyword, as a syntactic disambiguator.
16532 Return TRUE iff it is present, in which case it will be
16536 cp_parser_optional_template_keyword (cp_parser *parser)
16538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16540 /* The `template' keyword can only be used within templates;
16541 outside templates the parser can always figure out what is a
16542 template and what is not. */
16543 if (!processing_template_decl)
16545 error ("%<template%> (as a disambiguator) is only allowed "
16546 "within templates");
16547 /* If this part of the token stream is rescanned, the same
16548 error message would be generated. So, we purge the token
16549 from the stream. */
16550 cp_lexer_purge_token (parser->lexer);
16555 /* Consume the `template' keyword. */
16556 cp_lexer_consume_token (parser->lexer);
16564 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16565 set PARSER->SCOPE, and perform other related actions. */
16568 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16573 /* Get the stored value. */
16574 value = cp_lexer_consume_token (parser->lexer)->value;
16575 /* Perform any access checks that were deferred. */
16576 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16577 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16578 /* Set the scope from the stored value. */
16579 parser->scope = TREE_VALUE (value);
16580 parser->qualifying_scope = TREE_TYPE (value);
16581 parser->object_scope = NULL_TREE;
16584 /* Consume tokens up through a non-nested END token. */
16587 cp_parser_cache_group (cp_parser *parser,
16588 enum cpp_ttype end,
16595 /* Abort a parenthesized expression if we encounter a brace. */
16596 if ((end == CPP_CLOSE_PAREN || depth == 0)
16597 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16599 /* If we've reached the end of the file, stop. */
16600 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16601 || (end != CPP_PRAGMA_EOL
16602 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16604 /* Consume the next token. */
16605 token = cp_lexer_consume_token (parser->lexer);
16606 /* See if it starts a new group. */
16607 if (token->type == CPP_OPEN_BRACE)
16609 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16613 else if (token->type == CPP_OPEN_PAREN)
16614 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16615 else if (token->type == CPP_PRAGMA)
16616 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16617 else if (token->type == end)
16622 /* Begin parsing tentatively. We always save tokens while parsing
16623 tentatively so that if the tentative parsing fails we can restore the
16627 cp_parser_parse_tentatively (cp_parser* parser)
16629 /* Enter a new parsing context. */
16630 parser->context = cp_parser_context_new (parser->context);
16631 /* Begin saving tokens. */
16632 cp_lexer_save_tokens (parser->lexer);
16633 /* In order to avoid repetitive access control error messages,
16634 access checks are queued up until we are no longer parsing
16636 push_deferring_access_checks (dk_deferred);
16639 /* Commit to the currently active tentative parse. */
16642 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16644 cp_parser_context *context;
16647 /* Mark all of the levels as committed. */
16648 lexer = parser->lexer;
16649 for (context = parser->context; context->next; context = context->next)
16651 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16653 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16654 while (!cp_lexer_saving_tokens (lexer))
16655 lexer = lexer->next;
16656 cp_lexer_commit_tokens (lexer);
16660 /* Abort the currently active tentative parse. All consumed tokens
16661 will be rolled back, and no diagnostics will be issued. */
16664 cp_parser_abort_tentative_parse (cp_parser* parser)
16666 cp_parser_simulate_error (parser);
16667 /* Now, pretend that we want to see if the construct was
16668 successfully parsed. */
16669 cp_parser_parse_definitely (parser);
16672 /* Stop parsing tentatively. If a parse error has occurred, restore the
16673 token stream. Otherwise, commit to the tokens we have consumed.
16674 Returns true if no error occurred; false otherwise. */
16677 cp_parser_parse_definitely (cp_parser* parser)
16679 bool error_occurred;
16680 cp_parser_context *context;
16682 /* Remember whether or not an error occurred, since we are about to
16683 destroy that information. */
16684 error_occurred = cp_parser_error_occurred (parser);
16685 /* Remove the topmost context from the stack. */
16686 context = parser->context;
16687 parser->context = context->next;
16688 /* If no parse errors occurred, commit to the tentative parse. */
16689 if (!error_occurred)
16691 /* Commit to the tokens read tentatively, unless that was
16693 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16694 cp_lexer_commit_tokens (parser->lexer);
16696 pop_to_parent_deferring_access_checks ();
16698 /* Otherwise, if errors occurred, roll back our state so that things
16699 are just as they were before we began the tentative parse. */
16702 cp_lexer_rollback_tokens (parser->lexer);
16703 pop_deferring_access_checks ();
16705 /* Add the context to the front of the free list. */
16706 context->next = cp_parser_context_free_list;
16707 cp_parser_context_free_list = context;
16709 return !error_occurred;
16712 /* Returns true if we are parsing tentatively and are not committed to
16713 this tentative parse. */
16716 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16718 return (cp_parser_parsing_tentatively (parser)
16719 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16722 /* Returns nonzero iff an error has occurred during the most recent
16723 tentative parse. */
16726 cp_parser_error_occurred (cp_parser* parser)
16728 return (cp_parser_parsing_tentatively (parser)
16729 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16732 /* Returns nonzero if GNU extensions are allowed. */
16735 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16737 return parser->allow_gnu_extensions_p;
16740 /* Objective-C++ Productions */
16743 /* Parse an Objective-C expression, which feeds into a primary-expression
16747 objc-message-expression
16748 objc-string-literal
16749 objc-encode-expression
16750 objc-protocol-expression
16751 objc-selector-expression
16753 Returns a tree representation of the expression. */
16756 cp_parser_objc_expression (cp_parser* parser)
16758 /* Try to figure out what kind of declaration is present. */
16759 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16763 case CPP_OPEN_SQUARE:
16764 return cp_parser_objc_message_expression (parser);
16766 case CPP_OBJC_STRING:
16767 kwd = cp_lexer_consume_token (parser->lexer);
16768 return objc_build_string_object (kwd->value);
16771 switch (kwd->keyword)
16773 case RID_AT_ENCODE:
16774 return cp_parser_objc_encode_expression (parser);
16776 case RID_AT_PROTOCOL:
16777 return cp_parser_objc_protocol_expression (parser);
16779 case RID_AT_SELECTOR:
16780 return cp_parser_objc_selector_expression (parser);
16786 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
16787 cp_parser_skip_to_end_of_block_or_statement (parser);
16790 return error_mark_node;
16793 /* Parse an Objective-C message expression.
16795 objc-message-expression:
16796 [ objc-message-receiver objc-message-args ]
16798 Returns a representation of an Objective-C message. */
16801 cp_parser_objc_message_expression (cp_parser* parser)
16803 tree receiver, messageargs;
16805 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
16806 receiver = cp_parser_objc_message_receiver (parser);
16807 messageargs = cp_parser_objc_message_args (parser);
16808 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16810 return objc_build_message_expr (build_tree_list (receiver, messageargs));
16813 /* Parse an objc-message-receiver.
16815 objc-message-receiver:
16817 simple-type-specifier
16819 Returns a representation of the type or expression. */
16822 cp_parser_objc_message_receiver (cp_parser* parser)
16826 /* An Objective-C message receiver may be either (1) a type
16827 or (2) an expression. */
16828 cp_parser_parse_tentatively (parser);
16829 rcv = cp_parser_expression (parser, false);
16831 if (cp_parser_parse_definitely (parser))
16834 rcv = cp_parser_simple_type_specifier (parser,
16835 /*decl_specs=*/NULL,
16836 CP_PARSER_FLAGS_NONE);
16838 return objc_get_class_reference (rcv);
16841 /* Parse the arguments and selectors comprising an Objective-C message.
16846 objc-selector-args , objc-comma-args
16848 objc-selector-args:
16849 objc-selector [opt] : assignment-expression
16850 objc-selector-args objc-selector [opt] : assignment-expression
16853 assignment-expression
16854 objc-comma-args , assignment-expression
16856 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16857 selector arguments and TREE_VALUE containing a list of comma
16861 cp_parser_objc_message_args (cp_parser* parser)
16863 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16864 bool maybe_unary_selector_p = true;
16865 cp_token *token = cp_lexer_peek_token (parser->lexer);
16867 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16869 tree selector = NULL_TREE, arg;
16871 if (token->type != CPP_COLON)
16872 selector = cp_parser_objc_selector (parser);
16874 /* Detect if we have a unary selector. */
16875 if (maybe_unary_selector_p
16876 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16877 return build_tree_list (selector, NULL_TREE);
16879 maybe_unary_selector_p = false;
16880 cp_parser_require (parser, CPP_COLON, "`:'");
16881 arg = cp_parser_assignment_expression (parser, false);
16884 = chainon (sel_args,
16885 build_tree_list (selector, arg));
16887 token = cp_lexer_peek_token (parser->lexer);
16890 /* Handle non-selector arguments, if any. */
16891 while (token->type == CPP_COMMA)
16895 cp_lexer_consume_token (parser->lexer);
16896 arg = cp_parser_assignment_expression (parser, false);
16899 = chainon (addl_args,
16900 build_tree_list (NULL_TREE, arg));
16902 token = cp_lexer_peek_token (parser->lexer);
16905 return build_tree_list (sel_args, addl_args);
16908 /* Parse an Objective-C encode expression.
16910 objc-encode-expression:
16911 @encode objc-typename
16913 Returns an encoded representation of the type argument. */
16916 cp_parser_objc_encode_expression (cp_parser* parser)
16920 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
16921 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16922 type = complete_type (cp_parser_type_id (parser));
16923 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16927 error ("%<@encode%> must specify a type as an argument");
16928 return error_mark_node;
16931 return objc_build_encode_expr (type);
16934 /* Parse an Objective-C @defs expression. */
16937 cp_parser_objc_defs_expression (cp_parser *parser)
16941 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
16942 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16943 name = cp_parser_identifier (parser);
16944 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16946 return objc_get_class_ivars (name);
16949 /* Parse an Objective-C protocol expression.
16951 objc-protocol-expression:
16952 @protocol ( identifier )
16954 Returns a representation of the protocol expression. */
16957 cp_parser_objc_protocol_expression (cp_parser* parser)
16961 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
16962 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16963 proto = cp_parser_identifier (parser);
16964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16966 return objc_build_protocol_expr (proto);
16969 /* Parse an Objective-C selector expression.
16971 objc-selector-expression:
16972 @selector ( objc-method-signature )
16974 objc-method-signature:
16980 objc-selector-seq objc-selector :
16982 Returns a representation of the method selector. */
16985 cp_parser_objc_selector_expression (cp_parser* parser)
16987 tree sel_seq = NULL_TREE;
16988 bool maybe_unary_selector_p = true;
16991 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
16992 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16993 token = cp_lexer_peek_token (parser->lexer);
16995 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
16996 || token->type == CPP_SCOPE)
16998 tree selector = NULL_TREE;
17000 if (token->type != CPP_COLON
17001 || token->type == CPP_SCOPE)
17002 selector = cp_parser_objc_selector (parser);
17004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17005 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17007 /* Detect if we have a unary selector. */
17008 if (maybe_unary_selector_p)
17010 sel_seq = selector;
17011 goto finish_selector;
17015 cp_parser_error (parser, "expected %<:%>");
17018 maybe_unary_selector_p = false;
17019 token = cp_lexer_consume_token (parser->lexer);
17021 if (token->type == CPP_SCOPE)
17024 = chainon (sel_seq,
17025 build_tree_list (selector, NULL_TREE));
17027 = chainon (sel_seq,
17028 build_tree_list (NULL_TREE, NULL_TREE));
17032 = chainon (sel_seq,
17033 build_tree_list (selector, NULL_TREE));
17035 token = cp_lexer_peek_token (parser->lexer);
17039 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17041 return objc_build_selector_expr (sel_seq);
17044 /* Parse a list of identifiers.
17046 objc-identifier-list:
17048 objc-identifier-list , identifier
17050 Returns a TREE_LIST of identifier nodes. */
17053 cp_parser_objc_identifier_list (cp_parser* parser)
17055 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17056 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17058 while (sep->type == CPP_COMMA)
17060 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17061 list = chainon (list,
17062 build_tree_list (NULL_TREE,
17063 cp_parser_identifier (parser)));
17064 sep = cp_lexer_peek_token (parser->lexer);
17070 /* Parse an Objective-C alias declaration.
17072 objc-alias-declaration:
17073 @compatibility_alias identifier identifier ;
17075 This function registers the alias mapping with the Objective-C front-end.
17076 It returns nothing. */
17079 cp_parser_objc_alias_declaration (cp_parser* parser)
17083 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17084 alias = cp_parser_identifier (parser);
17085 orig = cp_parser_identifier (parser);
17086 objc_declare_alias (alias, orig);
17087 cp_parser_consume_semicolon_at_end_of_statement (parser);
17090 /* Parse an Objective-C class forward-declaration.
17092 objc-class-declaration:
17093 @class objc-identifier-list ;
17095 The function registers the forward declarations with the Objective-C
17096 front-end. It returns nothing. */
17099 cp_parser_objc_class_declaration (cp_parser* parser)
17101 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17102 objc_declare_class (cp_parser_objc_identifier_list (parser));
17103 cp_parser_consume_semicolon_at_end_of_statement (parser);
17106 /* Parse a list of Objective-C protocol references.
17108 objc-protocol-refs-opt:
17109 objc-protocol-refs [opt]
17111 objc-protocol-refs:
17112 < objc-identifier-list >
17114 Returns a TREE_LIST of identifiers, if any. */
17117 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17119 tree protorefs = NULL_TREE;
17121 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17123 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17124 protorefs = cp_parser_objc_identifier_list (parser);
17125 cp_parser_require (parser, CPP_GREATER, "`>'");
17131 /* Parse a Objective-C visibility specification. */
17134 cp_parser_objc_visibility_spec (cp_parser* parser)
17136 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17138 switch (vis->keyword)
17140 case RID_AT_PRIVATE:
17141 objc_set_visibility (2);
17143 case RID_AT_PROTECTED:
17144 objc_set_visibility (0);
17146 case RID_AT_PUBLIC:
17147 objc_set_visibility (1);
17153 /* Eat '@private'/'@protected'/'@public'. */
17154 cp_lexer_consume_token (parser->lexer);
17157 /* Parse an Objective-C method type. */
17160 cp_parser_objc_method_type (cp_parser* parser)
17162 objc_set_method_type
17163 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17168 /* Parse an Objective-C protocol qualifier. */
17171 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17173 tree quals = NULL_TREE, node;
17174 cp_token *token = cp_lexer_peek_token (parser->lexer);
17176 node = token->value;
17178 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17179 && (node == ridpointers [(int) RID_IN]
17180 || node == ridpointers [(int) RID_OUT]
17181 || node == ridpointers [(int) RID_INOUT]
17182 || node == ridpointers [(int) RID_BYCOPY]
17183 || node == ridpointers [(int) RID_BYREF]
17184 || node == ridpointers [(int) RID_ONEWAY]))
17186 quals = tree_cons (NULL_TREE, node, quals);
17187 cp_lexer_consume_token (parser->lexer);
17188 token = cp_lexer_peek_token (parser->lexer);
17189 node = token->value;
17195 /* Parse an Objective-C typename. */
17198 cp_parser_objc_typename (cp_parser* parser)
17200 tree typename = NULL_TREE;
17202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17204 tree proto_quals, cp_type = NULL_TREE;
17206 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17207 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17209 /* An ObjC type name may consist of just protocol qualifiers, in which
17210 case the type shall default to 'id'. */
17211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17212 cp_type = cp_parser_type_id (parser);
17214 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17215 typename = build_tree_list (proto_quals, cp_type);
17221 /* Check to see if TYPE refers to an Objective-C selector name. */
17224 cp_parser_objc_selector_p (enum cpp_ttype type)
17226 return (type == CPP_NAME || type == CPP_KEYWORD
17227 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17228 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17229 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17230 || type == CPP_XOR || type == CPP_XOR_EQ);
17233 /* Parse an Objective-C selector. */
17236 cp_parser_objc_selector (cp_parser* parser)
17238 cp_token *token = cp_lexer_consume_token (parser->lexer);
17240 if (!cp_parser_objc_selector_p (token->type))
17242 error ("invalid Objective-C++ selector name");
17243 return error_mark_node;
17246 /* C++ operator names are allowed to appear in ObjC selectors. */
17247 switch (token->type)
17249 case CPP_AND_AND: return get_identifier ("and");
17250 case CPP_AND_EQ: return get_identifier ("and_eq");
17251 case CPP_AND: return get_identifier ("bitand");
17252 case CPP_OR: return get_identifier ("bitor");
17253 case CPP_COMPL: return get_identifier ("compl");
17254 case CPP_NOT: return get_identifier ("not");
17255 case CPP_NOT_EQ: return get_identifier ("not_eq");
17256 case CPP_OR_OR: return get_identifier ("or");
17257 case CPP_OR_EQ: return get_identifier ("or_eq");
17258 case CPP_XOR: return get_identifier ("xor");
17259 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17260 default: return token->value;
17264 /* Parse an Objective-C params list. */
17267 cp_parser_objc_method_keyword_params (cp_parser* parser)
17269 tree params = NULL_TREE;
17270 bool maybe_unary_selector_p = true;
17271 cp_token *token = cp_lexer_peek_token (parser->lexer);
17273 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17275 tree selector = NULL_TREE, typename, identifier;
17277 if (token->type != CPP_COLON)
17278 selector = cp_parser_objc_selector (parser);
17280 /* Detect if we have a unary selector. */
17281 if (maybe_unary_selector_p
17282 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17285 maybe_unary_selector_p = false;
17286 cp_parser_require (parser, CPP_COLON, "`:'");
17287 typename = cp_parser_objc_typename (parser);
17288 identifier = cp_parser_identifier (parser);
17292 objc_build_keyword_decl (selector,
17296 token = cp_lexer_peek_token (parser->lexer);
17302 /* Parse the non-keyword Objective-C params. */
17305 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17307 tree params = make_node (TREE_LIST);
17308 cp_token *token = cp_lexer_peek_token (parser->lexer);
17309 *ellipsisp = false; /* Initially, assume no ellipsis. */
17311 while (token->type == CPP_COMMA)
17313 cp_parameter_declarator *parmdecl;
17316 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17317 token = cp_lexer_peek_token (parser->lexer);
17319 if (token->type == CPP_ELLIPSIS)
17321 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17326 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17327 parm = grokdeclarator (parmdecl->declarator,
17328 &parmdecl->decl_specifiers,
17329 PARM, /*initialized=*/0,
17330 /*attrlist=*/NULL);
17332 chainon (params, build_tree_list (NULL_TREE, parm));
17333 token = cp_lexer_peek_token (parser->lexer);
17339 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17342 cp_parser_objc_interstitial_code (cp_parser* parser)
17344 cp_token *token = cp_lexer_peek_token (parser->lexer);
17346 /* If the next token is `extern' and the following token is a string
17347 literal, then we have a linkage specification. */
17348 if (token->keyword == RID_EXTERN
17349 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17350 cp_parser_linkage_specification (parser);
17351 /* Handle #pragma, if any. */
17352 else if (token->type == CPP_PRAGMA)
17353 cp_parser_pragma (parser, pragma_external);
17354 /* Allow stray semicolons. */
17355 else if (token->type == CPP_SEMICOLON)
17356 cp_lexer_consume_token (parser->lexer);
17357 /* Finally, try to parse a block-declaration, or a function-definition. */
17359 cp_parser_block_declaration (parser, /*statement_p=*/false);
17362 /* Parse a method signature. */
17365 cp_parser_objc_method_signature (cp_parser* parser)
17367 tree rettype, kwdparms, optparms;
17368 bool ellipsis = false;
17370 cp_parser_objc_method_type (parser);
17371 rettype = cp_parser_objc_typename (parser);
17372 kwdparms = cp_parser_objc_method_keyword_params (parser);
17373 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17375 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17378 /* Pars an Objective-C method prototype list. */
17381 cp_parser_objc_method_prototype_list (cp_parser* parser)
17383 cp_token *token = cp_lexer_peek_token (parser->lexer);
17385 while (token->keyword != RID_AT_END)
17387 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17389 objc_add_method_declaration
17390 (cp_parser_objc_method_signature (parser));
17391 cp_parser_consume_semicolon_at_end_of_statement (parser);
17394 /* Allow for interspersed non-ObjC++ code. */
17395 cp_parser_objc_interstitial_code (parser);
17397 token = cp_lexer_peek_token (parser->lexer);
17400 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17401 objc_finish_interface ();
17404 /* Parse an Objective-C method definition list. */
17407 cp_parser_objc_method_definition_list (cp_parser* parser)
17409 cp_token *token = cp_lexer_peek_token (parser->lexer);
17411 while (token->keyword != RID_AT_END)
17415 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17417 push_deferring_access_checks (dk_deferred);
17418 objc_start_method_definition
17419 (cp_parser_objc_method_signature (parser));
17421 /* For historical reasons, we accept an optional semicolon. */
17422 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17423 cp_lexer_consume_token (parser->lexer);
17425 perform_deferred_access_checks ();
17426 stop_deferring_access_checks ();
17427 meth = cp_parser_function_definition_after_declarator (parser,
17429 pop_deferring_access_checks ();
17430 objc_finish_method_definition (meth);
17433 /* Allow for interspersed non-ObjC++ code. */
17434 cp_parser_objc_interstitial_code (parser);
17436 token = cp_lexer_peek_token (parser->lexer);
17439 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17440 objc_finish_implementation ();
17443 /* Parse Objective-C ivars. */
17446 cp_parser_objc_class_ivars (cp_parser* parser)
17448 cp_token *token = cp_lexer_peek_token (parser->lexer);
17450 if (token->type != CPP_OPEN_BRACE)
17451 return; /* No ivars specified. */
17453 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17454 token = cp_lexer_peek_token (parser->lexer);
17456 while (token->type != CPP_CLOSE_BRACE)
17458 cp_decl_specifier_seq declspecs;
17459 int decl_class_or_enum_p;
17460 tree prefix_attributes;
17462 cp_parser_objc_visibility_spec (parser);
17464 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17467 cp_parser_decl_specifier_seq (parser,
17468 CP_PARSER_FLAGS_OPTIONAL,
17470 &decl_class_or_enum_p);
17471 prefix_attributes = declspecs.attributes;
17472 declspecs.attributes = NULL_TREE;
17474 /* Keep going until we hit the `;' at the end of the
17476 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17478 tree width = NULL_TREE, attributes, first_attribute, decl;
17479 cp_declarator *declarator = NULL;
17480 int ctor_dtor_or_conv_p;
17482 /* Check for a (possibly unnamed) bitfield declaration. */
17483 token = cp_lexer_peek_token (parser->lexer);
17484 if (token->type == CPP_COLON)
17487 if (token->type == CPP_NAME
17488 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17491 /* Get the name of the bitfield. */
17492 declarator = make_id_declarator (NULL_TREE,
17493 cp_parser_identifier (parser),
17497 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17498 /* Get the width of the bitfield. */
17500 = cp_parser_constant_expression (parser,
17501 /*allow_non_constant=*/false,
17506 /* Parse the declarator. */
17508 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17509 &ctor_dtor_or_conv_p,
17510 /*parenthesized_p=*/NULL,
17511 /*member_p=*/false);
17514 /* Look for attributes that apply to the ivar. */
17515 attributes = cp_parser_attributes_opt (parser);
17516 /* Remember which attributes are prefix attributes and
17518 first_attribute = attributes;
17519 /* Combine the attributes. */
17520 attributes = chainon (prefix_attributes, attributes);
17524 /* Create the bitfield declaration. */
17525 decl = grokbitfield (declarator, &declspecs, width);
17526 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17529 decl = grokfield (declarator, &declspecs,
17530 NULL_TREE, /*init_const_expr_p=*/false,
17531 NULL_TREE, attributes);
17533 /* Add the instance variable. */
17534 objc_add_instance_variable (decl);
17536 /* Reset PREFIX_ATTRIBUTES. */
17537 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17538 attributes = TREE_CHAIN (attributes);
17540 TREE_CHAIN (attributes) = NULL_TREE;
17542 token = cp_lexer_peek_token (parser->lexer);
17544 if (token->type == CPP_COMMA)
17546 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17552 cp_parser_consume_semicolon_at_end_of_statement (parser);
17553 token = cp_lexer_peek_token (parser->lexer);
17556 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17557 /* For historical reasons, we accept an optional semicolon. */
17558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17559 cp_lexer_consume_token (parser->lexer);
17562 /* Parse an Objective-C protocol declaration. */
17565 cp_parser_objc_protocol_declaration (cp_parser* parser)
17567 tree proto, protorefs;
17570 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17573 error ("identifier expected after %<@protocol%>");
17577 /* See if we have a forward declaration or a definition. */
17578 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17580 /* Try a forward declaration first. */
17581 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17583 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17585 cp_parser_consume_semicolon_at_end_of_statement (parser);
17588 /* Ok, we got a full-fledged definition (or at least should). */
17591 proto = cp_parser_identifier (parser);
17592 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17593 objc_start_protocol (proto, protorefs);
17594 cp_parser_objc_method_prototype_list (parser);
17598 /* Parse an Objective-C superclass or category. */
17601 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17604 cp_token *next = cp_lexer_peek_token (parser->lexer);
17606 *super = *categ = NULL_TREE;
17607 if (next->type == CPP_COLON)
17609 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17610 *super = cp_parser_identifier (parser);
17612 else if (next->type == CPP_OPEN_PAREN)
17614 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17615 *categ = cp_parser_identifier (parser);
17616 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17620 /* Parse an Objective-C class interface. */
17623 cp_parser_objc_class_interface (cp_parser* parser)
17625 tree name, super, categ, protos;
17627 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17628 name = cp_parser_identifier (parser);
17629 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17630 protos = cp_parser_objc_protocol_refs_opt (parser);
17632 /* We have either a class or a category on our hands. */
17634 objc_start_category_interface (name, categ, protos);
17637 objc_start_class_interface (name, super, protos);
17638 /* Handle instance variable declarations, if any. */
17639 cp_parser_objc_class_ivars (parser);
17640 objc_continue_interface ();
17643 cp_parser_objc_method_prototype_list (parser);
17646 /* Parse an Objective-C class implementation. */
17649 cp_parser_objc_class_implementation (cp_parser* parser)
17651 tree name, super, categ;
17653 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17654 name = cp_parser_identifier (parser);
17655 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17657 /* We have either a class or a category on our hands. */
17659 objc_start_category_implementation (name, categ);
17662 objc_start_class_implementation (name, super);
17663 /* Handle instance variable declarations, if any. */
17664 cp_parser_objc_class_ivars (parser);
17665 objc_continue_implementation ();
17668 cp_parser_objc_method_definition_list (parser);
17671 /* Consume the @end token and finish off the implementation. */
17674 cp_parser_objc_end_implementation (cp_parser* parser)
17676 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17677 objc_finish_implementation ();
17680 /* Parse an Objective-C declaration. */
17683 cp_parser_objc_declaration (cp_parser* parser)
17685 /* Try to figure out what kind of declaration is present. */
17686 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17688 switch (kwd->keyword)
17691 cp_parser_objc_alias_declaration (parser);
17694 cp_parser_objc_class_declaration (parser);
17696 case RID_AT_PROTOCOL:
17697 cp_parser_objc_protocol_declaration (parser);
17699 case RID_AT_INTERFACE:
17700 cp_parser_objc_class_interface (parser);
17702 case RID_AT_IMPLEMENTATION:
17703 cp_parser_objc_class_implementation (parser);
17706 cp_parser_objc_end_implementation (parser);
17709 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17710 cp_parser_skip_to_end_of_block_or_statement (parser);
17714 /* Parse an Objective-C try-catch-finally statement.
17716 objc-try-catch-finally-stmt:
17717 @try compound-statement objc-catch-clause-seq [opt]
17718 objc-finally-clause [opt]
17720 objc-catch-clause-seq:
17721 objc-catch-clause objc-catch-clause-seq [opt]
17724 @catch ( exception-declaration ) compound-statement
17726 objc-finally-clause
17727 @finally compound-statement
17729 Returns NULL_TREE. */
17732 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17733 location_t location;
17736 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17737 location = cp_lexer_peek_token (parser->lexer)->location;
17738 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17739 node, lest it get absorbed into the surrounding block. */
17740 stmt = push_stmt_list ();
17741 cp_parser_compound_statement (parser, NULL, false);
17742 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17744 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17746 cp_parameter_declarator *parmdecl;
17749 cp_lexer_consume_token (parser->lexer);
17750 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17751 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17752 parm = grokdeclarator (parmdecl->declarator,
17753 &parmdecl->decl_specifiers,
17754 PARM, /*initialized=*/0,
17755 /*attrlist=*/NULL);
17756 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17757 objc_begin_catch_clause (parm);
17758 cp_parser_compound_statement (parser, NULL, false);
17759 objc_finish_catch_clause ();
17762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17764 cp_lexer_consume_token (parser->lexer);
17765 location = cp_lexer_peek_token (parser->lexer)->location;
17766 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17767 node, lest it get absorbed into the surrounding block. */
17768 stmt = push_stmt_list ();
17769 cp_parser_compound_statement (parser, NULL, false);
17770 objc_build_finally_clause (location, pop_stmt_list (stmt));
17773 return objc_finish_try_stmt ();
17776 /* Parse an Objective-C synchronized statement.
17778 objc-synchronized-stmt:
17779 @synchronized ( expression ) compound-statement
17781 Returns NULL_TREE. */
17784 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17785 location_t location;
17788 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17790 location = cp_lexer_peek_token (parser->lexer)->location;
17791 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17792 lock = cp_parser_expression (parser, false);
17793 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17795 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17796 node, lest it get absorbed into the surrounding block. */
17797 stmt = push_stmt_list ();
17798 cp_parser_compound_statement (parser, NULL, false);
17800 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17803 /* Parse an Objective-C throw statement.
17806 @throw assignment-expression [opt] ;
17808 Returns a constructed '@throw' statement. */
17811 cp_parser_objc_throw_statement (cp_parser *parser) {
17812 tree expr = NULL_TREE;
17814 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17817 expr = cp_parser_assignment_expression (parser, false);
17819 cp_parser_consume_semicolon_at_end_of_statement (parser);
17821 return objc_build_throw_stmt (expr);
17824 /* Parse an Objective-C statement. */
17827 cp_parser_objc_statement (cp_parser * parser) {
17828 /* Try to figure out what kind of declaration is present. */
17829 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17831 switch (kwd->keyword)
17834 return cp_parser_objc_try_catch_finally_statement (parser);
17835 case RID_AT_SYNCHRONIZED:
17836 return cp_parser_objc_synchronized_statement (parser);
17838 return cp_parser_objc_throw_statement (parser);
17840 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17841 cp_parser_skip_to_end_of_block_or_statement (parser);
17844 return error_mark_node;
17847 /* OpenMP 2.5 parsing routines. */
17849 /* All OpenMP clauses. OpenMP 2.5. */
17850 typedef enum pragma_omp_clause {
17851 PRAGMA_OMP_CLAUSE_NONE = 0,
17853 PRAGMA_OMP_CLAUSE_COPYIN,
17854 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
17855 PRAGMA_OMP_CLAUSE_DEFAULT,
17856 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
17857 PRAGMA_OMP_CLAUSE_IF,
17858 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
17859 PRAGMA_OMP_CLAUSE_NOWAIT,
17860 PRAGMA_OMP_CLAUSE_NUM_THREADS,
17861 PRAGMA_OMP_CLAUSE_ORDERED,
17862 PRAGMA_OMP_CLAUSE_PRIVATE,
17863 PRAGMA_OMP_CLAUSE_REDUCTION,
17864 PRAGMA_OMP_CLAUSE_SCHEDULE,
17865 PRAGMA_OMP_CLAUSE_SHARED
17866 } pragma_omp_clause;
17868 /* Returns name of the next clause.
17869 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
17870 the token is not consumed. Otherwise appropriate pragma_omp_clause is
17871 returned and the token is consumed. */
17873 static pragma_omp_clause
17874 cp_parser_omp_clause_name (cp_parser *parser)
17876 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
17878 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
17879 result = PRAGMA_OMP_CLAUSE_IF;
17880 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
17881 result = PRAGMA_OMP_CLAUSE_DEFAULT;
17882 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
17883 result = PRAGMA_OMP_CLAUSE_PRIVATE;
17884 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17886 tree id = cp_lexer_peek_token (parser->lexer)->value;
17887 const char *p = IDENTIFIER_POINTER (id);
17892 if (!strcmp ("copyin", p))
17893 result = PRAGMA_OMP_CLAUSE_COPYIN;
17894 else if (!strcmp ("copyprivate", p))
17895 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
17898 if (!strcmp ("firstprivate", p))
17899 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
17902 if (!strcmp ("lastprivate", p))
17903 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
17906 if (!strcmp ("nowait", p))
17907 result = PRAGMA_OMP_CLAUSE_NOWAIT;
17908 else if (!strcmp ("num_threads", p))
17909 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
17912 if (!strcmp ("ordered", p))
17913 result = PRAGMA_OMP_CLAUSE_ORDERED;
17916 if (!strcmp ("reduction", p))
17917 result = PRAGMA_OMP_CLAUSE_REDUCTION;
17920 if (!strcmp ("schedule", p))
17921 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
17922 else if (!strcmp ("shared", p))
17923 result = PRAGMA_OMP_CLAUSE_SHARED;
17928 if (result != PRAGMA_OMP_CLAUSE_NONE)
17929 cp_lexer_consume_token (parser->lexer);
17934 /* Validate that a clause of the given type does not already exist. */
17937 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
17941 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
17942 if (OMP_CLAUSE_CODE (c) == code)
17944 error ("too many %qs clauses", name);
17952 variable-list , identifier
17954 In addition, we match a closing parenthesis. An opening parenthesis
17955 will have been consumed by the caller.
17957 If KIND is nonzero, create the appropriate node and install the decl
17958 in OMP_CLAUSE_DECL and add the node to the head of the list.
17960 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
17961 return the list created. */
17964 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
17971 name = cp_parser_id_expression (parser, /*template_p=*/false,
17972 /*check_dependency_p=*/true,
17973 /*template_p=*/NULL,
17974 /*declarator_p=*/false,
17975 /*optional_p=*/false,
17976 /*member_p=*/false);
17977 if (name == error_mark_node)
17980 decl = cp_parser_lookup_name_simple (parser, name);
17981 if (decl == error_mark_node)
17982 cp_parser_name_lookup_error (parser, name, decl, NULL);
17983 else if (kind != 0)
17985 tree u = build_omp_clause (kind);
17986 OMP_CLAUSE_DECL (u) = decl;
17987 OMP_CLAUSE_CHAIN (u) = list;
17991 list = tree_cons (decl, NULL_TREE, list);
17994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17996 cp_lexer_consume_token (parser->lexer);
17999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18003 /* Try to resync to an unnested comma. Copied from
18004 cp_parser_parenthesized_expression_list. */
18006 ending = cp_parser_skip_to_closing_parenthesis (parser,
18007 /*recovering=*/true,
18009 /*consume_paren=*/true);
18017 /* Similarly, but expect leading and trailing parenthesis. This is a very
18018 common case for omp clauses. */
18021 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18023 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18024 return cp_parser_omp_var_list_no_open (parser, kind, list);
18029 default ( shared | none ) */
18032 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18034 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18041 tree id = cp_lexer_peek_token (parser->lexer)->value;
18042 const char *p = IDENTIFIER_POINTER (id);
18047 if (strcmp ("none", p) != 0)
18049 kind = OMP_CLAUSE_DEFAULT_NONE;
18053 if (strcmp ("shared", p) != 0)
18055 kind = OMP_CLAUSE_DEFAULT_SHARED;
18062 cp_lexer_consume_token (parser->lexer);
18067 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18072 /*or_comma=*/false,
18073 /*consume_paren=*/true);
18075 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18078 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18079 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18080 OMP_CLAUSE_CHAIN (c) = list;
18081 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18087 if ( expression ) */
18090 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18097 t = cp_parser_condition (parser);
18099 if (t == error_mark_node
18100 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18101 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18102 /*or_comma=*/false,
18103 /*consume_paren=*/true);
18105 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18107 c = build_omp_clause (OMP_CLAUSE_IF);
18108 OMP_CLAUSE_IF_EXPR (c) = t;
18109 OMP_CLAUSE_CHAIN (c) = list;
18118 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18122 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18124 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18125 OMP_CLAUSE_CHAIN (c) = list;
18130 num_threads ( expression ) */
18133 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18137 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18140 t = cp_parser_expression (parser, false);
18142 if (t == error_mark_node
18143 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18145 /*or_comma=*/false,
18146 /*consume_paren=*/true);
18148 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18150 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18151 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18152 OMP_CLAUSE_CHAIN (c) = list;
18161 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18165 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18167 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18168 OMP_CLAUSE_CHAIN (c) = list;
18173 reduction ( reduction-operator : variable-list )
18175 reduction-operator:
18176 One of: + * - & ^ | && || */
18179 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18181 enum tree_code code;
18184 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18187 switch (cp_lexer_peek_token (parser->lexer)->type)
18199 code = BIT_AND_EXPR;
18202 code = BIT_XOR_EXPR;
18205 code = BIT_IOR_EXPR;
18208 code = TRUTH_ANDIF_EXPR;
18211 code = TRUTH_ORIF_EXPR;
18214 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18217 /*or_comma=*/false,
18218 /*consume_paren=*/true);
18221 cp_lexer_consume_token (parser->lexer);
18223 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18226 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18227 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18228 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18234 schedule ( schedule-kind )
18235 schedule ( schedule-kind , expression )
18238 static | dynamic | guided | runtime */
18241 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18245 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18248 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18250 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18252 tree id = cp_lexer_peek_token (parser->lexer)->value;
18253 const char *p = IDENTIFIER_POINTER (id);
18258 if (strcmp ("dynamic", p) != 0)
18260 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18264 if (strcmp ("guided", p) != 0)
18266 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18270 if (strcmp ("runtime", p) != 0)
18272 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18279 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18280 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18283 cp_lexer_consume_token (parser->lexer);
18285 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18287 cp_lexer_consume_token (parser->lexer);
18289 t = cp_parser_assignment_expression (parser, false);
18291 if (t == error_mark_node)
18293 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18294 error ("schedule %<runtime%> does not take "
18295 "a %<chunk_size%> parameter");
18297 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18302 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18305 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18306 OMP_CLAUSE_CHAIN (c) = list;
18310 cp_parser_error (parser, "invalid schedule kind");
18312 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18313 /*or_comma=*/false,
18314 /*consume_paren=*/true);
18318 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18319 is a bitmask in MASK. Return the list of clauses found; the result
18320 of clause default goes in *pdefault. */
18323 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18324 const char *where, cp_token *pragma_tok)
18326 tree clauses = NULL;
18328 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18330 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18331 const char *c_name;
18332 tree prev = clauses;
18336 case PRAGMA_OMP_CLAUSE_COPYIN:
18337 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18340 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18341 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18343 c_name = "copyprivate";
18345 case PRAGMA_OMP_CLAUSE_DEFAULT:
18346 clauses = cp_parser_omp_clause_default (parser, clauses);
18347 c_name = "default";
18349 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18352 c_name = "firstprivate";
18354 case PRAGMA_OMP_CLAUSE_IF:
18355 clauses = cp_parser_omp_clause_if (parser, clauses);
18358 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18361 c_name = "lastprivate";
18363 case PRAGMA_OMP_CLAUSE_NOWAIT:
18364 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18367 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18368 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18369 c_name = "num_threads";
18371 case PRAGMA_OMP_CLAUSE_ORDERED:
18372 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18373 c_name = "ordered";
18375 case PRAGMA_OMP_CLAUSE_PRIVATE:
18376 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18378 c_name = "private";
18380 case PRAGMA_OMP_CLAUSE_REDUCTION:
18381 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18382 c_name = "reduction";
18384 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18385 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18386 c_name = "schedule";
18388 case PRAGMA_OMP_CLAUSE_SHARED:
18389 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18394 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18398 if (((mask >> c_kind) & 1) == 0)
18400 /* Remove the invalid clause(s) from the list to avoid
18401 confusing the rest of the compiler. */
18403 error ("%qs is not valid for %qs", c_name, where);
18407 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18408 return finish_omp_clauses (clauses);
18415 In practice, we're also interested in adding the statement to an
18416 outer node. So it is convenient if we work around the fact that
18417 cp_parser_statement calls add_stmt. */
18420 cp_parser_begin_omp_structured_block (cp_parser *parser)
18422 unsigned save = parser->in_statement;
18424 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18425 This preserves the "not within loop or switch" style error messages
18426 for nonsense cases like
18432 if (parser->in_statement)
18433 parser->in_statement = IN_OMP_BLOCK;
18439 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18441 parser->in_statement = save;
18445 cp_parser_omp_structured_block (cp_parser *parser)
18447 tree stmt = begin_omp_structured_block ();
18448 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18450 cp_parser_statement (parser, NULL_TREE, false);
18452 cp_parser_end_omp_structured_block (parser, save);
18453 return finish_omp_structured_block (stmt);
18457 # pragma omp atomic new-line
18461 x binop= expr | x++ | ++x | x-- | --x
18463 +, *, -, /, &, ^, |, <<, >>
18465 where x is an lvalue expression with scalar type. */
18468 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18471 enum tree_code code;
18473 cp_parser_require_pragma_eol (parser, pragma_tok);
18475 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18477 switch (TREE_CODE (lhs))
18482 case PREINCREMENT_EXPR:
18483 case POSTINCREMENT_EXPR:
18484 lhs = TREE_OPERAND (lhs, 0);
18486 rhs = integer_one_node;
18489 case PREDECREMENT_EXPR:
18490 case POSTDECREMENT_EXPR:
18491 lhs = TREE_OPERAND (lhs, 0);
18493 rhs = integer_one_node;
18497 switch (cp_lexer_peek_token (parser->lexer)->type)
18503 code = TRUNC_DIV_EXPR;
18511 case CPP_LSHIFT_EQ:
18512 code = LSHIFT_EXPR;
18514 case CPP_RSHIFT_EQ:
18515 code = RSHIFT_EXPR;
18518 code = BIT_AND_EXPR;
18521 code = BIT_IOR_EXPR;
18524 code = BIT_XOR_EXPR;
18527 cp_parser_error (parser,
18528 "invalid operator for %<#pragma omp atomic%>");
18531 cp_lexer_consume_token (parser->lexer);
18533 rhs = cp_parser_expression (parser, false);
18534 if (rhs == error_mark_node)
18538 finish_omp_atomic (code, lhs, rhs);
18539 cp_parser_consume_semicolon_at_end_of_statement (parser);
18543 cp_parser_skip_to_end_of_block_or_statement (parser);
18548 # pragma omp barrier new-line */
18551 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18553 cp_parser_require_pragma_eol (parser, pragma_tok);
18554 finish_omp_barrier ();
18558 # pragma omp critical [(name)] new-line
18559 structured-block */
18562 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18564 tree stmt, name = NULL;
18566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18568 cp_lexer_consume_token (parser->lexer);
18570 name = cp_parser_identifier (parser);
18572 if (name == error_mark_node
18573 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18575 /*or_comma=*/false,
18576 /*consume_paren=*/true);
18577 if (name == error_mark_node)
18580 cp_parser_require_pragma_eol (parser, pragma_tok);
18582 stmt = cp_parser_omp_structured_block (parser);
18583 return c_finish_omp_critical (stmt, name);
18587 # pragma omp flush flush-vars[opt] new-line
18590 ( variable-list ) */
18593 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18596 (void) cp_parser_omp_var_list (parser, 0, NULL);
18597 cp_parser_require_pragma_eol (parser, pragma_tok);
18599 finish_omp_flush ();
18602 /* Parse the restricted form of the for statment allowed by OpenMP. */
18605 cp_parser_omp_for_loop (cp_parser *parser)
18607 tree init, cond, incr, body, decl, pre_body;
18610 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18612 cp_parser_error (parser, "for statement expected");
18615 loc = cp_lexer_consume_token (parser->lexer)->location;
18616 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18619 init = decl = NULL;
18620 pre_body = push_stmt_list ();
18621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18623 cp_decl_specifier_seq type_specifiers;
18625 /* First, try to parse as an initialized declaration. See
18626 cp_parser_condition, from whence the bulk of this is copied. */
18628 cp_parser_parse_tentatively (parser);
18629 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18631 if (!cp_parser_error_occurred (parser))
18633 tree asm_specification, attributes;
18634 cp_declarator *declarator;
18636 declarator = cp_parser_declarator (parser,
18637 CP_PARSER_DECLARATOR_NAMED,
18638 /*ctor_dtor_or_conv_p=*/NULL,
18639 /*parenthesized_p=*/NULL,
18640 /*member_p=*/false);
18641 attributes = cp_parser_attributes_opt (parser);
18642 asm_specification = cp_parser_asm_specification_opt (parser);
18644 cp_parser_require (parser, CPP_EQ, "`='");
18645 if (cp_parser_parse_definitely (parser))
18649 decl = start_decl (declarator, &type_specifiers,
18650 /*initialized_p=*/false, attributes,
18651 /*prefix_attributes=*/NULL_TREE,
18654 init = cp_parser_assignment_expression (parser, false);
18656 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18657 asm_specification, LOOKUP_ONLYCONVERTING);
18660 pop_scope (pushed_scope);
18664 cp_parser_abort_tentative_parse (parser);
18666 /* If parsing as an initialized declaration failed, try again as
18667 a simple expression. */
18669 init = cp_parser_expression (parser, false);
18671 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18672 pre_body = pop_stmt_list (pre_body);
18675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18676 cond = cp_parser_condition (parser);
18677 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18681 incr = cp_parser_expression (parser, false);
18683 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18685 /*or_comma=*/false,
18686 /*consume_paren=*/true);
18688 /* Note that we saved the original contents of this flag when we entered
18689 the structured block, and so we don't need to re-save it here. */
18690 parser->in_statement = IN_OMP_FOR;
18692 /* Note that the grammar doesn't call for a structured block here,
18693 though the loop as a whole is a structured block. */
18694 body = push_stmt_list ();
18695 cp_parser_statement (parser, NULL_TREE, false);
18696 body = pop_stmt_list (body);
18698 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18702 #pragma omp for for-clause[optseq] new-line
18705 #define OMP_FOR_CLAUSE_MASK \
18706 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18707 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18708 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18709 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18710 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18711 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18712 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18715 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18717 tree clauses, sb, ret;
18720 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18721 "#pragma omp for", pragma_tok);
18723 sb = begin_omp_structured_block ();
18724 save = cp_parser_begin_omp_structured_block (parser);
18726 ret = cp_parser_omp_for_loop (parser);
18728 OMP_FOR_CLAUSES (ret) = clauses;
18730 cp_parser_end_omp_structured_block (parser, save);
18731 add_stmt (finish_omp_structured_block (sb));
18737 # pragma omp master new-line
18738 structured-block */
18741 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18743 cp_parser_require_pragma_eol (parser, pragma_tok);
18744 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18748 # pragma omp ordered new-line
18749 structured-block */
18752 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18754 cp_parser_require_pragma_eol (parser, pragma_tok);
18755 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18761 { section-sequence }
18764 section-directive[opt] structured-block
18765 section-sequence section-directive structured-block */
18768 cp_parser_omp_sections_scope (cp_parser *parser)
18770 tree stmt, substmt;
18771 bool error_suppress = false;
18774 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18777 stmt = push_stmt_list ();
18779 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18783 substmt = begin_omp_structured_block ();
18784 save = cp_parser_begin_omp_structured_block (parser);
18788 cp_parser_statement (parser, NULL_TREE, false);
18790 tok = cp_lexer_peek_token (parser->lexer);
18791 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18793 if (tok->type == CPP_CLOSE_BRACE)
18795 if (tok->type == CPP_EOF)
18799 cp_parser_end_omp_structured_block (parser, save);
18800 substmt = finish_omp_structured_block (substmt);
18801 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18802 add_stmt (substmt);
18807 tok = cp_lexer_peek_token (parser->lexer);
18808 if (tok->type == CPP_CLOSE_BRACE)
18810 if (tok->type == CPP_EOF)
18813 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18815 cp_lexer_consume_token (parser->lexer);
18816 cp_parser_require_pragma_eol (parser, tok);
18817 error_suppress = false;
18819 else if (!error_suppress)
18821 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
18822 error_suppress = true;
18825 substmt = cp_parser_omp_structured_block (parser);
18826 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18827 add_stmt (substmt);
18829 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
18831 substmt = pop_stmt_list (stmt);
18833 stmt = make_node (OMP_SECTIONS);
18834 TREE_TYPE (stmt) = void_type_node;
18835 OMP_SECTIONS_BODY (stmt) = substmt;
18842 # pragma omp sections sections-clause[optseq] newline
18845 #define OMP_SECTIONS_CLAUSE_MASK \
18846 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18847 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18848 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18849 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18850 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18853 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
18857 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
18858 "#pragma omp sections", pragma_tok);
18860 ret = cp_parser_omp_sections_scope (parser);
18862 OMP_SECTIONS_CLAUSES (ret) = clauses;
18868 # pragma parallel parallel-clause new-line
18869 # pragma parallel for parallel-for-clause new-line
18870 # pragma parallel sections parallel-sections-clause new-line */
18872 #define OMP_PARALLEL_CLAUSE_MASK \
18873 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
18874 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18875 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18876 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
18877 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
18878 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
18879 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18880 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
18883 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
18885 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
18886 const char *p_name = "#pragma omp parallel";
18887 tree stmt, clauses, par_clause, ws_clause, block;
18888 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
18891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18893 cp_lexer_consume_token (parser->lexer);
18894 p_kind = PRAGMA_OMP_PARALLEL_FOR;
18895 p_name = "#pragma omp parallel for";
18896 mask |= OMP_FOR_CLAUSE_MASK;
18897 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18899 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18901 tree id = cp_lexer_peek_token (parser->lexer)->value;
18902 const char *p = IDENTIFIER_POINTER (id);
18903 if (strcmp (p, "sections") == 0)
18905 cp_lexer_consume_token (parser->lexer);
18906 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
18907 p_name = "#pragma omp parallel sections";
18908 mask |= OMP_SECTIONS_CLAUSE_MASK;
18909 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
18913 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
18914 block = begin_omp_parallel ();
18915 save = cp_parser_begin_omp_structured_block (parser);
18919 case PRAGMA_OMP_PARALLEL:
18920 cp_parser_already_scoped_statement (parser);
18921 par_clause = clauses;
18924 case PRAGMA_OMP_PARALLEL_FOR:
18925 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18926 stmt = cp_parser_omp_for_loop (parser);
18928 OMP_FOR_CLAUSES (stmt) = ws_clause;
18931 case PRAGMA_OMP_PARALLEL_SECTIONS:
18932 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
18933 stmt = cp_parser_omp_sections_scope (parser);
18935 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
18939 gcc_unreachable ();
18942 cp_parser_end_omp_structured_block (parser, save);
18943 stmt = finish_omp_parallel (par_clause, block);
18944 if (p_kind != PRAGMA_OMP_PARALLEL)
18945 OMP_PARALLEL_COMBINED (stmt) = 1;
18950 # pragma omp single single-clause[optseq] new-line
18951 structured-block */
18953 #define OMP_SINGLE_CLAUSE_MASK \
18954 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18955 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18956 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18957 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18960 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
18962 tree stmt = make_node (OMP_SINGLE);
18963 TREE_TYPE (stmt) = void_type_node;
18965 OMP_SINGLE_CLAUSES (stmt)
18966 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18967 "#pragma omp single", pragma_tok);
18968 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
18970 return add_stmt (stmt);
18974 # pragma omp threadprivate (variable-list) */
18977 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
18981 vars = cp_parser_omp_var_list (parser, 0, NULL);
18982 cp_parser_require_pragma_eol (parser, pragma_tok);
18984 if (!targetm.have_tls)
18985 sorry ("threadprivate variables not supported in this target");
18987 finish_omp_threadprivate (vars);
18990 /* Main entry point to OpenMP statement pragmas. */
18993 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
18997 switch (pragma_tok->pragma_kind)
18999 case PRAGMA_OMP_ATOMIC:
19000 cp_parser_omp_atomic (parser, pragma_tok);
19002 case PRAGMA_OMP_CRITICAL:
19003 stmt = cp_parser_omp_critical (parser, pragma_tok);
19005 case PRAGMA_OMP_FOR:
19006 stmt = cp_parser_omp_for (parser, pragma_tok);
19008 case PRAGMA_OMP_MASTER:
19009 stmt = cp_parser_omp_master (parser, pragma_tok);
19011 case PRAGMA_OMP_ORDERED:
19012 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19014 case PRAGMA_OMP_PARALLEL:
19015 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19017 case PRAGMA_OMP_SECTIONS:
19018 stmt = cp_parser_omp_sections (parser, pragma_tok);
19020 case PRAGMA_OMP_SINGLE:
19021 stmt = cp_parser_omp_single (parser, pragma_tok);
19024 gcc_unreachable ();
19028 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19033 static GTY (()) cp_parser *the_parser;
19036 /* Special handling for the first token or line in the file. The first
19037 thing in the file might be #pragma GCC pch_preprocess, which loads a
19038 PCH file, which is a GC collection point. So we need to handle this
19039 first pragma without benefit of an existing lexer structure.
19041 Always returns one token to the caller in *FIRST_TOKEN. This is
19042 either the true first token of the file, or the first token after
19043 the initial pragma. */
19046 cp_parser_initial_pragma (cp_token *first_token)
19050 cp_lexer_get_preprocessor_token (NULL, first_token);
19051 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19054 cp_lexer_get_preprocessor_token (NULL, first_token);
19055 if (first_token->type == CPP_STRING)
19057 name = first_token->value;
19059 cp_lexer_get_preprocessor_token (NULL, first_token);
19060 if (first_token->type != CPP_PRAGMA_EOL)
19061 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19064 error ("expected string literal");
19066 /* Skip to the end of the pragma. */
19067 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19068 cp_lexer_get_preprocessor_token (NULL, first_token);
19070 /* Now actually load the PCH file. */
19072 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19074 /* Read one more token to return to our caller. We have to do this
19075 after reading the PCH file in, since its pointers have to be
19077 cp_lexer_get_preprocessor_token (NULL, first_token);
19080 /* Normal parsing of a pragma token. Here we can (and must) use the
19084 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19086 cp_token *pragma_tok;
19089 pragma_tok = cp_lexer_consume_token (parser->lexer);
19090 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19091 parser->lexer->in_pragma = true;
19093 id = pragma_tok->pragma_kind;
19096 case PRAGMA_GCC_PCH_PREPROCESS:
19097 error ("%<#pragma GCC pch_preprocess%> must be first");
19100 case PRAGMA_OMP_BARRIER:
19103 case pragma_compound:
19104 cp_parser_omp_barrier (parser, pragma_tok);
19107 error ("%<#pragma omp barrier%> may only be "
19108 "used in compound statements");
19115 case PRAGMA_OMP_FLUSH:
19118 case pragma_compound:
19119 cp_parser_omp_flush (parser, pragma_tok);
19122 error ("%<#pragma omp flush%> may only be "
19123 "used in compound statements");
19130 case PRAGMA_OMP_THREADPRIVATE:
19131 cp_parser_omp_threadprivate (parser, pragma_tok);
19134 case PRAGMA_OMP_ATOMIC:
19135 case PRAGMA_OMP_CRITICAL:
19136 case PRAGMA_OMP_FOR:
19137 case PRAGMA_OMP_MASTER:
19138 case PRAGMA_OMP_ORDERED:
19139 case PRAGMA_OMP_PARALLEL:
19140 case PRAGMA_OMP_SECTIONS:
19141 case PRAGMA_OMP_SINGLE:
19142 if (context == pragma_external)
19144 cp_parser_omp_construct (parser, pragma_tok);
19147 case PRAGMA_OMP_SECTION:
19148 error ("%<#pragma omp section%> may only be used in "
19149 "%<#pragma omp sections%> construct");
19153 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19154 c_invoke_pragma_handler (id);
19158 cp_parser_error (parser, "expected declaration specifiers");
19162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19166 /* The interface the pragma parsers have to the lexer. */
19169 pragma_lex (tree *value)
19172 enum cpp_ttype ret;
19174 tok = cp_lexer_peek_token (the_parser->lexer);
19177 *value = tok->value;
19179 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19181 else if (ret == CPP_STRING)
19182 *value = cp_parser_string_literal (the_parser, false, false);
19185 cp_lexer_consume_token (the_parser->lexer);
19186 if (ret == CPP_KEYWORD)
19194 /* External interface. */
19196 /* Parse one entire translation unit. */
19199 c_parse_file (void)
19201 bool error_occurred;
19202 static bool already_called = false;
19204 if (already_called)
19206 sorry ("inter-module optimizations not implemented for C++");
19209 already_called = true;
19211 the_parser = cp_parser_new ();
19212 push_deferring_access_checks (flag_access_control
19213 ? dk_no_deferred : dk_no_check);
19214 error_occurred = cp_parser_translation_unit (the_parser);
19218 /* This variable must be provided by every front end. */
19222 #include "gt-cp-parser.h"