2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 /* The location at which this token was found. */
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
102 /* The cp_lexer structure represents the C++ lexer. It is responsible
103 for managing the token stream from the preprocessor and supplying
104 it to the parser. Tokens are never added to the cp_lexer after
107 typedef struct cp_lexer GTY (())
109 /* The memory allocated for the buffer. NULL if this lexer does not
110 own the token buffer. */
111 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112 /* If the lexer owns the buffer, this is the number of tokens in the
114 size_t buffer_length;
116 /* A pointer just past the last available token. The tokens
117 in this lexer are [buffer, last_token). */
118 cp_token_position GTY ((skip)) last_token;
120 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
121 no more available tokens. */
122 cp_token_position GTY ((skip)) next_token;
124 /* A stack indicating positions at which cp_lexer_save_tokens was
125 called. The top entry is the most recent position at which we
126 began saving tokens. If the stack is non-empty, we are saving
128 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
130 /* The next lexer in a linked list of lexers. */
131 struct cp_lexer *next;
133 /* True if we should output debugging information. */
136 /* True if we're in the context of parsing a pragma, and should not
137 increment past the end-of-line marker. */
141 /* cp_token_cache is a range of tokens. There is no need to represent
142 allocate heap memory for it, since tokens are never removed from the
143 lexer's array. There is also no need for the GC to walk through
144 a cp_token_cache, since everything in here is referenced through
147 typedef struct cp_token_cache GTY(())
149 /* The beginning of the token range. */
150 cp_token * GTY((skip)) first;
152 /* Points immediately after the last token in the range. */
153 cp_token * GTY ((skip)) last;
158 static cp_lexer *cp_lexer_new_main
160 static cp_lexer *cp_lexer_new_from_tokens
161 (cp_token_cache *tokens);
162 static void cp_lexer_destroy
164 static int cp_lexer_saving_tokens
166 static cp_token_position cp_lexer_token_position
168 static cp_token *cp_lexer_token_at
169 (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171 (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
174 static cp_token *cp_lexer_peek_nth_token
175 (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177 (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181 (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
184 static void cp_lexer_purge_token
186 static void cp_lexer_purge_tokens_after
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
190 static void cp_lexer_commit_tokens
192 static void cp_lexer_rollback_tokens
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196 (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
199 static void cp_lexer_start_debugging
200 (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205 about passing NULL to functions that require non-NULL arguments
206 (fputs, fprintf). It will never be used, so all we need is a value
207 of the right type that's guaranteed not to be NULL. */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
213 static cp_token_cache *cp_token_cache_new
214 (cp_token *, cp_token *);
216 static void cp_parser_initial_pragma
219 /* Manifest constants. */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
223 /* A token type for keywords, as opposed to ordinary identifiers. */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
226 /* A token type for template-ids. If a template-id is processed while
227 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228 the value of the CPP_TEMPLATE_ID is whatever was returned by
229 cp_parser_template_id. */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
232 /* A token type for nested-name-specifiers. If a
233 nested-name-specifier is processed while parsing tentatively, it is
234 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236 cp_parser_nested_name_specifier_opt. */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
239 /* A token type for tokens that are not tokens at all; these are used
240 to represent slots in the array where there used to be a token
241 that has now been deleted. */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
244 /* The number of token types, including C++-specific ones. */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written. */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
313 cpp_get_options (parse_in)->client_diagnostic = true;
314 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
316 gcc_assert (lexer->next_token->type != CPP_PURGED);
320 /* Create a new lexer whose token stream is primed with the tokens in
321 CACHE. When these tokens are exhausted, no new tokens will be read. */
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
326 cp_token *first = cache->first;
327 cp_token *last = cache->last;
328 cp_lexer *lexer = GGC_CNEW (cp_lexer);
330 /* We do not own the buffer. */
331 lexer->buffer = NULL;
332 lexer->buffer_length = 0;
333 lexer->next_token = first == last ? &eof_token : first;
334 lexer->last_token = last;
336 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337 CP_SAVED_TOKEN_STACK);
339 #ifdef ENABLE_CHECKING
340 /* Initially we are not debugging. */
341 lexer->debugging_p = false;
344 gcc_assert (lexer->next_token->type != CPP_PURGED);
348 /* Frees all resources associated with LEXER. */
351 cp_lexer_destroy (cp_lexer *lexer)
354 ggc_free (lexer->buffer);
355 VEC_free (cp_token_position, heap, lexer->saved_tokens);
359 /* Returns nonzero if debugging information should be output. */
361 #ifdef ENABLE_CHECKING
364 cp_lexer_debugging_p (cp_lexer *lexer)
366 return lexer->debugging_p;
369 #endif /* ENABLE_CHECKING */
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
374 gcc_assert (!previous_p || lexer->next_token != &eof_token);
376 return lexer->next_token - previous_p;
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
385 /* nonzero if we are presently saving tokens. */
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
390 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 /* Store the next token from the preprocessor in *TOKEN. Return true
394 if we reach EOF. If LEXER is NULL, assume we are handling an
395 initial #pragma pch_preprocess, and thus want the lexer to return
396 processed strings. */
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
401 static int is_extern_c = 0;
403 /* Get a new token from the preprocessor. */
405 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407 token->keyword = RID_MAX;
408 token->pragma_kind = PRAGMA_NONE;
410 /* On some systems, some header files are surrounded by an
411 implicit extern "C" block. Set a flag in the token if it
412 comes from such a header. */
413 is_extern_c += pending_lang_change;
414 pending_lang_change = 0;
415 token->implicit_extern_c = is_extern_c > 0;
417 /* Check to see if this token is a keyword. */
418 if (token->type == CPP_NAME)
420 if (C_IS_RESERVED_WORD (token->u.value))
422 /* Mark this token as a keyword. */
423 token->type = CPP_KEYWORD;
424 /* Record which keyword. */
425 token->keyword = C_RID_CODE (token->u.value);
426 /* Update the value. Some keywords are mapped to particular
427 entities, rather than simply having the value of the
428 corresponding IDENTIFIER_NODE. For example, `__const' is
429 mapped to `const'. */
430 token->u.value = ridpointers[token->keyword];
434 if (warn_cxx0x_compat
435 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
438 /* Warn about the C++0x keyword (but still treat it as
440 warning (OPT_Wc__0x_compat,
441 "identifier %<%s%> will become a keyword in C++0x",
442 IDENTIFIER_POINTER (token->u.value));
444 /* Clear out the C_RID_CODE so we don't warn about this
445 particular identifier-turned-keyword again. */
446 C_SET_RID_CODE (token->u.value, RID_MAX);
449 token->ambiguous_p = false;
450 token->keyword = RID_MAX;
453 /* Handle Objective-C++ keywords. */
454 else if (token->type == CPP_AT_NAME)
456 token->type = CPP_KEYWORD;
457 switch (C_RID_CODE (token->u.value))
459 /* Map 'class' to '@class', 'private' to '@private', etc. */
460 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464 case RID_THROW: token->keyword = RID_AT_THROW; break;
465 case RID_TRY: token->keyword = RID_AT_TRY; break;
466 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467 default: token->keyword = C_RID_CODE (token->u.value);
470 else if (token->type == CPP_PRAGMA)
472 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
473 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474 token->u.value = NULL_TREE;
478 /* Update the globals input_location and the input file stack from TOKEN. */
480 cp_lexer_set_source_position_from_token (cp_token *token)
482 if (token->type != CPP_EOF)
484 input_location = token->location;
488 /* Return a pointer to the next token in the token stream, but do not
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
494 if (cp_lexer_debugging_p (lexer))
496 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498 putc ('\n', cp_lexer_debug_stream);
500 return lexer->next_token;
503 /* Return true if the next token has the indicated TYPE. */
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
508 return cp_lexer_peek_token (lexer)->type == type;
511 /* Return true if the next token does not have the indicated TYPE. */
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
516 return !cp_lexer_next_token_is (lexer, type);
519 /* Return true if the next token is the indicated KEYWORD. */
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
524 return cp_lexer_peek_token (lexer)->keyword == keyword;
527 /* Return true if the next token is not the indicated KEYWORD. */
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
532 return cp_lexer_peek_token (lexer)->keyword != keyword;
535 /* Return true if the next token is a keyword for a decl-specifier. */
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
542 token = cp_lexer_peek_token (lexer);
543 switch (token->keyword)
545 /* auto specifier: storage-class-specifier in C++,
546 simple-type-specifier in C++0x. */
548 /* Storage classes. */
554 /* Elaborated type specifiers. */
560 /* Simple type specifiers. */
574 /* GNU extensions. */
577 /* C++0x extensions. */
586 /* Return a pointer to the Nth token in the token stream. If N is 1,
587 then this is precisely equivalent to cp_lexer_peek_token (except
588 that it is not inline). One would like to disallow that case, but
589 there is one case (cp_parser_nth_token_starts_template_id) where
590 the caller passes a variable for N and it might be 1. */
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
597 /* N is 1-based, not zero-based. */
600 if (cp_lexer_debugging_p (lexer))
601 fprintf (cp_lexer_debug_stream,
602 "cp_lexer: peeking ahead %ld at token: ", (long)n);
605 token = lexer->next_token;
606 gcc_assert (!n || token != &eof_token);
610 if (token == lexer->last_token)
616 if (token->type != CPP_PURGED)
620 if (cp_lexer_debugging_p (lexer))
622 cp_lexer_print_token (cp_lexer_debug_stream, token);
623 putc ('\n', cp_lexer_debug_stream);
629 /* Return the next token, and advance the lexer's next_token pointer
630 to point to the next non-purged token. */
633 cp_lexer_consume_token (cp_lexer* lexer)
635 cp_token *token = lexer->next_token;
637 gcc_assert (token != &eof_token);
638 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
643 if (lexer->next_token == lexer->last_token)
645 lexer->next_token = &eof_token;
650 while (lexer->next_token->type == CPP_PURGED);
652 cp_lexer_set_source_position_from_token (token);
654 /* Provide debugging output. */
655 if (cp_lexer_debugging_p (lexer))
657 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658 cp_lexer_print_token (cp_lexer_debug_stream, token);
659 putc ('\n', cp_lexer_debug_stream);
665 /* Permanently remove the next token from the token stream, and
666 advance the next_token pointer to refer to the next non-purged
670 cp_lexer_purge_token (cp_lexer *lexer)
672 cp_token *tok = lexer->next_token;
674 gcc_assert (tok != &eof_token);
675 tok->type = CPP_PURGED;
676 tok->location = UNKNOWN_LOCATION;
677 tok->u.value = NULL_TREE;
678 tok->keyword = RID_MAX;
683 if (tok == lexer->last_token)
689 while (tok->type == CPP_PURGED);
690 lexer->next_token = tok;
693 /* Permanently remove all tokens after TOK, up to, but not
694 including, the token that will be returned next by
695 cp_lexer_peek_token. */
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 cp_token *peek = lexer->next_token;
702 if (peek == &eof_token)
703 peek = lexer->last_token;
705 gcc_assert (tok < peek);
707 for ( tok += 1; tok != peek; tok += 1)
709 tok->type = CPP_PURGED;
710 tok->location = UNKNOWN_LOCATION;
711 tok->u.value = NULL_TREE;
712 tok->keyword = RID_MAX;
716 /* Begin saving tokens. All tokens consumed after this point will be
720 cp_lexer_save_tokens (cp_lexer* lexer)
722 /* Provide debugging output. */
723 if (cp_lexer_debugging_p (lexer))
724 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726 VEC_safe_push (cp_token_position, heap,
727 lexer->saved_tokens, lexer->next_token);
730 /* Commit to the portion of the token stream most recently saved. */
733 cp_lexer_commit_tokens (cp_lexer* lexer)
735 /* Provide debugging output. */
736 if (cp_lexer_debugging_p (lexer))
737 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739 VEC_pop (cp_token_position, lexer->saved_tokens);
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743 to the token stream. Stop saving tokens. */
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 /* Provide debugging output. */
749 if (cp_lexer_debugging_p (lexer))
750 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
755 /* Print a representation of the TOKEN on the STREAM. */
757 #ifdef ENABLE_CHECKING
760 cp_lexer_print_token (FILE * stream, cp_token *token)
762 /* We don't use cpp_type2name here because the parser defines
763 a few tokens of its own. */
764 static const char *const token_names[] = {
765 /* cpplib-defined token types */
771 /* C++ parser token types - see "Manifest constants", above. */
774 "NESTED_NAME_SPECIFIER",
778 /* If we have a name for the token, print it out. Otherwise, we
779 simply give the numeric code. */
780 gcc_assert (token->type < ARRAY_SIZE(token_names));
781 fputs (token_names[token->type], stream);
783 /* For some tokens, print the associated data. */
787 /* Some keywords have a value that is not an IDENTIFIER_NODE.
788 For example, `struct' is mapped to an INTEGER_CST. */
789 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791 /* else fall through */
793 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1016 cp_parameter_declarator *parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1083 declarator = declarator->declarator;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1109 clear_decl_specs (¶meter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1120 function_declarator_p (const cp_declarator *declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1130 declarator = declarator->declarator;
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 typedef enum cp_parser_flags
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, do not allow user-defined types. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 /* The different kinds of declarators we want to parse. */
1206 typedef enum cp_parser_declarator_kind
1208 /* We want an abstract declarator. */
1209 CP_PARSER_DECLARATOR_ABSTRACT,
1210 /* We want a named declarator. */
1211 CP_PARSER_DECLARATOR_NAMED,
1212 /* We don't mind, but the name must be an unqualified-id. */
1213 CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1216 /* The precedence values used to parse binary expressions. The minimum value
1217 of PREC must be 1, because zero is reserved to quickly discriminate
1218 binary operators from other tokens. */
1223 PREC_LOGICAL_OR_EXPRESSION,
1224 PREC_LOGICAL_AND_EXPRESSION,
1225 PREC_INCLUSIVE_OR_EXPRESSION,
1226 PREC_EXCLUSIVE_OR_EXPRESSION,
1227 PREC_AND_EXPRESSION,
1228 PREC_EQUALITY_EXPRESSION,
1229 PREC_RELATIONAL_EXPRESSION,
1230 PREC_SHIFT_EXPRESSION,
1231 PREC_ADDITIVE_EXPRESSION,
1232 PREC_MULTIPLICATIVE_EXPRESSION,
1234 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238 precedence value. */
1240 typedef struct cp_parser_binary_operations_map_node
1242 /* The token type. */
1243 enum cpp_ttype token_type;
1244 /* The corresponding tree code. */
1245 enum tree_code tree_type;
1246 /* The precedence of this operator. */
1247 enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1250 /* The status of a tentative parse. */
1252 typedef enum cp_parser_status_kind
1254 /* No errors have occurred. */
1255 CP_PARSER_STATUS_KIND_NO_ERROR,
1256 /* An error has occurred. */
1257 CP_PARSER_STATUS_KIND_ERROR,
1258 /* We are committed to this tentative parse, whether or not an error
1260 CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1263 typedef struct cp_parser_expression_stack_entry
1265 /* Left hand side of the binary operation we are currently
1268 /* Original tree code for left hand side, if it was a binary
1269 expression itself (used for -Wparentheses). */
1270 enum tree_code lhs_type;
1271 /* Tree code for the binary operation we are parsing. */
1272 enum tree_code tree_type;
1273 /* Precedence of the binary operation we are parsing. */
1275 } cp_parser_expression_stack_entry;
1277 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1278 entries because precedence levels on the stack are monotonically
1280 typedef struct cp_parser_expression_stack_entry
1281 cp_parser_expression_stack[NUM_PREC_VALUES];
1283 /* Context that is saved and restored when parsing tentatively. */
1284 typedef struct cp_parser_context GTY (())
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct cp_parser GTY(())
1393 /* The lexer from which we are obtaining tokens. */
1396 /* The scope in which names should be looked up. If NULL_TREE, then
1397 we look up names in the scope that is currently open in the
1398 source program. If non-NULL, this is either a TYPE or
1399 NAMESPACE_DECL for the scope in which we should look. It can
1400 also be ERROR_MARK, when we've parsed a bogus scope.
1402 This value is not cleared automatically after a name is looked
1403 up, so we must be careful to clear it before starting a new look
1404 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1405 will look up `Z' in the scope of `X', rather than the current
1406 scope.) Unfortunately, it is difficult to tell when name lookup
1407 is complete, because we sometimes peek at a token, look it up,
1408 and then decide not to consume it. */
1411 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412 last lookup took place. OBJECT_SCOPE is used if an expression
1413 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414 respectively. QUALIFYING_SCOPE is used for an expression of the
1415 form "X::Y"; it refers to X. */
1417 tree qualifying_scope;
1419 /* A stack of parsing contexts. All but the bottom entry on the
1420 stack will be tentative contexts.
1422 We parse tentatively in order to determine which construct is in
1423 use in some situations. For example, in order to determine
1424 whether a statement is an expression-statement or a
1425 declaration-statement we parse it tentatively as a
1426 declaration-statement. If that fails, we then reparse the same
1427 token stream as an expression-statement. */
1428 cp_parser_context *context;
1430 /* True if we are parsing GNU C++. If this flag is not set, then
1431 GNU extensions are not recognized. */
1432 bool allow_gnu_extensions_p;
1434 /* TRUE if the `>' token should be interpreted as the greater-than
1435 operator. FALSE if it is the end of a template-id or
1436 template-parameter-list. In C++0x mode, this flag also applies to
1437 `>>' tokens, which are viewed as two consecutive `>' tokens when
1438 this flag is FALSE. */
1439 bool greater_than_is_operator_p;
1441 /* TRUE if default arguments are allowed within a parameter list
1442 that starts at this point. FALSE if only a gnu extension makes
1443 them permissible. */
1444 bool default_arg_ok_p;
1446 /* TRUE if we are parsing an integral constant-expression. See
1447 [expr.const] for a precise definition. */
1448 bool integral_constant_expression_p;
1450 /* TRUE if we are parsing an integral constant-expression -- but a
1451 non-constant expression should be permitted as well. This flag
1452 is used when parsing an array bound so that GNU variable-length
1453 arrays are tolerated. */
1454 bool allow_non_integral_constant_expression_p;
1456 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457 been seen that makes the expression non-constant. */
1458 bool non_integral_constant_expression_p;
1460 /* TRUE if local variable names and `this' are forbidden in the
1462 bool local_variables_forbidden_p;
1464 /* TRUE if the declaration we are parsing is part of a
1465 linkage-specification of the form `extern string-literal
1467 bool in_unbraced_linkage_specification_p;
1469 /* TRUE if we are presently parsing a declarator, after the
1470 direct-declarator. */
1471 bool in_declarator_p;
1473 /* TRUE if we are presently parsing a template-argument-list. */
1474 bool in_template_argument_list_p;
1476 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477 to IN_OMP_BLOCK if parsing OpenMP structured block and
1478 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1479 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480 iteration-statement, OpenMP block or loop within that switch. */
1481 #define IN_SWITCH_STMT 1
1482 #define IN_ITERATION_STMT 2
1483 #define IN_OMP_BLOCK 4
1484 #define IN_OMP_FOR 8
1485 #define IN_IF_STMT 16
1486 unsigned char in_statement;
1488 /* TRUE if we are presently parsing the body of a switch statement.
1489 Note that this doesn't quite overlap with in_statement above.
1490 The difference relates to giving the right sets of error messages:
1491 "case not in switch" vs "break statement used with OpenMP...". */
1492 bool in_switch_statement_p;
1494 /* TRUE if we are parsing a type-id in an expression context. In
1495 such a situation, both "type (expr)" and "type (type)" are valid
1497 bool in_type_id_in_expr_p;
1499 /* TRUE if we are currently in a header file where declarations are
1500 implicitly extern "C". */
1501 bool implicit_extern_c;
1503 /* TRUE if strings in expressions should be translated to the execution
1505 bool translate_strings_p;
1507 /* TRUE if we are presently parsing the body of a function, but not
1509 bool in_function_body;
1511 /* If non-NULL, then we are parsing a construct where new type
1512 definitions are not permitted. The string stored here will be
1513 issued as an error message if a type is defined. */
1514 const char *type_definition_forbidden_message;
1516 /* A list of lists. The outer list is a stack, used for member
1517 functions of local classes. At each level there are two sub-list,
1518 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520 TREE_VALUE's. The functions are chained in reverse declaration
1523 The TREE_PURPOSE sublist contains those functions with default
1524 arguments that need post processing, and the TREE_VALUE sublist
1525 contains those functions with definitions that need post
1528 These lists can only be processed once the outermost class being
1529 defined is complete. */
1530 tree unparsed_functions_queues;
1532 /* The number of classes whose definitions are currently in
1534 unsigned num_classes_being_defined;
1536 /* The number of template parameter lists that apply directly to the
1537 current declaration. */
1538 unsigned num_template_parameter_lists;
1543 /* Constructors and destructors. */
1545 static cp_parser *cp_parser_new
1548 /* Routines to parse various constructs.
1550 Those that return `tree' will return the error_mark_node (rather
1551 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552 Sometimes, they will return an ordinary node if error-recovery was
1553 attempted, even though a parse error occurred. So, to check
1554 whether or not a parse error occurred, you should always use
1555 cp_parser_error_occurred. If the construct is optional (indicated
1556 either by an `_opt' in the name of the function that does the
1557 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558 the construct is not present. */
1560 /* Lexical conventions [gram.lex] */
1562 static tree cp_parser_identifier
1564 static tree cp_parser_string_literal
1565 (cp_parser *, bool, bool);
1567 /* Basic concepts [gram.basic] */
1569 static bool cp_parser_translation_unit
1572 /* Expressions [gram.expr] */
1574 static tree cp_parser_primary_expression
1575 (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577 (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_class_or_namespace_name
1585 (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587 (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589 (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593 (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595 (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597 (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1600 static tree cp_parser_new_expression
1602 static tree cp_parser_new_placement
1604 static tree cp_parser_new_type_id
1605 (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1608 static cp_declarator *cp_parser_direct_new_declarator
1610 static tree cp_parser_new_initializer
1612 static tree cp_parser_delete_expression
1614 static tree cp_parser_cast_expression
1615 (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617 (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619 (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621 (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1624 static tree cp_parser_expression
1625 (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627 (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1631 /* Statements [gram.stmt.stmt] */
1633 static void cp_parser_statement
1634 (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1637 static tree cp_parser_expression_statement
1638 (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640 (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642 (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644 (cp_parser *, bool *);
1645 static tree cp_parser_condition
1647 static tree cp_parser_iteration_statement
1649 static void cp_parser_for_init_statement
1651 static tree cp_parser_jump_statement
1653 static void cp_parser_declaration_statement
1656 static tree cp_parser_implicitly_scoped_statement
1657 (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1661 /* Declarations [gram.dcl.dcl] */
1663 static void cp_parser_declaration_seq_opt
1665 static void cp_parser_declaration
1667 static void cp_parser_block_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1675 static tree cp_parser_function_specifier_opt
1676 (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1680 static tree cp_parser_simple_type_specifier
1681 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1684 static tree cp_parser_nonclass_name
1685 (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687 (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1690 static void cp_parser_enumerator_list
1691 (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693 (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1696 static void cp_parser_namespace_definition
1698 static void cp_parser_namespace_body
1700 static tree cp_parser_qualified_namespace_specifier
1702 static void cp_parser_namespace_alias_definition
1704 static bool cp_parser_using_declaration
1705 (cp_parser *, bool);
1706 static void cp_parser_using_directive
1708 static void cp_parser_asm_definition
1710 static void cp_parser_linkage_specification
1712 static void cp_parser_static_assert
1713 (cp_parser *, bool);
1714 static tree cp_parser_decltype
1717 /* Declarators [gram.dcl.decl] */
1719 static tree cp_parser_init_declarator
1720 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726 (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1729 static tree cp_parser_declarator_id
1730 (cp_parser *, bool);
1731 static tree cp_parser_type_id
1733 static void cp_parser_type_specifier_seq
1734 (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738 (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740 (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument
1742 (cp_parser *, bool);
1743 static void cp_parser_function_body
1745 static tree cp_parser_initializer
1746 (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748 (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750 (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752 (cp_parser *, bool *);
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1757 /* Classes [gram.class] */
1759 static tree cp_parser_class_name
1760 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1763 static tree cp_parser_class_head
1764 (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1767 static void cp_parser_member_specification_opt
1769 static void cp_parser_member_declaration
1771 static tree cp_parser_pure_specifier
1773 static tree cp_parser_constant_initializer
1776 /* Derived classes [gram.class.derived] */
1778 static tree cp_parser_base_clause
1780 static tree cp_parser_base_specifier
1783 /* Special member functions [gram.special] */
1785 static tree cp_parser_conversion_function_id
1787 static tree cp_parser_conversion_type_id
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1791 static bool cp_parser_ctor_initializer_opt
1793 static void cp_parser_mem_initializer_list
1795 static tree cp_parser_mem_initializer
1797 static tree cp_parser_mem_initializer_id
1800 /* Overloading [gram.over] */
1802 static tree cp_parser_operator_function_id
1804 static tree cp_parser_operator
1807 /* Templates [gram.temp] */
1809 static void cp_parser_template_declaration
1810 (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1813 static tree cp_parser_template_parameter
1814 (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816 (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818 (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820 (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1823 static tree cp_parser_template_argument
1825 static void cp_parser_explicit_instantiation
1827 static void cp_parser_explicit_specialization
1830 /* Exception handling [gram.exception] */
1832 static tree cp_parser_try_block
1834 static bool cp_parser_function_try_block
1836 static void cp_parser_handler_seq
1838 static void cp_parser_handler
1840 static tree cp_parser_exception_declaration
1842 static tree cp_parser_throw_expression
1844 static tree cp_parser_exception_specification_opt
1846 static tree cp_parser_type_id_list
1849 /* GNU Extensions */
1851 static tree cp_parser_asm_specification_opt
1853 static tree cp_parser_asm_operand_list
1855 static tree cp_parser_asm_clobber_list
1857 static tree cp_parser_attributes_opt
1859 static tree cp_parser_attribute_list
1861 static bool cp_parser_extension_opt
1862 (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868 (cp_parser *, enum pragma_context);
1870 /* Objective-C++ Productions */
1872 static tree cp_parser_objc_message_receiver
1874 static tree cp_parser_objc_message_args
1876 static tree cp_parser_objc_message_expression
1878 static tree cp_parser_objc_encode_expression
1880 static tree cp_parser_objc_defs_expression
1882 static tree cp_parser_objc_protocol_expression
1884 static tree cp_parser_objc_selector_expression
1886 static tree cp_parser_objc_expression
1888 static bool cp_parser_objc_selector_p
1890 static tree cp_parser_objc_selector
1892 static tree cp_parser_objc_protocol_refs_opt
1894 static void cp_parser_objc_declaration
1896 static tree cp_parser_objc_statement
1899 /* Utility Routines */
1901 static tree cp_parser_lookup_name
1902 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904 (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1907 static bool cp_parser_check_declarator_template_parameters
1908 (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910 (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1913 static tree cp_parser_global_scope_opt
1914 (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916 (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920 (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922 (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924 (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928 (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1933 static void cp_parser_save_default_args
1934 (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936 (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938 (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940 (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942 (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1945 static void cp_parser_set_storage_class
1946 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948 (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950 (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952 (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954 (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1957 static bool cp_parser_next_token_starts_class_definition_p
1959 static bool cp_parser_next_token_ends_template_argument_p
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962 (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1965 static void cp_parser_check_class_key
1966 (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968 (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1971 static void cp_parser_pre_parsed_nested_name_specifier
1973 static bool cp_parser_cache_group
1974 (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1977 static void cp_parser_commit_to_tentative_parse
1979 static void cp_parser_abort_tentative_parse
1981 static bool cp_parser_parse_definitely
1983 static inline bool cp_parser_parsing_tentatively
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1987 static void cp_parser_error
1988 (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990 (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1993 static bool cp_parser_check_type_definition
1995 static void cp_parser_check_for_definition_in_return_type
1996 (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998 (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000 (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002 (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2005 static int cp_parser_skip_to_closing_parenthesis
2006 (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2011 static void cp_parser_skip_to_end_of_block_or_statement
2013 static bool cp_parser_skip_to_closing_brace
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2017 static void cp_parser_skip_to_pragma_eol
2018 (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2021 static bool cp_parser_allow_gnu_extensions_p
2023 static bool cp_parser_is_string_literal
2025 static bool cp_parser_is_keyword
2026 (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028 (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2032 /* Returns nonzero if we are parsing tentatively. */
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2037 return parser->context->next != NULL;
2040 /* Returns nonzero if TOKEN is a string literal. */
2043 cp_parser_is_string_literal (cp_token* token)
2045 return (token->type == CPP_STRING ||
2046 token->type == CPP_STRING16 ||
2047 token->type == CPP_STRING32 ||
2048 token->type == CPP_WSTRING);
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2056 return token->keyword == keyword;
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060 FILE:LINE: MESSAGE before TOKEN
2061 where TOKEN is the next token in the input stream. MESSAGE
2062 (specified by the caller) is usually of the form "expected
2066 cp_parser_error (cp_parser* parser, const char* message)
2068 if (!cp_parser_simulate_error (parser))
2070 cp_token *token = cp_lexer_peek_token (parser->lexer);
2071 /* This diagnostic makes more sense if it is tagged to the line
2072 of the token we just peeked at. */
2073 cp_lexer_set_source_position_from_token (token);
2075 if (token->type == CPP_PRAGMA)
2077 error ("%H%<#pragma%> is not allowed here", &token->location);
2078 cp_parser_skip_to_pragma_eol (parser, token);
2082 c_parse_error (message,
2083 /* Because c_parser_error does not understand
2084 CPP_KEYWORD, keywords are treated like
2086 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091 /* Issue an error about name-lookup failing. NAME is the
2092 IDENTIFIER_NODE DECL is the result of
2093 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2094 the thing that we hoped to find. */
2097 cp_parser_name_lookup_error (cp_parser* parser,
2100 const char* desired,
2101 location_t location)
2103 /* If name lookup completely failed, tell the user that NAME was not
2105 if (decl == error_mark_node)
2107 if (parser->scope && parser->scope != global_namespace)
2108 error ("%H%<%E::%E%> has not been declared",
2109 &location, parser->scope, name);
2110 else if (parser->scope == global_namespace)
2111 error ("%H%<::%E%> has not been declared", &location, name);
2112 else if (parser->object_scope
2113 && !CLASS_TYPE_P (parser->object_scope))
2114 error ("%Hrequest for member %qE in non-class type %qT",
2115 &location, name, parser->object_scope);
2116 else if (parser->object_scope)
2117 error ("%H%<%T::%E%> has not been declared",
2118 &location, parser->object_scope, name);
2120 error ("%H%qE has not been declared", &location, name);
2122 else if (parser->scope && parser->scope != global_namespace)
2123 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124 else if (parser->scope == global_namespace)
2125 error ("%H%<::%E%> %s", &location, name, desired);
2127 error ("%H%qE %s", &location, name, desired);
2130 /* If we are parsing tentatively, remember that an error has occurred
2131 during this tentative parse. Returns true if the error was
2132 simulated; false if a message should be issued by the caller. */
2135 cp_parser_simulate_error (cp_parser* parser)
2137 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2139 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2145 /* Check for repeated decl-specifiers. */
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149 location_t location)
2153 for (ds = ds_first; ds != ds_last; ++ds)
2155 unsigned count = decl_specs->specs[(int)ds];
2158 /* The "long" specifier is a special case because of "long long". */
2162 error ("%H%<long long long%> is too long for GCC", &location);
2163 else if (pedantic && !in_system_header && warn_long_long
2164 && cxx_dialect == cxx98)
2165 pedwarn (OPT_Wlong_long,
2166 "%HISO C++ 1998 does not support %<long long%>",
2171 static const char *const decl_spec_names[] = {
2187 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2192 /* This function is called when a type is defined. If type
2193 definitions are forbidden at this point, an error message is
2197 cp_parser_check_type_definition (cp_parser* parser)
2199 /* If types are forbidden here, issue a message. */
2200 if (parser->type_definition_forbidden_message)
2202 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2203 in the message need to be interpreted. */
2204 error (parser->type_definition_forbidden_message);
2210 /* This function is called when the DECLARATOR is processed. The TYPE
2211 was a type defined in the decl-specifiers. If it is invalid to
2212 define a type in the decl-specifiers for DECLARATOR, an error is
2213 issued. TYPE_LOCATION is the location of TYPE and is used
2214 for error reporting. */
2217 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2218 tree type, location_t type_location)
2220 /* [dcl.fct] forbids type definitions in return types.
2221 Unfortunately, it's not easy to know whether or not we are
2222 processing a return type until after the fact. */
2224 && (declarator->kind == cdk_pointer
2225 || declarator->kind == cdk_reference
2226 || declarator->kind == cdk_ptrmem))
2227 declarator = declarator->declarator;
2229 && declarator->kind == cdk_function)
2231 error ("%Hnew types may not be defined in a return type", &type_location);
2232 inform (type_location,
2233 "(perhaps a semicolon is missing after the definition of %qT)",
2238 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2239 "<" in any valid C++ program. If the next token is indeed "<",
2240 issue a message warning the user about what appears to be an
2241 invalid attempt to form a template-id. LOCATION is the location
2242 of the type-specifier (TYPE) */
2245 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2246 tree type, location_t location)
2248 cp_token_position start = 0;
2250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2253 error ("%H%qT is not a template", &location, type);
2254 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2255 error ("%H%qE is not a template", &location, type);
2257 error ("%Hinvalid template-id", &location);
2258 /* Remember the location of the invalid "<". */
2259 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2260 start = cp_lexer_token_position (parser->lexer, true);
2261 /* Consume the "<". */
2262 cp_lexer_consume_token (parser->lexer);
2263 /* Parse the template arguments. */
2264 cp_parser_enclosed_template_argument_list (parser);
2265 /* Permanently remove the invalid template arguments so that
2266 this error message is not issued again. */
2268 cp_lexer_purge_tokens_after (parser->lexer, start);
2272 /* If parsing an integral constant-expression, issue an error message
2273 about the fact that THING appeared and return true. Otherwise,
2274 return false. In either case, set
2275 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2278 cp_parser_non_integral_constant_expression (cp_parser *parser,
2281 parser->non_integral_constant_expression_p = true;
2282 if (parser->integral_constant_expression_p)
2284 if (!parser->allow_non_integral_constant_expression_p)
2286 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2287 in the message need to be interpreted. */
2288 char *message = concat (thing,
2289 " cannot appear in a constant-expression",
2299 /* Emit a diagnostic for an invalid type name. SCOPE is the
2300 qualifying scope (or NULL, if none) for ID. This function commits
2301 to the current active tentative parse, if any. (Otherwise, the
2302 problematic construct might be encountered again later, resulting
2303 in duplicate error messages.) LOCATION is the location of ID. */
2306 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2307 tree scope, tree id,
2308 location_t location)
2310 tree decl, old_scope;
2311 /* Try to lookup the identifier. */
2312 old_scope = parser->scope;
2313 parser->scope = scope;
2314 decl = cp_parser_lookup_name_simple (parser, id, location);
2315 parser->scope = old_scope;
2316 /* If the lookup found a template-name, it means that the user forgot
2317 to specify an argument list. Emit a useful error message. */
2318 if (TREE_CODE (decl) == TEMPLATE_DECL)
2319 error ("%Hinvalid use of template-name %qE without an argument list",
2321 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2322 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2323 else if (TREE_CODE (decl) == TYPE_DECL)
2324 /* Something like 'unsigned A a;' */
2325 error ("%Hinvalid combination of multiple type-specifiers",
2327 else if (!parser->scope)
2329 /* Issue an error message. */
2330 error ("%H%qE does not name a type", &location, id);
2331 /* If we're in a template class, it's possible that the user was
2332 referring to a type from a base class. For example:
2334 template <typename T> struct A { typedef T X; };
2335 template <typename T> struct B : public A<T> { X x; };
2337 The user should have said "typename A<T>::X". */
2338 if (processing_template_decl && current_class_type
2339 && TYPE_BINFO (current_class_type))
2343 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347 tree base_type = BINFO_TYPE (b);
2348 if (CLASS_TYPE_P (base_type)
2349 && dependent_type_p (base_type))
2352 /* Go from a particular instantiation of the
2353 template (which will have an empty TYPE_FIELDs),
2354 to the main version. */
2355 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2356 for (field = TYPE_FIELDS (base_type);
2358 field = TREE_CHAIN (field))
2359 if (TREE_CODE (field) == TYPE_DECL
2360 && DECL_NAME (field) == id)
2363 "(perhaps %<typename %T::%E%> was intended)",
2364 BINFO_TYPE (b), id);
2373 /* Here we diagnose qualified-ids where the scope is actually correct,
2374 but the identifier does not resolve to a valid type name. */
2375 else if (parser->scope != error_mark_node)
2377 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2378 error ("%H%qE in namespace %qE does not name a type",
2379 &location, id, parser->scope);
2380 else if (TYPE_P (parser->scope))
2381 error ("%H%qE in class %qT does not name a type",
2382 &location, id, parser->scope);
2386 cp_parser_commit_to_tentative_parse (parser);
2389 /* Check for a common situation where a type-name should be present,
2390 but is not, and issue a sensible error message. Returns true if an
2391 invalid type-name was detected.
2393 The situation handled by this function are variable declarations of the
2394 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2395 Usually, `ID' should name a type, but if we got here it means that it
2396 does not. We try to emit the best possible error message depending on
2397 how exactly the id-expression looks like. */
2400 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2403 cp_token *token = cp_lexer_peek_token (parser->lexer);
2405 cp_parser_parse_tentatively (parser);
2406 id = cp_parser_id_expression (parser,
2407 /*template_keyword_p=*/false,
2408 /*check_dependency_p=*/true,
2409 /*template_p=*/NULL,
2410 /*declarator_p=*/true,
2411 /*optional_p=*/false);
2412 /* After the id-expression, there should be a plain identifier,
2413 otherwise this is not a simple variable declaration. Also, if
2414 the scope is dependent, we cannot do much. */
2415 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2416 || (parser->scope && TYPE_P (parser->scope)
2417 && dependent_type_p (parser->scope))
2418 || TREE_CODE (id) == TYPE_DECL)
2420 cp_parser_abort_tentative_parse (parser);
2423 if (!cp_parser_parse_definitely (parser))
2426 /* Emit a diagnostic for the invalid type. */
2427 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2428 id, token->location);
2429 /* Skip to the end of the declaration; there's no point in
2430 trying to process it. */
2431 cp_parser_skip_to_end_of_block_or_statement (parser);
2435 /* Consume tokens up to, and including, the next non-nested closing `)'.
2436 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2437 are doing error recovery. Returns -1 if OR_COMMA is true and we
2438 found an unnested comma. */
2441 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2446 unsigned paren_depth = 0;
2447 unsigned brace_depth = 0;
2449 if (recovering && !or_comma
2450 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2455 cp_token * token = cp_lexer_peek_token (parser->lexer);
2457 switch (token->type)
2460 case CPP_PRAGMA_EOL:
2461 /* If we've run out of tokens, then there is no closing `)'. */
2465 /* This matches the processing in skip_to_end_of_statement. */
2470 case CPP_OPEN_BRACE:
2473 case CPP_CLOSE_BRACE:
2479 if (recovering && or_comma && !brace_depth && !paren_depth)
2483 case CPP_OPEN_PAREN:
2488 case CPP_CLOSE_PAREN:
2489 if (!brace_depth && !paren_depth--)
2492 cp_lexer_consume_token (parser->lexer);
2501 /* Consume the token. */
2502 cp_lexer_consume_token (parser->lexer);
2506 /* Consume tokens until we reach the end of the current statement.
2507 Normally, that will be just before consuming a `;'. However, if a
2508 non-nested `}' comes first, then we stop before consuming that. */
2511 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2513 unsigned nesting_depth = 0;
2517 cp_token *token = cp_lexer_peek_token (parser->lexer);
2519 switch (token->type)
2522 case CPP_PRAGMA_EOL:
2523 /* If we've run out of tokens, stop. */
2527 /* If the next token is a `;', we have reached the end of the
2533 case CPP_CLOSE_BRACE:
2534 /* If this is a non-nested '}', stop before consuming it.
2535 That way, when confronted with something like:
2539 we stop before consuming the closing '}', even though we
2540 have not yet reached a `;'. */
2541 if (nesting_depth == 0)
2544 /* If it is the closing '}' for a block that we have
2545 scanned, stop -- but only after consuming the token.
2551 we will stop after the body of the erroneously declared
2552 function, but before consuming the following `typedef'
2554 if (--nesting_depth == 0)
2556 cp_lexer_consume_token (parser->lexer);
2560 case CPP_OPEN_BRACE:
2568 /* Consume the token. */
2569 cp_lexer_consume_token (parser->lexer);
2573 /* This function is called at the end of a statement or declaration.
2574 If the next token is a semicolon, it is consumed; otherwise, error
2575 recovery is attempted. */
2578 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2580 /* Look for the trailing `;'. */
2581 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2583 /* If there is additional (erroneous) input, skip to the end of
2585 cp_parser_skip_to_end_of_statement (parser);
2586 /* If the next token is now a `;', consume it. */
2587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2588 cp_lexer_consume_token (parser->lexer);
2592 /* Skip tokens until we have consumed an entire block, or until we
2593 have consumed a non-nested `;'. */
2596 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2598 int nesting_depth = 0;
2600 while (nesting_depth >= 0)
2602 cp_token *token = cp_lexer_peek_token (parser->lexer);
2604 switch (token->type)
2607 case CPP_PRAGMA_EOL:
2608 /* If we've run out of tokens, stop. */
2612 /* Stop if this is an unnested ';'. */
2617 case CPP_CLOSE_BRACE:
2618 /* Stop if this is an unnested '}', or closes the outermost
2625 case CPP_OPEN_BRACE:
2634 /* Consume the token. */
2635 cp_lexer_consume_token (parser->lexer);
2639 /* Skip tokens until a non-nested closing curly brace is the next
2640 token, or there are no more tokens. Return true in the first case,
2644 cp_parser_skip_to_closing_brace (cp_parser *parser)
2646 unsigned nesting_depth = 0;
2650 cp_token *token = cp_lexer_peek_token (parser->lexer);
2652 switch (token->type)
2655 case CPP_PRAGMA_EOL:
2656 /* If we've run out of tokens, stop. */
2659 case CPP_CLOSE_BRACE:
2660 /* If the next token is a non-nested `}', then we have reached
2661 the end of the current block. */
2662 if (nesting_depth-- == 0)
2666 case CPP_OPEN_BRACE:
2667 /* If it the next token is a `{', then we are entering a new
2668 block. Consume the entire block. */
2676 /* Consume the token. */
2677 cp_lexer_consume_token (parser->lexer);
2681 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2682 parameter is the PRAGMA token, allowing us to purge the entire pragma
2686 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 parser->lexer->in_pragma = false;
2693 token = cp_lexer_consume_token (parser->lexer);
2694 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2696 /* Ensure that the pragma is not parsed again. */
2697 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2700 /* Require pragma end of line, resyncing with it as necessary. The
2701 arguments are as for cp_parser_skip_to_pragma_eol. */
2704 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2706 parser->lexer->in_pragma = false;
2707 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2711 /* This is a simple wrapper around make_typename_type. When the id is
2712 an unresolved identifier node, we can provide a superior diagnostic
2713 using cp_parser_diagnose_invalid_type_name. */
2716 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2717 tree id, location_t id_location)
2720 if (TREE_CODE (id) == IDENTIFIER_NODE)
2722 result = make_typename_type (scope, id, typename_type,
2723 /*complain=*/tf_none);
2724 if (result == error_mark_node)
2725 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2728 return make_typename_type (scope, id, typename_type, tf_error);
2731 /* This is a wrapper around the
2732 make_{pointer,ptrmem,reference}_declarator functions that decides
2733 which one to call based on the CODE and CLASS_TYPE arguments. The
2734 CODE argument should be one of the values returned by
2735 cp_parser_ptr_operator. */
2736 static cp_declarator *
2737 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2738 cp_cv_quals cv_qualifiers,
2739 cp_declarator *target)
2741 if (code == ERROR_MARK)
2742 return cp_error_declarator;
2744 if (code == INDIRECT_REF)
2745 if (class_type == NULL_TREE)
2746 return make_pointer_declarator (cv_qualifiers, target);
2748 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2749 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2750 return make_reference_declarator (cv_qualifiers, target, false);
2751 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2752 return make_reference_declarator (cv_qualifiers, target, true);
2756 /* Create a new C++ parser. */
2759 cp_parser_new (void)
2765 /* cp_lexer_new_main is called before calling ggc_alloc because
2766 cp_lexer_new_main might load a PCH file. */
2767 lexer = cp_lexer_new_main ();
2769 /* Initialize the binops_by_token so that we can get the tree
2770 directly from the token. */
2771 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2772 binops_by_token[binops[i].token_type] = binops[i];
2774 parser = GGC_CNEW (cp_parser);
2775 parser->lexer = lexer;
2776 parser->context = cp_parser_context_new (NULL);
2778 /* For now, we always accept GNU extensions. */
2779 parser->allow_gnu_extensions_p = 1;
2781 /* The `>' token is a greater-than operator, not the end of a
2783 parser->greater_than_is_operator_p = true;
2785 parser->default_arg_ok_p = true;
2787 /* We are not parsing a constant-expression. */
2788 parser->integral_constant_expression_p = false;
2789 parser->allow_non_integral_constant_expression_p = false;
2790 parser->non_integral_constant_expression_p = false;
2792 /* Local variable names are not forbidden. */
2793 parser->local_variables_forbidden_p = false;
2795 /* We are not processing an `extern "C"' declaration. */
2796 parser->in_unbraced_linkage_specification_p = false;
2798 /* We are not processing a declarator. */
2799 parser->in_declarator_p = false;
2801 /* We are not processing a template-argument-list. */
2802 parser->in_template_argument_list_p = false;
2804 /* We are not in an iteration statement. */
2805 parser->in_statement = 0;
2807 /* We are not in a switch statement. */
2808 parser->in_switch_statement_p = false;
2810 /* We are not parsing a type-id inside an expression. */
2811 parser->in_type_id_in_expr_p = false;
2813 /* Declarations aren't implicitly extern "C". */
2814 parser->implicit_extern_c = false;
2816 /* String literals should be translated to the execution character set. */
2817 parser->translate_strings_p = true;
2819 /* We are not parsing a function body. */
2820 parser->in_function_body = false;
2822 /* The unparsed function queue is empty. */
2823 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2825 /* There are no classes being defined. */
2826 parser->num_classes_being_defined = 0;
2828 /* No template parameters apply. */
2829 parser->num_template_parameter_lists = 0;
2834 /* Create a cp_lexer structure which will emit the tokens in CACHE
2835 and push it onto the parser's lexer stack. This is used for delayed
2836 parsing of in-class method bodies and default arguments, and should
2837 not be confused with tentative parsing. */
2839 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2841 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2842 lexer->next = parser->lexer;
2843 parser->lexer = lexer;
2845 /* Move the current source position to that of the first token in the
2847 cp_lexer_set_source_position_from_token (lexer->next_token);
2850 /* Pop the top lexer off the parser stack. This is never used for the
2851 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2853 cp_parser_pop_lexer (cp_parser *parser)
2855 cp_lexer *lexer = parser->lexer;
2856 parser->lexer = lexer->next;
2857 cp_lexer_destroy (lexer);
2859 /* Put the current source position back where it was before this
2860 lexer was pushed. */
2861 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2864 /* Lexical conventions [gram.lex] */
2866 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2870 cp_parser_identifier (cp_parser* parser)
2874 /* Look for the identifier. */
2875 token = cp_parser_require (parser, CPP_NAME, "identifier");
2876 /* Return the value. */
2877 return token ? token->u.value : error_mark_node;
2880 /* Parse a sequence of adjacent string constants. Returns a
2881 TREE_STRING representing the combined, nul-terminated string
2882 constant. If TRANSLATE is true, translate the string to the
2883 execution character set. If WIDE_OK is true, a wide string is
2886 C++98 [lex.string] says that if a narrow string literal token is
2887 adjacent to a wide string literal token, the behavior is undefined.
2888 However, C99 6.4.5p4 says that this results in a wide string literal.
2889 We follow C99 here, for consistency with the C front end.
2891 This code is largely lifted from lex_string() in c-lex.c.
2893 FUTURE: ObjC++ will need to handle @-strings here. */
2895 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 struct obstack str_ob;
2900 cpp_string str, istr, *strs;
2902 enum cpp_ttype type;
2904 tok = cp_lexer_peek_token (parser->lexer);
2905 if (!cp_parser_is_string_literal (tok))
2907 cp_parser_error (parser, "expected string-literal");
2908 return error_mark_node;
2913 /* Try to avoid the overhead of creating and destroying an obstack
2914 for the common case of just one string. */
2915 if (!cp_parser_is_string_literal
2916 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2918 cp_lexer_consume_token (parser->lexer);
2920 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2921 str.len = TREE_STRING_LENGTH (tok->u.value);
2928 gcc_obstack_init (&str_ob);
2933 cp_lexer_consume_token (parser->lexer);
2935 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2936 str.len = TREE_STRING_LENGTH (tok->u.value);
2938 if (type != tok->type)
2940 if (type == CPP_STRING)
2942 else if (tok->type != CPP_STRING)
2943 error ("%Hunsupported non-standard concatenation "
2944 "of string literals", &tok->location);
2947 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2949 tok = cp_lexer_peek_token (parser->lexer);
2951 while (cp_parser_is_string_literal (tok));
2953 strs = (cpp_string *) obstack_finish (&str_ob);
2956 if (type != CPP_STRING && !wide_ok)
2958 cp_parser_error (parser, "a wide string is invalid in this context");
2962 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2963 (parse_in, strs, count, &istr, type))
2965 value = build_string (istr.len, (const char *)istr.text);
2966 free (CONST_CAST (unsigned char *, istr.text));
2972 TREE_TYPE (value) = char_array_type_node;
2975 TREE_TYPE (value) = char16_array_type_node;
2978 TREE_TYPE (value) = char32_array_type_node;
2981 TREE_TYPE (value) = wchar_array_type_node;
2985 value = fix_string_type (value);
2988 /* cpp_interpret_string has issued an error. */
2989 value = error_mark_node;
2992 obstack_free (&str_ob, 0);
2998 /* Basic concepts [gram.basic] */
3000 /* Parse a translation-unit.
3003 declaration-seq [opt]
3005 Returns TRUE if all went well. */
3008 cp_parser_translation_unit (cp_parser* parser)
3010 /* The address of the first non-permanent object on the declarator
3012 static void *declarator_obstack_base;
3016 /* Create the declarator obstack, if necessary. */
3017 if (!cp_error_declarator)
3019 gcc_obstack_init (&declarator_obstack);
3020 /* Create the error declarator. */
3021 cp_error_declarator = make_declarator (cdk_error);
3022 /* Create the empty parameter list. */
3023 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3024 /* Remember where the base of the declarator obstack lies. */
3025 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3028 cp_parser_declaration_seq_opt (parser);
3030 /* If there are no tokens left then all went well. */
3031 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3033 /* Get rid of the token array; we don't need it any more. */
3034 cp_lexer_destroy (parser->lexer);
3035 parser->lexer = NULL;
3037 /* This file might have been a context that's implicitly extern
3038 "C". If so, pop the lang context. (Only relevant for PCH.) */
3039 if (parser->implicit_extern_c)
3041 pop_lang_context ();
3042 parser->implicit_extern_c = false;
3046 finish_translation_unit ();
3052 cp_parser_error (parser, "expected declaration");
3056 /* Make sure the declarator obstack was fully cleaned up. */
3057 gcc_assert (obstack_next_free (&declarator_obstack)
3058 == declarator_obstack_base);
3060 /* All went well. */
3064 /* Expressions [gram.expr] */
3066 /* Parse a primary-expression.
3077 ( compound-statement )
3078 __builtin_va_arg ( assignment-expression , type-id )
3079 __builtin_offsetof ( type-id , offsetof-expression )
3082 __has_nothrow_assign ( type-id )
3083 __has_nothrow_constructor ( type-id )
3084 __has_nothrow_copy ( type-id )
3085 __has_trivial_assign ( type-id )
3086 __has_trivial_constructor ( type-id )
3087 __has_trivial_copy ( type-id )
3088 __has_trivial_destructor ( type-id )
3089 __has_virtual_destructor ( type-id )
3090 __is_abstract ( type-id )
3091 __is_base_of ( type-id , type-id )
3092 __is_class ( type-id )
3093 __is_convertible_to ( type-id , type-id )
3094 __is_empty ( type-id )
3095 __is_enum ( type-id )
3096 __is_pod ( type-id )
3097 __is_polymorphic ( type-id )
3098 __is_union ( type-id )
3100 Objective-C++ Extension:
3108 ADDRESS_P is true iff this expression was immediately preceded by
3109 "&" and therefore might denote a pointer-to-member. CAST_P is true
3110 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3111 true iff this expression is a template argument.
3113 Returns a representation of the expression. Upon return, *IDK
3114 indicates what kind of id-expression (if any) was present. */
3117 cp_parser_primary_expression (cp_parser *parser,
3120 bool template_arg_p,
3123 cp_token *token = NULL;
3125 /* Assume the primary expression is not an id-expression. */
3126 *idk = CP_ID_KIND_NONE;
3128 /* Peek at the next token. */
3129 token = cp_lexer_peek_token (parser->lexer);
3130 switch (token->type)
3143 token = cp_lexer_consume_token (parser->lexer);
3144 /* Floating-point literals are only allowed in an integral
3145 constant expression if they are cast to an integral or
3146 enumeration type. */
3147 if (TREE_CODE (token->u.value) == REAL_CST
3148 && parser->integral_constant_expression_p
3151 /* CAST_P will be set even in invalid code like "int(2.7 +
3152 ...)". Therefore, we have to check that the next token
3153 is sure to end the cast. */
3156 cp_token *next_token;
3158 next_token = cp_lexer_peek_token (parser->lexer);
3159 if (/* The comma at the end of an
3160 enumerator-definition. */
3161 next_token->type != CPP_COMMA
3162 /* The curly brace at the end of an enum-specifier. */
3163 && next_token->type != CPP_CLOSE_BRACE
3164 /* The end of a statement. */
3165 && next_token->type != CPP_SEMICOLON
3166 /* The end of the cast-expression. */
3167 && next_token->type != CPP_CLOSE_PAREN
3168 /* The end of an array bound. */
3169 && next_token->type != CPP_CLOSE_SQUARE
3170 /* The closing ">" in a template-argument-list. */
3171 && (next_token->type != CPP_GREATER
3172 || parser->greater_than_is_operator_p)
3173 /* C++0x only: A ">>" treated like two ">" tokens,
3174 in a template-argument-list. */
3175 && (next_token->type != CPP_RSHIFT
3176 || (cxx_dialect == cxx98)
3177 || parser->greater_than_is_operator_p))
3181 /* If we are within a cast, then the constraint that the
3182 cast is to an integral or enumeration type will be
3183 checked at that point. If we are not within a cast, then
3184 this code is invalid. */
3186 cp_parser_non_integral_constant_expression
3187 (parser, "floating-point literal");
3189 return token->u.value;
3195 /* ??? Should wide strings be allowed when parser->translate_strings_p
3196 is false (i.e. in attributes)? If not, we can kill the third
3197 argument to cp_parser_string_literal. */
3198 return cp_parser_string_literal (parser,
3199 parser->translate_strings_p,
3202 case CPP_OPEN_PAREN:
3205 bool saved_greater_than_is_operator_p;
3207 /* Consume the `('. */
3208 cp_lexer_consume_token (parser->lexer);
3209 /* Within a parenthesized expression, a `>' token is always
3210 the greater-than operator. */
3211 saved_greater_than_is_operator_p
3212 = parser->greater_than_is_operator_p;
3213 parser->greater_than_is_operator_p = true;
3214 /* If we see `( { ' then we are looking at the beginning of
3215 a GNU statement-expression. */
3216 if (cp_parser_allow_gnu_extensions_p (parser)
3217 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3219 /* Statement-expressions are not allowed by the standard. */
3220 pedwarn (OPT_pedantic,
3221 "%HISO C++ forbids braced-groups within expressions",
3224 /* And they're not allowed outside of a function-body; you
3225 cannot, for example, write:
3227 int i = ({ int j = 3; j + 1; });
3229 at class or namespace scope. */
3230 if (!parser->in_function_body
3231 || parser->in_template_argument_list_p)
3233 error ("%Hstatement-expressions are not allowed outside "
3234 "functions nor in template-argument lists",
3236 cp_parser_skip_to_end_of_block_or_statement (parser);
3237 expr = error_mark_node;
3241 /* Start the statement-expression. */
3242 expr = begin_stmt_expr ();
3243 /* Parse the compound-statement. */
3244 cp_parser_compound_statement (parser, expr, false);
3246 expr = finish_stmt_expr (expr, false);
3251 /* Parse the parenthesized expression. */
3252 expr = cp_parser_expression (parser, cast_p);
3253 /* Let the front end know that this expression was
3254 enclosed in parentheses. This matters in case, for
3255 example, the expression is of the form `A::B', since
3256 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3258 finish_parenthesized_expr (expr);
3260 /* The `>' token might be the end of a template-id or
3261 template-parameter-list now. */
3262 parser->greater_than_is_operator_p
3263 = saved_greater_than_is_operator_p;
3264 /* Consume the `)'. */
3265 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3266 cp_parser_skip_to_end_of_statement (parser);
3272 switch (token->keyword)
3274 /* These two are the boolean literals. */
3276 cp_lexer_consume_token (parser->lexer);
3277 return boolean_true_node;
3279 cp_lexer_consume_token (parser->lexer);
3280 return boolean_false_node;
3282 /* The `__null' literal. */
3284 cp_lexer_consume_token (parser->lexer);
3287 /* Recognize the `this' keyword. */
3289 cp_lexer_consume_token (parser->lexer);
3290 if (parser->local_variables_forbidden_p)
3292 error ("%H%<this%> may not be used in this context",
3294 return error_mark_node;
3296 /* Pointers cannot appear in constant-expressions. */
3297 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3298 return error_mark_node;
3299 return finish_this_expr ();
3301 /* The `operator' keyword can be the beginning of an
3306 case RID_FUNCTION_NAME:
3307 case RID_PRETTY_FUNCTION_NAME:
3308 case RID_C99_FUNCTION_NAME:
3309 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3310 __func__ are the names of variables -- but they are
3311 treated specially. Therefore, they are handled here,
3312 rather than relying on the generic id-expression logic
3313 below. Grammatically, these names are id-expressions.
3315 Consume the token. */
3316 token = cp_lexer_consume_token (parser->lexer);
3317 /* Look up the name. */
3318 return finish_fname (token->u.value);
3325 /* The `__builtin_va_arg' construct is used to handle
3326 `va_arg'. Consume the `__builtin_va_arg' token. */
3327 cp_lexer_consume_token (parser->lexer);
3328 /* Look for the opening `('. */
3329 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3330 /* Now, parse the assignment-expression. */
3331 expression = cp_parser_assignment_expression (parser,
3333 /* Look for the `,'. */
3334 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3335 /* Parse the type-id. */
3336 type = cp_parser_type_id (parser);
3337 /* Look for the closing `)'. */
3338 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3339 /* Using `va_arg' in a constant-expression is not
3341 if (cp_parser_non_integral_constant_expression (parser,
3343 return error_mark_node;
3344 return build_x_va_arg (expression, type);
3348 return cp_parser_builtin_offsetof (parser);
3350 case RID_HAS_NOTHROW_ASSIGN:
3351 case RID_HAS_NOTHROW_CONSTRUCTOR:
3352 case RID_HAS_NOTHROW_COPY:
3353 case RID_HAS_TRIVIAL_ASSIGN:
3354 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3355 case RID_HAS_TRIVIAL_COPY:
3356 case RID_HAS_TRIVIAL_DESTRUCTOR:
3357 case RID_HAS_VIRTUAL_DESTRUCTOR:
3358 case RID_IS_ABSTRACT:
3359 case RID_IS_BASE_OF:
3361 case RID_IS_CONVERTIBLE_TO:
3365 case RID_IS_POLYMORPHIC:
3367 return cp_parser_trait_expr (parser, token->keyword);
3369 /* Objective-C++ expressions. */
3371 case RID_AT_PROTOCOL:
3372 case RID_AT_SELECTOR:
3373 return cp_parser_objc_expression (parser);
3376 cp_parser_error (parser, "expected primary-expression");
3377 return error_mark_node;
3380 /* An id-expression can start with either an identifier, a
3381 `::' as the beginning of a qualified-id, or the "operator"
3385 case CPP_TEMPLATE_ID:
3386 case CPP_NESTED_NAME_SPECIFIER:
3390 const char *error_msg;
3393 cp_token *id_expr_token;
3396 /* Parse the id-expression. */
3398 = cp_parser_id_expression (parser,
3399 /*template_keyword_p=*/false,
3400 /*check_dependency_p=*/true,
3402 /*declarator_p=*/false,
3403 /*optional_p=*/false);
3404 if (id_expression == error_mark_node)
3405 return error_mark_node;
3406 id_expr_token = token;
3407 token = cp_lexer_peek_token (parser->lexer);
3408 done = (token->type != CPP_OPEN_SQUARE
3409 && token->type != CPP_OPEN_PAREN
3410 && token->type != CPP_DOT
3411 && token->type != CPP_DEREF
3412 && token->type != CPP_PLUS_PLUS
3413 && token->type != CPP_MINUS_MINUS);
3414 /* If we have a template-id, then no further lookup is
3415 required. If the template-id was for a template-class, we
3416 will sometimes have a TYPE_DECL at this point. */
3417 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3418 || TREE_CODE (id_expression) == TYPE_DECL)
3419 decl = id_expression;
3420 /* Look up the name. */
3423 tree ambiguous_decls;
3425 decl = cp_parser_lookup_name (parser, id_expression,
3428 /*is_namespace=*/false,
3429 /*check_dependency=*/true,
3431 id_expr_token->location);
3432 /* If the lookup was ambiguous, an error will already have
3434 if (ambiguous_decls)
3435 return error_mark_node;
3437 /* In Objective-C++, an instance variable (ivar) may be preferred
3438 to whatever cp_parser_lookup_name() found. */
3439 decl = objc_lookup_ivar (decl, id_expression);
3441 /* If name lookup gives us a SCOPE_REF, then the
3442 qualifying scope was dependent. */
3443 if (TREE_CODE (decl) == SCOPE_REF)
3445 /* At this point, we do not know if DECL is a valid
3446 integral constant expression. We assume that it is
3447 in fact such an expression, so that code like:
3449 template <int N> struct A {
3453 is accepted. At template-instantiation time, we
3454 will check that B<N>::i is actually a constant. */
3457 /* Check to see if DECL is a local variable in a context
3458 where that is forbidden. */
3459 if (parser->local_variables_forbidden_p
3460 && local_variable_p (decl))
3462 /* It might be that we only found DECL because we are
3463 trying to be generous with pre-ISO scoping rules.
3464 For example, consider:
3468 for (int i = 0; i < 10; ++i) {}
3469 extern void f(int j = i);
3472 Here, name look up will originally find the out
3473 of scope `i'. We need to issue a warning message,
3474 but then use the global `i'. */
3475 decl = check_for_out_of_scope_variable (decl);
3476 if (local_variable_p (decl))
3478 error ("%Hlocal variable %qD may not appear in this context",
3479 &id_expr_token->location, decl);
3480 return error_mark_node;
3485 decl = (finish_id_expression
3486 (id_expression, decl, parser->scope,
3488 parser->integral_constant_expression_p,
3489 parser->allow_non_integral_constant_expression_p,
3490 &parser->non_integral_constant_expression_p,
3491 template_p, done, address_p,
3494 id_expr_token->location));
3496 cp_parser_error (parser, error_msg);
3500 /* Anything else is an error. */
3502 /* ...unless we have an Objective-C++ message or string literal,
3504 if (c_dialect_objc ()
3505 && (token->type == CPP_OPEN_SQUARE
3506 || token->type == CPP_OBJC_STRING))
3507 return cp_parser_objc_expression (parser);
3509 cp_parser_error (parser, "expected primary-expression");
3510 return error_mark_node;
3514 /* Parse an id-expression.
3521 :: [opt] nested-name-specifier template [opt] unqualified-id
3523 :: operator-function-id
3526 Return a representation of the unqualified portion of the
3527 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3528 a `::' or nested-name-specifier.
3530 Often, if the id-expression was a qualified-id, the caller will
3531 want to make a SCOPE_REF to represent the qualified-id. This
3532 function does not do this in order to avoid wastefully creating
3533 SCOPE_REFs when they are not required.
3535 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3539 uninstantiated templates.
3541 If *TEMPLATE_P is non-NULL, it is set to true iff the
3542 `template' keyword is used to explicitly indicate that the entity
3543 named is a template.
3545 If DECLARATOR_P is true, the id-expression is appearing as part of
3546 a declarator, rather than as part of an expression. */
3549 cp_parser_id_expression (cp_parser *parser,
3550 bool template_keyword_p,
3551 bool check_dependency_p,
3556 bool global_scope_p;
3557 bool nested_name_specifier_p;
3559 /* Assume the `template' keyword was not used. */
3561 *template_p = template_keyword_p;
3563 /* Look for the optional `::' operator. */
3565 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3567 /* Look for the optional nested-name-specifier. */
3568 nested_name_specifier_p
3569 = (cp_parser_nested_name_specifier_opt (parser,
3570 /*typename_keyword_p=*/false,
3575 /* If there is a nested-name-specifier, then we are looking at
3576 the first qualified-id production. */
3577 if (nested_name_specifier_p)
3580 tree saved_object_scope;
3581 tree saved_qualifying_scope;
3582 tree unqualified_id;
3585 /* See if the next token is the `template' keyword. */
3587 template_p = &is_template;
3588 *template_p = cp_parser_optional_template_keyword (parser);
3589 /* Name lookup we do during the processing of the
3590 unqualified-id might obliterate SCOPE. */
3591 saved_scope = parser->scope;
3592 saved_object_scope = parser->object_scope;
3593 saved_qualifying_scope = parser->qualifying_scope;
3594 /* Process the final unqualified-id. */
3595 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598 /*optional_p=*/false);
3599 /* Restore the SAVED_SCOPE for our caller. */
3600 parser->scope = saved_scope;
3601 parser->object_scope = saved_object_scope;
3602 parser->qualifying_scope = saved_qualifying_scope;
3604 return unqualified_id;
3606 /* Otherwise, if we are in global scope, then we are looking at one
3607 of the other qualified-id productions. */
3608 else if (global_scope_p)
3613 /* Peek at the next token. */
3614 token = cp_lexer_peek_token (parser->lexer);
3616 /* If it's an identifier, and the next token is not a "<", then
3617 we can avoid the template-id case. This is an optimization
3618 for this common case. */
3619 if (token->type == CPP_NAME
3620 && !cp_parser_nth_token_starts_template_argument_list_p
3622 return cp_parser_identifier (parser);
3624 cp_parser_parse_tentatively (parser);
3625 /* Try a template-id. */
3626 id = cp_parser_template_id (parser,
3627 /*template_keyword_p=*/false,
3628 /*check_dependency_p=*/true,
3630 /* If that worked, we're done. */
3631 if (cp_parser_parse_definitely (parser))
3634 /* Peek at the next token. (Changes in the token buffer may
3635 have invalidated the pointer obtained above.) */
3636 token = cp_lexer_peek_token (parser->lexer);
3638 switch (token->type)
3641 return cp_parser_identifier (parser);
3644 if (token->keyword == RID_OPERATOR)
3645 return cp_parser_operator_function_id (parser);
3649 cp_parser_error (parser, "expected id-expression");
3650 return error_mark_node;
3654 return cp_parser_unqualified_id (parser, template_keyword_p,
3655 /*check_dependency_p=*/true,
3660 /* Parse an unqualified-id.
3664 operator-function-id
3665 conversion-function-id
3669 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3670 keyword, in a construct like `A::template ...'.
3672 Returns a representation of unqualified-id. For the `identifier'
3673 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3674 production a BIT_NOT_EXPR is returned; the operand of the
3675 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3676 other productions, see the documentation accompanying the
3677 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3678 names are looked up in uninstantiated templates. If DECLARATOR_P
3679 is true, the unqualified-id is appearing as part of a declarator,
3680 rather than as part of an expression. */
3683 cp_parser_unqualified_id (cp_parser* parser,
3684 bool template_keyword_p,
3685 bool check_dependency_p,
3691 /* Peek at the next token. */
3692 token = cp_lexer_peek_token (parser->lexer);
3694 switch (token->type)
3700 /* We don't know yet whether or not this will be a
3702 cp_parser_parse_tentatively (parser);
3703 /* Try a template-id. */
3704 id = cp_parser_template_id (parser, template_keyword_p,
3707 /* If it worked, we're done. */
3708 if (cp_parser_parse_definitely (parser))
3710 /* Otherwise, it's an ordinary identifier. */
3711 return cp_parser_identifier (parser);
3714 case CPP_TEMPLATE_ID:
3715 return cp_parser_template_id (parser, template_keyword_p,
3722 tree qualifying_scope;
3727 /* Consume the `~' token. */
3728 cp_lexer_consume_token (parser->lexer);
3729 /* Parse the class-name. The standard, as written, seems to
3732 template <typename T> struct S { ~S (); };
3733 template <typename T> S<T>::~S() {}
3735 is invalid, since `~' must be followed by a class-name, but
3736 `S<T>' is dependent, and so not known to be a class.
3737 That's not right; we need to look in uninstantiated
3738 templates. A further complication arises from:
3740 template <typename T> void f(T t) {
3744 Here, it is not possible to look up `T' in the scope of `T'
3745 itself. We must look in both the current scope, and the
3746 scope of the containing complete expression.
3748 Yet another issue is:
3757 The standard does not seem to say that the `S' in `~S'
3758 should refer to the type `S' and not the data member
3761 /* DR 244 says that we look up the name after the "~" in the
3762 same scope as we looked up the qualifying name. That idea
3763 isn't fully worked out; it's more complicated than that. */
3764 scope = parser->scope;
3765 object_scope = parser->object_scope;
3766 qualifying_scope = parser->qualifying_scope;
3768 /* Check for invalid scopes. */
3769 if (scope == error_mark_node)
3771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3772 cp_lexer_consume_token (parser->lexer);
3773 return error_mark_node;
3775 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3777 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3778 error ("%Hscope %qT before %<~%> is not a class-name",
3779 &token->location, scope);
3780 cp_parser_simulate_error (parser);
3781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3782 cp_lexer_consume_token (parser->lexer);
3783 return error_mark_node;
3785 gcc_assert (!scope || TYPE_P (scope));
3787 /* If the name is of the form "X::~X" it's OK. */
3788 token = cp_lexer_peek_token (parser->lexer);
3790 && token->type == CPP_NAME
3791 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3793 && constructor_name_p (token->u.value, scope))
3795 cp_lexer_consume_token (parser->lexer);
3796 return build_nt (BIT_NOT_EXPR, scope);
3799 /* If there was an explicit qualification (S::~T), first look
3800 in the scope given by the qualification (i.e., S). */
3802 type_decl = NULL_TREE;
3805 cp_parser_parse_tentatively (parser);
3806 type_decl = cp_parser_class_name (parser,
3807 /*typename_keyword_p=*/false,
3808 /*template_keyword_p=*/false,
3810 /*check_dependency=*/false,
3811 /*class_head_p=*/false,
3813 if (cp_parser_parse_definitely (parser))
3816 /* In "N::S::~S", look in "N" as well. */
3817 if (!done && scope && qualifying_scope)
3819 cp_parser_parse_tentatively (parser);
3820 parser->scope = qualifying_scope;
3821 parser->object_scope = NULL_TREE;
3822 parser->qualifying_scope = NULL_TREE;
3824 = cp_parser_class_name (parser,
3825 /*typename_keyword_p=*/false,
3826 /*template_keyword_p=*/false,
3828 /*check_dependency=*/false,
3829 /*class_head_p=*/false,
3831 if (cp_parser_parse_definitely (parser))
3834 /* In "p->S::~T", look in the scope given by "*p" as well. */
3835 else if (!done && object_scope)
3837 cp_parser_parse_tentatively (parser);
3838 parser->scope = object_scope;
3839 parser->object_scope = NULL_TREE;
3840 parser->qualifying_scope = NULL_TREE;
3842 = cp_parser_class_name (parser,
3843 /*typename_keyword_p=*/false,
3844 /*template_keyword_p=*/false,
3846 /*check_dependency=*/false,
3847 /*class_head_p=*/false,
3849 if (cp_parser_parse_definitely (parser))
3852 /* Look in the surrounding context. */
3855 parser->scope = NULL_TREE;
3856 parser->object_scope = NULL_TREE;
3857 parser->qualifying_scope = NULL_TREE;
3859 = cp_parser_class_name (parser,
3860 /*typename_keyword_p=*/false,
3861 /*template_keyword_p=*/false,
3863 /*check_dependency=*/false,
3864 /*class_head_p=*/false,
3867 /* If an error occurred, assume that the name of the
3868 destructor is the same as the name of the qualifying
3869 class. That allows us to keep parsing after running
3870 into ill-formed destructor names. */
3871 if (type_decl == error_mark_node && scope)
3872 return build_nt (BIT_NOT_EXPR, scope);
3873 else if (type_decl == error_mark_node)
3874 return error_mark_node;
3876 /* Check that destructor name and scope match. */
3877 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3880 error ("%Hdeclaration of %<~%T%> as member of %qT",
3881 &token->location, type_decl, scope);
3882 cp_parser_simulate_error (parser);
3883 return error_mark_node;
3888 A typedef-name that names a class shall not be used as the
3889 identifier in the declarator for a destructor declaration. */
3891 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3892 && !DECL_SELF_REFERENCE_P (type_decl)
3893 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3894 error ("%Htypedef-name %qD used as destructor declarator",
3895 &token->location, type_decl);
3897 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3901 if (token->keyword == RID_OPERATOR)
3905 /* This could be a template-id, so we try that first. */
3906 cp_parser_parse_tentatively (parser);
3907 /* Try a template-id. */
3908 id = cp_parser_template_id (parser, template_keyword_p,
3909 /*check_dependency_p=*/true,
3911 /* If that worked, we're done. */
3912 if (cp_parser_parse_definitely (parser))
3914 /* We still don't know whether we're looking at an
3915 operator-function-id or a conversion-function-id. */
3916 cp_parser_parse_tentatively (parser);
3917 /* Try an operator-function-id. */
3918 id = cp_parser_operator_function_id (parser);
3919 /* If that didn't work, try a conversion-function-id. */
3920 if (!cp_parser_parse_definitely (parser))
3921 id = cp_parser_conversion_function_id (parser);
3930 cp_parser_error (parser, "expected unqualified-id");
3931 return error_mark_node;
3935 /* Parse an (optional) nested-name-specifier.
3937 nested-name-specifier:
3938 class-or-namespace-name :: nested-name-specifier [opt]
3939 class-or-namespace-name :: template nested-name-specifier [opt]
3941 PARSER->SCOPE should be set appropriately before this function is
3942 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3943 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3946 Sets PARSER->SCOPE to the class (TYPE) or namespace
3947 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3948 it unchanged if there is no nested-name-specifier. Returns the new
3949 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3951 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3952 part of a declaration and/or decl-specifier. */
3955 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3956 bool typename_keyword_p,
3957 bool check_dependency_p,
3959 bool is_declaration)
3961 bool success = false;
3962 cp_token_position start = 0;
3965 /* Remember where the nested-name-specifier starts. */
3966 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3968 start = cp_lexer_token_position (parser->lexer, false);
3969 push_deferring_access_checks (dk_deferred);
3976 tree saved_qualifying_scope;
3977 bool template_keyword_p;
3979 /* Spot cases that cannot be the beginning of a
3980 nested-name-specifier. */
3981 token = cp_lexer_peek_token (parser->lexer);
3983 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3984 the already parsed nested-name-specifier. */
3985 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3987 /* Grab the nested-name-specifier and continue the loop. */
3988 cp_parser_pre_parsed_nested_name_specifier (parser);
3989 /* If we originally encountered this nested-name-specifier
3990 with IS_DECLARATION set to false, we will not have
3991 resolved TYPENAME_TYPEs, so we must do so here. */
3993 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3995 new_scope = resolve_typename_type (parser->scope,
3996 /*only_current_p=*/false);
3997 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3998 parser->scope = new_scope;
4004 /* Spot cases that cannot be the beginning of a
4005 nested-name-specifier. On the second and subsequent times
4006 through the loop, we look for the `template' keyword. */
4007 if (success && token->keyword == RID_TEMPLATE)
4009 /* A template-id can start a nested-name-specifier. */
4010 else if (token->type == CPP_TEMPLATE_ID)
4014 /* If the next token is not an identifier, then it is
4015 definitely not a class-or-namespace-name. */
4016 if (token->type != CPP_NAME)
4018 /* If the following token is neither a `<' (to begin a
4019 template-id), nor a `::', then we are not looking at a
4020 nested-name-specifier. */
4021 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4022 if (token->type != CPP_SCOPE
4023 && !cp_parser_nth_token_starts_template_argument_list_p
4028 /* The nested-name-specifier is optional, so we parse
4030 cp_parser_parse_tentatively (parser);
4032 /* Look for the optional `template' keyword, if this isn't the
4033 first time through the loop. */
4035 template_keyword_p = cp_parser_optional_template_keyword (parser);
4037 template_keyword_p = false;
4039 /* Save the old scope since the name lookup we are about to do
4040 might destroy it. */
4041 old_scope = parser->scope;
4042 saved_qualifying_scope = parser->qualifying_scope;
4043 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4044 look up names in "X<T>::I" in order to determine that "Y" is
4045 a template. So, if we have a typename at this point, we make
4046 an effort to look through it. */
4048 && !typename_keyword_p
4050 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4051 parser->scope = resolve_typename_type (parser->scope,
4052 /*only_current_p=*/false);
4053 /* Parse the qualifying entity. */
4055 = cp_parser_class_or_namespace_name (parser,
4061 /* Look for the `::' token. */
4062 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4064 /* If we found what we wanted, we keep going; otherwise, we're
4066 if (!cp_parser_parse_definitely (parser))
4068 bool error_p = false;
4070 /* Restore the OLD_SCOPE since it was valid before the
4071 failed attempt at finding the last
4072 class-or-namespace-name. */
4073 parser->scope = old_scope;
4074 parser->qualifying_scope = saved_qualifying_scope;
4075 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4077 /* If the next token is an identifier, and the one after
4078 that is a `::', then any valid interpretation would have
4079 found a class-or-namespace-name. */
4080 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4081 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4083 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4086 token = cp_lexer_consume_token (parser->lexer);
4089 if (!token->ambiguous_p)
4092 tree ambiguous_decls;
4094 decl = cp_parser_lookup_name (parser, token->u.value,
4096 /*is_template=*/false,
4097 /*is_namespace=*/false,
4098 /*check_dependency=*/true,
4101 if (TREE_CODE (decl) == TEMPLATE_DECL)
4102 error ("%H%qD used without template parameters",
4103 &token->location, decl);
4104 else if (ambiguous_decls)
4106 error ("%Hreference to %qD is ambiguous",
4107 &token->location, token->u.value);
4108 print_candidates (ambiguous_decls);
4109 decl = error_mark_node;
4112 cp_parser_name_lookup_error
4113 (parser, token->u.value, decl,
4114 "is not a class or namespace",
4117 parser->scope = error_mark_node;
4119 /* Treat this as a successful nested-name-specifier
4124 If the name found is not a class-name (clause
4125 _class_) or namespace-name (_namespace.def_), the
4126 program is ill-formed. */
4129 cp_lexer_consume_token (parser->lexer);
4133 /* We've found one valid nested-name-specifier. */
4135 /* Name lookup always gives us a DECL. */
4136 if (TREE_CODE (new_scope) == TYPE_DECL)
4137 new_scope = TREE_TYPE (new_scope);
4138 /* Uses of "template" must be followed by actual templates. */
4139 if (template_keyword_p
4140 && !(CLASS_TYPE_P (new_scope)
4141 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4142 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4143 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4144 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4145 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4146 == TEMPLATE_ID_EXPR)))
4147 permerror (input_location, TYPE_P (new_scope)
4148 ? "%qT is not a template"
4149 : "%qD is not a template",
4151 /* If it is a class scope, try to complete it; we are about to
4152 be looking up names inside the class. */
4153 if (TYPE_P (new_scope)
4154 /* Since checking types for dependency can be expensive,
4155 avoid doing it if the type is already complete. */
4156 && !COMPLETE_TYPE_P (new_scope)
4157 /* Do not try to complete dependent types. */
4158 && !dependent_type_p (new_scope))
4160 new_scope = complete_type (new_scope);
4161 /* If it is a typedef to current class, use the current
4162 class instead, as the typedef won't have any names inside
4164 if (!COMPLETE_TYPE_P (new_scope)
4165 && currently_open_class (new_scope))
4166 new_scope = TYPE_MAIN_VARIANT (new_scope);
4168 /* Make sure we look in the right scope the next time through
4170 parser->scope = new_scope;
4173 /* If parsing tentatively, replace the sequence of tokens that makes
4174 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4175 token. That way, should we re-parse the token stream, we will
4176 not have to repeat the effort required to do the parse, nor will
4177 we issue duplicate error messages. */
4178 if (success && start)
4182 token = cp_lexer_token_at (parser->lexer, start);
4183 /* Reset the contents of the START token. */
4184 token->type = CPP_NESTED_NAME_SPECIFIER;
4185 /* Retrieve any deferred checks. Do not pop this access checks yet
4186 so the memory will not be reclaimed during token replacing below. */
4187 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4188 token->u.tree_check_value->value = parser->scope;
4189 token->u.tree_check_value->checks = get_deferred_access_checks ();
4190 token->u.tree_check_value->qualifying_scope =
4191 parser->qualifying_scope;
4192 token->keyword = RID_MAX;
4194 /* Purge all subsequent tokens. */
4195 cp_lexer_purge_tokens_after (parser->lexer, start);
4199 pop_to_parent_deferring_access_checks ();
4201 return success ? parser->scope : NULL_TREE;
4204 /* Parse a nested-name-specifier. See
4205 cp_parser_nested_name_specifier_opt for details. This function
4206 behaves identically, except that it will an issue an error if no
4207 nested-name-specifier is present. */
4210 cp_parser_nested_name_specifier (cp_parser *parser,
4211 bool typename_keyword_p,
4212 bool check_dependency_p,
4214 bool is_declaration)
4218 /* Look for the nested-name-specifier. */
4219 scope = cp_parser_nested_name_specifier_opt (parser,
4224 /* If it was not present, issue an error message. */
4227 cp_parser_error (parser, "expected nested-name-specifier");
4228 parser->scope = NULL_TREE;
4234 /* Parse a class-or-namespace-name.
4236 class-or-namespace-name:
4240 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4241 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4242 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4243 TYPE_P is TRUE iff the next name should be taken as a class-name,
4244 even the same name is declared to be another entity in the same
4247 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4248 specified by the class-or-namespace-name. If neither is found the
4249 ERROR_MARK_NODE is returned. */
4252 cp_parser_class_or_namespace_name (cp_parser *parser,
4253 bool typename_keyword_p,
4254 bool template_keyword_p,
4255 bool check_dependency_p,
4257 bool is_declaration)
4260 tree saved_qualifying_scope;
4261 tree saved_object_scope;
4265 /* Before we try to parse the class-name, we must save away the
4266 current PARSER->SCOPE since cp_parser_class_name will destroy
4268 saved_scope = parser->scope;
4269 saved_qualifying_scope = parser->qualifying_scope;
4270 saved_object_scope = parser->object_scope;
4271 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4272 there is no need to look for a namespace-name. */
4273 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4275 cp_parser_parse_tentatively (parser);
4276 scope = cp_parser_class_name (parser,
4279 type_p ? class_type : none_type,
4281 /*class_head_p=*/false,
4283 /* If that didn't work, try for a namespace-name. */
4284 if (!only_class_p && !cp_parser_parse_definitely (parser))
4286 /* Restore the saved scope. */
4287 parser->scope = saved_scope;
4288 parser->qualifying_scope = saved_qualifying_scope;
4289 parser->object_scope = saved_object_scope;
4290 /* If we are not looking at an identifier followed by the scope
4291 resolution operator, then this is not part of a
4292 nested-name-specifier. (Note that this function is only used
4293 to parse the components of a nested-name-specifier.) */
4294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4295 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4296 return error_mark_node;
4297 scope = cp_parser_namespace_name (parser);
4303 /* Parse a postfix-expression.
4307 postfix-expression [ expression ]
4308 postfix-expression ( expression-list [opt] )
4309 simple-type-specifier ( expression-list [opt] )
4310 typename :: [opt] nested-name-specifier identifier
4311 ( expression-list [opt] )
4312 typename :: [opt] nested-name-specifier template [opt] template-id
4313 ( expression-list [opt] )
4314 postfix-expression . template [opt] id-expression
4315 postfix-expression -> template [opt] id-expression
4316 postfix-expression . pseudo-destructor-name
4317 postfix-expression -> pseudo-destructor-name
4318 postfix-expression ++
4319 postfix-expression --
4320 dynamic_cast < type-id > ( expression )
4321 static_cast < type-id > ( expression )
4322 reinterpret_cast < type-id > ( expression )
4323 const_cast < type-id > ( expression )
4324 typeid ( expression )
4330 ( type-id ) { initializer-list , [opt] }
4332 This extension is a GNU version of the C99 compound-literal
4333 construct. (The C99 grammar uses `type-name' instead of `type-id',
4334 but they are essentially the same concept.)
4336 If ADDRESS_P is true, the postfix expression is the operand of the
4337 `&' operator. CAST_P is true if this expression is the target of a
4340 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4341 class member access expressions [expr.ref].
4343 Returns a representation of the expression. */
4346 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4347 bool member_access_only_p)
4351 cp_id_kind idk = CP_ID_KIND_NONE;
4352 tree postfix_expression = NULL_TREE;
4353 bool is_member_access = false;
4355 /* Peek at the next token. */
4356 token = cp_lexer_peek_token (parser->lexer);
4357 /* Some of the productions are determined by keywords. */
4358 keyword = token->keyword;
4368 const char *saved_message;
4370 /* All of these can be handled in the same way from the point
4371 of view of parsing. Begin by consuming the token
4372 identifying the cast. */
4373 cp_lexer_consume_token (parser->lexer);
4375 /* New types cannot be defined in the cast. */
4376 saved_message = parser->type_definition_forbidden_message;
4377 parser->type_definition_forbidden_message
4378 = "types may not be defined in casts";
4380 /* Look for the opening `<'. */
4381 cp_parser_require (parser, CPP_LESS, "%<<%>");
4382 /* Parse the type to which we are casting. */
4383 type = cp_parser_type_id (parser);
4384 /* Look for the closing `>'. */
4385 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4386 /* Restore the old message. */
4387 parser->type_definition_forbidden_message = saved_message;
4389 /* And the expression which is being cast. */
4390 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4391 expression = cp_parser_expression (parser, /*cast_p=*/true);
4392 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4394 /* Only type conversions to integral or enumeration types
4395 can be used in constant-expressions. */
4396 if (!cast_valid_in_integral_constant_expression_p (type)
4397 && (cp_parser_non_integral_constant_expression
4399 "a cast to a type other than an integral or "
4400 "enumeration type")))
4401 return error_mark_node;
4407 = build_dynamic_cast (type, expression, tf_warning_or_error);
4411 = build_static_cast (type, expression, tf_warning_or_error);
4415 = build_reinterpret_cast (type, expression,
4416 tf_warning_or_error);
4420 = build_const_cast (type, expression, tf_warning_or_error);
4431 const char *saved_message;
4432 bool saved_in_type_id_in_expr_p;
4434 /* Consume the `typeid' token. */
4435 cp_lexer_consume_token (parser->lexer);
4436 /* Look for the `(' token. */
4437 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4438 /* Types cannot be defined in a `typeid' expression. */
4439 saved_message = parser->type_definition_forbidden_message;
4440 parser->type_definition_forbidden_message
4441 = "types may not be defined in a %<typeid%> expression";
4442 /* We can't be sure yet whether we're looking at a type-id or an
4444 cp_parser_parse_tentatively (parser);
4445 /* Try a type-id first. */
4446 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4447 parser->in_type_id_in_expr_p = true;
4448 type = cp_parser_type_id (parser);
4449 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4450 /* Look for the `)' token. Otherwise, we can't be sure that
4451 we're not looking at an expression: consider `typeid (int
4452 (3))', for example. */
4453 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4454 /* If all went well, simply lookup the type-id. */
4455 if (cp_parser_parse_definitely (parser))
4456 postfix_expression = get_typeid (type);
4457 /* Otherwise, fall back to the expression variant. */
4462 /* Look for an expression. */
4463 expression = cp_parser_expression (parser, /*cast_p=*/false);
4464 /* Compute its typeid. */
4465 postfix_expression = build_typeid (expression);
4466 /* Look for the `)' token. */
4467 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4469 /* Restore the saved message. */
4470 parser->type_definition_forbidden_message = saved_message;
4471 /* `typeid' may not appear in an integral constant expression. */
4472 if (cp_parser_non_integral_constant_expression(parser,
4473 "%<typeid%> operator"))
4474 return error_mark_node;
4481 /* The syntax permitted here is the same permitted for an
4482 elaborated-type-specifier. */
4483 type = cp_parser_elaborated_type_specifier (parser,
4484 /*is_friend=*/false,
4485 /*is_declaration=*/false);
4486 postfix_expression = cp_parser_functional_cast (parser, type);
4494 /* If the next thing is a simple-type-specifier, we may be
4495 looking at a functional cast. We could also be looking at
4496 an id-expression. So, we try the functional cast, and if
4497 that doesn't work we fall back to the primary-expression. */
4498 cp_parser_parse_tentatively (parser);
4499 /* Look for the simple-type-specifier. */
4500 type = cp_parser_simple_type_specifier (parser,
4501 /*decl_specs=*/NULL,
4502 CP_PARSER_FLAGS_NONE);
4503 /* Parse the cast itself. */
4504 if (!cp_parser_error_occurred (parser))
4506 = cp_parser_functional_cast (parser, type);
4507 /* If that worked, we're done. */
4508 if (cp_parser_parse_definitely (parser))
4511 /* If the functional-cast didn't work out, try a
4512 compound-literal. */
4513 if (cp_parser_allow_gnu_extensions_p (parser)
4514 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4516 VEC(constructor_elt,gc) *initializer_list = NULL;
4517 bool saved_in_type_id_in_expr_p;
4519 cp_parser_parse_tentatively (parser);
4520 /* Consume the `('. */
4521 cp_lexer_consume_token (parser->lexer);
4522 /* Parse the type. */
4523 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4524 parser->in_type_id_in_expr_p = true;
4525 type = cp_parser_type_id (parser);
4526 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4527 /* Look for the `)'. */
4528 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4529 /* Look for the `{'. */
4530 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4531 /* If things aren't going well, there's no need to
4533 if (!cp_parser_error_occurred (parser))
4535 bool non_constant_p;
4536 /* Parse the initializer-list. */
4538 = cp_parser_initializer_list (parser, &non_constant_p);
4539 /* Allow a trailing `,'. */
4540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4541 cp_lexer_consume_token (parser->lexer);
4542 /* Look for the final `}'. */
4543 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4545 /* If that worked, we're definitely looking at a
4546 compound-literal expression. */
4547 if (cp_parser_parse_definitely (parser))
4549 /* Warn the user that a compound literal is not
4550 allowed in standard C++. */
4551 pedwarn (OPT_pedantic, "ISO C++ forbids compound-literals");
4552 /* For simplicity, we disallow compound literals in
4553 constant-expressions. We could
4554 allow compound literals of integer type, whose
4555 initializer was a constant, in constant
4556 expressions. Permitting that usage, as a further
4557 extension, would not change the meaning of any
4558 currently accepted programs. (Of course, as
4559 compound literals are not part of ISO C++, the
4560 standard has nothing to say.) */
4561 if (cp_parser_non_integral_constant_expression
4562 (parser, "non-constant compound literals"))
4564 postfix_expression = error_mark_node;
4567 /* Form the representation of the compound-literal. */
4569 = (finish_compound_literal
4570 (type, build_constructor (init_list_type_node,
4571 initializer_list)));
4576 /* It must be a primary-expression. */
4578 = cp_parser_primary_expression (parser, address_p, cast_p,
4579 /*template_arg_p=*/false,
4585 /* Keep looping until the postfix-expression is complete. */
4588 if (idk == CP_ID_KIND_UNQUALIFIED
4589 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4590 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4591 /* It is not a Koenig lookup function call. */
4593 = unqualified_name_lookup_error (postfix_expression);
4595 /* Peek at the next token. */
4596 token = cp_lexer_peek_token (parser->lexer);
4598 switch (token->type)
4600 case CPP_OPEN_SQUARE:
4602 = cp_parser_postfix_open_square_expression (parser,
4605 idk = CP_ID_KIND_NONE;
4606 is_member_access = false;
4609 case CPP_OPEN_PAREN:
4610 /* postfix-expression ( expression-list [opt] ) */
4613 bool is_builtin_constant_p;
4614 bool saved_integral_constant_expression_p = false;
4615 bool saved_non_integral_constant_expression_p = false;
4618 is_member_access = false;
4620 is_builtin_constant_p
4621 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4622 if (is_builtin_constant_p)
4624 /* The whole point of __builtin_constant_p is to allow
4625 non-constant expressions to appear as arguments. */
4626 saved_integral_constant_expression_p
4627 = parser->integral_constant_expression_p;
4628 saved_non_integral_constant_expression_p
4629 = parser->non_integral_constant_expression_p;
4630 parser->integral_constant_expression_p = false;
4632 args = (cp_parser_parenthesized_expression_list
4633 (parser, /*is_attribute_list=*/false,
4634 /*cast_p=*/false, /*allow_expansion_p=*/true,
4635 /*non_constant_p=*/NULL));
4636 if (is_builtin_constant_p)
4638 parser->integral_constant_expression_p
4639 = saved_integral_constant_expression_p;
4640 parser->non_integral_constant_expression_p
4641 = saved_non_integral_constant_expression_p;
4644 if (args == error_mark_node)
4646 postfix_expression = error_mark_node;
4650 /* Function calls are not permitted in
4651 constant-expressions. */
4652 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4653 && cp_parser_non_integral_constant_expression (parser,
4656 postfix_expression = error_mark_node;
4661 if (idk == CP_ID_KIND_UNQUALIFIED)
4663 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4669 = perform_koenig_lookup (postfix_expression, args);
4673 = unqualified_fn_lookup_error (postfix_expression);
4675 /* We do not perform argument-dependent lookup if
4676 normal lookup finds a non-function, in accordance
4677 with the expected resolution of DR 218. */
4678 else if (args && is_overloaded_fn (postfix_expression))
4680 tree fn = get_first_fn (postfix_expression);
4682 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4683 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4685 /* Only do argument dependent lookup if regular
4686 lookup does not find a set of member functions.
4687 [basic.lookup.koenig]/2a */
4688 if (!DECL_FUNCTION_MEMBER_P (fn))
4692 = perform_koenig_lookup (postfix_expression, args);
4697 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4699 tree instance = TREE_OPERAND (postfix_expression, 0);
4700 tree fn = TREE_OPERAND (postfix_expression, 1);
4702 if (processing_template_decl
4703 && (type_dependent_expression_p (instance)
4704 || (!BASELINK_P (fn)
4705 && TREE_CODE (fn) != FIELD_DECL)
4706 || type_dependent_expression_p (fn)
4707 || any_type_dependent_arguments_p (args)))
4710 = build_nt_call_list (postfix_expression, args);
4714 if (BASELINK_P (fn))
4716 = (build_new_method_call
4717 (instance, fn, args, NULL_TREE,
4718 (idk == CP_ID_KIND_QUALIFIED
4719 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4721 tf_warning_or_error));
4724 = finish_call_expr (postfix_expression, args,
4725 /*disallow_virtual=*/false,
4727 tf_warning_or_error);
4729 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4730 || TREE_CODE (postfix_expression) == MEMBER_REF
4731 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4732 postfix_expression = (build_offset_ref_call_from_tree
4733 (postfix_expression, args));
4734 else if (idk == CP_ID_KIND_QUALIFIED)
4735 /* A call to a static class member, or a namespace-scope
4738 = finish_call_expr (postfix_expression, args,
4739 /*disallow_virtual=*/true,
4741 tf_warning_or_error);
4743 /* All other function calls. */
4745 = finish_call_expr (postfix_expression, args,
4746 /*disallow_virtual=*/false,
4748 tf_warning_or_error);
4750 if (warn_disallowed_functions)
4751 warn_if_disallowed_function_p (postfix_expression);
4753 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4754 idk = CP_ID_KIND_NONE;
4760 /* postfix-expression . template [opt] id-expression
4761 postfix-expression . pseudo-destructor-name
4762 postfix-expression -> template [opt] id-expression
4763 postfix-expression -> pseudo-destructor-name */
4765 /* Consume the `.' or `->' operator. */
4766 cp_lexer_consume_token (parser->lexer);
4769 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4774 is_member_access = true;
4778 /* postfix-expression ++ */
4779 /* Consume the `++' token. */
4780 cp_lexer_consume_token (parser->lexer);
4781 /* Generate a representation for the complete expression. */
4783 = finish_increment_expr (postfix_expression,
4784 POSTINCREMENT_EXPR);
4785 /* Increments may not appear in constant-expressions. */
4786 if (cp_parser_non_integral_constant_expression (parser,
4788 postfix_expression = error_mark_node;
4789 idk = CP_ID_KIND_NONE;
4790 is_member_access = false;
4793 case CPP_MINUS_MINUS:
4794 /* postfix-expression -- */
4795 /* Consume the `--' token. */
4796 cp_lexer_consume_token (parser->lexer);
4797 /* Generate a representation for the complete expression. */
4799 = finish_increment_expr (postfix_expression,
4800 POSTDECREMENT_EXPR);
4801 /* Decrements may not appear in constant-expressions. */
4802 if (cp_parser_non_integral_constant_expression (parser,
4804 postfix_expression = error_mark_node;
4805 idk = CP_ID_KIND_NONE;
4806 is_member_access = false;
4810 if (member_access_only_p)
4811 return is_member_access? postfix_expression : error_mark_node;
4813 return postfix_expression;
4817 /* We should never get here. */
4819 return error_mark_node;
4822 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4823 by cp_parser_builtin_offsetof. We're looking for
4825 postfix-expression [ expression ]
4827 FOR_OFFSETOF is set if we're being called in that context, which
4828 changes how we deal with integer constant expressions. */
4831 cp_parser_postfix_open_square_expression (cp_parser *parser,
4832 tree postfix_expression,
4837 /* Consume the `[' token. */
4838 cp_lexer_consume_token (parser->lexer);
4840 /* Parse the index expression. */
4841 /* ??? For offsetof, there is a question of what to allow here. If
4842 offsetof is not being used in an integral constant expression context,
4843 then we *could* get the right answer by computing the value at runtime.
4844 If we are in an integral constant expression context, then we might
4845 could accept any constant expression; hard to say without analysis.
4846 Rather than open the barn door too wide right away, allow only integer
4847 constant expressions here. */
4849 index = cp_parser_constant_expression (parser, false, NULL);
4851 index = cp_parser_expression (parser, /*cast_p=*/false);
4853 /* Look for the closing `]'. */
4854 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4856 /* Build the ARRAY_REF. */
4857 postfix_expression = grok_array_decl (postfix_expression, index);
4859 /* When not doing offsetof, array references are not permitted in
4860 constant-expressions. */
4862 && (cp_parser_non_integral_constant_expression
4863 (parser, "an array reference")))
4864 postfix_expression = error_mark_node;
4866 return postfix_expression;
4869 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4870 by cp_parser_builtin_offsetof. We're looking for
4872 postfix-expression . template [opt] id-expression
4873 postfix-expression . pseudo-destructor-name
4874 postfix-expression -> template [opt] id-expression
4875 postfix-expression -> pseudo-destructor-name
4877 FOR_OFFSETOF is set if we're being called in that context. That sorta
4878 limits what of the above we'll actually accept, but nevermind.
4879 TOKEN_TYPE is the "." or "->" token, which will already have been
4880 removed from the stream. */
4883 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4884 enum cpp_ttype token_type,
4885 tree postfix_expression,
4886 bool for_offsetof, cp_id_kind *idk,
4887 location_t location)
4891 bool pseudo_destructor_p;
4892 tree scope = NULL_TREE;
4894 /* If this is a `->' operator, dereference the pointer. */
4895 if (token_type == CPP_DEREF)
4896 postfix_expression = build_x_arrow (postfix_expression);
4897 /* Check to see whether or not the expression is type-dependent. */
4898 dependent_p = type_dependent_expression_p (postfix_expression);
4899 /* The identifier following the `->' or `.' is not qualified. */
4900 parser->scope = NULL_TREE;
4901 parser->qualifying_scope = NULL_TREE;
4902 parser->object_scope = NULL_TREE;
4903 *idk = CP_ID_KIND_NONE;
4904 /* Enter the scope corresponding to the type of the object
4905 given by the POSTFIX_EXPRESSION. */
4906 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4908 scope = TREE_TYPE (postfix_expression);
4909 /* According to the standard, no expression should ever have
4910 reference type. Unfortunately, we do not currently match
4911 the standard in this respect in that our internal representation
4912 of an expression may have reference type even when the standard
4913 says it does not. Therefore, we have to manually obtain the
4914 underlying type here. */
4915 scope = non_reference (scope);
4916 /* The type of the POSTFIX_EXPRESSION must be complete. */
4917 if (scope == unknown_type_node)
4919 error ("%H%qE does not have class type", &location, postfix_expression);
4923 scope = complete_type_or_else (scope, NULL_TREE);
4924 /* Let the name lookup machinery know that we are processing a
4925 class member access expression. */
4926 parser->context->object_type = scope;
4927 /* If something went wrong, we want to be able to discern that case,
4928 as opposed to the case where there was no SCOPE due to the type
4929 of expression being dependent. */
4931 scope = error_mark_node;
4932 /* If the SCOPE was erroneous, make the various semantic analysis
4933 functions exit quickly -- and without issuing additional error
4935 if (scope == error_mark_node)
4936 postfix_expression = error_mark_node;
4939 /* Assume this expression is not a pseudo-destructor access. */
4940 pseudo_destructor_p = false;
4942 /* If the SCOPE is a scalar type, then, if this is a valid program,
4943 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4944 is type dependent, it can be pseudo-destructor-name or something else.
4945 Try to parse it as pseudo-destructor-name first. */
4946 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4951 cp_parser_parse_tentatively (parser);
4952 /* Parse the pseudo-destructor-name. */
4954 cp_parser_pseudo_destructor_name (parser, &s, &type);
4956 && (cp_parser_error_occurred (parser)
4957 || TREE_CODE (type) != TYPE_DECL
4958 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4959 cp_parser_abort_tentative_parse (parser);
4960 else if (cp_parser_parse_definitely (parser))
4962 pseudo_destructor_p = true;
4964 = finish_pseudo_destructor_expr (postfix_expression,
4965 s, TREE_TYPE (type));
4969 if (!pseudo_destructor_p)
4971 /* If the SCOPE is not a scalar type, we are looking at an
4972 ordinary class member access expression, rather than a
4973 pseudo-destructor-name. */
4975 cp_token *token = cp_lexer_peek_token (parser->lexer);
4976 /* Parse the id-expression. */
4977 name = (cp_parser_id_expression
4979 cp_parser_optional_template_keyword (parser),
4980 /*check_dependency_p=*/true,
4982 /*declarator_p=*/false,
4983 /*optional_p=*/false));
4984 /* In general, build a SCOPE_REF if the member name is qualified.
4985 However, if the name was not dependent and has already been
4986 resolved; there is no need to build the SCOPE_REF. For example;
4988 struct X { void f(); };
4989 template <typename T> void f(T* t) { t->X::f(); }
4991 Even though "t" is dependent, "X::f" is not and has been resolved
4992 to a BASELINK; there is no need to include scope information. */
4994 /* But we do need to remember that there was an explicit scope for
4995 virtual function calls. */
4997 *idk = CP_ID_KIND_QUALIFIED;
4999 /* If the name is a template-id that names a type, we will get a
5000 TYPE_DECL here. That is invalid code. */
5001 if (TREE_CODE (name) == TYPE_DECL)
5003 error ("%Hinvalid use of %qD", &token->location, name);
5004 postfix_expression = error_mark_node;
5008 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5010 name = build_qualified_name (/*type=*/NULL_TREE,
5014 parser->scope = NULL_TREE;
5015 parser->qualifying_scope = NULL_TREE;
5016 parser->object_scope = NULL_TREE;
5018 if (scope && name && BASELINK_P (name))
5019 adjust_result_of_qualified_name_lookup
5020 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5022 = finish_class_member_access_expr (postfix_expression, name,
5024 tf_warning_or_error);
5028 /* We no longer need to look up names in the scope of the object on
5029 the left-hand side of the `.' or `->' operator. */
5030 parser->context->object_type = NULL_TREE;
5032 /* Outside of offsetof, these operators may not appear in
5033 constant-expressions. */
5035 && (cp_parser_non_integral_constant_expression
5036 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5037 postfix_expression = error_mark_node;
5039 return postfix_expression;
5042 /* Parse a parenthesized expression-list.
5045 assignment-expression
5046 expression-list, assignment-expression
5051 identifier, expression-list
5053 CAST_P is true if this expression is the target of a cast.
5055 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5058 Returns a TREE_LIST. The TREE_VALUE of each node is a
5059 representation of an assignment-expression. Note that a TREE_LIST
5060 is returned even if there is only a single expression in the list.
5061 error_mark_node is returned if the ( and or ) are
5062 missing. NULL_TREE is returned on no expressions. The parentheses
5063 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5064 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5065 indicates whether or not all of the expressions in the list were
5069 cp_parser_parenthesized_expression_list (cp_parser* parser,
5070 bool is_attribute_list,
5072 bool allow_expansion_p,
5073 bool *non_constant_p)
5075 tree expression_list = NULL_TREE;
5076 bool fold_expr_p = is_attribute_list;
5077 tree identifier = NULL_TREE;
5078 bool saved_greater_than_is_operator_p;
5080 /* Assume all the expressions will be constant. */
5082 *non_constant_p = false;
5084 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5085 return error_mark_node;
5087 /* Within a parenthesized expression, a `>' token is always
5088 the greater-than operator. */
5089 saved_greater_than_is_operator_p
5090 = parser->greater_than_is_operator_p;
5091 parser->greater_than_is_operator_p = true;
5093 /* Consume expressions until there are no more. */
5094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5099 /* At the beginning of attribute lists, check to see if the
5100 next token is an identifier. */
5101 if (is_attribute_list
5102 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5106 /* Consume the identifier. */
5107 token = cp_lexer_consume_token (parser->lexer);
5108 /* Save the identifier. */
5109 identifier = token->u.value;
5113 bool expr_non_constant_p;
5115 /* Parse the next assignment-expression. */
5116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5118 /* A braced-init-list. */
5119 maybe_warn_cpp0x ("extended initializer lists");
5120 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5121 if (non_constant_p && expr_non_constant_p)
5122 *non_constant_p = true;
5124 else if (non_constant_p)
5126 expr = (cp_parser_constant_expression
5127 (parser, /*allow_non_constant_p=*/true,
5128 &expr_non_constant_p));
5129 if (expr_non_constant_p)
5130 *non_constant_p = true;
5133 expr = cp_parser_assignment_expression (parser, cast_p);
5136 expr = fold_non_dependent_expr (expr);
5138 /* If we have an ellipsis, then this is an expression
5140 if (allow_expansion_p
5141 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5143 /* Consume the `...'. */
5144 cp_lexer_consume_token (parser->lexer);
5146 /* Build the argument pack. */
5147 expr = make_pack_expansion (expr);
5150 /* Add it to the list. We add error_mark_node
5151 expressions to the list, so that we can still tell if
5152 the correct form for a parenthesized expression-list
5153 is found. That gives better errors. */
5154 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5156 if (expr == error_mark_node)
5160 /* After the first item, attribute lists look the same as
5161 expression lists. */
5162 is_attribute_list = false;
5165 /* If the next token isn't a `,', then we are done. */
5166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5169 /* Otherwise, consume the `,' and keep going. */
5170 cp_lexer_consume_token (parser->lexer);
5173 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5178 /* We try and resync to an unnested comma, as that will give the
5179 user better diagnostics. */
5180 ending = cp_parser_skip_to_closing_parenthesis (parser,
5181 /*recovering=*/true,
5183 /*consume_paren=*/true);
5188 parser->greater_than_is_operator_p
5189 = saved_greater_than_is_operator_p;
5190 return error_mark_node;
5194 parser->greater_than_is_operator_p
5195 = saved_greater_than_is_operator_p;
5197 /* We built up the list in reverse order so we must reverse it now. */
5198 expression_list = nreverse (expression_list);
5200 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5202 return expression_list;
5205 /* Parse a pseudo-destructor-name.
5207 pseudo-destructor-name:
5208 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5209 :: [opt] nested-name-specifier template template-id :: ~ type-name
5210 :: [opt] nested-name-specifier [opt] ~ type-name
5212 If either of the first two productions is used, sets *SCOPE to the
5213 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5214 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5215 or ERROR_MARK_NODE if the parse fails. */
5218 cp_parser_pseudo_destructor_name (cp_parser* parser,
5222 bool nested_name_specifier_p;
5224 /* Assume that things will not work out. */
5225 *type = error_mark_node;
5227 /* Look for the optional `::' operator. */
5228 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5229 /* Look for the optional nested-name-specifier. */
5230 nested_name_specifier_p
5231 = (cp_parser_nested_name_specifier_opt (parser,
5232 /*typename_keyword_p=*/false,
5233 /*check_dependency_p=*/true,
5235 /*is_declaration=*/true)
5237 /* Now, if we saw a nested-name-specifier, we might be doing the
5238 second production. */
5239 if (nested_name_specifier_p
5240 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5242 /* Consume the `template' keyword. */
5243 cp_lexer_consume_token (parser->lexer);
5244 /* Parse the template-id. */
5245 cp_parser_template_id (parser,
5246 /*template_keyword_p=*/true,
5247 /*check_dependency_p=*/false,
5248 /*is_declaration=*/true);
5249 /* Look for the `::' token. */
5250 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5252 /* If the next token is not a `~', then there might be some
5253 additional qualification. */
5254 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5256 /* At this point, we're looking for "type-name :: ~". The type-name
5257 must not be a class-name, since this is a pseudo-destructor. So,
5258 it must be either an enum-name, or a typedef-name -- both of which
5259 are just identifiers. So, we peek ahead to check that the "::"
5260 and "~" tokens are present; if they are not, then we can avoid
5261 calling type_name. */
5262 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5263 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5264 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5266 cp_parser_error (parser, "non-scalar type");
5270 /* Look for the type-name. */
5271 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5272 if (*scope == error_mark_node)
5275 /* Look for the `::' token. */
5276 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5281 /* Look for the `~'. */
5282 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5283 /* Look for the type-name again. We are not responsible for
5284 checking that it matches the first type-name. */
5285 *type = cp_parser_nonclass_name (parser);
5288 /* Parse a unary-expression.
5294 unary-operator cast-expression
5295 sizeof unary-expression
5303 __extension__ cast-expression
5304 __alignof__ unary-expression
5305 __alignof__ ( type-id )
5306 __real__ cast-expression
5307 __imag__ cast-expression
5310 ADDRESS_P is true iff the unary-expression is appearing as the
5311 operand of the `&' operator. CAST_P is true if this expression is
5312 the target of a cast.
5314 Returns a representation of the expression. */
5317 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5320 enum tree_code unary_operator;
5322 /* Peek at the next token. */
5323 token = cp_lexer_peek_token (parser->lexer);
5324 /* Some keywords give away the kind of expression. */
5325 if (token->type == CPP_KEYWORD)
5327 enum rid keyword = token->keyword;
5337 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5338 /* Consume the token. */
5339 cp_lexer_consume_token (parser->lexer);
5340 /* Parse the operand. */
5341 operand = cp_parser_sizeof_operand (parser, keyword);
5343 if (TYPE_P (operand))
5344 return cxx_sizeof_or_alignof_type (operand, op, true);
5346 return cxx_sizeof_or_alignof_expr (operand, op, true);
5350 return cp_parser_new_expression (parser);
5353 return cp_parser_delete_expression (parser);
5357 /* The saved value of the PEDANTIC flag. */
5361 /* Save away the PEDANTIC flag. */
5362 cp_parser_extension_opt (parser, &saved_pedantic);
5363 /* Parse the cast-expression. */
5364 expr = cp_parser_simple_cast_expression (parser);
5365 /* Restore the PEDANTIC flag. */
5366 pedantic = saved_pedantic;
5376 /* Consume the `__real__' or `__imag__' token. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* Parse the cast-expression. */
5379 expression = cp_parser_simple_cast_expression (parser);
5380 /* Create the complete representation. */
5381 return build_x_unary_op ((keyword == RID_REALPART
5382 ? REALPART_EXPR : IMAGPART_EXPR),
5384 tf_warning_or_error);
5393 /* Look for the `:: new' and `:: delete', which also signal the
5394 beginning of a new-expression, or delete-expression,
5395 respectively. If the next token is `::', then it might be one of
5397 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5401 /* See if the token after the `::' is one of the keywords in
5402 which we're interested. */
5403 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5404 /* If it's `new', we have a new-expression. */
5405 if (keyword == RID_NEW)
5406 return cp_parser_new_expression (parser);
5407 /* Similarly, for `delete'. */
5408 else if (keyword == RID_DELETE)
5409 return cp_parser_delete_expression (parser);
5412 /* Look for a unary operator. */
5413 unary_operator = cp_parser_unary_operator (token);
5414 /* The `++' and `--' operators can be handled similarly, even though
5415 they are not technically unary-operators in the grammar. */
5416 if (unary_operator == ERROR_MARK)
5418 if (token->type == CPP_PLUS_PLUS)
5419 unary_operator = PREINCREMENT_EXPR;
5420 else if (token->type == CPP_MINUS_MINUS)
5421 unary_operator = PREDECREMENT_EXPR;
5422 /* Handle the GNU address-of-label extension. */
5423 else if (cp_parser_allow_gnu_extensions_p (parser)
5424 && token->type == CPP_AND_AND)
5429 /* Consume the '&&' token. */
5430 cp_lexer_consume_token (parser->lexer);
5431 /* Look for the identifier. */
5432 identifier = cp_parser_identifier (parser);
5433 /* Create an expression representing the address. */
5434 expression = finish_label_address_expr (identifier);
5435 if (cp_parser_non_integral_constant_expression (parser,
5436 "the address of a label"))
5437 expression = error_mark_node;
5441 if (unary_operator != ERROR_MARK)
5443 tree cast_expression;
5444 tree expression = error_mark_node;
5445 const char *non_constant_p = NULL;
5447 /* Consume the operator token. */
5448 token = cp_lexer_consume_token (parser->lexer);
5449 /* Parse the cast-expression. */
5451 = cp_parser_cast_expression (parser,
5452 unary_operator == ADDR_EXPR,
5454 /* Now, build an appropriate representation. */
5455 switch (unary_operator)
5458 non_constant_p = "%<*%>";
5459 expression = build_x_indirect_ref (cast_expression, "unary *",
5460 tf_warning_or_error);
5464 non_constant_p = "%<&%>";
5467 expression = build_x_unary_op (unary_operator, cast_expression,
5468 tf_warning_or_error);
5471 case PREINCREMENT_EXPR:
5472 case PREDECREMENT_EXPR:
5473 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5474 ? "%<++%>" : "%<--%>");
5476 case UNARY_PLUS_EXPR:
5478 case TRUTH_NOT_EXPR:
5479 expression = finish_unary_op_expr (unary_operator, cast_expression);
5487 && cp_parser_non_integral_constant_expression (parser,
5489 expression = error_mark_node;
5494 return cp_parser_postfix_expression (parser, address_p, cast_p,
5495 /*member_access_only_p=*/false);
5498 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5499 unary-operator, the corresponding tree code is returned. */
5501 static enum tree_code
5502 cp_parser_unary_operator (cp_token* token)
5504 switch (token->type)
5507 return INDIRECT_REF;
5513 return UNARY_PLUS_EXPR;
5519 return TRUTH_NOT_EXPR;
5522 return BIT_NOT_EXPR;
5529 /* Parse a new-expression.
5532 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5533 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5535 Returns a representation of the expression. */
5538 cp_parser_new_expression (cp_parser* parser)
5540 bool global_scope_p;
5546 /* Look for the optional `::' operator. */
5548 = (cp_parser_global_scope_opt (parser,
5549 /*current_scope_valid_p=*/false)
5551 /* Look for the `new' operator. */
5552 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5553 /* There's no easy way to tell a new-placement from the
5554 `( type-id )' construct. */
5555 cp_parser_parse_tentatively (parser);
5556 /* Look for a new-placement. */
5557 placement = cp_parser_new_placement (parser);
5558 /* If that didn't work out, there's no new-placement. */
5559 if (!cp_parser_parse_definitely (parser))
5560 placement = NULL_TREE;
5562 /* If the next token is a `(', then we have a parenthesized
5564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5567 /* Consume the `('. */
5568 cp_lexer_consume_token (parser->lexer);
5569 /* Parse the type-id. */
5570 type = cp_parser_type_id (parser);
5571 /* Look for the closing `)'. */
5572 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5573 token = cp_lexer_peek_token (parser->lexer);
5574 /* There should not be a direct-new-declarator in this production,
5575 but GCC used to allowed this, so we check and emit a sensible error
5576 message for this case. */
5577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5579 error ("%Harray bound forbidden after parenthesized type-id",
5581 inform (token->location,
5582 "try removing the parentheses around the type-id");
5583 cp_parser_direct_new_declarator (parser);
5587 /* Otherwise, there must be a new-type-id. */
5589 type = cp_parser_new_type_id (parser, &nelts);
5591 /* If the next token is a `(' or '{', then we have a new-initializer. */
5592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5593 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5594 initializer = cp_parser_new_initializer (parser);
5596 initializer = NULL_TREE;
5598 /* A new-expression may not appear in an integral constant
5600 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5601 return error_mark_node;
5603 /* Create a representation of the new-expression. */
5604 return build_new (placement, type, nelts, initializer, global_scope_p,
5605 tf_warning_or_error);
5608 /* Parse a new-placement.
5613 Returns the same representation as for an expression-list. */
5616 cp_parser_new_placement (cp_parser* parser)
5618 tree expression_list;
5620 /* Parse the expression-list. */
5621 expression_list = (cp_parser_parenthesized_expression_list
5622 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5623 /*non_constant_p=*/NULL));
5625 return expression_list;
5628 /* Parse a new-type-id.
5631 type-specifier-seq new-declarator [opt]
5633 Returns the TYPE allocated. If the new-type-id indicates an array
5634 type, *NELTS is set to the number of elements in the last array
5635 bound; the TYPE will not include the last array bound. */
5638 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5640 cp_decl_specifier_seq type_specifier_seq;
5641 cp_declarator *new_declarator;
5642 cp_declarator *declarator;
5643 cp_declarator *outer_declarator;
5644 const char *saved_message;
5647 /* The type-specifier sequence must not contain type definitions.
5648 (It cannot contain declarations of new types either, but if they
5649 are not definitions we will catch that because they are not
5651 saved_message = parser->type_definition_forbidden_message;
5652 parser->type_definition_forbidden_message
5653 = "types may not be defined in a new-type-id";
5654 /* Parse the type-specifier-seq. */
5655 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5656 &type_specifier_seq);
5657 /* Restore the old message. */
5658 parser->type_definition_forbidden_message = saved_message;
5659 /* Parse the new-declarator. */
5660 new_declarator = cp_parser_new_declarator_opt (parser);
5662 /* Determine the number of elements in the last array dimension, if
5665 /* Skip down to the last array dimension. */
5666 declarator = new_declarator;
5667 outer_declarator = NULL;
5668 while (declarator && (declarator->kind == cdk_pointer
5669 || declarator->kind == cdk_ptrmem))
5671 outer_declarator = declarator;
5672 declarator = declarator->declarator;
5675 && declarator->kind == cdk_array
5676 && declarator->declarator
5677 && declarator->declarator->kind == cdk_array)
5679 outer_declarator = declarator;
5680 declarator = declarator->declarator;
5683 if (declarator && declarator->kind == cdk_array)
5685 *nelts = declarator->u.array.bounds;
5686 if (*nelts == error_mark_node)
5687 *nelts = integer_one_node;
5689 if (outer_declarator)
5690 outer_declarator->declarator = declarator->declarator;
5692 new_declarator = NULL;
5695 type = groktypename (&type_specifier_seq, new_declarator);
5699 /* Parse an (optional) new-declarator.
5702 ptr-operator new-declarator [opt]
5703 direct-new-declarator
5705 Returns the declarator. */
5707 static cp_declarator *
5708 cp_parser_new_declarator_opt (cp_parser* parser)
5710 enum tree_code code;
5712 cp_cv_quals cv_quals;
5714 /* We don't know if there's a ptr-operator next, or not. */
5715 cp_parser_parse_tentatively (parser);
5716 /* Look for a ptr-operator. */
5717 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5718 /* If that worked, look for more new-declarators. */
5719 if (cp_parser_parse_definitely (parser))
5721 cp_declarator *declarator;
5723 /* Parse another optional declarator. */
5724 declarator = cp_parser_new_declarator_opt (parser);
5726 return cp_parser_make_indirect_declarator
5727 (code, type, cv_quals, declarator);
5730 /* If the next token is a `[', there is a direct-new-declarator. */
5731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5732 return cp_parser_direct_new_declarator (parser);
5737 /* Parse a direct-new-declarator.
5739 direct-new-declarator:
5741 direct-new-declarator [constant-expression]
5745 static cp_declarator *
5746 cp_parser_direct_new_declarator (cp_parser* parser)
5748 cp_declarator *declarator = NULL;
5754 /* Look for the opening `['. */
5755 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5756 /* The first expression is not required to be constant. */
5759 cp_token *token = cp_lexer_peek_token (parser->lexer);
5760 expression = cp_parser_expression (parser, /*cast_p=*/false);
5761 /* The standard requires that the expression have integral
5762 type. DR 74 adds enumeration types. We believe that the
5763 real intent is that these expressions be handled like the
5764 expression in a `switch' condition, which also allows
5765 classes with a single conversion to integral or
5766 enumeration type. */
5767 if (!processing_template_decl)
5770 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5775 error ("%Hexpression in new-declarator must have integral "
5776 "or enumeration type", &token->location);
5777 expression = error_mark_node;
5781 /* But all the other expressions must be. */
5784 = cp_parser_constant_expression (parser,
5785 /*allow_non_constant=*/false,
5787 /* Look for the closing `]'. */
5788 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5790 /* Add this bound to the declarator. */
5791 declarator = make_array_declarator (declarator, expression);
5793 /* If the next token is not a `[', then there are no more
5795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5802 /* Parse a new-initializer.
5805 ( expression-list [opt] )
5808 Returns a representation of the expression-list. If there is no
5809 expression-list, VOID_ZERO_NODE is returned. */
5812 cp_parser_new_initializer (cp_parser* parser)
5814 tree expression_list;
5816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5818 bool expr_non_constant_p;
5819 maybe_warn_cpp0x ("extended initializer lists");
5820 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5821 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5822 expression_list = build_tree_list (NULL_TREE, expression_list);
5825 expression_list = (cp_parser_parenthesized_expression_list
5826 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5827 /*non_constant_p=*/NULL));
5828 if (!expression_list)
5829 expression_list = void_zero_node;
5831 return expression_list;
5834 /* Parse a delete-expression.
5837 :: [opt] delete cast-expression
5838 :: [opt] delete [ ] cast-expression
5840 Returns a representation of the expression. */
5843 cp_parser_delete_expression (cp_parser* parser)
5845 bool global_scope_p;
5849 /* Look for the optional `::' operator. */
5851 = (cp_parser_global_scope_opt (parser,
5852 /*current_scope_valid_p=*/false)
5854 /* Look for the `delete' keyword. */
5855 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5856 /* See if the array syntax is in use. */
5857 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5859 /* Consume the `[' token. */
5860 cp_lexer_consume_token (parser->lexer);
5861 /* Look for the `]' token. */
5862 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5863 /* Remember that this is the `[]' construct. */
5869 /* Parse the cast-expression. */
5870 expression = cp_parser_simple_cast_expression (parser);
5872 /* A delete-expression may not appear in an integral constant
5874 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5875 return error_mark_node;
5877 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5880 /* Parse a cast-expression.
5884 ( type-id ) cast-expression
5886 ADDRESS_P is true iff the unary-expression is appearing as the
5887 operand of the `&' operator. CAST_P is true if this expression is
5888 the target of a cast.
5890 Returns a representation of the expression. */
5893 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5895 /* If it's a `(', then we might be looking at a cast. */
5896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5898 tree type = NULL_TREE;
5899 tree expr = NULL_TREE;
5900 bool compound_literal_p;
5901 const char *saved_message;
5903 /* There's no way to know yet whether or not this is a cast.
5904 For example, `(int (3))' is a unary-expression, while `(int)
5905 3' is a cast. So, we resort to parsing tentatively. */
5906 cp_parser_parse_tentatively (parser);
5907 /* Types may not be defined in a cast. */
5908 saved_message = parser->type_definition_forbidden_message;
5909 parser->type_definition_forbidden_message
5910 = "types may not be defined in casts";
5911 /* Consume the `('. */
5912 cp_lexer_consume_token (parser->lexer);
5913 /* A very tricky bit is that `(struct S) { 3 }' is a
5914 compound-literal (which we permit in C++ as an extension).
5915 But, that construct is not a cast-expression -- it is a
5916 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5917 is legal; if the compound-literal were a cast-expression,
5918 you'd need an extra set of parentheses.) But, if we parse
5919 the type-id, and it happens to be a class-specifier, then we
5920 will commit to the parse at that point, because we cannot
5921 undo the action that is done when creating a new class. So,
5922 then we cannot back up and do a postfix-expression.
5924 Therefore, we scan ahead to the closing `)', and check to see
5925 if the token after the `)' is a `{'. If so, we are not
5926 looking at a cast-expression.
5928 Save tokens so that we can put them back. */
5929 cp_lexer_save_tokens (parser->lexer);
5930 /* Skip tokens until the next token is a closing parenthesis.
5931 If we find the closing `)', and the next token is a `{', then
5932 we are looking at a compound-literal. */
5934 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5935 /*consume_paren=*/true)
5936 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5937 /* Roll back the tokens we skipped. */
5938 cp_lexer_rollback_tokens (parser->lexer);
5939 /* If we were looking at a compound-literal, simulate an error
5940 so that the call to cp_parser_parse_definitely below will
5942 if (compound_literal_p)
5943 cp_parser_simulate_error (parser);
5946 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5947 parser->in_type_id_in_expr_p = true;
5948 /* Look for the type-id. */
5949 type = cp_parser_type_id (parser);
5950 /* Look for the closing `)'. */
5951 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5952 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5955 /* Restore the saved message. */
5956 parser->type_definition_forbidden_message = saved_message;
5958 /* If ok so far, parse the dependent expression. We cannot be
5959 sure it is a cast. Consider `(T ())'. It is a parenthesized
5960 ctor of T, but looks like a cast to function returning T
5961 without a dependent expression. */
5962 if (!cp_parser_error_occurred (parser))
5963 expr = cp_parser_cast_expression (parser,
5964 /*address_p=*/false,
5967 if (cp_parser_parse_definitely (parser))
5969 /* Warn about old-style casts, if so requested. */
5970 if (warn_old_style_cast
5971 && !in_system_header
5972 && !VOID_TYPE_P (type)
5973 && current_lang_name != lang_name_c)
5974 warning (OPT_Wold_style_cast, "use of old-style cast");
5976 /* Only type conversions to integral or enumeration types
5977 can be used in constant-expressions. */
5978 if (!cast_valid_in_integral_constant_expression_p (type)
5979 && (cp_parser_non_integral_constant_expression
5981 "a cast to a type other than an integral or "
5982 "enumeration type")))
5983 return error_mark_node;
5985 /* Perform the cast. */
5986 expr = build_c_cast (type, expr);
5991 /* If we get here, then it's not a cast, so it must be a
5992 unary-expression. */
5993 return cp_parser_unary_expression (parser, address_p, cast_p);
5996 /* Parse a binary expression of the general form:
6000 pm-expression .* cast-expression
6001 pm-expression ->* cast-expression
6003 multiplicative-expression:
6005 multiplicative-expression * pm-expression
6006 multiplicative-expression / pm-expression
6007 multiplicative-expression % pm-expression
6009 additive-expression:
6010 multiplicative-expression
6011 additive-expression + multiplicative-expression
6012 additive-expression - multiplicative-expression
6016 shift-expression << additive-expression
6017 shift-expression >> additive-expression
6019 relational-expression:
6021 relational-expression < shift-expression
6022 relational-expression > shift-expression
6023 relational-expression <= shift-expression
6024 relational-expression >= shift-expression
6028 relational-expression:
6029 relational-expression <? shift-expression
6030 relational-expression >? shift-expression
6032 equality-expression:
6033 relational-expression
6034 equality-expression == relational-expression
6035 equality-expression != relational-expression
6039 and-expression & equality-expression
6041 exclusive-or-expression:
6043 exclusive-or-expression ^ and-expression
6045 inclusive-or-expression:
6046 exclusive-or-expression
6047 inclusive-or-expression | exclusive-or-expression
6049 logical-and-expression:
6050 inclusive-or-expression
6051 logical-and-expression && inclusive-or-expression
6053 logical-or-expression:
6054 logical-and-expression
6055 logical-or-expression || logical-and-expression
6057 All these are implemented with a single function like:
6060 simple-cast-expression
6061 binary-expression <token> binary-expression
6063 CAST_P is true if this expression is the target of a cast.
6065 The binops_by_token map is used to get the tree codes for each <token> type.
6066 binary-expressions are associated according to a precedence table. */
6068 #define TOKEN_PRECEDENCE(token) \
6069 (((token->type == CPP_GREATER \
6070 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6071 && !parser->greater_than_is_operator_p) \
6072 ? PREC_NOT_OPERATOR \
6073 : binops_by_token[token->type].prec)
6076 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6077 enum cp_parser_prec prec)
6079 cp_parser_expression_stack stack;
6080 cp_parser_expression_stack_entry *sp = &stack[0];
6083 enum tree_code tree_type, lhs_type, rhs_type;
6084 enum cp_parser_prec new_prec, lookahead_prec;
6087 /* Parse the first expression. */
6088 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6089 lhs_type = ERROR_MARK;
6093 /* Get an operator token. */
6094 token = cp_lexer_peek_token (parser->lexer);
6096 if (warn_cxx0x_compat
6097 && token->type == CPP_RSHIFT
6098 && !parser->greater_than_is_operator_p)
6100 warning (OPT_Wc__0x_compat,
6101 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6103 warning (OPT_Wc__0x_compat,
6104 "suggest parentheses around %<>>%> expression");
6107 new_prec = TOKEN_PRECEDENCE (token);
6109 /* Popping an entry off the stack means we completed a subexpression:
6110 - either we found a token which is not an operator (`>' where it is not
6111 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6112 will happen repeatedly;
6113 - or, we found an operator which has lower priority. This is the case
6114 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6116 if (new_prec <= prec)
6125 tree_type = binops_by_token[token->type].tree_type;
6127 /* We used the operator token. */
6128 cp_lexer_consume_token (parser->lexer);
6130 /* Extract another operand. It may be the RHS of this expression
6131 or the LHS of a new, higher priority expression. */
6132 rhs = cp_parser_simple_cast_expression (parser);
6133 rhs_type = ERROR_MARK;
6135 /* Get another operator token. Look up its precedence to avoid
6136 building a useless (immediately popped) stack entry for common
6137 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6138 token = cp_lexer_peek_token (parser->lexer);
6139 lookahead_prec = TOKEN_PRECEDENCE (token);
6140 if (lookahead_prec > new_prec)
6142 /* ... and prepare to parse the RHS of the new, higher priority
6143 expression. Since precedence levels on the stack are
6144 monotonically increasing, we do not have to care about
6147 sp->tree_type = tree_type;
6149 sp->lhs_type = lhs_type;
6152 lhs_type = rhs_type;
6154 new_prec = lookahead_prec;
6158 /* If the stack is not empty, we have parsed into LHS the right side
6159 (`4' in the example above) of an expression we had suspended.
6160 We can use the information on the stack to recover the LHS (`3')
6161 from the stack together with the tree code (`MULT_EXPR'), and
6162 the precedence of the higher level subexpression
6163 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6164 which will be used to actually build the additive expression. */
6167 tree_type = sp->tree_type;
6169 rhs_type = lhs_type;
6171 lhs_type = sp->lhs_type;
6174 overloaded_p = false;
6175 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6176 &overloaded_p, tf_warning_or_error);
6177 lhs_type = tree_type;
6179 /* If the binary operator required the use of an overloaded operator,
6180 then this expression cannot be an integral constant-expression.
6181 An overloaded operator can be used even if both operands are
6182 otherwise permissible in an integral constant-expression if at
6183 least one of the operands is of enumeration type. */
6186 && (cp_parser_non_integral_constant_expression
6187 (parser, "calls to overloaded operators")))
6188 return error_mark_node;
6195 /* Parse the `? expression : assignment-expression' part of a
6196 conditional-expression. The LOGICAL_OR_EXPR is the
6197 logical-or-expression that started the conditional-expression.
6198 Returns a representation of the entire conditional-expression.
6200 This routine is used by cp_parser_assignment_expression.
6202 ? expression : assignment-expression
6206 ? : assignment-expression */
6209 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6212 tree assignment_expr;
6214 /* Consume the `?' token. */
6215 cp_lexer_consume_token (parser->lexer);
6216 if (cp_parser_allow_gnu_extensions_p (parser)
6217 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6218 /* Implicit true clause. */
6221 /* Parse the expression. */
6222 expr = cp_parser_expression (parser, /*cast_p=*/false);
6224 /* The next token should be a `:'. */
6225 cp_parser_require (parser, CPP_COLON, "%<:%>");
6226 /* Parse the assignment-expression. */
6227 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6229 /* Build the conditional-expression. */
6230 return build_x_conditional_expr (logical_or_expr,
6233 tf_warning_or_error);
6236 /* Parse an assignment-expression.
6238 assignment-expression:
6239 conditional-expression
6240 logical-or-expression assignment-operator assignment_expression
6243 CAST_P is true if this expression is the target of a cast.
6245 Returns a representation for the expression. */
6248 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6252 /* If the next token is the `throw' keyword, then we're looking at
6253 a throw-expression. */
6254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6255 expr = cp_parser_throw_expression (parser);
6256 /* Otherwise, it must be that we are looking at a
6257 logical-or-expression. */
6260 /* Parse the binary expressions (logical-or-expression). */
6261 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6262 /* If the next token is a `?' then we're actually looking at a
6263 conditional-expression. */
6264 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6265 return cp_parser_question_colon_clause (parser, expr);
6268 enum tree_code assignment_operator;
6270 /* If it's an assignment-operator, we're using the second
6273 = cp_parser_assignment_operator_opt (parser);
6274 if (assignment_operator != ERROR_MARK)
6276 bool non_constant_p;
6278 /* Parse the right-hand side of the assignment. */
6279 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6281 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6282 maybe_warn_cpp0x ("extended initializer lists");
6284 /* An assignment may not appear in a
6285 constant-expression. */
6286 if (cp_parser_non_integral_constant_expression (parser,
6288 return error_mark_node;
6289 /* Build the assignment expression. */
6290 expr = build_x_modify_expr (expr,
6291 assignment_operator,
6293 tf_warning_or_error);
6301 /* Parse an (optional) assignment-operator.
6303 assignment-operator: one of
6304 = *= /= %= += -= >>= <<= &= ^= |=
6308 assignment-operator: one of
6311 If the next token is an assignment operator, the corresponding tree
6312 code is returned, and the token is consumed. For example, for
6313 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6314 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6315 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6316 operator, ERROR_MARK is returned. */
6318 static enum tree_code
6319 cp_parser_assignment_operator_opt (cp_parser* parser)
6324 /* Peek at the next token. */
6325 token = cp_lexer_peek_token (parser->lexer);
6327 switch (token->type)
6338 op = TRUNC_DIV_EXPR;
6342 op = TRUNC_MOD_EXPR;
6374 /* Nothing else is an assignment operator. */
6378 /* If it was an assignment operator, consume it. */
6379 if (op != ERROR_MARK)
6380 cp_lexer_consume_token (parser->lexer);
6385 /* Parse an expression.
6388 assignment-expression
6389 expression , assignment-expression
6391 CAST_P is true if this expression is the target of a cast.
6393 Returns a representation of the expression. */
6396 cp_parser_expression (cp_parser* parser, bool cast_p)
6398 tree expression = NULL_TREE;
6402 tree assignment_expression;
6404 /* Parse the next assignment-expression. */
6405 assignment_expression
6406 = cp_parser_assignment_expression (parser, cast_p);
6407 /* If this is the first assignment-expression, we can just
6410 expression = assignment_expression;
6412 expression = build_x_compound_expr (expression,
6413 assignment_expression,
6414 tf_warning_or_error);
6415 /* If the next token is not a comma, then we are done with the
6417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6419 /* Consume the `,'. */
6420 cp_lexer_consume_token (parser->lexer);
6421 /* A comma operator cannot appear in a constant-expression. */
6422 if (cp_parser_non_integral_constant_expression (parser,
6423 "a comma operator"))
6424 expression = error_mark_node;
6430 /* Parse a constant-expression.
6432 constant-expression:
6433 conditional-expression
6435 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6436 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6437 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6438 is false, NON_CONSTANT_P should be NULL. */
6441 cp_parser_constant_expression (cp_parser* parser,
6442 bool allow_non_constant_p,
6443 bool *non_constant_p)
6445 bool saved_integral_constant_expression_p;
6446 bool saved_allow_non_integral_constant_expression_p;
6447 bool saved_non_integral_constant_expression_p;
6450 /* It might seem that we could simply parse the
6451 conditional-expression, and then check to see if it were
6452 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6453 one that the compiler can figure out is constant, possibly after
6454 doing some simplifications or optimizations. The standard has a
6455 precise definition of constant-expression, and we must honor
6456 that, even though it is somewhat more restrictive.
6462 is not a legal declaration, because `(2, 3)' is not a
6463 constant-expression. The `,' operator is forbidden in a
6464 constant-expression. However, GCC's constant-folding machinery
6465 will fold this operation to an INTEGER_CST for `3'. */
6467 /* Save the old settings. */
6468 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6469 saved_allow_non_integral_constant_expression_p
6470 = parser->allow_non_integral_constant_expression_p;
6471 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6472 /* We are now parsing a constant-expression. */
6473 parser->integral_constant_expression_p = true;
6474 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6475 parser->non_integral_constant_expression_p = false;
6476 /* Although the grammar says "conditional-expression", we parse an
6477 "assignment-expression", which also permits "throw-expression"
6478 and the use of assignment operators. In the case that
6479 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6480 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6481 actually essential that we look for an assignment-expression.
6482 For example, cp_parser_initializer_clauses uses this function to
6483 determine whether a particular assignment-expression is in fact
6485 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6486 /* Restore the old settings. */
6487 parser->integral_constant_expression_p
6488 = saved_integral_constant_expression_p;
6489 parser->allow_non_integral_constant_expression_p
6490 = saved_allow_non_integral_constant_expression_p;
6491 if (allow_non_constant_p)
6492 *non_constant_p = parser->non_integral_constant_expression_p;
6493 else if (parser->non_integral_constant_expression_p)
6494 expression = error_mark_node;
6495 parser->non_integral_constant_expression_p
6496 = saved_non_integral_constant_expression_p;
6501 /* Parse __builtin_offsetof.
6503 offsetof-expression:
6504 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6506 offsetof-member-designator:
6508 | offsetof-member-designator "." id-expression
6509 | offsetof-member-designator "[" expression "]" */
6512 cp_parser_builtin_offsetof (cp_parser *parser)
6514 int save_ice_p, save_non_ice_p;
6519 /* We're about to accept non-integral-constant things, but will
6520 definitely yield an integral constant expression. Save and
6521 restore these values around our local parsing. */
6522 save_ice_p = parser->integral_constant_expression_p;
6523 save_non_ice_p = parser->non_integral_constant_expression_p;
6525 /* Consume the "__builtin_offsetof" token. */
6526 cp_lexer_consume_token (parser->lexer);
6527 /* Consume the opening `('. */
6528 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6529 /* Parse the type-id. */
6530 type = cp_parser_type_id (parser);
6531 /* Look for the `,'. */
6532 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6533 token = cp_lexer_peek_token (parser->lexer);
6535 /* Build the (type *)null that begins the traditional offsetof macro. */
6536 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6537 tf_warning_or_error);
6539 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6540 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6541 true, &dummy, token->location);
6544 token = cp_lexer_peek_token (parser->lexer);
6545 switch (token->type)
6547 case CPP_OPEN_SQUARE:
6548 /* offsetof-member-designator "[" expression "]" */
6549 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6553 /* offsetof-member-designator "." identifier */
6554 cp_lexer_consume_token (parser->lexer);
6555 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6560 case CPP_CLOSE_PAREN:
6561 /* Consume the ")" token. */
6562 cp_lexer_consume_token (parser->lexer);
6566 /* Error. We know the following require will fail, but
6567 that gives the proper error message. */
6568 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6569 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6570 expr = error_mark_node;
6576 /* If we're processing a template, we can't finish the semantics yet.
6577 Otherwise we can fold the entire expression now. */
6578 if (processing_template_decl)
6579 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6581 expr = finish_offsetof (expr);
6584 parser->integral_constant_expression_p = save_ice_p;
6585 parser->non_integral_constant_expression_p = save_non_ice_p;
6590 /* Parse a trait expression. */
6593 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6596 tree type1, type2 = NULL_TREE;
6597 bool binary = false;
6598 cp_decl_specifier_seq decl_specs;
6602 case RID_HAS_NOTHROW_ASSIGN:
6603 kind = CPTK_HAS_NOTHROW_ASSIGN;
6605 case RID_HAS_NOTHROW_CONSTRUCTOR:
6606 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6608 case RID_HAS_NOTHROW_COPY:
6609 kind = CPTK_HAS_NOTHROW_COPY;
6611 case RID_HAS_TRIVIAL_ASSIGN:
6612 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6614 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6615 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6617 case RID_HAS_TRIVIAL_COPY:
6618 kind = CPTK_HAS_TRIVIAL_COPY;
6620 case RID_HAS_TRIVIAL_DESTRUCTOR:
6621 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6623 case RID_HAS_VIRTUAL_DESTRUCTOR:
6624 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6626 case RID_IS_ABSTRACT:
6627 kind = CPTK_IS_ABSTRACT;
6629 case RID_IS_BASE_OF:
6630 kind = CPTK_IS_BASE_OF;
6634 kind = CPTK_IS_CLASS;
6636 case RID_IS_CONVERTIBLE_TO:
6637 kind = CPTK_IS_CONVERTIBLE_TO;
6641 kind = CPTK_IS_EMPTY;
6644 kind = CPTK_IS_ENUM;
6649 case RID_IS_POLYMORPHIC:
6650 kind = CPTK_IS_POLYMORPHIC;
6653 kind = CPTK_IS_UNION;
6659 /* Consume the token. */
6660 cp_lexer_consume_token (parser->lexer);
6662 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6664 type1 = cp_parser_type_id (parser);
6666 if (type1 == error_mark_node)
6667 return error_mark_node;
6669 /* Build a trivial decl-specifier-seq. */
6670 clear_decl_specs (&decl_specs);
6671 decl_specs.type = type1;
6673 /* Call grokdeclarator to figure out what type this is. */
6674 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6675 /*initialized=*/0, /*attrlist=*/NULL);
6679 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6681 type2 = cp_parser_type_id (parser);
6683 if (type2 == error_mark_node)
6684 return error_mark_node;
6686 /* Build a trivial decl-specifier-seq. */
6687 clear_decl_specs (&decl_specs);
6688 decl_specs.type = type2;
6690 /* Call grokdeclarator to figure out what type this is. */
6691 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6692 /*initialized=*/0, /*attrlist=*/NULL);
6695 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6697 /* Complete the trait expression, which may mean either processing
6698 the trait expr now or saving it for template instantiation. */
6699 return finish_trait_expr (kind, type1, type2);
6702 /* Statements [gram.stmt.stmt] */
6704 /* Parse a statement.
6708 expression-statement
6713 declaration-statement
6716 IN_COMPOUND is true when the statement is nested inside a
6717 cp_parser_compound_statement; this matters for certain pragmas.
6719 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6720 is a (possibly labeled) if statement which is not enclosed in braces
6721 and has an else clause. This is used to implement -Wparentheses. */
6724 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6725 bool in_compound, bool *if_p)
6729 location_t statement_location;
6734 /* There is no statement yet. */
6735 statement = NULL_TREE;
6736 /* Peek at the next token. */
6737 token = cp_lexer_peek_token (parser->lexer);
6738 /* Remember the location of the first token in the statement. */
6739 statement_location = token->location;
6740 /* If this is a keyword, then that will often determine what kind of
6741 statement we have. */
6742 if (token->type == CPP_KEYWORD)
6744 enum rid keyword = token->keyword;
6750 /* Looks like a labeled-statement with a case label.
6751 Parse the label, and then use tail recursion to parse
6753 cp_parser_label_for_labeled_statement (parser);
6758 statement = cp_parser_selection_statement (parser, if_p);
6764 statement = cp_parser_iteration_statement (parser);
6771 statement = cp_parser_jump_statement (parser);
6774 /* Objective-C++ exception-handling constructs. */
6777 case RID_AT_FINALLY:
6778 case RID_AT_SYNCHRONIZED:
6780 statement = cp_parser_objc_statement (parser);
6784 statement = cp_parser_try_block (parser);
6788 /* This must be a namespace alias definition. */
6789 cp_parser_declaration_statement (parser);
6793 /* It might be a keyword like `int' that can start a
6794 declaration-statement. */
6798 else if (token->type == CPP_NAME)
6800 /* If the next token is a `:', then we are looking at a
6801 labeled-statement. */
6802 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6803 if (token->type == CPP_COLON)
6805 /* Looks like a labeled-statement with an ordinary label.
6806 Parse the label, and then use tail recursion to parse
6808 cp_parser_label_for_labeled_statement (parser);
6812 /* Anything that starts with a `{' must be a compound-statement. */
6813 else if (token->type == CPP_OPEN_BRACE)
6814 statement = cp_parser_compound_statement (parser, NULL, false);
6815 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6816 a statement all its own. */
6817 else if (token->type == CPP_PRAGMA)
6819 /* Only certain OpenMP pragmas are attached to statements, and thus
6820 are considered statements themselves. All others are not. In
6821 the context of a compound, accept the pragma as a "statement" and
6822 return so that we can check for a close brace. Otherwise we
6823 require a real statement and must go back and read one. */
6825 cp_parser_pragma (parser, pragma_compound);
6826 else if (!cp_parser_pragma (parser, pragma_stmt))
6830 else if (token->type == CPP_EOF)
6832 cp_parser_error (parser, "expected statement");
6836 /* Everything else must be a declaration-statement or an
6837 expression-statement. Try for the declaration-statement
6838 first, unless we are looking at a `;', in which case we know that
6839 we have an expression-statement. */
6842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6844 cp_parser_parse_tentatively (parser);
6845 /* Try to parse the declaration-statement. */
6846 cp_parser_declaration_statement (parser);
6847 /* If that worked, we're done. */
6848 if (cp_parser_parse_definitely (parser))
6851 /* Look for an expression-statement instead. */
6852 statement = cp_parser_expression_statement (parser, in_statement_expr);
6855 /* Set the line number for the statement. */
6856 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6857 SET_EXPR_LOCATION (statement, statement_location);
6860 /* Parse the label for a labeled-statement, i.e.
6863 case constant-expression :
6867 case constant-expression ... constant-expression : statement
6869 When a label is parsed without errors, the label is added to the
6870 parse tree by the finish_* functions, so this function doesn't
6871 have to return the label. */
6874 cp_parser_label_for_labeled_statement (cp_parser* parser)
6878 /* The next token should be an identifier. */
6879 token = cp_lexer_peek_token (parser->lexer);
6880 if (token->type != CPP_NAME
6881 && token->type != CPP_KEYWORD)
6883 cp_parser_error (parser, "expected labeled-statement");
6887 switch (token->keyword)
6894 /* Consume the `case' token. */
6895 cp_lexer_consume_token (parser->lexer);
6896 /* Parse the constant-expression. */
6897 expr = cp_parser_constant_expression (parser,
6898 /*allow_non_constant_p=*/false,
6901 ellipsis = cp_lexer_peek_token (parser->lexer);
6902 if (ellipsis->type == CPP_ELLIPSIS)
6904 /* Consume the `...' token. */
6905 cp_lexer_consume_token (parser->lexer);
6907 cp_parser_constant_expression (parser,
6908 /*allow_non_constant_p=*/false,
6910 /* We don't need to emit warnings here, as the common code
6911 will do this for us. */
6914 expr_hi = NULL_TREE;
6916 if (parser->in_switch_statement_p)
6917 finish_case_label (expr, expr_hi);
6919 error ("%Hcase label %qE not within a switch statement",
6920 &token->location, expr);
6925 /* Consume the `default' token. */
6926 cp_lexer_consume_token (parser->lexer);
6928 if (parser->in_switch_statement_p)
6929 finish_case_label (NULL_TREE, NULL_TREE);
6931 error ("%Hcase label not within a switch statement", &token->location);
6935 /* Anything else must be an ordinary label. */
6936 finish_label_stmt (cp_parser_identifier (parser));
6940 /* Require the `:' token. */
6941 cp_parser_require (parser, CPP_COLON, "%<:%>");
6944 /* Parse an expression-statement.
6946 expression-statement:
6949 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6950 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6951 indicates whether this expression-statement is part of an
6952 expression statement. */
6955 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6957 tree statement = NULL_TREE;
6959 /* If the next token is a ';', then there is no expression
6961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6962 statement = cp_parser_expression (parser, /*cast_p=*/false);
6964 /* Consume the final `;'. */
6965 cp_parser_consume_semicolon_at_end_of_statement (parser);
6967 if (in_statement_expr
6968 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6969 /* This is the final expression statement of a statement
6971 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6973 statement = finish_expr_stmt (statement);
6980 /* Parse a compound-statement.
6983 { statement-seq [opt] }
6988 { label-declaration-seq [opt] statement-seq [opt] }
6990 label-declaration-seq:
6992 label-declaration-seq label-declaration
6994 Returns a tree representing the statement. */
6997 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7002 /* Consume the `{'. */
7003 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7004 return error_mark_node;
7005 /* Begin the compound-statement. */
7006 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7007 /* If the next keyword is `__label__' we have a label declaration. */
7008 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7009 cp_parser_label_declaration (parser);
7010 /* Parse an (optional) statement-seq. */
7011 cp_parser_statement_seq_opt (parser, in_statement_expr);
7012 /* Finish the compound-statement. */
7013 finish_compound_stmt (compound_stmt);
7014 /* Consume the `}'. */
7015 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7017 return compound_stmt;
7020 /* Parse an (optional) statement-seq.
7024 statement-seq [opt] statement */
7027 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7029 /* Scan statements until there aren't any more. */
7032 cp_token *token = cp_lexer_peek_token (parser->lexer);
7034 /* If we're looking at a `}', then we've run out of statements. */
7035 if (token->type == CPP_CLOSE_BRACE
7036 || token->type == CPP_EOF
7037 || token->type == CPP_PRAGMA_EOL)
7040 /* If we are in a compound statement and find 'else' then
7041 something went wrong. */
7042 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7044 if (parser->in_statement & IN_IF_STMT)
7048 token = cp_lexer_consume_token (parser->lexer);
7049 error ("%H%<else%> without a previous %<if%>", &token->location);
7053 /* Parse the statement. */
7054 cp_parser_statement (parser, in_statement_expr, true, NULL);
7058 /* Parse a selection-statement.
7060 selection-statement:
7061 if ( condition ) statement
7062 if ( condition ) statement else statement
7063 switch ( condition ) statement
7065 Returns the new IF_STMT or SWITCH_STMT.
7067 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7068 is a (possibly labeled) if statement which is not enclosed in
7069 braces and has an else clause. This is used to implement
7073 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7081 /* Peek at the next token. */
7082 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7084 /* See what kind of keyword it is. */
7085 keyword = token->keyword;
7094 /* Look for the `('. */
7095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7097 cp_parser_skip_to_end_of_statement (parser);
7098 return error_mark_node;
7101 /* Begin the selection-statement. */
7102 if (keyword == RID_IF)
7103 statement = begin_if_stmt ();
7105 statement = begin_switch_stmt ();
7107 /* Parse the condition. */
7108 condition = cp_parser_condition (parser);
7109 /* Look for the `)'. */
7110 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7111 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7112 /*consume_paren=*/true);
7114 if (keyword == RID_IF)
7117 unsigned char in_statement;
7119 /* Add the condition. */
7120 finish_if_stmt_cond (condition, statement);
7122 /* Parse the then-clause. */
7123 in_statement = parser->in_statement;
7124 parser->in_statement |= IN_IF_STMT;
7125 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7126 parser->in_statement = in_statement;
7128 finish_then_clause (statement);
7130 /* If the next token is `else', parse the else-clause. */
7131 if (cp_lexer_next_token_is_keyword (parser->lexer,
7134 /* Consume the `else' keyword. */
7135 cp_lexer_consume_token (parser->lexer);
7136 begin_else_clause (statement);
7137 /* Parse the else-clause. */
7138 cp_parser_implicitly_scoped_statement (parser, NULL);
7139 finish_else_clause (statement);
7141 /* If we are currently parsing a then-clause, then
7142 IF_P will not be NULL. We set it to true to
7143 indicate that this if statement has an else clause.
7144 This may trigger the Wparentheses warning below
7145 when we get back up to the parent if statement. */
7151 /* This if statement does not have an else clause. If
7152 NESTED_IF is true, then the then-clause is an if
7153 statement which does have an else clause. We warn
7154 about the potential ambiguity. */
7156 warning (OPT_Wparentheses,
7157 ("%Hsuggest explicit braces "
7158 "to avoid ambiguous %<else%>"),
7159 EXPR_LOCUS (statement));
7162 /* Now we're all done with the if-statement. */
7163 finish_if_stmt (statement);
7167 bool in_switch_statement_p;
7168 unsigned char in_statement;
7170 /* Add the condition. */
7171 finish_switch_cond (condition, statement);
7173 /* Parse the body of the switch-statement. */
7174 in_switch_statement_p = parser->in_switch_statement_p;
7175 in_statement = parser->in_statement;
7176 parser->in_switch_statement_p = true;
7177 parser->in_statement |= IN_SWITCH_STMT;
7178 cp_parser_implicitly_scoped_statement (parser, NULL);
7179 parser->in_switch_statement_p = in_switch_statement_p;
7180 parser->in_statement = in_statement;
7182 /* Now we're all done with the switch-statement. */
7183 finish_switch_stmt (statement);
7191 cp_parser_error (parser, "expected selection-statement");
7192 return error_mark_node;
7196 /* Parse a condition.
7200 type-specifier-seq declarator = initializer-clause
7201 type-specifier-seq declarator braced-init-list
7206 type-specifier-seq declarator asm-specification [opt]
7207 attributes [opt] = assignment-expression
7209 Returns the expression that should be tested. */
7212 cp_parser_condition (cp_parser* parser)
7214 cp_decl_specifier_seq type_specifiers;
7215 const char *saved_message;
7217 /* Try the declaration first. */
7218 cp_parser_parse_tentatively (parser);
7219 /* New types are not allowed in the type-specifier-seq for a
7221 saved_message = parser->type_definition_forbidden_message;
7222 parser->type_definition_forbidden_message
7223 = "types may not be defined in conditions";
7224 /* Parse the type-specifier-seq. */
7225 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7227 /* Restore the saved message. */
7228 parser->type_definition_forbidden_message = saved_message;
7229 /* If all is well, we might be looking at a declaration. */
7230 if (!cp_parser_error_occurred (parser))
7233 tree asm_specification;
7235 cp_declarator *declarator;
7236 tree initializer = NULL_TREE;
7238 /* Parse the declarator. */
7239 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7240 /*ctor_dtor_or_conv_p=*/NULL,
7241 /*parenthesized_p=*/NULL,
7242 /*member_p=*/false);
7243 /* Parse the attributes. */
7244 attributes = cp_parser_attributes_opt (parser);
7245 /* Parse the asm-specification. */
7246 asm_specification = cp_parser_asm_specification_opt (parser);
7247 /* If the next token is not an `=' or '{', then we might still be
7248 looking at an expression. For example:
7252 looks like a decl-specifier-seq and a declarator -- but then
7253 there is no `=', so this is an expression. */
7254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7255 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7256 cp_parser_simulate_error (parser);
7258 /* If we did see an `=' or '{', then we are looking at a declaration
7260 if (cp_parser_parse_definitely (parser))
7263 bool non_constant_p;
7264 bool flags = LOOKUP_ONLYCONVERTING;
7266 /* Create the declaration. */
7267 decl = start_decl (declarator, &type_specifiers,
7268 /*initialized_p=*/true,
7269 attributes, /*prefix_attributes=*/NULL_TREE,
7272 /* Parse the initializer. */
7273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7275 initializer = cp_parser_braced_list (parser, &non_constant_p);
7276 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7281 /* Consume the `='. */
7282 cp_lexer_consume_token (parser->lexer);
7283 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7285 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7286 maybe_warn_cpp0x ("extended initializer lists");
7288 if (!non_constant_p)
7289 initializer = fold_non_dependent_expr (initializer);
7291 /* Process the initializer. */
7292 cp_finish_decl (decl,
7293 initializer, !non_constant_p,
7298 pop_scope (pushed_scope);
7300 return convert_from_reference (decl);
7303 /* If we didn't even get past the declarator successfully, we are
7304 definitely not looking at a declaration. */
7306 cp_parser_abort_tentative_parse (parser);
7308 /* Otherwise, we are looking at an expression. */
7309 return cp_parser_expression (parser, /*cast_p=*/false);
7312 /* We check for a ) immediately followed by ; with no whitespacing
7313 between. This is used to issue a warning for:
7321 as the semicolon is probably extraneous.
7323 On parse errors, the next token might not be a ), so do nothing in
7327 check_empty_body (cp_parser* parser, const char* type)
7330 cp_token *close_paren;
7331 expanded_location close_loc;
7332 expanded_location semi_loc;
7334 close_paren = cp_lexer_peek_token (parser->lexer);
7335 if (close_paren->type != CPP_CLOSE_PAREN)
7338 close_loc = expand_location (close_paren->location);
7339 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7341 if (token->type != CPP_SEMICOLON
7342 || (token->flags & PREV_WHITE))
7345 semi_loc = expand_location (token->location);
7346 if (close_loc.line == semi_loc.line
7347 && close_loc.column+1 == semi_loc.column)
7348 warning (OPT_Wempty_body,
7349 "suggest a space before %<;%> or explicit braces around empty "
7350 "body in %<%s%> statement",
7354 /* Parse an iteration-statement.
7356 iteration-statement:
7357 while ( condition ) statement
7358 do statement while ( expression ) ;
7359 for ( for-init-statement condition [opt] ; expression [opt] )
7362 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7365 cp_parser_iteration_statement (cp_parser* parser)
7370 unsigned char in_statement;
7372 /* Peek at the next token. */
7373 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7375 return error_mark_node;
7377 /* Remember whether or not we are already within an iteration
7379 in_statement = parser->in_statement;
7381 /* See what kind of keyword it is. */
7382 keyword = token->keyword;
7389 /* Begin the while-statement. */
7390 statement = begin_while_stmt ();
7391 /* Look for the `('. */
7392 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7393 /* Parse the condition. */
7394 condition = cp_parser_condition (parser);
7395 finish_while_stmt_cond (condition, statement);
7396 check_empty_body (parser, "while");
7397 /* Look for the `)'. */
7398 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7399 /* Parse the dependent statement. */
7400 parser->in_statement = IN_ITERATION_STMT;
7401 cp_parser_already_scoped_statement (parser);
7402 parser->in_statement = in_statement;
7403 /* We're done with the while-statement. */
7404 finish_while_stmt (statement);
7412 /* Begin the do-statement. */
7413 statement = begin_do_stmt ();
7414 /* Parse the body of the do-statement. */
7415 parser->in_statement = IN_ITERATION_STMT;
7416 cp_parser_implicitly_scoped_statement (parser, NULL);
7417 parser->in_statement = in_statement;
7418 finish_do_body (statement);
7419 /* Look for the `while' keyword. */
7420 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7421 /* Look for the `('. */
7422 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7423 /* Parse the expression. */
7424 expression = cp_parser_expression (parser, /*cast_p=*/false);
7425 /* We're done with the do-statement. */
7426 finish_do_stmt (expression, statement);
7427 /* Look for the `)'. */
7428 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7429 /* Look for the `;'. */
7430 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7436 tree condition = NULL_TREE;
7437 tree expression = NULL_TREE;
7439 /* Begin the for-statement. */
7440 statement = begin_for_stmt ();
7441 /* Look for the `('. */
7442 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7443 /* Parse the initialization. */
7444 cp_parser_for_init_statement (parser);
7445 finish_for_init_stmt (statement);
7447 /* If there's a condition, process it. */
7448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7449 condition = cp_parser_condition (parser);
7450 finish_for_cond (condition, statement);
7451 /* Look for the `;'. */
7452 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7454 /* If there's an expression, process it. */
7455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7456 expression = cp_parser_expression (parser, /*cast_p=*/false);
7457 finish_for_expr (expression, statement);
7458 check_empty_body (parser, "for");
7459 /* Look for the `)'. */
7460 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7462 /* Parse the body of the for-statement. */
7463 parser->in_statement = IN_ITERATION_STMT;
7464 cp_parser_already_scoped_statement (parser);
7465 parser->in_statement = in_statement;
7467 /* We're done with the for-statement. */
7468 finish_for_stmt (statement);
7473 cp_parser_error (parser, "expected iteration-statement");
7474 statement = error_mark_node;
7481 /* Parse a for-init-statement.
7484 expression-statement
7485 simple-declaration */
7488 cp_parser_for_init_statement (cp_parser* parser)
7490 /* If the next token is a `;', then we have an empty
7491 expression-statement. Grammatically, this is also a
7492 simple-declaration, but an invalid one, because it does not
7493 declare anything. Therefore, if we did not handle this case
7494 specially, we would issue an error message about an invalid
7496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7498 /* We're going to speculatively look for a declaration, falling back
7499 to an expression, if necessary. */
7500 cp_parser_parse_tentatively (parser);
7501 /* Parse the declaration. */
7502 cp_parser_simple_declaration (parser,
7503 /*function_definition_allowed_p=*/false);
7504 /* If the tentative parse failed, then we shall need to look for an
7505 expression-statement. */
7506 if (cp_parser_parse_definitely (parser))
7510 cp_parser_expression_statement (parser, false);
7513 /* Parse a jump-statement.
7518 return expression [opt] ;
7519 return braced-init-list ;
7527 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7530 cp_parser_jump_statement (cp_parser* parser)
7532 tree statement = error_mark_node;
7535 unsigned char in_statement;
7537 /* Peek at the next token. */
7538 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7540 return error_mark_node;
7542 /* See what kind of keyword it is. */
7543 keyword = token->keyword;
7547 in_statement = parser->in_statement & ~IN_IF_STMT;
7548 switch (in_statement)
7551 error ("%Hbreak statement not within loop or switch", &token->location);
7554 gcc_assert ((in_statement & IN_SWITCH_STMT)
7555 || in_statement == IN_ITERATION_STMT);
7556 statement = finish_break_stmt ();
7559 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7562 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7565 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7569 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7572 error ("%Hcontinue statement not within a loop", &token->location);
7574 case IN_ITERATION_STMT:
7576 statement = finish_continue_stmt ();
7579 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7584 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7590 bool expr_non_constant_p;
7592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7594 maybe_warn_cpp0x ("extended initializer lists");
7595 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7597 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7598 expr = cp_parser_expression (parser, /*cast_p=*/false);
7600 /* If the next token is a `;', then there is no
7603 /* Build the return-statement. */
7604 statement = finish_return_stmt (expr);
7605 /* Look for the final `;'. */
7606 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7611 /* Create the goto-statement. */
7612 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7614 /* Issue a warning about this use of a GNU extension. */
7615 pedwarn (OPT_pedantic, "%HISO C++ forbids computed gotos", &token->location);
7616 /* Consume the '*' token. */
7617 cp_lexer_consume_token (parser->lexer);
7618 /* Parse the dependent expression. */
7619 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7622 finish_goto_stmt (cp_parser_identifier (parser));
7623 /* Look for the final `;'. */
7624 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7628 cp_parser_error (parser, "expected jump-statement");
7635 /* Parse a declaration-statement.
7637 declaration-statement:
7638 block-declaration */
7641 cp_parser_declaration_statement (cp_parser* parser)
7645 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7646 p = obstack_alloc (&declarator_obstack, 0);
7648 /* Parse the block-declaration. */
7649 cp_parser_block_declaration (parser, /*statement_p=*/true);
7651 /* Free any declarators allocated. */
7652 obstack_free (&declarator_obstack, p);
7654 /* Finish off the statement. */
7658 /* Some dependent statements (like `if (cond) statement'), are
7659 implicitly in their own scope. In other words, if the statement is
7660 a single statement (as opposed to a compound-statement), it is
7661 none-the-less treated as if it were enclosed in braces. Any
7662 declarations appearing in the dependent statement are out of scope
7663 after control passes that point. This function parses a statement,
7664 but ensures that is in its own scope, even if it is not a
7667 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7668 is a (possibly labeled) if statement which is not enclosed in
7669 braces and has an else clause. This is used to implement
7672 Returns the new statement. */
7675 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7682 /* Mark if () ; with a special NOP_EXPR. */
7683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7685 cp_lexer_consume_token (parser->lexer);
7686 statement = add_stmt (build_empty_stmt ());
7688 /* if a compound is opened, we simply parse the statement directly. */
7689 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7690 statement = cp_parser_compound_statement (parser, NULL, false);
7691 /* If the token is not a `{', then we must take special action. */
7694 /* Create a compound-statement. */
7695 statement = begin_compound_stmt (0);
7696 /* Parse the dependent-statement. */
7697 cp_parser_statement (parser, NULL_TREE, false, if_p);
7698 /* Finish the dummy compound-statement. */
7699 finish_compound_stmt (statement);
7702 /* Return the statement. */
7706 /* For some dependent statements (like `while (cond) statement'), we
7707 have already created a scope. Therefore, even if the dependent
7708 statement is a compound-statement, we do not want to create another
7712 cp_parser_already_scoped_statement (cp_parser* parser)
7714 /* If the token is a `{', then we must take special action. */
7715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7716 cp_parser_statement (parser, NULL_TREE, false, NULL);
7719 /* Avoid calling cp_parser_compound_statement, so that we
7720 don't create a new scope. Do everything else by hand. */
7721 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7722 cp_parser_statement_seq_opt (parser, NULL_TREE);
7723 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7727 /* Declarations [gram.dcl.dcl] */
7729 /* Parse an optional declaration-sequence.
7733 declaration-seq declaration */
7736 cp_parser_declaration_seq_opt (cp_parser* parser)
7742 token = cp_lexer_peek_token (parser->lexer);
7744 if (token->type == CPP_CLOSE_BRACE
7745 || token->type == CPP_EOF
7746 || token->type == CPP_PRAGMA_EOL)
7749 if (token->type == CPP_SEMICOLON)
7751 /* A declaration consisting of a single semicolon is
7752 invalid. Allow it unless we're being pedantic. */
7753 cp_lexer_consume_token (parser->lexer);
7754 if (!in_system_header)
7755 pedwarn (OPT_pedantic, "extra %<;%>");
7759 /* If we're entering or exiting a region that's implicitly
7760 extern "C", modify the lang context appropriately. */
7761 if (!parser->implicit_extern_c && token->implicit_extern_c)
7763 push_lang_context (lang_name_c);
7764 parser->implicit_extern_c = true;
7766 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7768 pop_lang_context ();
7769 parser->implicit_extern_c = false;
7772 if (token->type == CPP_PRAGMA)
7774 /* A top-level declaration can consist solely of a #pragma.
7775 A nested declaration cannot, so this is done here and not
7776 in cp_parser_declaration. (A #pragma at block scope is
7777 handled in cp_parser_statement.) */
7778 cp_parser_pragma (parser, pragma_external);
7782 /* Parse the declaration itself. */
7783 cp_parser_declaration (parser);
7787 /* Parse a declaration.
7792 template-declaration
7793 explicit-instantiation
7794 explicit-specialization
7795 linkage-specification
7796 namespace-definition
7801 __extension__ declaration */
7804 cp_parser_declaration (cp_parser* parser)
7811 /* Check for the `__extension__' keyword. */
7812 if (cp_parser_extension_opt (parser, &saved_pedantic))
7814 /* Parse the qualified declaration. */
7815 cp_parser_declaration (parser);
7816 /* Restore the PEDANTIC flag. */
7817 pedantic = saved_pedantic;
7822 /* Try to figure out what kind of declaration is present. */
7823 token1 = *cp_lexer_peek_token (parser->lexer);
7825 if (token1.type != CPP_EOF)
7826 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7829 token2.type = CPP_EOF;
7830 token2.keyword = RID_MAX;
7833 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7834 p = obstack_alloc (&declarator_obstack, 0);
7836 /* If the next token is `extern' and the following token is a string
7837 literal, then we have a linkage specification. */
7838 if (token1.keyword == RID_EXTERN
7839 && cp_parser_is_string_literal (&token2))
7840 cp_parser_linkage_specification (parser);
7841 /* If the next token is `template', then we have either a template
7842 declaration, an explicit instantiation, or an explicit
7844 else if (token1.keyword == RID_TEMPLATE)
7846 /* `template <>' indicates a template specialization. */
7847 if (token2.type == CPP_LESS
7848 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7849 cp_parser_explicit_specialization (parser);
7850 /* `template <' indicates a template declaration. */
7851 else if (token2.type == CPP_LESS)
7852 cp_parser_template_declaration (parser, /*member_p=*/false);
7853 /* Anything else must be an explicit instantiation. */
7855 cp_parser_explicit_instantiation (parser);
7857 /* If the next token is `export', then we have a template
7859 else if (token1.keyword == RID_EXPORT)
7860 cp_parser_template_declaration (parser, /*member_p=*/false);
7861 /* If the next token is `extern', 'static' or 'inline' and the one
7862 after that is `template', we have a GNU extended explicit
7863 instantiation directive. */
7864 else if (cp_parser_allow_gnu_extensions_p (parser)
7865 && (token1.keyword == RID_EXTERN
7866 || token1.keyword == RID_STATIC
7867 || token1.keyword == RID_INLINE)
7868 && token2.keyword == RID_TEMPLATE)
7869 cp_parser_explicit_instantiation (parser);
7870 /* If the next token is `namespace', check for a named or unnamed
7871 namespace definition. */
7872 else if (token1.keyword == RID_NAMESPACE
7873 && (/* A named namespace definition. */
7874 (token2.type == CPP_NAME
7875 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7877 /* An unnamed namespace definition. */
7878 || token2.type == CPP_OPEN_BRACE
7879 || token2.keyword == RID_ATTRIBUTE))
7880 cp_parser_namespace_definition (parser);
7881 /* An inline (associated) namespace definition. */
7882 else if (token1.keyword == RID_INLINE
7883 && token2.keyword == RID_NAMESPACE)
7884 cp_parser_namespace_definition (parser);
7885 /* Objective-C++ declaration/definition. */
7886 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7887 cp_parser_objc_declaration (parser);
7888 /* We must have either a block declaration or a function
7891 /* Try to parse a block-declaration, or a function-definition. */
7892 cp_parser_block_declaration (parser, /*statement_p=*/false);
7894 /* Free any declarators allocated. */
7895 obstack_free (&declarator_obstack, p);
7898 /* Parse a block-declaration.
7903 namespace-alias-definition
7910 __extension__ block-declaration
7915 static_assert-declaration
7917 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7918 part of a declaration-statement. */
7921 cp_parser_block_declaration (cp_parser *parser,
7927 /* Check for the `__extension__' keyword. */
7928 if (cp_parser_extension_opt (parser, &saved_pedantic))
7930 /* Parse the qualified declaration. */
7931 cp_parser_block_declaration (parser, statement_p);
7932 /* Restore the PEDANTIC flag. */
7933 pedantic = saved_pedantic;
7938 /* Peek at the next token to figure out which kind of declaration is
7940 token1 = cp_lexer_peek_token (parser->lexer);
7942 /* If the next keyword is `asm', we have an asm-definition. */
7943 if (token1->keyword == RID_ASM)
7946 cp_parser_commit_to_tentative_parse (parser);
7947 cp_parser_asm_definition (parser);
7949 /* If the next keyword is `namespace', we have a
7950 namespace-alias-definition. */
7951 else if (token1->keyword == RID_NAMESPACE)
7952 cp_parser_namespace_alias_definition (parser);
7953 /* If the next keyword is `using', we have either a
7954 using-declaration or a using-directive. */
7955 else if (token1->keyword == RID_USING)
7960 cp_parser_commit_to_tentative_parse (parser);
7961 /* If the token after `using' is `namespace', then we have a
7963 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7964 if (token2->keyword == RID_NAMESPACE)
7965 cp_parser_using_directive (parser);
7966 /* Otherwise, it's a using-declaration. */
7968 cp_parser_using_declaration (parser,
7969 /*access_declaration_p=*/false);
7971 /* If the next keyword is `__label__' we have a misplaced label
7973 else if (token1->keyword == RID_LABEL)
7975 cp_lexer_consume_token (parser->lexer);
7976 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7977 cp_parser_skip_to_end_of_statement (parser);
7978 /* If the next token is now a `;', consume it. */
7979 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7980 cp_lexer_consume_token (parser->lexer);
7982 /* If the next token is `static_assert' we have a static assertion. */
7983 else if (token1->keyword == RID_STATIC_ASSERT)
7984 cp_parser_static_assert (parser, /*member_p=*/false);
7985 /* Anything else must be a simple-declaration. */
7987 cp_parser_simple_declaration (parser, !statement_p);
7990 /* Parse a simple-declaration.
7993 decl-specifier-seq [opt] init-declarator-list [opt] ;
7995 init-declarator-list:
7997 init-declarator-list , init-declarator
7999 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8000 function-definition as a simple-declaration. */
8003 cp_parser_simple_declaration (cp_parser* parser,
8004 bool function_definition_allowed_p)
8006 cp_decl_specifier_seq decl_specifiers;
8007 int declares_class_or_enum;
8008 bool saw_declarator;
8010 /* Defer access checks until we know what is being declared; the
8011 checks for names appearing in the decl-specifier-seq should be
8012 done as if we were in the scope of the thing being declared. */
8013 push_deferring_access_checks (dk_deferred);
8015 /* Parse the decl-specifier-seq. We have to keep track of whether
8016 or not the decl-specifier-seq declares a named class or
8017 enumeration type, since that is the only case in which the
8018 init-declarator-list is allowed to be empty.
8022 In a simple-declaration, the optional init-declarator-list can be
8023 omitted only when declaring a class or enumeration, that is when
8024 the decl-specifier-seq contains either a class-specifier, an
8025 elaborated-type-specifier, or an enum-specifier. */
8026 cp_parser_decl_specifier_seq (parser,
8027 CP_PARSER_FLAGS_OPTIONAL,
8029 &declares_class_or_enum);
8030 /* We no longer need to defer access checks. */
8031 stop_deferring_access_checks ();
8033 /* In a block scope, a valid declaration must always have a
8034 decl-specifier-seq. By not trying to parse declarators, we can
8035 resolve the declaration/expression ambiguity more quickly. */
8036 if (!function_definition_allowed_p
8037 && !decl_specifiers.any_specifiers_p)
8039 cp_parser_error (parser, "expected declaration");
8043 /* If the next two tokens are both identifiers, the code is
8044 erroneous. The usual cause of this situation is code like:
8048 where "T" should name a type -- but does not. */
8049 if (!decl_specifiers.type
8050 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8052 /* If parsing tentatively, we should commit; we really are
8053 looking at a declaration. */
8054 cp_parser_commit_to_tentative_parse (parser);
8059 /* If we have seen at least one decl-specifier, and the next token
8060 is not a parenthesis, then we must be looking at a declaration.
8061 (After "int (" we might be looking at a functional cast.) */
8062 if (decl_specifiers.any_specifiers_p
8063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8064 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8065 cp_parser_commit_to_tentative_parse (parser);
8067 /* Keep going until we hit the `;' at the end of the simple
8069 saw_declarator = false;
8070 while (cp_lexer_next_token_is_not (parser->lexer,
8074 bool function_definition_p;
8079 /* If we are processing next declarator, coma is expected */
8080 token = cp_lexer_peek_token (parser->lexer);
8081 gcc_assert (token->type == CPP_COMMA);
8082 cp_lexer_consume_token (parser->lexer);
8085 saw_declarator = true;
8087 /* Parse the init-declarator. */
8088 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8090 function_definition_allowed_p,
8092 declares_class_or_enum,
8093 &function_definition_p);
8094 /* If an error occurred while parsing tentatively, exit quickly.
8095 (That usually happens when in the body of a function; each
8096 statement is treated as a declaration-statement until proven
8098 if (cp_parser_error_occurred (parser))
8100 /* Handle function definitions specially. */
8101 if (function_definition_p)
8103 /* If the next token is a `,', then we are probably
8104 processing something like:
8108 which is erroneous. */
8109 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8111 cp_token *token = cp_lexer_peek_token (parser->lexer);
8112 error ("%Hmixing declarations and function-definitions is forbidden",
8115 /* Otherwise, we're done with the list of declarators. */
8118 pop_deferring_access_checks ();
8122 /* The next token should be either a `,' or a `;'. */
8123 token = cp_lexer_peek_token (parser->lexer);
8124 /* If it's a `,', there are more declarators to come. */
8125 if (token->type == CPP_COMMA)
8126 /* will be consumed next time around */;
8127 /* If it's a `;', we are done. */
8128 else if (token->type == CPP_SEMICOLON)
8130 /* Anything else is an error. */
8133 /* If we have already issued an error message we don't need
8134 to issue another one. */
8135 if (decl != error_mark_node
8136 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8137 cp_parser_error (parser, "expected %<,%> or %<;%>");
8138 /* Skip tokens until we reach the end of the statement. */
8139 cp_parser_skip_to_end_of_statement (parser);
8140 /* If the next token is now a `;', consume it. */
8141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8142 cp_lexer_consume_token (parser->lexer);
8145 /* After the first time around, a function-definition is not
8146 allowed -- even if it was OK at first. For example:
8151 function_definition_allowed_p = false;
8154 /* Issue an error message if no declarators are present, and the
8155 decl-specifier-seq does not itself declare a class or
8157 if (!saw_declarator)
8159 if (cp_parser_declares_only_class_p (parser))
8160 shadow_tag (&decl_specifiers);
8161 /* Perform any deferred access checks. */
8162 perform_deferred_access_checks ();
8165 /* Consume the `;'. */
8166 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8169 pop_deferring_access_checks ();
8172 /* Parse a decl-specifier-seq.
8175 decl-specifier-seq [opt] decl-specifier
8178 storage-class-specifier
8189 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8191 The parser flags FLAGS is used to control type-specifier parsing.
8193 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8196 1: one of the decl-specifiers is an elaborated-type-specifier
8197 (i.e., a type declaration)
8198 2: one of the decl-specifiers is an enum-specifier or a
8199 class-specifier (i.e., a type definition)
8204 cp_parser_decl_specifier_seq (cp_parser* parser,
8205 cp_parser_flags flags,
8206 cp_decl_specifier_seq *decl_specs,
8207 int* declares_class_or_enum)
8209 bool constructor_possible_p = !parser->in_declarator_p;
8210 cp_token *start_token = NULL;
8212 /* Clear DECL_SPECS. */
8213 clear_decl_specs (decl_specs);
8215 /* Assume no class or enumeration type is declared. */
8216 *declares_class_or_enum = 0;
8218 /* Keep reading specifiers until there are no more to read. */
8222 bool found_decl_spec;
8225 /* Peek at the next token. */
8226 token = cp_lexer_peek_token (parser->lexer);
8228 /* Save the first token of the decl spec list for error
8231 start_token = token;
8232 /* Handle attributes. */
8233 if (token->keyword == RID_ATTRIBUTE)
8235 /* Parse the attributes. */
8236 decl_specs->attributes
8237 = chainon (decl_specs->attributes,
8238 cp_parser_attributes_opt (parser));
8241 /* Assume we will find a decl-specifier keyword. */
8242 found_decl_spec = true;
8243 /* If the next token is an appropriate keyword, we can simply
8244 add it to the list. */
8245 switch (token->keyword)
8250 if (!at_class_scope_p ())
8252 error ("%H%<friend%> used outside of class", &token->location);
8253 cp_lexer_purge_token (parser->lexer);
8257 ++decl_specs->specs[(int) ds_friend];
8258 /* Consume the token. */
8259 cp_lexer_consume_token (parser->lexer);
8263 /* function-specifier:
8270 cp_parser_function_specifier_opt (parser, decl_specs);
8276 ++decl_specs->specs[(int) ds_typedef];
8277 /* Consume the token. */
8278 cp_lexer_consume_token (parser->lexer);
8279 /* A constructor declarator cannot appear in a typedef. */
8280 constructor_possible_p = false;
8281 /* The "typedef" keyword can only occur in a declaration; we
8282 may as well commit at this point. */
8283 cp_parser_commit_to_tentative_parse (parser);
8285 if (decl_specs->storage_class != sc_none)
8286 decl_specs->conflicting_specifiers_p = true;
8289 /* storage-class-specifier:
8299 /* Consume the token. */
8300 cp_lexer_consume_token (parser->lexer);
8302 if (cxx_dialect == cxx98)
8304 /* Complain about `auto' as a storage specifier, if
8305 we're complaining about C++0x compatibility. */
8308 "%H%<auto%> will change meaning in C++0x; please remove it",
8311 /* Set the storage class anyway. */
8312 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8316 /* We do not yet support the use of `auto' as a
8318 error ("%HC++0x %<auto%> specifier not supported", &token->location);
8325 /* Consume the token. */
8326 cp_lexer_consume_token (parser->lexer);
8327 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8331 /* Consume the token. */
8332 cp_lexer_consume_token (parser->lexer);
8333 ++decl_specs->specs[(int) ds_thread];
8337 /* We did not yet find a decl-specifier yet. */
8338 found_decl_spec = false;
8342 /* Constructors are a special case. The `S' in `S()' is not a
8343 decl-specifier; it is the beginning of the declarator. */
8346 && constructor_possible_p
8347 && (cp_parser_constructor_declarator_p
8348 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8350 /* If we don't have a DECL_SPEC yet, then we must be looking at
8351 a type-specifier. */
8352 if (!found_decl_spec && !constructor_p)
8354 int decl_spec_declares_class_or_enum;
8355 bool is_cv_qualifier;
8359 = cp_parser_type_specifier (parser, flags,
8361 /*is_declaration=*/true,
8362 &decl_spec_declares_class_or_enum,
8364 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8366 /* If this type-specifier referenced a user-defined type
8367 (a typedef, class-name, etc.), then we can't allow any
8368 more such type-specifiers henceforth.
8372 The longest sequence of decl-specifiers that could
8373 possibly be a type name is taken as the
8374 decl-specifier-seq of a declaration. The sequence shall
8375 be self-consistent as described below.
8379 As a general rule, at most one type-specifier is allowed
8380 in the complete decl-specifier-seq of a declaration. The
8381 only exceptions are the following:
8383 -- const or volatile can be combined with any other
8386 -- signed or unsigned can be combined with char, long,
8394 void g (const int Pc);
8396 Here, Pc is *not* part of the decl-specifier seq; it's
8397 the declarator. Therefore, once we see a type-specifier
8398 (other than a cv-qualifier), we forbid any additional
8399 user-defined types. We *do* still allow things like `int
8400 int' to be considered a decl-specifier-seq, and issue the
8401 error message later. */
8402 if (type_spec && !is_cv_qualifier)
8403 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8404 /* A constructor declarator cannot follow a type-specifier. */
8407 constructor_possible_p = false;
8408 found_decl_spec = true;
8412 /* If we still do not have a DECL_SPEC, then there are no more
8414 if (!found_decl_spec)
8417 decl_specs->any_specifiers_p = true;
8418 /* After we see one decl-specifier, further decl-specifiers are
8420 flags |= CP_PARSER_FLAGS_OPTIONAL;
8423 cp_parser_check_decl_spec (decl_specs, start_token->location);
8425 /* Don't allow a friend specifier with a class definition. */
8426 if (decl_specs->specs[(int) ds_friend] != 0
8427 && (*declares_class_or_enum & 2))
8428 error ("%Hclass definition may not be declared a friend",
8429 &start_token->location);
8432 /* Parse an (optional) storage-class-specifier.
8434 storage-class-specifier:
8443 storage-class-specifier:
8446 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8449 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8451 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8454 if (cxx_dialect != cxx98)
8456 /* Fall through for C++98. */
8463 /* Consume the token. */
8464 return cp_lexer_consume_token (parser->lexer)->u.value;
8471 /* Parse an (optional) function-specifier.
8478 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8479 Updates DECL_SPECS, if it is non-NULL. */
8482 cp_parser_function_specifier_opt (cp_parser* parser,
8483 cp_decl_specifier_seq *decl_specs)
8485 cp_token *token = cp_lexer_peek_token (parser->lexer);
8486 switch (token->keyword)
8490 ++decl_specs->specs[(int) ds_inline];
8494 /* 14.5.2.3 [temp.mem]
8496 A member function template shall not be virtual. */
8497 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8498 error ("%Htemplates may not be %<virtual%>", &token->location);
8499 else if (decl_specs)
8500 ++decl_specs->specs[(int) ds_virtual];
8505 ++decl_specs->specs[(int) ds_explicit];
8512 /* Consume the token. */
8513 return cp_lexer_consume_token (parser->lexer)->u.value;
8516 /* Parse a linkage-specification.
8518 linkage-specification:
8519 extern string-literal { declaration-seq [opt] }
8520 extern string-literal declaration */
8523 cp_parser_linkage_specification (cp_parser* parser)
8527 /* Look for the `extern' keyword. */
8528 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8530 /* Look for the string-literal. */
8531 linkage = cp_parser_string_literal (parser, false, false);
8533 /* Transform the literal into an identifier. If the literal is a
8534 wide-character string, or contains embedded NULs, then we can't
8535 handle it as the user wants. */
8536 if (strlen (TREE_STRING_POINTER (linkage))
8537 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8539 cp_parser_error (parser, "invalid linkage-specification");
8540 /* Assume C++ linkage. */
8541 linkage = lang_name_cplusplus;
8544 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8546 /* We're now using the new linkage. */
8547 push_lang_context (linkage);
8549 /* If the next token is a `{', then we're using the first
8551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8553 /* Consume the `{' token. */
8554 cp_lexer_consume_token (parser->lexer);
8555 /* Parse the declarations. */
8556 cp_parser_declaration_seq_opt (parser);
8557 /* Look for the closing `}'. */
8558 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8560 /* Otherwise, there's just one declaration. */
8563 bool saved_in_unbraced_linkage_specification_p;
8565 saved_in_unbraced_linkage_specification_p
8566 = parser->in_unbraced_linkage_specification_p;
8567 parser->in_unbraced_linkage_specification_p = true;
8568 cp_parser_declaration (parser);
8569 parser->in_unbraced_linkage_specification_p
8570 = saved_in_unbraced_linkage_specification_p;
8573 /* We're done with the linkage-specification. */
8574 pop_lang_context ();
8577 /* Parse a static_assert-declaration.
8579 static_assert-declaration:
8580 static_assert ( constant-expression , string-literal ) ;
8582 If MEMBER_P, this static_assert is a class member. */
8585 cp_parser_static_assert(cp_parser *parser, bool member_p)
8590 location_t saved_loc;
8592 /* Peek at the `static_assert' token so we can keep track of exactly
8593 where the static assertion started. */
8594 token = cp_lexer_peek_token (parser->lexer);
8595 saved_loc = token->location;
8597 /* Look for the `static_assert' keyword. */
8598 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8599 "%<static_assert%>"))
8602 /* We know we are in a static assertion; commit to any tentative
8604 if (cp_parser_parsing_tentatively (parser))
8605 cp_parser_commit_to_tentative_parse (parser);
8607 /* Parse the `(' starting the static assertion condition. */
8608 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8610 /* Parse the constant-expression. */
8612 cp_parser_constant_expression (parser,
8613 /*allow_non_constant_p=*/false,
8614 /*non_constant_p=*/NULL);
8616 /* Parse the separating `,'. */
8617 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8619 /* Parse the string-literal message. */
8620 message = cp_parser_string_literal (parser,
8621 /*translate=*/false,
8624 /* A `)' completes the static assertion. */
8625 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8626 cp_parser_skip_to_closing_parenthesis (parser,
8627 /*recovering=*/true,
8629 /*consume_paren=*/true);
8631 /* A semicolon terminates the declaration. */
8632 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8634 /* Complete the static assertion, which may mean either processing
8635 the static assert now or saving it for template instantiation. */
8636 finish_static_assert (condition, message, saved_loc, member_p);
8639 /* Parse a `decltype' type. Returns the type.
8641 simple-type-specifier:
8642 decltype ( expression ) */
8645 cp_parser_decltype (cp_parser *parser)
8648 bool id_expression_or_member_access_p = false;
8649 const char *saved_message;
8650 bool saved_integral_constant_expression_p;
8651 bool saved_non_integral_constant_expression_p;
8652 cp_token *id_expr_start_token;
8654 /* Look for the `decltype' token. */
8655 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8656 return error_mark_node;
8658 /* Types cannot be defined in a `decltype' expression. Save away the
8660 saved_message = parser->type_definition_forbidden_message;
8662 /* And create the new one. */
8663 parser->type_definition_forbidden_message
8664 = "types may not be defined in %<decltype%> expressions";
8666 /* The restrictions on constant-expressions do not apply inside
8667 decltype expressions. */
8668 saved_integral_constant_expression_p
8669 = parser->integral_constant_expression_p;
8670 saved_non_integral_constant_expression_p
8671 = parser->non_integral_constant_expression_p;
8672 parser->integral_constant_expression_p = false;
8674 /* Do not actually evaluate the expression. */
8677 /* Parse the opening `('. */
8678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8679 return error_mark_node;
8681 /* First, try parsing an id-expression. */
8682 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8683 cp_parser_parse_tentatively (parser);
8684 expr = cp_parser_id_expression (parser,
8685 /*template_keyword_p=*/false,
8686 /*check_dependency_p=*/true,
8687 /*template_p=*/NULL,
8688 /*declarator_p=*/false,
8689 /*optional_p=*/false);
8691 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8693 bool non_integral_constant_expression_p = false;
8694 tree id_expression = expr;
8696 const char *error_msg;
8698 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8699 /* Lookup the name we got back from the id-expression. */
8700 expr = cp_parser_lookup_name (parser, expr,
8702 /*is_template=*/false,
8703 /*is_namespace=*/false,
8704 /*check_dependency=*/true,
8705 /*ambiguous_decls=*/NULL,
8706 id_expr_start_token->location);
8709 && expr != error_mark_node
8710 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8711 && TREE_CODE (expr) != TYPE_DECL
8712 && (TREE_CODE (expr) != BIT_NOT_EXPR
8713 || !TYPE_P (TREE_OPERAND (expr, 0)))
8714 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8716 /* Complete lookup of the id-expression. */
8717 expr = (finish_id_expression
8718 (id_expression, expr, parser->scope, &idk,
8719 /*integral_constant_expression_p=*/false,
8720 /*allow_non_integral_constant_expression_p=*/true,
8721 &non_integral_constant_expression_p,
8722 /*template_p=*/false,
8724 /*address_p=*/false,
8725 /*template_arg_p=*/false,
8727 id_expr_start_token->location));
8729 if (expr == error_mark_node)
8730 /* We found an id-expression, but it was something that we
8731 should not have found. This is an error, not something
8732 we can recover from, so note that we found an
8733 id-expression and we'll recover as gracefully as
8735 id_expression_or_member_access_p = true;
8739 && expr != error_mark_node
8740 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8741 /* We have an id-expression. */
8742 id_expression_or_member_access_p = true;
8745 if (!id_expression_or_member_access_p)
8747 /* Abort the id-expression parse. */
8748 cp_parser_abort_tentative_parse (parser);
8750 /* Parsing tentatively, again. */
8751 cp_parser_parse_tentatively (parser);
8753 /* Parse a class member access. */
8754 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8756 /*member_access_only_p=*/true);
8759 && expr != error_mark_node
8760 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8761 /* We have an id-expression. */
8762 id_expression_or_member_access_p = true;
8765 if (id_expression_or_member_access_p)
8766 /* We have parsed the complete id-expression or member access. */
8767 cp_parser_parse_definitely (parser);
8770 /* Abort our attempt to parse an id-expression or member access
8772 cp_parser_abort_tentative_parse (parser);
8774 /* Parse a full expression. */
8775 expr = cp_parser_expression (parser, /*cast_p=*/false);
8778 /* Go back to evaluating expressions. */
8781 /* Restore the old message and the integral constant expression
8783 parser->type_definition_forbidden_message = saved_message;
8784 parser->integral_constant_expression_p
8785 = saved_integral_constant_expression_p;
8786 parser->non_integral_constant_expression_p
8787 = saved_non_integral_constant_expression_p;
8789 if (expr == error_mark_node)
8791 /* Skip everything up to the closing `)'. */
8792 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8793 /*consume_paren=*/true);
8794 return error_mark_node;
8797 /* Parse to the closing `)'. */
8798 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8800 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8801 /*consume_paren=*/true);
8802 return error_mark_node;
8805 return finish_decltype_type (expr, id_expression_or_member_access_p);
8808 /* Special member functions [gram.special] */
8810 /* Parse a conversion-function-id.
8812 conversion-function-id:
8813 operator conversion-type-id
8815 Returns an IDENTIFIER_NODE representing the operator. */
8818 cp_parser_conversion_function_id (cp_parser* parser)
8822 tree saved_qualifying_scope;
8823 tree saved_object_scope;
8824 tree pushed_scope = NULL_TREE;
8826 /* Look for the `operator' token. */
8827 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8828 return error_mark_node;
8829 /* When we parse the conversion-type-id, the current scope will be
8830 reset. However, we need that information in able to look up the
8831 conversion function later, so we save it here. */
8832 saved_scope = parser->scope;
8833 saved_qualifying_scope = parser->qualifying_scope;
8834 saved_object_scope = parser->object_scope;
8835 /* We must enter the scope of the class so that the names of
8836 entities declared within the class are available in the
8837 conversion-type-id. For example, consider:
8844 S::operator I() { ... }
8846 In order to see that `I' is a type-name in the definition, we
8847 must be in the scope of `S'. */
8849 pushed_scope = push_scope (saved_scope);
8850 /* Parse the conversion-type-id. */
8851 type = cp_parser_conversion_type_id (parser);
8852 /* Leave the scope of the class, if any. */
8854 pop_scope (pushed_scope);
8855 /* Restore the saved scope. */
8856 parser->scope = saved_scope;
8857 parser->qualifying_scope = saved_qualifying_scope;
8858 parser->object_scope = saved_object_scope;
8859 /* If the TYPE is invalid, indicate failure. */
8860 if (type == error_mark_node)
8861 return error_mark_node;
8862 return mangle_conv_op_name_for_type (type);
8865 /* Parse a conversion-type-id:
8868 type-specifier-seq conversion-declarator [opt]
8870 Returns the TYPE specified. */
8873 cp_parser_conversion_type_id (cp_parser* parser)
8876 cp_decl_specifier_seq type_specifiers;
8877 cp_declarator *declarator;
8878 tree type_specified;
8880 /* Parse the attributes. */
8881 attributes = cp_parser_attributes_opt (parser);
8882 /* Parse the type-specifiers. */
8883 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8885 /* If that didn't work, stop. */
8886 if (type_specifiers.type == error_mark_node)
8887 return error_mark_node;
8888 /* Parse the conversion-declarator. */
8889 declarator = cp_parser_conversion_declarator_opt (parser);
8891 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8892 /*initialized=*/0, &attributes);
8894 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8895 return type_specified;
8898 /* Parse an (optional) conversion-declarator.
8900 conversion-declarator:
8901 ptr-operator conversion-declarator [opt]
8905 static cp_declarator *
8906 cp_parser_conversion_declarator_opt (cp_parser* parser)
8908 enum tree_code code;
8910 cp_cv_quals cv_quals;
8912 /* We don't know if there's a ptr-operator next, or not. */
8913 cp_parser_parse_tentatively (parser);
8914 /* Try the ptr-operator. */
8915 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8916 /* If it worked, look for more conversion-declarators. */
8917 if (cp_parser_parse_definitely (parser))
8919 cp_declarator *declarator;
8921 /* Parse another optional declarator. */
8922 declarator = cp_parser_conversion_declarator_opt (parser);
8924 return cp_parser_make_indirect_declarator
8925 (code, class_type, cv_quals, declarator);
8931 /* Parse an (optional) ctor-initializer.
8934 : mem-initializer-list
8936 Returns TRUE iff the ctor-initializer was actually present. */
8939 cp_parser_ctor_initializer_opt (cp_parser* parser)
8941 /* If the next token is not a `:', then there is no
8942 ctor-initializer. */
8943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8945 /* Do default initialization of any bases and members. */
8946 if (DECL_CONSTRUCTOR_P (current_function_decl))
8947 finish_mem_initializers (NULL_TREE);
8952 /* Consume the `:' token. */
8953 cp_lexer_consume_token (parser->lexer);
8954 /* And the mem-initializer-list. */
8955 cp_parser_mem_initializer_list (parser);
8960 /* Parse a mem-initializer-list.
8962 mem-initializer-list:
8963 mem-initializer ... [opt]
8964 mem-initializer ... [opt] , mem-initializer-list */
8967 cp_parser_mem_initializer_list (cp_parser* parser)
8969 tree mem_initializer_list = NULL_TREE;
8970 cp_token *token = cp_lexer_peek_token (parser->lexer);
8972 /* Let the semantic analysis code know that we are starting the
8973 mem-initializer-list. */
8974 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8975 error ("%Honly constructors take base initializers",
8978 /* Loop through the list. */
8981 tree mem_initializer;
8983 token = cp_lexer_peek_token (parser->lexer);
8984 /* Parse the mem-initializer. */
8985 mem_initializer = cp_parser_mem_initializer (parser);
8986 /* If the next token is a `...', we're expanding member initializers. */
8987 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8989 /* Consume the `...'. */
8990 cp_lexer_consume_token (parser->lexer);
8992 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8993 can be expanded but members cannot. */
8994 if (mem_initializer != error_mark_node
8995 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8997 error ("%Hcannot expand initializer for member %<%D%>",
8998 &token->location, TREE_PURPOSE (mem_initializer));
8999 mem_initializer = error_mark_node;
9002 /* Construct the pack expansion type. */
9003 if (mem_initializer != error_mark_node)
9004 mem_initializer = make_pack_expansion (mem_initializer);
9006 /* Add it to the list, unless it was erroneous. */
9007 if (mem_initializer != error_mark_node)
9009 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9010 mem_initializer_list = mem_initializer;
9012 /* If the next token is not a `,', we're done. */
9013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9015 /* Consume the `,' token. */
9016 cp_lexer_consume_token (parser->lexer);
9019 /* Perform semantic analysis. */
9020 if (DECL_CONSTRUCTOR_P (current_function_decl))
9021 finish_mem_initializers (mem_initializer_list);
9024 /* Parse a mem-initializer.
9027 mem-initializer-id ( expression-list [opt] )
9028 mem-initializer-id braced-init-list
9033 ( expression-list [opt] )
9035 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9036 class) or FIELD_DECL (for a non-static data member) to initialize;
9037 the TREE_VALUE is the expression-list. An empty initialization
9038 list is represented by void_list_node. */
9041 cp_parser_mem_initializer (cp_parser* parser)
9043 tree mem_initializer_id;
9044 tree expression_list;
9046 cp_token *token = cp_lexer_peek_token (parser->lexer);
9048 /* Find out what is being initialized. */
9049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9051 permerror (token->location,
9052 "anachronistic old-style base class initializer");
9053 mem_initializer_id = NULL_TREE;
9056 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9057 member = expand_member_init (mem_initializer_id);
9058 if (member && !DECL_P (member))
9059 in_base_initializer = 1;
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9063 bool expr_non_constant_p;
9064 maybe_warn_cpp0x ("extended initializer lists");
9065 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9066 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9067 expression_list = build_tree_list (NULL_TREE, expression_list);
9071 = cp_parser_parenthesized_expression_list (parser, false,
9073 /*allow_expansion_p=*/true,
9074 /*non_constant_p=*/NULL);
9075 if (expression_list == error_mark_node)
9076 return error_mark_node;
9077 if (!expression_list)
9078 expression_list = void_type_node;
9080 in_base_initializer = 0;
9082 return member ? build_tree_list (member, expression_list) : error_mark_node;
9085 /* Parse a mem-initializer-id.
9088 :: [opt] nested-name-specifier [opt] class-name
9091 Returns a TYPE indicating the class to be initializer for the first
9092 production. Returns an IDENTIFIER_NODE indicating the data member
9093 to be initialized for the second production. */
9096 cp_parser_mem_initializer_id (cp_parser* parser)
9098 bool global_scope_p;
9099 bool nested_name_specifier_p;
9100 bool template_p = false;
9103 cp_token *token = cp_lexer_peek_token (parser->lexer);
9105 /* `typename' is not allowed in this context ([temp.res]). */
9106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9108 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9109 "member initializer is implicitly a type)",
9111 cp_lexer_consume_token (parser->lexer);
9113 /* Look for the optional `::' operator. */
9115 = (cp_parser_global_scope_opt (parser,
9116 /*current_scope_valid_p=*/false)
9118 /* Look for the optional nested-name-specifier. The simplest way to
9123 The keyword `typename' is not permitted in a base-specifier or
9124 mem-initializer; in these contexts a qualified name that
9125 depends on a template-parameter is implicitly assumed to be a
9128 is to assume that we have seen the `typename' keyword at this
9130 nested_name_specifier_p
9131 = (cp_parser_nested_name_specifier_opt (parser,
9132 /*typename_keyword_p=*/true,
9133 /*check_dependency_p=*/true,
9135 /*is_declaration=*/true)
9137 if (nested_name_specifier_p)
9138 template_p = cp_parser_optional_template_keyword (parser);
9139 /* If there is a `::' operator or a nested-name-specifier, then we
9140 are definitely looking for a class-name. */
9141 if (global_scope_p || nested_name_specifier_p)
9142 return cp_parser_class_name (parser,
9143 /*typename_keyword_p=*/true,
9144 /*template_keyword_p=*/template_p,
9146 /*check_dependency_p=*/true,
9147 /*class_head_p=*/false,
9148 /*is_declaration=*/true);
9149 /* Otherwise, we could also be looking for an ordinary identifier. */
9150 cp_parser_parse_tentatively (parser);
9151 /* Try a class-name. */
9152 id = cp_parser_class_name (parser,
9153 /*typename_keyword_p=*/true,
9154 /*template_keyword_p=*/false,
9156 /*check_dependency_p=*/true,
9157 /*class_head_p=*/false,
9158 /*is_declaration=*/true);
9159 /* If we found one, we're done. */
9160 if (cp_parser_parse_definitely (parser))
9162 /* Otherwise, look for an ordinary identifier. */
9163 return cp_parser_identifier (parser);
9166 /* Overloading [gram.over] */
9168 /* Parse an operator-function-id.
9170 operator-function-id:
9173 Returns an IDENTIFIER_NODE for the operator which is a
9174 human-readable spelling of the identifier, e.g., `operator +'. */
9177 cp_parser_operator_function_id (cp_parser* parser)
9179 /* Look for the `operator' keyword. */
9180 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9181 return error_mark_node;
9182 /* And then the name of the operator itself. */
9183 return cp_parser_operator (parser);
9186 /* Parse an operator.
9189 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9190 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9191 || ++ -- , ->* -> () []
9198 Returns an IDENTIFIER_NODE for the operator which is a
9199 human-readable spelling of the identifier, e.g., `operator +'. */
9202 cp_parser_operator (cp_parser* parser)
9204 tree id = NULL_TREE;
9207 /* Peek at the next token. */
9208 token = cp_lexer_peek_token (parser->lexer);
9209 /* Figure out which operator we have. */
9210 switch (token->type)
9216 /* The keyword should be either `new' or `delete'. */
9217 if (token->keyword == RID_NEW)
9219 else if (token->keyword == RID_DELETE)
9224 /* Consume the `new' or `delete' token. */
9225 cp_lexer_consume_token (parser->lexer);
9227 /* Peek at the next token. */
9228 token = cp_lexer_peek_token (parser->lexer);
9229 /* If it's a `[' token then this is the array variant of the
9231 if (token->type == CPP_OPEN_SQUARE)
9233 /* Consume the `[' token. */
9234 cp_lexer_consume_token (parser->lexer);
9235 /* Look for the `]' token. */
9236 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9237 id = ansi_opname (op == NEW_EXPR
9238 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9240 /* Otherwise, we have the non-array variant. */
9242 id = ansi_opname (op);
9248 id = ansi_opname (PLUS_EXPR);
9252 id = ansi_opname (MINUS_EXPR);
9256 id = ansi_opname (MULT_EXPR);
9260 id = ansi_opname (TRUNC_DIV_EXPR);
9264 id = ansi_opname (TRUNC_MOD_EXPR);
9268 id = ansi_opname (BIT_XOR_EXPR);
9272 id = ansi_opname (BIT_AND_EXPR);
9276 id = ansi_opname (BIT_IOR_EXPR);
9280 id = ansi_opname (BIT_NOT_EXPR);
9284 id = ansi_opname (TRUTH_NOT_EXPR);
9288 id = ansi_assopname (NOP_EXPR);
9292 id = ansi_opname (LT_EXPR);
9296 id = ansi_opname (GT_EXPR);
9300 id = ansi_assopname (PLUS_EXPR);
9304 id = ansi_assopname (MINUS_EXPR);
9308 id = ansi_assopname (MULT_EXPR);
9312 id = ansi_assopname (TRUNC_DIV_EXPR);
9316 id = ansi_assopname (TRUNC_MOD_EXPR);
9320 id = ansi_assopname (BIT_XOR_EXPR);
9324 id = ansi_assopname (BIT_AND_EXPR);
9328 id = ansi_assopname (BIT_IOR_EXPR);
9332 id = ansi_opname (LSHIFT_EXPR);
9336 id = ansi_opname (RSHIFT_EXPR);
9340 id = ansi_assopname (LSHIFT_EXPR);
9344 id = ansi_assopname (RSHIFT_EXPR);
9348 id = ansi_opname (EQ_EXPR);
9352 id = ansi_opname (NE_EXPR);
9356 id = ansi_opname (LE_EXPR);
9359 case CPP_GREATER_EQ:
9360 id = ansi_opname (GE_EXPR);
9364 id = ansi_opname (TRUTH_ANDIF_EXPR);
9368 id = ansi_opname (TRUTH_ORIF_EXPR);
9372 id = ansi_opname (POSTINCREMENT_EXPR);
9375 case CPP_MINUS_MINUS:
9376 id = ansi_opname (PREDECREMENT_EXPR);
9380 id = ansi_opname (COMPOUND_EXPR);
9383 case CPP_DEREF_STAR:
9384 id = ansi_opname (MEMBER_REF);
9388 id = ansi_opname (COMPONENT_REF);
9391 case CPP_OPEN_PAREN:
9392 /* Consume the `('. */
9393 cp_lexer_consume_token (parser->lexer);
9394 /* Look for the matching `)'. */
9395 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9396 return ansi_opname (CALL_EXPR);
9398 case CPP_OPEN_SQUARE:
9399 /* Consume the `['. */
9400 cp_lexer_consume_token (parser->lexer);
9401 /* Look for the matching `]'. */
9402 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9403 return ansi_opname (ARRAY_REF);
9406 /* Anything else is an error. */
9410 /* If we have selected an identifier, we need to consume the
9413 cp_lexer_consume_token (parser->lexer);
9414 /* Otherwise, no valid operator name was present. */
9417 cp_parser_error (parser, "expected operator");
9418 id = error_mark_node;
9424 /* Parse a template-declaration.
9426 template-declaration:
9427 export [opt] template < template-parameter-list > declaration
9429 If MEMBER_P is TRUE, this template-declaration occurs within a
9432 The grammar rule given by the standard isn't correct. What
9435 template-declaration:
9436 export [opt] template-parameter-list-seq
9437 decl-specifier-seq [opt] init-declarator [opt] ;
9438 export [opt] template-parameter-list-seq
9441 template-parameter-list-seq:
9442 template-parameter-list-seq [opt]
9443 template < template-parameter-list > */
9446 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9448 /* Check for `export'. */
9449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9451 /* Consume the `export' token. */
9452 cp_lexer_consume_token (parser->lexer);
9453 /* Warn that we do not support `export'. */
9454 warning (0, "keyword %<export%> not implemented, and will be ignored");
9457 cp_parser_template_declaration_after_export (parser, member_p);
9460 /* Parse a template-parameter-list.
9462 template-parameter-list:
9464 template-parameter-list , template-parameter
9466 Returns a TREE_LIST. Each node represents a template parameter.
9467 The nodes are connected via their TREE_CHAINs. */
9470 cp_parser_template_parameter_list (cp_parser* parser)
9472 tree parameter_list = NULL_TREE;
9474 begin_template_parm_list ();
9479 bool is_parameter_pack;
9481 /* Parse the template-parameter. */
9482 parameter = cp_parser_template_parameter (parser,
9484 &is_parameter_pack);
9485 /* Add it to the list. */
9486 if (parameter != error_mark_node)
9487 parameter_list = process_template_parm (parameter_list,
9493 tree err_parm = build_tree_list (parameter, parameter);
9494 TREE_VALUE (err_parm) = error_mark_node;
9495 parameter_list = chainon (parameter_list, err_parm);
9498 /* If the next token is not a `,', we're done. */
9499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9501 /* Otherwise, consume the `,' token. */
9502 cp_lexer_consume_token (parser->lexer);
9505 return end_template_parm_list (parameter_list);
9508 /* Parse a template-parameter.
9512 parameter-declaration
9514 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9515 the parameter. The TREE_PURPOSE is the default value, if any.
9516 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9517 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9518 set to true iff this parameter is a parameter pack. */
9521 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9522 bool *is_parameter_pack)
9525 cp_parameter_declarator *parameter_declarator;
9526 cp_declarator *id_declarator;
9529 /* Assume it is a type parameter or a template parameter. */
9530 *is_non_type = false;
9531 /* Assume it not a parameter pack. */
9532 *is_parameter_pack = false;
9533 /* Peek at the next token. */
9534 token = cp_lexer_peek_token (parser->lexer);
9535 /* If it is `class' or `template', we have a type-parameter. */
9536 if (token->keyword == RID_TEMPLATE)
9537 return cp_parser_type_parameter (parser, is_parameter_pack);
9538 /* If it is `class' or `typename' we do not know yet whether it is a
9539 type parameter or a non-type parameter. Consider:
9541 template <typename T, typename T::X X> ...
9545 template <class C, class D*> ...
9547 Here, the first parameter is a type parameter, and the second is
9548 a non-type parameter. We can tell by looking at the token after
9549 the identifier -- if it is a `,', `=', or `>' then we have a type
9551 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9553 /* Peek at the token after `class' or `typename'. */
9554 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9555 /* If it's an ellipsis, we have a template type parameter
9557 if (token->type == CPP_ELLIPSIS)
9558 return cp_parser_type_parameter (parser, is_parameter_pack);
9559 /* If it's an identifier, skip it. */
9560 if (token->type == CPP_NAME)
9561 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9562 /* Now, see if the token looks like the end of a template
9564 if (token->type == CPP_COMMA
9565 || token->type == CPP_EQ
9566 || token->type == CPP_GREATER)
9567 return cp_parser_type_parameter (parser, is_parameter_pack);
9570 /* Otherwise, it is a non-type parameter.
9574 When parsing a default template-argument for a non-type
9575 template-parameter, the first non-nested `>' is taken as the end
9576 of the template parameter-list rather than a greater-than
9578 *is_non_type = true;
9579 parameter_declarator
9580 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9581 /*parenthesized_p=*/NULL);
9583 /* If the parameter declaration is marked as a parameter pack, set
9584 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9585 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9587 if (parameter_declarator
9588 && parameter_declarator->declarator
9589 && parameter_declarator->declarator->parameter_pack_p)
9591 *is_parameter_pack = true;
9592 parameter_declarator->declarator->parameter_pack_p = false;
9595 /* If the next token is an ellipsis, and we don't already have it
9596 marked as a parameter pack, then we have a parameter pack (that
9597 has no declarator). */
9598 if (!*is_parameter_pack
9599 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9600 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9602 /* Consume the `...'. */
9603 cp_lexer_consume_token (parser->lexer);
9604 maybe_warn_variadic_templates ();
9606 *is_parameter_pack = true;
9608 /* We might end up with a pack expansion as the type of the non-type
9609 template parameter, in which case this is a non-type template
9611 else if (parameter_declarator
9612 && parameter_declarator->decl_specifiers.type
9613 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9615 *is_parameter_pack = true;
9616 parameter_declarator->decl_specifiers.type =
9617 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9620 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9622 /* Parameter packs cannot have default arguments. However, a
9623 user may try to do so, so we'll parse them and give an
9624 appropriate diagnostic here. */
9626 /* Consume the `='. */
9627 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9628 cp_lexer_consume_token (parser->lexer);
9630 /* Find the name of the parameter pack. */
9631 id_declarator = parameter_declarator->declarator;
9632 while (id_declarator && id_declarator->kind != cdk_id)
9633 id_declarator = id_declarator->declarator;
9635 if (id_declarator && id_declarator->kind == cdk_id)
9636 error ("%Htemplate parameter pack %qD cannot have a default argument",
9637 &start_token->location, id_declarator->u.id.unqualified_name);
9639 error ("%Htemplate parameter pack cannot have a default argument",
9640 &start_token->location);
9642 /* Parse the default argument, but throw away the result. */
9643 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9646 parm = grokdeclarator (parameter_declarator->declarator,
9647 ¶meter_declarator->decl_specifiers,
9648 PARM, /*initialized=*/0,
9650 if (parm == error_mark_node)
9651 return error_mark_node;
9653 return build_tree_list (parameter_declarator->default_argument, parm);
9656 /* Parse a type-parameter.
9659 class identifier [opt]
9660 class identifier [opt] = type-id
9661 typename identifier [opt]
9662 typename identifier [opt] = type-id
9663 template < template-parameter-list > class identifier [opt]
9664 template < template-parameter-list > class identifier [opt]
9667 GNU Extension (variadic templates):
9670 class ... identifier [opt]
9671 typename ... identifier [opt]
9673 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9674 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9675 the declaration of the parameter.
9677 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9680 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9685 /* Look for a keyword to tell us what kind of parameter this is. */
9686 token = cp_parser_require (parser, CPP_KEYWORD,
9687 "%<class%>, %<typename%>, or %<template%>");
9689 return error_mark_node;
9691 switch (token->keyword)
9697 tree default_argument;
9699 /* If the next token is an ellipsis, we have a template
9701 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9703 /* Consume the `...' token. */
9704 cp_lexer_consume_token (parser->lexer);
9705 maybe_warn_variadic_templates ();
9707 *is_parameter_pack = true;
9710 /* If the next token is an identifier, then it names the
9712 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9713 identifier = cp_parser_identifier (parser);
9715 identifier = NULL_TREE;
9717 /* Create the parameter. */
9718 parameter = finish_template_type_parm (class_type_node, identifier);
9720 /* If the next token is an `=', we have a default argument. */
9721 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9723 /* Consume the `=' token. */
9724 cp_lexer_consume_token (parser->lexer);
9725 /* Parse the default-argument. */
9726 push_deferring_access_checks (dk_no_deferred);
9727 default_argument = cp_parser_type_id (parser);
9729 /* Template parameter packs cannot have default
9731 if (*is_parameter_pack)
9734 error ("%Htemplate parameter pack %qD cannot have a "
9735 "default argument", &token->location, identifier);
9737 error ("%Htemplate parameter packs cannot have "
9738 "default arguments", &token->location);
9739 default_argument = NULL_TREE;
9741 pop_deferring_access_checks ();
9744 default_argument = NULL_TREE;
9746 /* Create the combined representation of the parameter and the
9747 default argument. */
9748 parameter = build_tree_list (default_argument, parameter);
9754 tree parameter_list;
9756 tree default_argument;
9758 /* Look for the `<'. */
9759 cp_parser_require (parser, CPP_LESS, "%<<%>");
9760 /* Parse the template-parameter-list. */
9761 parameter_list = cp_parser_template_parameter_list (parser);
9762 /* Look for the `>'. */
9763 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9764 /* Look for the `class' keyword. */
9765 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9766 /* If the next token is an ellipsis, we have a template
9768 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9770 /* Consume the `...' token. */
9771 cp_lexer_consume_token (parser->lexer);
9772 maybe_warn_variadic_templates ();
9774 *is_parameter_pack = true;
9776 /* If the next token is an `=', then there is a
9777 default-argument. If the next token is a `>', we are at
9778 the end of the parameter-list. If the next token is a `,',
9779 then we are at the end of this parameter. */
9780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9781 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9782 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9784 identifier = cp_parser_identifier (parser);
9785 /* Treat invalid names as if the parameter were nameless. */
9786 if (identifier == error_mark_node)
9787 identifier = NULL_TREE;
9790 identifier = NULL_TREE;
9792 /* Create the template parameter. */
9793 parameter = finish_template_template_parm (class_type_node,
9796 /* If the next token is an `=', then there is a
9797 default-argument. */
9798 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9802 /* Consume the `='. */
9803 cp_lexer_consume_token (parser->lexer);
9804 /* Parse the id-expression. */
9805 push_deferring_access_checks (dk_no_deferred);
9806 /* save token before parsing the id-expression, for error
9808 token = cp_lexer_peek_token (parser->lexer);
9810 = cp_parser_id_expression (parser,
9811 /*template_keyword_p=*/false,
9812 /*check_dependency_p=*/true,
9813 /*template_p=*/&is_template,
9814 /*declarator_p=*/false,
9815 /*optional_p=*/false);
9816 if (TREE_CODE (default_argument) == TYPE_DECL)
9817 /* If the id-expression was a template-id that refers to
9818 a template-class, we already have the declaration here,
9819 so no further lookup is needed. */
9822 /* Look up the name. */
9824 = cp_parser_lookup_name (parser, default_argument,
9826 /*is_template=*/is_template,
9827 /*is_namespace=*/false,
9828 /*check_dependency=*/true,
9829 /*ambiguous_decls=*/NULL,
9831 /* See if the default argument is valid. */
9833 = check_template_template_default_arg (default_argument);
9835 /* Template parameter packs cannot have default
9837 if (*is_parameter_pack)
9840 error ("%Htemplate parameter pack %qD cannot "
9841 "have a default argument",
9842 &token->location, identifier);
9844 error ("%Htemplate parameter packs cannot "
9845 "have default arguments",
9847 default_argument = NULL_TREE;
9849 pop_deferring_access_checks ();
9852 default_argument = NULL_TREE;
9854 /* Create the combined representation of the parameter and the
9855 default argument. */
9856 parameter = build_tree_list (default_argument, parameter);
9868 /* Parse a template-id.
9871 template-name < template-argument-list [opt] >
9873 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9874 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9875 returned. Otherwise, if the template-name names a function, or set
9876 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9877 names a class, returns a TYPE_DECL for the specialization.
9879 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9880 uninstantiated templates. */
9883 cp_parser_template_id (cp_parser *parser,
9884 bool template_keyword_p,
9885 bool check_dependency_p,
9886 bool is_declaration)
9892 cp_token_position start_of_id = 0;
9893 deferred_access_check *chk;
9894 VEC (deferred_access_check,gc) *access_check;
9895 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9898 /* If the next token corresponds to a template-id, there is no need
9900 next_token = cp_lexer_peek_token (parser->lexer);
9901 if (next_token->type == CPP_TEMPLATE_ID)
9903 struct tree_check *check_value;
9905 /* Get the stored value. */
9906 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9907 /* Perform any access checks that were deferred. */
9908 access_check = check_value->checks;
9912 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9915 perform_or_defer_access_check (chk->binfo,
9920 /* Return the stored value. */
9921 return check_value->value;
9924 /* Avoid performing name lookup if there is no possibility of
9925 finding a template-id. */
9926 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9927 || (next_token->type == CPP_NAME
9928 && !cp_parser_nth_token_starts_template_argument_list_p
9931 cp_parser_error (parser, "expected template-id");
9932 return error_mark_node;
9935 /* Remember where the template-id starts. */
9936 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9937 start_of_id = cp_lexer_token_position (parser->lexer, false);
9939 push_deferring_access_checks (dk_deferred);
9941 /* Parse the template-name. */
9942 is_identifier = false;
9943 token = cp_lexer_peek_token (parser->lexer);
9944 templ = cp_parser_template_name (parser, template_keyword_p,
9948 if (templ == error_mark_node || is_identifier)
9950 pop_deferring_access_checks ();
9954 /* If we find the sequence `[:' after a template-name, it's probably
9955 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9956 parse correctly the argument list. */
9957 next_token = cp_lexer_peek_token (parser->lexer);
9958 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9959 if (next_token->type == CPP_OPEN_SQUARE
9960 && next_token->flags & DIGRAPH
9961 && next_token_2->type == CPP_COLON
9962 && !(next_token_2->flags & PREV_WHITE))
9964 cp_parser_parse_tentatively (parser);
9965 /* Change `:' into `::'. */
9966 next_token_2->type = CPP_SCOPE;
9967 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9969 cp_lexer_consume_token (parser->lexer);
9971 /* Parse the arguments. */
9972 arguments = cp_parser_enclosed_template_argument_list (parser);
9973 if (!cp_parser_parse_definitely (parser))
9975 /* If we couldn't parse an argument list, then we revert our changes
9976 and return simply an error. Maybe this is not a template-id
9978 next_token_2->type = CPP_COLON;
9979 cp_parser_error (parser, "expected %<<%>");
9980 pop_deferring_access_checks ();
9981 return error_mark_node;
9983 /* Otherwise, emit an error about the invalid digraph, but continue
9984 parsing because we got our argument list. */
9985 if (permerror (next_token->location,
9986 "%<<::%> cannot begin a template-argument list"))
9988 static bool hint = false;
9989 inform (next_token->location,
9990 "%<<:%> is an alternate spelling for %<[%>."
9991 " Insert whitespace between %<<%> and %<::%>");
9992 if (!hint && !flag_permissive)
9994 inform (next_token->location, "(if you use %<-fpermissive%>"
9995 " G++ will accept your code)");
10002 /* Look for the `<' that starts the template-argument-list. */
10003 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10005 pop_deferring_access_checks ();
10006 return error_mark_node;
10008 /* Parse the arguments. */
10009 arguments = cp_parser_enclosed_template_argument_list (parser);
10012 /* Build a representation of the specialization. */
10013 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10014 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10015 else if (DECL_CLASS_TEMPLATE_P (templ)
10016 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10018 bool entering_scope;
10019 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10020 template (rather than some instantiation thereof) only if
10021 is not nested within some other construct. For example, in
10022 "template <typename T> void f(T) { A<T>::", A<T> is just an
10023 instantiation of A. */
10024 entering_scope = (template_parm_scope_p ()
10025 && cp_lexer_next_token_is (parser->lexer,
10028 = finish_template_type (templ, arguments, entering_scope);
10032 /* If it's not a class-template or a template-template, it should be
10033 a function-template. */
10034 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10035 || TREE_CODE (templ) == OVERLOAD
10036 || BASELINK_P (templ)));
10038 template_id = lookup_template_function (templ, arguments);
10041 /* If parsing tentatively, replace the sequence of tokens that makes
10042 up the template-id with a CPP_TEMPLATE_ID token. That way,
10043 should we re-parse the token stream, we will not have to repeat
10044 the effort required to do the parse, nor will we issue duplicate
10045 error messages about problems during instantiation of the
10049 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10051 /* Reset the contents of the START_OF_ID token. */
10052 token->type = CPP_TEMPLATE_ID;
10053 /* Retrieve any deferred checks. Do not pop this access checks yet
10054 so the memory will not be reclaimed during token replacing below. */
10055 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10056 token->u.tree_check_value->value = template_id;
10057 token->u.tree_check_value->checks = get_deferred_access_checks ();
10058 token->keyword = RID_MAX;
10060 /* Purge all subsequent tokens. */
10061 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10063 /* ??? Can we actually assume that, if template_id ==
10064 error_mark_node, we will have issued a diagnostic to the
10065 user, as opposed to simply marking the tentative parse as
10067 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10068 error ("%Hparse error in template argument list",
10072 pop_deferring_access_checks ();
10073 return template_id;
10076 /* Parse a template-name.
10081 The standard should actually say:
10085 operator-function-id
10087 A defect report has been filed about this issue.
10089 A conversion-function-id cannot be a template name because they cannot
10090 be part of a template-id. In fact, looking at this code:
10092 a.operator K<int>()
10094 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10095 It is impossible to call a templated conversion-function-id with an
10096 explicit argument list, since the only allowed template parameter is
10097 the type to which it is converting.
10099 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10100 `template' keyword, in a construction like:
10104 In that case `f' is taken to be a template-name, even though there
10105 is no way of knowing for sure.
10107 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10108 name refers to a set of overloaded functions, at least one of which
10109 is a template, or an IDENTIFIER_NODE with the name of the template,
10110 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10111 names are looked up inside uninstantiated templates. */
10114 cp_parser_template_name (cp_parser* parser,
10115 bool template_keyword_p,
10116 bool check_dependency_p,
10117 bool is_declaration,
10118 bool *is_identifier)
10123 cp_token *token = cp_lexer_peek_token (parser->lexer);
10125 /* If the next token is `operator', then we have either an
10126 operator-function-id or a conversion-function-id. */
10127 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10129 /* We don't know whether we're looking at an
10130 operator-function-id or a conversion-function-id. */
10131 cp_parser_parse_tentatively (parser);
10132 /* Try an operator-function-id. */
10133 identifier = cp_parser_operator_function_id (parser);
10134 /* If that didn't work, try a conversion-function-id. */
10135 if (!cp_parser_parse_definitely (parser))
10137 cp_parser_error (parser, "expected template-name");
10138 return error_mark_node;
10141 /* Look for the identifier. */
10143 identifier = cp_parser_identifier (parser);
10145 /* If we didn't find an identifier, we don't have a template-id. */
10146 if (identifier == error_mark_node)
10147 return error_mark_node;
10149 /* If the name immediately followed the `template' keyword, then it
10150 is a template-name. However, if the next token is not `<', then
10151 we do not treat it as a template-name, since it is not being used
10152 as part of a template-id. This enables us to handle constructs
10155 template <typename T> struct S { S(); };
10156 template <typename T> S<T>::S();
10158 correctly. We would treat `S' as a template -- if it were `S<T>'
10159 -- but we do not if there is no `<'. */
10161 if (processing_template_decl
10162 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10164 /* In a declaration, in a dependent context, we pretend that the
10165 "template" keyword was present in order to improve error
10166 recovery. For example, given:
10168 template <typename T> void f(T::X<int>);
10170 we want to treat "X<int>" as a template-id. */
10172 && !template_keyword_p
10173 && parser->scope && TYPE_P (parser->scope)
10174 && check_dependency_p
10175 && dependent_type_p (parser->scope)
10176 /* Do not do this for dtors (or ctors), since they never
10177 need the template keyword before their name. */
10178 && !constructor_name_p (identifier, parser->scope))
10180 cp_token_position start = 0;
10182 /* Explain what went wrong. */
10183 error ("%Hnon-template %qD used as template",
10184 &token->location, identifier);
10185 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10186 parser->scope, identifier);
10187 /* If parsing tentatively, find the location of the "<" token. */
10188 if (cp_parser_simulate_error (parser))
10189 start = cp_lexer_token_position (parser->lexer, true);
10190 /* Parse the template arguments so that we can issue error
10191 messages about them. */
10192 cp_lexer_consume_token (parser->lexer);
10193 cp_parser_enclosed_template_argument_list (parser);
10194 /* Skip tokens until we find a good place from which to
10195 continue parsing. */
10196 cp_parser_skip_to_closing_parenthesis (parser,
10197 /*recovering=*/true,
10199 /*consume_paren=*/false);
10200 /* If parsing tentatively, permanently remove the
10201 template argument list. That will prevent duplicate
10202 error messages from being issued about the missing
10203 "template" keyword. */
10205 cp_lexer_purge_tokens_after (parser->lexer, start);
10207 *is_identifier = true;
10211 /* If the "template" keyword is present, then there is generally
10212 no point in doing name-lookup, so we just return IDENTIFIER.
10213 But, if the qualifying scope is non-dependent then we can
10214 (and must) do name-lookup normally. */
10215 if (template_keyword_p
10217 || (TYPE_P (parser->scope)
10218 && dependent_type_p (parser->scope))))
10222 /* Look up the name. */
10223 decl = cp_parser_lookup_name (parser, identifier,
10225 /*is_template=*/false,
10226 /*is_namespace=*/false,
10227 check_dependency_p,
10228 /*ambiguous_decls=*/NULL,
10230 decl = maybe_get_template_decl_from_type_decl (decl);
10232 /* If DECL is a template, then the name was a template-name. */
10233 if (TREE_CODE (decl) == TEMPLATE_DECL)
10237 tree fn = NULL_TREE;
10239 /* The standard does not explicitly indicate whether a name that
10240 names a set of overloaded declarations, some of which are
10241 templates, is a template-name. However, such a name should
10242 be a template-name; otherwise, there is no way to form a
10243 template-id for the overloaded templates. */
10244 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10245 if (TREE_CODE (fns) == OVERLOAD)
10246 for (fn = fns; fn; fn = OVL_NEXT (fn))
10247 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10252 /* The name does not name a template. */
10253 cp_parser_error (parser, "expected template-name");
10254 return error_mark_node;
10258 /* If DECL is dependent, and refers to a function, then just return
10259 its name; we will look it up again during template instantiation. */
10260 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10262 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10263 if (TYPE_P (scope) && dependent_type_p (scope))
10270 /* Parse a template-argument-list.
10272 template-argument-list:
10273 template-argument ... [opt]
10274 template-argument-list , template-argument ... [opt]
10276 Returns a TREE_VEC containing the arguments. */
10279 cp_parser_template_argument_list (cp_parser* parser)
10281 tree fixed_args[10];
10282 unsigned n_args = 0;
10283 unsigned alloced = 10;
10284 tree *arg_ary = fixed_args;
10286 bool saved_in_template_argument_list_p;
10288 bool saved_non_ice_p;
10290 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10291 parser->in_template_argument_list_p = true;
10292 /* Even if the template-id appears in an integral
10293 constant-expression, the contents of the argument list do
10295 saved_ice_p = parser->integral_constant_expression_p;
10296 parser->integral_constant_expression_p = false;
10297 saved_non_ice_p = parser->non_integral_constant_expression_p;
10298 parser->non_integral_constant_expression_p = false;
10299 /* Parse the arguments. */
10305 /* Consume the comma. */
10306 cp_lexer_consume_token (parser->lexer);
10308 /* Parse the template-argument. */
10309 argument = cp_parser_template_argument (parser);
10311 /* If the next token is an ellipsis, we're expanding a template
10313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10315 /* Consume the `...' token. */
10316 cp_lexer_consume_token (parser->lexer);
10318 /* Make the argument into a TYPE_PACK_EXPANSION or
10319 EXPR_PACK_EXPANSION. */
10320 argument = make_pack_expansion (argument);
10323 if (n_args == alloced)
10327 if (arg_ary == fixed_args)
10329 arg_ary = XNEWVEC (tree, alloced);
10330 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10333 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10335 arg_ary[n_args++] = argument;
10337 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10339 vec = make_tree_vec (n_args);
10342 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10344 if (arg_ary != fixed_args)
10346 parser->non_integral_constant_expression_p = saved_non_ice_p;
10347 parser->integral_constant_expression_p = saved_ice_p;
10348 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10352 /* Parse a template-argument.
10355 assignment-expression
10359 The representation is that of an assignment-expression, type-id, or
10360 id-expression -- except that the qualified id-expression is
10361 evaluated, so that the value returned is either a DECL or an
10364 Although the standard says "assignment-expression", it forbids
10365 throw-expressions or assignments in the template argument.
10366 Therefore, we use "conditional-expression" instead. */
10369 cp_parser_template_argument (cp_parser* parser)
10374 bool maybe_type_id = false;
10375 cp_token *token = NULL, *argument_start_token = NULL;
10378 /* There's really no way to know what we're looking at, so we just
10379 try each alternative in order.
10383 In a template-argument, an ambiguity between a type-id and an
10384 expression is resolved to a type-id, regardless of the form of
10385 the corresponding template-parameter.
10387 Therefore, we try a type-id first. */
10388 cp_parser_parse_tentatively (parser);
10389 argument = cp_parser_type_id (parser);
10390 /* If there was no error parsing the type-id but the next token is a
10391 '>>', our behavior depends on which dialect of C++ we're
10392 parsing. In C++98, we probably found a typo for '> >'. But there
10393 are type-id which are also valid expressions. For instance:
10395 struct X { int operator >> (int); };
10396 template <int V> struct Foo {};
10399 Here 'X()' is a valid type-id of a function type, but the user just
10400 wanted to write the expression "X() >> 5". Thus, we remember that we
10401 found a valid type-id, but we still try to parse the argument as an
10402 expression to see what happens.
10404 In C++0x, the '>>' will be considered two separate '>'
10406 if (!cp_parser_error_occurred (parser)
10407 && cxx_dialect == cxx98
10408 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10410 maybe_type_id = true;
10411 cp_parser_abort_tentative_parse (parser);
10415 /* If the next token isn't a `,' or a `>', then this argument wasn't
10416 really finished. This means that the argument is not a valid
10418 if (!cp_parser_next_token_ends_template_argument_p (parser))
10419 cp_parser_error (parser, "expected template-argument");
10420 /* If that worked, we're done. */
10421 if (cp_parser_parse_definitely (parser))
10424 /* We're still not sure what the argument will be. */
10425 cp_parser_parse_tentatively (parser);
10426 /* Try a template. */
10427 argument_start_token = cp_lexer_peek_token (parser->lexer);
10428 argument = cp_parser_id_expression (parser,
10429 /*template_keyword_p=*/false,
10430 /*check_dependency_p=*/true,
10432 /*declarator_p=*/false,
10433 /*optional_p=*/false);
10434 /* If the next token isn't a `,' or a `>', then this argument wasn't
10435 really finished. */
10436 if (!cp_parser_next_token_ends_template_argument_p (parser))
10437 cp_parser_error (parser, "expected template-argument");
10438 if (!cp_parser_error_occurred (parser))
10440 /* Figure out what is being referred to. If the id-expression
10441 was for a class template specialization, then we will have a
10442 TYPE_DECL at this point. There is no need to do name lookup
10443 at this point in that case. */
10444 if (TREE_CODE (argument) != TYPE_DECL)
10445 argument = cp_parser_lookup_name (parser, argument,
10447 /*is_template=*/template_p,
10448 /*is_namespace=*/false,
10449 /*check_dependency=*/true,
10450 /*ambiguous_decls=*/NULL,
10451 argument_start_token->location);
10452 if (TREE_CODE (argument) != TEMPLATE_DECL
10453 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10454 cp_parser_error (parser, "expected template-name");
10456 if (cp_parser_parse_definitely (parser))
10458 /* It must be a non-type argument. There permitted cases are given
10459 in [temp.arg.nontype]:
10461 -- an integral constant-expression of integral or enumeration
10464 -- the name of a non-type template-parameter; or
10466 -- the name of an object or function with external linkage...
10468 -- the address of an object or function with external linkage...
10470 -- a pointer to member... */
10471 /* Look for a non-type template parameter. */
10472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10474 cp_parser_parse_tentatively (parser);
10475 argument = cp_parser_primary_expression (parser,
10476 /*address_p=*/false,
10478 /*template_arg_p=*/true,
10480 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10481 || !cp_parser_next_token_ends_template_argument_p (parser))
10482 cp_parser_simulate_error (parser);
10483 if (cp_parser_parse_definitely (parser))
10487 /* If the next token is "&", the argument must be the address of an
10488 object or function with external linkage. */
10489 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10491 cp_lexer_consume_token (parser->lexer);
10492 /* See if we might have an id-expression. */
10493 token = cp_lexer_peek_token (parser->lexer);
10494 if (token->type == CPP_NAME
10495 || token->keyword == RID_OPERATOR
10496 || token->type == CPP_SCOPE
10497 || token->type == CPP_TEMPLATE_ID
10498 || token->type == CPP_NESTED_NAME_SPECIFIER)
10500 cp_parser_parse_tentatively (parser);
10501 argument = cp_parser_primary_expression (parser,
10504 /*template_arg_p=*/true,
10506 if (cp_parser_error_occurred (parser)
10507 || !cp_parser_next_token_ends_template_argument_p (parser))
10508 cp_parser_abort_tentative_parse (parser);
10511 if (TREE_CODE (argument) == INDIRECT_REF)
10513 gcc_assert (REFERENCE_REF_P (argument));
10514 argument = TREE_OPERAND (argument, 0);
10517 if (TREE_CODE (argument) == VAR_DECL)
10519 /* A variable without external linkage might still be a
10520 valid constant-expression, so no error is issued here
10521 if the external-linkage check fails. */
10522 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10523 cp_parser_simulate_error (parser);
10525 else if (is_overloaded_fn (argument))
10526 /* All overloaded functions are allowed; if the external
10527 linkage test does not pass, an error will be issued
10531 && (TREE_CODE (argument) == OFFSET_REF
10532 || TREE_CODE (argument) == SCOPE_REF))
10533 /* A pointer-to-member. */
10535 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10538 cp_parser_simulate_error (parser);
10540 if (cp_parser_parse_definitely (parser))
10543 argument = build_x_unary_op (ADDR_EXPR, argument,
10544 tf_warning_or_error);
10549 /* If the argument started with "&", there are no other valid
10550 alternatives at this point. */
10553 cp_parser_error (parser, "invalid non-type template argument");
10554 return error_mark_node;
10557 /* If the argument wasn't successfully parsed as a type-id followed
10558 by '>>', the argument can only be a constant expression now.
10559 Otherwise, we try parsing the constant-expression tentatively,
10560 because the argument could really be a type-id. */
10562 cp_parser_parse_tentatively (parser);
10563 argument = cp_parser_constant_expression (parser,
10564 /*allow_non_constant_p=*/false,
10565 /*non_constant_p=*/NULL);
10566 argument = fold_non_dependent_expr (argument);
10567 if (!maybe_type_id)
10569 if (!cp_parser_next_token_ends_template_argument_p (parser))
10570 cp_parser_error (parser, "expected template-argument");
10571 if (cp_parser_parse_definitely (parser))
10573 /* We did our best to parse the argument as a non type-id, but that
10574 was the only alternative that matched (albeit with a '>' after
10575 it). We can assume it's just a typo from the user, and a
10576 diagnostic will then be issued. */
10577 return cp_parser_type_id (parser);
10580 /* Parse an explicit-instantiation.
10582 explicit-instantiation:
10583 template declaration
10585 Although the standard says `declaration', what it really means is:
10587 explicit-instantiation:
10588 template decl-specifier-seq [opt] declarator [opt] ;
10590 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10591 supposed to be allowed. A defect report has been filed about this
10596 explicit-instantiation:
10597 storage-class-specifier template
10598 decl-specifier-seq [opt] declarator [opt] ;
10599 function-specifier template
10600 decl-specifier-seq [opt] declarator [opt] ; */
10603 cp_parser_explicit_instantiation (cp_parser* parser)
10605 int declares_class_or_enum;
10606 cp_decl_specifier_seq decl_specifiers;
10607 tree extension_specifier = NULL_TREE;
10610 /* Look for an (optional) storage-class-specifier or
10611 function-specifier. */
10612 if (cp_parser_allow_gnu_extensions_p (parser))
10614 extension_specifier
10615 = cp_parser_storage_class_specifier_opt (parser);
10616 if (!extension_specifier)
10617 extension_specifier
10618 = cp_parser_function_specifier_opt (parser,
10619 /*decl_specs=*/NULL);
10622 /* Look for the `template' keyword. */
10623 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10624 /* Let the front end know that we are processing an explicit
10626 begin_explicit_instantiation ();
10627 /* [temp.explicit] says that we are supposed to ignore access
10628 control while processing explicit instantiation directives. */
10629 push_deferring_access_checks (dk_no_check);
10630 /* Parse a decl-specifier-seq. */
10631 token = cp_lexer_peek_token (parser->lexer);
10632 cp_parser_decl_specifier_seq (parser,
10633 CP_PARSER_FLAGS_OPTIONAL,
10635 &declares_class_or_enum);
10636 /* If there was exactly one decl-specifier, and it declared a class,
10637 and there's no declarator, then we have an explicit type
10639 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10643 type = check_tag_decl (&decl_specifiers);
10644 /* Turn access control back on for names used during
10645 template instantiation. */
10646 pop_deferring_access_checks ();
10648 do_type_instantiation (type, extension_specifier,
10649 /*complain=*/tf_error);
10653 cp_declarator *declarator;
10656 /* Parse the declarator. */
10658 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10659 /*ctor_dtor_or_conv_p=*/NULL,
10660 /*parenthesized_p=*/NULL,
10661 /*member_p=*/false);
10662 if (declares_class_or_enum & 2)
10663 cp_parser_check_for_definition_in_return_type (declarator,
10664 decl_specifiers.type,
10665 decl_specifiers.type_location);
10666 if (declarator != cp_error_declarator)
10668 decl = grokdeclarator (declarator, &decl_specifiers,
10669 NORMAL, 0, &decl_specifiers.attributes);
10670 /* Turn access control back on for names used during
10671 template instantiation. */
10672 pop_deferring_access_checks ();
10673 /* Do the explicit instantiation. */
10674 do_decl_instantiation (decl, extension_specifier);
10678 pop_deferring_access_checks ();
10679 /* Skip the body of the explicit instantiation. */
10680 cp_parser_skip_to_end_of_statement (parser);
10683 /* We're done with the instantiation. */
10684 end_explicit_instantiation ();
10686 cp_parser_consume_semicolon_at_end_of_statement (parser);
10689 /* Parse an explicit-specialization.
10691 explicit-specialization:
10692 template < > declaration
10694 Although the standard says `declaration', what it really means is:
10696 explicit-specialization:
10697 template <> decl-specifier [opt] init-declarator [opt] ;
10698 template <> function-definition
10699 template <> explicit-specialization
10700 template <> template-declaration */
10703 cp_parser_explicit_specialization (cp_parser* parser)
10705 bool need_lang_pop;
10706 cp_token *token = cp_lexer_peek_token (parser->lexer);
10708 /* Look for the `template' keyword. */
10709 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10710 /* Look for the `<'. */
10711 cp_parser_require (parser, CPP_LESS, "%<<%>");
10712 /* Look for the `>'. */
10713 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10714 /* We have processed another parameter list. */
10715 ++parser->num_template_parameter_lists;
10718 A template ... explicit specialization ... shall not have C
10720 if (current_lang_name == lang_name_c)
10722 error ("%Htemplate specialization with C linkage", &token->location);
10723 /* Give it C++ linkage to avoid confusing other parts of the
10725 push_lang_context (lang_name_cplusplus);
10726 need_lang_pop = true;
10729 need_lang_pop = false;
10730 /* Let the front end know that we are beginning a specialization. */
10731 if (!begin_specialization ())
10733 end_specialization ();
10734 cp_parser_skip_to_end_of_block_or_statement (parser);
10738 /* If the next keyword is `template', we need to figure out whether
10739 or not we're looking a template-declaration. */
10740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10742 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10743 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10744 cp_parser_template_declaration_after_export (parser,
10745 /*member_p=*/false);
10747 cp_parser_explicit_specialization (parser);
10750 /* Parse the dependent declaration. */
10751 cp_parser_single_declaration (parser,
10753 /*member_p=*/false,
10754 /*explicit_specialization_p=*/true,
10755 /*friend_p=*/NULL);
10756 /* We're done with the specialization. */
10757 end_specialization ();
10758 /* For the erroneous case of a template with C linkage, we pushed an
10759 implicit C++ linkage scope; exit that scope now. */
10761 pop_lang_context ();
10762 /* We're done with this parameter list. */
10763 --parser->num_template_parameter_lists;
10766 /* Parse a type-specifier.
10769 simple-type-specifier
10772 elaborated-type-specifier
10780 Returns a representation of the type-specifier. For a
10781 class-specifier, enum-specifier, or elaborated-type-specifier, a
10782 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10784 The parser flags FLAGS is used to control type-specifier parsing.
10786 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10787 in a decl-specifier-seq.
10789 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10790 class-specifier, enum-specifier, or elaborated-type-specifier, then
10791 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10792 if a type is declared; 2 if it is defined. Otherwise, it is set to
10795 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10796 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10797 is set to FALSE. */
10800 cp_parser_type_specifier (cp_parser* parser,
10801 cp_parser_flags flags,
10802 cp_decl_specifier_seq *decl_specs,
10803 bool is_declaration,
10804 int* declares_class_or_enum,
10805 bool* is_cv_qualifier)
10807 tree type_spec = NULL_TREE;
10810 cp_decl_spec ds = ds_last;
10812 /* Assume this type-specifier does not declare a new type. */
10813 if (declares_class_or_enum)
10814 *declares_class_or_enum = 0;
10815 /* And that it does not specify a cv-qualifier. */
10816 if (is_cv_qualifier)
10817 *is_cv_qualifier = false;
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10821 /* If we're looking at a keyword, we can use that to guide the
10822 production we choose. */
10823 keyword = token->keyword;
10827 /* Look for the enum-specifier. */
10828 type_spec = cp_parser_enum_specifier (parser);
10829 /* If that worked, we're done. */
10832 if (declares_class_or_enum)
10833 *declares_class_or_enum = 2;
10835 cp_parser_set_decl_spec_type (decl_specs,
10838 /*user_defined_p=*/true);
10842 goto elaborated_type_specifier;
10844 /* Any of these indicate either a class-specifier, or an
10845 elaborated-type-specifier. */
10849 /* Parse tentatively so that we can back up if we don't find a
10850 class-specifier. */
10851 cp_parser_parse_tentatively (parser);
10852 /* Look for the class-specifier. */
10853 type_spec = cp_parser_class_specifier (parser);
10854 /* If that worked, we're done. */
10855 if (cp_parser_parse_definitely (parser))
10857 if (declares_class_or_enum)
10858 *declares_class_or_enum = 2;
10860 cp_parser_set_decl_spec_type (decl_specs,
10863 /*user_defined_p=*/true);
10867 /* Fall through. */
10868 elaborated_type_specifier:
10869 /* We're declaring (not defining) a class or enum. */
10870 if (declares_class_or_enum)
10871 *declares_class_or_enum = 1;
10873 /* Fall through. */
10875 /* Look for an elaborated-type-specifier. */
10877 = (cp_parser_elaborated_type_specifier
10879 decl_specs && decl_specs->specs[(int) ds_friend],
10882 cp_parser_set_decl_spec_type (decl_specs,
10885 /*user_defined_p=*/true);
10890 if (is_cv_qualifier)
10891 *is_cv_qualifier = true;
10896 if (is_cv_qualifier)
10897 *is_cv_qualifier = true;
10902 if (is_cv_qualifier)
10903 *is_cv_qualifier = true;
10907 /* The `__complex__' keyword is a GNU extension. */
10915 /* Handle simple keywords. */
10920 ++decl_specs->specs[(int)ds];
10921 decl_specs->any_specifiers_p = true;
10923 return cp_lexer_consume_token (parser->lexer)->u.value;
10926 /* If we do not already have a type-specifier, assume we are looking
10927 at a simple-type-specifier. */
10928 type_spec = cp_parser_simple_type_specifier (parser,
10932 /* If we didn't find a type-specifier, and a type-specifier was not
10933 optional in this context, issue an error message. */
10934 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10936 cp_parser_error (parser, "expected type specifier");
10937 return error_mark_node;
10943 /* Parse a simple-type-specifier.
10945 simple-type-specifier:
10946 :: [opt] nested-name-specifier [opt] type-name
10947 :: [opt] nested-name-specifier template template-id
10962 simple-type-specifier:
10964 decltype ( expression )
10970 simple-type-specifier:
10971 __typeof__ unary-expression
10972 __typeof__ ( type-id )
10974 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10975 appropriately updated. */
10978 cp_parser_simple_type_specifier (cp_parser* parser,
10979 cp_decl_specifier_seq *decl_specs,
10980 cp_parser_flags flags)
10982 tree type = NULL_TREE;
10985 /* Peek at the next token. */
10986 token = cp_lexer_peek_token (parser->lexer);
10988 /* If we're looking at a keyword, things are easy. */
10989 switch (token->keyword)
10993 decl_specs->explicit_char_p = true;
10994 type = char_type_node;
10997 type = char16_type_node;
11000 type = char32_type_node;
11003 type = wchar_type_node;
11006 type = boolean_type_node;
11010 ++decl_specs->specs[(int) ds_short];
11011 type = short_integer_type_node;
11015 decl_specs->explicit_int_p = true;
11016 type = integer_type_node;
11020 ++decl_specs->specs[(int) ds_long];
11021 type = long_integer_type_node;
11025 ++decl_specs->specs[(int) ds_signed];
11026 type = integer_type_node;
11030 ++decl_specs->specs[(int) ds_unsigned];
11031 type = unsigned_type_node;
11034 type = float_type_node;
11037 type = double_type_node;
11040 type = void_type_node;
11044 if (cxx_dialect != cxx98)
11046 /* Consume the token. */
11047 cp_lexer_consume_token (parser->lexer);
11048 /* We do not yet support the use of `auto' as a
11050 error ("%HC++0x %<auto%> specifier not supported", &token->location);
11055 /* Parse the `decltype' type. */
11056 type = cp_parser_decltype (parser);
11059 cp_parser_set_decl_spec_type (decl_specs, type,
11061 /*user_defined_p=*/true);
11066 /* Consume the `typeof' token. */
11067 cp_lexer_consume_token (parser->lexer);
11068 /* Parse the operand to `typeof'. */
11069 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11070 /* If it is not already a TYPE, take its type. */
11071 if (!TYPE_P (type))
11072 type = finish_typeof (type);
11075 cp_parser_set_decl_spec_type (decl_specs, type,
11077 /*user_defined_p=*/true);
11085 /* If the type-specifier was for a built-in type, we're done. */
11090 /* Record the type. */
11092 && (token->keyword != RID_SIGNED
11093 && token->keyword != RID_UNSIGNED
11094 && token->keyword != RID_SHORT
11095 && token->keyword != RID_LONG))
11096 cp_parser_set_decl_spec_type (decl_specs,
11099 /*user_defined=*/false);
11101 decl_specs->any_specifiers_p = true;
11103 /* Consume the token. */
11104 id = cp_lexer_consume_token (parser->lexer)->u.value;
11106 /* There is no valid C++ program where a non-template type is
11107 followed by a "<". That usually indicates that the user thought
11108 that the type was a template. */
11109 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11111 return TYPE_NAME (type);
11114 /* The type-specifier must be a user-defined type. */
11115 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11120 /* Don't gobble tokens or issue error messages if this is an
11121 optional type-specifier. */
11122 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11123 cp_parser_parse_tentatively (parser);
11125 /* Look for the optional `::' operator. */
11127 = (cp_parser_global_scope_opt (parser,
11128 /*current_scope_valid_p=*/false)
11130 /* Look for the nested-name specifier. */
11132 = (cp_parser_nested_name_specifier_opt (parser,
11133 /*typename_keyword_p=*/false,
11134 /*check_dependency_p=*/true,
11136 /*is_declaration=*/false)
11138 token = cp_lexer_peek_token (parser->lexer);
11139 /* If we have seen a nested-name-specifier, and the next token
11140 is `template', then we are using the template-id production. */
11142 && cp_parser_optional_template_keyword (parser))
11144 /* Look for the template-id. */
11145 type = cp_parser_template_id (parser,
11146 /*template_keyword_p=*/true,
11147 /*check_dependency_p=*/true,
11148 /*is_declaration=*/false);
11149 /* If the template-id did not name a type, we are out of
11151 if (TREE_CODE (type) != TYPE_DECL)
11153 cp_parser_error (parser, "expected template-id for type");
11157 /* Otherwise, look for a type-name. */
11159 type = cp_parser_type_name (parser);
11160 /* Keep track of all name-lookups performed in class scopes. */
11164 && TREE_CODE (type) == TYPE_DECL
11165 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11166 maybe_note_name_used_in_class (DECL_NAME (type), type);
11167 /* If it didn't work out, we don't have a TYPE. */
11168 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11169 && !cp_parser_parse_definitely (parser))
11171 if (type && decl_specs)
11172 cp_parser_set_decl_spec_type (decl_specs, type,
11174 /*user_defined=*/true);
11177 /* If we didn't get a type-name, issue an error message. */
11178 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11180 cp_parser_error (parser, "expected type-name");
11181 return error_mark_node;
11184 /* There is no valid C++ program where a non-template type is
11185 followed by a "<". That usually indicates that the user thought
11186 that the type was a template. */
11187 if (type && type != error_mark_node)
11189 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11190 If it is, then the '<'...'>' enclose protocol names rather than
11191 template arguments, and so everything is fine. */
11192 if (c_dialect_objc ()
11193 && (objc_is_id (type) || objc_is_class_name (type)))
11195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11196 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11198 /* Clobber the "unqualified" type previously entered into
11199 DECL_SPECS with the new, improved protocol-qualified version. */
11201 decl_specs->type = qual_type;
11206 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11213 /* Parse a type-name.
11226 Returns a TYPE_DECL for the type. */
11229 cp_parser_type_name (cp_parser* parser)
11233 /* We can't know yet whether it is a class-name or not. */
11234 cp_parser_parse_tentatively (parser);
11235 /* Try a class-name. */
11236 type_decl = cp_parser_class_name (parser,
11237 /*typename_keyword_p=*/false,
11238 /*template_keyword_p=*/false,
11240 /*check_dependency_p=*/true,
11241 /*class_head_p=*/false,
11242 /*is_declaration=*/false);
11243 /* If it's not a class-name, keep looking. */
11244 if (!cp_parser_parse_definitely (parser))
11246 /* It must be a typedef-name or an enum-name. */
11247 return cp_parser_nonclass_name (parser);
11253 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11261 Returns a TYPE_DECL for the type. */
11264 cp_parser_nonclass_name (cp_parser* parser)
11269 cp_token *token = cp_lexer_peek_token (parser->lexer);
11270 identifier = cp_parser_identifier (parser);
11271 if (identifier == error_mark_node)
11272 return error_mark_node;
11274 /* Look up the type-name. */
11275 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11277 if (TREE_CODE (type_decl) != TYPE_DECL
11278 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11280 /* See if this is an Objective-C type. */
11281 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11282 tree type = objc_get_protocol_qualified_type (identifier, protos);
11284 type_decl = TYPE_NAME (type);
11287 /* Issue an error if we did not find a type-name. */
11288 if (TREE_CODE (type_decl) != TYPE_DECL)
11290 if (!cp_parser_simulate_error (parser))
11291 cp_parser_name_lookup_error (parser, identifier, type_decl,
11292 "is not a type", token->location);
11293 return error_mark_node;
11295 /* Remember that the name was used in the definition of the
11296 current class so that we can check later to see if the
11297 meaning would have been different after the class was
11298 entirely defined. */
11299 else if (type_decl != error_mark_node
11301 maybe_note_name_used_in_class (identifier, type_decl);
11306 /* Parse an elaborated-type-specifier. Note that the grammar given
11307 here incorporates the resolution to DR68.
11309 elaborated-type-specifier:
11310 class-key :: [opt] nested-name-specifier [opt] identifier
11311 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11312 enum :: [opt] nested-name-specifier [opt] identifier
11313 typename :: [opt] nested-name-specifier identifier
11314 typename :: [opt] nested-name-specifier template [opt]
11319 elaborated-type-specifier:
11320 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11321 class-key attributes :: [opt] nested-name-specifier [opt]
11322 template [opt] template-id
11323 enum attributes :: [opt] nested-name-specifier [opt] identifier
11325 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11326 declared `friend'. If IS_DECLARATION is TRUE, then this
11327 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11328 something is being declared.
11330 Returns the TYPE specified. */
11333 cp_parser_elaborated_type_specifier (cp_parser* parser,
11335 bool is_declaration)
11337 enum tag_types tag_type;
11339 tree type = NULL_TREE;
11340 tree attributes = NULL_TREE;
11341 cp_token *token = NULL;
11343 /* See if we're looking at the `enum' keyword. */
11344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11346 /* Consume the `enum' token. */
11347 cp_lexer_consume_token (parser->lexer);
11348 /* Remember that it's an enumeration type. */
11349 tag_type = enum_type;
11350 /* Parse the attributes. */
11351 attributes = cp_parser_attributes_opt (parser);
11353 /* Or, it might be `typename'. */
11354 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11357 /* Consume the `typename' token. */
11358 cp_lexer_consume_token (parser->lexer);
11359 /* Remember that it's a `typename' type. */
11360 tag_type = typename_type;
11361 /* The `typename' keyword is only allowed in templates. */
11362 if (!processing_template_decl)
11363 permerror (input_location, "using %<typename%> outside of template");
11365 /* Otherwise it must be a class-key. */
11368 tag_type = cp_parser_class_key (parser);
11369 if (tag_type == none_type)
11370 return error_mark_node;
11371 /* Parse the attributes. */
11372 attributes = cp_parser_attributes_opt (parser);
11375 /* Look for the `::' operator. */
11376 cp_parser_global_scope_opt (parser,
11377 /*current_scope_valid_p=*/false);
11378 /* Look for the nested-name-specifier. */
11379 if (tag_type == typename_type)
11381 if (!cp_parser_nested_name_specifier (parser,
11382 /*typename_keyword_p=*/true,
11383 /*check_dependency_p=*/true,
11386 return error_mark_node;
11389 /* Even though `typename' is not present, the proposed resolution
11390 to Core Issue 180 says that in `class A<T>::B', `B' should be
11391 considered a type-name, even if `A<T>' is dependent. */
11392 cp_parser_nested_name_specifier_opt (parser,
11393 /*typename_keyword_p=*/true,
11394 /*check_dependency_p=*/true,
11397 /* For everything but enumeration types, consider a template-id.
11398 For an enumeration type, consider only a plain identifier. */
11399 if (tag_type != enum_type)
11401 bool template_p = false;
11404 /* Allow the `template' keyword. */
11405 template_p = cp_parser_optional_template_keyword (parser);
11406 /* If we didn't see `template', we don't know if there's a
11407 template-id or not. */
11409 cp_parser_parse_tentatively (parser);
11410 /* Parse the template-id. */
11411 token = cp_lexer_peek_token (parser->lexer);
11412 decl = cp_parser_template_id (parser, template_p,
11413 /*check_dependency_p=*/true,
11415 /* If we didn't find a template-id, look for an ordinary
11417 if (!template_p && !cp_parser_parse_definitely (parser))
11419 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11420 in effect, then we must assume that, upon instantiation, the
11421 template will correspond to a class. */
11422 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11423 && tag_type == typename_type)
11424 type = make_typename_type (parser->scope, decl,
11426 /*complain=*/tf_error);
11428 type = TREE_TYPE (decl);
11433 token = cp_lexer_peek_token (parser->lexer);
11434 identifier = cp_parser_identifier (parser);
11436 if (identifier == error_mark_node)
11438 parser->scope = NULL_TREE;
11439 return error_mark_node;
11442 /* For a `typename', we needn't call xref_tag. */
11443 if (tag_type == typename_type
11444 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11445 return cp_parser_make_typename_type (parser, parser->scope,
11448 /* Look up a qualified name in the usual way. */
11452 tree ambiguous_decls;
11454 decl = cp_parser_lookup_name (parser, identifier,
11456 /*is_template=*/false,
11457 /*is_namespace=*/false,
11458 /*check_dependency=*/true,
11462 /* If the lookup was ambiguous, an error will already have been
11464 if (ambiguous_decls)
11465 return error_mark_node;
11467 /* If we are parsing friend declaration, DECL may be a
11468 TEMPLATE_DECL tree node here. However, we need to check
11469 whether this TEMPLATE_DECL results in valid code. Consider
11470 the following example:
11473 template <class T> class C {};
11476 template <class T> friend class N::C; // #1, valid code
11478 template <class T> class Y {
11479 friend class N::C; // #2, invalid code
11482 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11483 name lookup of `N::C'. We see that friend declaration must
11484 be template for the code to be valid. Note that
11485 processing_template_decl does not work here since it is
11486 always 1 for the above two cases. */
11488 decl = (cp_parser_maybe_treat_template_as_class
11489 (decl, /*tag_name_p=*/is_friend
11490 && parser->num_template_parameter_lists));
11492 if (TREE_CODE (decl) != TYPE_DECL)
11494 cp_parser_diagnose_invalid_type_name (parser,
11498 return error_mark_node;
11501 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11503 bool allow_template = (parser->num_template_parameter_lists
11504 || DECL_SELF_REFERENCE_P (decl));
11505 type = check_elaborated_type_specifier (tag_type, decl,
11508 if (type == error_mark_node)
11509 return error_mark_node;
11512 /* Forward declarations of nested types, such as
11517 are invalid unless all components preceding the final '::'
11518 are complete. If all enclosing types are complete, these
11519 declarations become merely pointless.
11521 Invalid forward declarations of nested types are errors
11522 caught elsewhere in parsing. Those that are pointless arrive
11525 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11526 && !is_friend && !processing_explicit_instantiation)
11527 warning (0, "declaration %qD does not declare anything", decl);
11529 type = TREE_TYPE (decl);
11533 /* An elaborated-type-specifier sometimes introduces a new type and
11534 sometimes names an existing type. Normally, the rule is that it
11535 introduces a new type only if there is not an existing type of
11536 the same name already in scope. For example, given:
11539 void f() { struct S s; }
11541 the `struct S' in the body of `f' is the same `struct S' as in
11542 the global scope; the existing definition is used. However, if
11543 there were no global declaration, this would introduce a new
11544 local class named `S'.
11546 An exception to this rule applies to the following code:
11548 namespace N { struct S; }
11550 Here, the elaborated-type-specifier names a new type
11551 unconditionally; even if there is already an `S' in the
11552 containing scope this declaration names a new type.
11553 This exception only applies if the elaborated-type-specifier
11554 forms the complete declaration:
11558 A declaration consisting solely of `class-key identifier ;' is
11559 either a redeclaration of the name in the current scope or a
11560 forward declaration of the identifier as a class name. It
11561 introduces the name into the current scope.
11563 We are in this situation precisely when the next token is a `;'.
11565 An exception to the exception is that a `friend' declaration does
11566 *not* name a new type; i.e., given:
11568 struct S { friend struct T; };
11570 `T' is not a new type in the scope of `S'.
11572 Also, `new struct S' or `sizeof (struct S)' never results in the
11573 definition of a new type; a new type can only be declared in a
11574 declaration context. */
11580 /* Friends have special name lookup rules. */
11581 ts = ts_within_enclosing_non_class;
11582 else if (is_declaration
11583 && cp_lexer_next_token_is (parser->lexer,
11585 /* This is a `class-key identifier ;' */
11591 (parser->num_template_parameter_lists
11592 && (cp_parser_next_token_starts_class_definition_p (parser)
11593 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11594 /* An unqualified name was used to reference this type, so
11595 there were no qualifying templates. */
11596 if (!cp_parser_check_template_parameters (parser,
11597 /*num_templates=*/0,
11599 return error_mark_node;
11600 type = xref_tag (tag_type, identifier, ts, template_p);
11604 if (type == error_mark_node)
11605 return error_mark_node;
11607 /* Allow attributes on forward declarations of classes. */
11610 if (TREE_CODE (type) == TYPENAME_TYPE)
11611 warning (OPT_Wattributes,
11612 "attributes ignored on uninstantiated type");
11613 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11614 && ! processing_explicit_instantiation)
11615 warning (OPT_Wattributes,
11616 "attributes ignored on template instantiation");
11617 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11618 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11620 warning (OPT_Wattributes,
11621 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11624 if (tag_type != enum_type)
11625 cp_parser_check_class_key (tag_type, type);
11627 /* A "<" cannot follow an elaborated type specifier. If that
11628 happens, the user was probably trying to form a template-id. */
11629 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11634 /* Parse an enum-specifier.
11637 enum identifier [opt] { enumerator-list [opt] }
11640 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11643 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11644 if the token stream isn't an enum-specifier after all. */
11647 cp_parser_enum_specifier (cp_parser* parser)
11653 /* Parse tentatively so that we can back up if we don't find a
11655 cp_parser_parse_tentatively (parser);
11657 /* Caller guarantees that the current token is 'enum', an identifier
11658 possibly follows, and the token after that is an opening brace.
11659 If we don't have an identifier, fabricate an anonymous name for
11660 the enumeration being defined. */
11661 cp_lexer_consume_token (parser->lexer);
11663 attributes = cp_parser_attributes_opt (parser);
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11666 identifier = cp_parser_identifier (parser);
11668 identifier = make_anon_name ();
11670 /* Look for the `{' but don't consume it yet. */
11671 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11672 cp_parser_simulate_error (parser);
11674 if (!cp_parser_parse_definitely (parser))
11677 /* Issue an error message if type-definitions are forbidden here. */
11678 if (!cp_parser_check_type_definition (parser))
11679 type = error_mark_node;
11681 /* Create the new type. We do this before consuming the opening
11682 brace so the enum will be recorded as being on the line of its
11683 tag (or the 'enum' keyword, if there is no tag). */
11684 type = start_enum (identifier);
11686 /* Consume the opening brace. */
11687 cp_lexer_consume_token (parser->lexer);
11689 if (type == error_mark_node)
11691 cp_parser_skip_to_end_of_block_or_statement (parser);
11692 return error_mark_node;
11695 /* If the next token is not '}', then there are some enumerators. */
11696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11697 cp_parser_enumerator_list (parser, type);
11699 /* Consume the final '}'. */
11700 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11702 /* Look for trailing attributes to apply to this enumeration, and
11703 apply them if appropriate. */
11704 if (cp_parser_allow_gnu_extensions_p (parser))
11706 tree trailing_attr = cp_parser_attributes_opt (parser);
11707 cplus_decl_attributes (&type,
11709 (int) ATTR_FLAG_TYPE_IN_PLACE);
11712 /* Finish up the enumeration. */
11713 finish_enum (type);
11718 /* Parse an enumerator-list. The enumerators all have the indicated
11722 enumerator-definition
11723 enumerator-list , enumerator-definition */
11726 cp_parser_enumerator_list (cp_parser* parser, tree type)
11730 /* Parse an enumerator-definition. */
11731 cp_parser_enumerator_definition (parser, type);
11733 /* If the next token is not a ',', we've reached the end of
11735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11737 /* Otherwise, consume the `,' and keep going. */
11738 cp_lexer_consume_token (parser->lexer);
11739 /* If the next token is a `}', there is a trailing comma. */
11740 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11742 if (!in_system_header)
11743 pedwarn (OPT_pedantic, "comma at end of enumerator list");
11749 /* Parse an enumerator-definition. The enumerator has the indicated
11752 enumerator-definition:
11754 enumerator = constant-expression
11760 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11765 /* Look for the identifier. */
11766 identifier = cp_parser_identifier (parser);
11767 if (identifier == error_mark_node)
11770 /* If the next token is an '=', then there is an explicit value. */
11771 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11773 /* Consume the `=' token. */
11774 cp_lexer_consume_token (parser->lexer);
11775 /* Parse the value. */
11776 value = cp_parser_constant_expression (parser,
11777 /*allow_non_constant_p=*/false,
11783 /* Create the enumerator. */
11784 build_enumerator (identifier, value, type);
11787 /* Parse a namespace-name.
11790 original-namespace-name
11793 Returns the NAMESPACE_DECL for the namespace. */
11796 cp_parser_namespace_name (cp_parser* parser)
11799 tree namespace_decl;
11801 cp_token *token = cp_lexer_peek_token (parser->lexer);
11803 /* Get the name of the namespace. */
11804 identifier = cp_parser_identifier (parser);
11805 if (identifier == error_mark_node)
11806 return error_mark_node;
11808 /* Look up the identifier in the currently active scope. Look only
11809 for namespaces, due to:
11811 [basic.lookup.udir]
11813 When looking up a namespace-name in a using-directive or alias
11814 definition, only namespace names are considered.
11818 [basic.lookup.qual]
11820 During the lookup of a name preceding the :: scope resolution
11821 operator, object, function, and enumerator names are ignored.
11823 (Note that cp_parser_class_or_namespace_name only calls this
11824 function if the token after the name is the scope resolution
11826 namespace_decl = cp_parser_lookup_name (parser, identifier,
11828 /*is_template=*/false,
11829 /*is_namespace=*/true,
11830 /*check_dependency=*/true,
11831 /*ambiguous_decls=*/NULL,
11833 /* If it's not a namespace, issue an error. */
11834 if (namespace_decl == error_mark_node
11835 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11837 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11838 error ("%H%qD is not a namespace-name", &token->location, identifier);
11839 cp_parser_error (parser, "expected namespace-name");
11840 namespace_decl = error_mark_node;
11843 return namespace_decl;
11846 /* Parse a namespace-definition.
11848 namespace-definition:
11849 named-namespace-definition
11850 unnamed-namespace-definition
11852 named-namespace-definition:
11853 original-namespace-definition
11854 extension-namespace-definition
11856 original-namespace-definition:
11857 namespace identifier { namespace-body }
11859 extension-namespace-definition:
11860 namespace original-namespace-name { namespace-body }
11862 unnamed-namespace-definition:
11863 namespace { namespace-body } */
11866 cp_parser_namespace_definition (cp_parser* parser)
11868 tree identifier, attribs;
11869 bool has_visibility;
11872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11875 cp_lexer_consume_token (parser->lexer);
11880 /* Look for the `namespace' keyword. */
11881 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11883 /* Get the name of the namespace. We do not attempt to distinguish
11884 between an original-namespace-definition and an
11885 extension-namespace-definition at this point. The semantic
11886 analysis routines are responsible for that. */
11887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11888 identifier = cp_parser_identifier (parser);
11890 identifier = NULL_TREE;
11892 /* Parse any specified attributes. */
11893 attribs = cp_parser_attributes_opt (parser);
11895 /* Look for the `{' to start the namespace. */
11896 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11897 /* Start the namespace. */
11898 push_namespace (identifier);
11900 /* "inline namespace" is equivalent to a stub namespace definition
11901 followed by a strong using directive. */
11904 tree name_space = current_namespace;
11905 /* Set up namespace association. */
11906 DECL_NAMESPACE_ASSOCIATIONS (name_space)
11907 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11908 DECL_NAMESPACE_ASSOCIATIONS (name_space));
11909 /* Import the contents of the inline namespace. */
11911 do_using_directive (name_space);
11912 push_namespace (identifier);
11915 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11917 /* Parse the body of the namespace. */
11918 cp_parser_namespace_body (parser);
11920 #ifdef HANDLE_PRAGMA_VISIBILITY
11921 if (has_visibility)
11925 /* Finish the namespace. */
11927 /* Look for the final `}'. */
11928 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11931 /* Parse a namespace-body.
11934 declaration-seq [opt] */
11937 cp_parser_namespace_body (cp_parser* parser)
11939 cp_parser_declaration_seq_opt (parser);
11942 /* Parse a namespace-alias-definition.
11944 namespace-alias-definition:
11945 namespace identifier = qualified-namespace-specifier ; */
11948 cp_parser_namespace_alias_definition (cp_parser* parser)
11951 tree namespace_specifier;
11953 cp_token *token = cp_lexer_peek_token (parser->lexer);
11955 /* Look for the `namespace' keyword. */
11956 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11957 /* Look for the identifier. */
11958 identifier = cp_parser_identifier (parser);
11959 if (identifier == error_mark_node)
11961 /* Look for the `=' token. */
11962 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11963 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11965 error ("%H%<namespace%> definition is not allowed here", &token->location);
11966 /* Skip the definition. */
11967 cp_lexer_consume_token (parser->lexer);
11968 if (cp_parser_skip_to_closing_brace (parser))
11969 cp_lexer_consume_token (parser->lexer);
11972 cp_parser_require (parser, CPP_EQ, "%<=%>");
11973 /* Look for the qualified-namespace-specifier. */
11974 namespace_specifier
11975 = cp_parser_qualified_namespace_specifier (parser);
11976 /* Look for the `;' token. */
11977 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11979 /* Register the alias in the symbol table. */
11980 do_namespace_alias (identifier, namespace_specifier);
11983 /* Parse a qualified-namespace-specifier.
11985 qualified-namespace-specifier:
11986 :: [opt] nested-name-specifier [opt] namespace-name
11988 Returns a NAMESPACE_DECL corresponding to the specified
11992 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11994 /* Look for the optional `::'. */
11995 cp_parser_global_scope_opt (parser,
11996 /*current_scope_valid_p=*/false);
11998 /* Look for the optional nested-name-specifier. */
11999 cp_parser_nested_name_specifier_opt (parser,
12000 /*typename_keyword_p=*/false,
12001 /*check_dependency_p=*/true,
12003 /*is_declaration=*/true);
12005 return cp_parser_namespace_name (parser);
12008 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12009 access declaration.
12012 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12013 using :: unqualified-id ;
12015 access-declaration:
12021 cp_parser_using_declaration (cp_parser* parser,
12022 bool access_declaration_p)
12025 bool typename_p = false;
12026 bool global_scope_p;
12031 if (access_declaration_p)
12032 cp_parser_parse_tentatively (parser);
12035 /* Look for the `using' keyword. */
12036 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040 /* See if it's `typename'. */
12041 if (token->keyword == RID_TYPENAME)
12043 /* Remember that we've seen it. */
12045 /* Consume the `typename' token. */
12046 cp_lexer_consume_token (parser->lexer);
12050 /* Look for the optional global scope qualification. */
12052 = (cp_parser_global_scope_opt (parser,
12053 /*current_scope_valid_p=*/false)
12056 /* If we saw `typename', or didn't see `::', then there must be a
12057 nested-name-specifier present. */
12058 if (typename_p || !global_scope_p)
12059 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12060 /*check_dependency_p=*/true,
12062 /*is_declaration=*/true);
12063 /* Otherwise, we could be in either of the two productions. In that
12064 case, treat the nested-name-specifier as optional. */
12066 qscope = cp_parser_nested_name_specifier_opt (parser,
12067 /*typename_keyword_p=*/false,
12068 /*check_dependency_p=*/true,
12070 /*is_declaration=*/true);
12072 qscope = global_namespace;
12074 if (access_declaration_p && cp_parser_error_occurred (parser))
12075 /* Something has already gone wrong; there's no need to parse
12076 further. Since an error has occurred, the return value of
12077 cp_parser_parse_definitely will be false, as required. */
12078 return cp_parser_parse_definitely (parser);
12080 token = cp_lexer_peek_token (parser->lexer);
12081 /* Parse the unqualified-id. */
12082 identifier = cp_parser_unqualified_id (parser,
12083 /*template_keyword_p=*/false,
12084 /*check_dependency_p=*/true,
12085 /*declarator_p=*/true,
12086 /*optional_p=*/false);
12088 if (access_declaration_p)
12090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12091 cp_parser_simulate_error (parser);
12092 if (!cp_parser_parse_definitely (parser))
12096 /* The function we call to handle a using-declaration is different
12097 depending on what scope we are in. */
12098 if (qscope == error_mark_node || identifier == error_mark_node)
12100 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12101 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12102 /* [namespace.udecl]
12104 A using declaration shall not name a template-id. */
12105 error ("%Ha template-id may not appear in a using-declaration",
12109 if (at_class_scope_p ())
12111 /* Create the USING_DECL. */
12112 decl = do_class_using_decl (parser->scope, identifier);
12114 if (check_for_bare_parameter_packs (decl))
12117 /* Add it to the list of members in this class. */
12118 finish_member_declaration (decl);
12122 decl = cp_parser_lookup_name_simple (parser,
12125 if (decl == error_mark_node)
12126 cp_parser_name_lookup_error (parser, identifier,
12129 else if (check_for_bare_parameter_packs (decl))
12131 else if (!at_namespace_scope_p ())
12132 do_local_using_decl (decl, qscope, identifier);
12134 do_toplevel_using_decl (decl, qscope, identifier);
12138 /* Look for the final `;'. */
12139 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12144 /* Parse a using-directive.
12147 using namespace :: [opt] nested-name-specifier [opt]
12148 namespace-name ; */
12151 cp_parser_using_directive (cp_parser* parser)
12153 tree namespace_decl;
12156 /* Look for the `using' keyword. */
12157 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12158 /* And the `namespace' keyword. */
12159 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12160 /* Look for the optional `::' operator. */
12161 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12162 /* And the optional nested-name-specifier. */
12163 cp_parser_nested_name_specifier_opt (parser,
12164 /*typename_keyword_p=*/false,
12165 /*check_dependency_p=*/true,
12167 /*is_declaration=*/true);
12168 /* Get the namespace being used. */
12169 namespace_decl = cp_parser_namespace_name (parser);
12170 /* And any specified attributes. */
12171 attribs = cp_parser_attributes_opt (parser);
12172 /* Update the symbol table. */
12173 parse_using_directive (namespace_decl, attribs);
12174 /* Look for the final `;'. */
12175 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12178 /* Parse an asm-definition.
12181 asm ( string-literal ) ;
12186 asm volatile [opt] ( string-literal ) ;
12187 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12188 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12189 : asm-operand-list [opt] ) ;
12190 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12191 : asm-operand-list [opt]
12192 : asm-operand-list [opt] ) ; */
12195 cp_parser_asm_definition (cp_parser* parser)
12198 tree outputs = NULL_TREE;
12199 tree inputs = NULL_TREE;
12200 tree clobbers = NULL_TREE;
12202 bool volatile_p = false;
12203 bool extended_p = false;
12204 bool invalid_inputs_p = false;
12205 bool invalid_outputs_p = false;
12207 /* Look for the `asm' keyword. */
12208 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12209 /* See if the next token is `volatile'. */
12210 if (cp_parser_allow_gnu_extensions_p (parser)
12211 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12213 /* Remember that we saw the `volatile' keyword. */
12215 /* Consume the token. */
12216 cp_lexer_consume_token (parser->lexer);
12218 /* Look for the opening `('. */
12219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12221 /* Look for the string. */
12222 string = cp_parser_string_literal (parser, false, false);
12223 if (string == error_mark_node)
12225 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12226 /*consume_paren=*/true);
12230 /* If we're allowing GNU extensions, check for the extended assembly
12231 syntax. Unfortunately, the `:' tokens need not be separated by
12232 a space in C, and so, for compatibility, we tolerate that here
12233 too. Doing that means that we have to treat the `::' operator as
12235 if (cp_parser_allow_gnu_extensions_p (parser)
12236 && parser->in_function_body
12237 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12238 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12240 bool inputs_p = false;
12241 bool clobbers_p = false;
12243 /* The extended syntax was used. */
12246 /* Look for outputs. */
12247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12249 /* Consume the `:'. */
12250 cp_lexer_consume_token (parser->lexer);
12251 /* Parse the output-operands. */
12252 if (cp_lexer_next_token_is_not (parser->lexer,
12254 && cp_lexer_next_token_is_not (parser->lexer,
12256 && cp_lexer_next_token_is_not (parser->lexer,
12258 outputs = cp_parser_asm_operand_list (parser);
12260 if (outputs == error_mark_node)
12261 invalid_outputs_p = true;
12263 /* If the next token is `::', there are no outputs, and the
12264 next token is the beginning of the inputs. */
12265 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12266 /* The inputs are coming next. */
12269 /* Look for inputs. */
12271 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12273 /* Consume the `:' or `::'. */
12274 cp_lexer_consume_token (parser->lexer);
12275 /* Parse the output-operands. */
12276 if (cp_lexer_next_token_is_not (parser->lexer,
12278 && cp_lexer_next_token_is_not (parser->lexer,
12280 inputs = cp_parser_asm_operand_list (parser);
12282 if (inputs == error_mark_node)
12283 invalid_inputs_p = true;
12285 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12286 /* The clobbers are coming next. */
12289 /* Look for clobbers. */
12291 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12293 /* Consume the `:' or `::'. */
12294 cp_lexer_consume_token (parser->lexer);
12295 /* Parse the clobbers. */
12296 if (cp_lexer_next_token_is_not (parser->lexer,
12298 clobbers = cp_parser_asm_clobber_list (parser);
12301 /* Look for the closing `)'. */
12302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12303 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12304 /*consume_paren=*/true);
12305 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12307 if (!invalid_inputs_p && !invalid_outputs_p)
12309 /* Create the ASM_EXPR. */
12310 if (parser->in_function_body)
12312 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12314 /* If the extended syntax was not used, mark the ASM_EXPR. */
12317 tree temp = asm_stmt;
12318 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12319 temp = TREE_OPERAND (temp, 0);
12321 ASM_INPUT_P (temp) = 1;
12325 cgraph_add_asm_node (string);
12329 /* Declarators [gram.dcl.decl] */
12331 /* Parse an init-declarator.
12334 declarator initializer [opt]
12339 declarator asm-specification [opt] attributes [opt] initializer [opt]
12341 function-definition:
12342 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12344 decl-specifier-seq [opt] declarator function-try-block
12348 function-definition:
12349 __extension__ function-definition
12351 The DECL_SPECIFIERS apply to this declarator. Returns a
12352 representation of the entity declared. If MEMBER_P is TRUE, then
12353 this declarator appears in a class scope. The new DECL created by
12354 this declarator is returned.
12356 The CHECKS are access checks that should be performed once we know
12357 what entity is being declared (and, therefore, what classes have
12360 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12361 for a function-definition here as well. If the declarator is a
12362 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12363 be TRUE upon return. By that point, the function-definition will
12364 have been completely parsed.
12366 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12370 cp_parser_init_declarator (cp_parser* parser,
12371 cp_decl_specifier_seq *decl_specifiers,
12372 VEC (deferred_access_check,gc)* checks,
12373 bool function_definition_allowed_p,
12375 int declares_class_or_enum,
12376 bool* function_definition_p)
12378 cp_token *token = NULL, *asm_spec_start_token = NULL,
12379 *attributes_start_token = NULL;
12380 cp_declarator *declarator;
12381 tree prefix_attributes;
12383 tree asm_specification;
12385 tree decl = NULL_TREE;
12387 int is_initialized;
12388 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12389 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12391 enum cpp_ttype initialization_kind;
12392 bool is_direct_init = false;
12393 bool is_non_constant_init;
12394 int ctor_dtor_or_conv_p;
12396 tree pushed_scope = NULL;
12398 /* Gather the attributes that were provided with the
12399 decl-specifiers. */
12400 prefix_attributes = decl_specifiers->attributes;
12402 /* Assume that this is not the declarator for a function
12404 if (function_definition_p)
12405 *function_definition_p = false;
12407 /* Defer access checks while parsing the declarator; we cannot know
12408 what names are accessible until we know what is being
12410 resume_deferring_access_checks ();
12412 /* Parse the declarator. */
12413 token = cp_lexer_peek_token (parser->lexer);
12415 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12416 &ctor_dtor_or_conv_p,
12417 /*parenthesized_p=*/NULL,
12418 /*member_p=*/false);
12419 /* Gather up the deferred checks. */
12420 stop_deferring_access_checks ();
12422 /* If the DECLARATOR was erroneous, there's no need to go
12424 if (declarator == cp_error_declarator)
12425 return error_mark_node;
12427 /* Check that the number of template-parameter-lists is OK. */
12428 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12430 return error_mark_node;
12432 if (declares_class_or_enum & 2)
12433 cp_parser_check_for_definition_in_return_type (declarator,
12434 decl_specifiers->type,
12435 decl_specifiers->type_location);
12437 /* Figure out what scope the entity declared by the DECLARATOR is
12438 located in. `grokdeclarator' sometimes changes the scope, so
12439 we compute it now. */
12440 scope = get_scope_of_declarator (declarator);
12442 /* If we're allowing GNU extensions, look for an asm-specification
12444 if (cp_parser_allow_gnu_extensions_p (parser))
12446 /* Look for an asm-specification. */
12447 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12448 asm_specification = cp_parser_asm_specification_opt (parser);
12449 /* And attributes. */
12450 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12451 attributes = cp_parser_attributes_opt (parser);
12455 asm_specification = NULL_TREE;
12456 attributes = NULL_TREE;
12459 /* Peek at the next token. */
12460 token = cp_lexer_peek_token (parser->lexer);
12461 /* Check to see if the token indicates the start of a
12462 function-definition. */
12463 if (function_declarator_p (declarator)
12464 && cp_parser_token_starts_function_definition_p (token))
12466 if (!function_definition_allowed_p)
12468 /* If a function-definition should not appear here, issue an
12470 cp_parser_error (parser,
12471 "a function-definition is not allowed here");
12472 return error_mark_node;
12476 /* Neither attributes nor an asm-specification are allowed
12477 on a function-definition. */
12478 if (asm_specification)
12479 error ("%Han asm-specification is not allowed "
12480 "on a function-definition",
12481 &asm_spec_start_token->location);
12483 error ("%Hattributes are not allowed on a function-definition",
12484 &attributes_start_token->location);
12485 /* This is a function-definition. */
12486 *function_definition_p = true;
12488 /* Parse the function definition. */
12490 decl = cp_parser_save_member_function_body (parser,
12493 prefix_attributes);
12496 = (cp_parser_function_definition_from_specifiers_and_declarator
12497 (parser, decl_specifiers, prefix_attributes, declarator));
12505 Only in function declarations for constructors, destructors, and
12506 type conversions can the decl-specifier-seq be omitted.
12508 We explicitly postpone this check past the point where we handle
12509 function-definitions because we tolerate function-definitions
12510 that are missing their return types in some modes. */
12511 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12513 cp_parser_error (parser,
12514 "expected constructor, destructor, or type conversion");
12515 return error_mark_node;
12518 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12519 if (token->type == CPP_EQ
12520 || token->type == CPP_OPEN_PAREN
12521 || token->type == CPP_OPEN_BRACE)
12523 is_initialized = 1;
12524 initialization_kind = token->type;
12526 if (token->type == CPP_EQ
12527 && function_declarator_p (declarator))
12529 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12530 if (t2->keyword == RID_DEFAULT)
12531 is_initialized = 2;
12532 else if (t2->keyword == RID_DELETE)
12533 is_initialized = 3;
12538 /* If the init-declarator isn't initialized and isn't followed by a
12539 `,' or `;', it's not a valid init-declarator. */
12540 if (token->type != CPP_COMMA
12541 && token->type != CPP_SEMICOLON)
12543 cp_parser_error (parser, "expected initializer");
12544 return error_mark_node;
12546 is_initialized = 0;
12547 initialization_kind = CPP_EOF;
12550 /* Because start_decl has side-effects, we should only call it if we
12551 know we're going ahead. By this point, we know that we cannot
12552 possibly be looking at any other construct. */
12553 cp_parser_commit_to_tentative_parse (parser);
12555 /* If the decl specifiers were bad, issue an error now that we're
12556 sure this was intended to be a declarator. Then continue
12557 declaring the variable(s), as int, to try to cut down on further
12559 if (decl_specifiers->any_specifiers_p
12560 && decl_specifiers->type == error_mark_node)
12562 cp_parser_error (parser, "invalid type in declaration");
12563 decl_specifiers->type = integer_type_node;
12566 /* Check to see whether or not this declaration is a friend. */
12567 friend_p = cp_parser_friend_p (decl_specifiers);
12569 /* Enter the newly declared entry in the symbol table. If we're
12570 processing a declaration in a class-specifier, we wait until
12571 after processing the initializer. */
12574 if (parser->in_unbraced_linkage_specification_p)
12575 decl_specifiers->storage_class = sc_extern;
12576 decl = start_decl (declarator, decl_specifiers,
12577 is_initialized, attributes, prefix_attributes,
12581 /* Enter the SCOPE. That way unqualified names appearing in the
12582 initializer will be looked up in SCOPE. */
12583 pushed_scope = push_scope (scope);
12585 /* Perform deferred access control checks, now that we know in which
12586 SCOPE the declared entity resides. */
12587 if (!member_p && decl)
12589 tree saved_current_function_decl = NULL_TREE;
12591 /* If the entity being declared is a function, pretend that we
12592 are in its scope. If it is a `friend', it may have access to
12593 things that would not otherwise be accessible. */
12594 if (TREE_CODE (decl) == FUNCTION_DECL)
12596 saved_current_function_decl = current_function_decl;
12597 current_function_decl = decl;
12600 /* Perform access checks for template parameters. */
12601 cp_parser_perform_template_parameter_access_checks (checks);
12603 /* Perform the access control checks for the declarator and the
12604 decl-specifiers. */
12605 perform_deferred_access_checks ();
12607 /* Restore the saved value. */
12608 if (TREE_CODE (decl) == FUNCTION_DECL)
12609 current_function_decl = saved_current_function_decl;
12612 /* Parse the initializer. */
12613 initializer = NULL_TREE;
12614 is_direct_init = false;
12615 is_non_constant_init = true;
12616 if (is_initialized)
12618 if (function_declarator_p (declarator))
12620 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12621 if (initialization_kind == CPP_EQ)
12622 initializer = cp_parser_pure_specifier (parser);
12625 /* If the declaration was erroneous, we don't really
12626 know what the user intended, so just silently
12627 consume the initializer. */
12628 if (decl != error_mark_node)
12629 error ("%Hinitializer provided for function",
12630 &initializer_start_token->location);
12631 cp_parser_skip_to_closing_parenthesis (parser,
12632 /*recovering=*/true,
12633 /*or_comma=*/false,
12634 /*consume_paren=*/true);
12638 initializer = cp_parser_initializer (parser,
12640 &is_non_constant_init);
12643 /* The old parser allows attributes to appear after a parenthesized
12644 initializer. Mark Mitchell proposed removing this functionality
12645 on the GCC mailing lists on 2002-08-13. This parser accepts the
12646 attributes -- but ignores them. */
12647 if (cp_parser_allow_gnu_extensions_p (parser)
12648 && initialization_kind == CPP_OPEN_PAREN)
12649 if (cp_parser_attributes_opt (parser))
12650 warning (OPT_Wattributes,
12651 "attributes after parenthesized initializer ignored");
12653 /* For an in-class declaration, use `grokfield' to create the
12659 pop_scope (pushed_scope);
12660 pushed_scope = false;
12662 decl = grokfield (declarator, decl_specifiers,
12663 initializer, !is_non_constant_init,
12664 /*asmspec=*/NULL_TREE,
12665 prefix_attributes);
12666 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12667 cp_parser_save_default_args (parser, decl);
12670 /* Finish processing the declaration. But, skip friend
12672 if (!friend_p && decl && decl != error_mark_node)
12674 cp_finish_decl (decl,
12675 initializer, !is_non_constant_init,
12677 /* If the initializer is in parentheses, then this is
12678 a direct-initialization, which means that an
12679 `explicit' constructor is OK. Otherwise, an
12680 `explicit' constructor cannot be used. */
12681 ((is_direct_init || !is_initialized)
12682 ? 0 : LOOKUP_ONLYCONVERTING));
12684 else if ((cxx_dialect != cxx98) && friend_p
12685 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12686 /* Core issue #226 (C++0x only): A default template-argument
12687 shall not be specified in a friend class template
12689 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12690 /*is_partial=*/0, /*is_friend_decl=*/1);
12692 if (!friend_p && pushed_scope)
12693 pop_scope (pushed_scope);
12698 /* Parse a declarator.
12702 ptr-operator declarator
12704 abstract-declarator:
12705 ptr-operator abstract-declarator [opt]
12706 direct-abstract-declarator
12711 attributes [opt] direct-declarator
12712 attributes [opt] ptr-operator declarator
12714 abstract-declarator:
12715 attributes [opt] ptr-operator abstract-declarator [opt]
12716 attributes [opt] direct-abstract-declarator
12718 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12719 detect constructor, destructor or conversion operators. It is set
12720 to -1 if the declarator is a name, and +1 if it is a
12721 function. Otherwise it is set to zero. Usually you just want to
12722 test for >0, but internally the negative value is used.
12724 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12725 a decl-specifier-seq unless it declares a constructor, destructor,
12726 or conversion. It might seem that we could check this condition in
12727 semantic analysis, rather than parsing, but that makes it difficult
12728 to handle something like `f()'. We want to notice that there are
12729 no decl-specifiers, and therefore realize that this is an
12730 expression, not a declaration.)
12732 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12733 the declarator is a direct-declarator of the form "(...)".
12735 MEMBER_P is true iff this declarator is a member-declarator. */
12737 static cp_declarator *
12738 cp_parser_declarator (cp_parser* parser,
12739 cp_parser_declarator_kind dcl_kind,
12740 int* ctor_dtor_or_conv_p,
12741 bool* parenthesized_p,
12745 cp_declarator *declarator;
12746 enum tree_code code;
12747 cp_cv_quals cv_quals;
12749 tree attributes = NULL_TREE;
12751 /* Assume this is not a constructor, destructor, or type-conversion
12753 if (ctor_dtor_or_conv_p)
12754 *ctor_dtor_or_conv_p = 0;
12756 if (cp_parser_allow_gnu_extensions_p (parser))
12757 attributes = cp_parser_attributes_opt (parser);
12759 /* Peek at the next token. */
12760 token = cp_lexer_peek_token (parser->lexer);
12762 /* Check for the ptr-operator production. */
12763 cp_parser_parse_tentatively (parser);
12764 /* Parse the ptr-operator. */
12765 code = cp_parser_ptr_operator (parser,
12768 /* If that worked, then we have a ptr-operator. */
12769 if (cp_parser_parse_definitely (parser))
12771 /* If a ptr-operator was found, then this declarator was not
12773 if (parenthesized_p)
12774 *parenthesized_p = true;
12775 /* The dependent declarator is optional if we are parsing an
12776 abstract-declarator. */
12777 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12778 cp_parser_parse_tentatively (parser);
12780 /* Parse the dependent declarator. */
12781 declarator = cp_parser_declarator (parser, dcl_kind,
12782 /*ctor_dtor_or_conv_p=*/NULL,
12783 /*parenthesized_p=*/NULL,
12784 /*member_p=*/false);
12786 /* If we are parsing an abstract-declarator, we must handle the
12787 case where the dependent declarator is absent. */
12788 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12789 && !cp_parser_parse_definitely (parser))
12792 declarator = cp_parser_make_indirect_declarator
12793 (code, class_type, cv_quals, declarator);
12795 /* Everything else is a direct-declarator. */
12798 if (parenthesized_p)
12799 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12801 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12802 ctor_dtor_or_conv_p,
12806 if (attributes && declarator && declarator != cp_error_declarator)
12807 declarator->attributes = attributes;
12812 /* Parse a direct-declarator or direct-abstract-declarator.
12816 direct-declarator ( parameter-declaration-clause )
12817 cv-qualifier-seq [opt]
12818 exception-specification [opt]
12819 direct-declarator [ constant-expression [opt] ]
12822 direct-abstract-declarator:
12823 direct-abstract-declarator [opt]
12824 ( parameter-declaration-clause )
12825 cv-qualifier-seq [opt]
12826 exception-specification [opt]
12827 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12828 ( abstract-declarator )
12830 Returns a representation of the declarator. DCL_KIND is
12831 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12832 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12833 we are parsing a direct-declarator. It is
12834 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12835 of ambiguity we prefer an abstract declarator, as per
12836 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12837 cp_parser_declarator. */
12839 static cp_declarator *
12840 cp_parser_direct_declarator (cp_parser* parser,
12841 cp_parser_declarator_kind dcl_kind,
12842 int* ctor_dtor_or_conv_p,
12846 cp_declarator *declarator = NULL;
12847 tree scope = NULL_TREE;
12848 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12849 bool saved_in_declarator_p = parser->in_declarator_p;
12851 tree pushed_scope = NULL_TREE;
12855 /* Peek at the next token. */
12856 token = cp_lexer_peek_token (parser->lexer);
12857 if (token->type == CPP_OPEN_PAREN)
12859 /* This is either a parameter-declaration-clause, or a
12860 parenthesized declarator. When we know we are parsing a
12861 named declarator, it must be a parenthesized declarator
12862 if FIRST is true. For instance, `(int)' is a
12863 parameter-declaration-clause, with an omitted
12864 direct-abstract-declarator. But `((*))', is a
12865 parenthesized abstract declarator. Finally, when T is a
12866 template parameter `(T)' is a
12867 parameter-declaration-clause, and not a parenthesized
12870 We first try and parse a parameter-declaration-clause,
12871 and then try a nested declarator (if FIRST is true).
12873 It is not an error for it not to be a
12874 parameter-declaration-clause, even when FIRST is
12880 The first is the declaration of a function while the
12881 second is the definition of a variable, including its
12884 Having seen only the parenthesis, we cannot know which of
12885 these two alternatives should be selected. Even more
12886 complex are examples like:
12891 The former is a function-declaration; the latter is a
12892 variable initialization.
12894 Thus again, we try a parameter-declaration-clause, and if
12895 that fails, we back out and return. */
12897 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12899 cp_parameter_declarator *params;
12900 unsigned saved_num_template_parameter_lists;
12902 /* In a member-declarator, the only valid interpretation
12903 of a parenthesis is the start of a
12904 parameter-declaration-clause. (It is invalid to
12905 initialize a static data member with a parenthesized
12906 initializer; only the "=" form of initialization is
12909 cp_parser_parse_tentatively (parser);
12911 /* Consume the `('. */
12912 cp_lexer_consume_token (parser->lexer);
12915 /* If this is going to be an abstract declarator, we're
12916 in a declarator and we can't have default args. */
12917 parser->default_arg_ok_p = false;
12918 parser->in_declarator_p = true;
12921 /* Inside the function parameter list, surrounding
12922 template-parameter-lists do not apply. */
12923 saved_num_template_parameter_lists
12924 = parser->num_template_parameter_lists;
12925 parser->num_template_parameter_lists = 0;
12927 /* Parse the parameter-declaration-clause. */
12928 params = cp_parser_parameter_declaration_clause (parser);
12930 parser->num_template_parameter_lists
12931 = saved_num_template_parameter_lists;
12933 /* If all went well, parse the cv-qualifier-seq and the
12934 exception-specification. */
12935 if (member_p || cp_parser_parse_definitely (parser))
12937 cp_cv_quals cv_quals;
12938 tree exception_specification;
12940 if (ctor_dtor_or_conv_p)
12941 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12943 /* Consume the `)'. */
12944 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12946 /* Parse the cv-qualifier-seq. */
12947 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12948 /* And the exception-specification. */
12949 exception_specification
12950 = cp_parser_exception_specification_opt (parser);
12952 /* Create the function-declarator. */
12953 declarator = make_call_declarator (declarator,
12956 exception_specification);
12957 /* Any subsequent parameter lists are to do with
12958 return type, so are not those of the declared
12960 parser->default_arg_ok_p = false;
12962 /* Repeat the main loop. */
12967 /* If this is the first, we can try a parenthesized
12971 bool saved_in_type_id_in_expr_p;
12973 parser->default_arg_ok_p = saved_default_arg_ok_p;
12974 parser->in_declarator_p = saved_in_declarator_p;
12976 /* Consume the `('. */
12977 cp_lexer_consume_token (parser->lexer);
12978 /* Parse the nested declarator. */
12979 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12980 parser->in_type_id_in_expr_p = true;
12982 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12983 /*parenthesized_p=*/NULL,
12985 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12987 /* Expect a `)'. */
12988 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12989 declarator = cp_error_declarator;
12990 if (declarator == cp_error_declarator)
12993 goto handle_declarator;
12995 /* Otherwise, we must be done. */
12999 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13000 && token->type == CPP_OPEN_SQUARE)
13002 /* Parse an array-declarator. */
13005 if (ctor_dtor_or_conv_p)
13006 *ctor_dtor_or_conv_p = 0;
13009 parser->default_arg_ok_p = false;
13010 parser->in_declarator_p = true;
13011 /* Consume the `['. */
13012 cp_lexer_consume_token (parser->lexer);
13013 /* Peek at the next token. */
13014 token = cp_lexer_peek_token (parser->lexer);
13015 /* If the next token is `]', then there is no
13016 constant-expression. */
13017 if (token->type != CPP_CLOSE_SQUARE)
13019 bool non_constant_p;
13022 = cp_parser_constant_expression (parser,
13023 /*allow_non_constant=*/true,
13025 if (!non_constant_p)
13026 bounds = fold_non_dependent_expr (bounds);
13027 /* Normally, the array bound must be an integral constant
13028 expression. However, as an extension, we allow VLAs
13029 in function scopes. */
13030 else if (!parser->in_function_body)
13032 error ("%Harray bound is not an integer constant",
13034 bounds = error_mark_node;
13038 bounds = NULL_TREE;
13039 /* Look for the closing `]'. */
13040 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13042 declarator = cp_error_declarator;
13046 declarator = make_array_declarator (declarator, bounds);
13048 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13050 tree qualifying_scope;
13051 tree unqualified_name;
13052 special_function_kind sfk;
13054 bool pack_expansion_p = false;
13055 cp_token *declarator_id_start_token;
13057 /* Parse a declarator-id */
13058 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13061 cp_parser_parse_tentatively (parser);
13063 /* If we see an ellipsis, we should be looking at a
13065 if (token->type == CPP_ELLIPSIS)
13067 /* Consume the `...' */
13068 cp_lexer_consume_token (parser->lexer);
13070 pack_expansion_p = true;
13074 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13076 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13077 qualifying_scope = parser->scope;
13082 if (!unqualified_name && pack_expansion_p)
13084 /* Check whether an error occurred. */
13085 okay = !cp_parser_error_occurred (parser);
13087 /* We already consumed the ellipsis to mark a
13088 parameter pack, but we have no way to report it,
13089 so abort the tentative parse. We will be exiting
13090 immediately anyway. */
13091 cp_parser_abort_tentative_parse (parser);
13094 okay = cp_parser_parse_definitely (parser);
13097 unqualified_name = error_mark_node;
13098 else if (unqualified_name
13099 && (qualifying_scope
13100 || (TREE_CODE (unqualified_name)
13101 != IDENTIFIER_NODE)))
13103 cp_parser_error (parser, "expected unqualified-id");
13104 unqualified_name = error_mark_node;
13108 if (!unqualified_name)
13110 if (unqualified_name == error_mark_node)
13112 declarator = cp_error_declarator;
13113 pack_expansion_p = false;
13114 declarator->parameter_pack_p = false;
13118 if (qualifying_scope && at_namespace_scope_p ()
13119 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13121 /* In the declaration of a member of a template class
13122 outside of the class itself, the SCOPE will sometimes
13123 be a TYPENAME_TYPE. For example, given:
13125 template <typename T>
13126 int S<T>::R::i = 3;
13128 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13129 this context, we must resolve S<T>::R to an ordinary
13130 type, rather than a typename type.
13132 The reason we normally avoid resolving TYPENAME_TYPEs
13133 is that a specialization of `S' might render
13134 `S<T>::R' not a type. However, if `S' is
13135 specialized, then this `i' will not be used, so there
13136 is no harm in resolving the types here. */
13139 /* Resolve the TYPENAME_TYPE. */
13140 type = resolve_typename_type (qualifying_scope,
13141 /*only_current_p=*/false);
13142 /* If that failed, the declarator is invalid. */
13143 if (TREE_CODE (type) == TYPENAME_TYPE)
13144 error ("%H%<%T::%E%> is not a type",
13145 &declarator_id_start_token->location,
13146 TYPE_CONTEXT (qualifying_scope),
13147 TYPE_IDENTIFIER (qualifying_scope));
13148 qualifying_scope = type;
13153 if (unqualified_name)
13157 if (qualifying_scope
13158 && CLASS_TYPE_P (qualifying_scope))
13159 class_type = qualifying_scope;
13161 class_type = current_class_type;
13163 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13165 tree name_type = TREE_TYPE (unqualified_name);
13166 if (class_type && same_type_p (name_type, class_type))
13168 if (qualifying_scope
13169 && CLASSTYPE_USE_TEMPLATE (name_type))
13171 error ("%Hinvalid use of constructor as a template",
13172 &declarator_id_start_token->location);
13173 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13174 "name the constructor in a qualified name",
13176 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13177 class_type, name_type);
13178 declarator = cp_error_declarator;
13182 unqualified_name = constructor_name (class_type);
13186 /* We do not attempt to print the declarator
13187 here because we do not have enough
13188 information about its original syntactic
13190 cp_parser_error (parser, "invalid declarator");
13191 declarator = cp_error_declarator;
13198 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13199 sfk = sfk_destructor;
13200 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13201 sfk = sfk_conversion;
13202 else if (/* There's no way to declare a constructor
13203 for an anonymous type, even if the type
13204 got a name for linkage purposes. */
13205 !TYPE_WAS_ANONYMOUS (class_type)
13206 && constructor_name_p (unqualified_name,
13209 unqualified_name = constructor_name (class_type);
13210 sfk = sfk_constructor;
13213 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13214 *ctor_dtor_or_conv_p = -1;
13217 declarator = make_id_declarator (qualifying_scope,
13220 declarator->id_loc = token->location;
13221 declarator->parameter_pack_p = pack_expansion_p;
13223 if (pack_expansion_p)
13224 maybe_warn_variadic_templates ();
13226 handle_declarator:;
13227 scope = get_scope_of_declarator (declarator);
13229 /* Any names that appear after the declarator-id for a
13230 member are looked up in the containing scope. */
13231 pushed_scope = push_scope (scope);
13232 parser->in_declarator_p = true;
13233 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13234 || (declarator && declarator->kind == cdk_id))
13235 /* Default args are only allowed on function
13237 parser->default_arg_ok_p = saved_default_arg_ok_p;
13239 parser->default_arg_ok_p = false;
13248 /* For an abstract declarator, we might wind up with nothing at this
13249 point. That's an error; the declarator is not optional. */
13251 cp_parser_error (parser, "expected declarator");
13253 /* If we entered a scope, we must exit it now. */
13255 pop_scope (pushed_scope);
13257 parser->default_arg_ok_p = saved_default_arg_ok_p;
13258 parser->in_declarator_p = saved_in_declarator_p;
13263 /* Parse a ptr-operator.
13266 * cv-qualifier-seq [opt]
13268 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13273 & cv-qualifier-seq [opt]
13275 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13276 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13277 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13278 filled in with the TYPE containing the member. *CV_QUALS is
13279 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13280 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13281 Note that the tree codes returned by this function have nothing
13282 to do with the types of trees that will be eventually be created
13283 to represent the pointer or reference type being parsed. They are
13284 just constants with suggestive names. */
13285 static enum tree_code
13286 cp_parser_ptr_operator (cp_parser* parser,
13288 cp_cv_quals *cv_quals)
13290 enum tree_code code = ERROR_MARK;
13293 /* Assume that it's not a pointer-to-member. */
13295 /* And that there are no cv-qualifiers. */
13296 *cv_quals = TYPE_UNQUALIFIED;
13298 /* Peek at the next token. */
13299 token = cp_lexer_peek_token (parser->lexer);
13301 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13302 if (token->type == CPP_MULT)
13303 code = INDIRECT_REF;
13304 else if (token->type == CPP_AND)
13306 else if ((cxx_dialect != cxx98) &&
13307 token->type == CPP_AND_AND) /* C++0x only */
13308 code = NON_LVALUE_EXPR;
13310 if (code != ERROR_MARK)
13312 /* Consume the `*', `&' or `&&'. */
13313 cp_lexer_consume_token (parser->lexer);
13315 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13316 `&', if we are allowing GNU extensions. (The only qualifier
13317 that can legally appear after `&' is `restrict', but that is
13318 enforced during semantic analysis. */
13319 if (code == INDIRECT_REF
13320 || cp_parser_allow_gnu_extensions_p (parser))
13321 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13325 /* Try the pointer-to-member case. */
13326 cp_parser_parse_tentatively (parser);
13327 /* Look for the optional `::' operator. */
13328 cp_parser_global_scope_opt (parser,
13329 /*current_scope_valid_p=*/false);
13330 /* Look for the nested-name specifier. */
13331 token = cp_lexer_peek_token (parser->lexer);
13332 cp_parser_nested_name_specifier (parser,
13333 /*typename_keyword_p=*/false,
13334 /*check_dependency_p=*/true,
13336 /*is_declaration=*/false);
13337 /* If we found it, and the next token is a `*', then we are
13338 indeed looking at a pointer-to-member operator. */
13339 if (!cp_parser_error_occurred (parser)
13340 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13342 /* Indicate that the `*' operator was used. */
13343 code = INDIRECT_REF;
13345 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13346 error ("%H%qD is a namespace", &token->location, parser->scope);
13349 /* The type of which the member is a member is given by the
13351 *type = parser->scope;
13352 /* The next name will not be qualified. */
13353 parser->scope = NULL_TREE;
13354 parser->qualifying_scope = NULL_TREE;
13355 parser->object_scope = NULL_TREE;
13356 /* Look for the optional cv-qualifier-seq. */
13357 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13360 /* If that didn't work we don't have a ptr-operator. */
13361 if (!cp_parser_parse_definitely (parser))
13362 cp_parser_error (parser, "expected ptr-operator");
13368 /* Parse an (optional) cv-qualifier-seq.
13371 cv-qualifier cv-qualifier-seq [opt]
13382 Returns a bitmask representing the cv-qualifiers. */
13385 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13387 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13392 cp_cv_quals cv_qualifier;
13394 /* Peek at the next token. */
13395 token = cp_lexer_peek_token (parser->lexer);
13396 /* See if it's a cv-qualifier. */
13397 switch (token->keyword)
13400 cv_qualifier = TYPE_QUAL_CONST;
13404 cv_qualifier = TYPE_QUAL_VOLATILE;
13408 cv_qualifier = TYPE_QUAL_RESTRICT;
13412 cv_qualifier = TYPE_UNQUALIFIED;
13419 if (cv_quals & cv_qualifier)
13421 error ("%Hduplicate cv-qualifier", &token->location);
13422 cp_lexer_purge_token (parser->lexer);
13426 cp_lexer_consume_token (parser->lexer);
13427 cv_quals |= cv_qualifier;
13434 /* Parse a declarator-id.
13438 :: [opt] nested-name-specifier [opt] type-name
13440 In the `id-expression' case, the value returned is as for
13441 cp_parser_id_expression if the id-expression was an unqualified-id.
13442 If the id-expression was a qualified-id, then a SCOPE_REF is
13443 returned. The first operand is the scope (either a NAMESPACE_DECL
13444 or TREE_TYPE), but the second is still just a representation of an
13448 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13451 /* The expression must be an id-expression. Assume that qualified
13452 names are the names of types so that:
13455 int S<T>::R::i = 3;
13457 will work; we must treat `S<T>::R' as the name of a type.
13458 Similarly, assume that qualified names are templates, where
13462 int S<T>::R<T>::i = 3;
13465 id = cp_parser_id_expression (parser,
13466 /*template_keyword_p=*/false,
13467 /*check_dependency_p=*/false,
13468 /*template_p=*/NULL,
13469 /*declarator_p=*/true,
13471 if (id && BASELINK_P (id))
13472 id = BASELINK_FUNCTIONS (id);
13476 /* Parse a type-id.
13479 type-specifier-seq abstract-declarator [opt]
13481 Returns the TYPE specified. */
13484 cp_parser_type_id (cp_parser* parser)
13486 cp_decl_specifier_seq type_specifier_seq;
13487 cp_declarator *abstract_declarator;
13489 /* Parse the type-specifier-seq. */
13490 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13491 &type_specifier_seq);
13492 if (type_specifier_seq.type == error_mark_node)
13493 return error_mark_node;
13495 /* There might or might not be an abstract declarator. */
13496 cp_parser_parse_tentatively (parser);
13497 /* Look for the declarator. */
13498 abstract_declarator
13499 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13500 /*parenthesized_p=*/NULL,
13501 /*member_p=*/false);
13502 /* Check to see if there really was a declarator. */
13503 if (!cp_parser_parse_definitely (parser))
13504 abstract_declarator = NULL;
13506 return groktypename (&type_specifier_seq, abstract_declarator);
13509 /* Parse a type-specifier-seq.
13511 type-specifier-seq:
13512 type-specifier type-specifier-seq [opt]
13516 type-specifier-seq:
13517 attributes type-specifier-seq [opt]
13519 If IS_CONDITION is true, we are at the start of a "condition",
13520 e.g., we've just seen "if (".
13522 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13525 cp_parser_type_specifier_seq (cp_parser* parser,
13527 cp_decl_specifier_seq *type_specifier_seq)
13529 bool seen_type_specifier = false;
13530 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13531 cp_token *start_token = NULL;
13533 /* Clear the TYPE_SPECIFIER_SEQ. */
13534 clear_decl_specs (type_specifier_seq);
13536 /* Parse the type-specifiers and attributes. */
13539 tree type_specifier;
13540 bool is_cv_qualifier;
13542 /* Check for attributes first. */
13543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13545 type_specifier_seq->attributes =
13546 chainon (type_specifier_seq->attributes,
13547 cp_parser_attributes_opt (parser));
13551 /* record the token of the beginning of the type specifier seq,
13552 for error reporting purposes*/
13554 start_token = cp_lexer_peek_token (parser->lexer);
13556 /* Look for the type-specifier. */
13557 type_specifier = cp_parser_type_specifier (parser,
13559 type_specifier_seq,
13560 /*is_declaration=*/false,
13563 if (!type_specifier)
13565 /* If the first type-specifier could not be found, this is not a
13566 type-specifier-seq at all. */
13567 if (!seen_type_specifier)
13569 cp_parser_error (parser, "expected type-specifier");
13570 type_specifier_seq->type = error_mark_node;
13573 /* If subsequent type-specifiers could not be found, the
13574 type-specifier-seq is complete. */
13578 seen_type_specifier = true;
13579 /* The standard says that a condition can be:
13581 type-specifier-seq declarator = assignment-expression
13588 we should treat the "S" as a declarator, not as a
13589 type-specifier. The standard doesn't say that explicitly for
13590 type-specifier-seq, but it does say that for
13591 decl-specifier-seq in an ordinary declaration. Perhaps it
13592 would be clearer just to allow a decl-specifier-seq here, and
13593 then add a semantic restriction that if any decl-specifiers
13594 that are not type-specifiers appear, the program is invalid. */
13595 if (is_condition && !is_cv_qualifier)
13596 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13599 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13602 /* Parse a parameter-declaration-clause.
13604 parameter-declaration-clause:
13605 parameter-declaration-list [opt] ... [opt]
13606 parameter-declaration-list , ...
13608 Returns a representation for the parameter declarations. A return
13609 value of NULL indicates a parameter-declaration-clause consisting
13610 only of an ellipsis. */
13612 static cp_parameter_declarator *
13613 cp_parser_parameter_declaration_clause (cp_parser* parser)
13615 cp_parameter_declarator *parameters;
13620 /* Peek at the next token. */
13621 token = cp_lexer_peek_token (parser->lexer);
13622 /* Check for trivial parameter-declaration-clauses. */
13623 if (token->type == CPP_ELLIPSIS)
13625 /* Consume the `...' token. */
13626 cp_lexer_consume_token (parser->lexer);
13629 else if (token->type == CPP_CLOSE_PAREN)
13630 /* There are no parameters. */
13632 #ifndef NO_IMPLICIT_EXTERN_C
13633 if (in_system_header && current_class_type == NULL
13634 && current_lang_name == lang_name_c)
13638 return no_parameters;
13640 /* Check for `(void)', too, which is a special case. */
13641 else if (token->keyword == RID_VOID
13642 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13643 == CPP_CLOSE_PAREN))
13645 /* Consume the `void' token. */
13646 cp_lexer_consume_token (parser->lexer);
13647 /* There are no parameters. */
13648 return no_parameters;
13651 /* Parse the parameter-declaration-list. */
13652 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13653 /* If a parse error occurred while parsing the
13654 parameter-declaration-list, then the entire
13655 parameter-declaration-clause is erroneous. */
13659 /* Peek at the next token. */
13660 token = cp_lexer_peek_token (parser->lexer);
13661 /* If it's a `,', the clause should terminate with an ellipsis. */
13662 if (token->type == CPP_COMMA)
13664 /* Consume the `,'. */
13665 cp_lexer_consume_token (parser->lexer);
13666 /* Expect an ellipsis. */
13668 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13670 /* It might also be `...' if the optional trailing `,' was
13672 else if (token->type == CPP_ELLIPSIS)
13674 /* Consume the `...' token. */
13675 cp_lexer_consume_token (parser->lexer);
13676 /* And remember that we saw it. */
13680 ellipsis_p = false;
13682 /* Finish the parameter list. */
13683 if (parameters && ellipsis_p)
13684 parameters->ellipsis_p = true;
13689 /* Parse a parameter-declaration-list.
13691 parameter-declaration-list:
13692 parameter-declaration
13693 parameter-declaration-list , parameter-declaration
13695 Returns a representation of the parameter-declaration-list, as for
13696 cp_parser_parameter_declaration_clause. However, the
13697 `void_list_node' is never appended to the list. Upon return,
13698 *IS_ERROR will be true iff an error occurred. */
13700 static cp_parameter_declarator *
13701 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13703 cp_parameter_declarator *parameters = NULL;
13704 cp_parameter_declarator **tail = ¶meters;
13705 bool saved_in_unbraced_linkage_specification_p;
13707 /* Assume all will go well. */
13709 /* The special considerations that apply to a function within an
13710 unbraced linkage specifications do not apply to the parameters
13711 to the function. */
13712 saved_in_unbraced_linkage_specification_p
13713 = parser->in_unbraced_linkage_specification_p;
13714 parser->in_unbraced_linkage_specification_p = false;
13716 /* Look for more parameters. */
13719 cp_parameter_declarator *parameter;
13720 bool parenthesized_p;
13721 /* Parse the parameter. */
13723 = cp_parser_parameter_declaration (parser,
13724 /*template_parm_p=*/false,
13727 /* If a parse error occurred parsing the parameter declaration,
13728 then the entire parameter-declaration-list is erroneous. */
13735 /* Add the new parameter to the list. */
13737 tail = ¶meter->next;
13739 /* Peek at the next token. */
13740 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13741 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13742 /* These are for Objective-C++ */
13743 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13744 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13745 /* The parameter-declaration-list is complete. */
13747 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13751 /* Peek at the next token. */
13752 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13753 /* If it's an ellipsis, then the list is complete. */
13754 if (token->type == CPP_ELLIPSIS)
13756 /* Otherwise, there must be more parameters. Consume the
13758 cp_lexer_consume_token (parser->lexer);
13759 /* When parsing something like:
13761 int i(float f, double d)
13763 we can tell after seeing the declaration for "f" that we
13764 are not looking at an initialization of a variable "i",
13765 but rather at the declaration of a function "i".
13767 Due to the fact that the parsing of template arguments
13768 (as specified to a template-id) requires backtracking we
13769 cannot use this technique when inside a template argument
13771 if (!parser->in_template_argument_list_p
13772 && !parser->in_type_id_in_expr_p
13773 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13774 /* However, a parameter-declaration of the form
13775 "foat(f)" (which is a valid declaration of a
13776 parameter "f") can also be interpreted as an
13777 expression (the conversion of "f" to "float"). */
13778 && !parenthesized_p)
13779 cp_parser_commit_to_tentative_parse (parser);
13783 cp_parser_error (parser, "expected %<,%> or %<...%>");
13784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13785 cp_parser_skip_to_closing_parenthesis (parser,
13786 /*recovering=*/true,
13787 /*or_comma=*/false,
13788 /*consume_paren=*/false);
13793 parser->in_unbraced_linkage_specification_p
13794 = saved_in_unbraced_linkage_specification_p;
13799 /* Parse a parameter declaration.
13801 parameter-declaration:
13802 decl-specifier-seq ... [opt] declarator
13803 decl-specifier-seq declarator = assignment-expression
13804 decl-specifier-seq ... [opt] abstract-declarator [opt]
13805 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13807 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13808 declares a template parameter. (In that case, a non-nested `>'
13809 token encountered during the parsing of the assignment-expression
13810 is not interpreted as a greater-than operator.)
13812 Returns a representation of the parameter, or NULL if an error
13813 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13814 true iff the declarator is of the form "(p)". */
13816 static cp_parameter_declarator *
13817 cp_parser_parameter_declaration (cp_parser *parser,
13818 bool template_parm_p,
13819 bool *parenthesized_p)
13821 int declares_class_or_enum;
13822 bool greater_than_is_operator_p;
13823 cp_decl_specifier_seq decl_specifiers;
13824 cp_declarator *declarator;
13825 tree default_argument;
13826 cp_token *token = NULL, *declarator_token_start = NULL;
13827 const char *saved_message;
13829 /* In a template parameter, `>' is not an operator.
13833 When parsing a default template-argument for a non-type
13834 template-parameter, the first non-nested `>' is taken as the end
13835 of the template parameter-list rather than a greater-than
13837 greater_than_is_operator_p = !template_parm_p;
13839 /* Type definitions may not appear in parameter types. */
13840 saved_message = parser->type_definition_forbidden_message;
13841 parser->type_definition_forbidden_message
13842 = "types may not be defined in parameter types";
13844 /* Parse the declaration-specifiers. */
13845 cp_parser_decl_specifier_seq (parser,
13846 CP_PARSER_FLAGS_NONE,
13848 &declares_class_or_enum);
13849 /* If an error occurred, there's no reason to attempt to parse the
13850 rest of the declaration. */
13851 if (cp_parser_error_occurred (parser))
13853 parser->type_definition_forbidden_message = saved_message;
13857 /* Peek at the next token. */
13858 token = cp_lexer_peek_token (parser->lexer);
13860 /* If the next token is a `)', `,', `=', `>', or `...', then there
13861 is no declarator. However, when variadic templates are enabled,
13862 there may be a declarator following `...'. */
13863 if (token->type == CPP_CLOSE_PAREN
13864 || token->type == CPP_COMMA
13865 || token->type == CPP_EQ
13866 || token->type == CPP_GREATER)
13869 if (parenthesized_p)
13870 *parenthesized_p = false;
13872 /* Otherwise, there should be a declarator. */
13875 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13876 parser->default_arg_ok_p = false;
13878 /* After seeing a decl-specifier-seq, if the next token is not a
13879 "(", there is no possibility that the code is a valid
13880 expression. Therefore, if parsing tentatively, we commit at
13882 if (!parser->in_template_argument_list_p
13883 /* In an expression context, having seen:
13887 we cannot be sure whether we are looking at a
13888 function-type (taking a "char" as a parameter) or a cast
13889 of some object of type "char" to "int". */
13890 && !parser->in_type_id_in_expr_p
13891 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13892 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13893 cp_parser_commit_to_tentative_parse (parser);
13894 /* Parse the declarator. */
13895 declarator_token_start = token;
13896 declarator = cp_parser_declarator (parser,
13897 CP_PARSER_DECLARATOR_EITHER,
13898 /*ctor_dtor_or_conv_p=*/NULL,
13900 /*member_p=*/false);
13901 parser->default_arg_ok_p = saved_default_arg_ok_p;
13902 /* After the declarator, allow more attributes. */
13903 decl_specifiers.attributes
13904 = chainon (decl_specifiers.attributes,
13905 cp_parser_attributes_opt (parser));
13908 /* If the next token is an ellipsis, and we have not seen a
13909 declarator name, and the type of the declarator contains parameter
13910 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13911 a parameter pack expansion expression. Otherwise, leave the
13912 ellipsis for a C-style variadic function. */
13913 token = cp_lexer_peek_token (parser->lexer);
13914 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13916 tree type = decl_specifiers.type;
13918 if (type && DECL_P (type))
13919 type = TREE_TYPE (type);
13922 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13923 && declarator_can_be_parameter_pack (declarator)
13924 && (!declarator || !declarator->parameter_pack_p)
13925 && uses_parameter_packs (type))
13927 /* Consume the `...'. */
13928 cp_lexer_consume_token (parser->lexer);
13929 maybe_warn_variadic_templates ();
13931 /* Build a pack expansion type */
13933 declarator->parameter_pack_p = true;
13935 decl_specifiers.type = make_pack_expansion (type);
13939 /* The restriction on defining new types applies only to the type
13940 of the parameter, not to the default argument. */
13941 parser->type_definition_forbidden_message = saved_message;
13943 /* If the next token is `=', then process a default argument. */
13944 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13946 /* Consume the `='. */
13947 cp_lexer_consume_token (parser->lexer);
13949 /* If we are defining a class, then the tokens that make up the
13950 default argument must be saved and processed later. */
13951 if (!template_parm_p && at_class_scope_p ()
13952 && TYPE_BEING_DEFINED (current_class_type))
13954 unsigned depth = 0;
13955 int maybe_template_id = 0;
13956 cp_token *first_token;
13959 /* Add tokens until we have processed the entire default
13960 argument. We add the range [first_token, token). */
13961 first_token = cp_lexer_peek_token (parser->lexer);
13966 /* Peek at the next token. */
13967 token = cp_lexer_peek_token (parser->lexer);
13968 /* What we do depends on what token we have. */
13969 switch (token->type)
13971 /* In valid code, a default argument must be
13972 immediately followed by a `,' `)', or `...'. */
13974 if (depth == 0 && maybe_template_id)
13976 /* If we've seen a '<', we might be in a
13977 template-argument-list. Until Core issue 325 is
13978 resolved, we don't know how this situation ought
13979 to be handled, so try to DTRT. We check whether
13980 what comes after the comma is a valid parameter
13981 declaration list. If it is, then the comma ends
13982 the default argument; otherwise the default
13983 argument continues. */
13984 bool error = false;
13986 /* Set ITALP so cp_parser_parameter_declaration_list
13987 doesn't decide to commit to this parse. */
13988 bool saved_italp = parser->in_template_argument_list_p;
13989 parser->in_template_argument_list_p = true;
13991 cp_parser_parse_tentatively (parser);
13992 cp_lexer_consume_token (parser->lexer);
13993 cp_parser_parameter_declaration_list (parser, &error);
13994 if (!cp_parser_error_occurred (parser) && !error)
13996 cp_parser_abort_tentative_parse (parser);
13998 parser->in_template_argument_list_p = saved_italp;
14001 case CPP_CLOSE_PAREN:
14003 /* If we run into a non-nested `;', `}', or `]',
14004 then the code is invalid -- but the default
14005 argument is certainly over. */
14006 case CPP_SEMICOLON:
14007 case CPP_CLOSE_BRACE:
14008 case CPP_CLOSE_SQUARE:
14011 /* Update DEPTH, if necessary. */
14012 else if (token->type == CPP_CLOSE_PAREN
14013 || token->type == CPP_CLOSE_BRACE
14014 || token->type == CPP_CLOSE_SQUARE)
14018 case CPP_OPEN_PAREN:
14019 case CPP_OPEN_SQUARE:
14020 case CPP_OPEN_BRACE:
14026 /* This might be the comparison operator, or it might
14027 start a template argument list. */
14028 ++maybe_template_id;
14032 if (cxx_dialect == cxx98)
14034 /* Fall through for C++0x, which treats the `>>'
14035 operator like two `>' tokens in certain
14041 /* This might be an operator, or it might close a
14042 template argument list. But if a previous '<'
14043 started a template argument list, this will have
14044 closed it, so we can't be in one anymore. */
14045 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14046 if (maybe_template_id < 0)
14047 maybe_template_id = 0;
14051 /* If we run out of tokens, issue an error message. */
14053 case CPP_PRAGMA_EOL:
14054 error ("%Hfile ends in default argument", &token->location);
14060 /* In these cases, we should look for template-ids.
14061 For example, if the default argument is
14062 `X<int, double>()', we need to do name lookup to
14063 figure out whether or not `X' is a template; if
14064 so, the `,' does not end the default argument.
14066 That is not yet done. */
14073 /* If we've reached the end, stop. */
14077 /* Add the token to the token block. */
14078 token = cp_lexer_consume_token (parser->lexer);
14081 /* Create a DEFAULT_ARG to represent the unparsed default
14083 default_argument = make_node (DEFAULT_ARG);
14084 DEFARG_TOKENS (default_argument)
14085 = cp_token_cache_new (first_token, token);
14086 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14088 /* Outside of a class definition, we can just parse the
14089 assignment-expression. */
14092 token = cp_lexer_peek_token (parser->lexer);
14094 = cp_parser_default_argument (parser, template_parm_p);
14097 if (!parser->default_arg_ok_p)
14099 if (flag_permissive)
14100 warning (0, "deprecated use of default argument for parameter of non-function");
14103 error ("%Hdefault arguments are only "
14104 "permitted for function parameters",
14106 default_argument = NULL_TREE;
14109 else if ((declarator && declarator->parameter_pack_p)
14110 || (decl_specifiers.type
14111 && PACK_EXPANSION_P (decl_specifiers.type)))
14113 const char* kind = template_parm_p? "template " : "";
14115 /* Find the name of the parameter pack. */
14116 cp_declarator *id_declarator = declarator;
14117 while (id_declarator && id_declarator->kind != cdk_id)
14118 id_declarator = id_declarator->declarator;
14120 if (id_declarator && id_declarator->kind == cdk_id)
14121 error ("%H%sparameter pack %qD cannot have a default argument",
14122 &declarator_token_start->location,
14123 kind, id_declarator->u.id.unqualified_name);
14125 error ("%H%sparameter pack cannot have a default argument",
14126 &declarator_token_start->location, kind);
14128 default_argument = NULL_TREE;
14132 default_argument = NULL_TREE;
14134 return make_parameter_declarator (&decl_specifiers,
14139 /* Parse a default argument and return it.
14141 TEMPLATE_PARM_P is true if this is a default argument for a
14142 non-type template parameter. */
14144 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14146 tree default_argument = NULL_TREE;
14147 bool saved_greater_than_is_operator_p;
14148 bool saved_local_variables_forbidden_p;
14150 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14152 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14153 parser->greater_than_is_operator_p = !template_parm_p;
14154 /* Local variable names (and the `this' keyword) may not
14155 appear in a default argument. */
14156 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14157 parser->local_variables_forbidden_p = true;
14158 /* The default argument expression may cause implicitly
14159 defined member functions to be synthesized, which will
14160 result in garbage collection. We must treat this
14161 situation as if we were within the body of function so as
14162 to avoid collecting live data on the stack. */
14164 /* Parse the assignment-expression. */
14165 if (template_parm_p)
14166 push_deferring_access_checks (dk_no_deferred);
14168 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14169 if (template_parm_p)
14170 pop_deferring_access_checks ();
14171 /* Restore saved state. */
14173 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14174 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14176 return default_argument;
14179 /* Parse a function-body.
14182 compound_statement */
14185 cp_parser_function_body (cp_parser *parser)
14187 cp_parser_compound_statement (parser, NULL, false);
14190 /* Parse a ctor-initializer-opt followed by a function-body. Return
14191 true if a ctor-initializer was present. */
14194 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14197 bool ctor_initializer_p;
14199 /* Begin the function body. */
14200 body = begin_function_body ();
14201 /* Parse the optional ctor-initializer. */
14202 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14203 /* Parse the function-body. */
14204 cp_parser_function_body (parser);
14205 /* Finish the function body. */
14206 finish_function_body (body);
14208 return ctor_initializer_p;
14211 /* Parse an initializer.
14214 = initializer-clause
14215 ( expression-list )
14217 Returns an expression representing the initializer. If no
14218 initializer is present, NULL_TREE is returned.
14220 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14221 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14222 set to TRUE if there is no initializer present. If there is an
14223 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14224 is set to true; otherwise it is set to false. */
14227 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14228 bool* non_constant_p)
14233 /* Peek at the next token. */
14234 token = cp_lexer_peek_token (parser->lexer);
14236 /* Let our caller know whether or not this initializer was
14238 *is_direct_init = (token->type != CPP_EQ);
14239 /* Assume that the initializer is constant. */
14240 *non_constant_p = false;
14242 if (token->type == CPP_EQ)
14244 /* Consume the `='. */
14245 cp_lexer_consume_token (parser->lexer);
14246 /* Parse the initializer-clause. */
14247 init = cp_parser_initializer_clause (parser, non_constant_p);
14249 else if (token->type == CPP_OPEN_PAREN)
14250 init = cp_parser_parenthesized_expression_list (parser, false,
14252 /*allow_expansion_p=*/true,
14254 else if (token->type == CPP_OPEN_BRACE)
14256 maybe_warn_cpp0x ("extended initializer lists");
14257 init = cp_parser_braced_list (parser, non_constant_p);
14258 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14262 /* Anything else is an error. */
14263 cp_parser_error (parser, "expected initializer");
14264 init = error_mark_node;
14270 /* Parse an initializer-clause.
14272 initializer-clause:
14273 assignment-expression
14276 Returns an expression representing the initializer.
14278 If the `assignment-expression' production is used the value
14279 returned is simply a representation for the expression.
14281 Otherwise, calls cp_parser_braced_list. */
14284 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14288 /* Assume the expression is constant. */
14289 *non_constant_p = false;
14291 /* If it is not a `{', then we are looking at an
14292 assignment-expression. */
14293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14296 = cp_parser_constant_expression (parser,
14297 /*allow_non_constant_p=*/true,
14299 if (!*non_constant_p)
14300 initializer = fold_non_dependent_expr (initializer);
14303 initializer = cp_parser_braced_list (parser, non_constant_p);
14305 return initializer;
14308 /* Parse a brace-enclosed initializer list.
14311 { initializer-list , [opt] }
14314 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14315 the elements of the initializer-list (or NULL, if the last
14316 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14317 NULL_TREE. There is no way to detect whether or not the optional
14318 trailing `,' was provided. NON_CONSTANT_P is as for
14319 cp_parser_initializer. */
14322 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14326 /* Consume the `{' token. */
14327 cp_lexer_consume_token (parser->lexer);
14328 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14329 initializer = make_node (CONSTRUCTOR);
14330 /* If it's not a `}', then there is a non-trivial initializer. */
14331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14333 /* Parse the initializer list. */
14334 CONSTRUCTOR_ELTS (initializer)
14335 = cp_parser_initializer_list (parser, non_constant_p);
14336 /* A trailing `,' token is allowed. */
14337 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14338 cp_lexer_consume_token (parser->lexer);
14340 /* Now, there should be a trailing `}'. */
14341 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14342 TREE_TYPE (initializer) = init_list_type_node;
14343 return initializer;
14346 /* Parse an initializer-list.
14349 initializer-clause ... [opt]
14350 initializer-list , initializer-clause ... [opt]
14355 identifier : initializer-clause
14356 initializer-list, identifier : initializer-clause
14358 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14359 for the initializer. If the INDEX of the elt is non-NULL, it is the
14360 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14361 as for cp_parser_initializer. */
14363 static VEC(constructor_elt,gc) *
14364 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14366 VEC(constructor_elt,gc) *v = NULL;
14368 /* Assume all of the expressions are constant. */
14369 *non_constant_p = false;
14371 /* Parse the rest of the list. */
14377 bool clause_non_constant_p;
14379 /* If the next token is an identifier and the following one is a
14380 colon, we are looking at the GNU designated-initializer
14382 if (cp_parser_allow_gnu_extensions_p (parser)
14383 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14384 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14386 /* Warn the user that they are using an extension. */
14387 pedwarn (OPT_pedantic,
14388 "ISO C++ does not allow designated initializers");
14389 /* Consume the identifier. */
14390 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14391 /* Consume the `:'. */
14392 cp_lexer_consume_token (parser->lexer);
14395 identifier = NULL_TREE;
14397 /* Parse the initializer. */
14398 initializer = cp_parser_initializer_clause (parser,
14399 &clause_non_constant_p);
14400 /* If any clause is non-constant, so is the entire initializer. */
14401 if (clause_non_constant_p)
14402 *non_constant_p = true;
14404 /* If we have an ellipsis, this is an initializer pack
14406 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14408 /* Consume the `...'. */
14409 cp_lexer_consume_token (parser->lexer);
14411 /* Turn the initializer into an initializer expansion. */
14412 initializer = make_pack_expansion (initializer);
14415 /* Add it to the vector. */
14416 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14418 /* If the next token is not a comma, we have reached the end of
14420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14423 /* Peek at the next token. */
14424 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14425 /* If the next token is a `}', then we're still done. An
14426 initializer-clause can have a trailing `,' after the
14427 initializer-list and before the closing `}'. */
14428 if (token->type == CPP_CLOSE_BRACE)
14431 /* Consume the `,' token. */
14432 cp_lexer_consume_token (parser->lexer);
14438 /* Classes [gram.class] */
14440 /* Parse a class-name.
14446 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14447 to indicate that names looked up in dependent types should be
14448 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14449 keyword has been used to indicate that the name that appears next
14450 is a template. TAG_TYPE indicates the explicit tag given before
14451 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14452 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14453 is the class being defined in a class-head.
14455 Returns the TYPE_DECL representing the class. */
14458 cp_parser_class_name (cp_parser *parser,
14459 bool typename_keyword_p,
14460 bool template_keyword_p,
14461 enum tag_types tag_type,
14462 bool check_dependency_p,
14464 bool is_declaration)
14471 /* All class-names start with an identifier. */
14472 token = cp_lexer_peek_token (parser->lexer);
14473 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14475 cp_parser_error (parser, "expected class-name");
14476 return error_mark_node;
14479 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14480 to a template-id, so we save it here. */
14481 scope = parser->scope;
14482 if (scope == error_mark_node)
14483 return error_mark_node;
14485 /* Any name names a type if we're following the `typename' keyword
14486 in a qualified name where the enclosing scope is type-dependent. */
14487 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14488 && dependent_type_p (scope));
14489 /* Handle the common case (an identifier, but not a template-id)
14491 if (token->type == CPP_NAME
14492 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14494 cp_token *identifier_token;
14498 /* Look for the identifier. */
14499 identifier_token = cp_lexer_peek_token (parser->lexer);
14500 ambiguous_p = identifier_token->ambiguous_p;
14501 identifier = cp_parser_identifier (parser);
14502 /* If the next token isn't an identifier, we are certainly not
14503 looking at a class-name. */
14504 if (identifier == error_mark_node)
14505 decl = error_mark_node;
14506 /* If we know this is a type-name, there's no need to look it
14508 else if (typename_p)
14512 tree ambiguous_decls;
14513 /* If we already know that this lookup is ambiguous, then
14514 we've already issued an error message; there's no reason
14518 cp_parser_simulate_error (parser);
14519 return error_mark_node;
14521 /* If the next token is a `::', then the name must be a type
14524 [basic.lookup.qual]
14526 During the lookup for a name preceding the :: scope
14527 resolution operator, object, function, and enumerator
14528 names are ignored. */
14529 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14530 tag_type = typename_type;
14531 /* Look up the name. */
14532 decl = cp_parser_lookup_name (parser, identifier,
14534 /*is_template=*/false,
14535 /*is_namespace=*/false,
14536 check_dependency_p,
14538 identifier_token->location);
14539 if (ambiguous_decls)
14541 error ("%Hreference to %qD is ambiguous",
14542 &identifier_token->location, identifier);
14543 print_candidates (ambiguous_decls);
14544 if (cp_parser_parsing_tentatively (parser))
14546 identifier_token->ambiguous_p = true;
14547 cp_parser_simulate_error (parser);
14549 return error_mark_node;
14555 /* Try a template-id. */
14556 decl = cp_parser_template_id (parser, template_keyword_p,
14557 check_dependency_p,
14559 if (decl == error_mark_node)
14560 return error_mark_node;
14563 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14565 /* If this is a typename, create a TYPENAME_TYPE. */
14566 if (typename_p && decl != error_mark_node)
14568 decl = make_typename_type (scope, decl, typename_type,
14569 /*complain=*/tf_error);
14570 if (decl != error_mark_node)
14571 decl = TYPE_NAME (decl);
14574 /* Check to see that it is really the name of a class. */
14575 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14576 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14577 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14578 /* Situations like this:
14580 template <typename T> struct A {
14581 typename T::template X<int>::I i;
14584 are problematic. Is `T::template X<int>' a class-name? The
14585 standard does not seem to be definitive, but there is no other
14586 valid interpretation of the following `::'. Therefore, those
14587 names are considered class-names. */
14589 decl = make_typename_type (scope, decl, tag_type, tf_error);
14590 if (decl != error_mark_node)
14591 decl = TYPE_NAME (decl);
14593 else if (TREE_CODE (decl) != TYPE_DECL
14594 || TREE_TYPE (decl) == error_mark_node
14595 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14596 decl = error_mark_node;
14598 if (decl == error_mark_node)
14599 cp_parser_error (parser, "expected class-name");
14604 /* Parse a class-specifier.
14607 class-head { member-specification [opt] }
14609 Returns the TREE_TYPE representing the class. */
14612 cp_parser_class_specifier (cp_parser* parser)
14616 tree attributes = NULL_TREE;
14617 int has_trailing_semicolon;
14618 bool nested_name_specifier_p;
14619 unsigned saved_num_template_parameter_lists;
14620 bool saved_in_function_body;
14621 tree old_scope = NULL_TREE;
14622 tree scope = NULL_TREE;
14625 push_deferring_access_checks (dk_no_deferred);
14627 /* Parse the class-head. */
14628 type = cp_parser_class_head (parser,
14629 &nested_name_specifier_p,
14632 /* If the class-head was a semantic disaster, skip the entire body
14636 cp_parser_skip_to_end_of_block_or_statement (parser);
14637 pop_deferring_access_checks ();
14638 return error_mark_node;
14641 /* Look for the `{'. */
14642 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14644 pop_deferring_access_checks ();
14645 return error_mark_node;
14648 /* Process the base classes. If they're invalid, skip the
14649 entire class body. */
14650 if (!xref_basetypes (type, bases))
14652 /* Consuming the closing brace yields better error messages
14654 if (cp_parser_skip_to_closing_brace (parser))
14655 cp_lexer_consume_token (parser->lexer);
14656 pop_deferring_access_checks ();
14657 return error_mark_node;
14660 /* Issue an error message if type-definitions are forbidden here. */
14661 cp_parser_check_type_definition (parser);
14662 /* Remember that we are defining one more class. */
14663 ++parser->num_classes_being_defined;
14664 /* Inside the class, surrounding template-parameter-lists do not
14666 saved_num_template_parameter_lists
14667 = parser->num_template_parameter_lists;
14668 parser->num_template_parameter_lists = 0;
14669 /* We are not in a function body. */
14670 saved_in_function_body = parser->in_function_body;
14671 parser->in_function_body = false;
14673 /* Start the class. */
14674 if (nested_name_specifier_p)
14676 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14677 old_scope = push_inner_scope (scope);
14679 type = begin_class_definition (type, attributes);
14681 if (type == error_mark_node)
14682 /* If the type is erroneous, skip the entire body of the class. */
14683 cp_parser_skip_to_closing_brace (parser);
14685 /* Parse the member-specification. */
14686 cp_parser_member_specification_opt (parser);
14688 /* Look for the trailing `}'. */
14689 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14690 /* We get better error messages by noticing a common problem: a
14691 missing trailing `;'. */
14692 token = cp_lexer_peek_token (parser->lexer);
14693 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14694 /* Look for trailing attributes to apply to this class. */
14695 if (cp_parser_allow_gnu_extensions_p (parser))
14696 attributes = cp_parser_attributes_opt (parser);
14697 if (type != error_mark_node)
14698 type = finish_struct (type, attributes);
14699 if (nested_name_specifier_p)
14700 pop_inner_scope (old_scope, scope);
14701 /* If this class is not itself within the scope of another class,
14702 then we need to parse the bodies of all of the queued function
14703 definitions. Note that the queued functions defined in a class
14704 are not always processed immediately following the
14705 class-specifier for that class. Consider:
14708 struct B { void f() { sizeof (A); } };
14711 If `f' were processed before the processing of `A' were
14712 completed, there would be no way to compute the size of `A'.
14713 Note that the nesting we are interested in here is lexical --
14714 not the semantic nesting given by TYPE_CONTEXT. In particular,
14717 struct A { struct B; };
14718 struct A::B { void f() { } };
14720 there is no need to delay the parsing of `A::B::f'. */
14721 if (--parser->num_classes_being_defined == 0)
14725 tree class_type = NULL_TREE;
14726 tree pushed_scope = NULL_TREE;
14728 /* In a first pass, parse default arguments to the functions.
14729 Then, in a second pass, parse the bodies of the functions.
14730 This two-phased approach handles cases like:
14738 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14739 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14740 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14741 TREE_PURPOSE (parser->unparsed_functions_queues)
14742 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14744 fn = TREE_VALUE (queue_entry);
14745 /* If there are default arguments that have not yet been processed,
14746 take care of them now. */
14747 if (class_type != TREE_PURPOSE (queue_entry))
14750 pop_scope (pushed_scope);
14751 class_type = TREE_PURPOSE (queue_entry);
14752 pushed_scope = push_scope (class_type);
14754 /* Make sure that any template parameters are in scope. */
14755 maybe_begin_member_template_processing (fn);
14756 /* Parse the default argument expressions. */
14757 cp_parser_late_parsing_default_args (parser, fn);
14758 /* Remove any template parameters from the symbol table. */
14759 maybe_end_member_template_processing ();
14762 pop_scope (pushed_scope);
14763 /* Now parse the body of the functions. */
14764 for (TREE_VALUE (parser->unparsed_functions_queues)
14765 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14766 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14767 TREE_VALUE (parser->unparsed_functions_queues)
14768 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14770 /* Figure out which function we need to process. */
14771 fn = TREE_VALUE (queue_entry);
14772 /* Parse the function. */
14773 cp_parser_late_parsing_for_member (parser, fn);
14777 /* Put back any saved access checks. */
14778 pop_deferring_access_checks ();
14780 /* Restore saved state. */
14781 parser->in_function_body = saved_in_function_body;
14782 parser->num_template_parameter_lists
14783 = saved_num_template_parameter_lists;
14788 /* Parse a class-head.
14791 class-key identifier [opt] base-clause [opt]
14792 class-key nested-name-specifier identifier base-clause [opt]
14793 class-key nested-name-specifier [opt] template-id
14797 class-key attributes identifier [opt] base-clause [opt]
14798 class-key attributes nested-name-specifier identifier base-clause [opt]
14799 class-key attributes nested-name-specifier [opt] template-id
14802 Upon return BASES is initialized to the list of base classes (or
14803 NULL, if there are none) in the same form returned by
14804 cp_parser_base_clause.
14806 Returns the TYPE of the indicated class. Sets
14807 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14808 involving a nested-name-specifier was used, and FALSE otherwise.
14810 Returns error_mark_node if this is not a class-head.
14812 Returns NULL_TREE if the class-head is syntactically valid, but
14813 semantically invalid in a way that means we should skip the entire
14814 body of the class. */
14817 cp_parser_class_head (cp_parser* parser,
14818 bool* nested_name_specifier_p,
14819 tree *attributes_p,
14822 tree nested_name_specifier;
14823 enum tag_types class_key;
14824 tree id = NULL_TREE;
14825 tree type = NULL_TREE;
14827 bool template_id_p = false;
14828 bool qualified_p = false;
14829 bool invalid_nested_name_p = false;
14830 bool invalid_explicit_specialization_p = false;
14831 tree pushed_scope = NULL_TREE;
14832 unsigned num_templates;
14833 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14834 /* Assume no nested-name-specifier will be present. */
14835 *nested_name_specifier_p = false;
14836 /* Assume no template parameter lists will be used in defining the
14840 *bases = NULL_TREE;
14842 /* Look for the class-key. */
14843 class_key = cp_parser_class_key (parser);
14844 if (class_key == none_type)
14845 return error_mark_node;
14847 /* Parse the attributes. */
14848 attributes = cp_parser_attributes_opt (parser);
14850 /* If the next token is `::', that is invalid -- but sometimes
14851 people do try to write:
14855 Handle this gracefully by accepting the extra qualifier, and then
14856 issuing an error about it later if this really is a
14857 class-head. If it turns out just to be an elaborated type
14858 specifier, remain silent. */
14859 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14860 qualified_p = true;
14862 push_deferring_access_checks (dk_no_check);
14864 /* Determine the name of the class. Begin by looking for an
14865 optional nested-name-specifier. */
14866 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14867 nested_name_specifier
14868 = cp_parser_nested_name_specifier_opt (parser,
14869 /*typename_keyword_p=*/false,
14870 /*check_dependency_p=*/false,
14872 /*is_declaration=*/false);
14873 /* If there was a nested-name-specifier, then there *must* be an
14875 if (nested_name_specifier)
14877 type_start_token = cp_lexer_peek_token (parser->lexer);
14878 /* Although the grammar says `identifier', it really means
14879 `class-name' or `template-name'. You are only allowed to
14880 define a class that has already been declared with this
14883 The proposed resolution for Core Issue 180 says that wherever
14884 you see `class T::X' you should treat `X' as a type-name.
14886 It is OK to define an inaccessible class; for example:
14888 class A { class B; };
14891 We do not know if we will see a class-name, or a
14892 template-name. We look for a class-name first, in case the
14893 class-name is a template-id; if we looked for the
14894 template-name first we would stop after the template-name. */
14895 cp_parser_parse_tentatively (parser);
14896 type = cp_parser_class_name (parser,
14897 /*typename_keyword_p=*/false,
14898 /*template_keyword_p=*/false,
14900 /*check_dependency_p=*/false,
14901 /*class_head_p=*/true,
14902 /*is_declaration=*/false);
14903 /* If that didn't work, ignore the nested-name-specifier. */
14904 if (!cp_parser_parse_definitely (parser))
14906 invalid_nested_name_p = true;
14907 type_start_token = cp_lexer_peek_token (parser->lexer);
14908 id = cp_parser_identifier (parser);
14909 if (id == error_mark_node)
14912 /* If we could not find a corresponding TYPE, treat this
14913 declaration like an unqualified declaration. */
14914 if (type == error_mark_node)
14915 nested_name_specifier = NULL_TREE;
14916 /* Otherwise, count the number of templates used in TYPE and its
14917 containing scopes. */
14922 for (scope = TREE_TYPE (type);
14923 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14924 scope = (TYPE_P (scope)
14925 ? TYPE_CONTEXT (scope)
14926 : DECL_CONTEXT (scope)))
14928 && CLASS_TYPE_P (scope)
14929 && CLASSTYPE_TEMPLATE_INFO (scope)
14930 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14931 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14935 /* Otherwise, the identifier is optional. */
14938 /* We don't know whether what comes next is a template-id,
14939 an identifier, or nothing at all. */
14940 cp_parser_parse_tentatively (parser);
14941 /* Check for a template-id. */
14942 type_start_token = cp_lexer_peek_token (parser->lexer);
14943 id = cp_parser_template_id (parser,
14944 /*template_keyword_p=*/false,
14945 /*check_dependency_p=*/true,
14946 /*is_declaration=*/true);
14947 /* If that didn't work, it could still be an identifier. */
14948 if (!cp_parser_parse_definitely (parser))
14950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14952 type_start_token = cp_lexer_peek_token (parser->lexer);
14953 id = cp_parser_identifier (parser);
14960 template_id_p = true;
14965 pop_deferring_access_checks ();
14968 cp_parser_check_for_invalid_template_id (parser, id,
14969 type_start_token->location);
14971 /* If it's not a `:' or a `{' then we can't really be looking at a
14972 class-head, since a class-head only appears as part of a
14973 class-specifier. We have to detect this situation before calling
14974 xref_tag, since that has irreversible side-effects. */
14975 if (!cp_parser_next_token_starts_class_definition_p (parser))
14977 cp_parser_error (parser, "expected %<{%> or %<:%>");
14978 return error_mark_node;
14981 /* At this point, we're going ahead with the class-specifier, even
14982 if some other problem occurs. */
14983 cp_parser_commit_to_tentative_parse (parser);
14984 /* Issue the error about the overly-qualified name now. */
14987 cp_parser_error (parser,
14988 "global qualification of class name is invalid");
14989 return error_mark_node;
14991 else if (invalid_nested_name_p)
14993 cp_parser_error (parser,
14994 "qualified name does not name a class");
14995 return error_mark_node;
14997 else if (nested_name_specifier)
15001 /* Reject typedef-names in class heads. */
15002 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15004 error ("%Hinvalid class name in declaration of %qD",
15005 &type_start_token->location, type);
15010 /* Figure out in what scope the declaration is being placed. */
15011 scope = current_scope ();
15012 /* If that scope does not contain the scope in which the
15013 class was originally declared, the program is invalid. */
15014 if (scope && !is_ancestor (scope, nested_name_specifier))
15016 if (at_namespace_scope_p ())
15017 error ("%Hdeclaration of %qD in namespace %qD which does not "
15019 &type_start_token->location,
15020 type, scope, nested_name_specifier);
15022 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15023 &type_start_token->location,
15024 type, scope, nested_name_specifier);
15030 A declarator-id shall not be qualified except for the
15031 definition of a ... nested class outside of its class
15032 ... [or] the definition or explicit instantiation of a
15033 class member of a namespace outside of its namespace. */
15034 if (scope == nested_name_specifier)
15036 permerror (input_location, "%Hextra qualification not allowed",
15037 &nested_name_specifier_token_start->location);
15038 nested_name_specifier = NULL_TREE;
15042 /* An explicit-specialization must be preceded by "template <>". If
15043 it is not, try to recover gracefully. */
15044 if (at_namespace_scope_p ()
15045 && parser->num_template_parameter_lists == 0
15048 error ("%Han explicit specialization must be preceded by %<template <>%>",
15049 &type_start_token->location);
15050 invalid_explicit_specialization_p = true;
15051 /* Take the same action that would have been taken by
15052 cp_parser_explicit_specialization. */
15053 ++parser->num_template_parameter_lists;
15054 begin_specialization ();
15056 /* There must be no "return" statements between this point and the
15057 end of this function; set "type "to the correct return value and
15058 use "goto done;" to return. */
15059 /* Make sure that the right number of template parameters were
15061 if (!cp_parser_check_template_parameters (parser, num_templates,
15062 type_start_token->location))
15064 /* If something went wrong, there is no point in even trying to
15065 process the class-definition. */
15070 /* Look up the type. */
15073 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15074 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15075 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15077 error ("%Hfunction template %qD redeclared as a class template",
15078 &type_start_token->location, id);
15079 type = error_mark_node;
15083 type = TREE_TYPE (id);
15084 type = maybe_process_partial_specialization (type);
15086 if (nested_name_specifier)
15087 pushed_scope = push_scope (nested_name_specifier);
15089 else if (nested_name_specifier)
15095 template <typename T> struct S { struct T };
15096 template <typename T> struct S<T>::T { };
15098 we will get a TYPENAME_TYPE when processing the definition of
15099 `S::T'. We need to resolve it to the actual type before we
15100 try to define it. */
15101 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15103 class_type = resolve_typename_type (TREE_TYPE (type),
15104 /*only_current_p=*/false);
15105 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15106 type = TYPE_NAME (class_type);
15109 cp_parser_error (parser, "could not resolve typename type");
15110 type = error_mark_node;
15114 if (maybe_process_partial_specialization (TREE_TYPE (type))
15115 == error_mark_node)
15121 class_type = current_class_type;
15122 /* Enter the scope indicated by the nested-name-specifier. */
15123 pushed_scope = push_scope (nested_name_specifier);
15124 /* Get the canonical version of this type. */
15125 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15126 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15127 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15129 type = push_template_decl (type);
15130 if (type == error_mark_node)
15137 type = TREE_TYPE (type);
15138 *nested_name_specifier_p = true;
15140 else /* The name is not a nested name. */
15142 /* If the class was unnamed, create a dummy name. */
15144 id = make_anon_name ();
15145 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15146 parser->num_template_parameter_lists);
15149 /* Indicate whether this class was declared as a `class' or as a
15151 if (TREE_CODE (type) == RECORD_TYPE)
15152 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15153 cp_parser_check_class_key (class_key, type);
15155 /* If this type was already complete, and we see another definition,
15156 that's an error. */
15157 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15159 error ("%Hredefinition of %q#T",
15160 &type_start_token->location, type);
15161 error ("%Hprevious definition of %q+#T",
15162 &type_start_token->location, type);
15166 else if (type == error_mark_node)
15169 /* We will have entered the scope containing the class; the names of
15170 base classes should be looked up in that context. For example:
15172 struct A { struct B {}; struct C; };
15173 struct A::C : B {};
15177 /* Get the list of base-classes, if there is one. */
15178 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15179 *bases = cp_parser_base_clause (parser);
15182 /* Leave the scope given by the nested-name-specifier. We will
15183 enter the class scope itself while processing the members. */
15185 pop_scope (pushed_scope);
15187 if (invalid_explicit_specialization_p)
15189 end_specialization ();
15190 --parser->num_template_parameter_lists;
15192 *attributes_p = attributes;
15196 /* Parse a class-key.
15203 Returns the kind of class-key specified, or none_type to indicate
15206 static enum tag_types
15207 cp_parser_class_key (cp_parser* parser)
15210 enum tag_types tag_type;
15212 /* Look for the class-key. */
15213 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15217 /* Check to see if the TOKEN is a class-key. */
15218 tag_type = cp_parser_token_is_class_key (token);
15220 cp_parser_error (parser, "expected class-key");
15224 /* Parse an (optional) member-specification.
15226 member-specification:
15227 member-declaration member-specification [opt]
15228 access-specifier : member-specification [opt] */
15231 cp_parser_member_specification_opt (cp_parser* parser)
15238 /* Peek at the next token. */
15239 token = cp_lexer_peek_token (parser->lexer);
15240 /* If it's a `}', or EOF then we've seen all the members. */
15241 if (token->type == CPP_CLOSE_BRACE
15242 || token->type == CPP_EOF
15243 || token->type == CPP_PRAGMA_EOL)
15246 /* See if this token is a keyword. */
15247 keyword = token->keyword;
15251 case RID_PROTECTED:
15253 /* Consume the access-specifier. */
15254 cp_lexer_consume_token (parser->lexer);
15255 /* Remember which access-specifier is active. */
15256 current_access_specifier = token->u.value;
15257 /* Look for the `:'. */
15258 cp_parser_require (parser, CPP_COLON, "%<:%>");
15262 /* Accept #pragmas at class scope. */
15263 if (token->type == CPP_PRAGMA)
15265 cp_parser_pragma (parser, pragma_external);
15269 /* Otherwise, the next construction must be a
15270 member-declaration. */
15271 cp_parser_member_declaration (parser);
15276 /* Parse a member-declaration.
15278 member-declaration:
15279 decl-specifier-seq [opt] member-declarator-list [opt] ;
15280 function-definition ; [opt]
15281 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15283 template-declaration
15285 member-declarator-list:
15287 member-declarator-list , member-declarator
15290 declarator pure-specifier [opt]
15291 declarator constant-initializer [opt]
15292 identifier [opt] : constant-expression
15296 member-declaration:
15297 __extension__ member-declaration
15300 declarator attributes [opt] pure-specifier [opt]
15301 declarator attributes [opt] constant-initializer [opt]
15302 identifier [opt] attributes [opt] : constant-expression
15306 member-declaration:
15307 static_assert-declaration */
15310 cp_parser_member_declaration (cp_parser* parser)
15312 cp_decl_specifier_seq decl_specifiers;
15313 tree prefix_attributes;
15315 int declares_class_or_enum;
15317 cp_token *token = NULL;
15318 cp_token *decl_spec_token_start = NULL;
15319 cp_token *initializer_token_start = NULL;
15320 int saved_pedantic;
15322 /* Check for the `__extension__' keyword. */
15323 if (cp_parser_extension_opt (parser, &saved_pedantic))
15326 cp_parser_member_declaration (parser);
15327 /* Restore the old value of the PEDANTIC flag. */
15328 pedantic = saved_pedantic;
15333 /* Check for a template-declaration. */
15334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15336 /* An explicit specialization here is an error condition, and we
15337 expect the specialization handler to detect and report this. */
15338 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15339 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15340 cp_parser_explicit_specialization (parser);
15342 cp_parser_template_declaration (parser, /*member_p=*/true);
15347 /* Check for a using-declaration. */
15348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15350 /* Parse the using-declaration. */
15351 cp_parser_using_declaration (parser,
15352 /*access_declaration_p=*/false);
15356 /* Check for @defs. */
15357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15360 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15361 ivar = ivar_chains;
15365 ivar = TREE_CHAIN (member);
15366 TREE_CHAIN (member) = NULL_TREE;
15367 finish_member_declaration (member);
15372 /* If the next token is `static_assert' we have a static assertion. */
15373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15375 cp_parser_static_assert (parser, /*member_p=*/true);
15379 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15382 /* Parse the decl-specifier-seq. */
15383 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15384 cp_parser_decl_specifier_seq (parser,
15385 CP_PARSER_FLAGS_OPTIONAL,
15387 &declares_class_or_enum);
15388 prefix_attributes = decl_specifiers.attributes;
15389 decl_specifiers.attributes = NULL_TREE;
15390 /* Check for an invalid type-name. */
15391 if (!decl_specifiers.type
15392 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15394 /* If there is no declarator, then the decl-specifier-seq should
15396 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15398 /* If there was no decl-specifier-seq, and the next token is a
15399 `;', then we have something like:
15405 Each member-declaration shall declare at least one member
15406 name of the class. */
15407 if (!decl_specifiers.any_specifiers_p)
15409 cp_token *token = cp_lexer_peek_token (parser->lexer);
15410 if (!in_system_header_at (token->location))
15411 pedwarn (OPT_pedantic, "%Hextra %<;%>", &token->location);
15417 /* See if this declaration is a friend. */
15418 friend_p = cp_parser_friend_p (&decl_specifiers);
15419 /* If there were decl-specifiers, check to see if there was
15420 a class-declaration. */
15421 type = check_tag_decl (&decl_specifiers);
15422 /* Nested classes have already been added to the class, but
15423 a `friend' needs to be explicitly registered. */
15426 /* If the `friend' keyword was present, the friend must
15427 be introduced with a class-key. */
15428 if (!declares_class_or_enum)
15429 error ("%Ha class-key must be used when declaring a friend",
15430 &decl_spec_token_start->location);
15433 template <typename T> struct A {
15434 friend struct A<T>::B;
15437 A<T>::B will be represented by a TYPENAME_TYPE, and
15438 therefore not recognized by check_tag_decl. */
15440 && decl_specifiers.type
15441 && TYPE_P (decl_specifiers.type))
15442 type = decl_specifiers.type;
15443 if (!type || !TYPE_P (type))
15444 error ("%Hfriend declaration does not name a class or "
15445 "function", &decl_spec_token_start->location);
15447 make_friend_class (current_class_type, type,
15448 /*complain=*/true);
15450 /* If there is no TYPE, an error message will already have
15452 else if (!type || type == error_mark_node)
15454 /* An anonymous aggregate has to be handled specially; such
15455 a declaration really declares a data member (with a
15456 particular type), as opposed to a nested class. */
15457 else if (ANON_AGGR_TYPE_P (type))
15459 /* Remove constructors and such from TYPE, now that we
15460 know it is an anonymous aggregate. */
15461 fixup_anonymous_aggr (type);
15462 /* And make the corresponding data member. */
15463 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15464 /* Add it to the class. */
15465 finish_member_declaration (decl);
15468 cp_parser_check_access_in_redeclaration
15470 decl_spec_token_start->location);
15475 /* See if these declarations will be friends. */
15476 friend_p = cp_parser_friend_p (&decl_specifiers);
15478 /* Keep going until we hit the `;' at the end of the
15480 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15482 tree attributes = NULL_TREE;
15483 tree first_attribute;
15485 /* Peek at the next token. */
15486 token = cp_lexer_peek_token (parser->lexer);
15488 /* Check for a bitfield declaration. */
15489 if (token->type == CPP_COLON
15490 || (token->type == CPP_NAME
15491 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15497 /* Get the name of the bitfield. Note that we cannot just
15498 check TOKEN here because it may have been invalidated by
15499 the call to cp_lexer_peek_nth_token above. */
15500 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15501 identifier = cp_parser_identifier (parser);
15503 identifier = NULL_TREE;
15505 /* Consume the `:' token. */
15506 cp_lexer_consume_token (parser->lexer);
15507 /* Get the width of the bitfield. */
15509 = cp_parser_constant_expression (parser,
15510 /*allow_non_constant=*/false,
15513 /* Look for attributes that apply to the bitfield. */
15514 attributes = cp_parser_attributes_opt (parser);
15515 /* Remember which attributes are prefix attributes and
15517 first_attribute = attributes;
15518 /* Combine the attributes. */
15519 attributes = chainon (prefix_attributes, attributes);
15521 /* Create the bitfield declaration. */
15522 decl = grokbitfield (identifier
15523 ? make_id_declarator (NULL_TREE,
15533 cp_declarator *declarator;
15535 tree asm_specification;
15536 int ctor_dtor_or_conv_p;
15538 /* Parse the declarator. */
15540 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15541 &ctor_dtor_or_conv_p,
15542 /*parenthesized_p=*/NULL,
15543 /*member_p=*/true);
15545 /* If something went wrong parsing the declarator, make sure
15546 that we at least consume some tokens. */
15547 if (declarator == cp_error_declarator)
15549 /* Skip to the end of the statement. */
15550 cp_parser_skip_to_end_of_statement (parser);
15551 /* If the next token is not a semicolon, that is
15552 probably because we just skipped over the body of
15553 a function. So, we consume a semicolon if
15554 present, but do not issue an error message if it
15556 if (cp_lexer_next_token_is (parser->lexer,
15558 cp_lexer_consume_token (parser->lexer);
15562 if (declares_class_or_enum & 2)
15563 cp_parser_check_for_definition_in_return_type
15564 (declarator, decl_specifiers.type,
15565 decl_specifiers.type_location);
15567 /* Look for an asm-specification. */
15568 asm_specification = cp_parser_asm_specification_opt (parser);
15569 /* Look for attributes that apply to the declaration. */
15570 attributes = cp_parser_attributes_opt (parser);
15571 /* Remember which attributes are prefix attributes and
15573 first_attribute = attributes;
15574 /* Combine the attributes. */
15575 attributes = chainon (prefix_attributes, attributes);
15577 /* If it's an `=', then we have a constant-initializer or a
15578 pure-specifier. It is not correct to parse the
15579 initializer before registering the member declaration
15580 since the member declaration should be in scope while
15581 its initializer is processed. However, the rest of the
15582 front end does not yet provide an interface that allows
15583 us to handle this correctly. */
15584 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15588 A pure-specifier shall be used only in the declaration of
15589 a virtual function.
15591 A member-declarator can contain a constant-initializer
15592 only if it declares a static member of integral or
15595 Therefore, if the DECLARATOR is for a function, we look
15596 for a pure-specifier; otherwise, we look for a
15597 constant-initializer. When we call `grokfield', it will
15598 perform more stringent semantics checks. */
15599 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15600 if (function_declarator_p (declarator))
15601 initializer = cp_parser_pure_specifier (parser);
15603 /* Parse the initializer. */
15604 initializer = cp_parser_constant_initializer (parser);
15606 /* Otherwise, there is no initializer. */
15608 initializer = NULL_TREE;
15610 /* See if we are probably looking at a function
15611 definition. We are certainly not looking at a
15612 member-declarator. Calling `grokfield' has
15613 side-effects, so we must not do it unless we are sure
15614 that we are looking at a member-declarator. */
15615 if (cp_parser_token_starts_function_definition_p
15616 (cp_lexer_peek_token (parser->lexer)))
15618 /* The grammar does not allow a pure-specifier to be
15619 used when a member function is defined. (It is
15620 possible that this fact is an oversight in the
15621 standard, since a pure function may be defined
15622 outside of the class-specifier. */
15624 error ("%Hpure-specifier on function-definition",
15625 &initializer_token_start->location);
15626 decl = cp_parser_save_member_function_body (parser,
15630 /* If the member was not a friend, declare it here. */
15632 finish_member_declaration (decl);
15633 /* Peek at the next token. */
15634 token = cp_lexer_peek_token (parser->lexer);
15635 /* If the next token is a semicolon, consume it. */
15636 if (token->type == CPP_SEMICOLON)
15637 cp_lexer_consume_token (parser->lexer);
15641 /* Create the declaration. */
15642 decl = grokfield (declarator, &decl_specifiers,
15643 initializer, /*init_const_expr_p=*/true,
15648 /* Reset PREFIX_ATTRIBUTES. */
15649 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15650 attributes = TREE_CHAIN (attributes);
15652 TREE_CHAIN (attributes) = NULL_TREE;
15654 /* If there is any qualification still in effect, clear it
15655 now; we will be starting fresh with the next declarator. */
15656 parser->scope = NULL_TREE;
15657 parser->qualifying_scope = NULL_TREE;
15658 parser->object_scope = NULL_TREE;
15659 /* If it's a `,', then there are more declarators. */
15660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15661 cp_lexer_consume_token (parser->lexer);
15662 /* If the next token isn't a `;', then we have a parse error. */
15663 else if (cp_lexer_next_token_is_not (parser->lexer,
15666 cp_parser_error (parser, "expected %<;%>");
15667 /* Skip tokens until we find a `;'. */
15668 cp_parser_skip_to_end_of_statement (parser);
15675 /* Add DECL to the list of members. */
15677 finish_member_declaration (decl);
15679 if (TREE_CODE (decl) == FUNCTION_DECL)
15680 cp_parser_save_default_args (parser, decl);
15685 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15688 /* Parse a pure-specifier.
15693 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15694 Otherwise, ERROR_MARK_NODE is returned. */
15697 cp_parser_pure_specifier (cp_parser* parser)
15701 /* Look for the `=' token. */
15702 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15703 return error_mark_node;
15704 /* Look for the `0' token. */
15705 token = cp_lexer_consume_token (parser->lexer);
15707 /* Accept = default or = delete in c++0x mode. */
15708 if (token->keyword == RID_DEFAULT
15709 || token->keyword == RID_DELETE)
15711 maybe_warn_cpp0x ("defaulted and deleted functions");
15712 return token->u.value;
15715 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15716 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15718 cp_parser_error (parser,
15719 "invalid pure specifier (only %<= 0%> is allowed)");
15720 cp_parser_skip_to_end_of_statement (parser);
15721 return error_mark_node;
15723 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15725 error ("%Htemplates may not be %<virtual%>", &token->location);
15726 return error_mark_node;
15729 return integer_zero_node;
15732 /* Parse a constant-initializer.
15734 constant-initializer:
15735 = constant-expression
15737 Returns a representation of the constant-expression. */
15740 cp_parser_constant_initializer (cp_parser* parser)
15742 /* Look for the `=' token. */
15743 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15744 return error_mark_node;
15746 /* It is invalid to write:
15748 struct S { static const int i = { 7 }; };
15751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15753 cp_parser_error (parser,
15754 "a brace-enclosed initializer is not allowed here");
15755 /* Consume the opening brace. */
15756 cp_lexer_consume_token (parser->lexer);
15757 /* Skip the initializer. */
15758 cp_parser_skip_to_closing_brace (parser);
15759 /* Look for the trailing `}'. */
15760 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15762 return error_mark_node;
15765 return cp_parser_constant_expression (parser,
15766 /*allow_non_constant=*/false,
15770 /* Derived classes [gram.class.derived] */
15772 /* Parse a base-clause.
15775 : base-specifier-list
15777 base-specifier-list:
15778 base-specifier ... [opt]
15779 base-specifier-list , base-specifier ... [opt]
15781 Returns a TREE_LIST representing the base-classes, in the order in
15782 which they were declared. The representation of each node is as
15783 described by cp_parser_base_specifier.
15785 In the case that no bases are specified, this function will return
15786 NULL_TREE, not ERROR_MARK_NODE. */
15789 cp_parser_base_clause (cp_parser* parser)
15791 tree bases = NULL_TREE;
15793 /* Look for the `:' that begins the list. */
15794 cp_parser_require (parser, CPP_COLON, "%<:%>");
15796 /* Scan the base-specifier-list. */
15801 bool pack_expansion_p = false;
15803 /* Look for the base-specifier. */
15804 base = cp_parser_base_specifier (parser);
15805 /* Look for the (optional) ellipsis. */
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15808 /* Consume the `...'. */
15809 cp_lexer_consume_token (parser->lexer);
15811 pack_expansion_p = true;
15814 /* Add BASE to the front of the list. */
15815 if (base != error_mark_node)
15817 if (pack_expansion_p)
15818 /* Make this a pack expansion type. */
15819 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15822 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15824 TREE_CHAIN (base) = bases;
15828 /* Peek at the next token. */
15829 token = cp_lexer_peek_token (parser->lexer);
15830 /* If it's not a comma, then the list is complete. */
15831 if (token->type != CPP_COMMA)
15833 /* Consume the `,'. */
15834 cp_lexer_consume_token (parser->lexer);
15837 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15838 base class had a qualified name. However, the next name that
15839 appears is certainly not qualified. */
15840 parser->scope = NULL_TREE;
15841 parser->qualifying_scope = NULL_TREE;
15842 parser->object_scope = NULL_TREE;
15844 return nreverse (bases);
15847 /* Parse a base-specifier.
15850 :: [opt] nested-name-specifier [opt] class-name
15851 virtual access-specifier [opt] :: [opt] nested-name-specifier
15853 access-specifier virtual [opt] :: [opt] nested-name-specifier
15856 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15857 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15858 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15859 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15862 cp_parser_base_specifier (cp_parser* parser)
15866 bool virtual_p = false;
15867 bool duplicate_virtual_error_issued_p = false;
15868 bool duplicate_access_error_issued_p = false;
15869 bool class_scope_p, template_p;
15870 tree access = access_default_node;
15873 /* Process the optional `virtual' and `access-specifier'. */
15876 /* Peek at the next token. */
15877 token = cp_lexer_peek_token (parser->lexer);
15878 /* Process `virtual'. */
15879 switch (token->keyword)
15882 /* If `virtual' appears more than once, issue an error. */
15883 if (virtual_p && !duplicate_virtual_error_issued_p)
15885 cp_parser_error (parser,
15886 "%<virtual%> specified more than once in base-specified");
15887 duplicate_virtual_error_issued_p = true;
15892 /* Consume the `virtual' token. */
15893 cp_lexer_consume_token (parser->lexer);
15898 case RID_PROTECTED:
15900 /* If more than one access specifier appears, issue an
15902 if (access != access_default_node
15903 && !duplicate_access_error_issued_p)
15905 cp_parser_error (parser,
15906 "more than one access specifier in base-specified");
15907 duplicate_access_error_issued_p = true;
15910 access = ridpointers[(int) token->keyword];
15912 /* Consume the access-specifier. */
15913 cp_lexer_consume_token (parser->lexer);
15922 /* It is not uncommon to see programs mechanically, erroneously, use
15923 the 'typename' keyword to denote (dependent) qualified types
15924 as base classes. */
15925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15927 token = cp_lexer_peek_token (parser->lexer);
15928 if (!processing_template_decl)
15929 error ("%Hkeyword %<typename%> not allowed outside of templates",
15932 error ("%Hkeyword %<typename%> not allowed in this context "
15933 "(the base class is implicitly a type)",
15935 cp_lexer_consume_token (parser->lexer);
15938 /* Look for the optional `::' operator. */
15939 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15940 /* Look for the nested-name-specifier. The simplest way to
15945 The keyword `typename' is not permitted in a base-specifier or
15946 mem-initializer; in these contexts a qualified name that
15947 depends on a template-parameter is implicitly assumed to be a
15950 is to pretend that we have seen the `typename' keyword at this
15952 cp_parser_nested_name_specifier_opt (parser,
15953 /*typename_keyword_p=*/true,
15954 /*check_dependency_p=*/true,
15956 /*is_declaration=*/true);
15957 /* If the base class is given by a qualified name, assume that names
15958 we see are type names or templates, as appropriate. */
15959 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15960 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15962 /* Finally, look for the class-name. */
15963 type = cp_parser_class_name (parser,
15967 /*check_dependency_p=*/true,
15968 /*class_head_p=*/false,
15969 /*is_declaration=*/true);
15971 if (type == error_mark_node)
15972 return error_mark_node;
15974 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15977 /* Exception handling [gram.exception] */
15979 /* Parse an (optional) exception-specification.
15981 exception-specification:
15982 throw ( type-id-list [opt] )
15984 Returns a TREE_LIST representing the exception-specification. The
15985 TREE_VALUE of each node is a type. */
15988 cp_parser_exception_specification_opt (cp_parser* parser)
15993 /* Peek at the next token. */
15994 token = cp_lexer_peek_token (parser->lexer);
15995 /* If it's not `throw', then there's no exception-specification. */
15996 if (!cp_parser_is_keyword (token, RID_THROW))
15999 /* Consume the `throw'. */
16000 cp_lexer_consume_token (parser->lexer);
16002 /* Look for the `('. */
16003 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16005 /* Peek at the next token. */
16006 token = cp_lexer_peek_token (parser->lexer);
16007 /* If it's not a `)', then there is a type-id-list. */
16008 if (token->type != CPP_CLOSE_PAREN)
16010 const char *saved_message;
16012 /* Types may not be defined in an exception-specification. */
16013 saved_message = parser->type_definition_forbidden_message;
16014 parser->type_definition_forbidden_message
16015 = "types may not be defined in an exception-specification";
16016 /* Parse the type-id-list. */
16017 type_id_list = cp_parser_type_id_list (parser);
16018 /* Restore the saved message. */
16019 parser->type_definition_forbidden_message = saved_message;
16022 type_id_list = empty_except_spec;
16024 /* Look for the `)'. */
16025 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16027 return type_id_list;
16030 /* Parse an (optional) type-id-list.
16034 type-id-list , type-id ... [opt]
16036 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16037 in the order that the types were presented. */
16040 cp_parser_type_id_list (cp_parser* parser)
16042 tree types = NULL_TREE;
16049 /* Get the next type-id. */
16050 type = cp_parser_type_id (parser);
16051 /* Parse the optional ellipsis. */
16052 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16054 /* Consume the `...'. */
16055 cp_lexer_consume_token (parser->lexer);
16057 /* Turn the type into a pack expansion expression. */
16058 type = make_pack_expansion (type);
16060 /* Add it to the list. */
16061 types = add_exception_specifier (types, type, /*complain=*/1);
16062 /* Peek at the next token. */
16063 token = cp_lexer_peek_token (parser->lexer);
16064 /* If it is not a `,', we are done. */
16065 if (token->type != CPP_COMMA)
16067 /* Consume the `,'. */
16068 cp_lexer_consume_token (parser->lexer);
16071 return nreverse (types);
16074 /* Parse a try-block.
16077 try compound-statement handler-seq */
16080 cp_parser_try_block (cp_parser* parser)
16084 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16085 try_block = begin_try_block ();
16086 cp_parser_compound_statement (parser, NULL, true);
16087 finish_try_block (try_block);
16088 cp_parser_handler_seq (parser);
16089 finish_handler_sequence (try_block);
16094 /* Parse a function-try-block.
16096 function-try-block:
16097 try ctor-initializer [opt] function-body handler-seq */
16100 cp_parser_function_try_block (cp_parser* parser)
16102 tree compound_stmt;
16104 bool ctor_initializer_p;
16106 /* Look for the `try' keyword. */
16107 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16109 /* Let the rest of the front end know where we are. */
16110 try_block = begin_function_try_block (&compound_stmt);
16111 /* Parse the function-body. */
16113 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16114 /* We're done with the `try' part. */
16115 finish_function_try_block (try_block);
16116 /* Parse the handlers. */
16117 cp_parser_handler_seq (parser);
16118 /* We're done with the handlers. */
16119 finish_function_handler_sequence (try_block, compound_stmt);
16121 return ctor_initializer_p;
16124 /* Parse a handler-seq.
16127 handler handler-seq [opt] */
16130 cp_parser_handler_seq (cp_parser* parser)
16136 /* Parse the handler. */
16137 cp_parser_handler (parser);
16138 /* Peek at the next token. */
16139 token = cp_lexer_peek_token (parser->lexer);
16140 /* If it's not `catch' then there are no more handlers. */
16141 if (!cp_parser_is_keyword (token, RID_CATCH))
16146 /* Parse a handler.
16149 catch ( exception-declaration ) compound-statement */
16152 cp_parser_handler (cp_parser* parser)
16157 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16158 handler = begin_handler ();
16159 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16160 declaration = cp_parser_exception_declaration (parser);
16161 finish_handler_parms (declaration, handler);
16162 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16163 cp_parser_compound_statement (parser, NULL, false);
16164 finish_handler (handler);
16167 /* Parse an exception-declaration.
16169 exception-declaration:
16170 type-specifier-seq declarator
16171 type-specifier-seq abstract-declarator
16175 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16176 ellipsis variant is used. */
16179 cp_parser_exception_declaration (cp_parser* parser)
16181 cp_decl_specifier_seq type_specifiers;
16182 cp_declarator *declarator;
16183 const char *saved_message;
16185 /* If it's an ellipsis, it's easy to handle. */
16186 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16188 /* Consume the `...' token. */
16189 cp_lexer_consume_token (parser->lexer);
16193 /* Types may not be defined in exception-declarations. */
16194 saved_message = parser->type_definition_forbidden_message;
16195 parser->type_definition_forbidden_message
16196 = "types may not be defined in exception-declarations";
16198 /* Parse the type-specifier-seq. */
16199 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16201 /* If it's a `)', then there is no declarator. */
16202 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16205 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16206 /*ctor_dtor_or_conv_p=*/NULL,
16207 /*parenthesized_p=*/NULL,
16208 /*member_p=*/false);
16210 /* Restore the saved message. */
16211 parser->type_definition_forbidden_message = saved_message;
16213 if (!type_specifiers.any_specifiers_p)
16214 return error_mark_node;
16216 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16219 /* Parse a throw-expression.
16222 throw assignment-expression [opt]
16224 Returns a THROW_EXPR representing the throw-expression. */
16227 cp_parser_throw_expression (cp_parser* parser)
16232 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16233 token = cp_lexer_peek_token (parser->lexer);
16234 /* Figure out whether or not there is an assignment-expression
16235 following the "throw" keyword. */
16236 if (token->type == CPP_COMMA
16237 || token->type == CPP_SEMICOLON
16238 || token->type == CPP_CLOSE_PAREN
16239 || token->type == CPP_CLOSE_SQUARE
16240 || token->type == CPP_CLOSE_BRACE
16241 || token->type == CPP_COLON)
16242 expression = NULL_TREE;
16244 expression = cp_parser_assignment_expression (parser,
16247 return build_throw (expression);
16250 /* GNU Extensions */
16252 /* Parse an (optional) asm-specification.
16255 asm ( string-literal )
16257 If the asm-specification is present, returns a STRING_CST
16258 corresponding to the string-literal. Otherwise, returns
16262 cp_parser_asm_specification_opt (cp_parser* parser)
16265 tree asm_specification;
16267 /* Peek at the next token. */
16268 token = cp_lexer_peek_token (parser->lexer);
16269 /* If the next token isn't the `asm' keyword, then there's no
16270 asm-specification. */
16271 if (!cp_parser_is_keyword (token, RID_ASM))
16274 /* Consume the `asm' token. */
16275 cp_lexer_consume_token (parser->lexer);
16276 /* Look for the `('. */
16277 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16279 /* Look for the string-literal. */
16280 asm_specification = cp_parser_string_literal (parser, false, false);
16282 /* Look for the `)'. */
16283 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16285 return asm_specification;
16288 /* Parse an asm-operand-list.
16292 asm-operand-list , asm-operand
16295 string-literal ( expression )
16296 [ string-literal ] string-literal ( expression )
16298 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16299 each node is the expression. The TREE_PURPOSE is itself a
16300 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16301 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16302 is a STRING_CST for the string literal before the parenthesis. Returns
16303 ERROR_MARK_NODE if any of the operands are invalid. */
16306 cp_parser_asm_operand_list (cp_parser* parser)
16308 tree asm_operands = NULL_TREE;
16309 bool invalid_operands = false;
16313 tree string_literal;
16317 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16319 /* Consume the `[' token. */
16320 cp_lexer_consume_token (parser->lexer);
16321 /* Read the operand name. */
16322 name = cp_parser_identifier (parser);
16323 if (name != error_mark_node)
16324 name = build_string (IDENTIFIER_LENGTH (name),
16325 IDENTIFIER_POINTER (name));
16326 /* Look for the closing `]'. */
16327 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16331 /* Look for the string-literal. */
16332 string_literal = cp_parser_string_literal (parser, false, false);
16334 /* Look for the `('. */
16335 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16336 /* Parse the expression. */
16337 expression = cp_parser_expression (parser, /*cast_p=*/false);
16338 /* Look for the `)'. */
16339 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16341 if (name == error_mark_node
16342 || string_literal == error_mark_node
16343 || expression == error_mark_node)
16344 invalid_operands = true;
16346 /* Add this operand to the list. */
16347 asm_operands = tree_cons (build_tree_list (name, string_literal),
16350 /* If the next token is not a `,', there are no more
16352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16354 /* Consume the `,'. */
16355 cp_lexer_consume_token (parser->lexer);
16358 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16361 /* Parse an asm-clobber-list.
16365 asm-clobber-list , string-literal
16367 Returns a TREE_LIST, indicating the clobbers in the order that they
16368 appeared. The TREE_VALUE of each node is a STRING_CST. */
16371 cp_parser_asm_clobber_list (cp_parser* parser)
16373 tree clobbers = NULL_TREE;
16377 tree string_literal;
16379 /* Look for the string literal. */
16380 string_literal = cp_parser_string_literal (parser, false, false);
16381 /* Add it to the list. */
16382 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16383 /* If the next token is not a `,', then the list is
16385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16387 /* Consume the `,' token. */
16388 cp_lexer_consume_token (parser->lexer);
16394 /* Parse an (optional) series of attributes.
16397 attributes attribute
16400 __attribute__ (( attribute-list [opt] ))
16402 The return value is as for cp_parser_attribute_list. */
16405 cp_parser_attributes_opt (cp_parser* parser)
16407 tree attributes = NULL_TREE;
16412 tree attribute_list;
16414 /* Peek at the next token. */
16415 token = cp_lexer_peek_token (parser->lexer);
16416 /* If it's not `__attribute__', then we're done. */
16417 if (token->keyword != RID_ATTRIBUTE)
16420 /* Consume the `__attribute__' keyword. */
16421 cp_lexer_consume_token (parser->lexer);
16422 /* Look for the two `(' tokens. */
16423 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16424 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16426 /* Peek at the next token. */
16427 token = cp_lexer_peek_token (parser->lexer);
16428 if (token->type != CPP_CLOSE_PAREN)
16429 /* Parse the attribute-list. */
16430 attribute_list = cp_parser_attribute_list (parser);
16432 /* If the next token is a `)', then there is no attribute
16434 attribute_list = NULL;
16436 /* Look for the two `)' tokens. */
16437 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16438 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16440 /* Add these new attributes to the list. */
16441 attributes = chainon (attributes, attribute_list);
16447 /* Parse an attribute-list.
16451 attribute-list , attribute
16455 identifier ( identifier )
16456 identifier ( identifier , expression-list )
16457 identifier ( expression-list )
16459 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16460 to an attribute. The TREE_PURPOSE of each node is the identifier
16461 indicating which attribute is in use. The TREE_VALUE represents
16462 the arguments, if any. */
16465 cp_parser_attribute_list (cp_parser* parser)
16467 tree attribute_list = NULL_TREE;
16468 bool save_translate_strings_p = parser->translate_strings_p;
16470 parser->translate_strings_p = false;
16477 /* Look for the identifier. We also allow keywords here; for
16478 example `__attribute__ ((const))' is legal. */
16479 token = cp_lexer_peek_token (parser->lexer);
16480 if (token->type == CPP_NAME
16481 || token->type == CPP_KEYWORD)
16483 tree arguments = NULL_TREE;
16485 /* Consume the token. */
16486 token = cp_lexer_consume_token (parser->lexer);
16488 /* Save away the identifier that indicates which attribute
16490 identifier = token->u.value;
16491 attribute = build_tree_list (identifier, NULL_TREE);
16493 /* Peek at the next token. */
16494 token = cp_lexer_peek_token (parser->lexer);
16495 /* If it's an `(', then parse the attribute arguments. */
16496 if (token->type == CPP_OPEN_PAREN)
16498 arguments = cp_parser_parenthesized_expression_list
16499 (parser, true, /*cast_p=*/false,
16500 /*allow_expansion_p=*/false,
16501 /*non_constant_p=*/NULL);
16502 /* Save the arguments away. */
16503 TREE_VALUE (attribute) = arguments;
16506 if (arguments != error_mark_node)
16508 /* Add this attribute to the list. */
16509 TREE_CHAIN (attribute) = attribute_list;
16510 attribute_list = attribute;
16513 token = cp_lexer_peek_token (parser->lexer);
16515 /* Now, look for more attributes. If the next token isn't a
16516 `,', we're done. */
16517 if (token->type != CPP_COMMA)
16520 /* Consume the comma and keep going. */
16521 cp_lexer_consume_token (parser->lexer);
16523 parser->translate_strings_p = save_translate_strings_p;
16525 /* We built up the list in reverse order. */
16526 return nreverse (attribute_list);
16529 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16530 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16531 current value of the PEDANTIC flag, regardless of whether or not
16532 the `__extension__' keyword is present. The caller is responsible
16533 for restoring the value of the PEDANTIC flag. */
16536 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16538 /* Save the old value of the PEDANTIC flag. */
16539 *saved_pedantic = pedantic;
16541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16543 /* Consume the `__extension__' token. */
16544 cp_lexer_consume_token (parser->lexer);
16545 /* We're not being pedantic while the `__extension__' keyword is
16555 /* Parse a label declaration.
16558 __label__ label-declarator-seq ;
16560 label-declarator-seq:
16561 identifier , label-declarator-seq
16565 cp_parser_label_declaration (cp_parser* parser)
16567 /* Look for the `__label__' keyword. */
16568 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16574 /* Look for an identifier. */
16575 identifier = cp_parser_identifier (parser);
16576 /* If we failed, stop. */
16577 if (identifier == error_mark_node)
16579 /* Declare it as a label. */
16580 finish_label_decl (identifier);
16581 /* If the next token is a `;', stop. */
16582 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16584 /* Look for the `,' separating the label declarations. */
16585 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16588 /* Look for the final `;'. */
16589 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16592 /* Support Functions */
16594 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16595 NAME should have one of the representations used for an
16596 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16597 is returned. If PARSER->SCOPE is a dependent type, then a
16598 SCOPE_REF is returned.
16600 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16601 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16602 was formed. Abstractly, such entities should not be passed to this
16603 function, because they do not need to be looked up, but it is
16604 simpler to check for this special case here, rather than at the
16607 In cases not explicitly covered above, this function returns a
16608 DECL, OVERLOAD, or baselink representing the result of the lookup.
16609 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16612 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16613 (e.g., "struct") that was used. In that case bindings that do not
16614 refer to types are ignored.
16616 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16619 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16622 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16625 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16626 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16627 NULL_TREE otherwise. */
16630 cp_parser_lookup_name (cp_parser *parser, tree name,
16631 enum tag_types tag_type,
16634 bool check_dependency,
16635 tree *ambiguous_decls,
16636 location_t name_location)
16640 tree object_type = parser->context->object_type;
16642 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16643 flags |= LOOKUP_COMPLAIN;
16645 /* Assume that the lookup will be unambiguous. */
16646 if (ambiguous_decls)
16647 *ambiguous_decls = NULL_TREE;
16649 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16650 no longer valid. Note that if we are parsing tentatively, and
16651 the parse fails, OBJECT_TYPE will be automatically restored. */
16652 parser->context->object_type = NULL_TREE;
16654 if (name == error_mark_node)
16655 return error_mark_node;
16657 /* A template-id has already been resolved; there is no lookup to
16659 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16661 if (BASELINK_P (name))
16663 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16664 == TEMPLATE_ID_EXPR);
16668 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16669 it should already have been checked to make sure that the name
16670 used matches the type being destroyed. */
16671 if (TREE_CODE (name) == BIT_NOT_EXPR)
16675 /* Figure out to which type this destructor applies. */
16677 type = parser->scope;
16678 else if (object_type)
16679 type = object_type;
16681 type = current_class_type;
16682 /* If that's not a class type, there is no destructor. */
16683 if (!type || !CLASS_TYPE_P (type))
16684 return error_mark_node;
16685 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16686 lazily_declare_fn (sfk_destructor, type);
16687 if (!CLASSTYPE_DESTRUCTORS (type))
16688 return error_mark_node;
16689 /* If it was a class type, return the destructor. */
16690 return CLASSTYPE_DESTRUCTORS (type);
16693 /* By this point, the NAME should be an ordinary identifier. If
16694 the id-expression was a qualified name, the qualifying scope is
16695 stored in PARSER->SCOPE at this point. */
16696 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16698 /* Perform the lookup. */
16703 if (parser->scope == error_mark_node)
16704 return error_mark_node;
16706 /* If the SCOPE is dependent, the lookup must be deferred until
16707 the template is instantiated -- unless we are explicitly
16708 looking up names in uninstantiated templates. Even then, we
16709 cannot look up the name if the scope is not a class type; it
16710 might, for example, be a template type parameter. */
16711 dependent_p = (TYPE_P (parser->scope)
16712 && !(parser->in_declarator_p
16713 && currently_open_class (parser->scope))
16714 && dependent_type_p (parser->scope));
16715 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16722 /* The resolution to Core Issue 180 says that `struct
16723 A::B' should be considered a type-name, even if `A'
16725 type = make_typename_type (parser->scope, name, tag_type,
16726 /*complain=*/tf_error);
16727 decl = TYPE_NAME (type);
16729 else if (is_template
16730 && (cp_parser_next_token_ends_template_argument_p (parser)
16731 || cp_lexer_next_token_is (parser->lexer,
16733 decl = make_unbound_class_template (parser->scope,
16735 /*complain=*/tf_error);
16737 decl = build_qualified_name (/*type=*/NULL_TREE,
16738 parser->scope, name,
16743 tree pushed_scope = NULL_TREE;
16745 /* If PARSER->SCOPE is a dependent type, then it must be a
16746 class type, and we must not be checking dependencies;
16747 otherwise, we would have processed this lookup above. So
16748 that PARSER->SCOPE is not considered a dependent base by
16749 lookup_member, we must enter the scope here. */
16751 pushed_scope = push_scope (parser->scope);
16752 /* If the PARSER->SCOPE is a template specialization, it
16753 may be instantiated during name lookup. In that case,
16754 errors may be issued. Even if we rollback the current
16755 tentative parse, those errors are valid. */
16756 decl = lookup_qualified_name (parser->scope, name,
16757 tag_type != none_type,
16758 /*complain=*/true);
16760 /* If we have a single function from a using decl, pull it out. */
16762 && TREE_CODE (decl) == OVERLOAD
16763 && !really_overloaded_fn (decl))
16764 decl = OVL_FUNCTION (decl);
16767 pop_scope (pushed_scope);
16769 parser->qualifying_scope = parser->scope;
16770 parser->object_scope = NULL_TREE;
16772 else if (object_type)
16774 tree object_decl = NULL_TREE;
16775 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16776 OBJECT_TYPE is not a class. */
16777 if (CLASS_TYPE_P (object_type))
16778 /* If the OBJECT_TYPE is a template specialization, it may
16779 be instantiated during name lookup. In that case, errors
16780 may be issued. Even if we rollback the current tentative
16781 parse, those errors are valid. */
16782 object_decl = lookup_member (object_type,
16785 tag_type != none_type);
16786 /* Look it up in the enclosing context, too. */
16787 decl = lookup_name_real (name, tag_type != none_type,
16789 /*block_p=*/true, is_namespace, flags);
16790 parser->object_scope = object_type;
16791 parser->qualifying_scope = NULL_TREE;
16793 decl = object_decl;
16797 decl = lookup_name_real (name, tag_type != none_type,
16799 /*block_p=*/true, is_namespace, flags);
16800 parser->qualifying_scope = NULL_TREE;
16801 parser->object_scope = NULL_TREE;
16804 /* If the lookup failed, let our caller know. */
16805 if (!decl || decl == error_mark_node)
16806 return error_mark_node;
16808 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16809 if (TREE_CODE (decl) == TREE_LIST)
16811 if (ambiguous_decls)
16812 *ambiguous_decls = decl;
16813 /* The error message we have to print is too complicated for
16814 cp_parser_error, so we incorporate its actions directly. */
16815 if (!cp_parser_simulate_error (parser))
16817 error ("%Hreference to %qD is ambiguous",
16818 &name_location, name);
16819 print_candidates (decl);
16821 return error_mark_node;
16824 gcc_assert (DECL_P (decl)
16825 || TREE_CODE (decl) == OVERLOAD
16826 || TREE_CODE (decl) == SCOPE_REF
16827 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16828 || BASELINK_P (decl));
16830 /* If we have resolved the name of a member declaration, check to
16831 see if the declaration is accessible. When the name resolves to
16832 set of overloaded functions, accessibility is checked when
16833 overload resolution is done.
16835 During an explicit instantiation, access is not checked at all,
16836 as per [temp.explicit]. */
16838 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16843 /* Like cp_parser_lookup_name, but for use in the typical case where
16844 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16845 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16848 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16850 return cp_parser_lookup_name (parser, name,
16852 /*is_template=*/false,
16853 /*is_namespace=*/false,
16854 /*check_dependency=*/true,
16855 /*ambiguous_decls=*/NULL,
16859 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16860 the current context, return the TYPE_DECL. If TAG_NAME_P is
16861 true, the DECL indicates the class being defined in a class-head,
16862 or declared in an elaborated-type-specifier.
16864 Otherwise, return DECL. */
16867 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16869 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16870 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16873 template <typename T> struct B;
16876 template <typename T> struct A::B {};
16878 Similarly, in an elaborated-type-specifier:
16880 namespace N { struct X{}; }
16883 template <typename T> friend struct N::X;
16886 However, if the DECL refers to a class type, and we are in
16887 the scope of the class, then the name lookup automatically
16888 finds the TYPE_DECL created by build_self_reference rather
16889 than a TEMPLATE_DECL. For example, in:
16891 template <class T> struct S {
16895 there is no need to handle such case. */
16897 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16898 return DECL_TEMPLATE_RESULT (decl);
16903 /* If too many, or too few, template-parameter lists apply to the
16904 declarator, issue an error message. Returns TRUE if all went well,
16905 and FALSE otherwise. */
16908 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16909 cp_declarator *declarator,
16910 location_t declarator_location)
16912 unsigned num_templates;
16914 /* We haven't seen any classes that involve template parameters yet. */
16917 switch (declarator->kind)
16920 if (declarator->u.id.qualifying_scope)
16925 scope = declarator->u.id.qualifying_scope;
16926 member = declarator->u.id.unqualified_name;
16928 while (scope && CLASS_TYPE_P (scope))
16930 /* You're supposed to have one `template <...>'
16931 for every template class, but you don't need one
16932 for a full specialization. For example:
16934 template <class T> struct S{};
16935 template <> struct S<int> { void f(); };
16936 void S<int>::f () {}
16938 is correct; there shouldn't be a `template <>' for
16939 the definition of `S<int>::f'. */
16940 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16941 /* If SCOPE does not have template information of any
16942 kind, then it is not a template, nor is it nested
16943 within a template. */
16945 if (explicit_class_specialization_p (scope))
16947 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16950 scope = TYPE_CONTEXT (scope);
16953 else if (TREE_CODE (declarator->u.id.unqualified_name)
16954 == TEMPLATE_ID_EXPR)
16955 /* If the DECLARATOR has the form `X<y>' then it uses one
16956 additional level of template parameters. */
16959 return cp_parser_check_template_parameters (parser,
16961 declarator_location);
16966 case cdk_reference:
16968 return (cp_parser_check_declarator_template_parameters
16969 (parser, declarator->declarator, declarator_location));
16975 gcc_unreachable ();
16980 /* NUM_TEMPLATES were used in the current declaration. If that is
16981 invalid, return FALSE and issue an error messages. Otherwise,
16985 cp_parser_check_template_parameters (cp_parser* parser,
16986 unsigned num_templates,
16987 location_t location)
16989 /* If there are more template classes than parameter lists, we have
16992 template <class T> void S<T>::R<T>::f (); */
16993 if (parser->num_template_parameter_lists < num_templates)
16995 error ("%Htoo few template-parameter-lists", &location);
16998 /* If there are the same number of template classes and parameter
16999 lists, that's OK. */
17000 if (parser->num_template_parameter_lists == num_templates)
17002 /* If there are more, but only one more, then we are referring to a
17003 member template. That's OK too. */
17004 if (parser->num_template_parameter_lists == num_templates + 1)
17006 /* Otherwise, there are too many template parameter lists. We have
17009 template <class T> template <class U> void S::f(); */
17010 error ("%Htoo many template-parameter-lists", &location);
17014 /* Parse an optional `::' token indicating that the following name is
17015 from the global namespace. If so, PARSER->SCOPE is set to the
17016 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17017 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17018 Returns the new value of PARSER->SCOPE, if the `::' token is
17019 present, and NULL_TREE otherwise. */
17022 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17026 /* Peek at the next token. */
17027 token = cp_lexer_peek_token (parser->lexer);
17028 /* If we're looking at a `::' token then we're starting from the
17029 global namespace, not our current location. */
17030 if (token->type == CPP_SCOPE)
17032 /* Consume the `::' token. */
17033 cp_lexer_consume_token (parser->lexer);
17034 /* Set the SCOPE so that we know where to start the lookup. */
17035 parser->scope = global_namespace;
17036 parser->qualifying_scope = global_namespace;
17037 parser->object_scope = NULL_TREE;
17039 return parser->scope;
17041 else if (!current_scope_valid_p)
17043 parser->scope = NULL_TREE;
17044 parser->qualifying_scope = NULL_TREE;
17045 parser->object_scope = NULL_TREE;
17051 /* Returns TRUE if the upcoming token sequence is the start of a
17052 constructor declarator. If FRIEND_P is true, the declarator is
17053 preceded by the `friend' specifier. */
17056 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17058 bool constructor_p;
17059 tree type_decl = NULL_TREE;
17060 bool nested_name_p;
17061 cp_token *next_token;
17063 /* The common case is that this is not a constructor declarator, so
17064 try to avoid doing lots of work if at all possible. It's not
17065 valid declare a constructor at function scope. */
17066 if (parser->in_function_body)
17068 /* And only certain tokens can begin a constructor declarator. */
17069 next_token = cp_lexer_peek_token (parser->lexer);
17070 if (next_token->type != CPP_NAME
17071 && next_token->type != CPP_SCOPE
17072 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17073 && next_token->type != CPP_TEMPLATE_ID)
17076 /* Parse tentatively; we are going to roll back all of the tokens
17078 cp_parser_parse_tentatively (parser);
17079 /* Assume that we are looking at a constructor declarator. */
17080 constructor_p = true;
17082 /* Look for the optional `::' operator. */
17083 cp_parser_global_scope_opt (parser,
17084 /*current_scope_valid_p=*/false);
17085 /* Look for the nested-name-specifier. */
17087 = (cp_parser_nested_name_specifier_opt (parser,
17088 /*typename_keyword_p=*/false,
17089 /*check_dependency_p=*/false,
17091 /*is_declaration=*/false)
17093 /* Outside of a class-specifier, there must be a
17094 nested-name-specifier. */
17095 if (!nested_name_p &&
17096 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17098 constructor_p = false;
17099 /* If we still think that this might be a constructor-declarator,
17100 look for a class-name. */
17105 template <typename T> struct S { S(); };
17106 template <typename T> S<T>::S ();
17108 we must recognize that the nested `S' names a class.
17111 template <typename T> S<T>::S<T> ();
17113 we must recognize that the nested `S' names a template. */
17114 type_decl = cp_parser_class_name (parser,
17115 /*typename_keyword_p=*/false,
17116 /*template_keyword_p=*/false,
17118 /*check_dependency_p=*/false,
17119 /*class_head_p=*/false,
17120 /*is_declaration=*/false);
17121 /* If there was no class-name, then this is not a constructor. */
17122 constructor_p = !cp_parser_error_occurred (parser);
17125 /* If we're still considering a constructor, we have to see a `(',
17126 to begin the parameter-declaration-clause, followed by either a
17127 `)', an `...', or a decl-specifier. We need to check for a
17128 type-specifier to avoid being fooled into thinking that:
17132 is a constructor. (It is actually a function named `f' that
17133 takes one parameter (of type `int') and returns a value of type
17136 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17139 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17140 /* A parameter declaration begins with a decl-specifier,
17141 which is either the "attribute" keyword, a storage class
17142 specifier, or (usually) a type-specifier. */
17143 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17146 tree pushed_scope = NULL_TREE;
17147 unsigned saved_num_template_parameter_lists;
17149 /* Names appearing in the type-specifier should be looked up
17150 in the scope of the class. */
17151 if (current_class_type)
17155 type = TREE_TYPE (type_decl);
17156 if (TREE_CODE (type) == TYPENAME_TYPE)
17158 type = resolve_typename_type (type,
17159 /*only_current_p=*/false);
17160 if (TREE_CODE (type) == TYPENAME_TYPE)
17162 cp_parser_abort_tentative_parse (parser);
17166 pushed_scope = push_scope (type);
17169 /* Inside the constructor parameter list, surrounding
17170 template-parameter-lists do not apply. */
17171 saved_num_template_parameter_lists
17172 = parser->num_template_parameter_lists;
17173 parser->num_template_parameter_lists = 0;
17175 /* Look for the type-specifier. */
17176 cp_parser_type_specifier (parser,
17177 CP_PARSER_FLAGS_NONE,
17178 /*decl_specs=*/NULL,
17179 /*is_declarator=*/true,
17180 /*declares_class_or_enum=*/NULL,
17181 /*is_cv_qualifier=*/NULL);
17183 parser->num_template_parameter_lists
17184 = saved_num_template_parameter_lists;
17186 /* Leave the scope of the class. */
17188 pop_scope (pushed_scope);
17190 constructor_p = !cp_parser_error_occurred (parser);
17194 constructor_p = false;
17195 /* We did not really want to consume any tokens. */
17196 cp_parser_abort_tentative_parse (parser);
17198 return constructor_p;
17201 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17202 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17203 they must be performed once we are in the scope of the function.
17205 Returns the function defined. */
17208 cp_parser_function_definition_from_specifiers_and_declarator
17209 (cp_parser* parser,
17210 cp_decl_specifier_seq *decl_specifiers,
17212 const cp_declarator *declarator)
17217 /* Begin the function-definition. */
17218 success_p = start_function (decl_specifiers, declarator, attributes);
17220 /* The things we're about to see are not directly qualified by any
17221 template headers we've seen thus far. */
17222 reset_specialization ();
17224 /* If there were names looked up in the decl-specifier-seq that we
17225 did not check, check them now. We must wait until we are in the
17226 scope of the function to perform the checks, since the function
17227 might be a friend. */
17228 perform_deferred_access_checks ();
17232 /* Skip the entire function. */
17233 cp_parser_skip_to_end_of_block_or_statement (parser);
17234 fn = error_mark_node;
17236 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17238 /* Seen already, skip it. An error message has already been output. */
17239 cp_parser_skip_to_end_of_block_or_statement (parser);
17240 fn = current_function_decl;
17241 current_function_decl = NULL_TREE;
17242 /* If this is a function from a class, pop the nested class. */
17243 if (current_class_name)
17244 pop_nested_class ();
17247 fn = cp_parser_function_definition_after_declarator (parser,
17248 /*inline_p=*/false);
17253 /* Parse the part of a function-definition that follows the
17254 declarator. INLINE_P is TRUE iff this function is an inline
17255 function defined with a class-specifier.
17257 Returns the function defined. */
17260 cp_parser_function_definition_after_declarator (cp_parser* parser,
17264 bool ctor_initializer_p = false;
17265 bool saved_in_unbraced_linkage_specification_p;
17266 bool saved_in_function_body;
17267 unsigned saved_num_template_parameter_lists;
17270 saved_in_function_body = parser->in_function_body;
17271 parser->in_function_body = true;
17272 /* If the next token is `return', then the code may be trying to
17273 make use of the "named return value" extension that G++ used to
17275 token = cp_lexer_peek_token (parser->lexer);
17276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17278 /* Consume the `return' keyword. */
17279 cp_lexer_consume_token (parser->lexer);
17280 /* Look for the identifier that indicates what value is to be
17282 cp_parser_identifier (parser);
17283 /* Issue an error message. */
17284 error ("%Hnamed return values are no longer supported",
17286 /* Skip tokens until we reach the start of the function body. */
17289 cp_token *token = cp_lexer_peek_token (parser->lexer);
17290 if (token->type == CPP_OPEN_BRACE
17291 || token->type == CPP_EOF
17292 || token->type == CPP_PRAGMA_EOL)
17294 cp_lexer_consume_token (parser->lexer);
17297 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17298 anything declared inside `f'. */
17299 saved_in_unbraced_linkage_specification_p
17300 = parser->in_unbraced_linkage_specification_p;
17301 parser->in_unbraced_linkage_specification_p = false;
17302 /* Inside the function, surrounding template-parameter-lists do not
17304 saved_num_template_parameter_lists
17305 = parser->num_template_parameter_lists;
17306 parser->num_template_parameter_lists = 0;
17307 /* If the next token is `try', then we are looking at a
17308 function-try-block. */
17309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17310 ctor_initializer_p = cp_parser_function_try_block (parser);
17311 /* A function-try-block includes the function-body, so we only do
17312 this next part if we're not processing a function-try-block. */
17315 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17317 /* Finish the function. */
17318 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17319 (inline_p ? 2 : 0));
17320 /* Generate code for it, if necessary. */
17321 expand_or_defer_fn (fn);
17322 /* Restore the saved values. */
17323 parser->in_unbraced_linkage_specification_p
17324 = saved_in_unbraced_linkage_specification_p;
17325 parser->num_template_parameter_lists
17326 = saved_num_template_parameter_lists;
17327 parser->in_function_body = saved_in_function_body;
17332 /* Parse a template-declaration, assuming that the `export' (and
17333 `extern') keywords, if present, has already been scanned. MEMBER_P
17334 is as for cp_parser_template_declaration. */
17337 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17339 tree decl = NULL_TREE;
17340 VEC (deferred_access_check,gc) *checks;
17341 tree parameter_list;
17342 bool friend_p = false;
17343 bool need_lang_pop;
17346 /* Look for the `template' keyword. */
17347 token = cp_lexer_peek_token (parser->lexer);
17348 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17352 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17354 if (at_class_scope_p () && current_function_decl)
17356 /* 14.5.2.2 [temp.mem]
17358 A local class shall not have member templates. */
17359 error ("%Hinvalid declaration of member template in local class",
17361 cp_parser_skip_to_end_of_block_or_statement (parser);
17366 A template ... shall not have C linkage. */
17367 if (current_lang_name == lang_name_c)
17369 error ("%Htemplate with C linkage", &token->location);
17370 /* Give it C++ linkage to avoid confusing other parts of the
17372 push_lang_context (lang_name_cplusplus);
17373 need_lang_pop = true;
17376 need_lang_pop = false;
17378 /* We cannot perform access checks on the template parameter
17379 declarations until we know what is being declared, just as we
17380 cannot check the decl-specifier list. */
17381 push_deferring_access_checks (dk_deferred);
17383 /* If the next token is `>', then we have an invalid
17384 specialization. Rather than complain about an invalid template
17385 parameter, issue an error message here. */
17386 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17388 cp_parser_error (parser, "invalid explicit specialization");
17389 begin_specialization ();
17390 parameter_list = NULL_TREE;
17393 /* Parse the template parameters. */
17394 parameter_list = cp_parser_template_parameter_list (parser);
17396 /* Get the deferred access checks from the parameter list. These
17397 will be checked once we know what is being declared, as for a
17398 member template the checks must be performed in the scope of the
17399 class containing the member. */
17400 checks = get_deferred_access_checks ();
17402 /* Look for the `>'. */
17403 cp_parser_skip_to_end_of_template_parameter_list (parser);
17404 /* We just processed one more parameter list. */
17405 ++parser->num_template_parameter_lists;
17406 /* If the next token is `template', there are more template
17408 if (cp_lexer_next_token_is_keyword (parser->lexer,
17410 cp_parser_template_declaration_after_export (parser, member_p);
17413 /* There are no access checks when parsing a template, as we do not
17414 know if a specialization will be a friend. */
17415 push_deferring_access_checks (dk_no_check);
17416 token = cp_lexer_peek_token (parser->lexer);
17417 decl = cp_parser_single_declaration (parser,
17420 /*explicit_specialization_p=*/false,
17422 pop_deferring_access_checks ();
17424 /* If this is a member template declaration, let the front
17426 if (member_p && !friend_p && decl)
17428 if (TREE_CODE (decl) == TYPE_DECL)
17429 cp_parser_check_access_in_redeclaration (decl, token->location);
17431 decl = finish_member_template_decl (decl);
17433 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17434 make_friend_class (current_class_type, TREE_TYPE (decl),
17435 /*complain=*/true);
17437 /* We are done with the current parameter list. */
17438 --parser->num_template_parameter_lists;
17440 pop_deferring_access_checks ();
17443 finish_template_decl (parameter_list);
17445 /* Register member declarations. */
17446 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17447 finish_member_declaration (decl);
17448 /* For the erroneous case of a template with C linkage, we pushed an
17449 implicit C++ linkage scope; exit that scope now. */
17451 pop_lang_context ();
17452 /* If DECL is a function template, we must return to parse it later.
17453 (Even though there is no definition, there might be default
17454 arguments that need handling.) */
17455 if (member_p && decl
17456 && (TREE_CODE (decl) == FUNCTION_DECL
17457 || DECL_FUNCTION_TEMPLATE_P (decl)))
17458 TREE_VALUE (parser->unparsed_functions_queues)
17459 = tree_cons (NULL_TREE, decl,
17460 TREE_VALUE (parser->unparsed_functions_queues));
17463 /* Perform the deferred access checks from a template-parameter-list.
17464 CHECKS is a TREE_LIST of access checks, as returned by
17465 get_deferred_access_checks. */
17468 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17470 ++processing_template_parmlist;
17471 perform_access_checks (checks);
17472 --processing_template_parmlist;
17475 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17476 `function-definition' sequence. MEMBER_P is true, this declaration
17477 appears in a class scope.
17479 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17480 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17483 cp_parser_single_declaration (cp_parser* parser,
17484 VEC (deferred_access_check,gc)* checks,
17486 bool explicit_specialization_p,
17489 int declares_class_or_enum;
17490 tree decl = NULL_TREE;
17491 cp_decl_specifier_seq decl_specifiers;
17492 bool function_definition_p = false;
17493 cp_token *decl_spec_token_start;
17495 /* This function is only used when processing a template
17497 gcc_assert (innermost_scope_kind () == sk_template_parms
17498 || innermost_scope_kind () == sk_template_spec);
17500 /* Defer access checks until we know what is being declared. */
17501 push_deferring_access_checks (dk_deferred);
17503 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17505 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17506 cp_parser_decl_specifier_seq (parser,
17507 CP_PARSER_FLAGS_OPTIONAL,
17509 &declares_class_or_enum);
17511 *friend_p = cp_parser_friend_p (&decl_specifiers);
17513 /* There are no template typedefs. */
17514 if (decl_specifiers.specs[(int) ds_typedef])
17516 error ("%Htemplate declaration of %qs",
17517 &decl_spec_token_start->location, "typedef");
17518 decl = error_mark_node;
17521 /* Gather up the access checks that occurred the
17522 decl-specifier-seq. */
17523 stop_deferring_access_checks ();
17525 /* Check for the declaration of a template class. */
17526 if (declares_class_or_enum)
17528 if (cp_parser_declares_only_class_p (parser))
17530 decl = shadow_tag (&decl_specifiers);
17535 friend template <typename T> struct A<T>::B;
17538 A<T>::B will be represented by a TYPENAME_TYPE, and
17539 therefore not recognized by shadow_tag. */
17540 if (friend_p && *friend_p
17542 && decl_specifiers.type
17543 && TYPE_P (decl_specifiers.type))
17544 decl = decl_specifiers.type;
17546 if (decl && decl != error_mark_node)
17547 decl = TYPE_NAME (decl);
17549 decl = error_mark_node;
17551 /* Perform access checks for template parameters. */
17552 cp_parser_perform_template_parameter_access_checks (checks);
17555 /* If it's not a template class, try for a template function. If
17556 the next token is a `;', then this declaration does not declare
17557 anything. But, if there were errors in the decl-specifiers, then
17558 the error might well have come from an attempted class-specifier.
17559 In that case, there's no need to warn about a missing declarator. */
17561 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17562 || decl_specifiers.type != error_mark_node))
17564 decl = cp_parser_init_declarator (parser,
17567 /*function_definition_allowed_p=*/true,
17569 declares_class_or_enum,
17570 &function_definition_p);
17572 /* 7.1.1-1 [dcl.stc]
17574 A storage-class-specifier shall not be specified in an explicit
17575 specialization... */
17577 && explicit_specialization_p
17578 && decl_specifiers.storage_class != sc_none)
17580 error ("%Hexplicit template specialization cannot have a storage class",
17581 &decl_spec_token_start->location);
17582 decl = error_mark_node;
17586 pop_deferring_access_checks ();
17588 /* Clear any current qualification; whatever comes next is the start
17589 of something new. */
17590 parser->scope = NULL_TREE;
17591 parser->qualifying_scope = NULL_TREE;
17592 parser->object_scope = NULL_TREE;
17593 /* Look for a trailing `;' after the declaration. */
17594 if (!function_definition_p
17595 && (decl == error_mark_node
17596 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17597 cp_parser_skip_to_end_of_block_or_statement (parser);
17602 /* Parse a cast-expression that is not the operand of a unary "&". */
17605 cp_parser_simple_cast_expression (cp_parser *parser)
17607 return cp_parser_cast_expression (parser, /*address_p=*/false,
17611 /* Parse a functional cast to TYPE. Returns an expression
17612 representing the cast. */
17615 cp_parser_functional_cast (cp_parser* parser, tree type)
17617 tree expression_list;
17621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17623 maybe_warn_cpp0x ("extended initializer lists");
17624 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17625 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17626 if (TREE_CODE (type) == TYPE_DECL)
17627 type = TREE_TYPE (type);
17628 return finish_compound_literal (type, expression_list);
17632 = cp_parser_parenthesized_expression_list (parser, false,
17634 /*allow_expansion_p=*/true,
17635 /*non_constant_p=*/NULL);
17637 cast = build_functional_cast (type, expression_list,
17638 tf_warning_or_error);
17639 /* [expr.const]/1: In an integral constant expression "only type
17640 conversions to integral or enumeration type can be used". */
17641 if (TREE_CODE (type) == TYPE_DECL)
17642 type = TREE_TYPE (type);
17643 if (cast != error_mark_node
17644 && !cast_valid_in_integral_constant_expression_p (type)
17645 && (cp_parser_non_integral_constant_expression
17646 (parser, "a call to a constructor")))
17647 return error_mark_node;
17651 /* Save the tokens that make up the body of a member function defined
17652 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17653 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17654 specifiers applied to the declaration. Returns the FUNCTION_DECL
17655 for the member function. */
17658 cp_parser_save_member_function_body (cp_parser* parser,
17659 cp_decl_specifier_seq *decl_specifiers,
17660 cp_declarator *declarator,
17667 /* Create the function-declaration. */
17668 fn = start_method (decl_specifiers, declarator, attributes);
17669 /* If something went badly wrong, bail out now. */
17670 if (fn == error_mark_node)
17672 /* If there's a function-body, skip it. */
17673 if (cp_parser_token_starts_function_definition_p
17674 (cp_lexer_peek_token (parser->lexer)))
17675 cp_parser_skip_to_end_of_block_or_statement (parser);
17676 return error_mark_node;
17679 /* Remember it, if there default args to post process. */
17680 cp_parser_save_default_args (parser, fn);
17682 /* Save away the tokens that make up the body of the
17684 first = parser->lexer->next_token;
17685 /* We can have braced-init-list mem-initializers before the fn body. */
17686 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17688 cp_lexer_consume_token (parser->lexer);
17689 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17690 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17692 /* cache_group will stop after an un-nested { } pair, too. */
17693 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17696 /* variadic mem-inits have ... after the ')'. */
17697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17698 cp_lexer_consume_token (parser->lexer);
17701 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17702 /* Handle function try blocks. */
17703 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17704 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17705 last = parser->lexer->next_token;
17707 /* Save away the inline definition; we will process it when the
17708 class is complete. */
17709 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17710 DECL_PENDING_INLINE_P (fn) = 1;
17712 /* We need to know that this was defined in the class, so that
17713 friend templates are handled correctly. */
17714 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17716 /* We're done with the inline definition. */
17717 finish_method (fn);
17719 /* Add FN to the queue of functions to be parsed later. */
17720 TREE_VALUE (parser->unparsed_functions_queues)
17721 = tree_cons (NULL_TREE, fn,
17722 TREE_VALUE (parser->unparsed_functions_queues));
17727 /* Parse a template-argument-list, as well as the trailing ">" (but
17728 not the opening ">"). See cp_parser_template_argument_list for the
17732 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17736 tree saved_qualifying_scope;
17737 tree saved_object_scope;
17738 bool saved_greater_than_is_operator_p;
17739 bool saved_skip_evaluation;
17743 When parsing a template-id, the first non-nested `>' is taken as
17744 the end of the template-argument-list rather than a greater-than
17746 saved_greater_than_is_operator_p
17747 = parser->greater_than_is_operator_p;
17748 parser->greater_than_is_operator_p = false;
17749 /* Parsing the argument list may modify SCOPE, so we save it
17751 saved_scope = parser->scope;
17752 saved_qualifying_scope = parser->qualifying_scope;
17753 saved_object_scope = parser->object_scope;
17754 /* We need to evaluate the template arguments, even though this
17755 template-id may be nested within a "sizeof". */
17756 saved_skip_evaluation = skip_evaluation;
17757 skip_evaluation = false;
17758 /* Parse the template-argument-list itself. */
17759 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17760 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17761 arguments = NULL_TREE;
17763 arguments = cp_parser_template_argument_list (parser);
17764 /* Look for the `>' that ends the template-argument-list. If we find
17765 a '>>' instead, it's probably just a typo. */
17766 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17768 if (cxx_dialect != cxx98)
17770 /* In C++0x, a `>>' in a template argument list or cast
17771 expression is considered to be two separate `>'
17772 tokens. So, change the current token to a `>', but don't
17773 consume it: it will be consumed later when the outer
17774 template argument list (or cast expression) is parsed.
17775 Note that this replacement of `>' for `>>' is necessary
17776 even if we are parsing tentatively: in the tentative
17777 case, after calling
17778 cp_parser_enclosed_template_argument_list we will always
17779 throw away all of the template arguments and the first
17780 closing `>', either because the template argument list
17781 was erroneous or because we are replacing those tokens
17782 with a CPP_TEMPLATE_ID token. The second `>' (which will
17783 not have been thrown away) is needed either to close an
17784 outer template argument list or to complete a new-style
17786 cp_token *token = cp_lexer_peek_token (parser->lexer);
17787 token->type = CPP_GREATER;
17789 else if (!saved_greater_than_is_operator_p)
17791 /* If we're in a nested template argument list, the '>>' has
17792 to be a typo for '> >'. We emit the error message, but we
17793 continue parsing and we push a '>' as next token, so that
17794 the argument list will be parsed correctly. Note that the
17795 global source location is still on the token before the
17796 '>>', so we need to say explicitly where we want it. */
17797 cp_token *token = cp_lexer_peek_token (parser->lexer);
17798 error ("%H%<>>%> should be %<> >%> "
17799 "within a nested template argument list",
17802 token->type = CPP_GREATER;
17806 /* If this is not a nested template argument list, the '>>'
17807 is a typo for '>'. Emit an error message and continue.
17808 Same deal about the token location, but here we can get it
17809 right by consuming the '>>' before issuing the diagnostic. */
17810 cp_token *token = cp_lexer_consume_token (parser->lexer);
17811 error ("%Hspurious %<>>%>, use %<>%> to terminate "
17812 "a template argument list", &token->location);
17816 cp_parser_skip_to_end_of_template_parameter_list (parser);
17817 /* The `>' token might be a greater-than operator again now. */
17818 parser->greater_than_is_operator_p
17819 = saved_greater_than_is_operator_p;
17820 /* Restore the SAVED_SCOPE. */
17821 parser->scope = saved_scope;
17822 parser->qualifying_scope = saved_qualifying_scope;
17823 parser->object_scope = saved_object_scope;
17824 skip_evaluation = saved_skip_evaluation;
17829 /* MEMBER_FUNCTION is a member function, or a friend. If default
17830 arguments, or the body of the function have not yet been parsed,
17834 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17836 /* If this member is a template, get the underlying
17838 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17839 member_function = DECL_TEMPLATE_RESULT (member_function);
17841 /* There should not be any class definitions in progress at this
17842 point; the bodies of members are only parsed outside of all class
17844 gcc_assert (parser->num_classes_being_defined == 0);
17845 /* While we're parsing the member functions we might encounter more
17846 classes. We want to handle them right away, but we don't want
17847 them getting mixed up with functions that are currently in the
17849 parser->unparsed_functions_queues
17850 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17852 /* Make sure that any template parameters are in scope. */
17853 maybe_begin_member_template_processing (member_function);
17855 /* If the body of the function has not yet been parsed, parse it
17857 if (DECL_PENDING_INLINE_P (member_function))
17859 tree function_scope;
17860 cp_token_cache *tokens;
17862 /* The function is no longer pending; we are processing it. */
17863 tokens = DECL_PENDING_INLINE_INFO (member_function);
17864 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17865 DECL_PENDING_INLINE_P (member_function) = 0;
17867 /* If this is a local class, enter the scope of the containing
17869 function_scope = current_function_decl;
17870 if (function_scope)
17871 push_function_context ();
17873 /* Push the body of the function onto the lexer stack. */
17874 cp_parser_push_lexer_for_tokens (parser, tokens);
17876 /* Let the front end know that we going to be defining this
17878 start_preparsed_function (member_function, NULL_TREE,
17879 SF_PRE_PARSED | SF_INCLASS_INLINE);
17881 /* Don't do access checking if it is a templated function. */
17882 if (processing_template_decl)
17883 push_deferring_access_checks (dk_no_check);
17885 /* Now, parse the body of the function. */
17886 cp_parser_function_definition_after_declarator (parser,
17887 /*inline_p=*/true);
17889 if (processing_template_decl)
17890 pop_deferring_access_checks ();
17892 /* Leave the scope of the containing function. */
17893 if (function_scope)
17894 pop_function_context ();
17895 cp_parser_pop_lexer (parser);
17898 /* Remove any template parameters from the symbol table. */
17899 maybe_end_member_template_processing ();
17901 /* Restore the queue. */
17902 parser->unparsed_functions_queues
17903 = TREE_CHAIN (parser->unparsed_functions_queues);
17906 /* If DECL contains any default args, remember it on the unparsed
17907 functions queue. */
17910 cp_parser_save_default_args (cp_parser* parser, tree decl)
17914 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17916 probe = TREE_CHAIN (probe))
17917 if (TREE_PURPOSE (probe))
17919 TREE_PURPOSE (parser->unparsed_functions_queues)
17920 = tree_cons (current_class_type, decl,
17921 TREE_PURPOSE (parser->unparsed_functions_queues));
17926 /* FN is a FUNCTION_DECL which may contains a parameter with an
17927 unparsed DEFAULT_ARG. Parse the default args now. This function
17928 assumes that the current scope is the scope in which the default
17929 argument should be processed. */
17932 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17934 bool saved_local_variables_forbidden_p;
17937 /* While we're parsing the default args, we might (due to the
17938 statement expression extension) encounter more classes. We want
17939 to handle them right away, but we don't want them getting mixed
17940 up with default args that are currently in the queue. */
17941 parser->unparsed_functions_queues
17942 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17944 /* Local variable names (and the `this' keyword) may not appear
17945 in a default argument. */
17946 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17947 parser->local_variables_forbidden_p = true;
17949 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17951 parm = TREE_CHAIN (parm))
17953 cp_token_cache *tokens;
17954 tree default_arg = TREE_PURPOSE (parm);
17956 VEC(tree,gc) *insts;
17963 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17964 /* This can happen for a friend declaration for a function
17965 already declared with default arguments. */
17968 /* Push the saved tokens for the default argument onto the parser's
17970 tokens = DEFARG_TOKENS (default_arg);
17971 cp_parser_push_lexer_for_tokens (parser, tokens);
17973 /* Parse the assignment-expression. */
17974 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17976 if (!processing_template_decl)
17977 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17979 TREE_PURPOSE (parm) = parsed_arg;
17981 /* Update any instantiations we've already created. */
17982 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17983 VEC_iterate (tree, insts, ix, copy); ix++)
17984 TREE_PURPOSE (copy) = parsed_arg;
17986 /* If the token stream has not been completely used up, then
17987 there was extra junk after the end of the default
17989 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17990 cp_parser_error (parser, "expected %<,%>");
17992 /* Revert to the main lexer. */
17993 cp_parser_pop_lexer (parser);
17996 /* Make sure no default arg is missing. */
17997 check_default_args (fn);
17999 /* Restore the state of local_variables_forbidden_p. */
18000 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18002 /* Restore the queue. */
18003 parser->unparsed_functions_queues
18004 = TREE_CHAIN (parser->unparsed_functions_queues);
18007 /* Parse the operand of `sizeof' (or a similar operator). Returns
18008 either a TYPE or an expression, depending on the form of the
18009 input. The KEYWORD indicates which kind of expression we have
18013 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18015 tree expr = NULL_TREE;
18016 const char *saved_message;
18018 bool saved_integral_constant_expression_p;
18019 bool saved_non_integral_constant_expression_p;
18020 bool pack_expansion_p = false;
18022 /* Types cannot be defined in a `sizeof' expression. Save away the
18024 saved_message = parser->type_definition_forbidden_message;
18025 /* And create the new one. */
18026 tmp = concat ("types may not be defined in %<",
18027 IDENTIFIER_POINTER (ridpointers[keyword]),
18028 "%> expressions", NULL);
18029 parser->type_definition_forbidden_message = tmp;
18031 /* The restrictions on constant-expressions do not apply inside
18032 sizeof expressions. */
18033 saved_integral_constant_expression_p
18034 = parser->integral_constant_expression_p;
18035 saved_non_integral_constant_expression_p
18036 = parser->non_integral_constant_expression_p;
18037 parser->integral_constant_expression_p = false;
18039 /* If it's a `...', then we are computing the length of a parameter
18041 if (keyword == RID_SIZEOF
18042 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18044 /* Consume the `...'. */
18045 cp_lexer_consume_token (parser->lexer);
18046 maybe_warn_variadic_templates ();
18048 /* Note that this is an expansion. */
18049 pack_expansion_p = true;
18052 /* Do not actually evaluate the expression. */
18054 /* If it's a `(', then we might be looking at the type-id
18056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18059 bool saved_in_type_id_in_expr_p;
18061 /* We can't be sure yet whether we're looking at a type-id or an
18063 cp_parser_parse_tentatively (parser);
18064 /* Consume the `('. */
18065 cp_lexer_consume_token (parser->lexer);
18066 /* Parse the type-id. */
18067 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18068 parser->in_type_id_in_expr_p = true;
18069 type = cp_parser_type_id (parser);
18070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18071 /* Now, look for the trailing `)'. */
18072 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18073 /* If all went well, then we're done. */
18074 if (cp_parser_parse_definitely (parser))
18076 cp_decl_specifier_seq decl_specs;
18078 /* Build a trivial decl-specifier-seq. */
18079 clear_decl_specs (&decl_specs);
18080 decl_specs.type = type;
18082 /* Call grokdeclarator to figure out what type this is. */
18083 expr = grokdeclarator (NULL,
18087 /*attrlist=*/NULL);
18091 /* If the type-id production did not work out, then we must be
18092 looking at the unary-expression production. */
18094 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18097 if (pack_expansion_p)
18098 /* Build a pack expansion. */
18099 expr = make_pack_expansion (expr);
18101 /* Go back to evaluating expressions. */
18104 /* Free the message we created. */
18106 /* And restore the old one. */
18107 parser->type_definition_forbidden_message = saved_message;
18108 parser->integral_constant_expression_p
18109 = saved_integral_constant_expression_p;
18110 parser->non_integral_constant_expression_p
18111 = saved_non_integral_constant_expression_p;
18116 /* If the current declaration has no declarator, return true. */
18119 cp_parser_declares_only_class_p (cp_parser *parser)
18121 /* If the next token is a `;' or a `,' then there is no
18123 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18124 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18127 /* Update the DECL_SPECS to reflect the storage class indicated by
18131 cp_parser_set_storage_class (cp_parser *parser,
18132 cp_decl_specifier_seq *decl_specs,
18134 location_t location)
18136 cp_storage_class storage_class;
18138 if (parser->in_unbraced_linkage_specification_p)
18140 error ("%Hinvalid use of %qD in linkage specification",
18141 &location, ridpointers[keyword]);
18144 else if (decl_specs->storage_class != sc_none)
18146 decl_specs->conflicting_specifiers_p = true;
18150 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18151 && decl_specs->specs[(int) ds_thread])
18153 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18154 decl_specs->specs[(int) ds_thread] = 0;
18160 storage_class = sc_auto;
18163 storage_class = sc_register;
18166 storage_class = sc_static;
18169 storage_class = sc_extern;
18172 storage_class = sc_mutable;
18175 gcc_unreachable ();
18177 decl_specs->storage_class = storage_class;
18179 /* A storage class specifier cannot be applied alongside a typedef
18180 specifier. If there is a typedef specifier present then set
18181 conflicting_specifiers_p which will trigger an error later
18182 on in grokdeclarator. */
18183 if (decl_specs->specs[(int)ds_typedef])
18184 decl_specs->conflicting_specifiers_p = true;
18187 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18188 is true, the type is a user-defined type; otherwise it is a
18189 built-in type specified by a keyword. */
18192 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18194 location_t location,
18195 bool user_defined_p)
18197 decl_specs->any_specifiers_p = true;
18199 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18200 (with, for example, in "typedef int wchar_t;") we remember that
18201 this is what happened. In system headers, we ignore these
18202 declarations so that G++ can work with system headers that are not
18204 if (decl_specs->specs[(int) ds_typedef]
18206 && (type_spec == boolean_type_node
18207 || type_spec == char16_type_node
18208 || type_spec == char32_type_node
18209 || type_spec == wchar_type_node)
18210 && (decl_specs->type
18211 || decl_specs->specs[(int) ds_long]
18212 || decl_specs->specs[(int) ds_short]
18213 || decl_specs->specs[(int) ds_unsigned]
18214 || decl_specs->specs[(int) ds_signed]))
18216 decl_specs->redefined_builtin_type = type_spec;
18217 if (!decl_specs->type)
18219 decl_specs->type = type_spec;
18220 decl_specs->user_defined_type_p = false;
18221 decl_specs->type_location = location;
18224 else if (decl_specs->type)
18225 decl_specs->multiple_types_p = true;
18228 decl_specs->type = type_spec;
18229 decl_specs->user_defined_type_p = user_defined_p;
18230 decl_specs->redefined_builtin_type = NULL_TREE;
18231 decl_specs->type_location = location;
18235 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18236 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18239 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18241 return decl_specifiers->specs[(int) ds_friend] != 0;
18244 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18245 issue an error message indicating that TOKEN_DESC was expected.
18247 Returns the token consumed, if the token had the appropriate type.
18248 Otherwise, returns NULL. */
18251 cp_parser_require (cp_parser* parser,
18252 enum cpp_ttype type,
18253 const char* token_desc)
18255 if (cp_lexer_next_token_is (parser->lexer, type))
18256 return cp_lexer_consume_token (parser->lexer);
18259 /* Output the MESSAGE -- unless we're parsing tentatively. */
18260 if (!cp_parser_simulate_error (parser))
18262 char *message = concat ("expected ", token_desc, NULL);
18263 cp_parser_error (parser, message);
18270 /* An error message is produced if the next token is not '>'.
18271 All further tokens are skipped until the desired token is
18272 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18275 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18277 /* Current level of '< ... >'. */
18278 unsigned level = 0;
18279 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18280 unsigned nesting_depth = 0;
18282 /* Are we ready, yet? If not, issue error message. */
18283 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18286 /* Skip tokens until the desired token is found. */
18289 /* Peek at the next token. */
18290 switch (cp_lexer_peek_token (parser->lexer)->type)
18293 if (!nesting_depth)
18298 if (cxx_dialect == cxx98)
18299 /* C++0x views the `>>' operator as two `>' tokens, but
18302 else if (!nesting_depth && level-- == 0)
18304 /* We've hit a `>>' where the first `>' closes the
18305 template argument list, and the second `>' is
18306 spurious. Just consume the `>>' and stop; we've
18307 already produced at least one error. */
18308 cp_lexer_consume_token (parser->lexer);
18311 /* Fall through for C++0x, so we handle the second `>' in
18315 if (!nesting_depth && level-- == 0)
18317 /* We've reached the token we want, consume it and stop. */
18318 cp_lexer_consume_token (parser->lexer);
18323 case CPP_OPEN_PAREN:
18324 case CPP_OPEN_SQUARE:
18328 case CPP_CLOSE_PAREN:
18329 case CPP_CLOSE_SQUARE:
18330 if (nesting_depth-- == 0)
18335 case CPP_PRAGMA_EOL:
18336 case CPP_SEMICOLON:
18337 case CPP_OPEN_BRACE:
18338 case CPP_CLOSE_BRACE:
18339 /* The '>' was probably forgotten, don't look further. */
18346 /* Consume this token. */
18347 cp_lexer_consume_token (parser->lexer);
18351 /* If the next token is the indicated keyword, consume it. Otherwise,
18352 issue an error message indicating that TOKEN_DESC was expected.
18354 Returns the token consumed, if the token had the appropriate type.
18355 Otherwise, returns NULL. */
18358 cp_parser_require_keyword (cp_parser* parser,
18360 const char* token_desc)
18362 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18364 if (token && token->keyword != keyword)
18366 dyn_string_t error_msg;
18368 /* Format the error message. */
18369 error_msg = dyn_string_new (0);
18370 dyn_string_append_cstr (error_msg, "expected ");
18371 dyn_string_append_cstr (error_msg, token_desc);
18372 cp_parser_error (parser, error_msg->s);
18373 dyn_string_delete (error_msg);
18380 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18381 function-definition. */
18384 cp_parser_token_starts_function_definition_p (cp_token* token)
18386 return (/* An ordinary function-body begins with an `{'. */
18387 token->type == CPP_OPEN_BRACE
18388 /* A ctor-initializer begins with a `:'. */
18389 || token->type == CPP_COLON
18390 /* A function-try-block begins with `try'. */
18391 || token->keyword == RID_TRY
18392 /* The named return value extension begins with `return'. */
18393 || token->keyword == RID_RETURN);
18396 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18400 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18404 token = cp_lexer_peek_token (parser->lexer);
18405 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18408 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18409 C++0x) ending a template-argument. */
18412 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18416 token = cp_lexer_peek_token (parser->lexer);
18417 return (token->type == CPP_COMMA
18418 || token->type == CPP_GREATER
18419 || token->type == CPP_ELLIPSIS
18420 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18423 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18424 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18427 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18432 token = cp_lexer_peek_nth_token (parser->lexer, n);
18433 if (token->type == CPP_LESS)
18435 /* Check for the sequence `<::' in the original code. It would be lexed as
18436 `[:', where `[' is a digraph, and there is no whitespace before
18438 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18441 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18442 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18448 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18449 or none_type otherwise. */
18451 static enum tag_types
18452 cp_parser_token_is_class_key (cp_token* token)
18454 switch (token->keyword)
18459 return record_type;
18468 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18471 cp_parser_check_class_key (enum tag_types class_key, tree type)
18473 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18474 permerror (input_location, "%qs tag used in naming %q#T",
18475 class_key == union_type ? "union"
18476 : class_key == record_type ? "struct" : "class",
18480 /* Issue an error message if DECL is redeclared with different
18481 access than its original declaration [class.access.spec/3].
18482 This applies to nested classes and nested class templates.
18486 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18488 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18491 if ((TREE_PRIVATE (decl)
18492 != (current_access_specifier == access_private_node))
18493 || (TREE_PROTECTED (decl)
18494 != (current_access_specifier == access_protected_node)))
18495 error ("%H%qD redeclared with different access", &location, decl);
18498 /* Look for the `template' keyword, as a syntactic disambiguator.
18499 Return TRUE iff it is present, in which case it will be
18503 cp_parser_optional_template_keyword (cp_parser *parser)
18505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18507 /* The `template' keyword can only be used within templates;
18508 outside templates the parser can always figure out what is a
18509 template and what is not. */
18510 if (!processing_template_decl)
18512 cp_token *token = cp_lexer_peek_token (parser->lexer);
18513 error ("%H%<template%> (as a disambiguator) is only allowed "
18514 "within templates", &token->location);
18515 /* If this part of the token stream is rescanned, the same
18516 error message would be generated. So, we purge the token
18517 from the stream. */
18518 cp_lexer_purge_token (parser->lexer);
18523 /* Consume the `template' keyword. */
18524 cp_lexer_consume_token (parser->lexer);
18532 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18533 set PARSER->SCOPE, and perform other related actions. */
18536 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18539 struct tree_check *check_value;
18540 deferred_access_check *chk;
18541 VEC (deferred_access_check,gc) *checks;
18543 /* Get the stored value. */
18544 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18545 /* Perform any access checks that were deferred. */
18546 checks = check_value->checks;
18550 VEC_iterate (deferred_access_check, checks, i, chk) ;
18553 perform_or_defer_access_check (chk->binfo,
18558 /* Set the scope from the stored value. */
18559 parser->scope = check_value->value;
18560 parser->qualifying_scope = check_value->qualifying_scope;
18561 parser->object_scope = NULL_TREE;
18564 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18565 encounter the end of a block before what we were looking for. */
18568 cp_parser_cache_group (cp_parser *parser,
18569 enum cpp_ttype end,
18574 cp_token *token = cp_lexer_peek_token (parser->lexer);
18576 /* Abort a parenthesized expression if we encounter a semicolon. */
18577 if ((end == CPP_CLOSE_PAREN || depth == 0)
18578 && token->type == CPP_SEMICOLON)
18580 /* If we've reached the end of the file, stop. */
18581 if (token->type == CPP_EOF
18582 || (end != CPP_PRAGMA_EOL
18583 && token->type == CPP_PRAGMA_EOL))
18585 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18586 /* We've hit the end of an enclosing block, so there's been some
18587 kind of syntax error. */
18590 /* Consume the token. */
18591 cp_lexer_consume_token (parser->lexer);
18592 /* See if it starts a new group. */
18593 if (token->type == CPP_OPEN_BRACE)
18595 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18596 /* In theory this should probably check end == '}', but
18597 cp_parser_save_member_function_body needs it to exit
18598 after either '}' or ')' when called with ')'. */
18602 else if (token->type == CPP_OPEN_PAREN)
18604 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18605 if (depth == 0 && end == CPP_CLOSE_PAREN)
18608 else if (token->type == CPP_PRAGMA)
18609 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18610 else if (token->type == end)
18615 /* Begin parsing tentatively. We always save tokens while parsing
18616 tentatively so that if the tentative parsing fails we can restore the
18620 cp_parser_parse_tentatively (cp_parser* parser)
18622 /* Enter a new parsing context. */
18623 parser->context = cp_parser_context_new (parser->context);
18624 /* Begin saving tokens. */
18625 cp_lexer_save_tokens (parser->lexer);
18626 /* In order to avoid repetitive access control error messages,
18627 access checks are queued up until we are no longer parsing
18629 push_deferring_access_checks (dk_deferred);
18632 /* Commit to the currently active tentative parse. */
18635 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18637 cp_parser_context *context;
18640 /* Mark all of the levels as committed. */
18641 lexer = parser->lexer;
18642 for (context = parser->context; context->next; context = context->next)
18644 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18646 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18647 while (!cp_lexer_saving_tokens (lexer))
18648 lexer = lexer->next;
18649 cp_lexer_commit_tokens (lexer);
18653 /* Abort the currently active tentative parse. All consumed tokens
18654 will be rolled back, and no diagnostics will be issued. */
18657 cp_parser_abort_tentative_parse (cp_parser* parser)
18659 cp_parser_simulate_error (parser);
18660 /* Now, pretend that we want to see if the construct was
18661 successfully parsed. */
18662 cp_parser_parse_definitely (parser);
18665 /* Stop parsing tentatively. If a parse error has occurred, restore the
18666 token stream. Otherwise, commit to the tokens we have consumed.
18667 Returns true if no error occurred; false otherwise. */
18670 cp_parser_parse_definitely (cp_parser* parser)
18672 bool error_occurred;
18673 cp_parser_context *context;
18675 /* Remember whether or not an error occurred, since we are about to
18676 destroy that information. */
18677 error_occurred = cp_parser_error_occurred (parser);
18678 /* Remove the topmost context from the stack. */
18679 context = parser->context;
18680 parser->context = context->next;
18681 /* If no parse errors occurred, commit to the tentative parse. */
18682 if (!error_occurred)
18684 /* Commit to the tokens read tentatively, unless that was
18686 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18687 cp_lexer_commit_tokens (parser->lexer);
18689 pop_to_parent_deferring_access_checks ();
18691 /* Otherwise, if errors occurred, roll back our state so that things
18692 are just as they were before we began the tentative parse. */
18695 cp_lexer_rollback_tokens (parser->lexer);
18696 pop_deferring_access_checks ();
18698 /* Add the context to the front of the free list. */
18699 context->next = cp_parser_context_free_list;
18700 cp_parser_context_free_list = context;
18702 return !error_occurred;
18705 /* Returns true if we are parsing tentatively and are not committed to
18706 this tentative parse. */
18709 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18711 return (cp_parser_parsing_tentatively (parser)
18712 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18715 /* Returns nonzero iff an error has occurred during the most recent
18716 tentative parse. */
18719 cp_parser_error_occurred (cp_parser* parser)
18721 return (cp_parser_parsing_tentatively (parser)
18722 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18725 /* Returns nonzero if GNU extensions are allowed. */
18728 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18730 return parser->allow_gnu_extensions_p;
18733 /* Objective-C++ Productions */
18736 /* Parse an Objective-C expression, which feeds into a primary-expression
18740 objc-message-expression
18741 objc-string-literal
18742 objc-encode-expression
18743 objc-protocol-expression
18744 objc-selector-expression
18746 Returns a tree representation of the expression. */
18749 cp_parser_objc_expression (cp_parser* parser)
18751 /* Try to figure out what kind of declaration is present. */
18752 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18756 case CPP_OPEN_SQUARE:
18757 return cp_parser_objc_message_expression (parser);
18759 case CPP_OBJC_STRING:
18760 kwd = cp_lexer_consume_token (parser->lexer);
18761 return objc_build_string_object (kwd->u.value);
18764 switch (kwd->keyword)
18766 case RID_AT_ENCODE:
18767 return cp_parser_objc_encode_expression (parser);
18769 case RID_AT_PROTOCOL:
18770 return cp_parser_objc_protocol_expression (parser);
18772 case RID_AT_SELECTOR:
18773 return cp_parser_objc_selector_expression (parser);
18779 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18780 &kwd->location, kwd->u.value);
18781 cp_parser_skip_to_end_of_block_or_statement (parser);
18784 return error_mark_node;
18787 /* Parse an Objective-C message expression.
18789 objc-message-expression:
18790 [ objc-message-receiver objc-message-args ]
18792 Returns a representation of an Objective-C message. */
18795 cp_parser_objc_message_expression (cp_parser* parser)
18797 tree receiver, messageargs;
18799 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18800 receiver = cp_parser_objc_message_receiver (parser);
18801 messageargs = cp_parser_objc_message_args (parser);
18802 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18804 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18807 /* Parse an objc-message-receiver.
18809 objc-message-receiver:
18811 simple-type-specifier
18813 Returns a representation of the type or expression. */
18816 cp_parser_objc_message_receiver (cp_parser* parser)
18820 /* An Objective-C message receiver may be either (1) a type
18821 or (2) an expression. */
18822 cp_parser_parse_tentatively (parser);
18823 rcv = cp_parser_expression (parser, false);
18825 if (cp_parser_parse_definitely (parser))
18828 rcv = cp_parser_simple_type_specifier (parser,
18829 /*decl_specs=*/NULL,
18830 CP_PARSER_FLAGS_NONE);
18832 return objc_get_class_reference (rcv);
18835 /* Parse the arguments and selectors comprising an Objective-C message.
18840 objc-selector-args , objc-comma-args
18842 objc-selector-args:
18843 objc-selector [opt] : assignment-expression
18844 objc-selector-args objc-selector [opt] : assignment-expression
18847 assignment-expression
18848 objc-comma-args , assignment-expression
18850 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18851 selector arguments and TREE_VALUE containing a list of comma
18855 cp_parser_objc_message_args (cp_parser* parser)
18857 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18858 bool maybe_unary_selector_p = true;
18859 cp_token *token = cp_lexer_peek_token (parser->lexer);
18861 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18863 tree selector = NULL_TREE, arg;
18865 if (token->type != CPP_COLON)
18866 selector = cp_parser_objc_selector (parser);
18868 /* Detect if we have a unary selector. */
18869 if (maybe_unary_selector_p
18870 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18871 return build_tree_list (selector, NULL_TREE);
18873 maybe_unary_selector_p = false;
18874 cp_parser_require (parser, CPP_COLON, "%<:%>");
18875 arg = cp_parser_assignment_expression (parser, false);
18878 = chainon (sel_args,
18879 build_tree_list (selector, arg));
18881 token = cp_lexer_peek_token (parser->lexer);
18884 /* Handle non-selector arguments, if any. */
18885 while (token->type == CPP_COMMA)
18889 cp_lexer_consume_token (parser->lexer);
18890 arg = cp_parser_assignment_expression (parser, false);
18893 = chainon (addl_args,
18894 build_tree_list (NULL_TREE, arg));
18896 token = cp_lexer_peek_token (parser->lexer);
18899 return build_tree_list (sel_args, addl_args);
18902 /* Parse an Objective-C encode expression.
18904 objc-encode-expression:
18905 @encode objc-typename
18907 Returns an encoded representation of the type argument. */
18910 cp_parser_objc_encode_expression (cp_parser* parser)
18915 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18916 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18917 token = cp_lexer_peek_token (parser->lexer);
18918 type = complete_type (cp_parser_type_id (parser));
18919 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18923 error ("%H%<@encode%> must specify a type as an argument",
18925 return error_mark_node;
18928 return objc_build_encode_expr (type);
18931 /* Parse an Objective-C @defs expression. */
18934 cp_parser_objc_defs_expression (cp_parser *parser)
18938 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18939 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18940 name = cp_parser_identifier (parser);
18941 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18943 return objc_get_class_ivars (name);
18946 /* Parse an Objective-C protocol expression.
18948 objc-protocol-expression:
18949 @protocol ( identifier )
18951 Returns a representation of the protocol expression. */
18954 cp_parser_objc_protocol_expression (cp_parser* parser)
18958 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18959 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18960 proto = cp_parser_identifier (parser);
18961 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18963 return objc_build_protocol_expr (proto);
18966 /* Parse an Objective-C selector expression.
18968 objc-selector-expression:
18969 @selector ( objc-method-signature )
18971 objc-method-signature:
18977 objc-selector-seq objc-selector :
18979 Returns a representation of the method selector. */
18982 cp_parser_objc_selector_expression (cp_parser* parser)
18984 tree sel_seq = NULL_TREE;
18985 bool maybe_unary_selector_p = true;
18988 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18989 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18990 token = cp_lexer_peek_token (parser->lexer);
18992 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18993 || token->type == CPP_SCOPE)
18995 tree selector = NULL_TREE;
18997 if (token->type != CPP_COLON
18998 || token->type == CPP_SCOPE)
18999 selector = cp_parser_objc_selector (parser);
19001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19002 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19004 /* Detect if we have a unary selector. */
19005 if (maybe_unary_selector_p)
19007 sel_seq = selector;
19008 goto finish_selector;
19012 cp_parser_error (parser, "expected %<:%>");
19015 maybe_unary_selector_p = false;
19016 token = cp_lexer_consume_token (parser->lexer);
19018 if (token->type == CPP_SCOPE)
19021 = chainon (sel_seq,
19022 build_tree_list (selector, NULL_TREE));
19024 = chainon (sel_seq,
19025 build_tree_list (NULL_TREE, NULL_TREE));
19029 = chainon (sel_seq,
19030 build_tree_list (selector, NULL_TREE));
19032 token = cp_lexer_peek_token (parser->lexer);
19036 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19038 return objc_build_selector_expr (sel_seq);
19041 /* Parse a list of identifiers.
19043 objc-identifier-list:
19045 objc-identifier-list , identifier
19047 Returns a TREE_LIST of identifier nodes. */
19050 cp_parser_objc_identifier_list (cp_parser* parser)
19052 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19053 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19055 while (sep->type == CPP_COMMA)
19057 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19058 list = chainon (list,
19059 build_tree_list (NULL_TREE,
19060 cp_parser_identifier (parser)));
19061 sep = cp_lexer_peek_token (parser->lexer);
19067 /* Parse an Objective-C alias declaration.
19069 objc-alias-declaration:
19070 @compatibility_alias identifier identifier ;
19072 This function registers the alias mapping with the Objective-C front end.
19073 It returns nothing. */
19076 cp_parser_objc_alias_declaration (cp_parser* parser)
19080 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19081 alias = cp_parser_identifier (parser);
19082 orig = cp_parser_identifier (parser);
19083 objc_declare_alias (alias, orig);
19084 cp_parser_consume_semicolon_at_end_of_statement (parser);
19087 /* Parse an Objective-C class forward-declaration.
19089 objc-class-declaration:
19090 @class objc-identifier-list ;
19092 The function registers the forward declarations with the Objective-C
19093 front end. It returns nothing. */
19096 cp_parser_objc_class_declaration (cp_parser* parser)
19098 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19099 objc_declare_class (cp_parser_objc_identifier_list (parser));
19100 cp_parser_consume_semicolon_at_end_of_statement (parser);
19103 /* Parse a list of Objective-C protocol references.
19105 objc-protocol-refs-opt:
19106 objc-protocol-refs [opt]
19108 objc-protocol-refs:
19109 < objc-identifier-list >
19111 Returns a TREE_LIST of identifiers, if any. */
19114 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19116 tree protorefs = NULL_TREE;
19118 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19120 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19121 protorefs = cp_parser_objc_identifier_list (parser);
19122 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19128 /* Parse a Objective-C visibility specification. */
19131 cp_parser_objc_visibility_spec (cp_parser* parser)
19133 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19135 switch (vis->keyword)
19137 case RID_AT_PRIVATE:
19138 objc_set_visibility (2);
19140 case RID_AT_PROTECTED:
19141 objc_set_visibility (0);
19143 case RID_AT_PUBLIC:
19144 objc_set_visibility (1);
19150 /* Eat '@private'/'@protected'/'@public'. */
19151 cp_lexer_consume_token (parser->lexer);
19154 /* Parse an Objective-C method type. */
19157 cp_parser_objc_method_type (cp_parser* parser)
19159 objc_set_method_type
19160 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19165 /* Parse an Objective-C protocol qualifier. */
19168 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19170 tree quals = NULL_TREE, node;
19171 cp_token *token = cp_lexer_peek_token (parser->lexer);
19173 node = token->u.value;
19175 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19176 && (node == ridpointers [(int) RID_IN]
19177 || node == ridpointers [(int) RID_OUT]
19178 || node == ridpointers [(int) RID_INOUT]
19179 || node == ridpointers [(int) RID_BYCOPY]
19180 || node == ridpointers [(int) RID_BYREF]
19181 || node == ridpointers [(int) RID_ONEWAY]))
19183 quals = tree_cons (NULL_TREE, node, quals);
19184 cp_lexer_consume_token (parser->lexer);
19185 token = cp_lexer_peek_token (parser->lexer);
19186 node = token->u.value;
19192 /* Parse an Objective-C typename. */
19195 cp_parser_objc_typename (cp_parser* parser)
19197 tree type_name = NULL_TREE;
19199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19201 tree proto_quals, cp_type = NULL_TREE;
19203 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19204 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19206 /* An ObjC type name may consist of just protocol qualifiers, in which
19207 case the type shall default to 'id'. */
19208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19209 cp_type = cp_parser_type_id (parser);
19211 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19212 type_name = build_tree_list (proto_quals, cp_type);
19218 /* Check to see if TYPE refers to an Objective-C selector name. */
19221 cp_parser_objc_selector_p (enum cpp_ttype type)
19223 return (type == CPP_NAME || type == CPP_KEYWORD
19224 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19225 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19226 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19227 || type == CPP_XOR || type == CPP_XOR_EQ);
19230 /* Parse an Objective-C selector. */
19233 cp_parser_objc_selector (cp_parser* parser)
19235 cp_token *token = cp_lexer_consume_token (parser->lexer);
19237 if (!cp_parser_objc_selector_p (token->type))
19239 error ("%Hinvalid Objective-C++ selector name", &token->location);
19240 return error_mark_node;
19243 /* C++ operator names are allowed to appear in ObjC selectors. */
19244 switch (token->type)
19246 case CPP_AND_AND: return get_identifier ("and");
19247 case CPP_AND_EQ: return get_identifier ("and_eq");
19248 case CPP_AND: return get_identifier ("bitand");
19249 case CPP_OR: return get_identifier ("bitor");
19250 case CPP_COMPL: return get_identifier ("compl");
19251 case CPP_NOT: return get_identifier ("not");
19252 case CPP_NOT_EQ: return get_identifier ("not_eq");
19253 case CPP_OR_OR: return get_identifier ("or");
19254 case CPP_OR_EQ: return get_identifier ("or_eq");
19255 case CPP_XOR: return get_identifier ("xor");
19256 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19257 default: return token->u.value;
19261 /* Parse an Objective-C params list. */
19264 cp_parser_objc_method_keyword_params (cp_parser* parser)
19266 tree params = NULL_TREE;
19267 bool maybe_unary_selector_p = true;
19268 cp_token *token = cp_lexer_peek_token (parser->lexer);
19270 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19272 tree selector = NULL_TREE, type_name, identifier;
19274 if (token->type != CPP_COLON)
19275 selector = cp_parser_objc_selector (parser);
19277 /* Detect if we have a unary selector. */
19278 if (maybe_unary_selector_p
19279 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19282 maybe_unary_selector_p = false;
19283 cp_parser_require (parser, CPP_COLON, "%<:%>");
19284 type_name = cp_parser_objc_typename (parser);
19285 identifier = cp_parser_identifier (parser);
19289 objc_build_keyword_decl (selector,
19293 token = cp_lexer_peek_token (parser->lexer);
19299 /* Parse the non-keyword Objective-C params. */
19302 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19304 tree params = make_node (TREE_LIST);
19305 cp_token *token = cp_lexer_peek_token (parser->lexer);
19306 *ellipsisp = false; /* Initially, assume no ellipsis. */
19308 while (token->type == CPP_COMMA)
19310 cp_parameter_declarator *parmdecl;
19313 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19314 token = cp_lexer_peek_token (parser->lexer);
19316 if (token->type == CPP_ELLIPSIS)
19318 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19323 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19324 parm = grokdeclarator (parmdecl->declarator,
19325 &parmdecl->decl_specifiers,
19326 PARM, /*initialized=*/0,
19327 /*attrlist=*/NULL);
19329 chainon (params, build_tree_list (NULL_TREE, parm));
19330 token = cp_lexer_peek_token (parser->lexer);
19336 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19339 cp_parser_objc_interstitial_code (cp_parser* parser)
19341 cp_token *token = cp_lexer_peek_token (parser->lexer);
19343 /* If the next token is `extern' and the following token is a string
19344 literal, then we have a linkage specification. */
19345 if (token->keyword == RID_EXTERN
19346 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19347 cp_parser_linkage_specification (parser);
19348 /* Handle #pragma, if any. */
19349 else if (token->type == CPP_PRAGMA)
19350 cp_parser_pragma (parser, pragma_external);
19351 /* Allow stray semicolons. */
19352 else if (token->type == CPP_SEMICOLON)
19353 cp_lexer_consume_token (parser->lexer);
19354 /* Finally, try to parse a block-declaration, or a function-definition. */
19356 cp_parser_block_declaration (parser, /*statement_p=*/false);
19359 /* Parse a method signature. */
19362 cp_parser_objc_method_signature (cp_parser* parser)
19364 tree rettype, kwdparms, optparms;
19365 bool ellipsis = false;
19367 cp_parser_objc_method_type (parser);
19368 rettype = cp_parser_objc_typename (parser);
19369 kwdparms = cp_parser_objc_method_keyword_params (parser);
19370 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19372 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19375 /* Pars an Objective-C method prototype list. */
19378 cp_parser_objc_method_prototype_list (cp_parser* parser)
19380 cp_token *token = cp_lexer_peek_token (parser->lexer);
19382 while (token->keyword != RID_AT_END)
19384 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19386 objc_add_method_declaration
19387 (cp_parser_objc_method_signature (parser));
19388 cp_parser_consume_semicolon_at_end_of_statement (parser);
19391 /* Allow for interspersed non-ObjC++ code. */
19392 cp_parser_objc_interstitial_code (parser);
19394 token = cp_lexer_peek_token (parser->lexer);
19397 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19398 objc_finish_interface ();
19401 /* Parse an Objective-C method definition list. */
19404 cp_parser_objc_method_definition_list (cp_parser* parser)
19406 cp_token *token = cp_lexer_peek_token (parser->lexer);
19408 while (token->keyword != RID_AT_END)
19412 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19414 push_deferring_access_checks (dk_deferred);
19415 objc_start_method_definition
19416 (cp_parser_objc_method_signature (parser));
19418 /* For historical reasons, we accept an optional semicolon. */
19419 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19420 cp_lexer_consume_token (parser->lexer);
19422 perform_deferred_access_checks ();
19423 stop_deferring_access_checks ();
19424 meth = cp_parser_function_definition_after_declarator (parser,
19426 pop_deferring_access_checks ();
19427 objc_finish_method_definition (meth);
19430 /* Allow for interspersed non-ObjC++ code. */
19431 cp_parser_objc_interstitial_code (parser);
19433 token = cp_lexer_peek_token (parser->lexer);
19436 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19437 objc_finish_implementation ();
19440 /* Parse Objective-C ivars. */
19443 cp_parser_objc_class_ivars (cp_parser* parser)
19445 cp_token *token = cp_lexer_peek_token (parser->lexer);
19447 if (token->type != CPP_OPEN_BRACE)
19448 return; /* No ivars specified. */
19450 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19451 token = cp_lexer_peek_token (parser->lexer);
19453 while (token->type != CPP_CLOSE_BRACE)
19455 cp_decl_specifier_seq declspecs;
19456 int decl_class_or_enum_p;
19457 tree prefix_attributes;
19459 cp_parser_objc_visibility_spec (parser);
19461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19464 cp_parser_decl_specifier_seq (parser,
19465 CP_PARSER_FLAGS_OPTIONAL,
19467 &decl_class_or_enum_p);
19468 prefix_attributes = declspecs.attributes;
19469 declspecs.attributes = NULL_TREE;
19471 /* Keep going until we hit the `;' at the end of the
19473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19475 tree width = NULL_TREE, attributes, first_attribute, decl;
19476 cp_declarator *declarator = NULL;
19477 int ctor_dtor_or_conv_p;
19479 /* Check for a (possibly unnamed) bitfield declaration. */
19480 token = cp_lexer_peek_token (parser->lexer);
19481 if (token->type == CPP_COLON)
19484 if (token->type == CPP_NAME
19485 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19488 /* Get the name of the bitfield. */
19489 declarator = make_id_declarator (NULL_TREE,
19490 cp_parser_identifier (parser),
19494 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19495 /* Get the width of the bitfield. */
19497 = cp_parser_constant_expression (parser,
19498 /*allow_non_constant=*/false,
19503 /* Parse the declarator. */
19505 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19506 &ctor_dtor_or_conv_p,
19507 /*parenthesized_p=*/NULL,
19508 /*member_p=*/false);
19511 /* Look for attributes that apply to the ivar. */
19512 attributes = cp_parser_attributes_opt (parser);
19513 /* Remember which attributes are prefix attributes and
19515 first_attribute = attributes;
19516 /* Combine the attributes. */
19517 attributes = chainon (prefix_attributes, attributes);
19520 /* Create the bitfield declaration. */
19521 decl = grokbitfield (declarator, &declspecs,
19525 decl = grokfield (declarator, &declspecs,
19526 NULL_TREE, /*init_const_expr_p=*/false,
19527 NULL_TREE, attributes);
19529 /* Add the instance variable. */
19530 objc_add_instance_variable (decl);
19532 /* Reset PREFIX_ATTRIBUTES. */
19533 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19534 attributes = TREE_CHAIN (attributes);
19536 TREE_CHAIN (attributes) = NULL_TREE;
19538 token = cp_lexer_peek_token (parser->lexer);
19540 if (token->type == CPP_COMMA)
19542 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19548 cp_parser_consume_semicolon_at_end_of_statement (parser);
19549 token = cp_lexer_peek_token (parser->lexer);
19552 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19553 /* For historical reasons, we accept an optional semicolon. */
19554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19555 cp_lexer_consume_token (parser->lexer);
19558 /* Parse an Objective-C protocol declaration. */
19561 cp_parser_objc_protocol_declaration (cp_parser* parser)
19563 tree proto, protorefs;
19566 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19569 tok = cp_lexer_peek_token (parser->lexer);
19570 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19574 /* See if we have a forward declaration or a definition. */
19575 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19577 /* Try a forward declaration first. */
19578 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19580 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19582 cp_parser_consume_semicolon_at_end_of_statement (parser);
19585 /* Ok, we got a full-fledged definition (or at least should). */
19588 proto = cp_parser_identifier (parser);
19589 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19590 objc_start_protocol (proto, protorefs);
19591 cp_parser_objc_method_prototype_list (parser);
19595 /* Parse an Objective-C superclass or category. */
19598 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19601 cp_token *next = cp_lexer_peek_token (parser->lexer);
19603 *super = *categ = NULL_TREE;
19604 if (next->type == CPP_COLON)
19606 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19607 *super = cp_parser_identifier (parser);
19609 else if (next->type == CPP_OPEN_PAREN)
19611 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19612 *categ = cp_parser_identifier (parser);
19613 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19617 /* Parse an Objective-C class interface. */
19620 cp_parser_objc_class_interface (cp_parser* parser)
19622 tree name, super, categ, protos;
19624 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19625 name = cp_parser_identifier (parser);
19626 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19627 protos = cp_parser_objc_protocol_refs_opt (parser);
19629 /* We have either a class or a category on our hands. */
19631 objc_start_category_interface (name, categ, protos);
19634 objc_start_class_interface (name, super, protos);
19635 /* Handle instance variable declarations, if any. */
19636 cp_parser_objc_class_ivars (parser);
19637 objc_continue_interface ();
19640 cp_parser_objc_method_prototype_list (parser);
19643 /* Parse an Objective-C class implementation. */
19646 cp_parser_objc_class_implementation (cp_parser* parser)
19648 tree name, super, categ;
19650 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19651 name = cp_parser_identifier (parser);
19652 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19654 /* We have either a class or a category on our hands. */
19656 objc_start_category_implementation (name, categ);
19659 objc_start_class_implementation (name, super);
19660 /* Handle instance variable declarations, if any. */
19661 cp_parser_objc_class_ivars (parser);
19662 objc_continue_implementation ();
19665 cp_parser_objc_method_definition_list (parser);
19668 /* Consume the @end token and finish off the implementation. */
19671 cp_parser_objc_end_implementation (cp_parser* parser)
19673 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19674 objc_finish_implementation ();
19677 /* Parse an Objective-C declaration. */
19680 cp_parser_objc_declaration (cp_parser* parser)
19682 /* Try to figure out what kind of declaration is present. */
19683 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19685 switch (kwd->keyword)
19688 cp_parser_objc_alias_declaration (parser);
19691 cp_parser_objc_class_declaration (parser);
19693 case RID_AT_PROTOCOL:
19694 cp_parser_objc_protocol_declaration (parser);
19696 case RID_AT_INTERFACE:
19697 cp_parser_objc_class_interface (parser);
19699 case RID_AT_IMPLEMENTATION:
19700 cp_parser_objc_class_implementation (parser);
19703 cp_parser_objc_end_implementation (parser);
19706 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19707 &kwd->location, kwd->u.value);
19708 cp_parser_skip_to_end_of_block_or_statement (parser);
19712 /* Parse an Objective-C try-catch-finally statement.
19714 objc-try-catch-finally-stmt:
19715 @try compound-statement objc-catch-clause-seq [opt]
19716 objc-finally-clause [opt]
19718 objc-catch-clause-seq:
19719 objc-catch-clause objc-catch-clause-seq [opt]
19722 @catch ( exception-declaration ) compound-statement
19724 objc-finally-clause
19725 @finally compound-statement
19727 Returns NULL_TREE. */
19730 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19731 location_t location;
19734 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19735 location = cp_lexer_peek_token (parser->lexer)->location;
19736 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19737 node, lest it get absorbed into the surrounding block. */
19738 stmt = push_stmt_list ();
19739 cp_parser_compound_statement (parser, NULL, false);
19740 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19742 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19744 cp_parameter_declarator *parmdecl;
19747 cp_lexer_consume_token (parser->lexer);
19748 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19749 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19750 parm = grokdeclarator (parmdecl->declarator,
19751 &parmdecl->decl_specifiers,
19752 PARM, /*initialized=*/0,
19753 /*attrlist=*/NULL);
19754 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19755 objc_begin_catch_clause (parm);
19756 cp_parser_compound_statement (parser, NULL, false);
19757 objc_finish_catch_clause ();
19760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19762 cp_lexer_consume_token (parser->lexer);
19763 location = cp_lexer_peek_token (parser->lexer)->location;
19764 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19765 node, lest it get absorbed into the surrounding block. */
19766 stmt = push_stmt_list ();
19767 cp_parser_compound_statement (parser, NULL, false);
19768 objc_build_finally_clause (location, pop_stmt_list (stmt));
19771 return objc_finish_try_stmt ();
19774 /* Parse an Objective-C synchronized statement.
19776 objc-synchronized-stmt:
19777 @synchronized ( expression ) compound-statement
19779 Returns NULL_TREE. */
19782 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19783 location_t location;
19786 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19788 location = cp_lexer_peek_token (parser->lexer)->location;
19789 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19790 lock = cp_parser_expression (parser, false);
19791 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19793 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19794 node, lest it get absorbed into the surrounding block. */
19795 stmt = push_stmt_list ();
19796 cp_parser_compound_statement (parser, NULL, false);
19798 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19801 /* Parse an Objective-C throw statement.
19804 @throw assignment-expression [opt] ;
19806 Returns a constructed '@throw' statement. */
19809 cp_parser_objc_throw_statement (cp_parser *parser) {
19810 tree expr = NULL_TREE;
19812 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19815 expr = cp_parser_assignment_expression (parser, false);
19817 cp_parser_consume_semicolon_at_end_of_statement (parser);
19819 return objc_build_throw_stmt (expr);
19822 /* Parse an Objective-C statement. */
19825 cp_parser_objc_statement (cp_parser * parser) {
19826 /* Try to figure out what kind of declaration is present. */
19827 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19829 switch (kwd->keyword)
19832 return cp_parser_objc_try_catch_finally_statement (parser);
19833 case RID_AT_SYNCHRONIZED:
19834 return cp_parser_objc_synchronized_statement (parser);
19836 return cp_parser_objc_throw_statement (parser);
19838 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19839 &kwd->location, kwd->u.value);
19840 cp_parser_skip_to_end_of_block_or_statement (parser);
19843 return error_mark_node;
19846 /* OpenMP 2.5 parsing routines. */
19848 /* Returns name of the next clause.
19849 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19850 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19851 returned and the token is consumed. */
19853 static pragma_omp_clause
19854 cp_parser_omp_clause_name (cp_parser *parser)
19856 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19859 result = PRAGMA_OMP_CLAUSE_IF;
19860 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19861 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19862 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19863 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19864 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19866 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19867 const char *p = IDENTIFIER_POINTER (id);
19872 if (!strcmp ("collapse", p))
19873 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19874 else if (!strcmp ("copyin", p))
19875 result = PRAGMA_OMP_CLAUSE_COPYIN;
19876 else if (!strcmp ("copyprivate", p))
19877 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19880 if (!strcmp ("firstprivate", p))
19881 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19884 if (!strcmp ("lastprivate", p))
19885 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19888 if (!strcmp ("nowait", p))
19889 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19890 else if (!strcmp ("num_threads", p))
19891 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19894 if (!strcmp ("ordered", p))
19895 result = PRAGMA_OMP_CLAUSE_ORDERED;
19898 if (!strcmp ("reduction", p))
19899 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19902 if (!strcmp ("schedule", p))
19903 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19904 else if (!strcmp ("shared", p))
19905 result = PRAGMA_OMP_CLAUSE_SHARED;
19908 if (!strcmp ("untied", p))
19909 result = PRAGMA_OMP_CLAUSE_UNTIED;
19914 if (result != PRAGMA_OMP_CLAUSE_NONE)
19915 cp_lexer_consume_token (parser->lexer);
19920 /* Validate that a clause of the given type does not already exist. */
19923 check_no_duplicate_clause (tree clauses, enum tree_code code,
19924 const char *name, location_t location)
19928 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19929 if (OMP_CLAUSE_CODE (c) == code)
19931 error ("%Htoo many %qs clauses", &location, name);
19939 variable-list , identifier
19941 In addition, we match a closing parenthesis. An opening parenthesis
19942 will have been consumed by the caller.
19944 If KIND is nonzero, create the appropriate node and install the decl
19945 in OMP_CLAUSE_DECL and add the node to the head of the list.
19947 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19948 return the list created. */
19951 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19959 token = cp_lexer_peek_token (parser->lexer);
19960 name = cp_parser_id_expression (parser, /*template_p=*/false,
19961 /*check_dependency_p=*/true,
19962 /*template_p=*/NULL,
19963 /*declarator_p=*/false,
19964 /*optional_p=*/false);
19965 if (name == error_mark_node)
19968 decl = cp_parser_lookup_name_simple (parser, name, token->location);
19969 if (decl == error_mark_node)
19970 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19971 else if (kind != 0)
19973 tree u = build_omp_clause (kind);
19974 OMP_CLAUSE_DECL (u) = decl;
19975 OMP_CLAUSE_CHAIN (u) = list;
19979 list = tree_cons (decl, NULL_TREE, list);
19982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19984 cp_lexer_consume_token (parser->lexer);
19987 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19991 /* Try to resync to an unnested comma. Copied from
19992 cp_parser_parenthesized_expression_list. */
19994 ending = cp_parser_skip_to_closing_parenthesis (parser,
19995 /*recovering=*/true,
19997 /*consume_paren=*/true);
20005 /* Similarly, but expect leading and trailing parenthesis. This is a very
20006 common case for omp clauses. */
20009 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20011 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20012 return cp_parser_omp_var_list_no_open (parser, kind, list);
20017 collapse ( constant-expression ) */
20020 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20026 loc = cp_lexer_peek_token (parser->lexer)->location;
20027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20030 num = cp_parser_constant_expression (parser, false, NULL);
20032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20034 /*or_comma=*/false,
20035 /*consume_paren=*/true);
20037 if (num == error_mark_node)
20039 num = fold_non_dependent_expr (num);
20040 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20041 || !host_integerp (num, 0)
20042 || (n = tree_low_cst (num, 0)) <= 0
20045 error ("%Hcollapse argument needs positive constant integer expression",
20050 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20051 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20052 OMP_CLAUSE_CHAIN (c) = list;
20053 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20059 default ( shared | none ) */
20062 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20064 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20067 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20072 const char *p = IDENTIFIER_POINTER (id);
20077 if (strcmp ("none", p) != 0)
20079 kind = OMP_CLAUSE_DEFAULT_NONE;
20083 if (strcmp ("shared", p) != 0)
20085 kind = OMP_CLAUSE_DEFAULT_SHARED;
20092 cp_lexer_consume_token (parser->lexer);
20097 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20100 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20101 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20102 /*or_comma=*/false,
20103 /*consume_paren=*/true);
20105 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20108 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20109 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20110 OMP_CLAUSE_CHAIN (c) = list;
20111 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20117 if ( expression ) */
20120 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20124 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20127 t = cp_parser_condition (parser);
20129 if (t == error_mark_node
20130 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20132 /*or_comma=*/false,
20133 /*consume_paren=*/true);
20135 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20137 c = build_omp_clause (OMP_CLAUSE_IF);
20138 OMP_CLAUSE_IF_EXPR (c) = t;
20139 OMP_CLAUSE_CHAIN (c) = list;
20148 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20149 tree list, location_t location)
20153 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20155 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20156 OMP_CLAUSE_CHAIN (c) = list;
20161 num_threads ( expression ) */
20164 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20165 location_t location)
20169 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20172 t = cp_parser_expression (parser, false);
20174 if (t == error_mark_node
20175 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20176 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20177 /*or_comma=*/false,
20178 /*consume_paren=*/true);
20180 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20181 "num_threads", location);
20183 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20184 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20185 OMP_CLAUSE_CHAIN (c) = list;
20194 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20195 tree list, location_t location)
20199 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20200 "ordered", location);
20202 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20203 OMP_CLAUSE_CHAIN (c) = list;
20208 reduction ( reduction-operator : variable-list )
20210 reduction-operator:
20211 One of: + * - & ^ | && || */
20214 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20216 enum tree_code code;
20219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20222 switch (cp_lexer_peek_token (parser->lexer)->type)
20234 code = BIT_AND_EXPR;
20237 code = BIT_XOR_EXPR;
20240 code = BIT_IOR_EXPR;
20243 code = TRUTH_ANDIF_EXPR;
20246 code = TRUTH_ORIF_EXPR;
20249 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20250 "%<|%>, %<&&%>, or %<||%>");
20252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20253 /*or_comma=*/false,
20254 /*consume_paren=*/true);
20257 cp_lexer_consume_token (parser->lexer);
20259 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20262 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20263 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20264 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20270 schedule ( schedule-kind )
20271 schedule ( schedule-kind , expression )
20274 static | dynamic | guided | runtime | auto */
20277 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20284 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20289 const char *p = IDENTIFIER_POINTER (id);
20294 if (strcmp ("dynamic", p) != 0)
20296 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20300 if (strcmp ("guided", p) != 0)
20302 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20306 if (strcmp ("runtime", p) != 0)
20308 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20315 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20317 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20318 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20321 cp_lexer_consume_token (parser->lexer);
20323 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20326 cp_lexer_consume_token (parser->lexer);
20328 token = cp_lexer_peek_token (parser->lexer);
20329 t = cp_parser_assignment_expression (parser, false);
20331 if (t == error_mark_node)
20333 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20334 error ("%Hschedule %<runtime%> does not take "
20335 "a %<chunk_size%> parameter", &token->location);
20336 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20337 error ("%Hschedule %<auto%> does not take "
20338 "a %<chunk_size%> parameter", &token->location);
20340 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20345 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20348 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20349 OMP_CLAUSE_CHAIN (c) = list;
20353 cp_parser_error (parser, "invalid schedule kind");
20355 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20356 /*or_comma=*/false,
20357 /*consume_paren=*/true);
20365 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20366 tree list, location_t location)
20370 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20372 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20373 OMP_CLAUSE_CHAIN (c) = list;
20377 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20378 is a bitmask in MASK. Return the list of clauses found; the result
20379 of clause default goes in *pdefault. */
20382 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20383 const char *where, cp_token *pragma_tok)
20385 tree clauses = NULL;
20387 cp_token *token = NULL;
20389 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20391 pragma_omp_clause c_kind;
20392 const char *c_name;
20393 tree prev = clauses;
20395 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20396 cp_lexer_consume_token (parser->lexer);
20398 token = cp_lexer_peek_token (parser->lexer);
20399 c_kind = cp_parser_omp_clause_name (parser);
20404 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20405 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20407 c_name = "collapse";
20409 case PRAGMA_OMP_CLAUSE_COPYIN:
20410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20413 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20416 c_name = "copyprivate";
20418 case PRAGMA_OMP_CLAUSE_DEFAULT:
20419 clauses = cp_parser_omp_clause_default (parser, clauses,
20421 c_name = "default";
20423 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20424 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20426 c_name = "firstprivate";
20428 case PRAGMA_OMP_CLAUSE_IF:
20429 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20432 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20433 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20435 c_name = "lastprivate";
20437 case PRAGMA_OMP_CLAUSE_NOWAIT:
20438 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20441 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20442 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20444 c_name = "num_threads";
20446 case PRAGMA_OMP_CLAUSE_ORDERED:
20447 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20449 c_name = "ordered";
20451 case PRAGMA_OMP_CLAUSE_PRIVATE:
20452 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20454 c_name = "private";
20456 case PRAGMA_OMP_CLAUSE_REDUCTION:
20457 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20458 c_name = "reduction";
20460 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20461 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20463 c_name = "schedule";
20465 case PRAGMA_OMP_CLAUSE_SHARED:
20466 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20470 case PRAGMA_OMP_CLAUSE_UNTIED:
20471 clauses = cp_parser_omp_clause_untied (parser, clauses,
20476 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20480 if (((mask >> c_kind) & 1) == 0)
20482 /* Remove the invalid clause(s) from the list to avoid
20483 confusing the rest of the compiler. */
20485 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20490 return finish_omp_clauses (clauses);
20497 In practice, we're also interested in adding the statement to an
20498 outer node. So it is convenient if we work around the fact that
20499 cp_parser_statement calls add_stmt. */
20502 cp_parser_begin_omp_structured_block (cp_parser *parser)
20504 unsigned save = parser->in_statement;
20506 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20507 This preserves the "not within loop or switch" style error messages
20508 for nonsense cases like
20514 if (parser->in_statement)
20515 parser->in_statement = IN_OMP_BLOCK;
20521 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20523 parser->in_statement = save;
20527 cp_parser_omp_structured_block (cp_parser *parser)
20529 tree stmt = begin_omp_structured_block ();
20530 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20532 cp_parser_statement (parser, NULL_TREE, false, NULL);
20534 cp_parser_end_omp_structured_block (parser, save);
20535 return finish_omp_structured_block (stmt);
20539 # pragma omp atomic new-line
20543 x binop= expr | x++ | ++x | x-- | --x
20545 +, *, -, /, &, ^, |, <<, >>
20547 where x is an lvalue expression with scalar type. */
20550 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20553 enum tree_code code;
20555 cp_parser_require_pragma_eol (parser, pragma_tok);
20557 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20559 switch (TREE_CODE (lhs))
20564 case PREINCREMENT_EXPR:
20565 case POSTINCREMENT_EXPR:
20566 lhs = TREE_OPERAND (lhs, 0);
20568 rhs = integer_one_node;
20571 case PREDECREMENT_EXPR:
20572 case POSTDECREMENT_EXPR:
20573 lhs = TREE_OPERAND (lhs, 0);
20575 rhs = integer_one_node;
20579 switch (cp_lexer_peek_token (parser->lexer)->type)
20585 code = TRUNC_DIV_EXPR;
20593 case CPP_LSHIFT_EQ:
20594 code = LSHIFT_EXPR;
20596 case CPP_RSHIFT_EQ:
20597 code = RSHIFT_EXPR;
20600 code = BIT_AND_EXPR;
20603 code = BIT_IOR_EXPR;
20606 code = BIT_XOR_EXPR;
20609 cp_parser_error (parser,
20610 "invalid operator for %<#pragma omp atomic%>");
20613 cp_lexer_consume_token (parser->lexer);
20615 rhs = cp_parser_expression (parser, false);
20616 if (rhs == error_mark_node)
20620 finish_omp_atomic (code, lhs, rhs);
20621 cp_parser_consume_semicolon_at_end_of_statement (parser);
20625 cp_parser_skip_to_end_of_block_or_statement (parser);
20630 # pragma omp barrier new-line */
20633 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20635 cp_parser_require_pragma_eol (parser, pragma_tok);
20636 finish_omp_barrier ();
20640 # pragma omp critical [(name)] new-line
20641 structured-block */
20644 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20646 tree stmt, name = NULL;
20648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20650 cp_lexer_consume_token (parser->lexer);
20652 name = cp_parser_identifier (parser);
20654 if (name == error_mark_node
20655 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20657 /*or_comma=*/false,
20658 /*consume_paren=*/true);
20659 if (name == error_mark_node)
20662 cp_parser_require_pragma_eol (parser, pragma_tok);
20664 stmt = cp_parser_omp_structured_block (parser);
20665 return c_finish_omp_critical (stmt, name);
20669 # pragma omp flush flush-vars[opt] new-line
20672 ( variable-list ) */
20675 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20678 (void) cp_parser_omp_var_list (parser, 0, NULL);
20679 cp_parser_require_pragma_eol (parser, pragma_tok);
20681 finish_omp_flush ();
20684 /* Helper function, to parse omp for increment expression. */
20687 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20689 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20695 cp_parser_skip_to_end_of_statement (parser);
20696 return error_mark_node;
20699 token = cp_lexer_peek_token (parser->lexer);
20700 op = binops_by_token [token->type].tree_type;
20709 cp_parser_skip_to_end_of_statement (parser);
20710 return error_mark_node;
20713 cp_lexer_consume_token (parser->lexer);
20714 rhs = cp_parser_binary_expression (parser, false,
20715 PREC_RELATIONAL_EXPRESSION);
20716 if (rhs == error_mark_node
20717 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20719 cp_parser_skip_to_end_of_statement (parser);
20720 return error_mark_node;
20723 return build2 (op, boolean_type_node, lhs, rhs);
20726 /* Helper function, to parse omp for increment expression. */
20729 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20731 cp_token *token = cp_lexer_peek_token (parser->lexer);
20737 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20739 op = (token->type == CPP_PLUS_PLUS
20740 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20741 cp_lexer_consume_token (parser->lexer);
20742 lhs = cp_parser_cast_expression (parser, false, false);
20744 return error_mark_node;
20745 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20748 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20750 return error_mark_node;
20752 token = cp_lexer_peek_token (parser->lexer);
20753 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20755 op = (token->type == CPP_PLUS_PLUS
20756 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20757 cp_lexer_consume_token (parser->lexer);
20758 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20761 op = cp_parser_assignment_operator_opt (parser);
20762 if (op == ERROR_MARK)
20763 return error_mark_node;
20765 if (op != NOP_EXPR)
20767 rhs = cp_parser_assignment_expression (parser, false);
20768 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20769 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20772 lhs = cp_parser_binary_expression (parser, false,
20773 PREC_ADDITIVE_EXPRESSION);
20774 token = cp_lexer_peek_token (parser->lexer);
20775 decl_first = lhs == decl;
20778 if (token->type != CPP_PLUS
20779 && token->type != CPP_MINUS)
20780 return error_mark_node;
20784 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20785 cp_lexer_consume_token (parser->lexer);
20786 rhs = cp_parser_binary_expression (parser, false,
20787 PREC_ADDITIVE_EXPRESSION);
20788 token = cp_lexer_peek_token (parser->lexer);
20789 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20791 if (lhs == NULL_TREE)
20793 if (op == PLUS_EXPR)
20796 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20799 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20800 NULL, tf_warning_or_error);
20803 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20807 if (rhs != decl || op == MINUS_EXPR)
20808 return error_mark_node;
20809 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20812 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20814 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20817 /* Parse the restricted form of the for statement allowed by OpenMP. */
20820 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20822 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20823 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20824 tree this_pre_body, cl;
20825 location_t loc_first;
20826 bool collapse_err = false;
20827 int i, collapse = 1, nbraces = 0;
20829 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20830 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20831 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20833 gcc_assert (collapse >= 1);
20835 declv = make_tree_vec (collapse);
20836 initv = make_tree_vec (collapse);
20837 condv = make_tree_vec (collapse);
20838 incrv = make_tree_vec (collapse);
20840 loc_first = cp_lexer_peek_token (parser->lexer)->location;
20842 for (i = 0; i < collapse; i++)
20844 int bracecount = 0;
20845 bool add_private_clause = false;
20848 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20850 cp_parser_error (parser, "for statement expected");
20853 loc = cp_lexer_consume_token (parser->lexer)->location;
20855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20858 init = decl = real_decl = NULL;
20859 this_pre_body = push_stmt_list ();
20860 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20862 cp_decl_specifier_seq type_specifiers;
20864 /* First, try to parse as an initialized declaration. See
20865 cp_parser_condition, from whence the bulk of this is copied. */
20867 cp_parser_parse_tentatively (parser);
20868 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20870 if (!cp_parser_error_occurred (parser))
20872 tree asm_specification, attributes;
20873 cp_declarator *declarator;
20875 declarator = cp_parser_declarator (parser,
20876 CP_PARSER_DECLARATOR_NAMED,
20877 /*ctor_dtor_or_conv_p=*/NULL,
20878 /*parenthesized_p=*/NULL,
20879 /*member_p=*/false);
20880 attributes = cp_parser_attributes_opt (parser);
20881 asm_specification = cp_parser_asm_specification_opt (parser);
20883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20884 cp_parser_require (parser, CPP_EQ, "%<=%>");
20885 if (cp_parser_parse_definitely (parser))
20889 decl = start_decl (declarator, &type_specifiers,
20890 /*initialized_p=*/false, attributes,
20891 /*prefix_attributes=*/NULL_TREE,
20894 if (CLASS_TYPE_P (TREE_TYPE (decl))
20895 || type_dependent_expression_p (decl))
20897 bool is_direct_init, is_non_constant_init;
20899 init = cp_parser_initializer (parser,
20901 &is_non_constant_init);
20903 cp_finish_decl (decl, init, !is_non_constant_init,
20905 LOOKUP_ONLYCONVERTING);
20906 if (CLASS_TYPE_P (TREE_TYPE (decl)))
20909 = tree_cons (NULL, this_pre_body, for_block);
20913 init = pop_stmt_list (this_pre_body);
20914 this_pre_body = NULL_TREE;
20918 cp_parser_require (parser, CPP_EQ, "%<=%>");
20919 init = cp_parser_assignment_expression (parser, false);
20921 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20922 init = error_mark_node;
20924 cp_finish_decl (decl, NULL_TREE,
20925 /*init_const_expr_p=*/false,
20927 LOOKUP_ONLYCONVERTING);
20931 pop_scope (pushed_scope);
20935 cp_parser_abort_tentative_parse (parser);
20937 /* If parsing as an initialized declaration failed, try again as
20938 a simple expression. */
20942 cp_parser_parse_tentatively (parser);
20943 decl = cp_parser_primary_expression (parser, false, false,
20945 if (!cp_parser_error_occurred (parser)
20948 && CLASS_TYPE_P (TREE_TYPE (decl)))
20952 cp_parser_parse_definitely (parser);
20953 cp_parser_require (parser, CPP_EQ, "%<=%>");
20954 rhs = cp_parser_assignment_expression (parser, false);
20955 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20957 tf_warning_or_error));
20958 add_private_clause = true;
20963 cp_parser_abort_tentative_parse (parser);
20964 init = cp_parser_expression (parser, false);
20967 if (TREE_CODE (init) == MODIFY_EXPR
20968 || TREE_CODE (init) == MODOP_EXPR)
20969 real_decl = TREE_OPERAND (init, 0);
20974 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20977 this_pre_body = pop_stmt_list (this_pre_body);
20981 pre_body = push_stmt_list ();
20983 add_stmt (this_pre_body);
20984 pre_body = pop_stmt_list (pre_body);
20987 pre_body = this_pre_body;
20992 if (par_clauses != NULL && real_decl != NULL_TREE)
20995 for (c = par_clauses; *c ; )
20996 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20997 && OMP_CLAUSE_DECL (*c) == real_decl)
20999 error ("%Hiteration variable %qD should not be firstprivate",
21001 *c = OMP_CLAUSE_CHAIN (*c);
21003 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21004 && OMP_CLAUSE_DECL (*c) == real_decl)
21006 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21007 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21008 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21009 OMP_CLAUSE_DECL (l) = real_decl;
21010 OMP_CLAUSE_CHAIN (l) = clauses;
21011 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21013 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21014 CP_OMP_CLAUSE_INFO (*c) = NULL;
21015 add_private_clause = false;
21019 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21020 && OMP_CLAUSE_DECL (*c) == real_decl)
21021 add_private_clause = false;
21022 c = &OMP_CLAUSE_CHAIN (*c);
21026 if (add_private_clause)
21029 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21031 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21032 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21033 && OMP_CLAUSE_DECL (c) == decl)
21035 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21036 && OMP_CLAUSE_DECL (c) == decl)
21037 error ("%Hiteration variable %qD should not be firstprivate",
21039 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21040 && OMP_CLAUSE_DECL (c) == decl)
21041 error ("%Hiteration variable %qD should not be reduction",
21046 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21047 OMP_CLAUSE_DECL (c) = decl;
21048 c = finish_omp_clauses (c);
21051 OMP_CLAUSE_CHAIN (c) = clauses;
21058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21060 /* If decl is an iterator, preserve LHS and RHS of the relational
21061 expr until finish_omp_for. */
21063 && (type_dependent_expression_p (decl)
21064 || CLASS_TYPE_P (TREE_TYPE (decl))))
21065 cond = cp_parser_omp_for_cond (parser, decl);
21067 cond = cp_parser_condition (parser);
21069 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21074 /* If decl is an iterator, preserve the operator on decl
21075 until finish_omp_for. */
21077 && (type_dependent_expression_p (decl)
21078 || CLASS_TYPE_P (TREE_TYPE (decl))))
21079 incr = cp_parser_omp_for_incr (parser, decl);
21081 incr = cp_parser_expression (parser, false);
21084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21085 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21086 /*or_comma=*/false,
21087 /*consume_paren=*/true);
21089 TREE_VEC_ELT (declv, i) = decl;
21090 TREE_VEC_ELT (initv, i) = init;
21091 TREE_VEC_ELT (condv, i) = cond;
21092 TREE_VEC_ELT (incrv, i) = incr;
21094 if (i == collapse - 1)
21097 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21098 in between the collapsed for loops to be still considered perfectly
21099 nested. Hopefully the final version clarifies this.
21100 For now handle (multiple) {'s and empty statements. */
21101 cp_parser_parse_tentatively (parser);
21104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21106 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21108 cp_lexer_consume_token (parser->lexer);
21111 else if (bracecount
21112 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21113 cp_lexer_consume_token (parser->lexer);
21116 loc = cp_lexer_peek_token (parser->lexer)->location;
21117 error ("%Hnot enough collapsed for loops", &loc);
21118 collapse_err = true;
21119 cp_parser_abort_tentative_parse (parser);
21128 cp_parser_parse_definitely (parser);
21129 nbraces += bracecount;
21133 /* Note that we saved the original contents of this flag when we entered
21134 the structured block, and so we don't need to re-save it here. */
21135 parser->in_statement = IN_OMP_FOR;
21137 /* Note that the grammar doesn't call for a structured block here,
21138 though the loop as a whole is a structured block. */
21139 body = push_stmt_list ();
21140 cp_parser_statement (parser, NULL_TREE, false, NULL);
21141 body = pop_stmt_list (body);
21143 if (declv == NULL_TREE)
21146 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21147 pre_body, clauses);
21151 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21153 cp_lexer_consume_token (parser->lexer);
21156 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21157 cp_lexer_consume_token (parser->lexer);
21162 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21163 error ("%Hcollapsed loops not perfectly nested", &loc);
21165 collapse_err = true;
21166 cp_parser_statement_seq_opt (parser, NULL);
21167 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21173 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21174 for_block = TREE_CHAIN (for_block);
21181 #pragma omp for for-clause[optseq] new-line
21184 #define OMP_FOR_CLAUSE_MASK \
21185 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21186 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21187 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21188 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21189 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21190 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21191 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21192 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21195 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21197 tree clauses, sb, ret;
21200 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21201 "#pragma omp for", pragma_tok);
21203 sb = begin_omp_structured_block ();
21204 save = cp_parser_begin_omp_structured_block (parser);
21206 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21208 cp_parser_end_omp_structured_block (parser, save);
21209 add_stmt (finish_omp_structured_block (sb));
21215 # pragma omp master new-line
21216 structured-block */
21219 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21221 cp_parser_require_pragma_eol (parser, pragma_tok);
21222 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21226 # pragma omp ordered new-line
21227 structured-block */
21230 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21232 cp_parser_require_pragma_eol (parser, pragma_tok);
21233 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21239 { section-sequence }
21242 section-directive[opt] structured-block
21243 section-sequence section-directive structured-block */
21246 cp_parser_omp_sections_scope (cp_parser *parser)
21248 tree stmt, substmt;
21249 bool error_suppress = false;
21252 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21255 stmt = push_stmt_list ();
21257 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21261 substmt = begin_omp_structured_block ();
21262 save = cp_parser_begin_omp_structured_block (parser);
21266 cp_parser_statement (parser, NULL_TREE, false, NULL);
21268 tok = cp_lexer_peek_token (parser->lexer);
21269 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21271 if (tok->type == CPP_CLOSE_BRACE)
21273 if (tok->type == CPP_EOF)
21277 cp_parser_end_omp_structured_block (parser, save);
21278 substmt = finish_omp_structured_block (substmt);
21279 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21280 add_stmt (substmt);
21285 tok = cp_lexer_peek_token (parser->lexer);
21286 if (tok->type == CPP_CLOSE_BRACE)
21288 if (tok->type == CPP_EOF)
21291 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21293 cp_lexer_consume_token (parser->lexer);
21294 cp_parser_require_pragma_eol (parser, tok);
21295 error_suppress = false;
21297 else if (!error_suppress)
21299 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21300 error_suppress = true;
21303 substmt = cp_parser_omp_structured_block (parser);
21304 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21305 add_stmt (substmt);
21307 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21309 substmt = pop_stmt_list (stmt);
21311 stmt = make_node (OMP_SECTIONS);
21312 TREE_TYPE (stmt) = void_type_node;
21313 OMP_SECTIONS_BODY (stmt) = substmt;
21320 # pragma omp sections sections-clause[optseq] newline
21323 #define OMP_SECTIONS_CLAUSE_MASK \
21324 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21325 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21326 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21327 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21328 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21331 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21335 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21336 "#pragma omp sections", pragma_tok);
21338 ret = cp_parser_omp_sections_scope (parser);
21340 OMP_SECTIONS_CLAUSES (ret) = clauses;
21346 # pragma parallel parallel-clause new-line
21347 # pragma parallel for parallel-for-clause new-line
21348 # pragma parallel sections parallel-sections-clause new-line */
21350 #define OMP_PARALLEL_CLAUSE_MASK \
21351 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21352 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21353 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21354 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21355 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21356 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21357 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21358 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21361 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21363 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21364 const char *p_name = "#pragma omp parallel";
21365 tree stmt, clauses, par_clause, ws_clause, block;
21366 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21371 cp_lexer_consume_token (parser->lexer);
21372 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21373 p_name = "#pragma omp parallel for";
21374 mask |= OMP_FOR_CLAUSE_MASK;
21375 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21377 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21379 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21380 const char *p = IDENTIFIER_POINTER (id);
21381 if (strcmp (p, "sections") == 0)
21383 cp_lexer_consume_token (parser->lexer);
21384 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21385 p_name = "#pragma omp parallel sections";
21386 mask |= OMP_SECTIONS_CLAUSE_MASK;
21387 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21391 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21392 block = begin_omp_parallel ();
21393 save = cp_parser_begin_omp_structured_block (parser);
21397 case PRAGMA_OMP_PARALLEL:
21398 cp_parser_statement (parser, NULL_TREE, false, NULL);
21399 par_clause = clauses;
21402 case PRAGMA_OMP_PARALLEL_FOR:
21403 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21404 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21407 case PRAGMA_OMP_PARALLEL_SECTIONS:
21408 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21409 stmt = cp_parser_omp_sections_scope (parser);
21411 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21415 gcc_unreachable ();
21418 cp_parser_end_omp_structured_block (parser, save);
21419 stmt = finish_omp_parallel (par_clause, block);
21420 if (p_kind != PRAGMA_OMP_PARALLEL)
21421 OMP_PARALLEL_COMBINED (stmt) = 1;
21426 # pragma omp single single-clause[optseq] new-line
21427 structured-block */
21429 #define OMP_SINGLE_CLAUSE_MASK \
21430 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21431 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21432 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21433 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21436 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21438 tree stmt = make_node (OMP_SINGLE);
21439 TREE_TYPE (stmt) = void_type_node;
21441 OMP_SINGLE_CLAUSES (stmt)
21442 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21443 "#pragma omp single", pragma_tok);
21444 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21446 return add_stmt (stmt);
21450 # pragma omp task task-clause[optseq] new-line
21451 structured-block */
21453 #define OMP_TASK_CLAUSE_MASK \
21454 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21455 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21456 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21457 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21458 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21459 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21462 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21464 tree clauses, block;
21467 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21468 "#pragma omp task", pragma_tok);
21469 block = begin_omp_task ();
21470 save = cp_parser_begin_omp_structured_block (parser);
21471 cp_parser_statement (parser, NULL_TREE, false, NULL);
21472 cp_parser_end_omp_structured_block (parser, save);
21473 return finish_omp_task (clauses, block);
21477 # pragma omp taskwait new-line */
21480 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21482 cp_parser_require_pragma_eol (parser, pragma_tok);
21483 finish_omp_taskwait ();
21487 # pragma omp threadprivate (variable-list) */
21490 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21494 vars = cp_parser_omp_var_list (parser, 0, NULL);
21495 cp_parser_require_pragma_eol (parser, pragma_tok);
21497 finish_omp_threadprivate (vars);
21500 /* Main entry point to OpenMP statement pragmas. */
21503 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21507 switch (pragma_tok->pragma_kind)
21509 case PRAGMA_OMP_ATOMIC:
21510 cp_parser_omp_atomic (parser, pragma_tok);
21512 case PRAGMA_OMP_CRITICAL:
21513 stmt = cp_parser_omp_critical (parser, pragma_tok);
21515 case PRAGMA_OMP_FOR:
21516 stmt = cp_parser_omp_for (parser, pragma_tok);
21518 case PRAGMA_OMP_MASTER:
21519 stmt = cp_parser_omp_master (parser, pragma_tok);
21521 case PRAGMA_OMP_ORDERED:
21522 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21524 case PRAGMA_OMP_PARALLEL:
21525 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21527 case PRAGMA_OMP_SECTIONS:
21528 stmt = cp_parser_omp_sections (parser, pragma_tok);
21530 case PRAGMA_OMP_SINGLE:
21531 stmt = cp_parser_omp_single (parser, pragma_tok);
21533 case PRAGMA_OMP_TASK:
21534 stmt = cp_parser_omp_task (parser, pragma_tok);
21537 gcc_unreachable ();
21541 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21546 static GTY (()) cp_parser *the_parser;
21549 /* Special handling for the first token or line in the file. The first
21550 thing in the file might be #pragma GCC pch_preprocess, which loads a
21551 PCH file, which is a GC collection point. So we need to handle this
21552 first pragma without benefit of an existing lexer structure.
21554 Always returns one token to the caller in *FIRST_TOKEN. This is
21555 either the true first token of the file, or the first token after
21556 the initial pragma. */
21559 cp_parser_initial_pragma (cp_token *first_token)
21563 cp_lexer_get_preprocessor_token (NULL, first_token);
21564 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21567 cp_lexer_get_preprocessor_token (NULL, first_token);
21568 if (first_token->type == CPP_STRING)
21570 name = first_token->u.value;
21572 cp_lexer_get_preprocessor_token (NULL, first_token);
21573 if (first_token->type != CPP_PRAGMA_EOL)
21574 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21575 &first_token->location);
21578 error ("%Hexpected string literal", &first_token->location);
21580 /* Skip to the end of the pragma. */
21581 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21582 cp_lexer_get_preprocessor_token (NULL, first_token);
21584 /* Now actually load the PCH file. */
21586 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21588 /* Read one more token to return to our caller. We have to do this
21589 after reading the PCH file in, since its pointers have to be
21591 cp_lexer_get_preprocessor_token (NULL, first_token);
21594 /* Normal parsing of a pragma token. Here we can (and must) use the
21598 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21600 cp_token *pragma_tok;
21603 pragma_tok = cp_lexer_consume_token (parser->lexer);
21604 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21605 parser->lexer->in_pragma = true;
21607 id = pragma_tok->pragma_kind;
21610 case PRAGMA_GCC_PCH_PREPROCESS:
21611 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21612 &pragma_tok->location);
21615 case PRAGMA_OMP_BARRIER:
21618 case pragma_compound:
21619 cp_parser_omp_barrier (parser, pragma_tok);
21622 error ("%H%<#pragma omp barrier%> may only be "
21623 "used in compound statements", &pragma_tok->location);
21630 case PRAGMA_OMP_FLUSH:
21633 case pragma_compound:
21634 cp_parser_omp_flush (parser, pragma_tok);
21637 error ("%H%<#pragma omp flush%> may only be "
21638 "used in compound statements", &pragma_tok->location);
21645 case PRAGMA_OMP_TASKWAIT:
21648 case pragma_compound:
21649 cp_parser_omp_taskwait (parser, pragma_tok);
21652 error ("%H%<#pragma omp taskwait%> may only be "
21653 "used in compound statements",
21654 &pragma_tok->location);
21661 case PRAGMA_OMP_THREADPRIVATE:
21662 cp_parser_omp_threadprivate (parser, pragma_tok);
21665 case PRAGMA_OMP_ATOMIC:
21666 case PRAGMA_OMP_CRITICAL:
21667 case PRAGMA_OMP_FOR:
21668 case PRAGMA_OMP_MASTER:
21669 case PRAGMA_OMP_ORDERED:
21670 case PRAGMA_OMP_PARALLEL:
21671 case PRAGMA_OMP_SECTIONS:
21672 case PRAGMA_OMP_SINGLE:
21673 case PRAGMA_OMP_TASK:
21674 if (context == pragma_external)
21676 cp_parser_omp_construct (parser, pragma_tok);
21679 case PRAGMA_OMP_SECTION:
21680 error ("%H%<#pragma omp section%> may only be used in "
21681 "%<#pragma omp sections%> construct", &pragma_tok->location);
21685 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21686 c_invoke_pragma_handler (id);
21690 cp_parser_error (parser, "expected declaration specifiers");
21694 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21698 /* The interface the pragma parsers have to the lexer. */
21701 pragma_lex (tree *value)
21704 enum cpp_ttype ret;
21706 tok = cp_lexer_peek_token (the_parser->lexer);
21709 *value = tok->u.value;
21711 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21713 else if (ret == CPP_STRING)
21714 *value = cp_parser_string_literal (the_parser, false, false);
21717 cp_lexer_consume_token (the_parser->lexer);
21718 if (ret == CPP_KEYWORD)
21726 /* External interface. */
21728 /* Parse one entire translation unit. */
21731 c_parse_file (void)
21733 bool error_occurred;
21734 static bool already_called = false;
21736 if (already_called)
21738 sorry ("inter-module optimizations not implemented for C++");
21741 already_called = true;
21743 the_parser = cp_parser_new ();
21744 push_deferring_access_checks (flag_access_control
21745 ? dk_no_deferred : dk_no_check);
21746 error_occurred = cp_parser_translation_unit (the_parser);
21750 #include "gt-cp-parser.h"