2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 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"
35 #include "diagnostic.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 /* A token's value and its associated deferred access checks and
52 struct GTY(()) tree_check {
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
64 typedef struct GTY (()) cp_token {
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 location at which this token was found. */
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
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, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
106 typedef struct GTY (()) cp_lexer {
107 /* The memory allocated for the buffer. NULL if this lexer does not
108 own the token buffer. */
109 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110 /* If the lexer owns the buffer, this is the number of tokens in the
112 size_t buffer_length;
114 /* A pointer just past the last available token. The tokens
115 in this lexer are [buffer, last_token). */
116 cp_token_position GTY ((skip)) last_token;
118 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
119 no more available tokens. */
120 cp_token_position GTY ((skip)) next_token;
122 /* A stack indicating positions at which cp_lexer_save_tokens was
123 called. The top entry is the most recent position at which we
124 began saving tokens. If the stack is non-empty, we are saving
126 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128 /* The next lexer in a linked list of lexers. */
129 struct cp_lexer *next;
131 /* True if we should output debugging information. */
134 /* True if we're in the context of parsing a pragma, and should not
135 increment past the end-of-line marker. */
139 /* cp_token_cache is a range of tokens. There is no need to represent
140 allocate heap memory for it, since tokens are never removed from the
141 lexer's array. There is also no need for the GC to walk through
142 a cp_token_cache, since everything in here is referenced through
145 typedef struct GTY(()) cp_token_cache {
146 /* The beginning of the token range. */
147 cp_token * GTY((skip)) first;
149 /* Points immediately after the last token in the range. */
150 cp_token * GTY ((skip)) last;
155 static cp_lexer *cp_lexer_new_main
157 static cp_lexer *cp_lexer_new_from_tokens
158 (cp_token_cache *tokens);
159 static void cp_lexer_destroy
161 static int cp_lexer_saving_tokens
163 static cp_token_position cp_lexer_token_position
165 static cp_token *cp_lexer_token_at
166 (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168 (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
171 static cp_token *cp_lexer_peek_nth_token
172 (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174 (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178 (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
181 static void cp_lexer_purge_token
183 static void cp_lexer_purge_tokens_after
184 (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
187 static void cp_lexer_commit_tokens
189 static void cp_lexer_rollback_tokens
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193 (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
196 static void cp_lexer_start_debugging
197 (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202 about passing NULL to functions that require non-NULL arguments
203 (fputs, fprintf). It will never be used, so all we need is a value
204 of the right type that's guaranteed not to be NULL. */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
210 static cp_token_cache *cp_token_cache_new
211 (cp_token *, cp_token *);
213 static void cp_parser_initial_pragma
216 /* Manifest constants. */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
220 /* A token type for keywords, as opposed to ordinary identifiers. */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
223 /* A token type for template-ids. If a template-id is processed while
224 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225 the value of the CPP_TEMPLATE_ID is whatever was returned by
226 cp_parser_template_id. */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
229 /* A token type for nested-name-specifiers. If a
230 nested-name-specifier is processed while parsing tentatively, it is
231 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233 cp_parser_nested_name_specifier_opt. */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
236 /* A token type for tokens that are not tokens at all; these are used
237 to represent slots in the array where there used to be a token
238 that has now been deleted. */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
241 /* The number of token types, including C++-specific ones. */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written. */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252 sizeof, typeof, or alignof. */
253 int cp_unevaluated_operand;
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
259 cp_lexer_new_main (void)
261 cp_token first_token;
268 /* It's possible that parsing the first pragma will load a PCH file,
269 which is a GC collection point. So we have to do that before
270 allocating any memory. */
271 cp_parser_initial_pragma (&first_token);
273 c_common_no_more_pch ();
275 /* Allocate the memory. */
276 lexer = GGC_CNEW (cp_lexer);
278 #ifdef ENABLE_CHECKING
279 /* Initially we are not debugging. */
280 lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283 CP_SAVED_TOKEN_STACK);
285 /* Create the buffer. */
286 alloc = CP_LEXER_BUFFER_SIZE;
287 buffer = GGC_NEWVEC (cp_token, alloc);
289 /* Put the first token in the buffer. */
294 /* Get the remaining tokens from the preprocessor. */
295 while (pos->type != CPP_EOF)
302 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303 pos = buffer + space;
305 cp_lexer_get_preprocessor_token (lexer, pos);
307 lexer->buffer = buffer;
308 lexer->buffer_length = alloc - space;
309 lexer->last_token = pos;
310 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312 /* Subsequent preprocessor diagnostics should use compiler
313 diagnostic functions to get the compiler source location. */
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_STRING_NO_JOIN);
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);
429 if (warn_cxx0x_compat
430 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
433 /* Warn about the C++0x keyword (but still treat it as
435 warning (OPT_Wc__0x_compat,
436 "identifier %qE will become a keyword in C++0x",
439 /* Clear out the C_RID_CODE so we don't warn about this
440 particular identifier-turned-keyword again. */
441 C_SET_RID_CODE (token->u.value, RID_MAX);
444 token->ambiguous_p = false;
445 token->keyword = RID_MAX;
448 /* Handle Objective-C++ keywords. */
449 else if (token->type == CPP_AT_NAME)
451 token->type = CPP_KEYWORD;
452 switch (C_RID_CODE (token->u.value))
454 /* Map 'class' to '@class', 'private' to '@private', etc. */
455 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459 case RID_THROW: token->keyword = RID_AT_THROW; break;
460 case RID_TRY: token->keyword = RID_AT_TRY; break;
461 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462 default: token->keyword = C_RID_CODE (token->u.value);
465 else if (token->type == CPP_PRAGMA)
467 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
468 token->pragma_kind = ((enum pragma_kind)
469 TREE_INT_CST_LOW (token->u.value));
470 token->u.value = NULL_TREE;
474 /* Update the globals input_location and the input file stack from TOKEN. */
476 cp_lexer_set_source_position_from_token (cp_token *token)
478 if (token->type != CPP_EOF)
480 input_location = token->location;
484 /* Return a pointer to the next token in the token stream, but do not
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
490 if (cp_lexer_debugging_p (lexer))
492 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494 putc ('\n', cp_lexer_debug_stream);
496 return lexer->next_token;
499 /* Return true if the next token has the indicated TYPE. */
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
504 return cp_lexer_peek_token (lexer)->type == type;
507 /* Return true if the next token does not have the indicated TYPE. */
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
512 return !cp_lexer_next_token_is (lexer, type);
515 /* Return true if the next token is the indicated KEYWORD. */
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
520 return cp_lexer_peek_token (lexer)->keyword == keyword;
523 /* Return true if the next token is not the indicated KEYWORD. */
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
528 return cp_lexer_peek_token (lexer)->keyword != keyword;
531 /* Return true if the next token is a keyword for a decl-specifier. */
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
538 token = cp_lexer_peek_token (lexer);
539 switch (token->keyword)
541 /* auto specifier: storage-class-specifier in C++,
542 simple-type-specifier in C++0x. */
544 /* Storage classes. */
550 /* Elaborated type specifiers. */
556 /* Simple type specifiers. */
570 /* GNU extensions. */
573 /* C++0x extensions. */
582 /* Return a pointer to the Nth token in the token stream. If N is 1,
583 then this is precisely equivalent to cp_lexer_peek_token (except
584 that it is not inline). One would like to disallow that case, but
585 there is one case (cp_parser_nth_token_starts_template_id) where
586 the caller passes a variable for N and it might be 1. */
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
593 /* N is 1-based, not zero-based. */
596 if (cp_lexer_debugging_p (lexer))
597 fprintf (cp_lexer_debug_stream,
598 "cp_lexer: peeking ahead %ld at token: ", (long)n);
601 token = lexer->next_token;
602 gcc_assert (!n || token != &eof_token);
606 if (token == lexer->last_token)
612 if (token->type != CPP_PURGED)
616 if (cp_lexer_debugging_p (lexer))
618 cp_lexer_print_token (cp_lexer_debug_stream, token);
619 putc ('\n', cp_lexer_debug_stream);
625 /* Return the next token, and advance the lexer's next_token pointer
626 to point to the next non-purged token. */
629 cp_lexer_consume_token (cp_lexer* lexer)
631 cp_token *token = lexer->next_token;
633 gcc_assert (token != &eof_token);
634 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639 if (lexer->next_token == lexer->last_token)
641 lexer->next_token = &eof_token;
646 while (lexer->next_token->type == CPP_PURGED);
648 cp_lexer_set_source_position_from_token (token);
650 /* Provide debugging output. */
651 if (cp_lexer_debugging_p (lexer))
653 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654 cp_lexer_print_token (cp_lexer_debug_stream, token);
655 putc ('\n', cp_lexer_debug_stream);
661 /* Permanently remove the next token from the token stream, and
662 advance the next_token pointer to refer to the next non-purged
666 cp_lexer_purge_token (cp_lexer *lexer)
668 cp_token *tok = lexer->next_token;
670 gcc_assert (tok != &eof_token);
671 tok->type = CPP_PURGED;
672 tok->location = UNKNOWN_LOCATION;
673 tok->u.value = NULL_TREE;
674 tok->keyword = RID_MAX;
679 if (tok == lexer->last_token)
685 while (tok->type == CPP_PURGED);
686 lexer->next_token = tok;
689 /* Permanently remove all tokens after TOK, up to, but not
690 including, the token that will be returned next by
691 cp_lexer_peek_token. */
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
696 cp_token *peek = lexer->next_token;
698 if (peek == &eof_token)
699 peek = lexer->last_token;
701 gcc_assert (tok < peek);
703 for ( tok += 1; tok != peek; tok += 1)
705 tok->type = CPP_PURGED;
706 tok->location = UNKNOWN_LOCATION;
707 tok->u.value = NULL_TREE;
708 tok->keyword = RID_MAX;
712 /* Begin saving tokens. All tokens consumed after this point will be
716 cp_lexer_save_tokens (cp_lexer* lexer)
718 /* Provide debugging output. */
719 if (cp_lexer_debugging_p (lexer))
720 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
722 VEC_safe_push (cp_token_position, heap,
723 lexer->saved_tokens, lexer->next_token);
726 /* Commit to the portion of the token stream most recently saved. */
729 cp_lexer_commit_tokens (cp_lexer* lexer)
731 /* Provide debugging output. */
732 if (cp_lexer_debugging_p (lexer))
733 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
735 VEC_pop (cp_token_position, lexer->saved_tokens);
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739 to the token stream. Stop saving tokens. */
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
744 /* Provide debugging output. */
745 if (cp_lexer_debugging_p (lexer))
746 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
748 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 /* Print a representation of the TOKEN on the STREAM. */
753 #ifdef ENABLE_CHECKING
756 cp_lexer_print_token (FILE * stream, cp_token *token)
758 /* We don't use cpp_type2name here because the parser defines
759 a few tokens of its own. */
760 static const char *const token_names[] = {
761 /* cpplib-defined token types */
767 /* C++ parser token types - see "Manifest constants", above. */
770 "NESTED_NAME_SPECIFIER",
774 /* If we have a name for the token, print it out. Otherwise, we
775 simply give the numeric code. */
776 gcc_assert (token->type < ARRAY_SIZE(token_names));
777 fputs (token_names[token->type], stream);
779 /* For some tokens, print the associated data. */
783 /* Some keywords have a value that is not an IDENTIFIER_NODE.
784 For example, `struct' is mapped to an INTEGER_CST. */
785 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
787 /* else fall through */
789 fputs (IDENTIFIER_POINTER (token->u.value), stream);
797 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
805 /* Start emitting debugging information. */
808 cp_lexer_start_debugging (cp_lexer* lexer)
810 lexer->debugging_p = true;
813 /* Stop emitting debugging information. */
816 cp_lexer_stop_debugging (cp_lexer* lexer)
818 lexer->debugging_p = false;
821 #endif /* ENABLE_CHECKING */
823 /* Create a new cp_token_cache, representing a range of tokens. */
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
828 cp_token_cache *cache = GGC_NEW (cp_token_cache);
829 cache->first = first;
835 /* Decl-specifiers. */
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
847 /* Nothing other than the parser should be creating declarators;
848 declarators are a semi-syntactic representation of C++ entities.
849 Other parts of the front end that need to create entities (like
850 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
852 static cp_declarator *make_call_declarator
853 (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855 (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857 (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859 (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861 (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863 (cp_cv_quals, tree, cp_declarator *);
865 /* An erroneous declarator. */
866 static cp_declarator *cp_error_declarator;
868 /* The obstack on which declarators and related data structures are
870 static struct obstack declarator_obstack;
872 /* Alloc BYTES from the declarator memory pool. */
875 alloc_declarator (size_t bytes)
877 return obstack_alloc (&declarator_obstack, bytes);
880 /* Allocate a declarator of the indicated KIND. Clear fields that are
881 common to all declarators. */
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
886 cp_declarator *declarator;
888 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889 declarator->kind = kind;
890 declarator->attributes = NULL_TREE;
891 declarator->declarator = NULL;
892 declarator->parameter_pack_p = false;
897 /* Make a declarator for a generalized identifier. If
898 QUALIFYING_SCOPE is non-NULL, the identifier is
899 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900 UNQUALIFIED_NAME. SFK indicates the kind of special function this
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905 special_function_kind sfk)
907 cp_declarator *declarator;
909 /* It is valid to write:
911 class C { void f(); };
915 The standard is not clear about whether `typedef const C D' is
916 legal; as of 2002-09-15 the committee is considering that
917 question. EDG 3.0 allows that syntax. Therefore, we do as
919 if (qualifying_scope && TYPE_P (qualifying_scope))
920 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926 declarator = make_declarator (cdk_id);
927 declarator->u.id.qualifying_scope = qualifying_scope;
928 declarator->u.id.unqualified_name = unqualified_name;
929 declarator->u.id.sfk = sfk;
934 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
935 of modifiers such as const or volatile to apply to the pointer
936 type, represented as identifiers. */
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 cp_declarator *declarator;
943 declarator = make_declarator (cdk_pointer);
944 declarator->declarator = target;
945 declarator->u.pointer.qualifiers = cv_qualifiers;
946 declarator->u.pointer.class_type = NULL_TREE;
949 declarator->parameter_pack_p = target->parameter_pack_p;
950 target->parameter_pack_p = false;
953 declarator->parameter_pack_p = false;
958 /* Like make_pointer_declarator -- but for references. */
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
964 cp_declarator *declarator;
966 declarator = make_declarator (cdk_reference);
967 declarator->declarator = target;
968 declarator->u.reference.qualifiers = cv_qualifiers;
969 declarator->u.reference.rvalue_ref = rvalue_ref;
972 declarator->parameter_pack_p = target->parameter_pack_p;
973 target->parameter_pack_p = false;
976 declarator->parameter_pack_p = false;
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982 member of CLASS_TYPE. */
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986 cp_declarator *pointee)
988 cp_declarator *declarator;
990 declarator = make_declarator (cdk_ptrmem);
991 declarator->declarator = pointee;
992 declarator->u.pointer.qualifiers = cv_qualifiers;
993 declarator->u.pointer.class_type = class_type;
997 declarator->parameter_pack_p = pointee->parameter_pack_p;
998 pointee->parameter_pack_p = false;
1001 declarator->parameter_pack_p = false;
1006 /* Make a declarator for the function given by TARGET, with the
1007 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1008 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1009 indicates what exceptions can be thrown. */
1012 make_call_declarator (cp_declarator *target,
1014 cp_cv_quals cv_qualifiers,
1015 tree exception_specification,
1016 tree late_return_type)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1025 declarator->u.function.late_return_type = late_return_type;
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1032 declarator->parameter_pack_p = false;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1054 declarator->parameter_pack_p = false;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1082 declarator = declarator->declarator;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1108 clear_decl_specs (¶meter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 function_declarator_p (const cp_declarator *declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1129 declarator = declarator->declarator;
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, treat user-defined type-names
1200 as non-type identifiers. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202 /* When parsing a type-specifier, do not try to parse a class-specifier
1203 or enum-specifier. */
1204 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1207 /* This type is used for parameters and variables which hold
1208 combinations of the above flags. */
1209 typedef int cp_parser_flags;
1211 /* The different kinds of declarators we want to parse. */
1213 typedef enum cp_parser_declarator_kind
1215 /* We want an abstract declarator. */
1216 CP_PARSER_DECLARATOR_ABSTRACT,
1217 /* We want a named declarator. */
1218 CP_PARSER_DECLARATOR_NAMED,
1219 /* We don't mind, but the name must be an unqualified-id. */
1220 CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1223 /* The precedence values used to parse binary expressions. The minimum value
1224 of PREC must be 1, because zero is reserved to quickly discriminate
1225 binary operators from other tokens. */
1230 PREC_LOGICAL_OR_EXPRESSION,
1231 PREC_LOGICAL_AND_EXPRESSION,
1232 PREC_INCLUSIVE_OR_EXPRESSION,
1233 PREC_EXCLUSIVE_OR_EXPRESSION,
1234 PREC_AND_EXPRESSION,
1235 PREC_EQUALITY_EXPRESSION,
1236 PREC_RELATIONAL_EXPRESSION,
1237 PREC_SHIFT_EXPRESSION,
1238 PREC_ADDITIVE_EXPRESSION,
1239 PREC_MULTIPLICATIVE_EXPRESSION,
1241 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245 precedence value. */
1247 typedef struct cp_parser_binary_operations_map_node
1249 /* The token type. */
1250 enum cpp_ttype token_type;
1251 /* The corresponding tree code. */
1252 enum tree_code tree_type;
1253 /* The precedence of this operator. */
1254 enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1257 /* The status of a tentative parse. */
1259 typedef enum cp_parser_status_kind
1261 /* No errors have occurred. */
1262 CP_PARSER_STATUS_KIND_NO_ERROR,
1263 /* An error has occurred. */
1264 CP_PARSER_STATUS_KIND_ERROR,
1265 /* We are committed to this tentative parse, whether or not an error
1267 CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1270 typedef struct cp_parser_expression_stack_entry
1272 /* Left hand side of the binary operation we are currently
1275 /* Original tree code for left hand side, if it was a binary
1276 expression itself (used for -Wparentheses). */
1277 enum tree_code lhs_type;
1278 /* Tree code for the binary operation we are parsing. */
1279 enum tree_code tree_type;
1280 /* Precedence of the binary operation we are parsing. */
1281 enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1284 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1285 entries because precedence levels on the stack are monotonically
1287 typedef struct cp_parser_expression_stack_entry
1288 cp_parser_expression_stack[NUM_PREC_VALUES];
1290 /* Context that is saved and restored when parsing tentatively. */
1291 typedef struct GTY (()) cp_parser_context {
1292 /* If this is a tentative parsing context, the status of the
1294 enum cp_parser_status_kind status;
1295 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1296 that are looked up in this context must be looked up both in the
1297 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298 the context of the containing expression. */
1301 /* The next parsing context in the stack. */
1302 struct cp_parser_context *next;
1303 } cp_parser_context;
1307 /* Constructors and destructors. */
1309 static cp_parser_context *cp_parser_context_new
1310 (cp_parser_context *);
1312 /* Class variables. */
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317 Transformed into an associative array (binops_by_token) by
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1353 /* The same as binops, but initialized by cp_parser_new so that
1354 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358 /* Constructors and destructors. */
1360 /* Construct a new context. The context below this one on the stack
1361 is given by NEXT. */
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1366 cp_parser_context *context;
1368 /* Allocate the storage. */
1369 if (cp_parser_context_free_list != NULL)
1371 /* Pull the first entry from the free list. */
1372 context = cp_parser_context_free_list;
1373 cp_parser_context_free_list = context->next;
1374 memset (context, 0, sizeof (*context));
1377 context = GGC_CNEW (cp_parser_context);
1379 /* No errors have occurred yet in this context. */
1380 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381 /* If this is not the bottommost context, copy information that we
1382 need from the previous context. */
1385 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386 expression, then we are parsing one in this context, too. */
1387 context->object_type = next->object_type;
1388 /* Thread the stack. */
1389 context->next = next;
1395 /* The cp_parser structure represents the C++ parser. */
1397 typedef struct GTY(()) cp_parser {
1398 /* The lexer from which we are obtaining tokens. */
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600 (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602 (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1605 static tree cp_parser_new_expression
1607 static VEC(tree,gc) *cp_parser_new_placement
1609 static tree cp_parser_new_type_id
1610 (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1613 static cp_declarator *cp_parser_direct_new_declarator
1615 static VEC(tree,gc) *cp_parser_new_initializer
1617 static tree cp_parser_delete_expression
1619 static tree cp_parser_cast_expression
1620 (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1629 static tree cp_parser_expression
1630 (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632 (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1635 static tree cp_parser_lambda_expression
1637 static void cp_parser_lambda_introducer
1638 (cp_parser *, tree);
1639 static void cp_parser_lambda_declarator_opt
1640 (cp_parser *, tree);
1641 static void cp_parser_lambda_body
1642 (cp_parser *, tree);
1644 /* Statements [gram.stmt.stmt] */
1646 static void cp_parser_statement
1647 (cp_parser *, tree, bool, bool *);
1648 static void cp_parser_label_for_labeled_statement
1650 static tree cp_parser_expression_statement
1651 (cp_parser *, tree);
1652 static tree cp_parser_compound_statement
1653 (cp_parser *, tree, bool);
1654 static void cp_parser_statement_seq_opt
1655 (cp_parser *, tree);
1656 static tree cp_parser_selection_statement
1657 (cp_parser *, bool *);
1658 static tree cp_parser_condition
1660 static tree cp_parser_iteration_statement
1662 static void cp_parser_for_init_statement
1664 static tree cp_parser_jump_statement
1666 static void cp_parser_declaration_statement
1669 static tree cp_parser_implicitly_scoped_statement
1670 (cp_parser *, bool *);
1671 static void cp_parser_already_scoped_statement
1674 /* Declarations [gram.dcl.dcl] */
1676 static void cp_parser_declaration_seq_opt
1678 static void cp_parser_declaration
1680 static void cp_parser_block_declaration
1681 (cp_parser *, bool);
1682 static void cp_parser_simple_declaration
1683 (cp_parser *, bool);
1684 static void cp_parser_decl_specifier_seq
1685 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1686 static tree cp_parser_storage_class_specifier_opt
1688 static tree cp_parser_function_specifier_opt
1689 (cp_parser *, cp_decl_specifier_seq *);
1690 static tree cp_parser_type_specifier
1691 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693 static tree cp_parser_simple_type_specifier
1694 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1695 static tree cp_parser_type_name
1697 static tree cp_parser_nonclass_name
1698 (cp_parser* parser);
1699 static tree cp_parser_elaborated_type_specifier
1700 (cp_parser *, bool, bool);
1701 static tree cp_parser_enum_specifier
1703 static void cp_parser_enumerator_list
1704 (cp_parser *, tree);
1705 static void cp_parser_enumerator_definition
1706 (cp_parser *, tree);
1707 static tree cp_parser_namespace_name
1709 static void cp_parser_namespace_definition
1711 static void cp_parser_namespace_body
1713 static tree cp_parser_qualified_namespace_specifier
1715 static void cp_parser_namespace_alias_definition
1717 static bool cp_parser_using_declaration
1718 (cp_parser *, bool);
1719 static void cp_parser_using_directive
1721 static void cp_parser_asm_definition
1723 static void cp_parser_linkage_specification
1725 static void cp_parser_static_assert
1726 (cp_parser *, bool);
1727 static tree cp_parser_decltype
1730 /* Declarators [gram.dcl.decl] */
1732 static tree cp_parser_init_declarator
1733 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1734 static cp_declarator *cp_parser_declarator
1735 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1736 static cp_declarator *cp_parser_direct_declarator
1737 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1738 static enum tree_code cp_parser_ptr_operator
1739 (cp_parser *, tree *, cp_cv_quals *);
1740 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742 static tree cp_parser_late_return_type_opt
1744 static tree cp_parser_declarator_id
1745 (cp_parser *, bool);
1746 static tree cp_parser_type_id
1748 static tree cp_parser_template_type_arg
1750 static tree cp_parser_trailing_type_id (cp_parser *);
1751 static tree cp_parser_type_id_1
1752 (cp_parser *, bool, bool);
1753 static void cp_parser_type_specifier_seq
1754 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1755 static tree cp_parser_parameter_declaration_clause
1757 static tree cp_parser_parameter_declaration_list
1758 (cp_parser *, bool *);
1759 static cp_parameter_declarator *cp_parser_parameter_declaration
1760 (cp_parser *, bool, bool *);
1761 static tree cp_parser_default_argument
1762 (cp_parser *, bool);
1763 static void cp_parser_function_body
1765 static tree cp_parser_initializer
1766 (cp_parser *, bool *, bool *);
1767 static tree cp_parser_initializer_clause
1768 (cp_parser *, bool *);
1769 static tree cp_parser_braced_list
1770 (cp_parser*, bool*);
1771 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1772 (cp_parser *, bool *);
1774 static bool cp_parser_ctor_initializer_opt_and_function_body
1777 /* Classes [gram.class] */
1779 static tree cp_parser_class_name
1780 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1781 static tree cp_parser_class_specifier
1783 static tree cp_parser_class_head
1784 (cp_parser *, bool *, tree *, tree *);
1785 static enum tag_types cp_parser_class_key
1787 static void cp_parser_member_specification_opt
1789 static void cp_parser_member_declaration
1791 static tree cp_parser_pure_specifier
1793 static tree cp_parser_constant_initializer
1796 /* Derived classes [gram.class.derived] */
1798 static tree cp_parser_base_clause
1800 static tree cp_parser_base_specifier
1803 /* Special member functions [gram.special] */
1805 static tree cp_parser_conversion_function_id
1807 static tree cp_parser_conversion_type_id
1809 static cp_declarator *cp_parser_conversion_declarator_opt
1811 static bool cp_parser_ctor_initializer_opt
1813 static void cp_parser_mem_initializer_list
1815 static tree cp_parser_mem_initializer
1817 static tree cp_parser_mem_initializer_id
1820 /* Overloading [gram.over] */
1822 static tree cp_parser_operator_function_id
1824 static tree cp_parser_operator
1827 /* Templates [gram.temp] */
1829 static void cp_parser_template_declaration
1830 (cp_parser *, bool);
1831 static tree cp_parser_template_parameter_list
1833 static tree cp_parser_template_parameter
1834 (cp_parser *, bool *, bool *);
1835 static tree cp_parser_type_parameter
1836 (cp_parser *, bool *);
1837 static tree cp_parser_template_id
1838 (cp_parser *, bool, bool, bool);
1839 static tree cp_parser_template_name
1840 (cp_parser *, bool, bool, bool, bool *);
1841 static tree cp_parser_template_argument_list
1843 static tree cp_parser_template_argument
1845 static void cp_parser_explicit_instantiation
1847 static void cp_parser_explicit_specialization
1850 /* Exception handling [gram.exception] */
1852 static tree cp_parser_try_block
1854 static bool cp_parser_function_try_block
1856 static void cp_parser_handler_seq
1858 static void cp_parser_handler
1860 static tree cp_parser_exception_declaration
1862 static tree cp_parser_throw_expression
1864 static tree cp_parser_exception_specification_opt
1866 static tree cp_parser_type_id_list
1869 /* GNU Extensions */
1871 static tree cp_parser_asm_specification_opt
1873 static tree cp_parser_asm_operand_list
1875 static tree cp_parser_asm_clobber_list
1877 static tree cp_parser_asm_label_list
1879 static tree cp_parser_attributes_opt
1881 static tree cp_parser_attribute_list
1883 static bool cp_parser_extension_opt
1884 (cp_parser *, int *);
1885 static void cp_parser_label_declaration
1888 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1889 static bool cp_parser_pragma
1890 (cp_parser *, enum pragma_context);
1892 /* Objective-C++ Productions */
1894 static tree cp_parser_objc_message_receiver
1896 static tree cp_parser_objc_message_args
1898 static tree cp_parser_objc_message_expression
1900 static tree cp_parser_objc_encode_expression
1902 static tree cp_parser_objc_defs_expression
1904 static tree cp_parser_objc_protocol_expression
1906 static tree cp_parser_objc_selector_expression
1908 static tree cp_parser_objc_expression
1910 static bool cp_parser_objc_selector_p
1912 static tree cp_parser_objc_selector
1914 static tree cp_parser_objc_protocol_refs_opt
1916 static void cp_parser_objc_declaration
1918 static tree cp_parser_objc_statement
1921 /* Utility Routines */
1923 static tree cp_parser_lookup_name
1924 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1925 static tree cp_parser_lookup_name_simple
1926 (cp_parser *, tree, location_t);
1927 static tree cp_parser_maybe_treat_template_as_class
1929 static bool cp_parser_check_declarator_template_parameters
1930 (cp_parser *, cp_declarator *, location_t);
1931 static bool cp_parser_check_template_parameters
1932 (cp_parser *, unsigned, location_t, cp_declarator *);
1933 static tree cp_parser_simple_cast_expression
1935 static tree cp_parser_global_scope_opt
1936 (cp_parser *, bool);
1937 static bool cp_parser_constructor_declarator_p
1938 (cp_parser *, bool);
1939 static tree cp_parser_function_definition_from_specifiers_and_declarator
1940 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1941 static tree cp_parser_function_definition_after_declarator
1942 (cp_parser *, bool);
1943 static void cp_parser_template_declaration_after_export
1944 (cp_parser *, bool);
1945 static void cp_parser_perform_template_parameter_access_checks
1946 (VEC (deferred_access_check,gc)*);
1947 static tree cp_parser_single_declaration
1948 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1949 static tree cp_parser_functional_cast
1950 (cp_parser *, tree);
1951 static tree cp_parser_save_member_function_body
1952 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1953 static tree cp_parser_enclosed_template_argument_list
1955 static void cp_parser_save_default_args
1956 (cp_parser *, tree);
1957 static void cp_parser_late_parsing_for_member
1958 (cp_parser *, tree);
1959 static void cp_parser_late_parsing_default_args
1960 (cp_parser *, tree);
1961 static tree cp_parser_sizeof_operand
1962 (cp_parser *, enum rid);
1963 static tree cp_parser_trait_expr
1964 (cp_parser *, enum rid);
1965 static bool cp_parser_declares_only_class_p
1967 static void cp_parser_set_storage_class
1968 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1969 static void cp_parser_set_decl_spec_type
1970 (cp_decl_specifier_seq *, tree, location_t, bool);
1971 static bool cp_parser_friend_p
1972 (const cp_decl_specifier_seq *);
1973 static cp_token *cp_parser_require
1974 (cp_parser *, enum cpp_ttype, const char *);
1975 static cp_token *cp_parser_require_keyword
1976 (cp_parser *, enum rid, const char *);
1977 static bool cp_parser_token_starts_function_definition_p
1979 static bool cp_parser_next_token_starts_class_definition_p
1981 static bool cp_parser_next_token_ends_template_argument_p
1983 static bool cp_parser_nth_token_starts_template_argument_list_p
1984 (cp_parser *, size_t);
1985 static enum tag_types cp_parser_token_is_class_key
1987 static void cp_parser_check_class_key
1988 (enum tag_types, tree type);
1989 static void cp_parser_check_access_in_redeclaration
1990 (tree type, location_t location);
1991 static bool cp_parser_optional_template_keyword
1993 static void cp_parser_pre_parsed_nested_name_specifier
1995 static bool cp_parser_cache_group
1996 (cp_parser *, enum cpp_ttype, unsigned);
1997 static void cp_parser_parse_tentatively
1999 static void cp_parser_commit_to_tentative_parse
2001 static void cp_parser_abort_tentative_parse
2003 static bool cp_parser_parse_definitely
2005 static inline bool cp_parser_parsing_tentatively
2007 static bool cp_parser_uncommitted_to_tentative_parse_p
2009 static void cp_parser_error
2010 (cp_parser *, const char *);
2011 static void cp_parser_name_lookup_error
2012 (cp_parser *, tree, tree, const char *, location_t);
2013 static bool cp_parser_simulate_error
2015 static bool cp_parser_check_type_definition
2017 static void cp_parser_check_for_definition_in_return_type
2018 (cp_declarator *, tree, location_t type_location);
2019 static void cp_parser_check_for_invalid_template_id
2020 (cp_parser *, tree, location_t location);
2021 static bool cp_parser_non_integral_constant_expression
2022 (cp_parser *, const char *);
2023 static void cp_parser_diagnose_invalid_type_name
2024 (cp_parser *, tree, tree, location_t);
2025 static bool cp_parser_parse_and_diagnose_invalid_type_name
2027 static int cp_parser_skip_to_closing_parenthesis
2028 (cp_parser *, bool, bool, bool);
2029 static void cp_parser_skip_to_end_of_statement
2031 static void cp_parser_consume_semicolon_at_end_of_statement
2033 static void cp_parser_skip_to_end_of_block_or_statement
2035 static bool cp_parser_skip_to_closing_brace
2037 static void cp_parser_skip_to_end_of_template_parameter_list
2039 static void cp_parser_skip_to_pragma_eol
2040 (cp_parser*, cp_token *);
2041 static bool cp_parser_error_occurred
2043 static bool cp_parser_allow_gnu_extensions_p
2045 static bool cp_parser_is_string_literal
2047 static bool cp_parser_is_keyword
2048 (cp_token *, enum rid);
2049 static tree cp_parser_make_typename_type
2050 (cp_parser *, tree, tree, location_t location);
2051 static cp_declarator * cp_parser_make_indirect_declarator
2052 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054 /* Returns nonzero if we are parsing tentatively. */
2057 cp_parser_parsing_tentatively (cp_parser* parser)
2059 return parser->context->next != NULL;
2062 /* Returns nonzero if TOKEN is a string literal. */
2065 cp_parser_is_string_literal (cp_token* token)
2067 return (token->type == CPP_STRING ||
2068 token->type == CPP_STRING16 ||
2069 token->type == CPP_STRING32 ||
2070 token->type == CPP_WSTRING ||
2071 token->type == CPP_UTF8STRING);
2074 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2077 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079 return token->keyword == keyword;
2082 /* If not parsing tentatively, issue a diagnostic of the form
2083 FILE:LINE: MESSAGE before TOKEN
2084 where TOKEN is the next token in the input stream. MESSAGE
2085 (specified by the caller) is usually of the form "expected
2089 cp_parser_error (cp_parser* parser, const char* message)
2091 if (!cp_parser_simulate_error (parser))
2093 cp_token *token = cp_lexer_peek_token (parser->lexer);
2094 /* This diagnostic makes more sense if it is tagged to the line
2095 of the token we just peeked at. */
2096 cp_lexer_set_source_position_from_token (token);
2098 if (token->type == CPP_PRAGMA)
2100 error_at (token->location,
2101 "%<#pragma%> is not allowed here");
2102 cp_parser_skip_to_pragma_eol (parser, token);
2106 c_parse_error (message,
2107 /* Because c_parser_error does not understand
2108 CPP_KEYWORD, keywords are treated like
2110 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2111 token->u.value, token->flags);
2115 /* Issue an error about name-lookup failing. NAME is the
2116 IDENTIFIER_NODE DECL is the result of
2117 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2118 the thing that we hoped to find. */
2121 cp_parser_name_lookup_error (cp_parser* parser,
2124 const char* desired,
2125 location_t location)
2127 /* If name lookup completely failed, tell the user that NAME was not
2129 if (decl == error_mark_node)
2131 if (parser->scope && parser->scope != global_namespace)
2132 error_at (location, "%<%E::%E%> has not been declared",
2133 parser->scope, name);
2134 else if (parser->scope == global_namespace)
2135 error_at (location, "%<::%E%> has not been declared", name);
2136 else if (parser->object_scope
2137 && !CLASS_TYPE_P (parser->object_scope))
2138 error_at (location, "request for member %qE in non-class type %qT",
2139 name, parser->object_scope);
2140 else if (parser->object_scope)
2141 error_at (location, "%<%T::%E%> has not been declared",
2142 parser->object_scope, name);
2144 error_at (location, "%qE has not been declared", name);
2146 else if (parser->scope && parser->scope != global_namespace)
2147 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2148 else if (parser->scope == global_namespace)
2149 error_at (location, "%<::%E%> %s", name, desired);
2151 error_at (location, "%qE %s", name, desired);
2154 /* If we are parsing tentatively, remember that an error has occurred
2155 during this tentative parse. Returns true if the error was
2156 simulated; false if a message should be issued by the caller. */
2159 cp_parser_simulate_error (cp_parser* parser)
2161 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2169 /* Check for repeated decl-specifiers. */
2172 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2173 location_t location)
2177 for (ds = ds_first; ds != ds_last; ++ds)
2179 unsigned count = decl_specs->specs[ds];
2182 /* The "long" specifier is a special case because of "long long". */
2186 error_at (location, "%<long long long%> is too long for GCC");
2188 pedwarn_cxx98 (location, OPT_Wlong_long,
2189 "ISO C++ 1998 does not support %<long long%>");
2193 static const char *const decl_spec_names[] = {
2210 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2215 /* This function is called when a type is defined. If type
2216 definitions are forbidden at this point, an error message is
2220 cp_parser_check_type_definition (cp_parser* parser)
2222 /* If types are forbidden here, issue a message. */
2223 if (parser->type_definition_forbidden_message)
2225 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2226 in the message need to be interpreted. */
2227 error (parser->type_definition_forbidden_message);
2233 /* This function is called when the DECLARATOR is processed. The TYPE
2234 was a type defined in the decl-specifiers. If it is invalid to
2235 define a type in the decl-specifiers for DECLARATOR, an error is
2236 issued. TYPE_LOCATION is the location of TYPE and is used
2237 for error reporting. */
2240 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2241 tree type, location_t type_location)
2243 /* [dcl.fct] forbids type definitions in return types.
2244 Unfortunately, it's not easy to know whether or not we are
2245 processing a return type until after the fact. */
2247 && (declarator->kind == cdk_pointer
2248 || declarator->kind == cdk_reference
2249 || declarator->kind == cdk_ptrmem))
2250 declarator = declarator->declarator;
2252 && declarator->kind == cdk_function)
2254 error_at (type_location,
2255 "new types may not be defined in a return type");
2256 inform (type_location,
2257 "(perhaps a semicolon is missing after the definition of %qT)",
2262 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2263 "<" in any valid C++ program. If the next token is indeed "<",
2264 issue a message warning the user about what appears to be an
2265 invalid attempt to form a template-id. LOCATION is the location
2266 of the type-specifier (TYPE) */
2269 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2270 tree type, location_t location)
2272 cp_token_position start = 0;
2274 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2277 error_at (location, "%qT is not a template", type);
2278 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2279 error_at (location, "%qE is not a template", type);
2281 error_at (location, "invalid template-id");
2282 /* Remember the location of the invalid "<". */
2283 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2284 start = cp_lexer_token_position (parser->lexer, true);
2285 /* Consume the "<". */
2286 cp_lexer_consume_token (parser->lexer);
2287 /* Parse the template arguments. */
2288 cp_parser_enclosed_template_argument_list (parser);
2289 /* Permanently remove the invalid template arguments so that
2290 this error message is not issued again. */
2292 cp_lexer_purge_tokens_after (parser->lexer, start);
2296 /* If parsing an integral constant-expression, issue an error message
2297 about the fact that THING appeared and return true. Otherwise,
2298 return false. In either case, set
2299 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2302 cp_parser_non_integral_constant_expression (cp_parser *parser,
2305 parser->non_integral_constant_expression_p = true;
2306 if (parser->integral_constant_expression_p)
2308 if (!parser->allow_non_integral_constant_expression_p)
2310 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2311 in the message need to be interpreted. */
2312 char *message = concat (thing,
2313 " cannot appear in a constant-expression",
2323 /* Emit a diagnostic for an invalid type name. SCOPE is the
2324 qualifying scope (or NULL, if none) for ID. This function commits
2325 to the current active tentative parse, if any. (Otherwise, the
2326 problematic construct might be encountered again later, resulting
2327 in duplicate error messages.) LOCATION is the location of ID. */
2330 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2331 tree scope, tree id,
2332 location_t location)
2334 tree decl, old_scope;
2335 /* Try to lookup the identifier. */
2336 old_scope = parser->scope;
2337 parser->scope = scope;
2338 decl = cp_parser_lookup_name_simple (parser, id, location);
2339 parser->scope = old_scope;
2340 /* If the lookup found a template-name, it means that the user forgot
2341 to specify an argument list. Emit a useful error message. */
2342 if (TREE_CODE (decl) == TEMPLATE_DECL)
2344 "invalid use of template-name %qE without an argument list",
2346 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2347 error_at (location, "invalid use of destructor %qD as a type", id);
2348 else if (TREE_CODE (decl) == TYPE_DECL)
2349 /* Something like 'unsigned A a;' */
2350 error_at (location, "invalid combination of multiple type-specifiers");
2351 else if (!parser->scope)
2353 /* Issue an error message. */
2354 error_at (location, "%qE does not name a type", id);
2355 /* If we're in a template class, it's possible that the user was
2356 referring to a type from a base class. For example:
2358 template <typename T> struct A { typedef T X; };
2359 template <typename T> struct B : public A<T> { X x; };
2361 The user should have said "typename A<T>::X". */
2362 if (processing_template_decl && current_class_type
2363 && TYPE_BINFO (current_class_type))
2367 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2371 tree base_type = BINFO_TYPE (b);
2372 if (CLASS_TYPE_P (base_type)
2373 && dependent_type_p (base_type))
2376 /* Go from a particular instantiation of the
2377 template (which will have an empty TYPE_FIELDs),
2378 to the main version. */
2379 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2380 for (field = TYPE_FIELDS (base_type);
2382 field = TREE_CHAIN (field))
2383 if (TREE_CODE (field) == TYPE_DECL
2384 && DECL_NAME (field) == id)
2387 "(perhaps %<typename %T::%E%> was intended)",
2388 BINFO_TYPE (b), id);
2397 /* Here we diagnose qualified-ids where the scope is actually correct,
2398 but the identifier does not resolve to a valid type name. */
2399 else if (parser->scope != error_mark_node)
2401 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2402 error_at (location, "%qE in namespace %qE does not name a type",
2404 else if (CLASS_TYPE_P (parser->scope)
2405 && constructor_name_p (id, parser->scope))
2408 error_at (location, "%<%T::%E%> names the constructor, not"
2409 " the type", parser->scope, id);
2410 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2411 error_at (location, "and %qT has no template constructors",
2414 else if (TYPE_P (parser->scope)
2415 && dependent_scope_p (parser->scope))
2416 error_at (location, "need %<typename%> before %<%T::%E%> because "
2417 "%qT is a dependent scope",
2418 parser->scope, id, parser->scope);
2419 else if (TYPE_P (parser->scope))
2420 error_at (location, "%qE in class %qT does not name a type",
2425 cp_parser_commit_to_tentative_parse (parser);
2428 /* Check for a common situation where a type-name should be present,
2429 but is not, and issue a sensible error message. Returns true if an
2430 invalid type-name was detected.
2432 The situation handled by this function are variable declarations of the
2433 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2434 Usually, `ID' should name a type, but if we got here it means that it
2435 does not. We try to emit the best possible error message depending on
2436 how exactly the id-expression looks like. */
2439 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2442 cp_token *token = cp_lexer_peek_token (parser->lexer);
2444 /* Avoid duplicate error about ambiguous lookup. */
2445 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2448 if (next->type == CPP_NAME && next->ambiguous_p)
2452 cp_parser_parse_tentatively (parser);
2453 id = cp_parser_id_expression (parser,
2454 /*template_keyword_p=*/false,
2455 /*check_dependency_p=*/true,
2456 /*template_p=*/NULL,
2457 /*declarator_p=*/true,
2458 /*optional_p=*/false);
2459 /* If the next token is a (, this is a function with no explicit return
2460 type, i.e. constructor, destructor or conversion op. */
2461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2462 || TREE_CODE (id) == TYPE_DECL)
2464 cp_parser_abort_tentative_parse (parser);
2467 if (!cp_parser_parse_definitely (parser))
2470 /* Emit a diagnostic for the invalid type. */
2471 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2472 id, token->location);
2474 /* If we aren't in the middle of a declarator (i.e. in a
2475 parameter-declaration-clause), skip to the end of the declaration;
2476 there's no point in trying to process it. */
2477 if (!parser->in_declarator_p)
2478 cp_parser_skip_to_end_of_block_or_statement (parser);
2482 /* Consume tokens up to, and including, the next non-nested closing `)'.
2483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2484 are doing error recovery. Returns -1 if OR_COMMA is true and we
2485 found an unnested comma. */
2488 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2493 unsigned paren_depth = 0;
2494 unsigned brace_depth = 0;
2495 unsigned square_depth = 0;
2497 if (recovering && !or_comma
2498 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2503 cp_token * token = cp_lexer_peek_token (parser->lexer);
2505 switch (token->type)
2508 case CPP_PRAGMA_EOL:
2509 /* If we've run out of tokens, then there is no closing `)'. */
2512 /* This is good for lambda expression capture-lists. */
2513 case CPP_OPEN_SQUARE:
2516 case CPP_CLOSE_SQUARE:
2517 if (!square_depth--)
2522 /* This matches the processing in skip_to_end_of_statement. */
2527 case CPP_OPEN_BRACE:
2530 case CPP_CLOSE_BRACE:
2536 if (recovering && or_comma && !brace_depth && !paren_depth
2541 case CPP_OPEN_PAREN:
2546 case CPP_CLOSE_PAREN:
2547 if (!brace_depth && !paren_depth--)
2550 cp_lexer_consume_token (parser->lexer);
2559 /* Consume the token. */
2560 cp_lexer_consume_token (parser->lexer);
2564 /* Consume tokens until we reach the end of the current statement.
2565 Normally, that will be just before consuming a `;'. However, if a
2566 non-nested `}' comes first, then we stop before consuming that. */
2569 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571 unsigned nesting_depth = 0;
2575 cp_token *token = cp_lexer_peek_token (parser->lexer);
2577 switch (token->type)
2580 case CPP_PRAGMA_EOL:
2581 /* If we've run out of tokens, stop. */
2585 /* If the next token is a `;', we have reached the end of the
2591 case CPP_CLOSE_BRACE:
2592 /* If this is a non-nested '}', stop before consuming it.
2593 That way, when confronted with something like:
2597 we stop before consuming the closing '}', even though we
2598 have not yet reached a `;'. */
2599 if (nesting_depth == 0)
2602 /* If it is the closing '}' for a block that we have
2603 scanned, stop -- but only after consuming the token.
2609 we will stop after the body of the erroneously declared
2610 function, but before consuming the following `typedef'
2612 if (--nesting_depth == 0)
2614 cp_lexer_consume_token (parser->lexer);
2618 case CPP_OPEN_BRACE:
2626 /* Consume the token. */
2627 cp_lexer_consume_token (parser->lexer);
2631 /* This function is called at the end of a statement or declaration.
2632 If the next token is a semicolon, it is consumed; otherwise, error
2633 recovery is attempted. */
2636 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638 /* Look for the trailing `;'. */
2639 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641 /* If there is additional (erroneous) input, skip to the end of
2643 cp_parser_skip_to_end_of_statement (parser);
2644 /* If the next token is now a `;', consume it. */
2645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2646 cp_lexer_consume_token (parser->lexer);
2650 /* Skip tokens until we have consumed an entire block, or until we
2651 have consumed a non-nested `;'. */
2654 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656 int nesting_depth = 0;
2658 while (nesting_depth >= 0)
2660 cp_token *token = cp_lexer_peek_token (parser->lexer);
2662 switch (token->type)
2665 case CPP_PRAGMA_EOL:
2666 /* If we've run out of tokens, stop. */
2670 /* Stop if this is an unnested ';'. */
2675 case CPP_CLOSE_BRACE:
2676 /* Stop if this is an unnested '}', or closes the outermost
2679 if (nesting_depth < 0)
2685 case CPP_OPEN_BRACE:
2694 /* Consume the token. */
2695 cp_lexer_consume_token (parser->lexer);
2699 /* Skip tokens until a non-nested closing curly brace is the next
2700 token, or there are no more tokens. Return true in the first case,
2704 cp_parser_skip_to_closing_brace (cp_parser *parser)
2706 unsigned nesting_depth = 0;
2710 cp_token *token = cp_lexer_peek_token (parser->lexer);
2712 switch (token->type)
2715 case CPP_PRAGMA_EOL:
2716 /* If we've run out of tokens, stop. */
2719 case CPP_CLOSE_BRACE:
2720 /* If the next token is a non-nested `}', then we have reached
2721 the end of the current block. */
2722 if (nesting_depth-- == 0)
2726 case CPP_OPEN_BRACE:
2727 /* If it the next token is a `{', then we are entering a new
2728 block. Consume the entire block. */
2736 /* Consume the token. */
2737 cp_lexer_consume_token (parser->lexer);
2741 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2742 parameter is the PRAGMA token, allowing us to purge the entire pragma
2746 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2750 parser->lexer->in_pragma = false;
2753 token = cp_lexer_consume_token (parser->lexer);
2754 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756 /* Ensure that the pragma is not parsed again. */
2757 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2760 /* Require pragma end of line, resyncing with it as necessary. The
2761 arguments are as for cp_parser_skip_to_pragma_eol. */
2764 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766 parser->lexer->in_pragma = false;
2767 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2771 /* This is a simple wrapper around make_typename_type. When the id is
2772 an unresolved identifier node, we can provide a superior diagnostic
2773 using cp_parser_diagnose_invalid_type_name. */
2776 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2777 tree id, location_t id_location)
2780 if (TREE_CODE (id) == IDENTIFIER_NODE)
2782 result = make_typename_type (scope, id, typename_type,
2783 /*complain=*/tf_none);
2784 if (result == error_mark_node)
2785 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2788 return make_typename_type (scope, id, typename_type, tf_error);
2791 /* This is a wrapper around the
2792 make_{pointer,ptrmem,reference}_declarator functions that decides
2793 which one to call based on the CODE and CLASS_TYPE arguments. The
2794 CODE argument should be one of the values returned by
2795 cp_parser_ptr_operator. */
2796 static cp_declarator *
2797 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2798 cp_cv_quals cv_qualifiers,
2799 cp_declarator *target)
2801 if (code == ERROR_MARK)
2802 return cp_error_declarator;
2804 if (code == INDIRECT_REF)
2805 if (class_type == NULL_TREE)
2806 return make_pointer_declarator (cv_qualifiers, target);
2808 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2809 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2810 return make_reference_declarator (cv_qualifiers, target, false);
2811 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2812 return make_reference_declarator (cv_qualifiers, target, true);
2816 /* Create a new C++ parser. */
2819 cp_parser_new (void)
2825 /* cp_lexer_new_main is called before calling ggc_alloc because
2826 cp_lexer_new_main might load a PCH file. */
2827 lexer = cp_lexer_new_main ();
2829 /* Initialize the binops_by_token so that we can get the tree
2830 directly from the token. */
2831 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2832 binops_by_token[binops[i].token_type] = binops[i];
2834 parser = GGC_CNEW (cp_parser);
2835 parser->lexer = lexer;
2836 parser->context = cp_parser_context_new (NULL);
2838 /* For now, we always accept GNU extensions. */
2839 parser->allow_gnu_extensions_p = 1;
2841 /* The `>' token is a greater-than operator, not the end of a
2843 parser->greater_than_is_operator_p = true;
2845 parser->default_arg_ok_p = true;
2847 /* We are not parsing a constant-expression. */
2848 parser->integral_constant_expression_p = false;
2849 parser->allow_non_integral_constant_expression_p = false;
2850 parser->non_integral_constant_expression_p = false;
2852 /* Local variable names are not forbidden. */
2853 parser->local_variables_forbidden_p = false;
2855 /* We are not processing an `extern "C"' declaration. */
2856 parser->in_unbraced_linkage_specification_p = false;
2858 /* We are not processing a declarator. */
2859 parser->in_declarator_p = false;
2861 /* We are not processing a template-argument-list. */
2862 parser->in_template_argument_list_p = false;
2864 /* We are not in an iteration statement. */
2865 parser->in_statement = 0;
2867 /* We are not in a switch statement. */
2868 parser->in_switch_statement_p = false;
2870 /* We are not parsing a type-id inside an expression. */
2871 parser->in_type_id_in_expr_p = false;
2873 /* Declarations aren't implicitly extern "C". */
2874 parser->implicit_extern_c = false;
2876 /* String literals should be translated to the execution character set. */
2877 parser->translate_strings_p = true;
2879 /* We are not parsing a function body. */
2880 parser->in_function_body = false;
2882 /* The unparsed function queue is empty. */
2883 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885 /* There are no classes being defined. */
2886 parser->num_classes_being_defined = 0;
2888 /* No template parameters apply. */
2889 parser->num_template_parameter_lists = 0;
2894 /* Create a cp_lexer structure which will emit the tokens in CACHE
2895 and push it onto the parser's lexer stack. This is used for delayed
2896 parsing of in-class method bodies and default arguments, and should
2897 not be confused with tentative parsing. */
2899 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2902 lexer->next = parser->lexer;
2903 parser->lexer = lexer;
2905 /* Move the current source position to that of the first token in the
2907 cp_lexer_set_source_position_from_token (lexer->next_token);
2910 /* Pop the top lexer off the parser stack. This is never used for the
2911 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2913 cp_parser_pop_lexer (cp_parser *parser)
2915 cp_lexer *lexer = parser->lexer;
2916 parser->lexer = lexer->next;
2917 cp_lexer_destroy (lexer);
2919 /* Put the current source position back where it was before this
2920 lexer was pushed. */
2921 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2924 /* Lexical conventions [gram.lex] */
2926 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2930 cp_parser_identifier (cp_parser* parser)
2934 /* Look for the identifier. */
2935 token = cp_parser_require (parser, CPP_NAME, "identifier");
2936 /* Return the value. */
2937 return token ? token->u.value : error_mark_node;
2940 /* Parse a sequence of adjacent string constants. Returns a
2941 TREE_STRING representing the combined, nul-terminated string
2942 constant. If TRANSLATE is true, translate the string to the
2943 execution character set. If WIDE_OK is true, a wide string is
2946 C++98 [lex.string] says that if a narrow string literal token is
2947 adjacent to a wide string literal token, the behavior is undefined.
2948 However, C99 6.4.5p4 says that this results in a wide string literal.
2949 We follow C99 here, for consistency with the C front end.
2951 This code is largely lifted from lex_string() in c-lex.c.
2953 FUTURE: ObjC++ will need to handle @-strings here. */
2955 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2959 struct obstack str_ob;
2960 cpp_string str, istr, *strs;
2962 enum cpp_ttype type;
2964 tok = cp_lexer_peek_token (parser->lexer);
2965 if (!cp_parser_is_string_literal (tok))
2967 cp_parser_error (parser, "expected string-literal");
2968 return error_mark_node;
2973 /* Try to avoid the overhead of creating and destroying an obstack
2974 for the common case of just one string. */
2975 if (!cp_parser_is_string_literal
2976 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978 cp_lexer_consume_token (parser->lexer);
2980 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2981 str.len = TREE_STRING_LENGTH (tok->u.value);
2988 gcc_obstack_init (&str_ob);
2993 cp_lexer_consume_token (parser->lexer);
2995 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2996 str.len = TREE_STRING_LENGTH (tok->u.value);
2998 if (type != tok->type)
3000 if (type == CPP_STRING)
3002 else if (tok->type != CPP_STRING)
3003 error_at (tok->location,
3004 "unsupported non-standard concatenation "
3005 "of string literals");
3008 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010 tok = cp_lexer_peek_token (parser->lexer);
3012 while (cp_parser_is_string_literal (tok));
3014 strs = (cpp_string *) obstack_finish (&str_ob);
3017 if (type != CPP_STRING && !wide_ok)
3019 cp_parser_error (parser, "a wide string is invalid in this context");
3023 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3024 (parse_in, strs, count, &istr, type))
3026 value = build_string (istr.len, (const char *)istr.text);
3027 free (CONST_CAST (unsigned char *, istr.text));
3033 case CPP_UTF8STRING:
3034 TREE_TYPE (value) = char_array_type_node;
3037 TREE_TYPE (value) = char16_array_type_node;
3040 TREE_TYPE (value) = char32_array_type_node;
3043 TREE_TYPE (value) = wchar_array_type_node;
3047 value = fix_string_type (value);
3050 /* cpp_interpret_string has issued an error. */
3051 value = error_mark_node;
3054 obstack_free (&str_ob, 0);
3060 /* Basic concepts [gram.basic] */
3062 /* Parse a translation-unit.
3065 declaration-seq [opt]
3067 Returns TRUE if all went well. */
3070 cp_parser_translation_unit (cp_parser* parser)
3072 /* The address of the first non-permanent object on the declarator
3074 static void *declarator_obstack_base;
3078 /* Create the declarator obstack, if necessary. */
3079 if (!cp_error_declarator)
3081 gcc_obstack_init (&declarator_obstack);
3082 /* Create the error declarator. */
3083 cp_error_declarator = make_declarator (cdk_error);
3084 /* Create the empty parameter list. */
3085 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3086 /* Remember where the base of the declarator obstack lies. */
3087 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3090 cp_parser_declaration_seq_opt (parser);
3092 /* If there are no tokens left then all went well. */
3093 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095 /* Get rid of the token array; we don't need it any more. */
3096 cp_lexer_destroy (parser->lexer);
3097 parser->lexer = NULL;
3099 /* This file might have been a context that's implicitly extern
3100 "C". If so, pop the lang context. (Only relevant for PCH.) */
3101 if (parser->implicit_extern_c)
3103 pop_lang_context ();
3104 parser->implicit_extern_c = false;
3108 finish_translation_unit ();
3114 cp_parser_error (parser, "expected declaration");
3118 /* Make sure the declarator obstack was fully cleaned up. */
3119 gcc_assert (obstack_next_free (&declarator_obstack)
3120 == declarator_obstack_base);
3122 /* All went well. */
3126 /* Expressions [gram.expr] */
3128 /* Parse a primary-expression.
3139 ( compound-statement )
3140 __builtin_va_arg ( assignment-expression , type-id )
3141 __builtin_offsetof ( type-id , offsetof-expression )
3144 __has_nothrow_assign ( type-id )
3145 __has_nothrow_constructor ( type-id )
3146 __has_nothrow_copy ( type-id )
3147 __has_trivial_assign ( type-id )
3148 __has_trivial_constructor ( type-id )
3149 __has_trivial_copy ( type-id )
3150 __has_trivial_destructor ( type-id )
3151 __has_virtual_destructor ( type-id )
3152 __is_abstract ( type-id )
3153 __is_base_of ( type-id , type-id )
3154 __is_class ( type-id )
3155 __is_convertible_to ( type-id , type-id )
3156 __is_empty ( type-id )
3157 __is_enum ( type-id )
3158 __is_pod ( type-id )
3159 __is_polymorphic ( type-id )
3160 __is_union ( type-id )
3162 Objective-C++ Extension:
3170 ADDRESS_P is true iff this expression was immediately preceded by
3171 "&" and therefore might denote a pointer-to-member. CAST_P is true
3172 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3173 true iff this expression is a template argument.
3175 Returns a representation of the expression. Upon return, *IDK
3176 indicates what kind of id-expression (if any) was present. */
3179 cp_parser_primary_expression (cp_parser *parser,
3182 bool template_arg_p,
3185 cp_token *token = NULL;
3187 /* Assume the primary expression is not an id-expression. */
3188 *idk = CP_ID_KIND_NONE;
3190 /* Peek at the next token. */
3191 token = cp_lexer_peek_token (parser->lexer);
3192 switch (token->type)
3205 token = cp_lexer_consume_token (parser->lexer);
3206 if (TREE_CODE (token->u.value) == FIXED_CST)
3208 error_at (token->location,
3209 "fixed-point types not supported in C++");
3210 return error_mark_node;
3212 /* Floating-point literals are only allowed in an integral
3213 constant expression if they are cast to an integral or
3214 enumeration type. */
3215 if (TREE_CODE (token->u.value) == REAL_CST
3216 && parser->integral_constant_expression_p
3219 /* CAST_P will be set even in invalid code like "int(2.7 +
3220 ...)". Therefore, we have to check that the next token
3221 is sure to end the cast. */
3224 cp_token *next_token;
3226 next_token = cp_lexer_peek_token (parser->lexer);
3227 if (/* The comma at the end of an
3228 enumerator-definition. */
3229 next_token->type != CPP_COMMA
3230 /* The curly brace at the end of an enum-specifier. */
3231 && next_token->type != CPP_CLOSE_BRACE
3232 /* The end of a statement. */
3233 && next_token->type != CPP_SEMICOLON
3234 /* The end of the cast-expression. */
3235 && next_token->type != CPP_CLOSE_PAREN
3236 /* The end of an array bound. */
3237 && next_token->type != CPP_CLOSE_SQUARE
3238 /* The closing ">" in a template-argument-list. */
3239 && (next_token->type != CPP_GREATER
3240 || parser->greater_than_is_operator_p)
3241 /* C++0x only: A ">>" treated like two ">" tokens,
3242 in a template-argument-list. */
3243 && (next_token->type != CPP_RSHIFT
3244 || (cxx_dialect == cxx98)
3245 || parser->greater_than_is_operator_p))
3249 /* If we are within a cast, then the constraint that the
3250 cast is to an integral or enumeration type will be
3251 checked at that point. If we are not within a cast, then
3252 this code is invalid. */
3254 cp_parser_non_integral_constant_expression
3255 (parser, "floating-point literal");
3257 return token->u.value;
3263 case CPP_UTF8STRING:
3264 /* ??? Should wide strings be allowed when parser->translate_strings_p
3265 is false (i.e. in attributes)? If not, we can kill the third
3266 argument to cp_parser_string_literal. */
3267 return cp_parser_string_literal (parser,
3268 parser->translate_strings_p,
3271 case CPP_OPEN_PAREN:
3274 bool saved_greater_than_is_operator_p;
3276 /* Consume the `('. */
3277 cp_lexer_consume_token (parser->lexer);
3278 /* Within a parenthesized expression, a `>' token is always
3279 the greater-than operator. */
3280 saved_greater_than_is_operator_p
3281 = parser->greater_than_is_operator_p;
3282 parser->greater_than_is_operator_p = true;
3283 /* If we see `( { ' then we are looking at the beginning of
3284 a GNU statement-expression. */
3285 if (cp_parser_allow_gnu_extensions_p (parser)
3286 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288 /* Statement-expressions are not allowed by the standard. */
3289 pedwarn (token->location, OPT_pedantic,
3290 "ISO C++ forbids braced-groups within expressions");
3292 /* And they're not allowed outside of a function-body; you
3293 cannot, for example, write:
3295 int i = ({ int j = 3; j + 1; });
3297 at class or namespace scope. */
3298 if (!parser->in_function_body
3299 || parser->in_template_argument_list_p)
3301 error_at (token->location,
3302 "statement-expressions are not allowed outside "
3303 "functions nor in template-argument lists");
3304 cp_parser_skip_to_end_of_block_or_statement (parser);
3305 expr = error_mark_node;
3309 /* Start the statement-expression. */
3310 expr = begin_stmt_expr ();
3311 /* Parse the compound-statement. */
3312 cp_parser_compound_statement (parser, expr, false);
3314 expr = finish_stmt_expr (expr, false);
3319 /* Parse the parenthesized expression. */
3320 expr = cp_parser_expression (parser, cast_p, idk);
3321 /* Let the front end know that this expression was
3322 enclosed in parentheses. This matters in case, for
3323 example, the expression is of the form `A::B', since
3324 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326 finish_parenthesized_expr (expr);
3328 /* The `>' token might be the end of a template-id or
3329 template-parameter-list now. */
3330 parser->greater_than_is_operator_p
3331 = saved_greater_than_is_operator_p;
3332 /* Consume the `)'. */
3333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3334 cp_parser_skip_to_end_of_statement (parser);
3339 case CPP_OPEN_SQUARE:
3340 if (c_dialect_objc ())
3341 /* We have an Objective-C++ message. */
3342 return cp_parser_objc_expression (parser);
3343 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3344 return cp_parser_lambda_expression (parser);
3346 case CPP_OBJC_STRING:
3347 if (c_dialect_objc ())
3348 /* We have an Objective-C++ string literal. */
3349 return cp_parser_objc_expression (parser);
3350 cp_parser_error (parser, "expected primary-expression");
3351 return error_mark_node;
3354 switch (token->keyword)
3356 /* These two are the boolean literals. */
3358 cp_lexer_consume_token (parser->lexer);
3359 return boolean_true_node;
3361 cp_lexer_consume_token (parser->lexer);
3362 return boolean_false_node;
3364 /* The `__null' literal. */
3366 cp_lexer_consume_token (parser->lexer);
3369 /* Recognize the `this' keyword. */
3371 cp_lexer_consume_token (parser->lexer);
3372 if (parser->local_variables_forbidden_p)
3374 error_at (token->location,
3375 "%<this%> may not be used in this context");
3376 return error_mark_node;
3378 /* Pointers cannot appear in constant-expressions. */
3379 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3380 return error_mark_node;
3381 return finish_this_expr ();
3383 /* The `operator' keyword can be the beginning of an
3388 case RID_FUNCTION_NAME:
3389 case RID_PRETTY_FUNCTION_NAME:
3390 case RID_C99_FUNCTION_NAME:
3394 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3395 __func__ are the names of variables -- but they are
3396 treated specially. Therefore, they are handled here,
3397 rather than relying on the generic id-expression logic
3398 below. Grammatically, these names are id-expressions.
3400 Consume the token. */
3401 token = cp_lexer_consume_token (parser->lexer);
3403 switch (token->keyword)
3405 case RID_FUNCTION_NAME:
3406 name = "%<__FUNCTION__%>";
3408 case RID_PRETTY_FUNCTION_NAME:
3409 name = "%<__PRETTY_FUNCTION__%>";
3411 case RID_C99_FUNCTION_NAME:
3412 name = "%<__func__%>";
3418 if (cp_parser_non_integral_constant_expression (parser, name))
3419 return error_mark_node;
3421 /* Look up the name. */
3422 return finish_fname (token->u.value);
3430 /* The `__builtin_va_arg' construct is used to handle
3431 `va_arg'. Consume the `__builtin_va_arg' token. */
3432 cp_lexer_consume_token (parser->lexer);
3433 /* Look for the opening `('. */
3434 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3435 /* Now, parse the assignment-expression. */
3436 expression = cp_parser_assignment_expression (parser,
3437 /*cast_p=*/false, NULL);
3438 /* Look for the `,'. */
3439 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3440 /* Parse the type-id. */
3441 type = cp_parser_type_id (parser);
3442 /* Look for the closing `)'. */
3443 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3444 /* Using `va_arg' in a constant-expression is not
3446 if (cp_parser_non_integral_constant_expression (parser,
3448 return error_mark_node;
3449 return build_x_va_arg (expression, type);
3453 return cp_parser_builtin_offsetof (parser);
3455 case RID_HAS_NOTHROW_ASSIGN:
3456 case RID_HAS_NOTHROW_CONSTRUCTOR:
3457 case RID_HAS_NOTHROW_COPY:
3458 case RID_HAS_TRIVIAL_ASSIGN:
3459 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3460 case RID_HAS_TRIVIAL_COPY:
3461 case RID_HAS_TRIVIAL_DESTRUCTOR:
3462 case RID_HAS_VIRTUAL_DESTRUCTOR:
3463 case RID_IS_ABSTRACT:
3464 case RID_IS_BASE_OF:
3466 case RID_IS_CONVERTIBLE_TO:
3470 case RID_IS_POLYMORPHIC:
3471 case RID_IS_STD_LAYOUT:
3472 case RID_IS_TRIVIAL:
3474 return cp_parser_trait_expr (parser, token->keyword);
3476 /* Objective-C++ expressions. */
3478 case RID_AT_PROTOCOL:
3479 case RID_AT_SELECTOR:
3480 return cp_parser_objc_expression (parser);
3483 cp_parser_error (parser, "expected primary-expression");
3484 return error_mark_node;
3487 /* An id-expression can start with either an identifier, a
3488 `::' as the beginning of a qualified-id, or the "operator"
3492 case CPP_TEMPLATE_ID:
3493 case CPP_NESTED_NAME_SPECIFIER:
3497 const char *error_msg;
3500 cp_token *id_expr_token;
3503 /* Parse the id-expression. */
3505 = cp_parser_id_expression (parser,
3506 /*template_keyword_p=*/false,
3507 /*check_dependency_p=*/true,
3509 /*declarator_p=*/false,
3510 /*optional_p=*/false);
3511 if (id_expression == error_mark_node)
3512 return error_mark_node;
3513 id_expr_token = token;
3514 token = cp_lexer_peek_token (parser->lexer);
3515 done = (token->type != CPP_OPEN_SQUARE
3516 && token->type != CPP_OPEN_PAREN
3517 && token->type != CPP_DOT
3518 && token->type != CPP_DEREF
3519 && token->type != CPP_PLUS_PLUS
3520 && token->type != CPP_MINUS_MINUS);
3521 /* If we have a template-id, then no further lookup is
3522 required. If the template-id was for a template-class, we
3523 will sometimes have a TYPE_DECL at this point. */
3524 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3525 || TREE_CODE (id_expression) == TYPE_DECL)
3526 decl = id_expression;
3527 /* Look up the name. */
3530 tree ambiguous_decls;
3532 /* If we already know that this lookup is ambiguous, then
3533 we've already issued an error message; there's no reason
3535 if (id_expr_token->type == CPP_NAME
3536 && id_expr_token->ambiguous_p)
3538 cp_parser_simulate_error (parser);
3539 return error_mark_node;
3542 decl = cp_parser_lookup_name (parser, id_expression,
3545 /*is_namespace=*/false,
3546 /*check_dependency=*/true,
3548 id_expr_token->location);
3549 /* If the lookup was ambiguous, an error will already have
3551 if (ambiguous_decls)
3552 return error_mark_node;
3554 /* In Objective-C++, an instance variable (ivar) may be preferred
3555 to whatever cp_parser_lookup_name() found. */
3556 decl = objc_lookup_ivar (decl, id_expression);
3558 /* If name lookup gives us a SCOPE_REF, then the
3559 qualifying scope was dependent. */
3560 if (TREE_CODE (decl) == SCOPE_REF)
3562 /* At this point, we do not know if DECL is a valid
3563 integral constant expression. We assume that it is
3564 in fact such an expression, so that code like:
3566 template <int N> struct A {
3570 is accepted. At template-instantiation time, we
3571 will check that B<N>::i is actually a constant. */
3574 /* Check to see if DECL is a local variable in a context
3575 where that is forbidden. */
3576 if (parser->local_variables_forbidden_p
3577 && local_variable_p (decl))
3579 /* It might be that we only found DECL because we are
3580 trying to be generous with pre-ISO scoping rules.
3581 For example, consider:
3585 for (int i = 0; i < 10; ++i) {}
3586 extern void f(int j = i);
3589 Here, name look up will originally find the out
3590 of scope `i'. We need to issue a warning message,
3591 but then use the global `i'. */
3592 decl = check_for_out_of_scope_variable (decl);
3593 if (local_variable_p (decl))
3595 error_at (id_expr_token->location,
3596 "local variable %qD may not appear in this context",
3598 return error_mark_node;
3603 decl = (finish_id_expression
3604 (id_expression, decl, parser->scope,
3606 parser->integral_constant_expression_p,
3607 parser->allow_non_integral_constant_expression_p,
3608 &parser->non_integral_constant_expression_p,
3609 template_p, done, address_p,
3612 id_expr_token->location));
3614 cp_parser_error (parser, error_msg);
3618 /* Anything else is an error. */
3620 cp_parser_error (parser, "expected primary-expression");
3621 return error_mark_node;
3625 /* Parse an id-expression.
3632 :: [opt] nested-name-specifier template [opt] unqualified-id
3634 :: operator-function-id
3637 Return a representation of the unqualified portion of the
3638 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3639 a `::' or nested-name-specifier.
3641 Often, if the id-expression was a qualified-id, the caller will
3642 want to make a SCOPE_REF to represent the qualified-id. This
3643 function does not do this in order to avoid wastefully creating
3644 SCOPE_REFs when they are not required.
3646 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3649 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3650 uninstantiated templates.
3652 If *TEMPLATE_P is non-NULL, it is set to true iff the
3653 `template' keyword is used to explicitly indicate that the entity
3654 named is a template.
3656 If DECLARATOR_P is true, the id-expression is appearing as part of
3657 a declarator, rather than as part of an expression. */
3660 cp_parser_id_expression (cp_parser *parser,
3661 bool template_keyword_p,
3662 bool check_dependency_p,
3667 bool global_scope_p;
3668 bool nested_name_specifier_p;
3670 /* Assume the `template' keyword was not used. */
3672 *template_p = template_keyword_p;
3674 /* Look for the optional `::' operator. */
3676 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678 /* Look for the optional nested-name-specifier. */
3679 nested_name_specifier_p
3680 = (cp_parser_nested_name_specifier_opt (parser,
3681 /*typename_keyword_p=*/false,
3686 /* If there is a nested-name-specifier, then we are looking at
3687 the first qualified-id production. */
3688 if (nested_name_specifier_p)
3691 tree saved_object_scope;
3692 tree saved_qualifying_scope;
3693 tree unqualified_id;
3696 /* See if the next token is the `template' keyword. */
3698 template_p = &is_template;
3699 *template_p = cp_parser_optional_template_keyword (parser);
3700 /* Name lookup we do during the processing of the
3701 unqualified-id might obliterate SCOPE. */
3702 saved_scope = parser->scope;
3703 saved_object_scope = parser->object_scope;
3704 saved_qualifying_scope = parser->qualifying_scope;
3705 /* Process the final unqualified-id. */
3706 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3709 /*optional_p=*/false);
3710 /* Restore the SAVED_SCOPE for our caller. */
3711 parser->scope = saved_scope;
3712 parser->object_scope = saved_object_scope;
3713 parser->qualifying_scope = saved_qualifying_scope;
3715 return unqualified_id;
3717 /* Otherwise, if we are in global scope, then we are looking at one
3718 of the other qualified-id productions. */
3719 else if (global_scope_p)
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3727 /* If it's an identifier, and the next token is not a "<", then
3728 we can avoid the template-id case. This is an optimization
3729 for this common case. */
3730 if (token->type == CPP_NAME
3731 && !cp_parser_nth_token_starts_template_argument_list_p
3733 return cp_parser_identifier (parser);
3735 cp_parser_parse_tentatively (parser);
3736 /* Try a template-id. */
3737 id = cp_parser_template_id (parser,
3738 /*template_keyword_p=*/false,
3739 /*check_dependency_p=*/true,
3741 /* If that worked, we're done. */
3742 if (cp_parser_parse_definitely (parser))
3745 /* Peek at the next token. (Changes in the token buffer may
3746 have invalidated the pointer obtained above.) */
3747 token = cp_lexer_peek_token (parser->lexer);
3749 switch (token->type)
3752 return cp_parser_identifier (parser);
3755 if (token->keyword == RID_OPERATOR)
3756 return cp_parser_operator_function_id (parser);
3760 cp_parser_error (parser, "expected id-expression");
3761 return error_mark_node;
3765 return cp_parser_unqualified_id (parser, template_keyword_p,
3766 /*check_dependency_p=*/true,
3771 /* Parse an unqualified-id.
3775 operator-function-id
3776 conversion-function-id
3780 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3781 keyword, in a construct like `A::template ...'.
3783 Returns a representation of unqualified-id. For the `identifier'
3784 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3785 production a BIT_NOT_EXPR is returned; the operand of the
3786 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3787 other productions, see the documentation accompanying the
3788 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3789 names are looked up in uninstantiated templates. If DECLARATOR_P
3790 is true, the unqualified-id is appearing as part of a declarator,
3791 rather than as part of an expression. */
3794 cp_parser_unqualified_id (cp_parser* parser,
3795 bool template_keyword_p,
3796 bool check_dependency_p,
3802 /* Peek at the next token. */
3803 token = cp_lexer_peek_token (parser->lexer);
3805 switch (token->type)
3811 /* We don't know yet whether or not this will be a
3813 cp_parser_parse_tentatively (parser);
3814 /* Try a template-id. */
3815 id = cp_parser_template_id (parser, template_keyword_p,
3818 /* If it worked, we're done. */
3819 if (cp_parser_parse_definitely (parser))
3821 /* Otherwise, it's an ordinary identifier. */
3822 return cp_parser_identifier (parser);
3825 case CPP_TEMPLATE_ID:
3826 return cp_parser_template_id (parser, template_keyword_p,
3833 tree qualifying_scope;
3838 /* Consume the `~' token. */
3839 cp_lexer_consume_token (parser->lexer);
3840 /* Parse the class-name. The standard, as written, seems to
3843 template <typename T> struct S { ~S (); };
3844 template <typename T> S<T>::~S() {}
3846 is invalid, since `~' must be followed by a class-name, but
3847 `S<T>' is dependent, and so not known to be a class.
3848 That's not right; we need to look in uninstantiated
3849 templates. A further complication arises from:
3851 template <typename T> void f(T t) {
3855 Here, it is not possible to look up `T' in the scope of `T'
3856 itself. We must look in both the current scope, and the
3857 scope of the containing complete expression.
3859 Yet another issue is:
3868 The standard does not seem to say that the `S' in `~S'
3869 should refer to the type `S' and not the data member
3872 /* DR 244 says that we look up the name after the "~" in the
3873 same scope as we looked up the qualifying name. That idea
3874 isn't fully worked out; it's more complicated than that. */
3875 scope = parser->scope;
3876 object_scope = parser->object_scope;
3877 qualifying_scope = parser->qualifying_scope;
3879 /* Check for invalid scopes. */
3880 if (scope == error_mark_node)
3882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883 cp_lexer_consume_token (parser->lexer);
3884 return error_mark_node;
3886 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3889 error_at (token->location,
3890 "scope %qT before %<~%> is not a class-name",
3892 cp_parser_simulate_error (parser);
3893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3894 cp_lexer_consume_token (parser->lexer);
3895 return error_mark_node;
3897 gcc_assert (!scope || TYPE_P (scope));
3899 /* If the name is of the form "X::~X" it's OK. */
3900 token = cp_lexer_peek_token (parser->lexer);
3902 && token->type == CPP_NAME
3903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3905 && constructor_name_p (token->u.value, scope))
3907 cp_lexer_consume_token (parser->lexer);
3908 return build_nt (BIT_NOT_EXPR, scope);
3911 /* If there was an explicit qualification (S::~T), first look
3912 in the scope given by the qualification (i.e., S).
3914 Note: in the calls to cp_parser_class_name below we pass
3915 typename_type so that lookup finds the injected-class-name
3916 rather than the constructor. */
3918 type_decl = NULL_TREE;
3921 cp_parser_parse_tentatively (parser);
3922 type_decl = cp_parser_class_name (parser,
3923 /*typename_keyword_p=*/false,
3924 /*template_keyword_p=*/false,
3926 /*check_dependency=*/false,
3927 /*class_head_p=*/false,
3929 if (cp_parser_parse_definitely (parser))
3932 /* In "N::S::~S", look in "N" as well. */
3933 if (!done && scope && qualifying_scope)
3935 cp_parser_parse_tentatively (parser);
3936 parser->scope = qualifying_scope;
3937 parser->object_scope = NULL_TREE;
3938 parser->qualifying_scope = NULL_TREE;
3940 = cp_parser_class_name (parser,
3941 /*typename_keyword_p=*/false,
3942 /*template_keyword_p=*/false,
3944 /*check_dependency=*/false,
3945 /*class_head_p=*/false,
3947 if (cp_parser_parse_definitely (parser))
3950 /* In "p->S::~T", look in the scope given by "*p" as well. */
3951 else if (!done && object_scope)
3953 cp_parser_parse_tentatively (parser);
3954 parser->scope = object_scope;
3955 parser->object_scope = NULL_TREE;
3956 parser->qualifying_scope = NULL_TREE;
3958 = cp_parser_class_name (parser,
3959 /*typename_keyword_p=*/false,
3960 /*template_keyword_p=*/false,
3962 /*check_dependency=*/false,
3963 /*class_head_p=*/false,
3965 if (cp_parser_parse_definitely (parser))
3968 /* Look in the surrounding context. */
3971 parser->scope = NULL_TREE;
3972 parser->object_scope = NULL_TREE;
3973 parser->qualifying_scope = NULL_TREE;
3974 if (processing_template_decl)
3975 cp_parser_parse_tentatively (parser);
3977 = cp_parser_class_name (parser,
3978 /*typename_keyword_p=*/false,
3979 /*template_keyword_p=*/false,
3981 /*check_dependency=*/false,
3982 /*class_head_p=*/false,
3984 if (processing_template_decl
3985 && ! cp_parser_parse_definitely (parser))
3987 /* We couldn't find a type with this name, so just accept
3988 it and check for a match at instantiation time. */
3989 type_decl = cp_parser_identifier (parser);
3990 if (type_decl != error_mark_node)
3991 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3995 /* If an error occurred, assume that the name of the
3996 destructor is the same as the name of the qualifying
3997 class. That allows us to keep parsing after running
3998 into ill-formed destructor names. */
3999 if (type_decl == error_mark_node && scope)
4000 return build_nt (BIT_NOT_EXPR, scope);
4001 else if (type_decl == error_mark_node)
4002 return error_mark_node;
4004 /* Check that destructor name and scope match. */
4005 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4007 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 error_at (token->location,
4009 "declaration of %<~%T%> as member of %qT",
4011 cp_parser_simulate_error (parser);
4012 return error_mark_node;
4017 A typedef-name that names a class shall not be used as the
4018 identifier in the declarator for a destructor declaration. */
4020 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4021 && !DECL_SELF_REFERENCE_P (type_decl)
4022 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4023 error_at (token->location,
4024 "typedef-name %qD used as destructor declarator",
4027 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4031 if (token->keyword == RID_OPERATOR)
4035 /* This could be a template-id, so we try that first. */
4036 cp_parser_parse_tentatively (parser);
4037 /* Try a template-id. */
4038 id = cp_parser_template_id (parser, template_keyword_p,
4039 /*check_dependency_p=*/true,
4041 /* If that worked, we're done. */
4042 if (cp_parser_parse_definitely (parser))
4044 /* We still don't know whether we're looking at an
4045 operator-function-id or a conversion-function-id. */
4046 cp_parser_parse_tentatively (parser);
4047 /* Try an operator-function-id. */
4048 id = cp_parser_operator_function_id (parser);
4049 /* If that didn't work, try a conversion-function-id. */
4050 if (!cp_parser_parse_definitely (parser))
4051 id = cp_parser_conversion_function_id (parser);
4060 cp_parser_error (parser, "expected unqualified-id");
4061 return error_mark_node;
4065 /* Parse an (optional) nested-name-specifier.
4067 nested-name-specifier: [C++98]
4068 class-or-namespace-name :: nested-name-specifier [opt]
4069 class-or-namespace-name :: template nested-name-specifier [opt]
4071 nested-name-specifier: [C++0x]
4074 nested-name-specifier identifier ::
4075 nested-name-specifier template [opt] simple-template-id ::
4077 PARSER->SCOPE should be set appropriately before this function is
4078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4082 Sets PARSER->SCOPE to the class (TYPE) or namespace
4083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4084 it unchanged if there is no nested-name-specifier. Returns the new
4085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4088 part of a declaration and/or decl-specifier. */
4091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4092 bool typename_keyword_p,
4093 bool check_dependency_p,
4095 bool is_declaration)
4097 bool success = false;
4098 cp_token_position start = 0;
4101 /* Remember where the nested-name-specifier starts. */
4102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4104 start = cp_lexer_token_position (parser->lexer, false);
4105 push_deferring_access_checks (dk_deferred);
4112 tree saved_qualifying_scope;
4113 bool template_keyword_p;
4115 /* Spot cases that cannot be the beginning of a
4116 nested-name-specifier. */
4117 token = cp_lexer_peek_token (parser->lexer);
4119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4120 the already parsed nested-name-specifier. */
4121 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4123 /* Grab the nested-name-specifier and continue the loop. */
4124 cp_parser_pre_parsed_nested_name_specifier (parser);
4125 /* If we originally encountered this nested-name-specifier
4126 with IS_DECLARATION set to false, we will not have
4127 resolved TYPENAME_TYPEs, so we must do so here. */
4129 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4131 new_scope = resolve_typename_type (parser->scope,
4132 /*only_current_p=*/false);
4133 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4134 parser->scope = new_scope;
4140 /* Spot cases that cannot be the beginning of a
4141 nested-name-specifier. On the second and subsequent times
4142 through the loop, we look for the `template' keyword. */
4143 if (success && token->keyword == RID_TEMPLATE)
4145 /* A template-id can start a nested-name-specifier. */
4146 else if (token->type == CPP_TEMPLATE_ID)
4150 /* If the next token is not an identifier, then it is
4151 definitely not a type-name or namespace-name. */
4152 if (token->type != CPP_NAME)
4154 /* If the following token is neither a `<' (to begin a
4155 template-id), nor a `::', then we are not looking at a
4156 nested-name-specifier. */
4157 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4158 if (token->type != CPP_SCOPE
4159 && !cp_parser_nth_token_starts_template_argument_list_p
4164 /* The nested-name-specifier is optional, so we parse
4166 cp_parser_parse_tentatively (parser);
4168 /* Look for the optional `template' keyword, if this isn't the
4169 first time through the loop. */
4171 template_keyword_p = cp_parser_optional_template_keyword (parser);
4173 template_keyword_p = false;
4175 /* Save the old scope since the name lookup we are about to do
4176 might destroy it. */
4177 old_scope = parser->scope;
4178 saved_qualifying_scope = parser->qualifying_scope;
4179 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4180 look up names in "X<T>::I" in order to determine that "Y" is
4181 a template. So, if we have a typename at this point, we make
4182 an effort to look through it. */
4184 && !typename_keyword_p
4186 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4187 parser->scope = resolve_typename_type (parser->scope,
4188 /*only_current_p=*/false);
4189 /* Parse the qualifying entity. */
4191 = cp_parser_qualifying_entity (parser,
4197 /* Look for the `::' token. */
4198 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4200 /* If we found what we wanted, we keep going; otherwise, we're
4202 if (!cp_parser_parse_definitely (parser))
4204 bool error_p = false;
4206 /* Restore the OLD_SCOPE since it was valid before the
4207 failed attempt at finding the last
4208 class-or-namespace-name. */
4209 parser->scope = old_scope;
4210 parser->qualifying_scope = saved_qualifying_scope;
4211 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4213 /* If the next token is an identifier, and the one after
4214 that is a `::', then any valid interpretation would have
4215 found a class-or-namespace-name. */
4216 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4217 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4219 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4222 token = cp_lexer_consume_token (parser->lexer);
4225 if (!token->ambiguous_p)
4228 tree ambiguous_decls;
4230 decl = cp_parser_lookup_name (parser, token->u.value,
4232 /*is_template=*/false,
4233 /*is_namespace=*/false,
4234 /*check_dependency=*/true,
4237 if (TREE_CODE (decl) == TEMPLATE_DECL)
4238 error_at (token->location,
4239 "%qD used without template parameters",
4241 else if (ambiguous_decls)
4243 error_at (token->location,
4244 "reference to %qD is ambiguous",
4246 print_candidates (ambiguous_decls);
4247 decl = error_mark_node;
4251 const char* msg = "is not a class or namespace";
4252 if (cxx_dialect != cxx98)
4253 msg = "is not a class, namespace, or enumeration";
4254 cp_parser_name_lookup_error
4255 (parser, token->u.value, decl, msg,
4259 parser->scope = error_mark_node;
4261 /* Treat this as a successful nested-name-specifier
4266 If the name found is not a class-name (clause
4267 _class_) or namespace-name (_namespace.def_), the
4268 program is ill-formed. */
4271 cp_lexer_consume_token (parser->lexer);
4275 /* We've found one valid nested-name-specifier. */
4277 /* Name lookup always gives us a DECL. */
4278 if (TREE_CODE (new_scope) == TYPE_DECL)
4279 new_scope = TREE_TYPE (new_scope);
4280 /* Uses of "template" must be followed by actual templates. */
4281 if (template_keyword_p
4282 && !(CLASS_TYPE_P (new_scope)
4283 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4284 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4285 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4286 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4287 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4288 == TEMPLATE_ID_EXPR)))
4289 permerror (input_location, TYPE_P (new_scope)
4290 ? "%qT is not a template"
4291 : "%qD is not a template",
4293 /* If it is a class scope, try to complete it; we are about to
4294 be looking up names inside the class. */
4295 if (TYPE_P (new_scope)
4296 /* Since checking types for dependency can be expensive,
4297 avoid doing it if the type is already complete. */
4298 && !COMPLETE_TYPE_P (new_scope)
4299 /* Do not try to complete dependent types. */
4300 && !dependent_type_p (new_scope))
4302 new_scope = complete_type (new_scope);
4303 /* If it is a typedef to current class, use the current
4304 class instead, as the typedef won't have any names inside
4306 if (!COMPLETE_TYPE_P (new_scope)
4307 && currently_open_class (new_scope))
4308 new_scope = TYPE_MAIN_VARIANT (new_scope);
4310 /* Make sure we look in the right scope the next time through
4312 parser->scope = new_scope;
4315 /* If parsing tentatively, replace the sequence of tokens that makes
4316 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4317 token. That way, should we re-parse the token stream, we will
4318 not have to repeat the effort required to do the parse, nor will
4319 we issue duplicate error messages. */
4320 if (success && start)
4324 token = cp_lexer_token_at (parser->lexer, start);
4325 /* Reset the contents of the START token. */
4326 token->type = CPP_NESTED_NAME_SPECIFIER;
4327 /* Retrieve any deferred checks. Do not pop this access checks yet
4328 so the memory will not be reclaimed during token replacing below. */
4329 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4330 token->u.tree_check_value->value = parser->scope;
4331 token->u.tree_check_value->checks = get_deferred_access_checks ();
4332 token->u.tree_check_value->qualifying_scope =
4333 parser->qualifying_scope;
4334 token->keyword = RID_MAX;
4336 /* Purge all subsequent tokens. */
4337 cp_lexer_purge_tokens_after (parser->lexer, start);
4341 pop_to_parent_deferring_access_checks ();
4343 return success ? parser->scope : NULL_TREE;
4346 /* Parse a nested-name-specifier. See
4347 cp_parser_nested_name_specifier_opt for details. This function
4348 behaves identically, except that it will an issue an error if no
4349 nested-name-specifier is present. */
4352 cp_parser_nested_name_specifier (cp_parser *parser,
4353 bool typename_keyword_p,
4354 bool check_dependency_p,
4356 bool is_declaration)
4360 /* Look for the nested-name-specifier. */
4361 scope = cp_parser_nested_name_specifier_opt (parser,
4366 /* If it was not present, issue an error message. */
4369 cp_parser_error (parser, "expected nested-name-specifier");
4370 parser->scope = NULL_TREE;
4376 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4377 this is either a class-name or a namespace-name (which corresponds
4378 to the class-or-namespace-name production in the grammar). For
4379 C++0x, it can also be a type-name that refers to an enumeration
4382 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4383 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4384 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4385 TYPE_P is TRUE iff the next name should be taken as a class-name,
4386 even the same name is declared to be another entity in the same
4389 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4390 specified by the class-or-namespace-name. If neither is found the
4391 ERROR_MARK_NODE is returned. */
4394 cp_parser_qualifying_entity (cp_parser *parser,
4395 bool typename_keyword_p,
4396 bool template_keyword_p,
4397 bool check_dependency_p,
4399 bool is_declaration)
4402 tree saved_qualifying_scope;
4403 tree saved_object_scope;
4406 bool successful_parse_p;
4408 /* Before we try to parse the class-name, we must save away the
4409 current PARSER->SCOPE since cp_parser_class_name will destroy
4411 saved_scope = parser->scope;
4412 saved_qualifying_scope = parser->qualifying_scope;
4413 saved_object_scope = parser->object_scope;
4414 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4415 there is no need to look for a namespace-name. */
4416 only_class_p = template_keyword_p
4417 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4419 cp_parser_parse_tentatively (parser);
4420 scope = cp_parser_class_name (parser,
4423 type_p ? class_type : none_type,
4425 /*class_head_p=*/false,
4427 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4428 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4430 && cxx_dialect != cxx98
4431 && !successful_parse_p)
4433 /* Restore the saved scope. */
4434 parser->scope = saved_scope;
4435 parser->qualifying_scope = saved_qualifying_scope;
4436 parser->object_scope = saved_object_scope;
4438 /* Parse tentatively. */
4439 cp_parser_parse_tentatively (parser);
4441 /* Parse a typedef-name or enum-name. */
4442 scope = cp_parser_nonclass_name (parser);
4443 successful_parse_p = cp_parser_parse_definitely (parser);
4445 /* If that didn't work, try for a namespace-name. */
4446 if (!only_class_p && !successful_parse_p)
4448 /* Restore the saved scope. */
4449 parser->scope = saved_scope;
4450 parser->qualifying_scope = saved_qualifying_scope;
4451 parser->object_scope = saved_object_scope;
4452 /* If we are not looking at an identifier followed by the scope
4453 resolution operator, then this is not part of a
4454 nested-name-specifier. (Note that this function is only used
4455 to parse the components of a nested-name-specifier.) */
4456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4457 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4458 return error_mark_node;
4459 scope = cp_parser_namespace_name (parser);
4465 /* Parse a postfix-expression.
4469 postfix-expression [ expression ]
4470 postfix-expression ( expression-list [opt] )
4471 simple-type-specifier ( expression-list [opt] )
4472 typename :: [opt] nested-name-specifier identifier
4473 ( expression-list [opt] )
4474 typename :: [opt] nested-name-specifier template [opt] template-id
4475 ( expression-list [opt] )
4476 postfix-expression . template [opt] id-expression
4477 postfix-expression -> template [opt] id-expression
4478 postfix-expression . pseudo-destructor-name
4479 postfix-expression -> pseudo-destructor-name
4480 postfix-expression ++
4481 postfix-expression --
4482 dynamic_cast < type-id > ( expression )
4483 static_cast < type-id > ( expression )
4484 reinterpret_cast < type-id > ( expression )
4485 const_cast < type-id > ( expression )
4486 typeid ( expression )
4492 ( type-id ) { initializer-list , [opt] }
4494 This extension is a GNU version of the C99 compound-literal
4495 construct. (The C99 grammar uses `type-name' instead of `type-id',
4496 but they are essentially the same concept.)
4498 If ADDRESS_P is true, the postfix expression is the operand of the
4499 `&' operator. CAST_P is true if this expression is the target of a
4502 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4503 class member access expressions [expr.ref].
4505 Returns a representation of the expression. */
4508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4509 bool member_access_only_p,
4510 cp_id_kind * pidk_return)
4514 cp_id_kind idk = CP_ID_KIND_NONE;
4515 tree postfix_expression = NULL_TREE;
4516 bool is_member_access = false;
4518 /* Peek at the next token. */
4519 token = cp_lexer_peek_token (parser->lexer);
4520 /* Some of the productions are determined by keywords. */
4521 keyword = token->keyword;
4531 const char *saved_message;
4533 /* All of these can be handled in the same way from the point
4534 of view of parsing. Begin by consuming the token
4535 identifying the cast. */
4536 cp_lexer_consume_token (parser->lexer);
4538 /* New types cannot be defined in the cast. */
4539 saved_message = parser->type_definition_forbidden_message;
4540 parser->type_definition_forbidden_message
4541 = G_("types may not be defined in casts");
4543 /* Look for the opening `<'. */
4544 cp_parser_require (parser, CPP_LESS, "%<<%>");
4545 /* Parse the type to which we are casting. */
4546 type = cp_parser_type_id (parser);
4547 /* Look for the closing `>'. */
4548 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4549 /* Restore the old message. */
4550 parser->type_definition_forbidden_message = saved_message;
4552 /* And the expression which is being cast. */
4553 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4555 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557 /* Only type conversions to integral or enumeration types
4558 can be used in constant-expressions. */
4559 if (!cast_valid_in_integral_constant_expression_p (type)
4560 && (cp_parser_non_integral_constant_expression
4562 "a cast to a type other than an integral or "
4563 "enumeration type")))
4564 return error_mark_node;
4570 = build_dynamic_cast (type, expression, tf_warning_or_error);
4574 = build_static_cast (type, expression, tf_warning_or_error);
4578 = build_reinterpret_cast (type, expression,
4579 tf_warning_or_error);
4583 = build_const_cast (type, expression, tf_warning_or_error);
4594 const char *saved_message;
4595 bool saved_in_type_id_in_expr_p;
4597 /* Consume the `typeid' token. */
4598 cp_lexer_consume_token (parser->lexer);
4599 /* Look for the `(' token. */
4600 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4601 /* Types cannot be defined in a `typeid' expression. */
4602 saved_message = parser->type_definition_forbidden_message;
4603 parser->type_definition_forbidden_message
4604 = G_("types may not be defined in a %<typeid%> expression");
4605 /* We can't be sure yet whether we're looking at a type-id or an
4607 cp_parser_parse_tentatively (parser);
4608 /* Try a type-id first. */
4609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4610 parser->in_type_id_in_expr_p = true;
4611 type = cp_parser_type_id (parser);
4612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4613 /* Look for the `)' token. Otherwise, we can't be sure that
4614 we're not looking at an expression: consider `typeid (int
4615 (3))', for example. */
4616 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617 /* If all went well, simply lookup the type-id. */
4618 if (cp_parser_parse_definitely (parser))
4619 postfix_expression = get_typeid (type);
4620 /* Otherwise, fall back to the expression variant. */
4625 /* Look for an expression. */
4626 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4627 /* Compute its typeid. */
4628 postfix_expression = build_typeid (expression);
4629 /* Look for the `)' token. */
4630 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4632 /* Restore the saved message. */
4633 parser->type_definition_forbidden_message = saved_message;
4634 /* `typeid' may not appear in an integral constant expression. */
4635 if (cp_parser_non_integral_constant_expression(parser,
4636 "%<typeid%> operator"))
4637 return error_mark_node;
4644 /* The syntax permitted here is the same permitted for an
4645 elaborated-type-specifier. */
4646 type = cp_parser_elaborated_type_specifier (parser,
4647 /*is_friend=*/false,
4648 /*is_declaration=*/false);
4649 postfix_expression = cp_parser_functional_cast (parser, type);
4657 /* If the next thing is a simple-type-specifier, we may be
4658 looking at a functional cast. We could also be looking at
4659 an id-expression. So, we try the functional cast, and if
4660 that doesn't work we fall back to the primary-expression. */
4661 cp_parser_parse_tentatively (parser);
4662 /* Look for the simple-type-specifier. */
4663 type = cp_parser_simple_type_specifier (parser,
4664 /*decl_specs=*/NULL,
4665 CP_PARSER_FLAGS_NONE);
4666 /* Parse the cast itself. */
4667 if (!cp_parser_error_occurred (parser))
4669 = cp_parser_functional_cast (parser, type);
4670 /* If that worked, we're done. */
4671 if (cp_parser_parse_definitely (parser))
4674 /* If the functional-cast didn't work out, try a
4675 compound-literal. */
4676 if (cp_parser_allow_gnu_extensions_p (parser)
4677 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4679 VEC(constructor_elt,gc) *initializer_list = NULL;
4680 bool saved_in_type_id_in_expr_p;
4682 cp_parser_parse_tentatively (parser);
4683 /* Consume the `('. */
4684 cp_lexer_consume_token (parser->lexer);
4685 /* Parse the type. */
4686 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4687 parser->in_type_id_in_expr_p = true;
4688 type = cp_parser_type_id (parser);
4689 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4690 /* Look for the `)'. */
4691 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4692 /* Look for the `{'. */
4693 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4694 /* If things aren't going well, there's no need to
4696 if (!cp_parser_error_occurred (parser))
4698 bool non_constant_p;
4699 /* Parse the initializer-list. */
4701 = cp_parser_initializer_list (parser, &non_constant_p);
4702 /* Allow a trailing `,'. */
4703 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4704 cp_lexer_consume_token (parser->lexer);
4705 /* Look for the final `}'. */
4706 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4708 /* If that worked, we're definitely looking at a
4709 compound-literal expression. */
4710 if (cp_parser_parse_definitely (parser))
4712 /* Warn the user that a compound literal is not
4713 allowed in standard C++. */
4714 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4715 /* For simplicity, we disallow compound literals in
4716 constant-expressions. We could
4717 allow compound literals of integer type, whose
4718 initializer was a constant, in constant
4719 expressions. Permitting that usage, as a further
4720 extension, would not change the meaning of any
4721 currently accepted programs. (Of course, as
4722 compound literals are not part of ISO C++, the
4723 standard has nothing to say.) */
4724 if (cp_parser_non_integral_constant_expression
4725 (parser, "non-constant compound literals"))
4727 postfix_expression = error_mark_node;
4730 /* Form the representation of the compound-literal. */
4732 = (finish_compound_literal
4733 (type, build_constructor (init_list_type_node,
4734 initializer_list)));
4739 /* It must be a primary-expression. */
4741 = cp_parser_primary_expression (parser, address_p, cast_p,
4742 /*template_arg_p=*/false,
4748 /* Keep looping until the postfix-expression is complete. */
4751 if (idk == CP_ID_KIND_UNQUALIFIED
4752 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4753 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4754 /* It is not a Koenig lookup function call. */
4756 = unqualified_name_lookup_error (postfix_expression);
4758 /* Peek at the next token. */
4759 token = cp_lexer_peek_token (parser->lexer);
4761 switch (token->type)
4763 case CPP_OPEN_SQUARE:
4765 = cp_parser_postfix_open_square_expression (parser,
4768 idk = CP_ID_KIND_NONE;
4769 is_member_access = false;
4772 case CPP_OPEN_PAREN:
4773 /* postfix-expression ( expression-list [opt] ) */
4776 bool is_builtin_constant_p;
4777 bool saved_integral_constant_expression_p = false;
4778 bool saved_non_integral_constant_expression_p = false;
4781 is_member_access = false;
4783 is_builtin_constant_p
4784 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4785 if (is_builtin_constant_p)
4787 /* The whole point of __builtin_constant_p is to allow
4788 non-constant expressions to appear as arguments. */
4789 saved_integral_constant_expression_p
4790 = parser->integral_constant_expression_p;
4791 saved_non_integral_constant_expression_p
4792 = parser->non_integral_constant_expression_p;
4793 parser->integral_constant_expression_p = false;
4795 args = (cp_parser_parenthesized_expression_list
4796 (parser, /*is_attribute_list=*/false,
4797 /*cast_p=*/false, /*allow_expansion_p=*/true,
4798 /*non_constant_p=*/NULL));
4799 if (is_builtin_constant_p)
4801 parser->integral_constant_expression_p
4802 = saved_integral_constant_expression_p;
4803 parser->non_integral_constant_expression_p
4804 = saved_non_integral_constant_expression_p;
4809 postfix_expression = error_mark_node;
4813 /* Function calls are not permitted in
4814 constant-expressions. */
4815 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4816 && cp_parser_non_integral_constant_expression (parser,
4819 postfix_expression = error_mark_node;
4820 release_tree_vector (args);
4825 if (idk == CP_ID_KIND_UNQUALIFIED
4826 || idk == CP_ID_KIND_TEMPLATE_ID)
4828 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4830 if (!VEC_empty (tree, args))
4833 if (!any_type_dependent_arguments_p (args))
4835 = perform_koenig_lookup (postfix_expression, args);
4839 = unqualified_fn_lookup_error (postfix_expression);
4841 /* We do not perform argument-dependent lookup if
4842 normal lookup finds a non-function, in accordance
4843 with the expected resolution of DR 218. */
4844 else if (!VEC_empty (tree, args)
4845 && is_overloaded_fn (postfix_expression))
4847 tree fn = get_first_fn (postfix_expression);
4848 fn = STRIP_TEMPLATE (fn);
4850 /* Do not do argument dependent lookup if regular
4851 lookup finds a member function or a block-scope
4852 function declaration. [basic.lookup.argdep]/3 */
4853 if (!DECL_FUNCTION_MEMBER_P (fn)
4854 && !DECL_LOCAL_FUNCTION_P (fn))
4857 if (!any_type_dependent_arguments_p (args))
4859 = perform_koenig_lookup (postfix_expression, args);
4864 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4866 tree instance = TREE_OPERAND (postfix_expression, 0);
4867 tree fn = TREE_OPERAND (postfix_expression, 1);
4869 if (processing_template_decl
4870 && (type_dependent_expression_p (instance)
4871 || (!BASELINK_P (fn)
4872 && TREE_CODE (fn) != FIELD_DECL)
4873 || type_dependent_expression_p (fn)
4874 || any_type_dependent_arguments_p (args)))
4877 = build_nt_call_vec (postfix_expression, args);
4878 release_tree_vector (args);
4882 if (BASELINK_P (fn))
4885 = (build_new_method_call
4886 (instance, fn, &args, NULL_TREE,
4887 (idk == CP_ID_KIND_QUALIFIED
4888 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4890 tf_warning_or_error));
4894 = finish_call_expr (postfix_expression, &args,
4895 /*disallow_virtual=*/false,
4897 tf_warning_or_error);
4899 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4900 || TREE_CODE (postfix_expression) == MEMBER_REF
4901 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4902 postfix_expression = (build_offset_ref_call_from_tree
4903 (postfix_expression, &args));
4904 else if (idk == CP_ID_KIND_QUALIFIED)
4905 /* A call to a static class member, or a namespace-scope
4908 = finish_call_expr (postfix_expression, &args,
4909 /*disallow_virtual=*/true,
4911 tf_warning_or_error);
4913 /* All other function calls. */
4915 = finish_call_expr (postfix_expression, &args,
4916 /*disallow_virtual=*/false,
4918 tf_warning_or_error);
4920 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4921 idk = CP_ID_KIND_NONE;
4923 release_tree_vector (args);
4929 /* postfix-expression . template [opt] id-expression
4930 postfix-expression . pseudo-destructor-name
4931 postfix-expression -> template [opt] id-expression
4932 postfix-expression -> pseudo-destructor-name */
4934 /* Consume the `.' or `->' operator. */
4935 cp_lexer_consume_token (parser->lexer);
4938 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4943 is_member_access = true;
4947 /* postfix-expression ++ */
4948 /* Consume the `++' token. */
4949 cp_lexer_consume_token (parser->lexer);
4950 /* Generate a representation for the complete expression. */
4952 = finish_increment_expr (postfix_expression,
4953 POSTINCREMENT_EXPR);
4954 /* Increments may not appear in constant-expressions. */
4955 if (cp_parser_non_integral_constant_expression (parser,
4957 postfix_expression = error_mark_node;
4958 idk = CP_ID_KIND_NONE;
4959 is_member_access = false;
4962 case CPP_MINUS_MINUS:
4963 /* postfix-expression -- */
4964 /* Consume the `--' token. */
4965 cp_lexer_consume_token (parser->lexer);
4966 /* Generate a representation for the complete expression. */
4968 = finish_increment_expr (postfix_expression,
4969 POSTDECREMENT_EXPR);
4970 /* Decrements may not appear in constant-expressions. */
4971 if (cp_parser_non_integral_constant_expression (parser,
4973 postfix_expression = error_mark_node;
4974 idk = CP_ID_KIND_NONE;
4975 is_member_access = false;
4979 if (pidk_return != NULL)
4980 * pidk_return = idk;
4981 if (member_access_only_p)
4982 return is_member_access? postfix_expression : error_mark_node;
4984 return postfix_expression;
4988 /* We should never get here. */
4990 return error_mark_node;
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994 by cp_parser_builtin_offsetof. We're looking for
4996 postfix-expression [ expression ]
4998 FOR_OFFSETOF is set if we're being called in that context, which
4999 changes how we deal with integer constant expressions. */
5002 cp_parser_postfix_open_square_expression (cp_parser *parser,
5003 tree postfix_expression,
5008 /* Consume the `[' token. */
5009 cp_lexer_consume_token (parser->lexer);
5011 /* Parse the index expression. */
5012 /* ??? For offsetof, there is a question of what to allow here. If
5013 offsetof is not being used in an integral constant expression context,
5014 then we *could* get the right answer by computing the value at runtime.
5015 If we are in an integral constant expression context, then we might
5016 could accept any constant expression; hard to say without analysis.
5017 Rather than open the barn door too wide right away, allow only integer
5018 constant expressions here. */
5020 index = cp_parser_constant_expression (parser, false, NULL);
5022 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5024 /* Look for the closing `]'. */
5025 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5027 /* Build the ARRAY_REF. */
5028 postfix_expression = grok_array_decl (postfix_expression, index);
5030 /* When not doing offsetof, array references are not permitted in
5031 constant-expressions. */
5033 && (cp_parser_non_integral_constant_expression
5034 (parser, "an array reference")))
5035 postfix_expression = error_mark_node;
5037 return postfix_expression;
5040 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5041 by cp_parser_builtin_offsetof. We're looking for
5043 postfix-expression . template [opt] id-expression
5044 postfix-expression . pseudo-destructor-name
5045 postfix-expression -> template [opt] id-expression
5046 postfix-expression -> pseudo-destructor-name
5048 FOR_OFFSETOF is set if we're being called in that context. That sorta
5049 limits what of the above we'll actually accept, but nevermind.
5050 TOKEN_TYPE is the "." or "->" token, which will already have been
5051 removed from the stream. */
5054 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5055 enum cpp_ttype token_type,
5056 tree postfix_expression,
5057 bool for_offsetof, cp_id_kind *idk,
5058 location_t location)
5062 bool pseudo_destructor_p;
5063 tree scope = NULL_TREE;
5065 /* If this is a `->' operator, dereference the pointer. */
5066 if (token_type == CPP_DEREF)
5067 postfix_expression = build_x_arrow (postfix_expression);
5068 /* Check to see whether or not the expression is type-dependent. */
5069 dependent_p = type_dependent_expression_p (postfix_expression);
5070 /* The identifier following the `->' or `.' is not qualified. */
5071 parser->scope = NULL_TREE;
5072 parser->qualifying_scope = NULL_TREE;
5073 parser->object_scope = NULL_TREE;
5074 *idk = CP_ID_KIND_NONE;
5076 /* Enter the scope corresponding to the type of the object
5077 given by the POSTFIX_EXPRESSION. */
5078 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5080 scope = TREE_TYPE (postfix_expression);
5081 /* According to the standard, no expression should ever have
5082 reference type. Unfortunately, we do not currently match
5083 the standard in this respect in that our internal representation
5084 of an expression may have reference type even when the standard
5085 says it does not. Therefore, we have to manually obtain the
5086 underlying type here. */
5087 scope = non_reference (scope);
5088 /* The type of the POSTFIX_EXPRESSION must be complete. */
5089 if (scope == unknown_type_node)
5091 error_at (location, "%qE does not have class type",
5092 postfix_expression);
5096 scope = complete_type_or_else (scope, NULL_TREE);
5097 /* Let the name lookup machinery know that we are processing a
5098 class member access expression. */
5099 parser->context->object_type = scope;
5100 /* If something went wrong, we want to be able to discern that case,
5101 as opposed to the case where there was no SCOPE due to the type
5102 of expression being dependent. */
5104 scope = error_mark_node;
5105 /* If the SCOPE was erroneous, make the various semantic analysis
5106 functions exit quickly -- and without issuing additional error
5108 if (scope == error_mark_node)
5109 postfix_expression = error_mark_node;
5112 /* Assume this expression is not a pseudo-destructor access. */
5113 pseudo_destructor_p = false;
5115 /* If the SCOPE is a scalar type, then, if this is a valid program,
5116 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5117 is type dependent, it can be pseudo-destructor-name or something else.
5118 Try to parse it as pseudo-destructor-name first. */
5119 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5124 cp_parser_parse_tentatively (parser);
5125 /* Parse the pseudo-destructor-name. */
5127 cp_parser_pseudo_destructor_name (parser, &s, &type);
5129 && (cp_parser_error_occurred (parser)
5130 || TREE_CODE (type) != TYPE_DECL
5131 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5132 cp_parser_abort_tentative_parse (parser);
5133 else if (cp_parser_parse_definitely (parser))
5135 pseudo_destructor_p = true;
5137 = finish_pseudo_destructor_expr (postfix_expression,
5138 s, TREE_TYPE (type));
5142 if (!pseudo_destructor_p)
5144 /* If the SCOPE is not a scalar type, we are looking at an
5145 ordinary class member access expression, rather than a
5146 pseudo-destructor-name. */
5148 cp_token *token = cp_lexer_peek_token (parser->lexer);
5149 /* Parse the id-expression. */
5150 name = (cp_parser_id_expression
5152 cp_parser_optional_template_keyword (parser),
5153 /*check_dependency_p=*/true,
5155 /*declarator_p=*/false,
5156 /*optional_p=*/false));
5157 /* In general, build a SCOPE_REF if the member name is qualified.
5158 However, if the name was not dependent and has already been
5159 resolved; there is no need to build the SCOPE_REF. For example;
5161 struct X { void f(); };
5162 template <typename T> void f(T* t) { t->X::f(); }
5164 Even though "t" is dependent, "X::f" is not and has been resolved
5165 to a BASELINK; there is no need to include scope information. */
5167 /* But we do need to remember that there was an explicit scope for
5168 virtual function calls. */
5170 *idk = CP_ID_KIND_QUALIFIED;
5172 /* If the name is a template-id that names a type, we will get a
5173 TYPE_DECL here. That is invalid code. */
5174 if (TREE_CODE (name) == TYPE_DECL)
5176 error_at (token->location, "invalid use of %qD", name);
5177 postfix_expression = error_mark_node;
5181 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5183 name = build_qualified_name (/*type=*/NULL_TREE,
5187 parser->scope = NULL_TREE;
5188 parser->qualifying_scope = NULL_TREE;
5189 parser->object_scope = NULL_TREE;
5191 if (scope && name && BASELINK_P (name))
5192 adjust_result_of_qualified_name_lookup
5193 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5195 = finish_class_member_access_expr (postfix_expression, name,
5197 tf_warning_or_error);
5201 /* We no longer need to look up names in the scope of the object on
5202 the left-hand side of the `.' or `->' operator. */
5203 parser->context->object_type = NULL_TREE;
5205 /* Outside of offsetof, these operators may not appear in
5206 constant-expressions. */
5208 && (cp_parser_non_integral_constant_expression
5209 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5210 postfix_expression = error_mark_node;
5212 return postfix_expression;
5215 /* Parse a parenthesized expression-list.
5218 assignment-expression
5219 expression-list, assignment-expression
5224 identifier, expression-list
5226 CAST_P is true if this expression is the target of a cast.
5228 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5231 Returns a vector of trees. Each element is a representation of an
5232 assignment-expression. NULL is returned if the ( and or ) are
5233 missing. An empty, but allocated, vector is returned on no
5234 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5235 if this is really an attribute list being parsed. If
5236 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5237 not all of the expressions in the list were constant. */
5239 static VEC(tree,gc) *
5240 cp_parser_parenthesized_expression_list (cp_parser* parser,
5241 bool is_attribute_list,
5243 bool allow_expansion_p,
5244 bool *non_constant_p)
5246 VEC(tree,gc) *expression_list;
5247 bool fold_expr_p = is_attribute_list;
5248 tree identifier = NULL_TREE;
5249 bool saved_greater_than_is_operator_p;
5251 /* Assume all the expressions will be constant. */
5253 *non_constant_p = false;
5255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5258 expression_list = make_tree_vector ();
5260 /* Within a parenthesized expression, a `>' token is always
5261 the greater-than operator. */
5262 saved_greater_than_is_operator_p
5263 = parser->greater_than_is_operator_p;
5264 parser->greater_than_is_operator_p = true;
5266 /* Consume expressions until there are no more. */
5267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5272 /* At the beginning of attribute lists, check to see if the
5273 next token is an identifier. */
5274 if (is_attribute_list
5275 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5279 /* Consume the identifier. */
5280 token = cp_lexer_consume_token (parser->lexer);
5281 /* Save the identifier. */
5282 identifier = token->u.value;
5286 bool expr_non_constant_p;
5288 /* Parse the next assignment-expression. */
5289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5291 /* A braced-init-list. */
5292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5293 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5294 if (non_constant_p && expr_non_constant_p)
5295 *non_constant_p = true;
5297 else if (non_constant_p)
5299 expr = (cp_parser_constant_expression
5300 (parser, /*allow_non_constant_p=*/true,
5301 &expr_non_constant_p));
5302 if (expr_non_constant_p)
5303 *non_constant_p = true;
5306 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5309 expr = fold_non_dependent_expr (expr);
5311 /* If we have an ellipsis, then this is an expression
5313 if (allow_expansion_p
5314 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5316 /* Consume the `...'. */
5317 cp_lexer_consume_token (parser->lexer);
5319 /* Build the argument pack. */
5320 expr = make_pack_expansion (expr);
5323 /* Add it to the list. We add error_mark_node
5324 expressions to the list, so that we can still tell if
5325 the correct form for a parenthesized expression-list
5326 is found. That gives better errors. */
5327 VEC_safe_push (tree, gc, expression_list, expr);
5329 if (expr == error_mark_node)
5333 /* After the first item, attribute lists look the same as
5334 expression lists. */
5335 is_attribute_list = false;
5338 /* If the next token isn't a `,', then we are done. */
5339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5342 /* Otherwise, consume the `,' and keep going. */
5343 cp_lexer_consume_token (parser->lexer);
5346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5351 /* We try and resync to an unnested comma, as that will give the
5352 user better diagnostics. */
5353 ending = cp_parser_skip_to_closing_parenthesis (parser,
5354 /*recovering=*/true,
5356 /*consume_paren=*/true);
5361 parser->greater_than_is_operator_p
5362 = saved_greater_than_is_operator_p;
5367 parser->greater_than_is_operator_p
5368 = saved_greater_than_is_operator_p;
5371 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5373 return expression_list;
5376 /* Parse a pseudo-destructor-name.
5378 pseudo-destructor-name:
5379 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5380 :: [opt] nested-name-specifier template template-id :: ~ type-name
5381 :: [opt] nested-name-specifier [opt] ~ type-name
5383 If either of the first two productions is used, sets *SCOPE to the
5384 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5385 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5386 or ERROR_MARK_NODE if the parse fails. */
5389 cp_parser_pseudo_destructor_name (cp_parser* parser,
5393 bool nested_name_specifier_p;
5395 /* Assume that things will not work out. */
5396 *type = error_mark_node;
5398 /* Look for the optional `::' operator. */
5399 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5400 /* Look for the optional nested-name-specifier. */
5401 nested_name_specifier_p
5402 = (cp_parser_nested_name_specifier_opt (parser,
5403 /*typename_keyword_p=*/false,
5404 /*check_dependency_p=*/true,
5406 /*is_declaration=*/false)
5408 /* Now, if we saw a nested-name-specifier, we might be doing the
5409 second production. */
5410 if (nested_name_specifier_p
5411 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5413 /* Consume the `template' keyword. */
5414 cp_lexer_consume_token (parser->lexer);
5415 /* Parse the template-id. */
5416 cp_parser_template_id (parser,
5417 /*template_keyword_p=*/true,
5418 /*check_dependency_p=*/false,
5419 /*is_declaration=*/true);
5420 /* Look for the `::' token. */
5421 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5423 /* If the next token is not a `~', then there might be some
5424 additional qualification. */
5425 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5427 /* At this point, we're looking for "type-name :: ~". The type-name
5428 must not be a class-name, since this is a pseudo-destructor. So,
5429 it must be either an enum-name, or a typedef-name -- both of which
5430 are just identifiers. So, we peek ahead to check that the "::"
5431 and "~" tokens are present; if they are not, then we can avoid
5432 calling type_name. */
5433 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5434 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5435 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5437 cp_parser_error (parser, "non-scalar type");
5441 /* Look for the type-name. */
5442 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5443 if (*scope == error_mark_node)
5446 /* Look for the `::' token. */
5447 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5452 /* Look for the `~'. */
5453 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5454 /* Look for the type-name again. We are not responsible for
5455 checking that it matches the first type-name. */
5456 *type = cp_parser_nonclass_name (parser);
5459 /* Parse a unary-expression.
5465 unary-operator cast-expression
5466 sizeof unary-expression
5474 __extension__ cast-expression
5475 __alignof__ unary-expression
5476 __alignof__ ( type-id )
5477 __real__ cast-expression
5478 __imag__ cast-expression
5481 ADDRESS_P is true iff the unary-expression is appearing as the
5482 operand of the `&' operator. CAST_P is true if this expression is
5483 the target of a cast.
5485 Returns a representation of the expression. */
5488 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5492 enum tree_code unary_operator;
5494 /* Peek at the next token. */
5495 token = cp_lexer_peek_token (parser->lexer);
5496 /* Some keywords give away the kind of expression. */
5497 if (token->type == CPP_KEYWORD)
5499 enum rid keyword = token->keyword;
5509 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5510 /* Consume the token. */
5511 cp_lexer_consume_token (parser->lexer);
5512 /* Parse the operand. */
5513 operand = cp_parser_sizeof_operand (parser, keyword);
5515 if (TYPE_P (operand))
5516 return cxx_sizeof_or_alignof_type (operand, op, true);
5518 return cxx_sizeof_or_alignof_expr (operand, op, true);
5522 return cp_parser_new_expression (parser);
5525 return cp_parser_delete_expression (parser);
5529 /* The saved value of the PEDANTIC flag. */
5533 /* Save away the PEDANTIC flag. */
5534 cp_parser_extension_opt (parser, &saved_pedantic);
5535 /* Parse the cast-expression. */
5536 expr = cp_parser_simple_cast_expression (parser);
5537 /* Restore the PEDANTIC flag. */
5538 pedantic = saved_pedantic;
5548 /* Consume the `__real__' or `__imag__' token. */
5549 cp_lexer_consume_token (parser->lexer);
5550 /* Parse the cast-expression. */
5551 expression = cp_parser_simple_cast_expression (parser);
5552 /* Create the complete representation. */
5553 return build_x_unary_op ((keyword == RID_REALPART
5554 ? REALPART_EXPR : IMAGPART_EXPR),
5556 tf_warning_or_error);
5565 /* Look for the `:: new' and `:: delete', which also signal the
5566 beginning of a new-expression, or delete-expression,
5567 respectively. If the next token is `::', then it might be one of
5569 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5573 /* See if the token after the `::' is one of the keywords in
5574 which we're interested. */
5575 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5576 /* If it's `new', we have a new-expression. */
5577 if (keyword == RID_NEW)
5578 return cp_parser_new_expression (parser);
5579 /* Similarly, for `delete'. */
5580 else if (keyword == RID_DELETE)
5581 return cp_parser_delete_expression (parser);
5584 /* Look for a unary operator. */
5585 unary_operator = cp_parser_unary_operator (token);
5586 /* The `++' and `--' operators can be handled similarly, even though
5587 they are not technically unary-operators in the grammar. */
5588 if (unary_operator == ERROR_MARK)
5590 if (token->type == CPP_PLUS_PLUS)
5591 unary_operator = PREINCREMENT_EXPR;
5592 else if (token->type == CPP_MINUS_MINUS)
5593 unary_operator = PREDECREMENT_EXPR;
5594 /* Handle the GNU address-of-label extension. */
5595 else if (cp_parser_allow_gnu_extensions_p (parser)
5596 && token->type == CPP_AND_AND)
5600 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5602 /* Consume the '&&' token. */
5603 cp_lexer_consume_token (parser->lexer);
5604 /* Look for the identifier. */
5605 identifier = cp_parser_identifier (parser);
5606 /* Create an expression representing the address. */
5607 expression = finish_label_address_expr (identifier, loc);
5608 if (cp_parser_non_integral_constant_expression (parser,
5609 "the address of a label"))
5610 expression = error_mark_node;
5614 if (unary_operator != ERROR_MARK)
5616 tree cast_expression;
5617 tree expression = error_mark_node;
5618 const char *non_constant_p = NULL;
5620 /* Consume the operator token. */
5621 token = cp_lexer_consume_token (parser->lexer);
5622 /* Parse the cast-expression. */
5624 = cp_parser_cast_expression (parser,
5625 unary_operator == ADDR_EXPR,
5626 /*cast_p=*/false, pidk);
5627 /* Now, build an appropriate representation. */
5628 switch (unary_operator)
5631 non_constant_p = "%<*%>";
5632 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5633 tf_warning_or_error);
5637 non_constant_p = "%<&%>";
5640 expression = build_x_unary_op (unary_operator, cast_expression,
5641 tf_warning_or_error);
5644 case PREINCREMENT_EXPR:
5645 case PREDECREMENT_EXPR:
5646 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5647 ? "%<++%>" : "%<--%>");
5649 case UNARY_PLUS_EXPR:
5651 case TRUTH_NOT_EXPR:
5652 expression = finish_unary_op_expr (unary_operator, cast_expression);
5660 && cp_parser_non_integral_constant_expression (parser,
5662 expression = error_mark_node;
5667 return cp_parser_postfix_expression (parser, address_p, cast_p,
5668 /*member_access_only_p=*/false,
5672 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5673 unary-operator, the corresponding tree code is returned. */
5675 static enum tree_code
5676 cp_parser_unary_operator (cp_token* token)
5678 switch (token->type)
5681 return INDIRECT_REF;
5687 return UNARY_PLUS_EXPR;
5693 return TRUTH_NOT_EXPR;
5696 return BIT_NOT_EXPR;
5703 /* Parse a new-expression.
5706 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5707 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5709 Returns a representation of the expression. */
5712 cp_parser_new_expression (cp_parser* parser)
5714 bool global_scope_p;
5715 VEC(tree,gc) *placement;
5717 VEC(tree,gc) *initializer;
5721 /* Look for the optional `::' operator. */
5723 = (cp_parser_global_scope_opt (parser,
5724 /*current_scope_valid_p=*/false)
5726 /* Look for the `new' operator. */
5727 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5728 /* There's no easy way to tell a new-placement from the
5729 `( type-id )' construct. */
5730 cp_parser_parse_tentatively (parser);
5731 /* Look for a new-placement. */
5732 placement = cp_parser_new_placement (parser);
5733 /* If that didn't work out, there's no new-placement. */
5734 if (!cp_parser_parse_definitely (parser))
5736 if (placement != NULL)
5737 release_tree_vector (placement);
5741 /* If the next token is a `(', then we have a parenthesized
5743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5746 /* Consume the `('. */
5747 cp_lexer_consume_token (parser->lexer);
5748 /* Parse the type-id. */
5749 type = cp_parser_type_id (parser);
5750 /* Look for the closing `)'. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5752 token = cp_lexer_peek_token (parser->lexer);
5753 /* There should not be a direct-new-declarator in this production,
5754 but GCC used to allowed this, so we check and emit a sensible error
5755 message for this case. */
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5758 error_at (token->location,
5759 "array bound forbidden after parenthesized type-id");
5760 inform (token->location,
5761 "try removing the parentheses around the type-id");
5762 cp_parser_direct_new_declarator (parser);
5766 /* Otherwise, there must be a new-type-id. */
5768 type = cp_parser_new_type_id (parser, &nelts);
5770 /* If the next token is a `(' or '{', then we have a new-initializer. */
5771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5772 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5773 initializer = cp_parser_new_initializer (parser);
5777 /* A new-expression may not appear in an integral constant
5779 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5780 ret = error_mark_node;
5783 /* Create a representation of the new-expression. */
5784 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5785 tf_warning_or_error);
5788 if (placement != NULL)
5789 release_tree_vector (placement);
5790 if (initializer != NULL)
5791 release_tree_vector (initializer);
5796 /* Parse a new-placement.
5801 Returns the same representation as for an expression-list. */
5803 static VEC(tree,gc) *
5804 cp_parser_new_placement (cp_parser* parser)
5806 VEC(tree,gc) *expression_list;
5808 /* Parse the expression-list. */
5809 expression_list = (cp_parser_parenthesized_expression_list
5810 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5811 /*non_constant_p=*/NULL));
5813 return expression_list;
5816 /* Parse a new-type-id.
5819 type-specifier-seq new-declarator [opt]
5821 Returns the TYPE allocated. If the new-type-id indicates an array
5822 type, *NELTS is set to the number of elements in the last array
5823 bound; the TYPE will not include the last array bound. */
5826 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5828 cp_decl_specifier_seq type_specifier_seq;
5829 cp_declarator *new_declarator;
5830 cp_declarator *declarator;
5831 cp_declarator *outer_declarator;
5832 const char *saved_message;
5835 /* The type-specifier sequence must not contain type definitions.
5836 (It cannot contain declarations of new types either, but if they
5837 are not definitions we will catch that because they are not
5839 saved_message = parser->type_definition_forbidden_message;
5840 parser->type_definition_forbidden_message
5841 = G_("types may not be defined in a new-type-id");
5842 /* Parse the type-specifier-seq. */
5843 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5844 /*is_trailing_return=*/false,
5845 &type_specifier_seq);
5846 /* Restore the old message. */
5847 parser->type_definition_forbidden_message = saved_message;
5848 /* Parse the new-declarator. */
5849 new_declarator = cp_parser_new_declarator_opt (parser);
5851 /* Determine the number of elements in the last array dimension, if
5854 /* Skip down to the last array dimension. */
5855 declarator = new_declarator;
5856 outer_declarator = NULL;
5857 while (declarator && (declarator->kind == cdk_pointer
5858 || declarator->kind == cdk_ptrmem))
5860 outer_declarator = declarator;
5861 declarator = declarator->declarator;
5864 && declarator->kind == cdk_array
5865 && declarator->declarator
5866 && declarator->declarator->kind == cdk_array)
5868 outer_declarator = declarator;
5869 declarator = declarator->declarator;
5872 if (declarator && declarator->kind == cdk_array)
5874 *nelts = declarator->u.array.bounds;
5875 if (*nelts == error_mark_node)
5876 *nelts = integer_one_node;
5878 if (outer_declarator)
5879 outer_declarator->declarator = declarator->declarator;
5881 new_declarator = NULL;
5884 type = groktypename (&type_specifier_seq, new_declarator, false);
5888 /* Parse an (optional) new-declarator.
5891 ptr-operator new-declarator [opt]
5892 direct-new-declarator
5894 Returns the declarator. */
5896 static cp_declarator *
5897 cp_parser_new_declarator_opt (cp_parser* parser)
5899 enum tree_code code;
5901 cp_cv_quals cv_quals;
5903 /* We don't know if there's a ptr-operator next, or not. */
5904 cp_parser_parse_tentatively (parser);
5905 /* Look for a ptr-operator. */
5906 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5907 /* If that worked, look for more new-declarators. */
5908 if (cp_parser_parse_definitely (parser))
5910 cp_declarator *declarator;
5912 /* Parse another optional declarator. */
5913 declarator = cp_parser_new_declarator_opt (parser);
5915 return cp_parser_make_indirect_declarator
5916 (code, type, cv_quals, declarator);
5919 /* If the next token is a `[', there is a direct-new-declarator. */
5920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5921 return cp_parser_direct_new_declarator (parser);
5926 /* Parse a direct-new-declarator.
5928 direct-new-declarator:
5930 direct-new-declarator [constant-expression]
5934 static cp_declarator *
5935 cp_parser_direct_new_declarator (cp_parser* parser)
5937 cp_declarator *declarator = NULL;
5943 /* Look for the opening `['. */
5944 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5945 /* The first expression is not required to be constant. */
5948 cp_token *token = cp_lexer_peek_token (parser->lexer);
5949 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5950 /* The standard requires that the expression have integral
5951 type. DR 74 adds enumeration types. We believe that the
5952 real intent is that these expressions be handled like the
5953 expression in a `switch' condition, which also allows
5954 classes with a single conversion to integral or
5955 enumeration type. */
5956 if (!processing_template_decl)
5959 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5964 error_at (token->location,
5965 "expression in new-declarator must have integral "
5966 "or enumeration type");
5967 expression = error_mark_node;
5971 /* But all the other expressions must be. */
5974 = cp_parser_constant_expression (parser,
5975 /*allow_non_constant=*/false,
5977 /* Look for the closing `]'. */
5978 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5980 /* Add this bound to the declarator. */
5981 declarator = make_array_declarator (declarator, expression);
5983 /* If the next token is not a `[', then there are no more
5985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5992 /* Parse a new-initializer.
5995 ( expression-list [opt] )
5998 Returns a representation of the expression-list. */
6000 static VEC(tree,gc) *
6001 cp_parser_new_initializer (cp_parser* parser)
6003 VEC(tree,gc) *expression_list;
6005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6008 bool expr_non_constant_p;
6009 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6010 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6011 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6012 expression_list = make_tree_vector_single (t);
6015 expression_list = (cp_parser_parenthesized_expression_list
6016 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6017 /*non_constant_p=*/NULL));
6019 return expression_list;
6022 /* Parse a delete-expression.
6025 :: [opt] delete cast-expression
6026 :: [opt] delete [ ] cast-expression
6028 Returns a representation of the expression. */
6031 cp_parser_delete_expression (cp_parser* parser)
6033 bool global_scope_p;
6037 /* Look for the optional `::' operator. */
6039 = (cp_parser_global_scope_opt (parser,
6040 /*current_scope_valid_p=*/false)
6042 /* Look for the `delete' keyword. */
6043 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6044 /* See if the array syntax is in use. */
6045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6047 /* Consume the `[' token. */
6048 cp_lexer_consume_token (parser->lexer);
6049 /* Look for the `]' token. */
6050 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6051 /* Remember that this is the `[]' construct. */
6057 /* Parse the cast-expression. */
6058 expression = cp_parser_simple_cast_expression (parser);
6060 /* A delete-expression may not appear in an integral constant
6062 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6063 return error_mark_node;
6065 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6068 /* Returns true if TOKEN may start a cast-expression and false
6072 cp_parser_token_starts_cast_expression (cp_token *token)
6074 switch (token->type)
6080 case CPP_CLOSE_SQUARE:
6081 case CPP_CLOSE_PAREN:
6082 case CPP_CLOSE_BRACE:
6086 case CPP_DEREF_STAR:
6094 case CPP_GREATER_EQ:
6114 /* '[' may start a primary-expression in obj-c++. */
6115 case CPP_OPEN_SQUARE:
6116 return c_dialect_objc ();
6123 /* Parse a cast-expression.
6127 ( type-id ) cast-expression
6129 ADDRESS_P is true iff the unary-expression is appearing as the
6130 operand of the `&' operator. CAST_P is true if this expression is
6131 the target of a cast.
6133 Returns a representation of the expression. */
6136 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6139 /* If it's a `(', then we might be looking at a cast. */
6140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6142 tree type = NULL_TREE;
6143 tree expr = NULL_TREE;
6144 bool compound_literal_p;
6145 const char *saved_message;
6147 /* There's no way to know yet whether or not this is a cast.
6148 For example, `(int (3))' is a unary-expression, while `(int)
6149 3' is a cast. So, we resort to parsing tentatively. */
6150 cp_parser_parse_tentatively (parser);
6151 /* Types may not be defined in a cast. */
6152 saved_message = parser->type_definition_forbidden_message;
6153 parser->type_definition_forbidden_message
6154 = G_("types may not be defined in casts");
6155 /* Consume the `('. */
6156 cp_lexer_consume_token (parser->lexer);
6157 /* A very tricky bit is that `(struct S) { 3 }' is a
6158 compound-literal (which we permit in C++ as an extension).
6159 But, that construct is not a cast-expression -- it is a
6160 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6161 is legal; if the compound-literal were a cast-expression,
6162 you'd need an extra set of parentheses.) But, if we parse
6163 the type-id, and it happens to be a class-specifier, then we
6164 will commit to the parse at that point, because we cannot
6165 undo the action that is done when creating a new class. So,
6166 then we cannot back up and do a postfix-expression.
6168 Therefore, we scan ahead to the closing `)', and check to see
6169 if the token after the `)' is a `{'. If so, we are not
6170 looking at a cast-expression.
6172 Save tokens so that we can put them back. */
6173 cp_lexer_save_tokens (parser->lexer);
6174 /* Skip tokens until the next token is a closing parenthesis.
6175 If we find the closing `)', and the next token is a `{', then
6176 we are looking at a compound-literal. */
6178 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6179 /*consume_paren=*/true)
6180 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6181 /* Roll back the tokens we skipped. */
6182 cp_lexer_rollback_tokens (parser->lexer);
6183 /* If we were looking at a compound-literal, simulate an error
6184 so that the call to cp_parser_parse_definitely below will
6186 if (compound_literal_p)
6187 cp_parser_simulate_error (parser);
6190 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6191 parser->in_type_id_in_expr_p = true;
6192 /* Look for the type-id. */
6193 type = cp_parser_type_id (parser);
6194 /* Look for the closing `)'. */
6195 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6196 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6199 /* Restore the saved message. */
6200 parser->type_definition_forbidden_message = saved_message;
6202 /* At this point this can only be either a cast or a
6203 parenthesized ctor such as `(T ())' that looks like a cast to
6204 function returning T. */
6205 if (!cp_parser_error_occurred (parser)
6206 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6209 cp_parser_parse_definitely (parser);
6210 expr = cp_parser_cast_expression (parser,
6211 /*address_p=*/false,
6212 /*cast_p=*/true, pidk);
6214 /* Warn about old-style casts, if so requested. */
6215 if (warn_old_style_cast
6216 && !in_system_header
6217 && !VOID_TYPE_P (type)
6218 && current_lang_name != lang_name_c)
6219 warning (OPT_Wold_style_cast, "use of old-style cast");
6221 /* Only type conversions to integral or enumeration types
6222 can be used in constant-expressions. */
6223 if (!cast_valid_in_integral_constant_expression_p (type)
6224 && (cp_parser_non_integral_constant_expression
6226 "a cast to a type other than an integral or "
6227 "enumeration type")))
6228 return error_mark_node;
6230 /* Perform the cast. */
6231 expr = build_c_cast (input_location, type, expr);
6235 cp_parser_abort_tentative_parse (parser);
6238 /* If we get here, then it's not a cast, so it must be a
6239 unary-expression. */
6240 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6243 /* Parse a binary expression of the general form:
6247 pm-expression .* cast-expression
6248 pm-expression ->* cast-expression
6250 multiplicative-expression:
6252 multiplicative-expression * pm-expression
6253 multiplicative-expression / pm-expression
6254 multiplicative-expression % pm-expression
6256 additive-expression:
6257 multiplicative-expression
6258 additive-expression + multiplicative-expression
6259 additive-expression - multiplicative-expression
6263 shift-expression << additive-expression
6264 shift-expression >> additive-expression
6266 relational-expression:
6268 relational-expression < shift-expression
6269 relational-expression > shift-expression
6270 relational-expression <= shift-expression
6271 relational-expression >= shift-expression
6275 relational-expression:
6276 relational-expression <? shift-expression
6277 relational-expression >? shift-expression
6279 equality-expression:
6280 relational-expression
6281 equality-expression == relational-expression
6282 equality-expression != relational-expression
6286 and-expression & equality-expression
6288 exclusive-or-expression:
6290 exclusive-or-expression ^ and-expression
6292 inclusive-or-expression:
6293 exclusive-or-expression
6294 inclusive-or-expression | exclusive-or-expression
6296 logical-and-expression:
6297 inclusive-or-expression
6298 logical-and-expression && inclusive-or-expression
6300 logical-or-expression:
6301 logical-and-expression
6302 logical-or-expression || logical-and-expression
6304 All these are implemented with a single function like:
6307 simple-cast-expression
6308 binary-expression <token> binary-expression
6310 CAST_P is true if this expression is the target of a cast.
6312 The binops_by_token map is used to get the tree codes for each <token> type.
6313 binary-expressions are associated according to a precedence table. */
6315 #define TOKEN_PRECEDENCE(token) \
6316 (((token->type == CPP_GREATER \
6317 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6318 && !parser->greater_than_is_operator_p) \
6319 ? PREC_NOT_OPERATOR \
6320 : binops_by_token[token->type].prec)
6323 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6324 bool no_toplevel_fold_p,
6325 enum cp_parser_prec prec,
6328 cp_parser_expression_stack stack;
6329 cp_parser_expression_stack_entry *sp = &stack[0];
6332 enum tree_code tree_type, lhs_type, rhs_type;
6333 enum cp_parser_prec new_prec, lookahead_prec;
6336 /* Parse the first expression. */
6337 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6338 lhs_type = ERROR_MARK;
6342 /* Get an operator token. */
6343 token = cp_lexer_peek_token (parser->lexer);
6345 if (warn_cxx0x_compat
6346 && token->type == CPP_RSHIFT
6347 && !parser->greater_than_is_operator_p)
6349 if (warning_at (token->location, OPT_Wc__0x_compat,
6350 "%<>>%> operator will be treated as"
6351 " two right angle brackets in C++0x"))
6352 inform (token->location,
6353 "suggest parentheses around %<>>%> expression");
6356 new_prec = TOKEN_PRECEDENCE (token);
6358 /* Popping an entry off the stack means we completed a subexpression:
6359 - either we found a token which is not an operator (`>' where it is not
6360 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6361 will happen repeatedly;
6362 - or, we found an operator which has lower priority. This is the case
6363 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6365 if (new_prec <= prec)
6374 tree_type = binops_by_token[token->type].tree_type;
6376 /* We used the operator token. */
6377 cp_lexer_consume_token (parser->lexer);
6379 /* For "false && x" or "true || x", x will never be executed;
6380 disable warnings while evaluating it. */
6381 if (tree_type == TRUTH_ANDIF_EXPR)
6382 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6383 else if (tree_type == TRUTH_ORIF_EXPR)
6384 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6386 /* Extract another operand. It may be the RHS of this expression
6387 or the LHS of a new, higher priority expression. */
6388 rhs = cp_parser_simple_cast_expression (parser);
6389 rhs_type = ERROR_MARK;
6391 /* Get another operator token. Look up its precedence to avoid
6392 building a useless (immediately popped) stack entry for common
6393 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6394 token = cp_lexer_peek_token (parser->lexer);
6395 lookahead_prec = TOKEN_PRECEDENCE (token);
6396 if (lookahead_prec > new_prec)
6398 /* ... and prepare to parse the RHS of the new, higher priority
6399 expression. Since precedence levels on the stack are
6400 monotonically increasing, we do not have to care about
6403 sp->tree_type = tree_type;
6405 sp->lhs_type = lhs_type;
6408 lhs_type = rhs_type;
6410 new_prec = lookahead_prec;
6414 lookahead_prec = new_prec;
6415 /* If the stack is not empty, we have parsed into LHS the right side
6416 (`4' in the example above) of an expression we had suspended.
6417 We can use the information on the stack to recover the LHS (`3')
6418 from the stack together with the tree code (`MULT_EXPR'), and
6419 the precedence of the higher level subexpression
6420 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6421 which will be used to actually build the additive expression. */
6424 tree_type = sp->tree_type;
6426 rhs_type = lhs_type;
6428 lhs_type = sp->lhs_type;
6431 /* Undo the disabling of warnings done above. */
6432 if (tree_type == TRUTH_ANDIF_EXPR)
6433 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6434 else if (tree_type == TRUTH_ORIF_EXPR)
6435 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6437 overloaded_p = false;
6438 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6439 ERROR_MARK for everything that is not a binary expression.
6440 This makes warn_about_parentheses miss some warnings that
6441 involve unary operators. For unary expressions we should
6442 pass the correct tree_code unless the unary expression was
6443 surrounded by parentheses.
6445 if (no_toplevel_fold_p
6446 && lookahead_prec <= prec
6448 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6449 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6451 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6452 &overloaded_p, tf_warning_or_error);
6453 lhs_type = tree_type;
6455 /* If the binary operator required the use of an overloaded operator,
6456 then this expression cannot be an integral constant-expression.
6457 An overloaded operator can be used even if both operands are
6458 otherwise permissible in an integral constant-expression if at
6459 least one of the operands is of enumeration type. */
6462 && (cp_parser_non_integral_constant_expression
6463 (parser, "calls to overloaded operators")))
6464 return error_mark_node;
6471 /* Parse the `? expression : assignment-expression' part of a
6472 conditional-expression. The LOGICAL_OR_EXPR is the
6473 logical-or-expression that started the conditional-expression.
6474 Returns a representation of the entire conditional-expression.
6476 This routine is used by cp_parser_assignment_expression.
6478 ? expression : assignment-expression
6482 ? : assignment-expression */
6485 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6488 tree assignment_expr;
6490 /* Consume the `?' token. */
6491 cp_lexer_consume_token (parser->lexer);
6492 if (cp_parser_allow_gnu_extensions_p (parser)
6493 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6495 /* Implicit true clause. */
6497 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6501 /* Parse the expression. */
6502 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6503 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6504 c_inhibit_evaluation_warnings +=
6505 ((logical_or_expr == truthvalue_true_node)
6506 - (logical_or_expr == truthvalue_false_node));
6509 /* The next token should be a `:'. */
6510 cp_parser_require (parser, CPP_COLON, "%<:%>");
6511 /* Parse the assignment-expression. */
6512 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6513 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6515 /* Build the conditional-expression. */
6516 return build_x_conditional_expr (logical_or_expr,
6519 tf_warning_or_error);
6522 /* Parse an assignment-expression.
6524 assignment-expression:
6525 conditional-expression
6526 logical-or-expression assignment-operator assignment_expression
6529 CAST_P is true if this expression is the target of a cast.
6531 Returns a representation for the expression. */
6534 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6539 /* If the next token is the `throw' keyword, then we're looking at
6540 a throw-expression. */
6541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6542 expr = cp_parser_throw_expression (parser);
6543 /* Otherwise, it must be that we are looking at a
6544 logical-or-expression. */
6547 /* Parse the binary expressions (logical-or-expression). */
6548 expr = cp_parser_binary_expression (parser, cast_p, false,
6549 PREC_NOT_OPERATOR, pidk);
6550 /* If the next token is a `?' then we're actually looking at a
6551 conditional-expression. */
6552 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6553 return cp_parser_question_colon_clause (parser, expr);
6556 enum tree_code assignment_operator;
6558 /* If it's an assignment-operator, we're using the second
6561 = cp_parser_assignment_operator_opt (parser);
6562 if (assignment_operator != ERROR_MARK)
6564 bool non_constant_p;
6566 /* Parse the right-hand side of the assignment. */
6567 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6569 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6572 /* An assignment may not appear in a
6573 constant-expression. */
6574 if (cp_parser_non_integral_constant_expression (parser,
6576 return error_mark_node;
6577 /* Build the assignment expression. */
6578 expr = build_x_modify_expr (expr,
6579 assignment_operator,
6581 tf_warning_or_error);
6589 /* Parse an (optional) assignment-operator.
6591 assignment-operator: one of
6592 = *= /= %= += -= >>= <<= &= ^= |=
6596 assignment-operator: one of
6599 If the next token is an assignment operator, the corresponding tree
6600 code is returned, and the token is consumed. For example, for
6601 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6602 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6603 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6604 operator, ERROR_MARK is returned. */
6606 static enum tree_code
6607 cp_parser_assignment_operator_opt (cp_parser* parser)
6612 /* Peek at the next token. */
6613 token = cp_lexer_peek_token (parser->lexer);
6615 switch (token->type)
6626 op = TRUNC_DIV_EXPR;
6630 op = TRUNC_MOD_EXPR;
6662 /* Nothing else is an assignment operator. */
6666 /* If it was an assignment operator, consume it. */
6667 if (op != ERROR_MARK)
6668 cp_lexer_consume_token (parser->lexer);
6673 /* Parse an expression.
6676 assignment-expression
6677 expression , assignment-expression
6679 CAST_P is true if this expression is the target of a cast.
6681 Returns a representation of the expression. */
6684 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6686 tree expression = NULL_TREE;
6690 tree assignment_expression;
6692 /* Parse the next assignment-expression. */
6693 assignment_expression
6694 = cp_parser_assignment_expression (parser, cast_p, pidk);
6695 /* If this is the first assignment-expression, we can just
6698 expression = assignment_expression;
6700 expression = build_x_compound_expr (expression,
6701 assignment_expression,
6702 tf_warning_or_error);
6703 /* If the next token is not a comma, then we are done with the
6705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6707 /* Consume the `,'. */
6708 cp_lexer_consume_token (parser->lexer);
6709 /* A comma operator cannot appear in a constant-expression. */
6710 if (cp_parser_non_integral_constant_expression (parser,
6711 "a comma operator"))
6712 expression = error_mark_node;
6718 /* Parse a constant-expression.
6720 constant-expression:
6721 conditional-expression
6723 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6724 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6725 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6726 is false, NON_CONSTANT_P should be NULL. */
6729 cp_parser_constant_expression (cp_parser* parser,
6730 bool allow_non_constant_p,
6731 bool *non_constant_p)
6733 bool saved_integral_constant_expression_p;
6734 bool saved_allow_non_integral_constant_expression_p;
6735 bool saved_non_integral_constant_expression_p;
6738 /* It might seem that we could simply parse the
6739 conditional-expression, and then check to see if it were
6740 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6741 one that the compiler can figure out is constant, possibly after
6742 doing some simplifications or optimizations. The standard has a
6743 precise definition of constant-expression, and we must honor
6744 that, even though it is somewhat more restrictive.
6750 is not a legal declaration, because `(2, 3)' is not a
6751 constant-expression. The `,' operator is forbidden in a
6752 constant-expression. However, GCC's constant-folding machinery
6753 will fold this operation to an INTEGER_CST for `3'. */
6755 /* Save the old settings. */
6756 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6757 saved_allow_non_integral_constant_expression_p
6758 = parser->allow_non_integral_constant_expression_p;
6759 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6760 /* We are now parsing a constant-expression. */
6761 parser->integral_constant_expression_p = true;
6762 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6763 parser->non_integral_constant_expression_p = false;
6764 /* Although the grammar says "conditional-expression", we parse an
6765 "assignment-expression", which also permits "throw-expression"
6766 and the use of assignment operators. In the case that
6767 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6768 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6769 actually essential that we look for an assignment-expression.
6770 For example, cp_parser_initializer_clauses uses this function to
6771 determine whether a particular assignment-expression is in fact
6773 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6774 /* Restore the old settings. */
6775 parser->integral_constant_expression_p
6776 = saved_integral_constant_expression_p;
6777 parser->allow_non_integral_constant_expression_p
6778 = saved_allow_non_integral_constant_expression_p;
6779 if (allow_non_constant_p)
6780 *non_constant_p = parser->non_integral_constant_expression_p;
6781 else if (parser->non_integral_constant_expression_p)
6782 expression = error_mark_node;
6783 parser->non_integral_constant_expression_p
6784 = saved_non_integral_constant_expression_p;
6789 /* Parse __builtin_offsetof.
6791 offsetof-expression:
6792 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6794 offsetof-member-designator:
6796 | offsetof-member-designator "." id-expression
6797 | offsetof-member-designator "[" expression "]"
6798 | offsetof-member-designator "->" id-expression */
6801 cp_parser_builtin_offsetof (cp_parser *parser)
6803 int save_ice_p, save_non_ice_p;
6808 /* We're about to accept non-integral-constant things, but will
6809 definitely yield an integral constant expression. Save and
6810 restore these values around our local parsing. */
6811 save_ice_p = parser->integral_constant_expression_p;
6812 save_non_ice_p = parser->non_integral_constant_expression_p;
6814 /* Consume the "__builtin_offsetof" token. */
6815 cp_lexer_consume_token (parser->lexer);
6816 /* Consume the opening `('. */
6817 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6818 /* Parse the type-id. */
6819 type = cp_parser_type_id (parser);
6820 /* Look for the `,'. */
6821 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6822 token = cp_lexer_peek_token (parser->lexer);
6824 /* Build the (type *)null that begins the traditional offsetof macro. */
6825 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6826 tf_warning_or_error);
6828 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6829 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6830 true, &dummy, token->location);
6833 token = cp_lexer_peek_token (parser->lexer);
6834 switch (token->type)
6836 case CPP_OPEN_SQUARE:
6837 /* offsetof-member-designator "[" expression "]" */
6838 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6842 /* offsetof-member-designator "->" identifier */
6843 expr = grok_array_decl (expr, integer_zero_node);
6847 /* offsetof-member-designator "." identifier */
6848 cp_lexer_consume_token (parser->lexer);
6849 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6854 case CPP_CLOSE_PAREN:
6855 /* Consume the ")" token. */
6856 cp_lexer_consume_token (parser->lexer);
6860 /* Error. We know the following require will fail, but
6861 that gives the proper error message. */
6862 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6864 expr = error_mark_node;
6870 /* If we're processing a template, we can't finish the semantics yet.
6871 Otherwise we can fold the entire expression now. */
6872 if (processing_template_decl)
6873 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6875 expr = finish_offsetof (expr);
6878 parser->integral_constant_expression_p = save_ice_p;
6879 parser->non_integral_constant_expression_p = save_non_ice_p;
6884 /* Parse a trait expression. */
6887 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6890 tree type1, type2 = NULL_TREE;
6891 bool binary = false;
6892 cp_decl_specifier_seq decl_specs;
6896 case RID_HAS_NOTHROW_ASSIGN:
6897 kind = CPTK_HAS_NOTHROW_ASSIGN;
6899 case RID_HAS_NOTHROW_CONSTRUCTOR:
6900 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6902 case RID_HAS_NOTHROW_COPY:
6903 kind = CPTK_HAS_NOTHROW_COPY;
6905 case RID_HAS_TRIVIAL_ASSIGN:
6906 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6908 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6909 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6911 case RID_HAS_TRIVIAL_COPY:
6912 kind = CPTK_HAS_TRIVIAL_COPY;
6914 case RID_HAS_TRIVIAL_DESTRUCTOR:
6915 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6917 case RID_HAS_VIRTUAL_DESTRUCTOR:
6918 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6920 case RID_IS_ABSTRACT:
6921 kind = CPTK_IS_ABSTRACT;
6923 case RID_IS_BASE_OF:
6924 kind = CPTK_IS_BASE_OF;
6928 kind = CPTK_IS_CLASS;
6930 case RID_IS_CONVERTIBLE_TO:
6931 kind = CPTK_IS_CONVERTIBLE_TO;
6935 kind = CPTK_IS_EMPTY;
6938 kind = CPTK_IS_ENUM;
6943 case RID_IS_POLYMORPHIC:
6944 kind = CPTK_IS_POLYMORPHIC;
6946 case RID_IS_STD_LAYOUT:
6947 kind = CPTK_IS_STD_LAYOUT;
6949 case RID_IS_TRIVIAL:
6950 kind = CPTK_IS_TRIVIAL;
6953 kind = CPTK_IS_UNION;
6959 /* Consume the token. */
6960 cp_lexer_consume_token (parser->lexer);
6962 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6964 type1 = cp_parser_type_id (parser);
6966 if (type1 == error_mark_node)
6967 return error_mark_node;
6969 /* Build a trivial decl-specifier-seq. */
6970 clear_decl_specs (&decl_specs);
6971 decl_specs.type = type1;
6973 /* Call grokdeclarator to figure out what type this is. */
6974 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6975 /*initialized=*/0, /*attrlist=*/NULL);
6979 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6981 type2 = cp_parser_type_id (parser);
6983 if (type2 == error_mark_node)
6984 return error_mark_node;
6986 /* Build a trivial decl-specifier-seq. */
6987 clear_decl_specs (&decl_specs);
6988 decl_specs.type = type2;
6990 /* Call grokdeclarator to figure out what type this is. */
6991 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6992 /*initialized=*/0, /*attrlist=*/NULL);
6995 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6997 /* Complete the trait expression, which may mean either processing
6998 the trait expr now or saving it for template instantiation. */
6999 return finish_trait_expr (kind, type1, type2);
7002 /* Lambdas that appear in variable initializer or default argument scope
7003 get that in their mangling, so we need to record it. We might as well
7004 use the count for function and namespace scopes as well. */
7005 static GTY(()) tree lambda_scope;
7006 static GTY(()) int lambda_count;
7007 typedef struct GTY(()) tree_int
7012 DEF_VEC_O(tree_int);
7013 DEF_VEC_ALLOC_O(tree_int,gc);
7014 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7017 start_lambda_scope (tree decl)
7021 /* Once we're inside a function, we ignore other scopes and just push
7022 the function again so that popping works properly. */
7023 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7024 decl = current_function_decl;
7025 ti.t = lambda_scope;
7026 ti.i = lambda_count;
7027 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7028 if (lambda_scope != decl)
7030 /* Don't reset the count if we're still in the same function. */
7031 lambda_scope = decl;
7037 record_lambda_scope (tree lambda)
7039 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7040 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7044 finish_lambda_scope (void)
7046 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7047 if (lambda_scope != p->t)
7049 lambda_scope = p->t;
7050 lambda_count = p->i;
7052 VEC_pop (tree_int, lambda_scope_stack);
7055 /* Parse a lambda expression.
7058 lambda-introducer lambda-declarator [opt] compound-statement
7060 Returns a representation of the expression. */
7063 cp_parser_lambda_expression (cp_parser* parser)
7065 tree lambda_expr = build_lambda_expr ();
7068 LAMBDA_EXPR_LOCATION (lambda_expr)
7069 = cp_lexer_peek_token (parser->lexer)->location;
7071 /* We may be in the middle of deferred access check. Disable
7073 push_deferring_access_checks (dk_no_deferred);
7075 type = begin_lambda_type (lambda_expr);
7077 record_lambda_scope (lambda_expr);
7079 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7080 determine_visibility (TYPE_NAME (type));
7083 /* Inside the class, surrounding template-parameter-lists do not apply. */
7084 unsigned int saved_num_template_parameter_lists
7085 = parser->num_template_parameter_lists;
7087 parser->num_template_parameter_lists = 0;
7089 cp_parser_lambda_introducer (parser, lambda_expr);
7091 /* By virtue of defining a local class, a lambda expression has access to
7092 the private variables of enclosing classes. */
7094 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7096 cp_parser_lambda_body (parser, lambda_expr);
7098 /* The capture list was built up in reverse order; fix that now. */
7100 tree newlist = NULL_TREE;
7103 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7106 tree field = TREE_PURPOSE (elt);
7109 next = TREE_CHAIN (elt);
7110 TREE_CHAIN (elt) = newlist;
7113 /* Also add __ to the beginning of the field name so that code
7114 outside the lambda body can't see the captured name. We could
7115 just remove the name entirely, but this is more useful for
7117 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7118 /* The 'this' capture already starts with __. */
7121 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7122 buf[1] = buf[0] = '_';
7123 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7124 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7125 DECL_NAME (field) = get_identifier (buf);
7127 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7130 maybe_add_lambda_conv_op (type);
7132 type = finish_struct (type, /*attributes=*/NULL_TREE);
7134 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7137 pop_deferring_access_checks ();
7139 return build_lambda_object (lambda_expr);
7142 /* Parse the beginning of a lambda expression.
7145 [ lambda-capture [opt] ]
7147 LAMBDA_EXPR is the current representation of the lambda expression. */
7150 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7152 /* Need commas after the first capture. */
7155 /* Eat the leading `['. */
7156 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7158 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7159 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7160 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7161 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7162 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7163 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7165 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7167 cp_lexer_consume_token (parser->lexer);
7171 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7173 cp_token* capture_token;
7175 tree capture_init_expr;
7176 cp_id_kind idk = CP_ID_KIND_NONE;
7177 bool explicit_init_p = false;
7179 enum capture_kind_type
7184 enum capture_kind_type capture_kind = BY_COPY;
7186 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7188 error ("expected end of capture-list");
7195 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7197 /* Possibly capture `this'. */
7198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7200 cp_lexer_consume_token (parser->lexer);
7201 add_capture (lambda_expr,
7202 /*id=*/get_identifier ("__this"),
7203 /*initializer=*/finish_this_expr(),
7204 /*by_reference_p=*/false,
7209 /* Remember whether we want to capture as a reference or not. */
7210 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7212 capture_kind = BY_REFERENCE;
7213 cp_lexer_consume_token (parser->lexer);
7216 /* Get the identifier. */
7217 capture_token = cp_lexer_peek_token (parser->lexer);
7218 capture_id = cp_parser_identifier (parser);
7220 if (capture_id == error_mark_node)
7221 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7222 delimiters, but I modified this to stop on unnested ']' as well. It
7223 was already changed to stop on unnested '}', so the
7224 "closing_parenthesis" name is no more misleading with my change. */
7226 cp_parser_skip_to_closing_parenthesis (parser,
7227 /*recovering=*/true,
7229 /*consume_paren=*/true);
7233 /* Find the initializer for this capture. */
7234 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7236 /* An explicit expression exists. */
7237 cp_lexer_consume_token (parser->lexer);
7238 pedwarn (input_location, OPT_pedantic,
7239 "ISO C++ does not allow initializers "
7240 "in lambda expression capture lists");
7241 capture_init_expr = cp_parser_assignment_expression (parser,
7244 explicit_init_p = true;
7248 const char* error_msg;
7250 /* Turn the identifier into an id-expression. */
7252 = cp_parser_lookup_name
7256 /*is_template=*/false,
7257 /*is_namespace=*/false,
7258 /*check_dependency=*/true,
7259 /*ambiguous_decls=*/NULL,
7260 capture_token->location);
7263 = finish_id_expression
7268 /*integral_constant_expression_p=*/false,
7269 /*allow_non_integral_constant_expression_p=*/false,
7270 /*non_integral_constant_expression_p=*/NULL,
7271 /*template_p=*/false,
7273 /*address_p=*/false,
7274 /*template_arg_p=*/false,
7276 capture_token->location);
7279 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7281 = unqualified_name_lookup_error (capture_init_expr);
7283 add_capture (lambda_expr,
7286 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7290 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7293 /* Parse the (optional) middle of a lambda expression.
7296 ( parameter-declaration-clause [opt] )
7297 attribute-specifier [opt]
7299 exception-specification [opt]
7300 lambda-return-type-clause [opt]
7302 LAMBDA_EXPR is the current representation of the lambda expression. */
7305 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7307 /* 5.1.1.4 of the standard says:
7308 If a lambda-expression does not include a lambda-declarator, it is as if
7309 the lambda-declarator were ().
7310 This means an empty parameter list, no attributes, and no exception
7312 tree param_list = void_list_node;
7313 tree attributes = NULL_TREE;
7314 tree exception_spec = NULL_TREE;
7317 /* The lambda-declarator is optional, but must begin with an opening
7318 parenthesis if present. */
7319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7321 cp_lexer_consume_token (parser->lexer);
7323 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7325 /* Parse parameters. */
7326 param_list = cp_parser_parameter_declaration_clause (parser);
7328 /* Default arguments shall not be specified in the
7329 parameter-declaration-clause of a lambda-declarator. */
7330 for (t = param_list; t; t = TREE_CHAIN (t))
7331 if (TREE_PURPOSE (t))
7332 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7333 "default argument specified for lambda parameter");
7335 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7337 attributes = cp_parser_attributes_opt (parser);
7339 /* Parse optional `mutable' keyword. */
7340 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7342 cp_lexer_consume_token (parser->lexer);
7343 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7346 /* Parse optional exception specification. */
7347 exception_spec = cp_parser_exception_specification_opt (parser);
7349 /* Parse optional trailing return type. */
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7352 cp_lexer_consume_token (parser->lexer);
7353 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7356 /* The function parameters must be in scope all the way until after the
7357 trailing-return-type in case of decltype. */
7358 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7359 pop_binding (DECL_NAME (t), t);
7364 /* Create the function call operator.
7366 Messing with declarators like this is no uglier than building up the
7367 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7370 cp_decl_specifier_seq return_type_specs;
7371 cp_declarator* declarator;
7376 clear_decl_specs (&return_type_specs);
7377 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7378 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7380 /* Maybe we will deduce the return type later, but we can use void
7381 as a placeholder return type anyways. */
7382 return_type_specs.type = void_type_node;
7384 p = obstack_alloc (&declarator_obstack, 0);
7386 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7389 quals = TYPE_UNQUALIFIED;
7390 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7391 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7393 /* A lambda with no captures has a static op() and a conversion op
7394 to function type. */
7395 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7396 error ("lambda expression with no captures declared mutable");
7397 return_type_specs.storage_class = sc_static;
7399 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7400 quals = TYPE_QUAL_CONST;
7401 declarator = make_call_declarator (declarator, param_list, quals,
7403 /*late_return_type=*/NULL_TREE);
7405 fco = grokmethod (&return_type_specs,
7408 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7409 DECL_ARTIFICIAL (fco) = 1;
7411 finish_member_declaration (fco);
7413 obstack_free (&declarator_obstack, p);
7417 /* Parse the body of a lambda expression, which is simply
7421 but which requires special handling.
7422 LAMBDA_EXPR is the current representation of the lambda expression. */
7425 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7427 bool nested = (current_function_decl != NULL_TREE);
7429 push_function_context ();
7431 /* Finish the function call operator
7433 + late_parsing_for_member
7434 + function_definition_after_declarator
7435 + ctor_initializer_opt_and_function_body */
7437 tree fco = lambda_function (lambda_expr);
7441 /* Let the front end know that we are going to be defining this
7443 start_preparsed_function (fco,
7445 SF_PRE_PARSED | SF_INCLASS_INLINE);
7447 start_lambda_scope (fco);
7448 body = begin_function_body ();
7450 /* 5.1.1.4 of the standard says:
7451 If a lambda-expression does not include a trailing-return-type, it
7452 is as if the trailing-return-type denotes the following type:
7453 * if the compound-statement is of the form
7454 { return attribute-specifier [opt] expression ; }
7455 the type of the returned expression after lvalue-to-rvalue
7456 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7457 (_conv.array_ 4.2), and function-to-pointer conversion
7459 * otherwise, void. */
7461 /* In a lambda that has neither a lambda-return-type-clause
7462 nor a deducible form, errors should be reported for return statements
7463 in the body. Since we used void as the placeholder return type, parsing
7464 the body as usual will give such desired behavior. */
7465 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7466 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7467 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7468 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7471 tree expr = NULL_TREE;
7472 cp_id_kind idk = CP_ID_KIND_NONE;
7474 /* Parse tentatively in case there's more after the initial return
7476 cp_parser_parse_tentatively (parser);
7478 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7479 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7481 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7483 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7484 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7486 if (cp_parser_parse_definitely (parser))
7488 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7490 compound_stmt = begin_compound_stmt (0);
7491 /* Will get error here if type not deduced yet. */
7492 finish_return_stmt (expr);
7493 finish_compound_stmt (compound_stmt);
7501 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7502 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7503 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7504 cp_parser_compound_stmt does not pass it. */
7505 cp_parser_function_body (parser);
7506 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7509 finish_function_body (body);
7510 finish_lambda_scope ();
7512 /* Finish the function and generate code for it if necessary. */
7513 expand_or_defer_fn (finish_function (/*inline*/2));
7517 pop_function_context();
7520 /* Statements [gram.stmt.stmt] */
7522 /* Parse a statement.
7526 expression-statement
7531 declaration-statement
7534 IN_COMPOUND is true when the statement is nested inside a
7535 cp_parser_compound_statement; this matters for certain pragmas.
7537 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7538 is a (possibly labeled) if statement which is not enclosed in braces
7539 and has an else clause. This is used to implement -Wparentheses. */
7542 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7543 bool in_compound, bool *if_p)
7547 location_t statement_location;
7552 /* There is no statement yet. */
7553 statement = NULL_TREE;
7554 /* Peek at the next token. */
7555 token = cp_lexer_peek_token (parser->lexer);
7556 /* Remember the location of the first token in the statement. */
7557 statement_location = token->location;
7558 /* If this is a keyword, then that will often determine what kind of
7559 statement we have. */
7560 if (token->type == CPP_KEYWORD)
7562 enum rid keyword = token->keyword;
7568 /* Looks like a labeled-statement with a case label.
7569 Parse the label, and then use tail recursion to parse
7571 cp_parser_label_for_labeled_statement (parser);
7576 statement = cp_parser_selection_statement (parser, if_p);
7582 statement = cp_parser_iteration_statement (parser);
7589 statement = cp_parser_jump_statement (parser);
7592 /* Objective-C++ exception-handling constructs. */
7595 case RID_AT_FINALLY:
7596 case RID_AT_SYNCHRONIZED:
7598 statement = cp_parser_objc_statement (parser);
7602 statement = cp_parser_try_block (parser);
7606 /* This must be a namespace alias definition. */
7607 cp_parser_declaration_statement (parser);
7611 /* It might be a keyword like `int' that can start a
7612 declaration-statement. */
7616 else if (token->type == CPP_NAME)
7618 /* If the next token is a `:', then we are looking at a
7619 labeled-statement. */
7620 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7621 if (token->type == CPP_COLON)
7623 /* Looks like a labeled-statement with an ordinary label.
7624 Parse the label, and then use tail recursion to parse
7626 cp_parser_label_for_labeled_statement (parser);
7630 /* Anything that starts with a `{' must be a compound-statement. */
7631 else if (token->type == CPP_OPEN_BRACE)
7632 statement = cp_parser_compound_statement (parser, NULL, false);
7633 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7634 a statement all its own. */
7635 else if (token->type == CPP_PRAGMA)
7637 /* Only certain OpenMP pragmas are attached to statements, and thus
7638 are considered statements themselves. All others are not. In
7639 the context of a compound, accept the pragma as a "statement" and
7640 return so that we can check for a close brace. Otherwise we
7641 require a real statement and must go back and read one. */
7643 cp_parser_pragma (parser, pragma_compound);
7644 else if (!cp_parser_pragma (parser, pragma_stmt))
7648 else if (token->type == CPP_EOF)
7650 cp_parser_error (parser, "expected statement");
7654 /* Everything else must be a declaration-statement or an
7655 expression-statement. Try for the declaration-statement
7656 first, unless we are looking at a `;', in which case we know that
7657 we have an expression-statement. */
7660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7662 cp_parser_parse_tentatively (parser);
7663 /* Try to parse the declaration-statement. */
7664 cp_parser_declaration_statement (parser);
7665 /* If that worked, we're done. */
7666 if (cp_parser_parse_definitely (parser))
7669 /* Look for an expression-statement instead. */
7670 statement = cp_parser_expression_statement (parser, in_statement_expr);
7673 /* Set the line number for the statement. */
7674 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7675 SET_EXPR_LOCATION (statement, statement_location);
7678 /* Parse the label for a labeled-statement, i.e.
7681 case constant-expression :
7685 case constant-expression ... constant-expression : statement
7687 When a label is parsed without errors, the label is added to the
7688 parse tree by the finish_* functions, so this function doesn't
7689 have to return the label. */
7692 cp_parser_label_for_labeled_statement (cp_parser* parser)
7695 tree label = NULL_TREE;
7697 /* The next token should be an identifier. */
7698 token = cp_lexer_peek_token (parser->lexer);
7699 if (token->type != CPP_NAME
7700 && token->type != CPP_KEYWORD)
7702 cp_parser_error (parser, "expected labeled-statement");
7706 switch (token->keyword)
7713 /* Consume the `case' token. */
7714 cp_lexer_consume_token (parser->lexer);
7715 /* Parse the constant-expression. */
7716 expr = cp_parser_constant_expression (parser,
7717 /*allow_non_constant_p=*/false,
7720 ellipsis = cp_lexer_peek_token (parser->lexer);
7721 if (ellipsis->type == CPP_ELLIPSIS)
7723 /* Consume the `...' token. */
7724 cp_lexer_consume_token (parser->lexer);
7726 cp_parser_constant_expression (parser,
7727 /*allow_non_constant_p=*/false,
7729 /* We don't need to emit warnings here, as the common code
7730 will do this for us. */
7733 expr_hi = NULL_TREE;
7735 if (parser->in_switch_statement_p)
7736 finish_case_label (token->location, expr, expr_hi);
7738 error_at (token->location,
7739 "case label %qE not within a switch statement",
7745 /* Consume the `default' token. */
7746 cp_lexer_consume_token (parser->lexer);
7748 if (parser->in_switch_statement_p)
7749 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7751 error_at (token->location, "case label not within a switch statement");
7755 /* Anything else must be an ordinary label. */
7756 label = finish_label_stmt (cp_parser_identifier (parser));
7760 /* Require the `:' token. */
7761 cp_parser_require (parser, CPP_COLON, "%<:%>");
7763 /* An ordinary label may optionally be followed by attributes.
7764 However, this is only permitted if the attributes are then
7765 followed by a semicolon. This is because, for backward
7766 compatibility, when parsing
7767 lab: __attribute__ ((unused)) int i;
7768 we want the attribute to attach to "i", not "lab". */
7769 if (label != NULL_TREE
7770 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7774 cp_parser_parse_tentatively (parser);
7775 attrs = cp_parser_attributes_opt (parser);
7776 if (attrs == NULL_TREE
7777 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7778 cp_parser_abort_tentative_parse (parser);
7779 else if (!cp_parser_parse_definitely (parser))
7782 cplus_decl_attributes (&label, attrs, 0);
7786 /* Parse an expression-statement.
7788 expression-statement:
7791 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7792 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7793 indicates whether this expression-statement is part of an
7794 expression statement. */
7797 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7799 tree statement = NULL_TREE;
7800 cp_token *token = cp_lexer_peek_token (parser->lexer);
7802 /* If the next token is a ';', then there is no expression
7804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7805 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7807 /* Give a helpful message for "A<T>::type t;" and the like. */
7808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7809 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7811 if (TREE_CODE (statement) == SCOPE_REF)
7812 error_at (token->location, "need %<typename%> before %qE because "
7813 "%qT is a dependent scope",
7814 statement, TREE_OPERAND (statement, 0));
7815 else if (is_overloaded_fn (statement)
7816 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7819 tree fn = get_first_fn (statement);
7820 error_at (token->location,
7821 "%<%T::%D%> names the constructor, not the type",
7822 DECL_CONTEXT (fn), DECL_NAME (fn));
7826 /* Consume the final `;'. */
7827 cp_parser_consume_semicolon_at_end_of_statement (parser);
7829 if (in_statement_expr
7830 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7831 /* This is the final expression statement of a statement
7833 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7835 statement = finish_expr_stmt (statement);
7842 /* Parse a compound-statement.
7845 { statement-seq [opt] }
7850 { label-declaration-seq [opt] statement-seq [opt] }
7852 label-declaration-seq:
7854 label-declaration-seq label-declaration
7856 Returns a tree representing the statement. */
7859 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7864 /* Consume the `{'. */
7865 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7866 return error_mark_node;
7867 /* Begin the compound-statement. */
7868 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7869 /* If the next keyword is `__label__' we have a label declaration. */
7870 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7871 cp_parser_label_declaration (parser);
7872 /* Parse an (optional) statement-seq. */
7873 cp_parser_statement_seq_opt (parser, in_statement_expr);
7874 /* Finish the compound-statement. */
7875 finish_compound_stmt (compound_stmt);
7876 /* Consume the `}'. */
7877 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7879 return compound_stmt;
7882 /* Parse an (optional) statement-seq.
7886 statement-seq [opt] statement */
7889 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7891 /* Scan statements until there aren't any more. */
7894 cp_token *token = cp_lexer_peek_token (parser->lexer);
7896 /* If we're looking at a `}', then we've run out of statements. */
7897 if (token->type == CPP_CLOSE_BRACE
7898 || token->type == CPP_EOF
7899 || token->type == CPP_PRAGMA_EOL)
7902 /* If we are in a compound statement and find 'else' then
7903 something went wrong. */
7904 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7906 if (parser->in_statement & IN_IF_STMT)
7910 token = cp_lexer_consume_token (parser->lexer);
7911 error_at (token->location, "%<else%> without a previous %<if%>");
7915 /* Parse the statement. */
7916 cp_parser_statement (parser, in_statement_expr, true, NULL);
7920 /* Parse a selection-statement.
7922 selection-statement:
7923 if ( condition ) statement
7924 if ( condition ) statement else statement
7925 switch ( condition ) statement
7927 Returns the new IF_STMT or SWITCH_STMT.
7929 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7930 is a (possibly labeled) if statement which is not enclosed in
7931 braces and has an else clause. This is used to implement
7935 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7943 /* Peek at the next token. */
7944 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7946 /* See what kind of keyword it is. */
7947 keyword = token->keyword;
7956 /* Look for the `('. */
7957 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7959 cp_parser_skip_to_end_of_statement (parser);
7960 return error_mark_node;
7963 /* Begin the selection-statement. */
7964 if (keyword == RID_IF)
7965 statement = begin_if_stmt ();
7967 statement = begin_switch_stmt ();
7969 /* Parse the condition. */
7970 condition = cp_parser_condition (parser);
7971 /* Look for the `)'. */
7972 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7973 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7974 /*consume_paren=*/true);
7976 if (keyword == RID_IF)
7979 unsigned char in_statement;
7981 /* Add the condition. */
7982 finish_if_stmt_cond (condition, statement);
7984 /* Parse the then-clause. */
7985 in_statement = parser->in_statement;
7986 parser->in_statement |= IN_IF_STMT;
7987 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7989 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7990 add_stmt (build_empty_stmt (loc));
7991 cp_lexer_consume_token (parser->lexer);
7992 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7993 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7994 "empty body in an %<if%> statement");
7998 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7999 parser->in_statement = in_statement;
8001 finish_then_clause (statement);
8003 /* If the next token is `else', parse the else-clause. */
8004 if (cp_lexer_next_token_is_keyword (parser->lexer,
8007 /* Consume the `else' keyword. */
8008 cp_lexer_consume_token (parser->lexer);
8009 begin_else_clause (statement);
8010 /* Parse the else-clause. */
8011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8014 loc = cp_lexer_peek_token (parser->lexer)->location;
8016 OPT_Wempty_body, "suggest braces around "
8017 "empty body in an %<else%> statement");
8018 add_stmt (build_empty_stmt (loc));
8019 cp_lexer_consume_token (parser->lexer);
8022 cp_parser_implicitly_scoped_statement (parser, NULL);
8024 finish_else_clause (statement);
8026 /* If we are currently parsing a then-clause, then
8027 IF_P will not be NULL. We set it to true to
8028 indicate that this if statement has an else clause.
8029 This may trigger the Wparentheses warning below
8030 when we get back up to the parent if statement. */
8036 /* This if statement does not have an else clause. If
8037 NESTED_IF is true, then the then-clause is an if
8038 statement which does have an else clause. We warn
8039 about the potential ambiguity. */
8041 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8042 "suggest explicit braces to avoid ambiguous"
8046 /* Now we're all done with the if-statement. */
8047 finish_if_stmt (statement);
8051 bool in_switch_statement_p;
8052 unsigned char in_statement;
8054 /* Add the condition. */
8055 finish_switch_cond (condition, statement);
8057 /* Parse the body of the switch-statement. */
8058 in_switch_statement_p = parser->in_switch_statement_p;
8059 in_statement = parser->in_statement;
8060 parser->in_switch_statement_p = true;
8061 parser->in_statement |= IN_SWITCH_STMT;
8062 cp_parser_implicitly_scoped_statement (parser, NULL);
8063 parser->in_switch_statement_p = in_switch_statement_p;
8064 parser->in_statement = in_statement;
8066 /* Now we're all done with the switch-statement. */
8067 finish_switch_stmt (statement);
8075 cp_parser_error (parser, "expected selection-statement");
8076 return error_mark_node;
8080 /* Parse a condition.
8084 type-specifier-seq declarator = initializer-clause
8085 type-specifier-seq declarator braced-init-list
8090 type-specifier-seq declarator asm-specification [opt]
8091 attributes [opt] = assignment-expression
8093 Returns the expression that should be tested. */
8096 cp_parser_condition (cp_parser* parser)
8098 cp_decl_specifier_seq type_specifiers;
8099 const char *saved_message;
8101 /* Try the declaration first. */
8102 cp_parser_parse_tentatively (parser);
8103 /* New types are not allowed in the type-specifier-seq for a
8105 saved_message = parser->type_definition_forbidden_message;
8106 parser->type_definition_forbidden_message
8107 = G_("types may not be defined in conditions");
8108 /* Parse the type-specifier-seq. */
8109 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8110 /*is_trailing_return=*/false,
8112 /* Restore the saved message. */
8113 parser->type_definition_forbidden_message = saved_message;
8114 /* If all is well, we might be looking at a declaration. */
8115 if (!cp_parser_error_occurred (parser))
8118 tree asm_specification;
8120 cp_declarator *declarator;
8121 tree initializer = NULL_TREE;
8123 /* Parse the declarator. */
8124 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8125 /*ctor_dtor_or_conv_p=*/NULL,
8126 /*parenthesized_p=*/NULL,
8127 /*member_p=*/false);
8128 /* Parse the attributes. */
8129 attributes = cp_parser_attributes_opt (parser);
8130 /* Parse the asm-specification. */
8131 asm_specification = cp_parser_asm_specification_opt (parser);
8132 /* If the next token is not an `=' or '{', then we might still be
8133 looking at an expression. For example:
8137 looks like a decl-specifier-seq and a declarator -- but then
8138 there is no `=', so this is an expression. */
8139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8140 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8141 cp_parser_simulate_error (parser);
8143 /* If we did see an `=' or '{', then we are looking at a declaration
8145 if (cp_parser_parse_definitely (parser))
8148 bool non_constant_p;
8149 bool flags = LOOKUP_ONLYCONVERTING;
8151 /* Create the declaration. */
8152 decl = start_decl (declarator, &type_specifiers,
8153 /*initialized_p=*/true,
8154 attributes, /*prefix_attributes=*/NULL_TREE,
8157 /* Parse the initializer. */
8158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8160 initializer = cp_parser_braced_list (parser, &non_constant_p);
8161 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8166 /* Consume the `='. */
8167 cp_parser_require (parser, CPP_EQ, "%<=%>");
8168 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8170 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8171 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8173 if (!non_constant_p)
8174 initializer = fold_non_dependent_expr (initializer);
8176 /* Process the initializer. */
8177 cp_finish_decl (decl,
8178 initializer, !non_constant_p,
8183 pop_scope (pushed_scope);
8185 return convert_from_reference (decl);
8188 /* If we didn't even get past the declarator successfully, we are
8189 definitely not looking at a declaration. */
8191 cp_parser_abort_tentative_parse (parser);
8193 /* Otherwise, we are looking at an expression. */
8194 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8197 /* Parse an iteration-statement.
8199 iteration-statement:
8200 while ( condition ) statement
8201 do statement while ( expression ) ;
8202 for ( for-init-statement condition [opt] ; expression [opt] )
8205 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8208 cp_parser_iteration_statement (cp_parser* parser)
8213 unsigned char in_statement;
8215 /* Peek at the next token. */
8216 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8218 return error_mark_node;
8220 /* Remember whether or not we are already within an iteration
8222 in_statement = parser->in_statement;
8224 /* See what kind of keyword it is. */
8225 keyword = token->keyword;
8232 /* Begin the while-statement. */
8233 statement = begin_while_stmt ();
8234 /* Look for the `('. */
8235 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8236 /* Parse the condition. */
8237 condition = cp_parser_condition (parser);
8238 finish_while_stmt_cond (condition, statement);
8239 /* Look for the `)'. */
8240 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8241 /* Parse the dependent statement. */
8242 parser->in_statement = IN_ITERATION_STMT;
8243 cp_parser_already_scoped_statement (parser);
8244 parser->in_statement = in_statement;
8245 /* We're done with the while-statement. */
8246 finish_while_stmt (statement);
8254 /* Begin the do-statement. */
8255 statement = begin_do_stmt ();
8256 /* Parse the body of the do-statement. */
8257 parser->in_statement = IN_ITERATION_STMT;
8258 cp_parser_implicitly_scoped_statement (parser, NULL);
8259 parser->in_statement = in_statement;
8260 finish_do_body (statement);
8261 /* Look for the `while' keyword. */
8262 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8263 /* Look for the `('. */
8264 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8265 /* Parse the expression. */
8266 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8267 /* We're done with the do-statement. */
8268 finish_do_stmt (expression, statement);
8269 /* Look for the `)'. */
8270 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8271 /* Look for the `;'. */
8272 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8278 tree condition = NULL_TREE;
8279 tree expression = NULL_TREE;
8281 /* Begin the for-statement. */
8282 statement = begin_for_stmt ();
8283 /* Look for the `('. */
8284 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8285 /* Parse the initialization. */
8286 cp_parser_for_init_statement (parser);
8287 finish_for_init_stmt (statement);
8289 /* If there's a condition, process it. */
8290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8291 condition = cp_parser_condition (parser);
8292 finish_for_cond (condition, statement);
8293 /* Look for the `;'. */
8294 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8296 /* If there's an expression, process it. */
8297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8298 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8299 finish_for_expr (expression, statement);
8300 /* Look for the `)'. */
8301 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8303 /* Parse the body of the for-statement. */
8304 parser->in_statement = IN_ITERATION_STMT;
8305 cp_parser_already_scoped_statement (parser);
8306 parser->in_statement = in_statement;
8308 /* We're done with the for-statement. */
8309 finish_for_stmt (statement);
8314 cp_parser_error (parser, "expected iteration-statement");
8315 statement = error_mark_node;
8322 /* Parse a for-init-statement.
8325 expression-statement
8326 simple-declaration */
8329 cp_parser_for_init_statement (cp_parser* parser)
8331 /* If the next token is a `;', then we have an empty
8332 expression-statement. Grammatically, this is also a
8333 simple-declaration, but an invalid one, because it does not
8334 declare anything. Therefore, if we did not handle this case
8335 specially, we would issue an error message about an invalid
8337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8339 /* We're going to speculatively look for a declaration, falling back
8340 to an expression, if necessary. */
8341 cp_parser_parse_tentatively (parser);
8342 /* Parse the declaration. */
8343 cp_parser_simple_declaration (parser,
8344 /*function_definition_allowed_p=*/false);
8345 /* If the tentative parse failed, then we shall need to look for an
8346 expression-statement. */
8347 if (cp_parser_parse_definitely (parser))
8351 cp_parser_expression_statement (parser, false);
8354 /* Parse a jump-statement.
8359 return expression [opt] ;
8360 return braced-init-list ;
8368 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8371 cp_parser_jump_statement (cp_parser* parser)
8373 tree statement = error_mark_node;
8376 unsigned char in_statement;
8378 /* Peek at the next token. */
8379 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8381 return error_mark_node;
8383 /* See what kind of keyword it is. */
8384 keyword = token->keyword;
8388 in_statement = parser->in_statement & ~IN_IF_STMT;
8389 switch (in_statement)
8392 error_at (token->location, "break statement not within loop or switch");
8395 gcc_assert ((in_statement & IN_SWITCH_STMT)
8396 || in_statement == IN_ITERATION_STMT);
8397 statement = finish_break_stmt ();
8400 error_at (token->location, "invalid exit from OpenMP structured block");
8403 error_at (token->location, "break statement used with OpenMP for loop");
8406 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8410 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8413 error_at (token->location, "continue statement not within a loop");
8415 case IN_ITERATION_STMT:
8417 statement = finish_continue_stmt ();
8420 error_at (token->location, "invalid exit from OpenMP structured block");
8425 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8431 bool expr_non_constant_p;
8433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8435 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8436 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8438 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8439 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8441 /* If the next token is a `;', then there is no
8444 /* Build the return-statement. */
8445 statement = finish_return_stmt (expr);
8446 /* Look for the final `;'. */
8447 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8452 /* Create the goto-statement. */
8453 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8455 /* Issue a warning about this use of a GNU extension. */
8456 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8457 /* Consume the '*' token. */
8458 cp_lexer_consume_token (parser->lexer);
8459 /* Parse the dependent expression. */
8460 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8463 finish_goto_stmt (cp_parser_identifier (parser));
8464 /* Look for the final `;'. */
8465 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8469 cp_parser_error (parser, "expected jump-statement");
8476 /* Parse a declaration-statement.
8478 declaration-statement:
8479 block-declaration */
8482 cp_parser_declaration_statement (cp_parser* parser)
8486 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8487 p = obstack_alloc (&declarator_obstack, 0);
8489 /* Parse the block-declaration. */
8490 cp_parser_block_declaration (parser, /*statement_p=*/true);
8492 /* Free any declarators allocated. */
8493 obstack_free (&declarator_obstack, p);
8495 /* Finish off the statement. */
8499 /* Some dependent statements (like `if (cond) statement'), are
8500 implicitly in their own scope. In other words, if the statement is
8501 a single statement (as opposed to a compound-statement), it is
8502 none-the-less treated as if it were enclosed in braces. Any
8503 declarations appearing in the dependent statement are out of scope
8504 after control passes that point. This function parses a statement,
8505 but ensures that is in its own scope, even if it is not a
8508 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8509 is a (possibly labeled) if statement which is not enclosed in
8510 braces and has an else clause. This is used to implement
8513 Returns the new statement. */
8516 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8523 /* Mark if () ; with a special NOP_EXPR. */
8524 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8526 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8527 cp_lexer_consume_token (parser->lexer);
8528 statement = add_stmt (build_empty_stmt (loc));
8530 /* if a compound is opened, we simply parse the statement directly. */
8531 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8532 statement = cp_parser_compound_statement (parser, NULL, false);
8533 /* If the token is not a `{', then we must take special action. */
8536 /* Create a compound-statement. */
8537 statement = begin_compound_stmt (0);
8538 /* Parse the dependent-statement. */
8539 cp_parser_statement (parser, NULL_TREE, false, if_p);
8540 /* Finish the dummy compound-statement. */
8541 finish_compound_stmt (statement);
8544 /* Return the statement. */
8548 /* For some dependent statements (like `while (cond) statement'), we
8549 have already created a scope. Therefore, even if the dependent
8550 statement is a compound-statement, we do not want to create another
8554 cp_parser_already_scoped_statement (cp_parser* parser)
8556 /* If the token is a `{', then we must take special action. */
8557 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8558 cp_parser_statement (parser, NULL_TREE, false, NULL);
8561 /* Avoid calling cp_parser_compound_statement, so that we
8562 don't create a new scope. Do everything else by hand. */
8563 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8564 /* If the next keyword is `__label__' we have a label declaration. */
8565 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8566 cp_parser_label_declaration (parser);
8567 /* Parse an (optional) statement-seq. */
8568 cp_parser_statement_seq_opt (parser, NULL_TREE);
8569 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8573 /* Declarations [gram.dcl.dcl] */
8575 /* Parse an optional declaration-sequence.
8579 declaration-seq declaration */
8582 cp_parser_declaration_seq_opt (cp_parser* parser)
8588 token = cp_lexer_peek_token (parser->lexer);
8590 if (token->type == CPP_CLOSE_BRACE
8591 || token->type == CPP_EOF
8592 || token->type == CPP_PRAGMA_EOL)
8595 if (token->type == CPP_SEMICOLON)
8597 /* A declaration consisting of a single semicolon is
8598 invalid. Allow it unless we're being pedantic. */
8599 cp_lexer_consume_token (parser->lexer);
8600 if (!in_system_header)
8601 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8605 /* If we're entering or exiting a region that's implicitly
8606 extern "C", modify the lang context appropriately. */
8607 if (!parser->implicit_extern_c && token->implicit_extern_c)
8609 push_lang_context (lang_name_c);
8610 parser->implicit_extern_c = true;
8612 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8614 pop_lang_context ();
8615 parser->implicit_extern_c = false;
8618 if (token->type == CPP_PRAGMA)
8620 /* A top-level declaration can consist solely of a #pragma.
8621 A nested declaration cannot, so this is done here and not
8622 in cp_parser_declaration. (A #pragma at block scope is
8623 handled in cp_parser_statement.) */
8624 cp_parser_pragma (parser, pragma_external);
8628 /* Parse the declaration itself. */
8629 cp_parser_declaration (parser);
8633 /* Parse a declaration.
8638 template-declaration
8639 explicit-instantiation
8640 explicit-specialization
8641 linkage-specification
8642 namespace-definition
8647 __extension__ declaration */
8650 cp_parser_declaration (cp_parser* parser)
8657 /* Check for the `__extension__' keyword. */
8658 if (cp_parser_extension_opt (parser, &saved_pedantic))
8660 /* Parse the qualified declaration. */
8661 cp_parser_declaration (parser);
8662 /* Restore the PEDANTIC flag. */
8663 pedantic = saved_pedantic;
8668 /* Try to figure out what kind of declaration is present. */
8669 token1 = *cp_lexer_peek_token (parser->lexer);
8671 if (token1.type != CPP_EOF)
8672 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8675 token2.type = CPP_EOF;
8676 token2.keyword = RID_MAX;
8679 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8680 p = obstack_alloc (&declarator_obstack, 0);
8682 /* If the next token is `extern' and the following token is a string
8683 literal, then we have a linkage specification. */
8684 if (token1.keyword == RID_EXTERN
8685 && cp_parser_is_string_literal (&token2))
8686 cp_parser_linkage_specification (parser);
8687 /* If the next token is `template', then we have either a template
8688 declaration, an explicit instantiation, or an explicit
8690 else if (token1.keyword == RID_TEMPLATE)
8692 /* `template <>' indicates a template specialization. */
8693 if (token2.type == CPP_LESS
8694 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8695 cp_parser_explicit_specialization (parser);
8696 /* `template <' indicates a template declaration. */
8697 else if (token2.type == CPP_LESS)
8698 cp_parser_template_declaration (parser, /*member_p=*/false);
8699 /* Anything else must be an explicit instantiation. */
8701 cp_parser_explicit_instantiation (parser);
8703 /* If the next token is `export', then we have a template
8705 else if (token1.keyword == RID_EXPORT)
8706 cp_parser_template_declaration (parser, /*member_p=*/false);
8707 /* If the next token is `extern', 'static' or 'inline' and the one
8708 after that is `template', we have a GNU extended explicit
8709 instantiation directive. */
8710 else if (cp_parser_allow_gnu_extensions_p (parser)
8711 && (token1.keyword == RID_EXTERN
8712 || token1.keyword == RID_STATIC
8713 || token1.keyword == RID_INLINE)
8714 && token2.keyword == RID_TEMPLATE)
8715 cp_parser_explicit_instantiation (parser);
8716 /* If the next token is `namespace', check for a named or unnamed
8717 namespace definition. */
8718 else if (token1.keyword == RID_NAMESPACE
8719 && (/* A named namespace definition. */
8720 (token2.type == CPP_NAME
8721 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8723 /* An unnamed namespace definition. */
8724 || token2.type == CPP_OPEN_BRACE
8725 || token2.keyword == RID_ATTRIBUTE))
8726 cp_parser_namespace_definition (parser);
8727 /* An inline (associated) namespace definition. */
8728 else if (token1.keyword == RID_INLINE
8729 && token2.keyword == RID_NAMESPACE)
8730 cp_parser_namespace_definition (parser);
8731 /* Objective-C++ declaration/definition. */
8732 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8733 cp_parser_objc_declaration (parser);
8734 /* We must have either a block declaration or a function
8737 /* Try to parse a block-declaration, or a function-definition. */
8738 cp_parser_block_declaration (parser, /*statement_p=*/false);
8740 /* Free any declarators allocated. */
8741 obstack_free (&declarator_obstack, p);
8744 /* Parse a block-declaration.
8749 namespace-alias-definition
8756 __extension__ block-declaration
8761 static_assert-declaration
8763 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8764 part of a declaration-statement. */
8767 cp_parser_block_declaration (cp_parser *parser,
8773 /* Check for the `__extension__' keyword. */
8774 if (cp_parser_extension_opt (parser, &saved_pedantic))
8776 /* Parse the qualified declaration. */
8777 cp_parser_block_declaration (parser, statement_p);
8778 /* Restore the PEDANTIC flag. */
8779 pedantic = saved_pedantic;
8784 /* Peek at the next token to figure out which kind of declaration is
8786 token1 = cp_lexer_peek_token (parser->lexer);
8788 /* If the next keyword is `asm', we have an asm-definition. */
8789 if (token1->keyword == RID_ASM)
8792 cp_parser_commit_to_tentative_parse (parser);
8793 cp_parser_asm_definition (parser);
8795 /* If the next keyword is `namespace', we have a
8796 namespace-alias-definition. */
8797 else if (token1->keyword == RID_NAMESPACE)
8798 cp_parser_namespace_alias_definition (parser);
8799 /* If the next keyword is `using', we have either a
8800 using-declaration or a using-directive. */
8801 else if (token1->keyword == RID_USING)
8806 cp_parser_commit_to_tentative_parse (parser);
8807 /* If the token after `using' is `namespace', then we have a
8809 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8810 if (token2->keyword == RID_NAMESPACE)
8811 cp_parser_using_directive (parser);
8812 /* Otherwise, it's a using-declaration. */
8814 cp_parser_using_declaration (parser,
8815 /*access_declaration_p=*/false);
8817 /* If the next keyword is `__label__' we have a misplaced label
8819 else if (token1->keyword == RID_LABEL)
8821 cp_lexer_consume_token (parser->lexer);
8822 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8823 cp_parser_skip_to_end_of_statement (parser);
8824 /* If the next token is now a `;', consume it. */
8825 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8826 cp_lexer_consume_token (parser->lexer);
8828 /* If the next token is `static_assert' we have a static assertion. */
8829 else if (token1->keyword == RID_STATIC_ASSERT)
8830 cp_parser_static_assert (parser, /*member_p=*/false);
8831 /* Anything else must be a simple-declaration. */
8833 cp_parser_simple_declaration (parser, !statement_p);
8836 /* Parse a simple-declaration.
8839 decl-specifier-seq [opt] init-declarator-list [opt] ;
8841 init-declarator-list:
8843 init-declarator-list , init-declarator
8845 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8846 function-definition as a simple-declaration. */
8849 cp_parser_simple_declaration (cp_parser* parser,
8850 bool function_definition_allowed_p)
8852 cp_decl_specifier_seq decl_specifiers;
8853 int declares_class_or_enum;
8854 bool saw_declarator;
8856 /* Defer access checks until we know what is being declared; the
8857 checks for names appearing in the decl-specifier-seq should be
8858 done as if we were in the scope of the thing being declared. */
8859 push_deferring_access_checks (dk_deferred);
8861 /* Parse the decl-specifier-seq. We have to keep track of whether
8862 or not the decl-specifier-seq declares a named class or
8863 enumeration type, since that is the only case in which the
8864 init-declarator-list is allowed to be empty.
8868 In a simple-declaration, the optional init-declarator-list can be
8869 omitted only when declaring a class or enumeration, that is when
8870 the decl-specifier-seq contains either a class-specifier, an
8871 elaborated-type-specifier, or an enum-specifier. */
8872 cp_parser_decl_specifier_seq (parser,
8873 CP_PARSER_FLAGS_OPTIONAL,
8875 &declares_class_or_enum);
8876 /* We no longer need to defer access checks. */
8877 stop_deferring_access_checks ();
8879 /* In a block scope, a valid declaration must always have a
8880 decl-specifier-seq. By not trying to parse declarators, we can
8881 resolve the declaration/expression ambiguity more quickly. */
8882 if (!function_definition_allowed_p
8883 && !decl_specifiers.any_specifiers_p)
8885 cp_parser_error (parser, "expected declaration");
8889 /* If the next two tokens are both identifiers, the code is
8890 erroneous. The usual cause of this situation is code like:
8894 where "T" should name a type -- but does not. */
8895 if (!decl_specifiers.any_type_specifiers_p
8896 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8898 /* If parsing tentatively, we should commit; we really are
8899 looking at a declaration. */
8900 cp_parser_commit_to_tentative_parse (parser);
8905 /* If we have seen at least one decl-specifier, and the next token
8906 is not a parenthesis, then we must be looking at a declaration.
8907 (After "int (" we might be looking at a functional cast.) */
8908 if (decl_specifiers.any_specifiers_p
8909 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8910 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8911 && !cp_parser_error_occurred (parser))
8912 cp_parser_commit_to_tentative_parse (parser);
8914 /* Keep going until we hit the `;' at the end of the simple
8916 saw_declarator = false;
8917 while (cp_lexer_next_token_is_not (parser->lexer,
8921 bool function_definition_p;
8926 /* If we are processing next declarator, coma is expected */
8927 token = cp_lexer_peek_token (parser->lexer);
8928 gcc_assert (token->type == CPP_COMMA);
8929 cp_lexer_consume_token (parser->lexer);
8932 saw_declarator = true;
8934 /* Parse the init-declarator. */
8935 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8937 function_definition_allowed_p,
8939 declares_class_or_enum,
8940 &function_definition_p);
8941 /* If an error occurred while parsing tentatively, exit quickly.
8942 (That usually happens when in the body of a function; each
8943 statement is treated as a declaration-statement until proven
8945 if (cp_parser_error_occurred (parser))
8947 /* Handle function definitions specially. */
8948 if (function_definition_p)
8950 /* If the next token is a `,', then we are probably
8951 processing something like:
8955 which is erroneous. */
8956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8958 cp_token *token = cp_lexer_peek_token (parser->lexer);
8959 error_at (token->location,
8961 " declarations and function-definitions is forbidden");
8963 /* Otherwise, we're done with the list of declarators. */
8966 pop_deferring_access_checks ();
8970 /* The next token should be either a `,' or a `;'. */
8971 token = cp_lexer_peek_token (parser->lexer);
8972 /* If it's a `,', there are more declarators to come. */
8973 if (token->type == CPP_COMMA)
8974 /* will be consumed next time around */;
8975 /* If it's a `;', we are done. */
8976 else if (token->type == CPP_SEMICOLON)
8978 /* Anything else is an error. */
8981 /* If we have already issued an error message we don't need
8982 to issue another one. */
8983 if (decl != error_mark_node
8984 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8985 cp_parser_error (parser, "expected %<,%> or %<;%>");
8986 /* Skip tokens until we reach the end of the statement. */
8987 cp_parser_skip_to_end_of_statement (parser);
8988 /* If the next token is now a `;', consume it. */
8989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8990 cp_lexer_consume_token (parser->lexer);
8993 /* After the first time around, a function-definition is not
8994 allowed -- even if it was OK at first. For example:
8999 function_definition_allowed_p = false;
9002 /* Issue an error message if no declarators are present, and the
9003 decl-specifier-seq does not itself declare a class or
9005 if (!saw_declarator)
9007 if (cp_parser_declares_only_class_p (parser))
9008 shadow_tag (&decl_specifiers);
9009 /* Perform any deferred access checks. */
9010 perform_deferred_access_checks ();
9013 /* Consume the `;'. */
9014 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9017 pop_deferring_access_checks ();
9020 /* Parse a decl-specifier-seq.
9023 decl-specifier-seq [opt] decl-specifier
9026 storage-class-specifier
9037 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9039 The parser flags FLAGS is used to control type-specifier parsing.
9041 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9044 1: one of the decl-specifiers is an elaborated-type-specifier
9045 (i.e., a type declaration)
9046 2: one of the decl-specifiers is an enum-specifier or a
9047 class-specifier (i.e., a type definition)
9052 cp_parser_decl_specifier_seq (cp_parser* parser,
9053 cp_parser_flags flags,
9054 cp_decl_specifier_seq *decl_specs,
9055 int* declares_class_or_enum)
9057 bool constructor_possible_p = !parser->in_declarator_p;
9058 cp_token *start_token = NULL;
9060 /* Clear DECL_SPECS. */
9061 clear_decl_specs (decl_specs);
9063 /* Assume no class or enumeration type is declared. */
9064 *declares_class_or_enum = 0;
9066 /* Keep reading specifiers until there are no more to read. */
9070 bool found_decl_spec;
9073 /* Peek at the next token. */
9074 token = cp_lexer_peek_token (parser->lexer);
9076 /* Save the first token of the decl spec list for error
9079 start_token = token;
9080 /* Handle attributes. */
9081 if (token->keyword == RID_ATTRIBUTE)
9083 /* Parse the attributes. */
9084 decl_specs->attributes
9085 = chainon (decl_specs->attributes,
9086 cp_parser_attributes_opt (parser));
9089 /* Assume we will find a decl-specifier keyword. */
9090 found_decl_spec = true;
9091 /* If the next token is an appropriate keyword, we can simply
9092 add it to the list. */
9093 switch (token->keyword)
9099 if (!at_class_scope_p ())
9101 error_at (token->location, "%<friend%> used outside of class");
9102 cp_lexer_purge_token (parser->lexer);
9106 ++decl_specs->specs[(int) ds_friend];
9107 /* Consume the token. */
9108 cp_lexer_consume_token (parser->lexer);
9113 ++decl_specs->specs[(int) ds_constexpr];
9114 cp_lexer_consume_token (parser->lexer);
9117 /* function-specifier:
9124 cp_parser_function_specifier_opt (parser, decl_specs);
9130 ++decl_specs->specs[(int) ds_typedef];
9131 /* Consume the token. */
9132 cp_lexer_consume_token (parser->lexer);
9133 /* A constructor declarator cannot appear in a typedef. */
9134 constructor_possible_p = false;
9135 /* The "typedef" keyword can only occur in a declaration; we
9136 may as well commit at this point. */
9137 cp_parser_commit_to_tentative_parse (parser);
9139 if (decl_specs->storage_class != sc_none)
9140 decl_specs->conflicting_specifiers_p = true;
9143 /* storage-class-specifier:
9153 if (cxx_dialect == cxx98)
9155 /* Consume the token. */
9156 cp_lexer_consume_token (parser->lexer);
9158 /* Complain about `auto' as a storage specifier, if
9159 we're complaining about C++0x compatibility. */
9160 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9161 " will change meaning in C++0x; please remove it");
9163 /* Set the storage class anyway. */
9164 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9168 /* C++0x auto type-specifier. */
9169 found_decl_spec = false;
9176 /* Consume the token. */
9177 cp_lexer_consume_token (parser->lexer);
9178 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9182 /* Consume the token. */
9183 cp_lexer_consume_token (parser->lexer);
9184 ++decl_specs->specs[(int) ds_thread];
9188 /* We did not yet find a decl-specifier yet. */
9189 found_decl_spec = false;
9193 /* Constructors are a special case. The `S' in `S()' is not a
9194 decl-specifier; it is the beginning of the declarator. */
9197 && constructor_possible_p
9198 && (cp_parser_constructor_declarator_p
9199 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9201 /* If we don't have a DECL_SPEC yet, then we must be looking at
9202 a type-specifier. */
9203 if (!found_decl_spec && !constructor_p)
9205 int decl_spec_declares_class_or_enum;
9206 bool is_cv_qualifier;
9210 = cp_parser_type_specifier (parser, flags,
9212 /*is_declaration=*/true,
9213 &decl_spec_declares_class_or_enum,
9215 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9217 /* If this type-specifier referenced a user-defined type
9218 (a typedef, class-name, etc.), then we can't allow any
9219 more such type-specifiers henceforth.
9223 The longest sequence of decl-specifiers that could
9224 possibly be a type name is taken as the
9225 decl-specifier-seq of a declaration. The sequence shall
9226 be self-consistent as described below.
9230 As a general rule, at most one type-specifier is allowed
9231 in the complete decl-specifier-seq of a declaration. The
9232 only exceptions are the following:
9234 -- const or volatile can be combined with any other
9237 -- signed or unsigned can be combined with char, long,
9245 void g (const int Pc);
9247 Here, Pc is *not* part of the decl-specifier seq; it's
9248 the declarator. Therefore, once we see a type-specifier
9249 (other than a cv-qualifier), we forbid any additional
9250 user-defined types. We *do* still allow things like `int
9251 int' to be considered a decl-specifier-seq, and issue the
9252 error message later. */
9253 if (type_spec && !is_cv_qualifier)
9254 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9255 /* A constructor declarator cannot follow a type-specifier. */
9258 constructor_possible_p = false;
9259 found_decl_spec = true;
9260 if (!is_cv_qualifier)
9261 decl_specs->any_type_specifiers_p = true;
9265 /* If we still do not have a DECL_SPEC, then there are no more
9267 if (!found_decl_spec)
9270 decl_specs->any_specifiers_p = true;
9271 /* After we see one decl-specifier, further decl-specifiers are
9273 flags |= CP_PARSER_FLAGS_OPTIONAL;
9276 cp_parser_check_decl_spec (decl_specs, start_token->location);
9278 /* Don't allow a friend specifier with a class definition. */
9279 if (decl_specs->specs[(int) ds_friend] != 0
9280 && (*declares_class_or_enum & 2))
9281 error_at (start_token->location,
9282 "class definition may not be declared a friend");
9285 /* Parse an (optional) storage-class-specifier.
9287 storage-class-specifier:
9296 storage-class-specifier:
9299 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9302 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9304 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9307 if (cxx_dialect != cxx98)
9309 /* Fall through for C++98. */
9316 /* Consume the token. */
9317 return cp_lexer_consume_token (parser->lexer)->u.value;
9324 /* Parse an (optional) function-specifier.
9331 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9332 Updates DECL_SPECS, if it is non-NULL. */
9335 cp_parser_function_specifier_opt (cp_parser* parser,
9336 cp_decl_specifier_seq *decl_specs)
9338 cp_token *token = cp_lexer_peek_token (parser->lexer);
9339 switch (token->keyword)
9343 ++decl_specs->specs[(int) ds_inline];
9347 /* 14.5.2.3 [temp.mem]
9349 A member function template shall not be virtual. */
9350 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9351 error_at (token->location, "templates may not be %<virtual%>");
9352 else if (decl_specs)
9353 ++decl_specs->specs[(int) ds_virtual];
9358 ++decl_specs->specs[(int) ds_explicit];
9365 /* Consume the token. */
9366 return cp_lexer_consume_token (parser->lexer)->u.value;
9369 /* Parse a linkage-specification.
9371 linkage-specification:
9372 extern string-literal { declaration-seq [opt] }
9373 extern string-literal declaration */
9376 cp_parser_linkage_specification (cp_parser* parser)
9380 /* Look for the `extern' keyword. */
9381 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9383 /* Look for the string-literal. */
9384 linkage = cp_parser_string_literal (parser, false, false);
9386 /* Transform the literal into an identifier. If the literal is a
9387 wide-character string, or contains embedded NULs, then we can't
9388 handle it as the user wants. */
9389 if (strlen (TREE_STRING_POINTER (linkage))
9390 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9392 cp_parser_error (parser, "invalid linkage-specification");
9393 /* Assume C++ linkage. */
9394 linkage = lang_name_cplusplus;
9397 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9399 /* We're now using the new linkage. */
9400 push_lang_context (linkage);
9402 /* If the next token is a `{', then we're using the first
9404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9406 /* Consume the `{' token. */
9407 cp_lexer_consume_token (parser->lexer);
9408 /* Parse the declarations. */
9409 cp_parser_declaration_seq_opt (parser);
9410 /* Look for the closing `}'. */
9411 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9413 /* Otherwise, there's just one declaration. */
9416 bool saved_in_unbraced_linkage_specification_p;
9418 saved_in_unbraced_linkage_specification_p
9419 = parser->in_unbraced_linkage_specification_p;
9420 parser->in_unbraced_linkage_specification_p = true;
9421 cp_parser_declaration (parser);
9422 parser->in_unbraced_linkage_specification_p
9423 = saved_in_unbraced_linkage_specification_p;
9426 /* We're done with the linkage-specification. */
9427 pop_lang_context ();
9430 /* Parse a static_assert-declaration.
9432 static_assert-declaration:
9433 static_assert ( constant-expression , string-literal ) ;
9435 If MEMBER_P, this static_assert is a class member. */
9438 cp_parser_static_assert(cp_parser *parser, bool member_p)
9443 location_t saved_loc;
9445 /* Peek at the `static_assert' token so we can keep track of exactly
9446 where the static assertion started. */
9447 token = cp_lexer_peek_token (parser->lexer);
9448 saved_loc = token->location;
9450 /* Look for the `static_assert' keyword. */
9451 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9452 "%<static_assert%>"))
9455 /* We know we are in a static assertion; commit to any tentative
9457 if (cp_parser_parsing_tentatively (parser))
9458 cp_parser_commit_to_tentative_parse (parser);
9460 /* Parse the `(' starting the static assertion condition. */
9461 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9463 /* Parse the constant-expression. */
9465 cp_parser_constant_expression (parser,
9466 /*allow_non_constant_p=*/false,
9467 /*non_constant_p=*/NULL);
9469 /* Parse the separating `,'. */
9470 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9472 /* Parse the string-literal message. */
9473 message = cp_parser_string_literal (parser,
9474 /*translate=*/false,
9477 /* A `)' completes the static assertion. */
9478 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9479 cp_parser_skip_to_closing_parenthesis (parser,
9480 /*recovering=*/true,
9482 /*consume_paren=*/true);
9484 /* A semicolon terminates the declaration. */
9485 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9487 /* Complete the static assertion, which may mean either processing
9488 the static assert now or saving it for template instantiation. */
9489 finish_static_assert (condition, message, saved_loc, member_p);
9492 /* Parse a `decltype' type. Returns the type.
9494 simple-type-specifier:
9495 decltype ( expression ) */
9498 cp_parser_decltype (cp_parser *parser)
9501 bool id_expression_or_member_access_p = false;
9502 const char *saved_message;
9503 bool saved_integral_constant_expression_p;
9504 bool saved_non_integral_constant_expression_p;
9505 cp_token *id_expr_start_token;
9507 /* Look for the `decltype' token. */
9508 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9509 return error_mark_node;
9511 /* Types cannot be defined in a `decltype' expression. Save away the
9513 saved_message = parser->type_definition_forbidden_message;
9515 /* And create the new one. */
9516 parser->type_definition_forbidden_message
9517 = G_("types may not be defined in %<decltype%> expressions");
9519 /* The restrictions on constant-expressions do not apply inside
9520 decltype expressions. */
9521 saved_integral_constant_expression_p
9522 = parser->integral_constant_expression_p;
9523 saved_non_integral_constant_expression_p
9524 = parser->non_integral_constant_expression_p;
9525 parser->integral_constant_expression_p = false;
9527 /* Do not actually evaluate the expression. */
9528 ++cp_unevaluated_operand;
9530 /* Do not warn about problems with the expression. */
9531 ++c_inhibit_evaluation_warnings;
9533 /* Parse the opening `('. */
9534 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9535 return error_mark_node;
9537 /* First, try parsing an id-expression. */
9538 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9539 cp_parser_parse_tentatively (parser);
9540 expr = cp_parser_id_expression (parser,
9541 /*template_keyword_p=*/false,
9542 /*check_dependency_p=*/true,
9543 /*template_p=*/NULL,
9544 /*declarator_p=*/false,
9545 /*optional_p=*/false);
9547 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9549 bool non_integral_constant_expression_p = false;
9550 tree id_expression = expr;
9552 const char *error_msg;
9554 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9555 /* Lookup the name we got back from the id-expression. */
9556 expr = cp_parser_lookup_name (parser, expr,
9558 /*is_template=*/false,
9559 /*is_namespace=*/false,
9560 /*check_dependency=*/true,
9561 /*ambiguous_decls=*/NULL,
9562 id_expr_start_token->location);
9565 && expr != error_mark_node
9566 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9567 && TREE_CODE (expr) != TYPE_DECL
9568 && (TREE_CODE (expr) != BIT_NOT_EXPR
9569 || !TYPE_P (TREE_OPERAND (expr, 0)))
9570 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9572 /* Complete lookup of the id-expression. */
9573 expr = (finish_id_expression
9574 (id_expression, expr, parser->scope, &idk,
9575 /*integral_constant_expression_p=*/false,
9576 /*allow_non_integral_constant_expression_p=*/true,
9577 &non_integral_constant_expression_p,
9578 /*template_p=*/false,
9580 /*address_p=*/false,
9581 /*template_arg_p=*/false,
9583 id_expr_start_token->location));
9585 if (expr == error_mark_node)
9586 /* We found an id-expression, but it was something that we
9587 should not have found. This is an error, not something
9588 we can recover from, so note that we found an
9589 id-expression and we'll recover as gracefully as
9591 id_expression_or_member_access_p = true;
9595 && expr != error_mark_node
9596 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9597 /* We have an id-expression. */
9598 id_expression_or_member_access_p = true;
9601 if (!id_expression_or_member_access_p)
9603 /* Abort the id-expression parse. */
9604 cp_parser_abort_tentative_parse (parser);
9606 /* Parsing tentatively, again. */
9607 cp_parser_parse_tentatively (parser);
9609 /* Parse a class member access. */
9610 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9612 /*member_access_only_p=*/true, NULL);
9615 && expr != error_mark_node
9616 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9617 /* We have an id-expression. */
9618 id_expression_or_member_access_p = true;
9621 if (id_expression_or_member_access_p)
9622 /* We have parsed the complete id-expression or member access. */
9623 cp_parser_parse_definitely (parser);
9626 bool saved_greater_than_is_operator_p;
9628 /* Abort our attempt to parse an id-expression or member access
9630 cp_parser_abort_tentative_parse (parser);
9632 /* Within a parenthesized expression, a `>' token is always
9633 the greater-than operator. */
9634 saved_greater_than_is_operator_p
9635 = parser->greater_than_is_operator_p;
9636 parser->greater_than_is_operator_p = true;
9638 /* Parse a full expression. */
9639 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9641 /* The `>' token might be the end of a template-id or
9642 template-parameter-list now. */
9643 parser->greater_than_is_operator_p
9644 = saved_greater_than_is_operator_p;
9647 /* Go back to evaluating expressions. */
9648 --cp_unevaluated_operand;
9649 --c_inhibit_evaluation_warnings;
9651 /* Restore the old message and the integral constant expression
9653 parser->type_definition_forbidden_message = saved_message;
9654 parser->integral_constant_expression_p
9655 = saved_integral_constant_expression_p;
9656 parser->non_integral_constant_expression_p
9657 = saved_non_integral_constant_expression_p;
9659 if (expr == error_mark_node)
9661 /* Skip everything up to the closing `)'. */
9662 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9663 /*consume_paren=*/true);
9664 return error_mark_node;
9667 /* Parse to the closing `)'. */
9668 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9670 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9671 /*consume_paren=*/true);
9672 return error_mark_node;
9675 return finish_decltype_type (expr, id_expression_or_member_access_p);
9678 /* Special member functions [gram.special] */
9680 /* Parse a conversion-function-id.
9682 conversion-function-id:
9683 operator conversion-type-id
9685 Returns an IDENTIFIER_NODE representing the operator. */
9688 cp_parser_conversion_function_id (cp_parser* parser)
9692 tree saved_qualifying_scope;
9693 tree saved_object_scope;
9694 tree pushed_scope = NULL_TREE;
9696 /* Look for the `operator' token. */
9697 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9698 return error_mark_node;
9699 /* When we parse the conversion-type-id, the current scope will be
9700 reset. However, we need that information in able to look up the
9701 conversion function later, so we save it here. */
9702 saved_scope = parser->scope;
9703 saved_qualifying_scope = parser->qualifying_scope;
9704 saved_object_scope = parser->object_scope;
9705 /* We must enter the scope of the class so that the names of
9706 entities declared within the class are available in the
9707 conversion-type-id. For example, consider:
9714 S::operator I() { ... }
9716 In order to see that `I' is a type-name in the definition, we
9717 must be in the scope of `S'. */
9719 pushed_scope = push_scope (saved_scope);
9720 /* Parse the conversion-type-id. */
9721 type = cp_parser_conversion_type_id (parser);
9722 /* Leave the scope of the class, if any. */
9724 pop_scope (pushed_scope);
9725 /* Restore the saved scope. */
9726 parser->scope = saved_scope;
9727 parser->qualifying_scope = saved_qualifying_scope;
9728 parser->object_scope = saved_object_scope;
9729 /* If the TYPE is invalid, indicate failure. */
9730 if (type == error_mark_node)
9731 return error_mark_node;
9732 return mangle_conv_op_name_for_type (type);
9735 /* Parse a conversion-type-id:
9738 type-specifier-seq conversion-declarator [opt]
9740 Returns the TYPE specified. */
9743 cp_parser_conversion_type_id (cp_parser* parser)
9746 cp_decl_specifier_seq type_specifiers;
9747 cp_declarator *declarator;
9748 tree type_specified;
9750 /* Parse the attributes. */
9751 attributes = cp_parser_attributes_opt (parser);
9752 /* Parse the type-specifiers. */
9753 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9754 /*is_trailing_return=*/false,
9756 /* If that didn't work, stop. */
9757 if (type_specifiers.type == error_mark_node)
9758 return error_mark_node;
9759 /* Parse the conversion-declarator. */
9760 declarator = cp_parser_conversion_declarator_opt (parser);
9762 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9763 /*initialized=*/0, &attributes);
9765 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9767 /* Don't give this error when parsing tentatively. This happens to
9768 work because we always parse this definitively once. */
9769 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9770 && type_uses_auto (type_specified))
9772 error ("invalid use of %<auto%> in conversion operator");
9773 return error_mark_node;
9776 return type_specified;
9779 /* Parse an (optional) conversion-declarator.
9781 conversion-declarator:
9782 ptr-operator conversion-declarator [opt]
9786 static cp_declarator *
9787 cp_parser_conversion_declarator_opt (cp_parser* parser)
9789 enum tree_code code;
9791 cp_cv_quals cv_quals;
9793 /* We don't know if there's a ptr-operator next, or not. */
9794 cp_parser_parse_tentatively (parser);
9795 /* Try the ptr-operator. */
9796 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9797 /* If it worked, look for more conversion-declarators. */
9798 if (cp_parser_parse_definitely (parser))
9800 cp_declarator *declarator;
9802 /* Parse another optional declarator. */
9803 declarator = cp_parser_conversion_declarator_opt (parser);
9805 return cp_parser_make_indirect_declarator
9806 (code, class_type, cv_quals, declarator);
9812 /* Parse an (optional) ctor-initializer.
9815 : mem-initializer-list
9817 Returns TRUE iff the ctor-initializer was actually present. */
9820 cp_parser_ctor_initializer_opt (cp_parser* parser)
9822 /* If the next token is not a `:', then there is no
9823 ctor-initializer. */
9824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9826 /* Do default initialization of any bases and members. */
9827 if (DECL_CONSTRUCTOR_P (current_function_decl))
9828 finish_mem_initializers (NULL_TREE);
9833 /* Consume the `:' token. */
9834 cp_lexer_consume_token (parser->lexer);
9835 /* And the mem-initializer-list. */
9836 cp_parser_mem_initializer_list (parser);
9841 /* Parse a mem-initializer-list.
9843 mem-initializer-list:
9844 mem-initializer ... [opt]
9845 mem-initializer ... [opt] , mem-initializer-list */
9848 cp_parser_mem_initializer_list (cp_parser* parser)
9850 tree mem_initializer_list = NULL_TREE;
9851 cp_token *token = cp_lexer_peek_token (parser->lexer);
9853 /* Let the semantic analysis code know that we are starting the
9854 mem-initializer-list. */
9855 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9856 error_at (token->location,
9857 "only constructors take base initializers");
9859 /* Loop through the list. */
9862 tree mem_initializer;
9864 token = cp_lexer_peek_token (parser->lexer);
9865 /* Parse the mem-initializer. */
9866 mem_initializer = cp_parser_mem_initializer (parser);
9867 /* If the next token is a `...', we're expanding member initializers. */
9868 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9870 /* Consume the `...'. */
9871 cp_lexer_consume_token (parser->lexer);
9873 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9874 can be expanded but members cannot. */
9875 if (mem_initializer != error_mark_node
9876 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9878 error_at (token->location,
9879 "cannot expand initializer for member %<%D%>",
9880 TREE_PURPOSE (mem_initializer));
9881 mem_initializer = error_mark_node;
9884 /* Construct the pack expansion type. */
9885 if (mem_initializer != error_mark_node)
9886 mem_initializer = make_pack_expansion (mem_initializer);
9888 /* Add it to the list, unless it was erroneous. */
9889 if (mem_initializer != error_mark_node)
9891 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9892 mem_initializer_list = mem_initializer;
9894 /* If the next token is not a `,', we're done. */
9895 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9897 /* Consume the `,' token. */
9898 cp_lexer_consume_token (parser->lexer);
9901 /* Perform semantic analysis. */
9902 if (DECL_CONSTRUCTOR_P (current_function_decl))
9903 finish_mem_initializers (mem_initializer_list);
9906 /* Parse a mem-initializer.
9909 mem-initializer-id ( expression-list [opt] )
9910 mem-initializer-id braced-init-list
9915 ( expression-list [opt] )
9917 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9918 class) or FIELD_DECL (for a non-static data member) to initialize;
9919 the TREE_VALUE is the expression-list. An empty initialization
9920 list is represented by void_list_node. */
9923 cp_parser_mem_initializer (cp_parser* parser)
9925 tree mem_initializer_id;
9926 tree expression_list;
9928 cp_token *token = cp_lexer_peek_token (parser->lexer);
9930 /* Find out what is being initialized. */
9931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9933 permerror (token->location,
9934 "anachronistic old-style base class initializer");
9935 mem_initializer_id = NULL_TREE;
9939 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9940 if (mem_initializer_id == error_mark_node)
9941 return mem_initializer_id;
9943 member = expand_member_init (mem_initializer_id);
9944 if (member && !DECL_P (member))
9945 in_base_initializer = 1;
9947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9949 bool expr_non_constant_p;
9950 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9951 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9952 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9953 expression_list = build_tree_list (NULL_TREE, expression_list);
9958 vec = cp_parser_parenthesized_expression_list (parser, false,
9960 /*allow_expansion_p=*/true,
9961 /*non_constant_p=*/NULL);
9963 return error_mark_node;
9964 expression_list = build_tree_list_vec (vec);
9965 release_tree_vector (vec);
9968 if (expression_list == error_mark_node)
9969 return error_mark_node;
9970 if (!expression_list)
9971 expression_list = void_type_node;
9973 in_base_initializer = 0;
9975 return member ? build_tree_list (member, expression_list) : error_mark_node;
9978 /* Parse a mem-initializer-id.
9981 :: [opt] nested-name-specifier [opt] class-name
9984 Returns a TYPE indicating the class to be initializer for the first
9985 production. Returns an IDENTIFIER_NODE indicating the data member
9986 to be initialized for the second production. */
9989 cp_parser_mem_initializer_id (cp_parser* parser)
9991 bool global_scope_p;
9992 bool nested_name_specifier_p;
9993 bool template_p = false;
9996 cp_token *token = cp_lexer_peek_token (parser->lexer);
9998 /* `typename' is not allowed in this context ([temp.res]). */
9999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10001 error_at (token->location,
10002 "keyword %<typename%> not allowed in this context (a qualified "
10003 "member initializer is implicitly a type)");
10004 cp_lexer_consume_token (parser->lexer);
10006 /* Look for the optional `::' operator. */
10008 = (cp_parser_global_scope_opt (parser,
10009 /*current_scope_valid_p=*/false)
10011 /* Look for the optional nested-name-specifier. The simplest way to
10016 The keyword `typename' is not permitted in a base-specifier or
10017 mem-initializer; in these contexts a qualified name that
10018 depends on a template-parameter is implicitly assumed to be a
10021 is to assume that we have seen the `typename' keyword at this
10023 nested_name_specifier_p
10024 = (cp_parser_nested_name_specifier_opt (parser,
10025 /*typename_keyword_p=*/true,
10026 /*check_dependency_p=*/true,
10028 /*is_declaration=*/true)
10030 if (nested_name_specifier_p)
10031 template_p = cp_parser_optional_template_keyword (parser);
10032 /* If there is a `::' operator or a nested-name-specifier, then we
10033 are definitely looking for a class-name. */
10034 if (global_scope_p || nested_name_specifier_p)
10035 return cp_parser_class_name (parser,
10036 /*typename_keyword_p=*/true,
10037 /*template_keyword_p=*/template_p,
10039 /*check_dependency_p=*/true,
10040 /*class_head_p=*/false,
10041 /*is_declaration=*/true);
10042 /* Otherwise, we could also be looking for an ordinary identifier. */
10043 cp_parser_parse_tentatively (parser);
10044 /* Try a class-name. */
10045 id = cp_parser_class_name (parser,
10046 /*typename_keyword_p=*/true,
10047 /*template_keyword_p=*/false,
10049 /*check_dependency_p=*/true,
10050 /*class_head_p=*/false,
10051 /*is_declaration=*/true);
10052 /* If we found one, we're done. */
10053 if (cp_parser_parse_definitely (parser))
10055 /* Otherwise, look for an ordinary identifier. */
10056 return cp_parser_identifier (parser);
10059 /* Overloading [gram.over] */
10061 /* Parse an operator-function-id.
10063 operator-function-id:
10066 Returns an IDENTIFIER_NODE for the operator which is a
10067 human-readable spelling of the identifier, e.g., `operator +'. */
10070 cp_parser_operator_function_id (cp_parser* parser)
10072 /* Look for the `operator' keyword. */
10073 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10074 return error_mark_node;
10075 /* And then the name of the operator itself. */
10076 return cp_parser_operator (parser);
10079 /* Parse an operator.
10082 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10083 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10084 || ++ -- , ->* -> () []
10091 Returns an IDENTIFIER_NODE for the operator which is a
10092 human-readable spelling of the identifier, e.g., `operator +'. */
10095 cp_parser_operator (cp_parser* parser)
10097 tree id = NULL_TREE;
10100 /* Peek at the next token. */
10101 token = cp_lexer_peek_token (parser->lexer);
10102 /* Figure out which operator we have. */
10103 switch (token->type)
10109 /* The keyword should be either `new' or `delete'. */
10110 if (token->keyword == RID_NEW)
10112 else if (token->keyword == RID_DELETE)
10117 /* Consume the `new' or `delete' token. */
10118 cp_lexer_consume_token (parser->lexer);
10120 /* Peek at the next token. */
10121 token = cp_lexer_peek_token (parser->lexer);
10122 /* If it's a `[' token then this is the array variant of the
10124 if (token->type == CPP_OPEN_SQUARE)
10126 /* Consume the `[' token. */
10127 cp_lexer_consume_token (parser->lexer);
10128 /* Look for the `]' token. */
10129 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10130 id = ansi_opname (op == NEW_EXPR
10131 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10133 /* Otherwise, we have the non-array variant. */
10135 id = ansi_opname (op);
10141 id = ansi_opname (PLUS_EXPR);
10145 id = ansi_opname (MINUS_EXPR);
10149 id = ansi_opname (MULT_EXPR);
10153 id = ansi_opname (TRUNC_DIV_EXPR);
10157 id = ansi_opname (TRUNC_MOD_EXPR);
10161 id = ansi_opname (BIT_XOR_EXPR);
10165 id = ansi_opname (BIT_AND_EXPR);
10169 id = ansi_opname (BIT_IOR_EXPR);
10173 id = ansi_opname (BIT_NOT_EXPR);
10177 id = ansi_opname (TRUTH_NOT_EXPR);
10181 id = ansi_assopname (NOP_EXPR);
10185 id = ansi_opname (LT_EXPR);
10189 id = ansi_opname (GT_EXPR);
10193 id = ansi_assopname (PLUS_EXPR);
10197 id = ansi_assopname (MINUS_EXPR);
10201 id = ansi_assopname (MULT_EXPR);
10205 id = ansi_assopname (TRUNC_DIV_EXPR);
10209 id = ansi_assopname (TRUNC_MOD_EXPR);
10213 id = ansi_assopname (BIT_XOR_EXPR);
10217 id = ansi_assopname (BIT_AND_EXPR);
10221 id = ansi_assopname (BIT_IOR_EXPR);
10225 id = ansi_opname (LSHIFT_EXPR);
10229 id = ansi_opname (RSHIFT_EXPR);
10232 case CPP_LSHIFT_EQ:
10233 id = ansi_assopname (LSHIFT_EXPR);
10236 case CPP_RSHIFT_EQ:
10237 id = ansi_assopname (RSHIFT_EXPR);
10241 id = ansi_opname (EQ_EXPR);
10245 id = ansi_opname (NE_EXPR);
10249 id = ansi_opname (LE_EXPR);
10252 case CPP_GREATER_EQ:
10253 id = ansi_opname (GE_EXPR);
10257 id = ansi_opname (TRUTH_ANDIF_EXPR);
10261 id = ansi_opname (TRUTH_ORIF_EXPR);
10264 case CPP_PLUS_PLUS:
10265 id = ansi_opname (POSTINCREMENT_EXPR);
10268 case CPP_MINUS_MINUS:
10269 id = ansi_opname (PREDECREMENT_EXPR);
10273 id = ansi_opname (COMPOUND_EXPR);
10276 case CPP_DEREF_STAR:
10277 id = ansi_opname (MEMBER_REF);
10281 id = ansi_opname (COMPONENT_REF);
10284 case CPP_OPEN_PAREN:
10285 /* Consume the `('. */
10286 cp_lexer_consume_token (parser->lexer);
10287 /* Look for the matching `)'. */
10288 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10289 return ansi_opname (CALL_EXPR);
10291 case CPP_OPEN_SQUARE:
10292 /* Consume the `['. */
10293 cp_lexer_consume_token (parser->lexer);
10294 /* Look for the matching `]'. */
10295 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10296 return ansi_opname (ARRAY_REF);
10299 /* Anything else is an error. */
10303 /* If we have selected an identifier, we need to consume the
10306 cp_lexer_consume_token (parser->lexer);
10307 /* Otherwise, no valid operator name was present. */
10310 cp_parser_error (parser, "expected operator");
10311 id = error_mark_node;
10317 /* Parse a template-declaration.
10319 template-declaration:
10320 export [opt] template < template-parameter-list > declaration
10322 If MEMBER_P is TRUE, this template-declaration occurs within a
10325 The grammar rule given by the standard isn't correct. What
10326 is really meant is:
10328 template-declaration:
10329 export [opt] template-parameter-list-seq
10330 decl-specifier-seq [opt] init-declarator [opt] ;
10331 export [opt] template-parameter-list-seq
10332 function-definition
10334 template-parameter-list-seq:
10335 template-parameter-list-seq [opt]
10336 template < template-parameter-list > */
10339 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10341 /* Check for `export'. */
10342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10344 /* Consume the `export' token. */
10345 cp_lexer_consume_token (parser->lexer);
10346 /* Warn that we do not support `export'. */
10347 warning (0, "keyword %<export%> not implemented, and will be ignored");
10350 cp_parser_template_declaration_after_export (parser, member_p);
10353 /* Parse a template-parameter-list.
10355 template-parameter-list:
10357 template-parameter-list , template-parameter
10359 Returns a TREE_LIST. Each node represents a template parameter.
10360 The nodes are connected via their TREE_CHAINs. */
10363 cp_parser_template_parameter_list (cp_parser* parser)
10365 tree parameter_list = NULL_TREE;
10367 begin_template_parm_list ();
10372 bool is_parameter_pack;
10373 location_t parm_loc;
10375 /* Parse the template-parameter. */
10376 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10377 parameter = cp_parser_template_parameter (parser,
10379 &is_parameter_pack);
10380 /* Add it to the list. */
10381 if (parameter != error_mark_node)
10382 parameter_list = process_template_parm (parameter_list,
10386 is_parameter_pack);
10389 tree err_parm = build_tree_list (parameter, parameter);
10390 TREE_VALUE (err_parm) = error_mark_node;
10391 parameter_list = chainon (parameter_list, err_parm);
10394 /* If the next token is not a `,', we're done. */
10395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10397 /* Otherwise, consume the `,' token. */
10398 cp_lexer_consume_token (parser->lexer);
10401 return end_template_parm_list (parameter_list);
10404 /* Parse a template-parameter.
10406 template-parameter:
10408 parameter-declaration
10410 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10411 the parameter. The TREE_PURPOSE is the default value, if any.
10412 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10413 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10414 set to true iff this parameter is a parameter pack. */
10417 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10418 bool *is_parameter_pack)
10421 cp_parameter_declarator *parameter_declarator;
10422 cp_declarator *id_declarator;
10425 /* Assume it is a type parameter or a template parameter. */
10426 *is_non_type = false;
10427 /* Assume it not a parameter pack. */
10428 *is_parameter_pack = false;
10429 /* Peek at the next token. */
10430 token = cp_lexer_peek_token (parser->lexer);
10431 /* If it is `class' or `template', we have a type-parameter. */
10432 if (token->keyword == RID_TEMPLATE)
10433 return cp_parser_type_parameter (parser, is_parameter_pack);
10434 /* If it is `class' or `typename' we do not know yet whether it is a
10435 type parameter or a non-type parameter. Consider:
10437 template <typename T, typename T::X X> ...
10441 template <class C, class D*> ...
10443 Here, the first parameter is a type parameter, and the second is
10444 a non-type parameter. We can tell by looking at the token after
10445 the identifier -- if it is a `,', `=', or `>' then we have a type
10447 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10449 /* Peek at the token after `class' or `typename'. */
10450 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10451 /* If it's an ellipsis, we have a template type parameter
10453 if (token->type == CPP_ELLIPSIS)
10454 return cp_parser_type_parameter (parser, is_parameter_pack);
10455 /* If it's an identifier, skip it. */
10456 if (token->type == CPP_NAME)
10457 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10458 /* Now, see if the token looks like the end of a template
10460 if (token->type == CPP_COMMA
10461 || token->type == CPP_EQ
10462 || token->type == CPP_GREATER)
10463 return cp_parser_type_parameter (parser, is_parameter_pack);
10466 /* Otherwise, it is a non-type parameter.
10470 When parsing a default template-argument for a non-type
10471 template-parameter, the first non-nested `>' is taken as the end
10472 of the template parameter-list rather than a greater-than
10474 *is_non_type = true;
10475 parameter_declarator
10476 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10477 /*parenthesized_p=*/NULL);
10479 /* If the parameter declaration is marked as a parameter pack, set
10480 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10481 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10483 if (parameter_declarator
10484 && parameter_declarator->declarator
10485 && parameter_declarator->declarator->parameter_pack_p)
10487 *is_parameter_pack = true;
10488 parameter_declarator->declarator->parameter_pack_p = false;
10491 /* If the next token is an ellipsis, and we don't already have it
10492 marked as a parameter pack, then we have a parameter pack (that
10493 has no declarator). */
10494 if (!*is_parameter_pack
10495 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10496 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10498 /* Consume the `...'. */
10499 cp_lexer_consume_token (parser->lexer);
10500 maybe_warn_variadic_templates ();
10502 *is_parameter_pack = true;
10504 /* We might end up with a pack expansion as the type of the non-type
10505 template parameter, in which case this is a non-type template
10507 else if (parameter_declarator
10508 && parameter_declarator->decl_specifiers.type
10509 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10511 *is_parameter_pack = true;
10512 parameter_declarator->decl_specifiers.type =
10513 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10516 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10518 /* Parameter packs cannot have default arguments. However, a
10519 user may try to do so, so we'll parse them and give an
10520 appropriate diagnostic here. */
10522 /* Consume the `='. */
10523 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10524 cp_lexer_consume_token (parser->lexer);
10526 /* Find the name of the parameter pack. */
10527 id_declarator = parameter_declarator->declarator;
10528 while (id_declarator && id_declarator->kind != cdk_id)
10529 id_declarator = id_declarator->declarator;
10531 if (id_declarator && id_declarator->kind == cdk_id)
10532 error_at (start_token->location,
10533 "template parameter pack %qD cannot have a default argument",
10534 id_declarator->u.id.unqualified_name);
10536 error_at (start_token->location,
10537 "template parameter pack cannot have a default argument");
10539 /* Parse the default argument, but throw away the result. */
10540 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10543 parm = grokdeclarator (parameter_declarator->declarator,
10544 ¶meter_declarator->decl_specifiers,
10545 TPARM, /*initialized=*/0,
10546 /*attrlist=*/NULL);
10547 if (parm == error_mark_node)
10548 return error_mark_node;
10550 return build_tree_list (parameter_declarator->default_argument, parm);
10553 /* Parse a type-parameter.
10556 class identifier [opt]
10557 class identifier [opt] = type-id
10558 typename identifier [opt]
10559 typename identifier [opt] = type-id
10560 template < template-parameter-list > class identifier [opt]
10561 template < template-parameter-list > class identifier [opt]
10564 GNU Extension (variadic templates):
10567 class ... identifier [opt]
10568 typename ... identifier [opt]
10570 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10571 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10572 the declaration of the parameter.
10574 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10577 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10582 /* Look for a keyword to tell us what kind of parameter this is. */
10583 token = cp_parser_require (parser, CPP_KEYWORD,
10584 "%<class%>, %<typename%>, or %<template%>");
10586 return error_mark_node;
10588 switch (token->keyword)
10594 tree default_argument;
10596 /* If the next token is an ellipsis, we have a template
10598 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10600 /* Consume the `...' token. */
10601 cp_lexer_consume_token (parser->lexer);
10602 maybe_warn_variadic_templates ();
10604 *is_parameter_pack = true;
10607 /* If the next token is an identifier, then it names the
10609 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10610 identifier = cp_parser_identifier (parser);
10612 identifier = NULL_TREE;
10614 /* Create the parameter. */
10615 parameter = finish_template_type_parm (class_type_node, identifier);
10617 /* If the next token is an `=', we have a default argument. */
10618 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10620 /* Consume the `=' token. */
10621 cp_lexer_consume_token (parser->lexer);
10622 /* Parse the default-argument. */
10623 push_deferring_access_checks (dk_no_deferred);
10624 default_argument = cp_parser_type_id (parser);
10626 /* Template parameter packs cannot have default
10628 if (*is_parameter_pack)
10631 error_at (token->location,
10632 "template parameter pack %qD cannot have a "
10633 "default argument", identifier);
10635 error_at (token->location,
10636 "template parameter packs cannot have "
10637 "default arguments");
10638 default_argument = NULL_TREE;
10640 pop_deferring_access_checks ();
10643 default_argument = NULL_TREE;
10645 /* Create the combined representation of the parameter and the
10646 default argument. */
10647 parameter = build_tree_list (default_argument, parameter);
10654 tree default_argument;
10656 /* Look for the `<'. */
10657 cp_parser_require (parser, CPP_LESS, "%<<%>");
10658 /* Parse the template-parameter-list. */
10659 cp_parser_template_parameter_list (parser);
10660 /* Look for the `>'. */
10661 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10662 /* Look for the `class' keyword. */
10663 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10664 /* If the next token is an ellipsis, we have a template
10666 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10668 /* Consume the `...' token. */
10669 cp_lexer_consume_token (parser->lexer);
10670 maybe_warn_variadic_templates ();
10672 *is_parameter_pack = true;
10674 /* If the next token is an `=', then there is a
10675 default-argument. If the next token is a `>', we are at
10676 the end of the parameter-list. If the next token is a `,',
10677 then we are at the end of this parameter. */
10678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10679 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10680 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10682 identifier = cp_parser_identifier (parser);
10683 /* Treat invalid names as if the parameter were nameless. */
10684 if (identifier == error_mark_node)
10685 identifier = NULL_TREE;
10688 identifier = NULL_TREE;
10690 /* Create the template parameter. */
10691 parameter = finish_template_template_parm (class_type_node,
10694 /* If the next token is an `=', then there is a
10695 default-argument. */
10696 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10700 /* Consume the `='. */
10701 cp_lexer_consume_token (parser->lexer);
10702 /* Parse the id-expression. */
10703 push_deferring_access_checks (dk_no_deferred);
10704 /* save token before parsing the id-expression, for error
10706 token = cp_lexer_peek_token (parser->lexer);
10708 = cp_parser_id_expression (parser,
10709 /*template_keyword_p=*/false,
10710 /*check_dependency_p=*/true,
10711 /*template_p=*/&is_template,
10712 /*declarator_p=*/false,
10713 /*optional_p=*/false);
10714 if (TREE_CODE (default_argument) == TYPE_DECL)
10715 /* If the id-expression was a template-id that refers to
10716 a template-class, we already have the declaration here,
10717 so no further lookup is needed. */
10720 /* Look up the name. */
10722 = cp_parser_lookup_name (parser, default_argument,
10724 /*is_template=*/is_template,
10725 /*is_namespace=*/false,
10726 /*check_dependency=*/true,
10727 /*ambiguous_decls=*/NULL,
10729 /* See if the default argument is valid. */
10731 = check_template_template_default_arg (default_argument);
10733 /* Template parameter packs cannot have default
10735 if (*is_parameter_pack)
10738 error_at (token->location,
10739 "template parameter pack %qD cannot "
10740 "have a default argument",
10743 error_at (token->location, "template parameter packs cannot "
10744 "have default arguments");
10745 default_argument = NULL_TREE;
10747 pop_deferring_access_checks ();
10750 default_argument = NULL_TREE;
10752 /* Create the combined representation of the parameter and the
10753 default argument. */
10754 parameter = build_tree_list (default_argument, parameter);
10759 gcc_unreachable ();
10766 /* Parse a template-id.
10769 template-name < template-argument-list [opt] >
10771 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10772 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10773 returned. Otherwise, if the template-name names a function, or set
10774 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10775 names a class, returns a TYPE_DECL for the specialization.
10777 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10778 uninstantiated templates. */
10781 cp_parser_template_id (cp_parser *parser,
10782 bool template_keyword_p,
10783 bool check_dependency_p,
10784 bool is_declaration)
10790 cp_token_position start_of_id = 0;
10791 deferred_access_check *chk;
10792 VEC (deferred_access_check,gc) *access_check;
10793 cp_token *next_token = NULL, *next_token_2 = NULL;
10794 bool is_identifier;
10796 /* If the next token corresponds to a template-id, there is no need
10798 next_token = cp_lexer_peek_token (parser->lexer);
10799 if (next_token->type == CPP_TEMPLATE_ID)
10801 struct tree_check *check_value;
10803 /* Get the stored value. */
10804 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10805 /* Perform any access checks that were deferred. */
10806 access_check = check_value->checks;
10810 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10813 perform_or_defer_access_check (chk->binfo,
10818 /* Return the stored value. */
10819 return check_value->value;
10822 /* Avoid performing name lookup if there is no possibility of
10823 finding a template-id. */
10824 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10825 || (next_token->type == CPP_NAME
10826 && !cp_parser_nth_token_starts_template_argument_list_p
10829 cp_parser_error (parser, "expected template-id");
10830 return error_mark_node;
10833 /* Remember where the template-id starts. */
10834 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10835 start_of_id = cp_lexer_token_position (parser->lexer, false);
10837 push_deferring_access_checks (dk_deferred);
10839 /* Parse the template-name. */
10840 is_identifier = false;
10841 templ = cp_parser_template_name (parser, template_keyword_p,
10842 check_dependency_p,
10845 if (templ == error_mark_node || is_identifier)
10847 pop_deferring_access_checks ();
10851 /* If we find the sequence `[:' after a template-name, it's probably
10852 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10853 parse correctly the argument list. */
10854 next_token = cp_lexer_peek_token (parser->lexer);
10855 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10856 if (next_token->type == CPP_OPEN_SQUARE
10857 && next_token->flags & DIGRAPH
10858 && next_token_2->type == CPP_COLON
10859 && !(next_token_2->flags & PREV_WHITE))
10861 cp_parser_parse_tentatively (parser);
10862 /* Change `:' into `::'. */
10863 next_token_2->type = CPP_SCOPE;
10864 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10866 cp_lexer_consume_token (parser->lexer);
10868 /* Parse the arguments. */
10869 arguments = cp_parser_enclosed_template_argument_list (parser);
10870 if (!cp_parser_parse_definitely (parser))
10872 /* If we couldn't parse an argument list, then we revert our changes
10873 and return simply an error. Maybe this is not a template-id
10875 next_token_2->type = CPP_COLON;
10876 cp_parser_error (parser, "expected %<<%>");
10877 pop_deferring_access_checks ();
10878 return error_mark_node;
10880 /* Otherwise, emit an error about the invalid digraph, but continue
10881 parsing because we got our argument list. */
10882 if (permerror (next_token->location,
10883 "%<<::%> cannot begin a template-argument list"))
10885 static bool hint = false;
10886 inform (next_token->location,
10887 "%<<:%> is an alternate spelling for %<[%>."
10888 " Insert whitespace between %<<%> and %<::%>");
10889 if (!hint && !flag_permissive)
10891 inform (next_token->location, "(if you use %<-fpermissive%>"
10892 " G++ will accept your code)");
10899 /* Look for the `<' that starts the template-argument-list. */
10900 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10902 pop_deferring_access_checks ();
10903 return error_mark_node;
10905 /* Parse the arguments. */
10906 arguments = cp_parser_enclosed_template_argument_list (parser);
10909 /* Build a representation of the specialization. */
10910 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10911 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10912 else if (DECL_CLASS_TEMPLATE_P (templ)
10913 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10915 bool entering_scope;
10916 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10917 template (rather than some instantiation thereof) only if
10918 is not nested within some other construct. For example, in
10919 "template <typename T> void f(T) { A<T>::", A<T> is just an
10920 instantiation of A. */
10921 entering_scope = (template_parm_scope_p ()
10922 && cp_lexer_next_token_is (parser->lexer,
10925 = finish_template_type (templ, arguments, entering_scope);
10929 /* If it's not a class-template or a template-template, it should be
10930 a function-template. */
10931 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10932 || TREE_CODE (templ) == OVERLOAD
10933 || BASELINK_P (templ)));
10935 template_id = lookup_template_function (templ, arguments);
10938 /* If parsing tentatively, replace the sequence of tokens that makes
10939 up the template-id with a CPP_TEMPLATE_ID token. That way,
10940 should we re-parse the token stream, we will not have to repeat
10941 the effort required to do the parse, nor will we issue duplicate
10942 error messages about problems during instantiation of the
10946 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10948 /* Reset the contents of the START_OF_ID token. */
10949 token->type = CPP_TEMPLATE_ID;
10950 /* Retrieve any deferred checks. Do not pop this access checks yet
10951 so the memory will not be reclaimed during token replacing below. */
10952 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10953 token->u.tree_check_value->value = template_id;
10954 token->u.tree_check_value->checks = get_deferred_access_checks ();
10955 token->keyword = RID_MAX;
10957 /* Purge all subsequent tokens. */
10958 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10960 /* ??? Can we actually assume that, if template_id ==
10961 error_mark_node, we will have issued a diagnostic to the
10962 user, as opposed to simply marking the tentative parse as
10964 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10965 error_at (token->location, "parse error in template argument list");
10968 pop_deferring_access_checks ();
10969 return template_id;
10972 /* Parse a template-name.
10977 The standard should actually say:
10981 operator-function-id
10983 A defect report has been filed about this issue.
10985 A conversion-function-id cannot be a template name because they cannot
10986 be part of a template-id. In fact, looking at this code:
10988 a.operator K<int>()
10990 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10991 It is impossible to call a templated conversion-function-id with an
10992 explicit argument list, since the only allowed template parameter is
10993 the type to which it is converting.
10995 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10996 `template' keyword, in a construction like:
11000 In that case `f' is taken to be a template-name, even though there
11001 is no way of knowing for sure.
11003 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11004 name refers to a set of overloaded functions, at least one of which
11005 is a template, or an IDENTIFIER_NODE with the name of the template,
11006 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11007 names are looked up inside uninstantiated templates. */
11010 cp_parser_template_name (cp_parser* parser,
11011 bool template_keyword_p,
11012 bool check_dependency_p,
11013 bool is_declaration,
11014 bool *is_identifier)
11019 cp_token *token = cp_lexer_peek_token (parser->lexer);
11021 /* If the next token is `operator', then we have either an
11022 operator-function-id or a conversion-function-id. */
11023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11025 /* We don't know whether we're looking at an
11026 operator-function-id or a conversion-function-id. */
11027 cp_parser_parse_tentatively (parser);
11028 /* Try an operator-function-id. */
11029 identifier = cp_parser_operator_function_id (parser);
11030 /* If that didn't work, try a conversion-function-id. */
11031 if (!cp_parser_parse_definitely (parser))
11033 cp_parser_error (parser, "expected template-name");
11034 return error_mark_node;
11037 /* Look for the identifier. */
11039 identifier = cp_parser_identifier (parser);
11041 /* If we didn't find an identifier, we don't have a template-id. */
11042 if (identifier == error_mark_node)
11043 return error_mark_node;
11045 /* If the name immediately followed the `template' keyword, then it
11046 is a template-name. However, if the next token is not `<', then
11047 we do not treat it as a template-name, since it is not being used
11048 as part of a template-id. This enables us to handle constructs
11051 template <typename T> struct S { S(); };
11052 template <typename T> S<T>::S();
11054 correctly. We would treat `S' as a template -- if it were `S<T>'
11055 -- but we do not if there is no `<'. */
11057 if (processing_template_decl
11058 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11060 /* In a declaration, in a dependent context, we pretend that the
11061 "template" keyword was present in order to improve error
11062 recovery. For example, given:
11064 template <typename T> void f(T::X<int>);
11066 we want to treat "X<int>" as a template-id. */
11068 && !template_keyword_p
11069 && parser->scope && TYPE_P (parser->scope)
11070 && check_dependency_p
11071 && dependent_scope_p (parser->scope)
11072 /* Do not do this for dtors (or ctors), since they never
11073 need the template keyword before their name. */
11074 && !constructor_name_p (identifier, parser->scope))
11076 cp_token_position start = 0;
11078 /* Explain what went wrong. */
11079 error_at (token->location, "non-template %qD used as template",
11081 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11082 parser->scope, identifier);
11083 /* If parsing tentatively, find the location of the "<" token. */
11084 if (cp_parser_simulate_error (parser))
11085 start = cp_lexer_token_position (parser->lexer, true);
11086 /* Parse the template arguments so that we can issue error
11087 messages about them. */
11088 cp_lexer_consume_token (parser->lexer);
11089 cp_parser_enclosed_template_argument_list (parser);
11090 /* Skip tokens until we find a good place from which to
11091 continue parsing. */
11092 cp_parser_skip_to_closing_parenthesis (parser,
11093 /*recovering=*/true,
11095 /*consume_paren=*/false);
11096 /* If parsing tentatively, permanently remove the
11097 template argument list. That will prevent duplicate
11098 error messages from being issued about the missing
11099 "template" keyword. */
11101 cp_lexer_purge_tokens_after (parser->lexer, start);
11103 *is_identifier = true;
11107 /* If the "template" keyword is present, then there is generally
11108 no point in doing name-lookup, so we just return IDENTIFIER.
11109 But, if the qualifying scope is non-dependent then we can
11110 (and must) do name-lookup normally. */
11111 if (template_keyword_p
11113 || (TYPE_P (parser->scope)
11114 && dependent_type_p (parser->scope))))
11118 /* Look up the name. */
11119 decl = cp_parser_lookup_name (parser, identifier,
11121 /*is_template=*/true,
11122 /*is_namespace=*/false,
11123 check_dependency_p,
11124 /*ambiguous_decls=*/NULL,
11127 /* If DECL is a template, then the name was a template-name. */
11128 if (TREE_CODE (decl) == TEMPLATE_DECL)
11132 tree fn = NULL_TREE;
11134 /* The standard does not explicitly indicate whether a name that
11135 names a set of overloaded declarations, some of which are
11136 templates, is a template-name. However, such a name should
11137 be a template-name; otherwise, there is no way to form a
11138 template-id for the overloaded templates. */
11139 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11140 if (TREE_CODE (fns) == OVERLOAD)
11141 for (fn = fns; fn; fn = OVL_NEXT (fn))
11142 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11147 /* The name does not name a template. */
11148 cp_parser_error (parser, "expected template-name");
11149 return error_mark_node;
11153 /* If DECL is dependent, and refers to a function, then just return
11154 its name; we will look it up again during template instantiation. */
11155 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11157 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11158 if (TYPE_P (scope) && dependent_type_p (scope))
11165 /* Parse a template-argument-list.
11167 template-argument-list:
11168 template-argument ... [opt]
11169 template-argument-list , template-argument ... [opt]
11171 Returns a TREE_VEC containing the arguments. */
11174 cp_parser_template_argument_list (cp_parser* parser)
11176 tree fixed_args[10];
11177 unsigned n_args = 0;
11178 unsigned alloced = 10;
11179 tree *arg_ary = fixed_args;
11181 bool saved_in_template_argument_list_p;
11183 bool saved_non_ice_p;
11185 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11186 parser->in_template_argument_list_p = true;
11187 /* Even if the template-id appears in an integral
11188 constant-expression, the contents of the argument list do
11190 saved_ice_p = parser->integral_constant_expression_p;
11191 parser->integral_constant_expression_p = false;
11192 saved_non_ice_p = parser->non_integral_constant_expression_p;
11193 parser->non_integral_constant_expression_p = false;
11194 /* Parse the arguments. */
11200 /* Consume the comma. */
11201 cp_lexer_consume_token (parser->lexer);
11203 /* Parse the template-argument. */
11204 argument = cp_parser_template_argument (parser);
11206 /* If the next token is an ellipsis, we're expanding a template
11208 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11210 if (argument == error_mark_node)
11212 cp_token *token = cp_lexer_peek_token (parser->lexer);
11213 error_at (token->location,
11214 "expected parameter pack before %<...%>");
11216 /* Consume the `...' token. */
11217 cp_lexer_consume_token (parser->lexer);
11219 /* Make the argument into a TYPE_PACK_EXPANSION or
11220 EXPR_PACK_EXPANSION. */
11221 argument = make_pack_expansion (argument);
11224 if (n_args == alloced)
11228 if (arg_ary == fixed_args)
11230 arg_ary = XNEWVEC (tree, alloced);
11231 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11234 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11236 arg_ary[n_args++] = argument;
11238 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11240 vec = make_tree_vec (n_args);
11243 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11245 if (arg_ary != fixed_args)
11247 parser->non_integral_constant_expression_p = saved_non_ice_p;
11248 parser->integral_constant_expression_p = saved_ice_p;
11249 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11250 #ifdef ENABLE_CHECKING
11251 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11256 /* Parse a template-argument.
11259 assignment-expression
11263 The representation is that of an assignment-expression, type-id, or
11264 id-expression -- except that the qualified id-expression is
11265 evaluated, so that the value returned is either a DECL or an
11268 Although the standard says "assignment-expression", it forbids
11269 throw-expressions or assignments in the template argument.
11270 Therefore, we use "conditional-expression" instead. */
11273 cp_parser_template_argument (cp_parser* parser)
11278 bool maybe_type_id = false;
11279 cp_token *token = NULL, *argument_start_token = NULL;
11282 /* There's really no way to know what we're looking at, so we just
11283 try each alternative in order.
11287 In a template-argument, an ambiguity between a type-id and an
11288 expression is resolved to a type-id, regardless of the form of
11289 the corresponding template-parameter.
11291 Therefore, we try a type-id first. */
11292 cp_parser_parse_tentatively (parser);
11293 argument = cp_parser_template_type_arg (parser);
11294 /* If there was no error parsing the type-id but the next token is a
11295 '>>', our behavior depends on which dialect of C++ we're
11296 parsing. In C++98, we probably found a typo for '> >'. But there
11297 are type-id which are also valid expressions. For instance:
11299 struct X { int operator >> (int); };
11300 template <int V> struct Foo {};
11303 Here 'X()' is a valid type-id of a function type, but the user just
11304 wanted to write the expression "X() >> 5". Thus, we remember that we
11305 found a valid type-id, but we still try to parse the argument as an
11306 expression to see what happens.
11308 In C++0x, the '>>' will be considered two separate '>'
11310 if (!cp_parser_error_occurred (parser)
11311 && cxx_dialect == cxx98
11312 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11314 maybe_type_id = true;
11315 cp_parser_abort_tentative_parse (parser);
11319 /* If the next token isn't a `,' or a `>', then this argument wasn't
11320 really finished. This means that the argument is not a valid
11322 if (!cp_parser_next_token_ends_template_argument_p (parser))
11323 cp_parser_error (parser, "expected template-argument");
11324 /* If that worked, we're done. */
11325 if (cp_parser_parse_definitely (parser))
11328 /* We're still not sure what the argument will be. */
11329 cp_parser_parse_tentatively (parser);
11330 /* Try a template. */
11331 argument_start_token = cp_lexer_peek_token (parser->lexer);
11332 argument = cp_parser_id_expression (parser,
11333 /*template_keyword_p=*/false,
11334 /*check_dependency_p=*/true,
11336 /*declarator_p=*/false,
11337 /*optional_p=*/false);
11338 /* If the next token isn't a `,' or a `>', then this argument wasn't
11339 really finished. */
11340 if (!cp_parser_next_token_ends_template_argument_p (parser))
11341 cp_parser_error (parser, "expected template-argument");
11342 if (!cp_parser_error_occurred (parser))
11344 /* Figure out what is being referred to. If the id-expression
11345 was for a class template specialization, then we will have a
11346 TYPE_DECL at this point. There is no need to do name lookup
11347 at this point in that case. */
11348 if (TREE_CODE (argument) != TYPE_DECL)
11349 argument = cp_parser_lookup_name (parser, argument,
11351 /*is_template=*/template_p,
11352 /*is_namespace=*/false,
11353 /*check_dependency=*/true,
11354 /*ambiguous_decls=*/NULL,
11355 argument_start_token->location);
11356 if (TREE_CODE (argument) != TEMPLATE_DECL
11357 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11358 cp_parser_error (parser, "expected template-name");
11360 if (cp_parser_parse_definitely (parser))
11362 /* It must be a non-type argument. There permitted cases are given
11363 in [temp.arg.nontype]:
11365 -- an integral constant-expression of integral or enumeration
11368 -- the name of a non-type template-parameter; or
11370 -- the name of an object or function with external linkage...
11372 -- the address of an object or function with external linkage...
11374 -- a pointer to member... */
11375 /* Look for a non-type template parameter. */
11376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11378 cp_parser_parse_tentatively (parser);
11379 argument = cp_parser_primary_expression (parser,
11380 /*address_p=*/false,
11382 /*template_arg_p=*/true,
11384 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11385 || !cp_parser_next_token_ends_template_argument_p (parser))
11386 cp_parser_simulate_error (parser);
11387 if (cp_parser_parse_definitely (parser))
11391 /* If the next token is "&", the argument must be the address of an
11392 object or function with external linkage. */
11393 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11395 cp_lexer_consume_token (parser->lexer);
11396 /* See if we might have an id-expression. */
11397 token = cp_lexer_peek_token (parser->lexer);
11398 if (token->type == CPP_NAME
11399 || token->keyword == RID_OPERATOR
11400 || token->type == CPP_SCOPE
11401 || token->type == CPP_TEMPLATE_ID
11402 || token->type == CPP_NESTED_NAME_SPECIFIER)
11404 cp_parser_parse_tentatively (parser);
11405 argument = cp_parser_primary_expression (parser,
11408 /*template_arg_p=*/true,
11410 if (cp_parser_error_occurred (parser)
11411 || !cp_parser_next_token_ends_template_argument_p (parser))
11412 cp_parser_abort_tentative_parse (parser);
11417 if (TREE_CODE (argument) == INDIRECT_REF)
11419 gcc_assert (REFERENCE_REF_P (argument));
11420 argument = TREE_OPERAND (argument, 0);
11423 /* If we're in a template, we represent a qualified-id referring
11424 to a static data member as a SCOPE_REF even if the scope isn't
11425 dependent so that we can check access control later. */
11427 if (TREE_CODE (probe) == SCOPE_REF)
11428 probe = TREE_OPERAND (probe, 1);
11429 if (TREE_CODE (probe) == VAR_DECL)
11431 /* A variable without external linkage might still be a
11432 valid constant-expression, so no error is issued here
11433 if the external-linkage check fails. */
11434 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11435 cp_parser_simulate_error (parser);
11437 else if (is_overloaded_fn (argument))
11438 /* All overloaded functions are allowed; if the external
11439 linkage test does not pass, an error will be issued
11443 && (TREE_CODE (argument) == OFFSET_REF
11444 || TREE_CODE (argument) == SCOPE_REF))
11445 /* A pointer-to-member. */
11447 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11450 cp_parser_simulate_error (parser);
11452 if (cp_parser_parse_definitely (parser))
11455 argument = build_x_unary_op (ADDR_EXPR, argument,
11456 tf_warning_or_error);
11461 /* If the argument started with "&", there are no other valid
11462 alternatives at this point. */
11465 cp_parser_error (parser, "invalid non-type template argument");
11466 return error_mark_node;
11469 /* If the argument wasn't successfully parsed as a type-id followed
11470 by '>>', the argument can only be a constant expression now.
11471 Otherwise, we try parsing the constant-expression tentatively,
11472 because the argument could really be a type-id. */
11474 cp_parser_parse_tentatively (parser);
11475 argument = cp_parser_constant_expression (parser,
11476 /*allow_non_constant_p=*/false,
11477 /*non_constant_p=*/NULL);
11478 argument = fold_non_dependent_expr (argument);
11479 if (!maybe_type_id)
11481 if (!cp_parser_next_token_ends_template_argument_p (parser))
11482 cp_parser_error (parser, "expected template-argument");
11483 if (cp_parser_parse_definitely (parser))
11485 /* We did our best to parse the argument as a non type-id, but that
11486 was the only alternative that matched (albeit with a '>' after
11487 it). We can assume it's just a typo from the user, and a
11488 diagnostic will then be issued. */
11489 return cp_parser_template_type_arg (parser);
11492 /* Parse an explicit-instantiation.
11494 explicit-instantiation:
11495 template declaration
11497 Although the standard says `declaration', what it really means is:
11499 explicit-instantiation:
11500 template decl-specifier-seq [opt] declarator [opt] ;
11502 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11503 supposed to be allowed. A defect report has been filed about this
11508 explicit-instantiation:
11509 storage-class-specifier template
11510 decl-specifier-seq [opt] declarator [opt] ;
11511 function-specifier template
11512 decl-specifier-seq [opt] declarator [opt] ; */
11515 cp_parser_explicit_instantiation (cp_parser* parser)
11517 int declares_class_or_enum;
11518 cp_decl_specifier_seq decl_specifiers;
11519 tree extension_specifier = NULL_TREE;
11521 /* Look for an (optional) storage-class-specifier or
11522 function-specifier. */
11523 if (cp_parser_allow_gnu_extensions_p (parser))
11525 extension_specifier
11526 = cp_parser_storage_class_specifier_opt (parser);
11527 if (!extension_specifier)
11528 extension_specifier
11529 = cp_parser_function_specifier_opt (parser,
11530 /*decl_specs=*/NULL);
11533 /* Look for the `template' keyword. */
11534 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11535 /* Let the front end know that we are processing an explicit
11537 begin_explicit_instantiation ();
11538 /* [temp.explicit] says that we are supposed to ignore access
11539 control while processing explicit instantiation directives. */
11540 push_deferring_access_checks (dk_no_check);
11541 /* Parse a decl-specifier-seq. */
11542 cp_parser_decl_specifier_seq (parser,
11543 CP_PARSER_FLAGS_OPTIONAL,
11545 &declares_class_or_enum);
11546 /* If there was exactly one decl-specifier, and it declared a class,
11547 and there's no declarator, then we have an explicit type
11549 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11553 type = check_tag_decl (&decl_specifiers);
11554 /* Turn access control back on for names used during
11555 template instantiation. */
11556 pop_deferring_access_checks ();
11558 do_type_instantiation (type, extension_specifier,
11559 /*complain=*/tf_error);
11563 cp_declarator *declarator;
11566 /* Parse the declarator. */
11568 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11569 /*ctor_dtor_or_conv_p=*/NULL,
11570 /*parenthesized_p=*/NULL,
11571 /*member_p=*/false);
11572 if (declares_class_or_enum & 2)
11573 cp_parser_check_for_definition_in_return_type (declarator,
11574 decl_specifiers.type,
11575 decl_specifiers.type_location);
11576 if (declarator != cp_error_declarator)
11578 decl = grokdeclarator (declarator, &decl_specifiers,
11579 NORMAL, 0, &decl_specifiers.attributes);
11580 /* Turn access control back on for names used during
11581 template instantiation. */
11582 pop_deferring_access_checks ();
11583 /* Do the explicit instantiation. */
11584 do_decl_instantiation (decl, extension_specifier);
11588 pop_deferring_access_checks ();
11589 /* Skip the body of the explicit instantiation. */
11590 cp_parser_skip_to_end_of_statement (parser);
11593 /* We're done with the instantiation. */
11594 end_explicit_instantiation ();
11596 cp_parser_consume_semicolon_at_end_of_statement (parser);
11599 /* Parse an explicit-specialization.
11601 explicit-specialization:
11602 template < > declaration
11604 Although the standard says `declaration', what it really means is:
11606 explicit-specialization:
11607 template <> decl-specifier [opt] init-declarator [opt] ;
11608 template <> function-definition
11609 template <> explicit-specialization
11610 template <> template-declaration */
11613 cp_parser_explicit_specialization (cp_parser* parser)
11615 bool need_lang_pop;
11616 cp_token *token = cp_lexer_peek_token (parser->lexer);
11618 /* Look for the `template' keyword. */
11619 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11620 /* Look for the `<'. */
11621 cp_parser_require (parser, CPP_LESS, "%<<%>");
11622 /* Look for the `>'. */
11623 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11624 /* We have processed another parameter list. */
11625 ++parser->num_template_parameter_lists;
11628 A template ... explicit specialization ... shall not have C
11630 if (current_lang_name == lang_name_c)
11632 error_at (token->location, "template specialization with C linkage");
11633 /* Give it C++ linkage to avoid confusing other parts of the
11635 push_lang_context (lang_name_cplusplus);
11636 need_lang_pop = true;
11639 need_lang_pop = false;
11640 /* Let the front end know that we are beginning a specialization. */
11641 if (!begin_specialization ())
11643 end_specialization ();
11647 /* If the next keyword is `template', we need to figure out whether
11648 or not we're looking a template-declaration. */
11649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11651 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11652 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11653 cp_parser_template_declaration_after_export (parser,
11654 /*member_p=*/false);
11656 cp_parser_explicit_specialization (parser);
11659 /* Parse the dependent declaration. */
11660 cp_parser_single_declaration (parser,
11662 /*member_p=*/false,
11663 /*explicit_specialization_p=*/true,
11664 /*friend_p=*/NULL);
11665 /* We're done with the specialization. */
11666 end_specialization ();
11667 /* For the erroneous case of a template with C linkage, we pushed an
11668 implicit C++ linkage scope; exit that scope now. */
11670 pop_lang_context ();
11671 /* We're done with this parameter list. */
11672 --parser->num_template_parameter_lists;
11675 /* Parse a type-specifier.
11678 simple-type-specifier
11681 elaborated-type-specifier
11689 Returns a representation of the type-specifier. For a
11690 class-specifier, enum-specifier, or elaborated-type-specifier, a
11691 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11693 The parser flags FLAGS is used to control type-specifier parsing.
11695 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11696 in a decl-specifier-seq.
11698 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11699 class-specifier, enum-specifier, or elaborated-type-specifier, then
11700 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11701 if a type is declared; 2 if it is defined. Otherwise, it is set to
11704 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11705 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11706 is set to FALSE. */
11709 cp_parser_type_specifier (cp_parser* parser,
11710 cp_parser_flags flags,
11711 cp_decl_specifier_seq *decl_specs,
11712 bool is_declaration,
11713 int* declares_class_or_enum,
11714 bool* is_cv_qualifier)
11716 tree type_spec = NULL_TREE;
11719 cp_decl_spec ds = ds_last;
11721 /* Assume this type-specifier does not declare a new type. */
11722 if (declares_class_or_enum)
11723 *declares_class_or_enum = 0;
11724 /* And that it does not specify a cv-qualifier. */
11725 if (is_cv_qualifier)
11726 *is_cv_qualifier = false;
11727 /* Peek at the next token. */
11728 token = cp_lexer_peek_token (parser->lexer);
11730 /* If we're looking at a keyword, we can use that to guide the
11731 production we choose. */
11732 keyword = token->keyword;
11736 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11737 goto elaborated_type_specifier;
11739 /* Look for the enum-specifier. */
11740 type_spec = cp_parser_enum_specifier (parser);
11741 /* If that worked, we're done. */
11744 if (declares_class_or_enum)
11745 *declares_class_or_enum = 2;
11747 cp_parser_set_decl_spec_type (decl_specs,
11750 /*user_defined_p=*/true);
11754 goto elaborated_type_specifier;
11756 /* Any of these indicate either a class-specifier, or an
11757 elaborated-type-specifier. */
11761 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11762 goto elaborated_type_specifier;
11764 /* Parse tentatively so that we can back up if we don't find a
11765 class-specifier. */
11766 cp_parser_parse_tentatively (parser);
11767 /* Look for the class-specifier. */
11768 type_spec = cp_parser_class_specifier (parser);
11769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11770 /* If that worked, we're done. */
11771 if (cp_parser_parse_definitely (parser))
11773 if (declares_class_or_enum)
11774 *declares_class_or_enum = 2;
11776 cp_parser_set_decl_spec_type (decl_specs,
11779 /*user_defined_p=*/true);
11783 /* Fall through. */
11784 elaborated_type_specifier:
11785 /* We're declaring (not defining) a class or enum. */
11786 if (declares_class_or_enum)
11787 *declares_class_or_enum = 1;
11789 /* Fall through. */
11791 /* Look for an elaborated-type-specifier. */
11793 = (cp_parser_elaborated_type_specifier
11795 decl_specs && decl_specs->specs[(int) ds_friend],
11798 cp_parser_set_decl_spec_type (decl_specs,
11801 /*user_defined_p=*/true);
11806 if (is_cv_qualifier)
11807 *is_cv_qualifier = true;
11812 if (is_cv_qualifier)
11813 *is_cv_qualifier = true;
11818 if (is_cv_qualifier)
11819 *is_cv_qualifier = true;
11823 /* The `__complex__' keyword is a GNU extension. */
11831 /* Handle simple keywords. */
11836 ++decl_specs->specs[(int)ds];
11837 decl_specs->any_specifiers_p = true;
11839 return cp_lexer_consume_token (parser->lexer)->u.value;
11842 /* If we do not already have a type-specifier, assume we are looking
11843 at a simple-type-specifier. */
11844 type_spec = cp_parser_simple_type_specifier (parser,
11848 /* If we didn't find a type-specifier, and a type-specifier was not
11849 optional in this context, issue an error message. */
11850 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11852 cp_parser_error (parser, "expected type specifier");
11853 return error_mark_node;
11859 /* Parse a simple-type-specifier.
11861 simple-type-specifier:
11862 :: [opt] nested-name-specifier [opt] type-name
11863 :: [opt] nested-name-specifier template template-id
11878 simple-type-specifier:
11880 decltype ( expression )
11886 simple-type-specifier:
11887 __typeof__ unary-expression
11888 __typeof__ ( type-id )
11890 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11891 appropriately updated. */
11894 cp_parser_simple_type_specifier (cp_parser* parser,
11895 cp_decl_specifier_seq *decl_specs,
11896 cp_parser_flags flags)
11898 tree type = NULL_TREE;
11901 /* Peek at the next token. */
11902 token = cp_lexer_peek_token (parser->lexer);
11904 /* If we're looking at a keyword, things are easy. */
11905 switch (token->keyword)
11909 decl_specs->explicit_char_p = true;
11910 type = char_type_node;
11913 type = char16_type_node;
11916 type = char32_type_node;
11919 type = wchar_type_node;
11922 type = boolean_type_node;
11926 ++decl_specs->specs[(int) ds_short];
11927 type = short_integer_type_node;
11931 decl_specs->explicit_int_p = true;
11932 type = integer_type_node;
11936 ++decl_specs->specs[(int) ds_long];
11937 type = long_integer_type_node;
11941 ++decl_specs->specs[(int) ds_signed];
11942 type = integer_type_node;
11946 ++decl_specs->specs[(int) ds_unsigned];
11947 type = unsigned_type_node;
11950 type = float_type_node;
11953 type = double_type_node;
11956 type = void_type_node;
11960 maybe_warn_cpp0x (CPP0X_AUTO);
11961 type = make_auto ();
11965 /* Parse the `decltype' type. */
11966 type = cp_parser_decltype (parser);
11969 cp_parser_set_decl_spec_type (decl_specs, type,
11971 /*user_defined_p=*/true);
11976 /* Consume the `typeof' token. */
11977 cp_lexer_consume_token (parser->lexer);
11978 /* Parse the operand to `typeof'. */
11979 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11980 /* If it is not already a TYPE, take its type. */
11981 if (!TYPE_P (type))
11982 type = finish_typeof (type);
11985 cp_parser_set_decl_spec_type (decl_specs, type,
11987 /*user_defined_p=*/true);
11995 /* If the type-specifier was for a built-in type, we're done. */
11998 /* Record the type. */
12000 && (token->keyword != RID_SIGNED
12001 && token->keyword != RID_UNSIGNED
12002 && token->keyword != RID_SHORT
12003 && token->keyword != RID_LONG))
12004 cp_parser_set_decl_spec_type (decl_specs,
12007 /*user_defined=*/false);
12009 decl_specs->any_specifiers_p = true;
12011 /* Consume the token. */
12012 cp_lexer_consume_token (parser->lexer);
12014 /* There is no valid C++ program where a non-template type is
12015 followed by a "<". That usually indicates that the user thought
12016 that the type was a template. */
12017 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12019 return TYPE_NAME (type);
12022 /* The type-specifier must be a user-defined type. */
12023 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12028 /* Don't gobble tokens or issue error messages if this is an
12029 optional type-specifier. */
12030 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12031 cp_parser_parse_tentatively (parser);
12033 /* Look for the optional `::' operator. */
12035 = (cp_parser_global_scope_opt (parser,
12036 /*current_scope_valid_p=*/false)
12038 /* Look for the nested-name specifier. */
12040 = (cp_parser_nested_name_specifier_opt (parser,
12041 /*typename_keyword_p=*/false,
12042 /*check_dependency_p=*/true,
12044 /*is_declaration=*/false)
12046 token = cp_lexer_peek_token (parser->lexer);
12047 /* If we have seen a nested-name-specifier, and the next token
12048 is `template', then we are using the template-id production. */
12050 && cp_parser_optional_template_keyword (parser))
12052 /* Look for the template-id. */
12053 type = cp_parser_template_id (parser,
12054 /*template_keyword_p=*/true,
12055 /*check_dependency_p=*/true,
12056 /*is_declaration=*/false);
12057 /* If the template-id did not name a type, we are out of
12059 if (TREE_CODE (type) != TYPE_DECL)
12061 cp_parser_error (parser, "expected template-id for type");
12065 /* Otherwise, look for a type-name. */
12067 type = cp_parser_type_name (parser);
12068 /* Keep track of all name-lookups performed in class scopes. */
12072 && TREE_CODE (type) == TYPE_DECL
12073 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12074 maybe_note_name_used_in_class (DECL_NAME (type), type);
12075 /* If it didn't work out, we don't have a TYPE. */
12076 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12077 && !cp_parser_parse_definitely (parser))
12079 if (type && decl_specs)
12080 cp_parser_set_decl_spec_type (decl_specs, type,
12082 /*user_defined=*/true);
12085 /* If we didn't get a type-name, issue an error message. */
12086 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12088 cp_parser_error (parser, "expected type-name");
12089 return error_mark_node;
12092 /* There is no valid C++ program where a non-template type is
12093 followed by a "<". That usually indicates that the user thought
12094 that the type was a template. */
12095 if (type && type != error_mark_node)
12097 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12098 If it is, then the '<'...'>' enclose protocol names rather than
12099 template arguments, and so everything is fine. */
12100 if (c_dialect_objc ()
12101 && (objc_is_id (type) || objc_is_class_name (type)))
12103 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12104 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12106 /* Clobber the "unqualified" type previously entered into
12107 DECL_SPECS with the new, improved protocol-qualified version. */
12109 decl_specs->type = qual_type;
12114 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12121 /* Parse a type-name.
12134 Returns a TYPE_DECL for the type. */
12137 cp_parser_type_name (cp_parser* parser)
12141 /* We can't know yet whether it is a class-name or not. */
12142 cp_parser_parse_tentatively (parser);
12143 /* Try a class-name. */
12144 type_decl = cp_parser_class_name (parser,
12145 /*typename_keyword_p=*/false,
12146 /*template_keyword_p=*/false,
12148 /*check_dependency_p=*/true,
12149 /*class_head_p=*/false,
12150 /*is_declaration=*/false);
12151 /* If it's not a class-name, keep looking. */
12152 if (!cp_parser_parse_definitely (parser))
12154 /* It must be a typedef-name or an enum-name. */
12155 return cp_parser_nonclass_name (parser);
12161 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12169 Returns a TYPE_DECL for the type. */
12172 cp_parser_nonclass_name (cp_parser* parser)
12177 cp_token *token = cp_lexer_peek_token (parser->lexer);
12178 identifier = cp_parser_identifier (parser);
12179 if (identifier == error_mark_node)
12180 return error_mark_node;
12182 /* Look up the type-name. */
12183 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12185 if (TREE_CODE (type_decl) != TYPE_DECL
12186 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12188 /* See if this is an Objective-C type. */
12189 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12190 tree type = objc_get_protocol_qualified_type (identifier, protos);
12192 type_decl = TYPE_NAME (type);
12195 /* Issue an error if we did not find a type-name. */
12196 if (TREE_CODE (type_decl) != TYPE_DECL)
12198 if (!cp_parser_simulate_error (parser))
12199 cp_parser_name_lookup_error (parser, identifier, type_decl,
12200 "is not a type", token->location);
12201 return error_mark_node;
12203 /* Remember that the name was used in the definition of the
12204 current class so that we can check later to see if the
12205 meaning would have been different after the class was
12206 entirely defined. */
12207 else if (type_decl != error_mark_node
12209 maybe_note_name_used_in_class (identifier, type_decl);
12214 /* Parse an elaborated-type-specifier. Note that the grammar given
12215 here incorporates the resolution to DR68.
12217 elaborated-type-specifier:
12218 class-key :: [opt] nested-name-specifier [opt] identifier
12219 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12220 enum-key :: [opt] nested-name-specifier [opt] identifier
12221 typename :: [opt] nested-name-specifier identifier
12222 typename :: [opt] nested-name-specifier template [opt]
12227 elaborated-type-specifier:
12228 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12229 class-key attributes :: [opt] nested-name-specifier [opt]
12230 template [opt] template-id
12231 enum attributes :: [opt] nested-name-specifier [opt] identifier
12233 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12234 declared `friend'. If IS_DECLARATION is TRUE, then this
12235 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12236 something is being declared.
12238 Returns the TYPE specified. */
12241 cp_parser_elaborated_type_specifier (cp_parser* parser,
12243 bool is_declaration)
12245 enum tag_types tag_type;
12247 tree type = NULL_TREE;
12248 tree attributes = NULL_TREE;
12250 cp_token *token = NULL;
12252 /* See if we're looking at the `enum' keyword. */
12253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12255 /* Consume the `enum' token. */
12256 cp_lexer_consume_token (parser->lexer);
12257 /* Remember that it's an enumeration type. */
12258 tag_type = enum_type;
12259 /* Parse the optional `struct' or `class' key (for C++0x scoped
12261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12262 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12264 if (cxx_dialect == cxx98)
12265 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12267 /* Consume the `struct' or `class'. */
12268 cp_lexer_consume_token (parser->lexer);
12270 /* Parse the attributes. */
12271 attributes = cp_parser_attributes_opt (parser);
12273 /* Or, it might be `typename'. */
12274 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12277 /* Consume the `typename' token. */
12278 cp_lexer_consume_token (parser->lexer);
12279 /* Remember that it's a `typename' type. */
12280 tag_type = typename_type;
12282 /* Otherwise it must be a class-key. */
12285 tag_type = cp_parser_class_key (parser);
12286 if (tag_type == none_type)
12287 return error_mark_node;
12288 /* Parse the attributes. */
12289 attributes = cp_parser_attributes_opt (parser);
12292 /* Look for the `::' operator. */
12293 globalscope = cp_parser_global_scope_opt (parser,
12294 /*current_scope_valid_p=*/false);
12295 /* Look for the nested-name-specifier. */
12296 if (tag_type == typename_type && !globalscope)
12298 if (!cp_parser_nested_name_specifier (parser,
12299 /*typename_keyword_p=*/true,
12300 /*check_dependency_p=*/true,
12303 return error_mark_node;
12306 /* Even though `typename' is not present, the proposed resolution
12307 to Core Issue 180 says that in `class A<T>::B', `B' should be
12308 considered a type-name, even if `A<T>' is dependent. */
12309 cp_parser_nested_name_specifier_opt (parser,
12310 /*typename_keyword_p=*/true,
12311 /*check_dependency_p=*/true,
12314 /* For everything but enumeration types, consider a template-id.
12315 For an enumeration type, consider only a plain identifier. */
12316 if (tag_type != enum_type)
12318 bool template_p = false;
12321 /* Allow the `template' keyword. */
12322 template_p = cp_parser_optional_template_keyword (parser);
12323 /* If we didn't see `template', we don't know if there's a
12324 template-id or not. */
12326 cp_parser_parse_tentatively (parser);
12327 /* Parse the template-id. */
12328 token = cp_lexer_peek_token (parser->lexer);
12329 decl = cp_parser_template_id (parser, template_p,
12330 /*check_dependency_p=*/true,
12332 /* If we didn't find a template-id, look for an ordinary
12334 if (!template_p && !cp_parser_parse_definitely (parser))
12336 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12337 in effect, then we must assume that, upon instantiation, the
12338 template will correspond to a class. */
12339 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12340 && tag_type == typename_type)
12341 type = make_typename_type (parser->scope, decl,
12343 /*complain=*/tf_error);
12344 /* If the `typename' keyword is in effect and DECL is not a type
12345 decl. Then type is non existant. */
12346 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12349 type = TREE_TYPE (decl);
12354 token = cp_lexer_peek_token (parser->lexer);
12355 identifier = cp_parser_identifier (parser);
12357 if (identifier == error_mark_node)
12359 parser->scope = NULL_TREE;
12360 return error_mark_node;
12363 /* For a `typename', we needn't call xref_tag. */
12364 if (tag_type == typename_type
12365 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12366 return cp_parser_make_typename_type (parser, parser->scope,
12369 /* Look up a qualified name in the usual way. */
12373 tree ambiguous_decls;
12375 decl = cp_parser_lookup_name (parser, identifier,
12377 /*is_template=*/false,
12378 /*is_namespace=*/false,
12379 /*check_dependency=*/true,
12383 /* If the lookup was ambiguous, an error will already have been
12385 if (ambiguous_decls)
12386 return error_mark_node;
12388 /* If we are parsing friend declaration, DECL may be a
12389 TEMPLATE_DECL tree node here. However, we need to check
12390 whether this TEMPLATE_DECL results in valid code. Consider
12391 the following example:
12394 template <class T> class C {};
12397 template <class T> friend class N::C; // #1, valid code
12399 template <class T> class Y {
12400 friend class N::C; // #2, invalid code
12403 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12404 name lookup of `N::C'. We see that friend declaration must
12405 be template for the code to be valid. Note that
12406 processing_template_decl does not work here since it is
12407 always 1 for the above two cases. */
12409 decl = (cp_parser_maybe_treat_template_as_class
12410 (decl, /*tag_name_p=*/is_friend
12411 && parser->num_template_parameter_lists));
12413 if (TREE_CODE (decl) != TYPE_DECL)
12415 cp_parser_diagnose_invalid_type_name (parser,
12419 return error_mark_node;
12422 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12424 bool allow_template = (parser->num_template_parameter_lists
12425 || DECL_SELF_REFERENCE_P (decl));
12426 type = check_elaborated_type_specifier (tag_type, decl,
12429 if (type == error_mark_node)
12430 return error_mark_node;
12433 /* Forward declarations of nested types, such as
12438 are invalid unless all components preceding the final '::'
12439 are complete. If all enclosing types are complete, these
12440 declarations become merely pointless.
12442 Invalid forward declarations of nested types are errors
12443 caught elsewhere in parsing. Those that are pointless arrive
12446 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12447 && !is_friend && !processing_explicit_instantiation)
12448 warning (0, "declaration %qD does not declare anything", decl);
12450 type = TREE_TYPE (decl);
12454 /* An elaborated-type-specifier sometimes introduces a new type and
12455 sometimes names an existing type. Normally, the rule is that it
12456 introduces a new type only if there is not an existing type of
12457 the same name already in scope. For example, given:
12460 void f() { struct S s; }
12462 the `struct S' in the body of `f' is the same `struct S' as in
12463 the global scope; the existing definition is used. However, if
12464 there were no global declaration, this would introduce a new
12465 local class named `S'.
12467 An exception to this rule applies to the following code:
12469 namespace N { struct S; }
12471 Here, the elaborated-type-specifier names a new type
12472 unconditionally; even if there is already an `S' in the
12473 containing scope this declaration names a new type.
12474 This exception only applies if the elaborated-type-specifier
12475 forms the complete declaration:
12479 A declaration consisting solely of `class-key identifier ;' is
12480 either a redeclaration of the name in the current scope or a
12481 forward declaration of the identifier as a class name. It
12482 introduces the name into the current scope.
12484 We are in this situation precisely when the next token is a `;'.
12486 An exception to the exception is that a `friend' declaration does
12487 *not* name a new type; i.e., given:
12489 struct S { friend struct T; };
12491 `T' is not a new type in the scope of `S'.
12493 Also, `new struct S' or `sizeof (struct S)' never results in the
12494 definition of a new type; a new type can only be declared in a
12495 declaration context. */
12501 /* Friends have special name lookup rules. */
12502 ts = ts_within_enclosing_non_class;
12503 else if (is_declaration
12504 && cp_lexer_next_token_is (parser->lexer,
12506 /* This is a `class-key identifier ;' */
12512 (parser->num_template_parameter_lists
12513 && (cp_parser_next_token_starts_class_definition_p (parser)
12514 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12515 /* An unqualified name was used to reference this type, so
12516 there were no qualifying templates. */
12517 if (!cp_parser_check_template_parameters (parser,
12518 /*num_templates=*/0,
12520 /*declarator=*/NULL))
12521 return error_mark_node;
12522 type = xref_tag (tag_type, identifier, ts, template_p);
12526 if (type == error_mark_node)
12527 return error_mark_node;
12529 /* Allow attributes on forward declarations of classes. */
12532 if (TREE_CODE (type) == TYPENAME_TYPE)
12533 warning (OPT_Wattributes,
12534 "attributes ignored on uninstantiated type");
12535 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12536 && ! processing_explicit_instantiation)
12537 warning (OPT_Wattributes,
12538 "attributes ignored on template instantiation");
12539 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12540 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12542 warning (OPT_Wattributes,
12543 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12546 if (tag_type != enum_type)
12547 cp_parser_check_class_key (tag_type, type);
12549 /* A "<" cannot follow an elaborated type specifier. If that
12550 happens, the user was probably trying to form a template-id. */
12551 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12556 /* Parse an enum-specifier.
12559 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12564 enum struct [C++0x]
12567 : type-specifier-seq
12570 enum-key attributes[opt] identifier [opt] enum-base [opt]
12571 { enumerator-list [opt] }attributes[opt]
12573 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12574 if the token stream isn't an enum-specifier after all. */
12577 cp_parser_enum_specifier (cp_parser* parser)
12582 bool scoped_enum_p = false;
12583 bool has_underlying_type = false;
12584 tree underlying_type = NULL_TREE;
12586 /* Parse tentatively so that we can back up if we don't find a
12588 cp_parser_parse_tentatively (parser);
12590 /* Caller guarantees that the current token is 'enum', an identifier
12591 possibly follows, and the token after that is an opening brace.
12592 If we don't have an identifier, fabricate an anonymous name for
12593 the enumeration being defined. */
12594 cp_lexer_consume_token (parser->lexer);
12596 /* Parse the "class" or "struct", which indicates a scoped
12597 enumeration type in C++0x. */
12598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12599 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12601 if (cxx_dialect == cxx98)
12602 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12604 /* Consume the `struct' or `class' token. */
12605 cp_lexer_consume_token (parser->lexer);
12607 scoped_enum_p = true;
12610 attributes = cp_parser_attributes_opt (parser);
12612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12613 identifier = cp_parser_identifier (parser);
12615 identifier = make_anon_name ();
12617 /* Check for the `:' that denotes a specified underlying type in C++0x.
12618 Note that a ':' could also indicate a bitfield width, however. */
12619 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12621 cp_decl_specifier_seq type_specifiers;
12623 /* Consume the `:'. */
12624 cp_lexer_consume_token (parser->lexer);
12626 /* Parse the type-specifier-seq. */
12627 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12628 /*is_trailing_return=*/false,
12631 /* At this point this is surely not elaborated type specifier. */
12632 if (!cp_parser_parse_definitely (parser))
12635 if (cxx_dialect == cxx98)
12636 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12638 has_underlying_type = true;
12640 /* If that didn't work, stop. */
12641 if (type_specifiers.type != error_mark_node)
12643 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12644 /*initialized=*/0, NULL);
12645 if (underlying_type == error_mark_node)
12646 underlying_type = NULL_TREE;
12650 /* Look for the `{' but don't consume it yet. */
12651 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12653 cp_parser_error (parser, "expected %<{%>");
12654 if (has_underlying_type)
12658 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12661 /* Issue an error message if type-definitions are forbidden here. */
12662 if (!cp_parser_check_type_definition (parser))
12663 type = error_mark_node;
12665 /* Create the new type. We do this before consuming the opening
12666 brace so the enum will be recorded as being on the line of its
12667 tag (or the 'enum' keyword, if there is no tag). */
12668 type = start_enum (identifier, underlying_type, scoped_enum_p);
12670 /* Consume the opening brace. */
12671 cp_lexer_consume_token (parser->lexer);
12673 if (type == error_mark_node)
12675 cp_parser_skip_to_end_of_block_or_statement (parser);
12676 return error_mark_node;
12679 /* If the next token is not '}', then there are some enumerators. */
12680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12681 cp_parser_enumerator_list (parser, type);
12683 /* Consume the final '}'. */
12684 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12686 /* Look for trailing attributes to apply to this enumeration, and
12687 apply them if appropriate. */
12688 if (cp_parser_allow_gnu_extensions_p (parser))
12690 tree trailing_attr = cp_parser_attributes_opt (parser);
12691 trailing_attr = chainon (trailing_attr, attributes);
12692 cplus_decl_attributes (&type,
12694 (int) ATTR_FLAG_TYPE_IN_PLACE);
12697 /* Finish up the enumeration. */
12698 finish_enum (type);
12703 /* Parse an enumerator-list. The enumerators all have the indicated
12707 enumerator-definition
12708 enumerator-list , enumerator-definition */
12711 cp_parser_enumerator_list (cp_parser* parser, tree type)
12715 /* Parse an enumerator-definition. */
12716 cp_parser_enumerator_definition (parser, type);
12718 /* If the next token is not a ',', we've reached the end of
12720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12722 /* Otherwise, consume the `,' and keep going. */
12723 cp_lexer_consume_token (parser->lexer);
12724 /* If the next token is a `}', there is a trailing comma. */
12725 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12727 if (!in_system_header)
12728 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12734 /* Parse an enumerator-definition. The enumerator has the indicated
12737 enumerator-definition:
12739 enumerator = constant-expression
12745 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12750 /* Look for the identifier. */
12751 identifier = cp_parser_identifier (parser);
12752 if (identifier == error_mark_node)
12755 /* If the next token is an '=', then there is an explicit value. */
12756 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12758 /* Consume the `=' token. */
12759 cp_lexer_consume_token (parser->lexer);
12760 /* Parse the value. */
12761 value = cp_parser_constant_expression (parser,
12762 /*allow_non_constant_p=*/false,
12768 /* If we are processing a template, make sure the initializer of the
12769 enumerator doesn't contain any bare template parameter pack. */
12770 if (check_for_bare_parameter_packs (value))
12771 value = error_mark_node;
12773 /* Create the enumerator. */
12774 build_enumerator (identifier, value, type);
12777 /* Parse a namespace-name.
12780 original-namespace-name
12783 Returns the NAMESPACE_DECL for the namespace. */
12786 cp_parser_namespace_name (cp_parser* parser)
12789 tree namespace_decl;
12791 cp_token *token = cp_lexer_peek_token (parser->lexer);
12793 /* Get the name of the namespace. */
12794 identifier = cp_parser_identifier (parser);
12795 if (identifier == error_mark_node)
12796 return error_mark_node;
12798 /* Look up the identifier in the currently active scope. Look only
12799 for namespaces, due to:
12801 [basic.lookup.udir]
12803 When looking up a namespace-name in a using-directive or alias
12804 definition, only namespace names are considered.
12808 [basic.lookup.qual]
12810 During the lookup of a name preceding the :: scope resolution
12811 operator, object, function, and enumerator names are ignored.
12813 (Note that cp_parser_qualifying_entity only calls this
12814 function if the token after the name is the scope resolution
12816 namespace_decl = cp_parser_lookup_name (parser, identifier,
12818 /*is_template=*/false,
12819 /*is_namespace=*/true,
12820 /*check_dependency=*/true,
12821 /*ambiguous_decls=*/NULL,
12823 /* If it's not a namespace, issue an error. */
12824 if (namespace_decl == error_mark_node
12825 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12827 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12828 error_at (token->location, "%qD is not a namespace-name", identifier);
12829 cp_parser_error (parser, "expected namespace-name");
12830 namespace_decl = error_mark_node;
12833 return namespace_decl;
12836 /* Parse a namespace-definition.
12838 namespace-definition:
12839 named-namespace-definition
12840 unnamed-namespace-definition
12842 named-namespace-definition:
12843 original-namespace-definition
12844 extension-namespace-definition
12846 original-namespace-definition:
12847 namespace identifier { namespace-body }
12849 extension-namespace-definition:
12850 namespace original-namespace-name { namespace-body }
12852 unnamed-namespace-definition:
12853 namespace { namespace-body } */
12856 cp_parser_namespace_definition (cp_parser* parser)
12858 tree identifier, attribs;
12859 bool has_visibility;
12862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12865 cp_lexer_consume_token (parser->lexer);
12870 /* Look for the `namespace' keyword. */
12871 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12873 /* Get the name of the namespace. We do not attempt to distinguish
12874 between an original-namespace-definition and an
12875 extension-namespace-definition at this point. The semantic
12876 analysis routines are responsible for that. */
12877 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12878 identifier = cp_parser_identifier (parser);
12880 identifier = NULL_TREE;
12882 /* Parse any specified attributes. */
12883 attribs = cp_parser_attributes_opt (parser);
12885 /* Look for the `{' to start the namespace. */
12886 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12887 /* Start the namespace. */
12888 push_namespace (identifier);
12890 /* "inline namespace" is equivalent to a stub namespace definition
12891 followed by a strong using directive. */
12894 tree name_space = current_namespace;
12895 /* Set up namespace association. */
12896 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12897 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12898 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12899 /* Import the contents of the inline namespace. */
12901 do_using_directive (name_space);
12902 push_namespace (identifier);
12905 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12907 /* Parse the body of the namespace. */
12908 cp_parser_namespace_body (parser);
12910 #ifdef HANDLE_PRAGMA_VISIBILITY
12911 if (has_visibility)
12912 pop_visibility (1);
12915 /* Finish the namespace. */
12917 /* Look for the final `}'. */
12918 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12921 /* Parse a namespace-body.
12924 declaration-seq [opt] */
12927 cp_parser_namespace_body (cp_parser* parser)
12929 cp_parser_declaration_seq_opt (parser);
12932 /* Parse a namespace-alias-definition.
12934 namespace-alias-definition:
12935 namespace identifier = qualified-namespace-specifier ; */
12938 cp_parser_namespace_alias_definition (cp_parser* parser)
12941 tree namespace_specifier;
12943 cp_token *token = cp_lexer_peek_token (parser->lexer);
12945 /* Look for the `namespace' keyword. */
12946 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12947 /* Look for the identifier. */
12948 identifier = cp_parser_identifier (parser);
12949 if (identifier == error_mark_node)
12951 /* Look for the `=' token. */
12952 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12953 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12955 error_at (token->location, "%<namespace%> definition is not allowed here");
12956 /* Skip the definition. */
12957 cp_lexer_consume_token (parser->lexer);
12958 if (cp_parser_skip_to_closing_brace (parser))
12959 cp_lexer_consume_token (parser->lexer);
12962 cp_parser_require (parser, CPP_EQ, "%<=%>");
12963 /* Look for the qualified-namespace-specifier. */
12964 namespace_specifier
12965 = cp_parser_qualified_namespace_specifier (parser);
12966 /* Look for the `;' token. */
12967 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12969 /* Register the alias in the symbol table. */
12970 do_namespace_alias (identifier, namespace_specifier);
12973 /* Parse a qualified-namespace-specifier.
12975 qualified-namespace-specifier:
12976 :: [opt] nested-name-specifier [opt] namespace-name
12978 Returns a NAMESPACE_DECL corresponding to the specified
12982 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12984 /* Look for the optional `::'. */
12985 cp_parser_global_scope_opt (parser,
12986 /*current_scope_valid_p=*/false);
12988 /* Look for the optional nested-name-specifier. */
12989 cp_parser_nested_name_specifier_opt (parser,
12990 /*typename_keyword_p=*/false,
12991 /*check_dependency_p=*/true,
12993 /*is_declaration=*/true);
12995 return cp_parser_namespace_name (parser);
12998 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12999 access declaration.
13002 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13003 using :: unqualified-id ;
13005 access-declaration:
13011 cp_parser_using_declaration (cp_parser* parser,
13012 bool access_declaration_p)
13015 bool typename_p = false;
13016 bool global_scope_p;
13021 if (access_declaration_p)
13022 cp_parser_parse_tentatively (parser);
13025 /* Look for the `using' keyword. */
13026 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13028 /* Peek at the next token. */
13029 token = cp_lexer_peek_token (parser->lexer);
13030 /* See if it's `typename'. */
13031 if (token->keyword == RID_TYPENAME)
13033 /* Remember that we've seen it. */
13035 /* Consume the `typename' token. */
13036 cp_lexer_consume_token (parser->lexer);
13040 /* Look for the optional global scope qualification. */
13042 = (cp_parser_global_scope_opt (parser,
13043 /*current_scope_valid_p=*/false)
13046 /* If we saw `typename', or didn't see `::', then there must be a
13047 nested-name-specifier present. */
13048 if (typename_p || !global_scope_p)
13049 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13050 /*check_dependency_p=*/true,
13052 /*is_declaration=*/true);
13053 /* Otherwise, we could be in either of the two productions. In that
13054 case, treat the nested-name-specifier as optional. */
13056 qscope = cp_parser_nested_name_specifier_opt (parser,
13057 /*typename_keyword_p=*/false,
13058 /*check_dependency_p=*/true,
13060 /*is_declaration=*/true);
13062 qscope = global_namespace;
13064 if (access_declaration_p && cp_parser_error_occurred (parser))
13065 /* Something has already gone wrong; there's no need to parse
13066 further. Since an error has occurred, the return value of
13067 cp_parser_parse_definitely will be false, as required. */
13068 return cp_parser_parse_definitely (parser);
13070 token = cp_lexer_peek_token (parser->lexer);
13071 /* Parse the unqualified-id. */
13072 identifier = cp_parser_unqualified_id (parser,
13073 /*template_keyword_p=*/false,
13074 /*check_dependency_p=*/true,
13075 /*declarator_p=*/true,
13076 /*optional_p=*/false);
13078 if (access_declaration_p)
13080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13081 cp_parser_simulate_error (parser);
13082 if (!cp_parser_parse_definitely (parser))
13086 /* The function we call to handle a using-declaration is different
13087 depending on what scope we are in. */
13088 if (qscope == error_mark_node || identifier == error_mark_node)
13090 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13091 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13092 /* [namespace.udecl]
13094 A using declaration shall not name a template-id. */
13095 error_at (token->location,
13096 "a template-id may not appear in a using-declaration");
13099 if (at_class_scope_p ())
13101 /* Create the USING_DECL. */
13102 decl = do_class_using_decl (parser->scope, identifier);
13104 if (check_for_bare_parameter_packs (decl))
13107 /* Add it to the list of members in this class. */
13108 finish_member_declaration (decl);
13112 decl = cp_parser_lookup_name_simple (parser,
13115 if (decl == error_mark_node)
13116 cp_parser_name_lookup_error (parser, identifier,
13119 else if (check_for_bare_parameter_packs (decl))
13121 else if (!at_namespace_scope_p ())
13122 do_local_using_decl (decl, qscope, identifier);
13124 do_toplevel_using_decl (decl, qscope, identifier);
13128 /* Look for the final `;'. */
13129 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13134 /* Parse a using-directive.
13137 using namespace :: [opt] nested-name-specifier [opt]
13138 namespace-name ; */
13141 cp_parser_using_directive (cp_parser* parser)
13143 tree namespace_decl;
13146 /* Look for the `using' keyword. */
13147 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13148 /* And the `namespace' keyword. */
13149 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13150 /* Look for the optional `::' operator. */
13151 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13152 /* And the optional nested-name-specifier. */
13153 cp_parser_nested_name_specifier_opt (parser,
13154 /*typename_keyword_p=*/false,
13155 /*check_dependency_p=*/true,
13157 /*is_declaration=*/true);
13158 /* Get the namespace being used. */
13159 namespace_decl = cp_parser_namespace_name (parser);
13160 /* And any specified attributes. */
13161 attribs = cp_parser_attributes_opt (parser);
13162 /* Update the symbol table. */
13163 parse_using_directive (namespace_decl, attribs);
13164 /* Look for the final `;'. */
13165 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13168 /* Parse an asm-definition.
13171 asm ( string-literal ) ;
13176 asm volatile [opt] ( string-literal ) ;
13177 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13178 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13179 : asm-operand-list [opt] ) ;
13180 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13181 : asm-operand-list [opt]
13182 : asm-clobber-list [opt] ) ;
13183 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13184 : asm-clobber-list [opt]
13185 : asm-goto-list ) ; */
13188 cp_parser_asm_definition (cp_parser* parser)
13191 tree outputs = NULL_TREE;
13192 tree inputs = NULL_TREE;
13193 tree clobbers = NULL_TREE;
13194 tree labels = NULL_TREE;
13196 bool volatile_p = false;
13197 bool extended_p = false;
13198 bool invalid_inputs_p = false;
13199 bool invalid_outputs_p = false;
13200 bool goto_p = false;
13201 const char *missing = NULL;
13203 /* Look for the `asm' keyword. */
13204 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13205 /* See if the next token is `volatile'. */
13206 if (cp_parser_allow_gnu_extensions_p (parser)
13207 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13209 /* Remember that we saw the `volatile' keyword. */
13211 /* Consume the token. */
13212 cp_lexer_consume_token (parser->lexer);
13214 if (cp_parser_allow_gnu_extensions_p (parser)
13215 && parser->in_function_body
13216 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13218 /* Remember that we saw the `goto' keyword. */
13220 /* Consume the token. */
13221 cp_lexer_consume_token (parser->lexer);
13223 /* Look for the opening `('. */
13224 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13226 /* Look for the string. */
13227 string = cp_parser_string_literal (parser, false, false);
13228 if (string == error_mark_node)
13230 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13231 /*consume_paren=*/true);
13235 /* If we're allowing GNU extensions, check for the extended assembly
13236 syntax. Unfortunately, the `:' tokens need not be separated by
13237 a space in C, and so, for compatibility, we tolerate that here
13238 too. Doing that means that we have to treat the `::' operator as
13240 if (cp_parser_allow_gnu_extensions_p (parser)
13241 && parser->in_function_body
13242 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13243 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13245 bool inputs_p = false;
13246 bool clobbers_p = false;
13247 bool labels_p = false;
13249 /* The extended syntax was used. */
13252 /* Look for outputs. */
13253 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13255 /* Consume the `:'. */
13256 cp_lexer_consume_token (parser->lexer);
13257 /* Parse the output-operands. */
13258 if (cp_lexer_next_token_is_not (parser->lexer,
13260 && cp_lexer_next_token_is_not (parser->lexer,
13262 && cp_lexer_next_token_is_not (parser->lexer,
13265 outputs = cp_parser_asm_operand_list (parser);
13267 if (outputs == error_mark_node)
13268 invalid_outputs_p = true;
13270 /* If the next token is `::', there are no outputs, and the
13271 next token is the beginning of the inputs. */
13272 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13273 /* The inputs are coming next. */
13276 /* Look for inputs. */
13278 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13280 /* Consume the `:' or `::'. */
13281 cp_lexer_consume_token (parser->lexer);
13282 /* Parse the output-operands. */
13283 if (cp_lexer_next_token_is_not (parser->lexer,
13285 && cp_lexer_next_token_is_not (parser->lexer,
13287 && cp_lexer_next_token_is_not (parser->lexer,
13289 inputs = cp_parser_asm_operand_list (parser);
13291 if (inputs == error_mark_node)
13292 invalid_inputs_p = true;
13294 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13295 /* The clobbers are coming next. */
13298 /* Look for clobbers. */
13300 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13303 /* Consume the `:' or `::'. */
13304 cp_lexer_consume_token (parser->lexer);
13305 /* Parse the clobbers. */
13306 if (cp_lexer_next_token_is_not (parser->lexer,
13308 && cp_lexer_next_token_is_not (parser->lexer,
13310 clobbers = cp_parser_asm_clobber_list (parser);
13313 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13314 /* The labels are coming next. */
13317 /* Look for labels. */
13319 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13322 /* Consume the `:' or `::'. */
13323 cp_lexer_consume_token (parser->lexer);
13324 /* Parse the labels. */
13325 labels = cp_parser_asm_label_list (parser);
13328 if (goto_p && !labels_p)
13329 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13332 missing = "%<:%> or %<::%>";
13334 /* Look for the closing `)'. */
13335 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13336 missing ? missing : "%<)%>"))
13337 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13338 /*consume_paren=*/true);
13339 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13341 if (!invalid_inputs_p && !invalid_outputs_p)
13343 /* Create the ASM_EXPR. */
13344 if (parser->in_function_body)
13346 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13347 inputs, clobbers, labels);
13348 /* If the extended syntax was not used, mark the ASM_EXPR. */
13351 tree temp = asm_stmt;
13352 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13353 temp = TREE_OPERAND (temp, 0);
13355 ASM_INPUT_P (temp) = 1;
13359 cgraph_add_asm_node (string);
13363 /* Declarators [gram.dcl.decl] */
13365 /* Parse an init-declarator.
13368 declarator initializer [opt]
13373 declarator asm-specification [opt] attributes [opt] initializer [opt]
13375 function-definition:
13376 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13378 decl-specifier-seq [opt] declarator function-try-block
13382 function-definition:
13383 __extension__ function-definition
13385 The DECL_SPECIFIERS apply to this declarator. Returns a
13386 representation of the entity declared. If MEMBER_P is TRUE, then
13387 this declarator appears in a class scope. The new DECL created by
13388 this declarator is returned.
13390 The CHECKS are access checks that should be performed once we know
13391 what entity is being declared (and, therefore, what classes have
13394 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13395 for a function-definition here as well. If the declarator is a
13396 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13397 be TRUE upon return. By that point, the function-definition will
13398 have been completely parsed.
13400 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13404 cp_parser_init_declarator (cp_parser* parser,
13405 cp_decl_specifier_seq *decl_specifiers,
13406 VEC (deferred_access_check,gc)* checks,
13407 bool function_definition_allowed_p,
13409 int declares_class_or_enum,
13410 bool* function_definition_p)
13412 cp_token *token = NULL, *asm_spec_start_token = NULL,
13413 *attributes_start_token = NULL;
13414 cp_declarator *declarator;
13415 tree prefix_attributes;
13417 tree asm_specification;
13419 tree decl = NULL_TREE;
13421 int is_initialized;
13422 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13423 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13425 enum cpp_ttype initialization_kind;
13426 bool is_direct_init = false;
13427 bool is_non_constant_init;
13428 int ctor_dtor_or_conv_p;
13430 tree pushed_scope = NULL;
13432 /* Gather the attributes that were provided with the
13433 decl-specifiers. */
13434 prefix_attributes = decl_specifiers->attributes;
13436 /* Assume that this is not the declarator for a function
13438 if (function_definition_p)
13439 *function_definition_p = false;
13441 /* Defer access checks while parsing the declarator; we cannot know
13442 what names are accessible until we know what is being
13444 resume_deferring_access_checks ();
13446 /* Parse the declarator. */
13447 token = cp_lexer_peek_token (parser->lexer);
13449 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13450 &ctor_dtor_or_conv_p,
13451 /*parenthesized_p=*/NULL,
13452 /*member_p=*/false);
13453 /* Gather up the deferred checks. */
13454 stop_deferring_access_checks ();
13456 /* If the DECLARATOR was erroneous, there's no need to go
13458 if (declarator == cp_error_declarator)
13459 return error_mark_node;
13461 /* Check that the number of template-parameter-lists is OK. */
13462 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13464 return error_mark_node;
13466 if (declares_class_or_enum & 2)
13467 cp_parser_check_for_definition_in_return_type (declarator,
13468 decl_specifiers->type,
13469 decl_specifiers->type_location);
13471 /* Figure out what scope the entity declared by the DECLARATOR is
13472 located in. `grokdeclarator' sometimes changes the scope, so
13473 we compute it now. */
13474 scope = get_scope_of_declarator (declarator);
13476 /* If we're allowing GNU extensions, look for an asm-specification
13478 if (cp_parser_allow_gnu_extensions_p (parser))
13480 /* Look for an asm-specification. */
13481 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13482 asm_specification = cp_parser_asm_specification_opt (parser);
13483 /* And attributes. */
13484 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13485 attributes = cp_parser_attributes_opt (parser);
13489 asm_specification = NULL_TREE;
13490 attributes = NULL_TREE;
13493 /* Peek at the next token. */
13494 token = cp_lexer_peek_token (parser->lexer);
13495 /* Check to see if the token indicates the start of a
13496 function-definition. */
13497 if (function_declarator_p (declarator)
13498 && cp_parser_token_starts_function_definition_p (token))
13500 if (!function_definition_allowed_p)
13502 /* If a function-definition should not appear here, issue an
13504 cp_parser_error (parser,
13505 "a function-definition is not allowed here");
13506 return error_mark_node;
13510 location_t func_brace_location
13511 = cp_lexer_peek_token (parser->lexer)->location;
13513 /* Neither attributes nor an asm-specification are allowed
13514 on a function-definition. */
13515 if (asm_specification)
13516 error_at (asm_spec_start_token->location,
13517 "an asm-specification is not allowed "
13518 "on a function-definition");
13520 error_at (attributes_start_token->location,
13521 "attributes are not allowed on a function-definition");
13522 /* This is a function-definition. */
13523 *function_definition_p = true;
13525 /* Parse the function definition. */
13527 decl = cp_parser_save_member_function_body (parser,
13530 prefix_attributes);
13533 = (cp_parser_function_definition_from_specifiers_and_declarator
13534 (parser, decl_specifiers, prefix_attributes, declarator));
13536 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13538 /* This is where the prologue starts... */
13539 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13540 = func_brace_location;
13549 Only in function declarations for constructors, destructors, and
13550 type conversions can the decl-specifier-seq be omitted.
13552 We explicitly postpone this check past the point where we handle
13553 function-definitions because we tolerate function-definitions
13554 that are missing their return types in some modes. */
13555 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13557 cp_parser_error (parser,
13558 "expected constructor, destructor, or type conversion");
13559 return error_mark_node;
13562 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13563 if (token->type == CPP_EQ
13564 || token->type == CPP_OPEN_PAREN
13565 || token->type == CPP_OPEN_BRACE)
13567 is_initialized = SD_INITIALIZED;
13568 initialization_kind = token->type;
13570 if (token->type == CPP_EQ
13571 && function_declarator_p (declarator))
13573 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13574 if (t2->keyword == RID_DEFAULT)
13575 is_initialized = SD_DEFAULTED;
13576 else if (t2->keyword == RID_DELETE)
13577 is_initialized = SD_DELETED;
13582 /* If the init-declarator isn't initialized and isn't followed by a
13583 `,' or `;', it's not a valid init-declarator. */
13584 if (token->type != CPP_COMMA
13585 && token->type != CPP_SEMICOLON)
13587 cp_parser_error (parser, "expected initializer");
13588 return error_mark_node;
13590 is_initialized = SD_UNINITIALIZED;
13591 initialization_kind = CPP_EOF;
13594 /* Because start_decl has side-effects, we should only call it if we
13595 know we're going ahead. By this point, we know that we cannot
13596 possibly be looking at any other construct. */
13597 cp_parser_commit_to_tentative_parse (parser);
13599 /* If the decl specifiers were bad, issue an error now that we're
13600 sure this was intended to be a declarator. Then continue
13601 declaring the variable(s), as int, to try to cut down on further
13603 if (decl_specifiers->any_specifiers_p
13604 && decl_specifiers->type == error_mark_node)
13606 cp_parser_error (parser, "invalid type in declaration");
13607 decl_specifiers->type = integer_type_node;
13610 /* Check to see whether or not this declaration is a friend. */
13611 friend_p = cp_parser_friend_p (decl_specifiers);
13613 /* Enter the newly declared entry in the symbol table. If we're
13614 processing a declaration in a class-specifier, we wait until
13615 after processing the initializer. */
13618 if (parser->in_unbraced_linkage_specification_p)
13619 decl_specifiers->storage_class = sc_extern;
13620 decl = start_decl (declarator, decl_specifiers,
13621 is_initialized, attributes, prefix_attributes,
13625 /* Enter the SCOPE. That way unqualified names appearing in the
13626 initializer will be looked up in SCOPE. */
13627 pushed_scope = push_scope (scope);
13629 /* Perform deferred access control checks, now that we know in which
13630 SCOPE the declared entity resides. */
13631 if (!member_p && decl)
13633 tree saved_current_function_decl = NULL_TREE;
13635 /* If the entity being declared is a function, pretend that we
13636 are in its scope. If it is a `friend', it may have access to
13637 things that would not otherwise be accessible. */
13638 if (TREE_CODE (decl) == FUNCTION_DECL)
13640 saved_current_function_decl = current_function_decl;
13641 current_function_decl = decl;
13644 /* Perform access checks for template parameters. */
13645 cp_parser_perform_template_parameter_access_checks (checks);
13647 /* Perform the access control checks for the declarator and the
13648 decl-specifiers. */
13649 perform_deferred_access_checks ();
13651 /* Restore the saved value. */
13652 if (TREE_CODE (decl) == FUNCTION_DECL)
13653 current_function_decl = saved_current_function_decl;
13656 /* Parse the initializer. */
13657 initializer = NULL_TREE;
13658 is_direct_init = false;
13659 is_non_constant_init = true;
13660 if (is_initialized)
13662 if (function_declarator_p (declarator))
13664 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13665 if (initialization_kind == CPP_EQ)
13666 initializer = cp_parser_pure_specifier (parser);
13669 /* If the declaration was erroneous, we don't really
13670 know what the user intended, so just silently
13671 consume the initializer. */
13672 if (decl != error_mark_node)
13673 error_at (initializer_start_token->location,
13674 "initializer provided for function");
13675 cp_parser_skip_to_closing_parenthesis (parser,
13676 /*recovering=*/true,
13677 /*or_comma=*/false,
13678 /*consume_paren=*/true);
13683 /* We want to record the extra mangling scope for in-class
13684 initializers of class members and initializers of static data
13685 member templates. The former is a C++0x feature which isn't
13686 implemented yet, and I expect it will involve deferring
13687 parsing of the initializer until end of class as with default
13688 arguments. So right here we only handle the latter. */
13689 if (!member_p && processing_template_decl)
13690 start_lambda_scope (decl);
13691 initializer = cp_parser_initializer (parser,
13693 &is_non_constant_init);
13694 if (!member_p && processing_template_decl)
13695 finish_lambda_scope ();
13699 /* The old parser allows attributes to appear after a parenthesized
13700 initializer. Mark Mitchell proposed removing this functionality
13701 on the GCC mailing lists on 2002-08-13. This parser accepts the
13702 attributes -- but ignores them. */
13703 if (cp_parser_allow_gnu_extensions_p (parser)
13704 && initialization_kind == CPP_OPEN_PAREN)
13705 if (cp_parser_attributes_opt (parser))
13706 warning (OPT_Wattributes,
13707 "attributes after parenthesized initializer ignored");
13709 /* For an in-class declaration, use `grokfield' to create the
13715 pop_scope (pushed_scope);
13716 pushed_scope = false;
13718 decl = grokfield (declarator, decl_specifiers,
13719 initializer, !is_non_constant_init,
13720 /*asmspec=*/NULL_TREE,
13721 prefix_attributes);
13722 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13723 cp_parser_save_default_args (parser, decl);
13726 /* Finish processing the declaration. But, skip friend
13728 if (!friend_p && decl && decl != error_mark_node)
13730 cp_finish_decl (decl,
13731 initializer, !is_non_constant_init,
13733 /* If the initializer is in parentheses, then this is
13734 a direct-initialization, which means that an
13735 `explicit' constructor is OK. Otherwise, an
13736 `explicit' constructor cannot be used. */
13737 ((is_direct_init || !is_initialized)
13738 ? 0 : LOOKUP_ONLYCONVERTING));
13740 else if ((cxx_dialect != cxx98) && friend_p
13741 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13742 /* Core issue #226 (C++0x only): A default template-argument
13743 shall not be specified in a friend class template
13745 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13746 /*is_partial=*/0, /*is_friend_decl=*/1);
13748 if (!friend_p && pushed_scope)
13749 pop_scope (pushed_scope);
13754 /* Parse a declarator.
13758 ptr-operator declarator
13760 abstract-declarator:
13761 ptr-operator abstract-declarator [opt]
13762 direct-abstract-declarator
13767 attributes [opt] direct-declarator
13768 attributes [opt] ptr-operator declarator
13770 abstract-declarator:
13771 attributes [opt] ptr-operator abstract-declarator [opt]
13772 attributes [opt] direct-abstract-declarator
13774 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13775 detect constructor, destructor or conversion operators. It is set
13776 to -1 if the declarator is a name, and +1 if it is a
13777 function. Otherwise it is set to zero. Usually you just want to
13778 test for >0, but internally the negative value is used.
13780 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13781 a decl-specifier-seq unless it declares a constructor, destructor,
13782 or conversion. It might seem that we could check this condition in
13783 semantic analysis, rather than parsing, but that makes it difficult
13784 to handle something like `f()'. We want to notice that there are
13785 no decl-specifiers, and therefore realize that this is an
13786 expression, not a declaration.)
13788 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13789 the declarator is a direct-declarator of the form "(...)".
13791 MEMBER_P is true iff this declarator is a member-declarator. */
13793 static cp_declarator *
13794 cp_parser_declarator (cp_parser* parser,
13795 cp_parser_declarator_kind dcl_kind,
13796 int* ctor_dtor_or_conv_p,
13797 bool* parenthesized_p,
13800 cp_declarator *declarator;
13801 enum tree_code code;
13802 cp_cv_quals cv_quals;
13804 tree attributes = NULL_TREE;
13806 /* Assume this is not a constructor, destructor, or type-conversion
13808 if (ctor_dtor_or_conv_p)
13809 *ctor_dtor_or_conv_p = 0;
13811 if (cp_parser_allow_gnu_extensions_p (parser))
13812 attributes = cp_parser_attributes_opt (parser);
13814 /* Check for the ptr-operator production. */
13815 cp_parser_parse_tentatively (parser);
13816 /* Parse the ptr-operator. */
13817 code = cp_parser_ptr_operator (parser,
13820 /* If that worked, then we have a ptr-operator. */
13821 if (cp_parser_parse_definitely (parser))
13823 /* If a ptr-operator was found, then this declarator was not
13825 if (parenthesized_p)
13826 *parenthesized_p = true;
13827 /* The dependent declarator is optional if we are parsing an
13828 abstract-declarator. */
13829 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13830 cp_parser_parse_tentatively (parser);
13832 /* Parse the dependent declarator. */
13833 declarator = cp_parser_declarator (parser, dcl_kind,
13834 /*ctor_dtor_or_conv_p=*/NULL,
13835 /*parenthesized_p=*/NULL,
13836 /*member_p=*/false);
13838 /* If we are parsing an abstract-declarator, we must handle the
13839 case where the dependent declarator is absent. */
13840 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13841 && !cp_parser_parse_definitely (parser))
13844 declarator = cp_parser_make_indirect_declarator
13845 (code, class_type, cv_quals, declarator);
13847 /* Everything else is a direct-declarator. */
13850 if (parenthesized_p)
13851 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13853 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13854 ctor_dtor_or_conv_p,
13858 if (attributes && declarator && declarator != cp_error_declarator)
13859 declarator->attributes = attributes;
13864 /* Parse a direct-declarator or direct-abstract-declarator.
13868 direct-declarator ( parameter-declaration-clause )
13869 cv-qualifier-seq [opt]
13870 exception-specification [opt]
13871 direct-declarator [ constant-expression [opt] ]
13874 direct-abstract-declarator:
13875 direct-abstract-declarator [opt]
13876 ( parameter-declaration-clause )
13877 cv-qualifier-seq [opt]
13878 exception-specification [opt]
13879 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13880 ( abstract-declarator )
13882 Returns a representation of the declarator. DCL_KIND is
13883 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13884 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13885 we are parsing a direct-declarator. It is
13886 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13887 of ambiguity we prefer an abstract declarator, as per
13888 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13889 cp_parser_declarator. */
13891 static cp_declarator *
13892 cp_parser_direct_declarator (cp_parser* parser,
13893 cp_parser_declarator_kind dcl_kind,
13894 int* ctor_dtor_or_conv_p,
13898 cp_declarator *declarator = NULL;
13899 tree scope = NULL_TREE;
13900 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13901 bool saved_in_declarator_p = parser->in_declarator_p;
13903 tree pushed_scope = NULL_TREE;
13907 /* Peek at the next token. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 if (token->type == CPP_OPEN_PAREN)
13911 /* This is either a parameter-declaration-clause, or a
13912 parenthesized declarator. When we know we are parsing a
13913 named declarator, it must be a parenthesized declarator
13914 if FIRST is true. For instance, `(int)' is a
13915 parameter-declaration-clause, with an omitted
13916 direct-abstract-declarator. But `((*))', is a
13917 parenthesized abstract declarator. Finally, when T is a
13918 template parameter `(T)' is a
13919 parameter-declaration-clause, and not a parenthesized
13922 We first try and parse a parameter-declaration-clause,
13923 and then try a nested declarator (if FIRST is true).
13925 It is not an error for it not to be a
13926 parameter-declaration-clause, even when FIRST is
13932 The first is the declaration of a function while the
13933 second is the definition of a variable, including its
13936 Having seen only the parenthesis, we cannot know which of
13937 these two alternatives should be selected. Even more
13938 complex are examples like:
13943 The former is a function-declaration; the latter is a
13944 variable initialization.
13946 Thus again, we try a parameter-declaration-clause, and if
13947 that fails, we back out and return. */
13949 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13952 unsigned saved_num_template_parameter_lists;
13953 bool is_declarator = false;
13956 /* In a member-declarator, the only valid interpretation
13957 of a parenthesis is the start of a
13958 parameter-declaration-clause. (It is invalid to
13959 initialize a static data member with a parenthesized
13960 initializer; only the "=" form of initialization is
13963 cp_parser_parse_tentatively (parser);
13965 /* Consume the `('. */
13966 cp_lexer_consume_token (parser->lexer);
13969 /* If this is going to be an abstract declarator, we're
13970 in a declarator and we can't have default args. */
13971 parser->default_arg_ok_p = false;
13972 parser->in_declarator_p = true;
13975 /* Inside the function parameter list, surrounding
13976 template-parameter-lists do not apply. */
13977 saved_num_template_parameter_lists
13978 = parser->num_template_parameter_lists;
13979 parser->num_template_parameter_lists = 0;
13981 begin_scope (sk_function_parms, NULL_TREE);
13983 /* Parse the parameter-declaration-clause. */
13984 params = cp_parser_parameter_declaration_clause (parser);
13986 parser->num_template_parameter_lists
13987 = saved_num_template_parameter_lists;
13989 /* If all went well, parse the cv-qualifier-seq and the
13990 exception-specification. */
13991 if (member_p || cp_parser_parse_definitely (parser))
13993 cp_cv_quals cv_quals;
13994 tree exception_specification;
13997 is_declarator = true;
13999 if (ctor_dtor_or_conv_p)
14000 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14002 /* Consume the `)'. */
14003 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14005 /* Parse the cv-qualifier-seq. */
14006 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14007 /* And the exception-specification. */
14008 exception_specification
14009 = cp_parser_exception_specification_opt (parser);
14012 = cp_parser_late_return_type_opt (parser);
14014 /* Create the function-declarator. */
14015 declarator = make_call_declarator (declarator,
14018 exception_specification,
14020 /* Any subsequent parameter lists are to do with
14021 return type, so are not those of the declared
14023 parser->default_arg_ok_p = false;
14026 /* Remove the function parms from scope. */
14027 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14028 pop_binding (DECL_NAME (t), t);
14032 /* Repeat the main loop. */
14036 /* If this is the first, we can try a parenthesized
14040 bool saved_in_type_id_in_expr_p;
14042 parser->default_arg_ok_p = saved_default_arg_ok_p;
14043 parser->in_declarator_p = saved_in_declarator_p;
14045 /* Consume the `('. */
14046 cp_lexer_consume_token (parser->lexer);
14047 /* Parse the nested declarator. */
14048 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14049 parser->in_type_id_in_expr_p = true;
14051 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14052 /*parenthesized_p=*/NULL,
14054 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14056 /* Expect a `)'. */
14057 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14058 declarator = cp_error_declarator;
14059 if (declarator == cp_error_declarator)
14062 goto handle_declarator;
14064 /* Otherwise, we must be done. */
14068 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14069 && token->type == CPP_OPEN_SQUARE)
14071 /* Parse an array-declarator. */
14074 if (ctor_dtor_or_conv_p)
14075 *ctor_dtor_or_conv_p = 0;
14078 parser->default_arg_ok_p = false;
14079 parser->in_declarator_p = true;
14080 /* Consume the `['. */
14081 cp_lexer_consume_token (parser->lexer);
14082 /* Peek at the next token. */
14083 token = cp_lexer_peek_token (parser->lexer);
14084 /* If the next token is `]', then there is no
14085 constant-expression. */
14086 if (token->type != CPP_CLOSE_SQUARE)
14088 bool non_constant_p;
14091 = cp_parser_constant_expression (parser,
14092 /*allow_non_constant=*/true,
14094 if (!non_constant_p)
14095 bounds = fold_non_dependent_expr (bounds);
14096 /* Normally, the array bound must be an integral constant
14097 expression. However, as an extension, we allow VLAs
14098 in function scopes. */
14099 else if (!parser->in_function_body)
14101 error_at (token->location,
14102 "array bound is not an integer constant");
14103 bounds = error_mark_node;
14105 else if (processing_template_decl && !error_operand_p (bounds))
14107 /* Remember this wasn't a constant-expression. */
14108 bounds = build_nop (TREE_TYPE (bounds), bounds);
14109 TREE_SIDE_EFFECTS (bounds) = 1;
14113 bounds = NULL_TREE;
14114 /* Look for the closing `]'. */
14115 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14117 declarator = cp_error_declarator;
14121 declarator = make_array_declarator (declarator, bounds);
14123 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14126 tree qualifying_scope;
14127 tree unqualified_name;
14128 special_function_kind sfk;
14130 bool pack_expansion_p = false;
14131 cp_token *declarator_id_start_token;
14133 /* Parse a declarator-id */
14134 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14137 cp_parser_parse_tentatively (parser);
14139 /* If we see an ellipsis, we should be looking at a
14141 if (token->type == CPP_ELLIPSIS)
14143 /* Consume the `...' */
14144 cp_lexer_consume_token (parser->lexer);
14146 pack_expansion_p = true;
14150 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14152 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14153 qualifying_scope = parser->scope;
14158 if (!unqualified_name && pack_expansion_p)
14160 /* Check whether an error occurred. */
14161 okay = !cp_parser_error_occurred (parser);
14163 /* We already consumed the ellipsis to mark a
14164 parameter pack, but we have no way to report it,
14165 so abort the tentative parse. We will be exiting
14166 immediately anyway. */
14167 cp_parser_abort_tentative_parse (parser);
14170 okay = cp_parser_parse_definitely (parser);
14173 unqualified_name = error_mark_node;
14174 else if (unqualified_name
14175 && (qualifying_scope
14176 || (TREE_CODE (unqualified_name)
14177 != IDENTIFIER_NODE)))
14179 cp_parser_error (parser, "expected unqualified-id");
14180 unqualified_name = error_mark_node;
14184 if (!unqualified_name)
14186 if (unqualified_name == error_mark_node)
14188 declarator = cp_error_declarator;
14189 pack_expansion_p = false;
14190 declarator->parameter_pack_p = false;
14194 if (qualifying_scope && at_namespace_scope_p ()
14195 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14197 /* In the declaration of a member of a template class
14198 outside of the class itself, the SCOPE will sometimes
14199 be a TYPENAME_TYPE. For example, given:
14201 template <typename T>
14202 int S<T>::R::i = 3;
14204 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14205 this context, we must resolve S<T>::R to an ordinary
14206 type, rather than a typename type.
14208 The reason we normally avoid resolving TYPENAME_TYPEs
14209 is that a specialization of `S' might render
14210 `S<T>::R' not a type. However, if `S' is
14211 specialized, then this `i' will not be used, so there
14212 is no harm in resolving the types here. */
14215 /* Resolve the TYPENAME_TYPE. */
14216 type = resolve_typename_type (qualifying_scope,
14217 /*only_current_p=*/false);
14218 /* If that failed, the declarator is invalid. */
14219 if (TREE_CODE (type) == TYPENAME_TYPE)
14221 if (typedef_variant_p (type))
14222 error_at (declarator_id_start_token->location,
14223 "cannot define member of dependent typedef "
14226 error_at (declarator_id_start_token->location,
14227 "%<%T::%E%> is not a type",
14228 TYPE_CONTEXT (qualifying_scope),
14229 TYPE_IDENTIFIER (qualifying_scope));
14231 qualifying_scope = type;
14236 if (unqualified_name)
14240 if (qualifying_scope
14241 && CLASS_TYPE_P (qualifying_scope))
14242 class_type = qualifying_scope;
14244 class_type = current_class_type;
14246 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14248 tree name_type = TREE_TYPE (unqualified_name);
14249 if (class_type && same_type_p (name_type, class_type))
14251 if (qualifying_scope
14252 && CLASSTYPE_USE_TEMPLATE (name_type))
14254 error_at (declarator_id_start_token->location,
14255 "invalid use of constructor as a template");
14256 inform (declarator_id_start_token->location,
14257 "use %<%T::%D%> instead of %<%T::%D%> to "
14258 "name the constructor in a qualified name",
14260 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14261 class_type, name_type);
14262 declarator = cp_error_declarator;
14266 unqualified_name = constructor_name (class_type);
14270 /* We do not attempt to print the declarator
14271 here because we do not have enough
14272 information about its original syntactic
14274 cp_parser_error (parser, "invalid declarator");
14275 declarator = cp_error_declarator;
14282 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14283 sfk = sfk_destructor;
14284 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14285 sfk = sfk_conversion;
14286 else if (/* There's no way to declare a constructor
14287 for an anonymous type, even if the type
14288 got a name for linkage purposes. */
14289 !TYPE_WAS_ANONYMOUS (class_type)
14290 && constructor_name_p (unqualified_name,
14293 unqualified_name = constructor_name (class_type);
14294 sfk = sfk_constructor;
14296 else if (is_overloaded_fn (unqualified_name)
14297 && DECL_CONSTRUCTOR_P (get_first_fn
14298 (unqualified_name)))
14299 sfk = sfk_constructor;
14301 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14302 *ctor_dtor_or_conv_p = -1;
14305 declarator = make_id_declarator (qualifying_scope,
14308 declarator->id_loc = token->location;
14309 declarator->parameter_pack_p = pack_expansion_p;
14311 if (pack_expansion_p)
14312 maybe_warn_variadic_templates ();
14315 handle_declarator:;
14316 scope = get_scope_of_declarator (declarator);
14318 /* Any names that appear after the declarator-id for a
14319 member are looked up in the containing scope. */
14320 pushed_scope = push_scope (scope);
14321 parser->in_declarator_p = true;
14322 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14323 || (declarator && declarator->kind == cdk_id))
14324 /* Default args are only allowed on function
14326 parser->default_arg_ok_p = saved_default_arg_ok_p;
14328 parser->default_arg_ok_p = false;
14337 /* For an abstract declarator, we might wind up with nothing at this
14338 point. That's an error; the declarator is not optional. */
14340 cp_parser_error (parser, "expected declarator");
14342 /* If we entered a scope, we must exit it now. */
14344 pop_scope (pushed_scope);
14346 parser->default_arg_ok_p = saved_default_arg_ok_p;
14347 parser->in_declarator_p = saved_in_declarator_p;
14352 /* Parse a ptr-operator.
14355 * cv-qualifier-seq [opt]
14357 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14362 & cv-qualifier-seq [opt]
14364 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14365 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14366 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14367 filled in with the TYPE containing the member. *CV_QUALS is
14368 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14369 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14370 Note that the tree codes returned by this function have nothing
14371 to do with the types of trees that will be eventually be created
14372 to represent the pointer or reference type being parsed. They are
14373 just constants with suggestive names. */
14374 static enum tree_code
14375 cp_parser_ptr_operator (cp_parser* parser,
14377 cp_cv_quals *cv_quals)
14379 enum tree_code code = ERROR_MARK;
14382 /* Assume that it's not a pointer-to-member. */
14384 /* And that there are no cv-qualifiers. */
14385 *cv_quals = TYPE_UNQUALIFIED;
14387 /* Peek at the next token. */
14388 token = cp_lexer_peek_token (parser->lexer);
14390 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14391 if (token->type == CPP_MULT)
14392 code = INDIRECT_REF;
14393 else if (token->type == CPP_AND)
14395 else if ((cxx_dialect != cxx98) &&
14396 token->type == CPP_AND_AND) /* C++0x only */
14397 code = NON_LVALUE_EXPR;
14399 if (code != ERROR_MARK)
14401 /* Consume the `*', `&' or `&&'. */
14402 cp_lexer_consume_token (parser->lexer);
14404 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14405 `&', if we are allowing GNU extensions. (The only qualifier
14406 that can legally appear after `&' is `restrict', but that is
14407 enforced during semantic analysis. */
14408 if (code == INDIRECT_REF
14409 || cp_parser_allow_gnu_extensions_p (parser))
14410 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14414 /* Try the pointer-to-member case. */
14415 cp_parser_parse_tentatively (parser);
14416 /* Look for the optional `::' operator. */
14417 cp_parser_global_scope_opt (parser,
14418 /*current_scope_valid_p=*/false);
14419 /* Look for the nested-name specifier. */
14420 token = cp_lexer_peek_token (parser->lexer);
14421 cp_parser_nested_name_specifier (parser,
14422 /*typename_keyword_p=*/false,
14423 /*check_dependency_p=*/true,
14425 /*is_declaration=*/false);
14426 /* If we found it, and the next token is a `*', then we are
14427 indeed looking at a pointer-to-member operator. */
14428 if (!cp_parser_error_occurred (parser)
14429 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14431 /* Indicate that the `*' operator was used. */
14432 code = INDIRECT_REF;
14434 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14435 error_at (token->location, "%qD is a namespace", parser->scope);
14438 /* The type of which the member is a member is given by the
14440 *type = parser->scope;
14441 /* The next name will not be qualified. */
14442 parser->scope = NULL_TREE;
14443 parser->qualifying_scope = NULL_TREE;
14444 parser->object_scope = NULL_TREE;
14445 /* Look for the optional cv-qualifier-seq. */
14446 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14449 /* If that didn't work we don't have a ptr-operator. */
14450 if (!cp_parser_parse_definitely (parser))
14451 cp_parser_error (parser, "expected ptr-operator");
14457 /* Parse an (optional) cv-qualifier-seq.
14460 cv-qualifier cv-qualifier-seq [opt]
14471 Returns a bitmask representing the cv-qualifiers. */
14474 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14476 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14481 cp_cv_quals cv_qualifier;
14483 /* Peek at the next token. */
14484 token = cp_lexer_peek_token (parser->lexer);
14485 /* See if it's a cv-qualifier. */
14486 switch (token->keyword)
14489 cv_qualifier = TYPE_QUAL_CONST;
14493 cv_qualifier = TYPE_QUAL_VOLATILE;
14497 cv_qualifier = TYPE_QUAL_RESTRICT;
14501 cv_qualifier = TYPE_UNQUALIFIED;
14508 if (cv_quals & cv_qualifier)
14510 error_at (token->location, "duplicate cv-qualifier");
14511 cp_lexer_purge_token (parser->lexer);
14515 cp_lexer_consume_token (parser->lexer);
14516 cv_quals |= cv_qualifier;
14523 /* Parse a late-specified return type, if any. This is not a separate
14524 non-terminal, but part of a function declarator, which looks like
14526 -> trailing-type-specifier-seq abstract-declarator(opt)
14528 Returns the type indicated by the type-id. */
14531 cp_parser_late_return_type_opt (cp_parser* parser)
14535 /* Peek at the next token. */
14536 token = cp_lexer_peek_token (parser->lexer);
14537 /* A late-specified return type is indicated by an initial '->'. */
14538 if (token->type != CPP_DEREF)
14541 /* Consume the ->. */
14542 cp_lexer_consume_token (parser->lexer);
14544 return cp_parser_trailing_type_id (parser);
14547 /* Parse a declarator-id.
14551 :: [opt] nested-name-specifier [opt] type-name
14553 In the `id-expression' case, the value returned is as for
14554 cp_parser_id_expression if the id-expression was an unqualified-id.
14555 If the id-expression was a qualified-id, then a SCOPE_REF is
14556 returned. The first operand is the scope (either a NAMESPACE_DECL
14557 or TREE_TYPE), but the second is still just a representation of an
14561 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14564 /* The expression must be an id-expression. Assume that qualified
14565 names are the names of types so that:
14568 int S<T>::R::i = 3;
14570 will work; we must treat `S<T>::R' as the name of a type.
14571 Similarly, assume that qualified names are templates, where
14575 int S<T>::R<T>::i = 3;
14578 id = cp_parser_id_expression (parser,
14579 /*template_keyword_p=*/false,
14580 /*check_dependency_p=*/false,
14581 /*template_p=*/NULL,
14582 /*declarator_p=*/true,
14584 if (id && BASELINK_P (id))
14585 id = BASELINK_FUNCTIONS (id);
14589 /* Parse a type-id.
14592 type-specifier-seq abstract-declarator [opt]
14594 Returns the TYPE specified. */
14597 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14598 bool is_trailing_return)
14600 cp_decl_specifier_seq type_specifier_seq;
14601 cp_declarator *abstract_declarator;
14603 /* Parse the type-specifier-seq. */
14604 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14605 is_trailing_return,
14606 &type_specifier_seq);
14607 if (type_specifier_seq.type == error_mark_node)
14608 return error_mark_node;
14610 /* There might or might not be an abstract declarator. */
14611 cp_parser_parse_tentatively (parser);
14612 /* Look for the declarator. */
14613 abstract_declarator
14614 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14615 /*parenthesized_p=*/NULL,
14616 /*member_p=*/false);
14617 /* Check to see if there really was a declarator. */
14618 if (!cp_parser_parse_definitely (parser))
14619 abstract_declarator = NULL;
14621 if (type_specifier_seq.type
14622 && type_uses_auto (type_specifier_seq.type))
14624 /* A type-id with type 'auto' is only ok if the abstract declarator
14625 is a function declarator with a late-specified return type. */
14626 if (abstract_declarator
14627 && abstract_declarator->kind == cdk_function
14628 && abstract_declarator->u.function.late_return_type)
14632 error ("invalid use of %<auto%>");
14633 return error_mark_node;
14637 return groktypename (&type_specifier_seq, abstract_declarator,
14641 static tree cp_parser_type_id (cp_parser *parser)
14643 return cp_parser_type_id_1 (parser, false, false);
14646 static tree cp_parser_template_type_arg (cp_parser *parser)
14648 return cp_parser_type_id_1 (parser, true, false);
14651 static tree cp_parser_trailing_type_id (cp_parser *parser)
14653 return cp_parser_type_id_1 (parser, false, true);
14656 /* Parse a type-specifier-seq.
14658 type-specifier-seq:
14659 type-specifier type-specifier-seq [opt]
14663 type-specifier-seq:
14664 attributes type-specifier-seq [opt]
14666 If IS_DECLARATION is true, we are at the start of a "condition" or
14667 exception-declaration, so we might be followed by a declarator-id.
14669 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14670 i.e. we've just seen "->".
14672 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14675 cp_parser_type_specifier_seq (cp_parser* parser,
14676 bool is_declaration,
14677 bool is_trailing_return,
14678 cp_decl_specifier_seq *type_specifier_seq)
14680 bool seen_type_specifier = false;
14681 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14682 cp_token *start_token = NULL;
14684 /* Clear the TYPE_SPECIFIER_SEQ. */
14685 clear_decl_specs (type_specifier_seq);
14687 /* In the context of a trailing return type, enum E { } is an
14688 elaborated-type-specifier followed by a function-body, not an
14690 if (is_trailing_return)
14691 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14693 /* Parse the type-specifiers and attributes. */
14696 tree type_specifier;
14697 bool is_cv_qualifier;
14699 /* Check for attributes first. */
14700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14702 type_specifier_seq->attributes =
14703 chainon (type_specifier_seq->attributes,
14704 cp_parser_attributes_opt (parser));
14708 /* record the token of the beginning of the type specifier seq,
14709 for error reporting purposes*/
14711 start_token = cp_lexer_peek_token (parser->lexer);
14713 /* Look for the type-specifier. */
14714 type_specifier = cp_parser_type_specifier (parser,
14716 type_specifier_seq,
14717 /*is_declaration=*/false,
14720 if (!type_specifier)
14722 /* If the first type-specifier could not be found, this is not a
14723 type-specifier-seq at all. */
14724 if (!seen_type_specifier)
14726 cp_parser_error (parser, "expected type-specifier");
14727 type_specifier_seq->type = error_mark_node;
14730 /* If subsequent type-specifiers could not be found, the
14731 type-specifier-seq is complete. */
14735 seen_type_specifier = true;
14736 /* The standard says that a condition can be:
14738 type-specifier-seq declarator = assignment-expression
14745 we should treat the "S" as a declarator, not as a
14746 type-specifier. The standard doesn't say that explicitly for
14747 type-specifier-seq, but it does say that for
14748 decl-specifier-seq in an ordinary declaration. Perhaps it
14749 would be clearer just to allow a decl-specifier-seq here, and
14750 then add a semantic restriction that if any decl-specifiers
14751 that are not type-specifiers appear, the program is invalid. */
14752 if (is_declaration && !is_cv_qualifier)
14753 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14756 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14759 /* Parse a parameter-declaration-clause.
14761 parameter-declaration-clause:
14762 parameter-declaration-list [opt] ... [opt]
14763 parameter-declaration-list , ...
14765 Returns a representation for the parameter declarations. A return
14766 value of NULL indicates a parameter-declaration-clause consisting
14767 only of an ellipsis. */
14770 cp_parser_parameter_declaration_clause (cp_parser* parser)
14777 /* Peek at the next token. */
14778 token = cp_lexer_peek_token (parser->lexer);
14779 /* Check for trivial parameter-declaration-clauses. */
14780 if (token->type == CPP_ELLIPSIS)
14782 /* Consume the `...' token. */
14783 cp_lexer_consume_token (parser->lexer);
14786 else if (token->type == CPP_CLOSE_PAREN)
14787 /* There are no parameters. */
14789 #ifndef NO_IMPLICIT_EXTERN_C
14790 if (in_system_header && current_class_type == NULL
14791 && current_lang_name == lang_name_c)
14795 return void_list_node;
14797 /* Check for `(void)', too, which is a special case. */
14798 else if (token->keyword == RID_VOID
14799 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14800 == CPP_CLOSE_PAREN))
14802 /* Consume the `void' token. */
14803 cp_lexer_consume_token (parser->lexer);
14804 /* There are no parameters. */
14805 return void_list_node;
14808 /* Parse the parameter-declaration-list. */
14809 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14810 /* If a parse error occurred while parsing the
14811 parameter-declaration-list, then the entire
14812 parameter-declaration-clause is erroneous. */
14816 /* Peek at the next token. */
14817 token = cp_lexer_peek_token (parser->lexer);
14818 /* If it's a `,', the clause should terminate with an ellipsis. */
14819 if (token->type == CPP_COMMA)
14821 /* Consume the `,'. */
14822 cp_lexer_consume_token (parser->lexer);
14823 /* Expect an ellipsis. */
14825 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14827 /* It might also be `...' if the optional trailing `,' was
14829 else if (token->type == CPP_ELLIPSIS)
14831 /* Consume the `...' token. */
14832 cp_lexer_consume_token (parser->lexer);
14833 /* And remember that we saw it. */
14837 ellipsis_p = false;
14839 /* Finish the parameter list. */
14841 parameters = chainon (parameters, void_list_node);
14846 /* Parse a parameter-declaration-list.
14848 parameter-declaration-list:
14849 parameter-declaration
14850 parameter-declaration-list , parameter-declaration
14852 Returns a representation of the parameter-declaration-list, as for
14853 cp_parser_parameter_declaration_clause. However, the
14854 `void_list_node' is never appended to the list. Upon return,
14855 *IS_ERROR will be true iff an error occurred. */
14858 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14860 tree parameters = NULL_TREE;
14861 tree *tail = ¶meters;
14862 bool saved_in_unbraced_linkage_specification_p;
14865 /* Assume all will go well. */
14867 /* The special considerations that apply to a function within an
14868 unbraced linkage specifications do not apply to the parameters
14869 to the function. */
14870 saved_in_unbraced_linkage_specification_p
14871 = parser->in_unbraced_linkage_specification_p;
14872 parser->in_unbraced_linkage_specification_p = false;
14874 /* Look for more parameters. */
14877 cp_parameter_declarator *parameter;
14878 tree decl = error_mark_node;
14879 bool parenthesized_p;
14880 /* Parse the parameter. */
14882 = cp_parser_parameter_declaration (parser,
14883 /*template_parm_p=*/false,
14886 /* We don't know yet if the enclosing context is deprecated, so wait
14887 and warn in grokparms if appropriate. */
14888 deprecated_state = DEPRECATED_SUPPRESS;
14891 decl = grokdeclarator (parameter->declarator,
14892 ¶meter->decl_specifiers,
14894 parameter->default_argument != NULL_TREE,
14895 ¶meter->decl_specifiers.attributes);
14897 deprecated_state = DEPRECATED_NORMAL;
14899 /* If a parse error occurred parsing the parameter declaration,
14900 then the entire parameter-declaration-list is erroneous. */
14901 if (decl == error_mark_node)
14904 parameters = error_mark_node;
14908 if (parameter->decl_specifiers.attributes)
14909 cplus_decl_attributes (&decl,
14910 parameter->decl_specifiers.attributes,
14912 if (DECL_NAME (decl))
14913 decl = pushdecl (decl);
14915 if (decl != error_mark_node)
14917 retrofit_lang_decl (decl);
14918 DECL_PARM_INDEX (decl) = ++index;
14921 /* Add the new parameter to the list. */
14922 *tail = build_tree_list (parameter->default_argument, decl);
14923 tail = &TREE_CHAIN (*tail);
14925 /* Peek at the next token. */
14926 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14927 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14928 /* These are for Objective-C++ */
14929 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14930 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14931 /* The parameter-declaration-list is complete. */
14933 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14937 /* Peek at the next token. */
14938 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14939 /* If it's an ellipsis, then the list is complete. */
14940 if (token->type == CPP_ELLIPSIS)
14942 /* Otherwise, there must be more parameters. Consume the
14944 cp_lexer_consume_token (parser->lexer);
14945 /* When parsing something like:
14947 int i(float f, double d)
14949 we can tell after seeing the declaration for "f" that we
14950 are not looking at an initialization of a variable "i",
14951 but rather at the declaration of a function "i".
14953 Due to the fact that the parsing of template arguments
14954 (as specified to a template-id) requires backtracking we
14955 cannot use this technique when inside a template argument
14957 if (!parser->in_template_argument_list_p
14958 && !parser->in_type_id_in_expr_p
14959 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14960 /* However, a parameter-declaration of the form
14961 "foat(f)" (which is a valid declaration of a
14962 parameter "f") can also be interpreted as an
14963 expression (the conversion of "f" to "float"). */
14964 && !parenthesized_p)
14965 cp_parser_commit_to_tentative_parse (parser);
14969 cp_parser_error (parser, "expected %<,%> or %<...%>");
14970 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14971 cp_parser_skip_to_closing_parenthesis (parser,
14972 /*recovering=*/true,
14973 /*or_comma=*/false,
14974 /*consume_paren=*/false);
14979 parser->in_unbraced_linkage_specification_p
14980 = saved_in_unbraced_linkage_specification_p;
14985 /* Parse a parameter declaration.
14987 parameter-declaration:
14988 decl-specifier-seq ... [opt] declarator
14989 decl-specifier-seq declarator = assignment-expression
14990 decl-specifier-seq ... [opt] abstract-declarator [opt]
14991 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14993 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14994 declares a template parameter. (In that case, a non-nested `>'
14995 token encountered during the parsing of the assignment-expression
14996 is not interpreted as a greater-than operator.)
14998 Returns a representation of the parameter, or NULL if an error
14999 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15000 true iff the declarator is of the form "(p)". */
15002 static cp_parameter_declarator *
15003 cp_parser_parameter_declaration (cp_parser *parser,
15004 bool template_parm_p,
15005 bool *parenthesized_p)
15007 int declares_class_or_enum;
15008 cp_decl_specifier_seq decl_specifiers;
15009 cp_declarator *declarator;
15010 tree default_argument;
15011 cp_token *token = NULL, *declarator_token_start = NULL;
15012 const char *saved_message;
15014 /* In a template parameter, `>' is not an operator.
15018 When parsing a default template-argument for a non-type
15019 template-parameter, the first non-nested `>' is taken as the end
15020 of the template parameter-list rather than a greater-than
15023 /* Type definitions may not appear in parameter types. */
15024 saved_message = parser->type_definition_forbidden_message;
15025 parser->type_definition_forbidden_message
15026 = G_("types may not be defined in parameter types");
15028 /* Parse the declaration-specifiers. */
15029 cp_parser_decl_specifier_seq (parser,
15030 CP_PARSER_FLAGS_NONE,
15032 &declares_class_or_enum);
15034 /* Complain about missing 'typename' or other invalid type names. */
15035 if (!decl_specifiers.any_type_specifiers_p)
15036 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15038 /* If an error occurred, there's no reason to attempt to parse the
15039 rest of the declaration. */
15040 if (cp_parser_error_occurred (parser))
15042 parser->type_definition_forbidden_message = saved_message;
15046 /* Peek at the next token. */
15047 token = cp_lexer_peek_token (parser->lexer);
15049 /* If the next token is a `)', `,', `=', `>', or `...', then there
15050 is no declarator. However, when variadic templates are enabled,
15051 there may be a declarator following `...'. */
15052 if (token->type == CPP_CLOSE_PAREN
15053 || token->type == CPP_COMMA
15054 || token->type == CPP_EQ
15055 || token->type == CPP_GREATER)
15058 if (parenthesized_p)
15059 *parenthesized_p = false;
15061 /* Otherwise, there should be a declarator. */
15064 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15065 parser->default_arg_ok_p = false;
15067 /* After seeing a decl-specifier-seq, if the next token is not a
15068 "(", there is no possibility that the code is a valid
15069 expression. Therefore, if parsing tentatively, we commit at
15071 if (!parser->in_template_argument_list_p
15072 /* In an expression context, having seen:
15076 we cannot be sure whether we are looking at a
15077 function-type (taking a "char" as a parameter) or a cast
15078 of some object of type "char" to "int". */
15079 && !parser->in_type_id_in_expr_p
15080 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15081 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15082 cp_parser_commit_to_tentative_parse (parser);
15083 /* Parse the declarator. */
15084 declarator_token_start = token;
15085 declarator = cp_parser_declarator (parser,
15086 CP_PARSER_DECLARATOR_EITHER,
15087 /*ctor_dtor_or_conv_p=*/NULL,
15089 /*member_p=*/false);
15090 parser->default_arg_ok_p = saved_default_arg_ok_p;
15091 /* After the declarator, allow more attributes. */
15092 decl_specifiers.attributes
15093 = chainon (decl_specifiers.attributes,
15094 cp_parser_attributes_opt (parser));
15097 /* If the next token is an ellipsis, and we have not seen a
15098 declarator name, and the type of the declarator contains parameter
15099 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15100 a parameter pack expansion expression. Otherwise, leave the
15101 ellipsis for a C-style variadic function. */
15102 token = cp_lexer_peek_token (parser->lexer);
15103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15105 tree type = decl_specifiers.type;
15107 if (type && DECL_P (type))
15108 type = TREE_TYPE (type);
15111 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15112 && declarator_can_be_parameter_pack (declarator)
15113 && (!declarator || !declarator->parameter_pack_p)
15114 && uses_parameter_packs (type))
15116 /* Consume the `...'. */
15117 cp_lexer_consume_token (parser->lexer);
15118 maybe_warn_variadic_templates ();
15120 /* Build a pack expansion type */
15122 declarator->parameter_pack_p = true;
15124 decl_specifiers.type = make_pack_expansion (type);
15128 /* The restriction on defining new types applies only to the type
15129 of the parameter, not to the default argument. */
15130 parser->type_definition_forbidden_message = saved_message;
15132 /* If the next token is `=', then process a default argument. */
15133 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15135 /* Consume the `='. */
15136 cp_lexer_consume_token (parser->lexer);
15138 /* If we are defining a class, then the tokens that make up the
15139 default argument must be saved and processed later. */
15140 if (!template_parm_p && at_class_scope_p ()
15141 && TYPE_BEING_DEFINED (current_class_type)
15142 && !LAMBDA_TYPE_P (current_class_type))
15144 unsigned depth = 0;
15145 int maybe_template_id = 0;
15146 cp_token *first_token;
15149 /* Add tokens until we have processed the entire default
15150 argument. We add the range [first_token, token). */
15151 first_token = cp_lexer_peek_token (parser->lexer);
15156 /* Peek at the next token. */
15157 token = cp_lexer_peek_token (parser->lexer);
15158 /* What we do depends on what token we have. */
15159 switch (token->type)
15161 /* In valid code, a default argument must be
15162 immediately followed by a `,' `)', or `...'. */
15164 if (depth == 0 && maybe_template_id)
15166 /* If we've seen a '<', we might be in a
15167 template-argument-list. Until Core issue 325 is
15168 resolved, we don't know how this situation ought
15169 to be handled, so try to DTRT. We check whether
15170 what comes after the comma is a valid parameter
15171 declaration list. If it is, then the comma ends
15172 the default argument; otherwise the default
15173 argument continues. */
15174 bool error = false;
15176 /* Set ITALP so cp_parser_parameter_declaration_list
15177 doesn't decide to commit to this parse. */
15178 bool saved_italp = parser->in_template_argument_list_p;
15179 parser->in_template_argument_list_p = true;
15181 cp_parser_parse_tentatively (parser);
15182 cp_lexer_consume_token (parser->lexer);
15183 cp_parser_parameter_declaration_list (parser, &error);
15184 if (!cp_parser_error_occurred (parser) && !error)
15186 cp_parser_abort_tentative_parse (parser);
15188 parser->in_template_argument_list_p = saved_italp;
15191 case CPP_CLOSE_PAREN:
15193 /* If we run into a non-nested `;', `}', or `]',
15194 then the code is invalid -- but the default
15195 argument is certainly over. */
15196 case CPP_SEMICOLON:
15197 case CPP_CLOSE_BRACE:
15198 case CPP_CLOSE_SQUARE:
15201 /* Update DEPTH, if necessary. */
15202 else if (token->type == CPP_CLOSE_PAREN
15203 || token->type == CPP_CLOSE_BRACE
15204 || token->type == CPP_CLOSE_SQUARE)
15208 case CPP_OPEN_PAREN:
15209 case CPP_OPEN_SQUARE:
15210 case CPP_OPEN_BRACE:
15216 /* This might be the comparison operator, or it might
15217 start a template argument list. */
15218 ++maybe_template_id;
15222 if (cxx_dialect == cxx98)
15224 /* Fall through for C++0x, which treats the `>>'
15225 operator like two `>' tokens in certain
15231 /* This might be an operator, or it might close a
15232 template argument list. But if a previous '<'
15233 started a template argument list, this will have
15234 closed it, so we can't be in one anymore. */
15235 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15236 if (maybe_template_id < 0)
15237 maybe_template_id = 0;
15241 /* If we run out of tokens, issue an error message. */
15243 case CPP_PRAGMA_EOL:
15244 error_at (token->location, "file ends in default argument");
15250 /* In these cases, we should look for template-ids.
15251 For example, if the default argument is
15252 `X<int, double>()', we need to do name lookup to
15253 figure out whether or not `X' is a template; if
15254 so, the `,' does not end the default argument.
15256 That is not yet done. */
15263 /* If we've reached the end, stop. */
15267 /* Add the token to the token block. */
15268 token = cp_lexer_consume_token (parser->lexer);
15271 /* Create a DEFAULT_ARG to represent the unparsed default
15273 default_argument = make_node (DEFAULT_ARG);
15274 DEFARG_TOKENS (default_argument)
15275 = cp_token_cache_new (first_token, token);
15276 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15278 /* Outside of a class definition, we can just parse the
15279 assignment-expression. */
15282 token = cp_lexer_peek_token (parser->lexer);
15284 = cp_parser_default_argument (parser, template_parm_p);
15287 if (!parser->default_arg_ok_p)
15289 if (flag_permissive)
15290 warning (0, "deprecated use of default argument for parameter of non-function");
15293 error_at (token->location,
15294 "default arguments are only "
15295 "permitted for function parameters");
15296 default_argument = NULL_TREE;
15299 else if ((declarator && declarator->parameter_pack_p)
15300 || (decl_specifiers.type
15301 && PACK_EXPANSION_P (decl_specifiers.type)))
15303 /* Find the name of the parameter pack. */
15304 cp_declarator *id_declarator = declarator;
15305 while (id_declarator && id_declarator->kind != cdk_id)
15306 id_declarator = id_declarator->declarator;
15308 if (id_declarator && id_declarator->kind == cdk_id)
15309 error_at (declarator_token_start->location,
15311 ? "template parameter pack %qD"
15312 " cannot have a default argument"
15313 : "parameter pack %qD cannot have a default argument",
15314 id_declarator->u.id.unqualified_name);
15316 error_at (declarator_token_start->location,
15318 ? "template parameter pack cannot have a default argument"
15319 : "parameter pack cannot have a default argument");
15321 default_argument = NULL_TREE;
15325 default_argument = NULL_TREE;
15327 return make_parameter_declarator (&decl_specifiers,
15332 /* Parse a default argument and return it.
15334 TEMPLATE_PARM_P is true if this is a default argument for a
15335 non-type template parameter. */
15337 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15339 tree default_argument = NULL_TREE;
15340 bool saved_greater_than_is_operator_p;
15341 bool saved_local_variables_forbidden_p;
15343 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15345 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15346 parser->greater_than_is_operator_p = !template_parm_p;
15347 /* Local variable names (and the `this' keyword) may not
15348 appear in a default argument. */
15349 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15350 parser->local_variables_forbidden_p = true;
15351 /* Parse the assignment-expression. */
15352 if (template_parm_p)
15353 push_deferring_access_checks (dk_no_deferred);
15355 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15356 if (template_parm_p)
15357 pop_deferring_access_checks ();
15358 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15359 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15361 return default_argument;
15364 /* Parse a function-body.
15367 compound_statement */
15370 cp_parser_function_body (cp_parser *parser)
15372 cp_parser_compound_statement (parser, NULL, false);
15375 /* Parse a ctor-initializer-opt followed by a function-body. Return
15376 true if a ctor-initializer was present. */
15379 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15382 bool ctor_initializer_p;
15384 /* Begin the function body. */
15385 body = begin_function_body ();
15386 /* Parse the optional ctor-initializer. */
15387 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15388 /* Parse the function-body. */
15389 cp_parser_function_body (parser);
15390 /* Finish the function body. */
15391 finish_function_body (body);
15393 return ctor_initializer_p;
15396 /* Parse an initializer.
15399 = initializer-clause
15400 ( expression-list )
15402 Returns an expression representing the initializer. If no
15403 initializer is present, NULL_TREE is returned.
15405 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15406 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15407 set to TRUE if there is no initializer present. If there is an
15408 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15409 is set to true; otherwise it is set to false. */
15412 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15413 bool* non_constant_p)
15418 /* Peek at the next token. */
15419 token = cp_lexer_peek_token (parser->lexer);
15421 /* Let our caller know whether or not this initializer was
15423 *is_direct_init = (token->type != CPP_EQ);
15424 /* Assume that the initializer is constant. */
15425 *non_constant_p = false;
15427 if (token->type == CPP_EQ)
15429 /* Consume the `='. */
15430 cp_lexer_consume_token (parser->lexer);
15431 /* Parse the initializer-clause. */
15432 init = cp_parser_initializer_clause (parser, non_constant_p);
15434 else if (token->type == CPP_OPEN_PAREN)
15437 vec = cp_parser_parenthesized_expression_list (parser, false,
15439 /*allow_expansion_p=*/true,
15442 return error_mark_node;
15443 init = build_tree_list_vec (vec);
15444 release_tree_vector (vec);
15446 else if (token->type == CPP_OPEN_BRACE)
15448 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15449 init = cp_parser_braced_list (parser, non_constant_p);
15450 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15454 /* Anything else is an error. */
15455 cp_parser_error (parser, "expected initializer");
15456 init = error_mark_node;
15462 /* Parse an initializer-clause.
15464 initializer-clause:
15465 assignment-expression
15468 Returns an expression representing the initializer.
15470 If the `assignment-expression' production is used the value
15471 returned is simply a representation for the expression.
15473 Otherwise, calls cp_parser_braced_list. */
15476 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15480 /* Assume the expression is constant. */
15481 *non_constant_p = false;
15483 /* If it is not a `{', then we are looking at an
15484 assignment-expression. */
15485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15488 = cp_parser_constant_expression (parser,
15489 /*allow_non_constant_p=*/true,
15491 if (!*non_constant_p)
15492 initializer = fold_non_dependent_expr (initializer);
15495 initializer = cp_parser_braced_list (parser, non_constant_p);
15497 return initializer;
15500 /* Parse a brace-enclosed initializer list.
15503 { initializer-list , [opt] }
15506 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15507 the elements of the initializer-list (or NULL, if the last
15508 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15509 NULL_TREE. There is no way to detect whether or not the optional
15510 trailing `,' was provided. NON_CONSTANT_P is as for
15511 cp_parser_initializer. */
15514 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15518 /* Consume the `{' token. */
15519 cp_lexer_consume_token (parser->lexer);
15520 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15521 initializer = make_node (CONSTRUCTOR);
15522 /* If it's not a `}', then there is a non-trivial initializer. */
15523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15525 /* Parse the initializer list. */
15526 CONSTRUCTOR_ELTS (initializer)
15527 = cp_parser_initializer_list (parser, non_constant_p);
15528 /* A trailing `,' token is allowed. */
15529 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15530 cp_lexer_consume_token (parser->lexer);
15532 /* Now, there should be a trailing `}'. */
15533 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15534 TREE_TYPE (initializer) = init_list_type_node;
15535 return initializer;
15538 /* Parse an initializer-list.
15541 initializer-clause ... [opt]
15542 initializer-list , initializer-clause ... [opt]
15547 identifier : initializer-clause
15548 initializer-list, identifier : initializer-clause
15550 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15551 for the initializer. If the INDEX of the elt is non-NULL, it is the
15552 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15553 as for cp_parser_initializer. */
15555 static VEC(constructor_elt,gc) *
15556 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15558 VEC(constructor_elt,gc) *v = NULL;
15560 /* Assume all of the expressions are constant. */
15561 *non_constant_p = false;
15563 /* Parse the rest of the list. */
15569 bool clause_non_constant_p;
15571 /* If the next token is an identifier and the following one is a
15572 colon, we are looking at the GNU designated-initializer
15574 if (cp_parser_allow_gnu_extensions_p (parser)
15575 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15578 /* Warn the user that they are using an extension. */
15579 pedwarn (input_location, OPT_pedantic,
15580 "ISO C++ does not allow designated initializers");
15581 /* Consume the identifier. */
15582 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15583 /* Consume the `:'. */
15584 cp_lexer_consume_token (parser->lexer);
15587 identifier = NULL_TREE;
15589 /* Parse the initializer. */
15590 initializer = cp_parser_initializer_clause (parser,
15591 &clause_non_constant_p);
15592 /* If any clause is non-constant, so is the entire initializer. */
15593 if (clause_non_constant_p)
15594 *non_constant_p = true;
15596 /* If we have an ellipsis, this is an initializer pack
15598 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15600 /* Consume the `...'. */
15601 cp_lexer_consume_token (parser->lexer);
15603 /* Turn the initializer into an initializer expansion. */
15604 initializer = make_pack_expansion (initializer);
15607 /* Add it to the vector. */
15608 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15610 /* If the next token is not a comma, we have reached the end of
15612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15615 /* Peek at the next token. */
15616 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15617 /* If the next token is a `}', then we're still done. An
15618 initializer-clause can have a trailing `,' after the
15619 initializer-list and before the closing `}'. */
15620 if (token->type == CPP_CLOSE_BRACE)
15623 /* Consume the `,' token. */
15624 cp_lexer_consume_token (parser->lexer);
15630 /* Classes [gram.class] */
15632 /* Parse a class-name.
15638 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15639 to indicate that names looked up in dependent types should be
15640 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15641 keyword has been used to indicate that the name that appears next
15642 is a template. TAG_TYPE indicates the explicit tag given before
15643 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15644 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15645 is the class being defined in a class-head.
15647 Returns the TYPE_DECL representing the class. */
15650 cp_parser_class_name (cp_parser *parser,
15651 bool typename_keyword_p,
15652 bool template_keyword_p,
15653 enum tag_types tag_type,
15654 bool check_dependency_p,
15656 bool is_declaration)
15662 tree identifier = NULL_TREE;
15664 /* All class-names start with an identifier. */
15665 token = cp_lexer_peek_token (parser->lexer);
15666 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15668 cp_parser_error (parser, "expected class-name");
15669 return error_mark_node;
15672 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15673 to a template-id, so we save it here. */
15674 scope = parser->scope;
15675 if (scope == error_mark_node)
15676 return error_mark_node;
15678 /* Any name names a type if we're following the `typename' keyword
15679 in a qualified name where the enclosing scope is type-dependent. */
15680 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15681 && dependent_type_p (scope));
15682 /* Handle the common case (an identifier, but not a template-id)
15684 if (token->type == CPP_NAME
15685 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15687 cp_token *identifier_token;
15690 /* Look for the identifier. */
15691 identifier_token = cp_lexer_peek_token (parser->lexer);
15692 ambiguous_p = identifier_token->ambiguous_p;
15693 identifier = cp_parser_identifier (parser);
15694 /* If the next token isn't an identifier, we are certainly not
15695 looking at a class-name. */
15696 if (identifier == error_mark_node)
15697 decl = error_mark_node;
15698 /* If we know this is a type-name, there's no need to look it
15700 else if (typename_p)
15704 tree ambiguous_decls;
15705 /* If we already know that this lookup is ambiguous, then
15706 we've already issued an error message; there's no reason
15710 cp_parser_simulate_error (parser);
15711 return error_mark_node;
15713 /* If the next token is a `::', then the name must be a type
15716 [basic.lookup.qual]
15718 During the lookup for a name preceding the :: scope
15719 resolution operator, object, function, and enumerator
15720 names are ignored. */
15721 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15722 tag_type = typename_type;
15723 /* Look up the name. */
15724 decl = cp_parser_lookup_name (parser, identifier,
15726 /*is_template=*/false,
15727 /*is_namespace=*/false,
15728 check_dependency_p,
15730 identifier_token->location);
15731 if (ambiguous_decls)
15733 if (cp_parser_parsing_tentatively (parser))
15734 cp_parser_simulate_error (parser);
15735 return error_mark_node;
15741 /* Try a template-id. */
15742 decl = cp_parser_template_id (parser, template_keyword_p,
15743 check_dependency_p,
15745 if (decl == error_mark_node)
15746 return error_mark_node;
15749 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15751 /* If this is a typename, create a TYPENAME_TYPE. */
15752 if (typename_p && decl != error_mark_node)
15754 decl = make_typename_type (scope, decl, typename_type,
15755 /*complain=*/tf_error);
15756 if (decl != error_mark_node)
15757 decl = TYPE_NAME (decl);
15760 /* Check to see that it is really the name of a class. */
15761 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15762 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15763 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15764 /* Situations like this:
15766 template <typename T> struct A {
15767 typename T::template X<int>::I i;
15770 are problematic. Is `T::template X<int>' a class-name? The
15771 standard does not seem to be definitive, but there is no other
15772 valid interpretation of the following `::'. Therefore, those
15773 names are considered class-names. */
15775 decl = make_typename_type (scope, decl, tag_type, tf_error);
15776 if (decl != error_mark_node)
15777 decl = TYPE_NAME (decl);
15779 else if (TREE_CODE (decl) != TYPE_DECL
15780 || TREE_TYPE (decl) == error_mark_node
15781 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15782 decl = error_mark_node;
15784 if (decl == error_mark_node)
15785 cp_parser_error (parser, "expected class-name");
15786 else if (identifier && !parser->scope)
15787 maybe_note_name_used_in_class (identifier, decl);
15792 /* Parse a class-specifier.
15795 class-head { member-specification [opt] }
15797 Returns the TREE_TYPE representing the class. */
15800 cp_parser_class_specifier (cp_parser* parser)
15803 tree attributes = NULL_TREE;
15804 bool nested_name_specifier_p;
15805 unsigned saved_num_template_parameter_lists;
15806 bool saved_in_function_body;
15807 bool saved_in_unbraced_linkage_specification_p;
15808 tree old_scope = NULL_TREE;
15809 tree scope = NULL_TREE;
15812 push_deferring_access_checks (dk_no_deferred);
15814 /* Parse the class-head. */
15815 type = cp_parser_class_head (parser,
15816 &nested_name_specifier_p,
15819 /* If the class-head was a semantic disaster, skip the entire body
15823 cp_parser_skip_to_end_of_block_or_statement (parser);
15824 pop_deferring_access_checks ();
15825 return error_mark_node;
15828 /* Look for the `{'. */
15829 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15831 pop_deferring_access_checks ();
15832 return error_mark_node;
15835 /* Process the base classes. If they're invalid, skip the
15836 entire class body. */
15837 if (!xref_basetypes (type, bases))
15839 /* Consuming the closing brace yields better error messages
15841 if (cp_parser_skip_to_closing_brace (parser))
15842 cp_lexer_consume_token (parser->lexer);
15843 pop_deferring_access_checks ();
15844 return error_mark_node;
15847 /* Issue an error message if type-definitions are forbidden here. */
15848 cp_parser_check_type_definition (parser);
15849 /* Remember that we are defining one more class. */
15850 ++parser->num_classes_being_defined;
15851 /* Inside the class, surrounding template-parameter-lists do not
15853 saved_num_template_parameter_lists
15854 = parser->num_template_parameter_lists;
15855 parser->num_template_parameter_lists = 0;
15856 /* We are not in a function body. */
15857 saved_in_function_body = parser->in_function_body;
15858 parser->in_function_body = false;
15859 /* We are not immediately inside an extern "lang" block. */
15860 saved_in_unbraced_linkage_specification_p
15861 = parser->in_unbraced_linkage_specification_p;
15862 parser->in_unbraced_linkage_specification_p = false;
15864 /* Start the class. */
15865 if (nested_name_specifier_p)
15867 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15868 old_scope = push_inner_scope (scope);
15870 type = begin_class_definition (type, attributes);
15872 if (type == error_mark_node)
15873 /* If the type is erroneous, skip the entire body of the class. */
15874 cp_parser_skip_to_closing_brace (parser);
15876 /* Parse the member-specification. */
15877 cp_parser_member_specification_opt (parser);
15879 /* Look for the trailing `}'. */
15880 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15881 /* Look for trailing attributes to apply to this class. */
15882 if (cp_parser_allow_gnu_extensions_p (parser))
15883 attributes = cp_parser_attributes_opt (parser);
15884 if (type != error_mark_node)
15885 type = finish_struct (type, attributes);
15886 if (nested_name_specifier_p)
15887 pop_inner_scope (old_scope, scope);
15888 /* If this class is not itself within the scope of another class,
15889 then we need to parse the bodies of all of the queued function
15890 definitions. Note that the queued functions defined in a class
15891 are not always processed immediately following the
15892 class-specifier for that class. Consider:
15895 struct B { void f() { sizeof (A); } };
15898 If `f' were processed before the processing of `A' were
15899 completed, there would be no way to compute the size of `A'.
15900 Note that the nesting we are interested in here is lexical --
15901 not the semantic nesting given by TYPE_CONTEXT. In particular,
15904 struct A { struct B; };
15905 struct A::B { void f() { } };
15907 there is no need to delay the parsing of `A::B::f'. */
15908 if (--parser->num_classes_being_defined == 0)
15912 tree class_type = NULL_TREE;
15913 tree pushed_scope = NULL_TREE;
15915 /* In a first pass, parse default arguments to the functions.
15916 Then, in a second pass, parse the bodies of the functions.
15917 This two-phased approach handles cases like:
15925 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15926 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15927 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15928 TREE_PURPOSE (parser->unparsed_functions_queues)
15929 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15931 fn = TREE_VALUE (queue_entry);
15932 /* If there are default arguments that have not yet been processed,
15933 take care of them now. */
15934 if (class_type != TREE_PURPOSE (queue_entry))
15937 pop_scope (pushed_scope);
15938 class_type = TREE_PURPOSE (queue_entry);
15939 pushed_scope = push_scope (class_type);
15941 /* Make sure that any template parameters are in scope. */
15942 maybe_begin_member_template_processing (fn);
15943 /* Parse the default argument expressions. */
15944 cp_parser_late_parsing_default_args (parser, fn);
15945 /* Remove any template parameters from the symbol table. */
15946 maybe_end_member_template_processing ();
15949 pop_scope (pushed_scope);
15950 /* Now parse the body of the functions. */
15951 for (TREE_VALUE (parser->unparsed_functions_queues)
15952 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15953 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15954 TREE_VALUE (parser->unparsed_functions_queues)
15955 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15957 /* Figure out which function we need to process. */
15958 fn = TREE_VALUE (queue_entry);
15959 /* Parse the function. */
15960 cp_parser_late_parsing_for_member (parser, fn);
15964 /* Put back any saved access checks. */
15965 pop_deferring_access_checks ();
15967 /* Restore saved state. */
15968 parser->in_function_body = saved_in_function_body;
15969 parser->num_template_parameter_lists
15970 = saved_num_template_parameter_lists;
15971 parser->in_unbraced_linkage_specification_p
15972 = saved_in_unbraced_linkage_specification_p;
15977 /* Parse a class-head.
15980 class-key identifier [opt] base-clause [opt]
15981 class-key nested-name-specifier identifier base-clause [opt]
15982 class-key nested-name-specifier [opt] template-id
15986 class-key attributes identifier [opt] base-clause [opt]
15987 class-key attributes nested-name-specifier identifier base-clause [opt]
15988 class-key attributes nested-name-specifier [opt] template-id
15991 Upon return BASES is initialized to the list of base classes (or
15992 NULL, if there are none) in the same form returned by
15993 cp_parser_base_clause.
15995 Returns the TYPE of the indicated class. Sets
15996 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15997 involving a nested-name-specifier was used, and FALSE otherwise.
15999 Returns error_mark_node if this is not a class-head.
16001 Returns NULL_TREE if the class-head is syntactically valid, but
16002 semantically invalid in a way that means we should skip the entire
16003 body of the class. */
16006 cp_parser_class_head (cp_parser* parser,
16007 bool* nested_name_specifier_p,
16008 tree *attributes_p,
16011 tree nested_name_specifier;
16012 enum tag_types class_key;
16013 tree id = NULL_TREE;
16014 tree type = NULL_TREE;
16016 bool template_id_p = false;
16017 bool qualified_p = false;
16018 bool invalid_nested_name_p = false;
16019 bool invalid_explicit_specialization_p = false;
16020 tree pushed_scope = NULL_TREE;
16021 unsigned num_templates;
16022 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16023 /* Assume no nested-name-specifier will be present. */
16024 *nested_name_specifier_p = false;
16025 /* Assume no template parameter lists will be used in defining the
16029 *bases = NULL_TREE;
16031 /* Look for the class-key. */
16032 class_key = cp_parser_class_key (parser);
16033 if (class_key == none_type)
16034 return error_mark_node;
16036 /* Parse the attributes. */
16037 attributes = cp_parser_attributes_opt (parser);
16039 /* If the next token is `::', that is invalid -- but sometimes
16040 people do try to write:
16044 Handle this gracefully by accepting the extra qualifier, and then
16045 issuing an error about it later if this really is a
16046 class-head. If it turns out just to be an elaborated type
16047 specifier, remain silent. */
16048 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16049 qualified_p = true;
16051 push_deferring_access_checks (dk_no_check);
16053 /* Determine the name of the class. Begin by looking for an
16054 optional nested-name-specifier. */
16055 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16056 nested_name_specifier
16057 = cp_parser_nested_name_specifier_opt (parser,
16058 /*typename_keyword_p=*/false,
16059 /*check_dependency_p=*/false,
16061 /*is_declaration=*/false);
16062 /* If there was a nested-name-specifier, then there *must* be an
16064 if (nested_name_specifier)
16066 type_start_token = cp_lexer_peek_token (parser->lexer);
16067 /* Although the grammar says `identifier', it really means
16068 `class-name' or `template-name'. You are only allowed to
16069 define a class that has already been declared with this
16072 The proposed resolution for Core Issue 180 says that wherever
16073 you see `class T::X' you should treat `X' as a type-name.
16075 It is OK to define an inaccessible class; for example:
16077 class A { class B; };
16080 We do not know if we will see a class-name, or a
16081 template-name. We look for a class-name first, in case the
16082 class-name is a template-id; if we looked for the
16083 template-name first we would stop after the template-name. */
16084 cp_parser_parse_tentatively (parser);
16085 type = cp_parser_class_name (parser,
16086 /*typename_keyword_p=*/false,
16087 /*template_keyword_p=*/false,
16089 /*check_dependency_p=*/false,
16090 /*class_head_p=*/true,
16091 /*is_declaration=*/false);
16092 /* If that didn't work, ignore the nested-name-specifier. */
16093 if (!cp_parser_parse_definitely (parser))
16095 invalid_nested_name_p = true;
16096 type_start_token = cp_lexer_peek_token (parser->lexer);
16097 id = cp_parser_identifier (parser);
16098 if (id == error_mark_node)
16101 /* If we could not find a corresponding TYPE, treat this
16102 declaration like an unqualified declaration. */
16103 if (type == error_mark_node)
16104 nested_name_specifier = NULL_TREE;
16105 /* Otherwise, count the number of templates used in TYPE and its
16106 containing scopes. */
16111 for (scope = TREE_TYPE (type);
16112 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16113 scope = (TYPE_P (scope)
16114 ? TYPE_CONTEXT (scope)
16115 : DECL_CONTEXT (scope)))
16117 && CLASS_TYPE_P (scope)
16118 && CLASSTYPE_TEMPLATE_INFO (scope)
16119 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16120 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16124 /* Otherwise, the identifier is optional. */
16127 /* We don't know whether what comes next is a template-id,
16128 an identifier, or nothing at all. */
16129 cp_parser_parse_tentatively (parser);
16130 /* Check for a template-id. */
16131 type_start_token = cp_lexer_peek_token (parser->lexer);
16132 id = cp_parser_template_id (parser,
16133 /*template_keyword_p=*/false,
16134 /*check_dependency_p=*/true,
16135 /*is_declaration=*/true);
16136 /* If that didn't work, it could still be an identifier. */
16137 if (!cp_parser_parse_definitely (parser))
16139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16141 type_start_token = cp_lexer_peek_token (parser->lexer);
16142 id = cp_parser_identifier (parser);
16149 template_id_p = true;
16154 pop_deferring_access_checks ();
16157 cp_parser_check_for_invalid_template_id (parser, id,
16158 type_start_token->location);
16160 /* If it's not a `:' or a `{' then we can't really be looking at a
16161 class-head, since a class-head only appears as part of a
16162 class-specifier. We have to detect this situation before calling
16163 xref_tag, since that has irreversible side-effects. */
16164 if (!cp_parser_next_token_starts_class_definition_p (parser))
16166 cp_parser_error (parser, "expected %<{%> or %<:%>");
16167 return error_mark_node;
16170 /* At this point, we're going ahead with the class-specifier, even
16171 if some other problem occurs. */
16172 cp_parser_commit_to_tentative_parse (parser);
16173 /* Issue the error about the overly-qualified name now. */
16176 cp_parser_error (parser,
16177 "global qualification of class name is invalid");
16178 return error_mark_node;
16180 else if (invalid_nested_name_p)
16182 cp_parser_error (parser,
16183 "qualified name does not name a class");
16184 return error_mark_node;
16186 else if (nested_name_specifier)
16190 /* Reject typedef-names in class heads. */
16191 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16193 error_at (type_start_token->location,
16194 "invalid class name in declaration of %qD",
16200 /* Figure out in what scope the declaration is being placed. */
16201 scope = current_scope ();
16202 /* If that scope does not contain the scope in which the
16203 class was originally declared, the program is invalid. */
16204 if (scope && !is_ancestor (scope, nested_name_specifier))
16206 if (at_namespace_scope_p ())
16207 error_at (type_start_token->location,
16208 "declaration of %qD in namespace %qD which does not "
16210 type, scope, nested_name_specifier);
16212 error_at (type_start_token->location,
16213 "declaration of %qD in %qD which does not enclose %qD",
16214 type, scope, nested_name_specifier);
16220 A declarator-id shall not be qualified except for the
16221 definition of a ... nested class outside of its class
16222 ... [or] the definition or explicit instantiation of a
16223 class member of a namespace outside of its namespace. */
16224 if (scope == nested_name_specifier)
16226 permerror (nested_name_specifier_token_start->location,
16227 "extra qualification not allowed");
16228 nested_name_specifier = NULL_TREE;
16232 /* An explicit-specialization must be preceded by "template <>". If
16233 it is not, try to recover gracefully. */
16234 if (at_namespace_scope_p ()
16235 && parser->num_template_parameter_lists == 0
16238 error_at (type_start_token->location,
16239 "an explicit specialization must be preceded by %<template <>%>");
16240 invalid_explicit_specialization_p = true;
16241 /* Take the same action that would have been taken by
16242 cp_parser_explicit_specialization. */
16243 ++parser->num_template_parameter_lists;
16244 begin_specialization ();
16246 /* There must be no "return" statements between this point and the
16247 end of this function; set "type "to the correct return value and
16248 use "goto done;" to return. */
16249 /* Make sure that the right number of template parameters were
16251 if (!cp_parser_check_template_parameters (parser, num_templates,
16252 type_start_token->location,
16253 /*declarator=*/NULL))
16255 /* If something went wrong, there is no point in even trying to
16256 process the class-definition. */
16261 /* Look up the type. */
16264 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16265 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16266 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16268 error_at (type_start_token->location,
16269 "function template %qD redeclared as a class template", id);
16270 type = error_mark_node;
16274 type = TREE_TYPE (id);
16275 type = maybe_process_partial_specialization (type);
16277 if (nested_name_specifier)
16278 pushed_scope = push_scope (nested_name_specifier);
16280 else if (nested_name_specifier)
16286 template <typename T> struct S { struct T };
16287 template <typename T> struct S<T>::T { };
16289 we will get a TYPENAME_TYPE when processing the definition of
16290 `S::T'. We need to resolve it to the actual type before we
16291 try to define it. */
16292 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16294 class_type = resolve_typename_type (TREE_TYPE (type),
16295 /*only_current_p=*/false);
16296 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16297 type = TYPE_NAME (class_type);
16300 cp_parser_error (parser, "could not resolve typename type");
16301 type = error_mark_node;
16305 if (maybe_process_partial_specialization (TREE_TYPE (type))
16306 == error_mark_node)
16312 class_type = current_class_type;
16313 /* Enter the scope indicated by the nested-name-specifier. */
16314 pushed_scope = push_scope (nested_name_specifier);
16315 /* Get the canonical version of this type. */
16316 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16317 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16318 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16320 type = push_template_decl (type);
16321 if (type == error_mark_node)
16328 type = TREE_TYPE (type);
16329 *nested_name_specifier_p = true;
16331 else /* The name is not a nested name. */
16333 /* If the class was unnamed, create a dummy name. */
16335 id = make_anon_name ();
16336 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16337 parser->num_template_parameter_lists);
16340 /* Indicate whether this class was declared as a `class' or as a
16342 if (TREE_CODE (type) == RECORD_TYPE)
16343 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16344 cp_parser_check_class_key (class_key, type);
16346 /* If this type was already complete, and we see another definition,
16347 that's an error. */
16348 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16350 error_at (type_start_token->location, "redefinition of %q#T",
16352 error_at (type_start_token->location, "previous definition of %q+#T",
16357 else if (type == error_mark_node)
16360 /* We will have entered the scope containing the class; the names of
16361 base classes should be looked up in that context. For example:
16363 struct A { struct B {}; struct C; };
16364 struct A::C : B {};
16368 /* Get the list of base-classes, if there is one. */
16369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16370 *bases = cp_parser_base_clause (parser);
16373 /* Leave the scope given by the nested-name-specifier. We will
16374 enter the class scope itself while processing the members. */
16376 pop_scope (pushed_scope);
16378 if (invalid_explicit_specialization_p)
16380 end_specialization ();
16381 --parser->num_template_parameter_lists;
16383 *attributes_p = attributes;
16387 /* Parse a class-key.
16394 Returns the kind of class-key specified, or none_type to indicate
16397 static enum tag_types
16398 cp_parser_class_key (cp_parser* parser)
16401 enum tag_types tag_type;
16403 /* Look for the class-key. */
16404 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16408 /* Check to see if the TOKEN is a class-key. */
16409 tag_type = cp_parser_token_is_class_key (token);
16411 cp_parser_error (parser, "expected class-key");
16415 /* Parse an (optional) member-specification.
16417 member-specification:
16418 member-declaration member-specification [opt]
16419 access-specifier : member-specification [opt] */
16422 cp_parser_member_specification_opt (cp_parser* parser)
16429 /* Peek at the next token. */
16430 token = cp_lexer_peek_token (parser->lexer);
16431 /* If it's a `}', or EOF then we've seen all the members. */
16432 if (token->type == CPP_CLOSE_BRACE
16433 || token->type == CPP_EOF
16434 || token->type == CPP_PRAGMA_EOL)
16437 /* See if this token is a keyword. */
16438 keyword = token->keyword;
16442 case RID_PROTECTED:
16444 /* Consume the access-specifier. */
16445 cp_lexer_consume_token (parser->lexer);
16446 /* Remember which access-specifier is active. */
16447 current_access_specifier = token->u.value;
16448 /* Look for the `:'. */
16449 cp_parser_require (parser, CPP_COLON, "%<:%>");
16453 /* Accept #pragmas at class scope. */
16454 if (token->type == CPP_PRAGMA)
16456 cp_parser_pragma (parser, pragma_external);
16460 /* Otherwise, the next construction must be a
16461 member-declaration. */
16462 cp_parser_member_declaration (parser);
16467 /* Parse a member-declaration.
16469 member-declaration:
16470 decl-specifier-seq [opt] member-declarator-list [opt] ;
16471 function-definition ; [opt]
16472 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16474 template-declaration
16476 member-declarator-list:
16478 member-declarator-list , member-declarator
16481 declarator pure-specifier [opt]
16482 declarator constant-initializer [opt]
16483 identifier [opt] : constant-expression
16487 member-declaration:
16488 __extension__ member-declaration
16491 declarator attributes [opt] pure-specifier [opt]
16492 declarator attributes [opt] constant-initializer [opt]
16493 identifier [opt] attributes [opt] : constant-expression
16497 member-declaration:
16498 static_assert-declaration */
16501 cp_parser_member_declaration (cp_parser* parser)
16503 cp_decl_specifier_seq decl_specifiers;
16504 tree prefix_attributes;
16506 int declares_class_or_enum;
16508 cp_token *token = NULL;
16509 cp_token *decl_spec_token_start = NULL;
16510 cp_token *initializer_token_start = NULL;
16511 int saved_pedantic;
16513 /* Check for the `__extension__' keyword. */
16514 if (cp_parser_extension_opt (parser, &saved_pedantic))
16517 cp_parser_member_declaration (parser);
16518 /* Restore the old value of the PEDANTIC flag. */
16519 pedantic = saved_pedantic;
16524 /* Check for a template-declaration. */
16525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16527 /* An explicit specialization here is an error condition, and we
16528 expect the specialization handler to detect and report this. */
16529 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16530 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16531 cp_parser_explicit_specialization (parser);
16533 cp_parser_template_declaration (parser, /*member_p=*/true);
16538 /* Check for a using-declaration. */
16539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16541 /* Parse the using-declaration. */
16542 cp_parser_using_declaration (parser,
16543 /*access_declaration_p=*/false);
16547 /* Check for @defs. */
16548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16551 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16552 ivar = ivar_chains;
16556 ivar = TREE_CHAIN (member);
16557 TREE_CHAIN (member) = NULL_TREE;
16558 finish_member_declaration (member);
16563 /* If the next token is `static_assert' we have a static assertion. */
16564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16566 cp_parser_static_assert (parser, /*member_p=*/true);
16570 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16573 /* Parse the decl-specifier-seq. */
16574 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16575 cp_parser_decl_specifier_seq (parser,
16576 CP_PARSER_FLAGS_OPTIONAL,
16578 &declares_class_or_enum);
16579 prefix_attributes = decl_specifiers.attributes;
16580 decl_specifiers.attributes = NULL_TREE;
16581 /* Check for an invalid type-name. */
16582 if (!decl_specifiers.any_type_specifiers_p
16583 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16585 /* If there is no declarator, then the decl-specifier-seq should
16587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16589 /* If there was no decl-specifier-seq, and the next token is a
16590 `;', then we have something like:
16596 Each member-declaration shall declare at least one member
16597 name of the class. */
16598 if (!decl_specifiers.any_specifiers_p)
16600 cp_token *token = cp_lexer_peek_token (parser->lexer);
16601 if (!in_system_header_at (token->location))
16602 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16608 /* See if this declaration is a friend. */
16609 friend_p = cp_parser_friend_p (&decl_specifiers);
16610 /* If there were decl-specifiers, check to see if there was
16611 a class-declaration. */
16612 type = check_tag_decl (&decl_specifiers);
16613 /* Nested classes have already been added to the class, but
16614 a `friend' needs to be explicitly registered. */
16617 /* If the `friend' keyword was present, the friend must
16618 be introduced with a class-key. */
16619 if (!declares_class_or_enum)
16620 error_at (decl_spec_token_start->location,
16621 "a class-key must be used when declaring a friend");
16624 template <typename T> struct A {
16625 friend struct A<T>::B;
16628 A<T>::B will be represented by a TYPENAME_TYPE, and
16629 therefore not recognized by check_tag_decl. */
16631 && decl_specifiers.type
16632 && TYPE_P (decl_specifiers.type))
16633 type = decl_specifiers.type;
16634 if (!type || !TYPE_P (type))
16635 error_at (decl_spec_token_start->location,
16636 "friend declaration does not name a class or "
16639 make_friend_class (current_class_type, type,
16640 /*complain=*/true);
16642 /* If there is no TYPE, an error message will already have
16644 else if (!type || type == error_mark_node)
16646 /* An anonymous aggregate has to be handled specially; such
16647 a declaration really declares a data member (with a
16648 particular type), as opposed to a nested class. */
16649 else if (ANON_AGGR_TYPE_P (type))
16651 /* Remove constructors and such from TYPE, now that we
16652 know it is an anonymous aggregate. */
16653 fixup_anonymous_aggr (type);
16654 /* And make the corresponding data member. */
16655 decl = build_decl (decl_spec_token_start->location,
16656 FIELD_DECL, NULL_TREE, type);
16657 /* Add it to the class. */
16658 finish_member_declaration (decl);
16661 cp_parser_check_access_in_redeclaration
16663 decl_spec_token_start->location);
16668 /* See if these declarations will be friends. */
16669 friend_p = cp_parser_friend_p (&decl_specifiers);
16671 /* Keep going until we hit the `;' at the end of the
16673 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16675 tree attributes = NULL_TREE;
16676 tree first_attribute;
16678 /* Peek at the next token. */
16679 token = cp_lexer_peek_token (parser->lexer);
16681 /* Check for a bitfield declaration. */
16682 if (token->type == CPP_COLON
16683 || (token->type == CPP_NAME
16684 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16690 /* Get the name of the bitfield. Note that we cannot just
16691 check TOKEN here because it may have been invalidated by
16692 the call to cp_lexer_peek_nth_token above. */
16693 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16694 identifier = cp_parser_identifier (parser);
16696 identifier = NULL_TREE;
16698 /* Consume the `:' token. */
16699 cp_lexer_consume_token (parser->lexer);
16700 /* Get the width of the bitfield. */
16702 = cp_parser_constant_expression (parser,
16703 /*allow_non_constant=*/false,
16706 /* Look for attributes that apply to the bitfield. */
16707 attributes = cp_parser_attributes_opt (parser);
16708 /* Remember which attributes are prefix attributes and
16710 first_attribute = attributes;
16711 /* Combine the attributes. */
16712 attributes = chainon (prefix_attributes, attributes);
16714 /* Create the bitfield declaration. */
16715 decl = grokbitfield (identifier
16716 ? make_id_declarator (NULL_TREE,
16726 cp_declarator *declarator;
16728 tree asm_specification;
16729 int ctor_dtor_or_conv_p;
16731 /* Parse the declarator. */
16733 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16734 &ctor_dtor_or_conv_p,
16735 /*parenthesized_p=*/NULL,
16736 /*member_p=*/true);
16738 /* If something went wrong parsing the declarator, make sure
16739 that we at least consume some tokens. */
16740 if (declarator == cp_error_declarator)
16742 /* Skip to the end of the statement. */
16743 cp_parser_skip_to_end_of_statement (parser);
16744 /* If the next token is not a semicolon, that is
16745 probably because we just skipped over the body of
16746 a function. So, we consume a semicolon if
16747 present, but do not issue an error message if it
16749 if (cp_lexer_next_token_is (parser->lexer,
16751 cp_lexer_consume_token (parser->lexer);
16755 if (declares_class_or_enum & 2)
16756 cp_parser_check_for_definition_in_return_type
16757 (declarator, decl_specifiers.type,
16758 decl_specifiers.type_location);
16760 /* Look for an asm-specification. */
16761 asm_specification = cp_parser_asm_specification_opt (parser);
16762 /* Look for attributes that apply to the declaration. */
16763 attributes = cp_parser_attributes_opt (parser);
16764 /* Remember which attributes are prefix attributes and
16766 first_attribute = attributes;
16767 /* Combine the attributes. */
16768 attributes = chainon (prefix_attributes, attributes);
16770 /* If it's an `=', then we have a constant-initializer or a
16771 pure-specifier. It is not correct to parse the
16772 initializer before registering the member declaration
16773 since the member declaration should be in scope while
16774 its initializer is processed. However, the rest of the
16775 front end does not yet provide an interface that allows
16776 us to handle this correctly. */
16777 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16781 A pure-specifier shall be used only in the declaration of
16782 a virtual function.
16784 A member-declarator can contain a constant-initializer
16785 only if it declares a static member of integral or
16788 Therefore, if the DECLARATOR is for a function, we look
16789 for a pure-specifier; otherwise, we look for a
16790 constant-initializer. When we call `grokfield', it will
16791 perform more stringent semantics checks. */
16792 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16793 if (function_declarator_p (declarator))
16794 initializer = cp_parser_pure_specifier (parser);
16796 /* Parse the initializer. */
16797 initializer = cp_parser_constant_initializer (parser);
16799 /* Otherwise, there is no initializer. */
16801 initializer = NULL_TREE;
16803 /* See if we are probably looking at a function
16804 definition. We are certainly not looking at a
16805 member-declarator. Calling `grokfield' has
16806 side-effects, so we must not do it unless we are sure
16807 that we are looking at a member-declarator. */
16808 if (cp_parser_token_starts_function_definition_p
16809 (cp_lexer_peek_token (parser->lexer)))
16811 /* The grammar does not allow a pure-specifier to be
16812 used when a member function is defined. (It is
16813 possible that this fact is an oversight in the
16814 standard, since a pure function may be defined
16815 outside of the class-specifier. */
16817 error_at (initializer_token_start->location,
16818 "pure-specifier on function-definition");
16819 decl = cp_parser_save_member_function_body (parser,
16823 /* If the member was not a friend, declare it here. */
16825 finish_member_declaration (decl);
16826 /* Peek at the next token. */
16827 token = cp_lexer_peek_token (parser->lexer);
16828 /* If the next token is a semicolon, consume it. */
16829 if (token->type == CPP_SEMICOLON)
16830 cp_lexer_consume_token (parser->lexer);
16834 if (declarator->kind == cdk_function)
16835 declarator->id_loc = token->location;
16836 /* Create the declaration. */
16837 decl = grokfield (declarator, &decl_specifiers,
16838 initializer, /*init_const_expr_p=*/true,
16843 /* Reset PREFIX_ATTRIBUTES. */
16844 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16845 attributes = TREE_CHAIN (attributes);
16847 TREE_CHAIN (attributes) = NULL_TREE;
16849 /* If there is any qualification still in effect, clear it
16850 now; we will be starting fresh with the next declarator. */
16851 parser->scope = NULL_TREE;
16852 parser->qualifying_scope = NULL_TREE;
16853 parser->object_scope = NULL_TREE;
16854 /* If it's a `,', then there are more declarators. */
16855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16856 cp_lexer_consume_token (parser->lexer);
16857 /* If the next token isn't a `;', then we have a parse error. */
16858 else if (cp_lexer_next_token_is_not (parser->lexer,
16861 cp_parser_error (parser, "expected %<;%>");
16862 /* Skip tokens until we find a `;'. */
16863 cp_parser_skip_to_end_of_statement (parser);
16870 /* Add DECL to the list of members. */
16872 finish_member_declaration (decl);
16874 if (TREE_CODE (decl) == FUNCTION_DECL)
16875 cp_parser_save_default_args (parser, decl);
16880 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16883 /* Parse a pure-specifier.
16888 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16889 Otherwise, ERROR_MARK_NODE is returned. */
16892 cp_parser_pure_specifier (cp_parser* parser)
16896 /* Look for the `=' token. */
16897 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16898 return error_mark_node;
16899 /* Look for the `0' token. */
16900 token = cp_lexer_peek_token (parser->lexer);
16902 if (token->type == CPP_EOF
16903 || token->type == CPP_PRAGMA_EOL)
16904 return error_mark_node;
16906 cp_lexer_consume_token (parser->lexer);
16908 /* Accept = default or = delete in c++0x mode. */
16909 if (token->keyword == RID_DEFAULT
16910 || token->keyword == RID_DELETE)
16912 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16913 return token->u.value;
16916 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16917 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16919 cp_parser_error (parser,
16920 "invalid pure specifier (only %<= 0%> is allowed)");
16921 cp_parser_skip_to_end_of_statement (parser);
16922 return error_mark_node;
16924 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16926 error_at (token->location, "templates may not be %<virtual%>");
16927 return error_mark_node;
16930 return integer_zero_node;
16933 /* Parse a constant-initializer.
16935 constant-initializer:
16936 = constant-expression
16938 Returns a representation of the constant-expression. */
16941 cp_parser_constant_initializer (cp_parser* parser)
16943 /* Look for the `=' token. */
16944 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16945 return error_mark_node;
16947 /* It is invalid to write:
16949 struct S { static const int i = { 7 }; };
16952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16954 cp_parser_error (parser,
16955 "a brace-enclosed initializer is not allowed here");
16956 /* Consume the opening brace. */
16957 cp_lexer_consume_token (parser->lexer);
16958 /* Skip the initializer. */
16959 cp_parser_skip_to_closing_brace (parser);
16960 /* Look for the trailing `}'. */
16961 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16963 return error_mark_node;
16966 return cp_parser_constant_expression (parser,
16967 /*allow_non_constant=*/false,
16971 /* Derived classes [gram.class.derived] */
16973 /* Parse a base-clause.
16976 : base-specifier-list
16978 base-specifier-list:
16979 base-specifier ... [opt]
16980 base-specifier-list , base-specifier ... [opt]
16982 Returns a TREE_LIST representing the base-classes, in the order in
16983 which they were declared. The representation of each node is as
16984 described by cp_parser_base_specifier.
16986 In the case that no bases are specified, this function will return
16987 NULL_TREE, not ERROR_MARK_NODE. */
16990 cp_parser_base_clause (cp_parser* parser)
16992 tree bases = NULL_TREE;
16994 /* Look for the `:' that begins the list. */
16995 cp_parser_require (parser, CPP_COLON, "%<:%>");
16997 /* Scan the base-specifier-list. */
17002 bool pack_expansion_p = false;
17004 /* Look for the base-specifier. */
17005 base = cp_parser_base_specifier (parser);
17006 /* Look for the (optional) ellipsis. */
17007 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17009 /* Consume the `...'. */
17010 cp_lexer_consume_token (parser->lexer);
17012 pack_expansion_p = true;
17015 /* Add BASE to the front of the list. */
17016 if (base != error_mark_node)
17018 if (pack_expansion_p)
17019 /* Make this a pack expansion type. */
17020 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17023 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17025 TREE_CHAIN (base) = bases;
17029 /* Peek at the next token. */
17030 token = cp_lexer_peek_token (parser->lexer);
17031 /* If it's not a comma, then the list is complete. */
17032 if (token->type != CPP_COMMA)
17034 /* Consume the `,'. */
17035 cp_lexer_consume_token (parser->lexer);
17038 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17039 base class had a qualified name. However, the next name that
17040 appears is certainly not qualified. */
17041 parser->scope = NULL_TREE;
17042 parser->qualifying_scope = NULL_TREE;
17043 parser->object_scope = NULL_TREE;
17045 return nreverse (bases);
17048 /* Parse a base-specifier.
17051 :: [opt] nested-name-specifier [opt] class-name
17052 virtual access-specifier [opt] :: [opt] nested-name-specifier
17054 access-specifier virtual [opt] :: [opt] nested-name-specifier
17057 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17058 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17059 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17060 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17063 cp_parser_base_specifier (cp_parser* parser)
17067 bool virtual_p = false;
17068 bool duplicate_virtual_error_issued_p = false;
17069 bool duplicate_access_error_issued_p = false;
17070 bool class_scope_p, template_p;
17071 tree access = access_default_node;
17074 /* Process the optional `virtual' and `access-specifier'. */
17077 /* Peek at the next token. */
17078 token = cp_lexer_peek_token (parser->lexer);
17079 /* Process `virtual'. */
17080 switch (token->keyword)
17083 /* If `virtual' appears more than once, issue an error. */
17084 if (virtual_p && !duplicate_virtual_error_issued_p)
17086 cp_parser_error (parser,
17087 "%<virtual%> specified more than once in base-specified");
17088 duplicate_virtual_error_issued_p = true;
17093 /* Consume the `virtual' token. */
17094 cp_lexer_consume_token (parser->lexer);
17099 case RID_PROTECTED:
17101 /* If more than one access specifier appears, issue an
17103 if (access != access_default_node
17104 && !duplicate_access_error_issued_p)
17106 cp_parser_error (parser,
17107 "more than one access specifier in base-specified");
17108 duplicate_access_error_issued_p = true;
17111 access = ridpointers[(int) token->keyword];
17113 /* Consume the access-specifier. */
17114 cp_lexer_consume_token (parser->lexer);
17123 /* It is not uncommon to see programs mechanically, erroneously, use
17124 the 'typename' keyword to denote (dependent) qualified types
17125 as base classes. */
17126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17128 token = cp_lexer_peek_token (parser->lexer);
17129 if (!processing_template_decl)
17130 error_at (token->location,
17131 "keyword %<typename%> not allowed outside of templates");
17133 error_at (token->location,
17134 "keyword %<typename%> not allowed in this context "
17135 "(the base class is implicitly a type)");
17136 cp_lexer_consume_token (parser->lexer);
17139 /* Look for the optional `::' operator. */
17140 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17141 /* Look for the nested-name-specifier. The simplest way to
17146 The keyword `typename' is not permitted in a base-specifier or
17147 mem-initializer; in these contexts a qualified name that
17148 depends on a template-parameter is implicitly assumed to be a
17151 is to pretend that we have seen the `typename' keyword at this
17153 cp_parser_nested_name_specifier_opt (parser,
17154 /*typename_keyword_p=*/true,
17155 /*check_dependency_p=*/true,
17157 /*is_declaration=*/true);
17158 /* If the base class is given by a qualified name, assume that names
17159 we see are type names or templates, as appropriate. */
17160 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17161 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17163 /* Finally, look for the class-name. */
17164 type = cp_parser_class_name (parser,
17168 /*check_dependency_p=*/true,
17169 /*class_head_p=*/false,
17170 /*is_declaration=*/true);
17172 if (type == error_mark_node)
17173 return error_mark_node;
17175 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17178 /* Exception handling [gram.exception] */
17180 /* Parse an (optional) exception-specification.
17182 exception-specification:
17183 throw ( type-id-list [opt] )
17185 Returns a TREE_LIST representing the exception-specification. The
17186 TREE_VALUE of each node is a type. */
17189 cp_parser_exception_specification_opt (cp_parser* parser)
17194 /* Peek at the next token. */
17195 token = cp_lexer_peek_token (parser->lexer);
17196 /* If it's not `throw', then there's no exception-specification. */
17197 if (!cp_parser_is_keyword (token, RID_THROW))
17200 /* Consume the `throw'. */
17201 cp_lexer_consume_token (parser->lexer);
17203 /* Look for the `('. */
17204 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17206 /* Peek at the next token. */
17207 token = cp_lexer_peek_token (parser->lexer);
17208 /* If it's not a `)', then there is a type-id-list. */
17209 if (token->type != CPP_CLOSE_PAREN)
17211 const char *saved_message;
17213 /* Types may not be defined in an exception-specification. */
17214 saved_message = parser->type_definition_forbidden_message;
17215 parser->type_definition_forbidden_message
17216 = G_("types may not be defined in an exception-specification");
17217 /* Parse the type-id-list. */
17218 type_id_list = cp_parser_type_id_list (parser);
17219 /* Restore the saved message. */
17220 parser->type_definition_forbidden_message = saved_message;
17223 type_id_list = empty_except_spec;
17225 /* Look for the `)'. */
17226 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17228 return type_id_list;
17231 /* Parse an (optional) type-id-list.
17235 type-id-list , type-id ... [opt]
17237 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17238 in the order that the types were presented. */
17241 cp_parser_type_id_list (cp_parser* parser)
17243 tree types = NULL_TREE;
17250 /* Get the next type-id. */
17251 type = cp_parser_type_id (parser);
17252 /* Parse the optional ellipsis. */
17253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17255 /* Consume the `...'. */
17256 cp_lexer_consume_token (parser->lexer);
17258 /* Turn the type into a pack expansion expression. */
17259 type = make_pack_expansion (type);
17261 /* Add it to the list. */
17262 types = add_exception_specifier (types, type, /*complain=*/1);
17263 /* Peek at the next token. */
17264 token = cp_lexer_peek_token (parser->lexer);
17265 /* If it is not a `,', we are done. */
17266 if (token->type != CPP_COMMA)
17268 /* Consume the `,'. */
17269 cp_lexer_consume_token (parser->lexer);
17272 return nreverse (types);
17275 /* Parse a try-block.
17278 try compound-statement handler-seq */
17281 cp_parser_try_block (cp_parser* parser)
17285 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17286 try_block = begin_try_block ();
17287 cp_parser_compound_statement (parser, NULL, true);
17288 finish_try_block (try_block);
17289 cp_parser_handler_seq (parser);
17290 finish_handler_sequence (try_block);
17295 /* Parse a function-try-block.
17297 function-try-block:
17298 try ctor-initializer [opt] function-body handler-seq */
17301 cp_parser_function_try_block (cp_parser* parser)
17303 tree compound_stmt;
17305 bool ctor_initializer_p;
17307 /* Look for the `try' keyword. */
17308 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17310 /* Let the rest of the front end know where we are. */
17311 try_block = begin_function_try_block (&compound_stmt);
17312 /* Parse the function-body. */
17314 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17315 /* We're done with the `try' part. */
17316 finish_function_try_block (try_block);
17317 /* Parse the handlers. */
17318 cp_parser_handler_seq (parser);
17319 /* We're done with the handlers. */
17320 finish_function_handler_sequence (try_block, compound_stmt);
17322 return ctor_initializer_p;
17325 /* Parse a handler-seq.
17328 handler handler-seq [opt] */
17331 cp_parser_handler_seq (cp_parser* parser)
17337 /* Parse the handler. */
17338 cp_parser_handler (parser);
17339 /* Peek at the next token. */
17340 token = cp_lexer_peek_token (parser->lexer);
17341 /* If it's not `catch' then there are no more handlers. */
17342 if (!cp_parser_is_keyword (token, RID_CATCH))
17347 /* Parse a handler.
17350 catch ( exception-declaration ) compound-statement */
17353 cp_parser_handler (cp_parser* parser)
17358 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17359 handler = begin_handler ();
17360 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17361 declaration = cp_parser_exception_declaration (parser);
17362 finish_handler_parms (declaration, handler);
17363 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17364 cp_parser_compound_statement (parser, NULL, false);
17365 finish_handler (handler);
17368 /* Parse an exception-declaration.
17370 exception-declaration:
17371 type-specifier-seq declarator
17372 type-specifier-seq abstract-declarator
17376 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17377 ellipsis variant is used. */
17380 cp_parser_exception_declaration (cp_parser* parser)
17382 cp_decl_specifier_seq type_specifiers;
17383 cp_declarator *declarator;
17384 const char *saved_message;
17386 /* If it's an ellipsis, it's easy to handle. */
17387 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17389 /* Consume the `...' token. */
17390 cp_lexer_consume_token (parser->lexer);
17394 /* Types may not be defined in exception-declarations. */
17395 saved_message = parser->type_definition_forbidden_message;
17396 parser->type_definition_forbidden_message
17397 = G_("types may not be defined in exception-declarations");
17399 /* Parse the type-specifier-seq. */
17400 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17401 /*is_trailing_return=*/false,
17403 /* If it's a `)', then there is no declarator. */
17404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17407 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17408 /*ctor_dtor_or_conv_p=*/NULL,
17409 /*parenthesized_p=*/NULL,
17410 /*member_p=*/false);
17412 /* Restore the saved message. */
17413 parser->type_definition_forbidden_message = saved_message;
17415 if (!type_specifiers.any_specifiers_p)
17416 return error_mark_node;
17418 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17421 /* Parse a throw-expression.
17424 throw assignment-expression [opt]
17426 Returns a THROW_EXPR representing the throw-expression. */
17429 cp_parser_throw_expression (cp_parser* parser)
17434 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17435 token = cp_lexer_peek_token (parser->lexer);
17436 /* Figure out whether or not there is an assignment-expression
17437 following the "throw" keyword. */
17438 if (token->type == CPP_COMMA
17439 || token->type == CPP_SEMICOLON
17440 || token->type == CPP_CLOSE_PAREN
17441 || token->type == CPP_CLOSE_SQUARE
17442 || token->type == CPP_CLOSE_BRACE
17443 || token->type == CPP_COLON)
17444 expression = NULL_TREE;
17446 expression = cp_parser_assignment_expression (parser,
17447 /*cast_p=*/false, NULL);
17449 return build_throw (expression);
17452 /* GNU Extensions */
17454 /* Parse an (optional) asm-specification.
17457 asm ( string-literal )
17459 If the asm-specification is present, returns a STRING_CST
17460 corresponding to the string-literal. Otherwise, returns
17464 cp_parser_asm_specification_opt (cp_parser* parser)
17467 tree asm_specification;
17469 /* Peek at the next token. */
17470 token = cp_lexer_peek_token (parser->lexer);
17471 /* If the next token isn't the `asm' keyword, then there's no
17472 asm-specification. */
17473 if (!cp_parser_is_keyword (token, RID_ASM))
17476 /* Consume the `asm' token. */
17477 cp_lexer_consume_token (parser->lexer);
17478 /* Look for the `('. */
17479 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17481 /* Look for the string-literal. */
17482 asm_specification = cp_parser_string_literal (parser, false, false);
17484 /* Look for the `)'. */
17485 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17487 return asm_specification;
17490 /* Parse an asm-operand-list.
17494 asm-operand-list , asm-operand
17497 string-literal ( expression )
17498 [ string-literal ] string-literal ( expression )
17500 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17501 each node is the expression. The TREE_PURPOSE is itself a
17502 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17503 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17504 is a STRING_CST for the string literal before the parenthesis. Returns
17505 ERROR_MARK_NODE if any of the operands are invalid. */
17508 cp_parser_asm_operand_list (cp_parser* parser)
17510 tree asm_operands = NULL_TREE;
17511 bool invalid_operands = false;
17515 tree string_literal;
17519 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17521 /* Consume the `[' token. */
17522 cp_lexer_consume_token (parser->lexer);
17523 /* Read the operand name. */
17524 name = cp_parser_identifier (parser);
17525 if (name != error_mark_node)
17526 name = build_string (IDENTIFIER_LENGTH (name),
17527 IDENTIFIER_POINTER (name));
17528 /* Look for the closing `]'. */
17529 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17533 /* Look for the string-literal. */
17534 string_literal = cp_parser_string_literal (parser, false, false);
17536 /* Look for the `('. */
17537 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17538 /* Parse the expression. */
17539 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17540 /* Look for the `)'. */
17541 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17543 if (name == error_mark_node
17544 || string_literal == error_mark_node
17545 || expression == error_mark_node)
17546 invalid_operands = true;
17548 /* Add this operand to the list. */
17549 asm_operands = tree_cons (build_tree_list (name, string_literal),
17552 /* If the next token is not a `,', there are no more
17554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17556 /* Consume the `,'. */
17557 cp_lexer_consume_token (parser->lexer);
17560 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17563 /* Parse an asm-clobber-list.
17567 asm-clobber-list , string-literal
17569 Returns a TREE_LIST, indicating the clobbers in the order that they
17570 appeared. The TREE_VALUE of each node is a STRING_CST. */
17573 cp_parser_asm_clobber_list (cp_parser* parser)
17575 tree clobbers = NULL_TREE;
17579 tree string_literal;
17581 /* Look for the string literal. */
17582 string_literal = cp_parser_string_literal (parser, false, false);
17583 /* Add it to the list. */
17584 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17585 /* If the next token is not a `,', then the list is
17587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17589 /* Consume the `,' token. */
17590 cp_lexer_consume_token (parser->lexer);
17596 /* Parse an asm-label-list.
17600 asm-label-list , identifier
17602 Returns a TREE_LIST, indicating the labels in the order that they
17603 appeared. The TREE_VALUE of each node is a label. */
17606 cp_parser_asm_label_list (cp_parser* parser)
17608 tree labels = NULL_TREE;
17612 tree identifier, label, name;
17614 /* Look for the identifier. */
17615 identifier = cp_parser_identifier (parser);
17616 if (!error_operand_p (identifier))
17618 label = lookup_label (identifier);
17619 if (TREE_CODE (label) == LABEL_DECL)
17621 TREE_USED (label) = 1;
17622 check_goto (label);
17623 name = build_string (IDENTIFIER_LENGTH (identifier),
17624 IDENTIFIER_POINTER (identifier));
17625 labels = tree_cons (name, label, labels);
17628 /* If the next token is not a `,', then the list is
17630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17632 /* Consume the `,' token. */
17633 cp_lexer_consume_token (parser->lexer);
17636 return nreverse (labels);
17639 /* Parse an (optional) series of attributes.
17642 attributes attribute
17645 __attribute__ (( attribute-list [opt] ))
17647 The return value is as for cp_parser_attribute_list. */
17650 cp_parser_attributes_opt (cp_parser* parser)
17652 tree attributes = NULL_TREE;
17657 tree attribute_list;
17659 /* Peek at the next token. */
17660 token = cp_lexer_peek_token (parser->lexer);
17661 /* If it's not `__attribute__', then we're done. */
17662 if (token->keyword != RID_ATTRIBUTE)
17665 /* Consume the `__attribute__' keyword. */
17666 cp_lexer_consume_token (parser->lexer);
17667 /* Look for the two `(' tokens. */
17668 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17669 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17671 /* Peek at the next token. */
17672 token = cp_lexer_peek_token (parser->lexer);
17673 if (token->type != CPP_CLOSE_PAREN)
17674 /* Parse the attribute-list. */
17675 attribute_list = cp_parser_attribute_list (parser);
17677 /* If the next token is a `)', then there is no attribute
17679 attribute_list = NULL;
17681 /* Look for the two `)' tokens. */
17682 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17683 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17685 /* Add these new attributes to the list. */
17686 attributes = chainon (attributes, attribute_list);
17692 /* Parse an attribute-list.
17696 attribute-list , attribute
17700 identifier ( identifier )
17701 identifier ( identifier , expression-list )
17702 identifier ( expression-list )
17704 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17705 to an attribute. The TREE_PURPOSE of each node is the identifier
17706 indicating which attribute is in use. The TREE_VALUE represents
17707 the arguments, if any. */
17710 cp_parser_attribute_list (cp_parser* parser)
17712 tree attribute_list = NULL_TREE;
17713 bool save_translate_strings_p = parser->translate_strings_p;
17715 parser->translate_strings_p = false;
17722 /* Look for the identifier. We also allow keywords here; for
17723 example `__attribute__ ((const))' is legal. */
17724 token = cp_lexer_peek_token (parser->lexer);
17725 if (token->type == CPP_NAME
17726 || token->type == CPP_KEYWORD)
17728 tree arguments = NULL_TREE;
17730 /* Consume the token. */
17731 token = cp_lexer_consume_token (parser->lexer);
17733 /* Save away the identifier that indicates which attribute
17735 identifier = (token->type == CPP_KEYWORD)
17736 /* For keywords, use the canonical spelling, not the
17737 parsed identifier. */
17738 ? ridpointers[(int) token->keyword]
17741 attribute = build_tree_list (identifier, NULL_TREE);
17743 /* Peek at the next token. */
17744 token = cp_lexer_peek_token (parser->lexer);
17745 /* If it's an `(', then parse the attribute arguments. */
17746 if (token->type == CPP_OPEN_PAREN)
17749 vec = cp_parser_parenthesized_expression_list
17750 (parser, true, /*cast_p=*/false,
17751 /*allow_expansion_p=*/false,
17752 /*non_constant_p=*/NULL);
17754 arguments = error_mark_node;
17757 arguments = build_tree_list_vec (vec);
17758 release_tree_vector (vec);
17760 /* Save the arguments away. */
17761 TREE_VALUE (attribute) = arguments;
17764 if (arguments != error_mark_node)
17766 /* Add this attribute to the list. */
17767 TREE_CHAIN (attribute) = attribute_list;
17768 attribute_list = attribute;
17771 token = cp_lexer_peek_token (parser->lexer);
17773 /* Now, look for more attributes. If the next token isn't a
17774 `,', we're done. */
17775 if (token->type != CPP_COMMA)
17778 /* Consume the comma and keep going. */
17779 cp_lexer_consume_token (parser->lexer);
17781 parser->translate_strings_p = save_translate_strings_p;
17783 /* We built up the list in reverse order. */
17784 return nreverse (attribute_list);
17787 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17788 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17789 current value of the PEDANTIC flag, regardless of whether or not
17790 the `__extension__' keyword is present. The caller is responsible
17791 for restoring the value of the PEDANTIC flag. */
17794 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17796 /* Save the old value of the PEDANTIC flag. */
17797 *saved_pedantic = pedantic;
17799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17801 /* Consume the `__extension__' token. */
17802 cp_lexer_consume_token (parser->lexer);
17803 /* We're not being pedantic while the `__extension__' keyword is
17813 /* Parse a label declaration.
17816 __label__ label-declarator-seq ;
17818 label-declarator-seq:
17819 identifier , label-declarator-seq
17823 cp_parser_label_declaration (cp_parser* parser)
17825 /* Look for the `__label__' keyword. */
17826 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17832 /* Look for an identifier. */
17833 identifier = cp_parser_identifier (parser);
17834 /* If we failed, stop. */
17835 if (identifier == error_mark_node)
17837 /* Declare it as a label. */
17838 finish_label_decl (identifier);
17839 /* If the next token is a `;', stop. */
17840 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17842 /* Look for the `,' separating the label declarations. */
17843 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17846 /* Look for the final `;'. */
17847 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17850 /* Support Functions */
17852 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17853 NAME should have one of the representations used for an
17854 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17855 is returned. If PARSER->SCOPE is a dependent type, then a
17856 SCOPE_REF is returned.
17858 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17859 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17860 was formed. Abstractly, such entities should not be passed to this
17861 function, because they do not need to be looked up, but it is
17862 simpler to check for this special case here, rather than at the
17865 In cases not explicitly covered above, this function returns a
17866 DECL, OVERLOAD, or baselink representing the result of the lookup.
17867 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17870 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17871 (e.g., "struct") that was used. In that case bindings that do not
17872 refer to types are ignored.
17874 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17877 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17880 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17883 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17884 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17885 NULL_TREE otherwise. */
17888 cp_parser_lookup_name (cp_parser *parser, tree name,
17889 enum tag_types tag_type,
17892 bool check_dependency,
17893 tree *ambiguous_decls,
17894 location_t name_location)
17898 tree object_type = parser->context->object_type;
17900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17901 flags |= LOOKUP_COMPLAIN;
17903 /* Assume that the lookup will be unambiguous. */
17904 if (ambiguous_decls)
17905 *ambiguous_decls = NULL_TREE;
17907 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17908 no longer valid. Note that if we are parsing tentatively, and
17909 the parse fails, OBJECT_TYPE will be automatically restored. */
17910 parser->context->object_type = NULL_TREE;
17912 if (name == error_mark_node)
17913 return error_mark_node;
17915 /* A template-id has already been resolved; there is no lookup to
17917 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17919 if (BASELINK_P (name))
17921 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17922 == TEMPLATE_ID_EXPR);
17926 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17927 it should already have been checked to make sure that the name
17928 used matches the type being destroyed. */
17929 if (TREE_CODE (name) == BIT_NOT_EXPR)
17933 /* Figure out to which type this destructor applies. */
17935 type = parser->scope;
17936 else if (object_type)
17937 type = object_type;
17939 type = current_class_type;
17940 /* If that's not a class type, there is no destructor. */
17941 if (!type || !CLASS_TYPE_P (type))
17942 return error_mark_node;
17943 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17944 lazily_declare_fn (sfk_destructor, type);
17945 if (!CLASSTYPE_DESTRUCTORS (type))
17946 return error_mark_node;
17947 /* If it was a class type, return the destructor. */
17948 return CLASSTYPE_DESTRUCTORS (type);
17951 /* By this point, the NAME should be an ordinary identifier. If
17952 the id-expression was a qualified name, the qualifying scope is
17953 stored in PARSER->SCOPE at this point. */
17954 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17956 /* Perform the lookup. */
17961 if (parser->scope == error_mark_node)
17962 return error_mark_node;
17964 /* If the SCOPE is dependent, the lookup must be deferred until
17965 the template is instantiated -- unless we are explicitly
17966 looking up names in uninstantiated templates. Even then, we
17967 cannot look up the name if the scope is not a class type; it
17968 might, for example, be a template type parameter. */
17969 dependent_p = (TYPE_P (parser->scope)
17970 && dependent_scope_p (parser->scope));
17971 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17973 /* Defer lookup. */
17974 decl = error_mark_node;
17977 tree pushed_scope = NULL_TREE;
17979 /* If PARSER->SCOPE is a dependent type, then it must be a
17980 class type, and we must not be checking dependencies;
17981 otherwise, we would have processed this lookup above. So
17982 that PARSER->SCOPE is not considered a dependent base by
17983 lookup_member, we must enter the scope here. */
17985 pushed_scope = push_scope (parser->scope);
17987 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17988 lookup result and the nested-name-specifier nominates a class C:
17989 * if the name specified after the nested-name-specifier, when
17990 looked up in C, is the injected-class-name of C (Clause 9), or
17991 * if the name specified after the nested-name-specifier is the
17992 same as the identifier or the simple-template-id's template-
17993 name in the last component of the nested-name-specifier,
17994 the name is instead considered to name the constructor of
17995 class C. [ Note: for example, the constructor is not an
17996 acceptable lookup result in an elaborated-type-specifier so
17997 the constructor would not be used in place of the
17998 injected-class-name. --end note ] Such a constructor name
17999 shall be used only in the declarator-id of a declaration that
18000 names a constructor or in a using-declaration. */
18001 if (tag_type == none_type
18002 && CLASS_TYPE_P (parser->scope)
18003 && constructor_name_p (name, parser->scope))
18004 name = ctor_identifier;
18006 /* If the PARSER->SCOPE is a template specialization, it
18007 may be instantiated during name lookup. In that case,
18008 errors may be issued. Even if we rollback the current
18009 tentative parse, those errors are valid. */
18010 decl = lookup_qualified_name (parser->scope, name,
18011 tag_type != none_type,
18012 /*complain=*/true);
18014 /* If we have a single function from a using decl, pull it out. */
18015 if (TREE_CODE (decl) == OVERLOAD
18016 && !really_overloaded_fn (decl))
18017 decl = OVL_FUNCTION (decl);
18020 pop_scope (pushed_scope);
18023 /* If the scope is a dependent type and either we deferred lookup or
18024 we did lookup but didn't find the name, rememeber the name. */
18025 if (decl == error_mark_node && TYPE_P (parser->scope)
18026 && dependent_type_p (parser->scope))
18032 /* The resolution to Core Issue 180 says that `struct
18033 A::B' should be considered a type-name, even if `A'
18035 type = make_typename_type (parser->scope, name, tag_type,
18036 /*complain=*/tf_error);
18037 decl = TYPE_NAME (type);
18039 else if (is_template
18040 && (cp_parser_next_token_ends_template_argument_p (parser)
18041 || cp_lexer_next_token_is (parser->lexer,
18043 decl = make_unbound_class_template (parser->scope,
18045 /*complain=*/tf_error);
18047 decl = build_qualified_name (/*type=*/NULL_TREE,
18048 parser->scope, name,
18051 parser->qualifying_scope = parser->scope;
18052 parser->object_scope = NULL_TREE;
18054 else if (object_type)
18056 tree object_decl = NULL_TREE;
18057 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18058 OBJECT_TYPE is not a class. */
18059 if (CLASS_TYPE_P (object_type))
18060 /* If the OBJECT_TYPE is a template specialization, it may
18061 be instantiated during name lookup. In that case, errors
18062 may be issued. Even if we rollback the current tentative
18063 parse, those errors are valid. */
18064 object_decl = lookup_member (object_type,
18067 tag_type != none_type);
18068 /* Look it up in the enclosing context, too. */
18069 decl = lookup_name_real (name, tag_type != none_type,
18071 /*block_p=*/true, is_namespace, flags);
18072 parser->object_scope = object_type;
18073 parser->qualifying_scope = NULL_TREE;
18075 decl = object_decl;
18079 decl = lookup_name_real (name, tag_type != none_type,
18081 /*block_p=*/true, is_namespace, flags);
18082 parser->qualifying_scope = NULL_TREE;
18083 parser->object_scope = NULL_TREE;
18086 /* If the lookup failed, let our caller know. */
18087 if (!decl || decl == error_mark_node)
18088 return error_mark_node;
18090 /* Pull out the template from an injected-class-name (or multiple). */
18092 decl = maybe_get_template_decl_from_type_decl (decl);
18094 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18095 if (TREE_CODE (decl) == TREE_LIST)
18097 if (ambiguous_decls)
18098 *ambiguous_decls = decl;
18099 /* The error message we have to print is too complicated for
18100 cp_parser_error, so we incorporate its actions directly. */
18101 if (!cp_parser_simulate_error (parser))
18103 error_at (name_location, "reference to %qD is ambiguous",
18105 print_candidates (decl);
18107 return error_mark_node;
18110 gcc_assert (DECL_P (decl)
18111 || TREE_CODE (decl) == OVERLOAD
18112 || TREE_CODE (decl) == SCOPE_REF
18113 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18114 || BASELINK_P (decl));
18116 /* If we have resolved the name of a member declaration, check to
18117 see if the declaration is accessible. When the name resolves to
18118 set of overloaded functions, accessibility is checked when
18119 overload resolution is done.
18121 During an explicit instantiation, access is not checked at all,
18122 as per [temp.explicit]. */
18124 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18129 /* Like cp_parser_lookup_name, but for use in the typical case where
18130 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18131 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18134 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18136 return cp_parser_lookup_name (parser, name,
18138 /*is_template=*/false,
18139 /*is_namespace=*/false,
18140 /*check_dependency=*/true,
18141 /*ambiguous_decls=*/NULL,
18145 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18146 the current context, return the TYPE_DECL. If TAG_NAME_P is
18147 true, the DECL indicates the class being defined in a class-head,
18148 or declared in an elaborated-type-specifier.
18150 Otherwise, return DECL. */
18153 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18155 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18156 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18159 template <typename T> struct B;
18162 template <typename T> struct A::B {};
18164 Similarly, in an elaborated-type-specifier:
18166 namespace N { struct X{}; }
18169 template <typename T> friend struct N::X;
18172 However, if the DECL refers to a class type, and we are in
18173 the scope of the class, then the name lookup automatically
18174 finds the TYPE_DECL created by build_self_reference rather
18175 than a TEMPLATE_DECL. For example, in:
18177 template <class T> struct S {
18181 there is no need to handle such case. */
18183 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18184 return DECL_TEMPLATE_RESULT (decl);
18189 /* If too many, or too few, template-parameter lists apply to the
18190 declarator, issue an error message. Returns TRUE if all went well,
18191 and FALSE otherwise. */
18194 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18195 cp_declarator *declarator,
18196 location_t declarator_location)
18198 unsigned num_templates;
18200 /* We haven't seen any classes that involve template parameters yet. */
18203 switch (declarator->kind)
18206 if (declarator->u.id.qualifying_scope)
18210 scope = declarator->u.id.qualifying_scope;
18212 while (scope && CLASS_TYPE_P (scope))
18214 /* You're supposed to have one `template <...>'
18215 for every template class, but you don't need one
18216 for a full specialization. For example:
18218 template <class T> struct S{};
18219 template <> struct S<int> { void f(); };
18220 void S<int>::f () {}
18222 is correct; there shouldn't be a `template <>' for
18223 the definition of `S<int>::f'. */
18224 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18225 /* If SCOPE does not have template information of any
18226 kind, then it is not a template, nor is it nested
18227 within a template. */
18229 if (explicit_class_specialization_p (scope))
18231 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18234 scope = TYPE_CONTEXT (scope);
18237 else if (TREE_CODE (declarator->u.id.unqualified_name)
18238 == TEMPLATE_ID_EXPR)
18239 /* If the DECLARATOR has the form `X<y>' then it uses one
18240 additional level of template parameters. */
18243 return cp_parser_check_template_parameters
18244 (parser, num_templates, declarator_location, declarator);
18250 case cdk_reference:
18252 return (cp_parser_check_declarator_template_parameters
18253 (parser, declarator->declarator, declarator_location));
18259 gcc_unreachable ();
18264 /* NUM_TEMPLATES were used in the current declaration. If that is
18265 invalid, return FALSE and issue an error messages. Otherwise,
18266 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18267 declarator and we can print more accurate diagnostics. */
18270 cp_parser_check_template_parameters (cp_parser* parser,
18271 unsigned num_templates,
18272 location_t location,
18273 cp_declarator *declarator)
18275 /* If there are the same number of template classes and parameter
18276 lists, that's OK. */
18277 if (parser->num_template_parameter_lists == num_templates)
18279 /* If there are more, but only one more, then we are referring to a
18280 member template. That's OK too. */
18281 if (parser->num_template_parameter_lists == num_templates + 1)
18283 /* If there are more template classes than parameter lists, we have
18286 template <class T> void S<T>::R<T>::f (); */
18287 if (parser->num_template_parameter_lists < num_templates)
18289 if (declarator && !current_function_decl)
18290 error_at (location, "specializing member %<%T::%E%> "
18291 "requires %<template<>%> syntax",
18292 declarator->u.id.qualifying_scope,
18293 declarator->u.id.unqualified_name);
18294 else if (declarator)
18295 error_at (location, "invalid declaration of %<%T::%E%>",
18296 declarator->u.id.qualifying_scope,
18297 declarator->u.id.unqualified_name);
18299 error_at (location, "too few template-parameter-lists");
18302 /* Otherwise, there are too many template parameter lists. We have
18305 template <class T> template <class U> void S::f(); */
18306 error_at (location, "too many template-parameter-lists");
18310 /* Parse an optional `::' token indicating that the following name is
18311 from the global namespace. If so, PARSER->SCOPE is set to the
18312 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18313 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18314 Returns the new value of PARSER->SCOPE, if the `::' token is
18315 present, and NULL_TREE otherwise. */
18318 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18322 /* Peek at the next token. */
18323 token = cp_lexer_peek_token (parser->lexer);
18324 /* If we're looking at a `::' token then we're starting from the
18325 global namespace, not our current location. */
18326 if (token->type == CPP_SCOPE)
18328 /* Consume the `::' token. */
18329 cp_lexer_consume_token (parser->lexer);
18330 /* Set the SCOPE so that we know where to start the lookup. */
18331 parser->scope = global_namespace;
18332 parser->qualifying_scope = global_namespace;
18333 parser->object_scope = NULL_TREE;
18335 return parser->scope;
18337 else if (!current_scope_valid_p)
18339 parser->scope = NULL_TREE;
18340 parser->qualifying_scope = NULL_TREE;
18341 parser->object_scope = NULL_TREE;
18347 /* Returns TRUE if the upcoming token sequence is the start of a
18348 constructor declarator. If FRIEND_P is true, the declarator is
18349 preceded by the `friend' specifier. */
18352 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18354 bool constructor_p;
18355 tree nested_name_specifier;
18356 cp_token *next_token;
18358 /* The common case is that this is not a constructor declarator, so
18359 try to avoid doing lots of work if at all possible. It's not
18360 valid declare a constructor at function scope. */
18361 if (parser->in_function_body)
18363 /* And only certain tokens can begin a constructor declarator. */
18364 next_token = cp_lexer_peek_token (parser->lexer);
18365 if (next_token->type != CPP_NAME
18366 && next_token->type != CPP_SCOPE
18367 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18368 && next_token->type != CPP_TEMPLATE_ID)
18371 /* Parse tentatively; we are going to roll back all of the tokens
18373 cp_parser_parse_tentatively (parser);
18374 /* Assume that we are looking at a constructor declarator. */
18375 constructor_p = true;
18377 /* Look for the optional `::' operator. */
18378 cp_parser_global_scope_opt (parser,
18379 /*current_scope_valid_p=*/false);
18380 /* Look for the nested-name-specifier. */
18381 nested_name_specifier
18382 = (cp_parser_nested_name_specifier_opt (parser,
18383 /*typename_keyword_p=*/false,
18384 /*check_dependency_p=*/false,
18386 /*is_declaration=*/false));
18387 /* Outside of a class-specifier, there must be a
18388 nested-name-specifier. */
18389 if (!nested_name_specifier &&
18390 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18392 constructor_p = false;
18393 else if (nested_name_specifier == error_mark_node)
18394 constructor_p = false;
18396 /* If we have a class scope, this is easy; DR 147 says that S::S always
18397 names the constructor, and no other qualified name could. */
18398 if (constructor_p && nested_name_specifier
18399 && TYPE_P (nested_name_specifier))
18401 tree id = cp_parser_unqualified_id (parser,
18402 /*template_keyword_p=*/false,
18403 /*check_dependency_p=*/false,
18404 /*declarator_p=*/true,
18405 /*optional_p=*/false);
18406 if (is_overloaded_fn (id))
18407 id = DECL_NAME (get_first_fn (id));
18408 if (!constructor_name_p (id, nested_name_specifier))
18409 constructor_p = false;
18411 /* If we still think that this might be a constructor-declarator,
18412 look for a class-name. */
18413 else if (constructor_p)
18417 template <typename T> struct S {
18421 we must recognize that the nested `S' names a class. */
18423 type_decl = cp_parser_class_name (parser,
18424 /*typename_keyword_p=*/false,
18425 /*template_keyword_p=*/false,
18427 /*check_dependency_p=*/false,
18428 /*class_head_p=*/false,
18429 /*is_declaration=*/false);
18430 /* If there was no class-name, then this is not a constructor. */
18431 constructor_p = !cp_parser_error_occurred (parser);
18433 /* If we're still considering a constructor, we have to see a `(',
18434 to begin the parameter-declaration-clause, followed by either a
18435 `)', an `...', or a decl-specifier. We need to check for a
18436 type-specifier to avoid being fooled into thinking that:
18440 is a constructor. (It is actually a function named `f' that
18441 takes one parameter (of type `int') and returns a value of type
18444 && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18445 constructor_p = false;
18448 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18449 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18450 /* A parameter declaration begins with a decl-specifier,
18451 which is either the "attribute" keyword, a storage class
18452 specifier, or (usually) a type-specifier. */
18453 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18456 tree pushed_scope = NULL_TREE;
18457 unsigned saved_num_template_parameter_lists;
18459 /* Names appearing in the type-specifier should be looked up
18460 in the scope of the class. */
18461 if (current_class_type)
18465 type = TREE_TYPE (type_decl);
18466 if (TREE_CODE (type) == TYPENAME_TYPE)
18468 type = resolve_typename_type (type,
18469 /*only_current_p=*/false);
18470 if (TREE_CODE (type) == TYPENAME_TYPE)
18472 cp_parser_abort_tentative_parse (parser);
18476 pushed_scope = push_scope (type);
18479 /* Inside the constructor parameter list, surrounding
18480 template-parameter-lists do not apply. */
18481 saved_num_template_parameter_lists
18482 = parser->num_template_parameter_lists;
18483 parser->num_template_parameter_lists = 0;
18485 /* Look for the type-specifier. */
18486 cp_parser_type_specifier (parser,
18487 CP_PARSER_FLAGS_NONE,
18488 /*decl_specs=*/NULL,
18489 /*is_declarator=*/true,
18490 /*declares_class_or_enum=*/NULL,
18491 /*is_cv_qualifier=*/NULL);
18493 parser->num_template_parameter_lists
18494 = saved_num_template_parameter_lists;
18496 /* Leave the scope of the class. */
18498 pop_scope (pushed_scope);
18500 constructor_p = !cp_parser_error_occurred (parser);
18504 /* We did not really want to consume any tokens. */
18505 cp_parser_abort_tentative_parse (parser);
18507 return constructor_p;
18510 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18511 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18512 they must be performed once we are in the scope of the function.
18514 Returns the function defined. */
18517 cp_parser_function_definition_from_specifiers_and_declarator
18518 (cp_parser* parser,
18519 cp_decl_specifier_seq *decl_specifiers,
18521 const cp_declarator *declarator)
18526 /* Begin the function-definition. */
18527 success_p = start_function (decl_specifiers, declarator, attributes);
18529 /* The things we're about to see are not directly qualified by any
18530 template headers we've seen thus far. */
18531 reset_specialization ();
18533 /* If there were names looked up in the decl-specifier-seq that we
18534 did not check, check them now. We must wait until we are in the
18535 scope of the function to perform the checks, since the function
18536 might be a friend. */
18537 perform_deferred_access_checks ();
18541 /* Skip the entire function. */
18542 cp_parser_skip_to_end_of_block_or_statement (parser);
18543 fn = error_mark_node;
18545 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18547 /* Seen already, skip it. An error message has already been output. */
18548 cp_parser_skip_to_end_of_block_or_statement (parser);
18549 fn = current_function_decl;
18550 current_function_decl = NULL_TREE;
18551 /* If this is a function from a class, pop the nested class. */
18552 if (current_class_name)
18553 pop_nested_class ();
18556 fn = cp_parser_function_definition_after_declarator (parser,
18557 /*inline_p=*/false);
18562 /* Parse the part of a function-definition that follows the
18563 declarator. INLINE_P is TRUE iff this function is an inline
18564 function defined within a class-specifier.
18566 Returns the function defined. */
18569 cp_parser_function_definition_after_declarator (cp_parser* parser,
18573 bool ctor_initializer_p = false;
18574 bool saved_in_unbraced_linkage_specification_p;
18575 bool saved_in_function_body;
18576 unsigned saved_num_template_parameter_lists;
18579 saved_in_function_body = parser->in_function_body;
18580 parser->in_function_body = true;
18581 /* If the next token is `return', then the code may be trying to
18582 make use of the "named return value" extension that G++ used to
18584 token = cp_lexer_peek_token (parser->lexer);
18585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18587 /* Consume the `return' keyword. */
18588 cp_lexer_consume_token (parser->lexer);
18589 /* Look for the identifier that indicates what value is to be
18591 cp_parser_identifier (parser);
18592 /* Issue an error message. */
18593 error_at (token->location,
18594 "named return values are no longer supported");
18595 /* Skip tokens until we reach the start of the function body. */
18598 cp_token *token = cp_lexer_peek_token (parser->lexer);
18599 if (token->type == CPP_OPEN_BRACE
18600 || token->type == CPP_EOF
18601 || token->type == CPP_PRAGMA_EOL)
18603 cp_lexer_consume_token (parser->lexer);
18606 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18607 anything declared inside `f'. */
18608 saved_in_unbraced_linkage_specification_p
18609 = parser->in_unbraced_linkage_specification_p;
18610 parser->in_unbraced_linkage_specification_p = false;
18611 /* Inside the function, surrounding template-parameter-lists do not
18613 saved_num_template_parameter_lists
18614 = parser->num_template_parameter_lists;
18615 parser->num_template_parameter_lists = 0;
18617 start_lambda_scope (current_function_decl);
18619 /* If the next token is `try', then we are looking at a
18620 function-try-block. */
18621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18622 ctor_initializer_p = cp_parser_function_try_block (parser);
18623 /* A function-try-block includes the function-body, so we only do
18624 this next part if we're not processing a function-try-block. */
18627 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18629 finish_lambda_scope ();
18631 /* Finish the function. */
18632 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18633 (inline_p ? 2 : 0));
18634 /* Generate code for it, if necessary. */
18635 expand_or_defer_fn (fn);
18636 /* Restore the saved values. */
18637 parser->in_unbraced_linkage_specification_p
18638 = saved_in_unbraced_linkage_specification_p;
18639 parser->num_template_parameter_lists
18640 = saved_num_template_parameter_lists;
18641 parser->in_function_body = saved_in_function_body;
18646 /* Parse a template-declaration, assuming that the `export' (and
18647 `extern') keywords, if present, has already been scanned. MEMBER_P
18648 is as for cp_parser_template_declaration. */
18651 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18653 tree decl = NULL_TREE;
18654 VEC (deferred_access_check,gc) *checks;
18655 tree parameter_list;
18656 bool friend_p = false;
18657 bool need_lang_pop;
18660 /* Look for the `template' keyword. */
18661 token = cp_lexer_peek_token (parser->lexer);
18662 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18666 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18668 if (at_class_scope_p () && current_function_decl)
18670 /* 14.5.2.2 [temp.mem]
18672 A local class shall not have member templates. */
18673 error_at (token->location,
18674 "invalid declaration of member template in local class");
18675 cp_parser_skip_to_end_of_block_or_statement (parser);
18680 A template ... shall not have C linkage. */
18681 if (current_lang_name == lang_name_c)
18683 error_at (token->location, "template with C linkage");
18684 /* Give it C++ linkage to avoid confusing other parts of the
18686 push_lang_context (lang_name_cplusplus);
18687 need_lang_pop = true;
18690 need_lang_pop = false;
18692 /* We cannot perform access checks on the template parameter
18693 declarations until we know what is being declared, just as we
18694 cannot check the decl-specifier list. */
18695 push_deferring_access_checks (dk_deferred);
18697 /* If the next token is `>', then we have an invalid
18698 specialization. Rather than complain about an invalid template
18699 parameter, issue an error message here. */
18700 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18702 cp_parser_error (parser, "invalid explicit specialization");
18703 begin_specialization ();
18704 parameter_list = NULL_TREE;
18707 /* Parse the template parameters. */
18708 parameter_list = cp_parser_template_parameter_list (parser);
18710 /* Get the deferred access checks from the parameter list. These
18711 will be checked once we know what is being declared, as for a
18712 member template the checks must be performed in the scope of the
18713 class containing the member. */
18714 checks = get_deferred_access_checks ();
18716 /* Look for the `>'. */
18717 cp_parser_skip_to_end_of_template_parameter_list (parser);
18718 /* We just processed one more parameter list. */
18719 ++parser->num_template_parameter_lists;
18720 /* If the next token is `template', there are more template
18722 if (cp_lexer_next_token_is_keyword (parser->lexer,
18724 cp_parser_template_declaration_after_export (parser, member_p);
18727 /* There are no access checks when parsing a template, as we do not
18728 know if a specialization will be a friend. */
18729 push_deferring_access_checks (dk_no_check);
18730 token = cp_lexer_peek_token (parser->lexer);
18731 decl = cp_parser_single_declaration (parser,
18734 /*explicit_specialization_p=*/false,
18736 pop_deferring_access_checks ();
18738 /* If this is a member template declaration, let the front
18740 if (member_p && !friend_p && decl)
18742 if (TREE_CODE (decl) == TYPE_DECL)
18743 cp_parser_check_access_in_redeclaration (decl, token->location);
18745 decl = finish_member_template_decl (decl);
18747 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18748 make_friend_class (current_class_type, TREE_TYPE (decl),
18749 /*complain=*/true);
18751 /* We are done with the current parameter list. */
18752 --parser->num_template_parameter_lists;
18754 pop_deferring_access_checks ();
18757 finish_template_decl (parameter_list);
18759 /* Register member declarations. */
18760 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18761 finish_member_declaration (decl);
18762 /* For the erroneous case of a template with C linkage, we pushed an
18763 implicit C++ linkage scope; exit that scope now. */
18765 pop_lang_context ();
18766 /* If DECL is a function template, we must return to parse it later.
18767 (Even though there is no definition, there might be default
18768 arguments that need handling.) */
18769 if (member_p && decl
18770 && (TREE_CODE (decl) == FUNCTION_DECL
18771 || DECL_FUNCTION_TEMPLATE_P (decl)))
18772 TREE_VALUE (parser->unparsed_functions_queues)
18773 = tree_cons (NULL_TREE, decl,
18774 TREE_VALUE (parser->unparsed_functions_queues));
18777 /* Perform the deferred access checks from a template-parameter-list.
18778 CHECKS is a TREE_LIST of access checks, as returned by
18779 get_deferred_access_checks. */
18782 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18784 ++processing_template_parmlist;
18785 perform_access_checks (checks);
18786 --processing_template_parmlist;
18789 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18790 `function-definition' sequence. MEMBER_P is true, this declaration
18791 appears in a class scope.
18793 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18794 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18797 cp_parser_single_declaration (cp_parser* parser,
18798 VEC (deferred_access_check,gc)* checks,
18800 bool explicit_specialization_p,
18803 int declares_class_or_enum;
18804 tree decl = NULL_TREE;
18805 cp_decl_specifier_seq decl_specifiers;
18806 bool function_definition_p = false;
18807 cp_token *decl_spec_token_start;
18809 /* This function is only used when processing a template
18811 gcc_assert (innermost_scope_kind () == sk_template_parms
18812 || innermost_scope_kind () == sk_template_spec);
18814 /* Defer access checks until we know what is being declared. */
18815 push_deferring_access_checks (dk_deferred);
18817 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18819 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18820 cp_parser_decl_specifier_seq (parser,
18821 CP_PARSER_FLAGS_OPTIONAL,
18823 &declares_class_or_enum);
18825 *friend_p = cp_parser_friend_p (&decl_specifiers);
18827 /* There are no template typedefs. */
18828 if (decl_specifiers.specs[(int) ds_typedef])
18830 error_at (decl_spec_token_start->location,
18831 "template declaration of %<typedef%>");
18832 decl = error_mark_node;
18835 /* Gather up the access checks that occurred the
18836 decl-specifier-seq. */
18837 stop_deferring_access_checks ();
18839 /* Check for the declaration of a template class. */
18840 if (declares_class_or_enum)
18842 if (cp_parser_declares_only_class_p (parser))
18844 decl = shadow_tag (&decl_specifiers);
18849 friend template <typename T> struct A<T>::B;
18852 A<T>::B will be represented by a TYPENAME_TYPE, and
18853 therefore not recognized by shadow_tag. */
18854 if (friend_p && *friend_p
18856 && decl_specifiers.type
18857 && TYPE_P (decl_specifiers.type))
18858 decl = decl_specifiers.type;
18860 if (decl && decl != error_mark_node)
18861 decl = TYPE_NAME (decl);
18863 decl = error_mark_node;
18865 /* Perform access checks for template parameters. */
18866 cp_parser_perform_template_parameter_access_checks (checks);
18870 /* Complain about missing 'typename' or other invalid type names. */
18871 if (!decl_specifiers.any_type_specifiers_p)
18872 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18874 /* If it's not a template class, try for a template function. If
18875 the next token is a `;', then this declaration does not declare
18876 anything. But, if there were errors in the decl-specifiers, then
18877 the error might well have come from an attempted class-specifier.
18878 In that case, there's no need to warn about a missing declarator. */
18880 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18881 || decl_specifiers.type != error_mark_node))
18883 decl = cp_parser_init_declarator (parser,
18886 /*function_definition_allowed_p=*/true,
18888 declares_class_or_enum,
18889 &function_definition_p);
18891 /* 7.1.1-1 [dcl.stc]
18893 A storage-class-specifier shall not be specified in an explicit
18894 specialization... */
18896 && explicit_specialization_p
18897 && decl_specifiers.storage_class != sc_none)
18899 error_at (decl_spec_token_start->location,
18900 "explicit template specialization cannot have a storage class");
18901 decl = error_mark_node;
18905 pop_deferring_access_checks ();
18907 /* Clear any current qualification; whatever comes next is the start
18908 of something new. */
18909 parser->scope = NULL_TREE;
18910 parser->qualifying_scope = NULL_TREE;
18911 parser->object_scope = NULL_TREE;
18912 /* Look for a trailing `;' after the declaration. */
18913 if (!function_definition_p
18914 && (decl == error_mark_node
18915 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18916 cp_parser_skip_to_end_of_block_or_statement (parser);
18921 /* Parse a cast-expression that is not the operand of a unary "&". */
18924 cp_parser_simple_cast_expression (cp_parser *parser)
18926 return cp_parser_cast_expression (parser, /*address_p=*/false,
18927 /*cast_p=*/false, NULL);
18930 /* Parse a functional cast to TYPE. Returns an expression
18931 representing the cast. */
18934 cp_parser_functional_cast (cp_parser* parser, tree type)
18937 tree expression_list;
18941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18943 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18944 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18945 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18946 if (TREE_CODE (type) == TYPE_DECL)
18947 type = TREE_TYPE (type);
18948 return finish_compound_literal (type, expression_list);
18952 vec = cp_parser_parenthesized_expression_list (parser, false,
18954 /*allow_expansion_p=*/true,
18955 /*non_constant_p=*/NULL);
18957 expression_list = error_mark_node;
18960 expression_list = build_tree_list_vec (vec);
18961 release_tree_vector (vec);
18964 cast = build_functional_cast (type, expression_list,
18965 tf_warning_or_error);
18966 /* [expr.const]/1: In an integral constant expression "only type
18967 conversions to integral or enumeration type can be used". */
18968 if (TREE_CODE (type) == TYPE_DECL)
18969 type = TREE_TYPE (type);
18970 if (cast != error_mark_node
18971 && !cast_valid_in_integral_constant_expression_p (type)
18972 && (cp_parser_non_integral_constant_expression
18973 (parser, "a call to a constructor")))
18974 return error_mark_node;
18978 /* Save the tokens that make up the body of a member function defined
18979 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18980 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18981 specifiers applied to the declaration. Returns the FUNCTION_DECL
18982 for the member function. */
18985 cp_parser_save_member_function_body (cp_parser* parser,
18986 cp_decl_specifier_seq *decl_specifiers,
18987 cp_declarator *declarator,
18994 /* Create the FUNCTION_DECL. */
18995 fn = grokmethod (decl_specifiers, declarator, attributes);
18996 /* If something went badly wrong, bail out now. */
18997 if (fn == error_mark_node)
18999 /* If there's a function-body, skip it. */
19000 if (cp_parser_token_starts_function_definition_p
19001 (cp_lexer_peek_token (parser->lexer)))
19002 cp_parser_skip_to_end_of_block_or_statement (parser);
19003 return error_mark_node;
19006 /* Remember it, if there default args to post process. */
19007 cp_parser_save_default_args (parser, fn);
19009 /* Save away the tokens that make up the body of the
19011 first = parser->lexer->next_token;
19012 /* We can have braced-init-list mem-initializers before the fn body. */
19013 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19015 cp_lexer_consume_token (parser->lexer);
19016 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19017 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19019 /* cache_group will stop after an un-nested { } pair, too. */
19020 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19023 /* variadic mem-inits have ... after the ')'. */
19024 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19025 cp_lexer_consume_token (parser->lexer);
19028 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19029 /* Handle function try blocks. */
19030 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19031 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19032 last = parser->lexer->next_token;
19034 /* Save away the inline definition; we will process it when the
19035 class is complete. */
19036 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19037 DECL_PENDING_INLINE_P (fn) = 1;
19039 /* We need to know that this was defined in the class, so that
19040 friend templates are handled correctly. */
19041 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19043 /* Add FN to the queue of functions to be parsed later. */
19044 TREE_VALUE (parser->unparsed_functions_queues)
19045 = tree_cons (NULL_TREE, fn,
19046 TREE_VALUE (parser->unparsed_functions_queues));
19051 /* Parse a template-argument-list, as well as the trailing ">" (but
19052 not the opening ">"). See cp_parser_template_argument_list for the
19056 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19060 tree saved_qualifying_scope;
19061 tree saved_object_scope;
19062 bool saved_greater_than_is_operator_p;
19063 int saved_unevaluated_operand;
19064 int saved_inhibit_evaluation_warnings;
19068 When parsing a template-id, the first non-nested `>' is taken as
19069 the end of the template-argument-list rather than a greater-than
19071 saved_greater_than_is_operator_p
19072 = parser->greater_than_is_operator_p;
19073 parser->greater_than_is_operator_p = false;
19074 /* Parsing the argument list may modify SCOPE, so we save it
19076 saved_scope = parser->scope;
19077 saved_qualifying_scope = parser->qualifying_scope;
19078 saved_object_scope = parser->object_scope;
19079 /* We need to evaluate the template arguments, even though this
19080 template-id may be nested within a "sizeof". */
19081 saved_unevaluated_operand = cp_unevaluated_operand;
19082 cp_unevaluated_operand = 0;
19083 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19084 c_inhibit_evaluation_warnings = 0;
19085 /* Parse the template-argument-list itself. */
19086 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19087 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19088 arguments = NULL_TREE;
19090 arguments = cp_parser_template_argument_list (parser);
19091 /* Look for the `>' that ends the template-argument-list. If we find
19092 a '>>' instead, it's probably just a typo. */
19093 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19095 if (cxx_dialect != cxx98)
19097 /* In C++0x, a `>>' in a template argument list or cast
19098 expression is considered to be two separate `>'
19099 tokens. So, change the current token to a `>', but don't
19100 consume it: it will be consumed later when the outer
19101 template argument list (or cast expression) is parsed.
19102 Note that this replacement of `>' for `>>' is necessary
19103 even if we are parsing tentatively: in the tentative
19104 case, after calling
19105 cp_parser_enclosed_template_argument_list we will always
19106 throw away all of the template arguments and the first
19107 closing `>', either because the template argument list
19108 was erroneous or because we are replacing those tokens
19109 with a CPP_TEMPLATE_ID token. The second `>' (which will
19110 not have been thrown away) is needed either to close an
19111 outer template argument list or to complete a new-style
19113 cp_token *token = cp_lexer_peek_token (parser->lexer);
19114 token->type = CPP_GREATER;
19116 else if (!saved_greater_than_is_operator_p)
19118 /* If we're in a nested template argument list, the '>>' has
19119 to be a typo for '> >'. We emit the error message, but we
19120 continue parsing and we push a '>' as next token, so that
19121 the argument list will be parsed correctly. Note that the
19122 global source location is still on the token before the
19123 '>>', so we need to say explicitly where we want it. */
19124 cp_token *token = cp_lexer_peek_token (parser->lexer);
19125 error_at (token->location, "%<>>%> should be %<> >%> "
19126 "within a nested template argument list");
19128 token->type = CPP_GREATER;
19132 /* If this is not a nested template argument list, the '>>'
19133 is a typo for '>'. Emit an error message and continue.
19134 Same deal about the token location, but here we can get it
19135 right by consuming the '>>' before issuing the diagnostic. */
19136 cp_token *token = cp_lexer_consume_token (parser->lexer);
19137 error_at (token->location,
19138 "spurious %<>>%>, use %<>%> to terminate "
19139 "a template argument list");
19143 cp_parser_skip_to_end_of_template_parameter_list (parser);
19144 /* The `>' token might be a greater-than operator again now. */
19145 parser->greater_than_is_operator_p
19146 = saved_greater_than_is_operator_p;
19147 /* Restore the SAVED_SCOPE. */
19148 parser->scope = saved_scope;
19149 parser->qualifying_scope = saved_qualifying_scope;
19150 parser->object_scope = saved_object_scope;
19151 cp_unevaluated_operand = saved_unevaluated_operand;
19152 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19157 /* MEMBER_FUNCTION is a member function, or a friend. If default
19158 arguments, or the body of the function have not yet been parsed,
19162 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19164 /* If this member is a template, get the underlying
19166 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19167 member_function = DECL_TEMPLATE_RESULT (member_function);
19169 /* There should not be any class definitions in progress at this
19170 point; the bodies of members are only parsed outside of all class
19172 gcc_assert (parser->num_classes_being_defined == 0);
19173 /* While we're parsing the member functions we might encounter more
19174 classes. We want to handle them right away, but we don't want
19175 them getting mixed up with functions that are currently in the
19177 parser->unparsed_functions_queues
19178 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19180 /* Make sure that any template parameters are in scope. */
19181 maybe_begin_member_template_processing (member_function);
19183 /* If the body of the function has not yet been parsed, parse it
19185 if (DECL_PENDING_INLINE_P (member_function))
19187 tree function_scope;
19188 cp_token_cache *tokens;
19190 /* The function is no longer pending; we are processing it. */
19191 tokens = DECL_PENDING_INLINE_INFO (member_function);
19192 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19193 DECL_PENDING_INLINE_P (member_function) = 0;
19195 /* If this is a local class, enter the scope of the containing
19197 function_scope = current_function_decl;
19198 if (function_scope)
19199 push_function_context ();
19201 /* Push the body of the function onto the lexer stack. */
19202 cp_parser_push_lexer_for_tokens (parser, tokens);
19204 /* Let the front end know that we going to be defining this
19206 start_preparsed_function (member_function, NULL_TREE,
19207 SF_PRE_PARSED | SF_INCLASS_INLINE);
19209 /* Don't do access checking if it is a templated function. */
19210 if (processing_template_decl)
19211 push_deferring_access_checks (dk_no_check);
19213 /* Now, parse the body of the function. */
19214 cp_parser_function_definition_after_declarator (parser,
19215 /*inline_p=*/true);
19217 if (processing_template_decl)
19218 pop_deferring_access_checks ();
19220 /* Leave the scope of the containing function. */
19221 if (function_scope)
19222 pop_function_context ();
19223 cp_parser_pop_lexer (parser);
19226 /* Remove any template parameters from the symbol table. */
19227 maybe_end_member_template_processing ();
19229 /* Restore the queue. */
19230 parser->unparsed_functions_queues
19231 = TREE_CHAIN (parser->unparsed_functions_queues);
19234 /* If DECL contains any default args, remember it on the unparsed
19235 functions queue. */
19238 cp_parser_save_default_args (cp_parser* parser, tree decl)
19242 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19244 probe = TREE_CHAIN (probe))
19245 if (TREE_PURPOSE (probe))
19247 TREE_PURPOSE (parser->unparsed_functions_queues)
19248 = tree_cons (current_class_type, decl,
19249 TREE_PURPOSE (parser->unparsed_functions_queues));
19254 /* FN is a FUNCTION_DECL which may contains a parameter with an
19255 unparsed DEFAULT_ARG. Parse the default args now. This function
19256 assumes that the current scope is the scope in which the default
19257 argument should be processed. */
19260 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19262 bool saved_local_variables_forbidden_p;
19263 tree parm, parmdecl;
19265 /* While we're parsing the default args, we might (due to the
19266 statement expression extension) encounter more classes. We want
19267 to handle them right away, but we don't want them getting mixed
19268 up with default args that are currently in the queue. */
19269 parser->unparsed_functions_queues
19270 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19272 /* Local variable names (and the `this' keyword) may not appear
19273 in a default argument. */
19274 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19275 parser->local_variables_forbidden_p = true;
19277 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19278 parmdecl = DECL_ARGUMENTS (fn);
19279 parm && parm != void_list_node;
19280 parm = TREE_CHAIN (parm),
19281 parmdecl = TREE_CHAIN (parmdecl))
19283 cp_token_cache *tokens;
19284 tree default_arg = TREE_PURPOSE (parm);
19286 VEC(tree,gc) *insts;
19293 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19294 /* This can happen for a friend declaration for a function
19295 already declared with default arguments. */
19298 /* Push the saved tokens for the default argument onto the parser's
19300 tokens = DEFARG_TOKENS (default_arg);
19301 cp_parser_push_lexer_for_tokens (parser, tokens);
19303 start_lambda_scope (parmdecl);
19305 /* Parse the assignment-expression. */
19306 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19307 if (parsed_arg == error_mark_node)
19309 cp_parser_pop_lexer (parser);
19313 if (!processing_template_decl)
19314 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19316 TREE_PURPOSE (parm) = parsed_arg;
19318 /* Update any instantiations we've already created. */
19319 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19320 VEC_iterate (tree, insts, ix, copy); ix++)
19321 TREE_PURPOSE (copy) = parsed_arg;
19323 finish_lambda_scope ();
19325 /* If the token stream has not been completely used up, then
19326 there was extra junk after the end of the default
19328 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19329 cp_parser_error (parser, "expected %<,%>");
19331 /* Revert to the main lexer. */
19332 cp_parser_pop_lexer (parser);
19335 /* Make sure no default arg is missing. */
19336 check_default_args (fn);
19338 /* Restore the state of local_variables_forbidden_p. */
19339 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19341 /* Restore the queue. */
19342 parser->unparsed_functions_queues
19343 = TREE_CHAIN (parser->unparsed_functions_queues);
19346 /* Parse the operand of `sizeof' (or a similar operator). Returns
19347 either a TYPE or an expression, depending on the form of the
19348 input. The KEYWORD indicates which kind of expression we have
19352 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19354 tree expr = NULL_TREE;
19355 const char *saved_message;
19357 bool saved_integral_constant_expression_p;
19358 bool saved_non_integral_constant_expression_p;
19359 bool pack_expansion_p = false;
19361 /* Types cannot be defined in a `sizeof' expression. Save away the
19363 saved_message = parser->type_definition_forbidden_message;
19364 /* And create the new one. */
19365 tmp = concat ("types may not be defined in %<",
19366 IDENTIFIER_POINTER (ridpointers[keyword]),
19367 "%> expressions", NULL);
19368 parser->type_definition_forbidden_message = tmp;
19370 /* The restrictions on constant-expressions do not apply inside
19371 sizeof expressions. */
19372 saved_integral_constant_expression_p
19373 = parser->integral_constant_expression_p;
19374 saved_non_integral_constant_expression_p
19375 = parser->non_integral_constant_expression_p;
19376 parser->integral_constant_expression_p = false;
19378 /* If it's a `...', then we are computing the length of a parameter
19380 if (keyword == RID_SIZEOF
19381 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19383 /* Consume the `...'. */
19384 cp_lexer_consume_token (parser->lexer);
19385 maybe_warn_variadic_templates ();
19387 /* Note that this is an expansion. */
19388 pack_expansion_p = true;
19391 /* Do not actually evaluate the expression. */
19392 ++cp_unevaluated_operand;
19393 ++c_inhibit_evaluation_warnings;
19394 /* If it's a `(', then we might be looking at the type-id
19396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19399 bool saved_in_type_id_in_expr_p;
19401 /* We can't be sure yet whether we're looking at a type-id or an
19403 cp_parser_parse_tentatively (parser);
19404 /* Consume the `('. */
19405 cp_lexer_consume_token (parser->lexer);
19406 /* Parse the type-id. */
19407 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19408 parser->in_type_id_in_expr_p = true;
19409 type = cp_parser_type_id (parser);
19410 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19411 /* Now, look for the trailing `)'. */
19412 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19413 /* If all went well, then we're done. */
19414 if (cp_parser_parse_definitely (parser))
19416 cp_decl_specifier_seq decl_specs;
19418 /* Build a trivial decl-specifier-seq. */
19419 clear_decl_specs (&decl_specs);
19420 decl_specs.type = type;
19422 /* Call grokdeclarator to figure out what type this is. */
19423 expr = grokdeclarator (NULL,
19427 /*attrlist=*/NULL);
19431 /* If the type-id production did not work out, then we must be
19432 looking at the unary-expression production. */
19434 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19435 /*cast_p=*/false, NULL);
19437 if (pack_expansion_p)
19438 /* Build a pack expansion. */
19439 expr = make_pack_expansion (expr);
19441 /* Go back to evaluating expressions. */
19442 --cp_unevaluated_operand;
19443 --c_inhibit_evaluation_warnings;
19445 /* Free the message we created. */
19447 /* And restore the old one. */
19448 parser->type_definition_forbidden_message = saved_message;
19449 parser->integral_constant_expression_p
19450 = saved_integral_constant_expression_p;
19451 parser->non_integral_constant_expression_p
19452 = saved_non_integral_constant_expression_p;
19457 /* If the current declaration has no declarator, return true. */
19460 cp_parser_declares_only_class_p (cp_parser *parser)
19462 /* If the next token is a `;' or a `,' then there is no
19464 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19465 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19468 /* Update the DECL_SPECS to reflect the storage class indicated by
19472 cp_parser_set_storage_class (cp_parser *parser,
19473 cp_decl_specifier_seq *decl_specs,
19475 location_t location)
19477 cp_storage_class storage_class;
19479 if (parser->in_unbraced_linkage_specification_p)
19481 error_at (location, "invalid use of %qD in linkage specification",
19482 ridpointers[keyword]);
19485 else if (decl_specs->storage_class != sc_none)
19487 decl_specs->conflicting_specifiers_p = true;
19491 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19492 && decl_specs->specs[(int) ds_thread])
19494 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19495 decl_specs->specs[(int) ds_thread] = 0;
19501 storage_class = sc_auto;
19504 storage_class = sc_register;
19507 storage_class = sc_static;
19510 storage_class = sc_extern;
19513 storage_class = sc_mutable;
19516 gcc_unreachable ();
19518 decl_specs->storage_class = storage_class;
19520 /* A storage class specifier cannot be applied alongside a typedef
19521 specifier. If there is a typedef specifier present then set
19522 conflicting_specifiers_p which will trigger an error later
19523 on in grokdeclarator. */
19524 if (decl_specs->specs[(int)ds_typedef])
19525 decl_specs->conflicting_specifiers_p = true;
19528 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19529 is true, the type is a user-defined type; otherwise it is a
19530 built-in type specified by a keyword. */
19533 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19535 location_t location,
19536 bool user_defined_p)
19538 decl_specs->any_specifiers_p = true;
19540 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19541 (with, for example, in "typedef int wchar_t;") we remember that
19542 this is what happened. In system headers, we ignore these
19543 declarations so that G++ can work with system headers that are not
19545 if (decl_specs->specs[(int) ds_typedef]
19547 && (type_spec == boolean_type_node
19548 || type_spec == char16_type_node
19549 || type_spec == char32_type_node
19550 || type_spec == wchar_type_node)
19551 && (decl_specs->type
19552 || decl_specs->specs[(int) ds_long]
19553 || decl_specs->specs[(int) ds_short]
19554 || decl_specs->specs[(int) ds_unsigned]
19555 || decl_specs->specs[(int) ds_signed]))
19557 decl_specs->redefined_builtin_type = type_spec;
19558 if (!decl_specs->type)
19560 decl_specs->type = type_spec;
19561 decl_specs->user_defined_type_p = false;
19562 decl_specs->type_location = location;
19565 else if (decl_specs->type)
19566 decl_specs->multiple_types_p = true;
19569 decl_specs->type = type_spec;
19570 decl_specs->user_defined_type_p = user_defined_p;
19571 decl_specs->redefined_builtin_type = NULL_TREE;
19572 decl_specs->type_location = location;
19576 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19577 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19580 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19582 return decl_specifiers->specs[(int) ds_friend] != 0;
19585 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19586 issue an error message indicating that TOKEN_DESC was expected.
19588 Returns the token consumed, if the token had the appropriate type.
19589 Otherwise, returns NULL. */
19592 cp_parser_require (cp_parser* parser,
19593 enum cpp_ttype type,
19594 const char* token_desc)
19596 if (cp_lexer_next_token_is (parser->lexer, type))
19597 return cp_lexer_consume_token (parser->lexer);
19600 /* Output the MESSAGE -- unless we're parsing tentatively. */
19601 if (!cp_parser_simulate_error (parser))
19603 char *message = concat ("expected ", token_desc, NULL);
19604 cp_parser_error (parser, message);
19611 /* An error message is produced if the next token is not '>'.
19612 All further tokens are skipped until the desired token is
19613 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19616 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19618 /* Current level of '< ... >'. */
19619 unsigned level = 0;
19620 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19621 unsigned nesting_depth = 0;
19623 /* Are we ready, yet? If not, issue error message. */
19624 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19627 /* Skip tokens until the desired token is found. */
19630 /* Peek at the next token. */
19631 switch (cp_lexer_peek_token (parser->lexer)->type)
19634 if (!nesting_depth)
19639 if (cxx_dialect == cxx98)
19640 /* C++0x views the `>>' operator as two `>' tokens, but
19643 else if (!nesting_depth && level-- == 0)
19645 /* We've hit a `>>' where the first `>' closes the
19646 template argument list, and the second `>' is
19647 spurious. Just consume the `>>' and stop; we've
19648 already produced at least one error. */
19649 cp_lexer_consume_token (parser->lexer);
19652 /* Fall through for C++0x, so we handle the second `>' in
19656 if (!nesting_depth && level-- == 0)
19658 /* We've reached the token we want, consume it and stop. */
19659 cp_lexer_consume_token (parser->lexer);
19664 case CPP_OPEN_PAREN:
19665 case CPP_OPEN_SQUARE:
19669 case CPP_CLOSE_PAREN:
19670 case CPP_CLOSE_SQUARE:
19671 if (nesting_depth-- == 0)
19676 case CPP_PRAGMA_EOL:
19677 case CPP_SEMICOLON:
19678 case CPP_OPEN_BRACE:
19679 case CPP_CLOSE_BRACE:
19680 /* The '>' was probably forgotten, don't look further. */
19687 /* Consume this token. */
19688 cp_lexer_consume_token (parser->lexer);
19692 /* If the next token is the indicated keyword, consume it. Otherwise,
19693 issue an error message indicating that TOKEN_DESC was expected.
19695 Returns the token consumed, if the token had the appropriate type.
19696 Otherwise, returns NULL. */
19699 cp_parser_require_keyword (cp_parser* parser,
19701 const char* token_desc)
19703 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19705 if (token && token->keyword != keyword)
19707 dyn_string_t error_msg;
19709 /* Format the error message. */
19710 error_msg = dyn_string_new (0);
19711 dyn_string_append_cstr (error_msg, "expected ");
19712 dyn_string_append_cstr (error_msg, token_desc);
19713 cp_parser_error (parser, error_msg->s);
19714 dyn_string_delete (error_msg);
19721 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19722 function-definition. */
19725 cp_parser_token_starts_function_definition_p (cp_token* token)
19727 return (/* An ordinary function-body begins with an `{'. */
19728 token->type == CPP_OPEN_BRACE
19729 /* A ctor-initializer begins with a `:'. */
19730 || token->type == CPP_COLON
19731 /* A function-try-block begins with `try'. */
19732 || token->keyword == RID_TRY
19733 /* The named return value extension begins with `return'. */
19734 || token->keyword == RID_RETURN);
19737 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19741 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19745 token = cp_lexer_peek_token (parser->lexer);
19746 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19749 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19750 C++0x) ending a template-argument. */
19753 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19757 token = cp_lexer_peek_token (parser->lexer);
19758 return (token->type == CPP_COMMA
19759 || token->type == CPP_GREATER
19760 || token->type == CPP_ELLIPSIS
19761 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19764 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19765 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19768 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19773 token = cp_lexer_peek_nth_token (parser->lexer, n);
19774 if (token->type == CPP_LESS)
19776 /* Check for the sequence `<::' in the original code. It would be lexed as
19777 `[:', where `[' is a digraph, and there is no whitespace before
19779 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19782 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19783 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19789 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19790 or none_type otherwise. */
19792 static enum tag_types
19793 cp_parser_token_is_class_key (cp_token* token)
19795 switch (token->keyword)
19800 return record_type;
19809 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19812 cp_parser_check_class_key (enum tag_types class_key, tree type)
19814 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19815 permerror (input_location, "%qs tag used in naming %q#T",
19816 class_key == union_type ? "union"
19817 : class_key == record_type ? "struct" : "class",
19821 /* Issue an error message if DECL is redeclared with different
19822 access than its original declaration [class.access.spec/3].
19823 This applies to nested classes and nested class templates.
19827 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19829 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19832 if ((TREE_PRIVATE (decl)
19833 != (current_access_specifier == access_private_node))
19834 || (TREE_PROTECTED (decl)
19835 != (current_access_specifier == access_protected_node)))
19836 error_at (location, "%qD redeclared with different access", decl);
19839 /* Look for the `template' keyword, as a syntactic disambiguator.
19840 Return TRUE iff it is present, in which case it will be
19844 cp_parser_optional_template_keyword (cp_parser *parser)
19846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19848 /* The `template' keyword can only be used within templates;
19849 outside templates the parser can always figure out what is a
19850 template and what is not. */
19851 if (!processing_template_decl)
19853 cp_token *token = cp_lexer_peek_token (parser->lexer);
19854 error_at (token->location,
19855 "%<template%> (as a disambiguator) is only allowed "
19856 "within templates");
19857 /* If this part of the token stream is rescanned, the same
19858 error message would be generated. So, we purge the token
19859 from the stream. */
19860 cp_lexer_purge_token (parser->lexer);
19865 /* Consume the `template' keyword. */
19866 cp_lexer_consume_token (parser->lexer);
19874 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19875 set PARSER->SCOPE, and perform other related actions. */
19878 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19881 struct tree_check *check_value;
19882 deferred_access_check *chk;
19883 VEC (deferred_access_check,gc) *checks;
19885 /* Get the stored value. */
19886 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19887 /* Perform any access checks that were deferred. */
19888 checks = check_value->checks;
19892 VEC_iterate (deferred_access_check, checks, i, chk) ;
19895 perform_or_defer_access_check (chk->binfo,
19900 /* Set the scope from the stored value. */
19901 parser->scope = check_value->value;
19902 parser->qualifying_scope = check_value->qualifying_scope;
19903 parser->object_scope = NULL_TREE;
19906 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19907 encounter the end of a block before what we were looking for. */
19910 cp_parser_cache_group (cp_parser *parser,
19911 enum cpp_ttype end,
19916 cp_token *token = cp_lexer_peek_token (parser->lexer);
19918 /* Abort a parenthesized expression if we encounter a semicolon. */
19919 if ((end == CPP_CLOSE_PAREN || depth == 0)
19920 && token->type == CPP_SEMICOLON)
19922 /* If we've reached the end of the file, stop. */
19923 if (token->type == CPP_EOF
19924 || (end != CPP_PRAGMA_EOL
19925 && token->type == CPP_PRAGMA_EOL))
19927 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19928 /* We've hit the end of an enclosing block, so there's been some
19929 kind of syntax error. */
19932 /* Consume the token. */
19933 cp_lexer_consume_token (parser->lexer);
19934 /* See if it starts a new group. */
19935 if (token->type == CPP_OPEN_BRACE)
19937 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19938 /* In theory this should probably check end == '}', but
19939 cp_parser_save_member_function_body needs it to exit
19940 after either '}' or ')' when called with ')'. */
19944 else if (token->type == CPP_OPEN_PAREN)
19946 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19947 if (depth == 0 && end == CPP_CLOSE_PAREN)
19950 else if (token->type == CPP_PRAGMA)
19951 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19952 else if (token->type == end)
19957 /* Begin parsing tentatively. We always save tokens while parsing
19958 tentatively so that if the tentative parsing fails we can restore the
19962 cp_parser_parse_tentatively (cp_parser* parser)
19964 /* Enter a new parsing context. */
19965 parser->context = cp_parser_context_new (parser->context);
19966 /* Begin saving tokens. */
19967 cp_lexer_save_tokens (parser->lexer);
19968 /* In order to avoid repetitive access control error messages,
19969 access checks are queued up until we are no longer parsing
19971 push_deferring_access_checks (dk_deferred);
19974 /* Commit to the currently active tentative parse. */
19977 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19979 cp_parser_context *context;
19982 /* Mark all of the levels as committed. */
19983 lexer = parser->lexer;
19984 for (context = parser->context; context->next; context = context->next)
19986 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19988 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19989 while (!cp_lexer_saving_tokens (lexer))
19990 lexer = lexer->next;
19991 cp_lexer_commit_tokens (lexer);
19995 /* Abort the currently active tentative parse. All consumed tokens
19996 will be rolled back, and no diagnostics will be issued. */
19999 cp_parser_abort_tentative_parse (cp_parser* parser)
20001 cp_parser_simulate_error (parser);
20002 /* Now, pretend that we want to see if the construct was
20003 successfully parsed. */
20004 cp_parser_parse_definitely (parser);
20007 /* Stop parsing tentatively. If a parse error has occurred, restore the
20008 token stream. Otherwise, commit to the tokens we have consumed.
20009 Returns true if no error occurred; false otherwise. */
20012 cp_parser_parse_definitely (cp_parser* parser)
20014 bool error_occurred;
20015 cp_parser_context *context;
20017 /* Remember whether or not an error occurred, since we are about to
20018 destroy that information. */
20019 error_occurred = cp_parser_error_occurred (parser);
20020 /* Remove the topmost context from the stack. */
20021 context = parser->context;
20022 parser->context = context->next;
20023 /* If no parse errors occurred, commit to the tentative parse. */
20024 if (!error_occurred)
20026 /* Commit to the tokens read tentatively, unless that was
20028 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20029 cp_lexer_commit_tokens (parser->lexer);
20031 pop_to_parent_deferring_access_checks ();
20033 /* Otherwise, if errors occurred, roll back our state so that things
20034 are just as they were before we began the tentative parse. */
20037 cp_lexer_rollback_tokens (parser->lexer);
20038 pop_deferring_access_checks ();
20040 /* Add the context to the front of the free list. */
20041 context->next = cp_parser_context_free_list;
20042 cp_parser_context_free_list = context;
20044 return !error_occurred;
20047 /* Returns true if we are parsing tentatively and are not committed to
20048 this tentative parse. */
20051 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20053 return (cp_parser_parsing_tentatively (parser)
20054 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20057 /* Returns nonzero iff an error has occurred during the most recent
20058 tentative parse. */
20061 cp_parser_error_occurred (cp_parser* parser)
20063 return (cp_parser_parsing_tentatively (parser)
20064 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20067 /* Returns nonzero if GNU extensions are allowed. */
20070 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20072 return parser->allow_gnu_extensions_p;
20075 /* Objective-C++ Productions */
20078 /* Parse an Objective-C expression, which feeds into a primary-expression
20082 objc-message-expression
20083 objc-string-literal
20084 objc-encode-expression
20085 objc-protocol-expression
20086 objc-selector-expression
20088 Returns a tree representation of the expression. */
20091 cp_parser_objc_expression (cp_parser* parser)
20093 /* Try to figure out what kind of declaration is present. */
20094 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20098 case CPP_OPEN_SQUARE:
20099 return cp_parser_objc_message_expression (parser);
20101 case CPP_OBJC_STRING:
20102 kwd = cp_lexer_consume_token (parser->lexer);
20103 return objc_build_string_object (kwd->u.value);
20106 switch (kwd->keyword)
20108 case RID_AT_ENCODE:
20109 return cp_parser_objc_encode_expression (parser);
20111 case RID_AT_PROTOCOL:
20112 return cp_parser_objc_protocol_expression (parser);
20114 case RID_AT_SELECTOR:
20115 return cp_parser_objc_selector_expression (parser);
20121 error_at (kwd->location,
20122 "misplaced %<@%D%> Objective-C++ construct",
20124 cp_parser_skip_to_end_of_block_or_statement (parser);
20127 return error_mark_node;
20130 /* Parse an Objective-C message expression.
20132 objc-message-expression:
20133 [ objc-message-receiver objc-message-args ]
20135 Returns a representation of an Objective-C message. */
20138 cp_parser_objc_message_expression (cp_parser* parser)
20140 tree receiver, messageargs;
20142 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20143 receiver = cp_parser_objc_message_receiver (parser);
20144 messageargs = cp_parser_objc_message_args (parser);
20145 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20147 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20150 /* Parse an objc-message-receiver.
20152 objc-message-receiver:
20154 simple-type-specifier
20156 Returns a representation of the type or expression. */
20159 cp_parser_objc_message_receiver (cp_parser* parser)
20163 /* An Objective-C message receiver may be either (1) a type
20164 or (2) an expression. */
20165 cp_parser_parse_tentatively (parser);
20166 rcv = cp_parser_expression (parser, false, NULL);
20168 if (cp_parser_parse_definitely (parser))
20171 rcv = cp_parser_simple_type_specifier (parser,
20172 /*decl_specs=*/NULL,
20173 CP_PARSER_FLAGS_NONE);
20175 return objc_get_class_reference (rcv);
20178 /* Parse the arguments and selectors comprising an Objective-C message.
20183 objc-selector-args , objc-comma-args
20185 objc-selector-args:
20186 objc-selector [opt] : assignment-expression
20187 objc-selector-args objc-selector [opt] : assignment-expression
20190 assignment-expression
20191 objc-comma-args , assignment-expression
20193 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20194 selector arguments and TREE_VALUE containing a list of comma
20198 cp_parser_objc_message_args (cp_parser* parser)
20200 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20201 bool maybe_unary_selector_p = true;
20202 cp_token *token = cp_lexer_peek_token (parser->lexer);
20204 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20206 tree selector = NULL_TREE, arg;
20208 if (token->type != CPP_COLON)
20209 selector = cp_parser_objc_selector (parser);
20211 /* Detect if we have a unary selector. */
20212 if (maybe_unary_selector_p
20213 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20214 return build_tree_list (selector, NULL_TREE);
20216 maybe_unary_selector_p = false;
20217 cp_parser_require (parser, CPP_COLON, "%<:%>");
20218 arg = cp_parser_assignment_expression (parser, false, NULL);
20221 = chainon (sel_args,
20222 build_tree_list (selector, arg));
20224 token = cp_lexer_peek_token (parser->lexer);
20227 /* Handle non-selector arguments, if any. */
20228 while (token->type == CPP_COMMA)
20232 cp_lexer_consume_token (parser->lexer);
20233 arg = cp_parser_assignment_expression (parser, false, NULL);
20236 = chainon (addl_args,
20237 build_tree_list (NULL_TREE, arg));
20239 token = cp_lexer_peek_token (parser->lexer);
20242 return build_tree_list (sel_args, addl_args);
20245 /* Parse an Objective-C encode expression.
20247 objc-encode-expression:
20248 @encode objc-typename
20250 Returns an encoded representation of the type argument. */
20253 cp_parser_objc_encode_expression (cp_parser* parser)
20258 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20259 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20260 token = cp_lexer_peek_token (parser->lexer);
20261 type = complete_type (cp_parser_type_id (parser));
20262 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20266 error_at (token->location,
20267 "%<@encode%> must specify a type as an argument");
20268 return error_mark_node;
20271 return objc_build_encode_expr (type);
20274 /* Parse an Objective-C @defs expression. */
20277 cp_parser_objc_defs_expression (cp_parser *parser)
20281 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20282 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20283 name = cp_parser_identifier (parser);
20284 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20286 return objc_get_class_ivars (name);
20289 /* Parse an Objective-C protocol expression.
20291 objc-protocol-expression:
20292 @protocol ( identifier )
20294 Returns a representation of the protocol expression. */
20297 cp_parser_objc_protocol_expression (cp_parser* parser)
20301 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20302 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20303 proto = cp_parser_identifier (parser);
20304 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20306 return objc_build_protocol_expr (proto);
20309 /* Parse an Objective-C selector expression.
20311 objc-selector-expression:
20312 @selector ( objc-method-signature )
20314 objc-method-signature:
20320 objc-selector-seq objc-selector :
20322 Returns a representation of the method selector. */
20325 cp_parser_objc_selector_expression (cp_parser* parser)
20327 tree sel_seq = NULL_TREE;
20328 bool maybe_unary_selector_p = true;
20330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20332 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20333 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20334 token = cp_lexer_peek_token (parser->lexer);
20336 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20337 || token->type == CPP_SCOPE)
20339 tree selector = NULL_TREE;
20341 if (token->type != CPP_COLON
20342 || token->type == CPP_SCOPE)
20343 selector = cp_parser_objc_selector (parser);
20345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20346 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20348 /* Detect if we have a unary selector. */
20349 if (maybe_unary_selector_p)
20351 sel_seq = selector;
20352 goto finish_selector;
20356 cp_parser_error (parser, "expected %<:%>");
20359 maybe_unary_selector_p = false;
20360 token = cp_lexer_consume_token (parser->lexer);
20362 if (token->type == CPP_SCOPE)
20365 = chainon (sel_seq,
20366 build_tree_list (selector, NULL_TREE));
20368 = chainon (sel_seq,
20369 build_tree_list (NULL_TREE, NULL_TREE));
20373 = chainon (sel_seq,
20374 build_tree_list (selector, NULL_TREE));
20376 token = cp_lexer_peek_token (parser->lexer);
20380 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20382 return objc_build_selector_expr (loc, sel_seq);
20385 /* Parse a list of identifiers.
20387 objc-identifier-list:
20389 objc-identifier-list , identifier
20391 Returns a TREE_LIST of identifier nodes. */
20394 cp_parser_objc_identifier_list (cp_parser* parser)
20396 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20397 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20399 while (sep->type == CPP_COMMA)
20401 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20402 list = chainon (list,
20403 build_tree_list (NULL_TREE,
20404 cp_parser_identifier (parser)));
20405 sep = cp_lexer_peek_token (parser->lexer);
20411 /* Parse an Objective-C alias declaration.
20413 objc-alias-declaration:
20414 @compatibility_alias identifier identifier ;
20416 This function registers the alias mapping with the Objective-C front end.
20417 It returns nothing. */
20420 cp_parser_objc_alias_declaration (cp_parser* parser)
20424 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20425 alias = cp_parser_identifier (parser);
20426 orig = cp_parser_identifier (parser);
20427 objc_declare_alias (alias, orig);
20428 cp_parser_consume_semicolon_at_end_of_statement (parser);
20431 /* Parse an Objective-C class forward-declaration.
20433 objc-class-declaration:
20434 @class objc-identifier-list ;
20436 The function registers the forward declarations with the Objective-C
20437 front end. It returns nothing. */
20440 cp_parser_objc_class_declaration (cp_parser* parser)
20442 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20443 objc_declare_class (cp_parser_objc_identifier_list (parser));
20444 cp_parser_consume_semicolon_at_end_of_statement (parser);
20447 /* Parse a list of Objective-C protocol references.
20449 objc-protocol-refs-opt:
20450 objc-protocol-refs [opt]
20452 objc-protocol-refs:
20453 < objc-identifier-list >
20455 Returns a TREE_LIST of identifiers, if any. */
20458 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20460 tree protorefs = NULL_TREE;
20462 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20464 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20465 protorefs = cp_parser_objc_identifier_list (parser);
20466 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20472 /* Parse a Objective-C visibility specification. */
20475 cp_parser_objc_visibility_spec (cp_parser* parser)
20477 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20479 switch (vis->keyword)
20481 case RID_AT_PRIVATE:
20482 objc_set_visibility (2);
20484 case RID_AT_PROTECTED:
20485 objc_set_visibility (0);
20487 case RID_AT_PUBLIC:
20488 objc_set_visibility (1);
20494 /* Eat '@private'/'@protected'/'@public'. */
20495 cp_lexer_consume_token (parser->lexer);
20498 /* Parse an Objective-C method type. */
20501 cp_parser_objc_method_type (cp_parser* parser)
20503 objc_set_method_type
20504 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20509 /* Parse an Objective-C protocol qualifier. */
20512 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20514 tree quals = NULL_TREE, node;
20515 cp_token *token = cp_lexer_peek_token (parser->lexer);
20517 node = token->u.value;
20519 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20520 && (node == ridpointers [(int) RID_IN]
20521 || node == ridpointers [(int) RID_OUT]
20522 || node == ridpointers [(int) RID_INOUT]
20523 || node == ridpointers [(int) RID_BYCOPY]
20524 || node == ridpointers [(int) RID_BYREF]
20525 || node == ridpointers [(int) RID_ONEWAY]))
20527 quals = tree_cons (NULL_TREE, node, quals);
20528 cp_lexer_consume_token (parser->lexer);
20529 token = cp_lexer_peek_token (parser->lexer);
20530 node = token->u.value;
20536 /* Parse an Objective-C typename. */
20539 cp_parser_objc_typename (cp_parser* parser)
20541 tree type_name = NULL_TREE;
20543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20545 tree proto_quals, cp_type = NULL_TREE;
20547 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20548 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20550 /* An ObjC type name may consist of just protocol qualifiers, in which
20551 case the type shall default to 'id'. */
20552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20553 cp_type = cp_parser_type_id (parser);
20555 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20556 type_name = build_tree_list (proto_quals, cp_type);
20562 /* Check to see if TYPE refers to an Objective-C selector name. */
20565 cp_parser_objc_selector_p (enum cpp_ttype type)
20567 return (type == CPP_NAME || type == CPP_KEYWORD
20568 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20569 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20570 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20571 || type == CPP_XOR || type == CPP_XOR_EQ);
20574 /* Parse an Objective-C selector. */
20577 cp_parser_objc_selector (cp_parser* parser)
20579 cp_token *token = cp_lexer_consume_token (parser->lexer);
20581 if (!cp_parser_objc_selector_p (token->type))
20583 error_at (token->location, "invalid Objective-C++ selector name");
20584 return error_mark_node;
20587 /* C++ operator names are allowed to appear in ObjC selectors. */
20588 switch (token->type)
20590 case CPP_AND_AND: return get_identifier ("and");
20591 case CPP_AND_EQ: return get_identifier ("and_eq");
20592 case CPP_AND: return get_identifier ("bitand");
20593 case CPP_OR: return get_identifier ("bitor");
20594 case CPP_COMPL: return get_identifier ("compl");
20595 case CPP_NOT: return get_identifier ("not");
20596 case CPP_NOT_EQ: return get_identifier ("not_eq");
20597 case CPP_OR_OR: return get_identifier ("or");
20598 case CPP_OR_EQ: return get_identifier ("or_eq");
20599 case CPP_XOR: return get_identifier ("xor");
20600 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20601 default: return token->u.value;
20605 /* Parse an Objective-C params list. */
20608 cp_parser_objc_method_keyword_params (cp_parser* parser)
20610 tree params = NULL_TREE;
20611 bool maybe_unary_selector_p = true;
20612 cp_token *token = cp_lexer_peek_token (parser->lexer);
20614 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20616 tree selector = NULL_TREE, type_name, identifier;
20618 if (token->type != CPP_COLON)
20619 selector = cp_parser_objc_selector (parser);
20621 /* Detect if we have a unary selector. */
20622 if (maybe_unary_selector_p
20623 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20626 maybe_unary_selector_p = false;
20627 cp_parser_require (parser, CPP_COLON, "%<:%>");
20628 type_name = cp_parser_objc_typename (parser);
20629 identifier = cp_parser_identifier (parser);
20633 objc_build_keyword_decl (selector,
20637 token = cp_lexer_peek_token (parser->lexer);
20643 /* Parse the non-keyword Objective-C params. */
20646 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20648 tree params = make_node (TREE_LIST);
20649 cp_token *token = cp_lexer_peek_token (parser->lexer);
20650 *ellipsisp = false; /* Initially, assume no ellipsis. */
20652 while (token->type == CPP_COMMA)
20654 cp_parameter_declarator *parmdecl;
20657 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20658 token = cp_lexer_peek_token (parser->lexer);
20660 if (token->type == CPP_ELLIPSIS)
20662 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20667 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20668 parm = grokdeclarator (parmdecl->declarator,
20669 &parmdecl->decl_specifiers,
20670 PARM, /*initialized=*/0,
20671 /*attrlist=*/NULL);
20673 chainon (params, build_tree_list (NULL_TREE, parm));
20674 token = cp_lexer_peek_token (parser->lexer);
20680 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20683 cp_parser_objc_interstitial_code (cp_parser* parser)
20685 cp_token *token = cp_lexer_peek_token (parser->lexer);
20687 /* If the next token is `extern' and the following token is a string
20688 literal, then we have a linkage specification. */
20689 if (token->keyword == RID_EXTERN
20690 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20691 cp_parser_linkage_specification (parser);
20692 /* Handle #pragma, if any. */
20693 else if (token->type == CPP_PRAGMA)
20694 cp_parser_pragma (parser, pragma_external);
20695 /* Allow stray semicolons. */
20696 else if (token->type == CPP_SEMICOLON)
20697 cp_lexer_consume_token (parser->lexer);
20698 /* Finally, try to parse a block-declaration, or a function-definition. */
20700 cp_parser_block_declaration (parser, /*statement_p=*/false);
20703 /* Parse a method signature. */
20706 cp_parser_objc_method_signature (cp_parser* parser)
20708 tree rettype, kwdparms, optparms;
20709 bool ellipsis = false;
20711 cp_parser_objc_method_type (parser);
20712 rettype = cp_parser_objc_typename (parser);
20713 kwdparms = cp_parser_objc_method_keyword_params (parser);
20714 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20716 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20719 /* Pars an Objective-C method prototype list. */
20722 cp_parser_objc_method_prototype_list (cp_parser* parser)
20724 cp_token *token = cp_lexer_peek_token (parser->lexer);
20726 while (token->keyword != RID_AT_END)
20728 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20730 objc_add_method_declaration
20731 (cp_parser_objc_method_signature (parser));
20732 cp_parser_consume_semicolon_at_end_of_statement (parser);
20735 /* Allow for interspersed non-ObjC++ code. */
20736 cp_parser_objc_interstitial_code (parser);
20738 token = cp_lexer_peek_token (parser->lexer);
20741 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20742 objc_finish_interface ();
20745 /* Parse an Objective-C method definition list. */
20748 cp_parser_objc_method_definition_list (cp_parser* parser)
20750 cp_token *token = cp_lexer_peek_token (parser->lexer);
20752 while (token->keyword != RID_AT_END)
20756 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20758 push_deferring_access_checks (dk_deferred);
20759 objc_start_method_definition
20760 (cp_parser_objc_method_signature (parser));
20762 /* For historical reasons, we accept an optional semicolon. */
20763 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20764 cp_lexer_consume_token (parser->lexer);
20766 perform_deferred_access_checks ();
20767 stop_deferring_access_checks ();
20768 meth = cp_parser_function_definition_after_declarator (parser,
20770 pop_deferring_access_checks ();
20771 objc_finish_method_definition (meth);
20774 /* Allow for interspersed non-ObjC++ code. */
20775 cp_parser_objc_interstitial_code (parser);
20777 token = cp_lexer_peek_token (parser->lexer);
20780 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20781 objc_finish_implementation ();
20784 /* Parse Objective-C ivars. */
20787 cp_parser_objc_class_ivars (cp_parser* parser)
20789 cp_token *token = cp_lexer_peek_token (parser->lexer);
20791 if (token->type != CPP_OPEN_BRACE)
20792 return; /* No ivars specified. */
20794 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20795 token = cp_lexer_peek_token (parser->lexer);
20797 while (token->type != CPP_CLOSE_BRACE)
20799 cp_decl_specifier_seq declspecs;
20800 int decl_class_or_enum_p;
20801 tree prefix_attributes;
20803 cp_parser_objc_visibility_spec (parser);
20805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20808 cp_parser_decl_specifier_seq (parser,
20809 CP_PARSER_FLAGS_OPTIONAL,
20811 &decl_class_or_enum_p);
20812 prefix_attributes = declspecs.attributes;
20813 declspecs.attributes = NULL_TREE;
20815 /* Keep going until we hit the `;' at the end of the
20817 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20819 tree width = NULL_TREE, attributes, first_attribute, decl;
20820 cp_declarator *declarator = NULL;
20821 int ctor_dtor_or_conv_p;
20823 /* Check for a (possibly unnamed) bitfield declaration. */
20824 token = cp_lexer_peek_token (parser->lexer);
20825 if (token->type == CPP_COLON)
20828 if (token->type == CPP_NAME
20829 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20832 /* Get the name of the bitfield. */
20833 declarator = make_id_declarator (NULL_TREE,
20834 cp_parser_identifier (parser),
20838 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20839 /* Get the width of the bitfield. */
20841 = cp_parser_constant_expression (parser,
20842 /*allow_non_constant=*/false,
20847 /* Parse the declarator. */
20849 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20850 &ctor_dtor_or_conv_p,
20851 /*parenthesized_p=*/NULL,
20852 /*member_p=*/false);
20855 /* Look for attributes that apply to the ivar. */
20856 attributes = cp_parser_attributes_opt (parser);
20857 /* Remember which attributes are prefix attributes and
20859 first_attribute = attributes;
20860 /* Combine the attributes. */
20861 attributes = chainon (prefix_attributes, attributes);
20864 /* Create the bitfield declaration. */
20865 decl = grokbitfield (declarator, &declspecs,
20869 decl = grokfield (declarator, &declspecs,
20870 NULL_TREE, /*init_const_expr_p=*/false,
20871 NULL_TREE, attributes);
20873 /* Add the instance variable. */
20874 objc_add_instance_variable (decl);
20876 /* Reset PREFIX_ATTRIBUTES. */
20877 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20878 attributes = TREE_CHAIN (attributes);
20880 TREE_CHAIN (attributes) = NULL_TREE;
20882 token = cp_lexer_peek_token (parser->lexer);
20884 if (token->type == CPP_COMMA)
20886 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20892 cp_parser_consume_semicolon_at_end_of_statement (parser);
20893 token = cp_lexer_peek_token (parser->lexer);
20896 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20897 /* For historical reasons, we accept an optional semicolon. */
20898 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20899 cp_lexer_consume_token (parser->lexer);
20902 /* Parse an Objective-C protocol declaration. */
20905 cp_parser_objc_protocol_declaration (cp_parser* parser)
20907 tree proto, protorefs;
20910 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20913 tok = cp_lexer_peek_token (parser->lexer);
20914 error_at (tok->location, "identifier expected after %<@protocol%>");
20918 /* See if we have a forward declaration or a definition. */
20919 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20921 /* Try a forward declaration first. */
20922 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20924 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20926 cp_parser_consume_semicolon_at_end_of_statement (parser);
20929 /* Ok, we got a full-fledged definition (or at least should). */
20932 proto = cp_parser_identifier (parser);
20933 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20934 objc_start_protocol (proto, protorefs);
20935 cp_parser_objc_method_prototype_list (parser);
20939 /* Parse an Objective-C superclass or category. */
20942 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20945 cp_token *next = cp_lexer_peek_token (parser->lexer);
20947 *super = *categ = NULL_TREE;
20948 if (next->type == CPP_COLON)
20950 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20951 *super = cp_parser_identifier (parser);
20953 else if (next->type == CPP_OPEN_PAREN)
20955 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20956 *categ = cp_parser_identifier (parser);
20957 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20961 /* Parse an Objective-C class interface. */
20964 cp_parser_objc_class_interface (cp_parser* parser)
20966 tree name, super, categ, protos;
20968 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20969 name = cp_parser_identifier (parser);
20970 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20971 protos = cp_parser_objc_protocol_refs_opt (parser);
20973 /* We have either a class or a category on our hands. */
20975 objc_start_category_interface (name, categ, protos);
20978 objc_start_class_interface (name, super, protos);
20979 /* Handle instance variable declarations, if any. */
20980 cp_parser_objc_class_ivars (parser);
20981 objc_continue_interface ();
20984 cp_parser_objc_method_prototype_list (parser);
20987 /* Parse an Objective-C class implementation. */
20990 cp_parser_objc_class_implementation (cp_parser* parser)
20992 tree name, super, categ;
20994 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20995 name = cp_parser_identifier (parser);
20996 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20998 /* We have either a class or a category on our hands. */
21000 objc_start_category_implementation (name, categ);
21003 objc_start_class_implementation (name, super);
21004 /* Handle instance variable declarations, if any. */
21005 cp_parser_objc_class_ivars (parser);
21006 objc_continue_implementation ();
21009 cp_parser_objc_method_definition_list (parser);
21012 /* Consume the @end token and finish off the implementation. */
21015 cp_parser_objc_end_implementation (cp_parser* parser)
21017 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21018 objc_finish_implementation ();
21021 /* Parse an Objective-C declaration. */
21024 cp_parser_objc_declaration (cp_parser* parser)
21026 /* Try to figure out what kind of declaration is present. */
21027 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21029 switch (kwd->keyword)
21032 cp_parser_objc_alias_declaration (parser);
21035 cp_parser_objc_class_declaration (parser);
21037 case RID_AT_PROTOCOL:
21038 cp_parser_objc_protocol_declaration (parser);
21040 case RID_AT_INTERFACE:
21041 cp_parser_objc_class_interface (parser);
21043 case RID_AT_IMPLEMENTATION:
21044 cp_parser_objc_class_implementation (parser);
21047 cp_parser_objc_end_implementation (parser);
21050 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21052 cp_parser_skip_to_end_of_block_or_statement (parser);
21056 /* Parse an Objective-C try-catch-finally statement.
21058 objc-try-catch-finally-stmt:
21059 @try compound-statement objc-catch-clause-seq [opt]
21060 objc-finally-clause [opt]
21062 objc-catch-clause-seq:
21063 objc-catch-clause objc-catch-clause-seq [opt]
21066 @catch ( exception-declaration ) compound-statement
21068 objc-finally-clause
21069 @finally compound-statement
21071 Returns NULL_TREE. */
21074 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21075 location_t location;
21078 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21079 location = cp_lexer_peek_token (parser->lexer)->location;
21080 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21081 node, lest it get absorbed into the surrounding block. */
21082 stmt = push_stmt_list ();
21083 cp_parser_compound_statement (parser, NULL, false);
21084 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21086 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21088 cp_parameter_declarator *parmdecl;
21091 cp_lexer_consume_token (parser->lexer);
21092 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21093 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21094 parm = grokdeclarator (parmdecl->declarator,
21095 &parmdecl->decl_specifiers,
21096 PARM, /*initialized=*/0,
21097 /*attrlist=*/NULL);
21098 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21099 objc_begin_catch_clause (parm);
21100 cp_parser_compound_statement (parser, NULL, false);
21101 objc_finish_catch_clause ();
21104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21106 cp_lexer_consume_token (parser->lexer);
21107 location = cp_lexer_peek_token (parser->lexer)->location;
21108 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21109 node, lest it get absorbed into the surrounding block. */
21110 stmt = push_stmt_list ();
21111 cp_parser_compound_statement (parser, NULL, false);
21112 objc_build_finally_clause (location, pop_stmt_list (stmt));
21115 return objc_finish_try_stmt ();
21118 /* Parse an Objective-C synchronized statement.
21120 objc-synchronized-stmt:
21121 @synchronized ( expression ) compound-statement
21123 Returns NULL_TREE. */
21126 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21127 location_t location;
21130 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21132 location = cp_lexer_peek_token (parser->lexer)->location;
21133 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21134 lock = cp_parser_expression (parser, false, NULL);
21135 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21137 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21138 node, lest it get absorbed into the surrounding block. */
21139 stmt = push_stmt_list ();
21140 cp_parser_compound_statement (parser, NULL, false);
21142 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21145 /* Parse an Objective-C throw statement.
21148 @throw assignment-expression [opt] ;
21150 Returns a constructed '@throw' statement. */
21153 cp_parser_objc_throw_statement (cp_parser *parser) {
21154 tree expr = NULL_TREE;
21155 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21157 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21160 expr = cp_parser_assignment_expression (parser, false, NULL);
21162 cp_parser_consume_semicolon_at_end_of_statement (parser);
21164 return objc_build_throw_stmt (loc, expr);
21167 /* Parse an Objective-C statement. */
21170 cp_parser_objc_statement (cp_parser * parser) {
21171 /* Try to figure out what kind of declaration is present. */
21172 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21174 switch (kwd->keyword)
21177 return cp_parser_objc_try_catch_finally_statement (parser);
21178 case RID_AT_SYNCHRONIZED:
21179 return cp_parser_objc_synchronized_statement (parser);
21181 return cp_parser_objc_throw_statement (parser);
21183 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21185 cp_parser_skip_to_end_of_block_or_statement (parser);
21188 return error_mark_node;
21191 /* OpenMP 2.5 parsing routines. */
21193 /* Returns name of the next clause.
21194 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21195 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21196 returned and the token is consumed. */
21198 static pragma_omp_clause
21199 cp_parser_omp_clause_name (cp_parser *parser)
21201 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21203 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21204 result = PRAGMA_OMP_CLAUSE_IF;
21205 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21206 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21207 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21208 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21209 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21211 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21212 const char *p = IDENTIFIER_POINTER (id);
21217 if (!strcmp ("collapse", p))
21218 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21219 else if (!strcmp ("copyin", p))
21220 result = PRAGMA_OMP_CLAUSE_COPYIN;
21221 else if (!strcmp ("copyprivate", p))
21222 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21225 if (!strcmp ("firstprivate", p))
21226 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21229 if (!strcmp ("lastprivate", p))
21230 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21233 if (!strcmp ("nowait", p))
21234 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21235 else if (!strcmp ("num_threads", p))
21236 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21239 if (!strcmp ("ordered", p))
21240 result = PRAGMA_OMP_CLAUSE_ORDERED;
21243 if (!strcmp ("reduction", p))
21244 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21247 if (!strcmp ("schedule", p))
21248 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21249 else if (!strcmp ("shared", p))
21250 result = PRAGMA_OMP_CLAUSE_SHARED;
21253 if (!strcmp ("untied", p))
21254 result = PRAGMA_OMP_CLAUSE_UNTIED;
21259 if (result != PRAGMA_OMP_CLAUSE_NONE)
21260 cp_lexer_consume_token (parser->lexer);
21265 /* Validate that a clause of the given type does not already exist. */
21268 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21269 const char *name, location_t location)
21273 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21274 if (OMP_CLAUSE_CODE (c) == code)
21276 error_at (location, "too many %qs clauses", name);
21284 variable-list , identifier
21286 In addition, we match a closing parenthesis. An opening parenthesis
21287 will have been consumed by the caller.
21289 If KIND is nonzero, create the appropriate node and install the decl
21290 in OMP_CLAUSE_DECL and add the node to the head of the list.
21292 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21293 return the list created. */
21296 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21304 token = cp_lexer_peek_token (parser->lexer);
21305 name = cp_parser_id_expression (parser, /*template_p=*/false,
21306 /*check_dependency_p=*/true,
21307 /*template_p=*/NULL,
21308 /*declarator_p=*/false,
21309 /*optional_p=*/false);
21310 if (name == error_mark_node)
21313 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21314 if (decl == error_mark_node)
21315 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21316 else if (kind != 0)
21318 tree u = build_omp_clause (token->location, kind);
21319 OMP_CLAUSE_DECL (u) = decl;
21320 OMP_CLAUSE_CHAIN (u) = list;
21324 list = tree_cons (decl, NULL_TREE, list);
21327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21329 cp_lexer_consume_token (parser->lexer);
21332 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21336 /* Try to resync to an unnested comma. Copied from
21337 cp_parser_parenthesized_expression_list. */
21339 ending = cp_parser_skip_to_closing_parenthesis (parser,
21340 /*recovering=*/true,
21342 /*consume_paren=*/true);
21350 /* Similarly, but expect leading and trailing parenthesis. This is a very
21351 common case for omp clauses. */
21354 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21356 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21357 return cp_parser_omp_var_list_no_open (parser, kind, list);
21362 collapse ( constant-expression ) */
21365 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21371 loc = cp_lexer_peek_token (parser->lexer)->location;
21372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21375 num = cp_parser_constant_expression (parser, false, NULL);
21377 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21379 /*or_comma=*/false,
21380 /*consume_paren=*/true);
21382 if (num == error_mark_node)
21384 num = fold_non_dependent_expr (num);
21385 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21386 || !host_integerp (num, 0)
21387 || (n = tree_low_cst (num, 0)) <= 0
21390 error_at (loc, "collapse argument needs positive constant integer expression");
21394 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21395 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21396 OMP_CLAUSE_CHAIN (c) = list;
21397 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21403 default ( shared | none ) */
21406 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21408 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21411 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21415 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21416 const char *p = IDENTIFIER_POINTER (id);
21421 if (strcmp ("none", p) != 0)
21423 kind = OMP_CLAUSE_DEFAULT_NONE;
21427 if (strcmp ("shared", p) != 0)
21429 kind = OMP_CLAUSE_DEFAULT_SHARED;
21436 cp_lexer_consume_token (parser->lexer);
21441 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21445 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21446 /*or_comma=*/false,
21447 /*consume_paren=*/true);
21449 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21452 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21453 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21454 OMP_CLAUSE_CHAIN (c) = list;
21455 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21461 if ( expression ) */
21464 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21471 t = cp_parser_condition (parser);
21473 if (t == error_mark_node
21474 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21476 /*or_comma=*/false,
21477 /*consume_paren=*/true);
21479 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21481 c = build_omp_clause (location, OMP_CLAUSE_IF);
21482 OMP_CLAUSE_IF_EXPR (c) = t;
21483 OMP_CLAUSE_CHAIN (c) = list;
21492 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21493 tree list, location_t location)
21497 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21499 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21500 OMP_CLAUSE_CHAIN (c) = list;
21505 num_threads ( expression ) */
21508 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21509 location_t location)
21513 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21516 t = cp_parser_expression (parser, false, NULL);
21518 if (t == error_mark_node
21519 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21520 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21521 /*or_comma=*/false,
21522 /*consume_paren=*/true);
21524 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21525 "num_threads", location);
21527 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21528 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21529 OMP_CLAUSE_CHAIN (c) = list;
21538 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21539 tree list, location_t location)
21543 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21544 "ordered", location);
21546 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21547 OMP_CLAUSE_CHAIN (c) = list;
21552 reduction ( reduction-operator : variable-list )
21554 reduction-operator:
21555 One of: + * - & ^ | && || */
21558 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21560 enum tree_code code;
21563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21566 switch (cp_lexer_peek_token (parser->lexer)->type)
21578 code = BIT_AND_EXPR;
21581 code = BIT_XOR_EXPR;
21584 code = BIT_IOR_EXPR;
21587 code = TRUTH_ANDIF_EXPR;
21590 code = TRUTH_ORIF_EXPR;
21593 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21594 "%<|%>, %<&&%>, or %<||%>");
21596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21597 /*or_comma=*/false,
21598 /*consume_paren=*/true);
21601 cp_lexer_consume_token (parser->lexer);
21603 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21606 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21607 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21608 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21614 schedule ( schedule-kind )
21615 schedule ( schedule-kind , expression )
21618 static | dynamic | guided | runtime | auto */
21621 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21628 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21630 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21632 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21633 const char *p = IDENTIFIER_POINTER (id);
21638 if (strcmp ("dynamic", p) != 0)
21640 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21644 if (strcmp ("guided", p) != 0)
21646 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21650 if (strcmp ("runtime", p) != 0)
21652 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21659 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21660 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21661 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21662 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21665 cp_lexer_consume_token (parser->lexer);
21667 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21670 cp_lexer_consume_token (parser->lexer);
21672 token = cp_lexer_peek_token (parser->lexer);
21673 t = cp_parser_assignment_expression (parser, false, NULL);
21675 if (t == error_mark_node)
21677 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21678 error_at (token->location, "schedule %<runtime%> does not take "
21679 "a %<chunk_size%> parameter");
21680 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21681 error_at (token->location, "schedule %<auto%> does not take "
21682 "a %<chunk_size%> parameter");
21684 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21686 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21689 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21692 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21693 OMP_CLAUSE_CHAIN (c) = list;
21697 cp_parser_error (parser, "invalid schedule kind");
21699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21700 /*or_comma=*/false,
21701 /*consume_paren=*/true);
21709 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21710 tree list, location_t location)
21714 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21716 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21717 OMP_CLAUSE_CHAIN (c) = list;
21721 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21722 is a bitmask in MASK. Return the list of clauses found; the result
21723 of clause default goes in *pdefault. */
21726 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21727 const char *where, cp_token *pragma_tok)
21729 tree clauses = NULL;
21731 cp_token *token = NULL;
21733 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21735 pragma_omp_clause c_kind;
21736 const char *c_name;
21737 tree prev = clauses;
21739 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21740 cp_lexer_consume_token (parser->lexer);
21742 token = cp_lexer_peek_token (parser->lexer);
21743 c_kind = cp_parser_omp_clause_name (parser);
21748 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21749 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21751 c_name = "collapse";
21753 case PRAGMA_OMP_CLAUSE_COPYIN:
21754 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21757 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21758 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21760 c_name = "copyprivate";
21762 case PRAGMA_OMP_CLAUSE_DEFAULT:
21763 clauses = cp_parser_omp_clause_default (parser, clauses,
21765 c_name = "default";
21767 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21768 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21770 c_name = "firstprivate";
21772 case PRAGMA_OMP_CLAUSE_IF:
21773 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21776 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21779 c_name = "lastprivate";
21781 case PRAGMA_OMP_CLAUSE_NOWAIT:
21782 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21785 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21786 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21788 c_name = "num_threads";
21790 case PRAGMA_OMP_CLAUSE_ORDERED:
21791 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21793 c_name = "ordered";
21795 case PRAGMA_OMP_CLAUSE_PRIVATE:
21796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21798 c_name = "private";
21800 case PRAGMA_OMP_CLAUSE_REDUCTION:
21801 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21802 c_name = "reduction";
21804 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21805 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21807 c_name = "schedule";
21809 case PRAGMA_OMP_CLAUSE_SHARED:
21810 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21814 case PRAGMA_OMP_CLAUSE_UNTIED:
21815 clauses = cp_parser_omp_clause_untied (parser, clauses,
21820 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21824 if (((mask >> c_kind) & 1) == 0)
21826 /* Remove the invalid clause(s) from the list to avoid
21827 confusing the rest of the compiler. */
21829 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21834 return finish_omp_clauses (clauses);
21841 In practice, we're also interested in adding the statement to an
21842 outer node. So it is convenient if we work around the fact that
21843 cp_parser_statement calls add_stmt. */
21846 cp_parser_begin_omp_structured_block (cp_parser *parser)
21848 unsigned save = parser->in_statement;
21850 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21851 This preserves the "not within loop or switch" style error messages
21852 for nonsense cases like
21858 if (parser->in_statement)
21859 parser->in_statement = IN_OMP_BLOCK;
21865 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21867 parser->in_statement = save;
21871 cp_parser_omp_structured_block (cp_parser *parser)
21873 tree stmt = begin_omp_structured_block ();
21874 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21876 cp_parser_statement (parser, NULL_TREE, false, NULL);
21878 cp_parser_end_omp_structured_block (parser, save);
21879 return finish_omp_structured_block (stmt);
21883 # pragma omp atomic new-line
21887 x binop= expr | x++ | ++x | x-- | --x
21889 +, *, -, /, &, ^, |, <<, >>
21891 where x is an lvalue expression with scalar type. */
21894 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21897 enum tree_code code;
21899 cp_parser_require_pragma_eol (parser, pragma_tok);
21901 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21902 /*cast_p=*/false, NULL);
21903 switch (TREE_CODE (lhs))
21908 case PREINCREMENT_EXPR:
21909 case POSTINCREMENT_EXPR:
21910 lhs = TREE_OPERAND (lhs, 0);
21912 rhs = integer_one_node;
21915 case PREDECREMENT_EXPR:
21916 case POSTDECREMENT_EXPR:
21917 lhs = TREE_OPERAND (lhs, 0);
21919 rhs = integer_one_node;
21923 switch (cp_lexer_peek_token (parser->lexer)->type)
21929 code = TRUNC_DIV_EXPR;
21937 case CPP_LSHIFT_EQ:
21938 code = LSHIFT_EXPR;
21940 case CPP_RSHIFT_EQ:
21941 code = RSHIFT_EXPR;
21944 code = BIT_AND_EXPR;
21947 code = BIT_IOR_EXPR;
21950 code = BIT_XOR_EXPR;
21953 cp_parser_error (parser,
21954 "invalid operator for %<#pragma omp atomic%>");
21957 cp_lexer_consume_token (parser->lexer);
21959 rhs = cp_parser_expression (parser, false, NULL);
21960 if (rhs == error_mark_node)
21964 finish_omp_atomic (code, lhs, rhs);
21965 cp_parser_consume_semicolon_at_end_of_statement (parser);
21969 cp_parser_skip_to_end_of_block_or_statement (parser);
21974 # pragma omp barrier new-line */
21977 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21979 cp_parser_require_pragma_eol (parser, pragma_tok);
21980 finish_omp_barrier ();
21984 # pragma omp critical [(name)] new-line
21985 structured-block */
21988 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21990 tree stmt, name = NULL;
21992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21994 cp_lexer_consume_token (parser->lexer);
21996 name = cp_parser_identifier (parser);
21998 if (name == error_mark_node
21999 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22001 /*or_comma=*/false,
22002 /*consume_paren=*/true);
22003 if (name == error_mark_node)
22006 cp_parser_require_pragma_eol (parser, pragma_tok);
22008 stmt = cp_parser_omp_structured_block (parser);
22009 return c_finish_omp_critical (input_location, stmt, name);
22013 # pragma omp flush flush-vars[opt] new-line
22016 ( variable-list ) */
22019 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22022 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22023 cp_parser_require_pragma_eol (parser, pragma_tok);
22025 finish_omp_flush ();
22028 /* Helper function, to parse omp for increment expression. */
22031 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22033 tree cond = cp_parser_binary_expression (parser, false, true,
22034 PREC_NOT_OPERATOR, NULL);
22037 if (cond == error_mark_node
22038 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22040 cp_parser_skip_to_end_of_statement (parser);
22041 return error_mark_node;
22044 switch (TREE_CODE (cond))
22052 return error_mark_node;
22055 /* If decl is an iterator, preserve LHS and RHS of the relational
22056 expr until finish_omp_for. */
22058 && (type_dependent_expression_p (decl)
22059 || CLASS_TYPE_P (TREE_TYPE (decl))))
22062 return build_x_binary_op (TREE_CODE (cond),
22063 TREE_OPERAND (cond, 0), ERROR_MARK,
22064 TREE_OPERAND (cond, 1), ERROR_MARK,
22065 &overloaded_p, tf_warning_or_error);
22068 /* Helper function, to parse omp for increment expression. */
22071 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22073 cp_token *token = cp_lexer_peek_token (parser->lexer);
22079 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22081 op = (token->type == CPP_PLUS_PLUS
22082 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22083 cp_lexer_consume_token (parser->lexer);
22084 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22086 return error_mark_node;
22087 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22090 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22092 return error_mark_node;
22094 token = cp_lexer_peek_token (parser->lexer);
22095 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22097 op = (token->type == CPP_PLUS_PLUS
22098 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22099 cp_lexer_consume_token (parser->lexer);
22100 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22103 op = cp_parser_assignment_operator_opt (parser);
22104 if (op == ERROR_MARK)
22105 return error_mark_node;
22107 if (op != NOP_EXPR)
22109 rhs = cp_parser_assignment_expression (parser, false, NULL);
22110 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22111 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22114 lhs = cp_parser_binary_expression (parser, false, false,
22115 PREC_ADDITIVE_EXPRESSION, NULL);
22116 token = cp_lexer_peek_token (parser->lexer);
22117 decl_first = lhs == decl;
22120 if (token->type != CPP_PLUS
22121 && token->type != CPP_MINUS)
22122 return error_mark_node;
22126 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22127 cp_lexer_consume_token (parser->lexer);
22128 rhs = cp_parser_binary_expression (parser, false, false,
22129 PREC_ADDITIVE_EXPRESSION, NULL);
22130 token = cp_lexer_peek_token (parser->lexer);
22131 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22133 if (lhs == NULL_TREE)
22135 if (op == PLUS_EXPR)
22138 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22141 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22142 NULL, tf_warning_or_error);
22145 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22149 if (rhs != decl || op == MINUS_EXPR)
22150 return error_mark_node;
22151 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22154 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22156 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22159 /* Parse the restricted form of the for statement allowed by OpenMP. */
22162 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22164 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22165 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22166 tree this_pre_body, cl;
22167 location_t loc_first;
22168 bool collapse_err = false;
22169 int i, collapse = 1, nbraces = 0;
22171 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22172 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22173 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22175 gcc_assert (collapse >= 1);
22177 declv = make_tree_vec (collapse);
22178 initv = make_tree_vec (collapse);
22179 condv = make_tree_vec (collapse);
22180 incrv = make_tree_vec (collapse);
22182 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22184 for (i = 0; i < collapse; i++)
22186 int bracecount = 0;
22187 bool add_private_clause = false;
22190 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22192 cp_parser_error (parser, "for statement expected");
22195 loc = cp_lexer_consume_token (parser->lexer)->location;
22197 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22200 init = decl = real_decl = NULL;
22201 this_pre_body = push_stmt_list ();
22202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22204 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22208 integer-type var = lb
22209 random-access-iterator-type var = lb
22210 pointer-type var = lb
22212 cp_decl_specifier_seq type_specifiers;
22214 /* First, try to parse as an initialized declaration. See
22215 cp_parser_condition, from whence the bulk of this is copied. */
22217 cp_parser_parse_tentatively (parser);
22218 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22219 /*is_trailing_return=*/false,
22221 if (cp_parser_parse_definitely (parser))
22223 /* If parsing a type specifier seq succeeded, then this
22224 MUST be a initialized declaration. */
22225 tree asm_specification, attributes;
22226 cp_declarator *declarator;
22228 declarator = cp_parser_declarator (parser,
22229 CP_PARSER_DECLARATOR_NAMED,
22230 /*ctor_dtor_or_conv_p=*/NULL,
22231 /*parenthesized_p=*/NULL,
22232 /*member_p=*/false);
22233 attributes = cp_parser_attributes_opt (parser);
22234 asm_specification = cp_parser_asm_specification_opt (parser);
22236 if (declarator == cp_error_declarator)
22237 cp_parser_skip_to_end_of_statement (parser);
22241 tree pushed_scope, auto_node;
22243 decl = start_decl (declarator, &type_specifiers,
22244 SD_INITIALIZED, attributes,
22245 /*prefix_attributes=*/NULL_TREE,
22248 auto_node = type_uses_auto (TREE_TYPE (decl));
22249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22251 if (cp_lexer_next_token_is (parser->lexer,
22253 error ("parenthesized initialization is not allowed in "
22254 "OpenMP %<for%> loop");
22256 /* Trigger an error. */
22257 cp_parser_require (parser, CPP_EQ, "%<=%>");
22259 init = error_mark_node;
22260 cp_parser_skip_to_end_of_statement (parser);
22262 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22263 || type_dependent_expression_p (decl)
22266 bool is_direct_init, is_non_constant_init;
22268 init = cp_parser_initializer (parser,
22270 &is_non_constant_init);
22272 if (auto_node && describable_type (init))
22275 = do_auto_deduction (TREE_TYPE (decl), init,
22278 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22279 && !type_dependent_expression_p (decl))
22283 cp_finish_decl (decl, init, !is_non_constant_init,
22285 LOOKUP_ONLYCONVERTING);
22286 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22289 = tree_cons (NULL, this_pre_body, for_block);
22293 init = pop_stmt_list (this_pre_body);
22294 this_pre_body = NULL_TREE;
22299 cp_lexer_consume_token (parser->lexer);
22300 init = cp_parser_assignment_expression (parser, false, NULL);
22303 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22304 init = error_mark_node;
22306 cp_finish_decl (decl, NULL_TREE,
22307 /*init_const_expr_p=*/false,
22309 LOOKUP_ONLYCONVERTING);
22313 pop_scope (pushed_scope);
22319 /* If parsing a type specifier sequence failed, then
22320 this MUST be a simple expression. */
22321 cp_parser_parse_tentatively (parser);
22322 decl = cp_parser_primary_expression (parser, false, false,
22324 if (!cp_parser_error_occurred (parser)
22327 && CLASS_TYPE_P (TREE_TYPE (decl)))
22331 cp_parser_parse_definitely (parser);
22332 cp_parser_require (parser, CPP_EQ, "%<=%>");
22333 rhs = cp_parser_assignment_expression (parser, false, NULL);
22334 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22336 tf_warning_or_error));
22337 add_private_clause = true;
22342 cp_parser_abort_tentative_parse (parser);
22343 init = cp_parser_expression (parser, false, NULL);
22346 if (TREE_CODE (init) == MODIFY_EXPR
22347 || TREE_CODE (init) == MODOP_EXPR)
22348 real_decl = TREE_OPERAND (init, 0);
22353 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22356 this_pre_body = pop_stmt_list (this_pre_body);
22360 pre_body = push_stmt_list ();
22362 add_stmt (this_pre_body);
22363 pre_body = pop_stmt_list (pre_body);
22366 pre_body = this_pre_body;
22371 if (par_clauses != NULL && real_decl != NULL_TREE)
22374 for (c = par_clauses; *c ; )
22375 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22376 && OMP_CLAUSE_DECL (*c) == real_decl)
22378 error_at (loc, "iteration variable %qD"
22379 " should not be firstprivate", real_decl);
22380 *c = OMP_CLAUSE_CHAIN (*c);
22382 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22383 && OMP_CLAUSE_DECL (*c) == real_decl)
22385 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22386 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22387 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22388 OMP_CLAUSE_DECL (l) = real_decl;
22389 OMP_CLAUSE_CHAIN (l) = clauses;
22390 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22392 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22393 CP_OMP_CLAUSE_INFO (*c) = NULL;
22394 add_private_clause = false;
22398 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22399 && OMP_CLAUSE_DECL (*c) == real_decl)
22400 add_private_clause = false;
22401 c = &OMP_CLAUSE_CHAIN (*c);
22405 if (add_private_clause)
22408 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22410 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22411 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22412 && OMP_CLAUSE_DECL (c) == decl)
22414 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22415 && OMP_CLAUSE_DECL (c) == decl)
22416 error_at (loc, "iteration variable %qD "
22417 "should not be firstprivate",
22419 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22420 && OMP_CLAUSE_DECL (c) == decl)
22421 error_at (loc, "iteration variable %qD should not be reduction",
22426 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22427 OMP_CLAUSE_DECL (c) = decl;
22428 c = finish_omp_clauses (c);
22431 OMP_CLAUSE_CHAIN (c) = clauses;
22438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22439 cond = cp_parser_omp_for_cond (parser, decl);
22440 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22445 /* If decl is an iterator, preserve the operator on decl
22446 until finish_omp_for. */
22448 && (type_dependent_expression_p (decl)
22449 || CLASS_TYPE_P (TREE_TYPE (decl))))
22450 incr = cp_parser_omp_for_incr (parser, decl);
22452 incr = cp_parser_expression (parser, false, NULL);
22455 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22457 /*or_comma=*/false,
22458 /*consume_paren=*/true);
22460 TREE_VEC_ELT (declv, i) = decl;
22461 TREE_VEC_ELT (initv, i) = init;
22462 TREE_VEC_ELT (condv, i) = cond;
22463 TREE_VEC_ELT (incrv, i) = incr;
22465 if (i == collapse - 1)
22468 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22469 in between the collapsed for loops to be still considered perfectly
22470 nested. Hopefully the final version clarifies this.
22471 For now handle (multiple) {'s and empty statements. */
22472 cp_parser_parse_tentatively (parser);
22475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22477 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22479 cp_lexer_consume_token (parser->lexer);
22482 else if (bracecount
22483 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22484 cp_lexer_consume_token (parser->lexer);
22487 loc = cp_lexer_peek_token (parser->lexer)->location;
22488 error_at (loc, "not enough collapsed for loops");
22489 collapse_err = true;
22490 cp_parser_abort_tentative_parse (parser);
22499 cp_parser_parse_definitely (parser);
22500 nbraces += bracecount;
22504 /* Note that we saved the original contents of this flag when we entered
22505 the structured block, and so we don't need to re-save it here. */
22506 parser->in_statement = IN_OMP_FOR;
22508 /* Note that the grammar doesn't call for a structured block here,
22509 though the loop as a whole is a structured block. */
22510 body = push_stmt_list ();
22511 cp_parser_statement (parser, NULL_TREE, false, NULL);
22512 body = pop_stmt_list (body);
22514 if (declv == NULL_TREE)
22517 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22518 pre_body, clauses);
22522 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22524 cp_lexer_consume_token (parser->lexer);
22527 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22528 cp_lexer_consume_token (parser->lexer);
22533 error_at (cp_lexer_peek_token (parser->lexer)->location,
22534 "collapsed loops not perfectly nested");
22536 collapse_err = true;
22537 cp_parser_statement_seq_opt (parser, NULL);
22538 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22545 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22546 for_block = TREE_CHAIN (for_block);
22553 #pragma omp for for-clause[optseq] new-line
22556 #define OMP_FOR_CLAUSE_MASK \
22557 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22558 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22559 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22560 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22561 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22562 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22563 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22564 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22567 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22569 tree clauses, sb, ret;
22572 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22573 "#pragma omp for", pragma_tok);
22575 sb = begin_omp_structured_block ();
22576 save = cp_parser_begin_omp_structured_block (parser);
22578 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22580 cp_parser_end_omp_structured_block (parser, save);
22581 add_stmt (finish_omp_structured_block (sb));
22587 # pragma omp master new-line
22588 structured-block */
22591 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22593 cp_parser_require_pragma_eol (parser, pragma_tok);
22594 return c_finish_omp_master (input_location,
22595 cp_parser_omp_structured_block (parser));
22599 # pragma omp ordered new-line
22600 structured-block */
22603 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22605 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22606 cp_parser_require_pragma_eol (parser, pragma_tok);
22607 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22613 { section-sequence }
22616 section-directive[opt] structured-block
22617 section-sequence section-directive structured-block */
22620 cp_parser_omp_sections_scope (cp_parser *parser)
22622 tree stmt, substmt;
22623 bool error_suppress = false;
22626 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22629 stmt = push_stmt_list ();
22631 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22635 substmt = begin_omp_structured_block ();
22636 save = cp_parser_begin_omp_structured_block (parser);
22640 cp_parser_statement (parser, NULL_TREE, false, NULL);
22642 tok = cp_lexer_peek_token (parser->lexer);
22643 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22645 if (tok->type == CPP_CLOSE_BRACE)
22647 if (tok->type == CPP_EOF)
22651 cp_parser_end_omp_structured_block (parser, save);
22652 substmt = finish_omp_structured_block (substmt);
22653 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22654 add_stmt (substmt);
22659 tok = cp_lexer_peek_token (parser->lexer);
22660 if (tok->type == CPP_CLOSE_BRACE)
22662 if (tok->type == CPP_EOF)
22665 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22667 cp_lexer_consume_token (parser->lexer);
22668 cp_parser_require_pragma_eol (parser, tok);
22669 error_suppress = false;
22671 else if (!error_suppress)
22673 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22674 error_suppress = true;
22677 substmt = cp_parser_omp_structured_block (parser);
22678 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22679 add_stmt (substmt);
22681 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22683 substmt = pop_stmt_list (stmt);
22685 stmt = make_node (OMP_SECTIONS);
22686 TREE_TYPE (stmt) = void_type_node;
22687 OMP_SECTIONS_BODY (stmt) = substmt;
22694 # pragma omp sections sections-clause[optseq] newline
22697 #define OMP_SECTIONS_CLAUSE_MASK \
22698 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22699 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22700 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22701 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22702 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22705 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22709 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22710 "#pragma omp sections", pragma_tok);
22712 ret = cp_parser_omp_sections_scope (parser);
22714 OMP_SECTIONS_CLAUSES (ret) = clauses;
22720 # pragma parallel parallel-clause new-line
22721 # pragma parallel for parallel-for-clause new-line
22722 # pragma parallel sections parallel-sections-clause new-line */
22724 #define OMP_PARALLEL_CLAUSE_MASK \
22725 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22726 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22727 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22728 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22729 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22730 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22731 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22732 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22735 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22737 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22738 const char *p_name = "#pragma omp parallel";
22739 tree stmt, clauses, par_clause, ws_clause, block;
22740 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22742 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22746 cp_lexer_consume_token (parser->lexer);
22747 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22748 p_name = "#pragma omp parallel for";
22749 mask |= OMP_FOR_CLAUSE_MASK;
22750 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22752 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22754 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22755 const char *p = IDENTIFIER_POINTER (id);
22756 if (strcmp (p, "sections") == 0)
22758 cp_lexer_consume_token (parser->lexer);
22759 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22760 p_name = "#pragma omp parallel sections";
22761 mask |= OMP_SECTIONS_CLAUSE_MASK;
22762 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22766 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22767 block = begin_omp_parallel ();
22768 save = cp_parser_begin_omp_structured_block (parser);
22772 case PRAGMA_OMP_PARALLEL:
22773 cp_parser_statement (parser, NULL_TREE, false, NULL);
22774 par_clause = clauses;
22777 case PRAGMA_OMP_PARALLEL_FOR:
22778 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22779 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22782 case PRAGMA_OMP_PARALLEL_SECTIONS:
22783 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22784 stmt = cp_parser_omp_sections_scope (parser);
22786 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22790 gcc_unreachable ();
22793 cp_parser_end_omp_structured_block (parser, save);
22794 stmt = finish_omp_parallel (par_clause, block);
22795 if (p_kind != PRAGMA_OMP_PARALLEL)
22796 OMP_PARALLEL_COMBINED (stmt) = 1;
22801 # pragma omp single single-clause[optseq] new-line
22802 structured-block */
22804 #define OMP_SINGLE_CLAUSE_MASK \
22805 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22806 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22807 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22808 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22811 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22813 tree stmt = make_node (OMP_SINGLE);
22814 TREE_TYPE (stmt) = void_type_node;
22816 OMP_SINGLE_CLAUSES (stmt)
22817 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22818 "#pragma omp single", pragma_tok);
22819 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22821 return add_stmt (stmt);
22825 # pragma omp task task-clause[optseq] new-line
22826 structured-block */
22828 #define OMP_TASK_CLAUSE_MASK \
22829 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22830 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22831 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22832 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22833 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22834 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22837 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22839 tree clauses, block;
22842 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22843 "#pragma omp task", pragma_tok);
22844 block = begin_omp_task ();
22845 save = cp_parser_begin_omp_structured_block (parser);
22846 cp_parser_statement (parser, NULL_TREE, false, NULL);
22847 cp_parser_end_omp_structured_block (parser, save);
22848 return finish_omp_task (clauses, block);
22852 # pragma omp taskwait new-line */
22855 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22857 cp_parser_require_pragma_eol (parser, pragma_tok);
22858 finish_omp_taskwait ();
22862 # pragma omp threadprivate (variable-list) */
22865 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22869 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22870 cp_parser_require_pragma_eol (parser, pragma_tok);
22872 finish_omp_threadprivate (vars);
22875 /* Main entry point to OpenMP statement pragmas. */
22878 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22882 switch (pragma_tok->pragma_kind)
22884 case PRAGMA_OMP_ATOMIC:
22885 cp_parser_omp_atomic (parser, pragma_tok);
22887 case PRAGMA_OMP_CRITICAL:
22888 stmt = cp_parser_omp_critical (parser, pragma_tok);
22890 case PRAGMA_OMP_FOR:
22891 stmt = cp_parser_omp_for (parser, pragma_tok);
22893 case PRAGMA_OMP_MASTER:
22894 stmt = cp_parser_omp_master (parser, pragma_tok);
22896 case PRAGMA_OMP_ORDERED:
22897 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22899 case PRAGMA_OMP_PARALLEL:
22900 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22902 case PRAGMA_OMP_SECTIONS:
22903 stmt = cp_parser_omp_sections (parser, pragma_tok);
22905 case PRAGMA_OMP_SINGLE:
22906 stmt = cp_parser_omp_single (parser, pragma_tok);
22908 case PRAGMA_OMP_TASK:
22909 stmt = cp_parser_omp_task (parser, pragma_tok);
22912 gcc_unreachable ();
22916 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22921 static GTY (()) cp_parser *the_parser;
22924 /* Special handling for the first token or line in the file. The first
22925 thing in the file might be #pragma GCC pch_preprocess, which loads a
22926 PCH file, which is a GC collection point. So we need to handle this
22927 first pragma without benefit of an existing lexer structure.
22929 Always returns one token to the caller in *FIRST_TOKEN. This is
22930 either the true first token of the file, or the first token after
22931 the initial pragma. */
22934 cp_parser_initial_pragma (cp_token *first_token)
22938 cp_lexer_get_preprocessor_token (NULL, first_token);
22939 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22942 cp_lexer_get_preprocessor_token (NULL, first_token);
22943 if (first_token->type == CPP_STRING)
22945 name = first_token->u.value;
22947 cp_lexer_get_preprocessor_token (NULL, first_token);
22948 if (first_token->type != CPP_PRAGMA_EOL)
22949 error_at (first_token->location,
22950 "junk at end of %<#pragma GCC pch_preprocess%>");
22953 error_at (first_token->location, "expected string literal");
22955 /* Skip to the end of the pragma. */
22956 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22957 cp_lexer_get_preprocessor_token (NULL, first_token);
22959 /* Now actually load the PCH file. */
22961 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22963 /* Read one more token to return to our caller. We have to do this
22964 after reading the PCH file in, since its pointers have to be
22966 cp_lexer_get_preprocessor_token (NULL, first_token);
22969 /* Normal parsing of a pragma token. Here we can (and must) use the
22973 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22975 cp_token *pragma_tok;
22978 pragma_tok = cp_lexer_consume_token (parser->lexer);
22979 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22980 parser->lexer->in_pragma = true;
22982 id = pragma_tok->pragma_kind;
22985 case PRAGMA_GCC_PCH_PREPROCESS:
22986 error_at (pragma_tok->location,
22987 "%<#pragma GCC pch_preprocess%> must be first");
22990 case PRAGMA_OMP_BARRIER:
22993 case pragma_compound:
22994 cp_parser_omp_barrier (parser, pragma_tok);
22997 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22998 "used in compound statements");
23005 case PRAGMA_OMP_FLUSH:
23008 case pragma_compound:
23009 cp_parser_omp_flush (parser, pragma_tok);
23012 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23013 "used in compound statements");
23020 case PRAGMA_OMP_TASKWAIT:
23023 case pragma_compound:
23024 cp_parser_omp_taskwait (parser, pragma_tok);
23027 error_at (pragma_tok->location,
23028 "%<#pragma omp taskwait%> may only be "
23029 "used in compound statements");
23036 case PRAGMA_OMP_THREADPRIVATE:
23037 cp_parser_omp_threadprivate (parser, pragma_tok);
23040 case PRAGMA_OMP_ATOMIC:
23041 case PRAGMA_OMP_CRITICAL:
23042 case PRAGMA_OMP_FOR:
23043 case PRAGMA_OMP_MASTER:
23044 case PRAGMA_OMP_ORDERED:
23045 case PRAGMA_OMP_PARALLEL:
23046 case PRAGMA_OMP_SECTIONS:
23047 case PRAGMA_OMP_SINGLE:
23048 case PRAGMA_OMP_TASK:
23049 if (context == pragma_external)
23051 cp_parser_omp_construct (parser, pragma_tok);
23054 case PRAGMA_OMP_SECTION:
23055 error_at (pragma_tok->location,
23056 "%<#pragma omp section%> may only be used in "
23057 "%<#pragma omp sections%> construct");
23061 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23062 c_invoke_pragma_handler (id);
23066 cp_parser_error (parser, "expected declaration specifiers");
23070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23074 /* The interface the pragma parsers have to the lexer. */
23077 pragma_lex (tree *value)
23080 enum cpp_ttype ret;
23082 tok = cp_lexer_peek_token (the_parser->lexer);
23085 *value = tok->u.value;
23087 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23089 else if (ret == CPP_STRING)
23090 *value = cp_parser_string_literal (the_parser, false, false);
23093 cp_lexer_consume_token (the_parser->lexer);
23094 if (ret == CPP_KEYWORD)
23102 /* External interface. */
23104 /* Parse one entire translation unit. */
23107 c_parse_file (void)
23109 static bool already_called = false;
23111 if (already_called)
23113 sorry ("inter-module optimizations not implemented for C++");
23116 already_called = true;
23118 the_parser = cp_parser_new ();
23119 push_deferring_access_checks (flag_access_control
23120 ? dk_no_deferred : dk_no_check);
23121 cp_parser_translation_unit (the_parser);
23125 #include "gt-cp-parser.h"