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 true if the next token is a keyword for a decl-specifier. */
511 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
515 token = cp_lexer_peek_token (lexer);
516 switch (token->keyword)
518 /* Storage classes. */
525 /* Elaborated type specifiers. */
531 /* Simple type specifiers. */
543 /* GNU extensions. */
553 /* Return a pointer to the Nth token in the token stream. If N is 1,
554 then this is precisely equivalent to cp_lexer_peek_token (except
555 that it is not inline). One would like to disallow that case, but
556 there is one case (cp_parser_nth_token_starts_template_id) where
557 the caller passes a variable for N and it might be 1. */
560 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
564 /* N is 1-based, not zero-based. */
567 if (cp_lexer_debugging_p (lexer))
568 fprintf (cp_lexer_debug_stream,
569 "cp_lexer: peeking ahead %ld at token: ", (long)n);
572 token = lexer->next_token;
573 gcc_assert (!n || token != &eof_token);
577 if (token == lexer->last_token)
579 token = (cp_token *)&eof_token;
583 if (token->type != CPP_PURGED)
587 if (cp_lexer_debugging_p (lexer))
589 cp_lexer_print_token (cp_lexer_debug_stream, token);
590 putc ('\n', cp_lexer_debug_stream);
596 /* Return the next token, and advance the lexer's next_token pointer
597 to point to the next non-purged token. */
600 cp_lexer_consume_token (cp_lexer* lexer)
602 cp_token *token = lexer->next_token;
604 gcc_assert (token != &eof_token);
605 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
610 if (lexer->next_token == lexer->last_token)
612 lexer->next_token = (cp_token *)&eof_token;
617 while (lexer->next_token->type == CPP_PURGED);
619 cp_lexer_set_source_position_from_token (token);
621 /* Provide debugging output. */
622 if (cp_lexer_debugging_p (lexer))
624 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
625 cp_lexer_print_token (cp_lexer_debug_stream, token);
626 putc ('\n', cp_lexer_debug_stream);
632 /* Permanently remove the next token from the token stream, and
633 advance the next_token pointer to refer to the next non-purged
637 cp_lexer_purge_token (cp_lexer *lexer)
639 cp_token *tok = lexer->next_token;
641 gcc_assert (tok != &eof_token);
642 tok->type = CPP_PURGED;
643 tok->location = UNKNOWN_LOCATION;
644 tok->value = NULL_TREE;
645 tok->keyword = RID_MAX;
650 if (tok == lexer->last_token)
652 tok = (cp_token *)&eof_token;
656 while (tok->type == CPP_PURGED);
657 lexer->next_token = tok;
660 /* Permanently remove all tokens after TOK, up to, but not
661 including, the token that will be returned next by
662 cp_lexer_peek_token. */
665 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
667 cp_token *peek = lexer->next_token;
669 if (peek == &eof_token)
670 peek = lexer->last_token;
672 gcc_assert (tok < peek);
674 for ( tok += 1; tok != peek; tok += 1)
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->value = NULL_TREE;
679 tok->keyword = RID_MAX;
683 /* Begin saving tokens. All tokens consumed after this point will be
687 cp_lexer_save_tokens (cp_lexer* lexer)
689 /* Provide debugging output. */
690 if (cp_lexer_debugging_p (lexer))
691 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
693 VEC_safe_push (cp_token_position, heap,
694 lexer->saved_tokens, lexer->next_token);
697 /* Commit to the portion of the token stream most recently saved. */
700 cp_lexer_commit_tokens (cp_lexer* lexer)
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
704 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
706 VEC_pop (cp_token_position, lexer->saved_tokens);
709 /* Return all tokens saved since the last call to cp_lexer_save_tokens
710 to the token stream. Stop saving tokens. */
713 cp_lexer_rollback_tokens (cp_lexer* lexer)
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
719 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
722 /* Print a representation of the TOKEN on the STREAM. */
724 #ifdef ENABLE_CHECKING
727 cp_lexer_print_token (FILE * stream, cp_token *token)
729 /* We don't use cpp_type2name here because the parser defines
730 a few tokens of its own. */
731 static const char *const token_names[] = {
732 /* cpplib-defined token types */
738 /* C++ parser token types - see "Manifest constants", above. */
741 "NESTED_NAME_SPECIFIER",
745 /* If we have a name for the token, print it out. Otherwise, we
746 simply give the numeric code. */
747 gcc_assert (token->type < ARRAY_SIZE(token_names));
748 fputs (token_names[token->type], stream);
750 /* For some tokens, print the associated data. */
754 /* Some keywords have a value that is not an IDENTIFIER_NODE.
755 For example, `struct' is mapped to an INTEGER_CST. */
756 if (TREE_CODE (token->value) != IDENTIFIER_NODE)
758 /* else fall through */
760 fputs (IDENTIFIER_POINTER (token->value), stream);
765 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
773 /* Start emitting debugging information. */
776 cp_lexer_start_debugging (cp_lexer* lexer)
778 lexer->debugging_p = true;
781 /* Stop emitting debugging information. */
784 cp_lexer_stop_debugging (cp_lexer* lexer)
786 lexer->debugging_p = false;
789 #endif /* ENABLE_CHECKING */
791 /* Create a new cp_token_cache, representing a range of tokens. */
793 static cp_token_cache *
794 cp_token_cache_new (cp_token *first, cp_token *last)
796 cp_token_cache *cache = GGC_NEW (cp_token_cache);
797 cache->first = first;
803 /* Decl-specifiers. */
805 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
808 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
810 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
815 /* Nothing other than the parser should be creating declarators;
816 declarators are a semi-syntactic representation of C++ entities.
817 Other parts of the front end that need to create entities (like
818 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
820 static cp_declarator *make_call_declarator
821 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
822 static cp_declarator *make_array_declarator
823 (cp_declarator *, tree);
824 static cp_declarator *make_pointer_declarator
825 (cp_cv_quals, cp_declarator *);
826 static cp_declarator *make_reference_declarator
827 (cp_cv_quals, cp_declarator *);
828 static cp_parameter_declarator *make_parameter_declarator
829 (cp_decl_specifier_seq *, cp_declarator *, tree);
830 static cp_declarator *make_ptrmem_declarator
831 (cp_cv_quals, tree, cp_declarator *);
833 /* An erroneous declarator. */
834 static cp_declarator *cp_error_declarator;
836 /* The obstack on which declarators and related data structures are
838 static struct obstack declarator_obstack;
840 /* Alloc BYTES from the declarator memory pool. */
843 alloc_declarator (size_t bytes)
845 return obstack_alloc (&declarator_obstack, bytes);
848 /* Allocate a declarator of the indicated KIND. Clear fields that are
849 common to all declarators. */
851 static cp_declarator *
852 make_declarator (cp_declarator_kind kind)
854 cp_declarator *declarator;
856 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
857 declarator->kind = kind;
858 declarator->attributes = NULL_TREE;
859 declarator->declarator = NULL;
864 /* Make a declarator for a generalized identifier. If
865 QUALIFYING_SCOPE is non-NULL, the identifier is
866 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
867 UNQUALIFIED_NAME. SFK indicates the kind of special function this
870 static cp_declarator *
871 make_id_declarator (tree qualifying_scope, tree unqualified_name,
872 special_function_kind sfk)
874 cp_declarator *declarator;
876 /* It is valid to write:
878 class C { void f(); };
882 The standard is not clear about whether `typedef const C D' is
883 legal; as of 2002-09-15 the committee is considering that
884 question. EDG 3.0 allows that syntax. Therefore, we do as
886 if (qualifying_scope && TYPE_P (qualifying_scope))
887 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
889 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
890 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
891 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
893 declarator = make_declarator (cdk_id);
894 declarator->u.id.qualifying_scope = qualifying_scope;
895 declarator->u.id.unqualified_name = unqualified_name;
896 declarator->u.id.sfk = sfk;
901 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
902 of modifiers such as const or volatile to apply to the pointer
903 type, represented as identifiers. */
906 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
908 cp_declarator *declarator;
910 declarator = make_declarator (cdk_pointer);
911 declarator->declarator = target;
912 declarator->u.pointer.qualifiers = cv_qualifiers;
913 declarator->u.pointer.class_type = NULL_TREE;
918 /* Like make_pointer_declarator -- but for references. */
921 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
923 cp_declarator *declarator;
925 declarator = make_declarator (cdk_reference);
926 declarator->declarator = target;
927 declarator->u.pointer.qualifiers = cv_qualifiers;
928 declarator->u.pointer.class_type = NULL_TREE;
933 /* Like make_pointer_declarator -- but for a pointer to a non-static
934 member of CLASS_TYPE. */
937 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
938 cp_declarator *pointee)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_ptrmem);
943 declarator->declarator = pointee;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = class_type;
950 /* Make a declarator for the function given by TARGET, with the
951 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
952 "const"-qualified member function. The EXCEPTION_SPECIFICATION
953 indicates what exceptions can be thrown. */
956 make_call_declarator (cp_declarator *target,
957 cp_parameter_declarator *parms,
958 cp_cv_quals cv_qualifiers,
959 tree exception_specification)
961 cp_declarator *declarator;
963 declarator = make_declarator (cdk_function);
964 declarator->declarator = target;
965 declarator->u.function.parameters = parms;
966 declarator->u.function.qualifiers = cv_qualifiers;
967 declarator->u.function.exception_specification = exception_specification;
972 /* Make a declarator for an array of BOUNDS elements, each of which is
973 defined by ELEMENT. */
976 make_array_declarator (cp_declarator *element, tree bounds)
978 cp_declarator *declarator;
980 declarator = make_declarator (cdk_array);
981 declarator->declarator = element;
982 declarator->u.array.bounds = bounds;
987 cp_parameter_declarator *no_parameters;
989 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
990 DECLARATOR and DEFAULT_ARGUMENT. */
992 cp_parameter_declarator *
993 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
994 cp_declarator *declarator,
995 tree default_argument)
997 cp_parameter_declarator *parameter;
999 parameter = ((cp_parameter_declarator *)
1000 alloc_declarator (sizeof (cp_parameter_declarator)));
1001 parameter->next = NULL;
1002 if (decl_specifiers)
1003 parameter->decl_specifiers = *decl_specifiers;
1005 clear_decl_specs (¶meter->decl_specifiers);
1006 parameter->declarator = declarator;
1007 parameter->default_argument = default_argument;
1008 parameter->ellipsis_p = false;
1013 /* Returns true iff DECLARATOR is a declaration for a function. */
1016 function_declarator_p (const cp_declarator *declarator)
1020 if (declarator->kind == cdk_function
1021 && declarator->declarator->kind == cdk_id)
1023 if (declarator->kind == cdk_id
1024 || declarator->kind == cdk_error)
1026 declarator = declarator->declarator;
1036 A cp_parser parses the token stream as specified by the C++
1037 grammar. Its job is purely parsing, not semantic analysis. For
1038 example, the parser breaks the token stream into declarators,
1039 expressions, statements, and other similar syntactic constructs.
1040 It does not check that the types of the expressions on either side
1041 of an assignment-statement are compatible, or that a function is
1042 not declared with a parameter of type `void'.
1044 The parser invokes routines elsewhere in the compiler to perform
1045 semantic analysis and to build up the abstract syntax tree for the
1048 The parser (and the template instantiation code, which is, in a
1049 way, a close relative of parsing) are the only parts of the
1050 compiler that should be calling push_scope and pop_scope, or
1051 related functions. The parser (and template instantiation code)
1052 keeps track of what scope is presently active; everything else
1053 should simply honor that. (The code that generates static
1054 initializers may also need to set the scope, in order to check
1055 access control correctly when emitting the initializers.)
1060 The parser is of the standard recursive-descent variety. Upcoming
1061 tokens in the token stream are examined in order to determine which
1062 production to use when parsing a non-terminal. Some C++ constructs
1063 require arbitrary look ahead to disambiguate. For example, it is
1064 impossible, in the general case, to tell whether a statement is an
1065 expression or declaration without scanning the entire statement.
1066 Therefore, the parser is capable of "parsing tentatively." When the
1067 parser is not sure what construct comes next, it enters this mode.
1068 Then, while we attempt to parse the construct, the parser queues up
1069 error messages, rather than issuing them immediately, and saves the
1070 tokens it consumes. If the construct is parsed successfully, the
1071 parser "commits", i.e., it issues any queued error messages and
1072 the tokens that were being preserved are permanently discarded.
1073 If, however, the construct is not parsed successfully, the parser
1074 rolls back its state completely so that it can resume parsing using
1075 a different alternative.
1080 The performance of the parser could probably be improved substantially.
1081 We could often eliminate the need to parse tentatively by looking ahead
1082 a little bit. In some places, this approach might not entirely eliminate
1083 the need to parse tentatively, but it might still speed up the average
1086 /* Flags that are passed to some parsing functions. These values can
1087 be bitwise-ored together. */
1089 typedef enum cp_parser_flags
1092 CP_PARSER_FLAGS_NONE = 0x0,
1093 /* The construct is optional. If it is not present, then no error
1094 should be issued. */
1095 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1096 /* When parsing a type-specifier, do not allow user-defined types. */
1097 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1100 /* The different kinds of declarators we want to parse. */
1102 typedef enum cp_parser_declarator_kind
1104 /* We want an abstract declarator. */
1105 CP_PARSER_DECLARATOR_ABSTRACT,
1106 /* We want a named declarator. */
1107 CP_PARSER_DECLARATOR_NAMED,
1108 /* We don't mind, but the name must be an unqualified-id. */
1109 CP_PARSER_DECLARATOR_EITHER
1110 } cp_parser_declarator_kind;
1112 /* The precedence values used to parse binary expressions. The minimum value
1113 of PREC must be 1, because zero is reserved to quickly discriminate
1114 binary operators from other tokens. */
1119 PREC_LOGICAL_OR_EXPRESSION,
1120 PREC_LOGICAL_AND_EXPRESSION,
1121 PREC_INCLUSIVE_OR_EXPRESSION,
1122 PREC_EXCLUSIVE_OR_EXPRESSION,
1123 PREC_AND_EXPRESSION,
1124 PREC_EQUALITY_EXPRESSION,
1125 PREC_RELATIONAL_EXPRESSION,
1126 PREC_SHIFT_EXPRESSION,
1127 PREC_ADDITIVE_EXPRESSION,
1128 PREC_MULTIPLICATIVE_EXPRESSION,
1130 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1133 /* A mapping from a token type to a corresponding tree node type, with a
1134 precedence value. */
1136 typedef struct cp_parser_binary_operations_map_node
1138 /* The token type. */
1139 enum cpp_ttype token_type;
1140 /* The corresponding tree code. */
1141 enum tree_code tree_type;
1142 /* The precedence of this operator. */
1143 enum cp_parser_prec prec;
1144 } cp_parser_binary_operations_map_node;
1146 /* The status of a tentative parse. */
1148 typedef enum cp_parser_status_kind
1150 /* No errors have occurred. */
1151 CP_PARSER_STATUS_KIND_NO_ERROR,
1152 /* An error has occurred. */
1153 CP_PARSER_STATUS_KIND_ERROR,
1154 /* We are committed to this tentative parse, whether or not an error
1156 CP_PARSER_STATUS_KIND_COMMITTED
1157 } cp_parser_status_kind;
1159 typedef struct cp_parser_expression_stack_entry
1162 enum tree_code tree_type;
1164 } cp_parser_expression_stack_entry;
1166 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1167 entries because precedence levels on the stack are monotonically
1169 typedef struct cp_parser_expression_stack_entry
1170 cp_parser_expression_stack[NUM_PREC_VALUES];
1172 /* Context that is saved and restored when parsing tentatively. */
1173 typedef struct cp_parser_context GTY (())
1175 /* If this is a tentative parsing context, the status of the
1177 enum cp_parser_status_kind status;
1178 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1179 that are looked up in this context must be looked up both in the
1180 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1181 the context of the containing expression. */
1184 /* The next parsing context in the stack. */
1185 struct cp_parser_context *next;
1186 } cp_parser_context;
1190 /* Constructors and destructors. */
1192 static cp_parser_context *cp_parser_context_new
1193 (cp_parser_context *);
1195 /* Class variables. */
1197 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1199 /* The operator-precedence table used by cp_parser_binary_expression.
1200 Transformed into an associative array (binops_by_token) by
1203 static const cp_parser_binary_operations_map_node binops[] = {
1204 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1205 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1207 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1208 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1209 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1211 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1212 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1214 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1215 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1217 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1218 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1219 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1220 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1222 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1223 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1225 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1227 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1229 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1231 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1233 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1236 /* The same as binops, but initialized by cp_parser_new so that
1237 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1239 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1241 /* Constructors and destructors. */
1243 /* Construct a new context. The context below this one on the stack
1244 is given by NEXT. */
1246 static cp_parser_context *
1247 cp_parser_context_new (cp_parser_context* next)
1249 cp_parser_context *context;
1251 /* Allocate the storage. */
1252 if (cp_parser_context_free_list != NULL)
1254 /* Pull the first entry from the free list. */
1255 context = cp_parser_context_free_list;
1256 cp_parser_context_free_list = context->next;
1257 memset (context, 0, sizeof (*context));
1260 context = GGC_CNEW (cp_parser_context);
1262 /* No errors have occurred yet in this context. */
1263 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1264 /* If this is not the bottomost context, copy information that we
1265 need from the previous context. */
1268 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1269 expression, then we are parsing one in this context, too. */
1270 context->object_type = next->object_type;
1271 /* Thread the stack. */
1272 context->next = next;
1278 /* The cp_parser structure represents the C++ parser. */
1280 typedef struct cp_parser GTY(())
1282 /* The lexer from which we are obtaining tokens. */
1285 /* The scope in which names should be looked up. If NULL_TREE, then
1286 we look up names in the scope that is currently open in the
1287 source program. If non-NULL, this is either a TYPE or
1288 NAMESPACE_DECL for the scope in which we should look. It can
1289 also be ERROR_MARK, when we've parsed a bogus scope.
1291 This value is not cleared automatically after a name is looked
1292 up, so we must be careful to clear it before starting a new look
1293 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1294 will look up `Z' in the scope of `X', rather than the current
1295 scope.) Unfortunately, it is difficult to tell when name lookup
1296 is complete, because we sometimes peek at a token, look it up,
1297 and then decide not to consume it. */
1300 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1301 last lookup took place. OBJECT_SCOPE is used if an expression
1302 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1303 respectively. QUALIFYING_SCOPE is used for an expression of the
1304 form "X::Y"; it refers to X. */
1306 tree qualifying_scope;
1308 /* A stack of parsing contexts. All but the bottom entry on the
1309 stack will be tentative contexts.
1311 We parse tentatively in order to determine which construct is in
1312 use in some situations. For example, in order to determine
1313 whether a statement is an expression-statement or a
1314 declaration-statement we parse it tentatively as a
1315 declaration-statement. If that fails, we then reparse the same
1316 token stream as an expression-statement. */
1317 cp_parser_context *context;
1319 /* True if we are parsing GNU C++. If this flag is not set, then
1320 GNU extensions are not recognized. */
1321 bool allow_gnu_extensions_p;
1323 /* TRUE if the `>' token should be interpreted as the greater-than
1324 operator. FALSE if it is the end of a template-id or
1325 template-parameter-list. */
1326 bool greater_than_is_operator_p;
1328 /* TRUE if default arguments are allowed within a parameter list
1329 that starts at this point. FALSE if only a gnu extension makes
1330 them permissible. */
1331 bool default_arg_ok_p;
1333 /* TRUE if we are parsing an integral constant-expression. See
1334 [expr.const] for a precise definition. */
1335 bool integral_constant_expression_p;
1337 /* TRUE if we are parsing an integral constant-expression -- but a
1338 non-constant expression should be permitted as well. This flag
1339 is used when parsing an array bound so that GNU variable-length
1340 arrays are tolerated. */
1341 bool allow_non_integral_constant_expression_p;
1343 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1344 been seen that makes the expression non-constant. */
1345 bool non_integral_constant_expression_p;
1347 /* TRUE if local variable names and `this' are forbidden in the
1349 bool local_variables_forbidden_p;
1351 /* TRUE if the declaration we are parsing is part of a
1352 linkage-specification of the form `extern string-literal
1354 bool in_unbraced_linkage_specification_p;
1356 /* TRUE if we are presently parsing a declarator, after the
1357 direct-declarator. */
1358 bool in_declarator_p;
1360 /* TRUE if we are presently parsing a template-argument-list. */
1361 bool in_template_argument_list_p;
1363 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1364 to IN_OMP_BLOCK if parsing OpenMP structured block and
1365 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1366 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1367 iteration-statement, OpenMP block or loop within that switch. */
1368 #define IN_SWITCH_STMT 1
1369 #define IN_ITERATION_STMT 2
1370 #define IN_OMP_BLOCK 4
1371 #define IN_OMP_FOR 8
1372 unsigned char in_statement;
1374 /* TRUE if we are presently parsing the body of a switch statement.
1375 Note that this doesn't quite overlap with in_statement above.
1376 The difference relates to giving the right sets of error messages:
1377 "case not in switch" vs "break statement used with OpenMP...". */
1378 bool in_switch_statement_p;
1380 /* TRUE if we are parsing a type-id in an expression context. In
1381 such a situation, both "type (expr)" and "type (type)" are valid
1383 bool in_type_id_in_expr_p;
1385 /* TRUE if we are currently in a header file where declarations are
1386 implicitly extern "C". */
1387 bool implicit_extern_c;
1389 /* TRUE if strings in expressions should be translated to the execution
1391 bool translate_strings_p;
1393 /* TRUE if we are presently parsing the body of a function, but not
1395 bool in_function_body;
1397 /* If non-NULL, then we are parsing a construct where new type
1398 definitions are not permitted. The string stored here will be
1399 issued as an error message if a type is defined. */
1400 const char *type_definition_forbidden_message;
1402 /* A list of lists. The outer list is a stack, used for member
1403 functions of local classes. At each level there are two sub-list,
1404 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1405 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1406 TREE_VALUE's. The functions are chained in reverse declaration
1409 The TREE_PURPOSE sublist contains those functions with default
1410 arguments that need post processing, and the TREE_VALUE sublist
1411 contains those functions with definitions that need post
1414 These lists can only be processed once the outermost class being
1415 defined is complete. */
1416 tree unparsed_functions_queues;
1418 /* The number of classes whose definitions are currently in
1420 unsigned num_classes_being_defined;
1422 /* The number of template parameter lists that apply directly to the
1423 current declaration. */
1424 unsigned num_template_parameter_lists;
1429 /* Constructors and destructors. */
1431 static cp_parser *cp_parser_new
1434 /* Routines to parse various constructs.
1436 Those that return `tree' will return the error_mark_node (rather
1437 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1438 Sometimes, they will return an ordinary node if error-recovery was
1439 attempted, even though a parse error occurred. So, to check
1440 whether or not a parse error occurred, you should always use
1441 cp_parser_error_occurred. If the construct is optional (indicated
1442 either by an `_opt' in the name of the function that does the
1443 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1444 the construct is not present. */
1446 /* Lexical conventions [gram.lex] */
1448 static tree cp_parser_identifier
1450 static tree cp_parser_string_literal
1451 (cp_parser *, bool, bool);
1453 /* Basic concepts [gram.basic] */
1455 static bool cp_parser_translation_unit
1458 /* Expressions [gram.expr] */
1460 static tree cp_parser_primary_expression
1461 (cp_parser *, bool, bool, bool, cp_id_kind *);
1462 static tree cp_parser_id_expression
1463 (cp_parser *, bool, bool, bool *, bool, bool);
1464 static tree cp_parser_unqualified_id
1465 (cp_parser *, bool, bool, bool, bool);
1466 static tree cp_parser_nested_name_specifier_opt
1467 (cp_parser *, bool, bool, bool, bool);
1468 static tree cp_parser_nested_name_specifier
1469 (cp_parser *, bool, bool, bool, bool);
1470 static tree cp_parser_class_or_namespace_name
1471 (cp_parser *, bool, bool, bool, bool, bool);
1472 static tree cp_parser_postfix_expression
1473 (cp_parser *, bool, bool);
1474 static tree cp_parser_postfix_open_square_expression
1475 (cp_parser *, tree, bool);
1476 static tree cp_parser_postfix_dot_deref_expression
1477 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1478 static tree cp_parser_parenthesized_expression_list
1479 (cp_parser *, bool, bool, bool *);
1480 static void cp_parser_pseudo_destructor_name
1481 (cp_parser *, tree *, tree *);
1482 static tree cp_parser_unary_expression
1483 (cp_parser *, bool, bool);
1484 static enum tree_code cp_parser_unary_operator
1486 static tree cp_parser_new_expression
1488 static tree cp_parser_new_placement
1490 static tree cp_parser_new_type_id
1491 (cp_parser *, tree *);
1492 static cp_declarator *cp_parser_new_declarator_opt
1494 static cp_declarator *cp_parser_direct_new_declarator
1496 static tree cp_parser_new_initializer
1498 static tree cp_parser_delete_expression
1500 static tree cp_parser_cast_expression
1501 (cp_parser *, bool, bool);
1502 static tree cp_parser_binary_expression
1503 (cp_parser *, bool);
1504 static tree cp_parser_question_colon_clause
1505 (cp_parser *, tree);
1506 static tree cp_parser_assignment_expression
1507 (cp_parser *, bool);
1508 static enum tree_code cp_parser_assignment_operator_opt
1510 static tree cp_parser_expression
1511 (cp_parser *, bool);
1512 static tree cp_parser_constant_expression
1513 (cp_parser *, bool, bool *);
1514 static tree cp_parser_builtin_offsetof
1517 /* Statements [gram.stmt.stmt] */
1519 static void cp_parser_statement
1520 (cp_parser *, tree, bool);
1521 static void cp_parser_label_for_labeled_statement
1523 static tree cp_parser_expression_statement
1524 (cp_parser *, tree);
1525 static tree cp_parser_compound_statement
1526 (cp_parser *, tree, bool);
1527 static void cp_parser_statement_seq_opt
1528 (cp_parser *, tree);
1529 static tree cp_parser_selection_statement
1531 static tree cp_parser_condition
1533 static tree cp_parser_iteration_statement
1535 static void cp_parser_for_init_statement
1537 static tree cp_parser_jump_statement
1539 static void cp_parser_declaration_statement
1542 static tree cp_parser_implicitly_scoped_statement
1544 static void cp_parser_already_scoped_statement
1547 /* Declarations [gram.dcl.dcl] */
1549 static void cp_parser_declaration_seq_opt
1551 static void cp_parser_declaration
1553 static void cp_parser_block_declaration
1554 (cp_parser *, bool);
1555 static void cp_parser_simple_declaration
1556 (cp_parser *, bool);
1557 static void cp_parser_decl_specifier_seq
1558 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1559 static tree cp_parser_storage_class_specifier_opt
1561 static tree cp_parser_function_specifier_opt
1562 (cp_parser *, cp_decl_specifier_seq *);
1563 static tree cp_parser_type_specifier
1564 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1566 static tree cp_parser_simple_type_specifier
1567 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1568 static tree cp_parser_type_name
1570 static tree cp_parser_elaborated_type_specifier
1571 (cp_parser *, bool, bool);
1572 static tree cp_parser_enum_specifier
1574 static void cp_parser_enumerator_list
1575 (cp_parser *, tree);
1576 static void cp_parser_enumerator_definition
1577 (cp_parser *, tree);
1578 static tree cp_parser_namespace_name
1580 static void cp_parser_namespace_definition
1582 static void cp_parser_namespace_body
1584 static tree cp_parser_qualified_namespace_specifier
1586 static void cp_parser_namespace_alias_definition
1588 static bool cp_parser_using_declaration
1589 (cp_parser *, bool);
1590 static void cp_parser_using_directive
1592 static void cp_parser_asm_definition
1594 static void cp_parser_linkage_specification
1596 static void cp_parser_static_assert
1597 (cp_parser *, bool);
1599 /* Declarators [gram.dcl.decl] */
1601 static tree cp_parser_init_declarator
1602 (cp_parser *, cp_decl_specifier_seq *, tree, bool, bool, int, bool *);
1603 static cp_declarator *cp_parser_declarator
1604 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1605 static cp_declarator *cp_parser_direct_declarator
1606 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1607 static enum tree_code cp_parser_ptr_operator
1608 (cp_parser *, tree *, cp_cv_quals *);
1609 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1611 static tree cp_parser_declarator_id
1612 (cp_parser *, bool);
1613 static tree cp_parser_type_id
1615 static void cp_parser_type_specifier_seq
1616 (cp_parser *, bool, cp_decl_specifier_seq *);
1617 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1619 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1620 (cp_parser *, bool *);
1621 static cp_parameter_declarator *cp_parser_parameter_declaration
1622 (cp_parser *, bool, bool *);
1623 static void cp_parser_function_body
1625 static tree cp_parser_initializer
1626 (cp_parser *, bool *, bool *);
1627 static tree cp_parser_initializer_clause
1628 (cp_parser *, bool *);
1629 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1630 (cp_parser *, bool *);
1632 static bool cp_parser_ctor_initializer_opt_and_function_body
1635 /* Classes [gram.class] */
1637 static tree cp_parser_class_name
1638 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1639 static tree cp_parser_class_specifier
1641 static tree cp_parser_class_head
1642 (cp_parser *, bool *, tree *, tree *);
1643 static enum tag_types cp_parser_class_key
1645 static void cp_parser_member_specification_opt
1647 static void cp_parser_member_declaration
1649 static tree cp_parser_pure_specifier
1651 static tree cp_parser_constant_initializer
1654 /* Derived classes [gram.class.derived] */
1656 static tree cp_parser_base_clause
1658 static tree cp_parser_base_specifier
1661 /* Special member functions [gram.special] */
1663 static tree cp_parser_conversion_function_id
1665 static tree cp_parser_conversion_type_id
1667 static cp_declarator *cp_parser_conversion_declarator_opt
1669 static bool cp_parser_ctor_initializer_opt
1671 static void cp_parser_mem_initializer_list
1673 static tree cp_parser_mem_initializer
1675 static tree cp_parser_mem_initializer_id
1678 /* Overloading [gram.over] */
1680 static tree cp_parser_operator_function_id
1682 static tree cp_parser_operator
1685 /* Templates [gram.temp] */
1687 static void cp_parser_template_declaration
1688 (cp_parser *, bool);
1689 static tree cp_parser_template_parameter_list
1691 static tree cp_parser_template_parameter
1692 (cp_parser *, bool *);
1693 static tree cp_parser_type_parameter
1695 static tree cp_parser_template_id
1696 (cp_parser *, bool, bool, bool);
1697 static tree cp_parser_template_name
1698 (cp_parser *, bool, bool, bool, bool *);
1699 static tree cp_parser_template_argument_list
1701 static tree cp_parser_template_argument
1703 static void cp_parser_explicit_instantiation
1705 static void cp_parser_explicit_specialization
1708 /* Exception handling [gram.exception] */
1710 static tree cp_parser_try_block
1712 static bool cp_parser_function_try_block
1714 static void cp_parser_handler_seq
1716 static void cp_parser_handler
1718 static tree cp_parser_exception_declaration
1720 static tree cp_parser_throw_expression
1722 static tree cp_parser_exception_specification_opt
1724 static tree cp_parser_type_id_list
1727 /* GNU Extensions */
1729 static tree cp_parser_asm_specification_opt
1731 static tree cp_parser_asm_operand_list
1733 static tree cp_parser_asm_clobber_list
1735 static tree cp_parser_attributes_opt
1737 static tree cp_parser_attribute_list
1739 static bool cp_parser_extension_opt
1740 (cp_parser *, int *);
1741 static void cp_parser_label_declaration
1744 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1745 static bool cp_parser_pragma
1746 (cp_parser *, enum pragma_context);
1748 /* Objective-C++ Productions */
1750 static tree cp_parser_objc_message_receiver
1752 static tree cp_parser_objc_message_args
1754 static tree cp_parser_objc_message_expression
1756 static tree cp_parser_objc_encode_expression
1758 static tree cp_parser_objc_defs_expression
1760 static tree cp_parser_objc_protocol_expression
1762 static tree cp_parser_objc_selector_expression
1764 static tree cp_parser_objc_expression
1766 static bool cp_parser_objc_selector_p
1768 static tree cp_parser_objc_selector
1770 static tree cp_parser_objc_protocol_refs_opt
1772 static void cp_parser_objc_declaration
1774 static tree cp_parser_objc_statement
1777 /* Utility Routines */
1779 static tree cp_parser_lookup_name
1780 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1781 static tree cp_parser_lookup_name_simple
1782 (cp_parser *, tree);
1783 static tree cp_parser_maybe_treat_template_as_class
1785 static bool cp_parser_check_declarator_template_parameters
1786 (cp_parser *, cp_declarator *);
1787 static bool cp_parser_check_template_parameters
1788 (cp_parser *, unsigned);
1789 static tree cp_parser_simple_cast_expression
1791 static tree cp_parser_global_scope_opt
1792 (cp_parser *, bool);
1793 static bool cp_parser_constructor_declarator_p
1794 (cp_parser *, bool);
1795 static tree cp_parser_function_definition_from_specifiers_and_declarator
1796 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1797 static tree cp_parser_function_definition_after_declarator
1798 (cp_parser *, bool);
1799 static void cp_parser_template_declaration_after_export
1800 (cp_parser *, bool);
1801 static void cp_parser_perform_template_parameter_access_checks
1803 static tree cp_parser_single_declaration
1804 (cp_parser *, tree, bool, bool *);
1805 static tree cp_parser_functional_cast
1806 (cp_parser *, tree);
1807 static tree cp_parser_save_member_function_body
1808 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1809 static tree cp_parser_enclosed_template_argument_list
1811 static void cp_parser_save_default_args
1812 (cp_parser *, tree);
1813 static void cp_parser_late_parsing_for_member
1814 (cp_parser *, tree);
1815 static void cp_parser_late_parsing_default_args
1816 (cp_parser *, tree);
1817 static tree cp_parser_sizeof_operand
1818 (cp_parser *, enum rid);
1819 static bool cp_parser_declares_only_class_p
1821 static void cp_parser_set_storage_class
1822 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1823 static void cp_parser_set_decl_spec_type
1824 (cp_decl_specifier_seq *, tree, bool);
1825 static bool cp_parser_friend_p
1826 (const cp_decl_specifier_seq *);
1827 static cp_token *cp_parser_require
1828 (cp_parser *, enum cpp_ttype, const char *);
1829 static cp_token *cp_parser_require_keyword
1830 (cp_parser *, enum rid, const char *);
1831 static bool cp_parser_token_starts_function_definition_p
1833 static bool cp_parser_next_token_starts_class_definition_p
1835 static bool cp_parser_next_token_ends_template_argument_p
1837 static bool cp_parser_nth_token_starts_template_argument_list_p
1838 (cp_parser *, size_t);
1839 static enum tag_types cp_parser_token_is_class_key
1841 static void cp_parser_check_class_key
1842 (enum tag_types, tree type);
1843 static void cp_parser_check_access_in_redeclaration
1845 static bool cp_parser_optional_template_keyword
1847 static void cp_parser_pre_parsed_nested_name_specifier
1849 static void cp_parser_cache_group
1850 (cp_parser *, enum cpp_ttype, unsigned);
1851 static void cp_parser_parse_tentatively
1853 static void cp_parser_commit_to_tentative_parse
1855 static void cp_parser_abort_tentative_parse
1857 static bool cp_parser_parse_definitely
1859 static inline bool cp_parser_parsing_tentatively
1861 static bool cp_parser_uncommitted_to_tentative_parse_p
1863 static void cp_parser_error
1864 (cp_parser *, const char *);
1865 static void cp_parser_name_lookup_error
1866 (cp_parser *, tree, tree, const char *);
1867 static bool cp_parser_simulate_error
1869 static bool cp_parser_check_type_definition
1871 static void cp_parser_check_for_definition_in_return_type
1872 (cp_declarator *, tree);
1873 static void cp_parser_check_for_invalid_template_id
1874 (cp_parser *, tree);
1875 static bool cp_parser_non_integral_constant_expression
1876 (cp_parser *, const char *);
1877 static void cp_parser_diagnose_invalid_type_name
1878 (cp_parser *, tree, tree);
1879 static bool cp_parser_parse_and_diagnose_invalid_type_name
1881 static int cp_parser_skip_to_closing_parenthesis
1882 (cp_parser *, bool, bool, bool);
1883 static void cp_parser_skip_to_end_of_statement
1885 static void cp_parser_consume_semicolon_at_end_of_statement
1887 static void cp_parser_skip_to_end_of_block_or_statement
1889 static void cp_parser_skip_to_closing_brace
1891 static void cp_parser_skip_to_end_of_template_parameter_list
1893 static void cp_parser_skip_to_pragma_eol
1894 (cp_parser*, cp_token *);
1895 static bool cp_parser_error_occurred
1897 static bool cp_parser_allow_gnu_extensions_p
1899 static bool cp_parser_is_string_literal
1901 static bool cp_parser_is_keyword
1902 (cp_token *, enum rid);
1903 static tree cp_parser_make_typename_type
1904 (cp_parser *, tree, tree);
1906 /* Returns nonzero if we are parsing tentatively. */
1909 cp_parser_parsing_tentatively (cp_parser* parser)
1911 return parser->context->next != NULL;
1914 /* Returns nonzero if TOKEN is a string literal. */
1917 cp_parser_is_string_literal (cp_token* token)
1919 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1922 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1925 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1927 return token->keyword == keyword;
1930 /* If not parsing tentatively, issue a diagnostic of the form
1931 FILE:LINE: MESSAGE before TOKEN
1932 where TOKEN is the next token in the input stream. MESSAGE
1933 (specified by the caller) is usually of the form "expected
1937 cp_parser_error (cp_parser* parser, const char* message)
1939 if (!cp_parser_simulate_error (parser))
1941 cp_token *token = cp_lexer_peek_token (parser->lexer);
1942 /* This diagnostic makes more sense if it is tagged to the line
1943 of the token we just peeked at. */
1944 cp_lexer_set_source_position_from_token (token);
1946 if (token->type == CPP_PRAGMA)
1948 error ("%<#pragma%> is not allowed here");
1949 cp_parser_skip_to_pragma_eol (parser, token);
1953 c_parse_error (message,
1954 /* Because c_parser_error does not understand
1955 CPP_KEYWORD, keywords are treated like
1957 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1962 /* Issue an error about name-lookup failing. NAME is the
1963 IDENTIFIER_NODE DECL is the result of
1964 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1965 the thing that we hoped to find. */
1968 cp_parser_name_lookup_error (cp_parser* parser,
1971 const char* desired)
1973 /* If name lookup completely failed, tell the user that NAME was not
1975 if (decl == error_mark_node)
1977 if (parser->scope && parser->scope != global_namespace)
1978 error ("%<%D::%D%> has not been declared",
1979 parser->scope, name);
1980 else if (parser->scope == global_namespace)
1981 error ("%<::%D%> has not been declared", name);
1982 else if (parser->object_scope
1983 && !CLASS_TYPE_P (parser->object_scope))
1984 error ("request for member %qD in non-class type %qT",
1985 name, parser->object_scope);
1986 else if (parser->object_scope)
1987 error ("%<%T::%D%> has not been declared",
1988 parser->object_scope, name);
1990 error ("%qD has not been declared", name);
1992 else if (parser->scope && parser->scope != global_namespace)
1993 error ("%<%D::%D%> %s", parser->scope, name, desired);
1994 else if (parser->scope == global_namespace)
1995 error ("%<::%D%> %s", name, desired);
1997 error ("%qD %s", name, desired);
2000 /* If we are parsing tentatively, remember that an error has occurred
2001 during this tentative parse. Returns true if the error was
2002 simulated; false if a message should be issued by the caller. */
2005 cp_parser_simulate_error (cp_parser* parser)
2007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2009 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2015 /* Check for repeated decl-specifiers. */
2018 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2022 for (ds = ds_first; ds != ds_last; ++ds)
2024 unsigned count = decl_specs->specs[(int)ds];
2027 /* The "long" specifier is a special case because of "long long". */
2031 error ("%<long long long%> is too long for GCC");
2032 else if (pedantic && !in_system_header && warn_long_long)
2033 pedwarn ("ISO C++ does not support %<long long%>");
2037 static const char *const decl_spec_names[] = {
2053 error ("duplicate %qs", decl_spec_names[(int)ds]);
2058 /* This function is called when a type is defined. If type
2059 definitions are forbidden at this point, an error message is
2063 cp_parser_check_type_definition (cp_parser* parser)
2065 /* If types are forbidden here, issue a message. */
2066 if (parser->type_definition_forbidden_message)
2068 /* Use `%s' to print the string in case there are any escape
2069 characters in the message. */
2070 error ("%s", parser->type_definition_forbidden_message);
2076 /* This function is called when the DECLARATOR is processed. The TYPE
2077 was a type defined in the decl-specifiers. If it is invalid to
2078 define a type in the decl-specifiers for DECLARATOR, an error is
2082 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2085 /* [dcl.fct] forbids type definitions in return types.
2086 Unfortunately, it's not easy to know whether or not we are
2087 processing a return type until after the fact. */
2089 && (declarator->kind == cdk_pointer
2090 || declarator->kind == cdk_reference
2091 || declarator->kind == cdk_ptrmem))
2092 declarator = declarator->declarator;
2094 && declarator->kind == cdk_function)
2096 error ("new types may not be defined in a return type");
2097 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2102 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2103 "<" in any valid C++ program. If the next token is indeed "<",
2104 issue a message warning the user about what appears to be an
2105 invalid attempt to form a template-id. */
2108 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2111 cp_token_position start = 0;
2113 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2116 error ("%qT is not a template", type);
2117 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2118 error ("%qE is not a template", type);
2120 error ("invalid template-id");
2121 /* Remember the location of the invalid "<". */
2122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2123 start = cp_lexer_token_position (parser->lexer, true);
2124 /* Consume the "<". */
2125 cp_lexer_consume_token (parser->lexer);
2126 /* Parse the template arguments. */
2127 cp_parser_enclosed_template_argument_list (parser);
2128 /* Permanently remove the invalid template arguments so that
2129 this error message is not issued again. */
2131 cp_lexer_purge_tokens_after (parser->lexer, start);
2135 /* If parsing an integral constant-expression, issue an error message
2136 about the fact that THING appeared and return true. Otherwise,
2137 return false. In either case, set
2138 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2141 cp_parser_non_integral_constant_expression (cp_parser *parser,
2144 parser->non_integral_constant_expression_p = true;
2145 if (parser->integral_constant_expression_p)
2147 if (!parser->allow_non_integral_constant_expression_p)
2149 error ("%s cannot appear in a constant-expression", thing);
2156 /* Emit a diagnostic for an invalid type name. SCOPE is the
2157 qualifying scope (or NULL, if none) for ID. This function commits
2158 to the current active tentative parse, if any. (Otherwise, the
2159 problematic construct might be encountered again later, resulting
2160 in duplicate error messages.) */
2163 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2165 tree decl, old_scope;
2166 /* Try to lookup the identifier. */
2167 old_scope = parser->scope;
2168 parser->scope = scope;
2169 decl = cp_parser_lookup_name_simple (parser, id);
2170 parser->scope = old_scope;
2171 /* If the lookup found a template-name, it means that the user forgot
2172 to specify an argument list. Emit a useful error message. */
2173 if (TREE_CODE (decl) == TEMPLATE_DECL)
2174 error ("invalid use of template-name %qE without an argument list", decl);
2175 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2176 error ("invalid use of destructor %qD as a type", id);
2177 else if (TREE_CODE (decl) == TYPE_DECL)
2178 /* Something like 'unsigned A a;' */
2179 error ("invalid combination of multiple type-specifiers");
2180 else if (!parser->scope)
2182 /* Issue an error message. */
2183 error ("%qE does not name a type", id);
2184 /* If we're in a template class, it's possible that the user was
2185 referring to a type from a base class. For example:
2187 template <typename T> struct A { typedef T X; };
2188 template <typename T> struct B : public A<T> { X x; };
2190 The user should have said "typename A<T>::X". */
2191 if (processing_template_decl && current_class_type
2192 && TYPE_BINFO (current_class_type))
2196 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2200 tree base_type = BINFO_TYPE (b);
2201 if (CLASS_TYPE_P (base_type)
2202 && dependent_type_p (base_type))
2205 /* Go from a particular instantiation of the
2206 template (which will have an empty TYPE_FIELDs),
2207 to the main version. */
2208 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2209 for (field = TYPE_FIELDS (base_type);
2211 field = TREE_CHAIN (field))
2212 if (TREE_CODE (field) == TYPE_DECL
2213 && DECL_NAME (field) == id)
2215 inform ("(perhaps %<typename %T::%E%> was intended)",
2216 BINFO_TYPE (b), id);
2225 /* Here we diagnose qualified-ids where the scope is actually correct,
2226 but the identifier does not resolve to a valid type name. */
2227 else if (parser->scope != error_mark_node)
2229 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2230 error ("%qE in namespace %qE does not name a type",
2232 else if (TYPE_P (parser->scope))
2233 error ("%qE in class %qT does not name a type", id, parser->scope);
2237 cp_parser_commit_to_tentative_parse (parser);
2240 /* Check for a common situation where a type-name should be present,
2241 but is not, and issue a sensible error message. Returns true if an
2242 invalid type-name was detected.
2244 The situation handled by this function are variable declarations of the
2245 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2246 Usually, `ID' should name a type, but if we got here it means that it
2247 does not. We try to emit the best possible error message depending on
2248 how exactly the id-expression looks like. */
2251 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2255 cp_parser_parse_tentatively (parser);
2256 id = cp_parser_id_expression (parser,
2257 /*template_keyword_p=*/false,
2258 /*check_dependency_p=*/true,
2259 /*template_p=*/NULL,
2260 /*declarator_p=*/true,
2261 /*optional_p=*/false);
2262 /* After the id-expression, there should be a plain identifier,
2263 otherwise this is not a simple variable declaration. Also, if
2264 the scope is dependent, we cannot do much. */
2265 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2266 || (parser->scope && TYPE_P (parser->scope)
2267 && dependent_type_p (parser->scope)))
2269 cp_parser_abort_tentative_parse (parser);
2272 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2275 /* Emit a diagnostic for the invalid type. */
2276 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2277 /* Skip to the end of the declaration; there's no point in
2278 trying to process it. */
2279 cp_parser_skip_to_end_of_block_or_statement (parser);
2283 /* Consume tokens up to, and including, the next non-nested closing `)'.
2284 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2285 are doing error recovery. Returns -1 if OR_COMMA is true and we
2286 found an unnested comma. */
2289 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2294 unsigned paren_depth = 0;
2295 unsigned brace_depth = 0;
2297 if (recovering && !or_comma
2298 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2303 cp_token * token = cp_lexer_peek_token (parser->lexer);
2305 switch (token->type)
2308 case CPP_PRAGMA_EOL:
2309 /* If we've run out of tokens, then there is no closing `)'. */
2313 /* This matches the processing in skip_to_end_of_statement. */
2318 case CPP_OPEN_BRACE:
2321 case CPP_CLOSE_BRACE:
2327 if (recovering && or_comma && !brace_depth && !paren_depth)
2331 case CPP_OPEN_PAREN:
2336 case CPP_CLOSE_PAREN:
2337 if (!brace_depth && !paren_depth--)
2340 cp_lexer_consume_token (parser->lexer);
2349 /* Consume the token. */
2350 cp_lexer_consume_token (parser->lexer);
2354 /* Consume tokens until we reach the end of the current statement.
2355 Normally, that will be just before consuming a `;'. However, if a
2356 non-nested `}' comes first, then we stop before consuming that. */
2359 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2361 unsigned nesting_depth = 0;
2365 cp_token *token = cp_lexer_peek_token (parser->lexer);
2367 switch (token->type)
2370 case CPP_PRAGMA_EOL:
2371 /* If we've run out of tokens, stop. */
2375 /* If the next token is a `;', we have reached the end of the
2381 case CPP_CLOSE_BRACE:
2382 /* If this is a non-nested '}', stop before consuming it.
2383 That way, when confronted with something like:
2387 we stop before consuming the closing '}', even though we
2388 have not yet reached a `;'. */
2389 if (nesting_depth == 0)
2392 /* If it is the closing '}' for a block that we have
2393 scanned, stop -- but only after consuming the token.
2399 we will stop after the body of the erroneously declared
2400 function, but before consuming the following `typedef'
2402 if (--nesting_depth == 0)
2404 cp_lexer_consume_token (parser->lexer);
2408 case CPP_OPEN_BRACE:
2416 /* Consume the token. */
2417 cp_lexer_consume_token (parser->lexer);
2421 /* This function is called at the end of a statement or declaration.
2422 If the next token is a semicolon, it is consumed; otherwise, error
2423 recovery is attempted. */
2426 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2428 /* Look for the trailing `;'. */
2429 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2431 /* If there is additional (erroneous) input, skip to the end of
2433 cp_parser_skip_to_end_of_statement (parser);
2434 /* If the next token is now a `;', consume it. */
2435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2436 cp_lexer_consume_token (parser->lexer);
2440 /* Skip tokens until we have consumed an entire block, or until we
2441 have consumed a non-nested `;'. */
2444 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2446 int nesting_depth = 0;
2448 while (nesting_depth >= 0)
2450 cp_token *token = cp_lexer_peek_token (parser->lexer);
2452 switch (token->type)
2455 case CPP_PRAGMA_EOL:
2456 /* If we've run out of tokens, stop. */
2460 /* Stop if this is an unnested ';'. */
2465 case CPP_CLOSE_BRACE:
2466 /* Stop if this is an unnested '}', or closes the outermost
2473 case CPP_OPEN_BRACE:
2482 /* Consume the token. */
2483 cp_lexer_consume_token (parser->lexer);
2487 /* Skip tokens until a non-nested closing curly brace is the next
2491 cp_parser_skip_to_closing_brace (cp_parser *parser)
2493 unsigned nesting_depth = 0;
2497 cp_token *token = cp_lexer_peek_token (parser->lexer);
2499 switch (token->type)
2502 case CPP_PRAGMA_EOL:
2503 /* If we've run out of tokens, stop. */
2506 case CPP_CLOSE_BRACE:
2507 /* If the next token is a non-nested `}', then we have reached
2508 the end of the current block. */
2509 if (nesting_depth-- == 0)
2513 case CPP_OPEN_BRACE:
2514 /* If it the next token is a `{', then we are entering a new
2515 block. Consume the entire block. */
2523 /* Consume the token. */
2524 cp_lexer_consume_token (parser->lexer);
2528 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2529 parameter is the PRAGMA token, allowing us to purge the entire pragma
2533 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2537 parser->lexer->in_pragma = false;
2540 token = cp_lexer_consume_token (parser->lexer);
2541 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2543 /* Ensure that the pragma is not parsed again. */
2544 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2547 /* Require pragma end of line, resyncing with it as necessary. The
2548 arguments are as for cp_parser_skip_to_pragma_eol. */
2551 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2553 parser->lexer->in_pragma = false;
2554 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2555 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2558 /* This is a simple wrapper around make_typename_type. When the id is
2559 an unresolved identifier node, we can provide a superior diagnostic
2560 using cp_parser_diagnose_invalid_type_name. */
2563 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2566 if (TREE_CODE (id) == IDENTIFIER_NODE)
2568 result = make_typename_type (scope, id, typename_type,
2569 /*complain=*/tf_none);
2570 if (result == error_mark_node)
2571 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2574 return make_typename_type (scope, id, typename_type, tf_error);
2578 /* Create a new C++ parser. */
2581 cp_parser_new (void)
2587 /* cp_lexer_new_main is called before calling ggc_alloc because
2588 cp_lexer_new_main might load a PCH file. */
2589 lexer = cp_lexer_new_main ();
2591 /* Initialize the binops_by_token so that we can get the tree
2592 directly from the token. */
2593 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2594 binops_by_token[binops[i].token_type] = binops[i];
2596 parser = GGC_CNEW (cp_parser);
2597 parser->lexer = lexer;
2598 parser->context = cp_parser_context_new (NULL);
2600 /* For now, we always accept GNU extensions. */
2601 parser->allow_gnu_extensions_p = 1;
2603 /* The `>' token is a greater-than operator, not the end of a
2605 parser->greater_than_is_operator_p = true;
2607 parser->default_arg_ok_p = true;
2609 /* We are not parsing a constant-expression. */
2610 parser->integral_constant_expression_p = false;
2611 parser->allow_non_integral_constant_expression_p = false;
2612 parser->non_integral_constant_expression_p = false;
2614 /* Local variable names are not forbidden. */
2615 parser->local_variables_forbidden_p = false;
2617 /* We are not processing an `extern "C"' declaration. */
2618 parser->in_unbraced_linkage_specification_p = false;
2620 /* We are not processing a declarator. */
2621 parser->in_declarator_p = false;
2623 /* We are not processing a template-argument-list. */
2624 parser->in_template_argument_list_p = false;
2626 /* We are not in an iteration statement. */
2627 parser->in_statement = 0;
2629 /* We are not in a switch statement. */
2630 parser->in_switch_statement_p = false;
2632 /* We are not parsing a type-id inside an expression. */
2633 parser->in_type_id_in_expr_p = false;
2635 /* Declarations aren't implicitly extern "C". */
2636 parser->implicit_extern_c = false;
2638 /* String literals should be translated to the execution character set. */
2639 parser->translate_strings_p = true;
2641 /* We are not parsing a function body. */
2642 parser->in_function_body = false;
2644 /* The unparsed function queue is empty. */
2645 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2647 /* There are no classes being defined. */
2648 parser->num_classes_being_defined = 0;
2650 /* No template parameters apply. */
2651 parser->num_template_parameter_lists = 0;
2656 /* Create a cp_lexer structure which will emit the tokens in CACHE
2657 and push it onto the parser's lexer stack. This is used for delayed
2658 parsing of in-class method bodies and default arguments, and should
2659 not be confused with tentative parsing. */
2661 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2663 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2664 lexer->next = parser->lexer;
2665 parser->lexer = lexer;
2667 /* Move the current source position to that of the first token in the
2669 cp_lexer_set_source_position_from_token (lexer->next_token);
2672 /* Pop the top lexer off the parser stack. This is never used for the
2673 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2675 cp_parser_pop_lexer (cp_parser *parser)
2677 cp_lexer *lexer = parser->lexer;
2678 parser->lexer = lexer->next;
2679 cp_lexer_destroy (lexer);
2681 /* Put the current source position back where it was before this
2682 lexer was pushed. */
2683 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2686 /* Lexical conventions [gram.lex] */
2688 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2692 cp_parser_identifier (cp_parser* parser)
2696 /* Look for the identifier. */
2697 token = cp_parser_require (parser, CPP_NAME, "identifier");
2698 /* Return the value. */
2699 return token ? token->value : error_mark_node;
2702 /* Parse a sequence of adjacent string constants. Returns a
2703 TREE_STRING representing the combined, nul-terminated string
2704 constant. If TRANSLATE is true, translate the string to the
2705 execution character set. If WIDE_OK is true, a wide string is
2708 C++98 [lex.string] says that if a narrow string literal token is
2709 adjacent to a wide string literal token, the behavior is undefined.
2710 However, C99 6.4.5p4 says that this results in a wide string literal.
2711 We follow C99 here, for consistency with the C front end.
2713 This code is largely lifted from lex_string() in c-lex.c.
2715 FUTURE: ObjC++ will need to handle @-strings here. */
2717 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2722 struct obstack str_ob;
2723 cpp_string str, istr, *strs;
2726 tok = cp_lexer_peek_token (parser->lexer);
2727 if (!cp_parser_is_string_literal (tok))
2729 cp_parser_error (parser, "expected string-literal");
2730 return error_mark_node;
2733 /* Try to avoid the overhead of creating and destroying an obstack
2734 for the common case of just one string. */
2735 if (!cp_parser_is_string_literal
2736 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2738 cp_lexer_consume_token (parser->lexer);
2740 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2741 str.len = TREE_STRING_LENGTH (tok->value);
2743 if (tok->type == CPP_WSTRING)
2750 gcc_obstack_init (&str_ob);
2755 cp_lexer_consume_token (parser->lexer);
2757 str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2758 str.len = TREE_STRING_LENGTH (tok->value);
2759 if (tok->type == CPP_WSTRING)
2762 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2764 tok = cp_lexer_peek_token (parser->lexer);
2766 while (cp_parser_is_string_literal (tok));
2768 strs = (cpp_string *) obstack_finish (&str_ob);
2771 if (wide && !wide_ok)
2773 cp_parser_error (parser, "a wide string is invalid in this context");
2777 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2778 (parse_in, strs, count, &istr, wide))
2780 value = build_string (istr.len, (char *)istr.text);
2781 free ((void *)istr.text);
2783 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2784 value = fix_string_type (value);
2787 /* cpp_interpret_string has issued an error. */
2788 value = error_mark_node;
2791 obstack_free (&str_ob, 0);
2797 /* Basic concepts [gram.basic] */
2799 /* Parse a translation-unit.
2802 declaration-seq [opt]
2804 Returns TRUE if all went well. */
2807 cp_parser_translation_unit (cp_parser* parser)
2809 /* The address of the first non-permanent object on the declarator
2811 static void *declarator_obstack_base;
2815 /* Create the declarator obstack, if necessary. */
2816 if (!cp_error_declarator)
2818 gcc_obstack_init (&declarator_obstack);
2819 /* Create the error declarator. */
2820 cp_error_declarator = make_declarator (cdk_error);
2821 /* Create the empty parameter list. */
2822 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2823 /* Remember where the base of the declarator obstack lies. */
2824 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2827 cp_parser_declaration_seq_opt (parser);
2829 /* If there are no tokens left then all went well. */
2830 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2832 /* Get rid of the token array; we don't need it any more. */
2833 cp_lexer_destroy (parser->lexer);
2834 parser->lexer = NULL;
2836 /* This file might have been a context that's implicitly extern
2837 "C". If so, pop the lang context. (Only relevant for PCH.) */
2838 if (parser->implicit_extern_c)
2840 pop_lang_context ();
2841 parser->implicit_extern_c = false;
2845 finish_translation_unit ();
2851 cp_parser_error (parser, "expected declaration");
2855 /* Make sure the declarator obstack was fully cleaned up. */
2856 gcc_assert (obstack_next_free (&declarator_obstack)
2857 == declarator_obstack_base);
2859 /* All went well. */
2863 /* Expressions [gram.expr] */
2865 /* Parse a primary-expression.
2876 ( compound-statement )
2877 __builtin_va_arg ( assignment-expression , type-id )
2878 __builtin_offsetof ( type-id , offsetof-expression )
2880 Objective-C++ Extension:
2888 ADDRESS_P is true iff this expression was immediately preceded by
2889 "&" and therefore might denote a pointer-to-member. CAST_P is true
2890 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2891 true iff this expression is a template argument.
2893 Returns a representation of the expression. Upon return, *IDK
2894 indicates what kind of id-expression (if any) was present. */
2897 cp_parser_primary_expression (cp_parser *parser,
2900 bool template_arg_p,
2905 /* Assume the primary expression is not an id-expression. */
2906 *idk = CP_ID_KIND_NONE;
2908 /* Peek at the next token. */
2909 token = cp_lexer_peek_token (parser->lexer);
2910 switch (token->type)
2921 token = cp_lexer_consume_token (parser->lexer);
2922 /* Floating-point literals are only allowed in an integral
2923 constant expression if they are cast to an integral or
2924 enumeration type. */
2925 if (TREE_CODE (token->value) == REAL_CST
2926 && parser->integral_constant_expression_p
2929 /* CAST_P will be set even in invalid code like "int(2.7 +
2930 ...)". Therefore, we have to check that the next token
2931 is sure to end the cast. */
2934 cp_token *next_token;
2936 next_token = cp_lexer_peek_token (parser->lexer);
2937 if (/* The comma at the end of an
2938 enumerator-definition. */
2939 next_token->type != CPP_COMMA
2940 /* The curly brace at the end of an enum-specifier. */
2941 && next_token->type != CPP_CLOSE_BRACE
2942 /* The end of a statement. */
2943 && next_token->type != CPP_SEMICOLON
2944 /* The end of the cast-expression. */
2945 && next_token->type != CPP_CLOSE_PAREN
2946 /* The end of an array bound. */
2947 && next_token->type != CPP_CLOSE_SQUARE
2948 /* The closing ">" in a template-argument-list. */
2949 && (next_token->type != CPP_GREATER
2950 || parser->greater_than_is_operator_p))
2954 /* If we are within a cast, then the constraint that the
2955 cast is to an integral or enumeration type will be
2956 checked at that point. If we are not within a cast, then
2957 this code is invalid. */
2959 cp_parser_non_integral_constant_expression
2960 (parser, "floating-point literal");
2962 return token->value;
2966 /* ??? Should wide strings be allowed when parser->translate_strings_p
2967 is false (i.e. in attributes)? If not, we can kill the third
2968 argument to cp_parser_string_literal. */
2969 return cp_parser_string_literal (parser,
2970 parser->translate_strings_p,
2973 case CPP_OPEN_PAREN:
2976 bool saved_greater_than_is_operator_p;
2978 /* Consume the `('. */
2979 cp_lexer_consume_token (parser->lexer);
2980 /* Within a parenthesized expression, a `>' token is always
2981 the greater-than operator. */
2982 saved_greater_than_is_operator_p
2983 = parser->greater_than_is_operator_p;
2984 parser->greater_than_is_operator_p = true;
2985 /* If we see `( { ' then we are looking at the beginning of
2986 a GNU statement-expression. */
2987 if (cp_parser_allow_gnu_extensions_p (parser)
2988 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2990 /* Statement-expressions are not allowed by the standard. */
2992 pedwarn ("ISO C++ forbids braced-groups within expressions");
2994 /* And they're not allowed outside of a function-body; you
2995 cannot, for example, write:
2997 int i = ({ int j = 3; j + 1; });
2999 at class or namespace scope. */
3000 if (!parser->in_function_body)
3001 error ("statement-expressions are allowed only inside functions");
3002 /* Start the statement-expression. */
3003 expr = begin_stmt_expr ();
3004 /* Parse the compound-statement. */
3005 cp_parser_compound_statement (parser, expr, false);
3007 expr = finish_stmt_expr (expr, false);
3011 /* Parse the parenthesized expression. */
3012 expr = cp_parser_expression (parser, cast_p);
3013 /* Let the front end know that this expression was
3014 enclosed in parentheses. This matters in case, for
3015 example, the expression is of the form `A::B', since
3016 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3018 finish_parenthesized_expr (expr);
3020 /* The `>' token might be the end of a template-id or
3021 template-parameter-list now. */
3022 parser->greater_than_is_operator_p
3023 = saved_greater_than_is_operator_p;
3024 /* Consume the `)'. */
3025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3026 cp_parser_skip_to_end_of_statement (parser);
3032 switch (token->keyword)
3034 /* These two are the boolean literals. */
3036 cp_lexer_consume_token (parser->lexer);
3037 return boolean_true_node;
3039 cp_lexer_consume_token (parser->lexer);
3040 return boolean_false_node;
3042 /* The `__null' literal. */
3044 cp_lexer_consume_token (parser->lexer);
3047 /* Recognize the `this' keyword. */
3049 cp_lexer_consume_token (parser->lexer);
3050 if (parser->local_variables_forbidden_p)
3052 error ("%<this%> may not be used in this context");
3053 return error_mark_node;
3055 /* Pointers cannot appear in constant-expressions. */
3056 if (cp_parser_non_integral_constant_expression (parser,
3058 return error_mark_node;
3059 return finish_this_expr ();
3061 /* The `operator' keyword can be the beginning of an
3066 case RID_FUNCTION_NAME:
3067 case RID_PRETTY_FUNCTION_NAME:
3068 case RID_C99_FUNCTION_NAME:
3069 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3070 __func__ are the names of variables -- but they are
3071 treated specially. Therefore, they are handled here,
3072 rather than relying on the generic id-expression logic
3073 below. Grammatically, these names are id-expressions.
3075 Consume the token. */
3076 token = cp_lexer_consume_token (parser->lexer);
3077 /* Look up the name. */
3078 return finish_fname (token->value);
3085 /* The `__builtin_va_arg' construct is used to handle
3086 `va_arg'. Consume the `__builtin_va_arg' token. */
3087 cp_lexer_consume_token (parser->lexer);
3088 /* Look for the opening `('. */
3089 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3090 /* Now, parse the assignment-expression. */
3091 expression = cp_parser_assignment_expression (parser,
3093 /* Look for the `,'. */
3094 cp_parser_require (parser, CPP_COMMA, "`,'");
3095 /* Parse the type-id. */
3096 type = cp_parser_type_id (parser);
3097 /* Look for the closing `)'. */
3098 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3099 /* Using `va_arg' in a constant-expression is not
3101 if (cp_parser_non_integral_constant_expression (parser,
3103 return error_mark_node;
3104 return build_x_va_arg (expression, type);
3108 return cp_parser_builtin_offsetof (parser);
3110 /* Objective-C++ expressions. */
3112 case RID_AT_PROTOCOL:
3113 case RID_AT_SELECTOR:
3114 return cp_parser_objc_expression (parser);
3117 cp_parser_error (parser, "expected primary-expression");
3118 return error_mark_node;
3121 /* An id-expression can start with either an identifier, a
3122 `::' as the beginning of a qualified-id, or the "operator"
3126 case CPP_TEMPLATE_ID:
3127 case CPP_NESTED_NAME_SPECIFIER:
3131 const char *error_msg;
3136 /* Parse the id-expression. */
3138 = cp_parser_id_expression (parser,
3139 /*template_keyword_p=*/false,
3140 /*check_dependency_p=*/true,
3142 /*declarator_p=*/false,
3143 /*optional_p=*/false);
3144 if (id_expression == error_mark_node)
3145 return error_mark_node;
3146 token = cp_lexer_peek_token (parser->lexer);
3147 done = (token->type != CPP_OPEN_SQUARE
3148 && token->type != CPP_OPEN_PAREN
3149 && token->type != CPP_DOT
3150 && token->type != CPP_DEREF
3151 && token->type != CPP_PLUS_PLUS
3152 && token->type != CPP_MINUS_MINUS);
3153 /* If we have a template-id, then no further lookup is
3154 required. If the template-id was for a template-class, we
3155 will sometimes have a TYPE_DECL at this point. */
3156 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3157 || TREE_CODE (id_expression) == TYPE_DECL)
3158 decl = id_expression;
3159 /* Look up the name. */
3162 tree ambiguous_decls;
3164 decl = cp_parser_lookup_name (parser, id_expression,
3167 /*is_namespace=*/false,
3168 /*check_dependency=*/true,
3170 /* If the lookup was ambiguous, an error will already have
3172 if (ambiguous_decls)
3173 return error_mark_node;
3175 /* In Objective-C++, an instance variable (ivar) may be preferred
3176 to whatever cp_parser_lookup_name() found. */
3177 decl = objc_lookup_ivar (decl, id_expression);
3179 /* If name lookup gives us a SCOPE_REF, then the
3180 qualifying scope was dependent. */
3181 if (TREE_CODE (decl) == SCOPE_REF)
3183 /* Check to see if DECL is a local variable in a context
3184 where that is forbidden. */
3185 if (parser->local_variables_forbidden_p
3186 && local_variable_p (decl))
3188 /* It might be that we only found DECL because we are
3189 trying to be generous with pre-ISO scoping rules.
3190 For example, consider:
3194 for (int i = 0; i < 10; ++i) {}
3195 extern void f(int j = i);
3198 Here, name look up will originally find the out
3199 of scope `i'. We need to issue a warning message,
3200 but then use the global `i'. */
3201 decl = check_for_out_of_scope_variable (decl);
3202 if (local_variable_p (decl))
3204 error ("local variable %qD may not appear in this context",
3206 return error_mark_node;
3211 decl = (finish_id_expression
3212 (id_expression, decl, parser->scope,
3214 parser->integral_constant_expression_p,
3215 parser->allow_non_integral_constant_expression_p,
3216 &parser->non_integral_constant_expression_p,
3217 template_p, done, address_p,
3221 cp_parser_error (parser, error_msg);
3225 /* Anything else is an error. */
3227 /* ...unless we have an Objective-C++ message or string literal, that is. */
3228 if (c_dialect_objc ()
3229 && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3230 return cp_parser_objc_expression (parser);
3232 cp_parser_error (parser, "expected primary-expression");
3233 return error_mark_node;
3237 /* Parse an id-expression.
3244 :: [opt] nested-name-specifier template [opt] unqualified-id
3246 :: operator-function-id
3249 Return a representation of the unqualified portion of the
3250 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3251 a `::' or nested-name-specifier.
3253 Often, if the id-expression was a qualified-id, the caller will
3254 want to make a SCOPE_REF to represent the qualified-id. This
3255 function does not do this in order to avoid wastefully creating
3256 SCOPE_REFs when they are not required.
3258 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3261 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3262 uninstantiated templates.
3264 If *TEMPLATE_P is non-NULL, it is set to true iff the
3265 `template' keyword is used to explicitly indicate that the entity
3266 named is a template.
3268 If DECLARATOR_P is true, the id-expression is appearing as part of
3269 a declarator, rather than as part of an expression. */
3272 cp_parser_id_expression (cp_parser *parser,
3273 bool template_keyword_p,
3274 bool check_dependency_p,
3279 bool global_scope_p;
3280 bool nested_name_specifier_p;
3282 /* Assume the `template' keyword was not used. */
3284 *template_p = template_keyword_p;
3286 /* Look for the optional `::' operator. */
3288 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3290 /* Look for the optional nested-name-specifier. */
3291 nested_name_specifier_p
3292 = (cp_parser_nested_name_specifier_opt (parser,
3293 /*typename_keyword_p=*/false,
3298 /* If there is a nested-name-specifier, then we are looking at
3299 the first qualified-id production. */
3300 if (nested_name_specifier_p)
3303 tree saved_object_scope;
3304 tree saved_qualifying_scope;
3305 tree unqualified_id;
3308 /* See if the next token is the `template' keyword. */
3310 template_p = &is_template;
3311 *template_p = cp_parser_optional_template_keyword (parser);
3312 /* Name lookup we do during the processing of the
3313 unqualified-id might obliterate SCOPE. */
3314 saved_scope = parser->scope;
3315 saved_object_scope = parser->object_scope;
3316 saved_qualifying_scope = parser->qualifying_scope;
3317 /* Process the final unqualified-id. */
3318 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3321 /*optional_p=*/false);
3322 /* Restore the SAVED_SCOPE for our caller. */
3323 parser->scope = saved_scope;
3324 parser->object_scope = saved_object_scope;
3325 parser->qualifying_scope = saved_qualifying_scope;
3327 return unqualified_id;
3329 /* Otherwise, if we are in global scope, then we are looking at one
3330 of the other qualified-id productions. */
3331 else if (global_scope_p)
3336 /* Peek at the next token. */
3337 token = cp_lexer_peek_token (parser->lexer);
3339 /* If it's an identifier, and the next token is not a "<", then
3340 we can avoid the template-id case. This is an optimization
3341 for this common case. */
3342 if (token->type == CPP_NAME
3343 && !cp_parser_nth_token_starts_template_argument_list_p
3345 return cp_parser_identifier (parser);
3347 cp_parser_parse_tentatively (parser);
3348 /* Try a template-id. */
3349 id = cp_parser_template_id (parser,
3350 /*template_keyword_p=*/false,
3351 /*check_dependency_p=*/true,
3353 /* If that worked, we're done. */
3354 if (cp_parser_parse_definitely (parser))
3357 /* Peek at the next token. (Changes in the token buffer may
3358 have invalidated the pointer obtained above.) */
3359 token = cp_lexer_peek_token (parser->lexer);
3361 switch (token->type)
3364 return cp_parser_identifier (parser);
3367 if (token->keyword == RID_OPERATOR)
3368 return cp_parser_operator_function_id (parser);
3372 cp_parser_error (parser, "expected id-expression");
3373 return error_mark_node;
3377 return cp_parser_unqualified_id (parser, template_keyword_p,
3378 /*check_dependency_p=*/true,
3383 /* Parse an unqualified-id.
3387 operator-function-id
3388 conversion-function-id
3392 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3393 keyword, in a construct like `A::template ...'.
3395 Returns a representation of unqualified-id. For the `identifier'
3396 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3397 production a BIT_NOT_EXPR is returned; the operand of the
3398 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3399 other productions, see the documentation accompanying the
3400 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3401 names are looked up in uninstantiated templates. If DECLARATOR_P
3402 is true, the unqualified-id is appearing as part of a declarator,
3403 rather than as part of an expression. */
3406 cp_parser_unqualified_id (cp_parser* parser,
3407 bool template_keyword_p,
3408 bool check_dependency_p,
3414 /* Peek at the next token. */
3415 token = cp_lexer_peek_token (parser->lexer);
3417 switch (token->type)
3423 /* We don't know yet whether or not this will be a
3425 cp_parser_parse_tentatively (parser);
3426 /* Try a template-id. */
3427 id = cp_parser_template_id (parser, template_keyword_p,
3430 /* If it worked, we're done. */
3431 if (cp_parser_parse_definitely (parser))
3433 /* Otherwise, it's an ordinary identifier. */
3434 return cp_parser_identifier (parser);
3437 case CPP_TEMPLATE_ID:
3438 return cp_parser_template_id (parser, template_keyword_p,
3445 tree qualifying_scope;
3450 /* Consume the `~' token. */
3451 cp_lexer_consume_token (parser->lexer);
3452 /* Parse the class-name. The standard, as written, seems to
3455 template <typename T> struct S { ~S (); };
3456 template <typename T> S<T>::~S() {}
3458 is invalid, since `~' must be followed by a class-name, but
3459 `S<T>' is dependent, and so not known to be a class.
3460 That's not right; we need to look in uninstantiated
3461 templates. A further complication arises from:
3463 template <typename T> void f(T t) {
3467 Here, it is not possible to look up `T' in the scope of `T'
3468 itself. We must look in both the current scope, and the
3469 scope of the containing complete expression.
3471 Yet another issue is:
3480 The standard does not seem to say that the `S' in `~S'
3481 should refer to the type `S' and not the data member
3484 /* DR 244 says that we look up the name after the "~" in the
3485 same scope as we looked up the qualifying name. That idea
3486 isn't fully worked out; it's more complicated than that. */
3487 scope = parser->scope;
3488 object_scope = parser->object_scope;
3489 qualifying_scope = parser->qualifying_scope;
3491 /* Check for invalid scopes. */
3492 if (scope == error_mark_node)
3494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3495 cp_lexer_consume_token (parser->lexer);
3496 return error_mark_node;
3498 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3500 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3501 error ("scope %qT before %<~%> is not a class-name", scope);
3502 cp_parser_simulate_error (parser);
3503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3504 cp_lexer_consume_token (parser->lexer);
3505 return error_mark_node;
3507 gcc_assert (!scope || TYPE_P (scope));
3509 /* If the name is of the form "X::~X" it's OK. */
3510 token = cp_lexer_peek_token (parser->lexer);
3512 && token->type == CPP_NAME
3513 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3515 && constructor_name_p (token->value, scope))
3517 cp_lexer_consume_token (parser->lexer);
3518 return build_nt (BIT_NOT_EXPR, scope);
3521 /* If there was an explicit qualification (S::~T), first look
3522 in the scope given by the qualification (i.e., S). */
3524 type_decl = NULL_TREE;
3527 cp_parser_parse_tentatively (parser);
3528 type_decl = cp_parser_class_name (parser,
3529 /*typename_keyword_p=*/false,
3530 /*template_keyword_p=*/false,
3532 /*check_dependency=*/false,
3533 /*class_head_p=*/false,
3535 if (cp_parser_parse_definitely (parser))
3538 /* In "N::S::~S", look in "N" as well. */
3539 if (!done && scope && qualifying_scope)
3541 cp_parser_parse_tentatively (parser);
3542 parser->scope = qualifying_scope;
3543 parser->object_scope = NULL_TREE;
3544 parser->qualifying_scope = NULL_TREE;
3546 = cp_parser_class_name (parser,
3547 /*typename_keyword_p=*/false,
3548 /*template_keyword_p=*/false,
3550 /*check_dependency=*/false,
3551 /*class_head_p=*/false,
3553 if (cp_parser_parse_definitely (parser))
3556 /* In "p->S::~T", look in the scope given by "*p" as well. */
3557 else if (!done && object_scope)
3559 cp_parser_parse_tentatively (parser);
3560 parser->scope = object_scope;
3561 parser->object_scope = NULL_TREE;
3562 parser->qualifying_scope = NULL_TREE;
3564 = cp_parser_class_name (parser,
3565 /*typename_keyword_p=*/false,
3566 /*template_keyword_p=*/false,
3568 /*check_dependency=*/false,
3569 /*class_head_p=*/false,
3571 if (cp_parser_parse_definitely (parser))
3574 /* Look in the surrounding context. */
3577 parser->scope = NULL_TREE;
3578 parser->object_scope = NULL_TREE;
3579 parser->qualifying_scope = NULL_TREE;
3581 = cp_parser_class_name (parser,
3582 /*typename_keyword_p=*/false,
3583 /*template_keyword_p=*/false,
3585 /*check_dependency=*/false,
3586 /*class_head_p=*/false,
3589 /* If an error occurred, assume that the name of the
3590 destructor is the same as the name of the qualifying
3591 class. That allows us to keep parsing after running
3592 into ill-formed destructor names. */
3593 if (type_decl == error_mark_node && scope)
3594 return build_nt (BIT_NOT_EXPR, scope);
3595 else if (type_decl == error_mark_node)
3596 return error_mark_node;
3598 /* Check that destructor name and scope match. */
3599 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3601 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3602 error ("declaration of %<~%T%> as member of %qT",
3604 cp_parser_simulate_error (parser);
3605 return error_mark_node;
3610 A typedef-name that names a class shall not be used as the
3611 identifier in the declarator for a destructor declaration. */
3613 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3614 && !DECL_SELF_REFERENCE_P (type_decl)
3615 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3616 error ("typedef-name %qD used as destructor declarator",
3619 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3623 if (token->keyword == RID_OPERATOR)
3627 /* This could be a template-id, so we try that first. */
3628 cp_parser_parse_tentatively (parser);
3629 /* Try a template-id. */
3630 id = cp_parser_template_id (parser, template_keyword_p,
3631 /*check_dependency_p=*/true,
3633 /* If that worked, we're done. */
3634 if (cp_parser_parse_definitely (parser))
3636 /* We still don't know whether we're looking at an
3637 operator-function-id or a conversion-function-id. */
3638 cp_parser_parse_tentatively (parser);
3639 /* Try an operator-function-id. */
3640 id = cp_parser_operator_function_id (parser);
3641 /* If that didn't work, try a conversion-function-id. */
3642 if (!cp_parser_parse_definitely (parser))
3643 id = cp_parser_conversion_function_id (parser);
3652 cp_parser_error (parser, "expected unqualified-id");
3653 return error_mark_node;
3657 /* Parse an (optional) nested-name-specifier.
3659 nested-name-specifier:
3660 class-or-namespace-name :: nested-name-specifier [opt]
3661 class-or-namespace-name :: template nested-name-specifier [opt]
3663 PARSER->SCOPE should be set appropriately before this function is
3664 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3665 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3668 Sets PARSER->SCOPE to the class (TYPE) or namespace
3669 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3670 it unchanged if there is no nested-name-specifier. Returns the new
3671 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3673 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3674 part of a declaration and/or decl-specifier. */
3677 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3678 bool typename_keyword_p,
3679 bool check_dependency_p,
3681 bool is_declaration)
3683 bool success = false;
3684 cp_token_position start = 0;
3687 /* Remember where the nested-name-specifier starts. */
3688 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3690 start = cp_lexer_token_position (parser->lexer, false);
3691 push_deferring_access_checks (dk_deferred);
3698 tree saved_qualifying_scope;
3699 bool template_keyword_p;
3701 /* Spot cases that cannot be the beginning of a
3702 nested-name-specifier. */
3703 token = cp_lexer_peek_token (parser->lexer);
3705 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3706 the already parsed nested-name-specifier. */
3707 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3709 /* Grab the nested-name-specifier and continue the loop. */
3710 cp_parser_pre_parsed_nested_name_specifier (parser);
3711 /* If we originally encountered this nested-name-specifier
3712 with IS_DECLARATION set to false, we will not have
3713 resolved TYPENAME_TYPEs, so we must do so here. */
3715 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3717 new_scope = resolve_typename_type (parser->scope,
3718 /*only_current_p=*/false);
3719 if (new_scope != error_mark_node)
3720 parser->scope = new_scope;
3726 /* Spot cases that cannot be the beginning of a
3727 nested-name-specifier. On the second and subsequent times
3728 through the loop, we look for the `template' keyword. */
3729 if (success && token->keyword == RID_TEMPLATE)
3731 /* A template-id can start a nested-name-specifier. */
3732 else if (token->type == CPP_TEMPLATE_ID)
3736 /* If the next token is not an identifier, then it is
3737 definitely not a class-or-namespace-name. */
3738 if (token->type != CPP_NAME)
3740 /* If the following token is neither a `<' (to begin a
3741 template-id), nor a `::', then we are not looking at a
3742 nested-name-specifier. */
3743 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3744 if (token->type != CPP_SCOPE
3745 && !cp_parser_nth_token_starts_template_argument_list_p
3750 /* The nested-name-specifier is optional, so we parse
3752 cp_parser_parse_tentatively (parser);
3754 /* Look for the optional `template' keyword, if this isn't the
3755 first time through the loop. */
3757 template_keyword_p = cp_parser_optional_template_keyword (parser);
3759 template_keyword_p = false;
3761 /* Save the old scope since the name lookup we are about to do
3762 might destroy it. */
3763 old_scope = parser->scope;
3764 saved_qualifying_scope = parser->qualifying_scope;
3765 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3766 look up names in "X<T>::I" in order to determine that "Y" is
3767 a template. So, if we have a typename at this point, we make
3768 an effort to look through it. */
3770 && !typename_keyword_p
3772 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3773 parser->scope = resolve_typename_type (parser->scope,
3774 /*only_current_p=*/false);
3775 /* Parse the qualifying entity. */
3777 = cp_parser_class_or_namespace_name (parser,
3783 /* Look for the `::' token. */
3784 cp_parser_require (parser, CPP_SCOPE, "`::'");
3786 /* If we found what we wanted, we keep going; otherwise, we're
3788 if (!cp_parser_parse_definitely (parser))
3790 bool error_p = false;
3792 /* Restore the OLD_SCOPE since it was valid before the
3793 failed attempt at finding the last
3794 class-or-namespace-name. */
3795 parser->scope = old_scope;
3796 parser->qualifying_scope = saved_qualifying_scope;
3797 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3799 /* If the next token is an identifier, and the one after
3800 that is a `::', then any valid interpretation would have
3801 found a class-or-namespace-name. */
3802 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3803 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3805 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3808 token = cp_lexer_consume_token (parser->lexer);
3811 if (!token->ambiguous_p)
3814 tree ambiguous_decls;
3816 decl = cp_parser_lookup_name (parser, token->value,
3818 /*is_template=*/false,
3819 /*is_namespace=*/false,
3820 /*check_dependency=*/true,
3822 if (TREE_CODE (decl) == TEMPLATE_DECL)
3823 error ("%qD used without template parameters", decl);
3824 else if (ambiguous_decls)
3826 error ("reference to %qD is ambiguous",
3828 print_candidates (ambiguous_decls);
3829 decl = error_mark_node;
3832 cp_parser_name_lookup_error
3833 (parser, token->value, decl,
3834 "is not a class or namespace");
3836 parser->scope = error_mark_node;
3838 /* Treat this as a successful nested-name-specifier
3843 If the name found is not a class-name (clause
3844 _class_) or namespace-name (_namespace.def_), the
3845 program is ill-formed. */
3848 cp_lexer_consume_token (parser->lexer);
3852 /* We've found one valid nested-name-specifier. */
3854 /* Name lookup always gives us a DECL. */
3855 if (TREE_CODE (new_scope) == TYPE_DECL)
3856 new_scope = TREE_TYPE (new_scope);
3857 /* Uses of "template" must be followed by actual templates. */
3858 if (template_keyword_p
3859 && !(CLASS_TYPE_P (new_scope)
3860 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3861 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3862 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3863 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3864 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3865 == TEMPLATE_ID_EXPR)))
3866 pedwarn (TYPE_P (new_scope)
3867 ? "%qT is not a template"
3868 : "%qD is not a template",
3870 /* If it is a class scope, try to complete it; we are about to
3871 be looking up names inside the class. */
3872 if (TYPE_P (new_scope)
3873 /* Since checking types for dependency can be expensive,
3874 avoid doing it if the type is already complete. */
3875 && !COMPLETE_TYPE_P (new_scope)
3876 /* Do not try to complete dependent types. */
3877 && !dependent_type_p (new_scope))
3878 new_scope = complete_type (new_scope);
3879 /* Make sure we look in the right scope the next time through
3881 parser->scope = new_scope;
3884 /* If parsing tentatively, replace the sequence of tokens that makes
3885 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3886 token. That way, should we re-parse the token stream, we will
3887 not have to repeat the effort required to do the parse, nor will
3888 we issue duplicate error messages. */
3889 if (success && start)
3894 token = cp_lexer_token_at (parser->lexer, start);
3895 /* Reset the contents of the START token. */
3896 token->type = CPP_NESTED_NAME_SPECIFIER;
3897 /* Retrieve any deferred checks. Do not pop this access checks yet
3898 so the memory will not be reclaimed during token replacing below. */
3899 access_checks = get_deferred_access_checks ();
3900 token->value = build_tree_list (copy_list (access_checks),
3902 TREE_TYPE (token->value) = parser->qualifying_scope;
3903 token->keyword = RID_MAX;
3905 /* Purge all subsequent tokens. */
3906 cp_lexer_purge_tokens_after (parser->lexer, start);
3910 pop_to_parent_deferring_access_checks ();
3912 return success ? parser->scope : NULL_TREE;
3915 /* Parse a nested-name-specifier. See
3916 cp_parser_nested_name_specifier_opt for details. This function
3917 behaves identically, except that it will an issue an error if no
3918 nested-name-specifier is present. */
3921 cp_parser_nested_name_specifier (cp_parser *parser,
3922 bool typename_keyword_p,
3923 bool check_dependency_p,
3925 bool is_declaration)
3929 /* Look for the nested-name-specifier. */
3930 scope = cp_parser_nested_name_specifier_opt (parser,
3935 /* If it was not present, issue an error message. */
3938 cp_parser_error (parser, "expected nested-name-specifier");
3939 parser->scope = NULL_TREE;
3945 /* Parse a class-or-namespace-name.
3947 class-or-namespace-name:
3951 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3952 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3953 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3954 TYPE_P is TRUE iff the next name should be taken as a class-name,
3955 even the same name is declared to be another entity in the same
3958 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3959 specified by the class-or-namespace-name. If neither is found the
3960 ERROR_MARK_NODE is returned. */
3963 cp_parser_class_or_namespace_name (cp_parser *parser,
3964 bool typename_keyword_p,
3965 bool template_keyword_p,
3966 bool check_dependency_p,
3968 bool is_declaration)
3971 tree saved_qualifying_scope;
3972 tree saved_object_scope;
3976 /* Before we try to parse the class-name, we must save away the
3977 current PARSER->SCOPE since cp_parser_class_name will destroy
3979 saved_scope = parser->scope;
3980 saved_qualifying_scope = parser->qualifying_scope;
3981 saved_object_scope = parser->object_scope;
3982 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3983 there is no need to look for a namespace-name. */
3984 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3986 cp_parser_parse_tentatively (parser);
3987 scope = cp_parser_class_name (parser,
3990 type_p ? class_type : none_type,
3992 /*class_head_p=*/false,
3994 /* If that didn't work, try for a namespace-name. */
3995 if (!only_class_p && !cp_parser_parse_definitely (parser))
3997 /* Restore the saved scope. */
3998 parser->scope = saved_scope;
3999 parser->qualifying_scope = saved_qualifying_scope;
4000 parser->object_scope = saved_object_scope;
4001 /* If we are not looking at an identifier followed by the scope
4002 resolution operator, then this is not part of a
4003 nested-name-specifier. (Note that this function is only used
4004 to parse the components of a nested-name-specifier.) */
4005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4006 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4007 return error_mark_node;
4008 scope = cp_parser_namespace_name (parser);
4014 /* Parse a postfix-expression.
4018 postfix-expression [ expression ]
4019 postfix-expression ( expression-list [opt] )
4020 simple-type-specifier ( expression-list [opt] )
4021 typename :: [opt] nested-name-specifier identifier
4022 ( expression-list [opt] )
4023 typename :: [opt] nested-name-specifier template [opt] template-id
4024 ( expression-list [opt] )
4025 postfix-expression . template [opt] id-expression
4026 postfix-expression -> template [opt] id-expression
4027 postfix-expression . pseudo-destructor-name
4028 postfix-expression -> pseudo-destructor-name
4029 postfix-expression ++
4030 postfix-expression --
4031 dynamic_cast < type-id > ( expression )
4032 static_cast < type-id > ( expression )
4033 reinterpret_cast < type-id > ( expression )
4034 const_cast < type-id > ( expression )
4035 typeid ( expression )
4041 ( type-id ) { initializer-list , [opt] }
4043 This extension is a GNU version of the C99 compound-literal
4044 construct. (The C99 grammar uses `type-name' instead of `type-id',
4045 but they are essentially the same concept.)
4047 If ADDRESS_P is true, the postfix expression is the operand of the
4048 `&' operator. CAST_P is true if this expression is the target of a
4051 Returns a representation of the expression. */
4054 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4058 cp_id_kind idk = CP_ID_KIND_NONE;
4059 tree postfix_expression = NULL_TREE;
4061 /* Peek at the next token. */
4062 token = cp_lexer_peek_token (parser->lexer);
4063 /* Some of the productions are determined by keywords. */
4064 keyword = token->keyword;
4074 const char *saved_message;
4076 /* All of these can be handled in the same way from the point
4077 of view of parsing. Begin by consuming the token
4078 identifying the cast. */
4079 cp_lexer_consume_token (parser->lexer);
4081 /* New types cannot be defined in the cast. */
4082 saved_message = parser->type_definition_forbidden_message;
4083 parser->type_definition_forbidden_message
4084 = "types may not be defined in casts";
4086 /* Look for the opening `<'. */
4087 cp_parser_require (parser, CPP_LESS, "`<'");
4088 /* Parse the type to which we are casting. */
4089 type = cp_parser_type_id (parser);
4090 /* Look for the closing `>'. */
4091 cp_parser_require (parser, CPP_GREATER, "`>'");
4092 /* Restore the old message. */
4093 parser->type_definition_forbidden_message = saved_message;
4095 /* And the expression which is being cast. */
4096 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4097 expression = cp_parser_expression (parser, /*cast_p=*/true);
4098 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4100 /* Only type conversions to integral or enumeration types
4101 can be used in constant-expressions. */
4102 if (!cast_valid_in_integral_constant_expression_p (type)
4103 && (cp_parser_non_integral_constant_expression
4105 "a cast to a type other than an integral or "
4106 "enumeration type")))
4107 return error_mark_node;
4113 = build_dynamic_cast (type, expression);
4117 = build_static_cast (type, expression);
4121 = build_reinterpret_cast (type, expression);
4125 = build_const_cast (type, expression);
4136 const char *saved_message;
4137 bool saved_in_type_id_in_expr_p;
4139 /* Consume the `typeid' token. */
4140 cp_lexer_consume_token (parser->lexer);
4141 /* Look for the `(' token. */
4142 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4143 /* Types cannot be defined in a `typeid' expression. */
4144 saved_message = parser->type_definition_forbidden_message;
4145 parser->type_definition_forbidden_message
4146 = "types may not be defined in a `typeid\' expression";
4147 /* We can't be sure yet whether we're looking at a type-id or an
4149 cp_parser_parse_tentatively (parser);
4150 /* Try a type-id first. */
4151 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4152 parser->in_type_id_in_expr_p = true;
4153 type = cp_parser_type_id (parser);
4154 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4155 /* Look for the `)' token. Otherwise, we can't be sure that
4156 we're not looking at an expression: consider `typeid (int
4157 (3))', for example. */
4158 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4159 /* If all went well, simply lookup the type-id. */
4160 if (cp_parser_parse_definitely (parser))
4161 postfix_expression = get_typeid (type);
4162 /* Otherwise, fall back to the expression variant. */
4167 /* Look for an expression. */
4168 expression = cp_parser_expression (parser, /*cast_p=*/false);
4169 /* Compute its typeid. */
4170 postfix_expression = build_typeid (expression);
4171 /* Look for the `)' token. */
4172 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4174 /* Restore the saved message. */
4175 parser->type_definition_forbidden_message = saved_message;
4176 /* `typeid' may not appear in an integral constant expression. */
4177 if (cp_parser_non_integral_constant_expression(parser,
4178 "`typeid' operator"))
4179 return error_mark_node;
4186 /* The syntax permitted here is the same permitted for an
4187 elaborated-type-specifier. */
4188 type = cp_parser_elaborated_type_specifier (parser,
4189 /*is_friend=*/false,
4190 /*is_declaration=*/false);
4191 postfix_expression = cp_parser_functional_cast (parser, type);
4199 /* If the next thing is a simple-type-specifier, we may be
4200 looking at a functional cast. We could also be looking at
4201 an id-expression. So, we try the functional cast, and if
4202 that doesn't work we fall back to the primary-expression. */
4203 cp_parser_parse_tentatively (parser);
4204 /* Look for the simple-type-specifier. */
4205 type = cp_parser_simple_type_specifier (parser,
4206 /*decl_specs=*/NULL,
4207 CP_PARSER_FLAGS_NONE);
4208 /* Parse the cast itself. */
4209 if (!cp_parser_error_occurred (parser))
4211 = cp_parser_functional_cast (parser, type);
4212 /* If that worked, we're done. */
4213 if (cp_parser_parse_definitely (parser))
4216 /* If the functional-cast didn't work out, try a
4217 compound-literal. */
4218 if (cp_parser_allow_gnu_extensions_p (parser)
4219 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4221 VEC(constructor_elt,gc) *initializer_list = NULL;
4222 bool saved_in_type_id_in_expr_p;
4224 cp_parser_parse_tentatively (parser);
4225 /* Consume the `('. */
4226 cp_lexer_consume_token (parser->lexer);
4227 /* Parse the type. */
4228 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4229 parser->in_type_id_in_expr_p = true;
4230 type = cp_parser_type_id (parser);
4231 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4232 /* Look for the `)'. */
4233 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4234 /* Look for the `{'. */
4235 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4236 /* If things aren't going well, there's no need to
4238 if (!cp_parser_error_occurred (parser))
4240 bool non_constant_p;
4241 /* Parse the initializer-list. */
4243 = cp_parser_initializer_list (parser, &non_constant_p);
4244 /* Allow a trailing `,'. */
4245 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4246 cp_lexer_consume_token (parser->lexer);
4247 /* Look for the final `}'. */
4248 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4250 /* If that worked, we're definitely looking at a
4251 compound-literal expression. */
4252 if (cp_parser_parse_definitely (parser))
4254 /* Warn the user that a compound literal is not
4255 allowed in standard C++. */
4257 pedwarn ("ISO C++ forbids compound-literals");
4258 /* Form the representation of the compound-literal. */
4260 = finish_compound_literal (type, initializer_list);
4265 /* It must be a primary-expression. */
4267 = cp_parser_primary_expression (parser, address_p, cast_p,
4268 /*template_arg_p=*/false,
4274 /* Keep looping until the postfix-expression is complete. */
4277 if (idk == CP_ID_KIND_UNQUALIFIED
4278 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4279 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4280 /* It is not a Koenig lookup function call. */
4282 = unqualified_name_lookup_error (postfix_expression);
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4287 switch (token->type)
4289 case CPP_OPEN_SQUARE:
4291 = cp_parser_postfix_open_square_expression (parser,
4294 idk = CP_ID_KIND_NONE;
4297 case CPP_OPEN_PAREN:
4298 /* postfix-expression ( expression-list [opt] ) */
4301 bool is_builtin_constant_p;
4302 bool saved_integral_constant_expression_p = false;
4303 bool saved_non_integral_constant_expression_p = false;
4306 is_builtin_constant_p
4307 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4308 if (is_builtin_constant_p)
4310 /* The whole point of __builtin_constant_p is to allow
4311 non-constant expressions to appear as arguments. */
4312 saved_integral_constant_expression_p
4313 = parser->integral_constant_expression_p;
4314 saved_non_integral_constant_expression_p
4315 = parser->non_integral_constant_expression_p;
4316 parser->integral_constant_expression_p = false;
4318 args = (cp_parser_parenthesized_expression_list
4319 (parser, /*is_attribute_list=*/false,
4321 /*non_constant_p=*/NULL));
4322 if (is_builtin_constant_p)
4324 parser->integral_constant_expression_p
4325 = saved_integral_constant_expression_p;
4326 parser->non_integral_constant_expression_p
4327 = saved_non_integral_constant_expression_p;
4330 if (args == error_mark_node)
4332 postfix_expression = error_mark_node;
4336 /* Function calls are not permitted in
4337 constant-expressions. */
4338 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4339 && cp_parser_non_integral_constant_expression (parser,
4342 postfix_expression = error_mark_node;
4347 if (idk == CP_ID_KIND_UNQUALIFIED)
4349 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4355 = perform_koenig_lookup (postfix_expression, args);
4359 = unqualified_fn_lookup_error (postfix_expression);
4361 /* We do not perform argument-dependent lookup if
4362 normal lookup finds a non-function, in accordance
4363 with the expected resolution of DR 218. */
4364 else if (args && is_overloaded_fn (postfix_expression))
4366 tree fn = get_first_fn (postfix_expression);
4368 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4369 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4371 /* Only do argument dependent lookup if regular
4372 lookup does not find a set of member functions.
4373 [basic.lookup.koenig]/2a */
4374 if (!DECL_FUNCTION_MEMBER_P (fn))
4378 = perform_koenig_lookup (postfix_expression, args);
4383 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4385 tree instance = TREE_OPERAND (postfix_expression, 0);
4386 tree fn = TREE_OPERAND (postfix_expression, 1);
4388 if (processing_template_decl
4389 && (type_dependent_expression_p (instance)
4390 || (!BASELINK_P (fn)
4391 && TREE_CODE (fn) != FIELD_DECL)
4392 || type_dependent_expression_p (fn)
4393 || any_type_dependent_arguments_p (args)))
4396 = build_min_nt (CALL_EXPR, postfix_expression,
4401 if (BASELINK_P (fn))
4403 = (build_new_method_call
4404 (instance, fn, args, NULL_TREE,
4405 (idk == CP_ID_KIND_QUALIFIED
4406 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4410 = finish_call_expr (postfix_expression, args,
4411 /*disallow_virtual=*/false,
4412 /*koenig_p=*/false);
4414 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4415 || TREE_CODE (postfix_expression) == MEMBER_REF
4416 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4417 postfix_expression = (build_offset_ref_call_from_tree
4418 (postfix_expression, args));
4419 else if (idk == CP_ID_KIND_QUALIFIED)
4420 /* A call to a static class member, or a namespace-scope
4423 = finish_call_expr (postfix_expression, args,
4424 /*disallow_virtual=*/true,
4427 /* All other function calls. */
4429 = finish_call_expr (postfix_expression, args,
4430 /*disallow_virtual=*/false,
4433 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4434 idk = CP_ID_KIND_NONE;
4440 /* postfix-expression . template [opt] id-expression
4441 postfix-expression . pseudo-destructor-name
4442 postfix-expression -> template [opt] id-expression
4443 postfix-expression -> pseudo-destructor-name */
4445 /* Consume the `.' or `->' operator. */
4446 cp_lexer_consume_token (parser->lexer);
4449 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4455 /* postfix-expression ++ */
4456 /* Consume the `++' token. */
4457 cp_lexer_consume_token (parser->lexer);
4458 /* Generate a representation for the complete expression. */
4460 = finish_increment_expr (postfix_expression,
4461 POSTINCREMENT_EXPR);
4462 /* Increments may not appear in constant-expressions. */
4463 if (cp_parser_non_integral_constant_expression (parser,
4465 postfix_expression = error_mark_node;
4466 idk = CP_ID_KIND_NONE;
4469 case CPP_MINUS_MINUS:
4470 /* postfix-expression -- */
4471 /* Consume the `--' token. */
4472 cp_lexer_consume_token (parser->lexer);
4473 /* Generate a representation for the complete expression. */
4475 = finish_increment_expr (postfix_expression,
4476 POSTDECREMENT_EXPR);
4477 /* Decrements may not appear in constant-expressions. */
4478 if (cp_parser_non_integral_constant_expression (parser,
4480 postfix_expression = error_mark_node;
4481 idk = CP_ID_KIND_NONE;
4485 return postfix_expression;
4489 /* We should never get here. */
4491 return error_mark_node;
4494 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4495 by cp_parser_builtin_offsetof. We're looking for
4497 postfix-expression [ expression ]
4499 FOR_OFFSETOF is set if we're being called in that context, which
4500 changes how we deal with integer constant expressions. */
4503 cp_parser_postfix_open_square_expression (cp_parser *parser,
4504 tree postfix_expression,
4509 /* Consume the `[' token. */
4510 cp_lexer_consume_token (parser->lexer);
4512 /* Parse the index expression. */
4513 /* ??? For offsetof, there is a question of what to allow here. If
4514 offsetof is not being used in an integral constant expression context,
4515 then we *could* get the right answer by computing the value at runtime.
4516 If we are in an integral constant expression context, then we might
4517 could accept any constant expression; hard to say without analysis.
4518 Rather than open the barn door too wide right away, allow only integer
4519 constant expressions here. */
4521 index = cp_parser_constant_expression (parser, false, NULL);
4523 index = cp_parser_expression (parser, /*cast_p=*/false);
4525 /* Look for the closing `]'. */
4526 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4528 /* Build the ARRAY_REF. */
4529 postfix_expression = grok_array_decl (postfix_expression, index);
4531 /* When not doing offsetof, array references are not permitted in
4532 constant-expressions. */
4534 && (cp_parser_non_integral_constant_expression
4535 (parser, "an array reference")))
4536 postfix_expression = error_mark_node;
4538 return postfix_expression;
4541 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4542 by cp_parser_builtin_offsetof. We're looking for
4544 postfix-expression . template [opt] id-expression
4545 postfix-expression . pseudo-destructor-name
4546 postfix-expression -> template [opt] id-expression
4547 postfix-expression -> pseudo-destructor-name
4549 FOR_OFFSETOF is set if we're being called in that context. That sorta
4550 limits what of the above we'll actually accept, but nevermind.
4551 TOKEN_TYPE is the "." or "->" token, which will already have been
4552 removed from the stream. */
4555 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4556 enum cpp_ttype token_type,
4557 tree postfix_expression,
4558 bool for_offsetof, cp_id_kind *idk)
4562 bool pseudo_destructor_p;
4563 tree scope = NULL_TREE;
4565 /* If this is a `->' operator, dereference the pointer. */
4566 if (token_type == CPP_DEREF)
4567 postfix_expression = build_x_arrow (postfix_expression);
4568 /* Check to see whether or not the expression is type-dependent. */
4569 dependent_p = type_dependent_expression_p (postfix_expression);
4570 /* The identifier following the `->' or `.' is not qualified. */
4571 parser->scope = NULL_TREE;
4572 parser->qualifying_scope = NULL_TREE;
4573 parser->object_scope = NULL_TREE;
4574 *idk = CP_ID_KIND_NONE;
4575 /* Enter the scope corresponding to the type of the object
4576 given by the POSTFIX_EXPRESSION. */
4577 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4579 scope = TREE_TYPE (postfix_expression);
4580 /* According to the standard, no expression should ever have
4581 reference type. Unfortunately, we do not currently match
4582 the standard in this respect in that our internal representation
4583 of an expression may have reference type even when the standard
4584 says it does not. Therefore, we have to manually obtain the
4585 underlying type here. */
4586 scope = non_reference (scope);
4587 /* The type of the POSTFIX_EXPRESSION must be complete. */
4588 if (scope == unknown_type_node)
4590 error ("%qE does not have class type", postfix_expression);
4594 scope = complete_type_or_else (scope, NULL_TREE);
4595 /* Let the name lookup machinery know that we are processing a
4596 class member access expression. */
4597 parser->context->object_type = scope;
4598 /* If something went wrong, we want to be able to discern that case,
4599 as opposed to the case where there was no SCOPE due to the type
4600 of expression being dependent. */
4602 scope = error_mark_node;
4603 /* If the SCOPE was erroneous, make the various semantic analysis
4604 functions exit quickly -- and without issuing additional error
4606 if (scope == error_mark_node)
4607 postfix_expression = error_mark_node;
4610 /* Assume this expression is not a pseudo-destructor access. */
4611 pseudo_destructor_p = false;
4613 /* If the SCOPE is a scalar type, then, if this is a valid program,
4614 we must be looking at a pseudo-destructor-name. */
4615 if (scope && SCALAR_TYPE_P (scope))
4620 cp_parser_parse_tentatively (parser);
4621 /* Parse the pseudo-destructor-name. */
4623 cp_parser_pseudo_destructor_name (parser, &s, &type);
4624 if (cp_parser_parse_definitely (parser))
4626 pseudo_destructor_p = true;
4628 = finish_pseudo_destructor_expr (postfix_expression,
4629 s, TREE_TYPE (type));
4633 if (!pseudo_destructor_p)
4635 /* If the SCOPE is not a scalar type, we are looking at an
4636 ordinary class member access expression, rather than a
4637 pseudo-destructor-name. */
4639 /* Parse the id-expression. */
4640 name = (cp_parser_id_expression
4642 cp_parser_optional_template_keyword (parser),
4643 /*check_dependency_p=*/true,
4645 /*declarator_p=*/false,
4646 /*optional_p=*/false));
4647 /* In general, build a SCOPE_REF if the member name is qualified.
4648 However, if the name was not dependent and has already been
4649 resolved; there is no need to build the SCOPE_REF. For example;
4651 struct X { void f(); };
4652 template <typename T> void f(T* t) { t->X::f(); }
4654 Even though "t" is dependent, "X::f" is not and has been resolved
4655 to a BASELINK; there is no need to include scope information. */
4657 /* But we do need to remember that there was an explicit scope for
4658 virtual function calls. */
4660 *idk = CP_ID_KIND_QUALIFIED;
4662 /* If the name is a template-id that names a type, we will get a
4663 TYPE_DECL here. That is invalid code. */
4664 if (TREE_CODE (name) == TYPE_DECL)
4666 error ("invalid use of %qD", name);
4667 postfix_expression = error_mark_node;
4671 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4673 name = build_qualified_name (/*type=*/NULL_TREE,
4677 parser->scope = NULL_TREE;
4678 parser->qualifying_scope = NULL_TREE;
4679 parser->object_scope = NULL_TREE;
4681 if (scope && name && BASELINK_P (name))
4682 adjust_result_of_qualified_name_lookup
4683 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4685 = finish_class_member_access_expr (postfix_expression, name,
4690 /* We no longer need to look up names in the scope of the object on
4691 the left-hand side of the `.' or `->' operator. */
4692 parser->context->object_type = NULL_TREE;
4694 /* Outside of offsetof, these operators may not appear in
4695 constant-expressions. */
4697 && (cp_parser_non_integral_constant_expression
4698 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4699 postfix_expression = error_mark_node;
4701 return postfix_expression;
4704 /* Parse a parenthesized expression-list.
4707 assignment-expression
4708 expression-list, assignment-expression
4713 identifier, expression-list
4715 CAST_P is true if this expression is the target of a cast.
4717 Returns a TREE_LIST. The TREE_VALUE of each node is a
4718 representation of an assignment-expression. Note that a TREE_LIST
4719 is returned even if there is only a single expression in the list.
4720 error_mark_node is returned if the ( and or ) are
4721 missing. NULL_TREE is returned on no expressions. The parentheses
4722 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4723 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4724 indicates whether or not all of the expressions in the list were
4728 cp_parser_parenthesized_expression_list (cp_parser* parser,
4729 bool is_attribute_list,
4731 bool *non_constant_p)
4733 tree expression_list = NULL_TREE;
4734 bool fold_expr_p = is_attribute_list;
4735 tree identifier = NULL_TREE;
4737 /* Assume all the expressions will be constant. */
4739 *non_constant_p = false;
4741 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4742 return error_mark_node;
4744 /* Consume expressions until there are no more. */
4745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4750 /* At the beginning of attribute lists, check to see if the
4751 next token is an identifier. */
4752 if (is_attribute_list
4753 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4757 /* Consume the identifier. */
4758 token = cp_lexer_consume_token (parser->lexer);
4759 /* Save the identifier. */
4760 identifier = token->value;
4764 /* Parse the next assignment-expression. */
4767 bool expr_non_constant_p;
4768 expr = (cp_parser_constant_expression
4769 (parser, /*allow_non_constant_p=*/true,
4770 &expr_non_constant_p));
4771 if (expr_non_constant_p)
4772 *non_constant_p = true;
4775 expr = cp_parser_assignment_expression (parser, cast_p);
4778 expr = fold_non_dependent_expr (expr);
4780 /* Add it to the list. We add error_mark_node
4781 expressions to the list, so that we can still tell if
4782 the correct form for a parenthesized expression-list
4783 is found. That gives better errors. */
4784 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4786 if (expr == error_mark_node)
4790 /* After the first item, attribute lists look the same as
4791 expression lists. */
4792 is_attribute_list = false;
4795 /* If the next token isn't a `,', then we are done. */
4796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4799 /* Otherwise, consume the `,' and keep going. */
4800 cp_lexer_consume_token (parser->lexer);
4803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4808 /* We try and resync to an unnested comma, as that will give the
4809 user better diagnostics. */
4810 ending = cp_parser_skip_to_closing_parenthesis (parser,
4811 /*recovering=*/true,
4813 /*consume_paren=*/true);
4817 return error_mark_node;
4820 /* We built up the list in reverse order so we must reverse it now. */
4821 expression_list = nreverse (expression_list);
4823 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4825 return expression_list;
4828 /* Parse a pseudo-destructor-name.
4830 pseudo-destructor-name:
4831 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4832 :: [opt] nested-name-specifier template template-id :: ~ type-name
4833 :: [opt] nested-name-specifier [opt] ~ type-name
4835 If either of the first two productions is used, sets *SCOPE to the
4836 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4837 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4838 or ERROR_MARK_NODE if the parse fails. */
4841 cp_parser_pseudo_destructor_name (cp_parser* parser,
4845 bool nested_name_specifier_p;
4847 /* Assume that things will not work out. */
4848 *type = error_mark_node;
4850 /* Look for the optional `::' operator. */
4851 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4852 /* Look for the optional nested-name-specifier. */
4853 nested_name_specifier_p
4854 = (cp_parser_nested_name_specifier_opt (parser,
4855 /*typename_keyword_p=*/false,
4856 /*check_dependency_p=*/true,
4858 /*is_declaration=*/true)
4860 /* Now, if we saw a nested-name-specifier, we might be doing the
4861 second production. */
4862 if (nested_name_specifier_p
4863 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4865 /* Consume the `template' keyword. */
4866 cp_lexer_consume_token (parser->lexer);
4867 /* Parse the template-id. */
4868 cp_parser_template_id (parser,
4869 /*template_keyword_p=*/true,
4870 /*check_dependency_p=*/false,
4871 /*is_declaration=*/true);
4872 /* Look for the `::' token. */
4873 cp_parser_require (parser, CPP_SCOPE, "`::'");
4875 /* If the next token is not a `~', then there might be some
4876 additional qualification. */
4877 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4879 /* Look for the type-name. */
4880 *scope = TREE_TYPE (cp_parser_type_name (parser));
4882 if (*scope == error_mark_node)
4885 /* If we don't have ::~, then something has gone wrong. Since
4886 the only caller of this function is looking for something
4887 after `.' or `->' after a scalar type, most likely the
4888 program is trying to get a member of a non-aggregate
4890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4891 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4893 cp_parser_error (parser, "request for member of non-aggregate type");
4897 /* Look for the `::' token. */
4898 cp_parser_require (parser, CPP_SCOPE, "`::'");
4903 /* Look for the `~'. */
4904 cp_parser_require (parser, CPP_COMPL, "`~'");
4905 /* Look for the type-name again. We are not responsible for
4906 checking that it matches the first type-name. */
4907 *type = cp_parser_type_name (parser);
4910 /* Parse a unary-expression.
4916 unary-operator cast-expression
4917 sizeof unary-expression
4925 __extension__ cast-expression
4926 __alignof__ unary-expression
4927 __alignof__ ( type-id )
4928 __real__ cast-expression
4929 __imag__ cast-expression
4932 ADDRESS_P is true iff the unary-expression is appearing as the
4933 operand of the `&' operator. CAST_P is true if this expression is
4934 the target of a cast.
4936 Returns a representation of the expression. */
4939 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4942 enum tree_code unary_operator;
4944 /* Peek at the next token. */
4945 token = cp_lexer_peek_token (parser->lexer);
4946 /* Some keywords give away the kind of expression. */
4947 if (token->type == CPP_KEYWORD)
4949 enum rid keyword = token->keyword;
4959 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4960 /* Consume the token. */
4961 cp_lexer_consume_token (parser->lexer);
4962 /* Parse the operand. */
4963 operand = cp_parser_sizeof_operand (parser, keyword);
4965 if (TYPE_P (operand))
4966 return cxx_sizeof_or_alignof_type (operand, op, true);
4968 return cxx_sizeof_or_alignof_expr (operand, op);
4972 return cp_parser_new_expression (parser);
4975 return cp_parser_delete_expression (parser);
4979 /* The saved value of the PEDANTIC flag. */
4983 /* Save away the PEDANTIC flag. */
4984 cp_parser_extension_opt (parser, &saved_pedantic);
4985 /* Parse the cast-expression. */
4986 expr = cp_parser_simple_cast_expression (parser);
4987 /* Restore the PEDANTIC flag. */
4988 pedantic = saved_pedantic;
4998 /* Consume the `__real__' or `__imag__' token. */
4999 cp_lexer_consume_token (parser->lexer);
5000 /* Parse the cast-expression. */
5001 expression = cp_parser_simple_cast_expression (parser);
5002 /* Create the complete representation. */
5003 return build_x_unary_op ((keyword == RID_REALPART
5004 ? REALPART_EXPR : IMAGPART_EXPR),
5014 /* Look for the `:: new' and `:: delete', which also signal the
5015 beginning of a new-expression, or delete-expression,
5016 respectively. If the next token is `::', then it might be one of
5018 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5022 /* See if the token after the `::' is one of the keywords in
5023 which we're interested. */
5024 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5025 /* If it's `new', we have a new-expression. */
5026 if (keyword == RID_NEW)
5027 return cp_parser_new_expression (parser);
5028 /* Similarly, for `delete'. */
5029 else if (keyword == RID_DELETE)
5030 return cp_parser_delete_expression (parser);
5033 /* Look for a unary operator. */
5034 unary_operator = cp_parser_unary_operator (token);
5035 /* The `++' and `--' operators can be handled similarly, even though
5036 they are not technically unary-operators in the grammar. */
5037 if (unary_operator == ERROR_MARK)
5039 if (token->type == CPP_PLUS_PLUS)
5040 unary_operator = PREINCREMENT_EXPR;
5041 else if (token->type == CPP_MINUS_MINUS)
5042 unary_operator = PREDECREMENT_EXPR;
5043 /* Handle the GNU address-of-label extension. */
5044 else if (cp_parser_allow_gnu_extensions_p (parser)
5045 && token->type == CPP_AND_AND)
5049 /* Consume the '&&' token. */
5050 cp_lexer_consume_token (parser->lexer);
5051 /* Look for the identifier. */
5052 identifier = cp_parser_identifier (parser);
5053 /* Create an expression representing the address. */
5054 return finish_label_address_expr (identifier);
5057 if (unary_operator != ERROR_MARK)
5059 tree cast_expression;
5060 tree expression = error_mark_node;
5061 const char *non_constant_p = NULL;
5063 /* Consume the operator token. */
5064 token = cp_lexer_consume_token (parser->lexer);
5065 /* Parse the cast-expression. */
5067 = cp_parser_cast_expression (parser,
5068 unary_operator == ADDR_EXPR,
5070 /* Now, build an appropriate representation. */
5071 switch (unary_operator)
5074 non_constant_p = "`*'";
5075 expression = build_x_indirect_ref (cast_expression, "unary *");
5079 non_constant_p = "`&'";
5082 expression = build_x_unary_op (unary_operator, cast_expression);
5085 case PREINCREMENT_EXPR:
5086 case PREDECREMENT_EXPR:
5087 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5090 case UNARY_PLUS_EXPR:
5092 case TRUTH_NOT_EXPR:
5093 expression = finish_unary_op_expr (unary_operator, cast_expression);
5101 && cp_parser_non_integral_constant_expression (parser,
5103 expression = error_mark_node;
5108 return cp_parser_postfix_expression (parser, address_p, cast_p);
5111 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5112 unary-operator, the corresponding tree code is returned. */
5114 static enum tree_code
5115 cp_parser_unary_operator (cp_token* token)
5117 switch (token->type)
5120 return INDIRECT_REF;
5126 return UNARY_PLUS_EXPR;
5132 return TRUTH_NOT_EXPR;
5135 return BIT_NOT_EXPR;
5142 /* Parse a new-expression.
5145 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5146 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5148 Returns a representation of the expression. */
5151 cp_parser_new_expression (cp_parser* parser)
5153 bool global_scope_p;
5159 /* Look for the optional `::' operator. */
5161 = (cp_parser_global_scope_opt (parser,
5162 /*current_scope_valid_p=*/false)
5164 /* Look for the `new' operator. */
5165 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5166 /* There's no easy way to tell a new-placement from the
5167 `( type-id )' construct. */
5168 cp_parser_parse_tentatively (parser);
5169 /* Look for a new-placement. */
5170 placement = cp_parser_new_placement (parser);
5171 /* If that didn't work out, there's no new-placement. */
5172 if (!cp_parser_parse_definitely (parser))
5173 placement = NULL_TREE;
5175 /* If the next token is a `(', then we have a parenthesized
5177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5179 /* Consume the `('. */
5180 cp_lexer_consume_token (parser->lexer);
5181 /* Parse the type-id. */
5182 type = cp_parser_type_id (parser);
5183 /* Look for the closing `)'. */
5184 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5185 /* There should not be a direct-new-declarator in this production,
5186 but GCC used to allowed this, so we check and emit a sensible error
5187 message for this case. */
5188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5190 error ("array bound forbidden after parenthesized type-id");
5191 inform ("try removing the parentheses around the type-id");
5192 cp_parser_direct_new_declarator (parser);
5196 /* Otherwise, there must be a new-type-id. */
5198 type = cp_parser_new_type_id (parser, &nelts);
5200 /* If the next token is a `(', then we have a new-initializer. */
5201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5202 initializer = cp_parser_new_initializer (parser);
5204 initializer = NULL_TREE;
5206 /* A new-expression may not appear in an integral constant
5208 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5209 return error_mark_node;
5211 /* Create a representation of the new-expression. */
5212 return build_new (placement, type, nelts, initializer, global_scope_p);
5215 /* Parse a new-placement.
5220 Returns the same representation as for an expression-list. */
5223 cp_parser_new_placement (cp_parser* parser)
5225 tree expression_list;
5227 /* Parse the expression-list. */
5228 expression_list = (cp_parser_parenthesized_expression_list
5229 (parser, false, /*cast_p=*/false,
5230 /*non_constant_p=*/NULL));
5232 return expression_list;
5235 /* Parse a new-type-id.
5238 type-specifier-seq new-declarator [opt]
5240 Returns the TYPE allocated. If the new-type-id indicates an array
5241 type, *NELTS is set to the number of elements in the last array
5242 bound; the TYPE will not include the last array bound. */
5245 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5247 cp_decl_specifier_seq type_specifier_seq;
5248 cp_declarator *new_declarator;
5249 cp_declarator *declarator;
5250 cp_declarator *outer_declarator;
5251 const char *saved_message;
5254 /* The type-specifier sequence must not contain type definitions.
5255 (It cannot contain declarations of new types either, but if they
5256 are not definitions we will catch that because they are not
5258 saved_message = parser->type_definition_forbidden_message;
5259 parser->type_definition_forbidden_message
5260 = "types may not be defined in a new-type-id";
5261 /* Parse the type-specifier-seq. */
5262 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5263 &type_specifier_seq);
5264 /* Restore the old message. */
5265 parser->type_definition_forbidden_message = saved_message;
5266 /* Parse the new-declarator. */
5267 new_declarator = cp_parser_new_declarator_opt (parser);
5269 /* Determine the number of elements in the last array dimension, if
5272 /* Skip down to the last array dimension. */
5273 declarator = new_declarator;
5274 outer_declarator = NULL;
5275 while (declarator && (declarator->kind == cdk_pointer
5276 || declarator->kind == cdk_ptrmem))
5278 outer_declarator = declarator;
5279 declarator = declarator->declarator;
5282 && declarator->kind == cdk_array
5283 && declarator->declarator
5284 && declarator->declarator->kind == cdk_array)
5286 outer_declarator = declarator;
5287 declarator = declarator->declarator;
5290 if (declarator && declarator->kind == cdk_array)
5292 *nelts = declarator->u.array.bounds;
5293 if (*nelts == error_mark_node)
5294 *nelts = integer_one_node;
5296 if (outer_declarator)
5297 outer_declarator->declarator = declarator->declarator;
5299 new_declarator = NULL;
5302 type = groktypename (&type_specifier_seq, new_declarator);
5303 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5305 *nelts = array_type_nelts_top (type);
5306 type = TREE_TYPE (type);
5311 /* Parse an (optional) new-declarator.
5314 ptr-operator new-declarator [opt]
5315 direct-new-declarator
5317 Returns the declarator. */
5319 static cp_declarator *
5320 cp_parser_new_declarator_opt (cp_parser* parser)
5322 enum tree_code code;
5324 cp_cv_quals cv_quals;
5326 /* We don't know if there's a ptr-operator next, or not. */
5327 cp_parser_parse_tentatively (parser);
5328 /* Look for a ptr-operator. */
5329 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5330 /* If that worked, look for more new-declarators. */
5331 if (cp_parser_parse_definitely (parser))
5333 cp_declarator *declarator;
5335 /* Parse another optional declarator. */
5336 declarator = cp_parser_new_declarator_opt (parser);
5338 /* Create the representation of the declarator. */
5340 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5341 else if (code == INDIRECT_REF)
5342 declarator = make_pointer_declarator (cv_quals, declarator);
5344 declarator = make_reference_declarator (cv_quals, declarator);
5349 /* If the next token is a `[', there is a direct-new-declarator. */
5350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5351 return cp_parser_direct_new_declarator (parser);
5356 /* Parse a direct-new-declarator.
5358 direct-new-declarator:
5360 direct-new-declarator [constant-expression]
5364 static cp_declarator *
5365 cp_parser_direct_new_declarator (cp_parser* parser)
5367 cp_declarator *declarator = NULL;
5373 /* Look for the opening `['. */
5374 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5375 /* The first expression is not required to be constant. */
5378 expression = cp_parser_expression (parser, /*cast_p=*/false);
5379 /* The standard requires that the expression have integral
5380 type. DR 74 adds enumeration types. We believe that the
5381 real intent is that these expressions be handled like the
5382 expression in a `switch' condition, which also allows
5383 classes with a single conversion to integral or
5384 enumeration type. */
5385 if (!processing_template_decl)
5388 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5393 error ("expression in new-declarator must have integral "
5394 "or enumeration type");
5395 expression = error_mark_node;
5399 /* But all the other expressions must be. */
5402 = cp_parser_constant_expression (parser,
5403 /*allow_non_constant=*/false,
5405 /* Look for the closing `]'. */
5406 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5408 /* Add this bound to the declarator. */
5409 declarator = make_array_declarator (declarator, expression);
5411 /* If the next token is not a `[', then there are no more
5413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5420 /* Parse a new-initializer.
5423 ( expression-list [opt] )
5425 Returns a representation of the expression-list. If there is no
5426 expression-list, VOID_ZERO_NODE is returned. */
5429 cp_parser_new_initializer (cp_parser* parser)
5431 tree expression_list;
5433 expression_list = (cp_parser_parenthesized_expression_list
5434 (parser, false, /*cast_p=*/false,
5435 /*non_constant_p=*/NULL));
5436 if (!expression_list)
5437 expression_list = void_zero_node;
5439 return expression_list;
5442 /* Parse a delete-expression.
5445 :: [opt] delete cast-expression
5446 :: [opt] delete [ ] cast-expression
5448 Returns a representation of the expression. */
5451 cp_parser_delete_expression (cp_parser* parser)
5453 bool global_scope_p;
5457 /* Look for the optional `::' operator. */
5459 = (cp_parser_global_scope_opt (parser,
5460 /*current_scope_valid_p=*/false)
5462 /* Look for the `delete' keyword. */
5463 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5464 /* See if the array syntax is in use. */
5465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5467 /* Consume the `[' token. */
5468 cp_lexer_consume_token (parser->lexer);
5469 /* Look for the `]' token. */
5470 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5471 /* Remember that this is the `[]' construct. */
5477 /* Parse the cast-expression. */
5478 expression = cp_parser_simple_cast_expression (parser);
5480 /* A delete-expression may not appear in an integral constant
5482 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5483 return error_mark_node;
5485 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5488 /* Parse a cast-expression.
5492 ( type-id ) cast-expression
5494 ADDRESS_P is true iff the unary-expression is appearing as the
5495 operand of the `&' operator. CAST_P is true if this expression is
5496 the target of a cast.
5498 Returns a representation of the expression. */
5501 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5503 /* If it's a `(', then we might be looking at a cast. */
5504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5506 tree type = NULL_TREE;
5507 tree expr = NULL_TREE;
5508 bool compound_literal_p;
5509 const char *saved_message;
5511 /* There's no way to know yet whether or not this is a cast.
5512 For example, `(int (3))' is a unary-expression, while `(int)
5513 3' is a cast. So, we resort to parsing tentatively. */
5514 cp_parser_parse_tentatively (parser);
5515 /* Types may not be defined in a cast. */
5516 saved_message = parser->type_definition_forbidden_message;
5517 parser->type_definition_forbidden_message
5518 = "types may not be defined in casts";
5519 /* Consume the `('. */
5520 cp_lexer_consume_token (parser->lexer);
5521 /* A very tricky bit is that `(struct S) { 3 }' is a
5522 compound-literal (which we permit in C++ as an extension).
5523 But, that construct is not a cast-expression -- it is a
5524 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5525 is legal; if the compound-literal were a cast-expression,
5526 you'd need an extra set of parentheses.) But, if we parse
5527 the type-id, and it happens to be a class-specifier, then we
5528 will commit to the parse at that point, because we cannot
5529 undo the action that is done when creating a new class. So,
5530 then we cannot back up and do a postfix-expression.
5532 Therefore, we scan ahead to the closing `)', and check to see
5533 if the token after the `)' is a `{'. If so, we are not
5534 looking at a cast-expression.
5536 Save tokens so that we can put them back. */
5537 cp_lexer_save_tokens (parser->lexer);
5538 /* Skip tokens until the next token is a closing parenthesis.
5539 If we find the closing `)', and the next token is a `{', then
5540 we are looking at a compound-literal. */
5542 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5543 /*consume_paren=*/true)
5544 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5545 /* Roll back the tokens we skipped. */
5546 cp_lexer_rollback_tokens (parser->lexer);
5547 /* If we were looking at a compound-literal, simulate an error
5548 so that the call to cp_parser_parse_definitely below will
5550 if (compound_literal_p)
5551 cp_parser_simulate_error (parser);
5554 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5555 parser->in_type_id_in_expr_p = true;
5556 /* Look for the type-id. */
5557 type = cp_parser_type_id (parser);
5558 /* Look for the closing `)'. */
5559 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5560 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5563 /* Restore the saved message. */
5564 parser->type_definition_forbidden_message = saved_message;
5566 /* If ok so far, parse the dependent expression. We cannot be
5567 sure it is a cast. Consider `(T ())'. It is a parenthesized
5568 ctor of T, but looks like a cast to function returning T
5569 without a dependent expression. */
5570 if (!cp_parser_error_occurred (parser))
5571 expr = cp_parser_cast_expression (parser,
5572 /*address_p=*/false,
5575 if (cp_parser_parse_definitely (parser))
5577 /* Warn about old-style casts, if so requested. */
5578 if (warn_old_style_cast
5579 && !in_system_header
5580 && !VOID_TYPE_P (type)
5581 && current_lang_name != lang_name_c)
5582 warning (OPT_Wold_style_cast, "use of old-style cast");
5584 /* Only type conversions to integral or enumeration types
5585 can be used in constant-expressions. */
5586 if (!cast_valid_in_integral_constant_expression_p (type)
5587 && (cp_parser_non_integral_constant_expression
5589 "a cast to a type other than an integral or "
5590 "enumeration type")))
5591 return error_mark_node;
5593 /* Perform the cast. */
5594 expr = build_c_cast (type, expr);
5599 /* If we get here, then it's not a cast, so it must be a
5600 unary-expression. */
5601 return cp_parser_unary_expression (parser, address_p, cast_p);
5604 /* Parse a binary expression of the general form:
5608 pm-expression .* cast-expression
5609 pm-expression ->* cast-expression
5611 multiplicative-expression:
5613 multiplicative-expression * pm-expression
5614 multiplicative-expression / pm-expression
5615 multiplicative-expression % pm-expression
5617 additive-expression:
5618 multiplicative-expression
5619 additive-expression + multiplicative-expression
5620 additive-expression - multiplicative-expression
5624 shift-expression << additive-expression
5625 shift-expression >> additive-expression
5627 relational-expression:
5629 relational-expression < shift-expression
5630 relational-expression > shift-expression
5631 relational-expression <= shift-expression
5632 relational-expression >= shift-expression
5636 relational-expression:
5637 relational-expression <? shift-expression
5638 relational-expression >? shift-expression
5640 equality-expression:
5641 relational-expression
5642 equality-expression == relational-expression
5643 equality-expression != relational-expression
5647 and-expression & equality-expression
5649 exclusive-or-expression:
5651 exclusive-or-expression ^ and-expression
5653 inclusive-or-expression:
5654 exclusive-or-expression
5655 inclusive-or-expression | exclusive-or-expression
5657 logical-and-expression:
5658 inclusive-or-expression
5659 logical-and-expression && inclusive-or-expression
5661 logical-or-expression:
5662 logical-and-expression
5663 logical-or-expression || logical-and-expression
5665 All these are implemented with a single function like:
5668 simple-cast-expression
5669 binary-expression <token> binary-expression
5671 CAST_P is true if this expression is the target of a cast.
5673 The binops_by_token map is used to get the tree codes for each <token> type.
5674 binary-expressions are associated according to a precedence table. */
5676 #define TOKEN_PRECEDENCE(token) \
5677 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5678 ? PREC_NOT_OPERATOR \
5679 : binops_by_token[token->type].prec)
5682 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5684 cp_parser_expression_stack stack;
5685 cp_parser_expression_stack_entry *sp = &stack[0];
5688 enum tree_code tree_type;
5689 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5692 /* Parse the first expression. */
5693 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5697 /* Get an operator token. */
5698 token = cp_lexer_peek_token (parser->lexer);
5700 new_prec = TOKEN_PRECEDENCE (token);
5702 /* Popping an entry off the stack means we completed a subexpression:
5703 - either we found a token which is not an operator (`>' where it is not
5704 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5705 will happen repeatedly;
5706 - or, we found an operator which has lower priority. This is the case
5707 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5709 if (new_prec <= prec)
5718 tree_type = binops_by_token[token->type].tree_type;
5720 /* We used the operator token. */
5721 cp_lexer_consume_token (parser->lexer);
5723 /* Extract another operand. It may be the RHS of this expression
5724 or the LHS of a new, higher priority expression. */
5725 rhs = cp_parser_simple_cast_expression (parser);
5727 /* Get another operator token. Look up its precedence to avoid
5728 building a useless (immediately popped) stack entry for common
5729 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5730 token = cp_lexer_peek_token (parser->lexer);
5731 lookahead_prec = TOKEN_PRECEDENCE (token);
5732 if (lookahead_prec > new_prec)
5734 /* ... and prepare to parse the RHS of the new, higher priority
5735 expression. Since precedence levels on the stack are
5736 monotonically increasing, we do not have to care about
5739 sp->tree_type = tree_type;
5744 new_prec = lookahead_prec;
5748 /* If the stack is not empty, we have parsed into LHS the right side
5749 (`4' in the example above) of an expression we had suspended.
5750 We can use the information on the stack to recover the LHS (`3')
5751 from the stack together with the tree code (`MULT_EXPR'), and
5752 the precedence of the higher level subexpression
5753 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5754 which will be used to actually build the additive expression. */
5757 tree_type = sp->tree_type;
5762 overloaded_p = false;
5763 lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5765 /* If the binary operator required the use of an overloaded operator,
5766 then this expression cannot be an integral constant-expression.
5767 An overloaded operator can be used even if both operands are
5768 otherwise permissible in an integral constant-expression if at
5769 least one of the operands is of enumeration type. */
5772 && (cp_parser_non_integral_constant_expression
5773 (parser, "calls to overloaded operators")))
5774 return error_mark_node;
5781 /* Parse the `? expression : assignment-expression' part of a
5782 conditional-expression. The LOGICAL_OR_EXPR is the
5783 logical-or-expression that started the conditional-expression.
5784 Returns a representation of the entire conditional-expression.
5786 This routine is used by cp_parser_assignment_expression.
5788 ? expression : assignment-expression
5792 ? : assignment-expression */
5795 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5798 tree assignment_expr;
5800 /* Consume the `?' token. */
5801 cp_lexer_consume_token (parser->lexer);
5802 if (cp_parser_allow_gnu_extensions_p (parser)
5803 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5804 /* Implicit true clause. */
5807 /* Parse the expression. */
5808 expr = cp_parser_expression (parser, /*cast_p=*/false);
5810 /* The next token should be a `:'. */
5811 cp_parser_require (parser, CPP_COLON, "`:'");
5812 /* Parse the assignment-expression. */
5813 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5815 /* Build the conditional-expression. */
5816 return build_x_conditional_expr (logical_or_expr,
5821 /* Parse an assignment-expression.
5823 assignment-expression:
5824 conditional-expression
5825 logical-or-expression assignment-operator assignment_expression
5828 CAST_P is true if this expression is the target of a cast.
5830 Returns a representation for the expression. */
5833 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5837 /* If the next token is the `throw' keyword, then we're looking at
5838 a throw-expression. */
5839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5840 expr = cp_parser_throw_expression (parser);
5841 /* Otherwise, it must be that we are looking at a
5842 logical-or-expression. */
5845 /* Parse the binary expressions (logical-or-expression). */
5846 expr = cp_parser_binary_expression (parser, cast_p);
5847 /* If the next token is a `?' then we're actually looking at a
5848 conditional-expression. */
5849 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5850 return cp_parser_question_colon_clause (parser, expr);
5853 enum tree_code assignment_operator;
5855 /* If it's an assignment-operator, we're using the second
5858 = cp_parser_assignment_operator_opt (parser);
5859 if (assignment_operator != ERROR_MARK)
5863 /* Parse the right-hand side of the assignment. */
5864 rhs = cp_parser_assignment_expression (parser, cast_p);
5865 /* An assignment may not appear in a
5866 constant-expression. */
5867 if (cp_parser_non_integral_constant_expression (parser,
5869 return error_mark_node;
5870 /* Build the assignment expression. */
5871 expr = build_x_modify_expr (expr,
5872 assignment_operator,
5881 /* Parse an (optional) assignment-operator.
5883 assignment-operator: one of
5884 = *= /= %= += -= >>= <<= &= ^= |=
5888 assignment-operator: one of
5891 If the next token is an assignment operator, the corresponding tree
5892 code is returned, and the token is consumed. For example, for
5893 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5894 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5895 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5896 operator, ERROR_MARK is returned. */
5898 static enum tree_code
5899 cp_parser_assignment_operator_opt (cp_parser* parser)
5904 /* Peek at the next toen. */
5905 token = cp_lexer_peek_token (parser->lexer);
5907 switch (token->type)
5918 op = TRUNC_DIV_EXPR;
5922 op = TRUNC_MOD_EXPR;
5954 /* Nothing else is an assignment operator. */
5958 /* If it was an assignment operator, consume it. */
5959 if (op != ERROR_MARK)
5960 cp_lexer_consume_token (parser->lexer);
5965 /* Parse an expression.
5968 assignment-expression
5969 expression , assignment-expression
5971 CAST_P is true if this expression is the target of a cast.
5973 Returns a representation of the expression. */
5976 cp_parser_expression (cp_parser* parser, bool cast_p)
5978 tree expression = NULL_TREE;
5982 tree assignment_expression;
5984 /* Parse the next assignment-expression. */
5985 assignment_expression
5986 = cp_parser_assignment_expression (parser, cast_p);
5987 /* If this is the first assignment-expression, we can just
5990 expression = assignment_expression;
5992 expression = build_x_compound_expr (expression,
5993 assignment_expression);
5994 /* If the next token is not a comma, then we are done with the
5996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5998 /* Consume the `,'. */
5999 cp_lexer_consume_token (parser->lexer);
6000 /* A comma operator cannot appear in a constant-expression. */
6001 if (cp_parser_non_integral_constant_expression (parser,
6002 "a comma operator"))
6003 expression = error_mark_node;
6009 /* Parse a constant-expression.
6011 constant-expression:
6012 conditional-expression
6014 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6015 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6016 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6017 is false, NON_CONSTANT_P should be NULL. */
6020 cp_parser_constant_expression (cp_parser* parser,
6021 bool allow_non_constant_p,
6022 bool *non_constant_p)
6024 bool saved_integral_constant_expression_p;
6025 bool saved_allow_non_integral_constant_expression_p;
6026 bool saved_non_integral_constant_expression_p;
6029 /* It might seem that we could simply parse the
6030 conditional-expression, and then check to see if it were
6031 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6032 one that the compiler can figure out is constant, possibly after
6033 doing some simplifications or optimizations. The standard has a
6034 precise definition of constant-expression, and we must honor
6035 that, even though it is somewhat more restrictive.
6041 is not a legal declaration, because `(2, 3)' is not a
6042 constant-expression. The `,' operator is forbidden in a
6043 constant-expression. However, GCC's constant-folding machinery
6044 will fold this operation to an INTEGER_CST for `3'. */
6046 /* Save the old settings. */
6047 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6048 saved_allow_non_integral_constant_expression_p
6049 = parser->allow_non_integral_constant_expression_p;
6050 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6051 /* We are now parsing a constant-expression. */
6052 parser->integral_constant_expression_p = true;
6053 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6054 parser->non_integral_constant_expression_p = false;
6055 /* Although the grammar says "conditional-expression", we parse an
6056 "assignment-expression", which also permits "throw-expression"
6057 and the use of assignment operators. In the case that
6058 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6059 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6060 actually essential that we look for an assignment-expression.
6061 For example, cp_parser_initializer_clauses uses this function to
6062 determine whether a particular assignment-expression is in fact
6064 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6065 /* Restore the old settings. */
6066 parser->integral_constant_expression_p
6067 = saved_integral_constant_expression_p;
6068 parser->allow_non_integral_constant_expression_p
6069 = saved_allow_non_integral_constant_expression_p;
6070 if (allow_non_constant_p)
6071 *non_constant_p = parser->non_integral_constant_expression_p;
6072 else if (parser->non_integral_constant_expression_p)
6073 expression = error_mark_node;
6074 parser->non_integral_constant_expression_p
6075 = saved_non_integral_constant_expression_p;
6080 /* Parse __builtin_offsetof.
6082 offsetof-expression:
6083 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6085 offsetof-member-designator:
6087 | offsetof-member-designator "." id-expression
6088 | offsetof-member-designator "[" expression "]" */
6091 cp_parser_builtin_offsetof (cp_parser *parser)
6093 int save_ice_p, save_non_ice_p;
6097 /* We're about to accept non-integral-constant things, but will
6098 definitely yield an integral constant expression. Save and
6099 restore these values around our local parsing. */
6100 save_ice_p = parser->integral_constant_expression_p;
6101 save_non_ice_p = parser->non_integral_constant_expression_p;
6103 /* Consume the "__builtin_offsetof" token. */
6104 cp_lexer_consume_token (parser->lexer);
6105 /* Consume the opening `('. */
6106 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6107 /* Parse the type-id. */
6108 type = cp_parser_type_id (parser);
6109 /* Look for the `,'. */
6110 cp_parser_require (parser, CPP_COMMA, "`,'");
6112 /* Build the (type *)null that begins the traditional offsetof macro. */
6113 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6115 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6116 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6120 cp_token *token = cp_lexer_peek_token (parser->lexer);
6121 switch (token->type)
6123 case CPP_OPEN_SQUARE:
6124 /* offsetof-member-designator "[" expression "]" */
6125 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6129 /* offsetof-member-designator "." identifier */
6130 cp_lexer_consume_token (parser->lexer);
6131 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6135 case CPP_CLOSE_PAREN:
6136 /* Consume the ")" token. */
6137 cp_lexer_consume_token (parser->lexer);
6141 /* Error. We know the following require will fail, but
6142 that gives the proper error message. */
6143 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6144 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6145 expr = error_mark_node;
6151 /* If we're processing a template, we can't finish the semantics yet.
6152 Otherwise we can fold the entire expression now. */
6153 if (processing_template_decl)
6154 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6156 expr = finish_offsetof (expr);
6159 parser->integral_constant_expression_p = save_ice_p;
6160 parser->non_integral_constant_expression_p = save_non_ice_p;
6165 /* Statements [gram.stmt.stmt] */
6167 /* Parse a statement.
6171 expression-statement
6176 declaration-statement
6179 IN_COMPOUND is true when the statement is nested inside a
6180 cp_parser_compound_statement; this matters for certain pragmas. */
6183 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6188 location_t statement_location;
6191 /* There is no statement yet. */
6192 statement = NULL_TREE;
6193 /* Peek at the next token. */
6194 token = cp_lexer_peek_token (parser->lexer);
6195 /* Remember the location of the first token in the statement. */
6196 statement_location = token->location;
6197 /* If this is a keyword, then that will often determine what kind of
6198 statement we have. */
6199 if (token->type == CPP_KEYWORD)
6201 enum rid keyword = token->keyword;
6207 /* Looks like a labeled-statement with a case label.
6208 Parse the label, and then use tail recursion to parse
6210 cp_parser_label_for_labeled_statement (parser);
6215 statement = cp_parser_selection_statement (parser);
6221 statement = cp_parser_iteration_statement (parser);
6228 statement = cp_parser_jump_statement (parser);
6231 /* Objective-C++ exception-handling constructs. */
6234 case RID_AT_FINALLY:
6235 case RID_AT_SYNCHRONIZED:
6237 statement = cp_parser_objc_statement (parser);
6241 statement = cp_parser_try_block (parser);
6245 /* It might be a keyword like `int' that can start a
6246 declaration-statement. */
6250 else if (token->type == CPP_NAME)
6252 /* If the next token is a `:', then we are looking at a
6253 labeled-statement. */
6254 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6255 if (token->type == CPP_COLON)
6257 /* Looks like a labeled-statement with an ordinary label.
6258 Parse the label, and then use tail recursion to parse
6260 cp_parser_label_for_labeled_statement (parser);
6264 /* Anything that starts with a `{' must be a compound-statement. */
6265 else if (token->type == CPP_OPEN_BRACE)
6266 statement = cp_parser_compound_statement (parser, NULL, false);
6267 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6268 a statement all its own. */
6269 else if (token->type == CPP_PRAGMA)
6271 /* Only certain OpenMP pragmas are attached to statements, and thus
6272 are considered statements themselves. All others are not. In
6273 the context of a compound, accept the pragma as a "statement" and
6274 return so that we can check for a close brace. Otherwise we
6275 require a real statement and must go back and read one. */
6277 cp_parser_pragma (parser, pragma_compound);
6278 else if (!cp_parser_pragma (parser, pragma_stmt))
6282 else if (token->type == CPP_EOF)
6284 cp_parser_error (parser, "expected statement");
6288 /* Everything else must be a declaration-statement or an
6289 expression-statement. Try for the declaration-statement
6290 first, unless we are looking at a `;', in which case we know that
6291 we have an expression-statement. */
6294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6296 cp_parser_parse_tentatively (parser);
6297 /* Try to parse the declaration-statement. */
6298 cp_parser_declaration_statement (parser);
6299 /* If that worked, we're done. */
6300 if (cp_parser_parse_definitely (parser))
6303 /* Look for an expression-statement instead. */
6304 statement = cp_parser_expression_statement (parser, in_statement_expr);
6307 /* Set the line number for the statement. */
6308 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6309 SET_EXPR_LOCATION (statement, statement_location);
6312 /* Parse the label for a labeled-statement, i.e.
6315 case constant-expression :
6319 case constant-expression ... constant-expression : statement
6321 When a label is parsed without errors, the label is added to the
6322 parse tree by the finish_* functions, so this function doesn't
6323 have to return the label. */
6326 cp_parser_label_for_labeled_statement (cp_parser* parser)
6330 /* The next token should be an identifier. */
6331 token = cp_lexer_peek_token (parser->lexer);
6332 if (token->type != CPP_NAME
6333 && token->type != CPP_KEYWORD)
6335 cp_parser_error (parser, "expected labeled-statement");
6339 switch (token->keyword)
6346 /* Consume the `case' token. */
6347 cp_lexer_consume_token (parser->lexer);
6348 /* Parse the constant-expression. */
6349 expr = cp_parser_constant_expression (parser,
6350 /*allow_non_constant_p=*/false,
6353 ellipsis = cp_lexer_peek_token (parser->lexer);
6354 if (ellipsis->type == CPP_ELLIPSIS)
6356 /* Consume the `...' token. */
6357 cp_lexer_consume_token (parser->lexer);
6359 cp_parser_constant_expression (parser,
6360 /*allow_non_constant_p=*/false,
6362 /* We don't need to emit warnings here, as the common code
6363 will do this for us. */
6366 expr_hi = NULL_TREE;
6368 if (parser->in_switch_statement_p)
6369 finish_case_label (expr, expr_hi);
6371 error ("case label %qE not within a switch statement", expr);
6376 /* Consume the `default' token. */
6377 cp_lexer_consume_token (parser->lexer);
6379 if (parser->in_switch_statement_p)
6380 finish_case_label (NULL_TREE, NULL_TREE);
6382 error ("case label not within a switch statement");
6386 /* Anything else must be an ordinary label. */
6387 finish_label_stmt (cp_parser_identifier (parser));
6391 /* Require the `:' token. */
6392 cp_parser_require (parser, CPP_COLON, "`:'");
6395 /* Parse an expression-statement.
6397 expression-statement:
6400 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6401 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6402 indicates whether this expression-statement is part of an
6403 expression statement. */
6406 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6408 tree statement = NULL_TREE;
6410 /* If the next token is a ';', then there is no expression
6412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6413 statement = cp_parser_expression (parser, /*cast_p=*/false);
6415 /* Consume the final `;'. */
6416 cp_parser_consume_semicolon_at_end_of_statement (parser);
6418 if (in_statement_expr
6419 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6420 /* This is the final expression statement of a statement
6422 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6424 statement = finish_expr_stmt (statement);
6431 /* Parse a compound-statement.
6434 { statement-seq [opt] }
6436 Returns a tree representing the statement. */
6439 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6444 /* Consume the `{'. */
6445 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6446 return error_mark_node;
6447 /* Begin the compound-statement. */
6448 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6449 /* Parse an (optional) statement-seq. */
6450 cp_parser_statement_seq_opt (parser, in_statement_expr);
6451 /* Finish the compound-statement. */
6452 finish_compound_stmt (compound_stmt);
6453 /* Consume the `}'. */
6454 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6456 return compound_stmt;
6459 /* Parse an (optional) statement-seq.
6463 statement-seq [opt] statement */
6466 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6468 /* Scan statements until there aren't any more. */
6471 cp_token *token = cp_lexer_peek_token (parser->lexer);
6473 /* If we're looking at a `}', then we've run out of statements. */
6474 if (token->type == CPP_CLOSE_BRACE
6475 || token->type == CPP_EOF
6476 || token->type == CPP_PRAGMA_EOL)
6479 /* Parse the statement. */
6480 cp_parser_statement (parser, in_statement_expr, true);
6484 /* Parse a selection-statement.
6486 selection-statement:
6487 if ( condition ) statement
6488 if ( condition ) statement else statement
6489 switch ( condition ) statement
6491 Returns the new IF_STMT or SWITCH_STMT. */
6494 cp_parser_selection_statement (cp_parser* parser)
6499 /* Peek at the next token. */
6500 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6502 /* See what kind of keyword it is. */
6503 keyword = token->keyword;
6512 /* Look for the `('. */
6513 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6515 cp_parser_skip_to_end_of_statement (parser);
6516 return error_mark_node;
6519 /* Begin the selection-statement. */
6520 if (keyword == RID_IF)
6521 statement = begin_if_stmt ();
6523 statement = begin_switch_stmt ();
6525 /* Parse the condition. */
6526 condition = cp_parser_condition (parser);
6527 /* Look for the `)'. */
6528 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6529 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6530 /*consume_paren=*/true);
6532 if (keyword == RID_IF)
6534 /* Add the condition. */
6535 finish_if_stmt_cond (condition, statement);
6537 /* Parse the then-clause. */
6538 cp_parser_implicitly_scoped_statement (parser);
6539 finish_then_clause (statement);
6541 /* If the next token is `else', parse the else-clause. */
6542 if (cp_lexer_next_token_is_keyword (parser->lexer,
6545 /* Consume the `else' keyword. */
6546 cp_lexer_consume_token (parser->lexer);
6547 begin_else_clause (statement);
6548 /* Parse the else-clause. */
6549 cp_parser_implicitly_scoped_statement (parser);
6550 finish_else_clause (statement);
6553 /* Now we're all done with the if-statement. */
6554 finish_if_stmt (statement);
6558 bool in_switch_statement_p;
6559 unsigned char in_statement;
6561 /* Add the condition. */
6562 finish_switch_cond (condition, statement);
6564 /* Parse the body of the switch-statement. */
6565 in_switch_statement_p = parser->in_switch_statement_p;
6566 in_statement = parser->in_statement;
6567 parser->in_switch_statement_p = true;
6568 parser->in_statement |= IN_SWITCH_STMT;
6569 cp_parser_implicitly_scoped_statement (parser);
6570 parser->in_switch_statement_p = in_switch_statement_p;
6571 parser->in_statement = in_statement;
6573 /* Now we're all done with the switch-statement. */
6574 finish_switch_stmt (statement);
6582 cp_parser_error (parser, "expected selection-statement");
6583 return error_mark_node;
6587 /* Parse a condition.
6591 type-specifier-seq declarator = assignment-expression
6596 type-specifier-seq declarator asm-specification [opt]
6597 attributes [opt] = assignment-expression
6599 Returns the expression that should be tested. */
6602 cp_parser_condition (cp_parser* parser)
6604 cp_decl_specifier_seq type_specifiers;
6605 const char *saved_message;
6607 /* Try the declaration first. */
6608 cp_parser_parse_tentatively (parser);
6609 /* New types are not allowed in the type-specifier-seq for a
6611 saved_message = parser->type_definition_forbidden_message;
6612 parser->type_definition_forbidden_message
6613 = "types may not be defined in conditions";
6614 /* Parse the type-specifier-seq. */
6615 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6617 /* Restore the saved message. */
6618 parser->type_definition_forbidden_message = saved_message;
6619 /* If all is well, we might be looking at a declaration. */
6620 if (!cp_parser_error_occurred (parser))
6623 tree asm_specification;
6625 cp_declarator *declarator;
6626 tree initializer = NULL_TREE;
6628 /* Parse the declarator. */
6629 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6630 /*ctor_dtor_or_conv_p=*/NULL,
6631 /*parenthesized_p=*/NULL,
6632 /*member_p=*/false);
6633 /* Parse the attributes. */
6634 attributes = cp_parser_attributes_opt (parser);
6635 /* Parse the asm-specification. */
6636 asm_specification = cp_parser_asm_specification_opt (parser);
6637 /* If the next token is not an `=', then we might still be
6638 looking at an expression. For example:
6642 looks like a decl-specifier-seq and a declarator -- but then
6643 there is no `=', so this is an expression. */
6644 cp_parser_require (parser, CPP_EQ, "`='");
6645 /* If we did see an `=', then we are looking at a declaration
6647 if (cp_parser_parse_definitely (parser))
6650 bool non_constant_p;
6652 /* Create the declaration. */
6653 decl = start_decl (declarator, &type_specifiers,
6654 /*initialized_p=*/true,
6655 attributes, /*prefix_attributes=*/NULL_TREE,
6657 /* Parse the assignment-expression. */
6659 = cp_parser_constant_expression (parser,
6660 /*allow_non_constant_p=*/true,
6662 if (!non_constant_p)
6663 initializer = fold_non_dependent_expr (initializer);
6665 /* Process the initializer. */
6666 cp_finish_decl (decl,
6667 initializer, !non_constant_p,
6669 LOOKUP_ONLYCONVERTING);
6672 pop_scope (pushed_scope);
6674 return convert_from_reference (decl);
6677 /* If we didn't even get past the declarator successfully, we are
6678 definitely not looking at a declaration. */
6680 cp_parser_abort_tentative_parse (parser);
6682 /* Otherwise, we are looking at an expression. */
6683 return cp_parser_expression (parser, /*cast_p=*/false);
6686 /* Parse an iteration-statement.
6688 iteration-statement:
6689 while ( condition ) statement
6690 do statement while ( expression ) ;
6691 for ( for-init-statement condition [opt] ; expression [opt] )
6694 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6697 cp_parser_iteration_statement (cp_parser* parser)
6702 unsigned char in_statement;
6704 /* Peek at the next token. */
6705 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6707 return error_mark_node;
6709 /* Remember whether or not we are already within an iteration
6711 in_statement = parser->in_statement;
6713 /* See what kind of keyword it is. */
6714 keyword = token->keyword;
6721 /* Begin the while-statement. */
6722 statement = begin_while_stmt ();
6723 /* Look for the `('. */
6724 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6725 /* Parse the condition. */
6726 condition = cp_parser_condition (parser);
6727 finish_while_stmt_cond (condition, statement);
6728 /* Look for the `)'. */
6729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6730 /* Parse the dependent statement. */
6731 parser->in_statement = IN_ITERATION_STMT;
6732 cp_parser_already_scoped_statement (parser);
6733 parser->in_statement = in_statement;
6734 /* We're done with the while-statement. */
6735 finish_while_stmt (statement);
6743 /* Begin the do-statement. */
6744 statement = begin_do_stmt ();
6745 /* Parse the body of the do-statement. */
6746 parser->in_statement = IN_ITERATION_STMT;
6747 cp_parser_implicitly_scoped_statement (parser);
6748 parser->in_statement = in_statement;
6749 finish_do_body (statement);
6750 /* Look for the `while' keyword. */
6751 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6752 /* Look for the `('. */
6753 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6754 /* Parse the expression. */
6755 expression = cp_parser_expression (parser, /*cast_p=*/false);
6756 /* We're done with the do-statement. */
6757 finish_do_stmt (expression, statement);
6758 /* Look for the `)'. */
6759 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6760 /* Look for the `;'. */
6761 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6767 tree condition = NULL_TREE;
6768 tree expression = NULL_TREE;
6770 /* Begin the for-statement. */
6771 statement = begin_for_stmt ();
6772 /* Look for the `('. */
6773 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6774 /* Parse the initialization. */
6775 cp_parser_for_init_statement (parser);
6776 finish_for_init_stmt (statement);
6778 /* If there's a condition, process it. */
6779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6780 condition = cp_parser_condition (parser);
6781 finish_for_cond (condition, statement);
6782 /* Look for the `;'. */
6783 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6785 /* If there's an expression, process it. */
6786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6787 expression = cp_parser_expression (parser, /*cast_p=*/false);
6788 finish_for_expr (expression, statement);
6789 /* Look for the `)'. */
6790 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6792 /* Parse the body of the for-statement. */
6793 parser->in_statement = IN_ITERATION_STMT;
6794 cp_parser_already_scoped_statement (parser);
6795 parser->in_statement = in_statement;
6797 /* We're done with the for-statement. */
6798 finish_for_stmt (statement);
6803 cp_parser_error (parser, "expected iteration-statement");
6804 statement = error_mark_node;
6811 /* Parse a for-init-statement.
6814 expression-statement
6815 simple-declaration */
6818 cp_parser_for_init_statement (cp_parser* parser)
6820 /* If the next token is a `;', then we have an empty
6821 expression-statement. Grammatically, this is also a
6822 simple-declaration, but an invalid one, because it does not
6823 declare anything. Therefore, if we did not handle this case
6824 specially, we would issue an error message about an invalid
6826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6828 /* We're going to speculatively look for a declaration, falling back
6829 to an expression, if necessary. */
6830 cp_parser_parse_tentatively (parser);
6831 /* Parse the declaration. */
6832 cp_parser_simple_declaration (parser,
6833 /*function_definition_allowed_p=*/false);
6834 /* If the tentative parse failed, then we shall need to look for an
6835 expression-statement. */
6836 if (cp_parser_parse_definitely (parser))
6840 cp_parser_expression_statement (parser, false);
6843 /* Parse a jump-statement.
6848 return expression [opt] ;
6856 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6859 cp_parser_jump_statement (cp_parser* parser)
6861 tree statement = error_mark_node;
6865 /* Peek at the next token. */
6866 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6868 return error_mark_node;
6870 /* See what kind of keyword it is. */
6871 keyword = token->keyword;
6875 switch (parser->in_statement)
6878 error ("break statement not within loop or switch");
6881 gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6882 || parser->in_statement == IN_ITERATION_STMT);
6883 statement = finish_break_stmt ();
6886 error ("invalid exit from OpenMP structured block");
6889 error ("break statement used with OpenMP for loop");
6892 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6896 switch (parser->in_statement & ~IN_SWITCH_STMT)
6899 error ("continue statement not within a loop");
6901 case IN_ITERATION_STMT:
6903 statement = finish_continue_stmt ();
6906 error ("invalid exit from OpenMP structured block");
6911 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6918 /* If the next token is a `;', then there is no
6920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6921 expr = cp_parser_expression (parser, /*cast_p=*/false);
6924 /* Build the return-statement. */
6925 statement = finish_return_stmt (expr);
6926 /* Look for the final `;'. */
6927 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6932 /* Create the goto-statement. */
6933 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6935 /* Issue a warning about this use of a GNU extension. */
6937 pedwarn ("ISO C++ forbids computed gotos");
6938 /* Consume the '*' token. */
6939 cp_lexer_consume_token (parser->lexer);
6940 /* Parse the dependent expression. */
6941 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6944 finish_goto_stmt (cp_parser_identifier (parser));
6945 /* Look for the final `;'. */
6946 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6950 cp_parser_error (parser, "expected jump-statement");
6957 /* Parse a declaration-statement.
6959 declaration-statement:
6960 block-declaration */
6963 cp_parser_declaration_statement (cp_parser* parser)
6967 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6968 p = obstack_alloc (&declarator_obstack, 0);
6970 /* Parse the block-declaration. */
6971 cp_parser_block_declaration (parser, /*statement_p=*/true);
6973 /* Free any declarators allocated. */
6974 obstack_free (&declarator_obstack, p);
6976 /* Finish off the statement. */
6980 /* Some dependent statements (like `if (cond) statement'), are
6981 implicitly in their own scope. In other words, if the statement is
6982 a single statement (as opposed to a compound-statement), it is
6983 none-the-less treated as if it were enclosed in braces. Any
6984 declarations appearing in the dependent statement are out of scope
6985 after control passes that point. This function parses a statement,
6986 but ensures that is in its own scope, even if it is not a
6989 Returns the new statement. */
6992 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6996 /* Mark if () ; with a special NOP_EXPR. */
6997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6999 cp_lexer_consume_token (parser->lexer);
7000 statement = add_stmt (build_empty_stmt ());
7002 /* if a compound is opened, we simply parse the statement directly. */
7003 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7004 statement = cp_parser_compound_statement (parser, NULL, false);
7005 /* If the token is not a `{', then we must take special action. */
7008 /* Create a compound-statement. */
7009 statement = begin_compound_stmt (0);
7010 /* Parse the dependent-statement. */
7011 cp_parser_statement (parser, NULL_TREE, false);
7012 /* Finish the dummy compound-statement. */
7013 finish_compound_stmt (statement);
7016 /* Return the statement. */
7020 /* For some dependent statements (like `while (cond) statement'), we
7021 have already created a scope. Therefore, even if the dependent
7022 statement is a compound-statement, we do not want to create another
7026 cp_parser_already_scoped_statement (cp_parser* parser)
7028 /* If the token is a `{', then we must take special action. */
7029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7030 cp_parser_statement (parser, NULL_TREE, false);
7033 /* Avoid calling cp_parser_compound_statement, so that we
7034 don't create a new scope. Do everything else by hand. */
7035 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7036 cp_parser_statement_seq_opt (parser, NULL_TREE);
7037 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7041 /* Declarations [gram.dcl.dcl] */
7043 /* Parse an optional declaration-sequence.
7047 declaration-seq declaration */
7050 cp_parser_declaration_seq_opt (cp_parser* parser)
7056 token = cp_lexer_peek_token (parser->lexer);
7058 if (token->type == CPP_CLOSE_BRACE
7059 || token->type == CPP_EOF
7060 || token->type == CPP_PRAGMA_EOL)
7063 if (token->type == CPP_SEMICOLON)
7065 /* A declaration consisting of a single semicolon is
7066 invalid. Allow it unless we're being pedantic. */
7067 cp_lexer_consume_token (parser->lexer);
7068 if (pedantic && !in_system_header)
7069 pedwarn ("extra %<;%>");
7073 /* If we're entering or exiting a region that's implicitly
7074 extern "C", modify the lang context appropriately. */
7075 if (!parser->implicit_extern_c && token->implicit_extern_c)
7077 push_lang_context (lang_name_c);
7078 parser->implicit_extern_c = true;
7080 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7082 pop_lang_context ();
7083 parser->implicit_extern_c = false;
7086 if (token->type == CPP_PRAGMA)
7088 /* A top-level declaration can consist solely of a #pragma.
7089 A nested declaration cannot, so this is done here and not
7090 in cp_parser_declaration. (A #pragma at block scope is
7091 handled in cp_parser_statement.) */
7092 cp_parser_pragma (parser, pragma_external);
7096 /* Parse the declaration itself. */
7097 cp_parser_declaration (parser);
7101 /* Parse a declaration.
7106 template-declaration
7107 explicit-instantiation
7108 explicit-specialization
7109 linkage-specification
7110 namespace-definition
7115 __extension__ declaration */
7118 cp_parser_declaration (cp_parser* parser)
7125 /* Check for the `__extension__' keyword. */
7126 if (cp_parser_extension_opt (parser, &saved_pedantic))
7128 /* Parse the qualified declaration. */
7129 cp_parser_declaration (parser);
7130 /* Restore the PEDANTIC flag. */
7131 pedantic = saved_pedantic;
7136 /* Try to figure out what kind of declaration is present. */
7137 token1 = *cp_lexer_peek_token (parser->lexer);
7139 if (token1.type != CPP_EOF)
7140 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7143 token2.type = CPP_EOF;
7144 token2.keyword = RID_MAX;
7147 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7148 p = obstack_alloc (&declarator_obstack, 0);
7150 /* If the next token is `extern' and the following token is a string
7151 literal, then we have a linkage specification. */
7152 if (token1.keyword == RID_EXTERN
7153 && cp_parser_is_string_literal (&token2))
7154 cp_parser_linkage_specification (parser);
7155 /* If the next token is `template', then we have either a template
7156 declaration, an explicit instantiation, or an explicit
7158 else if (token1.keyword == RID_TEMPLATE)
7160 /* `template <>' indicates a template specialization. */
7161 if (token2.type == CPP_LESS
7162 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7163 cp_parser_explicit_specialization (parser);
7164 /* `template <' indicates a template declaration. */
7165 else if (token2.type == CPP_LESS)
7166 cp_parser_template_declaration (parser, /*member_p=*/false);
7167 /* Anything else must be an explicit instantiation. */
7169 cp_parser_explicit_instantiation (parser);
7171 /* If the next token is `export', then we have a template
7173 else if (token1.keyword == RID_EXPORT)
7174 cp_parser_template_declaration (parser, /*member_p=*/false);
7175 /* If the next token is `extern', 'static' or 'inline' and the one
7176 after that is `template', we have a GNU extended explicit
7177 instantiation directive. */
7178 else if (cp_parser_allow_gnu_extensions_p (parser)
7179 && (token1.keyword == RID_EXTERN
7180 || token1.keyword == RID_STATIC
7181 || token1.keyword == RID_INLINE)
7182 && token2.keyword == RID_TEMPLATE)
7183 cp_parser_explicit_instantiation (parser);
7184 /* If the next token is `namespace', check for a named or unnamed
7185 namespace definition. */
7186 else if (token1.keyword == RID_NAMESPACE
7187 && (/* A named namespace definition. */
7188 (token2.type == CPP_NAME
7189 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7191 /* An unnamed namespace definition. */
7192 || token2.type == CPP_OPEN_BRACE
7193 || token2.keyword == RID_ATTRIBUTE))
7194 cp_parser_namespace_definition (parser);
7195 /* Objective-C++ declaration/definition. */
7196 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7197 cp_parser_objc_declaration (parser);
7198 /* We must have either a block declaration or a function
7201 /* Try to parse a block-declaration, or a function-definition. */
7202 cp_parser_block_declaration (parser, /*statement_p=*/false);
7204 /* Free any declarators allocated. */
7205 obstack_free (&declarator_obstack, p);
7208 /* Parse a block-declaration.
7213 namespace-alias-definition
7220 __extension__ block-declaration
7226 static_assert-declaration
7228 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7229 part of a declaration-statement. */
7232 cp_parser_block_declaration (cp_parser *parser,
7238 /* Check for the `__extension__' keyword. */
7239 if (cp_parser_extension_opt (parser, &saved_pedantic))
7241 /* Parse the qualified declaration. */
7242 cp_parser_block_declaration (parser, statement_p);
7243 /* Restore the PEDANTIC flag. */
7244 pedantic = saved_pedantic;
7249 /* Peek at the next token to figure out which kind of declaration is
7251 token1 = cp_lexer_peek_token (parser->lexer);
7253 /* If the next keyword is `asm', we have an asm-definition. */
7254 if (token1->keyword == RID_ASM)
7257 cp_parser_commit_to_tentative_parse (parser);
7258 cp_parser_asm_definition (parser);
7260 /* If the next keyword is `namespace', we have a
7261 namespace-alias-definition. */
7262 else if (token1->keyword == RID_NAMESPACE)
7263 cp_parser_namespace_alias_definition (parser);
7264 /* If the next keyword is `using', we have either a
7265 using-declaration or a using-directive. */
7266 else if (token1->keyword == RID_USING)
7271 cp_parser_commit_to_tentative_parse (parser);
7272 /* If the token after `using' is `namespace', then we have a
7274 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7275 if (token2->keyword == RID_NAMESPACE)
7276 cp_parser_using_directive (parser);
7277 /* Otherwise, it's a using-declaration. */
7279 cp_parser_using_declaration (parser,
7280 /*access_declaration_p=*/false);
7282 /* If the next keyword is `__label__' we have a label declaration. */
7283 else if (token1->keyword == RID_LABEL)
7286 cp_parser_commit_to_tentative_parse (parser);
7287 cp_parser_label_declaration (parser);
7289 /* If the next token is `static_assert' we have a static assertion. */
7290 else if (token1->keyword == RID_STATIC_ASSERT)
7291 cp_parser_static_assert (parser, /*member_p=*/false);
7292 /* Anything else must be a simple-declaration. */
7294 cp_parser_simple_declaration (parser, !statement_p);
7297 /* Parse a simple-declaration.
7300 decl-specifier-seq [opt] init-declarator-list [opt] ;
7302 init-declarator-list:
7304 init-declarator-list , init-declarator
7306 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7307 function-definition as a simple-declaration. */
7310 cp_parser_simple_declaration (cp_parser* parser,
7311 bool function_definition_allowed_p)
7313 cp_decl_specifier_seq decl_specifiers;
7314 int declares_class_or_enum;
7315 bool saw_declarator;
7317 /* Defer access checks until we know what is being declared; the
7318 checks for names appearing in the decl-specifier-seq should be
7319 done as if we were in the scope of the thing being declared. */
7320 push_deferring_access_checks (dk_deferred);
7322 /* Parse the decl-specifier-seq. We have to keep track of whether
7323 or not the decl-specifier-seq declares a named class or
7324 enumeration type, since that is the only case in which the
7325 init-declarator-list is allowed to be empty.
7329 In a simple-declaration, the optional init-declarator-list can be
7330 omitted only when declaring a class or enumeration, that is when
7331 the decl-specifier-seq contains either a class-specifier, an
7332 elaborated-type-specifier, or an enum-specifier. */
7333 cp_parser_decl_specifier_seq (parser,
7334 CP_PARSER_FLAGS_OPTIONAL,
7336 &declares_class_or_enum);
7337 /* We no longer need to defer access checks. */
7338 stop_deferring_access_checks ();
7340 /* In a block scope, a valid declaration must always have a
7341 decl-specifier-seq. By not trying to parse declarators, we can
7342 resolve the declaration/expression ambiguity more quickly. */
7343 if (!function_definition_allowed_p
7344 && !decl_specifiers.any_specifiers_p)
7346 cp_parser_error (parser, "expected declaration");
7350 /* If the next two tokens are both identifiers, the code is
7351 erroneous. The usual cause of this situation is code like:
7355 where "T" should name a type -- but does not. */
7356 if (!decl_specifiers.type
7357 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7359 /* If parsing tentatively, we should commit; we really are
7360 looking at a declaration. */
7361 cp_parser_commit_to_tentative_parse (parser);
7366 /* If we have seen at least one decl-specifier, and the next token
7367 is not a parenthesis, then we must be looking at a declaration.
7368 (After "int (" we might be looking at a functional cast.) */
7369 if (decl_specifiers.any_specifiers_p
7370 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7371 cp_parser_commit_to_tentative_parse (parser);
7373 /* Keep going until we hit the `;' at the end of the simple
7375 saw_declarator = false;
7376 while (cp_lexer_next_token_is_not (parser->lexer,
7380 bool function_definition_p;
7385 /* If we are processing next declarator, coma is expected */
7386 token = cp_lexer_peek_token (parser->lexer);
7387 gcc_assert (token->type == CPP_COMMA);
7388 cp_lexer_consume_token (parser->lexer);
7391 saw_declarator = true;
7393 /* Parse the init-declarator. */
7394 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7395 /*checks=*/NULL_TREE,
7396 function_definition_allowed_p,
7398 declares_class_or_enum,
7399 &function_definition_p);
7400 /* If an error occurred while parsing tentatively, exit quickly.
7401 (That usually happens when in the body of a function; each
7402 statement is treated as a declaration-statement until proven
7404 if (cp_parser_error_occurred (parser))
7406 /* Handle function definitions specially. */
7407 if (function_definition_p)
7409 /* If the next token is a `,', then we are probably
7410 processing something like:
7414 which is erroneous. */
7415 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7416 error ("mixing declarations and function-definitions is forbidden");
7417 /* Otherwise, we're done with the list of declarators. */
7420 pop_deferring_access_checks ();
7424 /* The next token should be either a `,' or a `;'. */
7425 token = cp_lexer_peek_token (parser->lexer);
7426 /* If it's a `,', there are more declarators to come. */
7427 if (token->type == CPP_COMMA)
7428 /* will be consumed next time around */;
7429 /* If it's a `;', we are done. */
7430 else if (token->type == CPP_SEMICOLON)
7432 /* Anything else is an error. */
7435 /* If we have already issued an error message we don't need
7436 to issue another one. */
7437 if (decl != error_mark_node
7438 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7439 cp_parser_error (parser, "expected %<,%> or %<;%>");
7440 /* Skip tokens until we reach the end of the statement. */
7441 cp_parser_skip_to_end_of_statement (parser);
7442 /* If the next token is now a `;', consume it. */
7443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7444 cp_lexer_consume_token (parser->lexer);
7447 /* After the first time around, a function-definition is not
7448 allowed -- even if it was OK at first. For example:
7453 function_definition_allowed_p = false;
7456 /* Issue an error message if no declarators are present, and the
7457 decl-specifier-seq does not itself declare a class or
7459 if (!saw_declarator)
7461 if (cp_parser_declares_only_class_p (parser))
7462 shadow_tag (&decl_specifiers);
7463 /* Perform any deferred access checks. */
7464 perform_deferred_access_checks ();
7467 /* Consume the `;'. */
7468 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7471 pop_deferring_access_checks ();
7474 /* Parse a decl-specifier-seq.
7477 decl-specifier-seq [opt] decl-specifier
7480 storage-class-specifier
7491 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7493 The parser flags FLAGS is used to control type-specifier parsing.
7495 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7498 1: one of the decl-specifiers is an elaborated-type-specifier
7499 (i.e., a type declaration)
7500 2: one of the decl-specifiers is an enum-specifier or a
7501 class-specifier (i.e., a type definition)
7506 cp_parser_decl_specifier_seq (cp_parser* parser,
7507 cp_parser_flags flags,
7508 cp_decl_specifier_seq *decl_specs,
7509 int* declares_class_or_enum)
7511 bool constructor_possible_p = !parser->in_declarator_p;
7513 /* Clear DECL_SPECS. */
7514 clear_decl_specs (decl_specs);
7516 /* Assume no class or enumeration type is declared. */
7517 *declares_class_or_enum = 0;
7519 /* Keep reading specifiers until there are no more to read. */
7523 bool found_decl_spec;
7526 /* Peek at the next token. */
7527 token = cp_lexer_peek_token (parser->lexer);
7528 /* Handle attributes. */
7529 if (token->keyword == RID_ATTRIBUTE)
7531 /* Parse the attributes. */
7532 decl_specs->attributes
7533 = chainon (decl_specs->attributes,
7534 cp_parser_attributes_opt (parser));
7537 /* Assume we will find a decl-specifier keyword. */
7538 found_decl_spec = true;
7539 /* If the next token is an appropriate keyword, we can simply
7540 add it to the list. */
7541 switch (token->keyword)
7546 if (!at_class_scope_p ())
7548 error ("%<friend%> used outside of class");
7549 cp_lexer_purge_token (parser->lexer);
7553 ++decl_specs->specs[(int) ds_friend];
7554 /* Consume the token. */
7555 cp_lexer_consume_token (parser->lexer);
7559 /* function-specifier:
7566 cp_parser_function_specifier_opt (parser, decl_specs);
7572 ++decl_specs->specs[(int) ds_typedef];
7573 /* Consume the token. */
7574 cp_lexer_consume_token (parser->lexer);
7575 /* A constructor declarator cannot appear in a typedef. */
7576 constructor_possible_p = false;
7577 /* The "typedef" keyword can only occur in a declaration; we
7578 may as well commit at this point. */
7579 cp_parser_commit_to_tentative_parse (parser);
7581 if (decl_specs->storage_class != sc_none)
7582 decl_specs->conflicting_specifiers_p = true;
7585 /* storage-class-specifier:
7599 /* Consume the token. */
7600 cp_lexer_consume_token (parser->lexer);
7601 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7604 /* Consume the token. */
7605 cp_lexer_consume_token (parser->lexer);
7606 ++decl_specs->specs[(int) ds_thread];
7610 /* We did not yet find a decl-specifier yet. */
7611 found_decl_spec = false;
7615 /* Constructors are a special case. The `S' in `S()' is not a
7616 decl-specifier; it is the beginning of the declarator. */
7619 && constructor_possible_p
7620 && (cp_parser_constructor_declarator_p
7621 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7623 /* If we don't have a DECL_SPEC yet, then we must be looking at
7624 a type-specifier. */
7625 if (!found_decl_spec && !constructor_p)
7627 int decl_spec_declares_class_or_enum;
7628 bool is_cv_qualifier;
7632 = cp_parser_type_specifier (parser, flags,
7634 /*is_declaration=*/true,
7635 &decl_spec_declares_class_or_enum,
7638 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7640 /* If this type-specifier referenced a user-defined type
7641 (a typedef, class-name, etc.), then we can't allow any
7642 more such type-specifiers henceforth.
7646 The longest sequence of decl-specifiers that could
7647 possibly be a type name is taken as the
7648 decl-specifier-seq of a declaration. The sequence shall
7649 be self-consistent as described below.
7653 As a general rule, at most one type-specifier is allowed
7654 in the complete decl-specifier-seq of a declaration. The
7655 only exceptions are the following:
7657 -- const or volatile can be combined with any other
7660 -- signed or unsigned can be combined with char, long,
7668 void g (const int Pc);
7670 Here, Pc is *not* part of the decl-specifier seq; it's
7671 the declarator. Therefore, once we see a type-specifier
7672 (other than a cv-qualifier), we forbid any additional
7673 user-defined types. We *do* still allow things like `int
7674 int' to be considered a decl-specifier-seq, and issue the
7675 error message later. */
7676 if (type_spec && !is_cv_qualifier)
7677 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7678 /* A constructor declarator cannot follow a type-specifier. */
7681 constructor_possible_p = false;
7682 found_decl_spec = true;
7686 /* If we still do not have a DECL_SPEC, then there are no more
7688 if (!found_decl_spec)
7691 decl_specs->any_specifiers_p = true;
7692 /* After we see one decl-specifier, further decl-specifiers are
7694 flags |= CP_PARSER_FLAGS_OPTIONAL;
7697 cp_parser_check_decl_spec (decl_specs);
7699 /* Don't allow a friend specifier with a class definition. */
7700 if (decl_specs->specs[(int) ds_friend] != 0
7701 && (*declares_class_or_enum & 2))
7702 error ("class definition may not be declared a friend");
7705 /* Parse an (optional) storage-class-specifier.
7707 storage-class-specifier:
7716 storage-class-specifier:
7719 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7722 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7724 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7732 /* Consume the token. */
7733 return cp_lexer_consume_token (parser->lexer)->value;
7740 /* Parse an (optional) function-specifier.
7747 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7748 Updates DECL_SPECS, if it is non-NULL. */
7751 cp_parser_function_specifier_opt (cp_parser* parser,
7752 cp_decl_specifier_seq *decl_specs)
7754 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7758 ++decl_specs->specs[(int) ds_inline];
7762 /* 14.5.2.3 [temp.mem]
7764 A member function template shall not be virtual. */
7765 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7766 error ("templates may not be %<virtual%>");
7767 else if (decl_specs)
7768 ++decl_specs->specs[(int) ds_virtual];
7773 ++decl_specs->specs[(int) ds_explicit];
7780 /* Consume the token. */
7781 return cp_lexer_consume_token (parser->lexer)->value;
7784 /* Parse a linkage-specification.
7786 linkage-specification:
7787 extern string-literal { declaration-seq [opt] }
7788 extern string-literal declaration */
7791 cp_parser_linkage_specification (cp_parser* parser)
7795 /* Look for the `extern' keyword. */
7796 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7798 /* Look for the string-literal. */
7799 linkage = cp_parser_string_literal (parser, false, false);
7801 /* Transform the literal into an identifier. If the literal is a
7802 wide-character string, or contains embedded NULs, then we can't
7803 handle it as the user wants. */
7804 if (strlen (TREE_STRING_POINTER (linkage))
7805 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7807 cp_parser_error (parser, "invalid linkage-specification");
7808 /* Assume C++ linkage. */
7809 linkage = lang_name_cplusplus;
7812 linkage = get_identifier (TREE_STRING_POINTER (linkage));
7814 /* We're now using the new linkage. */
7815 push_lang_context (linkage);
7817 /* If the next token is a `{', then we're using the first
7819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7821 /* Consume the `{' token. */
7822 cp_lexer_consume_token (parser->lexer);
7823 /* Parse the declarations. */
7824 cp_parser_declaration_seq_opt (parser);
7825 /* Look for the closing `}'. */
7826 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7828 /* Otherwise, there's just one declaration. */
7831 bool saved_in_unbraced_linkage_specification_p;
7833 saved_in_unbraced_linkage_specification_p
7834 = parser->in_unbraced_linkage_specification_p;
7835 parser->in_unbraced_linkage_specification_p = true;
7836 cp_parser_declaration (parser);
7837 parser->in_unbraced_linkage_specification_p
7838 = saved_in_unbraced_linkage_specification_p;
7841 /* We're done with the linkage-specification. */
7842 pop_lang_context ();
7845 /* Parse a static_assert-declaration.
7847 static_assert-declaration:
7848 static_assert ( constant-expression , string-literal ) ;
7850 If MEMBER_P, this static_assert is a class member. */
7853 cp_parser_static_assert(cp_parser *parser, bool member_p)
7858 location_t saved_loc;
7860 /* Peek at the `static_assert' token so we can keep track of exactly
7861 where the static assertion started. */
7862 token = cp_lexer_peek_token (parser->lexer);
7863 saved_loc = token->location;
7865 /* Look for the `static_assert' keyword. */
7866 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
7870 /* We know we are in a static assertion; commit to any tentative
7872 if (cp_parser_parsing_tentatively (parser))
7873 cp_parser_commit_to_tentative_parse (parser);
7875 /* Parse the `(' starting the static assertion condition. */
7876 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7878 /* Parse the constant-expression. */
7880 cp_parser_constant_expression (parser,
7881 /*allow_non_constant_p=*/false,
7882 /*non_constant_p=*/NULL);
7884 /* Parse the separating `,'. */
7885 cp_parser_require (parser, CPP_COMMA, "`,'");
7887 /* Parse the string-literal message. */
7888 message = cp_parser_string_literal (parser,
7889 /*translate=*/false,
7892 /* A `)' completes the static assertion. */
7893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
7894 cp_parser_skip_to_closing_parenthesis (parser,
7895 /*recovering=*/true,
7897 /*consume_paren=*/true);
7899 /* A semicolon terminates the declaration. */
7900 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7902 /* Complete the static assertion, which may mean either processing
7903 the static assert now or saving it for template instantiation. */
7904 finish_static_assert (condition, message, saved_loc, member_p);
7907 /* Special member functions [gram.special] */
7909 /* Parse a conversion-function-id.
7911 conversion-function-id:
7912 operator conversion-type-id
7914 Returns an IDENTIFIER_NODE representing the operator. */
7917 cp_parser_conversion_function_id (cp_parser* parser)
7921 tree saved_qualifying_scope;
7922 tree saved_object_scope;
7923 tree pushed_scope = NULL_TREE;
7925 /* Look for the `operator' token. */
7926 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7927 return error_mark_node;
7928 /* When we parse the conversion-type-id, the current scope will be
7929 reset. However, we need that information in able to look up the
7930 conversion function later, so we save it here. */
7931 saved_scope = parser->scope;
7932 saved_qualifying_scope = parser->qualifying_scope;
7933 saved_object_scope = parser->object_scope;
7934 /* We must enter the scope of the class so that the names of
7935 entities declared within the class are available in the
7936 conversion-type-id. For example, consider:
7943 S::operator I() { ... }
7945 In order to see that `I' is a type-name in the definition, we
7946 must be in the scope of `S'. */
7948 pushed_scope = push_scope (saved_scope);
7949 /* Parse the conversion-type-id. */
7950 type = cp_parser_conversion_type_id (parser);
7951 /* Leave the scope of the class, if any. */
7953 pop_scope (pushed_scope);
7954 /* Restore the saved scope. */
7955 parser->scope = saved_scope;
7956 parser->qualifying_scope = saved_qualifying_scope;
7957 parser->object_scope = saved_object_scope;
7958 /* If the TYPE is invalid, indicate failure. */
7959 if (type == error_mark_node)
7960 return error_mark_node;
7961 return mangle_conv_op_name_for_type (type);
7964 /* Parse a conversion-type-id:
7967 type-specifier-seq conversion-declarator [opt]
7969 Returns the TYPE specified. */
7972 cp_parser_conversion_type_id (cp_parser* parser)
7975 cp_decl_specifier_seq type_specifiers;
7976 cp_declarator *declarator;
7977 tree type_specified;
7979 /* Parse the attributes. */
7980 attributes = cp_parser_attributes_opt (parser);
7981 /* Parse the type-specifiers. */
7982 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7984 /* If that didn't work, stop. */
7985 if (type_specifiers.type == error_mark_node)
7986 return error_mark_node;
7987 /* Parse the conversion-declarator. */
7988 declarator = cp_parser_conversion_declarator_opt (parser);
7990 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
7991 /*initialized=*/0, &attributes);
7993 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7994 return type_specified;
7997 /* Parse an (optional) conversion-declarator.
7999 conversion-declarator:
8000 ptr-operator conversion-declarator [opt]
8004 static cp_declarator *
8005 cp_parser_conversion_declarator_opt (cp_parser* parser)
8007 enum tree_code code;
8009 cp_cv_quals cv_quals;
8011 /* We don't know if there's a ptr-operator next, or not. */
8012 cp_parser_parse_tentatively (parser);
8013 /* Try the ptr-operator. */
8014 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8015 /* If it worked, look for more conversion-declarators. */
8016 if (cp_parser_parse_definitely (parser))
8018 cp_declarator *declarator;
8020 /* Parse another optional declarator. */
8021 declarator = cp_parser_conversion_declarator_opt (parser);
8023 /* Create the representation of the declarator. */
8025 declarator = make_ptrmem_declarator (cv_quals, class_type,
8027 else if (code == INDIRECT_REF)
8028 declarator = make_pointer_declarator (cv_quals, declarator);
8030 declarator = make_reference_declarator (cv_quals, declarator);
8038 /* Parse an (optional) ctor-initializer.
8041 : mem-initializer-list
8043 Returns TRUE iff the ctor-initializer was actually present. */
8046 cp_parser_ctor_initializer_opt (cp_parser* parser)
8048 /* If the next token is not a `:', then there is no
8049 ctor-initializer. */
8050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8052 /* Do default initialization of any bases and members. */
8053 if (DECL_CONSTRUCTOR_P (current_function_decl))
8054 finish_mem_initializers (NULL_TREE);
8059 /* Consume the `:' token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 /* And the mem-initializer-list. */
8062 cp_parser_mem_initializer_list (parser);
8067 /* Parse a mem-initializer-list.
8069 mem-initializer-list:
8071 mem-initializer , mem-initializer-list */
8074 cp_parser_mem_initializer_list (cp_parser* parser)
8076 tree mem_initializer_list = NULL_TREE;
8078 /* Let the semantic analysis code know that we are starting the
8079 mem-initializer-list. */
8080 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8081 error ("only constructors take base initializers");
8083 /* Loop through the list. */
8086 tree mem_initializer;
8088 /* Parse the mem-initializer. */
8089 mem_initializer = cp_parser_mem_initializer (parser);
8090 /* Add it to the list, unless it was erroneous. */
8091 if (mem_initializer != error_mark_node)
8093 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8094 mem_initializer_list = mem_initializer;
8096 /* If the next token is not a `,', we're done. */
8097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8099 /* Consume the `,' token. */
8100 cp_lexer_consume_token (parser->lexer);
8103 /* Perform semantic analysis. */
8104 if (DECL_CONSTRUCTOR_P (current_function_decl))
8105 finish_mem_initializers (mem_initializer_list);
8108 /* Parse a mem-initializer.
8111 mem-initializer-id ( expression-list [opt] )
8116 ( expression-list [opt] )
8118 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8119 class) or FIELD_DECL (for a non-static data member) to initialize;
8120 the TREE_VALUE is the expression-list. An empty initialization
8121 list is represented by void_list_node. */
8124 cp_parser_mem_initializer (cp_parser* parser)
8126 tree mem_initializer_id;
8127 tree expression_list;
8130 /* Find out what is being initialized. */
8131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8133 pedwarn ("anachronistic old-style base class initializer");
8134 mem_initializer_id = NULL_TREE;
8137 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8138 member = expand_member_init (mem_initializer_id);
8139 if (member && !DECL_P (member))
8140 in_base_initializer = 1;
8143 = cp_parser_parenthesized_expression_list (parser, false,
8145 /*non_constant_p=*/NULL);
8146 if (expression_list == error_mark_node)
8147 return error_mark_node;
8148 if (!expression_list)
8149 expression_list = void_type_node;
8151 in_base_initializer = 0;
8153 return member ? build_tree_list (member, expression_list) : error_mark_node;
8156 /* Parse a mem-initializer-id.
8159 :: [opt] nested-name-specifier [opt] class-name
8162 Returns a TYPE indicating the class to be initializer for the first
8163 production. Returns an IDENTIFIER_NODE indicating the data member
8164 to be initialized for the second production. */
8167 cp_parser_mem_initializer_id (cp_parser* parser)
8169 bool global_scope_p;
8170 bool nested_name_specifier_p;
8171 bool template_p = false;
8174 /* `typename' is not allowed in this context ([temp.res]). */
8175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8177 error ("keyword %<typename%> not allowed in this context (a qualified "
8178 "member initializer is implicitly a type)");
8179 cp_lexer_consume_token (parser->lexer);
8181 /* Look for the optional `::' operator. */
8183 = (cp_parser_global_scope_opt (parser,
8184 /*current_scope_valid_p=*/false)
8186 /* Look for the optional nested-name-specifier. The simplest way to
8191 The keyword `typename' is not permitted in a base-specifier or
8192 mem-initializer; in these contexts a qualified name that
8193 depends on a template-parameter is implicitly assumed to be a
8196 is to assume that we have seen the `typename' keyword at this
8198 nested_name_specifier_p
8199 = (cp_parser_nested_name_specifier_opt (parser,
8200 /*typename_keyword_p=*/true,
8201 /*check_dependency_p=*/true,
8203 /*is_declaration=*/true)
8205 if (nested_name_specifier_p)
8206 template_p = cp_parser_optional_template_keyword (parser);
8207 /* If there is a `::' operator or a nested-name-specifier, then we
8208 are definitely looking for a class-name. */
8209 if (global_scope_p || nested_name_specifier_p)
8210 return cp_parser_class_name (parser,
8211 /*typename_keyword_p=*/true,
8212 /*template_keyword_p=*/template_p,
8214 /*check_dependency_p=*/true,
8215 /*class_head_p=*/false,
8216 /*is_declaration=*/true);
8217 /* Otherwise, we could also be looking for an ordinary identifier. */
8218 cp_parser_parse_tentatively (parser);
8219 /* Try a class-name. */
8220 id = cp_parser_class_name (parser,
8221 /*typename_keyword_p=*/true,
8222 /*template_keyword_p=*/false,
8224 /*check_dependency_p=*/true,
8225 /*class_head_p=*/false,
8226 /*is_declaration=*/true);
8227 /* If we found one, we're done. */
8228 if (cp_parser_parse_definitely (parser))
8230 /* Otherwise, look for an ordinary identifier. */
8231 return cp_parser_identifier (parser);
8234 /* Overloading [gram.over] */
8236 /* Parse an operator-function-id.
8238 operator-function-id:
8241 Returns an IDENTIFIER_NODE for the operator which is a
8242 human-readable spelling of the identifier, e.g., `operator +'. */
8245 cp_parser_operator_function_id (cp_parser* parser)
8247 /* Look for the `operator' keyword. */
8248 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8249 return error_mark_node;
8250 /* And then the name of the operator itself. */
8251 return cp_parser_operator (parser);
8254 /* Parse an operator.
8257 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8258 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8259 || ++ -- , ->* -> () []
8266 Returns an IDENTIFIER_NODE for the operator which is a
8267 human-readable spelling of the identifier, e.g., `operator +'. */
8270 cp_parser_operator (cp_parser* parser)
8272 tree id = NULL_TREE;
8275 /* Peek at the next token. */
8276 token = cp_lexer_peek_token (parser->lexer);
8277 /* Figure out which operator we have. */
8278 switch (token->type)
8284 /* The keyword should be either `new' or `delete'. */
8285 if (token->keyword == RID_NEW)
8287 else if (token->keyword == RID_DELETE)
8292 /* Consume the `new' or `delete' token. */
8293 cp_lexer_consume_token (parser->lexer);
8295 /* Peek at the next token. */
8296 token = cp_lexer_peek_token (parser->lexer);
8297 /* If it's a `[' token then this is the array variant of the
8299 if (token->type == CPP_OPEN_SQUARE)
8301 /* Consume the `[' token. */
8302 cp_lexer_consume_token (parser->lexer);
8303 /* Look for the `]' token. */
8304 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8305 id = ansi_opname (op == NEW_EXPR
8306 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8308 /* Otherwise, we have the non-array variant. */
8310 id = ansi_opname (op);
8316 id = ansi_opname (PLUS_EXPR);
8320 id = ansi_opname (MINUS_EXPR);
8324 id = ansi_opname (MULT_EXPR);
8328 id = ansi_opname (TRUNC_DIV_EXPR);
8332 id = ansi_opname (TRUNC_MOD_EXPR);
8336 id = ansi_opname (BIT_XOR_EXPR);
8340 id = ansi_opname (BIT_AND_EXPR);
8344 id = ansi_opname (BIT_IOR_EXPR);
8348 id = ansi_opname (BIT_NOT_EXPR);
8352 id = ansi_opname (TRUTH_NOT_EXPR);
8356 id = ansi_assopname (NOP_EXPR);
8360 id = ansi_opname (LT_EXPR);
8364 id = ansi_opname (GT_EXPR);
8368 id = ansi_assopname (PLUS_EXPR);
8372 id = ansi_assopname (MINUS_EXPR);
8376 id = ansi_assopname (MULT_EXPR);
8380 id = ansi_assopname (TRUNC_DIV_EXPR);
8384 id = ansi_assopname (TRUNC_MOD_EXPR);
8388 id = ansi_assopname (BIT_XOR_EXPR);
8392 id = ansi_assopname (BIT_AND_EXPR);
8396 id = ansi_assopname (BIT_IOR_EXPR);
8400 id = ansi_opname (LSHIFT_EXPR);
8404 id = ansi_opname (RSHIFT_EXPR);
8408 id = ansi_assopname (LSHIFT_EXPR);
8412 id = ansi_assopname (RSHIFT_EXPR);
8416 id = ansi_opname (EQ_EXPR);
8420 id = ansi_opname (NE_EXPR);
8424 id = ansi_opname (LE_EXPR);
8427 case CPP_GREATER_EQ:
8428 id = ansi_opname (GE_EXPR);
8432 id = ansi_opname (TRUTH_ANDIF_EXPR);
8436 id = ansi_opname (TRUTH_ORIF_EXPR);
8440 id = ansi_opname (POSTINCREMENT_EXPR);
8443 case CPP_MINUS_MINUS:
8444 id = ansi_opname (PREDECREMENT_EXPR);
8448 id = ansi_opname (COMPOUND_EXPR);
8451 case CPP_DEREF_STAR:
8452 id = ansi_opname (MEMBER_REF);
8456 id = ansi_opname (COMPONENT_REF);
8459 case CPP_OPEN_PAREN:
8460 /* Consume the `('. */
8461 cp_lexer_consume_token (parser->lexer);
8462 /* Look for the matching `)'. */
8463 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8464 return ansi_opname (CALL_EXPR);
8466 case CPP_OPEN_SQUARE:
8467 /* Consume the `['. */
8468 cp_lexer_consume_token (parser->lexer);
8469 /* Look for the matching `]'. */
8470 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8471 return ansi_opname (ARRAY_REF);
8474 /* Anything else is an error. */
8478 /* If we have selected an identifier, we need to consume the
8481 cp_lexer_consume_token (parser->lexer);
8482 /* Otherwise, no valid operator name was present. */
8485 cp_parser_error (parser, "expected operator");
8486 id = error_mark_node;
8492 /* Parse a template-declaration.
8494 template-declaration:
8495 export [opt] template < template-parameter-list > declaration
8497 If MEMBER_P is TRUE, this template-declaration occurs within a
8500 The grammar rule given by the standard isn't correct. What
8503 template-declaration:
8504 export [opt] template-parameter-list-seq
8505 decl-specifier-seq [opt] init-declarator [opt] ;
8506 export [opt] template-parameter-list-seq
8509 template-parameter-list-seq:
8510 template-parameter-list-seq [opt]
8511 template < template-parameter-list > */
8514 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8516 /* Check for `export'. */
8517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8519 /* Consume the `export' token. */
8520 cp_lexer_consume_token (parser->lexer);
8521 /* Warn that we do not support `export'. */
8522 warning (0, "keyword %<export%> not implemented, and will be ignored");
8525 cp_parser_template_declaration_after_export (parser, member_p);
8528 /* Parse a template-parameter-list.
8530 template-parameter-list:
8532 template-parameter-list , template-parameter
8534 Returns a TREE_LIST. Each node represents a template parameter.
8535 The nodes are connected via their TREE_CHAINs. */
8538 cp_parser_template_parameter_list (cp_parser* parser)
8540 tree parameter_list = NULL_TREE;
8542 begin_template_parm_list ();
8549 /* Parse the template-parameter. */
8550 parameter = cp_parser_template_parameter (parser, &is_non_type);
8551 /* Add it to the list. */
8552 if (parameter != error_mark_node)
8553 parameter_list = process_template_parm (parameter_list,
8558 tree err_parm = build_tree_list (parameter, parameter);
8559 TREE_VALUE (err_parm) = error_mark_node;
8560 parameter_list = chainon (parameter_list, err_parm);
8563 /* Peek at the next token. */
8564 token = cp_lexer_peek_token (parser->lexer);
8565 /* If it's not a `,', we're done. */
8566 if (token->type != CPP_COMMA)
8568 /* Otherwise, consume the `,' token. */
8569 cp_lexer_consume_token (parser->lexer);
8572 return end_template_parm_list (parameter_list);
8575 /* Parse a template-parameter.
8579 parameter-declaration
8581 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8582 the parameter. The TREE_PURPOSE is the default value, if any.
8583 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8584 iff this parameter is a non-type parameter. */
8587 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8590 cp_parameter_declarator *parameter_declarator;
8593 /* Assume it is a type parameter or a template parameter. */
8594 *is_non_type = false;
8595 /* Peek at the next token. */
8596 token = cp_lexer_peek_token (parser->lexer);
8597 /* If it is `class' or `template', we have a type-parameter. */
8598 if (token->keyword == RID_TEMPLATE)
8599 return cp_parser_type_parameter (parser);
8600 /* If it is `class' or `typename' we do not know yet whether it is a
8601 type parameter or a non-type parameter. Consider:
8603 template <typename T, typename T::X X> ...
8607 template <class C, class D*> ...
8609 Here, the first parameter is a type parameter, and the second is
8610 a non-type parameter. We can tell by looking at the token after
8611 the identifier -- if it is a `,', `=', or `>' then we have a type
8613 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8615 /* Peek at the token after `class' or `typename'. */
8616 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8617 /* If it's an identifier, skip it. */
8618 if (token->type == CPP_NAME)
8619 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8620 /* Now, see if the token looks like the end of a template
8622 if (token->type == CPP_COMMA
8623 || token->type == CPP_EQ
8624 || token->type == CPP_GREATER)
8625 return cp_parser_type_parameter (parser);
8628 /* Otherwise, it is a non-type parameter.
8632 When parsing a default template-argument for a non-type
8633 template-parameter, the first non-nested `>' is taken as the end
8634 of the template parameter-list rather than a greater-than
8636 *is_non_type = true;
8637 parameter_declarator
8638 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8639 /*parenthesized_p=*/NULL);
8640 parm = grokdeclarator (parameter_declarator->declarator,
8641 ¶meter_declarator->decl_specifiers,
8642 PARM, /*initialized=*/0,
8644 if (parm == error_mark_node)
8645 return error_mark_node;
8646 return build_tree_list (parameter_declarator->default_argument, parm);
8649 /* Parse a type-parameter.
8652 class identifier [opt]
8653 class identifier [opt] = type-id
8654 typename identifier [opt]
8655 typename identifier [opt] = type-id
8656 template < template-parameter-list > class identifier [opt]
8657 template < template-parameter-list > class identifier [opt]
8660 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8661 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8662 the declaration of the parameter. */
8665 cp_parser_type_parameter (cp_parser* parser)
8670 /* Look for a keyword to tell us what kind of parameter this is. */
8671 token = cp_parser_require (parser, CPP_KEYWORD,
8672 "`class', `typename', or `template'");
8674 return error_mark_node;
8676 switch (token->keyword)
8682 tree default_argument;
8684 /* If the next token is an identifier, then it names the
8686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8687 identifier = cp_parser_identifier (parser);
8689 identifier = NULL_TREE;
8691 /* Create the parameter. */
8692 parameter = finish_template_type_parm (class_type_node, identifier);
8694 /* If the next token is an `=', we have a default argument. */
8695 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8697 /* Consume the `=' token. */
8698 cp_lexer_consume_token (parser->lexer);
8699 /* Parse the default-argument. */
8700 push_deferring_access_checks (dk_no_deferred);
8701 default_argument = cp_parser_type_id (parser);
8702 pop_deferring_access_checks ();
8705 default_argument = NULL_TREE;
8707 /* Create the combined representation of the parameter and the
8708 default argument. */
8709 parameter = build_tree_list (default_argument, parameter);
8715 tree parameter_list;
8717 tree default_argument;
8719 /* Look for the `<'. */
8720 cp_parser_require (parser, CPP_LESS, "`<'");
8721 /* Parse the template-parameter-list. */
8722 parameter_list = cp_parser_template_parameter_list (parser);
8723 /* Look for the `>'. */
8724 cp_parser_require (parser, CPP_GREATER, "`>'");
8725 /* Look for the `class' keyword. */
8726 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8727 /* If the next token is an `=', then there is a
8728 default-argument. If the next token is a `>', we are at
8729 the end of the parameter-list. If the next token is a `,',
8730 then we are at the end of this parameter. */
8731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8732 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8733 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8735 identifier = cp_parser_identifier (parser);
8736 /* Treat invalid names as if the parameter were nameless. */
8737 if (identifier == error_mark_node)
8738 identifier = NULL_TREE;
8741 identifier = NULL_TREE;
8743 /* Create the template parameter. */
8744 parameter = finish_template_template_parm (class_type_node,
8747 /* If the next token is an `=', then there is a
8748 default-argument. */
8749 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8753 /* Consume the `='. */
8754 cp_lexer_consume_token (parser->lexer);
8755 /* Parse the id-expression. */
8756 push_deferring_access_checks (dk_no_deferred);
8758 = cp_parser_id_expression (parser,
8759 /*template_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8761 /*template_p=*/&is_template,
8762 /*declarator_p=*/false,
8763 /*optional_p=*/false);
8764 if (TREE_CODE (default_argument) == TYPE_DECL)
8765 /* If the id-expression was a template-id that refers to
8766 a template-class, we already have the declaration here,
8767 so no further lookup is needed. */
8770 /* Look up the name. */
8772 = cp_parser_lookup_name (parser, default_argument,
8774 /*is_template=*/is_template,
8775 /*is_namespace=*/false,
8776 /*check_dependency=*/true,
8777 /*ambiguous_decls=*/NULL);
8778 /* See if the default argument is valid. */
8780 = check_template_template_default_arg (default_argument);
8781 pop_deferring_access_checks ();
8784 default_argument = NULL_TREE;
8786 /* Create the combined representation of the parameter and the
8787 default argument. */
8788 parameter = build_tree_list (default_argument, parameter);
8800 /* Parse a template-id.
8803 template-name < template-argument-list [opt] >
8805 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8806 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8807 returned. Otherwise, if the template-name names a function, or set
8808 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8809 names a class, returns a TYPE_DECL for the specialization.
8811 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8812 uninstantiated templates. */
8815 cp_parser_template_id (cp_parser *parser,
8816 bool template_keyword_p,
8817 bool check_dependency_p,
8818 bool is_declaration)
8823 cp_token_position start_of_id = 0;
8824 tree access_check = NULL_TREE;
8825 cp_token *next_token, *next_token_2;
8828 /* If the next token corresponds to a template-id, there is no need
8830 next_token = cp_lexer_peek_token (parser->lexer);
8831 if (next_token->type == CPP_TEMPLATE_ID)
8836 /* Get the stored value. */
8837 value = cp_lexer_consume_token (parser->lexer)->value;
8838 /* Perform any access checks that were deferred. */
8839 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8840 perform_or_defer_access_check (TREE_PURPOSE (check),
8842 TREE_VALUE (check));
8843 /* Return the stored value. */
8844 return TREE_VALUE (value);
8847 /* Avoid performing name lookup if there is no possibility of
8848 finding a template-id. */
8849 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8850 || (next_token->type == CPP_NAME
8851 && !cp_parser_nth_token_starts_template_argument_list_p
8854 cp_parser_error (parser, "expected template-id");
8855 return error_mark_node;
8858 /* Remember where the template-id starts. */
8859 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8860 start_of_id = cp_lexer_token_position (parser->lexer, false);
8862 push_deferring_access_checks (dk_deferred);
8864 /* Parse the template-name. */
8865 is_identifier = false;
8866 template = cp_parser_template_name (parser, template_keyword_p,
8870 if (template == error_mark_node || is_identifier)
8872 pop_deferring_access_checks ();
8876 /* If we find the sequence `[:' after a template-name, it's probably
8877 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8878 parse correctly the argument list. */
8879 next_token = cp_lexer_peek_token (parser->lexer);
8880 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8881 if (next_token->type == CPP_OPEN_SQUARE
8882 && next_token->flags & DIGRAPH
8883 && next_token_2->type == CPP_COLON
8884 && !(next_token_2->flags & PREV_WHITE))
8886 cp_parser_parse_tentatively (parser);
8887 /* Change `:' into `::'. */
8888 next_token_2->type = CPP_SCOPE;
8889 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8891 cp_lexer_consume_token (parser->lexer);
8892 /* Parse the arguments. */
8893 arguments = cp_parser_enclosed_template_argument_list (parser);
8894 if (!cp_parser_parse_definitely (parser))
8896 /* If we couldn't parse an argument list, then we revert our changes
8897 and return simply an error. Maybe this is not a template-id
8899 next_token_2->type = CPP_COLON;
8900 cp_parser_error (parser, "expected %<<%>");
8901 pop_deferring_access_checks ();
8902 return error_mark_node;
8904 /* Otherwise, emit an error about the invalid digraph, but continue
8905 parsing because we got our argument list. */
8906 pedwarn ("%<<::%> cannot begin a template-argument list");
8907 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8908 "between %<<%> and %<::%>");
8909 if (!flag_permissive)
8914 inform ("(if you use -fpermissive G++ will accept your code)");
8921 /* Look for the `<' that starts the template-argument-list. */
8922 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8924 pop_deferring_access_checks ();
8925 return error_mark_node;
8927 /* Parse the arguments. */
8928 arguments = cp_parser_enclosed_template_argument_list (parser);
8931 /* Build a representation of the specialization. */
8932 if (TREE_CODE (template) == IDENTIFIER_NODE)
8933 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8934 else if (DECL_CLASS_TEMPLATE_P (template)
8935 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8937 bool entering_scope;
8938 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8939 template (rather than some instantiation thereof) only if
8940 is not nested within some other construct. For example, in
8941 "template <typename T> void f(T) { A<T>::", A<T> is just an
8942 instantiation of A. */
8943 entering_scope = (template_parm_scope_p ()
8944 && cp_lexer_next_token_is (parser->lexer,
8947 = finish_template_type (template, arguments, entering_scope);
8951 /* If it's not a class-template or a template-template, it should be
8952 a function-template. */
8953 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8954 || TREE_CODE (template) == OVERLOAD
8955 || BASELINK_P (template)));
8957 template_id = lookup_template_function (template, arguments);
8960 /* Retrieve any deferred checks. Do not pop this access checks yet
8961 so the memory will not be reclaimed during token replacing below. */
8962 access_check = get_deferred_access_checks ();
8964 /* If parsing tentatively, replace the sequence of tokens that makes
8965 up the template-id with a CPP_TEMPLATE_ID token. That way,
8966 should we re-parse the token stream, we will not have to repeat
8967 the effort required to do the parse, nor will we issue duplicate
8968 error messages about problems during instantiation of the
8972 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8974 /* Reset the contents of the START_OF_ID token. */
8975 token->type = CPP_TEMPLATE_ID;
8976 token->value = build_tree_list (access_check, template_id);
8977 token->keyword = RID_MAX;
8979 /* Purge all subsequent tokens. */
8980 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8982 /* ??? Can we actually assume that, if template_id ==
8983 error_mark_node, we will have issued a diagnostic to the
8984 user, as opposed to simply marking the tentative parse as
8986 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8987 error ("parse error in template argument list");
8990 pop_deferring_access_checks ();
8994 /* Parse a template-name.
8999 The standard should actually say:
9003 operator-function-id
9005 A defect report has been filed about this issue.
9007 A conversion-function-id cannot be a template name because they cannot
9008 be part of a template-id. In fact, looking at this code:
9012 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9013 It is impossible to call a templated conversion-function-id with an
9014 explicit argument list, since the only allowed template parameter is
9015 the type to which it is converting.
9017 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9018 `template' keyword, in a construction like:
9022 In that case `f' is taken to be a template-name, even though there
9023 is no way of knowing for sure.
9025 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9026 name refers to a set of overloaded functions, at least one of which
9027 is a template, or an IDENTIFIER_NODE with the name of the template,
9028 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9029 names are looked up inside uninstantiated templates. */
9032 cp_parser_template_name (cp_parser* parser,
9033 bool template_keyword_p,
9034 bool check_dependency_p,
9035 bool is_declaration,
9036 bool *is_identifier)
9042 /* If the next token is `operator', then we have either an
9043 operator-function-id or a conversion-function-id. */
9044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9046 /* We don't know whether we're looking at an
9047 operator-function-id or a conversion-function-id. */
9048 cp_parser_parse_tentatively (parser);
9049 /* Try an operator-function-id. */
9050 identifier = cp_parser_operator_function_id (parser);
9051 /* If that didn't work, try a conversion-function-id. */
9052 if (!cp_parser_parse_definitely (parser))
9054 cp_parser_error (parser, "expected template-name");
9055 return error_mark_node;
9058 /* Look for the identifier. */
9060 identifier = cp_parser_identifier (parser);
9062 /* If we didn't find an identifier, we don't have a template-id. */
9063 if (identifier == error_mark_node)
9064 return error_mark_node;
9066 /* If the name immediately followed the `template' keyword, then it
9067 is a template-name. However, if the next token is not `<', then
9068 we do not treat it as a template-name, since it is not being used
9069 as part of a template-id. This enables us to handle constructs
9072 template <typename T> struct S { S(); };
9073 template <typename T> S<T>::S();
9075 correctly. We would treat `S' as a template -- if it were `S<T>'
9076 -- but we do not if there is no `<'. */
9078 if (processing_template_decl
9079 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9081 /* In a declaration, in a dependent context, we pretend that the
9082 "template" keyword was present in order to improve error
9083 recovery. For example, given:
9085 template <typename T> void f(T::X<int>);
9087 we want to treat "X<int>" as a template-id. */
9089 && !template_keyword_p
9090 && parser->scope && TYPE_P (parser->scope)
9091 && check_dependency_p
9092 && dependent_type_p (parser->scope)
9093 /* Do not do this for dtors (or ctors), since they never
9094 need the template keyword before their name. */
9095 && !constructor_name_p (identifier, parser->scope))
9097 cp_token_position start = 0;
9099 /* Explain what went wrong. */
9100 error ("non-template %qD used as template", identifier);
9101 inform ("use %<%T::template %D%> to indicate that it is a template",
9102 parser->scope, identifier);
9103 /* If parsing tentatively, find the location of the "<" token. */
9104 if (cp_parser_simulate_error (parser))
9105 start = cp_lexer_token_position (parser->lexer, true);
9106 /* Parse the template arguments so that we can issue error
9107 messages about them. */
9108 cp_lexer_consume_token (parser->lexer);
9109 cp_parser_enclosed_template_argument_list (parser);
9110 /* Skip tokens until we find a good place from which to
9111 continue parsing. */
9112 cp_parser_skip_to_closing_parenthesis (parser,
9113 /*recovering=*/true,
9115 /*consume_paren=*/false);
9116 /* If parsing tentatively, permanently remove the
9117 template argument list. That will prevent duplicate
9118 error messages from being issued about the missing
9119 "template" keyword. */
9121 cp_lexer_purge_tokens_after (parser->lexer, start);
9123 *is_identifier = true;
9127 /* If the "template" keyword is present, then there is generally
9128 no point in doing name-lookup, so we just return IDENTIFIER.
9129 But, if the qualifying scope is non-dependent then we can
9130 (and must) do name-lookup normally. */
9131 if (template_keyword_p
9133 || (TYPE_P (parser->scope)
9134 && dependent_type_p (parser->scope))))
9138 /* Look up the name. */
9139 decl = cp_parser_lookup_name (parser, identifier,
9141 /*is_template=*/false,
9142 /*is_namespace=*/false,
9144 /*ambiguous_decls=*/NULL);
9145 decl = maybe_get_template_decl_from_type_decl (decl);
9147 /* If DECL is a template, then the name was a template-name. */
9148 if (TREE_CODE (decl) == TEMPLATE_DECL)
9152 tree fn = NULL_TREE;
9154 /* The standard does not explicitly indicate whether a name that
9155 names a set of overloaded declarations, some of which are
9156 templates, is a template-name. However, such a name should
9157 be a template-name; otherwise, there is no way to form a
9158 template-id for the overloaded templates. */
9159 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9160 if (TREE_CODE (fns) == OVERLOAD)
9161 for (fn = fns; fn; fn = OVL_NEXT (fn))
9162 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9167 /* The name does not name a template. */
9168 cp_parser_error (parser, "expected template-name");
9169 return error_mark_node;
9173 /* If DECL is dependent, and refers to a function, then just return
9174 its name; we will look it up again during template instantiation. */
9175 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9177 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9178 if (TYPE_P (scope) && dependent_type_p (scope))
9185 /* Parse a template-argument-list.
9187 template-argument-list:
9189 template-argument-list , template-argument
9191 Returns a TREE_VEC containing the arguments. */
9194 cp_parser_template_argument_list (cp_parser* parser)
9196 tree fixed_args[10];
9197 unsigned n_args = 0;
9198 unsigned alloced = 10;
9199 tree *arg_ary = fixed_args;
9201 bool saved_in_template_argument_list_p;
9203 bool saved_non_ice_p;
9205 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9206 parser->in_template_argument_list_p = true;
9207 /* Even if the template-id appears in an integral
9208 constant-expression, the contents of the argument list do
9210 saved_ice_p = parser->integral_constant_expression_p;
9211 parser->integral_constant_expression_p = false;
9212 saved_non_ice_p = parser->non_integral_constant_expression_p;
9213 parser->non_integral_constant_expression_p = false;
9214 /* Parse the arguments. */
9220 /* Consume the comma. */
9221 cp_lexer_consume_token (parser->lexer);
9223 /* Parse the template-argument. */
9224 argument = cp_parser_template_argument (parser);
9225 if (n_args == alloced)
9229 if (arg_ary == fixed_args)
9231 arg_ary = XNEWVEC (tree, alloced);
9232 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9235 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9237 arg_ary[n_args++] = argument;
9239 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9241 vec = make_tree_vec (n_args);
9244 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9246 if (arg_ary != fixed_args)
9248 parser->non_integral_constant_expression_p = saved_non_ice_p;
9249 parser->integral_constant_expression_p = saved_ice_p;
9250 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9254 /* Parse a template-argument.
9257 assignment-expression
9261 The representation is that of an assignment-expression, type-id, or
9262 id-expression -- except that the qualified id-expression is
9263 evaluated, so that the value returned is either a DECL or an
9266 Although the standard says "assignment-expression", it forbids
9267 throw-expressions or assignments in the template argument.
9268 Therefore, we use "conditional-expression" instead. */
9271 cp_parser_template_argument (cp_parser* parser)
9276 bool maybe_type_id = false;
9280 /* There's really no way to know what we're looking at, so we just
9281 try each alternative in order.
9285 In a template-argument, an ambiguity between a type-id and an
9286 expression is resolved to a type-id, regardless of the form of
9287 the corresponding template-parameter.
9289 Therefore, we try a type-id first. */
9290 cp_parser_parse_tentatively (parser);
9291 argument = cp_parser_type_id (parser);
9292 /* If there was no error parsing the type-id but the next token is a '>>',
9293 we probably found a typo for '> >'. But there are type-id which are
9294 also valid expressions. For instance:
9296 struct X { int operator >> (int); };
9297 template <int V> struct Foo {};
9300 Here 'X()' is a valid type-id of a function type, but the user just
9301 wanted to write the expression "X() >> 5". Thus, we remember that we
9302 found a valid type-id, but we still try to parse the argument as an
9303 expression to see what happens. */
9304 if (!cp_parser_error_occurred (parser)
9305 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9307 maybe_type_id = true;
9308 cp_parser_abort_tentative_parse (parser);
9312 /* If the next token isn't a `,' or a `>', then this argument wasn't
9313 really finished. This means that the argument is not a valid
9315 if (!cp_parser_next_token_ends_template_argument_p (parser))
9316 cp_parser_error (parser, "expected template-argument");
9317 /* If that worked, we're done. */
9318 if (cp_parser_parse_definitely (parser))
9321 /* We're still not sure what the argument will be. */
9322 cp_parser_parse_tentatively (parser);
9323 /* Try a template. */
9324 argument = cp_parser_id_expression (parser,
9325 /*template_keyword_p=*/false,
9326 /*check_dependency_p=*/true,
9328 /*declarator_p=*/false,
9329 /*optional_p=*/false);
9330 /* If the next token isn't a `,' or a `>', then this argument wasn't
9332 if (!cp_parser_next_token_ends_template_argument_p (parser))
9333 cp_parser_error (parser, "expected template-argument");
9334 if (!cp_parser_error_occurred (parser))
9336 /* Figure out what is being referred to. If the id-expression
9337 was for a class template specialization, then we will have a
9338 TYPE_DECL at this point. There is no need to do name lookup
9339 at this point in that case. */
9340 if (TREE_CODE (argument) != TYPE_DECL)
9341 argument = cp_parser_lookup_name (parser, argument,
9343 /*is_template=*/template_p,
9344 /*is_namespace=*/false,
9345 /*check_dependency=*/true,
9346 /*ambiguous_decls=*/NULL);
9347 if (TREE_CODE (argument) != TEMPLATE_DECL
9348 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9349 cp_parser_error (parser, "expected template-name");
9351 if (cp_parser_parse_definitely (parser))
9353 /* It must be a non-type argument. There permitted cases are given
9354 in [temp.arg.nontype]:
9356 -- an integral constant-expression of integral or enumeration
9359 -- the name of a non-type template-parameter; or
9361 -- the name of an object or function with external linkage...
9363 -- the address of an object or function with external linkage...
9365 -- a pointer to member... */
9366 /* Look for a non-type template parameter. */
9367 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9369 cp_parser_parse_tentatively (parser);
9370 argument = cp_parser_primary_expression (parser,
9373 /*template_arg_p=*/true,
9375 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9376 || !cp_parser_next_token_ends_template_argument_p (parser))
9377 cp_parser_simulate_error (parser);
9378 if (cp_parser_parse_definitely (parser))
9382 /* If the next token is "&", the argument must be the address of an
9383 object or function with external linkage. */
9384 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9386 cp_lexer_consume_token (parser->lexer);
9387 /* See if we might have an id-expression. */
9388 token = cp_lexer_peek_token (parser->lexer);
9389 if (token->type == CPP_NAME
9390 || token->keyword == RID_OPERATOR
9391 || token->type == CPP_SCOPE
9392 || token->type == CPP_TEMPLATE_ID
9393 || token->type == CPP_NESTED_NAME_SPECIFIER)
9395 cp_parser_parse_tentatively (parser);
9396 argument = cp_parser_primary_expression (parser,
9399 /*template_arg_p=*/true,
9401 if (cp_parser_error_occurred (parser)
9402 || !cp_parser_next_token_ends_template_argument_p (parser))
9403 cp_parser_abort_tentative_parse (parser);
9406 if (TREE_CODE (argument) == INDIRECT_REF)
9408 gcc_assert (REFERENCE_REF_P (argument));
9409 argument = TREE_OPERAND (argument, 0);
9412 if (TREE_CODE (argument) == VAR_DECL)
9414 /* A variable without external linkage might still be a
9415 valid constant-expression, so no error is issued here
9416 if the external-linkage check fails. */
9417 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9418 cp_parser_simulate_error (parser);
9420 else if (is_overloaded_fn (argument))
9421 /* All overloaded functions are allowed; if the external
9422 linkage test does not pass, an error will be issued
9426 && (TREE_CODE (argument) == OFFSET_REF
9427 || TREE_CODE (argument) == SCOPE_REF))
9428 /* A pointer-to-member. */
9430 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9433 cp_parser_simulate_error (parser);
9435 if (cp_parser_parse_definitely (parser))
9438 argument = build_x_unary_op (ADDR_EXPR, argument);
9443 /* If the argument started with "&", there are no other valid
9444 alternatives at this point. */
9447 cp_parser_error (parser, "invalid non-type template argument");
9448 return error_mark_node;
9451 /* If the argument wasn't successfully parsed as a type-id followed
9452 by '>>', the argument can only be a constant expression now.
9453 Otherwise, we try parsing the constant-expression tentatively,
9454 because the argument could really be a type-id. */
9456 cp_parser_parse_tentatively (parser);
9457 argument = cp_parser_constant_expression (parser,
9458 /*allow_non_constant_p=*/false,
9459 /*non_constant_p=*/NULL);
9460 argument = fold_non_dependent_expr (argument);
9463 if (!cp_parser_next_token_ends_template_argument_p (parser))
9464 cp_parser_error (parser, "expected template-argument");
9465 if (cp_parser_parse_definitely (parser))
9467 /* We did our best to parse the argument as a non type-id, but that
9468 was the only alternative that matched (albeit with a '>' after
9469 it). We can assume it's just a typo from the user, and a
9470 diagnostic will then be issued. */
9471 return cp_parser_type_id (parser);
9474 /* Parse an explicit-instantiation.
9476 explicit-instantiation:
9477 template declaration
9479 Although the standard says `declaration', what it really means is:
9481 explicit-instantiation:
9482 template decl-specifier-seq [opt] declarator [opt] ;
9484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9485 supposed to be allowed. A defect report has been filed about this
9490 explicit-instantiation:
9491 storage-class-specifier template
9492 decl-specifier-seq [opt] declarator [opt] ;
9493 function-specifier template
9494 decl-specifier-seq [opt] declarator [opt] ; */
9497 cp_parser_explicit_instantiation (cp_parser* parser)
9499 int declares_class_or_enum;
9500 cp_decl_specifier_seq decl_specifiers;
9501 tree extension_specifier = NULL_TREE;
9503 /* Look for an (optional) storage-class-specifier or
9504 function-specifier. */
9505 if (cp_parser_allow_gnu_extensions_p (parser))
9508 = cp_parser_storage_class_specifier_opt (parser);
9509 if (!extension_specifier)
9511 = cp_parser_function_specifier_opt (parser,
9512 /*decl_specs=*/NULL);
9515 /* Look for the `template' keyword. */
9516 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9517 /* Let the front end know that we are processing an explicit
9519 begin_explicit_instantiation ();
9520 /* [temp.explicit] says that we are supposed to ignore access
9521 control while processing explicit instantiation directives. */
9522 push_deferring_access_checks (dk_no_check);
9523 /* Parse a decl-specifier-seq. */
9524 cp_parser_decl_specifier_seq (parser,
9525 CP_PARSER_FLAGS_OPTIONAL,
9527 &declares_class_or_enum);
9528 /* If there was exactly one decl-specifier, and it declared a class,
9529 and there's no declarator, then we have an explicit type
9531 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9535 type = check_tag_decl (&decl_specifiers);
9536 /* Turn access control back on for names used during
9537 template instantiation. */
9538 pop_deferring_access_checks ();
9540 do_type_instantiation (type, extension_specifier,
9541 /*complain=*/tf_error);
9545 cp_declarator *declarator;
9548 /* Parse the declarator. */
9550 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9551 /*ctor_dtor_or_conv_p=*/NULL,
9552 /*parenthesized_p=*/NULL,
9553 /*member_p=*/false);
9554 if (declares_class_or_enum & 2)
9555 cp_parser_check_for_definition_in_return_type (declarator,
9556 decl_specifiers.type);
9557 if (declarator != cp_error_declarator)
9559 decl = grokdeclarator (declarator, &decl_specifiers,
9560 NORMAL, 0, &decl_specifiers.attributes);
9561 /* Turn access control back on for names used during
9562 template instantiation. */
9563 pop_deferring_access_checks ();
9564 /* Do the explicit instantiation. */
9565 do_decl_instantiation (decl, extension_specifier);
9569 pop_deferring_access_checks ();
9570 /* Skip the body of the explicit instantiation. */
9571 cp_parser_skip_to_end_of_statement (parser);
9574 /* We're done with the instantiation. */
9575 end_explicit_instantiation ();
9577 cp_parser_consume_semicolon_at_end_of_statement (parser);
9580 /* Parse an explicit-specialization.
9582 explicit-specialization:
9583 template < > declaration
9585 Although the standard says `declaration', what it really means is:
9587 explicit-specialization:
9588 template <> decl-specifier [opt] init-declarator [opt] ;
9589 template <> function-definition
9590 template <> explicit-specialization
9591 template <> template-declaration */
9594 cp_parser_explicit_specialization (cp_parser* parser)
9597 /* Look for the `template' keyword. */
9598 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9599 /* Look for the `<'. */
9600 cp_parser_require (parser, CPP_LESS, "`<'");
9601 /* Look for the `>'. */
9602 cp_parser_require (parser, CPP_GREATER, "`>'");
9603 /* We have processed another parameter list. */
9604 ++parser->num_template_parameter_lists;
9607 A template ... explicit specialization ... shall not have C
9609 if (current_lang_name == lang_name_c)
9611 error ("template specialization with C linkage");
9612 /* Give it C++ linkage to avoid confusing other parts of the
9614 push_lang_context (lang_name_cplusplus);
9615 need_lang_pop = true;
9618 need_lang_pop = false;
9619 /* Let the front end know that we are beginning a specialization. */
9620 if (!begin_specialization ())
9622 end_specialization ();
9623 cp_parser_skip_to_end_of_block_or_statement (parser);
9627 /* If the next keyword is `template', we need to figure out whether
9628 or not we're looking a template-declaration. */
9629 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9631 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9632 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9633 cp_parser_template_declaration_after_export (parser,
9634 /*member_p=*/false);
9636 cp_parser_explicit_specialization (parser);
9639 /* Parse the dependent declaration. */
9640 cp_parser_single_declaration (parser,
9641 /*checks=*/NULL_TREE,
9644 /* We're done with the specialization. */
9645 end_specialization ();
9646 /* For the erroneous case of a template with C linkage, we pushed an
9647 implicit C++ linkage scope; exit that scope now. */
9649 pop_lang_context ();
9650 /* We're done with this parameter list. */
9651 --parser->num_template_parameter_lists;
9654 /* Parse a type-specifier.
9657 simple-type-specifier
9660 elaborated-type-specifier
9668 Returns a representation of the type-specifier. For a
9669 class-specifier, enum-specifier, or elaborated-type-specifier, a
9670 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9672 The parser flags FLAGS is used to control type-specifier parsing.
9674 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9675 in a decl-specifier-seq.
9677 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9678 class-specifier, enum-specifier, or elaborated-type-specifier, then
9679 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9680 if a type is declared; 2 if it is defined. Otherwise, it is set to
9683 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9684 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9688 cp_parser_type_specifier (cp_parser* parser,
9689 cp_parser_flags flags,
9690 cp_decl_specifier_seq *decl_specs,
9691 bool is_declaration,
9692 int* declares_class_or_enum,
9693 bool* is_cv_qualifier)
9695 tree type_spec = NULL_TREE;
9698 cp_decl_spec ds = ds_last;
9700 /* Assume this type-specifier does not declare a new type. */
9701 if (declares_class_or_enum)
9702 *declares_class_or_enum = 0;
9703 /* And that it does not specify a cv-qualifier. */
9704 if (is_cv_qualifier)
9705 *is_cv_qualifier = false;
9706 /* Peek at the next token. */
9707 token = cp_lexer_peek_token (parser->lexer);
9709 /* If we're looking at a keyword, we can use that to guide the
9710 production we choose. */
9711 keyword = token->keyword;
9715 /* Look for the enum-specifier. */
9716 type_spec = cp_parser_enum_specifier (parser);
9717 /* If that worked, we're done. */
9720 if (declares_class_or_enum)
9721 *declares_class_or_enum = 2;
9723 cp_parser_set_decl_spec_type (decl_specs,
9725 /*user_defined_p=*/true);
9729 goto elaborated_type_specifier;
9731 /* Any of these indicate either a class-specifier, or an
9732 elaborated-type-specifier. */
9736 /* Parse tentatively so that we can back up if we don't find a
9738 cp_parser_parse_tentatively (parser);
9739 /* Look for the class-specifier. */
9740 type_spec = cp_parser_class_specifier (parser);
9741 /* If that worked, we're done. */
9742 if (cp_parser_parse_definitely (parser))
9744 if (declares_class_or_enum)
9745 *declares_class_or_enum = 2;
9747 cp_parser_set_decl_spec_type (decl_specs,
9749 /*user_defined_p=*/true);
9754 elaborated_type_specifier:
9755 /* We're declaring (not defining) a class or enum. */
9756 if (declares_class_or_enum)
9757 *declares_class_or_enum = 1;
9761 /* Look for an elaborated-type-specifier. */
9763 = (cp_parser_elaborated_type_specifier
9765 decl_specs && decl_specs->specs[(int) ds_friend],
9768 cp_parser_set_decl_spec_type (decl_specs,
9770 /*user_defined_p=*/true);
9775 if (is_cv_qualifier)
9776 *is_cv_qualifier = true;
9781 if (is_cv_qualifier)
9782 *is_cv_qualifier = true;
9787 if (is_cv_qualifier)
9788 *is_cv_qualifier = true;
9792 /* The `__complex__' keyword is a GNU extension. */
9800 /* Handle simple keywords. */
9805 ++decl_specs->specs[(int)ds];
9806 decl_specs->any_specifiers_p = true;
9808 return cp_lexer_consume_token (parser->lexer)->value;
9811 /* If we do not already have a type-specifier, assume we are looking
9812 at a simple-type-specifier. */
9813 type_spec = cp_parser_simple_type_specifier (parser,
9817 /* If we didn't find a type-specifier, and a type-specifier was not
9818 optional in this context, issue an error message. */
9819 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9821 cp_parser_error (parser, "expected type specifier");
9822 return error_mark_node;
9828 /* Parse a simple-type-specifier.
9830 simple-type-specifier:
9831 :: [opt] nested-name-specifier [opt] type-name
9832 :: [opt] nested-name-specifier template template-id
9847 simple-type-specifier:
9848 __typeof__ unary-expression
9849 __typeof__ ( type-id )
9851 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9852 appropriately updated. */
9855 cp_parser_simple_type_specifier (cp_parser* parser,
9856 cp_decl_specifier_seq *decl_specs,
9857 cp_parser_flags flags)
9859 tree type = NULL_TREE;
9862 /* Peek at the next token. */
9863 token = cp_lexer_peek_token (parser->lexer);
9865 /* If we're looking at a keyword, things are easy. */
9866 switch (token->keyword)
9870 decl_specs->explicit_char_p = true;
9871 type = char_type_node;
9874 type = wchar_type_node;
9877 type = boolean_type_node;
9881 ++decl_specs->specs[(int) ds_short];
9882 type = short_integer_type_node;
9886 decl_specs->explicit_int_p = true;
9887 type = integer_type_node;
9891 ++decl_specs->specs[(int) ds_long];
9892 type = long_integer_type_node;
9896 ++decl_specs->specs[(int) ds_signed];
9897 type = integer_type_node;
9901 ++decl_specs->specs[(int) ds_unsigned];
9902 type = unsigned_type_node;
9905 type = float_type_node;
9908 type = double_type_node;
9911 type = void_type_node;
9915 /* Consume the `typeof' token. */
9916 cp_lexer_consume_token (parser->lexer);
9917 /* Parse the operand to `typeof'. */
9918 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9919 /* If it is not already a TYPE, take its type. */
9921 type = finish_typeof (type);
9924 cp_parser_set_decl_spec_type (decl_specs, type,
9925 /*user_defined_p=*/true);
9933 /* If the type-specifier was for a built-in type, we're done. */
9938 /* Record the type. */
9940 && (token->keyword != RID_SIGNED
9941 && token->keyword != RID_UNSIGNED
9942 && token->keyword != RID_SHORT
9943 && token->keyword != RID_LONG))
9944 cp_parser_set_decl_spec_type (decl_specs,
9946 /*user_defined=*/false);
9948 decl_specs->any_specifiers_p = true;
9950 /* Consume the token. */
9951 id = cp_lexer_consume_token (parser->lexer)->value;
9953 /* There is no valid C++ program where a non-template type is
9954 followed by a "<". That usually indicates that the user thought
9955 that the type was a template. */
9956 cp_parser_check_for_invalid_template_id (parser, type);
9958 return TYPE_NAME (type);
9961 /* The type-specifier must be a user-defined type. */
9962 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9967 /* Don't gobble tokens or issue error messages if this is an
9968 optional type-specifier. */
9969 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9970 cp_parser_parse_tentatively (parser);
9972 /* Look for the optional `::' operator. */
9974 = (cp_parser_global_scope_opt (parser,
9975 /*current_scope_valid_p=*/false)
9977 /* Look for the nested-name specifier. */
9979 = (cp_parser_nested_name_specifier_opt (parser,
9980 /*typename_keyword_p=*/false,
9981 /*check_dependency_p=*/true,
9983 /*is_declaration=*/false)
9985 /* If we have seen a nested-name-specifier, and the next token
9986 is `template', then we are using the template-id production. */
9988 && cp_parser_optional_template_keyword (parser))
9990 /* Look for the template-id. */
9991 type = cp_parser_template_id (parser,
9992 /*template_keyword_p=*/true,
9993 /*check_dependency_p=*/true,
9994 /*is_declaration=*/false);
9995 /* If the template-id did not name a type, we are out of
9997 if (TREE_CODE (type) != TYPE_DECL)
9999 cp_parser_error (parser, "expected template-id for type");
10003 /* Otherwise, look for a type-name. */
10005 type = cp_parser_type_name (parser);
10006 /* Keep track of all name-lookups performed in class scopes. */
10010 && TREE_CODE (type) == TYPE_DECL
10011 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10012 maybe_note_name_used_in_class (DECL_NAME (type), type);
10013 /* If it didn't work out, we don't have a TYPE. */
10014 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10015 && !cp_parser_parse_definitely (parser))
10017 if (type && decl_specs)
10018 cp_parser_set_decl_spec_type (decl_specs, type,
10019 /*user_defined=*/true);
10022 /* If we didn't get a type-name, issue an error message. */
10023 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10025 cp_parser_error (parser, "expected type-name");
10026 return error_mark_node;
10029 /* There is no valid C++ program where a non-template type is
10030 followed by a "<". That usually indicates that the user thought
10031 that the type was a template. */
10032 if (type && type != error_mark_node)
10034 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10035 If it is, then the '<'...'>' enclose protocol names rather than
10036 template arguments, and so everything is fine. */
10037 if (c_dialect_objc ()
10038 && (objc_is_id (type) || objc_is_class_name (type)))
10040 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10041 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10043 /* Clobber the "unqualified" type previously entered into
10044 DECL_SPECS with the new, improved protocol-qualified version. */
10046 decl_specs->type = qual_type;
10051 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10057 /* Parse a type-name.
10070 Returns a TYPE_DECL for the type. */
10073 cp_parser_type_name (cp_parser* parser)
10078 /* We can't know yet whether it is a class-name or not. */
10079 cp_parser_parse_tentatively (parser);
10080 /* Try a class-name. */
10081 type_decl = cp_parser_class_name (parser,
10082 /*typename_keyword_p=*/false,
10083 /*template_keyword_p=*/false,
10085 /*check_dependency_p=*/true,
10086 /*class_head_p=*/false,
10087 /*is_declaration=*/false);
10088 /* If it's not a class-name, keep looking. */
10089 if (!cp_parser_parse_definitely (parser))
10091 /* It must be a typedef-name or an enum-name. */
10092 identifier = cp_parser_identifier (parser);
10093 if (identifier == error_mark_node)
10094 return error_mark_node;
10096 /* Look up the type-name. */
10097 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10099 if (TREE_CODE (type_decl) != TYPE_DECL
10100 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10102 /* See if this is an Objective-C type. */
10103 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10104 tree type = objc_get_protocol_qualified_type (identifier, protos);
10106 type_decl = TYPE_NAME (type);
10109 /* Issue an error if we did not find a type-name. */
10110 if (TREE_CODE (type_decl) != TYPE_DECL)
10112 if (!cp_parser_simulate_error (parser))
10113 cp_parser_name_lookup_error (parser, identifier, type_decl,
10115 type_decl = error_mark_node;
10117 /* Remember that the name was used in the definition of the
10118 current class so that we can check later to see if the
10119 meaning would have been different after the class was
10120 entirely defined. */
10121 else if (type_decl != error_mark_node
10123 maybe_note_name_used_in_class (identifier, type_decl);
10130 /* Parse an elaborated-type-specifier. Note that the grammar given
10131 here incorporates the resolution to DR68.
10133 elaborated-type-specifier:
10134 class-key :: [opt] nested-name-specifier [opt] identifier
10135 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10136 enum :: [opt] nested-name-specifier [opt] identifier
10137 typename :: [opt] nested-name-specifier identifier
10138 typename :: [opt] nested-name-specifier template [opt]
10143 elaborated-type-specifier:
10144 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10145 class-key attributes :: [opt] nested-name-specifier [opt]
10146 template [opt] template-id
10147 enum attributes :: [opt] nested-name-specifier [opt] identifier
10149 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10150 declared `friend'. If IS_DECLARATION is TRUE, then this
10151 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10152 something is being declared.
10154 Returns the TYPE specified. */
10157 cp_parser_elaborated_type_specifier (cp_parser* parser,
10159 bool is_declaration)
10161 enum tag_types tag_type;
10163 tree type = NULL_TREE;
10164 tree attributes = NULL_TREE;
10166 /* See if we're looking at the `enum' keyword. */
10167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10169 /* Consume the `enum' token. */
10170 cp_lexer_consume_token (parser->lexer);
10171 /* Remember that it's an enumeration type. */
10172 tag_type = enum_type;
10173 /* Parse the attributes. */
10174 attributes = cp_parser_attributes_opt (parser);
10176 /* Or, it might be `typename'. */
10177 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10180 /* Consume the `typename' token. */
10181 cp_lexer_consume_token (parser->lexer);
10182 /* Remember that it's a `typename' type. */
10183 tag_type = typename_type;
10184 /* The `typename' keyword is only allowed in templates. */
10185 if (!processing_template_decl)
10186 pedwarn ("using %<typename%> outside of template");
10188 /* Otherwise it must be a class-key. */
10191 tag_type = cp_parser_class_key (parser);
10192 if (tag_type == none_type)
10193 return error_mark_node;
10194 /* Parse the attributes. */
10195 attributes = cp_parser_attributes_opt (parser);
10198 /* Look for the `::' operator. */
10199 cp_parser_global_scope_opt (parser,
10200 /*current_scope_valid_p=*/false);
10201 /* Look for the nested-name-specifier. */
10202 if (tag_type == typename_type)
10204 if (!cp_parser_nested_name_specifier (parser,
10205 /*typename_keyword_p=*/true,
10206 /*check_dependency_p=*/true,
10209 return error_mark_node;
10212 /* Even though `typename' is not present, the proposed resolution
10213 to Core Issue 180 says that in `class A<T>::B', `B' should be
10214 considered a type-name, even if `A<T>' is dependent. */
10215 cp_parser_nested_name_specifier_opt (parser,
10216 /*typename_keyword_p=*/true,
10217 /*check_dependency_p=*/true,
10220 /* For everything but enumeration types, consider a template-id. */
10221 /* For an enumeration type, consider only a plain identifier. */
10222 if (tag_type != enum_type)
10224 bool template_p = false;
10227 /* Allow the `template' keyword. */
10228 template_p = cp_parser_optional_template_keyword (parser);
10229 /* If we didn't see `template', we don't know if there's a
10230 template-id or not. */
10232 cp_parser_parse_tentatively (parser);
10233 /* Parse the template-id. */
10234 decl = cp_parser_template_id (parser, template_p,
10235 /*check_dependency_p=*/true,
10237 /* If we didn't find a template-id, look for an ordinary
10239 if (!template_p && !cp_parser_parse_definitely (parser))
10241 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10242 in effect, then we must assume that, upon instantiation, the
10243 template will correspond to a class. */
10244 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10245 && tag_type == typename_type)
10246 type = make_typename_type (parser->scope, decl,
10248 /*complain=*/tf_error);
10250 type = TREE_TYPE (decl);
10255 identifier = cp_parser_identifier (parser);
10257 if (identifier == error_mark_node)
10259 parser->scope = NULL_TREE;
10260 return error_mark_node;
10263 /* For a `typename', we needn't call xref_tag. */
10264 if (tag_type == typename_type
10265 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10266 return cp_parser_make_typename_type (parser, parser->scope,
10268 /* Look up a qualified name in the usual way. */
10273 decl = cp_parser_lookup_name (parser, identifier,
10275 /*is_template=*/false,
10276 /*is_namespace=*/false,
10277 /*check_dependency=*/true,
10278 /*ambiguous_decls=*/NULL);
10280 /* If we are parsing friend declaration, DECL may be a
10281 TEMPLATE_DECL tree node here. However, we need to check
10282 whether this TEMPLATE_DECL results in valid code. Consider
10283 the following example:
10286 template <class T> class C {};
10289 template <class T> friend class N::C; // #1, valid code
10291 template <class T> class Y {
10292 friend class N::C; // #2, invalid code
10295 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10296 name lookup of `N::C'. We see that friend declaration must
10297 be template for the code to be valid. Note that
10298 processing_template_decl does not work here since it is
10299 always 1 for the above two cases. */
10301 decl = (cp_parser_maybe_treat_template_as_class
10302 (decl, /*tag_name_p=*/is_friend
10303 && parser->num_template_parameter_lists));
10305 if (TREE_CODE (decl) != TYPE_DECL)
10307 cp_parser_diagnose_invalid_type_name (parser,
10310 return error_mark_node;
10313 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10315 bool allow_template = (parser->num_template_parameter_lists
10316 || DECL_SELF_REFERENCE_P (decl));
10317 type = check_elaborated_type_specifier (tag_type, decl,
10320 if (type == error_mark_node)
10321 return error_mark_node;
10324 type = TREE_TYPE (decl);
10328 /* An elaborated-type-specifier sometimes introduces a new type and
10329 sometimes names an existing type. Normally, the rule is that it
10330 introduces a new type only if there is not an existing type of
10331 the same name already in scope. For example, given:
10334 void f() { struct S s; }
10336 the `struct S' in the body of `f' is the same `struct S' as in
10337 the global scope; the existing definition is used. However, if
10338 there were no global declaration, this would introduce a new
10339 local class named `S'.
10341 An exception to this rule applies to the following code:
10343 namespace N { struct S; }
10345 Here, the elaborated-type-specifier names a new type
10346 unconditionally; even if there is already an `S' in the
10347 containing scope this declaration names a new type.
10348 This exception only applies if the elaborated-type-specifier
10349 forms the complete declaration:
10353 A declaration consisting solely of `class-key identifier ;' is
10354 either a redeclaration of the name in the current scope or a
10355 forward declaration of the identifier as a class name. It
10356 introduces the name into the current scope.
10358 We are in this situation precisely when the next token is a `;'.
10360 An exception to the exception is that a `friend' declaration does
10361 *not* name a new type; i.e., given:
10363 struct S { friend struct T; };
10365 `T' is not a new type in the scope of `S'.
10367 Also, `new struct S' or `sizeof (struct S)' never results in the
10368 definition of a new type; a new type can only be declared in a
10369 declaration context. */
10375 /* Friends have special name lookup rules. */
10376 ts = ts_within_enclosing_non_class;
10377 else if (is_declaration
10378 && cp_lexer_next_token_is (parser->lexer,
10380 /* This is a `class-key identifier ;' */
10386 (parser->num_template_parameter_lists
10387 && (cp_parser_next_token_starts_class_definition_p (parser)
10388 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10389 /* An unqualified name was used to reference this type, so
10390 there were no qualifying templates. */
10391 if (!cp_parser_check_template_parameters (parser,
10392 /*num_templates=*/0))
10393 return error_mark_node;
10394 type = xref_tag (tag_type, identifier, ts, template_p);
10398 if (type == error_mark_node)
10399 return error_mark_node;
10401 /* Allow attributes on forward declarations of classes. */
10404 if (TREE_CODE (type) == TYPENAME_TYPE)
10405 warning (OPT_Wattributes,
10406 "attributes ignored on uninstantiated type");
10407 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10408 && ! processing_explicit_instantiation)
10409 warning (OPT_Wattributes,
10410 "attributes ignored on template instantiation");
10411 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10412 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10414 warning (OPT_Wattributes,
10415 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10418 if (tag_type != enum_type)
10419 cp_parser_check_class_key (tag_type, type);
10421 /* A "<" cannot follow an elaborated type specifier. If that
10422 happens, the user was probably trying to form a template-id. */
10423 cp_parser_check_for_invalid_template_id (parser, type);
10428 /* Parse an enum-specifier.
10431 enum identifier [opt] { enumerator-list [opt] }
10434 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10437 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10438 if the token stream isn't an enum-specifier after all. */
10441 cp_parser_enum_specifier (cp_parser* parser)
10447 /* Parse tentatively so that we can back up if we don't find a
10449 cp_parser_parse_tentatively (parser);
10451 /* Caller guarantees that the current token is 'enum', an identifier
10452 possibly follows, and the token after that is an opening brace.
10453 If we don't have an identifier, fabricate an anonymous name for
10454 the enumeration being defined. */
10455 cp_lexer_consume_token (parser->lexer);
10457 attributes = cp_parser_attributes_opt (parser);
10459 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10460 identifier = cp_parser_identifier (parser);
10462 identifier = make_anon_name ();
10464 /* Look for the `{' but don't consume it yet. */
10465 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10466 cp_parser_simulate_error (parser);
10468 if (!cp_parser_parse_definitely (parser))
10471 /* Issue an error message if type-definitions are forbidden here. */
10472 if (!cp_parser_check_type_definition (parser))
10473 type = error_mark_node;
10475 /* Create the new type. We do this before consuming the opening
10476 brace so the enum will be recorded as being on the line of its
10477 tag (or the 'enum' keyword, if there is no tag). */
10478 type = start_enum (identifier);
10480 /* Consume the opening brace. */
10481 cp_lexer_consume_token (parser->lexer);
10483 if (type == error_mark_node)
10485 cp_parser_skip_to_end_of_block_or_statement (parser);
10486 return error_mark_node;
10489 /* If the next token is not '}', then there are some enumerators. */
10490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10491 cp_parser_enumerator_list (parser, type);
10493 /* Consume the final '}'. */
10494 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10496 /* Look for trailing attributes to apply to this enumeration, and
10497 apply them if appropriate. */
10498 if (cp_parser_allow_gnu_extensions_p (parser))
10500 tree trailing_attr = cp_parser_attributes_opt (parser);
10501 cplus_decl_attributes (&type,
10503 (int) ATTR_FLAG_TYPE_IN_PLACE);
10506 /* Finish up the enumeration. */
10507 finish_enum (type);
10512 /* Parse an enumerator-list. The enumerators all have the indicated
10516 enumerator-definition
10517 enumerator-list , enumerator-definition */
10520 cp_parser_enumerator_list (cp_parser* parser, tree type)
10524 /* Parse an enumerator-definition. */
10525 cp_parser_enumerator_definition (parser, type);
10527 /* If the next token is not a ',', we've reached the end of
10529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10531 /* Otherwise, consume the `,' and keep going. */
10532 cp_lexer_consume_token (parser->lexer);
10533 /* If the next token is a `}', there is a trailing comma. */
10534 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10536 if (pedantic && !in_system_header)
10537 pedwarn ("comma at end of enumerator list");
10543 /* Parse an enumerator-definition. The enumerator has the indicated
10546 enumerator-definition:
10548 enumerator = constant-expression
10554 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10559 /* Look for the identifier. */
10560 identifier = cp_parser_identifier (parser);
10561 if (identifier == error_mark_node)
10564 /* If the next token is an '=', then there is an explicit value. */
10565 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10567 /* Consume the `=' token. */
10568 cp_lexer_consume_token (parser->lexer);
10569 /* Parse the value. */
10570 value = cp_parser_constant_expression (parser,
10571 /*allow_non_constant_p=*/false,
10577 /* Create the enumerator. */
10578 build_enumerator (identifier, value, type);
10581 /* Parse a namespace-name.
10584 original-namespace-name
10587 Returns the NAMESPACE_DECL for the namespace. */
10590 cp_parser_namespace_name (cp_parser* parser)
10593 tree namespace_decl;
10595 /* Get the name of the namespace. */
10596 identifier = cp_parser_identifier (parser);
10597 if (identifier == error_mark_node)
10598 return error_mark_node;
10600 /* Look up the identifier in the currently active scope. Look only
10601 for namespaces, due to:
10603 [basic.lookup.udir]
10605 When looking up a namespace-name in a using-directive or alias
10606 definition, only namespace names are considered.
10610 [basic.lookup.qual]
10612 During the lookup of a name preceding the :: scope resolution
10613 operator, object, function, and enumerator names are ignored.
10615 (Note that cp_parser_class_or_namespace_name only calls this
10616 function if the token after the name is the scope resolution
10618 namespace_decl = cp_parser_lookup_name (parser, identifier,
10620 /*is_template=*/false,
10621 /*is_namespace=*/true,
10622 /*check_dependency=*/true,
10623 /*ambiguous_decls=*/NULL);
10624 /* If it's not a namespace, issue an error. */
10625 if (namespace_decl == error_mark_node
10626 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10628 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10629 error ("%qD is not a namespace-name", identifier);
10630 cp_parser_error (parser, "expected namespace-name");
10631 namespace_decl = error_mark_node;
10634 return namespace_decl;
10637 /* Parse a namespace-definition.
10639 namespace-definition:
10640 named-namespace-definition
10641 unnamed-namespace-definition
10643 named-namespace-definition:
10644 original-namespace-definition
10645 extension-namespace-definition
10647 original-namespace-definition:
10648 namespace identifier { namespace-body }
10650 extension-namespace-definition:
10651 namespace original-namespace-name { namespace-body }
10653 unnamed-namespace-definition:
10654 namespace { namespace-body } */
10657 cp_parser_namespace_definition (cp_parser* parser)
10659 tree identifier, attribs;
10661 /* Look for the `namespace' keyword. */
10662 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10664 /* Get the name of the namespace. We do not attempt to distinguish
10665 between an original-namespace-definition and an
10666 extension-namespace-definition at this point. The semantic
10667 analysis routines are responsible for that. */
10668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10669 identifier = cp_parser_identifier (parser);
10671 identifier = NULL_TREE;
10673 /* Parse any specified attributes. */
10674 attribs = cp_parser_attributes_opt (parser);
10676 /* Look for the `{' to start the namespace. */
10677 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10678 /* Start the namespace. */
10679 push_namespace_with_attribs (identifier, attribs);
10680 /* Parse the body of the namespace. */
10681 cp_parser_namespace_body (parser);
10682 /* Finish the namespace. */
10684 /* Look for the final `}'. */
10685 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10688 /* Parse a namespace-body.
10691 declaration-seq [opt] */
10694 cp_parser_namespace_body (cp_parser* parser)
10696 cp_parser_declaration_seq_opt (parser);
10699 /* Parse a namespace-alias-definition.
10701 namespace-alias-definition:
10702 namespace identifier = qualified-namespace-specifier ; */
10705 cp_parser_namespace_alias_definition (cp_parser* parser)
10708 tree namespace_specifier;
10710 /* Look for the `namespace' keyword. */
10711 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10712 /* Look for the identifier. */
10713 identifier = cp_parser_identifier (parser);
10714 if (identifier == error_mark_node)
10716 /* Look for the `=' token. */
10717 cp_parser_require (parser, CPP_EQ, "`='");
10718 /* Look for the qualified-namespace-specifier. */
10719 namespace_specifier
10720 = cp_parser_qualified_namespace_specifier (parser);
10721 /* Look for the `;' token. */
10722 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10724 /* Register the alias in the symbol table. */
10725 do_namespace_alias (identifier, namespace_specifier);
10728 /* Parse a qualified-namespace-specifier.
10730 qualified-namespace-specifier:
10731 :: [opt] nested-name-specifier [opt] namespace-name
10733 Returns a NAMESPACE_DECL corresponding to the specified
10737 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10739 /* Look for the optional `::'. */
10740 cp_parser_global_scope_opt (parser,
10741 /*current_scope_valid_p=*/false);
10743 /* Look for the optional nested-name-specifier. */
10744 cp_parser_nested_name_specifier_opt (parser,
10745 /*typename_keyword_p=*/false,
10746 /*check_dependency_p=*/true,
10748 /*is_declaration=*/true);
10750 return cp_parser_namespace_name (parser);
10753 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10754 access declaration.
10757 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10758 using :: unqualified-id ;
10760 access-declaration:
10766 cp_parser_using_declaration (cp_parser* parser,
10767 bool access_declaration_p)
10770 bool typename_p = false;
10771 bool global_scope_p;
10776 if (access_declaration_p)
10777 cp_parser_parse_tentatively (parser);
10780 /* Look for the `using' keyword. */
10781 cp_parser_require_keyword (parser, RID_USING, "`using'");
10783 /* Peek at the next token. */
10784 token = cp_lexer_peek_token (parser->lexer);
10785 /* See if it's `typename'. */
10786 if (token->keyword == RID_TYPENAME)
10788 /* Remember that we've seen it. */
10790 /* Consume the `typename' token. */
10791 cp_lexer_consume_token (parser->lexer);
10795 /* Look for the optional global scope qualification. */
10797 = (cp_parser_global_scope_opt (parser,
10798 /*current_scope_valid_p=*/false)
10801 /* If we saw `typename', or didn't see `::', then there must be a
10802 nested-name-specifier present. */
10803 if (typename_p || !global_scope_p)
10804 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10805 /*check_dependency_p=*/true,
10807 /*is_declaration=*/true);
10808 /* Otherwise, we could be in either of the two productions. In that
10809 case, treat the nested-name-specifier as optional. */
10811 qscope = cp_parser_nested_name_specifier_opt (parser,
10812 /*typename_keyword_p=*/false,
10813 /*check_dependency_p=*/true,
10815 /*is_declaration=*/true);
10817 qscope = global_namespace;
10819 if (access_declaration_p && cp_parser_error_occurred (parser))
10820 /* Something has already gone wrong; there's no need to parse
10821 further. Since an error has occurred, the return value of
10822 cp_parser_parse_definitely will be false, as required. */
10823 return cp_parser_parse_definitely (parser);
10825 /* Parse the unqualified-id. */
10826 identifier = cp_parser_unqualified_id (parser,
10827 /*template_keyword_p=*/false,
10828 /*check_dependency_p=*/true,
10829 /*declarator_p=*/true,
10830 /*optional_p=*/false);
10832 if (access_declaration_p)
10834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10835 cp_parser_simulate_error (parser);
10836 if (!cp_parser_parse_definitely (parser))
10840 /* The function we call to handle a using-declaration is different
10841 depending on what scope we are in. */
10842 if (qscope == error_mark_node || identifier == error_mark_node)
10844 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10845 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10846 /* [namespace.udecl]
10848 A using declaration shall not name a template-id. */
10849 error ("a template-id may not appear in a using-declaration");
10852 if (at_class_scope_p ())
10854 /* Create the USING_DECL. */
10855 decl = do_class_using_decl (parser->scope, identifier);
10856 /* Add it to the list of members in this class. */
10857 finish_member_declaration (decl);
10861 decl = cp_parser_lookup_name_simple (parser, identifier);
10862 if (decl == error_mark_node)
10863 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10864 else if (!at_namespace_scope_p ())
10865 do_local_using_decl (decl, qscope, identifier);
10867 do_toplevel_using_decl (decl, qscope, identifier);
10871 /* Look for the final `;'. */
10872 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10877 /* Parse a using-directive.
10880 using namespace :: [opt] nested-name-specifier [opt]
10881 namespace-name ; */
10884 cp_parser_using_directive (cp_parser* parser)
10886 tree namespace_decl;
10889 /* Look for the `using' keyword. */
10890 cp_parser_require_keyword (parser, RID_USING, "`using'");
10891 /* And the `namespace' keyword. */
10892 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10893 /* Look for the optional `::' operator. */
10894 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10895 /* And the optional nested-name-specifier. */
10896 cp_parser_nested_name_specifier_opt (parser,
10897 /*typename_keyword_p=*/false,
10898 /*check_dependency_p=*/true,
10900 /*is_declaration=*/true);
10901 /* Get the namespace being used. */
10902 namespace_decl = cp_parser_namespace_name (parser);
10903 /* And any specified attributes. */
10904 attribs = cp_parser_attributes_opt (parser);
10905 /* Update the symbol table. */
10906 parse_using_directive (namespace_decl, attribs);
10907 /* Look for the final `;'. */
10908 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10911 /* Parse an asm-definition.
10914 asm ( string-literal ) ;
10919 asm volatile [opt] ( string-literal ) ;
10920 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10921 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10922 : asm-operand-list [opt] ) ;
10923 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10924 : asm-operand-list [opt]
10925 : asm-operand-list [opt] ) ; */
10928 cp_parser_asm_definition (cp_parser* parser)
10931 tree outputs = NULL_TREE;
10932 tree inputs = NULL_TREE;
10933 tree clobbers = NULL_TREE;
10935 bool volatile_p = false;
10936 bool extended_p = false;
10938 /* Look for the `asm' keyword. */
10939 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10940 /* See if the next token is `volatile'. */
10941 if (cp_parser_allow_gnu_extensions_p (parser)
10942 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10944 /* Remember that we saw the `volatile' keyword. */
10946 /* Consume the token. */
10947 cp_lexer_consume_token (parser->lexer);
10949 /* Look for the opening `('. */
10950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10952 /* Look for the string. */
10953 string = cp_parser_string_literal (parser, false, false);
10954 if (string == error_mark_node)
10956 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10957 /*consume_paren=*/true);
10961 /* If we're allowing GNU extensions, check for the extended assembly
10962 syntax. Unfortunately, the `:' tokens need not be separated by
10963 a space in C, and so, for compatibility, we tolerate that here
10964 too. Doing that means that we have to treat the `::' operator as
10966 if (cp_parser_allow_gnu_extensions_p (parser)
10967 && parser->in_function_body
10968 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10969 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10971 bool inputs_p = false;
10972 bool clobbers_p = false;
10974 /* The extended syntax was used. */
10977 /* Look for outputs. */
10978 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10980 /* Consume the `:'. */
10981 cp_lexer_consume_token (parser->lexer);
10982 /* Parse the output-operands. */
10983 if (cp_lexer_next_token_is_not (parser->lexer,
10985 && cp_lexer_next_token_is_not (parser->lexer,
10987 && cp_lexer_next_token_is_not (parser->lexer,
10989 outputs = cp_parser_asm_operand_list (parser);
10991 /* If the next token is `::', there are no outputs, and the
10992 next token is the beginning of the inputs. */
10993 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10994 /* The inputs are coming next. */
10997 /* Look for inputs. */
10999 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11001 /* Consume the `:' or `::'. */
11002 cp_lexer_consume_token (parser->lexer);
11003 /* Parse the output-operands. */
11004 if (cp_lexer_next_token_is_not (parser->lexer,
11006 && cp_lexer_next_token_is_not (parser->lexer,
11008 inputs = cp_parser_asm_operand_list (parser);
11010 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11011 /* The clobbers are coming next. */
11014 /* Look for clobbers. */
11016 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11018 /* Consume the `:' or `::'. */
11019 cp_lexer_consume_token (parser->lexer);
11020 /* Parse the clobbers. */
11021 if (cp_lexer_next_token_is_not (parser->lexer,
11023 clobbers = cp_parser_asm_clobber_list (parser);
11026 /* Look for the closing `)'. */
11027 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11028 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11029 /*consume_paren=*/true);
11030 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11032 /* Create the ASM_EXPR. */
11033 if (parser->in_function_body)
11035 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11037 /* If the extended syntax was not used, mark the ASM_EXPR. */
11040 tree temp = asm_stmt;
11041 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11042 temp = TREE_OPERAND (temp, 0);
11044 ASM_INPUT_P (temp) = 1;
11048 cgraph_add_asm_node (string);
11051 /* Declarators [gram.dcl.decl] */
11053 /* Parse an init-declarator.
11056 declarator initializer [opt]
11061 declarator asm-specification [opt] attributes [opt] initializer [opt]
11063 function-definition:
11064 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11066 decl-specifier-seq [opt] declarator function-try-block
11070 function-definition:
11071 __extension__ function-definition
11073 The DECL_SPECIFIERS apply to this declarator. Returns a
11074 representation of the entity declared. If MEMBER_P is TRUE, then
11075 this declarator appears in a class scope. The new DECL created by
11076 this declarator is returned.
11078 The CHECKS are access checks that should be performed once we know
11079 what entity is being declared (and, therefore, what classes have
11082 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11083 for a function-definition here as well. If the declarator is a
11084 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11085 be TRUE upon return. By that point, the function-definition will
11086 have been completely parsed.
11088 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11092 cp_parser_init_declarator (cp_parser* parser,
11093 cp_decl_specifier_seq *decl_specifiers,
11095 bool function_definition_allowed_p,
11097 int declares_class_or_enum,
11098 bool* function_definition_p)
11101 cp_declarator *declarator;
11102 tree prefix_attributes;
11104 tree asm_specification;
11106 tree decl = NULL_TREE;
11108 bool is_initialized;
11109 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11110 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11112 enum cpp_ttype initialization_kind;
11113 bool is_parenthesized_init = false;
11114 bool is_non_constant_init;
11115 int ctor_dtor_or_conv_p;
11117 tree pushed_scope = NULL;
11119 /* Gather the attributes that were provided with the
11120 decl-specifiers. */
11121 prefix_attributes = decl_specifiers->attributes;
11123 /* Assume that this is not the declarator for a function
11125 if (function_definition_p)
11126 *function_definition_p = false;
11128 /* Defer access checks while parsing the declarator; we cannot know
11129 what names are accessible until we know what is being
11131 resume_deferring_access_checks ();
11133 /* Parse the declarator. */
11135 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11136 &ctor_dtor_or_conv_p,
11137 /*parenthesized_p=*/NULL,
11138 /*member_p=*/false);
11139 /* Gather up the deferred checks. */
11140 stop_deferring_access_checks ();
11142 /* If the DECLARATOR was erroneous, there's no need to go
11144 if (declarator == cp_error_declarator)
11145 return error_mark_node;
11147 /* Check that the number of template-parameter-lists is OK. */
11148 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11149 return error_mark_node;
11151 if (declares_class_or_enum & 2)
11152 cp_parser_check_for_definition_in_return_type (declarator,
11153 decl_specifiers->type);
11155 /* Figure out what scope the entity declared by the DECLARATOR is
11156 located in. `grokdeclarator' sometimes changes the scope, so
11157 we compute it now. */
11158 scope = get_scope_of_declarator (declarator);
11160 /* If we're allowing GNU extensions, look for an asm-specification
11162 if (cp_parser_allow_gnu_extensions_p (parser))
11164 /* Look for an asm-specification. */
11165 asm_specification = cp_parser_asm_specification_opt (parser);
11166 /* And attributes. */
11167 attributes = cp_parser_attributes_opt (parser);
11171 asm_specification = NULL_TREE;
11172 attributes = NULL_TREE;
11175 /* Peek at the next token. */
11176 token = cp_lexer_peek_token (parser->lexer);
11177 /* Check to see if the token indicates the start of a
11178 function-definition. */
11179 if (cp_parser_token_starts_function_definition_p (token))
11181 if (!function_definition_allowed_p)
11183 /* If a function-definition should not appear here, issue an
11185 cp_parser_error (parser,
11186 "a function-definition is not allowed here");
11187 return error_mark_node;
11191 /* Neither attributes nor an asm-specification are allowed
11192 on a function-definition. */
11193 if (asm_specification)
11194 error ("an asm-specification is not allowed on a function-definition");
11196 error ("attributes are not allowed on a function-definition");
11197 /* This is a function-definition. */
11198 *function_definition_p = true;
11200 /* Parse the function definition. */
11202 decl = cp_parser_save_member_function_body (parser,
11205 prefix_attributes);
11208 = (cp_parser_function_definition_from_specifiers_and_declarator
11209 (parser, decl_specifiers, prefix_attributes, declarator));
11217 Only in function declarations for constructors, destructors, and
11218 type conversions can the decl-specifier-seq be omitted.
11220 We explicitly postpone this check past the point where we handle
11221 function-definitions because we tolerate function-definitions
11222 that are missing their return types in some modes. */
11223 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11225 cp_parser_error (parser,
11226 "expected constructor, destructor, or type conversion");
11227 return error_mark_node;
11230 /* An `=' or an `(' indicates an initializer. */
11231 if (token->type == CPP_EQ
11232 || token->type == CPP_OPEN_PAREN)
11234 is_initialized = true;
11235 initialization_kind = token->type;
11239 /* If the init-declarator isn't initialized and isn't followed by a
11240 `,' or `;', it's not a valid init-declarator. */
11241 if (token->type != CPP_COMMA
11242 && token->type != CPP_SEMICOLON)
11244 cp_parser_error (parser, "expected initializer");
11245 return error_mark_node;
11247 is_initialized = false;
11248 initialization_kind = CPP_EOF;
11251 /* Because start_decl has side-effects, we should only call it if we
11252 know we're going ahead. By this point, we know that we cannot
11253 possibly be looking at any other construct. */
11254 cp_parser_commit_to_tentative_parse (parser);
11256 /* If the decl specifiers were bad, issue an error now that we're
11257 sure this was intended to be a declarator. Then continue
11258 declaring the variable(s), as int, to try to cut down on further
11260 if (decl_specifiers->any_specifiers_p
11261 && decl_specifiers->type == error_mark_node)
11263 cp_parser_error (parser, "invalid type in declaration");
11264 decl_specifiers->type = integer_type_node;
11267 /* Check to see whether or not this declaration is a friend. */
11268 friend_p = cp_parser_friend_p (decl_specifiers);
11270 /* Enter the newly declared entry in the symbol table. If we're
11271 processing a declaration in a class-specifier, we wait until
11272 after processing the initializer. */
11275 if (parser->in_unbraced_linkage_specification_p)
11276 decl_specifiers->storage_class = sc_extern;
11277 decl = start_decl (declarator, decl_specifiers,
11278 is_initialized, attributes, prefix_attributes,
11282 /* Enter the SCOPE. That way unqualified names appearing in the
11283 initializer will be looked up in SCOPE. */
11284 pushed_scope = push_scope (scope);
11286 /* Perform deferred access control checks, now that we know in which
11287 SCOPE the declared entity resides. */
11288 if (!member_p && decl)
11290 tree saved_current_function_decl = NULL_TREE;
11292 /* If the entity being declared is a function, pretend that we
11293 are in its scope. If it is a `friend', it may have access to
11294 things that would not otherwise be accessible. */
11295 if (TREE_CODE (decl) == FUNCTION_DECL)
11297 saved_current_function_decl = current_function_decl;
11298 current_function_decl = decl;
11301 /* Perform access checks for template parameters. */
11302 cp_parser_perform_template_parameter_access_checks (checks);
11304 /* Perform the access control checks for the declarator and the
11305 the decl-specifiers. */
11306 perform_deferred_access_checks ();
11308 /* Restore the saved value. */
11309 if (TREE_CODE (decl) == FUNCTION_DECL)
11310 current_function_decl = saved_current_function_decl;
11313 /* Parse the initializer. */
11314 initializer = NULL_TREE;
11315 is_parenthesized_init = false;
11316 is_non_constant_init = true;
11317 if (is_initialized)
11319 if (function_declarator_p (declarator))
11321 if (initialization_kind == CPP_EQ)
11322 initializer = cp_parser_pure_specifier (parser);
11325 /* If the declaration was erroneous, we don't really
11326 know what the user intended, so just silently
11327 consume the initializer. */
11328 if (decl != error_mark_node)
11329 error ("initializer provided for function");
11330 cp_parser_skip_to_closing_parenthesis (parser,
11331 /*recovering=*/true,
11332 /*or_comma=*/false,
11333 /*consume_paren=*/true);
11337 initializer = cp_parser_initializer (parser,
11338 &is_parenthesized_init,
11339 &is_non_constant_init);
11342 /* The old parser allows attributes to appear after a parenthesized
11343 initializer. Mark Mitchell proposed removing this functionality
11344 on the GCC mailing lists on 2002-08-13. This parser accepts the
11345 attributes -- but ignores them. */
11346 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11347 if (cp_parser_attributes_opt (parser))
11348 warning (OPT_Wattributes,
11349 "attributes after parenthesized initializer ignored");
11351 /* For an in-class declaration, use `grokfield' to create the
11357 pop_scope (pushed_scope);
11358 pushed_scope = false;
11360 decl = grokfield (declarator, decl_specifiers,
11361 initializer, !is_non_constant_init,
11362 /*asmspec=*/NULL_TREE,
11363 prefix_attributes);
11364 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11365 cp_parser_save_default_args (parser, decl);
11368 /* Finish processing the declaration. But, skip friend
11370 if (!friend_p && decl && decl != error_mark_node)
11372 cp_finish_decl (decl,
11373 initializer, !is_non_constant_init,
11375 /* If the initializer is in parentheses, then this is
11376 a direct-initialization, which means that an
11377 `explicit' constructor is OK. Otherwise, an
11378 `explicit' constructor cannot be used. */
11379 ((is_parenthesized_init || !is_initialized)
11380 ? 0 : LOOKUP_ONLYCONVERTING));
11382 if (!friend_p && pushed_scope)
11383 pop_scope (pushed_scope);
11388 /* Parse a declarator.
11392 ptr-operator declarator
11394 abstract-declarator:
11395 ptr-operator abstract-declarator [opt]
11396 direct-abstract-declarator
11401 attributes [opt] direct-declarator
11402 attributes [opt] ptr-operator declarator
11404 abstract-declarator:
11405 attributes [opt] ptr-operator abstract-declarator [opt]
11406 attributes [opt] direct-abstract-declarator
11408 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11409 detect constructor, destructor or conversion operators. It is set
11410 to -1 if the declarator is a name, and +1 if it is a
11411 function. Otherwise it is set to zero. Usually you just want to
11412 test for >0, but internally the negative value is used.
11414 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11415 a decl-specifier-seq unless it declares a constructor, destructor,
11416 or conversion. It might seem that we could check this condition in
11417 semantic analysis, rather than parsing, but that makes it difficult
11418 to handle something like `f()'. We want to notice that there are
11419 no decl-specifiers, and therefore realize that this is an
11420 expression, not a declaration.)
11422 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11423 the declarator is a direct-declarator of the form "(...)".
11425 MEMBER_P is true iff this declarator is a member-declarator. */
11427 static cp_declarator *
11428 cp_parser_declarator (cp_parser* parser,
11429 cp_parser_declarator_kind dcl_kind,
11430 int* ctor_dtor_or_conv_p,
11431 bool* parenthesized_p,
11435 cp_declarator *declarator;
11436 enum tree_code code;
11437 cp_cv_quals cv_quals;
11439 tree attributes = NULL_TREE;
11441 /* Assume this is not a constructor, destructor, or type-conversion
11443 if (ctor_dtor_or_conv_p)
11444 *ctor_dtor_or_conv_p = 0;
11446 if (cp_parser_allow_gnu_extensions_p (parser))
11447 attributes = cp_parser_attributes_opt (parser);
11449 /* Peek at the next token. */
11450 token = cp_lexer_peek_token (parser->lexer);
11452 /* Check for the ptr-operator production. */
11453 cp_parser_parse_tentatively (parser);
11454 /* Parse the ptr-operator. */
11455 code = cp_parser_ptr_operator (parser,
11458 /* If that worked, then we have a ptr-operator. */
11459 if (cp_parser_parse_definitely (parser))
11461 /* If a ptr-operator was found, then this declarator was not
11463 if (parenthesized_p)
11464 *parenthesized_p = true;
11465 /* The dependent declarator is optional if we are parsing an
11466 abstract-declarator. */
11467 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11468 cp_parser_parse_tentatively (parser);
11470 /* Parse the dependent declarator. */
11471 declarator = cp_parser_declarator (parser, dcl_kind,
11472 /*ctor_dtor_or_conv_p=*/NULL,
11473 /*parenthesized_p=*/NULL,
11474 /*member_p=*/false);
11476 /* If we are parsing an abstract-declarator, we must handle the
11477 case where the dependent declarator is absent. */
11478 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11479 && !cp_parser_parse_definitely (parser))
11482 /* Build the representation of the ptr-operator. */
11484 declarator = make_ptrmem_declarator (cv_quals,
11487 else if (code == INDIRECT_REF)
11488 declarator = make_pointer_declarator (cv_quals, declarator);
11490 declarator = make_reference_declarator (cv_quals, declarator);
11492 /* Everything else is a direct-declarator. */
11495 if (parenthesized_p)
11496 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11498 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11499 ctor_dtor_or_conv_p,
11503 if (attributes && declarator && declarator != cp_error_declarator)
11504 declarator->attributes = attributes;
11509 /* Parse a direct-declarator or direct-abstract-declarator.
11513 direct-declarator ( parameter-declaration-clause )
11514 cv-qualifier-seq [opt]
11515 exception-specification [opt]
11516 direct-declarator [ constant-expression [opt] ]
11519 direct-abstract-declarator:
11520 direct-abstract-declarator [opt]
11521 ( parameter-declaration-clause )
11522 cv-qualifier-seq [opt]
11523 exception-specification [opt]
11524 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11525 ( abstract-declarator )
11527 Returns a representation of the declarator. DCL_KIND is
11528 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11529 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11530 we are parsing a direct-declarator. It is
11531 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11532 of ambiguity we prefer an abstract declarator, as per
11533 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11534 cp_parser_declarator. */
11536 static cp_declarator *
11537 cp_parser_direct_declarator (cp_parser* parser,
11538 cp_parser_declarator_kind dcl_kind,
11539 int* ctor_dtor_or_conv_p,
11543 cp_declarator *declarator = NULL;
11544 tree scope = NULL_TREE;
11545 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11546 bool saved_in_declarator_p = parser->in_declarator_p;
11548 tree pushed_scope = NULL_TREE;
11552 /* Peek at the next token. */
11553 token = cp_lexer_peek_token (parser->lexer);
11554 if (token->type == CPP_OPEN_PAREN)
11556 /* This is either a parameter-declaration-clause, or a
11557 parenthesized declarator. When we know we are parsing a
11558 named declarator, it must be a parenthesized declarator
11559 if FIRST is true. For instance, `(int)' is a
11560 parameter-declaration-clause, with an omitted
11561 direct-abstract-declarator. But `((*))', is a
11562 parenthesized abstract declarator. Finally, when T is a
11563 template parameter `(T)' is a
11564 parameter-declaration-clause, and not a parenthesized
11567 We first try and parse a parameter-declaration-clause,
11568 and then try a nested declarator (if FIRST is true).
11570 It is not an error for it not to be a
11571 parameter-declaration-clause, even when FIRST is
11577 The first is the declaration of a function while the
11578 second is a the definition of a variable, including its
11581 Having seen only the parenthesis, we cannot know which of
11582 these two alternatives should be selected. Even more
11583 complex are examples like:
11588 The former is a function-declaration; the latter is a
11589 variable initialization.
11591 Thus again, we try a parameter-declaration-clause, and if
11592 that fails, we back out and return. */
11594 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11596 cp_parameter_declarator *params;
11597 unsigned saved_num_template_parameter_lists;
11599 /* In a member-declarator, the only valid interpretation
11600 of a parenthesis is the start of a
11601 parameter-declaration-clause. (It is invalid to
11602 initialize a static data member with a parenthesized
11603 initializer; only the "=" form of initialization is
11606 cp_parser_parse_tentatively (parser);
11608 /* Consume the `('. */
11609 cp_lexer_consume_token (parser->lexer);
11612 /* If this is going to be an abstract declarator, we're
11613 in a declarator and we can't have default args. */
11614 parser->default_arg_ok_p = false;
11615 parser->in_declarator_p = true;
11618 /* Inside the function parameter list, surrounding
11619 template-parameter-lists do not apply. */
11620 saved_num_template_parameter_lists
11621 = parser->num_template_parameter_lists;
11622 parser->num_template_parameter_lists = 0;
11624 /* Parse the parameter-declaration-clause. */
11625 params = cp_parser_parameter_declaration_clause (parser);
11627 parser->num_template_parameter_lists
11628 = saved_num_template_parameter_lists;
11630 /* If all went well, parse the cv-qualifier-seq and the
11631 exception-specification. */
11632 if (member_p || cp_parser_parse_definitely (parser))
11634 cp_cv_quals cv_quals;
11635 tree exception_specification;
11637 if (ctor_dtor_or_conv_p)
11638 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11640 /* Consume the `)'. */
11641 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11643 /* Parse the cv-qualifier-seq. */
11644 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11645 /* And the exception-specification. */
11646 exception_specification
11647 = cp_parser_exception_specification_opt (parser);
11649 /* Create the function-declarator. */
11650 declarator = make_call_declarator (declarator,
11653 exception_specification);
11654 /* Any subsequent parameter lists are to do with
11655 return type, so are not those of the declared
11657 parser->default_arg_ok_p = false;
11659 /* Repeat the main loop. */
11664 /* If this is the first, we can try a parenthesized
11668 bool saved_in_type_id_in_expr_p;
11670 parser->default_arg_ok_p = saved_default_arg_ok_p;
11671 parser->in_declarator_p = saved_in_declarator_p;
11673 /* Consume the `('. */
11674 cp_lexer_consume_token (parser->lexer);
11675 /* Parse the nested declarator. */
11676 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11677 parser->in_type_id_in_expr_p = true;
11679 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11680 /*parenthesized_p=*/NULL,
11682 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11684 /* Expect a `)'. */
11685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11686 declarator = cp_error_declarator;
11687 if (declarator == cp_error_declarator)
11690 goto handle_declarator;
11692 /* Otherwise, we must be done. */
11696 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11697 && token->type == CPP_OPEN_SQUARE)
11699 /* Parse an array-declarator. */
11702 if (ctor_dtor_or_conv_p)
11703 *ctor_dtor_or_conv_p = 0;
11706 parser->default_arg_ok_p = false;
11707 parser->in_declarator_p = true;
11708 /* Consume the `['. */
11709 cp_lexer_consume_token (parser->lexer);
11710 /* Peek at the next token. */
11711 token = cp_lexer_peek_token (parser->lexer);
11712 /* If the next token is `]', then there is no
11713 constant-expression. */
11714 if (token->type != CPP_CLOSE_SQUARE)
11716 bool non_constant_p;
11719 = cp_parser_constant_expression (parser,
11720 /*allow_non_constant=*/true,
11722 if (!non_constant_p)
11723 bounds = fold_non_dependent_expr (bounds);
11724 /* Normally, the array bound must be an integral constant
11725 expression. However, as an extension, we allow VLAs
11726 in function scopes. */
11727 else if (!parser->in_function_body)
11729 error ("array bound is not an integer constant");
11730 bounds = error_mark_node;
11734 bounds = NULL_TREE;
11735 /* Look for the closing `]'. */
11736 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11738 declarator = cp_error_declarator;
11742 declarator = make_array_declarator (declarator, bounds);
11744 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11746 tree qualifying_scope;
11747 tree unqualified_name;
11748 special_function_kind sfk;
11751 /* Parse a declarator-id */
11752 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11754 cp_parser_parse_tentatively (parser);
11756 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11757 qualifying_scope = parser->scope;
11760 if (!cp_parser_parse_definitely (parser))
11761 unqualified_name = error_mark_node;
11762 else if (unqualified_name
11763 && (qualifying_scope
11764 || (TREE_CODE (unqualified_name)
11765 != IDENTIFIER_NODE)))
11767 cp_parser_error (parser, "expected unqualified-id");
11768 unqualified_name = error_mark_node;
11772 if (!unqualified_name)
11774 if (unqualified_name == error_mark_node)
11776 declarator = cp_error_declarator;
11780 if (qualifying_scope && at_namespace_scope_p ()
11781 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11783 /* In the declaration of a member of a template class
11784 outside of the class itself, the SCOPE will sometimes
11785 be a TYPENAME_TYPE. For example, given:
11787 template <typename T>
11788 int S<T>::R::i = 3;
11790 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11791 this context, we must resolve S<T>::R to an ordinary
11792 type, rather than a typename type.
11794 The reason we normally avoid resolving TYPENAME_TYPEs
11795 is that a specialization of `S' might render
11796 `S<T>::R' not a type. However, if `S' is
11797 specialized, then this `i' will not be used, so there
11798 is no harm in resolving the types here. */
11801 /* Resolve the TYPENAME_TYPE. */
11802 type = resolve_typename_type (qualifying_scope,
11803 /*only_current_p=*/false);
11804 /* If that failed, the declarator is invalid. */
11805 if (type == error_mark_node)
11806 error ("%<%T::%D%> is not a type",
11807 TYPE_CONTEXT (qualifying_scope),
11808 TYPE_IDENTIFIER (qualifying_scope));
11809 qualifying_scope = type;
11813 if (unqualified_name)
11817 if (qualifying_scope
11818 && CLASS_TYPE_P (qualifying_scope))
11819 class_type = qualifying_scope;
11821 class_type = current_class_type;
11823 if (TREE_CODE (unqualified_name) == TYPE_DECL)
11825 tree name_type = TREE_TYPE (unqualified_name);
11826 if (class_type && same_type_p (name_type, class_type))
11828 if (qualifying_scope
11829 && CLASSTYPE_USE_TEMPLATE (name_type))
11831 error ("invalid use of constructor as a template");
11832 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11833 "name the constructor in a qualified name",
11835 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11836 class_type, name_type);
11837 declarator = cp_error_declarator;
11841 unqualified_name = constructor_name (class_type);
11845 /* We do not attempt to print the declarator
11846 here because we do not have enough
11847 information about its original syntactic
11849 cp_parser_error (parser, "invalid declarator");
11850 declarator = cp_error_declarator;
11857 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11858 sfk = sfk_destructor;
11859 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11860 sfk = sfk_conversion;
11861 else if (/* There's no way to declare a constructor
11862 for an anonymous type, even if the type
11863 got a name for linkage purposes. */
11864 !TYPE_WAS_ANONYMOUS (class_type)
11865 && constructor_name_p (unqualified_name,
11868 unqualified_name = constructor_name (class_type);
11869 sfk = sfk_constructor;
11872 if (ctor_dtor_or_conv_p && sfk != sfk_none)
11873 *ctor_dtor_or_conv_p = -1;
11876 declarator = make_id_declarator (qualifying_scope,
11879 declarator->id_loc = token->location;
11881 handle_declarator:;
11882 scope = get_scope_of_declarator (declarator);
11884 /* Any names that appear after the declarator-id for a
11885 member are looked up in the containing scope. */
11886 pushed_scope = push_scope (scope);
11887 parser->in_declarator_p = true;
11888 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11889 || (declarator && declarator->kind == cdk_id))
11890 /* Default args are only allowed on function
11892 parser->default_arg_ok_p = saved_default_arg_ok_p;
11894 parser->default_arg_ok_p = false;
11903 /* For an abstract declarator, we might wind up with nothing at this
11904 point. That's an error; the declarator is not optional. */
11906 cp_parser_error (parser, "expected declarator");
11908 /* If we entered a scope, we must exit it now. */
11910 pop_scope (pushed_scope);
11912 parser->default_arg_ok_p = saved_default_arg_ok_p;
11913 parser->in_declarator_p = saved_in_declarator_p;
11918 /* Parse a ptr-operator.
11921 * cv-qualifier-seq [opt]
11923 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11928 & cv-qualifier-seq [opt]
11930 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11931 Returns ADDR_EXPR if a reference was used. In the case of a
11932 pointer-to-member, *TYPE is filled in with the TYPE containing the
11933 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11934 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11935 ERROR_MARK if an error occurred. */
11937 static enum tree_code
11938 cp_parser_ptr_operator (cp_parser* parser,
11940 cp_cv_quals *cv_quals)
11942 enum tree_code code = ERROR_MARK;
11945 /* Assume that it's not a pointer-to-member. */
11947 /* And that there are no cv-qualifiers. */
11948 *cv_quals = TYPE_UNQUALIFIED;
11950 /* Peek at the next token. */
11951 token = cp_lexer_peek_token (parser->lexer);
11952 /* If it's a `*' or `&' we have a pointer or reference. */
11953 if (token->type == CPP_MULT || token->type == CPP_AND)
11955 /* Remember which ptr-operator we were processing. */
11956 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11958 /* Consume the `*' or `&'. */
11959 cp_lexer_consume_token (parser->lexer);
11961 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11962 `&', if we are allowing GNU extensions. (The only qualifier
11963 that can legally appear after `&' is `restrict', but that is
11964 enforced during semantic analysis. */
11965 if (code == INDIRECT_REF
11966 || cp_parser_allow_gnu_extensions_p (parser))
11967 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11971 /* Try the pointer-to-member case. */
11972 cp_parser_parse_tentatively (parser);
11973 /* Look for the optional `::' operator. */
11974 cp_parser_global_scope_opt (parser,
11975 /*current_scope_valid_p=*/false);
11976 /* Look for the nested-name specifier. */
11977 cp_parser_nested_name_specifier (parser,
11978 /*typename_keyword_p=*/false,
11979 /*check_dependency_p=*/true,
11981 /*is_declaration=*/false);
11982 /* If we found it, and the next token is a `*', then we are
11983 indeed looking at a pointer-to-member operator. */
11984 if (!cp_parser_error_occurred (parser)
11985 && cp_parser_require (parser, CPP_MULT, "`*'"))
11987 /* Indicate that the `*' operator was used. */
11988 code = INDIRECT_REF;
11990 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11991 error ("%qD is a namespace", parser->scope);
11994 /* The type of which the member is a member is given by the
11996 *type = parser->scope;
11997 /* The next name will not be qualified. */
11998 parser->scope = NULL_TREE;
11999 parser->qualifying_scope = NULL_TREE;
12000 parser->object_scope = NULL_TREE;
12001 /* Look for the optional cv-qualifier-seq. */
12002 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12005 /* If that didn't work we don't have a ptr-operator. */
12006 if (!cp_parser_parse_definitely (parser))
12007 cp_parser_error (parser, "expected ptr-operator");
12013 /* Parse an (optional) cv-qualifier-seq.
12016 cv-qualifier cv-qualifier-seq [opt]
12027 Returns a bitmask representing the cv-qualifiers. */
12030 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12032 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12037 cp_cv_quals cv_qualifier;
12039 /* Peek at the next token. */
12040 token = cp_lexer_peek_token (parser->lexer);
12041 /* See if it's a cv-qualifier. */
12042 switch (token->keyword)
12045 cv_qualifier = TYPE_QUAL_CONST;
12049 cv_qualifier = TYPE_QUAL_VOLATILE;
12053 cv_qualifier = TYPE_QUAL_RESTRICT;
12057 cv_qualifier = TYPE_UNQUALIFIED;
12064 if (cv_quals & cv_qualifier)
12066 error ("duplicate cv-qualifier");
12067 cp_lexer_purge_token (parser->lexer);
12071 cp_lexer_consume_token (parser->lexer);
12072 cv_quals |= cv_qualifier;
12079 /* Parse a declarator-id.
12083 :: [opt] nested-name-specifier [opt] type-name
12085 In the `id-expression' case, the value returned is as for
12086 cp_parser_id_expression if the id-expression was an unqualified-id.
12087 If the id-expression was a qualified-id, then a SCOPE_REF is
12088 returned. The first operand is the scope (either a NAMESPACE_DECL
12089 or TREE_TYPE), but the second is still just a representation of an
12093 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12096 /* The expression must be an id-expression. Assume that qualified
12097 names are the names of types so that:
12100 int S<T>::R::i = 3;
12102 will work; we must treat `S<T>::R' as the name of a type.
12103 Similarly, assume that qualified names are templates, where
12107 int S<T>::R<T>::i = 3;
12110 id = cp_parser_id_expression (parser,
12111 /*template_keyword_p=*/false,
12112 /*check_dependency_p=*/false,
12113 /*template_p=*/NULL,
12114 /*declarator_p=*/true,
12116 if (id && BASELINK_P (id))
12117 id = BASELINK_FUNCTIONS (id);
12121 /* Parse a type-id.
12124 type-specifier-seq abstract-declarator [opt]
12126 Returns the TYPE specified. */
12129 cp_parser_type_id (cp_parser* parser)
12131 cp_decl_specifier_seq type_specifier_seq;
12132 cp_declarator *abstract_declarator;
12134 /* Parse the type-specifier-seq. */
12135 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12136 &type_specifier_seq);
12137 if (type_specifier_seq.type == error_mark_node)
12138 return error_mark_node;
12140 /* There might or might not be an abstract declarator. */
12141 cp_parser_parse_tentatively (parser);
12142 /* Look for the declarator. */
12143 abstract_declarator
12144 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12145 /*parenthesized_p=*/NULL,
12146 /*member_p=*/false);
12147 /* Check to see if there really was a declarator. */
12148 if (!cp_parser_parse_definitely (parser))
12149 abstract_declarator = NULL;
12151 return groktypename (&type_specifier_seq, abstract_declarator);
12154 /* Parse a type-specifier-seq.
12156 type-specifier-seq:
12157 type-specifier type-specifier-seq [opt]
12161 type-specifier-seq:
12162 attributes type-specifier-seq [opt]
12164 If IS_CONDITION is true, we are at the start of a "condition",
12165 e.g., we've just seen "if (".
12167 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12170 cp_parser_type_specifier_seq (cp_parser* parser,
12172 cp_decl_specifier_seq *type_specifier_seq)
12174 bool seen_type_specifier = false;
12175 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12177 /* Clear the TYPE_SPECIFIER_SEQ. */
12178 clear_decl_specs (type_specifier_seq);
12180 /* Parse the type-specifiers and attributes. */
12183 tree type_specifier;
12184 bool is_cv_qualifier;
12186 /* Check for attributes first. */
12187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12189 type_specifier_seq->attributes =
12190 chainon (type_specifier_seq->attributes,
12191 cp_parser_attributes_opt (parser));
12195 /* Look for the type-specifier. */
12196 type_specifier = cp_parser_type_specifier (parser,
12198 type_specifier_seq,
12199 /*is_declaration=*/false,
12202 if (!type_specifier)
12204 /* If the first type-specifier could not be found, this is not a
12205 type-specifier-seq at all. */
12206 if (!seen_type_specifier)
12208 cp_parser_error (parser, "expected type-specifier");
12209 type_specifier_seq->type = error_mark_node;
12212 /* If subsequent type-specifiers could not be found, the
12213 type-specifier-seq is complete. */
12217 seen_type_specifier = true;
12218 /* The standard says that a condition can be:
12220 type-specifier-seq declarator = assignment-expression
12227 we should treat the "S" as a declarator, not as a
12228 type-specifier. The standard doesn't say that explicitly for
12229 type-specifier-seq, but it does say that for
12230 decl-specifier-seq in an ordinary declaration. Perhaps it
12231 would be clearer just to allow a decl-specifier-seq here, and
12232 then add a semantic restriction that if any decl-specifiers
12233 that are not type-specifiers appear, the program is invalid. */
12234 if (is_condition && !is_cv_qualifier)
12235 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12238 cp_parser_check_decl_spec (type_specifier_seq);
12241 /* Parse a parameter-declaration-clause.
12243 parameter-declaration-clause:
12244 parameter-declaration-list [opt] ... [opt]
12245 parameter-declaration-list , ...
12247 Returns a representation for the parameter declarations. A return
12248 value of NULL indicates a parameter-declaration-clause consisting
12249 only of an ellipsis. */
12251 static cp_parameter_declarator *
12252 cp_parser_parameter_declaration_clause (cp_parser* parser)
12254 cp_parameter_declarator *parameters;
12259 /* Peek at the next token. */
12260 token = cp_lexer_peek_token (parser->lexer);
12261 /* Check for trivial parameter-declaration-clauses. */
12262 if (token->type == CPP_ELLIPSIS)
12264 /* Consume the `...' token. */
12265 cp_lexer_consume_token (parser->lexer);
12268 else if (token->type == CPP_CLOSE_PAREN)
12269 /* There are no parameters. */
12271 #ifndef NO_IMPLICIT_EXTERN_C
12272 if (in_system_header && current_class_type == NULL
12273 && current_lang_name == lang_name_c)
12277 return no_parameters;
12279 /* Check for `(void)', too, which is a special case. */
12280 else if (token->keyword == RID_VOID
12281 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12282 == CPP_CLOSE_PAREN))
12284 /* Consume the `void' token. */
12285 cp_lexer_consume_token (parser->lexer);
12286 /* There are no parameters. */
12287 return no_parameters;
12290 /* Parse the parameter-declaration-list. */
12291 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12292 /* If a parse error occurred while parsing the
12293 parameter-declaration-list, then the entire
12294 parameter-declaration-clause is erroneous. */
12298 /* Peek at the next token. */
12299 token = cp_lexer_peek_token (parser->lexer);
12300 /* If it's a `,', the clause should terminate with an ellipsis. */
12301 if (token->type == CPP_COMMA)
12303 /* Consume the `,'. */
12304 cp_lexer_consume_token (parser->lexer);
12305 /* Expect an ellipsis. */
12307 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12309 /* It might also be `...' if the optional trailing `,' was
12311 else if (token->type == CPP_ELLIPSIS)
12313 /* Consume the `...' token. */
12314 cp_lexer_consume_token (parser->lexer);
12315 /* And remember that we saw it. */
12319 ellipsis_p = false;
12321 /* Finish the parameter list. */
12322 if (parameters && ellipsis_p)
12323 parameters->ellipsis_p = true;
12328 /* Parse a parameter-declaration-list.
12330 parameter-declaration-list:
12331 parameter-declaration
12332 parameter-declaration-list , parameter-declaration
12334 Returns a representation of the parameter-declaration-list, as for
12335 cp_parser_parameter_declaration_clause. However, the
12336 `void_list_node' is never appended to the list. Upon return,
12337 *IS_ERROR will be true iff an error occurred. */
12339 static cp_parameter_declarator *
12340 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12342 cp_parameter_declarator *parameters = NULL;
12343 cp_parameter_declarator **tail = ¶meters;
12344 bool saved_in_unbraced_linkage_specification_p;
12346 /* Assume all will go well. */
12348 /* The special considerations that apply to a function within an
12349 unbraced linkage specifications do not apply to the parameters
12350 to the function. */
12351 saved_in_unbraced_linkage_specification_p
12352 = parser->in_unbraced_linkage_specification_p;
12353 parser->in_unbraced_linkage_specification_p = false;
12355 /* Look for more parameters. */
12358 cp_parameter_declarator *parameter;
12359 bool parenthesized_p;
12360 /* Parse the parameter. */
12362 = cp_parser_parameter_declaration (parser,
12363 /*template_parm_p=*/false,
12366 /* If a parse error occurred parsing the parameter declaration,
12367 then the entire parameter-declaration-list is erroneous. */
12374 /* Add the new parameter to the list. */
12376 tail = ¶meter->next;
12378 /* Peek at the next token. */
12379 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12380 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12381 /* These are for Objective-C++ */
12382 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12384 /* The parameter-declaration-list is complete. */
12386 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12390 /* Peek at the next token. */
12391 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12392 /* If it's an ellipsis, then the list is complete. */
12393 if (token->type == CPP_ELLIPSIS)
12395 /* Otherwise, there must be more parameters. Consume the
12397 cp_lexer_consume_token (parser->lexer);
12398 /* When parsing something like:
12400 int i(float f, double d)
12402 we can tell after seeing the declaration for "f" that we
12403 are not looking at an initialization of a variable "i",
12404 but rather at the declaration of a function "i".
12406 Due to the fact that the parsing of template arguments
12407 (as specified to a template-id) requires backtracking we
12408 cannot use this technique when inside a template argument
12410 if (!parser->in_template_argument_list_p
12411 && !parser->in_type_id_in_expr_p
12412 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12413 /* However, a parameter-declaration of the form
12414 "foat(f)" (which is a valid declaration of a
12415 parameter "f") can also be interpreted as an
12416 expression (the conversion of "f" to "float"). */
12417 && !parenthesized_p)
12418 cp_parser_commit_to_tentative_parse (parser);
12422 cp_parser_error (parser, "expected %<,%> or %<...%>");
12423 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12424 cp_parser_skip_to_closing_parenthesis (parser,
12425 /*recovering=*/true,
12426 /*or_comma=*/false,
12427 /*consume_paren=*/false);
12432 parser->in_unbraced_linkage_specification_p
12433 = saved_in_unbraced_linkage_specification_p;
12438 /* Parse a parameter declaration.
12440 parameter-declaration:
12441 decl-specifier-seq declarator
12442 decl-specifier-seq declarator = assignment-expression
12443 decl-specifier-seq abstract-declarator [opt]
12444 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12446 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12447 declares a template parameter. (In that case, a non-nested `>'
12448 token encountered during the parsing of the assignment-expression
12449 is not interpreted as a greater-than operator.)
12451 Returns a representation of the parameter, or NULL if an error
12452 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12453 true iff the declarator is of the form "(p)". */
12455 static cp_parameter_declarator *
12456 cp_parser_parameter_declaration (cp_parser *parser,
12457 bool template_parm_p,
12458 bool *parenthesized_p)
12460 int declares_class_or_enum;
12461 bool greater_than_is_operator_p;
12462 cp_decl_specifier_seq decl_specifiers;
12463 cp_declarator *declarator;
12464 tree default_argument;
12466 const char *saved_message;
12468 /* In a template parameter, `>' is not an operator.
12472 When parsing a default template-argument for a non-type
12473 template-parameter, the first non-nested `>' is taken as the end
12474 of the template parameter-list rather than a greater-than
12476 greater_than_is_operator_p = !template_parm_p;
12478 /* Type definitions may not appear in parameter types. */
12479 saved_message = parser->type_definition_forbidden_message;
12480 parser->type_definition_forbidden_message
12481 = "types may not be defined in parameter types";
12483 /* Parse the declaration-specifiers. */
12484 cp_parser_decl_specifier_seq (parser,
12485 CP_PARSER_FLAGS_NONE,
12487 &declares_class_or_enum);
12488 /* If an error occurred, there's no reason to attempt to parse the
12489 rest of the declaration. */
12490 if (cp_parser_error_occurred (parser))
12492 parser->type_definition_forbidden_message = saved_message;
12496 /* Peek at the next token. */
12497 token = cp_lexer_peek_token (parser->lexer);
12498 /* If the next token is a `)', `,', `=', `>', or `...', then there
12499 is no declarator. */
12500 if (token->type == CPP_CLOSE_PAREN
12501 || token->type == CPP_COMMA
12502 || token->type == CPP_EQ
12503 || token->type == CPP_ELLIPSIS
12504 || token->type == CPP_GREATER)
12507 if (parenthesized_p)
12508 *parenthesized_p = false;
12510 /* Otherwise, there should be a declarator. */
12513 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12514 parser->default_arg_ok_p = false;
12516 /* After seeing a decl-specifier-seq, if the next token is not a
12517 "(", there is no possibility that the code is a valid
12518 expression. Therefore, if parsing tentatively, we commit at
12520 if (!parser->in_template_argument_list_p
12521 /* In an expression context, having seen:
12525 we cannot be sure whether we are looking at a
12526 function-type (taking a "char" as a parameter) or a cast
12527 of some object of type "char" to "int". */
12528 && !parser->in_type_id_in_expr_p
12529 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12530 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12531 cp_parser_commit_to_tentative_parse (parser);
12532 /* Parse the declarator. */
12533 declarator = cp_parser_declarator (parser,
12534 CP_PARSER_DECLARATOR_EITHER,
12535 /*ctor_dtor_or_conv_p=*/NULL,
12537 /*member_p=*/false);
12538 parser->default_arg_ok_p = saved_default_arg_ok_p;
12539 /* After the declarator, allow more attributes. */
12540 decl_specifiers.attributes
12541 = chainon (decl_specifiers.attributes,
12542 cp_parser_attributes_opt (parser));
12545 /* The restriction on defining new types applies only to the type
12546 of the parameter, not to the default argument. */
12547 parser->type_definition_forbidden_message = saved_message;
12549 /* If the next token is `=', then process a default argument. */
12550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12552 bool saved_greater_than_is_operator_p;
12553 /* Consume the `='. */
12554 cp_lexer_consume_token (parser->lexer);
12556 /* If we are defining a class, then the tokens that make up the
12557 default argument must be saved and processed later. */
12558 if (!template_parm_p && at_class_scope_p ()
12559 && TYPE_BEING_DEFINED (current_class_type))
12561 unsigned depth = 0;
12562 cp_token *first_token;
12565 /* Add tokens until we have processed the entire default
12566 argument. We add the range [first_token, token). */
12567 first_token = cp_lexer_peek_token (parser->lexer);
12572 /* Peek at the next token. */
12573 token = cp_lexer_peek_token (parser->lexer);
12574 /* What we do depends on what token we have. */
12575 switch (token->type)
12577 /* In valid code, a default argument must be
12578 immediately followed by a `,' `)', or `...'. */
12580 case CPP_CLOSE_PAREN:
12582 /* If we run into a non-nested `;', `}', or `]',
12583 then the code is invalid -- but the default
12584 argument is certainly over. */
12585 case CPP_SEMICOLON:
12586 case CPP_CLOSE_BRACE:
12587 case CPP_CLOSE_SQUARE:
12590 /* Update DEPTH, if necessary. */
12591 else if (token->type == CPP_CLOSE_PAREN
12592 || token->type == CPP_CLOSE_BRACE
12593 || token->type == CPP_CLOSE_SQUARE)
12597 case CPP_OPEN_PAREN:
12598 case CPP_OPEN_SQUARE:
12599 case CPP_OPEN_BRACE:
12604 /* If we see a non-nested `>', and `>' is not an
12605 operator, then it marks the end of the default
12607 if (!depth && !greater_than_is_operator_p)
12611 /* If we run out of tokens, issue an error message. */
12613 case CPP_PRAGMA_EOL:
12614 error ("file ends in default argument");
12620 /* In these cases, we should look for template-ids.
12621 For example, if the default argument is
12622 `X<int, double>()', we need to do name lookup to
12623 figure out whether or not `X' is a template; if
12624 so, the `,' does not end the default argument.
12626 That is not yet done. */
12633 /* If we've reached the end, stop. */
12637 /* Add the token to the token block. */
12638 token = cp_lexer_consume_token (parser->lexer);
12641 /* Create a DEFAULT_ARG to represented the unparsed default
12643 default_argument = make_node (DEFAULT_ARG);
12644 DEFARG_TOKENS (default_argument)
12645 = cp_token_cache_new (first_token, token);
12646 DEFARG_INSTANTIATIONS (default_argument) = NULL;
12648 /* Outside of a class definition, we can just parse the
12649 assignment-expression. */
12652 bool saved_local_variables_forbidden_p;
12654 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12656 saved_greater_than_is_operator_p
12657 = parser->greater_than_is_operator_p;
12658 parser->greater_than_is_operator_p = greater_than_is_operator_p;
12659 /* Local variable names (and the `this' keyword) may not
12660 appear in a default argument. */
12661 saved_local_variables_forbidden_p
12662 = parser->local_variables_forbidden_p;
12663 parser->local_variables_forbidden_p = true;
12664 /* The default argument expression may cause implicitly
12665 defined member functions to be synthesized, which will
12666 result in garbage collection. We must treat this
12667 situation as if we were within the body of function so as
12668 to avoid collecting live data on the stack. */
12670 /* Parse the assignment-expression. */
12671 if (template_parm_p)
12672 push_deferring_access_checks (dk_no_deferred);
12674 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12675 if (template_parm_p)
12676 pop_deferring_access_checks ();
12677 /* Restore saved state. */
12679 parser->greater_than_is_operator_p
12680 = saved_greater_than_is_operator_p;
12681 parser->local_variables_forbidden_p
12682 = saved_local_variables_forbidden_p;
12684 if (!parser->default_arg_ok_p)
12686 if (!flag_pedantic_errors)
12687 warning (0, "deprecated use of default argument for parameter of non-function");
12690 error ("default arguments are only permitted for function parameters");
12691 default_argument = NULL_TREE;
12696 default_argument = NULL_TREE;
12698 return make_parameter_declarator (&decl_specifiers,
12703 /* Parse a function-body.
12706 compound_statement */
12709 cp_parser_function_body (cp_parser *parser)
12711 cp_parser_compound_statement (parser, NULL, false);
12714 /* Parse a ctor-initializer-opt followed by a function-body. Return
12715 true if a ctor-initializer was present. */
12718 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12721 bool ctor_initializer_p;
12723 /* Begin the function body. */
12724 body = begin_function_body ();
12725 /* Parse the optional ctor-initializer. */
12726 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12727 /* Parse the function-body. */
12728 cp_parser_function_body (parser);
12729 /* Finish the function body. */
12730 finish_function_body (body);
12732 return ctor_initializer_p;
12735 /* Parse an initializer.
12738 = initializer-clause
12739 ( expression-list )
12741 Returns an expression representing the initializer. If no
12742 initializer is present, NULL_TREE is returned.
12744 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12745 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12746 set to FALSE if there is no initializer present. If there is an
12747 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12748 is set to true; otherwise it is set to false. */
12751 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12752 bool* non_constant_p)
12757 /* Peek at the next token. */
12758 token = cp_lexer_peek_token (parser->lexer);
12760 /* Let our caller know whether or not this initializer was
12762 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12763 /* Assume that the initializer is constant. */
12764 *non_constant_p = false;
12766 if (token->type == CPP_EQ)
12768 /* Consume the `='. */
12769 cp_lexer_consume_token (parser->lexer);
12770 /* Parse the initializer-clause. */
12771 init = cp_parser_initializer_clause (parser, non_constant_p);
12773 else if (token->type == CPP_OPEN_PAREN)
12774 init = cp_parser_parenthesized_expression_list (parser, false,
12779 /* Anything else is an error. */
12780 cp_parser_error (parser, "expected initializer");
12781 init = error_mark_node;
12787 /* Parse an initializer-clause.
12789 initializer-clause:
12790 assignment-expression
12791 { initializer-list , [opt] }
12794 Returns an expression representing the initializer.
12796 If the `assignment-expression' production is used the value
12797 returned is simply a representation for the expression.
12799 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12800 the elements of the initializer-list (or NULL, if the last
12801 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12802 NULL_TREE. There is no way to detect whether or not the optional
12803 trailing `,' was provided. NON_CONSTANT_P is as for
12804 cp_parser_initializer. */
12807 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12811 /* Assume the expression is constant. */
12812 *non_constant_p = false;
12814 /* If it is not a `{', then we are looking at an
12815 assignment-expression. */
12816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12819 = cp_parser_constant_expression (parser,
12820 /*allow_non_constant_p=*/true,
12822 if (!*non_constant_p)
12823 initializer = fold_non_dependent_expr (initializer);
12827 /* Consume the `{' token. */
12828 cp_lexer_consume_token (parser->lexer);
12829 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12830 initializer = make_node (CONSTRUCTOR);
12831 /* If it's not a `}', then there is a non-trivial initializer. */
12832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12834 /* Parse the initializer list. */
12835 CONSTRUCTOR_ELTS (initializer)
12836 = cp_parser_initializer_list (parser, non_constant_p);
12837 /* A trailing `,' token is allowed. */
12838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12839 cp_lexer_consume_token (parser->lexer);
12841 /* Now, there should be a trailing `}'. */
12842 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12845 return initializer;
12848 /* Parse an initializer-list.
12852 initializer-list , initializer-clause
12857 identifier : initializer-clause
12858 initializer-list, identifier : initializer-clause
12860 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
12861 for the initializer. If the INDEX of the elt is non-NULL, it is the
12862 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12863 as for cp_parser_initializer. */
12865 static VEC(constructor_elt,gc) *
12866 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12868 VEC(constructor_elt,gc) *v = NULL;
12870 /* Assume all of the expressions are constant. */
12871 *non_constant_p = false;
12873 /* Parse the rest of the list. */
12879 bool clause_non_constant_p;
12881 /* If the next token is an identifier and the following one is a
12882 colon, we are looking at the GNU designated-initializer
12884 if (cp_parser_allow_gnu_extensions_p (parser)
12885 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12886 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12888 /* Warn the user that they are using an extension. */
12890 pedwarn ("ISO C++ does not allow designated initializers");
12891 /* Consume the identifier. */
12892 identifier = cp_lexer_consume_token (parser->lexer)->value;
12893 /* Consume the `:'. */
12894 cp_lexer_consume_token (parser->lexer);
12897 identifier = NULL_TREE;
12899 /* Parse the initializer. */
12900 initializer = cp_parser_initializer_clause (parser,
12901 &clause_non_constant_p);
12902 /* If any clause is non-constant, so is the entire initializer. */
12903 if (clause_non_constant_p)
12904 *non_constant_p = true;
12906 /* Add it to the vector. */
12907 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12909 /* If the next token is not a comma, we have reached the end of
12911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12914 /* Peek at the next token. */
12915 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12916 /* If the next token is a `}', then we're still done. An
12917 initializer-clause can have a trailing `,' after the
12918 initializer-list and before the closing `}'. */
12919 if (token->type == CPP_CLOSE_BRACE)
12922 /* Consume the `,' token. */
12923 cp_lexer_consume_token (parser->lexer);
12929 /* Classes [gram.class] */
12931 /* Parse a class-name.
12937 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12938 to indicate that names looked up in dependent types should be
12939 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12940 keyword has been used to indicate that the name that appears next
12941 is a template. TAG_TYPE indicates the explicit tag given before
12942 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
12943 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
12944 is the class being defined in a class-head.
12946 Returns the TYPE_DECL representing the class. */
12949 cp_parser_class_name (cp_parser *parser,
12950 bool typename_keyword_p,
12951 bool template_keyword_p,
12952 enum tag_types tag_type,
12953 bool check_dependency_p,
12955 bool is_declaration)
12962 /* All class-names start with an identifier. */
12963 token = cp_lexer_peek_token (parser->lexer);
12964 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12966 cp_parser_error (parser, "expected class-name");
12967 return error_mark_node;
12970 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12971 to a template-id, so we save it here. */
12972 scope = parser->scope;
12973 if (scope == error_mark_node)
12974 return error_mark_node;
12976 /* Any name names a type if we're following the `typename' keyword
12977 in a qualified name where the enclosing scope is type-dependent. */
12978 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12979 && dependent_type_p (scope));
12980 /* Handle the common case (an identifier, but not a template-id)
12982 if (token->type == CPP_NAME
12983 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12985 cp_token *identifier_token;
12989 /* Look for the identifier. */
12990 identifier_token = cp_lexer_peek_token (parser->lexer);
12991 ambiguous_p = identifier_token->ambiguous_p;
12992 identifier = cp_parser_identifier (parser);
12993 /* If the next token isn't an identifier, we are certainly not
12994 looking at a class-name. */
12995 if (identifier == error_mark_node)
12996 decl = error_mark_node;
12997 /* If we know this is a type-name, there's no need to look it
12999 else if (typename_p)
13003 tree ambiguous_decls;
13004 /* If we already know that this lookup is ambiguous, then
13005 we've already issued an error message; there's no reason
13009 cp_parser_simulate_error (parser);
13010 return error_mark_node;
13012 /* If the next token is a `::', then the name must be a type
13015 [basic.lookup.qual]
13017 During the lookup for a name preceding the :: scope
13018 resolution operator, object, function, and enumerator
13019 names are ignored. */
13020 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13021 tag_type = typename_type;
13022 /* Look up the name. */
13023 decl = cp_parser_lookup_name (parser, identifier,
13025 /*is_template=*/false,
13026 /*is_namespace=*/false,
13027 check_dependency_p,
13029 if (ambiguous_decls)
13031 error ("reference to %qD is ambiguous", identifier);
13032 print_candidates (ambiguous_decls);
13033 if (cp_parser_parsing_tentatively (parser))
13035 identifier_token->ambiguous_p = true;
13036 cp_parser_simulate_error (parser);
13038 return error_mark_node;
13044 /* Try a template-id. */
13045 decl = cp_parser_template_id (parser, template_keyword_p,
13046 check_dependency_p,
13048 if (decl == error_mark_node)
13049 return error_mark_node;
13052 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13054 /* If this is a typename, create a TYPENAME_TYPE. */
13055 if (typename_p && decl != error_mark_node)
13057 decl = make_typename_type (scope, decl, typename_type,
13058 /*complain=*/tf_error);
13059 if (decl != error_mark_node)
13060 decl = TYPE_NAME (decl);
13063 /* Check to see that it is really the name of a class. */
13064 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13065 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13066 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13067 /* Situations like this:
13069 template <typename T> struct A {
13070 typename T::template X<int>::I i;
13073 are problematic. Is `T::template X<int>' a class-name? The
13074 standard does not seem to be definitive, but there is no other
13075 valid interpretation of the following `::'. Therefore, those
13076 names are considered class-names. */
13078 decl = make_typename_type (scope, decl, tag_type, tf_error);
13079 if (decl != error_mark_node)
13080 decl = TYPE_NAME (decl);
13082 else if (TREE_CODE (decl) != TYPE_DECL
13083 || TREE_TYPE (decl) == error_mark_node
13084 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13085 decl = error_mark_node;
13087 if (decl == error_mark_node)
13088 cp_parser_error (parser, "expected class-name");
13093 /* Parse a class-specifier.
13096 class-head { member-specification [opt] }
13098 Returns the TREE_TYPE representing the class. */
13101 cp_parser_class_specifier (cp_parser* parser)
13105 tree attributes = NULL_TREE;
13106 int has_trailing_semicolon;
13107 bool nested_name_specifier_p;
13108 unsigned saved_num_template_parameter_lists;
13109 bool saved_in_function_body;
13110 tree old_scope = NULL_TREE;
13111 tree scope = NULL_TREE;
13114 push_deferring_access_checks (dk_no_deferred);
13116 /* Parse the class-head. */
13117 type = cp_parser_class_head (parser,
13118 &nested_name_specifier_p,
13121 /* If the class-head was a semantic disaster, skip the entire body
13125 cp_parser_skip_to_end_of_block_or_statement (parser);
13126 pop_deferring_access_checks ();
13127 return error_mark_node;
13130 /* Look for the `{'. */
13131 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13133 pop_deferring_access_checks ();
13134 return error_mark_node;
13137 /* Process the base classes. If they're invalid, skip the
13138 entire class body. */
13139 if (!xref_basetypes (type, bases))
13141 cp_parser_skip_to_closing_brace (parser);
13143 /* Consuming the closing brace yields better error messages
13145 cp_lexer_consume_token (parser->lexer);
13146 pop_deferring_access_checks ();
13147 return error_mark_node;
13150 /* Issue an error message if type-definitions are forbidden here. */
13151 cp_parser_check_type_definition (parser);
13152 /* Remember that we are defining one more class. */
13153 ++parser->num_classes_being_defined;
13154 /* Inside the class, surrounding template-parameter-lists do not
13156 saved_num_template_parameter_lists
13157 = parser->num_template_parameter_lists;
13158 parser->num_template_parameter_lists = 0;
13159 /* We are not in a function body. */
13160 saved_in_function_body = parser->in_function_body;
13161 parser->in_function_body = false;
13163 /* Start the class. */
13164 if (nested_name_specifier_p)
13166 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13167 old_scope = push_inner_scope (scope);
13169 type = begin_class_definition (type, attributes);
13171 if (type == error_mark_node)
13172 /* If the type is erroneous, skip the entire body of the class. */
13173 cp_parser_skip_to_closing_brace (parser);
13175 /* Parse the member-specification. */
13176 cp_parser_member_specification_opt (parser);
13178 /* Look for the trailing `}'. */
13179 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13180 /* We get better error messages by noticing a common problem: a
13181 missing trailing `;'. */
13182 token = cp_lexer_peek_token (parser->lexer);
13183 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13184 /* Look for trailing attributes to apply to this class. */
13185 if (cp_parser_allow_gnu_extensions_p (parser))
13186 attributes = cp_parser_attributes_opt (parser);
13187 if (type != error_mark_node)
13188 type = finish_struct (type, attributes);
13189 if (nested_name_specifier_p)
13190 pop_inner_scope (old_scope, scope);
13191 /* If this class is not itself within the scope of another class,
13192 then we need to parse the bodies of all of the queued function
13193 definitions. Note that the queued functions defined in a class
13194 are not always processed immediately following the
13195 class-specifier for that class. Consider:
13198 struct B { void f() { sizeof (A); } };
13201 If `f' were processed before the processing of `A' were
13202 completed, there would be no way to compute the size of `A'.
13203 Note that the nesting we are interested in here is lexical --
13204 not the semantic nesting given by TYPE_CONTEXT. In particular,
13207 struct A { struct B; };
13208 struct A::B { void f() { } };
13210 there is no need to delay the parsing of `A::B::f'. */
13211 if (--parser->num_classes_being_defined == 0)
13215 tree class_type = NULL_TREE;
13216 tree pushed_scope = NULL_TREE;
13218 /* In a first pass, parse default arguments to the functions.
13219 Then, in a second pass, parse the bodies of the functions.
13220 This two-phased approach handles cases like:
13228 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13229 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13230 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13231 TREE_PURPOSE (parser->unparsed_functions_queues)
13232 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13234 fn = TREE_VALUE (queue_entry);
13235 /* If there are default arguments that have not yet been processed,
13236 take care of them now. */
13237 if (class_type != TREE_PURPOSE (queue_entry))
13240 pop_scope (pushed_scope);
13241 class_type = TREE_PURPOSE (queue_entry);
13242 pushed_scope = push_scope (class_type);
13244 /* Make sure that any template parameters are in scope. */
13245 maybe_begin_member_template_processing (fn);
13246 /* Parse the default argument expressions. */
13247 cp_parser_late_parsing_default_args (parser, fn);
13248 /* Remove any template parameters from the symbol table. */
13249 maybe_end_member_template_processing ();
13252 pop_scope (pushed_scope);
13253 /* Now parse the body of the functions. */
13254 for (TREE_VALUE (parser->unparsed_functions_queues)
13255 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13256 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13257 TREE_VALUE (parser->unparsed_functions_queues)
13258 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13260 /* Figure out which function we need to process. */
13261 fn = TREE_VALUE (queue_entry);
13262 /* Parse the function. */
13263 cp_parser_late_parsing_for_member (parser, fn);
13267 /* Put back any saved access checks. */
13268 pop_deferring_access_checks ();
13270 /* Restore saved state. */
13271 parser->in_function_body = saved_in_function_body;
13272 parser->num_template_parameter_lists
13273 = saved_num_template_parameter_lists;
13278 /* Parse a class-head.
13281 class-key identifier [opt] base-clause [opt]
13282 class-key nested-name-specifier identifier base-clause [opt]
13283 class-key nested-name-specifier [opt] template-id
13287 class-key attributes identifier [opt] base-clause [opt]
13288 class-key attributes nested-name-specifier identifier base-clause [opt]
13289 class-key attributes nested-name-specifier [opt] template-id
13292 Returns the TYPE of the indicated class. Sets
13293 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13294 involving a nested-name-specifier was used, and FALSE otherwise.
13296 Returns error_mark_node if this is not a class-head.
13298 Returns NULL_TREE if the class-head is syntactically valid, but
13299 semantically invalid in a way that means we should skip the entire
13300 body of the class. */
13303 cp_parser_class_head (cp_parser* parser,
13304 bool* nested_name_specifier_p,
13305 tree *attributes_p,
13308 tree nested_name_specifier;
13309 enum tag_types class_key;
13310 tree id = NULL_TREE;
13311 tree type = NULL_TREE;
13313 bool template_id_p = false;
13314 bool qualified_p = false;
13315 bool invalid_nested_name_p = false;
13316 bool invalid_explicit_specialization_p = false;
13317 tree pushed_scope = NULL_TREE;
13318 unsigned num_templates;
13320 /* Assume no nested-name-specifier will be present. */
13321 *nested_name_specifier_p = false;
13322 /* Assume no template parameter lists will be used in defining the
13326 /* Look for the class-key. */
13327 class_key = cp_parser_class_key (parser);
13328 if (class_key == none_type)
13329 return error_mark_node;
13331 /* Parse the attributes. */
13332 attributes = cp_parser_attributes_opt (parser);
13334 /* If the next token is `::', that is invalid -- but sometimes
13335 people do try to write:
13339 Handle this gracefully by accepting the extra qualifier, and then
13340 issuing an error about it later if this really is a
13341 class-head. If it turns out just to be an elaborated type
13342 specifier, remain silent. */
13343 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13344 qualified_p = true;
13346 push_deferring_access_checks (dk_no_check);
13348 /* Determine the name of the class. Begin by looking for an
13349 optional nested-name-specifier. */
13350 nested_name_specifier
13351 = cp_parser_nested_name_specifier_opt (parser,
13352 /*typename_keyword_p=*/false,
13353 /*check_dependency_p=*/false,
13355 /*is_declaration=*/false);
13356 /* If there was a nested-name-specifier, then there *must* be an
13358 if (nested_name_specifier)
13360 /* Although the grammar says `identifier', it really means
13361 `class-name' or `template-name'. You are only allowed to
13362 define a class that has already been declared with this
13365 The proposed resolution for Core Issue 180 says that wherever
13366 you see `class T::X' you should treat `X' as a type-name.
13368 It is OK to define an inaccessible class; for example:
13370 class A { class B; };
13373 We do not know if we will see a class-name, or a
13374 template-name. We look for a class-name first, in case the
13375 class-name is a template-id; if we looked for the
13376 template-name first we would stop after the template-name. */
13377 cp_parser_parse_tentatively (parser);
13378 type = cp_parser_class_name (parser,
13379 /*typename_keyword_p=*/false,
13380 /*template_keyword_p=*/false,
13382 /*check_dependency_p=*/false,
13383 /*class_head_p=*/true,
13384 /*is_declaration=*/false);
13385 /* If that didn't work, ignore the nested-name-specifier. */
13386 if (!cp_parser_parse_definitely (parser))
13388 invalid_nested_name_p = true;
13389 id = cp_parser_identifier (parser);
13390 if (id == error_mark_node)
13393 /* If we could not find a corresponding TYPE, treat this
13394 declaration like an unqualified declaration. */
13395 if (type == error_mark_node)
13396 nested_name_specifier = NULL_TREE;
13397 /* Otherwise, count the number of templates used in TYPE and its
13398 containing scopes. */
13403 for (scope = TREE_TYPE (type);
13404 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13405 scope = (TYPE_P (scope)
13406 ? TYPE_CONTEXT (scope)
13407 : DECL_CONTEXT (scope)))
13409 && CLASS_TYPE_P (scope)
13410 && CLASSTYPE_TEMPLATE_INFO (scope)
13411 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13412 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13416 /* Otherwise, the identifier is optional. */
13419 /* We don't know whether what comes next is a template-id,
13420 an identifier, or nothing at all. */
13421 cp_parser_parse_tentatively (parser);
13422 /* Check for a template-id. */
13423 id = cp_parser_template_id (parser,
13424 /*template_keyword_p=*/false,
13425 /*check_dependency_p=*/true,
13426 /*is_declaration=*/true);
13427 /* If that didn't work, it could still be an identifier. */
13428 if (!cp_parser_parse_definitely (parser))
13430 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13431 id = cp_parser_identifier (parser);
13437 template_id_p = true;
13442 pop_deferring_access_checks ();
13445 cp_parser_check_for_invalid_template_id (parser, id);
13447 /* If it's not a `:' or a `{' then we can't really be looking at a
13448 class-head, since a class-head only appears as part of a
13449 class-specifier. We have to detect this situation before calling
13450 xref_tag, since that has irreversible side-effects. */
13451 if (!cp_parser_next_token_starts_class_definition_p (parser))
13453 cp_parser_error (parser, "expected %<{%> or %<:%>");
13454 return error_mark_node;
13457 /* At this point, we're going ahead with the class-specifier, even
13458 if some other problem occurs. */
13459 cp_parser_commit_to_tentative_parse (parser);
13460 /* Issue the error about the overly-qualified name now. */
13462 cp_parser_error (parser,
13463 "global qualification of class name is invalid");
13464 else if (invalid_nested_name_p)
13465 cp_parser_error (parser,
13466 "qualified name does not name a class");
13467 else if (nested_name_specifier)
13471 /* Reject typedef-names in class heads. */
13472 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13474 error ("invalid class name in declaration of %qD", type);
13479 /* Figure out in what scope the declaration is being placed. */
13480 scope = current_scope ();
13481 /* If that scope does not contain the scope in which the
13482 class was originally declared, the program is invalid. */
13483 if (scope && !is_ancestor (scope, nested_name_specifier))
13485 error ("declaration of %qD in %qD which does not enclose %qD",
13486 type, scope, nested_name_specifier);
13492 A declarator-id shall not be qualified exception of the
13493 definition of a ... nested class outside of its class
13494 ... [or] a the definition or explicit instantiation of a
13495 class member of a namespace outside of its namespace. */
13496 if (scope == nested_name_specifier)
13498 pedwarn ("extra qualification ignored");
13499 nested_name_specifier = NULL_TREE;
13503 /* An explicit-specialization must be preceded by "template <>". If
13504 it is not, try to recover gracefully. */
13505 if (at_namespace_scope_p ()
13506 && parser->num_template_parameter_lists == 0
13509 error ("an explicit specialization must be preceded by %<template <>%>");
13510 invalid_explicit_specialization_p = true;
13511 /* Take the same action that would have been taken by
13512 cp_parser_explicit_specialization. */
13513 ++parser->num_template_parameter_lists;
13514 begin_specialization ();
13516 /* There must be no "return" statements between this point and the
13517 end of this function; set "type "to the correct return value and
13518 use "goto done;" to return. */
13519 /* Make sure that the right number of template parameters were
13521 if (!cp_parser_check_template_parameters (parser, num_templates))
13523 /* If something went wrong, there is no point in even trying to
13524 process the class-definition. */
13529 /* Look up the type. */
13532 type = TREE_TYPE (id);
13533 type = maybe_process_partial_specialization (type);
13534 if (nested_name_specifier)
13535 pushed_scope = push_scope (nested_name_specifier);
13537 else if (nested_name_specifier)
13543 template <typename T> struct S { struct T };
13544 template <typename T> struct S<T>::T { };
13546 we will get a TYPENAME_TYPE when processing the definition of
13547 `S::T'. We need to resolve it to the actual type before we
13548 try to define it. */
13549 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13551 class_type = resolve_typename_type (TREE_TYPE (type),
13552 /*only_current_p=*/false);
13553 if (class_type != error_mark_node)
13554 type = TYPE_NAME (class_type);
13557 cp_parser_error (parser, "could not resolve typename type");
13558 type = error_mark_node;
13562 maybe_process_partial_specialization (TREE_TYPE (type));
13563 class_type = current_class_type;
13564 /* Enter the scope indicated by the nested-name-specifier. */
13565 pushed_scope = push_scope (nested_name_specifier);
13566 /* Get the canonical version of this type. */
13567 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13568 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13569 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13571 type = push_template_decl (type);
13572 if (type == error_mark_node)
13579 type = TREE_TYPE (type);
13580 *nested_name_specifier_p = true;
13582 else /* The name is not a nested name. */
13584 /* If the class was unnamed, create a dummy name. */
13586 id = make_anon_name ();
13587 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13588 parser->num_template_parameter_lists);
13591 /* Indicate whether this class was declared as a `class' or as a
13593 if (TREE_CODE (type) == RECORD_TYPE)
13594 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13595 cp_parser_check_class_key (class_key, type);
13597 /* If this type was already complete, and we see another definition,
13598 that's an error. */
13599 if (type != error_mark_node && COMPLETE_TYPE_P (type))
13601 error ("redefinition of %q#T", type);
13602 error ("previous definition of %q+#T", type);
13606 else if (type == error_mark_node)
13609 /* We will have entered the scope containing the class; the names of
13610 base classes should be looked up in that context. For example:
13612 struct A { struct B {}; struct C; };
13613 struct A::C : B {};
13616 *bases = NULL_TREE;
13618 /* Get the list of base-classes, if there is one. */
13619 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13620 *bases = cp_parser_base_clause (parser);
13623 /* Leave the scope given by the nested-name-specifier. We will
13624 enter the class scope itself while processing the members. */
13626 pop_scope (pushed_scope);
13628 if (invalid_explicit_specialization_p)
13630 end_specialization ();
13631 --parser->num_template_parameter_lists;
13633 *attributes_p = attributes;
13637 /* Parse a class-key.
13644 Returns the kind of class-key specified, or none_type to indicate
13647 static enum tag_types
13648 cp_parser_class_key (cp_parser* parser)
13651 enum tag_types tag_type;
13653 /* Look for the class-key. */
13654 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13658 /* Check to see if the TOKEN is a class-key. */
13659 tag_type = cp_parser_token_is_class_key (token);
13661 cp_parser_error (parser, "expected class-key");
13665 /* Parse an (optional) member-specification.
13667 member-specification:
13668 member-declaration member-specification [opt]
13669 access-specifier : member-specification [opt] */
13672 cp_parser_member_specification_opt (cp_parser* parser)
13679 /* Peek at the next token. */
13680 token = cp_lexer_peek_token (parser->lexer);
13681 /* If it's a `}', or EOF then we've seen all the members. */
13682 if (token->type == CPP_CLOSE_BRACE
13683 || token->type == CPP_EOF
13684 || token->type == CPP_PRAGMA_EOL)
13687 /* See if this token is a keyword. */
13688 keyword = token->keyword;
13692 case RID_PROTECTED:
13694 /* Consume the access-specifier. */
13695 cp_lexer_consume_token (parser->lexer);
13696 /* Remember which access-specifier is active. */
13697 current_access_specifier = token->value;
13698 /* Look for the `:'. */
13699 cp_parser_require (parser, CPP_COLON, "`:'");
13703 /* Accept #pragmas at class scope. */
13704 if (token->type == CPP_PRAGMA)
13706 cp_parser_pragma (parser, pragma_external);
13710 /* Otherwise, the next construction must be a
13711 member-declaration. */
13712 cp_parser_member_declaration (parser);
13717 /* Parse a member-declaration.
13719 member-declaration:
13720 decl-specifier-seq [opt] member-declarator-list [opt] ;
13721 function-definition ; [opt]
13722 :: [opt] nested-name-specifier template [opt] unqualified-id ;
13724 template-declaration
13726 member-declarator-list:
13728 member-declarator-list , member-declarator
13731 declarator pure-specifier [opt]
13732 declarator constant-initializer [opt]
13733 identifier [opt] : constant-expression
13737 member-declaration:
13738 __extension__ member-declaration
13741 declarator attributes [opt] pure-specifier [opt]
13742 declarator attributes [opt] constant-initializer [opt]
13743 identifier [opt] attributes [opt] : constant-expression
13747 member-declaration:
13748 static_assert-declaration */
13751 cp_parser_member_declaration (cp_parser* parser)
13753 cp_decl_specifier_seq decl_specifiers;
13754 tree prefix_attributes;
13756 int declares_class_or_enum;
13759 int saved_pedantic;
13761 /* Check for the `__extension__' keyword. */
13762 if (cp_parser_extension_opt (parser, &saved_pedantic))
13765 cp_parser_member_declaration (parser);
13766 /* Restore the old value of the PEDANTIC flag. */
13767 pedantic = saved_pedantic;
13772 /* Check for a template-declaration. */
13773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13775 /* An explicit specialization here is an error condition, and we
13776 expect the specialization handler to detect and report this. */
13777 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13778 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13779 cp_parser_explicit_specialization (parser);
13781 cp_parser_template_declaration (parser, /*member_p=*/true);
13786 /* Check for a using-declaration. */
13787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13789 /* Parse the using-declaration. */
13790 cp_parser_using_declaration (parser,
13791 /*access_declaration_p=*/false);
13795 /* Check for @defs. */
13796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13799 tree ivar_chains = cp_parser_objc_defs_expression (parser);
13800 ivar = ivar_chains;
13804 ivar = TREE_CHAIN (member);
13805 TREE_CHAIN (member) = NULL_TREE;
13806 finish_member_declaration (member);
13811 /* If the next token is `static_assert' we have a static assertion. */
13812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
13814 cp_parser_static_assert (parser, /*member_p=*/true);
13818 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13821 /* Parse the decl-specifier-seq. */
13822 cp_parser_decl_specifier_seq (parser,
13823 CP_PARSER_FLAGS_OPTIONAL,
13825 &declares_class_or_enum);
13826 prefix_attributes = decl_specifiers.attributes;
13827 decl_specifiers.attributes = NULL_TREE;
13828 /* Check for an invalid type-name. */
13829 if (!decl_specifiers.type
13830 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13832 /* If there is no declarator, then the decl-specifier-seq should
13834 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13836 /* If there was no decl-specifier-seq, and the next token is a
13837 `;', then we have something like:
13843 Each member-declaration shall declare at least one member
13844 name of the class. */
13845 if (!decl_specifiers.any_specifiers_p)
13847 cp_token *token = cp_lexer_peek_token (parser->lexer);
13848 if (pedantic && !token->in_system_header)
13849 pedwarn ("%Hextra %<;%>", &token->location);
13855 /* See if this declaration is a friend. */
13856 friend_p = cp_parser_friend_p (&decl_specifiers);
13857 /* If there were decl-specifiers, check to see if there was
13858 a class-declaration. */
13859 type = check_tag_decl (&decl_specifiers);
13860 /* Nested classes have already been added to the class, but
13861 a `friend' needs to be explicitly registered. */
13864 /* If the `friend' keyword was present, the friend must
13865 be introduced with a class-key. */
13866 if (!declares_class_or_enum)
13867 error ("a class-key must be used when declaring a friend");
13870 template <typename T> struct A {
13871 friend struct A<T>::B;
13874 A<T>::B will be represented by a TYPENAME_TYPE, and
13875 therefore not recognized by check_tag_decl. */
13877 && decl_specifiers.type
13878 && TYPE_P (decl_specifiers.type))
13879 type = decl_specifiers.type;
13880 if (!type || !TYPE_P (type))
13881 error ("friend declaration does not name a class or "
13884 make_friend_class (current_class_type, type,
13885 /*complain=*/true);
13887 /* If there is no TYPE, an error message will already have
13889 else if (!type || type == error_mark_node)
13891 /* An anonymous aggregate has to be handled specially; such
13892 a declaration really declares a data member (with a
13893 particular type), as opposed to a nested class. */
13894 else if (ANON_AGGR_TYPE_P (type))
13896 /* Remove constructors and such from TYPE, now that we
13897 know it is an anonymous aggregate. */
13898 fixup_anonymous_aggr (type);
13899 /* And make the corresponding data member. */
13900 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13901 /* Add it to the class. */
13902 finish_member_declaration (decl);
13905 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13910 /* See if these declarations will be friends. */
13911 friend_p = cp_parser_friend_p (&decl_specifiers);
13913 /* Keep going until we hit the `;' at the end of the
13915 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13917 tree attributes = NULL_TREE;
13918 tree first_attribute;
13920 /* Peek at the next token. */
13921 token = cp_lexer_peek_token (parser->lexer);
13923 /* Check for a bitfield declaration. */
13924 if (token->type == CPP_COLON
13925 || (token->type == CPP_NAME
13926 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13932 /* Get the name of the bitfield. Note that we cannot just
13933 check TOKEN here because it may have been invalidated by
13934 the call to cp_lexer_peek_nth_token above. */
13935 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13936 identifier = cp_parser_identifier (parser);
13938 identifier = NULL_TREE;
13940 /* Consume the `:' token. */
13941 cp_lexer_consume_token (parser->lexer);
13942 /* Get the width of the bitfield. */
13944 = cp_parser_constant_expression (parser,
13945 /*allow_non_constant=*/false,
13948 /* Look for attributes that apply to the bitfield. */
13949 attributes = cp_parser_attributes_opt (parser);
13950 /* Remember which attributes are prefix attributes and
13952 first_attribute = attributes;
13953 /* Combine the attributes. */
13954 attributes = chainon (prefix_attributes, attributes);
13956 /* Create the bitfield declaration. */
13957 decl = grokbitfield (identifier
13958 ? make_id_declarator (NULL_TREE,
13964 /* Apply the attributes. */
13965 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13969 cp_declarator *declarator;
13971 tree asm_specification;
13972 int ctor_dtor_or_conv_p;
13974 /* Parse the declarator. */
13976 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13977 &ctor_dtor_or_conv_p,
13978 /*parenthesized_p=*/NULL,
13979 /*member_p=*/true);
13981 /* If something went wrong parsing the declarator, make sure
13982 that we at least consume some tokens. */
13983 if (declarator == cp_error_declarator)
13985 /* Skip to the end of the statement. */
13986 cp_parser_skip_to_end_of_statement (parser);
13987 /* If the next token is not a semicolon, that is
13988 probably because we just skipped over the body of
13989 a function. So, we consume a semicolon if
13990 present, but do not issue an error message if it
13992 if (cp_lexer_next_token_is (parser->lexer,
13994 cp_lexer_consume_token (parser->lexer);
13998 if (declares_class_or_enum & 2)
13999 cp_parser_check_for_definition_in_return_type
14000 (declarator, decl_specifiers.type);
14002 /* Look for an asm-specification. */
14003 asm_specification = cp_parser_asm_specification_opt (parser);
14004 /* Look for attributes that apply to the declaration. */
14005 attributes = cp_parser_attributes_opt (parser);
14006 /* Remember which attributes are prefix attributes and
14008 first_attribute = attributes;
14009 /* Combine the attributes. */
14010 attributes = chainon (prefix_attributes, attributes);
14012 /* If it's an `=', then we have a constant-initializer or a
14013 pure-specifier. It is not correct to parse the
14014 initializer before registering the member declaration
14015 since the member declaration should be in scope while
14016 its initializer is processed. However, the rest of the
14017 front end does not yet provide an interface that allows
14018 us to handle this correctly. */
14019 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14023 A pure-specifier shall be used only in the declaration of
14024 a virtual function.
14026 A member-declarator can contain a constant-initializer
14027 only if it declares a static member of integral or
14030 Therefore, if the DECLARATOR is for a function, we look
14031 for a pure-specifier; otherwise, we look for a
14032 constant-initializer. When we call `grokfield', it will
14033 perform more stringent semantics checks. */
14034 if (function_declarator_p (declarator))
14035 initializer = cp_parser_pure_specifier (parser);
14037 /* Parse the initializer. */
14038 initializer = cp_parser_constant_initializer (parser);
14040 /* Otherwise, there is no initializer. */
14042 initializer = NULL_TREE;
14044 /* See if we are probably looking at a function
14045 definition. We are certainly not looking at a
14046 member-declarator. Calling `grokfield' has
14047 side-effects, so we must not do it unless we are sure
14048 that we are looking at a member-declarator. */
14049 if (cp_parser_token_starts_function_definition_p
14050 (cp_lexer_peek_token (parser->lexer)))
14052 /* The grammar does not allow a pure-specifier to be
14053 used when a member function is defined. (It is
14054 possible that this fact is an oversight in the
14055 standard, since a pure function may be defined
14056 outside of the class-specifier. */
14058 error ("pure-specifier on function-definition");
14059 decl = cp_parser_save_member_function_body (parser,
14063 /* If the member was not a friend, declare it here. */
14065 finish_member_declaration (decl);
14066 /* Peek at the next token. */
14067 token = cp_lexer_peek_token (parser->lexer);
14068 /* If the next token is a semicolon, consume it. */
14069 if (token->type == CPP_SEMICOLON)
14070 cp_lexer_consume_token (parser->lexer);
14074 /* Create the declaration. */
14075 decl = grokfield (declarator, &decl_specifiers,
14076 initializer, /*init_const_expr_p=*/true,
14081 /* Reset PREFIX_ATTRIBUTES. */
14082 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14083 attributes = TREE_CHAIN (attributes);
14085 TREE_CHAIN (attributes) = NULL_TREE;
14087 /* If there is any qualification still in effect, clear it
14088 now; we will be starting fresh with the next declarator. */
14089 parser->scope = NULL_TREE;
14090 parser->qualifying_scope = NULL_TREE;
14091 parser->object_scope = NULL_TREE;
14092 /* If it's a `,', then there are more declarators. */
14093 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14094 cp_lexer_consume_token (parser->lexer);
14095 /* If the next token isn't a `;', then we have a parse error. */
14096 else if (cp_lexer_next_token_is_not (parser->lexer,
14099 cp_parser_error (parser, "expected %<;%>");
14100 /* Skip tokens until we find a `;'. */
14101 cp_parser_skip_to_end_of_statement (parser);
14108 /* Add DECL to the list of members. */
14110 finish_member_declaration (decl);
14112 if (TREE_CODE (decl) == FUNCTION_DECL)
14113 cp_parser_save_default_args (parser, decl);
14118 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14121 /* Parse a pure-specifier.
14126 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14127 Otherwise, ERROR_MARK_NODE is returned. */
14130 cp_parser_pure_specifier (cp_parser* parser)
14134 /* Look for the `=' token. */
14135 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14136 return error_mark_node;
14137 /* Look for the `0' token. */
14138 token = cp_lexer_consume_token (parser->lexer);
14139 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14140 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14142 cp_parser_error (parser,
14143 "invalid pure specifier (only `= 0' is allowed)");
14144 cp_parser_skip_to_end_of_statement (parser);
14145 return error_mark_node;
14147 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14149 error ("templates may not be %<virtual%>");
14150 return error_mark_node;
14153 return integer_zero_node;
14156 /* Parse a constant-initializer.
14158 constant-initializer:
14159 = constant-expression
14161 Returns a representation of the constant-expression. */
14164 cp_parser_constant_initializer (cp_parser* parser)
14166 /* Look for the `=' token. */
14167 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14168 return error_mark_node;
14170 /* It is invalid to write:
14172 struct S { static const int i = { 7 }; };
14175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14177 cp_parser_error (parser,
14178 "a brace-enclosed initializer is not allowed here");
14179 /* Consume the opening brace. */
14180 cp_lexer_consume_token (parser->lexer);
14181 /* Skip the initializer. */
14182 cp_parser_skip_to_closing_brace (parser);
14183 /* Look for the trailing `}'. */
14184 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14186 return error_mark_node;
14189 return cp_parser_constant_expression (parser,
14190 /*allow_non_constant=*/false,
14194 /* Derived classes [gram.class.derived] */
14196 /* Parse a base-clause.
14199 : base-specifier-list
14201 base-specifier-list:
14203 base-specifier-list , base-specifier
14205 Returns a TREE_LIST representing the base-classes, in the order in
14206 which they were declared. The representation of each node is as
14207 described by cp_parser_base_specifier.
14209 In the case that no bases are specified, this function will return
14210 NULL_TREE, not ERROR_MARK_NODE. */
14213 cp_parser_base_clause (cp_parser* parser)
14215 tree bases = NULL_TREE;
14217 /* Look for the `:' that begins the list. */
14218 cp_parser_require (parser, CPP_COLON, "`:'");
14220 /* Scan the base-specifier-list. */
14226 /* Look for the base-specifier. */
14227 base = cp_parser_base_specifier (parser);
14228 /* Add BASE to the front of the list. */
14229 if (base != error_mark_node)
14231 TREE_CHAIN (base) = bases;
14234 /* Peek at the next token. */
14235 token = cp_lexer_peek_token (parser->lexer);
14236 /* If it's not a comma, then the list is complete. */
14237 if (token->type != CPP_COMMA)
14239 /* Consume the `,'. */
14240 cp_lexer_consume_token (parser->lexer);
14243 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14244 base class had a qualified name. However, the next name that
14245 appears is certainly not qualified. */
14246 parser->scope = NULL_TREE;
14247 parser->qualifying_scope = NULL_TREE;
14248 parser->object_scope = NULL_TREE;
14250 return nreverse (bases);
14253 /* Parse a base-specifier.
14256 :: [opt] nested-name-specifier [opt] class-name
14257 virtual access-specifier [opt] :: [opt] nested-name-specifier
14259 access-specifier virtual [opt] :: [opt] nested-name-specifier
14262 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14263 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14264 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14265 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14268 cp_parser_base_specifier (cp_parser* parser)
14272 bool virtual_p = false;
14273 bool duplicate_virtual_error_issued_p = false;
14274 bool duplicate_access_error_issued_p = false;
14275 bool class_scope_p, template_p;
14276 tree access = access_default_node;
14279 /* Process the optional `virtual' and `access-specifier'. */
14282 /* Peek at the next token. */
14283 token = cp_lexer_peek_token (parser->lexer);
14284 /* Process `virtual'. */
14285 switch (token->keyword)
14288 /* If `virtual' appears more than once, issue an error. */
14289 if (virtual_p && !duplicate_virtual_error_issued_p)
14291 cp_parser_error (parser,
14292 "%<virtual%> specified more than once in base-specified");
14293 duplicate_virtual_error_issued_p = true;
14298 /* Consume the `virtual' token. */
14299 cp_lexer_consume_token (parser->lexer);
14304 case RID_PROTECTED:
14306 /* If more than one access specifier appears, issue an
14308 if (access != access_default_node
14309 && !duplicate_access_error_issued_p)
14311 cp_parser_error (parser,
14312 "more than one access specifier in base-specified");
14313 duplicate_access_error_issued_p = true;
14316 access = ridpointers[(int) token->keyword];
14318 /* Consume the access-specifier. */
14319 cp_lexer_consume_token (parser->lexer);
14328 /* It is not uncommon to see programs mechanically, erroneously, use
14329 the 'typename' keyword to denote (dependent) qualified types
14330 as base classes. */
14331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14333 if (!processing_template_decl)
14334 error ("keyword %<typename%> not allowed outside of templates");
14336 error ("keyword %<typename%> not allowed in this context "
14337 "(the base class is implicitly a type)");
14338 cp_lexer_consume_token (parser->lexer);
14341 /* Look for the optional `::' operator. */
14342 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14343 /* Look for the nested-name-specifier. The simplest way to
14348 The keyword `typename' is not permitted in a base-specifier or
14349 mem-initializer; in these contexts a qualified name that
14350 depends on a template-parameter is implicitly assumed to be a
14353 is to pretend that we have seen the `typename' keyword at this
14355 cp_parser_nested_name_specifier_opt (parser,
14356 /*typename_keyword_p=*/true,
14357 /*check_dependency_p=*/true,
14359 /*is_declaration=*/true);
14360 /* If the base class is given by a qualified name, assume that names
14361 we see are type names or templates, as appropriate. */
14362 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14363 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14365 /* Finally, look for the class-name. */
14366 type = cp_parser_class_name (parser,
14370 /*check_dependency_p=*/true,
14371 /*class_head_p=*/false,
14372 /*is_declaration=*/true);
14374 if (type == error_mark_node)
14375 return error_mark_node;
14377 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14380 /* Exception handling [gram.exception] */
14382 /* Parse an (optional) exception-specification.
14384 exception-specification:
14385 throw ( type-id-list [opt] )
14387 Returns a TREE_LIST representing the exception-specification. The
14388 TREE_VALUE of each node is a type. */
14391 cp_parser_exception_specification_opt (cp_parser* parser)
14396 /* Peek at the next token. */
14397 token = cp_lexer_peek_token (parser->lexer);
14398 /* If it's not `throw', then there's no exception-specification. */
14399 if (!cp_parser_is_keyword (token, RID_THROW))
14402 /* Consume the `throw'. */
14403 cp_lexer_consume_token (parser->lexer);
14405 /* Look for the `('. */
14406 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14408 /* Peek at the next token. */
14409 token = cp_lexer_peek_token (parser->lexer);
14410 /* If it's not a `)', then there is a type-id-list. */
14411 if (token->type != CPP_CLOSE_PAREN)
14413 const char *saved_message;
14415 /* Types may not be defined in an exception-specification. */
14416 saved_message = parser->type_definition_forbidden_message;
14417 parser->type_definition_forbidden_message
14418 = "types may not be defined in an exception-specification";
14419 /* Parse the type-id-list. */
14420 type_id_list = cp_parser_type_id_list (parser);
14421 /* Restore the saved message. */
14422 parser->type_definition_forbidden_message = saved_message;
14425 type_id_list = empty_except_spec;
14427 /* Look for the `)'. */
14428 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14430 return type_id_list;
14433 /* Parse an (optional) type-id-list.
14437 type-id-list , type-id
14439 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14440 in the order that the types were presented. */
14443 cp_parser_type_id_list (cp_parser* parser)
14445 tree types = NULL_TREE;
14452 /* Get the next type-id. */
14453 type = cp_parser_type_id (parser);
14454 /* Add it to the list. */
14455 types = add_exception_specifier (types, type, /*complain=*/1);
14456 /* Peek at the next token. */
14457 token = cp_lexer_peek_token (parser->lexer);
14458 /* If it is not a `,', we are done. */
14459 if (token->type != CPP_COMMA)
14461 /* Consume the `,'. */
14462 cp_lexer_consume_token (parser->lexer);
14465 return nreverse (types);
14468 /* Parse a try-block.
14471 try compound-statement handler-seq */
14474 cp_parser_try_block (cp_parser* parser)
14478 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14479 try_block = begin_try_block ();
14480 cp_parser_compound_statement (parser, NULL, true);
14481 finish_try_block (try_block);
14482 cp_parser_handler_seq (parser);
14483 finish_handler_sequence (try_block);
14488 /* Parse a function-try-block.
14490 function-try-block:
14491 try ctor-initializer [opt] function-body handler-seq */
14494 cp_parser_function_try_block (cp_parser* parser)
14496 tree compound_stmt;
14498 bool ctor_initializer_p;
14500 /* Look for the `try' keyword. */
14501 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14503 /* Let the rest of the front-end know where we are. */
14504 try_block = begin_function_try_block (&compound_stmt);
14505 /* Parse the function-body. */
14507 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14508 /* We're done with the `try' part. */
14509 finish_function_try_block (try_block);
14510 /* Parse the handlers. */
14511 cp_parser_handler_seq (parser);
14512 /* We're done with the handlers. */
14513 finish_function_handler_sequence (try_block, compound_stmt);
14515 return ctor_initializer_p;
14518 /* Parse a handler-seq.
14521 handler handler-seq [opt] */
14524 cp_parser_handler_seq (cp_parser* parser)
14530 /* Parse the handler. */
14531 cp_parser_handler (parser);
14532 /* Peek at the next token. */
14533 token = cp_lexer_peek_token (parser->lexer);
14534 /* If it's not `catch' then there are no more handlers. */
14535 if (!cp_parser_is_keyword (token, RID_CATCH))
14540 /* Parse a handler.
14543 catch ( exception-declaration ) compound-statement */
14546 cp_parser_handler (cp_parser* parser)
14551 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14552 handler = begin_handler ();
14553 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14554 declaration = cp_parser_exception_declaration (parser);
14555 finish_handler_parms (declaration, handler);
14556 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14557 cp_parser_compound_statement (parser, NULL, false);
14558 finish_handler (handler);
14561 /* Parse an exception-declaration.
14563 exception-declaration:
14564 type-specifier-seq declarator
14565 type-specifier-seq abstract-declarator
14569 Returns a VAR_DECL for the declaration, or NULL_TREE if the
14570 ellipsis variant is used. */
14573 cp_parser_exception_declaration (cp_parser* parser)
14575 cp_decl_specifier_seq type_specifiers;
14576 cp_declarator *declarator;
14577 const char *saved_message;
14579 /* If it's an ellipsis, it's easy to handle. */
14580 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14582 /* Consume the `...' token. */
14583 cp_lexer_consume_token (parser->lexer);
14587 /* Types may not be defined in exception-declarations. */
14588 saved_message = parser->type_definition_forbidden_message;
14589 parser->type_definition_forbidden_message
14590 = "types may not be defined in exception-declarations";
14592 /* Parse the type-specifier-seq. */
14593 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14595 /* If it's a `)', then there is no declarator. */
14596 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14599 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14600 /*ctor_dtor_or_conv_p=*/NULL,
14601 /*parenthesized_p=*/NULL,
14602 /*member_p=*/false);
14604 /* Restore the saved message. */
14605 parser->type_definition_forbidden_message = saved_message;
14607 if (!type_specifiers.any_specifiers_p)
14608 return error_mark_node;
14610 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14613 /* Parse a throw-expression.
14616 throw assignment-expression [opt]
14618 Returns a THROW_EXPR representing the throw-expression. */
14621 cp_parser_throw_expression (cp_parser* parser)
14626 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14627 token = cp_lexer_peek_token (parser->lexer);
14628 /* Figure out whether or not there is an assignment-expression
14629 following the "throw" keyword. */
14630 if (token->type == CPP_COMMA
14631 || token->type == CPP_SEMICOLON
14632 || token->type == CPP_CLOSE_PAREN
14633 || token->type == CPP_CLOSE_SQUARE
14634 || token->type == CPP_CLOSE_BRACE
14635 || token->type == CPP_COLON)
14636 expression = NULL_TREE;
14638 expression = cp_parser_assignment_expression (parser,
14641 return build_throw (expression);
14644 /* GNU Extensions */
14646 /* Parse an (optional) asm-specification.
14649 asm ( string-literal )
14651 If the asm-specification is present, returns a STRING_CST
14652 corresponding to the string-literal. Otherwise, returns
14656 cp_parser_asm_specification_opt (cp_parser* parser)
14659 tree asm_specification;
14661 /* Peek at the next token. */
14662 token = cp_lexer_peek_token (parser->lexer);
14663 /* If the next token isn't the `asm' keyword, then there's no
14664 asm-specification. */
14665 if (!cp_parser_is_keyword (token, RID_ASM))
14668 /* Consume the `asm' token. */
14669 cp_lexer_consume_token (parser->lexer);
14670 /* Look for the `('. */
14671 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14673 /* Look for the string-literal. */
14674 asm_specification = cp_parser_string_literal (parser, false, false);
14676 /* Look for the `)'. */
14677 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14679 return asm_specification;
14682 /* Parse an asm-operand-list.
14686 asm-operand-list , asm-operand
14689 string-literal ( expression )
14690 [ string-literal ] string-literal ( expression )
14692 Returns a TREE_LIST representing the operands. The TREE_VALUE of
14693 each node is the expression. The TREE_PURPOSE is itself a
14694 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14695 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14696 is a STRING_CST for the string literal before the parenthesis. */
14699 cp_parser_asm_operand_list (cp_parser* parser)
14701 tree asm_operands = NULL_TREE;
14705 tree string_literal;
14709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14711 /* Consume the `[' token. */
14712 cp_lexer_consume_token (parser->lexer);
14713 /* Read the operand name. */
14714 name = cp_parser_identifier (parser);
14715 if (name != error_mark_node)
14716 name = build_string (IDENTIFIER_LENGTH (name),
14717 IDENTIFIER_POINTER (name));
14718 /* Look for the closing `]'. */
14719 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14723 /* Look for the string-literal. */
14724 string_literal = cp_parser_string_literal (parser, false, false);
14726 /* Look for the `('. */
14727 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14728 /* Parse the expression. */
14729 expression = cp_parser_expression (parser, /*cast_p=*/false);
14730 /* Look for the `)'. */
14731 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14733 /* Add this operand to the list. */
14734 asm_operands = tree_cons (build_tree_list (name, string_literal),
14737 /* If the next token is not a `,', there are no more
14739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14741 /* Consume the `,'. */
14742 cp_lexer_consume_token (parser->lexer);
14745 return nreverse (asm_operands);
14748 /* Parse an asm-clobber-list.
14752 asm-clobber-list , string-literal
14754 Returns a TREE_LIST, indicating the clobbers in the order that they
14755 appeared. The TREE_VALUE of each node is a STRING_CST. */
14758 cp_parser_asm_clobber_list (cp_parser* parser)
14760 tree clobbers = NULL_TREE;
14764 tree string_literal;
14766 /* Look for the string literal. */
14767 string_literal = cp_parser_string_literal (parser, false, false);
14768 /* Add it to the list. */
14769 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14770 /* If the next token is not a `,', then the list is
14772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14774 /* Consume the `,' token. */
14775 cp_lexer_consume_token (parser->lexer);
14781 /* Parse an (optional) series of attributes.
14784 attributes attribute
14787 __attribute__ (( attribute-list [opt] ))
14789 The return value is as for cp_parser_attribute_list. */
14792 cp_parser_attributes_opt (cp_parser* parser)
14794 tree attributes = NULL_TREE;
14799 tree attribute_list;
14801 /* Peek at the next token. */
14802 token = cp_lexer_peek_token (parser->lexer);
14803 /* If it's not `__attribute__', then we're done. */
14804 if (token->keyword != RID_ATTRIBUTE)
14807 /* Consume the `__attribute__' keyword. */
14808 cp_lexer_consume_token (parser->lexer);
14809 /* Look for the two `(' tokens. */
14810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14811 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14813 /* Peek at the next token. */
14814 token = cp_lexer_peek_token (parser->lexer);
14815 if (token->type != CPP_CLOSE_PAREN)
14816 /* Parse the attribute-list. */
14817 attribute_list = cp_parser_attribute_list (parser);
14819 /* If the next token is a `)', then there is no attribute
14821 attribute_list = NULL;
14823 /* Look for the two `)' tokens. */
14824 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14827 /* Add these new attributes to the list. */
14828 attributes = chainon (attributes, attribute_list);
14834 /* Parse an attribute-list.
14838 attribute-list , attribute
14842 identifier ( identifier )
14843 identifier ( identifier , expression-list )
14844 identifier ( expression-list )
14846 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
14847 to an attribute. The TREE_PURPOSE of each node is the identifier
14848 indicating which attribute is in use. The TREE_VALUE represents
14849 the arguments, if any. */
14852 cp_parser_attribute_list (cp_parser* parser)
14854 tree attribute_list = NULL_TREE;
14855 bool save_translate_strings_p = parser->translate_strings_p;
14857 parser->translate_strings_p = false;
14864 /* Look for the identifier. We also allow keywords here; for
14865 example `__attribute__ ((const))' is legal. */
14866 token = cp_lexer_peek_token (parser->lexer);
14867 if (token->type == CPP_NAME
14868 || token->type == CPP_KEYWORD)
14870 tree arguments = NULL_TREE;
14872 /* Consume the token. */
14873 token = cp_lexer_consume_token (parser->lexer);
14875 /* Save away the identifier that indicates which attribute
14877 identifier = token->value;
14878 attribute = build_tree_list (identifier, NULL_TREE);
14880 /* Peek at the next token. */
14881 token = cp_lexer_peek_token (parser->lexer);
14882 /* If it's an `(', then parse the attribute arguments. */
14883 if (token->type == CPP_OPEN_PAREN)
14885 arguments = cp_parser_parenthesized_expression_list
14886 (parser, true, /*cast_p=*/false,
14887 /*non_constant_p=*/NULL);
14888 /* Save the arguments away. */
14889 TREE_VALUE (attribute) = arguments;
14892 if (arguments != error_mark_node)
14894 /* Add this attribute to the list. */
14895 TREE_CHAIN (attribute) = attribute_list;
14896 attribute_list = attribute;
14899 token = cp_lexer_peek_token (parser->lexer);
14901 /* Now, look for more attributes. If the next token isn't a
14902 `,', we're done. */
14903 if (token->type != CPP_COMMA)
14906 /* Consume the comma and keep going. */
14907 cp_lexer_consume_token (parser->lexer);
14909 parser->translate_strings_p = save_translate_strings_p;
14911 /* We built up the list in reverse order. */
14912 return nreverse (attribute_list);
14915 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14916 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14917 current value of the PEDANTIC flag, regardless of whether or not
14918 the `__extension__' keyword is present. The caller is responsible
14919 for restoring the value of the PEDANTIC flag. */
14922 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14924 /* Save the old value of the PEDANTIC flag. */
14925 *saved_pedantic = pedantic;
14927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14929 /* Consume the `__extension__' token. */
14930 cp_lexer_consume_token (parser->lexer);
14931 /* We're not being pedantic while the `__extension__' keyword is
14941 /* Parse a label declaration.
14944 __label__ label-declarator-seq ;
14946 label-declarator-seq:
14947 identifier , label-declarator-seq
14951 cp_parser_label_declaration (cp_parser* parser)
14953 /* Look for the `__label__' keyword. */
14954 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14960 /* Look for an identifier. */
14961 identifier = cp_parser_identifier (parser);
14962 /* If we failed, stop. */
14963 if (identifier == error_mark_node)
14965 /* Declare it as a label. */
14966 finish_label_decl (identifier);
14967 /* If the next token is a `;', stop. */
14968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14970 /* Look for the `,' separating the label declarations. */
14971 cp_parser_require (parser, CPP_COMMA, "`,'");
14974 /* Look for the final `;'. */
14975 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14978 /* Support Functions */
14980 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14981 NAME should have one of the representations used for an
14982 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14983 is returned. If PARSER->SCOPE is a dependent type, then a
14984 SCOPE_REF is returned.
14986 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14987 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14988 was formed. Abstractly, such entities should not be passed to this
14989 function, because they do not need to be looked up, but it is
14990 simpler to check for this special case here, rather than at the
14993 In cases not explicitly covered above, this function returns a
14994 DECL, OVERLOAD, or baselink representing the result of the lookup.
14995 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14998 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14999 (e.g., "struct") that was used. In that case bindings that do not
15000 refer to types are ignored.
15002 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15005 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15008 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15011 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15012 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15013 NULL_TREE otherwise. */
15016 cp_parser_lookup_name (cp_parser *parser, tree name,
15017 enum tag_types tag_type,
15020 bool check_dependency,
15021 tree *ambiguous_decls)
15025 tree object_type = parser->context->object_type;
15027 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15028 flags |= LOOKUP_COMPLAIN;
15030 /* Assume that the lookup will be unambiguous. */
15031 if (ambiguous_decls)
15032 *ambiguous_decls = NULL_TREE;
15034 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15035 no longer valid. Note that if we are parsing tentatively, and
15036 the parse fails, OBJECT_TYPE will be automatically restored. */
15037 parser->context->object_type = NULL_TREE;
15039 if (name == error_mark_node)
15040 return error_mark_node;
15042 /* A template-id has already been resolved; there is no lookup to
15044 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15046 if (BASELINK_P (name))
15048 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15049 == TEMPLATE_ID_EXPR);
15053 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15054 it should already have been checked to make sure that the name
15055 used matches the type being destroyed. */
15056 if (TREE_CODE (name) == BIT_NOT_EXPR)
15060 /* Figure out to which type this destructor applies. */
15062 type = parser->scope;
15063 else if (object_type)
15064 type = object_type;
15066 type = current_class_type;
15067 /* If that's not a class type, there is no destructor. */
15068 if (!type || !CLASS_TYPE_P (type))
15069 return error_mark_node;
15070 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15071 lazily_declare_fn (sfk_destructor, type);
15072 if (!CLASSTYPE_DESTRUCTORS (type))
15073 return error_mark_node;
15074 /* If it was a class type, return the destructor. */
15075 return CLASSTYPE_DESTRUCTORS (type);
15078 /* By this point, the NAME should be an ordinary identifier. If
15079 the id-expression was a qualified name, the qualifying scope is
15080 stored in PARSER->SCOPE at this point. */
15081 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15083 /* Perform the lookup. */
15088 if (parser->scope == error_mark_node)
15089 return error_mark_node;
15091 /* If the SCOPE is dependent, the lookup must be deferred until
15092 the template is instantiated -- unless we are explicitly
15093 looking up names in uninstantiated templates. Even then, we
15094 cannot look up the name if the scope is not a class type; it
15095 might, for example, be a template type parameter. */
15096 dependent_p = (TYPE_P (parser->scope)
15097 && !(parser->in_declarator_p
15098 && currently_open_class (parser->scope))
15099 && dependent_type_p (parser->scope));
15100 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15107 /* The resolution to Core Issue 180 says that `struct
15108 A::B' should be considered a type-name, even if `A'
15110 type = make_typename_type (parser->scope, name, tag_type,
15111 /*complain=*/tf_error);
15112 decl = TYPE_NAME (type);
15114 else if (is_template
15115 && (cp_parser_next_token_ends_template_argument_p (parser)
15116 || cp_lexer_next_token_is (parser->lexer,
15118 decl = make_unbound_class_template (parser->scope,
15120 /*complain=*/tf_error);
15122 decl = build_qualified_name (/*type=*/NULL_TREE,
15123 parser->scope, name,
15128 tree pushed_scope = NULL_TREE;
15130 /* If PARSER->SCOPE is a dependent type, then it must be a
15131 class type, and we must not be checking dependencies;
15132 otherwise, we would have processed this lookup above. So
15133 that PARSER->SCOPE is not considered a dependent base by
15134 lookup_member, we must enter the scope here. */
15136 pushed_scope = push_scope (parser->scope);
15137 /* If the PARSER->SCOPE is a template specialization, it
15138 may be instantiated during name lookup. In that case,
15139 errors may be issued. Even if we rollback the current
15140 tentative parse, those errors are valid. */
15141 decl = lookup_qualified_name (parser->scope, name,
15142 tag_type != none_type,
15143 /*complain=*/true);
15145 pop_scope (pushed_scope);
15147 parser->qualifying_scope = parser->scope;
15148 parser->object_scope = NULL_TREE;
15150 else if (object_type)
15152 tree object_decl = NULL_TREE;
15153 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15154 OBJECT_TYPE is not a class. */
15155 if (CLASS_TYPE_P (object_type))
15156 /* If the OBJECT_TYPE is a template specialization, it may
15157 be instantiated during name lookup. In that case, errors
15158 may be issued. Even if we rollback the current tentative
15159 parse, those errors are valid. */
15160 object_decl = lookup_member (object_type,
15163 tag_type != none_type);
15164 /* Look it up in the enclosing context, too. */
15165 decl = lookup_name_real (name, tag_type != none_type,
15167 /*block_p=*/true, is_namespace, flags);
15168 parser->object_scope = object_type;
15169 parser->qualifying_scope = NULL_TREE;
15171 decl = object_decl;
15175 decl = lookup_name_real (name, tag_type != none_type,
15177 /*block_p=*/true, is_namespace, flags);
15178 parser->qualifying_scope = NULL_TREE;
15179 parser->object_scope = NULL_TREE;
15182 /* If the lookup failed, let our caller know. */
15183 if (!decl || decl == error_mark_node)
15184 return error_mark_node;
15186 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15187 if (TREE_CODE (decl) == TREE_LIST)
15189 if (ambiguous_decls)
15190 *ambiguous_decls = decl;
15191 /* The error message we have to print is too complicated for
15192 cp_parser_error, so we incorporate its actions directly. */
15193 if (!cp_parser_simulate_error (parser))
15195 error ("reference to %qD is ambiguous", name);
15196 print_candidates (decl);
15198 return error_mark_node;
15201 gcc_assert (DECL_P (decl)
15202 || TREE_CODE (decl) == OVERLOAD
15203 || TREE_CODE (decl) == SCOPE_REF
15204 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15205 || BASELINK_P (decl));
15207 /* If we have resolved the name of a member declaration, check to
15208 see if the declaration is accessible. When the name resolves to
15209 set of overloaded functions, accessibility is checked when
15210 overload resolution is done.
15212 During an explicit instantiation, access is not checked at all,
15213 as per [temp.explicit]. */
15215 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15220 /* Like cp_parser_lookup_name, but for use in the typical case where
15221 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15222 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15225 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15227 return cp_parser_lookup_name (parser, name,
15229 /*is_template=*/false,
15230 /*is_namespace=*/false,
15231 /*check_dependency=*/true,
15232 /*ambiguous_decls=*/NULL);
15235 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15236 the current context, return the TYPE_DECL. If TAG_NAME_P is
15237 true, the DECL indicates the class being defined in a class-head,
15238 or declared in an elaborated-type-specifier.
15240 Otherwise, return DECL. */
15243 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15245 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15246 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15249 template <typename T> struct B;
15252 template <typename T> struct A::B {};
15254 Similarly, in an elaborated-type-specifier:
15256 namespace N { struct X{}; }
15259 template <typename T> friend struct N::X;
15262 However, if the DECL refers to a class type, and we are in
15263 the scope of the class, then the name lookup automatically
15264 finds the TYPE_DECL created by build_self_reference rather
15265 than a TEMPLATE_DECL. For example, in:
15267 template <class T> struct S {
15271 there is no need to handle such case. */
15273 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15274 return DECL_TEMPLATE_RESULT (decl);
15279 /* If too many, or too few, template-parameter lists apply to the
15280 declarator, issue an error message. Returns TRUE if all went well,
15281 and FALSE otherwise. */
15284 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15285 cp_declarator *declarator)
15287 unsigned num_templates;
15289 /* We haven't seen any classes that involve template parameters yet. */
15292 switch (declarator->kind)
15295 if (declarator->u.id.qualifying_scope)
15300 scope = declarator->u.id.qualifying_scope;
15301 member = declarator->u.id.unqualified_name;
15303 while (scope && CLASS_TYPE_P (scope))
15305 /* You're supposed to have one `template <...>'
15306 for every template class, but you don't need one
15307 for a full specialization. For example:
15309 template <class T> struct S{};
15310 template <> struct S<int> { void f(); };
15311 void S<int>::f () {}
15313 is correct; there shouldn't be a `template <>' for
15314 the definition of `S<int>::f'. */
15315 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15316 /* If SCOPE does not have template information of any
15317 kind, then it is not a template, nor is it nested
15318 within a template. */
15320 if (explicit_class_specialization_p (scope))
15322 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15325 scope = TYPE_CONTEXT (scope);
15328 else if (TREE_CODE (declarator->u.id.unqualified_name)
15329 == TEMPLATE_ID_EXPR)
15330 /* If the DECLARATOR has the form `X<y>' then it uses one
15331 additional level of template parameters. */
15334 return cp_parser_check_template_parameters (parser,
15340 case cdk_reference:
15342 return (cp_parser_check_declarator_template_parameters
15343 (parser, declarator->declarator));
15349 gcc_unreachable ();
15354 /* NUM_TEMPLATES were used in the current declaration. If that is
15355 invalid, return FALSE and issue an error messages. Otherwise,
15359 cp_parser_check_template_parameters (cp_parser* parser,
15360 unsigned num_templates)
15362 /* If there are more template classes than parameter lists, we have
15365 template <class T> void S<T>::R<T>::f (); */
15366 if (parser->num_template_parameter_lists < num_templates)
15368 error ("too few template-parameter-lists");
15371 /* If there are the same number of template classes and parameter
15372 lists, that's OK. */
15373 if (parser->num_template_parameter_lists == num_templates)
15375 /* If there are more, but only one more, then we are referring to a
15376 member template. That's OK too. */
15377 if (parser->num_template_parameter_lists == num_templates + 1)
15379 /* Otherwise, there are too many template parameter lists. We have
15382 template <class T> template <class U> void S::f(); */
15383 error ("too many template-parameter-lists");
15387 /* Parse an optional `::' token indicating that the following name is
15388 from the global namespace. If so, PARSER->SCOPE is set to the
15389 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15390 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15391 Returns the new value of PARSER->SCOPE, if the `::' token is
15392 present, and NULL_TREE otherwise. */
15395 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15399 /* Peek at the next token. */
15400 token = cp_lexer_peek_token (parser->lexer);
15401 /* If we're looking at a `::' token then we're starting from the
15402 global namespace, not our current location. */
15403 if (token->type == CPP_SCOPE)
15405 /* Consume the `::' token. */
15406 cp_lexer_consume_token (parser->lexer);
15407 /* Set the SCOPE so that we know where to start the lookup. */
15408 parser->scope = global_namespace;
15409 parser->qualifying_scope = global_namespace;
15410 parser->object_scope = NULL_TREE;
15412 return parser->scope;
15414 else if (!current_scope_valid_p)
15416 parser->scope = NULL_TREE;
15417 parser->qualifying_scope = NULL_TREE;
15418 parser->object_scope = NULL_TREE;
15424 /* Returns TRUE if the upcoming token sequence is the start of a
15425 constructor declarator. If FRIEND_P is true, the declarator is
15426 preceded by the `friend' specifier. */
15429 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15431 bool constructor_p;
15432 tree type_decl = NULL_TREE;
15433 bool nested_name_p;
15434 cp_token *next_token;
15436 /* The common case is that this is not a constructor declarator, so
15437 try to avoid doing lots of work if at all possible. It's not
15438 valid declare a constructor at function scope. */
15439 if (parser->in_function_body)
15441 /* And only certain tokens can begin a constructor declarator. */
15442 next_token = cp_lexer_peek_token (parser->lexer);
15443 if (next_token->type != CPP_NAME
15444 && next_token->type != CPP_SCOPE
15445 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15446 && next_token->type != CPP_TEMPLATE_ID)
15449 /* Parse tentatively; we are going to roll back all of the tokens
15451 cp_parser_parse_tentatively (parser);
15452 /* Assume that we are looking at a constructor declarator. */
15453 constructor_p = true;
15455 /* Look for the optional `::' operator. */
15456 cp_parser_global_scope_opt (parser,
15457 /*current_scope_valid_p=*/false);
15458 /* Look for the nested-name-specifier. */
15460 = (cp_parser_nested_name_specifier_opt (parser,
15461 /*typename_keyword_p=*/false,
15462 /*check_dependency_p=*/false,
15464 /*is_declaration=*/false)
15466 /* Outside of a class-specifier, there must be a
15467 nested-name-specifier. */
15468 if (!nested_name_p &&
15469 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15471 constructor_p = false;
15472 /* If we still think that this might be a constructor-declarator,
15473 look for a class-name. */
15478 template <typename T> struct S { S(); };
15479 template <typename T> S<T>::S ();
15481 we must recognize that the nested `S' names a class.
15484 template <typename T> S<T>::S<T> ();
15486 we must recognize that the nested `S' names a template. */
15487 type_decl = cp_parser_class_name (parser,
15488 /*typename_keyword_p=*/false,
15489 /*template_keyword_p=*/false,
15491 /*check_dependency_p=*/false,
15492 /*class_head_p=*/false,
15493 /*is_declaration=*/false);
15494 /* If there was no class-name, then this is not a constructor. */
15495 constructor_p = !cp_parser_error_occurred (parser);
15498 /* If we're still considering a constructor, we have to see a `(',
15499 to begin the parameter-declaration-clause, followed by either a
15500 `)', an `...', or a decl-specifier. We need to check for a
15501 type-specifier to avoid being fooled into thinking that:
15505 is a constructor. (It is actually a function named `f' that
15506 takes one parameter (of type `int') and returns a value of type
15509 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15512 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15513 /* A parameter declaration begins with a decl-specifier,
15514 which is either the "attribute" keyword, a storage class
15515 specifier, or (usually) a type-specifier. */
15516 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15519 tree pushed_scope = NULL_TREE;
15520 unsigned saved_num_template_parameter_lists;
15522 /* Names appearing in the type-specifier should be looked up
15523 in the scope of the class. */
15524 if (current_class_type)
15528 type = TREE_TYPE (type_decl);
15529 if (TREE_CODE (type) == TYPENAME_TYPE)
15531 type = resolve_typename_type (type,
15532 /*only_current_p=*/false);
15533 if (type == error_mark_node)
15535 cp_parser_abort_tentative_parse (parser);
15539 pushed_scope = push_scope (type);
15542 /* Inside the constructor parameter list, surrounding
15543 template-parameter-lists do not apply. */
15544 saved_num_template_parameter_lists
15545 = parser->num_template_parameter_lists;
15546 parser->num_template_parameter_lists = 0;
15548 /* Look for the type-specifier. */
15549 cp_parser_type_specifier (parser,
15550 CP_PARSER_FLAGS_NONE,
15551 /*decl_specs=*/NULL,
15552 /*is_declarator=*/true,
15553 /*declares_class_or_enum=*/NULL,
15554 /*is_cv_qualifier=*/NULL);
15556 parser->num_template_parameter_lists
15557 = saved_num_template_parameter_lists;
15559 /* Leave the scope of the class. */
15561 pop_scope (pushed_scope);
15563 constructor_p = !cp_parser_error_occurred (parser);
15567 constructor_p = false;
15568 /* We did not really want to consume any tokens. */
15569 cp_parser_abort_tentative_parse (parser);
15571 return constructor_p;
15574 /* Parse the definition of the function given by the DECL_SPECIFIERS,
15575 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
15576 they must be performed once we are in the scope of the function.
15578 Returns the function defined. */
15581 cp_parser_function_definition_from_specifiers_and_declarator
15582 (cp_parser* parser,
15583 cp_decl_specifier_seq *decl_specifiers,
15585 const cp_declarator *declarator)
15590 /* Begin the function-definition. */
15591 success_p = start_function (decl_specifiers, declarator, attributes);
15593 /* The things we're about to see are not directly qualified by any
15594 template headers we've seen thus far. */
15595 reset_specialization ();
15597 /* If there were names looked up in the decl-specifier-seq that we
15598 did not check, check them now. We must wait until we are in the
15599 scope of the function to perform the checks, since the function
15600 might be a friend. */
15601 perform_deferred_access_checks ();
15605 /* Skip the entire function. */
15606 cp_parser_skip_to_end_of_block_or_statement (parser);
15607 fn = error_mark_node;
15609 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
15611 /* Seen already, skip it. An error message has already been output. */
15612 cp_parser_skip_to_end_of_block_or_statement (parser);
15613 fn = current_function_decl;
15614 current_function_decl = NULL_TREE;
15615 /* If this is a function from a class, pop the nested class. */
15616 if (current_class_name)
15617 pop_nested_class ();
15620 fn = cp_parser_function_definition_after_declarator (parser,
15621 /*inline_p=*/false);
15626 /* Parse the part of a function-definition that follows the
15627 declarator. INLINE_P is TRUE iff this function is an inline
15628 function defined with a class-specifier.
15630 Returns the function defined. */
15633 cp_parser_function_definition_after_declarator (cp_parser* parser,
15637 bool ctor_initializer_p = false;
15638 bool saved_in_unbraced_linkage_specification_p;
15639 bool saved_in_function_body;
15640 unsigned saved_num_template_parameter_lists;
15642 saved_in_function_body = parser->in_function_body;
15643 parser->in_function_body = true;
15644 /* If the next token is `return', then the code may be trying to
15645 make use of the "named return value" extension that G++ used to
15647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15649 /* Consume the `return' keyword. */
15650 cp_lexer_consume_token (parser->lexer);
15651 /* Look for the identifier that indicates what value is to be
15653 cp_parser_identifier (parser);
15654 /* Issue an error message. */
15655 error ("named return values are no longer supported");
15656 /* Skip tokens until we reach the start of the function body. */
15659 cp_token *token = cp_lexer_peek_token (parser->lexer);
15660 if (token->type == CPP_OPEN_BRACE
15661 || token->type == CPP_EOF
15662 || token->type == CPP_PRAGMA_EOL)
15664 cp_lexer_consume_token (parser->lexer);
15667 /* The `extern' in `extern "C" void f () { ... }' does not apply to
15668 anything declared inside `f'. */
15669 saved_in_unbraced_linkage_specification_p
15670 = parser->in_unbraced_linkage_specification_p;
15671 parser->in_unbraced_linkage_specification_p = false;
15672 /* Inside the function, surrounding template-parameter-lists do not
15674 saved_num_template_parameter_lists
15675 = parser->num_template_parameter_lists;
15676 parser->num_template_parameter_lists = 0;
15677 /* If the next token is `try', then we are looking at a
15678 function-try-block. */
15679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15680 ctor_initializer_p = cp_parser_function_try_block (parser);
15681 /* A function-try-block includes the function-body, so we only do
15682 this next part if we're not processing a function-try-block. */
15685 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15687 /* Finish the function. */
15688 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15689 (inline_p ? 2 : 0));
15690 /* Generate code for it, if necessary. */
15691 expand_or_defer_fn (fn);
15692 /* Restore the saved values. */
15693 parser->in_unbraced_linkage_specification_p
15694 = saved_in_unbraced_linkage_specification_p;
15695 parser->num_template_parameter_lists
15696 = saved_num_template_parameter_lists;
15697 parser->in_function_body = saved_in_function_body;
15702 /* Parse a template-declaration, assuming that the `export' (and
15703 `extern') keywords, if present, has already been scanned. MEMBER_P
15704 is as for cp_parser_template_declaration. */
15707 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15709 tree decl = NULL_TREE;
15711 tree parameter_list;
15712 bool friend_p = false;
15713 bool need_lang_pop;
15715 /* Look for the `template' keyword. */
15716 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15720 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15722 if (at_class_scope_p () && current_function_decl)
15724 /* 14.5.2.2 [temp.mem]
15726 A local class shall not have member templates. */
15727 error ("invalid declaration of member template in local class");
15728 cp_parser_skip_to_end_of_block_or_statement (parser);
15733 A template ... shall not have C linkage. */
15734 if (current_lang_name == lang_name_c)
15736 error ("template with C linkage");
15737 /* Give it C++ linkage to avoid confusing other parts of the
15739 push_lang_context (lang_name_cplusplus);
15740 need_lang_pop = true;
15743 need_lang_pop = false;
15745 /* We cannot perform access checks on the template parameter
15746 declarations until we know what is being declared, just as we
15747 cannot check the decl-specifier list. */
15748 push_deferring_access_checks (dk_deferred);
15750 /* If the next token is `>', then we have an invalid
15751 specialization. Rather than complain about an invalid template
15752 parameter, issue an error message here. */
15753 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15755 cp_parser_error (parser, "invalid explicit specialization");
15756 begin_specialization ();
15757 parameter_list = NULL_TREE;
15760 /* Parse the template parameters. */
15761 parameter_list = cp_parser_template_parameter_list (parser);
15763 /* Get the deferred access checks from the parameter list. These
15764 will be checked once we know what is being declared, as for a
15765 member template the checks must be performed in the scope of the
15766 class containing the member. */
15767 checks = get_deferred_access_checks ();
15769 /* Look for the `>'. */
15770 cp_parser_skip_to_end_of_template_parameter_list (parser);
15771 /* We just processed one more parameter list. */
15772 ++parser->num_template_parameter_lists;
15773 /* If the next token is `template', there are more template
15775 if (cp_lexer_next_token_is_keyword (parser->lexer,
15777 cp_parser_template_declaration_after_export (parser, member_p);
15780 /* There are no access checks when parsing a template, as we do not
15781 know if a specialization will be a friend. */
15782 push_deferring_access_checks (dk_no_check);
15783 decl = cp_parser_single_declaration (parser,
15787 pop_deferring_access_checks ();
15789 /* If this is a member template declaration, let the front
15791 if (member_p && !friend_p && decl)
15793 if (TREE_CODE (decl) == TYPE_DECL)
15794 cp_parser_check_access_in_redeclaration (decl);
15796 decl = finish_member_template_decl (decl);
15798 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15799 make_friend_class (current_class_type, TREE_TYPE (decl),
15800 /*complain=*/true);
15802 /* We are done with the current parameter list. */
15803 --parser->num_template_parameter_lists;
15805 pop_deferring_access_checks ();
15808 finish_template_decl (parameter_list);
15810 /* Register member declarations. */
15811 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15812 finish_member_declaration (decl);
15813 /* For the erroneous case of a template with C linkage, we pushed an
15814 implicit C++ linkage scope; exit that scope now. */
15816 pop_lang_context ();
15817 /* If DECL is a function template, we must return to parse it later.
15818 (Even though there is no definition, there might be default
15819 arguments that need handling.) */
15820 if (member_p && decl
15821 && (TREE_CODE (decl) == FUNCTION_DECL
15822 || DECL_FUNCTION_TEMPLATE_P (decl)))
15823 TREE_VALUE (parser->unparsed_functions_queues)
15824 = tree_cons (NULL_TREE, decl,
15825 TREE_VALUE (parser->unparsed_functions_queues));
15828 /* Perform the deferred access checks from a template-parameter-list.
15829 CHECKS is a TREE_LIST of access checks, as returned by
15830 get_deferred_access_checks. */
15833 cp_parser_perform_template_parameter_access_checks (tree checks)
15835 ++processing_template_parmlist;
15836 perform_access_checks (checks);
15837 --processing_template_parmlist;
15840 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15841 `function-definition' sequence. MEMBER_P is true, this declaration
15842 appears in a class scope.
15844 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
15845 *FRIEND_P is set to TRUE iff the declaration is a friend. */
15848 cp_parser_single_declaration (cp_parser* parser,
15853 int declares_class_or_enum;
15854 tree decl = NULL_TREE;
15855 cp_decl_specifier_seq decl_specifiers;
15856 bool function_definition_p = false;
15858 /* This function is only used when processing a template
15860 gcc_assert (innermost_scope_kind () == sk_template_parms
15861 || innermost_scope_kind () == sk_template_spec);
15863 /* Defer access checks until we know what is being declared. */
15864 push_deferring_access_checks (dk_deferred);
15866 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15868 cp_parser_decl_specifier_seq (parser,
15869 CP_PARSER_FLAGS_OPTIONAL,
15871 &declares_class_or_enum);
15873 *friend_p = cp_parser_friend_p (&decl_specifiers);
15875 /* There are no template typedefs. */
15876 if (decl_specifiers.specs[(int) ds_typedef])
15878 error ("template declaration of %qs", "typedef");
15879 decl = error_mark_node;
15882 /* Gather up the access checks that occurred the
15883 decl-specifier-seq. */
15884 stop_deferring_access_checks ();
15886 /* Check for the declaration of a template class. */
15887 if (declares_class_or_enum)
15889 if (cp_parser_declares_only_class_p (parser))
15891 decl = shadow_tag (&decl_specifiers);
15896 friend template <typename T> struct A<T>::B;
15899 A<T>::B will be represented by a TYPENAME_TYPE, and
15900 therefore not recognized by shadow_tag. */
15901 if (friend_p && *friend_p
15903 && decl_specifiers.type
15904 && TYPE_P (decl_specifiers.type))
15905 decl = decl_specifiers.type;
15907 if (decl && decl != error_mark_node)
15908 decl = TYPE_NAME (decl);
15910 decl = error_mark_node;
15912 /* Perform access checks for template parameters. */
15913 cp_parser_perform_template_parameter_access_checks (checks);
15916 /* If it's not a template class, try for a template function. If
15917 the next token is a `;', then this declaration does not declare
15918 anything. But, if there were errors in the decl-specifiers, then
15919 the error might well have come from an attempted class-specifier.
15920 In that case, there's no need to warn about a missing declarator. */
15922 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15923 || decl_specifiers.type != error_mark_node))
15924 decl = cp_parser_init_declarator (parser,
15927 /*function_definition_allowed_p=*/true,
15929 declares_class_or_enum,
15930 &function_definition_p);
15932 pop_deferring_access_checks ();
15934 /* Clear any current qualification; whatever comes next is the start
15935 of something new. */
15936 parser->scope = NULL_TREE;
15937 parser->qualifying_scope = NULL_TREE;
15938 parser->object_scope = NULL_TREE;
15939 /* Look for a trailing `;' after the declaration. */
15940 if (!function_definition_p
15941 && (decl == error_mark_node
15942 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15943 cp_parser_skip_to_end_of_block_or_statement (parser);
15948 /* Parse a cast-expression that is not the operand of a unary "&". */
15951 cp_parser_simple_cast_expression (cp_parser *parser)
15953 return cp_parser_cast_expression (parser, /*address_p=*/false,
15957 /* Parse a functional cast to TYPE. Returns an expression
15958 representing the cast. */
15961 cp_parser_functional_cast (cp_parser* parser, tree type)
15963 tree expression_list;
15967 = cp_parser_parenthesized_expression_list (parser, false,
15969 /*non_constant_p=*/NULL);
15971 cast = build_functional_cast (type, expression_list);
15972 /* [expr.const]/1: In an integral constant expression "only type
15973 conversions to integral or enumeration type can be used". */
15974 if (TREE_CODE (type) == TYPE_DECL)
15975 type = TREE_TYPE (type);
15976 if (cast != error_mark_node
15977 && !cast_valid_in_integral_constant_expression_p (type)
15978 && (cp_parser_non_integral_constant_expression
15979 (parser, "a call to a constructor")))
15980 return error_mark_node;
15984 /* Save the tokens that make up the body of a member function defined
15985 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15986 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15987 specifiers applied to the declaration. Returns the FUNCTION_DECL
15988 for the member function. */
15991 cp_parser_save_member_function_body (cp_parser* parser,
15992 cp_decl_specifier_seq *decl_specifiers,
15993 cp_declarator *declarator,
16000 /* Create the function-declaration. */
16001 fn = start_method (decl_specifiers, declarator, attributes);
16002 /* If something went badly wrong, bail out now. */
16003 if (fn == error_mark_node)
16005 /* If there's a function-body, skip it. */
16006 if (cp_parser_token_starts_function_definition_p
16007 (cp_lexer_peek_token (parser->lexer)))
16008 cp_parser_skip_to_end_of_block_or_statement (parser);
16009 return error_mark_node;
16012 /* Remember it, if there default args to post process. */
16013 cp_parser_save_default_args (parser, fn);
16015 /* Save away the tokens that make up the body of the
16017 first = parser->lexer->next_token;
16018 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16019 /* Handle function try blocks. */
16020 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16021 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16022 last = parser->lexer->next_token;
16024 /* Save away the inline definition; we will process it when the
16025 class is complete. */
16026 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16027 DECL_PENDING_INLINE_P (fn) = 1;
16029 /* We need to know that this was defined in the class, so that
16030 friend templates are handled correctly. */
16031 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16033 /* We're done with the inline definition. */
16034 finish_method (fn);
16036 /* Add FN to the queue of functions to be parsed later. */
16037 TREE_VALUE (parser->unparsed_functions_queues)
16038 = tree_cons (NULL_TREE, fn,
16039 TREE_VALUE (parser->unparsed_functions_queues));
16044 /* Parse a template-argument-list, as well as the trailing ">" (but
16045 not the opening ">"). See cp_parser_template_argument_list for the
16049 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16053 tree saved_qualifying_scope;
16054 tree saved_object_scope;
16055 bool saved_greater_than_is_operator_p;
16056 bool saved_skip_evaluation;
16060 When parsing a template-id, the first non-nested `>' is taken as
16061 the end of the template-argument-list rather than a greater-than
16063 saved_greater_than_is_operator_p
16064 = parser->greater_than_is_operator_p;
16065 parser->greater_than_is_operator_p = false;
16066 /* Parsing the argument list may modify SCOPE, so we save it
16068 saved_scope = parser->scope;
16069 saved_qualifying_scope = parser->qualifying_scope;
16070 saved_object_scope = parser->object_scope;
16071 /* We need to evaluate the template arguments, even though this
16072 template-id may be nested within a "sizeof". */
16073 saved_skip_evaluation = skip_evaluation;
16074 skip_evaluation = false;
16075 /* Parse the template-argument-list itself. */
16076 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16077 arguments = NULL_TREE;
16079 arguments = cp_parser_template_argument_list (parser);
16080 /* Look for the `>' that ends the template-argument-list. If we find
16081 a '>>' instead, it's probably just a typo. */
16082 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16084 if (!saved_greater_than_is_operator_p)
16086 /* If we're in a nested template argument list, the '>>' has
16087 to be a typo for '> >'. We emit the error message, but we
16088 continue parsing and we push a '>' as next token, so that
16089 the argument list will be parsed correctly. Note that the
16090 global source location is still on the token before the
16091 '>>', so we need to say explicitly where we want it. */
16092 cp_token *token = cp_lexer_peek_token (parser->lexer);
16093 error ("%H%<>>%> should be %<> >%> "
16094 "within a nested template argument list",
16097 /* ??? Proper recovery should terminate two levels of
16098 template argument list here. */
16099 token->type = CPP_GREATER;
16103 /* If this is not a nested template argument list, the '>>'
16104 is a typo for '>'. Emit an error message and continue.
16105 Same deal about the token location, but here we can get it
16106 right by consuming the '>>' before issuing the diagnostic. */
16107 cp_lexer_consume_token (parser->lexer);
16108 error ("spurious %<>>%>, use %<>%> to terminate "
16109 "a template argument list");
16113 cp_parser_skip_to_end_of_template_parameter_list (parser);
16114 /* The `>' token might be a greater-than operator again now. */
16115 parser->greater_than_is_operator_p
16116 = saved_greater_than_is_operator_p;
16117 /* Restore the SAVED_SCOPE. */
16118 parser->scope = saved_scope;
16119 parser->qualifying_scope = saved_qualifying_scope;
16120 parser->object_scope = saved_object_scope;
16121 skip_evaluation = saved_skip_evaluation;
16126 /* MEMBER_FUNCTION is a member function, or a friend. If default
16127 arguments, or the body of the function have not yet been parsed,
16131 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16133 /* If this member is a template, get the underlying
16135 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16136 member_function = DECL_TEMPLATE_RESULT (member_function);
16138 /* There should not be any class definitions in progress at this
16139 point; the bodies of members are only parsed outside of all class
16141 gcc_assert (parser->num_classes_being_defined == 0);
16142 /* While we're parsing the member functions we might encounter more
16143 classes. We want to handle them right away, but we don't want
16144 them getting mixed up with functions that are currently in the
16146 parser->unparsed_functions_queues
16147 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16149 /* Make sure that any template parameters are in scope. */
16150 maybe_begin_member_template_processing (member_function);
16152 /* If the body of the function has not yet been parsed, parse it
16154 if (DECL_PENDING_INLINE_P (member_function))
16156 tree function_scope;
16157 cp_token_cache *tokens;
16159 /* The function is no longer pending; we are processing it. */
16160 tokens = DECL_PENDING_INLINE_INFO (member_function);
16161 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16162 DECL_PENDING_INLINE_P (member_function) = 0;
16164 /* If this is a local class, enter the scope of the containing
16166 function_scope = current_function_decl;
16167 if (function_scope)
16168 push_function_context_to (function_scope);
16171 /* Push the body of the function onto the lexer stack. */
16172 cp_parser_push_lexer_for_tokens (parser, tokens);
16174 /* Let the front end know that we going to be defining this
16176 start_preparsed_function (member_function, NULL_TREE,
16177 SF_PRE_PARSED | SF_INCLASS_INLINE);
16179 /* Don't do access checking if it is a templated function. */
16180 if (processing_template_decl)
16181 push_deferring_access_checks (dk_no_check);
16183 /* Now, parse the body of the function. */
16184 cp_parser_function_definition_after_declarator (parser,
16185 /*inline_p=*/true);
16187 if (processing_template_decl)
16188 pop_deferring_access_checks ();
16190 /* Leave the scope of the containing function. */
16191 if (function_scope)
16192 pop_function_context_from (function_scope);
16193 cp_parser_pop_lexer (parser);
16196 /* Remove any template parameters from the symbol table. */
16197 maybe_end_member_template_processing ();
16199 /* Restore the queue. */
16200 parser->unparsed_functions_queues
16201 = TREE_CHAIN (parser->unparsed_functions_queues);
16204 /* If DECL contains any default args, remember it on the unparsed
16205 functions queue. */
16208 cp_parser_save_default_args (cp_parser* parser, tree decl)
16212 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16214 probe = TREE_CHAIN (probe))
16215 if (TREE_PURPOSE (probe))
16217 TREE_PURPOSE (parser->unparsed_functions_queues)
16218 = tree_cons (current_class_type, decl,
16219 TREE_PURPOSE (parser->unparsed_functions_queues));
16224 /* FN is a FUNCTION_DECL which may contains a parameter with an
16225 unparsed DEFAULT_ARG. Parse the default args now. This function
16226 assumes that the current scope is the scope in which the default
16227 argument should be processed. */
16230 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16232 bool saved_local_variables_forbidden_p;
16235 /* While we're parsing the default args, we might (due to the
16236 statement expression extension) encounter more classes. We want
16237 to handle them right away, but we don't want them getting mixed
16238 up with default args that are currently in the queue. */
16239 parser->unparsed_functions_queues
16240 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16242 /* Local variable names (and the `this' keyword) may not appear
16243 in a default argument. */
16244 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16245 parser->local_variables_forbidden_p = true;
16247 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16249 parm = TREE_CHAIN (parm))
16251 cp_token_cache *tokens;
16252 tree default_arg = TREE_PURPOSE (parm);
16254 VEC(tree,gc) *insts;
16261 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16262 /* This can happen for a friend declaration for a function
16263 already declared with default arguments. */
16266 /* Push the saved tokens for the default argument onto the parser's
16268 tokens = DEFARG_TOKENS (default_arg);
16269 cp_parser_push_lexer_for_tokens (parser, tokens);
16271 /* Parse the assignment-expression. */
16272 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16274 if (!processing_template_decl)
16275 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16277 TREE_PURPOSE (parm) = parsed_arg;
16279 /* Update any instantiations we've already created. */
16280 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16281 VEC_iterate (tree, insts, ix, copy); ix++)
16282 TREE_PURPOSE (copy) = parsed_arg;
16284 /* If the token stream has not been completely used up, then
16285 there was extra junk after the end of the default
16287 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16288 cp_parser_error (parser, "expected %<,%>");
16290 /* Revert to the main lexer. */
16291 cp_parser_pop_lexer (parser);
16294 /* Make sure no default arg is missing. */
16295 check_default_args (fn);
16297 /* Restore the state of local_variables_forbidden_p. */
16298 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16300 /* Restore the queue. */
16301 parser->unparsed_functions_queues
16302 = TREE_CHAIN (parser->unparsed_functions_queues);
16305 /* Parse the operand of `sizeof' (or a similar operator). Returns
16306 either a TYPE or an expression, depending on the form of the
16307 input. The KEYWORD indicates which kind of expression we have
16311 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16313 static const char *format;
16314 tree expr = NULL_TREE;
16315 const char *saved_message;
16316 bool saved_integral_constant_expression_p;
16317 bool saved_non_integral_constant_expression_p;
16319 /* Initialize FORMAT the first time we get here. */
16321 format = "types may not be defined in '%s' expressions";
16323 /* Types cannot be defined in a `sizeof' expression. Save away the
16325 saved_message = parser->type_definition_forbidden_message;
16326 /* And create the new one. */
16327 parser->type_definition_forbidden_message
16328 = XNEWVEC (const char, strlen (format)
16329 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16331 sprintf ((char *) parser->type_definition_forbidden_message,
16332 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16334 /* The restrictions on constant-expressions do not apply inside
16335 sizeof expressions. */
16336 saved_integral_constant_expression_p
16337 = parser->integral_constant_expression_p;
16338 saved_non_integral_constant_expression_p
16339 = parser->non_integral_constant_expression_p;
16340 parser->integral_constant_expression_p = false;
16342 /* Do not actually evaluate the expression. */
16344 /* If it's a `(', then we might be looking at the type-id
16346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16349 bool saved_in_type_id_in_expr_p;
16351 /* We can't be sure yet whether we're looking at a type-id or an
16353 cp_parser_parse_tentatively (parser);
16354 /* Consume the `('. */
16355 cp_lexer_consume_token (parser->lexer);
16356 /* Parse the type-id. */
16357 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16358 parser->in_type_id_in_expr_p = true;
16359 type = cp_parser_type_id (parser);
16360 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16361 /* Now, look for the trailing `)'. */
16362 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16363 /* If all went well, then we're done. */
16364 if (cp_parser_parse_definitely (parser))
16366 cp_decl_specifier_seq decl_specs;
16368 /* Build a trivial decl-specifier-seq. */
16369 clear_decl_specs (&decl_specs);
16370 decl_specs.type = type;
16372 /* Call grokdeclarator to figure out what type this is. */
16373 expr = grokdeclarator (NULL,
16377 /*attrlist=*/NULL);
16381 /* If the type-id production did not work out, then we must be
16382 looking at the unary-expression production. */
16384 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16386 /* Go back to evaluating expressions. */
16389 /* Free the message we created. */
16390 free ((char *) parser->type_definition_forbidden_message);
16391 /* And restore the old one. */
16392 parser->type_definition_forbidden_message = saved_message;
16393 parser->integral_constant_expression_p
16394 = saved_integral_constant_expression_p;
16395 parser->non_integral_constant_expression_p
16396 = saved_non_integral_constant_expression_p;
16401 /* If the current declaration has no declarator, return true. */
16404 cp_parser_declares_only_class_p (cp_parser *parser)
16406 /* If the next token is a `;' or a `,' then there is no
16408 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16409 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16412 /* Update the DECL_SPECS to reflect the storage class indicated by
16416 cp_parser_set_storage_class (cp_parser *parser,
16417 cp_decl_specifier_seq *decl_specs,
16420 cp_storage_class storage_class;
16422 if (parser->in_unbraced_linkage_specification_p)
16424 error ("invalid use of %qD in linkage specification",
16425 ridpointers[keyword]);
16428 else if (decl_specs->storage_class != sc_none)
16430 decl_specs->conflicting_specifiers_p = true;
16434 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16435 && decl_specs->specs[(int) ds_thread])
16437 error ("%<__thread%> before %qD", ridpointers[keyword]);
16438 decl_specs->specs[(int) ds_thread] = 0;
16444 storage_class = sc_auto;
16447 storage_class = sc_register;
16450 storage_class = sc_static;
16453 storage_class = sc_extern;
16456 storage_class = sc_mutable;
16459 gcc_unreachable ();
16461 decl_specs->storage_class = storage_class;
16463 /* A storage class specifier cannot be applied alongside a typedef
16464 specifier. If there is a typedef specifier present then set
16465 conflicting_specifiers_p which will trigger an error later
16466 on in grokdeclarator. */
16467 if (decl_specs->specs[(int)ds_typedef])
16468 decl_specs->conflicting_specifiers_p = true;
16471 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16472 is true, the type is a user-defined type; otherwise it is a
16473 built-in type specified by a keyword. */
16476 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16478 bool user_defined_p)
16480 decl_specs->any_specifiers_p = true;
16482 /* If the user tries to redeclare bool or wchar_t (with, for
16483 example, in "typedef int wchar_t;") we remember that this is what
16484 happened. In system headers, we ignore these declarations so
16485 that G++ can work with system headers that are not C++-safe. */
16486 if (decl_specs->specs[(int) ds_typedef]
16488 && (type_spec == boolean_type_node
16489 || type_spec == wchar_type_node)
16490 && (decl_specs->type
16491 || decl_specs->specs[(int) ds_long]
16492 || decl_specs->specs[(int) ds_short]
16493 || decl_specs->specs[(int) ds_unsigned]
16494 || decl_specs->specs[(int) ds_signed]))
16496 decl_specs->redefined_builtin_type = type_spec;
16497 if (!decl_specs->type)
16499 decl_specs->type = type_spec;
16500 decl_specs->user_defined_type_p = false;
16503 else if (decl_specs->type)
16504 decl_specs->multiple_types_p = true;
16507 decl_specs->type = type_spec;
16508 decl_specs->user_defined_type_p = user_defined_p;
16509 decl_specs->redefined_builtin_type = NULL_TREE;
16513 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16514 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16517 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16519 return decl_specifiers->specs[(int) ds_friend] != 0;
16522 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16523 issue an error message indicating that TOKEN_DESC was expected.
16525 Returns the token consumed, if the token had the appropriate type.
16526 Otherwise, returns NULL. */
16529 cp_parser_require (cp_parser* parser,
16530 enum cpp_ttype type,
16531 const char* token_desc)
16533 if (cp_lexer_next_token_is (parser->lexer, type))
16534 return cp_lexer_consume_token (parser->lexer);
16537 /* Output the MESSAGE -- unless we're parsing tentatively. */
16538 if (!cp_parser_simulate_error (parser))
16540 char *message = concat ("expected ", token_desc, NULL);
16541 cp_parser_error (parser, message);
16548 /* An error message is produced if the next token is not '>'.
16549 All further tokens are skipped until the desired token is
16550 found or '{', '}', ';' or an unbalanced ')' or ']'. */
16553 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16555 /* Current level of '< ... >'. */
16556 unsigned level = 0;
16557 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
16558 unsigned nesting_depth = 0;
16560 /* Are we ready, yet? If not, issue error message. */
16561 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16564 /* Skip tokens until the desired token is found. */
16567 /* Peek at the next token. */
16568 switch (cp_lexer_peek_token (parser->lexer)->type)
16571 if (!nesting_depth)
16576 if (!nesting_depth && level-- == 0)
16578 /* We've reached the token we want, consume it and stop. */
16579 cp_lexer_consume_token (parser->lexer);
16584 case CPP_OPEN_PAREN:
16585 case CPP_OPEN_SQUARE:
16589 case CPP_CLOSE_PAREN:
16590 case CPP_CLOSE_SQUARE:
16591 if (nesting_depth-- == 0)
16596 case CPP_PRAGMA_EOL:
16597 case CPP_SEMICOLON:
16598 case CPP_OPEN_BRACE:
16599 case CPP_CLOSE_BRACE:
16600 /* The '>' was probably forgotten, don't look further. */
16607 /* Consume this token. */
16608 cp_lexer_consume_token (parser->lexer);
16612 /* If the next token is the indicated keyword, consume it. Otherwise,
16613 issue an error message indicating that TOKEN_DESC was expected.
16615 Returns the token consumed, if the token had the appropriate type.
16616 Otherwise, returns NULL. */
16619 cp_parser_require_keyword (cp_parser* parser,
16621 const char* token_desc)
16623 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16625 if (token && token->keyword != keyword)
16627 dyn_string_t error_msg;
16629 /* Format the error message. */
16630 error_msg = dyn_string_new (0);
16631 dyn_string_append_cstr (error_msg, "expected ");
16632 dyn_string_append_cstr (error_msg, token_desc);
16633 cp_parser_error (parser, error_msg->s);
16634 dyn_string_delete (error_msg);
16641 /* Returns TRUE iff TOKEN is a token that can begin the body of a
16642 function-definition. */
16645 cp_parser_token_starts_function_definition_p (cp_token* token)
16647 return (/* An ordinary function-body begins with an `{'. */
16648 token->type == CPP_OPEN_BRACE
16649 /* A ctor-initializer begins with a `:'. */
16650 || token->type == CPP_COLON
16651 /* A function-try-block begins with `try'. */
16652 || token->keyword == RID_TRY
16653 /* The named return value extension begins with `return'. */
16654 || token->keyword == RID_RETURN);
16657 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
16661 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16665 token = cp_lexer_peek_token (parser->lexer);
16666 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16669 /* Returns TRUE iff the next token is the "," or ">" ending a
16670 template-argument. */
16673 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16677 token = cp_lexer_peek_token (parser->lexer);
16678 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16681 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16682 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
16685 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16690 token = cp_lexer_peek_nth_token (parser->lexer, n);
16691 if (token->type == CPP_LESS)
16693 /* Check for the sequence `<::' in the original code. It would be lexed as
16694 `[:', where `[' is a digraph, and there is no whitespace before
16696 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16699 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16700 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16706 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16707 or none_type otherwise. */
16709 static enum tag_types
16710 cp_parser_token_is_class_key (cp_token* token)
16712 switch (token->keyword)
16717 return record_type;
16726 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
16729 cp_parser_check_class_key (enum tag_types class_key, tree type)
16731 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16732 pedwarn ("%qs tag used in naming %q#T",
16733 class_key == union_type ? "union"
16734 : class_key == record_type ? "struct" : "class",
16738 /* Issue an error message if DECL is redeclared with different
16739 access than its original declaration [class.access.spec/3].
16740 This applies to nested classes and nested class templates.
16744 cp_parser_check_access_in_redeclaration (tree decl)
16746 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16749 if ((TREE_PRIVATE (decl)
16750 != (current_access_specifier == access_private_node))
16751 || (TREE_PROTECTED (decl)
16752 != (current_access_specifier == access_protected_node)))
16753 error ("%qD redeclared with different access", decl);
16756 /* Look for the `template' keyword, as a syntactic disambiguator.
16757 Return TRUE iff it is present, in which case it will be
16761 cp_parser_optional_template_keyword (cp_parser *parser)
16763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16765 /* The `template' keyword can only be used within templates;
16766 outside templates the parser can always figure out what is a
16767 template and what is not. */
16768 if (!processing_template_decl)
16770 error ("%<template%> (as a disambiguator) is only allowed "
16771 "within templates");
16772 /* If this part of the token stream is rescanned, the same
16773 error message would be generated. So, we purge the token
16774 from the stream. */
16775 cp_lexer_purge_token (parser->lexer);
16780 /* Consume the `template' keyword. */
16781 cp_lexer_consume_token (parser->lexer);
16789 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
16790 set PARSER->SCOPE, and perform other related actions. */
16793 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16798 /* Get the stored value. */
16799 value = cp_lexer_consume_token (parser->lexer)->value;
16800 /* Perform any access checks that were deferred. */
16801 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16802 perform_or_defer_access_check (TREE_PURPOSE (check),
16803 TREE_VALUE (check),
16804 TREE_VALUE (check));
16805 /* Set the scope from the stored value. */
16806 parser->scope = TREE_VALUE (value);
16807 parser->qualifying_scope = TREE_TYPE (value);
16808 parser->object_scope = NULL_TREE;
16811 /* Consume tokens up through a non-nested END token. */
16814 cp_parser_cache_group (cp_parser *parser,
16815 enum cpp_ttype end,
16822 /* Abort a parenthesized expression if we encounter a brace. */
16823 if ((end == CPP_CLOSE_PAREN || depth == 0)
16824 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16826 /* If we've reached the end of the file, stop. */
16827 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16828 || (end != CPP_PRAGMA_EOL
16829 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16831 /* Consume the next token. */
16832 token = cp_lexer_consume_token (parser->lexer);
16833 /* See if it starts a new group. */
16834 if (token->type == CPP_OPEN_BRACE)
16836 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16840 else if (token->type == CPP_OPEN_PAREN)
16841 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16842 else if (token->type == CPP_PRAGMA)
16843 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16844 else if (token->type == end)
16849 /* Begin parsing tentatively. We always save tokens while parsing
16850 tentatively so that if the tentative parsing fails we can restore the
16854 cp_parser_parse_tentatively (cp_parser* parser)
16856 /* Enter a new parsing context. */
16857 parser->context = cp_parser_context_new (parser->context);
16858 /* Begin saving tokens. */
16859 cp_lexer_save_tokens (parser->lexer);
16860 /* In order to avoid repetitive access control error messages,
16861 access checks are queued up until we are no longer parsing
16863 push_deferring_access_checks (dk_deferred);
16866 /* Commit to the currently active tentative parse. */
16869 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16871 cp_parser_context *context;
16874 /* Mark all of the levels as committed. */
16875 lexer = parser->lexer;
16876 for (context = parser->context; context->next; context = context->next)
16878 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16880 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16881 while (!cp_lexer_saving_tokens (lexer))
16882 lexer = lexer->next;
16883 cp_lexer_commit_tokens (lexer);
16887 /* Abort the currently active tentative parse. All consumed tokens
16888 will be rolled back, and no diagnostics will be issued. */
16891 cp_parser_abort_tentative_parse (cp_parser* parser)
16893 cp_parser_simulate_error (parser);
16894 /* Now, pretend that we want to see if the construct was
16895 successfully parsed. */
16896 cp_parser_parse_definitely (parser);
16899 /* Stop parsing tentatively. If a parse error has occurred, restore the
16900 token stream. Otherwise, commit to the tokens we have consumed.
16901 Returns true if no error occurred; false otherwise. */
16904 cp_parser_parse_definitely (cp_parser* parser)
16906 bool error_occurred;
16907 cp_parser_context *context;
16909 /* Remember whether or not an error occurred, since we are about to
16910 destroy that information. */
16911 error_occurred = cp_parser_error_occurred (parser);
16912 /* Remove the topmost context from the stack. */
16913 context = parser->context;
16914 parser->context = context->next;
16915 /* If no parse errors occurred, commit to the tentative parse. */
16916 if (!error_occurred)
16918 /* Commit to the tokens read tentatively, unless that was
16920 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16921 cp_lexer_commit_tokens (parser->lexer);
16923 pop_to_parent_deferring_access_checks ();
16925 /* Otherwise, if errors occurred, roll back our state so that things
16926 are just as they were before we began the tentative parse. */
16929 cp_lexer_rollback_tokens (parser->lexer);
16930 pop_deferring_access_checks ();
16932 /* Add the context to the front of the free list. */
16933 context->next = cp_parser_context_free_list;
16934 cp_parser_context_free_list = context;
16936 return !error_occurred;
16939 /* Returns true if we are parsing tentatively and are not committed to
16940 this tentative parse. */
16943 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16945 return (cp_parser_parsing_tentatively (parser)
16946 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16949 /* Returns nonzero iff an error has occurred during the most recent
16950 tentative parse. */
16953 cp_parser_error_occurred (cp_parser* parser)
16955 return (cp_parser_parsing_tentatively (parser)
16956 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16959 /* Returns nonzero if GNU extensions are allowed. */
16962 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16964 return parser->allow_gnu_extensions_p;
16967 /* Objective-C++ Productions */
16970 /* Parse an Objective-C expression, which feeds into a primary-expression
16974 objc-message-expression
16975 objc-string-literal
16976 objc-encode-expression
16977 objc-protocol-expression
16978 objc-selector-expression
16980 Returns a tree representation of the expression. */
16983 cp_parser_objc_expression (cp_parser* parser)
16985 /* Try to figure out what kind of declaration is present. */
16986 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16990 case CPP_OPEN_SQUARE:
16991 return cp_parser_objc_message_expression (parser);
16993 case CPP_OBJC_STRING:
16994 kwd = cp_lexer_consume_token (parser->lexer);
16995 return objc_build_string_object (kwd->value);
16998 switch (kwd->keyword)
17000 case RID_AT_ENCODE:
17001 return cp_parser_objc_encode_expression (parser);
17003 case RID_AT_PROTOCOL:
17004 return cp_parser_objc_protocol_expression (parser);
17006 case RID_AT_SELECTOR:
17007 return cp_parser_objc_selector_expression (parser);
17013 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17014 cp_parser_skip_to_end_of_block_or_statement (parser);
17017 return error_mark_node;
17020 /* Parse an Objective-C message expression.
17022 objc-message-expression:
17023 [ objc-message-receiver objc-message-args ]
17025 Returns a representation of an Objective-C message. */
17028 cp_parser_objc_message_expression (cp_parser* parser)
17030 tree receiver, messageargs;
17032 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17033 receiver = cp_parser_objc_message_receiver (parser);
17034 messageargs = cp_parser_objc_message_args (parser);
17035 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17037 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17040 /* Parse an objc-message-receiver.
17042 objc-message-receiver:
17044 simple-type-specifier
17046 Returns a representation of the type or expression. */
17049 cp_parser_objc_message_receiver (cp_parser* parser)
17053 /* An Objective-C message receiver may be either (1) a type
17054 or (2) an expression. */
17055 cp_parser_parse_tentatively (parser);
17056 rcv = cp_parser_expression (parser, false);
17058 if (cp_parser_parse_definitely (parser))
17061 rcv = cp_parser_simple_type_specifier (parser,
17062 /*decl_specs=*/NULL,
17063 CP_PARSER_FLAGS_NONE);
17065 return objc_get_class_reference (rcv);
17068 /* Parse the arguments and selectors comprising an Objective-C message.
17073 objc-selector-args , objc-comma-args
17075 objc-selector-args:
17076 objc-selector [opt] : assignment-expression
17077 objc-selector-args objc-selector [opt] : assignment-expression
17080 assignment-expression
17081 objc-comma-args , assignment-expression
17083 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17084 selector arguments and TREE_VALUE containing a list of comma
17088 cp_parser_objc_message_args (cp_parser* parser)
17090 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17091 bool maybe_unary_selector_p = true;
17092 cp_token *token = cp_lexer_peek_token (parser->lexer);
17094 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17096 tree selector = NULL_TREE, arg;
17098 if (token->type != CPP_COLON)
17099 selector = cp_parser_objc_selector (parser);
17101 /* Detect if we have a unary selector. */
17102 if (maybe_unary_selector_p
17103 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17104 return build_tree_list (selector, NULL_TREE);
17106 maybe_unary_selector_p = false;
17107 cp_parser_require (parser, CPP_COLON, "`:'");
17108 arg = cp_parser_assignment_expression (parser, false);
17111 = chainon (sel_args,
17112 build_tree_list (selector, arg));
17114 token = cp_lexer_peek_token (parser->lexer);
17117 /* Handle non-selector arguments, if any. */
17118 while (token->type == CPP_COMMA)
17122 cp_lexer_consume_token (parser->lexer);
17123 arg = cp_parser_assignment_expression (parser, false);
17126 = chainon (addl_args,
17127 build_tree_list (NULL_TREE, arg));
17129 token = cp_lexer_peek_token (parser->lexer);
17132 return build_tree_list (sel_args, addl_args);
17135 /* Parse an Objective-C encode expression.
17137 objc-encode-expression:
17138 @encode objc-typename
17140 Returns an encoded representation of the type argument. */
17143 cp_parser_objc_encode_expression (cp_parser* parser)
17147 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17148 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17149 type = complete_type (cp_parser_type_id (parser));
17150 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17154 error ("%<@encode%> must specify a type as an argument");
17155 return error_mark_node;
17158 return objc_build_encode_expr (type);
17161 /* Parse an Objective-C @defs expression. */
17164 cp_parser_objc_defs_expression (cp_parser *parser)
17168 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17169 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17170 name = cp_parser_identifier (parser);
17171 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17173 return objc_get_class_ivars (name);
17176 /* Parse an Objective-C protocol expression.
17178 objc-protocol-expression:
17179 @protocol ( identifier )
17181 Returns a representation of the protocol expression. */
17184 cp_parser_objc_protocol_expression (cp_parser* parser)
17188 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17189 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17190 proto = cp_parser_identifier (parser);
17191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17193 return objc_build_protocol_expr (proto);
17196 /* Parse an Objective-C selector expression.
17198 objc-selector-expression:
17199 @selector ( objc-method-signature )
17201 objc-method-signature:
17207 objc-selector-seq objc-selector :
17209 Returns a representation of the method selector. */
17212 cp_parser_objc_selector_expression (cp_parser* parser)
17214 tree sel_seq = NULL_TREE;
17215 bool maybe_unary_selector_p = true;
17218 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17219 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17220 token = cp_lexer_peek_token (parser->lexer);
17222 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17223 || token->type == CPP_SCOPE)
17225 tree selector = NULL_TREE;
17227 if (token->type != CPP_COLON
17228 || token->type == CPP_SCOPE)
17229 selector = cp_parser_objc_selector (parser);
17231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17232 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17234 /* Detect if we have a unary selector. */
17235 if (maybe_unary_selector_p)
17237 sel_seq = selector;
17238 goto finish_selector;
17242 cp_parser_error (parser, "expected %<:%>");
17245 maybe_unary_selector_p = false;
17246 token = cp_lexer_consume_token (parser->lexer);
17248 if (token->type == CPP_SCOPE)
17251 = chainon (sel_seq,
17252 build_tree_list (selector, NULL_TREE));
17254 = chainon (sel_seq,
17255 build_tree_list (NULL_TREE, NULL_TREE));
17259 = chainon (sel_seq,
17260 build_tree_list (selector, NULL_TREE));
17262 token = cp_lexer_peek_token (parser->lexer);
17266 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17268 return objc_build_selector_expr (sel_seq);
17271 /* Parse a list of identifiers.
17273 objc-identifier-list:
17275 objc-identifier-list , identifier
17277 Returns a TREE_LIST of identifier nodes. */
17280 cp_parser_objc_identifier_list (cp_parser* parser)
17282 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17283 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17285 while (sep->type == CPP_COMMA)
17287 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17288 list = chainon (list,
17289 build_tree_list (NULL_TREE,
17290 cp_parser_identifier (parser)));
17291 sep = cp_lexer_peek_token (parser->lexer);
17297 /* Parse an Objective-C alias declaration.
17299 objc-alias-declaration:
17300 @compatibility_alias identifier identifier ;
17302 This function registers the alias mapping with the Objective-C front-end.
17303 It returns nothing. */
17306 cp_parser_objc_alias_declaration (cp_parser* parser)
17310 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17311 alias = cp_parser_identifier (parser);
17312 orig = cp_parser_identifier (parser);
17313 objc_declare_alias (alias, orig);
17314 cp_parser_consume_semicolon_at_end_of_statement (parser);
17317 /* Parse an Objective-C class forward-declaration.
17319 objc-class-declaration:
17320 @class objc-identifier-list ;
17322 The function registers the forward declarations with the Objective-C
17323 front-end. It returns nothing. */
17326 cp_parser_objc_class_declaration (cp_parser* parser)
17328 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17329 objc_declare_class (cp_parser_objc_identifier_list (parser));
17330 cp_parser_consume_semicolon_at_end_of_statement (parser);
17333 /* Parse a list of Objective-C protocol references.
17335 objc-protocol-refs-opt:
17336 objc-protocol-refs [opt]
17338 objc-protocol-refs:
17339 < objc-identifier-list >
17341 Returns a TREE_LIST of identifiers, if any. */
17344 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17346 tree protorefs = NULL_TREE;
17348 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17350 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17351 protorefs = cp_parser_objc_identifier_list (parser);
17352 cp_parser_require (parser, CPP_GREATER, "`>'");
17358 /* Parse a Objective-C visibility specification. */
17361 cp_parser_objc_visibility_spec (cp_parser* parser)
17363 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17365 switch (vis->keyword)
17367 case RID_AT_PRIVATE:
17368 objc_set_visibility (2);
17370 case RID_AT_PROTECTED:
17371 objc_set_visibility (0);
17373 case RID_AT_PUBLIC:
17374 objc_set_visibility (1);
17380 /* Eat '@private'/'@protected'/'@public'. */
17381 cp_lexer_consume_token (parser->lexer);
17384 /* Parse an Objective-C method type. */
17387 cp_parser_objc_method_type (cp_parser* parser)
17389 objc_set_method_type
17390 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17395 /* Parse an Objective-C protocol qualifier. */
17398 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17400 tree quals = NULL_TREE, node;
17401 cp_token *token = cp_lexer_peek_token (parser->lexer);
17403 node = token->value;
17405 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17406 && (node == ridpointers [(int) RID_IN]
17407 || node == ridpointers [(int) RID_OUT]
17408 || node == ridpointers [(int) RID_INOUT]
17409 || node == ridpointers [(int) RID_BYCOPY]
17410 || node == ridpointers [(int) RID_BYREF]
17411 || node == ridpointers [(int) RID_ONEWAY]))
17413 quals = tree_cons (NULL_TREE, node, quals);
17414 cp_lexer_consume_token (parser->lexer);
17415 token = cp_lexer_peek_token (parser->lexer);
17416 node = token->value;
17422 /* Parse an Objective-C typename. */
17425 cp_parser_objc_typename (cp_parser* parser)
17427 tree typename = NULL_TREE;
17429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17431 tree proto_quals, cp_type = NULL_TREE;
17433 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17434 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17436 /* An ObjC type name may consist of just protocol qualifiers, in which
17437 case the type shall default to 'id'. */
17438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17439 cp_type = cp_parser_type_id (parser);
17441 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17442 typename = build_tree_list (proto_quals, cp_type);
17448 /* Check to see if TYPE refers to an Objective-C selector name. */
17451 cp_parser_objc_selector_p (enum cpp_ttype type)
17453 return (type == CPP_NAME || type == CPP_KEYWORD
17454 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17455 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17456 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17457 || type == CPP_XOR || type == CPP_XOR_EQ);
17460 /* Parse an Objective-C selector. */
17463 cp_parser_objc_selector (cp_parser* parser)
17465 cp_token *token = cp_lexer_consume_token (parser->lexer);
17467 if (!cp_parser_objc_selector_p (token->type))
17469 error ("invalid Objective-C++ selector name");
17470 return error_mark_node;
17473 /* C++ operator names are allowed to appear in ObjC selectors. */
17474 switch (token->type)
17476 case CPP_AND_AND: return get_identifier ("and");
17477 case CPP_AND_EQ: return get_identifier ("and_eq");
17478 case CPP_AND: return get_identifier ("bitand");
17479 case CPP_OR: return get_identifier ("bitor");
17480 case CPP_COMPL: return get_identifier ("compl");
17481 case CPP_NOT: return get_identifier ("not");
17482 case CPP_NOT_EQ: return get_identifier ("not_eq");
17483 case CPP_OR_OR: return get_identifier ("or");
17484 case CPP_OR_EQ: return get_identifier ("or_eq");
17485 case CPP_XOR: return get_identifier ("xor");
17486 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17487 default: return token->value;
17491 /* Parse an Objective-C params list. */
17494 cp_parser_objc_method_keyword_params (cp_parser* parser)
17496 tree params = NULL_TREE;
17497 bool maybe_unary_selector_p = true;
17498 cp_token *token = cp_lexer_peek_token (parser->lexer);
17500 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17502 tree selector = NULL_TREE, typename, identifier;
17504 if (token->type != CPP_COLON)
17505 selector = cp_parser_objc_selector (parser);
17507 /* Detect if we have a unary selector. */
17508 if (maybe_unary_selector_p
17509 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17512 maybe_unary_selector_p = false;
17513 cp_parser_require (parser, CPP_COLON, "`:'");
17514 typename = cp_parser_objc_typename (parser);
17515 identifier = cp_parser_identifier (parser);
17519 objc_build_keyword_decl (selector,
17523 token = cp_lexer_peek_token (parser->lexer);
17529 /* Parse the non-keyword Objective-C params. */
17532 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17534 tree params = make_node (TREE_LIST);
17535 cp_token *token = cp_lexer_peek_token (parser->lexer);
17536 *ellipsisp = false; /* Initially, assume no ellipsis. */
17538 while (token->type == CPP_COMMA)
17540 cp_parameter_declarator *parmdecl;
17543 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17544 token = cp_lexer_peek_token (parser->lexer);
17546 if (token->type == CPP_ELLIPSIS)
17548 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
17553 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17554 parm = grokdeclarator (parmdecl->declarator,
17555 &parmdecl->decl_specifiers,
17556 PARM, /*initialized=*/0,
17557 /*attrlist=*/NULL);
17559 chainon (params, build_tree_list (NULL_TREE, parm));
17560 token = cp_lexer_peek_token (parser->lexer);
17566 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
17569 cp_parser_objc_interstitial_code (cp_parser* parser)
17571 cp_token *token = cp_lexer_peek_token (parser->lexer);
17573 /* If the next token is `extern' and the following token is a string
17574 literal, then we have a linkage specification. */
17575 if (token->keyword == RID_EXTERN
17576 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17577 cp_parser_linkage_specification (parser);
17578 /* Handle #pragma, if any. */
17579 else if (token->type == CPP_PRAGMA)
17580 cp_parser_pragma (parser, pragma_external);
17581 /* Allow stray semicolons. */
17582 else if (token->type == CPP_SEMICOLON)
17583 cp_lexer_consume_token (parser->lexer);
17584 /* Finally, try to parse a block-declaration, or a function-definition. */
17586 cp_parser_block_declaration (parser, /*statement_p=*/false);
17589 /* Parse a method signature. */
17592 cp_parser_objc_method_signature (cp_parser* parser)
17594 tree rettype, kwdparms, optparms;
17595 bool ellipsis = false;
17597 cp_parser_objc_method_type (parser);
17598 rettype = cp_parser_objc_typename (parser);
17599 kwdparms = cp_parser_objc_method_keyword_params (parser);
17600 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17602 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17605 /* Pars an Objective-C method prototype list. */
17608 cp_parser_objc_method_prototype_list (cp_parser* parser)
17610 cp_token *token = cp_lexer_peek_token (parser->lexer);
17612 while (token->keyword != RID_AT_END)
17614 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17616 objc_add_method_declaration
17617 (cp_parser_objc_method_signature (parser));
17618 cp_parser_consume_semicolon_at_end_of_statement (parser);
17621 /* Allow for interspersed non-ObjC++ code. */
17622 cp_parser_objc_interstitial_code (parser);
17624 token = cp_lexer_peek_token (parser->lexer);
17627 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17628 objc_finish_interface ();
17631 /* Parse an Objective-C method definition list. */
17634 cp_parser_objc_method_definition_list (cp_parser* parser)
17636 cp_token *token = cp_lexer_peek_token (parser->lexer);
17638 while (token->keyword != RID_AT_END)
17642 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17644 push_deferring_access_checks (dk_deferred);
17645 objc_start_method_definition
17646 (cp_parser_objc_method_signature (parser));
17648 /* For historical reasons, we accept an optional semicolon. */
17649 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17650 cp_lexer_consume_token (parser->lexer);
17652 perform_deferred_access_checks ();
17653 stop_deferring_access_checks ();
17654 meth = cp_parser_function_definition_after_declarator (parser,
17656 pop_deferring_access_checks ();
17657 objc_finish_method_definition (meth);
17660 /* Allow for interspersed non-ObjC++ code. */
17661 cp_parser_objc_interstitial_code (parser);
17663 token = cp_lexer_peek_token (parser->lexer);
17666 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17667 objc_finish_implementation ();
17670 /* Parse Objective-C ivars. */
17673 cp_parser_objc_class_ivars (cp_parser* parser)
17675 cp_token *token = cp_lexer_peek_token (parser->lexer);
17677 if (token->type != CPP_OPEN_BRACE)
17678 return; /* No ivars specified. */
17680 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
17681 token = cp_lexer_peek_token (parser->lexer);
17683 while (token->type != CPP_CLOSE_BRACE)
17685 cp_decl_specifier_seq declspecs;
17686 int decl_class_or_enum_p;
17687 tree prefix_attributes;
17689 cp_parser_objc_visibility_spec (parser);
17691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17694 cp_parser_decl_specifier_seq (parser,
17695 CP_PARSER_FLAGS_OPTIONAL,
17697 &decl_class_or_enum_p);
17698 prefix_attributes = declspecs.attributes;
17699 declspecs.attributes = NULL_TREE;
17701 /* Keep going until we hit the `;' at the end of the
17703 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17705 tree width = NULL_TREE, attributes, first_attribute, decl;
17706 cp_declarator *declarator = NULL;
17707 int ctor_dtor_or_conv_p;
17709 /* Check for a (possibly unnamed) bitfield declaration. */
17710 token = cp_lexer_peek_token (parser->lexer);
17711 if (token->type == CPP_COLON)
17714 if (token->type == CPP_NAME
17715 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17718 /* Get the name of the bitfield. */
17719 declarator = make_id_declarator (NULL_TREE,
17720 cp_parser_identifier (parser),
17724 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17725 /* Get the width of the bitfield. */
17727 = cp_parser_constant_expression (parser,
17728 /*allow_non_constant=*/false,
17733 /* Parse the declarator. */
17735 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17736 &ctor_dtor_or_conv_p,
17737 /*parenthesized_p=*/NULL,
17738 /*member_p=*/false);
17741 /* Look for attributes that apply to the ivar. */
17742 attributes = cp_parser_attributes_opt (parser);
17743 /* Remember which attributes are prefix attributes and
17745 first_attribute = attributes;
17746 /* Combine the attributes. */
17747 attributes = chainon (prefix_attributes, attributes);
17751 /* Create the bitfield declaration. */
17752 decl = grokbitfield (declarator, &declspecs, width);
17753 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17756 decl = grokfield (declarator, &declspecs,
17757 NULL_TREE, /*init_const_expr_p=*/false,
17758 NULL_TREE, attributes);
17760 /* Add the instance variable. */
17761 objc_add_instance_variable (decl);
17763 /* Reset PREFIX_ATTRIBUTES. */
17764 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17765 attributes = TREE_CHAIN (attributes);
17767 TREE_CHAIN (attributes) = NULL_TREE;
17769 token = cp_lexer_peek_token (parser->lexer);
17771 if (token->type == CPP_COMMA)
17773 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17779 cp_parser_consume_semicolon_at_end_of_statement (parser);
17780 token = cp_lexer_peek_token (parser->lexer);
17783 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
17784 /* For historical reasons, we accept an optional semicolon. */
17785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17786 cp_lexer_consume_token (parser->lexer);
17789 /* Parse an Objective-C protocol declaration. */
17792 cp_parser_objc_protocol_declaration (cp_parser* parser)
17794 tree proto, protorefs;
17797 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17800 error ("identifier expected after %<@protocol%>");
17804 /* See if we have a forward declaration or a definition. */
17805 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17807 /* Try a forward declaration first. */
17808 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17810 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17812 cp_parser_consume_semicolon_at_end_of_statement (parser);
17815 /* Ok, we got a full-fledged definition (or at least should). */
17818 proto = cp_parser_identifier (parser);
17819 protorefs = cp_parser_objc_protocol_refs_opt (parser);
17820 objc_start_protocol (proto, protorefs);
17821 cp_parser_objc_method_prototype_list (parser);
17825 /* Parse an Objective-C superclass or category. */
17828 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17831 cp_token *next = cp_lexer_peek_token (parser->lexer);
17833 *super = *categ = NULL_TREE;
17834 if (next->type == CPP_COLON)
17836 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
17837 *super = cp_parser_identifier (parser);
17839 else if (next->type == CPP_OPEN_PAREN)
17841 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17842 *categ = cp_parser_identifier (parser);
17843 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17847 /* Parse an Objective-C class interface. */
17850 cp_parser_objc_class_interface (cp_parser* parser)
17852 tree name, super, categ, protos;
17854 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
17855 name = cp_parser_identifier (parser);
17856 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17857 protos = cp_parser_objc_protocol_refs_opt (parser);
17859 /* We have either a class or a category on our hands. */
17861 objc_start_category_interface (name, categ, protos);
17864 objc_start_class_interface (name, super, protos);
17865 /* Handle instance variable declarations, if any. */
17866 cp_parser_objc_class_ivars (parser);
17867 objc_continue_interface ();
17870 cp_parser_objc_method_prototype_list (parser);
17873 /* Parse an Objective-C class implementation. */
17876 cp_parser_objc_class_implementation (cp_parser* parser)
17878 tree name, super, categ;
17880 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
17881 name = cp_parser_identifier (parser);
17882 cp_parser_objc_superclass_or_category (parser, &super, &categ);
17884 /* We have either a class or a category on our hands. */
17886 objc_start_category_implementation (name, categ);
17889 objc_start_class_implementation (name, super);
17890 /* Handle instance variable declarations, if any. */
17891 cp_parser_objc_class_ivars (parser);
17892 objc_continue_implementation ();
17895 cp_parser_objc_method_definition_list (parser);
17898 /* Consume the @end token and finish off the implementation. */
17901 cp_parser_objc_end_implementation (cp_parser* parser)
17903 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
17904 objc_finish_implementation ();
17907 /* Parse an Objective-C declaration. */
17910 cp_parser_objc_declaration (cp_parser* parser)
17912 /* Try to figure out what kind of declaration is present. */
17913 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17915 switch (kwd->keyword)
17918 cp_parser_objc_alias_declaration (parser);
17921 cp_parser_objc_class_declaration (parser);
17923 case RID_AT_PROTOCOL:
17924 cp_parser_objc_protocol_declaration (parser);
17926 case RID_AT_INTERFACE:
17927 cp_parser_objc_class_interface (parser);
17929 case RID_AT_IMPLEMENTATION:
17930 cp_parser_objc_class_implementation (parser);
17933 cp_parser_objc_end_implementation (parser);
17936 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
17937 cp_parser_skip_to_end_of_block_or_statement (parser);
17941 /* Parse an Objective-C try-catch-finally statement.
17943 objc-try-catch-finally-stmt:
17944 @try compound-statement objc-catch-clause-seq [opt]
17945 objc-finally-clause [opt]
17947 objc-catch-clause-seq:
17948 objc-catch-clause objc-catch-clause-seq [opt]
17951 @catch ( exception-declaration ) compound-statement
17953 objc-finally-clause
17954 @finally compound-statement
17956 Returns NULL_TREE. */
17959 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17960 location_t location;
17963 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17964 location = cp_lexer_peek_token (parser->lexer)->location;
17965 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17966 node, lest it get absorbed into the surrounding block. */
17967 stmt = push_stmt_list ();
17968 cp_parser_compound_statement (parser, NULL, false);
17969 objc_begin_try_stmt (location, pop_stmt_list (stmt));
17971 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17973 cp_parameter_declarator *parmdecl;
17976 cp_lexer_consume_token (parser->lexer);
17977 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17978 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17979 parm = grokdeclarator (parmdecl->declarator,
17980 &parmdecl->decl_specifiers,
17981 PARM, /*initialized=*/0,
17982 /*attrlist=*/NULL);
17983 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17984 objc_begin_catch_clause (parm);
17985 cp_parser_compound_statement (parser, NULL, false);
17986 objc_finish_catch_clause ();
17989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17991 cp_lexer_consume_token (parser->lexer);
17992 location = cp_lexer_peek_token (parser->lexer)->location;
17993 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17994 node, lest it get absorbed into the surrounding block. */
17995 stmt = push_stmt_list ();
17996 cp_parser_compound_statement (parser, NULL, false);
17997 objc_build_finally_clause (location, pop_stmt_list (stmt));
18000 return objc_finish_try_stmt ();
18003 /* Parse an Objective-C synchronized statement.
18005 objc-synchronized-stmt:
18006 @synchronized ( expression ) compound-statement
18008 Returns NULL_TREE. */
18011 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18012 location_t location;
18015 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18017 location = cp_lexer_peek_token (parser->lexer)->location;
18018 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18019 lock = cp_parser_expression (parser, false);
18020 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18022 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18023 node, lest it get absorbed into the surrounding block. */
18024 stmt = push_stmt_list ();
18025 cp_parser_compound_statement (parser, NULL, false);
18027 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18030 /* Parse an Objective-C throw statement.
18033 @throw assignment-expression [opt] ;
18035 Returns a constructed '@throw' statement. */
18038 cp_parser_objc_throw_statement (cp_parser *parser) {
18039 tree expr = NULL_TREE;
18041 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18044 expr = cp_parser_assignment_expression (parser, false);
18046 cp_parser_consume_semicolon_at_end_of_statement (parser);
18048 return objc_build_throw_stmt (expr);
18051 /* Parse an Objective-C statement. */
18054 cp_parser_objc_statement (cp_parser * parser) {
18055 /* Try to figure out what kind of declaration is present. */
18056 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18058 switch (kwd->keyword)
18061 return cp_parser_objc_try_catch_finally_statement (parser);
18062 case RID_AT_SYNCHRONIZED:
18063 return cp_parser_objc_synchronized_statement (parser);
18065 return cp_parser_objc_throw_statement (parser);
18067 error ("misplaced %<@%D%> Objective-C++ construct", kwd->value);
18068 cp_parser_skip_to_end_of_block_or_statement (parser);
18071 return error_mark_node;
18074 /* OpenMP 2.5 parsing routines. */
18076 /* Returns name of the next clause.
18077 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18078 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18079 returned and the token is consumed. */
18081 static pragma_omp_clause
18082 cp_parser_omp_clause_name (cp_parser *parser)
18084 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18087 result = PRAGMA_OMP_CLAUSE_IF;
18088 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18089 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18090 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18091 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18092 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18094 tree id = cp_lexer_peek_token (parser->lexer)->value;
18095 const char *p = IDENTIFIER_POINTER (id);
18100 if (!strcmp ("copyin", p))
18101 result = PRAGMA_OMP_CLAUSE_COPYIN;
18102 else if (!strcmp ("copyprivate", p))
18103 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18106 if (!strcmp ("firstprivate", p))
18107 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18110 if (!strcmp ("lastprivate", p))
18111 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18114 if (!strcmp ("nowait", p))
18115 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18116 else if (!strcmp ("num_threads", p))
18117 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18120 if (!strcmp ("ordered", p))
18121 result = PRAGMA_OMP_CLAUSE_ORDERED;
18124 if (!strcmp ("reduction", p))
18125 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18128 if (!strcmp ("schedule", p))
18129 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18130 else if (!strcmp ("shared", p))
18131 result = PRAGMA_OMP_CLAUSE_SHARED;
18136 if (result != PRAGMA_OMP_CLAUSE_NONE)
18137 cp_lexer_consume_token (parser->lexer);
18142 /* Validate that a clause of the given type does not already exist. */
18145 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18149 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18150 if (OMP_CLAUSE_CODE (c) == code)
18152 error ("too many %qs clauses", name);
18160 variable-list , identifier
18162 In addition, we match a closing parenthesis. An opening parenthesis
18163 will have been consumed by the caller.
18165 If KIND is nonzero, create the appropriate node and install the decl
18166 in OMP_CLAUSE_DECL and add the node to the head of the list.
18168 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18169 return the list created. */
18172 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18179 name = cp_parser_id_expression (parser, /*template_p=*/false,
18180 /*check_dependency_p=*/true,
18181 /*template_p=*/NULL,
18182 /*declarator_p=*/false,
18183 /*optional_p=*/false);
18184 if (name == error_mark_node)
18187 decl = cp_parser_lookup_name_simple (parser, name);
18188 if (decl == error_mark_node)
18189 cp_parser_name_lookup_error (parser, name, decl, NULL);
18190 else if (kind != 0)
18192 tree u = build_omp_clause (kind);
18193 OMP_CLAUSE_DECL (u) = decl;
18194 OMP_CLAUSE_CHAIN (u) = list;
18198 list = tree_cons (decl, NULL_TREE, list);
18201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18203 cp_lexer_consume_token (parser->lexer);
18206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18210 /* Try to resync to an unnested comma. Copied from
18211 cp_parser_parenthesized_expression_list. */
18213 ending = cp_parser_skip_to_closing_parenthesis (parser,
18214 /*recovering=*/true,
18216 /*consume_paren=*/true);
18224 /* Similarly, but expect leading and trailing parenthesis. This is a very
18225 common case for omp clauses. */
18228 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18230 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18231 return cp_parser_omp_var_list_no_open (parser, kind, list);
18236 default ( shared | none ) */
18239 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18241 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18244 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18248 tree id = cp_lexer_peek_token (parser->lexer)->value;
18249 const char *p = IDENTIFIER_POINTER (id);
18254 if (strcmp ("none", p) != 0)
18256 kind = OMP_CLAUSE_DEFAULT_NONE;
18260 if (strcmp ("shared", p) != 0)
18262 kind = OMP_CLAUSE_DEFAULT_SHARED;
18269 cp_lexer_consume_token (parser->lexer);
18274 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18277 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18279 /*or_comma=*/false,
18280 /*consume_paren=*/true);
18282 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18285 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18286 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18287 OMP_CLAUSE_CHAIN (c) = list;
18288 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18294 if ( expression ) */
18297 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18304 t = cp_parser_condition (parser);
18306 if (t == error_mark_node
18307 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18309 /*or_comma=*/false,
18310 /*consume_paren=*/true);
18312 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18314 c = build_omp_clause (OMP_CLAUSE_IF);
18315 OMP_CLAUSE_IF_EXPR (c) = t;
18316 OMP_CLAUSE_CHAIN (c) = list;
18325 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18329 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18331 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18332 OMP_CLAUSE_CHAIN (c) = list;
18337 num_threads ( expression ) */
18340 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18347 t = cp_parser_expression (parser, false);
18349 if (t == error_mark_node
18350 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18352 /*or_comma=*/false,
18353 /*consume_paren=*/true);
18355 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18357 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18358 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18359 OMP_CLAUSE_CHAIN (c) = list;
18368 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18372 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18374 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18375 OMP_CLAUSE_CHAIN (c) = list;
18380 reduction ( reduction-operator : variable-list )
18382 reduction-operator:
18383 One of: + * - & ^ | && || */
18386 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18388 enum tree_code code;
18391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18394 switch (cp_lexer_peek_token (parser->lexer)->type)
18406 code = BIT_AND_EXPR;
18409 code = BIT_XOR_EXPR;
18412 code = BIT_IOR_EXPR;
18415 code = TRUTH_ANDIF_EXPR;
18418 code = TRUTH_ORIF_EXPR;
18421 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18423 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18424 /*or_comma=*/false,
18425 /*consume_paren=*/true);
18428 cp_lexer_consume_token (parser->lexer);
18430 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18433 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18434 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18435 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18441 schedule ( schedule-kind )
18442 schedule ( schedule-kind , expression )
18445 static | dynamic | guided | runtime */
18448 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18452 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18455 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18457 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18459 tree id = cp_lexer_peek_token (parser->lexer)->value;
18460 const char *p = IDENTIFIER_POINTER (id);
18465 if (strcmp ("dynamic", p) != 0)
18467 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18471 if (strcmp ("guided", p) != 0)
18473 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18477 if (strcmp ("runtime", p) != 0)
18479 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18486 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18487 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18490 cp_lexer_consume_token (parser->lexer);
18492 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18494 cp_lexer_consume_token (parser->lexer);
18496 t = cp_parser_assignment_expression (parser, false);
18498 if (t == error_mark_node)
18500 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18501 error ("schedule %<runtime%> does not take "
18502 "a %<chunk_size%> parameter");
18504 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18506 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18509 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18512 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18513 OMP_CLAUSE_CHAIN (c) = list;
18517 cp_parser_error (parser, "invalid schedule kind");
18519 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18520 /*or_comma=*/false,
18521 /*consume_paren=*/true);
18525 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18526 is a bitmask in MASK. Return the list of clauses found; the result
18527 of clause default goes in *pdefault. */
18530 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18531 const char *where, cp_token *pragma_tok)
18533 tree clauses = NULL;
18535 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18537 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18538 const char *c_name;
18539 tree prev = clauses;
18543 case PRAGMA_OMP_CLAUSE_COPYIN:
18544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18547 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18548 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18550 c_name = "copyprivate";
18552 case PRAGMA_OMP_CLAUSE_DEFAULT:
18553 clauses = cp_parser_omp_clause_default (parser, clauses);
18554 c_name = "default";
18556 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18557 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18559 c_name = "firstprivate";
18561 case PRAGMA_OMP_CLAUSE_IF:
18562 clauses = cp_parser_omp_clause_if (parser, clauses);
18565 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18566 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18568 c_name = "lastprivate";
18570 case PRAGMA_OMP_CLAUSE_NOWAIT:
18571 clauses = cp_parser_omp_clause_nowait (parser, clauses);
18574 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18575 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18576 c_name = "num_threads";
18578 case PRAGMA_OMP_CLAUSE_ORDERED:
18579 clauses = cp_parser_omp_clause_ordered (parser, clauses);
18580 c_name = "ordered";
18582 case PRAGMA_OMP_CLAUSE_PRIVATE:
18583 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18585 c_name = "private";
18587 case PRAGMA_OMP_CLAUSE_REDUCTION:
18588 clauses = cp_parser_omp_clause_reduction (parser, clauses);
18589 c_name = "reduction";
18591 case PRAGMA_OMP_CLAUSE_SCHEDULE:
18592 clauses = cp_parser_omp_clause_schedule (parser, clauses);
18593 c_name = "schedule";
18595 case PRAGMA_OMP_CLAUSE_SHARED:
18596 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18601 cp_parser_error (parser, "expected %<#pragma omp%> clause");
18605 if (((mask >> c_kind) & 1) == 0)
18607 /* Remove the invalid clause(s) from the list to avoid
18608 confusing the rest of the compiler. */
18610 error ("%qs is not valid for %qs", c_name, where);
18614 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18615 return finish_omp_clauses (clauses);
18622 In practice, we're also interested in adding the statement to an
18623 outer node. So it is convenient if we work around the fact that
18624 cp_parser_statement calls add_stmt. */
18627 cp_parser_begin_omp_structured_block (cp_parser *parser)
18629 unsigned save = parser->in_statement;
18631 /* Only move the values to IN_OMP_BLOCK if they weren't false.
18632 This preserves the "not within loop or switch" style error messages
18633 for nonsense cases like
18639 if (parser->in_statement)
18640 parser->in_statement = IN_OMP_BLOCK;
18646 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18648 parser->in_statement = save;
18652 cp_parser_omp_structured_block (cp_parser *parser)
18654 tree stmt = begin_omp_structured_block ();
18655 unsigned int save = cp_parser_begin_omp_structured_block (parser);
18657 cp_parser_statement (parser, NULL_TREE, false);
18659 cp_parser_end_omp_structured_block (parser, save);
18660 return finish_omp_structured_block (stmt);
18664 # pragma omp atomic new-line
18668 x binop= expr | x++ | ++x | x-- | --x
18670 +, *, -, /, &, ^, |, <<, >>
18672 where x is an lvalue expression with scalar type. */
18675 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18678 enum tree_code code;
18680 cp_parser_require_pragma_eol (parser, pragma_tok);
18682 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18684 switch (TREE_CODE (lhs))
18689 case PREINCREMENT_EXPR:
18690 case POSTINCREMENT_EXPR:
18691 lhs = TREE_OPERAND (lhs, 0);
18693 rhs = integer_one_node;
18696 case PREDECREMENT_EXPR:
18697 case POSTDECREMENT_EXPR:
18698 lhs = TREE_OPERAND (lhs, 0);
18700 rhs = integer_one_node;
18704 switch (cp_lexer_peek_token (parser->lexer)->type)
18710 code = TRUNC_DIV_EXPR;
18718 case CPP_LSHIFT_EQ:
18719 code = LSHIFT_EXPR;
18721 case CPP_RSHIFT_EQ:
18722 code = RSHIFT_EXPR;
18725 code = BIT_AND_EXPR;
18728 code = BIT_IOR_EXPR;
18731 code = BIT_XOR_EXPR;
18734 cp_parser_error (parser,
18735 "invalid operator for %<#pragma omp atomic%>");
18738 cp_lexer_consume_token (parser->lexer);
18740 rhs = cp_parser_expression (parser, false);
18741 if (rhs == error_mark_node)
18745 finish_omp_atomic (code, lhs, rhs);
18746 cp_parser_consume_semicolon_at_end_of_statement (parser);
18750 cp_parser_skip_to_end_of_block_or_statement (parser);
18755 # pragma omp barrier new-line */
18758 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18760 cp_parser_require_pragma_eol (parser, pragma_tok);
18761 finish_omp_barrier ();
18765 # pragma omp critical [(name)] new-line
18766 structured-block */
18769 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18771 tree stmt, name = NULL;
18773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18775 cp_lexer_consume_token (parser->lexer);
18777 name = cp_parser_identifier (parser);
18779 if (name == error_mark_node
18780 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18782 /*or_comma=*/false,
18783 /*consume_paren=*/true);
18784 if (name == error_mark_node)
18787 cp_parser_require_pragma_eol (parser, pragma_tok);
18789 stmt = cp_parser_omp_structured_block (parser);
18790 return c_finish_omp_critical (stmt, name);
18794 # pragma omp flush flush-vars[opt] new-line
18797 ( variable-list ) */
18800 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18803 (void) cp_parser_omp_var_list (parser, 0, NULL);
18804 cp_parser_require_pragma_eol (parser, pragma_tok);
18806 finish_omp_flush ();
18809 /* Parse the restricted form of the for statment allowed by OpenMP. */
18812 cp_parser_omp_for_loop (cp_parser *parser)
18814 tree init, cond, incr, body, decl, pre_body;
18817 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18819 cp_parser_error (parser, "for statement expected");
18822 loc = cp_lexer_consume_token (parser->lexer)->location;
18823 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18826 init = decl = NULL;
18827 pre_body = push_stmt_list ();
18828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18830 cp_decl_specifier_seq type_specifiers;
18832 /* First, try to parse as an initialized declaration. See
18833 cp_parser_condition, from whence the bulk of this is copied. */
18835 cp_parser_parse_tentatively (parser);
18836 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18838 if (!cp_parser_error_occurred (parser))
18840 tree asm_specification, attributes;
18841 cp_declarator *declarator;
18843 declarator = cp_parser_declarator (parser,
18844 CP_PARSER_DECLARATOR_NAMED,
18845 /*ctor_dtor_or_conv_p=*/NULL,
18846 /*parenthesized_p=*/NULL,
18847 /*member_p=*/false);
18848 attributes = cp_parser_attributes_opt (parser);
18849 asm_specification = cp_parser_asm_specification_opt (parser);
18851 cp_parser_require (parser, CPP_EQ, "`='");
18852 if (cp_parser_parse_definitely (parser))
18856 decl = start_decl (declarator, &type_specifiers,
18857 /*initialized_p=*/false, attributes,
18858 /*prefix_attributes=*/NULL_TREE,
18861 init = cp_parser_assignment_expression (parser, false);
18863 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18864 asm_specification, LOOKUP_ONLYCONVERTING);
18867 pop_scope (pushed_scope);
18871 cp_parser_abort_tentative_parse (parser);
18873 /* If parsing as an initialized declaration failed, try again as
18874 a simple expression. */
18876 init = cp_parser_expression (parser, false);
18878 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18879 pre_body = pop_stmt_list (pre_body);
18882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18883 cond = cp_parser_condition (parser);
18884 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18888 incr = cp_parser_expression (parser, false);
18890 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18891 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18892 /*or_comma=*/false,
18893 /*consume_paren=*/true);
18895 /* Note that we saved the original contents of this flag when we entered
18896 the structured block, and so we don't need to re-save it here. */
18897 parser->in_statement = IN_OMP_FOR;
18899 /* Note that the grammar doesn't call for a structured block here,
18900 though the loop as a whole is a structured block. */
18901 body = push_stmt_list ();
18902 cp_parser_statement (parser, NULL_TREE, false);
18903 body = pop_stmt_list (body);
18905 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18909 #pragma omp for for-clause[optseq] new-line
18912 #define OMP_FOR_CLAUSE_MASK \
18913 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
18914 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18915 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18916 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
18917 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
18918 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18919 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18922 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18924 tree clauses, sb, ret;
18927 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18928 "#pragma omp for", pragma_tok);
18930 sb = begin_omp_structured_block ();
18931 save = cp_parser_begin_omp_structured_block (parser);
18933 ret = cp_parser_omp_for_loop (parser);
18935 OMP_FOR_CLAUSES (ret) = clauses;
18937 cp_parser_end_omp_structured_block (parser, save);
18938 add_stmt (finish_omp_structured_block (sb));
18944 # pragma omp master new-line
18945 structured-block */
18948 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18950 cp_parser_require_pragma_eol (parser, pragma_tok);
18951 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18955 # pragma omp ordered new-line
18956 structured-block */
18959 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18961 cp_parser_require_pragma_eol (parser, pragma_tok);
18962 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18968 { section-sequence }
18971 section-directive[opt] structured-block
18972 section-sequence section-directive structured-block */
18975 cp_parser_omp_sections_scope (cp_parser *parser)
18977 tree stmt, substmt;
18978 bool error_suppress = false;
18981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18984 stmt = push_stmt_list ();
18986 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18990 substmt = begin_omp_structured_block ();
18991 save = cp_parser_begin_omp_structured_block (parser);
18995 cp_parser_statement (parser, NULL_TREE, false);
18997 tok = cp_lexer_peek_token (parser->lexer);
18998 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19000 if (tok->type == CPP_CLOSE_BRACE)
19002 if (tok->type == CPP_EOF)
19006 cp_parser_end_omp_structured_block (parser, save);
19007 substmt = finish_omp_structured_block (substmt);
19008 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19009 add_stmt (substmt);
19014 tok = cp_lexer_peek_token (parser->lexer);
19015 if (tok->type == CPP_CLOSE_BRACE)
19017 if (tok->type == CPP_EOF)
19020 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19022 cp_lexer_consume_token (parser->lexer);
19023 cp_parser_require_pragma_eol (parser, tok);
19024 error_suppress = false;
19026 else if (!error_suppress)
19028 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19029 error_suppress = true;
19032 substmt = cp_parser_omp_structured_block (parser);
19033 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19034 add_stmt (substmt);
19036 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19038 substmt = pop_stmt_list (stmt);
19040 stmt = make_node (OMP_SECTIONS);
19041 TREE_TYPE (stmt) = void_type_node;
19042 OMP_SECTIONS_BODY (stmt) = substmt;
19049 # pragma omp sections sections-clause[optseq] newline
19052 #define OMP_SECTIONS_CLAUSE_MASK \
19053 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19054 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19055 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19056 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19057 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19060 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19064 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19065 "#pragma omp sections", pragma_tok);
19067 ret = cp_parser_omp_sections_scope (parser);
19069 OMP_SECTIONS_CLAUSES (ret) = clauses;
19075 # pragma parallel parallel-clause new-line
19076 # pragma parallel for parallel-for-clause new-line
19077 # pragma parallel sections parallel-sections-clause new-line */
19079 #define OMP_PARALLEL_CLAUSE_MASK \
19080 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19081 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19082 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19083 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19084 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19085 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19086 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19087 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19090 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19092 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19093 const char *p_name = "#pragma omp parallel";
19094 tree stmt, clauses, par_clause, ws_clause, block;
19095 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19100 cp_lexer_consume_token (parser->lexer);
19101 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19102 p_name = "#pragma omp parallel for";
19103 mask |= OMP_FOR_CLAUSE_MASK;
19104 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19106 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19108 tree id = cp_lexer_peek_token (parser->lexer)->value;
19109 const char *p = IDENTIFIER_POINTER (id);
19110 if (strcmp (p, "sections") == 0)
19112 cp_lexer_consume_token (parser->lexer);
19113 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19114 p_name = "#pragma omp parallel sections";
19115 mask |= OMP_SECTIONS_CLAUSE_MASK;
19116 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19120 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19121 block = begin_omp_parallel ();
19122 save = cp_parser_begin_omp_structured_block (parser);
19126 case PRAGMA_OMP_PARALLEL:
19127 cp_parser_already_scoped_statement (parser);
19128 par_clause = clauses;
19131 case PRAGMA_OMP_PARALLEL_FOR:
19132 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19133 stmt = cp_parser_omp_for_loop (parser);
19135 OMP_FOR_CLAUSES (stmt) = ws_clause;
19138 case PRAGMA_OMP_PARALLEL_SECTIONS:
19139 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19140 stmt = cp_parser_omp_sections_scope (parser);
19142 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19146 gcc_unreachable ();
19149 cp_parser_end_omp_structured_block (parser, save);
19150 stmt = finish_omp_parallel (par_clause, block);
19151 if (p_kind != PRAGMA_OMP_PARALLEL)
19152 OMP_PARALLEL_COMBINED (stmt) = 1;
19157 # pragma omp single single-clause[optseq] new-line
19158 structured-block */
19160 #define OMP_SINGLE_CLAUSE_MASK \
19161 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19162 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19163 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19164 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19167 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19169 tree stmt = make_node (OMP_SINGLE);
19170 TREE_TYPE (stmt) = void_type_node;
19172 OMP_SINGLE_CLAUSES (stmt)
19173 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19174 "#pragma omp single", pragma_tok);
19175 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19177 return add_stmt (stmt);
19181 # pragma omp threadprivate (variable-list) */
19184 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19188 vars = cp_parser_omp_var_list (parser, 0, NULL);
19189 cp_parser_require_pragma_eol (parser, pragma_tok);
19191 if (!targetm.have_tls)
19192 sorry ("threadprivate variables not supported in this target");
19194 finish_omp_threadprivate (vars);
19197 /* Main entry point to OpenMP statement pragmas. */
19200 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19204 switch (pragma_tok->pragma_kind)
19206 case PRAGMA_OMP_ATOMIC:
19207 cp_parser_omp_atomic (parser, pragma_tok);
19209 case PRAGMA_OMP_CRITICAL:
19210 stmt = cp_parser_omp_critical (parser, pragma_tok);
19212 case PRAGMA_OMP_FOR:
19213 stmt = cp_parser_omp_for (parser, pragma_tok);
19215 case PRAGMA_OMP_MASTER:
19216 stmt = cp_parser_omp_master (parser, pragma_tok);
19218 case PRAGMA_OMP_ORDERED:
19219 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19221 case PRAGMA_OMP_PARALLEL:
19222 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19224 case PRAGMA_OMP_SECTIONS:
19225 stmt = cp_parser_omp_sections (parser, pragma_tok);
19227 case PRAGMA_OMP_SINGLE:
19228 stmt = cp_parser_omp_single (parser, pragma_tok);
19231 gcc_unreachable ();
19235 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19240 static GTY (()) cp_parser *the_parser;
19243 /* Special handling for the first token or line in the file. The first
19244 thing in the file might be #pragma GCC pch_preprocess, which loads a
19245 PCH file, which is a GC collection point. So we need to handle this
19246 first pragma without benefit of an existing lexer structure.
19248 Always returns one token to the caller in *FIRST_TOKEN. This is
19249 either the true first token of the file, or the first token after
19250 the initial pragma. */
19253 cp_parser_initial_pragma (cp_token *first_token)
19257 cp_lexer_get_preprocessor_token (NULL, first_token);
19258 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19261 cp_lexer_get_preprocessor_token (NULL, first_token);
19262 if (first_token->type == CPP_STRING)
19264 name = first_token->value;
19266 cp_lexer_get_preprocessor_token (NULL, first_token);
19267 if (first_token->type != CPP_PRAGMA_EOL)
19268 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19271 error ("expected string literal");
19273 /* Skip to the end of the pragma. */
19274 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19275 cp_lexer_get_preprocessor_token (NULL, first_token);
19277 /* Now actually load the PCH file. */
19279 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19281 /* Read one more token to return to our caller. We have to do this
19282 after reading the PCH file in, since its pointers have to be
19284 cp_lexer_get_preprocessor_token (NULL, first_token);
19287 /* Normal parsing of a pragma token. Here we can (and must) use the
19291 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19293 cp_token *pragma_tok;
19296 pragma_tok = cp_lexer_consume_token (parser->lexer);
19297 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19298 parser->lexer->in_pragma = true;
19300 id = pragma_tok->pragma_kind;
19303 case PRAGMA_GCC_PCH_PREPROCESS:
19304 error ("%<#pragma GCC pch_preprocess%> must be first");
19307 case PRAGMA_OMP_BARRIER:
19310 case pragma_compound:
19311 cp_parser_omp_barrier (parser, pragma_tok);
19314 error ("%<#pragma omp barrier%> may only be "
19315 "used in compound statements");
19322 case PRAGMA_OMP_FLUSH:
19325 case pragma_compound:
19326 cp_parser_omp_flush (parser, pragma_tok);
19329 error ("%<#pragma omp flush%> may only be "
19330 "used in compound statements");
19337 case PRAGMA_OMP_THREADPRIVATE:
19338 cp_parser_omp_threadprivate (parser, pragma_tok);
19341 case PRAGMA_OMP_ATOMIC:
19342 case PRAGMA_OMP_CRITICAL:
19343 case PRAGMA_OMP_FOR:
19344 case PRAGMA_OMP_MASTER:
19345 case PRAGMA_OMP_ORDERED:
19346 case PRAGMA_OMP_PARALLEL:
19347 case PRAGMA_OMP_SECTIONS:
19348 case PRAGMA_OMP_SINGLE:
19349 if (context == pragma_external)
19351 cp_parser_omp_construct (parser, pragma_tok);
19354 case PRAGMA_OMP_SECTION:
19355 error ("%<#pragma omp section%> may only be used in "
19356 "%<#pragma omp sections%> construct");
19360 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19361 c_invoke_pragma_handler (id);
19365 cp_parser_error (parser, "expected declaration specifiers");
19369 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19373 /* The interface the pragma parsers have to the lexer. */
19376 pragma_lex (tree *value)
19379 enum cpp_ttype ret;
19381 tok = cp_lexer_peek_token (the_parser->lexer);
19384 *value = tok->value;
19386 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19388 else if (ret == CPP_STRING)
19389 *value = cp_parser_string_literal (the_parser, false, false);
19392 cp_lexer_consume_token (the_parser->lexer);
19393 if (ret == CPP_KEYWORD)
19401 /* External interface. */
19403 /* Parse one entire translation unit. */
19406 c_parse_file (void)
19408 bool error_occurred;
19409 static bool already_called = false;
19411 if (already_called)
19413 sorry ("inter-module optimizations not implemented for C++");
19416 already_called = true;
19418 the_parser = cp_parser_new ();
19419 push_deferring_access_checks (flag_access_control
19420 ? dk_no_deferred : dk_no_check);
19421 error_occurred = cp_parser_translation_unit (the_parser);
19425 #include "gt-cp-parser.h"