2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 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"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
94 static cp_token eof_token =
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
110 size_t buffer_length;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
129 /* True if we should output debugging information. */
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
154 /* floating-point literal */
158 /* %<__FUNCTION__%> */
160 /* %<__PRETTY_FUNCTION__%> */
168 /* %<typeid%> operator */
170 /* non-constant compound literals */
172 /* a function call */
178 /* an array reference */
184 /* the address of a label */
198 /* calls to overloaded operators */
202 /* a comma operator */
204 /* a call to a constructor */
206 } non_integral_constant;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
214 /* is not a class or namespace */
216 /* is not a class, namespace, or enumeration */
220 /* The various kinds of required token */
221 typedef enum required_token {
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
232 RT_GREATER, /* '>' */
234 RT_ELLIPSIS, /* '...' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
244 /* The type is CPP_KEYWORD */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
275 static cp_lexer *cp_lexer_new_main
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
281 static int cp_lexer_saving_tokens
283 static cp_token_position cp_lexer_token_position
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
301 static void cp_lexer_purge_token
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
307 static void cp_lexer_commit_tokens
309 static void cp_lexer_rollback_tokens
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
333 static void cp_parser_initial_pragma
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
379 cp_lexer_new_main (void)
381 cp_token first_token;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
409 /* Put the first token in the buffer. */
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
425 cp_lexer_get_preprocessor_token (lexer, pos);
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer *lexer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer *lexer)
486 return lexer->debugging_p;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
496 return lexer->next_token - previous_p;
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
505 static inline cp_token *
506 cp_lexer_previous_token (cp_lexer *lexer)
508 cp_token_position tp;
510 if (lexer->next_token == &eof_token)
511 tp = lexer->last_token - 1;
513 tp = cp_lexer_token_position (lexer, true);
515 return cp_lexer_token_at (lexer, tp);
518 /* nonzero if we are presently saving tokens. */
521 cp_lexer_saving_tokens (const cp_lexer* lexer)
523 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
526 /* Store the next token from the preprocessor in *TOKEN. Return true
527 if we reach EOF. If LEXER is NULL, assume we are handling an
528 initial #pragma pch_preprocess, and thus want the lexer to return
529 processed strings. */
532 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
534 static int is_extern_c = 0;
536 /* Get a new token from the preprocessor. */
538 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
539 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
540 token->keyword = RID_MAX;
541 token->pragma_kind = PRAGMA_NONE;
543 /* On some systems, some header files are surrounded by an
544 implicit extern "C" block. Set a flag in the token if it
545 comes from such a header. */
546 is_extern_c += pending_lang_change;
547 pending_lang_change = 0;
548 token->implicit_extern_c = is_extern_c > 0;
550 /* Check to see if this token is a keyword. */
551 if (token->type == CPP_NAME)
553 if (C_IS_RESERVED_WORD (token->u.value))
555 /* Mark this token as a keyword. */
556 token->type = CPP_KEYWORD;
557 /* Record which keyword. */
558 token->keyword = C_RID_CODE (token->u.value);
562 if (warn_cxx0x_compat
563 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
564 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
566 /* Warn about the C++0x keyword (but still treat it as
568 warning (OPT_Wc__0x_compat,
569 "identifier %qE will become a keyword in C++0x",
572 /* Clear out the C_RID_CODE so we don't warn about this
573 particular identifier-turned-keyword again. */
574 C_SET_RID_CODE (token->u.value, RID_MAX);
577 token->ambiguous_p = false;
578 token->keyword = RID_MAX;
581 else if (token->type == CPP_AT_NAME)
583 /* This only happens in Objective-C++; it must be a keyword. */
584 token->type = CPP_KEYWORD;
585 switch (C_RID_CODE (token->u.value))
587 /* Replace 'class' with '@class', 'private' with '@private',
588 etc. This prevents confusion with the C++ keyword
589 'class', and makes the tokens consistent with other
590 Objective-C 'AT' keywords. For example '@class' is
591 reported as RID_AT_CLASS which is consistent with
592 '@synchronized', which is reported as
595 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
596 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
597 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
598 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
599 case RID_THROW: token->keyword = RID_AT_THROW; break;
600 case RID_TRY: token->keyword = RID_AT_TRY; break;
601 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
602 default: token->keyword = C_RID_CODE (token->u.value);
605 else if (token->type == CPP_PRAGMA)
607 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
608 token->pragma_kind = ((enum pragma_kind)
609 TREE_INT_CST_LOW (token->u.value));
610 token->u.value = NULL_TREE;
614 /* Update the globals input_location and the input file stack from TOKEN. */
616 cp_lexer_set_source_position_from_token (cp_token *token)
618 if (token->type != CPP_EOF)
620 input_location = token->location;
624 /* Return a pointer to the next token in the token stream, but do not
627 static inline cp_token *
628 cp_lexer_peek_token (cp_lexer *lexer)
630 if (cp_lexer_debugging_p (lexer))
632 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
633 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
634 putc ('\n', cp_lexer_debug_stream);
636 return lexer->next_token;
639 /* Return true if the next token has the indicated TYPE. */
642 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
644 return cp_lexer_peek_token (lexer)->type == type;
647 /* Return true if the next token does not have the indicated TYPE. */
650 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
652 return !cp_lexer_next_token_is (lexer, type);
655 /* Return true if the next token is the indicated KEYWORD. */
658 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
660 return cp_lexer_peek_token (lexer)->keyword == keyword;
663 /* Return true if the next token is not the indicated KEYWORD. */
666 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
668 return cp_lexer_peek_token (lexer)->keyword != keyword;
671 /* Return true if the next token is a keyword for a decl-specifier. */
674 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
678 token = cp_lexer_peek_token (lexer);
679 switch (token->keyword)
681 /* auto specifier: storage-class-specifier in C++,
682 simple-type-specifier in C++0x. */
684 /* Storage classes. */
690 /* Elaborated type specifiers. */
696 /* Simple type specifiers. */
711 /* GNU extensions. */
714 /* C++0x extensions. */
723 /* Return a pointer to the Nth token in the token stream. If N is 1,
724 then this is precisely equivalent to cp_lexer_peek_token (except
725 that it is not inline). One would like to disallow that case, but
726 there is one case (cp_parser_nth_token_starts_template_id) where
727 the caller passes a variable for N and it might be 1. */
730 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
734 /* N is 1-based, not zero-based. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream,
739 "cp_lexer: peeking ahead %ld at token: ", (long)n);
742 token = lexer->next_token;
743 gcc_assert (!n || token != &eof_token);
747 if (token == lexer->last_token)
753 if (token->type != CPP_PURGED)
757 if (cp_lexer_debugging_p (lexer))
759 cp_lexer_print_token (cp_lexer_debug_stream, token);
760 putc ('\n', cp_lexer_debug_stream);
766 /* Return the next token, and advance the lexer's next_token pointer
767 to point to the next non-purged token. */
770 cp_lexer_consume_token (cp_lexer* lexer)
772 cp_token *token = lexer->next_token;
774 gcc_assert (token != &eof_token);
775 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
780 if (lexer->next_token == lexer->last_token)
782 lexer->next_token = &eof_token;
787 while (lexer->next_token->type == CPP_PURGED);
789 cp_lexer_set_source_position_from_token (token);
791 /* Provide debugging output. */
792 if (cp_lexer_debugging_p (lexer))
794 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
795 cp_lexer_print_token (cp_lexer_debug_stream, token);
796 putc ('\n', cp_lexer_debug_stream);
802 /* Permanently remove the next token from the token stream, and
803 advance the next_token pointer to refer to the next non-purged
807 cp_lexer_purge_token (cp_lexer *lexer)
809 cp_token *tok = lexer->next_token;
811 gcc_assert (tok != &eof_token);
812 tok->type = CPP_PURGED;
813 tok->location = UNKNOWN_LOCATION;
814 tok->u.value = NULL_TREE;
815 tok->keyword = RID_MAX;
820 if (tok == lexer->last_token)
826 while (tok->type == CPP_PURGED);
827 lexer->next_token = tok;
830 /* Permanently remove all tokens after TOK, up to, but not
831 including, the token that will be returned next by
832 cp_lexer_peek_token. */
835 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
837 cp_token *peek = lexer->next_token;
839 if (peek == &eof_token)
840 peek = lexer->last_token;
842 gcc_assert (tok < peek);
844 for ( tok += 1; tok != peek; tok += 1)
846 tok->type = CPP_PURGED;
847 tok->location = UNKNOWN_LOCATION;
848 tok->u.value = NULL_TREE;
849 tok->keyword = RID_MAX;
853 /* Begin saving tokens. All tokens consumed after this point will be
857 cp_lexer_save_tokens (cp_lexer* lexer)
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer))
861 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
863 VEC_safe_push (cp_token_position, heap,
864 lexer->saved_tokens, lexer->next_token);
867 /* Commit to the portion of the token stream most recently saved. */
870 cp_lexer_commit_tokens (cp_lexer* lexer)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
876 VEC_pop (cp_token_position, lexer->saved_tokens);
879 /* Return all tokens saved since the last call to cp_lexer_save_tokens
880 to the token stream. Stop saving tokens. */
883 cp_lexer_rollback_tokens (cp_lexer* lexer)
885 /* Provide debugging output. */
886 if (cp_lexer_debugging_p (lexer))
887 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
889 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
892 /* Print a representation of the TOKEN on the STREAM. */
894 #ifdef ENABLE_CHECKING
897 cp_lexer_print_token (FILE * stream, cp_token *token)
899 /* We don't use cpp_type2name here because the parser defines
900 a few tokens of its own. */
901 static const char *const token_names[] = {
902 /* cpplib-defined token types */
908 /* C++ parser token types - see "Manifest constants", above. */
911 "NESTED_NAME_SPECIFIER",
915 /* If we have a name for the token, print it out. Otherwise, we
916 simply give the numeric code. */
917 gcc_assert (token->type < ARRAY_SIZE(token_names));
918 fputs (token_names[token->type], stream);
920 /* For some tokens, print the associated data. */
924 /* Some keywords have a value that is not an IDENTIFIER_NODE.
925 For example, `struct' is mapped to an INTEGER_CST. */
926 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
928 /* else fall through */
930 fputs (IDENTIFIER_POINTER (token->u.value), stream);
938 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
946 /* Start emitting debugging information. */
949 cp_lexer_start_debugging (cp_lexer* lexer)
951 lexer->debugging_p = true;
954 /* Stop emitting debugging information. */
957 cp_lexer_stop_debugging (cp_lexer* lexer)
959 lexer->debugging_p = false;
962 #endif /* ENABLE_CHECKING */
964 /* Create a new cp_token_cache, representing a range of tokens. */
966 static cp_token_cache *
967 cp_token_cache_new (cp_token *first, cp_token *last)
969 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
970 cache->first = first;
976 /* Decl-specifiers. */
978 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
981 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
983 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
988 /* Nothing other than the parser should be creating declarators;
989 declarators are a semi-syntactic representation of C++ entities.
990 Other parts of the front end that need to create entities (like
991 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
993 static cp_declarator *make_call_declarator
994 (cp_declarator *, tree, cp_cv_quals, tree, tree);
995 static cp_declarator *make_array_declarator
996 (cp_declarator *, tree);
997 static cp_declarator *make_pointer_declarator
998 (cp_cv_quals, cp_declarator *);
999 static cp_declarator *make_reference_declarator
1000 (cp_cv_quals, cp_declarator *, bool);
1001 static cp_parameter_declarator *make_parameter_declarator
1002 (cp_decl_specifier_seq *, cp_declarator *, tree);
1003 static cp_declarator *make_ptrmem_declarator
1004 (cp_cv_quals, tree, cp_declarator *);
1006 /* An erroneous declarator. */
1007 static cp_declarator *cp_error_declarator;
1009 /* The obstack on which declarators and related data structures are
1011 static struct obstack declarator_obstack;
1013 /* Alloc BYTES from the declarator memory pool. */
1015 static inline void *
1016 alloc_declarator (size_t bytes)
1018 return obstack_alloc (&declarator_obstack, bytes);
1021 /* Allocate a declarator of the indicated KIND. Clear fields that are
1022 common to all declarators. */
1024 static cp_declarator *
1025 make_declarator (cp_declarator_kind kind)
1027 cp_declarator *declarator;
1029 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1030 declarator->kind = kind;
1031 declarator->attributes = NULL_TREE;
1032 declarator->declarator = NULL;
1033 declarator->parameter_pack_p = false;
1034 declarator->id_loc = UNKNOWN_LOCATION;
1039 /* Make a declarator for a generalized identifier. If
1040 QUALIFYING_SCOPE is non-NULL, the identifier is
1041 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1042 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1045 static cp_declarator *
1046 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1047 special_function_kind sfk)
1049 cp_declarator *declarator;
1051 /* It is valid to write:
1053 class C { void f(); };
1057 The standard is not clear about whether `typedef const C D' is
1058 legal; as of 2002-09-15 the committee is considering that
1059 question. EDG 3.0 allows that syntax. Therefore, we do as
1061 if (qualifying_scope && TYPE_P (qualifying_scope))
1062 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1064 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1065 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1066 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1068 declarator = make_declarator (cdk_id);
1069 declarator->u.id.qualifying_scope = qualifying_scope;
1070 declarator->u.id.unqualified_name = unqualified_name;
1071 declarator->u.id.sfk = sfk;
1076 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1077 of modifiers such as const or volatile to apply to the pointer
1078 type, represented as identifiers. */
1081 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1083 cp_declarator *declarator;
1085 declarator = make_declarator (cdk_pointer);
1086 declarator->declarator = target;
1087 declarator->u.pointer.qualifiers = cv_qualifiers;
1088 declarator->u.pointer.class_type = NULL_TREE;
1091 declarator->id_loc = target->id_loc;
1092 declarator->parameter_pack_p = target->parameter_pack_p;
1093 target->parameter_pack_p = false;
1096 declarator->parameter_pack_p = false;
1101 /* Like make_pointer_declarator -- but for references. */
1104 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1107 cp_declarator *declarator;
1109 declarator = make_declarator (cdk_reference);
1110 declarator->declarator = target;
1111 declarator->u.reference.qualifiers = cv_qualifiers;
1112 declarator->u.reference.rvalue_ref = rvalue_ref;
1115 declarator->id_loc = target->id_loc;
1116 declarator->parameter_pack_p = target->parameter_pack_p;
1117 target->parameter_pack_p = false;
1120 declarator->parameter_pack_p = false;
1125 /* Like make_pointer_declarator -- but for a pointer to a non-static
1126 member of CLASS_TYPE. */
1129 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1130 cp_declarator *pointee)
1132 cp_declarator *declarator;
1134 declarator = make_declarator (cdk_ptrmem);
1135 declarator->declarator = pointee;
1136 declarator->u.pointer.qualifiers = cv_qualifiers;
1137 declarator->u.pointer.class_type = class_type;
1141 declarator->parameter_pack_p = pointee->parameter_pack_p;
1142 pointee->parameter_pack_p = false;
1145 declarator->parameter_pack_p = false;
1150 /* Make a declarator for the function given by TARGET, with the
1151 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1152 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1153 indicates what exceptions can be thrown. */
1156 make_call_declarator (cp_declarator *target,
1158 cp_cv_quals cv_qualifiers,
1159 tree exception_specification,
1160 tree late_return_type)
1162 cp_declarator *declarator;
1164 declarator = make_declarator (cdk_function);
1165 declarator->declarator = target;
1166 declarator->u.function.parameters = parms;
1167 declarator->u.function.qualifiers = cv_qualifiers;
1168 declarator->u.function.exception_specification = exception_specification;
1169 declarator->u.function.late_return_type = late_return_type;
1172 declarator->id_loc = target->id_loc;
1173 declarator->parameter_pack_p = target->parameter_pack_p;
1174 target->parameter_pack_p = false;
1177 declarator->parameter_pack_p = false;
1182 /* Make a declarator for an array of BOUNDS elements, each of which is
1183 defined by ELEMENT. */
1186 make_array_declarator (cp_declarator *element, tree bounds)
1188 cp_declarator *declarator;
1190 declarator = make_declarator (cdk_array);
1191 declarator->declarator = element;
1192 declarator->u.array.bounds = bounds;
1195 declarator->id_loc = element->id_loc;
1196 declarator->parameter_pack_p = element->parameter_pack_p;
1197 element->parameter_pack_p = false;
1200 declarator->parameter_pack_p = false;
1205 /* Determine whether the declarator we've seen so far can be a
1206 parameter pack, when followed by an ellipsis. */
1208 declarator_can_be_parameter_pack (cp_declarator *declarator)
1210 /* Search for a declarator name, or any other declarator that goes
1211 after the point where the ellipsis could appear in a parameter
1212 pack. If we find any of these, then this declarator can not be
1213 made into a parameter pack. */
1215 while (declarator && !found)
1217 switch ((int)declarator->kind)
1228 declarator = declarator->declarator;
1236 cp_parameter_declarator *no_parameters;
1238 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1239 DECLARATOR and DEFAULT_ARGUMENT. */
1241 cp_parameter_declarator *
1242 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1243 cp_declarator *declarator,
1244 tree default_argument)
1246 cp_parameter_declarator *parameter;
1248 parameter = ((cp_parameter_declarator *)
1249 alloc_declarator (sizeof (cp_parameter_declarator)));
1250 parameter->next = NULL;
1251 if (decl_specifiers)
1252 parameter->decl_specifiers = *decl_specifiers;
1254 clear_decl_specs (¶meter->decl_specifiers);
1255 parameter->declarator = declarator;
1256 parameter->default_argument = default_argument;
1257 parameter->ellipsis_p = false;
1262 /* Returns true iff DECLARATOR is a declaration for a function. */
1265 function_declarator_p (const cp_declarator *declarator)
1269 if (declarator->kind == cdk_function
1270 && declarator->declarator->kind == cdk_id)
1272 if (declarator->kind == cdk_id
1273 || declarator->kind == cdk_error)
1275 declarator = declarator->declarator;
1285 A cp_parser parses the token stream as specified by the C++
1286 grammar. Its job is purely parsing, not semantic analysis. For
1287 example, the parser breaks the token stream into declarators,
1288 expressions, statements, and other similar syntactic constructs.
1289 It does not check that the types of the expressions on either side
1290 of an assignment-statement are compatible, or that a function is
1291 not declared with a parameter of type `void'.
1293 The parser invokes routines elsewhere in the compiler to perform
1294 semantic analysis and to build up the abstract syntax tree for the
1297 The parser (and the template instantiation code, which is, in a
1298 way, a close relative of parsing) are the only parts of the
1299 compiler that should be calling push_scope and pop_scope, or
1300 related functions. The parser (and template instantiation code)
1301 keeps track of what scope is presently active; everything else
1302 should simply honor that. (The code that generates static
1303 initializers may also need to set the scope, in order to check
1304 access control correctly when emitting the initializers.)
1309 The parser is of the standard recursive-descent variety. Upcoming
1310 tokens in the token stream are examined in order to determine which
1311 production to use when parsing a non-terminal. Some C++ constructs
1312 require arbitrary look ahead to disambiguate. For example, it is
1313 impossible, in the general case, to tell whether a statement is an
1314 expression or declaration without scanning the entire statement.
1315 Therefore, the parser is capable of "parsing tentatively." When the
1316 parser is not sure what construct comes next, it enters this mode.
1317 Then, while we attempt to parse the construct, the parser queues up
1318 error messages, rather than issuing them immediately, and saves the
1319 tokens it consumes. If the construct is parsed successfully, the
1320 parser "commits", i.e., it issues any queued error messages and
1321 the tokens that were being preserved are permanently discarded.
1322 If, however, the construct is not parsed successfully, the parser
1323 rolls back its state completely so that it can resume parsing using
1324 a different alternative.
1329 The performance of the parser could probably be improved substantially.
1330 We could often eliminate the need to parse tentatively by looking ahead
1331 a little bit. In some places, this approach might not entirely eliminate
1332 the need to parse tentatively, but it might still speed up the average
1335 /* Flags that are passed to some parsing functions. These values can
1336 be bitwise-ored together. */
1341 CP_PARSER_FLAGS_NONE = 0x0,
1342 /* The construct is optional. If it is not present, then no error
1343 should be issued. */
1344 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1345 /* When parsing a type-specifier, treat user-defined type-names
1346 as non-type identifiers. */
1347 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1348 /* When parsing a type-specifier, do not try to parse a class-specifier
1349 or enum-specifier. */
1350 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1351 /* When parsing a decl-specifier-seq, only allow type-specifier or
1353 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1356 /* This type is used for parameters and variables which hold
1357 combinations of the above flags. */
1358 typedef int cp_parser_flags;
1360 /* The different kinds of declarators we want to parse. */
1362 typedef enum cp_parser_declarator_kind
1364 /* We want an abstract declarator. */
1365 CP_PARSER_DECLARATOR_ABSTRACT,
1366 /* We want a named declarator. */
1367 CP_PARSER_DECLARATOR_NAMED,
1368 /* We don't mind, but the name must be an unqualified-id. */
1369 CP_PARSER_DECLARATOR_EITHER
1370 } cp_parser_declarator_kind;
1372 /* The precedence values used to parse binary expressions. The minimum value
1373 of PREC must be 1, because zero is reserved to quickly discriminate
1374 binary operators from other tokens. */
1379 PREC_LOGICAL_OR_EXPRESSION,
1380 PREC_LOGICAL_AND_EXPRESSION,
1381 PREC_INCLUSIVE_OR_EXPRESSION,
1382 PREC_EXCLUSIVE_OR_EXPRESSION,
1383 PREC_AND_EXPRESSION,
1384 PREC_EQUALITY_EXPRESSION,
1385 PREC_RELATIONAL_EXPRESSION,
1386 PREC_SHIFT_EXPRESSION,
1387 PREC_ADDITIVE_EXPRESSION,
1388 PREC_MULTIPLICATIVE_EXPRESSION,
1390 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1393 /* A mapping from a token type to a corresponding tree node type, with a
1394 precedence value. */
1396 typedef struct cp_parser_binary_operations_map_node
1398 /* The token type. */
1399 enum cpp_ttype token_type;
1400 /* The corresponding tree code. */
1401 enum tree_code tree_type;
1402 /* The precedence of this operator. */
1403 enum cp_parser_prec prec;
1404 } cp_parser_binary_operations_map_node;
1406 /* The status of a tentative parse. */
1408 typedef enum cp_parser_status_kind
1410 /* No errors have occurred. */
1411 CP_PARSER_STATUS_KIND_NO_ERROR,
1412 /* An error has occurred. */
1413 CP_PARSER_STATUS_KIND_ERROR,
1414 /* We are committed to this tentative parse, whether or not an error
1416 CP_PARSER_STATUS_KIND_COMMITTED
1417 } cp_parser_status_kind;
1419 typedef struct cp_parser_expression_stack_entry
1421 /* Left hand side of the binary operation we are currently
1424 /* Original tree code for left hand side, if it was a binary
1425 expression itself (used for -Wparentheses). */
1426 enum tree_code lhs_type;
1427 /* Tree code for the binary operation we are parsing. */
1428 enum tree_code tree_type;
1429 /* Precedence of the binary operation we are parsing. */
1430 enum cp_parser_prec prec;
1431 } cp_parser_expression_stack_entry;
1433 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1434 entries because precedence levels on the stack are monotonically
1436 typedef struct cp_parser_expression_stack_entry
1437 cp_parser_expression_stack[NUM_PREC_VALUES];
1439 /* Context that is saved and restored when parsing tentatively. */
1440 typedef struct GTY (()) cp_parser_context {
1441 /* If this is a tentative parsing context, the status of the
1443 enum cp_parser_status_kind status;
1444 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1445 that are looked up in this context must be looked up both in the
1446 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1447 the context of the containing expression. */
1450 /* The next parsing context in the stack. */
1451 struct cp_parser_context *next;
1452 } cp_parser_context;
1456 /* Constructors and destructors. */
1458 static cp_parser_context *cp_parser_context_new
1459 (cp_parser_context *);
1461 /* Class variables. */
1463 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1465 /* The operator-precedence table used by cp_parser_binary_expression.
1466 Transformed into an associative array (binops_by_token) by
1469 static const cp_parser_binary_operations_map_node binops[] = {
1470 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1471 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1473 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1474 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1475 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1477 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1478 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1480 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1481 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1483 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1484 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1485 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1486 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1488 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1489 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1491 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1493 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1495 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1497 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1499 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1502 /* The same as binops, but initialized by cp_parser_new so that
1503 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1505 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1507 /* Constructors and destructors. */
1509 /* Construct a new context. The context below this one on the stack
1510 is given by NEXT. */
1512 static cp_parser_context *
1513 cp_parser_context_new (cp_parser_context* next)
1515 cp_parser_context *context;
1517 /* Allocate the storage. */
1518 if (cp_parser_context_free_list != NULL)
1520 /* Pull the first entry from the free list. */
1521 context = cp_parser_context_free_list;
1522 cp_parser_context_free_list = context->next;
1523 memset (context, 0, sizeof (*context));
1526 context = ggc_alloc_cleared_cp_parser_context ();
1528 /* No errors have occurred yet in this context. */
1529 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1530 /* If this is not the bottommost context, copy information that we
1531 need from the previous context. */
1534 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1535 expression, then we are parsing one in this context, too. */
1536 context->object_type = next->object_type;
1537 /* Thread the stack. */
1538 context->next = next;
1544 /* An entry in a queue of function arguments that require post-processing. */
1546 typedef struct GTY(()) cp_default_arg_entry_d {
1547 /* The current_class_type when we parsed this arg. */
1550 /* The function decl itself. */
1552 } cp_default_arg_entry;
1554 DEF_VEC_O(cp_default_arg_entry);
1555 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1557 /* An entry in a stack for member functions of local classes. */
1559 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1560 /* Functions with default arguments that require post-processing.
1561 Functions appear in this list in declaration order. */
1562 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1564 /* Functions with defintions that require post-processing. Functions
1565 appear in this list in declaration order. */
1566 VEC(tree,gc) *funs_with_definitions;
1567 } cp_unparsed_functions_entry;
1569 DEF_VEC_O(cp_unparsed_functions_entry);
1570 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1572 /* The cp_parser structure represents the C++ parser. */
1574 typedef struct GTY(()) cp_parser {
1575 /* The lexer from which we are obtaining tokens. */
1578 /* The scope in which names should be looked up. If NULL_TREE, then
1579 we look up names in the scope that is currently open in the
1580 source program. If non-NULL, this is either a TYPE or
1581 NAMESPACE_DECL for the scope in which we should look. It can
1582 also be ERROR_MARK, when we've parsed a bogus scope.
1584 This value is not cleared automatically after a name is looked
1585 up, so we must be careful to clear it before starting a new look
1586 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1587 will look up `Z' in the scope of `X', rather than the current
1588 scope.) Unfortunately, it is difficult to tell when name lookup
1589 is complete, because we sometimes peek at a token, look it up,
1590 and then decide not to consume it. */
1593 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1594 last lookup took place. OBJECT_SCOPE is used if an expression
1595 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1596 respectively. QUALIFYING_SCOPE is used for an expression of the
1597 form "X::Y"; it refers to X. */
1599 tree qualifying_scope;
1601 /* A stack of parsing contexts. All but the bottom entry on the
1602 stack will be tentative contexts.
1604 We parse tentatively in order to determine which construct is in
1605 use in some situations. For example, in order to determine
1606 whether a statement is an expression-statement or a
1607 declaration-statement we parse it tentatively as a
1608 declaration-statement. If that fails, we then reparse the same
1609 token stream as an expression-statement. */
1610 cp_parser_context *context;
1612 /* True if we are parsing GNU C++. If this flag is not set, then
1613 GNU extensions are not recognized. */
1614 bool allow_gnu_extensions_p;
1616 /* TRUE if the `>' token should be interpreted as the greater-than
1617 operator. FALSE if it is the end of a template-id or
1618 template-parameter-list. In C++0x mode, this flag also applies to
1619 `>>' tokens, which are viewed as two consecutive `>' tokens when
1620 this flag is FALSE. */
1621 bool greater_than_is_operator_p;
1623 /* TRUE if default arguments are allowed within a parameter list
1624 that starts at this point. FALSE if only a gnu extension makes
1625 them permissible. */
1626 bool default_arg_ok_p;
1628 /* TRUE if we are parsing an integral constant-expression. See
1629 [expr.const] for a precise definition. */
1630 bool integral_constant_expression_p;
1632 /* TRUE if we are parsing an integral constant-expression -- but a
1633 non-constant expression should be permitted as well. This flag
1634 is used when parsing an array bound so that GNU variable-length
1635 arrays are tolerated. */
1636 bool allow_non_integral_constant_expression_p;
1638 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1639 been seen that makes the expression non-constant. */
1640 bool non_integral_constant_expression_p;
1642 /* TRUE if local variable names and `this' are forbidden in the
1644 bool local_variables_forbidden_p;
1646 /* TRUE if the declaration we are parsing is part of a
1647 linkage-specification of the form `extern string-literal
1649 bool in_unbraced_linkage_specification_p;
1651 /* TRUE if we are presently parsing a declarator, after the
1652 direct-declarator. */
1653 bool in_declarator_p;
1655 /* TRUE if we are presently parsing a template-argument-list. */
1656 bool in_template_argument_list_p;
1658 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1659 to IN_OMP_BLOCK if parsing OpenMP structured block and
1660 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1661 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1662 iteration-statement, OpenMP block or loop within that switch. */
1663 #define IN_SWITCH_STMT 1
1664 #define IN_ITERATION_STMT 2
1665 #define IN_OMP_BLOCK 4
1666 #define IN_OMP_FOR 8
1667 #define IN_IF_STMT 16
1668 unsigned char in_statement;
1670 /* TRUE if we are presently parsing the body of a switch statement.
1671 Note that this doesn't quite overlap with in_statement above.
1672 The difference relates to giving the right sets of error messages:
1673 "case not in switch" vs "break statement used with OpenMP...". */
1674 bool in_switch_statement_p;
1676 /* TRUE if we are parsing a type-id in an expression context. In
1677 such a situation, both "type (expr)" and "type (type)" are valid
1679 bool in_type_id_in_expr_p;
1681 /* TRUE if we are currently in a header file where declarations are
1682 implicitly extern "C". */
1683 bool implicit_extern_c;
1685 /* TRUE if strings in expressions should be translated to the execution
1687 bool translate_strings_p;
1689 /* TRUE if we are presently parsing the body of a function, but not
1691 bool in_function_body;
1693 /* If non-NULL, then we are parsing a construct where new type
1694 definitions are not permitted. The string stored here will be
1695 issued as an error message if a type is defined. */
1696 const char *type_definition_forbidden_message;
1698 /* A stack used for member functions of local classes. The lists
1699 contained in an individual entry can only be processed once the
1700 outermost class being defined is complete. */
1701 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1703 /* The number of classes whose definitions are currently in
1705 unsigned num_classes_being_defined;
1707 /* The number of template parameter lists that apply directly to the
1708 current declaration. */
1709 unsigned num_template_parameter_lists;
1712 /* Managing the unparsed function queues. */
1714 #define unparsed_funs_with_default_args \
1715 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1716 #define unparsed_funs_with_definitions \
1717 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1720 push_unparsed_function_queues (cp_parser *parser)
1722 VEC_safe_push (cp_unparsed_functions_entry, gc,
1723 parser->unparsed_queues, NULL);
1724 unparsed_funs_with_default_args = NULL;
1725 unparsed_funs_with_definitions = make_tree_vector ();
1729 pop_unparsed_function_queues (cp_parser *parser)
1731 release_tree_vector (unparsed_funs_with_definitions);
1732 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1737 /* Constructors and destructors. */
1739 static cp_parser *cp_parser_new
1742 /* Routines to parse various constructs.
1744 Those that return `tree' will return the error_mark_node (rather
1745 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1746 Sometimes, they will return an ordinary node if error-recovery was
1747 attempted, even though a parse error occurred. So, to check
1748 whether or not a parse error occurred, you should always use
1749 cp_parser_error_occurred. If the construct is optional (indicated
1750 either by an `_opt' in the name of the function that does the
1751 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1752 the construct is not present. */
1754 /* Lexical conventions [gram.lex] */
1756 static tree cp_parser_identifier
1758 static tree cp_parser_string_literal
1759 (cp_parser *, bool, bool);
1761 /* Basic concepts [gram.basic] */
1763 static bool cp_parser_translation_unit
1766 /* Expressions [gram.expr] */
1768 static tree cp_parser_primary_expression
1769 (cp_parser *, bool, bool, bool, cp_id_kind *);
1770 static tree cp_parser_id_expression
1771 (cp_parser *, bool, bool, bool *, bool, bool);
1772 static tree cp_parser_unqualified_id
1773 (cp_parser *, bool, bool, bool, bool);
1774 static tree cp_parser_nested_name_specifier_opt
1775 (cp_parser *, bool, bool, bool, bool);
1776 static tree cp_parser_nested_name_specifier
1777 (cp_parser *, bool, bool, bool, bool);
1778 static tree cp_parser_qualifying_entity
1779 (cp_parser *, bool, bool, bool, bool, bool);
1780 static tree cp_parser_postfix_expression
1781 (cp_parser *, bool, bool, bool, cp_id_kind *);
1782 static tree cp_parser_postfix_open_square_expression
1783 (cp_parser *, tree, bool);
1784 static tree cp_parser_postfix_dot_deref_expression
1785 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1786 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1787 (cp_parser *, int, bool, bool, bool *);
1788 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1789 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1790 static void cp_parser_pseudo_destructor_name
1791 (cp_parser *, tree *, tree *);
1792 static tree cp_parser_unary_expression
1793 (cp_parser *, bool, bool, cp_id_kind *);
1794 static enum tree_code cp_parser_unary_operator
1796 static tree cp_parser_new_expression
1798 static VEC(tree,gc) *cp_parser_new_placement
1800 static tree cp_parser_new_type_id
1801 (cp_parser *, tree *);
1802 static cp_declarator *cp_parser_new_declarator_opt
1804 static cp_declarator *cp_parser_direct_new_declarator
1806 static VEC(tree,gc) *cp_parser_new_initializer
1808 static tree cp_parser_delete_expression
1810 static tree cp_parser_cast_expression
1811 (cp_parser *, bool, bool, cp_id_kind *);
1812 static tree cp_parser_binary_expression
1813 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1814 static tree cp_parser_question_colon_clause
1815 (cp_parser *, tree);
1816 static tree cp_parser_assignment_expression
1817 (cp_parser *, bool, cp_id_kind *);
1818 static enum tree_code cp_parser_assignment_operator_opt
1820 static tree cp_parser_expression
1821 (cp_parser *, bool, cp_id_kind *);
1822 static tree cp_parser_constant_expression
1823 (cp_parser *, bool, bool *);
1824 static tree cp_parser_builtin_offsetof
1826 static tree cp_parser_lambda_expression
1828 static void cp_parser_lambda_introducer
1829 (cp_parser *, tree);
1830 static void cp_parser_lambda_declarator_opt
1831 (cp_parser *, tree);
1832 static void cp_parser_lambda_body
1833 (cp_parser *, tree);
1835 /* Statements [gram.stmt.stmt] */
1837 static void cp_parser_statement
1838 (cp_parser *, tree, bool, bool *);
1839 static void cp_parser_label_for_labeled_statement
1841 static tree cp_parser_expression_statement
1842 (cp_parser *, tree);
1843 static tree cp_parser_compound_statement
1844 (cp_parser *, tree, bool);
1845 static void cp_parser_statement_seq_opt
1846 (cp_parser *, tree);
1847 static tree cp_parser_selection_statement
1848 (cp_parser *, bool *);
1849 static tree cp_parser_condition
1851 static tree cp_parser_iteration_statement
1853 static void cp_parser_for_init_statement
1855 static tree cp_parser_c_for
1857 static tree cp_parser_range_for
1859 static tree cp_parser_jump_statement
1861 static void cp_parser_declaration_statement
1864 static tree cp_parser_implicitly_scoped_statement
1865 (cp_parser *, bool *);
1866 static void cp_parser_already_scoped_statement
1869 /* Declarations [gram.dcl.dcl] */
1871 static void cp_parser_declaration_seq_opt
1873 static void cp_parser_declaration
1875 static void cp_parser_block_declaration
1876 (cp_parser *, bool);
1877 static void cp_parser_simple_declaration
1878 (cp_parser *, bool);
1879 static void cp_parser_decl_specifier_seq
1880 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1881 static tree cp_parser_storage_class_specifier_opt
1883 static tree cp_parser_function_specifier_opt
1884 (cp_parser *, cp_decl_specifier_seq *);
1885 static tree cp_parser_type_specifier
1886 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1888 static tree cp_parser_simple_type_specifier
1889 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1890 static tree cp_parser_type_name
1892 static tree cp_parser_nonclass_name
1893 (cp_parser* parser);
1894 static tree cp_parser_elaborated_type_specifier
1895 (cp_parser *, bool, bool);
1896 static tree cp_parser_enum_specifier
1898 static void cp_parser_enumerator_list
1899 (cp_parser *, tree);
1900 static void cp_parser_enumerator_definition
1901 (cp_parser *, tree);
1902 static tree cp_parser_namespace_name
1904 static void cp_parser_namespace_definition
1906 static void cp_parser_namespace_body
1908 static tree cp_parser_qualified_namespace_specifier
1910 static void cp_parser_namespace_alias_definition
1912 static bool cp_parser_using_declaration
1913 (cp_parser *, bool);
1914 static void cp_parser_using_directive
1916 static void cp_parser_asm_definition
1918 static void cp_parser_linkage_specification
1920 static void cp_parser_static_assert
1921 (cp_parser *, bool);
1922 static tree cp_parser_decltype
1925 /* Declarators [gram.dcl.decl] */
1927 static tree cp_parser_init_declarator
1928 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1929 static cp_declarator *cp_parser_declarator
1930 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1931 static cp_declarator *cp_parser_direct_declarator
1932 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1933 static enum tree_code cp_parser_ptr_operator
1934 (cp_parser *, tree *, cp_cv_quals *);
1935 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1937 static tree cp_parser_late_return_type_opt
1939 static tree cp_parser_declarator_id
1940 (cp_parser *, bool);
1941 static tree cp_parser_type_id
1943 static tree cp_parser_template_type_arg
1945 static tree cp_parser_trailing_type_id (cp_parser *);
1946 static tree cp_parser_type_id_1
1947 (cp_parser *, bool, bool);
1948 static void cp_parser_type_specifier_seq
1949 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1950 static tree cp_parser_parameter_declaration_clause
1952 static tree cp_parser_parameter_declaration_list
1953 (cp_parser *, bool *);
1954 static cp_parameter_declarator *cp_parser_parameter_declaration
1955 (cp_parser *, bool, bool *);
1956 static tree cp_parser_default_argument
1957 (cp_parser *, bool);
1958 static void cp_parser_function_body
1960 static tree cp_parser_initializer
1961 (cp_parser *, bool *, bool *);
1962 static tree cp_parser_initializer_clause
1963 (cp_parser *, bool *);
1964 static tree cp_parser_braced_list
1965 (cp_parser*, bool*);
1966 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1967 (cp_parser *, bool *);
1969 static bool cp_parser_ctor_initializer_opt_and_function_body
1972 /* Classes [gram.class] */
1974 static tree cp_parser_class_name
1975 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1976 static tree cp_parser_class_specifier
1978 static tree cp_parser_class_head
1979 (cp_parser *, bool *, tree *, tree *);
1980 static enum tag_types cp_parser_class_key
1982 static void cp_parser_member_specification_opt
1984 static void cp_parser_member_declaration
1986 static tree cp_parser_pure_specifier
1988 static tree cp_parser_constant_initializer
1991 /* Derived classes [gram.class.derived] */
1993 static tree cp_parser_base_clause
1995 static tree cp_parser_base_specifier
1998 /* Special member functions [gram.special] */
2000 static tree cp_parser_conversion_function_id
2002 static tree cp_parser_conversion_type_id
2004 static cp_declarator *cp_parser_conversion_declarator_opt
2006 static bool cp_parser_ctor_initializer_opt
2008 static void cp_parser_mem_initializer_list
2010 static tree cp_parser_mem_initializer
2012 static tree cp_parser_mem_initializer_id
2015 /* Overloading [gram.over] */
2017 static tree cp_parser_operator_function_id
2019 static tree cp_parser_operator
2022 /* Templates [gram.temp] */
2024 static void cp_parser_template_declaration
2025 (cp_parser *, bool);
2026 static tree cp_parser_template_parameter_list
2028 static tree cp_parser_template_parameter
2029 (cp_parser *, bool *, bool *);
2030 static tree cp_parser_type_parameter
2031 (cp_parser *, bool *);
2032 static tree cp_parser_template_id
2033 (cp_parser *, bool, bool, bool);
2034 static tree cp_parser_template_name
2035 (cp_parser *, bool, bool, bool, bool *);
2036 static tree cp_parser_template_argument_list
2038 static tree cp_parser_template_argument
2040 static void cp_parser_explicit_instantiation
2042 static void cp_parser_explicit_specialization
2045 /* Exception handling [gram.exception] */
2047 static tree cp_parser_try_block
2049 static bool cp_parser_function_try_block
2051 static void cp_parser_handler_seq
2053 static void cp_parser_handler
2055 static tree cp_parser_exception_declaration
2057 static tree cp_parser_throw_expression
2059 static tree cp_parser_exception_specification_opt
2061 static tree cp_parser_type_id_list
2064 /* GNU Extensions */
2066 static tree cp_parser_asm_specification_opt
2068 static tree cp_parser_asm_operand_list
2070 static tree cp_parser_asm_clobber_list
2072 static tree cp_parser_asm_label_list
2074 static tree cp_parser_attributes_opt
2076 static tree cp_parser_attribute_list
2078 static bool cp_parser_extension_opt
2079 (cp_parser *, int *);
2080 static void cp_parser_label_declaration
2083 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2084 static bool cp_parser_pragma
2085 (cp_parser *, enum pragma_context);
2087 /* Objective-C++ Productions */
2089 static tree cp_parser_objc_message_receiver
2091 static tree cp_parser_objc_message_args
2093 static tree cp_parser_objc_message_expression
2095 static tree cp_parser_objc_encode_expression
2097 static tree cp_parser_objc_defs_expression
2099 static tree cp_parser_objc_protocol_expression
2101 static tree cp_parser_objc_selector_expression
2103 static tree cp_parser_objc_expression
2105 static bool cp_parser_objc_selector_p
2107 static tree cp_parser_objc_selector
2109 static tree cp_parser_objc_protocol_refs_opt
2111 static void cp_parser_objc_declaration
2112 (cp_parser *, tree);
2113 static tree cp_parser_objc_statement
2115 static bool cp_parser_objc_valid_prefix_attributes
2116 (cp_parser *, tree *);
2117 static void cp_parser_objc_at_property_declaration
2119 static void cp_parser_objc_at_synthesize_declaration
2121 static void cp_parser_objc_at_dynamic_declaration
2123 static tree cp_parser_objc_struct_declaration
2126 /* Utility Routines */
2128 static tree cp_parser_lookup_name
2129 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2130 static tree cp_parser_lookup_name_simple
2131 (cp_parser *, tree, location_t);
2132 static tree cp_parser_maybe_treat_template_as_class
2134 static bool cp_parser_check_declarator_template_parameters
2135 (cp_parser *, cp_declarator *, location_t);
2136 static bool cp_parser_check_template_parameters
2137 (cp_parser *, unsigned, location_t, cp_declarator *);
2138 static tree cp_parser_simple_cast_expression
2140 static tree cp_parser_global_scope_opt
2141 (cp_parser *, bool);
2142 static bool cp_parser_constructor_declarator_p
2143 (cp_parser *, bool);
2144 static tree cp_parser_function_definition_from_specifiers_and_declarator
2145 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2146 static tree cp_parser_function_definition_after_declarator
2147 (cp_parser *, bool);
2148 static void cp_parser_template_declaration_after_export
2149 (cp_parser *, bool);
2150 static void cp_parser_perform_template_parameter_access_checks
2151 (VEC (deferred_access_check,gc)*);
2152 static tree cp_parser_single_declaration
2153 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2154 static tree cp_parser_functional_cast
2155 (cp_parser *, tree);
2156 static tree cp_parser_save_member_function_body
2157 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2158 static tree cp_parser_enclosed_template_argument_list
2160 static void cp_parser_save_default_args
2161 (cp_parser *, tree);
2162 static void cp_parser_late_parsing_for_member
2163 (cp_parser *, tree);
2164 static void cp_parser_late_parsing_default_args
2165 (cp_parser *, tree);
2166 static tree cp_parser_sizeof_operand
2167 (cp_parser *, enum rid);
2168 static tree cp_parser_trait_expr
2169 (cp_parser *, enum rid);
2170 static bool cp_parser_declares_only_class_p
2172 static void cp_parser_set_storage_class
2173 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2174 static void cp_parser_set_decl_spec_type
2175 (cp_decl_specifier_seq *, tree, location_t, bool);
2176 static bool cp_parser_friend_p
2177 (const cp_decl_specifier_seq *);
2178 static void cp_parser_required_error
2179 (cp_parser *, required_token, bool);
2180 static cp_token *cp_parser_require
2181 (cp_parser *, enum cpp_ttype, required_token);
2182 static cp_token *cp_parser_require_keyword
2183 (cp_parser *, enum rid, required_token);
2184 static bool cp_parser_token_starts_function_definition_p
2186 static bool cp_parser_next_token_starts_class_definition_p
2188 static bool cp_parser_next_token_ends_template_argument_p
2190 static bool cp_parser_nth_token_starts_template_argument_list_p
2191 (cp_parser *, size_t);
2192 static enum tag_types cp_parser_token_is_class_key
2194 static void cp_parser_check_class_key
2195 (enum tag_types, tree type);
2196 static void cp_parser_check_access_in_redeclaration
2197 (tree type, location_t location);
2198 static bool cp_parser_optional_template_keyword
2200 static void cp_parser_pre_parsed_nested_name_specifier
2202 static bool cp_parser_cache_group
2203 (cp_parser *, enum cpp_ttype, unsigned);
2204 static void cp_parser_parse_tentatively
2206 static void cp_parser_commit_to_tentative_parse
2208 static void cp_parser_abort_tentative_parse
2210 static bool cp_parser_parse_definitely
2212 static inline bool cp_parser_parsing_tentatively
2214 static bool cp_parser_uncommitted_to_tentative_parse_p
2216 static void cp_parser_error
2217 (cp_parser *, const char *);
2218 static void cp_parser_name_lookup_error
2219 (cp_parser *, tree, tree, name_lookup_error, location_t);
2220 static bool cp_parser_simulate_error
2222 static bool cp_parser_check_type_definition
2224 static void cp_parser_check_for_definition_in_return_type
2225 (cp_declarator *, tree, location_t type_location);
2226 static void cp_parser_check_for_invalid_template_id
2227 (cp_parser *, tree, location_t location);
2228 static bool cp_parser_non_integral_constant_expression
2229 (cp_parser *, non_integral_constant);
2230 static void cp_parser_diagnose_invalid_type_name
2231 (cp_parser *, tree, tree, location_t);
2232 static bool cp_parser_parse_and_diagnose_invalid_type_name
2234 static int cp_parser_skip_to_closing_parenthesis
2235 (cp_parser *, bool, bool, bool);
2236 static void cp_parser_skip_to_end_of_statement
2238 static void cp_parser_consume_semicolon_at_end_of_statement
2240 static void cp_parser_skip_to_end_of_block_or_statement
2242 static bool cp_parser_skip_to_closing_brace
2244 static void cp_parser_skip_to_end_of_template_parameter_list
2246 static void cp_parser_skip_to_pragma_eol
2247 (cp_parser*, cp_token *);
2248 static bool cp_parser_error_occurred
2250 static bool cp_parser_allow_gnu_extensions_p
2252 static bool cp_parser_is_string_literal
2254 static bool cp_parser_is_keyword
2255 (cp_token *, enum rid);
2256 static tree cp_parser_make_typename_type
2257 (cp_parser *, tree, tree, location_t location);
2258 static cp_declarator * cp_parser_make_indirect_declarator
2259 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2261 /* Returns nonzero if we are parsing tentatively. */
2264 cp_parser_parsing_tentatively (cp_parser* parser)
2266 return parser->context->next != NULL;
2269 /* Returns nonzero if TOKEN is a string literal. */
2272 cp_parser_is_string_literal (cp_token* token)
2274 return (token->type == CPP_STRING ||
2275 token->type == CPP_STRING16 ||
2276 token->type == CPP_STRING32 ||
2277 token->type == CPP_WSTRING ||
2278 token->type == CPP_UTF8STRING);
2281 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2284 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2286 return token->keyword == keyword;
2289 /* If not parsing tentatively, issue a diagnostic of the form
2290 FILE:LINE: MESSAGE before TOKEN
2291 where TOKEN is the next token in the input stream. MESSAGE
2292 (specified by the caller) is usually of the form "expected
2296 cp_parser_error (cp_parser* parser, const char* gmsgid)
2298 if (!cp_parser_simulate_error (parser))
2300 cp_token *token = cp_lexer_peek_token (parser->lexer);
2301 /* This diagnostic makes more sense if it is tagged to the line
2302 of the token we just peeked at. */
2303 cp_lexer_set_source_position_from_token (token);
2305 if (token->type == CPP_PRAGMA)
2307 error_at (token->location,
2308 "%<#pragma%> is not allowed here");
2309 cp_parser_skip_to_pragma_eol (parser, token);
2313 c_parse_error (gmsgid,
2314 /* Because c_parser_error does not understand
2315 CPP_KEYWORD, keywords are treated like
2317 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2318 token->u.value, token->flags);
2322 /* Issue an error about name-lookup failing. NAME is the
2323 IDENTIFIER_NODE DECL is the result of
2324 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2325 the thing that we hoped to find. */
2328 cp_parser_name_lookup_error (cp_parser* parser,
2331 name_lookup_error desired,
2332 location_t location)
2334 /* If name lookup completely failed, tell the user that NAME was not
2336 if (decl == error_mark_node)
2338 if (parser->scope && parser->scope != global_namespace)
2339 error_at (location, "%<%E::%E%> has not been declared",
2340 parser->scope, name);
2341 else if (parser->scope == global_namespace)
2342 error_at (location, "%<::%E%> has not been declared", name);
2343 else if (parser->object_scope
2344 && !CLASS_TYPE_P (parser->object_scope))
2345 error_at (location, "request for member %qE in non-class type %qT",
2346 name, parser->object_scope);
2347 else if (parser->object_scope)
2348 error_at (location, "%<%T::%E%> has not been declared",
2349 parser->object_scope, name);
2351 error_at (location, "%qE has not been declared", name);
2353 else if (parser->scope && parser->scope != global_namespace)
2358 error_at (location, "%<%E::%E%> is not a type",
2359 parser->scope, name);
2362 error_at (location, "%<%E::%E%> is not a class or namespace",
2363 parser->scope, name);
2367 "%<%E::%E%> is not a class, namespace, or enumeration",
2368 parser->scope, name);
2375 else if (parser->scope == global_namespace)
2380 error_at (location, "%<::%E%> is not a type", name);
2383 error_at (location, "%<::%E%> is not a class or namespace", name);
2387 "%<::%E%> is not a class, namespace, or enumeration",
2399 error_at (location, "%qE is not a type", name);
2402 error_at (location, "%qE is not a class or namespace", name);
2406 "%qE is not a class, namespace, or enumeration", name);
2414 /* If we are parsing tentatively, remember that an error has occurred
2415 during this tentative parse. Returns true if the error was
2416 simulated; false if a message should be issued by the caller. */
2419 cp_parser_simulate_error (cp_parser* parser)
2421 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2423 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2429 /* Check for repeated decl-specifiers. */
2432 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2433 location_t location)
2437 for (ds = ds_first; ds != ds_last; ++ds)
2439 unsigned count = decl_specs->specs[ds];
2442 /* The "long" specifier is a special case because of "long long". */
2446 error_at (location, "%<long long long%> is too long for GCC");
2448 pedwarn_cxx98 (location, OPT_Wlong_long,
2449 "ISO C++ 1998 does not support %<long long%>");
2453 static const char *const decl_spec_names[] = {
2470 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2475 /* This function is called when a type is defined. If type
2476 definitions are forbidden at this point, an error message is
2480 cp_parser_check_type_definition (cp_parser* parser)
2482 /* If types are forbidden here, issue a message. */
2483 if (parser->type_definition_forbidden_message)
2485 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2486 in the message need to be interpreted. */
2487 error (parser->type_definition_forbidden_message);
2493 /* This function is called when the DECLARATOR is processed. The TYPE
2494 was a type defined in the decl-specifiers. If it is invalid to
2495 define a type in the decl-specifiers for DECLARATOR, an error is
2496 issued. TYPE_LOCATION is the location of TYPE and is used
2497 for error reporting. */
2500 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2501 tree type, location_t type_location)
2503 /* [dcl.fct] forbids type definitions in return types.
2504 Unfortunately, it's not easy to know whether or not we are
2505 processing a return type until after the fact. */
2507 && (declarator->kind == cdk_pointer
2508 || declarator->kind == cdk_reference
2509 || declarator->kind == cdk_ptrmem))
2510 declarator = declarator->declarator;
2512 && declarator->kind == cdk_function)
2514 error_at (type_location,
2515 "new types may not be defined in a return type");
2516 inform (type_location,
2517 "(perhaps a semicolon is missing after the definition of %qT)",
2522 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2523 "<" in any valid C++ program. If the next token is indeed "<",
2524 issue a message warning the user about what appears to be an
2525 invalid attempt to form a template-id. LOCATION is the location
2526 of the type-specifier (TYPE) */
2529 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2530 tree type, location_t location)
2532 cp_token_position start = 0;
2534 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2537 error_at (location, "%qT is not a template", type);
2538 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2539 error_at (location, "%qE is not a template", type);
2541 error_at (location, "invalid template-id");
2542 /* Remember the location of the invalid "<". */
2543 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2544 start = cp_lexer_token_position (parser->lexer, true);
2545 /* Consume the "<". */
2546 cp_lexer_consume_token (parser->lexer);
2547 /* Parse the template arguments. */
2548 cp_parser_enclosed_template_argument_list (parser);
2549 /* Permanently remove the invalid template arguments so that
2550 this error message is not issued again. */
2552 cp_lexer_purge_tokens_after (parser->lexer, start);
2556 /* If parsing an integral constant-expression, issue an error message
2557 about the fact that THING appeared and return true. Otherwise,
2558 return false. In either case, set
2559 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2562 cp_parser_non_integral_constant_expression (cp_parser *parser,
2563 non_integral_constant thing)
2565 parser->non_integral_constant_expression_p = true;
2566 if (parser->integral_constant_expression_p)
2568 if (!parser->allow_non_integral_constant_expression_p)
2570 const char *msg = NULL;
2574 error ("floating-point literal "
2575 "cannot appear in a constant-expression");
2578 error ("a cast to a type other than an integral or "
2579 "enumeration type cannot appear in a "
2580 "constant-expression");
2583 error ("%<typeid%> operator "
2584 "cannot appear in a constant-expression");
2587 error ("non-constant compound literals "
2588 "cannot appear in a constant-expression");
2591 error ("a function call "
2592 "cannot appear in a constant-expression");
2595 error ("an increment "
2596 "cannot appear in a constant-expression");
2599 error ("an decrement "
2600 "cannot appear in a constant-expression");
2603 error ("an array reference "
2604 "cannot appear in a constant-expression");
2606 case NIC_ADDR_LABEL:
2607 error ("the address of a label "
2608 "cannot appear in a constant-expression");
2610 case NIC_OVERLOADED:
2611 error ("calls to overloaded operators "
2612 "cannot appear in a constant-expression");
2614 case NIC_ASSIGNMENT:
2615 error ("an assignment cannot appear in a constant-expression");
2618 error ("a comma operator "
2619 "cannot appear in a constant-expression");
2621 case NIC_CONSTRUCTOR:
2622 error ("a call to a constructor "
2623 "cannot appear in a constant-expression");
2629 msg = "__FUNCTION__";
2631 case NIC_PRETTY_FUNC:
2632 msg = "__PRETTY_FUNCTION__";
2652 case NIC_PREINCREMENT:
2655 case NIC_PREDECREMENT:
2668 error ("%qs cannot appear in a constant-expression", msg);
2675 /* Emit a diagnostic for an invalid type name. SCOPE is the
2676 qualifying scope (or NULL, if none) for ID. This function commits
2677 to the current active tentative parse, if any. (Otherwise, the
2678 problematic construct might be encountered again later, resulting
2679 in duplicate error messages.) LOCATION is the location of ID. */
2682 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2683 tree scope, tree id,
2684 location_t location)
2686 tree decl, old_scope;
2687 /* Try to lookup the identifier. */
2688 old_scope = parser->scope;
2689 parser->scope = scope;
2690 decl = cp_parser_lookup_name_simple (parser, id, location);
2691 parser->scope = old_scope;
2692 /* If the lookup found a template-name, it means that the user forgot
2693 to specify an argument list. Emit a useful error message. */
2694 if (TREE_CODE (decl) == TEMPLATE_DECL)
2696 "invalid use of template-name %qE without an argument list",
2698 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2699 error_at (location, "invalid use of destructor %qD as a type", id);
2700 else if (TREE_CODE (decl) == TYPE_DECL)
2701 /* Something like 'unsigned A a;' */
2702 error_at (location, "invalid combination of multiple type-specifiers");
2703 else if (!parser->scope)
2705 /* Issue an error message. */
2706 error_at (location, "%qE does not name a type", id);
2707 /* If we're in a template class, it's possible that the user was
2708 referring to a type from a base class. For example:
2710 template <typename T> struct A { typedef T X; };
2711 template <typename T> struct B : public A<T> { X x; };
2713 The user should have said "typename A<T>::X". */
2714 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2715 inform (location, "C++0x %<constexpr%> only available with "
2716 "-std=c++0x or -std=gnu++0x");
2717 else if (processing_template_decl && current_class_type
2718 && TYPE_BINFO (current_class_type))
2722 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2726 tree base_type = BINFO_TYPE (b);
2727 if (CLASS_TYPE_P (base_type)
2728 && dependent_type_p (base_type))
2731 /* Go from a particular instantiation of the
2732 template (which will have an empty TYPE_FIELDs),
2733 to the main version. */
2734 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2735 for (field = TYPE_FIELDS (base_type);
2737 field = DECL_CHAIN (field))
2738 if (TREE_CODE (field) == TYPE_DECL
2739 && DECL_NAME (field) == id)
2742 "(perhaps %<typename %T::%E%> was intended)",
2743 BINFO_TYPE (b), id);
2752 /* Here we diagnose qualified-ids where the scope is actually correct,
2753 but the identifier does not resolve to a valid type name. */
2754 else if (parser->scope != error_mark_node)
2756 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2757 error_at (location, "%qE in namespace %qE does not name a type",
2759 else if (CLASS_TYPE_P (parser->scope)
2760 && constructor_name_p (id, parser->scope))
2763 error_at (location, "%<%T::%E%> names the constructor, not"
2764 " the type", parser->scope, id);
2765 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2766 error_at (location, "and %qT has no template constructors",
2769 else if (TYPE_P (parser->scope)
2770 && dependent_scope_p (parser->scope))
2771 error_at (location, "need %<typename%> before %<%T::%E%> because "
2772 "%qT is a dependent scope",
2773 parser->scope, id, parser->scope);
2774 else if (TYPE_P (parser->scope))
2775 error_at (location, "%qE in class %qT does not name a type",
2780 cp_parser_commit_to_tentative_parse (parser);
2783 /* Check for a common situation where a type-name should be present,
2784 but is not, and issue a sensible error message. Returns true if an
2785 invalid type-name was detected.
2787 The situation handled by this function are variable declarations of the
2788 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2789 Usually, `ID' should name a type, but if we got here it means that it
2790 does not. We try to emit the best possible error message depending on
2791 how exactly the id-expression looks like. */
2794 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2797 cp_token *token = cp_lexer_peek_token (parser->lexer);
2799 /* Avoid duplicate error about ambiguous lookup. */
2800 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2802 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2803 if (next->type == CPP_NAME && next->ambiguous_p)
2807 cp_parser_parse_tentatively (parser);
2808 id = cp_parser_id_expression (parser,
2809 /*template_keyword_p=*/false,
2810 /*check_dependency_p=*/true,
2811 /*template_p=*/NULL,
2812 /*declarator_p=*/true,
2813 /*optional_p=*/false);
2814 /* If the next token is a (, this is a function with no explicit return
2815 type, i.e. constructor, destructor or conversion op. */
2816 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2817 || TREE_CODE (id) == TYPE_DECL)
2819 cp_parser_abort_tentative_parse (parser);
2822 if (!cp_parser_parse_definitely (parser))
2825 /* Emit a diagnostic for the invalid type. */
2826 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2827 id, token->location);
2829 /* If we aren't in the middle of a declarator (i.e. in a
2830 parameter-declaration-clause), skip to the end of the declaration;
2831 there's no point in trying to process it. */
2832 if (!parser->in_declarator_p)
2833 cp_parser_skip_to_end_of_block_or_statement (parser);
2837 /* Consume tokens up to, and including, the next non-nested closing `)'.
2838 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2839 are doing error recovery. Returns -1 if OR_COMMA is true and we
2840 found an unnested comma. */
2843 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2848 unsigned paren_depth = 0;
2849 unsigned brace_depth = 0;
2850 unsigned square_depth = 0;
2852 if (recovering && !or_comma
2853 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2858 cp_token * token = cp_lexer_peek_token (parser->lexer);
2860 switch (token->type)
2863 case CPP_PRAGMA_EOL:
2864 /* If we've run out of tokens, then there is no closing `)'. */
2867 /* This is good for lambda expression capture-lists. */
2868 case CPP_OPEN_SQUARE:
2871 case CPP_CLOSE_SQUARE:
2872 if (!square_depth--)
2877 /* This matches the processing in skip_to_end_of_statement. */
2882 case CPP_OPEN_BRACE:
2885 case CPP_CLOSE_BRACE:
2891 if (recovering && or_comma && !brace_depth && !paren_depth
2896 case CPP_OPEN_PAREN:
2901 case CPP_CLOSE_PAREN:
2902 if (!brace_depth && !paren_depth--)
2905 cp_lexer_consume_token (parser->lexer);
2914 /* Consume the token. */
2915 cp_lexer_consume_token (parser->lexer);
2919 /* Consume tokens until we reach the end of the current statement.
2920 Normally, that will be just before consuming a `;'. However, if a
2921 non-nested `}' comes first, then we stop before consuming that. */
2924 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2926 unsigned nesting_depth = 0;
2930 cp_token *token = cp_lexer_peek_token (parser->lexer);
2932 switch (token->type)
2935 case CPP_PRAGMA_EOL:
2936 /* If we've run out of tokens, stop. */
2940 /* If the next token is a `;', we have reached the end of the
2946 case CPP_CLOSE_BRACE:
2947 /* If this is a non-nested '}', stop before consuming it.
2948 That way, when confronted with something like:
2952 we stop before consuming the closing '}', even though we
2953 have not yet reached a `;'. */
2954 if (nesting_depth == 0)
2957 /* If it is the closing '}' for a block that we have
2958 scanned, stop -- but only after consuming the token.
2964 we will stop after the body of the erroneously declared
2965 function, but before consuming the following `typedef'
2967 if (--nesting_depth == 0)
2969 cp_lexer_consume_token (parser->lexer);
2973 case CPP_OPEN_BRACE:
2981 /* Consume the token. */
2982 cp_lexer_consume_token (parser->lexer);
2986 /* This function is called at the end of a statement or declaration.
2987 If the next token is a semicolon, it is consumed; otherwise, error
2988 recovery is attempted. */
2991 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2993 /* Look for the trailing `;'. */
2994 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2996 /* If there is additional (erroneous) input, skip to the end of
2998 cp_parser_skip_to_end_of_statement (parser);
2999 /* If the next token is now a `;', consume it. */
3000 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3001 cp_lexer_consume_token (parser->lexer);
3005 /* Skip tokens until we have consumed an entire block, or until we
3006 have consumed a non-nested `;'. */
3009 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3011 int nesting_depth = 0;
3013 while (nesting_depth >= 0)
3015 cp_token *token = cp_lexer_peek_token (parser->lexer);
3017 switch (token->type)
3020 case CPP_PRAGMA_EOL:
3021 /* If we've run out of tokens, stop. */
3025 /* Stop if this is an unnested ';'. */
3030 case CPP_CLOSE_BRACE:
3031 /* Stop if this is an unnested '}', or closes the outermost
3034 if (nesting_depth < 0)
3040 case CPP_OPEN_BRACE:
3049 /* Consume the token. */
3050 cp_lexer_consume_token (parser->lexer);
3054 /* Skip tokens until a non-nested closing curly brace is the next
3055 token, or there are no more tokens. Return true in the first case,
3059 cp_parser_skip_to_closing_brace (cp_parser *parser)
3061 unsigned nesting_depth = 0;
3065 cp_token *token = cp_lexer_peek_token (parser->lexer);
3067 switch (token->type)
3070 case CPP_PRAGMA_EOL:
3071 /* If we've run out of tokens, stop. */
3074 case CPP_CLOSE_BRACE:
3075 /* If the next token is a non-nested `}', then we have reached
3076 the end of the current block. */
3077 if (nesting_depth-- == 0)
3081 case CPP_OPEN_BRACE:
3082 /* If it the next token is a `{', then we are entering a new
3083 block. Consume the entire block. */
3091 /* Consume the token. */
3092 cp_lexer_consume_token (parser->lexer);
3096 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3097 parameter is the PRAGMA token, allowing us to purge the entire pragma
3101 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3105 parser->lexer->in_pragma = false;
3108 token = cp_lexer_consume_token (parser->lexer);
3109 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3111 /* Ensure that the pragma is not parsed again. */
3112 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3115 /* Require pragma end of line, resyncing with it as necessary. The
3116 arguments are as for cp_parser_skip_to_pragma_eol. */
3119 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3121 parser->lexer->in_pragma = false;
3122 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3126 /* This is a simple wrapper around make_typename_type. When the id is
3127 an unresolved identifier node, we can provide a superior diagnostic
3128 using cp_parser_diagnose_invalid_type_name. */
3131 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3132 tree id, location_t id_location)
3135 if (TREE_CODE (id) == IDENTIFIER_NODE)
3137 result = make_typename_type (scope, id, typename_type,
3138 /*complain=*/tf_none);
3139 if (result == error_mark_node)
3140 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3143 return make_typename_type (scope, id, typename_type, tf_error);
3146 /* This is a wrapper around the
3147 make_{pointer,ptrmem,reference}_declarator functions that decides
3148 which one to call based on the CODE and CLASS_TYPE arguments. The
3149 CODE argument should be one of the values returned by
3150 cp_parser_ptr_operator. */
3151 static cp_declarator *
3152 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3153 cp_cv_quals cv_qualifiers,
3154 cp_declarator *target)
3156 if (code == ERROR_MARK)
3157 return cp_error_declarator;
3159 if (code == INDIRECT_REF)
3160 if (class_type == NULL_TREE)
3161 return make_pointer_declarator (cv_qualifiers, target);
3163 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3164 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3165 return make_reference_declarator (cv_qualifiers, target, false);
3166 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3167 return make_reference_declarator (cv_qualifiers, target, true);
3171 /* Create a new C++ parser. */
3174 cp_parser_new (void)
3180 /* cp_lexer_new_main is called before doing GC allocation because
3181 cp_lexer_new_main might load a PCH file. */
3182 lexer = cp_lexer_new_main ();
3184 /* Initialize the binops_by_token so that we can get the tree
3185 directly from the token. */
3186 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3187 binops_by_token[binops[i].token_type] = binops[i];
3189 parser = ggc_alloc_cleared_cp_parser ();
3190 parser->lexer = lexer;
3191 parser->context = cp_parser_context_new (NULL);
3193 /* For now, we always accept GNU extensions. */
3194 parser->allow_gnu_extensions_p = 1;
3196 /* The `>' token is a greater-than operator, not the end of a
3198 parser->greater_than_is_operator_p = true;
3200 parser->default_arg_ok_p = true;
3202 /* We are not parsing a constant-expression. */
3203 parser->integral_constant_expression_p = false;
3204 parser->allow_non_integral_constant_expression_p = false;
3205 parser->non_integral_constant_expression_p = false;
3207 /* Local variable names are not forbidden. */
3208 parser->local_variables_forbidden_p = false;
3210 /* We are not processing an `extern "C"' declaration. */
3211 parser->in_unbraced_linkage_specification_p = false;
3213 /* We are not processing a declarator. */
3214 parser->in_declarator_p = false;
3216 /* We are not processing a template-argument-list. */
3217 parser->in_template_argument_list_p = false;
3219 /* We are not in an iteration statement. */
3220 parser->in_statement = 0;
3222 /* We are not in a switch statement. */
3223 parser->in_switch_statement_p = false;
3225 /* We are not parsing a type-id inside an expression. */
3226 parser->in_type_id_in_expr_p = false;
3228 /* Declarations aren't implicitly extern "C". */
3229 parser->implicit_extern_c = false;
3231 /* String literals should be translated to the execution character set. */
3232 parser->translate_strings_p = true;
3234 /* We are not parsing a function body. */
3235 parser->in_function_body = false;
3237 /* The unparsed function queue is empty. */
3238 push_unparsed_function_queues (parser);
3240 /* There are no classes being defined. */
3241 parser->num_classes_being_defined = 0;
3243 /* No template parameters apply. */
3244 parser->num_template_parameter_lists = 0;
3249 /* Create a cp_lexer structure which will emit the tokens in CACHE
3250 and push it onto the parser's lexer stack. This is used for delayed
3251 parsing of in-class method bodies and default arguments, and should
3252 not be confused with tentative parsing. */
3254 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3256 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3257 lexer->next = parser->lexer;
3258 parser->lexer = lexer;
3260 /* Move the current source position to that of the first token in the
3262 cp_lexer_set_source_position_from_token (lexer->next_token);
3265 /* Pop the top lexer off the parser stack. This is never used for the
3266 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3268 cp_parser_pop_lexer (cp_parser *parser)
3270 cp_lexer *lexer = parser->lexer;
3271 parser->lexer = lexer->next;
3272 cp_lexer_destroy (lexer);
3274 /* Put the current source position back where it was before this
3275 lexer was pushed. */
3276 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3279 /* Lexical conventions [gram.lex] */
3281 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3285 cp_parser_identifier (cp_parser* parser)
3289 /* Look for the identifier. */
3290 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3291 /* Return the value. */
3292 return token ? token->u.value : error_mark_node;
3295 /* Parse a sequence of adjacent string constants. Returns a
3296 TREE_STRING representing the combined, nul-terminated string
3297 constant. If TRANSLATE is true, translate the string to the
3298 execution character set. If WIDE_OK is true, a wide string is
3301 C++98 [lex.string] says that if a narrow string literal token is
3302 adjacent to a wide string literal token, the behavior is undefined.
3303 However, C99 6.4.5p4 says that this results in a wide string literal.
3304 We follow C99 here, for consistency with the C front end.
3306 This code is largely lifted from lex_string() in c-lex.c.
3308 FUTURE: ObjC++ will need to handle @-strings here. */
3310 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3314 struct obstack str_ob;
3315 cpp_string str, istr, *strs;
3317 enum cpp_ttype type;
3319 tok = cp_lexer_peek_token (parser->lexer);
3320 if (!cp_parser_is_string_literal (tok))
3322 cp_parser_error (parser, "expected string-literal");
3323 return error_mark_node;
3328 /* Try to avoid the overhead of creating and destroying an obstack
3329 for the common case of just one string. */
3330 if (!cp_parser_is_string_literal
3331 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3333 cp_lexer_consume_token (parser->lexer);
3335 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3336 str.len = TREE_STRING_LENGTH (tok->u.value);
3343 gcc_obstack_init (&str_ob);
3348 cp_lexer_consume_token (parser->lexer);
3350 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3351 str.len = TREE_STRING_LENGTH (tok->u.value);
3353 if (type != tok->type)
3355 if (type == CPP_STRING)
3357 else if (tok->type != CPP_STRING)
3358 error_at (tok->location,
3359 "unsupported non-standard concatenation "
3360 "of string literals");
3363 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3365 tok = cp_lexer_peek_token (parser->lexer);
3367 while (cp_parser_is_string_literal (tok));
3369 strs = (cpp_string *) obstack_finish (&str_ob);
3372 if (type != CPP_STRING && !wide_ok)
3374 cp_parser_error (parser, "a wide string is invalid in this context");
3378 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3379 (parse_in, strs, count, &istr, type))
3381 value = build_string (istr.len, (const char *)istr.text);
3382 free (CONST_CAST (unsigned char *, istr.text));
3388 case CPP_UTF8STRING:
3389 TREE_TYPE (value) = char_array_type_node;
3392 TREE_TYPE (value) = char16_array_type_node;
3395 TREE_TYPE (value) = char32_array_type_node;
3398 TREE_TYPE (value) = wchar_array_type_node;
3402 value = fix_string_type (value);
3405 /* cpp_interpret_string has issued an error. */
3406 value = error_mark_node;
3409 obstack_free (&str_ob, 0);
3415 /* Basic concepts [gram.basic] */
3417 /* Parse a translation-unit.
3420 declaration-seq [opt]
3422 Returns TRUE if all went well. */
3425 cp_parser_translation_unit (cp_parser* parser)
3427 /* The address of the first non-permanent object on the declarator
3429 static void *declarator_obstack_base;
3433 /* Create the declarator obstack, if necessary. */
3434 if (!cp_error_declarator)
3436 gcc_obstack_init (&declarator_obstack);
3437 /* Create the error declarator. */
3438 cp_error_declarator = make_declarator (cdk_error);
3439 /* Create the empty parameter list. */
3440 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3441 /* Remember where the base of the declarator obstack lies. */
3442 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3445 cp_parser_declaration_seq_opt (parser);
3447 /* If there are no tokens left then all went well. */
3448 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3450 /* Get rid of the token array; we don't need it any more. */
3451 cp_lexer_destroy (parser->lexer);
3452 parser->lexer = NULL;
3454 /* This file might have been a context that's implicitly extern
3455 "C". If so, pop the lang context. (Only relevant for PCH.) */
3456 if (parser->implicit_extern_c)
3458 pop_lang_context ();
3459 parser->implicit_extern_c = false;
3463 finish_translation_unit ();
3469 cp_parser_error (parser, "expected declaration");
3473 /* Make sure the declarator obstack was fully cleaned up. */
3474 gcc_assert (obstack_next_free (&declarator_obstack)
3475 == declarator_obstack_base);
3477 /* All went well. */
3481 /* Expressions [gram.expr] */
3483 /* Parse a primary-expression.
3494 ( compound-statement )
3495 __builtin_va_arg ( assignment-expression , type-id )
3496 __builtin_offsetof ( type-id , offsetof-expression )
3499 __has_nothrow_assign ( type-id )
3500 __has_nothrow_constructor ( type-id )
3501 __has_nothrow_copy ( type-id )
3502 __has_trivial_assign ( type-id )
3503 __has_trivial_constructor ( type-id )
3504 __has_trivial_copy ( type-id )
3505 __has_trivial_destructor ( type-id )
3506 __has_virtual_destructor ( type-id )
3507 __is_abstract ( type-id )
3508 __is_base_of ( type-id , type-id )
3509 __is_class ( type-id )
3510 __is_convertible_to ( type-id , type-id )
3511 __is_empty ( type-id )
3512 __is_enum ( type-id )
3513 __is_pod ( type-id )
3514 __is_polymorphic ( type-id )
3515 __is_union ( type-id )
3517 Objective-C++ Extension:
3525 ADDRESS_P is true iff this expression was immediately preceded by
3526 "&" and therefore might denote a pointer-to-member. CAST_P is true
3527 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3528 true iff this expression is a template argument.
3530 Returns a representation of the expression. Upon return, *IDK
3531 indicates what kind of id-expression (if any) was present. */
3534 cp_parser_primary_expression (cp_parser *parser,
3537 bool template_arg_p,
3540 cp_token *token = NULL;
3542 /* Assume the primary expression is not an id-expression. */
3543 *idk = CP_ID_KIND_NONE;
3545 /* Peek at the next token. */
3546 token = cp_lexer_peek_token (parser->lexer);
3547 switch (token->type)
3560 token = cp_lexer_consume_token (parser->lexer);
3561 if (TREE_CODE (token->u.value) == FIXED_CST)
3563 error_at (token->location,
3564 "fixed-point types not supported in C++");
3565 return error_mark_node;
3567 /* Floating-point literals are only allowed in an integral
3568 constant expression if they are cast to an integral or
3569 enumeration type. */
3570 if (TREE_CODE (token->u.value) == REAL_CST
3571 && parser->integral_constant_expression_p
3574 /* CAST_P will be set even in invalid code like "int(2.7 +
3575 ...)". Therefore, we have to check that the next token
3576 is sure to end the cast. */
3579 cp_token *next_token;
3581 next_token = cp_lexer_peek_token (parser->lexer);
3582 if (/* The comma at the end of an
3583 enumerator-definition. */
3584 next_token->type != CPP_COMMA
3585 /* The curly brace at the end of an enum-specifier. */
3586 && next_token->type != CPP_CLOSE_BRACE
3587 /* The end of a statement. */
3588 && next_token->type != CPP_SEMICOLON
3589 /* The end of the cast-expression. */
3590 && next_token->type != CPP_CLOSE_PAREN
3591 /* The end of an array bound. */
3592 && next_token->type != CPP_CLOSE_SQUARE
3593 /* The closing ">" in a template-argument-list. */
3594 && (next_token->type != CPP_GREATER
3595 || parser->greater_than_is_operator_p)
3596 /* C++0x only: A ">>" treated like two ">" tokens,
3597 in a template-argument-list. */
3598 && (next_token->type != CPP_RSHIFT
3599 || (cxx_dialect == cxx98)
3600 || parser->greater_than_is_operator_p))
3604 /* If we are within a cast, then the constraint that the
3605 cast is to an integral or enumeration type will be
3606 checked at that point. If we are not within a cast, then
3607 this code is invalid. */
3609 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3611 return token->u.value;
3617 case CPP_UTF8STRING:
3618 /* ??? Should wide strings be allowed when parser->translate_strings_p
3619 is false (i.e. in attributes)? If not, we can kill the third
3620 argument to cp_parser_string_literal. */
3621 return cp_parser_string_literal (parser,
3622 parser->translate_strings_p,
3625 case CPP_OPEN_PAREN:
3628 bool saved_greater_than_is_operator_p;
3630 /* Consume the `('. */
3631 cp_lexer_consume_token (parser->lexer);
3632 /* Within a parenthesized expression, a `>' token is always
3633 the greater-than operator. */
3634 saved_greater_than_is_operator_p
3635 = parser->greater_than_is_operator_p;
3636 parser->greater_than_is_operator_p = true;
3637 /* If we see `( { ' then we are looking at the beginning of
3638 a GNU statement-expression. */
3639 if (cp_parser_allow_gnu_extensions_p (parser)
3640 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3642 /* Statement-expressions are not allowed by the standard. */
3643 pedwarn (token->location, OPT_pedantic,
3644 "ISO C++ forbids braced-groups within expressions");
3646 /* And they're not allowed outside of a function-body; you
3647 cannot, for example, write:
3649 int i = ({ int j = 3; j + 1; });
3651 at class or namespace scope. */
3652 if (!parser->in_function_body
3653 || parser->in_template_argument_list_p)
3655 error_at (token->location,
3656 "statement-expressions are not allowed outside "
3657 "functions nor in template-argument lists");
3658 cp_parser_skip_to_end_of_block_or_statement (parser);
3659 expr = error_mark_node;
3663 /* Start the statement-expression. */
3664 expr = begin_stmt_expr ();
3665 /* Parse the compound-statement. */
3666 cp_parser_compound_statement (parser, expr, false);
3668 expr = finish_stmt_expr (expr, false);
3673 /* Parse the parenthesized expression. */
3674 expr = cp_parser_expression (parser, cast_p, idk);
3675 /* Let the front end know that this expression was
3676 enclosed in parentheses. This matters in case, for
3677 example, the expression is of the form `A::B', since
3678 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3680 finish_parenthesized_expr (expr);
3682 /* The `>' token might be the end of a template-id or
3683 template-parameter-list now. */
3684 parser->greater_than_is_operator_p
3685 = saved_greater_than_is_operator_p;
3686 /* Consume the `)'. */
3687 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3688 cp_parser_skip_to_end_of_statement (parser);
3693 case CPP_OPEN_SQUARE:
3694 if (c_dialect_objc ())
3695 /* We have an Objective-C++ message. */
3696 return cp_parser_objc_expression (parser);
3697 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3698 return cp_parser_lambda_expression (parser);
3700 case CPP_OBJC_STRING:
3701 if (c_dialect_objc ())
3702 /* We have an Objective-C++ string literal. */
3703 return cp_parser_objc_expression (parser);
3704 cp_parser_error (parser, "expected primary-expression");
3705 return error_mark_node;
3708 switch (token->keyword)
3710 /* These two are the boolean literals. */
3712 cp_lexer_consume_token (parser->lexer);
3713 return boolean_true_node;
3715 cp_lexer_consume_token (parser->lexer);
3716 return boolean_false_node;
3718 /* The `__null' literal. */
3720 cp_lexer_consume_token (parser->lexer);
3723 /* The `nullptr' literal. */
3725 cp_lexer_consume_token (parser->lexer);
3726 return nullptr_node;
3728 /* Recognize the `this' keyword. */
3730 cp_lexer_consume_token (parser->lexer);
3731 if (parser->local_variables_forbidden_p)
3733 error_at (token->location,
3734 "%<this%> may not be used in this context");
3735 return error_mark_node;
3737 /* Pointers cannot appear in constant-expressions. */
3738 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3739 return error_mark_node;
3740 return finish_this_expr ();
3742 /* The `operator' keyword can be the beginning of an
3747 case RID_FUNCTION_NAME:
3748 case RID_PRETTY_FUNCTION_NAME:
3749 case RID_C99_FUNCTION_NAME:
3751 non_integral_constant name;
3753 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3754 __func__ are the names of variables -- but they are
3755 treated specially. Therefore, they are handled here,
3756 rather than relying on the generic id-expression logic
3757 below. Grammatically, these names are id-expressions.
3759 Consume the token. */
3760 token = cp_lexer_consume_token (parser->lexer);
3762 switch (token->keyword)
3764 case RID_FUNCTION_NAME:
3765 name = NIC_FUNC_NAME;
3767 case RID_PRETTY_FUNCTION_NAME:
3768 name = NIC_PRETTY_FUNC;
3770 case RID_C99_FUNCTION_NAME:
3771 name = NIC_C99_FUNC;
3777 if (cp_parser_non_integral_constant_expression (parser, name))
3778 return error_mark_node;
3780 /* Look up the name. */
3781 return finish_fname (token->u.value);
3789 /* The `__builtin_va_arg' construct is used to handle
3790 `va_arg'. Consume the `__builtin_va_arg' token. */
3791 cp_lexer_consume_token (parser->lexer);
3792 /* Look for the opening `('. */
3793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3794 /* Now, parse the assignment-expression. */
3795 expression = cp_parser_assignment_expression (parser,
3796 /*cast_p=*/false, NULL);
3797 /* Look for the `,'. */
3798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3799 /* Parse the type-id. */
3800 type = cp_parser_type_id (parser);
3801 /* Look for the closing `)'. */
3802 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3803 /* Using `va_arg' in a constant-expression is not
3805 if (cp_parser_non_integral_constant_expression (parser,
3807 return error_mark_node;
3808 return build_x_va_arg (expression, type);
3812 return cp_parser_builtin_offsetof (parser);
3814 case RID_HAS_NOTHROW_ASSIGN:
3815 case RID_HAS_NOTHROW_CONSTRUCTOR:
3816 case RID_HAS_NOTHROW_COPY:
3817 case RID_HAS_TRIVIAL_ASSIGN:
3818 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3819 case RID_HAS_TRIVIAL_COPY:
3820 case RID_HAS_TRIVIAL_DESTRUCTOR:
3821 case RID_HAS_VIRTUAL_DESTRUCTOR:
3822 case RID_IS_ABSTRACT:
3823 case RID_IS_BASE_OF:
3825 case RID_IS_CONVERTIBLE_TO:
3829 case RID_IS_POLYMORPHIC:
3830 case RID_IS_STD_LAYOUT:
3831 case RID_IS_TRIVIAL:
3833 case RID_IS_LITERAL_TYPE:
3834 return cp_parser_trait_expr (parser, token->keyword);
3836 /* Objective-C++ expressions. */
3838 case RID_AT_PROTOCOL:
3839 case RID_AT_SELECTOR:
3840 return cp_parser_objc_expression (parser);
3843 if (parser->in_function_body
3844 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3847 error_at (token->location,
3848 "a template declaration cannot appear at block scope");
3849 cp_parser_skip_to_end_of_block_or_statement (parser);
3850 return error_mark_node;
3853 cp_parser_error (parser, "expected primary-expression");
3854 return error_mark_node;
3857 /* An id-expression can start with either an identifier, a
3858 `::' as the beginning of a qualified-id, or the "operator"
3862 case CPP_TEMPLATE_ID:
3863 case CPP_NESTED_NAME_SPECIFIER:
3867 const char *error_msg;
3870 cp_token *id_expr_token;
3873 /* Parse the id-expression. */
3875 = cp_parser_id_expression (parser,
3876 /*template_keyword_p=*/false,
3877 /*check_dependency_p=*/true,
3879 /*declarator_p=*/false,
3880 /*optional_p=*/false);
3881 if (id_expression == error_mark_node)
3882 return error_mark_node;
3883 id_expr_token = token;
3884 token = cp_lexer_peek_token (parser->lexer);
3885 done = (token->type != CPP_OPEN_SQUARE
3886 && token->type != CPP_OPEN_PAREN
3887 && token->type != CPP_DOT
3888 && token->type != CPP_DEREF
3889 && token->type != CPP_PLUS_PLUS
3890 && token->type != CPP_MINUS_MINUS);
3891 /* If we have a template-id, then no further lookup is
3892 required. If the template-id was for a template-class, we
3893 will sometimes have a TYPE_DECL at this point. */
3894 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3895 || TREE_CODE (id_expression) == TYPE_DECL)
3896 decl = id_expression;
3897 /* Look up the name. */
3900 tree ambiguous_decls;
3902 /* If we already know that this lookup is ambiguous, then
3903 we've already issued an error message; there's no reason
3905 if (id_expr_token->type == CPP_NAME
3906 && id_expr_token->ambiguous_p)
3908 cp_parser_simulate_error (parser);
3909 return error_mark_node;
3912 decl = cp_parser_lookup_name (parser, id_expression,
3915 /*is_namespace=*/false,
3916 /*check_dependency=*/true,
3918 id_expr_token->location);
3919 /* If the lookup was ambiguous, an error will already have
3921 if (ambiguous_decls)
3922 return error_mark_node;
3924 /* In Objective-C++, we may have an Objective-C 2.0
3925 dot-syntax for classes here. */
3926 if (c_dialect_objc ()
3927 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3928 && TREE_CODE (decl) == TYPE_DECL
3929 && objc_is_class_name (decl))
3932 cp_lexer_consume_token (parser->lexer);
3933 component = cp_parser_identifier (parser);
3934 if (component == error_mark_node)
3935 return error_mark_node;
3937 return objc_build_class_component_ref (id_expression, component);
3940 /* In Objective-C++, an instance variable (ivar) may be preferred
3941 to whatever cp_parser_lookup_name() found. */
3942 decl = objc_lookup_ivar (decl, id_expression);
3944 /* If name lookup gives us a SCOPE_REF, then the
3945 qualifying scope was dependent. */
3946 if (TREE_CODE (decl) == SCOPE_REF)
3948 /* At this point, we do not know if DECL is a valid
3949 integral constant expression. We assume that it is
3950 in fact such an expression, so that code like:
3952 template <int N> struct A {
3956 is accepted. At template-instantiation time, we
3957 will check that B<N>::i is actually a constant. */
3960 /* Check to see if DECL is a local variable in a context
3961 where that is forbidden. */
3962 if (parser->local_variables_forbidden_p
3963 && local_variable_p (decl))
3965 /* It might be that we only found DECL because we are
3966 trying to be generous with pre-ISO scoping rules.
3967 For example, consider:
3971 for (int i = 0; i < 10; ++i) {}
3972 extern void f(int j = i);
3975 Here, name look up will originally find the out
3976 of scope `i'. We need to issue a warning message,
3977 but then use the global `i'. */
3978 decl = check_for_out_of_scope_variable (decl);
3979 if (local_variable_p (decl))
3981 error_at (id_expr_token->location,
3982 "local variable %qD may not appear in this context",
3984 return error_mark_node;
3989 decl = (finish_id_expression
3990 (id_expression, decl, parser->scope,
3992 parser->integral_constant_expression_p,
3993 parser->allow_non_integral_constant_expression_p,
3994 &parser->non_integral_constant_expression_p,
3995 template_p, done, address_p,
3998 id_expr_token->location));
4000 cp_parser_error (parser, error_msg);
4004 /* Anything else is an error. */
4006 cp_parser_error (parser, "expected primary-expression");
4007 return error_mark_node;
4011 /* Parse an id-expression.
4018 :: [opt] nested-name-specifier template [opt] unqualified-id
4020 :: operator-function-id
4023 Return a representation of the unqualified portion of the
4024 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4025 a `::' or nested-name-specifier.
4027 Often, if the id-expression was a qualified-id, the caller will
4028 want to make a SCOPE_REF to represent the qualified-id. This
4029 function does not do this in order to avoid wastefully creating
4030 SCOPE_REFs when they are not required.
4032 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4035 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4036 uninstantiated templates.
4038 If *TEMPLATE_P is non-NULL, it is set to true iff the
4039 `template' keyword is used to explicitly indicate that the entity
4040 named is a template.
4042 If DECLARATOR_P is true, the id-expression is appearing as part of
4043 a declarator, rather than as part of an expression. */
4046 cp_parser_id_expression (cp_parser *parser,
4047 bool template_keyword_p,
4048 bool check_dependency_p,
4053 bool global_scope_p;
4054 bool nested_name_specifier_p;
4056 /* Assume the `template' keyword was not used. */
4058 *template_p = template_keyword_p;
4060 /* Look for the optional `::' operator. */
4062 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4064 /* Look for the optional nested-name-specifier. */
4065 nested_name_specifier_p
4066 = (cp_parser_nested_name_specifier_opt (parser,
4067 /*typename_keyword_p=*/false,
4072 /* If there is a nested-name-specifier, then we are looking at
4073 the first qualified-id production. */
4074 if (nested_name_specifier_p)
4077 tree saved_object_scope;
4078 tree saved_qualifying_scope;
4079 tree unqualified_id;
4082 /* See if the next token is the `template' keyword. */
4084 template_p = &is_template;
4085 *template_p = cp_parser_optional_template_keyword (parser);
4086 /* Name lookup we do during the processing of the
4087 unqualified-id might obliterate SCOPE. */
4088 saved_scope = parser->scope;
4089 saved_object_scope = parser->object_scope;
4090 saved_qualifying_scope = parser->qualifying_scope;
4091 /* Process the final unqualified-id. */
4092 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4095 /*optional_p=*/false);
4096 /* Restore the SAVED_SCOPE for our caller. */
4097 parser->scope = saved_scope;
4098 parser->object_scope = saved_object_scope;
4099 parser->qualifying_scope = saved_qualifying_scope;
4101 return unqualified_id;
4103 /* Otherwise, if we are in global scope, then we are looking at one
4104 of the other qualified-id productions. */
4105 else if (global_scope_p)
4110 /* Peek at the next token. */
4111 token = cp_lexer_peek_token (parser->lexer);
4113 /* If it's an identifier, and the next token is not a "<", then
4114 we can avoid the template-id case. This is an optimization
4115 for this common case. */
4116 if (token->type == CPP_NAME
4117 && !cp_parser_nth_token_starts_template_argument_list_p
4119 return cp_parser_identifier (parser);
4121 cp_parser_parse_tentatively (parser);
4122 /* Try a template-id. */
4123 id = cp_parser_template_id (parser,
4124 /*template_keyword_p=*/false,
4125 /*check_dependency_p=*/true,
4127 /* If that worked, we're done. */
4128 if (cp_parser_parse_definitely (parser))
4131 /* Peek at the next token. (Changes in the token buffer may
4132 have invalidated the pointer obtained above.) */
4133 token = cp_lexer_peek_token (parser->lexer);
4135 switch (token->type)
4138 return cp_parser_identifier (parser);
4141 if (token->keyword == RID_OPERATOR)
4142 return cp_parser_operator_function_id (parser);
4146 cp_parser_error (parser, "expected id-expression");
4147 return error_mark_node;
4151 return cp_parser_unqualified_id (parser, template_keyword_p,
4152 /*check_dependency_p=*/true,
4157 /* Parse an unqualified-id.
4161 operator-function-id
4162 conversion-function-id
4166 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4167 keyword, in a construct like `A::template ...'.
4169 Returns a representation of unqualified-id. For the `identifier'
4170 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4171 production a BIT_NOT_EXPR is returned; the operand of the
4172 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4173 other productions, see the documentation accompanying the
4174 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4175 names are looked up in uninstantiated templates. If DECLARATOR_P
4176 is true, the unqualified-id is appearing as part of a declarator,
4177 rather than as part of an expression. */
4180 cp_parser_unqualified_id (cp_parser* parser,
4181 bool template_keyword_p,
4182 bool check_dependency_p,
4188 /* Peek at the next token. */
4189 token = cp_lexer_peek_token (parser->lexer);
4191 switch (token->type)
4197 /* We don't know yet whether or not this will be a
4199 cp_parser_parse_tentatively (parser);
4200 /* Try a template-id. */
4201 id = cp_parser_template_id (parser, template_keyword_p,
4204 /* If it worked, we're done. */
4205 if (cp_parser_parse_definitely (parser))
4207 /* Otherwise, it's an ordinary identifier. */
4208 return cp_parser_identifier (parser);
4211 case CPP_TEMPLATE_ID:
4212 return cp_parser_template_id (parser, template_keyword_p,
4219 tree qualifying_scope;
4224 /* Consume the `~' token. */
4225 cp_lexer_consume_token (parser->lexer);
4226 /* Parse the class-name. The standard, as written, seems to
4229 template <typename T> struct S { ~S (); };
4230 template <typename T> S<T>::~S() {}
4232 is invalid, since `~' must be followed by a class-name, but
4233 `S<T>' is dependent, and so not known to be a class.
4234 That's not right; we need to look in uninstantiated
4235 templates. A further complication arises from:
4237 template <typename T> void f(T t) {
4241 Here, it is not possible to look up `T' in the scope of `T'
4242 itself. We must look in both the current scope, and the
4243 scope of the containing complete expression.
4245 Yet another issue is:
4254 The standard does not seem to say that the `S' in `~S'
4255 should refer to the type `S' and not the data member
4258 /* DR 244 says that we look up the name after the "~" in the
4259 same scope as we looked up the qualifying name. That idea
4260 isn't fully worked out; it's more complicated than that. */
4261 scope = parser->scope;
4262 object_scope = parser->object_scope;
4263 qualifying_scope = parser->qualifying_scope;
4265 /* Check for invalid scopes. */
4266 if (scope == error_mark_node)
4268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4269 cp_lexer_consume_token (parser->lexer);
4270 return error_mark_node;
4272 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4275 error_at (token->location,
4276 "scope %qT before %<~%> is not a class-name",
4278 cp_parser_simulate_error (parser);
4279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4280 cp_lexer_consume_token (parser->lexer);
4281 return error_mark_node;
4283 gcc_assert (!scope || TYPE_P (scope));
4285 /* If the name is of the form "X::~X" it's OK even if X is a
4287 token = cp_lexer_peek_token (parser->lexer);
4289 && token->type == CPP_NAME
4290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4292 && (token->u.value == TYPE_IDENTIFIER (scope)
4293 || constructor_name_p (token->u.value, scope)))
4295 cp_lexer_consume_token (parser->lexer);
4296 return build_nt (BIT_NOT_EXPR, scope);
4299 /* If there was an explicit qualification (S::~T), first look
4300 in the scope given by the qualification (i.e., S).
4302 Note: in the calls to cp_parser_class_name below we pass
4303 typename_type so that lookup finds the injected-class-name
4304 rather than the constructor. */
4306 type_decl = NULL_TREE;
4309 cp_parser_parse_tentatively (parser);
4310 type_decl = cp_parser_class_name (parser,
4311 /*typename_keyword_p=*/false,
4312 /*template_keyword_p=*/false,
4314 /*check_dependency=*/false,
4315 /*class_head_p=*/false,
4317 if (cp_parser_parse_definitely (parser))
4320 /* In "N::S::~S", look in "N" as well. */
4321 if (!done && scope && qualifying_scope)
4323 cp_parser_parse_tentatively (parser);
4324 parser->scope = qualifying_scope;
4325 parser->object_scope = NULL_TREE;
4326 parser->qualifying_scope = NULL_TREE;
4328 = cp_parser_class_name (parser,
4329 /*typename_keyword_p=*/false,
4330 /*template_keyword_p=*/false,
4332 /*check_dependency=*/false,
4333 /*class_head_p=*/false,
4335 if (cp_parser_parse_definitely (parser))
4338 /* In "p->S::~T", look in the scope given by "*p" as well. */
4339 else if (!done && object_scope)
4341 cp_parser_parse_tentatively (parser);
4342 parser->scope = object_scope;
4343 parser->object_scope = NULL_TREE;
4344 parser->qualifying_scope = NULL_TREE;
4346 = cp_parser_class_name (parser,
4347 /*typename_keyword_p=*/false,
4348 /*template_keyword_p=*/false,
4350 /*check_dependency=*/false,
4351 /*class_head_p=*/false,
4353 if (cp_parser_parse_definitely (parser))
4356 /* Look in the surrounding context. */
4359 parser->scope = NULL_TREE;
4360 parser->object_scope = NULL_TREE;
4361 parser->qualifying_scope = NULL_TREE;
4362 if (processing_template_decl)
4363 cp_parser_parse_tentatively (parser);
4365 = cp_parser_class_name (parser,
4366 /*typename_keyword_p=*/false,
4367 /*template_keyword_p=*/false,
4369 /*check_dependency=*/false,
4370 /*class_head_p=*/false,
4372 if (processing_template_decl
4373 && ! cp_parser_parse_definitely (parser))
4375 /* We couldn't find a type with this name, so just accept
4376 it and check for a match at instantiation time. */
4377 type_decl = cp_parser_identifier (parser);
4378 if (type_decl != error_mark_node)
4379 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4383 /* If an error occurred, assume that the name of the
4384 destructor is the same as the name of the qualifying
4385 class. That allows us to keep parsing after running
4386 into ill-formed destructor names. */
4387 if (type_decl == error_mark_node && scope)
4388 return build_nt (BIT_NOT_EXPR, scope);
4389 else if (type_decl == error_mark_node)
4390 return error_mark_node;
4392 /* Check that destructor name and scope match. */
4393 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4395 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4396 error_at (token->location,
4397 "declaration of %<~%T%> as member of %qT",
4399 cp_parser_simulate_error (parser);
4400 return error_mark_node;
4405 A typedef-name that names a class shall not be used as the
4406 identifier in the declarator for a destructor declaration. */
4408 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4409 && !DECL_SELF_REFERENCE_P (type_decl)
4410 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4411 error_at (token->location,
4412 "typedef-name %qD used as destructor declarator",
4415 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4419 if (token->keyword == RID_OPERATOR)
4423 /* This could be a template-id, so we try that first. */
4424 cp_parser_parse_tentatively (parser);
4425 /* Try a template-id. */
4426 id = cp_parser_template_id (parser, template_keyword_p,
4427 /*check_dependency_p=*/true,
4429 /* If that worked, we're done. */
4430 if (cp_parser_parse_definitely (parser))
4432 /* We still don't know whether we're looking at an
4433 operator-function-id or a conversion-function-id. */
4434 cp_parser_parse_tentatively (parser);
4435 /* Try an operator-function-id. */
4436 id = cp_parser_operator_function_id (parser);
4437 /* If that didn't work, try a conversion-function-id. */
4438 if (!cp_parser_parse_definitely (parser))
4439 id = cp_parser_conversion_function_id (parser);
4448 cp_parser_error (parser, "expected unqualified-id");
4449 return error_mark_node;
4453 /* Parse an (optional) nested-name-specifier.
4455 nested-name-specifier: [C++98]
4456 class-or-namespace-name :: nested-name-specifier [opt]
4457 class-or-namespace-name :: template nested-name-specifier [opt]
4459 nested-name-specifier: [C++0x]
4462 nested-name-specifier identifier ::
4463 nested-name-specifier template [opt] simple-template-id ::
4465 PARSER->SCOPE should be set appropriately before this function is
4466 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4467 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4470 Sets PARSER->SCOPE to the class (TYPE) or namespace
4471 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4472 it unchanged if there is no nested-name-specifier. Returns the new
4473 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4475 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4476 part of a declaration and/or decl-specifier. */
4479 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4480 bool typename_keyword_p,
4481 bool check_dependency_p,
4483 bool is_declaration)
4485 bool success = false;
4486 cp_token_position start = 0;
4489 /* Remember where the nested-name-specifier starts. */
4490 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4492 start = cp_lexer_token_position (parser->lexer, false);
4493 push_deferring_access_checks (dk_deferred);
4500 tree saved_qualifying_scope;
4501 bool template_keyword_p;
4503 /* Spot cases that cannot be the beginning of a
4504 nested-name-specifier. */
4505 token = cp_lexer_peek_token (parser->lexer);
4507 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4508 the already parsed nested-name-specifier. */
4509 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4511 /* Grab the nested-name-specifier and continue the loop. */
4512 cp_parser_pre_parsed_nested_name_specifier (parser);
4513 /* If we originally encountered this nested-name-specifier
4514 with IS_DECLARATION set to false, we will not have
4515 resolved TYPENAME_TYPEs, so we must do so here. */
4517 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4519 new_scope = resolve_typename_type (parser->scope,
4520 /*only_current_p=*/false);
4521 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4522 parser->scope = new_scope;
4528 /* Spot cases that cannot be the beginning of a
4529 nested-name-specifier. On the second and subsequent times
4530 through the loop, we look for the `template' keyword. */
4531 if (success && token->keyword == RID_TEMPLATE)
4533 /* A template-id can start a nested-name-specifier. */
4534 else if (token->type == CPP_TEMPLATE_ID)
4538 /* If the next token is not an identifier, then it is
4539 definitely not a type-name or namespace-name. */
4540 if (token->type != CPP_NAME)
4542 /* If the following token is neither a `<' (to begin a
4543 template-id), nor a `::', then we are not looking at a
4544 nested-name-specifier. */
4545 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4546 if (token->type != CPP_SCOPE
4547 && !cp_parser_nth_token_starts_template_argument_list_p
4552 /* The nested-name-specifier is optional, so we parse
4554 cp_parser_parse_tentatively (parser);
4556 /* Look for the optional `template' keyword, if this isn't the
4557 first time through the loop. */
4559 template_keyword_p = cp_parser_optional_template_keyword (parser);
4561 template_keyword_p = false;
4563 /* Save the old scope since the name lookup we are about to do
4564 might destroy it. */
4565 old_scope = parser->scope;
4566 saved_qualifying_scope = parser->qualifying_scope;
4567 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4568 look up names in "X<T>::I" in order to determine that "Y" is
4569 a template. So, if we have a typename at this point, we make
4570 an effort to look through it. */
4572 && !typename_keyword_p
4574 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4575 parser->scope = resolve_typename_type (parser->scope,
4576 /*only_current_p=*/false);
4577 /* Parse the qualifying entity. */
4579 = cp_parser_qualifying_entity (parser,
4585 /* Look for the `::' token. */
4586 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4588 /* If we found what we wanted, we keep going; otherwise, we're
4590 if (!cp_parser_parse_definitely (parser))
4592 bool error_p = false;
4594 /* Restore the OLD_SCOPE since it was valid before the
4595 failed attempt at finding the last
4596 class-or-namespace-name. */
4597 parser->scope = old_scope;
4598 parser->qualifying_scope = saved_qualifying_scope;
4599 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4601 /* If the next token is an identifier, and the one after
4602 that is a `::', then any valid interpretation would have
4603 found a class-or-namespace-name. */
4604 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4607 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4610 token = cp_lexer_consume_token (parser->lexer);
4613 if (!token->ambiguous_p)
4616 tree ambiguous_decls;
4618 decl = cp_parser_lookup_name (parser, token->u.value,
4620 /*is_template=*/false,
4621 /*is_namespace=*/false,
4622 /*check_dependency=*/true,
4625 if (TREE_CODE (decl) == TEMPLATE_DECL)
4626 error_at (token->location,
4627 "%qD used without template parameters",
4629 else if (ambiguous_decls)
4631 error_at (token->location,
4632 "reference to %qD is ambiguous",
4634 print_candidates (ambiguous_decls);
4635 decl = error_mark_node;
4639 if (cxx_dialect != cxx98)
4640 cp_parser_name_lookup_error
4641 (parser, token->u.value, decl, NLE_NOT_CXX98,
4644 cp_parser_name_lookup_error
4645 (parser, token->u.value, decl, NLE_CXX98,
4649 parser->scope = error_mark_node;
4651 /* Treat this as a successful nested-name-specifier
4656 If the name found is not a class-name (clause
4657 _class_) or namespace-name (_namespace.def_), the
4658 program is ill-formed. */
4661 cp_lexer_consume_token (parser->lexer);
4665 /* We've found one valid nested-name-specifier. */
4667 /* Name lookup always gives us a DECL. */
4668 if (TREE_CODE (new_scope) == TYPE_DECL)
4669 new_scope = TREE_TYPE (new_scope);
4670 /* Uses of "template" must be followed by actual templates. */
4671 if (template_keyword_p
4672 && !(CLASS_TYPE_P (new_scope)
4673 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4674 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4675 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4676 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4677 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4678 == TEMPLATE_ID_EXPR)))
4679 permerror (input_location, TYPE_P (new_scope)
4680 ? "%qT is not a template"
4681 : "%qD is not a template",
4683 /* If it is a class scope, try to complete it; we are about to
4684 be looking up names inside the class. */
4685 if (TYPE_P (new_scope)
4686 /* Since checking types for dependency can be expensive,
4687 avoid doing it if the type is already complete. */
4688 && !COMPLETE_TYPE_P (new_scope)
4689 /* Do not try to complete dependent types. */
4690 && !dependent_type_p (new_scope))
4692 new_scope = complete_type (new_scope);
4693 /* If it is a typedef to current class, use the current
4694 class instead, as the typedef won't have any names inside
4696 if (!COMPLETE_TYPE_P (new_scope)
4697 && currently_open_class (new_scope))
4698 new_scope = TYPE_MAIN_VARIANT (new_scope);
4700 /* Make sure we look in the right scope the next time through
4702 parser->scope = new_scope;
4705 /* If parsing tentatively, replace the sequence of tokens that makes
4706 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4707 token. That way, should we re-parse the token stream, we will
4708 not have to repeat the effort required to do the parse, nor will
4709 we issue duplicate error messages. */
4710 if (success && start)
4714 token = cp_lexer_token_at (parser->lexer, start);
4715 /* Reset the contents of the START token. */
4716 token->type = CPP_NESTED_NAME_SPECIFIER;
4717 /* Retrieve any deferred checks. Do not pop this access checks yet
4718 so the memory will not be reclaimed during token replacing below. */
4719 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4720 token->u.tree_check_value->value = parser->scope;
4721 token->u.tree_check_value->checks = get_deferred_access_checks ();
4722 token->u.tree_check_value->qualifying_scope =
4723 parser->qualifying_scope;
4724 token->keyword = RID_MAX;
4726 /* Purge all subsequent tokens. */
4727 cp_lexer_purge_tokens_after (parser->lexer, start);
4731 pop_to_parent_deferring_access_checks ();
4733 return success ? parser->scope : NULL_TREE;
4736 /* Parse a nested-name-specifier. See
4737 cp_parser_nested_name_specifier_opt for details. This function
4738 behaves identically, except that it will an issue an error if no
4739 nested-name-specifier is present. */
4742 cp_parser_nested_name_specifier (cp_parser *parser,
4743 bool typename_keyword_p,
4744 bool check_dependency_p,
4746 bool is_declaration)
4750 /* Look for the nested-name-specifier. */
4751 scope = cp_parser_nested_name_specifier_opt (parser,
4756 /* If it was not present, issue an error message. */
4759 cp_parser_error (parser, "expected nested-name-specifier");
4760 parser->scope = NULL_TREE;
4766 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4767 this is either a class-name or a namespace-name (which corresponds
4768 to the class-or-namespace-name production in the grammar). For
4769 C++0x, it can also be a type-name that refers to an enumeration
4772 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4773 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4774 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4775 TYPE_P is TRUE iff the next name should be taken as a class-name,
4776 even the same name is declared to be another entity in the same
4779 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4780 specified by the class-or-namespace-name. If neither is found the
4781 ERROR_MARK_NODE is returned. */
4784 cp_parser_qualifying_entity (cp_parser *parser,
4785 bool typename_keyword_p,
4786 bool template_keyword_p,
4787 bool check_dependency_p,
4789 bool is_declaration)
4792 tree saved_qualifying_scope;
4793 tree saved_object_scope;
4796 bool successful_parse_p;
4798 /* Before we try to parse the class-name, we must save away the
4799 current PARSER->SCOPE since cp_parser_class_name will destroy
4801 saved_scope = parser->scope;
4802 saved_qualifying_scope = parser->qualifying_scope;
4803 saved_object_scope = parser->object_scope;
4804 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4805 there is no need to look for a namespace-name. */
4806 only_class_p = template_keyword_p
4807 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4809 cp_parser_parse_tentatively (parser);
4810 scope = cp_parser_class_name (parser,
4813 type_p ? class_type : none_type,
4815 /*class_head_p=*/false,
4817 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4818 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4820 && cxx_dialect != cxx98
4821 && !successful_parse_p)
4823 /* Restore the saved scope. */
4824 parser->scope = saved_scope;
4825 parser->qualifying_scope = saved_qualifying_scope;
4826 parser->object_scope = saved_object_scope;
4828 /* Parse tentatively. */
4829 cp_parser_parse_tentatively (parser);
4831 /* Parse a typedef-name or enum-name. */
4832 scope = cp_parser_nonclass_name (parser);
4834 /* "If the name found does not designate a namespace or a class,
4835 enumeration, or dependent type, the program is ill-formed."
4837 We cover classes and dependent types above and namespaces below,
4838 so this code is only looking for enums. */
4839 if (!scope || TREE_CODE (scope) != TYPE_DECL
4840 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4841 cp_parser_simulate_error (parser);
4843 successful_parse_p = cp_parser_parse_definitely (parser);
4845 /* If that didn't work, try for a namespace-name. */
4846 if (!only_class_p && !successful_parse_p)
4848 /* Restore the saved scope. */
4849 parser->scope = saved_scope;
4850 parser->qualifying_scope = saved_qualifying_scope;
4851 parser->object_scope = saved_object_scope;
4852 /* If we are not looking at an identifier followed by the scope
4853 resolution operator, then this is not part of a
4854 nested-name-specifier. (Note that this function is only used
4855 to parse the components of a nested-name-specifier.) */
4856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4858 return error_mark_node;
4859 scope = cp_parser_namespace_name (parser);
4865 /* Parse a postfix-expression.
4869 postfix-expression [ expression ]
4870 postfix-expression ( expression-list [opt] )
4871 simple-type-specifier ( expression-list [opt] )
4872 typename :: [opt] nested-name-specifier identifier
4873 ( expression-list [opt] )
4874 typename :: [opt] nested-name-specifier template [opt] template-id
4875 ( expression-list [opt] )
4876 postfix-expression . template [opt] id-expression
4877 postfix-expression -> template [opt] id-expression
4878 postfix-expression . pseudo-destructor-name
4879 postfix-expression -> pseudo-destructor-name
4880 postfix-expression ++
4881 postfix-expression --
4882 dynamic_cast < type-id > ( expression )
4883 static_cast < type-id > ( expression )
4884 reinterpret_cast < type-id > ( expression )
4885 const_cast < type-id > ( expression )
4886 typeid ( expression )
4892 ( type-id ) { initializer-list , [opt] }
4894 This extension is a GNU version of the C99 compound-literal
4895 construct. (The C99 grammar uses `type-name' instead of `type-id',
4896 but they are essentially the same concept.)
4898 If ADDRESS_P is true, the postfix expression is the operand of the
4899 `&' operator. CAST_P is true if this expression is the target of a
4902 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4903 class member access expressions [expr.ref].
4905 Returns a representation of the expression. */
4908 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4909 bool member_access_only_p,
4910 cp_id_kind * pidk_return)
4914 cp_id_kind idk = CP_ID_KIND_NONE;
4915 tree postfix_expression = NULL_TREE;
4916 bool is_member_access = false;
4918 /* Peek at the next token. */
4919 token = cp_lexer_peek_token (parser->lexer);
4920 /* Some of the productions are determined by keywords. */
4921 keyword = token->keyword;
4931 const char *saved_message;
4933 /* All of these can be handled in the same way from the point
4934 of view of parsing. Begin by consuming the token
4935 identifying the cast. */
4936 cp_lexer_consume_token (parser->lexer);
4938 /* New types cannot be defined in the cast. */
4939 saved_message = parser->type_definition_forbidden_message;
4940 parser->type_definition_forbidden_message
4941 = G_("types may not be defined in casts");
4943 /* Look for the opening `<'. */
4944 cp_parser_require (parser, CPP_LESS, RT_LESS);
4945 /* Parse the type to which we are casting. */
4946 type = cp_parser_type_id (parser);
4947 /* Look for the closing `>'. */
4948 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4949 /* Restore the old message. */
4950 parser->type_definition_forbidden_message = saved_message;
4952 /* And the expression which is being cast. */
4953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4955 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4957 /* Only type conversions to integral or enumeration types
4958 can be used in constant-expressions. */
4959 if (!cast_valid_in_integral_constant_expression_p (type)
4960 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4961 return error_mark_node;
4967 = build_dynamic_cast (type, expression, tf_warning_or_error);
4971 = build_static_cast (type, expression, tf_warning_or_error);
4975 = build_reinterpret_cast (type, expression,
4976 tf_warning_or_error);
4980 = build_const_cast (type, expression, tf_warning_or_error);
4991 const char *saved_message;
4992 bool saved_in_type_id_in_expr_p;
4994 /* Consume the `typeid' token. */
4995 cp_lexer_consume_token (parser->lexer);
4996 /* Look for the `(' token. */
4997 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4998 /* Types cannot be defined in a `typeid' expression. */
4999 saved_message = parser->type_definition_forbidden_message;
5000 parser->type_definition_forbidden_message
5001 = G_("types may not be defined in a %<typeid%> expression");
5002 /* We can't be sure yet whether we're looking at a type-id or an
5004 cp_parser_parse_tentatively (parser);
5005 /* Try a type-id first. */
5006 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5007 parser->in_type_id_in_expr_p = true;
5008 type = cp_parser_type_id (parser);
5009 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5010 /* Look for the `)' token. Otherwise, we can't be sure that
5011 we're not looking at an expression: consider `typeid (int
5012 (3))', for example. */
5013 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5014 /* If all went well, simply lookup the type-id. */
5015 if (cp_parser_parse_definitely (parser))
5016 postfix_expression = get_typeid (type);
5017 /* Otherwise, fall back to the expression variant. */
5022 /* Look for an expression. */
5023 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5024 /* Compute its typeid. */
5025 postfix_expression = build_typeid (expression);
5026 /* Look for the `)' token. */
5027 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5029 /* Restore the saved message. */
5030 parser->type_definition_forbidden_message = saved_message;
5031 /* `typeid' may not appear in an integral constant expression. */
5032 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5033 return error_mark_node;
5040 /* The syntax permitted here is the same permitted for an
5041 elaborated-type-specifier. */
5042 type = cp_parser_elaborated_type_specifier (parser,
5043 /*is_friend=*/false,
5044 /*is_declaration=*/false);
5045 postfix_expression = cp_parser_functional_cast (parser, type);
5053 /* If the next thing is a simple-type-specifier, we may be
5054 looking at a functional cast. We could also be looking at
5055 an id-expression. So, we try the functional cast, and if
5056 that doesn't work we fall back to the primary-expression. */
5057 cp_parser_parse_tentatively (parser);
5058 /* Look for the simple-type-specifier. */
5059 type = cp_parser_simple_type_specifier (parser,
5060 /*decl_specs=*/NULL,
5061 CP_PARSER_FLAGS_NONE);
5062 /* Parse the cast itself. */
5063 if (!cp_parser_error_occurred (parser))
5065 = cp_parser_functional_cast (parser, type);
5066 /* If that worked, we're done. */
5067 if (cp_parser_parse_definitely (parser))
5070 /* If the functional-cast didn't work out, try a
5071 compound-literal. */
5072 if (cp_parser_allow_gnu_extensions_p (parser)
5073 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5075 VEC(constructor_elt,gc) *initializer_list = NULL;
5076 bool saved_in_type_id_in_expr_p;
5078 cp_parser_parse_tentatively (parser);
5079 /* Consume the `('. */
5080 cp_lexer_consume_token (parser->lexer);
5081 /* Parse the type. */
5082 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5083 parser->in_type_id_in_expr_p = true;
5084 type = cp_parser_type_id (parser);
5085 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5086 /* Look for the `)'. */
5087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5088 /* Look for the `{'. */
5089 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5090 /* If things aren't going well, there's no need to
5092 if (!cp_parser_error_occurred (parser))
5094 bool non_constant_p;
5095 /* Parse the initializer-list. */
5097 = cp_parser_initializer_list (parser, &non_constant_p);
5098 /* Allow a trailing `,'. */
5099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5100 cp_lexer_consume_token (parser->lexer);
5101 /* Look for the final `}'. */
5102 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5104 /* If that worked, we're definitely looking at a
5105 compound-literal expression. */
5106 if (cp_parser_parse_definitely (parser))
5108 /* Warn the user that a compound literal is not
5109 allowed in standard C++. */
5110 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5111 /* For simplicity, we disallow compound literals in
5112 constant-expressions. We could
5113 allow compound literals of integer type, whose
5114 initializer was a constant, in constant
5115 expressions. Permitting that usage, as a further
5116 extension, would not change the meaning of any
5117 currently accepted programs. (Of course, as
5118 compound literals are not part of ISO C++, the
5119 standard has nothing to say.) */
5120 if (cp_parser_non_integral_constant_expression (parser,
5123 postfix_expression = error_mark_node;
5126 /* Form the representation of the compound-literal. */
5128 = (finish_compound_literal
5129 (type, build_constructor (init_list_type_node,
5130 initializer_list)));
5135 /* It must be a primary-expression. */
5137 = cp_parser_primary_expression (parser, address_p, cast_p,
5138 /*template_arg_p=*/false,
5144 /* Keep looping until the postfix-expression is complete. */
5147 if (idk == CP_ID_KIND_UNQUALIFIED
5148 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5149 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5150 /* It is not a Koenig lookup function call. */
5152 = unqualified_name_lookup_error (postfix_expression);
5154 /* Peek at the next token. */
5155 token = cp_lexer_peek_token (parser->lexer);
5157 switch (token->type)
5159 case CPP_OPEN_SQUARE:
5161 = cp_parser_postfix_open_square_expression (parser,
5164 idk = CP_ID_KIND_NONE;
5165 is_member_access = false;
5168 case CPP_OPEN_PAREN:
5169 /* postfix-expression ( expression-list [opt] ) */
5172 bool is_builtin_constant_p;
5173 bool saved_integral_constant_expression_p = false;
5174 bool saved_non_integral_constant_expression_p = false;
5177 is_member_access = false;
5179 is_builtin_constant_p
5180 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5181 if (is_builtin_constant_p)
5183 /* The whole point of __builtin_constant_p is to allow
5184 non-constant expressions to appear as arguments. */
5185 saved_integral_constant_expression_p
5186 = parser->integral_constant_expression_p;
5187 saved_non_integral_constant_expression_p
5188 = parser->non_integral_constant_expression_p;
5189 parser->integral_constant_expression_p = false;
5191 args = (cp_parser_parenthesized_expression_list
5193 /*cast_p=*/false, /*allow_expansion_p=*/true,
5194 /*non_constant_p=*/NULL));
5195 if (is_builtin_constant_p)
5197 parser->integral_constant_expression_p
5198 = saved_integral_constant_expression_p;
5199 parser->non_integral_constant_expression_p
5200 = saved_non_integral_constant_expression_p;
5205 postfix_expression = error_mark_node;
5209 /* Function calls are not permitted in
5210 constant-expressions. */
5211 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5212 && cp_parser_non_integral_constant_expression (parser,
5215 postfix_expression = error_mark_node;
5216 release_tree_vector (args);
5221 if (idk == CP_ID_KIND_UNQUALIFIED
5222 || idk == CP_ID_KIND_TEMPLATE_ID)
5224 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5226 if (!VEC_empty (tree, args))
5229 if (!any_type_dependent_arguments_p (args))
5231 = perform_koenig_lookup (postfix_expression, args,
5232 /*include_std=*/false);
5236 = unqualified_fn_lookup_error (postfix_expression);
5238 /* We do not perform argument-dependent lookup if
5239 normal lookup finds a non-function, in accordance
5240 with the expected resolution of DR 218. */
5241 else if (!VEC_empty (tree, args)
5242 && is_overloaded_fn (postfix_expression))
5244 tree fn = get_first_fn (postfix_expression);
5245 fn = STRIP_TEMPLATE (fn);
5247 /* Do not do argument dependent lookup if regular
5248 lookup finds a member function or a block-scope
5249 function declaration. [basic.lookup.argdep]/3 */
5250 if (!DECL_FUNCTION_MEMBER_P (fn)
5251 && !DECL_LOCAL_FUNCTION_P (fn))
5254 if (!any_type_dependent_arguments_p (args))
5256 = perform_koenig_lookup (postfix_expression, args,
5257 /*include_std=*/false);
5262 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5264 tree instance = TREE_OPERAND (postfix_expression, 0);
5265 tree fn = TREE_OPERAND (postfix_expression, 1);
5267 if (processing_template_decl
5268 && (type_dependent_expression_p (instance)
5269 || (!BASELINK_P (fn)
5270 && TREE_CODE (fn) != FIELD_DECL)
5271 || type_dependent_expression_p (fn)
5272 || any_type_dependent_arguments_p (args)))
5275 = build_nt_call_vec (postfix_expression, args);
5276 release_tree_vector (args);
5280 if (BASELINK_P (fn))
5283 = (build_new_method_call
5284 (instance, fn, &args, NULL_TREE,
5285 (idk == CP_ID_KIND_QUALIFIED
5286 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5288 tf_warning_or_error));
5292 = finish_call_expr (postfix_expression, &args,
5293 /*disallow_virtual=*/false,
5295 tf_warning_or_error);
5297 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5298 || TREE_CODE (postfix_expression) == MEMBER_REF
5299 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5300 postfix_expression = (build_offset_ref_call_from_tree
5301 (postfix_expression, &args));
5302 else if (idk == CP_ID_KIND_QUALIFIED)
5303 /* A call to a static class member, or a namespace-scope
5306 = finish_call_expr (postfix_expression, &args,
5307 /*disallow_virtual=*/true,
5309 tf_warning_or_error);
5311 /* All other function calls. */
5313 = finish_call_expr (postfix_expression, &args,
5314 /*disallow_virtual=*/false,
5316 tf_warning_or_error);
5318 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5319 idk = CP_ID_KIND_NONE;
5321 release_tree_vector (args);
5327 /* postfix-expression . template [opt] id-expression
5328 postfix-expression . pseudo-destructor-name
5329 postfix-expression -> template [opt] id-expression
5330 postfix-expression -> pseudo-destructor-name */
5332 /* Consume the `.' or `->' operator. */
5333 cp_lexer_consume_token (parser->lexer);
5336 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5341 is_member_access = true;
5345 /* postfix-expression ++ */
5346 /* Consume the `++' token. */
5347 cp_lexer_consume_token (parser->lexer);
5348 /* Generate a representation for the complete expression. */
5350 = finish_increment_expr (postfix_expression,
5351 POSTINCREMENT_EXPR);
5352 /* Increments may not appear in constant-expressions. */
5353 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5354 postfix_expression = error_mark_node;
5355 idk = CP_ID_KIND_NONE;
5356 is_member_access = false;
5359 case CPP_MINUS_MINUS:
5360 /* postfix-expression -- */
5361 /* Consume the `--' token. */
5362 cp_lexer_consume_token (parser->lexer);
5363 /* Generate a representation for the complete expression. */
5365 = finish_increment_expr (postfix_expression,
5366 POSTDECREMENT_EXPR);
5367 /* Decrements may not appear in constant-expressions. */
5368 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5369 postfix_expression = error_mark_node;
5370 idk = CP_ID_KIND_NONE;
5371 is_member_access = false;
5375 if (pidk_return != NULL)
5376 * pidk_return = idk;
5377 if (member_access_only_p)
5378 return is_member_access? postfix_expression : error_mark_node;
5380 return postfix_expression;
5384 /* We should never get here. */
5386 return error_mark_node;
5389 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5390 by cp_parser_builtin_offsetof. We're looking for
5392 postfix-expression [ expression ]
5394 FOR_OFFSETOF is set if we're being called in that context, which
5395 changes how we deal with integer constant expressions. */
5398 cp_parser_postfix_open_square_expression (cp_parser *parser,
5399 tree postfix_expression,
5404 /* Consume the `[' token. */
5405 cp_lexer_consume_token (parser->lexer);
5407 /* Parse the index expression. */
5408 /* ??? For offsetof, there is a question of what to allow here. If
5409 offsetof is not being used in an integral constant expression context,
5410 then we *could* get the right answer by computing the value at runtime.
5411 If we are in an integral constant expression context, then we might
5412 could accept any constant expression; hard to say without analysis.
5413 Rather than open the barn door too wide right away, allow only integer
5414 constant expressions here. */
5416 index = cp_parser_constant_expression (parser, false, NULL);
5418 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5420 /* Look for the closing `]'. */
5421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5423 /* Build the ARRAY_REF. */
5424 postfix_expression = grok_array_decl (postfix_expression, index);
5426 /* When not doing offsetof, array references are not permitted in
5427 constant-expressions. */
5429 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5430 postfix_expression = error_mark_node;
5432 return postfix_expression;
5435 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5436 by cp_parser_builtin_offsetof. We're looking for
5438 postfix-expression . template [opt] id-expression
5439 postfix-expression . pseudo-destructor-name
5440 postfix-expression -> template [opt] id-expression
5441 postfix-expression -> pseudo-destructor-name
5443 FOR_OFFSETOF is set if we're being called in that context. That sorta
5444 limits what of the above we'll actually accept, but nevermind.
5445 TOKEN_TYPE is the "." or "->" token, which will already have been
5446 removed from the stream. */
5449 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5450 enum cpp_ttype token_type,
5451 tree postfix_expression,
5452 bool for_offsetof, cp_id_kind *idk,
5453 location_t location)
5457 bool pseudo_destructor_p;
5458 tree scope = NULL_TREE;
5460 /* If this is a `->' operator, dereference the pointer. */
5461 if (token_type == CPP_DEREF)
5462 postfix_expression = build_x_arrow (postfix_expression);
5463 /* Check to see whether or not the expression is type-dependent. */
5464 dependent_p = type_dependent_expression_p (postfix_expression);
5465 /* The identifier following the `->' or `.' is not qualified. */
5466 parser->scope = NULL_TREE;
5467 parser->qualifying_scope = NULL_TREE;
5468 parser->object_scope = NULL_TREE;
5469 *idk = CP_ID_KIND_NONE;
5471 /* Enter the scope corresponding to the type of the object
5472 given by the POSTFIX_EXPRESSION. */
5473 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5475 scope = TREE_TYPE (postfix_expression);
5476 /* According to the standard, no expression should ever have
5477 reference type. Unfortunately, we do not currently match
5478 the standard in this respect in that our internal representation
5479 of an expression may have reference type even when the standard
5480 says it does not. Therefore, we have to manually obtain the
5481 underlying type here. */
5482 scope = non_reference (scope);
5483 /* The type of the POSTFIX_EXPRESSION must be complete. */
5484 if (scope == unknown_type_node)
5486 error_at (location, "%qE does not have class type",
5487 postfix_expression);
5491 scope = complete_type_or_else (scope, NULL_TREE);
5492 /* Let the name lookup machinery know that we are processing a
5493 class member access expression. */
5494 parser->context->object_type = scope;
5495 /* If something went wrong, we want to be able to discern that case,
5496 as opposed to the case where there was no SCOPE due to the type
5497 of expression being dependent. */
5499 scope = error_mark_node;
5500 /* If the SCOPE was erroneous, make the various semantic analysis
5501 functions exit quickly -- and without issuing additional error
5503 if (scope == error_mark_node)
5504 postfix_expression = error_mark_node;
5507 /* Assume this expression is not a pseudo-destructor access. */
5508 pseudo_destructor_p = false;
5510 /* If the SCOPE is a scalar type, then, if this is a valid program,
5511 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5512 is type dependent, it can be pseudo-destructor-name or something else.
5513 Try to parse it as pseudo-destructor-name first. */
5514 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5519 cp_parser_parse_tentatively (parser);
5520 /* Parse the pseudo-destructor-name. */
5522 cp_parser_pseudo_destructor_name (parser, &s, &type);
5524 && (cp_parser_error_occurred (parser)
5525 || TREE_CODE (type) != TYPE_DECL
5526 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5527 cp_parser_abort_tentative_parse (parser);
5528 else if (cp_parser_parse_definitely (parser))
5530 pseudo_destructor_p = true;
5532 = finish_pseudo_destructor_expr (postfix_expression,
5533 s, TREE_TYPE (type));
5537 if (!pseudo_destructor_p)
5539 /* If the SCOPE is not a scalar type, we are looking at an
5540 ordinary class member access expression, rather than a
5541 pseudo-destructor-name. */
5543 cp_token *token = cp_lexer_peek_token (parser->lexer);
5544 /* Parse the id-expression. */
5545 name = (cp_parser_id_expression
5547 cp_parser_optional_template_keyword (parser),
5548 /*check_dependency_p=*/true,
5550 /*declarator_p=*/false,
5551 /*optional_p=*/false));
5552 /* In general, build a SCOPE_REF if the member name is qualified.
5553 However, if the name was not dependent and has already been
5554 resolved; there is no need to build the SCOPE_REF. For example;
5556 struct X { void f(); };
5557 template <typename T> void f(T* t) { t->X::f(); }
5559 Even though "t" is dependent, "X::f" is not and has been resolved
5560 to a BASELINK; there is no need to include scope information. */
5562 /* But we do need to remember that there was an explicit scope for
5563 virtual function calls. */
5565 *idk = CP_ID_KIND_QUALIFIED;
5567 /* If the name is a template-id that names a type, we will get a
5568 TYPE_DECL here. That is invalid code. */
5569 if (TREE_CODE (name) == TYPE_DECL)
5571 error_at (token->location, "invalid use of %qD", name);
5572 postfix_expression = error_mark_node;
5576 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5578 name = build_qualified_name (/*type=*/NULL_TREE,
5582 parser->scope = NULL_TREE;
5583 parser->qualifying_scope = NULL_TREE;
5584 parser->object_scope = NULL_TREE;
5586 if (scope && name && BASELINK_P (name))
5587 adjust_result_of_qualified_name_lookup
5588 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5590 = finish_class_member_access_expr (postfix_expression, name,
5592 tf_warning_or_error);
5596 /* We no longer need to look up names in the scope of the object on
5597 the left-hand side of the `.' or `->' operator. */
5598 parser->context->object_type = NULL_TREE;
5600 /* Outside of offsetof, these operators may not appear in
5601 constant-expressions. */
5603 && (cp_parser_non_integral_constant_expression
5604 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5605 postfix_expression = error_mark_node;
5607 return postfix_expression;
5610 /* Parse a parenthesized expression-list.
5613 assignment-expression
5614 expression-list, assignment-expression
5619 identifier, expression-list
5621 CAST_P is true if this expression is the target of a cast.
5623 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5626 Returns a vector of trees. Each element is a representation of an
5627 assignment-expression. NULL is returned if the ( and or ) are
5628 missing. An empty, but allocated, vector is returned on no
5629 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5630 if we are parsing an attribute list for an attribute that wants a
5631 plain identifier argument, normal_attr for an attribute that wants
5632 an expression, or non_attr if we aren't parsing an attribute list. If
5633 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5634 not all of the expressions in the list were constant. */
5636 static VEC(tree,gc) *
5637 cp_parser_parenthesized_expression_list (cp_parser* parser,
5638 int is_attribute_list,
5640 bool allow_expansion_p,
5641 bool *non_constant_p)
5643 VEC(tree,gc) *expression_list;
5644 bool fold_expr_p = is_attribute_list != non_attr;
5645 tree identifier = NULL_TREE;
5646 bool saved_greater_than_is_operator_p;
5648 /* Assume all the expressions will be constant. */
5650 *non_constant_p = false;
5652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5655 expression_list = make_tree_vector ();
5657 /* Within a parenthesized expression, a `>' token is always
5658 the greater-than operator. */
5659 saved_greater_than_is_operator_p
5660 = parser->greater_than_is_operator_p;
5661 parser->greater_than_is_operator_p = true;
5663 /* Consume expressions until there are no more. */
5664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5669 /* At the beginning of attribute lists, check to see if the
5670 next token is an identifier. */
5671 if (is_attribute_list == id_attr
5672 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5676 /* Consume the identifier. */
5677 token = cp_lexer_consume_token (parser->lexer);
5678 /* Save the identifier. */
5679 identifier = token->u.value;
5683 bool expr_non_constant_p;
5685 /* Parse the next assignment-expression. */
5686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5688 /* A braced-init-list. */
5689 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5690 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5691 if (non_constant_p && expr_non_constant_p)
5692 *non_constant_p = true;
5694 else if (non_constant_p)
5696 expr = (cp_parser_constant_expression
5697 (parser, /*allow_non_constant_p=*/true,
5698 &expr_non_constant_p));
5699 if (expr_non_constant_p)
5700 *non_constant_p = true;
5703 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5706 expr = fold_non_dependent_expr (expr);
5708 /* If we have an ellipsis, then this is an expression
5710 if (allow_expansion_p
5711 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5713 /* Consume the `...'. */
5714 cp_lexer_consume_token (parser->lexer);
5716 /* Build the argument pack. */
5717 expr = make_pack_expansion (expr);
5720 /* Add it to the list. We add error_mark_node
5721 expressions to the list, so that we can still tell if
5722 the correct form for a parenthesized expression-list
5723 is found. That gives better errors. */
5724 VEC_safe_push (tree, gc, expression_list, expr);
5726 if (expr == error_mark_node)
5730 /* After the first item, attribute lists look the same as
5731 expression lists. */
5732 is_attribute_list = non_attr;
5735 /* If the next token isn't a `,', then we are done. */
5736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5739 /* Otherwise, consume the `,' and keep going. */
5740 cp_lexer_consume_token (parser->lexer);
5743 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5748 /* We try and resync to an unnested comma, as that will give the
5749 user better diagnostics. */
5750 ending = cp_parser_skip_to_closing_parenthesis (parser,
5751 /*recovering=*/true,
5753 /*consume_paren=*/true);
5758 parser->greater_than_is_operator_p
5759 = saved_greater_than_is_operator_p;
5764 parser->greater_than_is_operator_p
5765 = saved_greater_than_is_operator_p;
5768 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5770 return expression_list;
5773 /* Parse a pseudo-destructor-name.
5775 pseudo-destructor-name:
5776 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5777 :: [opt] nested-name-specifier template template-id :: ~ type-name
5778 :: [opt] nested-name-specifier [opt] ~ type-name
5780 If either of the first two productions is used, sets *SCOPE to the
5781 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5782 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5783 or ERROR_MARK_NODE if the parse fails. */
5786 cp_parser_pseudo_destructor_name (cp_parser* parser,
5790 bool nested_name_specifier_p;
5792 /* Assume that things will not work out. */
5793 *type = error_mark_node;
5795 /* Look for the optional `::' operator. */
5796 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5797 /* Look for the optional nested-name-specifier. */
5798 nested_name_specifier_p
5799 = (cp_parser_nested_name_specifier_opt (parser,
5800 /*typename_keyword_p=*/false,
5801 /*check_dependency_p=*/true,
5803 /*is_declaration=*/false)
5805 /* Now, if we saw a nested-name-specifier, we might be doing the
5806 second production. */
5807 if (nested_name_specifier_p
5808 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5810 /* Consume the `template' keyword. */
5811 cp_lexer_consume_token (parser->lexer);
5812 /* Parse the template-id. */
5813 cp_parser_template_id (parser,
5814 /*template_keyword_p=*/true,
5815 /*check_dependency_p=*/false,
5816 /*is_declaration=*/true);
5817 /* Look for the `::' token. */
5818 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5820 /* If the next token is not a `~', then there might be some
5821 additional qualification. */
5822 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5824 /* At this point, we're looking for "type-name :: ~". The type-name
5825 must not be a class-name, since this is a pseudo-destructor. So,
5826 it must be either an enum-name, or a typedef-name -- both of which
5827 are just identifiers. So, we peek ahead to check that the "::"
5828 and "~" tokens are present; if they are not, then we can avoid
5829 calling type_name. */
5830 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5831 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5832 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5834 cp_parser_error (parser, "non-scalar type");
5838 /* Look for the type-name. */
5839 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5840 if (*scope == error_mark_node)
5843 /* Look for the `::' token. */
5844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5849 /* Look for the `~'. */
5850 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5851 /* Look for the type-name again. We are not responsible for
5852 checking that it matches the first type-name. */
5853 *type = cp_parser_nonclass_name (parser);
5856 /* Parse a unary-expression.
5862 unary-operator cast-expression
5863 sizeof unary-expression
5871 __extension__ cast-expression
5872 __alignof__ unary-expression
5873 __alignof__ ( type-id )
5874 __real__ cast-expression
5875 __imag__ cast-expression
5878 ADDRESS_P is true iff the unary-expression is appearing as the
5879 operand of the `&' operator. CAST_P is true if this expression is
5880 the target of a cast.
5882 Returns a representation of the expression. */
5885 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5889 enum tree_code unary_operator;
5891 /* Peek at the next token. */
5892 token = cp_lexer_peek_token (parser->lexer);
5893 /* Some keywords give away the kind of expression. */
5894 if (token->type == CPP_KEYWORD)
5896 enum rid keyword = token->keyword;
5906 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5907 /* Consume the token. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* Parse the operand. */
5910 operand = cp_parser_sizeof_operand (parser, keyword);
5912 if (TYPE_P (operand))
5913 return cxx_sizeof_or_alignof_type (operand, op, true);
5915 return cxx_sizeof_or_alignof_expr (operand, op, true);
5919 return cp_parser_new_expression (parser);
5922 return cp_parser_delete_expression (parser);
5926 /* The saved value of the PEDANTIC flag. */
5930 /* Save away the PEDANTIC flag. */
5931 cp_parser_extension_opt (parser, &saved_pedantic);
5932 /* Parse the cast-expression. */
5933 expr = cp_parser_simple_cast_expression (parser);
5934 /* Restore the PEDANTIC flag. */
5935 pedantic = saved_pedantic;
5945 /* Consume the `__real__' or `__imag__' token. */
5946 cp_lexer_consume_token (parser->lexer);
5947 /* Parse the cast-expression. */
5948 expression = cp_parser_simple_cast_expression (parser);
5949 /* Create the complete representation. */
5950 return build_x_unary_op ((keyword == RID_REALPART
5951 ? REALPART_EXPR : IMAGPART_EXPR),
5953 tf_warning_or_error);
5960 const char *saved_message;
5961 bool saved_integral_constant_expression_p;
5962 bool saved_non_integral_constant_expression_p;
5963 bool saved_greater_than_is_operator_p;
5965 cp_lexer_consume_token (parser->lexer);
5966 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5968 saved_message = parser->type_definition_forbidden_message;
5969 parser->type_definition_forbidden_message
5970 = G_("types may not be defined in %<noexcept%> expressions");
5972 saved_integral_constant_expression_p
5973 = parser->integral_constant_expression_p;
5974 saved_non_integral_constant_expression_p
5975 = parser->non_integral_constant_expression_p;
5976 parser->integral_constant_expression_p = false;
5978 saved_greater_than_is_operator_p
5979 = parser->greater_than_is_operator_p;
5980 parser->greater_than_is_operator_p = true;
5982 ++cp_unevaluated_operand;
5983 ++c_inhibit_evaluation_warnings;
5984 expr = cp_parser_expression (parser, false, NULL);
5985 --c_inhibit_evaluation_warnings;
5986 --cp_unevaluated_operand;
5988 parser->greater_than_is_operator_p
5989 = saved_greater_than_is_operator_p;
5991 parser->integral_constant_expression_p
5992 = saved_integral_constant_expression_p;
5993 parser->non_integral_constant_expression_p
5994 = saved_non_integral_constant_expression_p;
5996 parser->type_definition_forbidden_message = saved_message;
5998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5999 return finish_noexcept_expr (expr, tf_warning_or_error);
6007 /* Look for the `:: new' and `:: delete', which also signal the
6008 beginning of a new-expression, or delete-expression,
6009 respectively. If the next token is `::', then it might be one of
6011 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6015 /* See if the token after the `::' is one of the keywords in
6016 which we're interested. */
6017 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6018 /* If it's `new', we have a new-expression. */
6019 if (keyword == RID_NEW)
6020 return cp_parser_new_expression (parser);
6021 /* Similarly, for `delete'. */
6022 else if (keyword == RID_DELETE)
6023 return cp_parser_delete_expression (parser);
6026 /* Look for a unary operator. */
6027 unary_operator = cp_parser_unary_operator (token);
6028 /* The `++' and `--' operators can be handled similarly, even though
6029 they are not technically unary-operators in the grammar. */
6030 if (unary_operator == ERROR_MARK)
6032 if (token->type == CPP_PLUS_PLUS)
6033 unary_operator = PREINCREMENT_EXPR;
6034 else if (token->type == CPP_MINUS_MINUS)
6035 unary_operator = PREDECREMENT_EXPR;
6036 /* Handle the GNU address-of-label extension. */
6037 else if (cp_parser_allow_gnu_extensions_p (parser)
6038 && token->type == CPP_AND_AND)
6042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6044 /* Consume the '&&' token. */
6045 cp_lexer_consume_token (parser->lexer);
6046 /* Look for the identifier. */
6047 identifier = cp_parser_identifier (parser);
6048 /* Create an expression representing the address. */
6049 expression = finish_label_address_expr (identifier, loc);
6050 if (cp_parser_non_integral_constant_expression (parser,
6052 expression = error_mark_node;
6056 if (unary_operator != ERROR_MARK)
6058 tree cast_expression;
6059 tree expression = error_mark_node;
6060 non_integral_constant non_constant_p = NIC_NONE;
6062 /* Consume the operator token. */
6063 token = cp_lexer_consume_token (parser->lexer);
6064 /* Parse the cast-expression. */
6066 = cp_parser_cast_expression (parser,
6067 unary_operator == ADDR_EXPR,
6068 /*cast_p=*/false, pidk);
6069 /* Now, build an appropriate representation. */
6070 switch (unary_operator)
6073 non_constant_p = NIC_STAR;
6074 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6075 tf_warning_or_error);
6079 non_constant_p = NIC_ADDR;
6082 expression = build_x_unary_op (unary_operator, cast_expression,
6083 tf_warning_or_error);
6086 case PREINCREMENT_EXPR:
6087 case PREDECREMENT_EXPR:
6088 non_constant_p = unary_operator == PREINCREMENT_EXPR
6089 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6091 case UNARY_PLUS_EXPR:
6093 case TRUTH_NOT_EXPR:
6094 expression = finish_unary_op_expr (unary_operator, cast_expression);
6101 if (non_constant_p != NIC_NONE
6102 && cp_parser_non_integral_constant_expression (parser,
6104 expression = error_mark_node;
6109 return cp_parser_postfix_expression (parser, address_p, cast_p,
6110 /*member_access_only_p=*/false,
6114 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6115 unary-operator, the corresponding tree code is returned. */
6117 static enum tree_code
6118 cp_parser_unary_operator (cp_token* token)
6120 switch (token->type)
6123 return INDIRECT_REF;
6129 return UNARY_PLUS_EXPR;
6135 return TRUTH_NOT_EXPR;
6138 return BIT_NOT_EXPR;
6145 /* Parse a new-expression.
6148 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6149 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6151 Returns a representation of the expression. */
6154 cp_parser_new_expression (cp_parser* parser)
6156 bool global_scope_p;
6157 VEC(tree,gc) *placement;
6159 VEC(tree,gc) *initializer;
6163 /* Look for the optional `::' operator. */
6165 = (cp_parser_global_scope_opt (parser,
6166 /*current_scope_valid_p=*/false)
6168 /* Look for the `new' operator. */
6169 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6170 /* There's no easy way to tell a new-placement from the
6171 `( type-id )' construct. */
6172 cp_parser_parse_tentatively (parser);
6173 /* Look for a new-placement. */
6174 placement = cp_parser_new_placement (parser);
6175 /* If that didn't work out, there's no new-placement. */
6176 if (!cp_parser_parse_definitely (parser))
6178 if (placement != NULL)
6179 release_tree_vector (placement);
6183 /* If the next token is a `(', then we have a parenthesized
6185 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6188 /* Consume the `('. */
6189 cp_lexer_consume_token (parser->lexer);
6190 /* Parse the type-id. */
6191 type = cp_parser_type_id (parser);
6192 /* Look for the closing `)'. */
6193 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6194 token = cp_lexer_peek_token (parser->lexer);
6195 /* There should not be a direct-new-declarator in this production,
6196 but GCC used to allowed this, so we check and emit a sensible error
6197 message for this case. */
6198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6200 error_at (token->location,
6201 "array bound forbidden after parenthesized type-id");
6202 inform (token->location,
6203 "try removing the parentheses around the type-id");
6204 cp_parser_direct_new_declarator (parser);
6208 /* Otherwise, there must be a new-type-id. */
6210 type = cp_parser_new_type_id (parser, &nelts);
6212 /* If the next token is a `(' or '{', then we have a new-initializer. */
6213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6215 initializer = cp_parser_new_initializer (parser);
6219 /* A new-expression may not appear in an integral constant
6221 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6222 ret = error_mark_node;
6225 /* Create a representation of the new-expression. */
6226 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6227 tf_warning_or_error);
6230 if (placement != NULL)
6231 release_tree_vector (placement);
6232 if (initializer != NULL)
6233 release_tree_vector (initializer);
6238 /* Parse a new-placement.
6243 Returns the same representation as for an expression-list. */
6245 static VEC(tree,gc) *
6246 cp_parser_new_placement (cp_parser* parser)
6248 VEC(tree,gc) *expression_list;
6250 /* Parse the expression-list. */
6251 expression_list = (cp_parser_parenthesized_expression_list
6252 (parser, non_attr, /*cast_p=*/false,
6253 /*allow_expansion_p=*/true,
6254 /*non_constant_p=*/NULL));
6256 return expression_list;
6259 /* Parse a new-type-id.
6262 type-specifier-seq new-declarator [opt]
6264 Returns the TYPE allocated. If the new-type-id indicates an array
6265 type, *NELTS is set to the number of elements in the last array
6266 bound; the TYPE will not include the last array bound. */
6269 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6271 cp_decl_specifier_seq type_specifier_seq;
6272 cp_declarator *new_declarator;
6273 cp_declarator *declarator;
6274 cp_declarator *outer_declarator;
6275 const char *saved_message;
6278 /* The type-specifier sequence must not contain type definitions.
6279 (It cannot contain declarations of new types either, but if they
6280 are not definitions we will catch that because they are not
6282 saved_message = parser->type_definition_forbidden_message;
6283 parser->type_definition_forbidden_message
6284 = G_("types may not be defined in a new-type-id");
6285 /* Parse the type-specifier-seq. */
6286 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6287 /*is_trailing_return=*/false,
6288 &type_specifier_seq);
6289 /* Restore the old message. */
6290 parser->type_definition_forbidden_message = saved_message;
6291 /* Parse the new-declarator. */
6292 new_declarator = cp_parser_new_declarator_opt (parser);
6294 /* Determine the number of elements in the last array dimension, if
6297 /* Skip down to the last array dimension. */
6298 declarator = new_declarator;
6299 outer_declarator = NULL;
6300 while (declarator && (declarator->kind == cdk_pointer
6301 || declarator->kind == cdk_ptrmem))
6303 outer_declarator = declarator;
6304 declarator = declarator->declarator;
6307 && declarator->kind == cdk_array
6308 && declarator->declarator
6309 && declarator->declarator->kind == cdk_array)
6311 outer_declarator = declarator;
6312 declarator = declarator->declarator;
6315 if (declarator && declarator->kind == cdk_array)
6317 *nelts = declarator->u.array.bounds;
6318 if (*nelts == error_mark_node)
6319 *nelts = integer_one_node;
6321 if (outer_declarator)
6322 outer_declarator->declarator = declarator->declarator;
6324 new_declarator = NULL;
6327 type = groktypename (&type_specifier_seq, new_declarator, false);
6331 /* Parse an (optional) new-declarator.
6334 ptr-operator new-declarator [opt]
6335 direct-new-declarator
6337 Returns the declarator. */
6339 static cp_declarator *
6340 cp_parser_new_declarator_opt (cp_parser* parser)
6342 enum tree_code code;
6344 cp_cv_quals cv_quals;
6346 /* We don't know if there's a ptr-operator next, or not. */
6347 cp_parser_parse_tentatively (parser);
6348 /* Look for a ptr-operator. */
6349 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6350 /* If that worked, look for more new-declarators. */
6351 if (cp_parser_parse_definitely (parser))
6353 cp_declarator *declarator;
6355 /* Parse another optional declarator. */
6356 declarator = cp_parser_new_declarator_opt (parser);
6358 return cp_parser_make_indirect_declarator
6359 (code, type, cv_quals, declarator);
6362 /* If the next token is a `[', there is a direct-new-declarator. */
6363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6364 return cp_parser_direct_new_declarator (parser);
6369 /* Parse a direct-new-declarator.
6371 direct-new-declarator:
6373 direct-new-declarator [constant-expression]
6377 static cp_declarator *
6378 cp_parser_direct_new_declarator (cp_parser* parser)
6380 cp_declarator *declarator = NULL;
6386 /* Look for the opening `['. */
6387 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6388 /* The first expression is not required to be constant. */
6391 cp_token *token = cp_lexer_peek_token (parser->lexer);
6392 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6393 /* The standard requires that the expression have integral
6394 type. DR 74 adds enumeration types. We believe that the
6395 real intent is that these expressions be handled like the
6396 expression in a `switch' condition, which also allows
6397 classes with a single conversion to integral or
6398 enumeration type. */
6399 if (!processing_template_decl)
6402 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6407 error_at (token->location,
6408 "expression in new-declarator must have integral "
6409 "or enumeration type");
6410 expression = error_mark_node;
6414 /* But all the other expressions must be. */
6417 = cp_parser_constant_expression (parser,
6418 /*allow_non_constant=*/false,
6420 /* Look for the closing `]'. */
6421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6423 /* Add this bound to the declarator. */
6424 declarator = make_array_declarator (declarator, expression);
6426 /* If the next token is not a `[', then there are no more
6428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6435 /* Parse a new-initializer.
6438 ( expression-list [opt] )
6441 Returns a representation of the expression-list. */
6443 static VEC(tree,gc) *
6444 cp_parser_new_initializer (cp_parser* parser)
6446 VEC(tree,gc) *expression_list;
6448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6451 bool expr_non_constant_p;
6452 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6453 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6454 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6455 expression_list = make_tree_vector_single (t);
6458 expression_list = (cp_parser_parenthesized_expression_list
6459 (parser, non_attr, /*cast_p=*/false,
6460 /*allow_expansion_p=*/true,
6461 /*non_constant_p=*/NULL));
6463 return expression_list;
6466 /* Parse a delete-expression.
6469 :: [opt] delete cast-expression
6470 :: [opt] delete [ ] cast-expression
6472 Returns a representation of the expression. */
6475 cp_parser_delete_expression (cp_parser* parser)
6477 bool global_scope_p;
6481 /* Look for the optional `::' operator. */
6483 = (cp_parser_global_scope_opt (parser,
6484 /*current_scope_valid_p=*/false)
6486 /* Look for the `delete' keyword. */
6487 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6488 /* See if the array syntax is in use. */
6489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6491 /* Consume the `[' token. */
6492 cp_lexer_consume_token (parser->lexer);
6493 /* Look for the `]' token. */
6494 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6495 /* Remember that this is the `[]' construct. */
6501 /* Parse the cast-expression. */
6502 expression = cp_parser_simple_cast_expression (parser);
6504 /* A delete-expression may not appear in an integral constant
6506 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6507 return error_mark_node;
6509 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6512 /* Returns true if TOKEN may start a cast-expression and false
6516 cp_parser_token_starts_cast_expression (cp_token *token)
6518 switch (token->type)
6524 case CPP_CLOSE_SQUARE:
6525 case CPP_CLOSE_PAREN:
6526 case CPP_CLOSE_BRACE:
6530 case CPP_DEREF_STAR:
6538 case CPP_GREATER_EQ:
6558 /* '[' may start a primary-expression in obj-c++. */
6559 case CPP_OPEN_SQUARE:
6560 return c_dialect_objc ();
6567 /* Parse a cast-expression.
6571 ( type-id ) cast-expression
6573 ADDRESS_P is true iff the unary-expression is appearing as the
6574 operand of the `&' operator. CAST_P is true if this expression is
6575 the target of a cast.
6577 Returns a representation of the expression. */
6580 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6583 /* If it's a `(', then we might be looking at a cast. */
6584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6586 tree type = NULL_TREE;
6587 tree expr = NULL_TREE;
6588 bool compound_literal_p;
6589 const char *saved_message;
6591 /* There's no way to know yet whether or not this is a cast.
6592 For example, `(int (3))' is a unary-expression, while `(int)
6593 3' is a cast. So, we resort to parsing tentatively. */
6594 cp_parser_parse_tentatively (parser);
6595 /* Types may not be defined in a cast. */
6596 saved_message = parser->type_definition_forbidden_message;
6597 parser->type_definition_forbidden_message
6598 = G_("types may not be defined in casts");
6599 /* Consume the `('. */
6600 cp_lexer_consume_token (parser->lexer);
6601 /* A very tricky bit is that `(struct S) { 3 }' is a
6602 compound-literal (which we permit in C++ as an extension).
6603 But, that construct is not a cast-expression -- it is a
6604 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6605 is legal; if the compound-literal were a cast-expression,
6606 you'd need an extra set of parentheses.) But, if we parse
6607 the type-id, and it happens to be a class-specifier, then we
6608 will commit to the parse at that point, because we cannot
6609 undo the action that is done when creating a new class. So,
6610 then we cannot back up and do a postfix-expression.
6612 Therefore, we scan ahead to the closing `)', and check to see
6613 if the token after the `)' is a `{'. If so, we are not
6614 looking at a cast-expression.
6616 Save tokens so that we can put them back. */
6617 cp_lexer_save_tokens (parser->lexer);
6618 /* Skip tokens until the next token is a closing parenthesis.
6619 If we find the closing `)', and the next token is a `{', then
6620 we are looking at a compound-literal. */
6622 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6623 /*consume_paren=*/true)
6624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6625 /* Roll back the tokens we skipped. */
6626 cp_lexer_rollback_tokens (parser->lexer);
6627 /* If we were looking at a compound-literal, simulate an error
6628 so that the call to cp_parser_parse_definitely below will
6630 if (compound_literal_p)
6631 cp_parser_simulate_error (parser);
6634 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6635 parser->in_type_id_in_expr_p = true;
6636 /* Look for the type-id. */
6637 type = cp_parser_type_id (parser);
6638 /* Look for the closing `)'. */
6639 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6640 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6643 /* Restore the saved message. */
6644 parser->type_definition_forbidden_message = saved_message;
6646 /* At this point this can only be either a cast or a
6647 parenthesized ctor such as `(T ())' that looks like a cast to
6648 function returning T. */
6649 if (!cp_parser_error_occurred (parser)
6650 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6653 cp_parser_parse_definitely (parser);
6654 expr = cp_parser_cast_expression (parser,
6655 /*address_p=*/false,
6656 /*cast_p=*/true, pidk);
6658 /* Warn about old-style casts, if so requested. */
6659 if (warn_old_style_cast
6660 && !in_system_header
6661 && !VOID_TYPE_P (type)
6662 && current_lang_name != lang_name_c)
6663 warning (OPT_Wold_style_cast, "use of old-style cast");
6665 /* Only type conversions to integral or enumeration types
6666 can be used in constant-expressions. */
6667 if (!cast_valid_in_integral_constant_expression_p (type)
6668 && cp_parser_non_integral_constant_expression (parser,
6670 return error_mark_node;
6672 /* Perform the cast. */
6673 expr = build_c_cast (input_location, type, expr);
6677 cp_parser_abort_tentative_parse (parser);
6680 /* If we get here, then it's not a cast, so it must be a
6681 unary-expression. */
6682 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6685 /* Parse a binary expression of the general form:
6689 pm-expression .* cast-expression
6690 pm-expression ->* cast-expression
6692 multiplicative-expression:
6694 multiplicative-expression * pm-expression
6695 multiplicative-expression / pm-expression
6696 multiplicative-expression % pm-expression
6698 additive-expression:
6699 multiplicative-expression
6700 additive-expression + multiplicative-expression
6701 additive-expression - multiplicative-expression
6705 shift-expression << additive-expression
6706 shift-expression >> additive-expression
6708 relational-expression:
6710 relational-expression < shift-expression
6711 relational-expression > shift-expression
6712 relational-expression <= shift-expression
6713 relational-expression >= shift-expression
6717 relational-expression:
6718 relational-expression <? shift-expression
6719 relational-expression >? shift-expression
6721 equality-expression:
6722 relational-expression
6723 equality-expression == relational-expression
6724 equality-expression != relational-expression
6728 and-expression & equality-expression
6730 exclusive-or-expression:
6732 exclusive-or-expression ^ and-expression
6734 inclusive-or-expression:
6735 exclusive-or-expression
6736 inclusive-or-expression | exclusive-or-expression
6738 logical-and-expression:
6739 inclusive-or-expression
6740 logical-and-expression && inclusive-or-expression
6742 logical-or-expression:
6743 logical-and-expression
6744 logical-or-expression || logical-and-expression
6746 All these are implemented with a single function like:
6749 simple-cast-expression
6750 binary-expression <token> binary-expression
6752 CAST_P is true if this expression is the target of a cast.
6754 The binops_by_token map is used to get the tree codes for each <token> type.
6755 binary-expressions are associated according to a precedence table. */
6757 #define TOKEN_PRECEDENCE(token) \
6758 (((token->type == CPP_GREATER \
6759 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6760 && !parser->greater_than_is_operator_p) \
6761 ? PREC_NOT_OPERATOR \
6762 : binops_by_token[token->type].prec)
6765 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6766 bool no_toplevel_fold_p,
6767 enum cp_parser_prec prec,
6770 cp_parser_expression_stack stack;
6771 cp_parser_expression_stack_entry *sp = &stack[0];
6774 enum tree_code tree_type, lhs_type, rhs_type;
6775 enum cp_parser_prec new_prec, lookahead_prec;
6778 /* Parse the first expression. */
6779 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6780 lhs_type = ERROR_MARK;
6784 /* Get an operator token. */
6785 token = cp_lexer_peek_token (parser->lexer);
6787 if (warn_cxx0x_compat
6788 && token->type == CPP_RSHIFT
6789 && !parser->greater_than_is_operator_p)
6791 if (warning_at (token->location, OPT_Wc__0x_compat,
6792 "%<>>%> operator will be treated as"
6793 " two right angle brackets in C++0x"))
6794 inform (token->location,
6795 "suggest parentheses around %<>>%> expression");
6798 new_prec = TOKEN_PRECEDENCE (token);
6800 /* Popping an entry off the stack means we completed a subexpression:
6801 - either we found a token which is not an operator (`>' where it is not
6802 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6803 will happen repeatedly;
6804 - or, we found an operator which has lower priority. This is the case
6805 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6807 if (new_prec <= prec)
6816 tree_type = binops_by_token[token->type].tree_type;
6818 /* We used the operator token. */
6819 cp_lexer_consume_token (parser->lexer);
6821 /* For "false && x" or "true || x", x will never be executed;
6822 disable warnings while evaluating it. */
6823 if (tree_type == TRUTH_ANDIF_EXPR)
6824 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6825 else if (tree_type == TRUTH_ORIF_EXPR)
6826 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6828 /* Extract another operand. It may be the RHS of this expression
6829 or the LHS of a new, higher priority expression. */
6830 rhs = cp_parser_simple_cast_expression (parser);
6831 rhs_type = ERROR_MARK;
6833 /* Get another operator token. Look up its precedence to avoid
6834 building a useless (immediately popped) stack entry for common
6835 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6836 token = cp_lexer_peek_token (parser->lexer);
6837 lookahead_prec = TOKEN_PRECEDENCE (token);
6838 if (lookahead_prec > new_prec)
6840 /* ... and prepare to parse the RHS of the new, higher priority
6841 expression. Since precedence levels on the stack are
6842 monotonically increasing, we do not have to care about
6845 sp->tree_type = tree_type;
6847 sp->lhs_type = lhs_type;
6850 lhs_type = rhs_type;
6852 new_prec = lookahead_prec;
6856 lookahead_prec = new_prec;
6857 /* If the stack is not empty, we have parsed into LHS the right side
6858 (`4' in the example above) of an expression we had suspended.
6859 We can use the information on the stack to recover the LHS (`3')
6860 from the stack together with the tree code (`MULT_EXPR'), and
6861 the precedence of the higher level subexpression
6862 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6863 which will be used to actually build the additive expression. */
6866 tree_type = sp->tree_type;
6868 rhs_type = lhs_type;
6870 lhs_type = sp->lhs_type;
6873 /* Undo the disabling of warnings done above. */
6874 if (tree_type == TRUTH_ANDIF_EXPR)
6875 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6876 else if (tree_type == TRUTH_ORIF_EXPR)
6877 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6879 overloaded_p = false;
6880 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6881 ERROR_MARK for everything that is not a binary expression.
6882 This makes warn_about_parentheses miss some warnings that
6883 involve unary operators. For unary expressions we should
6884 pass the correct tree_code unless the unary expression was
6885 surrounded by parentheses.
6887 if (no_toplevel_fold_p
6888 && lookahead_prec <= prec
6890 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6891 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6893 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6894 &overloaded_p, tf_warning_or_error);
6895 lhs_type = tree_type;
6897 /* If the binary operator required the use of an overloaded operator,
6898 then this expression cannot be an integral constant-expression.
6899 An overloaded operator can be used even if both operands are
6900 otherwise permissible in an integral constant-expression if at
6901 least one of the operands is of enumeration type. */
6904 && cp_parser_non_integral_constant_expression (parser,
6906 return error_mark_node;
6913 /* Parse the `? expression : assignment-expression' part of a
6914 conditional-expression. The LOGICAL_OR_EXPR is the
6915 logical-or-expression that started the conditional-expression.
6916 Returns a representation of the entire conditional-expression.
6918 This routine is used by cp_parser_assignment_expression.
6920 ? expression : assignment-expression
6924 ? : assignment-expression */
6927 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6930 tree assignment_expr;
6931 struct cp_token *token;
6933 /* Consume the `?' token. */
6934 cp_lexer_consume_token (parser->lexer);
6935 token = cp_lexer_peek_token (parser->lexer);
6936 if (cp_parser_allow_gnu_extensions_p (parser)
6937 && token->type == CPP_COLON)
6939 pedwarn (token->location, OPT_pedantic,
6940 "ISO C++ does not allow ?: with omitted middle operand");
6941 /* Implicit true clause. */
6943 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6944 warn_for_omitted_condop (token->location, logical_or_expr);
6948 /* Parse the expression. */
6949 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6950 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6951 c_inhibit_evaluation_warnings +=
6952 ((logical_or_expr == truthvalue_true_node)
6953 - (logical_or_expr == truthvalue_false_node));
6956 /* The next token should be a `:'. */
6957 cp_parser_require (parser, CPP_COLON, RT_COLON);
6958 /* Parse the assignment-expression. */
6959 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6960 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6962 /* Build the conditional-expression. */
6963 return build_x_conditional_expr (logical_or_expr,
6966 tf_warning_or_error);
6969 /* Parse an assignment-expression.
6971 assignment-expression:
6972 conditional-expression
6973 logical-or-expression assignment-operator assignment_expression
6976 CAST_P is true if this expression is the target of a cast.
6978 Returns a representation for the expression. */
6981 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6986 /* If the next token is the `throw' keyword, then we're looking at
6987 a throw-expression. */
6988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6989 expr = cp_parser_throw_expression (parser);
6990 /* Otherwise, it must be that we are looking at a
6991 logical-or-expression. */
6994 /* Parse the binary expressions (logical-or-expression). */
6995 expr = cp_parser_binary_expression (parser, cast_p, false,
6996 PREC_NOT_OPERATOR, pidk);
6997 /* If the next token is a `?' then we're actually looking at a
6998 conditional-expression. */
6999 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7000 return cp_parser_question_colon_clause (parser, expr);
7003 enum tree_code assignment_operator;
7005 /* If it's an assignment-operator, we're using the second
7008 = cp_parser_assignment_operator_opt (parser);
7009 if (assignment_operator != ERROR_MARK)
7011 bool non_constant_p;
7013 /* Parse the right-hand side of the assignment. */
7014 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7016 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7017 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7019 /* An assignment may not appear in a
7020 constant-expression. */
7021 if (cp_parser_non_integral_constant_expression (parser,
7023 return error_mark_node;
7024 /* Build the assignment expression. */
7025 expr = build_x_modify_expr (expr,
7026 assignment_operator,
7028 tf_warning_or_error);
7036 /* Parse an (optional) assignment-operator.
7038 assignment-operator: one of
7039 = *= /= %= += -= >>= <<= &= ^= |=
7043 assignment-operator: one of
7046 If the next token is an assignment operator, the corresponding tree
7047 code is returned, and the token is consumed. For example, for
7048 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7049 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7050 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7051 operator, ERROR_MARK is returned. */
7053 static enum tree_code
7054 cp_parser_assignment_operator_opt (cp_parser* parser)
7059 /* Peek at the next token. */
7060 token = cp_lexer_peek_token (parser->lexer);
7062 switch (token->type)
7073 op = TRUNC_DIV_EXPR;
7077 op = TRUNC_MOD_EXPR;
7109 /* Nothing else is an assignment operator. */
7113 /* If it was an assignment operator, consume it. */
7114 if (op != ERROR_MARK)
7115 cp_lexer_consume_token (parser->lexer);
7120 /* Parse an expression.
7123 assignment-expression
7124 expression , assignment-expression
7126 CAST_P is true if this expression is the target of a cast.
7128 Returns a representation of the expression. */
7131 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7133 tree expression = NULL_TREE;
7137 tree assignment_expression;
7139 /* Parse the next assignment-expression. */
7140 assignment_expression
7141 = cp_parser_assignment_expression (parser, cast_p, pidk);
7142 /* If this is the first assignment-expression, we can just
7145 expression = assignment_expression;
7147 expression = build_x_compound_expr (expression,
7148 assignment_expression,
7149 tf_warning_or_error);
7150 /* If the next token is not a comma, then we are done with the
7152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7154 /* Consume the `,'. */
7155 cp_lexer_consume_token (parser->lexer);
7156 /* A comma operator cannot appear in a constant-expression. */
7157 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7158 expression = error_mark_node;
7164 /* Parse a constant-expression.
7166 constant-expression:
7167 conditional-expression
7169 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7170 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7171 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7172 is false, NON_CONSTANT_P should be NULL. */
7175 cp_parser_constant_expression (cp_parser* parser,
7176 bool allow_non_constant_p,
7177 bool *non_constant_p)
7179 bool saved_integral_constant_expression_p;
7180 bool saved_allow_non_integral_constant_expression_p;
7181 bool saved_non_integral_constant_expression_p;
7184 /* It might seem that we could simply parse the
7185 conditional-expression, and then check to see if it were
7186 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7187 one that the compiler can figure out is constant, possibly after
7188 doing some simplifications or optimizations. The standard has a
7189 precise definition of constant-expression, and we must honor
7190 that, even though it is somewhat more restrictive.
7196 is not a legal declaration, because `(2, 3)' is not a
7197 constant-expression. The `,' operator is forbidden in a
7198 constant-expression. However, GCC's constant-folding machinery
7199 will fold this operation to an INTEGER_CST for `3'. */
7201 /* Save the old settings. */
7202 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7203 saved_allow_non_integral_constant_expression_p
7204 = parser->allow_non_integral_constant_expression_p;
7205 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7206 /* We are now parsing a constant-expression. */
7207 parser->integral_constant_expression_p = true;
7208 parser->allow_non_integral_constant_expression_p
7209 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7210 parser->non_integral_constant_expression_p = false;
7211 /* Although the grammar says "conditional-expression", we parse an
7212 "assignment-expression", which also permits "throw-expression"
7213 and the use of assignment operators. In the case that
7214 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7215 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7216 actually essential that we look for an assignment-expression.
7217 For example, cp_parser_initializer_clauses uses this function to
7218 determine whether a particular assignment-expression is in fact
7220 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7221 /* Restore the old settings. */
7222 parser->integral_constant_expression_p
7223 = saved_integral_constant_expression_p;
7224 parser->allow_non_integral_constant_expression_p
7225 = saved_allow_non_integral_constant_expression_p;
7226 if (allow_non_constant_p)
7227 *non_constant_p = parser->non_integral_constant_expression_p;
7228 else if (parser->non_integral_constant_expression_p
7229 && cxx_dialect < cxx0x)
7230 expression = error_mark_node;
7231 parser->non_integral_constant_expression_p
7232 = saved_non_integral_constant_expression_p;
7237 /* Parse __builtin_offsetof.
7239 offsetof-expression:
7240 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7242 offsetof-member-designator:
7244 | offsetof-member-designator "." id-expression
7245 | offsetof-member-designator "[" expression "]"
7246 | offsetof-member-designator "->" id-expression */
7249 cp_parser_builtin_offsetof (cp_parser *parser)
7251 int save_ice_p, save_non_ice_p;
7256 /* We're about to accept non-integral-constant things, but will
7257 definitely yield an integral constant expression. Save and
7258 restore these values around our local parsing. */
7259 save_ice_p = parser->integral_constant_expression_p;
7260 save_non_ice_p = parser->non_integral_constant_expression_p;
7262 /* Consume the "__builtin_offsetof" token. */
7263 cp_lexer_consume_token (parser->lexer);
7264 /* Consume the opening `('. */
7265 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7266 /* Parse the type-id. */
7267 type = cp_parser_type_id (parser);
7268 /* Look for the `,'. */
7269 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7270 token = cp_lexer_peek_token (parser->lexer);
7272 /* Build the (type *)null that begins the traditional offsetof macro. */
7273 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7274 tf_warning_or_error);
7276 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7277 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7278 true, &dummy, token->location);
7281 token = cp_lexer_peek_token (parser->lexer);
7282 switch (token->type)
7284 case CPP_OPEN_SQUARE:
7285 /* offsetof-member-designator "[" expression "]" */
7286 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7290 /* offsetof-member-designator "->" identifier */
7291 expr = grok_array_decl (expr, integer_zero_node);
7295 /* offsetof-member-designator "." identifier */
7296 cp_lexer_consume_token (parser->lexer);
7297 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7302 case CPP_CLOSE_PAREN:
7303 /* Consume the ")" token. */
7304 cp_lexer_consume_token (parser->lexer);
7308 /* Error. We know the following require will fail, but
7309 that gives the proper error message. */
7310 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7311 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7312 expr = error_mark_node;
7318 /* If we're processing a template, we can't finish the semantics yet.
7319 Otherwise we can fold the entire expression now. */
7320 if (processing_template_decl)
7321 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7323 expr = finish_offsetof (expr);
7326 parser->integral_constant_expression_p = save_ice_p;
7327 parser->non_integral_constant_expression_p = save_non_ice_p;
7332 /* Parse a trait expression. */
7335 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7338 tree type1, type2 = NULL_TREE;
7339 bool binary = false;
7340 cp_decl_specifier_seq decl_specs;
7344 case RID_HAS_NOTHROW_ASSIGN:
7345 kind = CPTK_HAS_NOTHROW_ASSIGN;
7347 case RID_HAS_NOTHROW_CONSTRUCTOR:
7348 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7350 case RID_HAS_NOTHROW_COPY:
7351 kind = CPTK_HAS_NOTHROW_COPY;
7353 case RID_HAS_TRIVIAL_ASSIGN:
7354 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7356 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7357 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7359 case RID_HAS_TRIVIAL_COPY:
7360 kind = CPTK_HAS_TRIVIAL_COPY;
7362 case RID_HAS_TRIVIAL_DESTRUCTOR:
7363 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7365 case RID_HAS_VIRTUAL_DESTRUCTOR:
7366 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7368 case RID_IS_ABSTRACT:
7369 kind = CPTK_IS_ABSTRACT;
7371 case RID_IS_BASE_OF:
7372 kind = CPTK_IS_BASE_OF;
7376 kind = CPTK_IS_CLASS;
7378 case RID_IS_CONVERTIBLE_TO:
7379 kind = CPTK_IS_CONVERTIBLE_TO;
7383 kind = CPTK_IS_EMPTY;
7386 kind = CPTK_IS_ENUM;
7391 case RID_IS_POLYMORPHIC:
7392 kind = CPTK_IS_POLYMORPHIC;
7394 case RID_IS_STD_LAYOUT:
7395 kind = CPTK_IS_STD_LAYOUT;
7397 case RID_IS_TRIVIAL:
7398 kind = CPTK_IS_TRIVIAL;
7401 kind = CPTK_IS_UNION;
7403 case RID_IS_LITERAL_TYPE:
7404 kind = CPTK_IS_LITERAL_TYPE;
7410 /* Consume the token. */
7411 cp_lexer_consume_token (parser->lexer);
7413 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7415 type1 = cp_parser_type_id (parser);
7417 if (type1 == error_mark_node)
7418 return error_mark_node;
7420 /* Build a trivial decl-specifier-seq. */
7421 clear_decl_specs (&decl_specs);
7422 decl_specs.type = type1;
7424 /* Call grokdeclarator to figure out what type this is. */
7425 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7426 /*initialized=*/0, /*attrlist=*/NULL);
7430 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7432 type2 = cp_parser_type_id (parser);
7434 if (type2 == error_mark_node)
7435 return error_mark_node;
7437 /* Build a trivial decl-specifier-seq. */
7438 clear_decl_specs (&decl_specs);
7439 decl_specs.type = type2;
7441 /* Call grokdeclarator to figure out what type this is. */
7442 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7443 /*initialized=*/0, /*attrlist=*/NULL);
7446 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7448 /* Complete the trait expression, which may mean either processing
7449 the trait expr now or saving it for template instantiation. */
7450 return finish_trait_expr (kind, type1, type2);
7453 /* Lambdas that appear in variable initializer or default argument scope
7454 get that in their mangling, so we need to record it. We might as well
7455 use the count for function and namespace scopes as well. */
7456 static GTY(()) tree lambda_scope;
7457 static GTY(()) int lambda_count;
7458 typedef struct GTY(()) tree_int
7463 DEF_VEC_O(tree_int);
7464 DEF_VEC_ALLOC_O(tree_int,gc);
7465 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7468 start_lambda_scope (tree decl)
7472 /* Once we're inside a function, we ignore other scopes and just push
7473 the function again so that popping works properly. */
7474 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7475 decl = current_function_decl;
7476 ti.t = lambda_scope;
7477 ti.i = lambda_count;
7478 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7479 if (lambda_scope != decl)
7481 /* Don't reset the count if we're still in the same function. */
7482 lambda_scope = decl;
7488 record_lambda_scope (tree lambda)
7490 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7491 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7495 finish_lambda_scope (void)
7497 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7498 if (lambda_scope != p->t)
7500 lambda_scope = p->t;
7501 lambda_count = p->i;
7503 VEC_pop (tree_int, lambda_scope_stack);
7506 /* Parse a lambda expression.
7509 lambda-introducer lambda-declarator [opt] compound-statement
7511 Returns a representation of the expression. */
7514 cp_parser_lambda_expression (cp_parser* parser)
7516 tree lambda_expr = build_lambda_expr ();
7519 LAMBDA_EXPR_LOCATION (lambda_expr)
7520 = cp_lexer_peek_token (parser->lexer)->location;
7522 if (cp_unevaluated_operand)
7523 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7524 "lambda-expression in unevaluated context");
7526 /* We may be in the middle of deferred access check. Disable
7528 push_deferring_access_checks (dk_no_deferred);
7530 cp_parser_lambda_introducer (parser, lambda_expr);
7532 type = begin_lambda_type (lambda_expr);
7534 record_lambda_scope (lambda_expr);
7536 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7537 determine_visibility (TYPE_NAME (type));
7539 /* Now that we've started the type, add the capture fields for any
7540 explicit captures. */
7541 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7544 /* Inside the class, surrounding template-parameter-lists do not apply. */
7545 unsigned int saved_num_template_parameter_lists
7546 = parser->num_template_parameter_lists;
7548 parser->num_template_parameter_lists = 0;
7550 /* By virtue of defining a local class, a lambda expression has access to
7551 the private variables of enclosing classes. */
7553 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7555 cp_parser_lambda_body (parser, lambda_expr);
7557 /* The capture list was built up in reverse order; fix that now. */
7559 tree newlist = NULL_TREE;
7562 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7565 tree field = TREE_PURPOSE (elt);
7568 next = TREE_CHAIN (elt);
7569 TREE_CHAIN (elt) = newlist;
7572 /* Also add __ to the beginning of the field name so that code
7573 outside the lambda body can't see the captured name. We could
7574 just remove the name entirely, but this is more useful for
7576 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7577 /* The 'this' capture already starts with __. */
7580 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7581 buf[1] = buf[0] = '_';
7582 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7583 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7584 DECL_NAME (field) = get_identifier (buf);
7586 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7589 maybe_add_lambda_conv_op (type);
7591 type = finish_struct (type, /*attributes=*/NULL_TREE);
7593 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7596 pop_deferring_access_checks ();
7598 return build_lambda_object (lambda_expr);
7601 /* Parse the beginning of a lambda expression.
7604 [ lambda-capture [opt] ]
7606 LAMBDA_EXPR is the current representation of the lambda expression. */
7609 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7611 /* Need commas after the first capture. */
7614 /* Eat the leading `['. */
7615 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7617 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7618 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7619 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7620 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7621 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7622 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7624 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7626 cp_lexer_consume_token (parser->lexer);
7630 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7632 cp_token* capture_token;
7634 tree capture_init_expr;
7635 cp_id_kind idk = CP_ID_KIND_NONE;
7636 bool explicit_init_p = false;
7638 enum capture_kind_type
7643 enum capture_kind_type capture_kind = BY_COPY;
7645 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7647 error ("expected end of capture-list");
7654 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7656 /* Possibly capture `this'. */
7657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7659 cp_lexer_consume_token (parser->lexer);
7660 add_capture (lambda_expr,
7661 /*id=*/get_identifier ("__this"),
7662 /*initializer=*/finish_this_expr(),
7663 /*by_reference_p=*/false,
7668 /* Remember whether we want to capture as a reference or not. */
7669 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7671 capture_kind = BY_REFERENCE;
7672 cp_lexer_consume_token (parser->lexer);
7675 /* Get the identifier. */
7676 capture_token = cp_lexer_peek_token (parser->lexer);
7677 capture_id = cp_parser_identifier (parser);
7679 if (capture_id == error_mark_node)
7680 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7681 delimiters, but I modified this to stop on unnested ']' as well. It
7682 was already changed to stop on unnested '}', so the
7683 "closing_parenthesis" name is no more misleading with my change. */
7685 cp_parser_skip_to_closing_parenthesis (parser,
7686 /*recovering=*/true,
7688 /*consume_paren=*/true);
7692 /* Find the initializer for this capture. */
7693 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7695 /* An explicit expression exists. */
7696 cp_lexer_consume_token (parser->lexer);
7697 pedwarn (input_location, OPT_pedantic,
7698 "ISO C++ does not allow initializers "
7699 "in lambda expression capture lists");
7700 capture_init_expr = cp_parser_assignment_expression (parser,
7703 explicit_init_p = true;
7707 const char* error_msg;
7709 /* Turn the identifier into an id-expression. */
7711 = cp_parser_lookup_name
7715 /*is_template=*/false,
7716 /*is_namespace=*/false,
7717 /*check_dependency=*/true,
7718 /*ambiguous_decls=*/NULL,
7719 capture_token->location);
7722 = finish_id_expression
7727 /*integral_constant_expression_p=*/false,
7728 /*allow_non_integral_constant_expression_p=*/false,
7729 /*non_integral_constant_expression_p=*/NULL,
7730 /*template_p=*/false,
7732 /*address_p=*/false,
7733 /*template_arg_p=*/false,
7735 capture_token->location);
7738 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7740 = unqualified_name_lookup_error (capture_init_expr);
7742 add_capture (lambda_expr,
7745 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7749 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7752 /* Parse the (optional) middle of a lambda expression.
7755 ( parameter-declaration-clause [opt] )
7756 attribute-specifier [opt]
7758 exception-specification [opt]
7759 lambda-return-type-clause [opt]
7761 LAMBDA_EXPR is the current representation of the lambda expression. */
7764 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7766 /* 5.1.1.4 of the standard says:
7767 If a lambda-expression does not include a lambda-declarator, it is as if
7768 the lambda-declarator were ().
7769 This means an empty parameter list, no attributes, and no exception
7771 tree param_list = void_list_node;
7772 tree attributes = NULL_TREE;
7773 tree exception_spec = NULL_TREE;
7776 /* The lambda-declarator is optional, but must begin with an opening
7777 parenthesis if present. */
7778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7780 cp_lexer_consume_token (parser->lexer);
7782 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7784 /* Parse parameters. */
7785 param_list = cp_parser_parameter_declaration_clause (parser);
7787 /* Default arguments shall not be specified in the
7788 parameter-declaration-clause of a lambda-declarator. */
7789 for (t = param_list; t; t = TREE_CHAIN (t))
7790 if (TREE_PURPOSE (t))
7791 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7792 "default argument specified for lambda parameter");
7794 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7796 attributes = cp_parser_attributes_opt (parser);
7798 /* Parse optional `mutable' keyword. */
7799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7801 cp_lexer_consume_token (parser->lexer);
7802 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7805 /* Parse optional exception specification. */
7806 exception_spec = cp_parser_exception_specification_opt (parser);
7808 /* Parse optional trailing return type. */
7809 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7811 cp_lexer_consume_token (parser->lexer);
7812 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7815 /* The function parameters must be in scope all the way until after the
7816 trailing-return-type in case of decltype. */
7817 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7818 pop_binding (DECL_NAME (t), t);
7823 /* Create the function call operator.
7825 Messing with declarators like this is no uglier than building up the
7826 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7829 cp_decl_specifier_seq return_type_specs;
7830 cp_declarator* declarator;
7835 clear_decl_specs (&return_type_specs);
7836 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7837 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7839 /* Maybe we will deduce the return type later, but we can use void
7840 as a placeholder return type anyways. */
7841 return_type_specs.type = void_type_node;
7843 p = obstack_alloc (&declarator_obstack, 0);
7845 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7848 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7849 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7850 declarator = make_call_declarator (declarator, param_list, quals,
7852 /*late_return_type=*/NULL_TREE);
7853 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7855 fco = grokmethod (&return_type_specs,
7858 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7859 DECL_ARTIFICIAL (fco) = 1;
7861 finish_member_declaration (fco);
7863 obstack_free (&declarator_obstack, p);
7867 /* Parse the body of a lambda expression, which is simply
7871 but which requires special handling.
7872 LAMBDA_EXPR is the current representation of the lambda expression. */
7875 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7877 bool nested = (current_function_decl != NULL_TREE);
7879 push_function_context ();
7881 /* Finish the function call operator
7883 + late_parsing_for_member
7884 + function_definition_after_declarator
7885 + ctor_initializer_opt_and_function_body */
7887 tree fco = lambda_function (lambda_expr);
7891 /* Let the front end know that we are going to be defining this
7893 start_preparsed_function (fco,
7895 SF_PRE_PARSED | SF_INCLASS_INLINE);
7897 start_lambda_scope (fco);
7898 body = begin_function_body ();
7900 /* 5.1.1.4 of the standard says:
7901 If a lambda-expression does not include a trailing-return-type, it
7902 is as if the trailing-return-type denotes the following type:
7903 * if the compound-statement is of the form
7904 { return attribute-specifier [opt] expression ; }
7905 the type of the returned expression after lvalue-to-rvalue
7906 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7907 (_conv.array_ 4.2), and function-to-pointer conversion
7909 * otherwise, void. */
7911 /* In a lambda that has neither a lambda-return-type-clause
7912 nor a deducible form, errors should be reported for return statements
7913 in the body. Since we used void as the placeholder return type, parsing
7914 the body as usual will give such desired behavior. */
7915 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7916 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7917 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7918 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7921 tree expr = NULL_TREE;
7922 cp_id_kind idk = CP_ID_KIND_NONE;
7924 /* Parse tentatively in case there's more after the initial return
7926 cp_parser_parse_tentatively (parser);
7928 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7929 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7931 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7933 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7934 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7936 if (cp_parser_parse_definitely (parser))
7938 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7940 compound_stmt = begin_compound_stmt (0);
7941 /* Will get error here if type not deduced yet. */
7942 finish_return_stmt (expr);
7943 finish_compound_stmt (compound_stmt);
7951 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7952 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7953 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7954 cp_parser_compound_stmt does not pass it. */
7955 cp_parser_function_body (parser);
7956 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7959 finish_function_body (body);
7960 finish_lambda_scope ();
7962 /* Finish the function and generate code for it if necessary. */
7963 expand_or_defer_fn (finish_function (/*inline*/2));
7967 pop_function_context();
7970 /* Statements [gram.stmt.stmt] */
7972 /* Parse a statement.
7976 expression-statement
7981 declaration-statement
7984 IN_COMPOUND is true when the statement is nested inside a
7985 cp_parser_compound_statement; this matters for certain pragmas.
7987 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7988 is a (possibly labeled) if statement which is not enclosed in braces
7989 and has an else clause. This is used to implement -Wparentheses. */
7992 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7993 bool in_compound, bool *if_p)
7997 location_t statement_location;
8002 /* There is no statement yet. */
8003 statement = NULL_TREE;
8004 /* Peek at the next token. */
8005 token = cp_lexer_peek_token (parser->lexer);
8006 /* Remember the location of the first token in the statement. */
8007 statement_location = token->location;
8008 /* If this is a keyword, then that will often determine what kind of
8009 statement we have. */
8010 if (token->type == CPP_KEYWORD)
8012 enum rid keyword = token->keyword;
8018 /* Looks like a labeled-statement with a case label.
8019 Parse the label, and then use tail recursion to parse
8021 cp_parser_label_for_labeled_statement (parser);
8026 statement = cp_parser_selection_statement (parser, if_p);
8032 statement = cp_parser_iteration_statement (parser);
8039 statement = cp_parser_jump_statement (parser);
8042 /* Objective-C++ exception-handling constructs. */
8045 case RID_AT_FINALLY:
8046 case RID_AT_SYNCHRONIZED:
8048 statement = cp_parser_objc_statement (parser);
8052 statement = cp_parser_try_block (parser);
8056 /* This must be a namespace alias definition. */
8057 cp_parser_declaration_statement (parser);
8061 /* It might be a keyword like `int' that can start a
8062 declaration-statement. */
8066 else if (token->type == CPP_NAME)
8068 /* If the next token is a `:', then we are looking at a
8069 labeled-statement. */
8070 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8071 if (token->type == CPP_COLON)
8073 /* Looks like a labeled-statement with an ordinary label.
8074 Parse the label, and then use tail recursion to parse
8076 cp_parser_label_for_labeled_statement (parser);
8080 /* Anything that starts with a `{' must be a compound-statement. */
8081 else if (token->type == CPP_OPEN_BRACE)
8082 statement = cp_parser_compound_statement (parser, NULL, false);
8083 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8084 a statement all its own. */
8085 else if (token->type == CPP_PRAGMA)
8087 /* Only certain OpenMP pragmas are attached to statements, and thus
8088 are considered statements themselves. All others are not. In
8089 the context of a compound, accept the pragma as a "statement" and
8090 return so that we can check for a close brace. Otherwise we
8091 require a real statement and must go back and read one. */
8093 cp_parser_pragma (parser, pragma_compound);
8094 else if (!cp_parser_pragma (parser, pragma_stmt))
8098 else if (token->type == CPP_EOF)
8100 cp_parser_error (parser, "expected statement");
8104 /* Everything else must be a declaration-statement or an
8105 expression-statement. Try for the declaration-statement
8106 first, unless we are looking at a `;', in which case we know that
8107 we have an expression-statement. */
8110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8112 cp_parser_parse_tentatively (parser);
8113 /* Try to parse the declaration-statement. */
8114 cp_parser_declaration_statement (parser);
8115 /* If that worked, we're done. */
8116 if (cp_parser_parse_definitely (parser))
8119 /* Look for an expression-statement instead. */
8120 statement = cp_parser_expression_statement (parser, in_statement_expr);
8123 /* Set the line number for the statement. */
8124 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8125 SET_EXPR_LOCATION (statement, statement_location);
8128 /* Parse the label for a labeled-statement, i.e.
8131 case constant-expression :
8135 case constant-expression ... constant-expression : statement
8137 When a label is parsed without errors, the label is added to the
8138 parse tree by the finish_* functions, so this function doesn't
8139 have to return the label. */
8142 cp_parser_label_for_labeled_statement (cp_parser* parser)
8145 tree label = NULL_TREE;
8147 /* The next token should be an identifier. */
8148 token = cp_lexer_peek_token (parser->lexer);
8149 if (token->type != CPP_NAME
8150 && token->type != CPP_KEYWORD)
8152 cp_parser_error (parser, "expected labeled-statement");
8156 switch (token->keyword)
8163 /* Consume the `case' token. */
8164 cp_lexer_consume_token (parser->lexer);
8165 /* Parse the constant-expression. */
8166 expr = cp_parser_constant_expression (parser,
8167 /*allow_non_constant_p=*/false,
8170 ellipsis = cp_lexer_peek_token (parser->lexer);
8171 if (ellipsis->type == CPP_ELLIPSIS)
8173 /* Consume the `...' token. */
8174 cp_lexer_consume_token (parser->lexer);
8176 cp_parser_constant_expression (parser,
8177 /*allow_non_constant_p=*/false,
8179 /* We don't need to emit warnings here, as the common code
8180 will do this for us. */
8183 expr_hi = NULL_TREE;
8185 if (parser->in_switch_statement_p)
8186 finish_case_label (token->location, expr, expr_hi);
8188 error_at (token->location,
8189 "case label %qE not within a switch statement",
8195 /* Consume the `default' token. */
8196 cp_lexer_consume_token (parser->lexer);
8198 if (parser->in_switch_statement_p)
8199 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8201 error_at (token->location, "case label not within a switch statement");
8205 /* Anything else must be an ordinary label. */
8206 label = finish_label_stmt (cp_parser_identifier (parser));
8210 /* Require the `:' token. */
8211 cp_parser_require (parser, CPP_COLON, RT_COLON);
8213 /* An ordinary label may optionally be followed by attributes.
8214 However, this is only permitted if the attributes are then
8215 followed by a semicolon. This is because, for backward
8216 compatibility, when parsing
8217 lab: __attribute__ ((unused)) int i;
8218 we want the attribute to attach to "i", not "lab". */
8219 if (label != NULL_TREE
8220 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8224 cp_parser_parse_tentatively (parser);
8225 attrs = cp_parser_attributes_opt (parser);
8226 if (attrs == NULL_TREE
8227 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8228 cp_parser_abort_tentative_parse (parser);
8229 else if (!cp_parser_parse_definitely (parser))
8232 cplus_decl_attributes (&label, attrs, 0);
8236 /* Parse an expression-statement.
8238 expression-statement:
8241 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8242 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8243 indicates whether this expression-statement is part of an
8244 expression statement. */
8247 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8249 tree statement = NULL_TREE;
8250 cp_token *token = cp_lexer_peek_token (parser->lexer);
8252 /* If the next token is a ';', then there is no expression
8254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8255 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8257 /* Give a helpful message for "A<T>::type t;" and the like. */
8258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8259 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8261 if (TREE_CODE (statement) == SCOPE_REF)
8262 error_at (token->location, "need %<typename%> before %qE because "
8263 "%qT is a dependent scope",
8264 statement, TREE_OPERAND (statement, 0));
8265 else if (is_overloaded_fn (statement)
8266 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8269 tree fn = get_first_fn (statement);
8270 error_at (token->location,
8271 "%<%T::%D%> names the constructor, not the type",
8272 DECL_CONTEXT (fn), DECL_NAME (fn));
8276 /* Consume the final `;'. */
8277 cp_parser_consume_semicolon_at_end_of_statement (parser);
8279 if (in_statement_expr
8280 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8281 /* This is the final expression statement of a statement
8283 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8285 statement = finish_expr_stmt (statement);
8292 /* Parse a compound-statement.
8295 { statement-seq [opt] }
8300 { label-declaration-seq [opt] statement-seq [opt] }
8302 label-declaration-seq:
8304 label-declaration-seq label-declaration
8306 Returns a tree representing the statement. */
8309 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8314 /* Consume the `{'. */
8315 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8316 return error_mark_node;
8317 /* Begin the compound-statement. */
8318 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8319 /* If the next keyword is `__label__' we have a label declaration. */
8320 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8321 cp_parser_label_declaration (parser);
8322 /* Parse an (optional) statement-seq. */
8323 cp_parser_statement_seq_opt (parser, in_statement_expr);
8324 /* Finish the compound-statement. */
8325 finish_compound_stmt (compound_stmt);
8326 /* Consume the `}'. */
8327 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8329 return compound_stmt;
8332 /* Parse an (optional) statement-seq.
8336 statement-seq [opt] statement */
8339 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8341 /* Scan statements until there aren't any more. */
8344 cp_token *token = cp_lexer_peek_token (parser->lexer);
8346 /* If we are looking at a `}', then we have run out of
8347 statements; the same is true if we have reached the end
8348 of file, or have stumbled upon a stray '@end'. */
8349 if (token->type == CPP_CLOSE_BRACE
8350 || token->type == CPP_EOF
8351 || token->type == CPP_PRAGMA_EOL
8352 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8355 /* If we are in a compound statement and find 'else' then
8356 something went wrong. */
8357 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8359 if (parser->in_statement & IN_IF_STMT)
8363 token = cp_lexer_consume_token (parser->lexer);
8364 error_at (token->location, "%<else%> without a previous %<if%>");
8368 /* Parse the statement. */
8369 cp_parser_statement (parser, in_statement_expr, true, NULL);
8373 /* Parse a selection-statement.
8375 selection-statement:
8376 if ( condition ) statement
8377 if ( condition ) statement else statement
8378 switch ( condition ) statement
8380 Returns the new IF_STMT or SWITCH_STMT.
8382 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8383 is a (possibly labeled) if statement which is not enclosed in
8384 braces and has an else clause. This is used to implement
8388 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8396 /* Peek at the next token. */
8397 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8399 /* See what kind of keyword it is. */
8400 keyword = token->keyword;
8409 /* Look for the `('. */
8410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8412 cp_parser_skip_to_end_of_statement (parser);
8413 return error_mark_node;
8416 /* Begin the selection-statement. */
8417 if (keyword == RID_IF)
8418 statement = begin_if_stmt ();
8420 statement = begin_switch_stmt ();
8422 /* Parse the condition. */
8423 condition = cp_parser_condition (parser);
8424 /* Look for the `)'. */
8425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8426 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8427 /*consume_paren=*/true);
8429 if (keyword == RID_IF)
8432 unsigned char in_statement;
8434 /* Add the condition. */
8435 finish_if_stmt_cond (condition, statement);
8437 /* Parse the then-clause. */
8438 in_statement = parser->in_statement;
8439 parser->in_statement |= IN_IF_STMT;
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8443 add_stmt (build_empty_stmt (loc));
8444 cp_lexer_consume_token (parser->lexer);
8445 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8446 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8447 "empty body in an %<if%> statement");
8451 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8452 parser->in_statement = in_statement;
8454 finish_then_clause (statement);
8456 /* If the next token is `else', parse the else-clause. */
8457 if (cp_lexer_next_token_is_keyword (parser->lexer,
8460 /* Consume the `else' keyword. */
8461 cp_lexer_consume_token (parser->lexer);
8462 begin_else_clause (statement);
8463 /* Parse the else-clause. */
8464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8467 loc = cp_lexer_peek_token (parser->lexer)->location;
8469 OPT_Wempty_body, "suggest braces around "
8470 "empty body in an %<else%> statement");
8471 add_stmt (build_empty_stmt (loc));
8472 cp_lexer_consume_token (parser->lexer);
8475 cp_parser_implicitly_scoped_statement (parser, NULL);
8477 finish_else_clause (statement);
8479 /* If we are currently parsing a then-clause, then
8480 IF_P will not be NULL. We set it to true to
8481 indicate that this if statement has an else clause.
8482 This may trigger the Wparentheses warning below
8483 when we get back up to the parent if statement. */
8489 /* This if statement does not have an else clause. If
8490 NESTED_IF is true, then the then-clause is an if
8491 statement which does have an else clause. We warn
8492 about the potential ambiguity. */
8494 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8495 "suggest explicit braces to avoid ambiguous"
8499 /* Now we're all done with the if-statement. */
8500 finish_if_stmt (statement);
8504 bool in_switch_statement_p;
8505 unsigned char in_statement;
8507 /* Add the condition. */
8508 finish_switch_cond (condition, statement);
8510 /* Parse the body of the switch-statement. */
8511 in_switch_statement_p = parser->in_switch_statement_p;
8512 in_statement = parser->in_statement;
8513 parser->in_switch_statement_p = true;
8514 parser->in_statement |= IN_SWITCH_STMT;
8515 cp_parser_implicitly_scoped_statement (parser, NULL);
8516 parser->in_switch_statement_p = in_switch_statement_p;
8517 parser->in_statement = in_statement;
8519 /* Now we're all done with the switch-statement. */
8520 finish_switch_stmt (statement);
8528 cp_parser_error (parser, "expected selection-statement");
8529 return error_mark_node;
8533 /* Parse a condition.
8537 type-specifier-seq declarator = initializer-clause
8538 type-specifier-seq declarator braced-init-list
8543 type-specifier-seq declarator asm-specification [opt]
8544 attributes [opt] = assignment-expression
8546 Returns the expression that should be tested. */
8549 cp_parser_condition (cp_parser* parser)
8551 cp_decl_specifier_seq type_specifiers;
8552 const char *saved_message;
8553 int declares_class_or_enum;
8555 /* Try the declaration first. */
8556 cp_parser_parse_tentatively (parser);
8557 /* New types are not allowed in the type-specifier-seq for a
8559 saved_message = parser->type_definition_forbidden_message;
8560 parser->type_definition_forbidden_message
8561 = G_("types may not be defined in conditions");
8562 /* Parse the type-specifier-seq. */
8563 cp_parser_decl_specifier_seq (parser,
8564 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8566 &declares_class_or_enum);
8567 /* Restore the saved message. */
8568 parser->type_definition_forbidden_message = saved_message;
8569 /* If all is well, we might be looking at a declaration. */
8570 if (!cp_parser_error_occurred (parser))
8573 tree asm_specification;
8575 cp_declarator *declarator;
8576 tree initializer = NULL_TREE;
8578 /* Parse the declarator. */
8579 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8580 /*ctor_dtor_or_conv_p=*/NULL,
8581 /*parenthesized_p=*/NULL,
8582 /*member_p=*/false);
8583 /* Parse the attributes. */
8584 attributes = cp_parser_attributes_opt (parser);
8585 /* Parse the asm-specification. */
8586 asm_specification = cp_parser_asm_specification_opt (parser);
8587 /* If the next token is not an `=' or '{', then we might still be
8588 looking at an expression. For example:
8592 looks like a decl-specifier-seq and a declarator -- but then
8593 there is no `=', so this is an expression. */
8594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8595 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8596 cp_parser_simulate_error (parser);
8598 /* If we did see an `=' or '{', then we are looking at a declaration
8600 if (cp_parser_parse_definitely (parser))
8603 bool non_constant_p;
8604 bool flags = LOOKUP_ONLYCONVERTING;
8606 /* Create the declaration. */
8607 decl = start_decl (declarator, &type_specifiers,
8608 /*initialized_p=*/true,
8609 attributes, /*prefix_attributes=*/NULL_TREE,
8612 /* Parse the initializer. */
8613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8615 initializer = cp_parser_braced_list (parser, &non_constant_p);
8616 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8621 /* Consume the `='. */
8622 cp_parser_require (parser, CPP_EQ, RT_EQ);
8623 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8625 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8628 if (!non_constant_p)
8629 initializer = fold_non_dependent_expr (initializer);
8631 /* Process the initializer. */
8632 cp_finish_decl (decl,
8633 initializer, !non_constant_p,
8638 pop_scope (pushed_scope);
8640 return convert_from_reference (decl);
8643 /* If we didn't even get past the declarator successfully, we are
8644 definitely not looking at a declaration. */
8646 cp_parser_abort_tentative_parse (parser);
8648 /* Otherwise, we are looking at an expression. */
8649 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8652 /* Parses a traditional for-statement until the closing ')', not included. */
8655 cp_parser_c_for (cp_parser *parser)
8657 /* Normal for loop */
8659 tree condition = NULL_TREE;
8660 tree expression = NULL_TREE;
8662 /* Begin the for-statement. */
8663 stmt = begin_for_stmt ();
8665 /* Parse the initialization. */
8666 cp_parser_for_init_statement (parser);
8667 finish_for_init_stmt (stmt);
8669 /* If there's a condition, process it. */
8670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8671 condition = cp_parser_condition (parser);
8672 finish_for_cond (condition, stmt);
8673 /* Look for the `;'. */
8674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8676 /* If there's an expression, process it. */
8677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8678 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8679 finish_for_expr (expression, stmt);
8684 /* Tries to parse a range-based for-statement:
8687 type-specifier-seq declarator : expression
8689 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8690 expression. Note that the *DECL is returned unfinished, so
8691 later you should call cp_finish_decl().
8693 Returns TRUE iff a range-based for is parsed. */
8696 cp_parser_range_for (cp_parser *parser)
8698 tree stmt, range_decl, range_expr;
8699 cp_decl_specifier_seq type_specifiers;
8700 cp_declarator *declarator;
8701 const char *saved_message;
8702 tree attributes, pushed_scope;
8704 cp_parser_parse_tentatively (parser);
8705 /* New types are not allowed in the type-specifier-seq for a
8706 range-based for loop. */
8707 saved_message = parser->type_definition_forbidden_message;
8708 parser->type_definition_forbidden_message
8709 = G_("types may not be defined in range-based for loops");
8710 /* Parse the type-specifier-seq. */
8711 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8712 /*is_trailing_return=*/false,
8714 /* Restore the saved message. */
8715 parser->type_definition_forbidden_message = saved_message;
8716 /* If all is well, we might be looking at a declaration. */
8717 if (cp_parser_error_occurred (parser))
8719 cp_parser_abort_tentative_parse (parser);
8722 /* Parse the declarator. */
8723 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8724 /*ctor_dtor_or_conv_p=*/NULL,
8725 /*parenthesized_p=*/NULL,
8726 /*member_p=*/false);
8727 /* Parse the attributes. */
8728 attributes = cp_parser_attributes_opt (parser);
8729 /* The next token should be `:'. */
8730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8731 cp_parser_simulate_error (parser);
8733 /* Check if it is a range-based for */
8734 if (!cp_parser_parse_definitely (parser))
8737 cp_parser_require (parser, CPP_COLON, RT_COLON);
8738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8740 bool expr_non_constant_p;
8741 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8744 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8746 /* If in template, STMT is converted to a normal for-statements
8747 at instantiation. If not, it is done just ahead. */
8748 if (processing_template_decl)
8749 stmt = begin_range_for_stmt ();
8751 stmt = begin_for_stmt ();
8753 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8754 range_decl = start_decl (declarator, &type_specifiers,
8755 /*initialized_p=*/SD_INITIALIZED,
8756 attributes, /*prefix_attributes=*/NULL_TREE,
8758 /* No scope allowed here */
8759 pop_scope (pushed_scope);
8761 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8762 finish_range_for_decl (stmt, range_decl, range_expr);
8764 /* Convert the range-based for loop into a normal for-statement. */
8765 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8770 /* Converts a range-based for-statement into a normal
8771 for-statement, as per the definition.
8773 for (RANGE_DECL : RANGE_EXPR)
8776 should be equivalent to:
8779 auto &&__range = RANGE_EXPR;
8780 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8784 RANGE_DECL = *__begin;
8789 If RANGE_EXPR is an array:
8790 BEGIN_EXPR = __range
8791 END_EXPR = __range + ARRAY_SIZE(__range)
8793 BEGIN_EXPR = begin(__range)
8794 END_EXPR = end(__range);
8796 When calling begin()/end() we must use argument dependent
8797 lookup, but always considering 'std' as an associated namespace. */
8800 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8802 tree range_type, range_temp;
8804 tree iter_type, begin_expr, end_expr;
8805 tree condition, expression;
8807 /* Find out the type deduced by the declaration
8808 * `auto &&__range = range_expr' */
8809 range_type = cp_build_reference_type (make_auto (), true);
8810 range_type = do_auto_deduction (range_type, range_expr,
8811 type_uses_auto (range_type));
8813 /* Create the __range variable */
8814 range_temp = build_decl (input_location, VAR_DECL,
8815 get_identifier ("__for_range"), range_type);
8816 TREE_USED (range_temp) = 1;
8817 DECL_ARTIFICIAL (range_temp) = 1;
8818 pushdecl (range_temp);
8819 cp_finish_decl (range_temp, range_expr,
8820 /*is_constant_init*/false, NULL_TREE,
8821 LOOKUP_ONLYCONVERTING);
8823 range_temp = convert_from_reference (range_temp);
8825 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8827 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8828 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8829 begin_expr = range_temp;
8831 = build_binary_op (input_location, PLUS_EXPR,
8833 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8837 /* If it is not an array, we must call begin(__range)/end__range() */
8840 begin_expr = get_identifier ("begin");
8841 vec = make_tree_vector ();
8842 VEC_safe_push (tree, gc, vec, range_temp);
8843 begin_expr = perform_koenig_lookup (begin_expr, vec,
8844 /*include_std=*/true);
8845 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8846 tf_warning_or_error);
8847 release_tree_vector (vec);
8849 end_expr = get_identifier ("end");
8850 vec = make_tree_vector ();
8851 VEC_safe_push (tree, gc, vec, range_temp);
8852 end_expr = perform_koenig_lookup (end_expr, vec,
8853 /*include_std=*/true);
8854 end_expr = finish_call_expr (end_expr, &vec, false, true,
8855 tf_warning_or_error);
8856 release_tree_vector (vec);
8858 /* The unqualified type of the __begin and __end temporaries should
8859 * be the same as required by the multiple auto declaration */
8860 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8861 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8862 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8863 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8866 /* The new for initialization statement */
8867 begin = build_decl (input_location, VAR_DECL,
8868 get_identifier ("__for_begin"), iter_type);
8869 TREE_USED (begin) = 1;
8870 DECL_ARTIFICIAL (begin) = 1;
8872 cp_finish_decl (begin, begin_expr,
8873 /*is_constant_init*/false, NULL_TREE,
8874 LOOKUP_ONLYCONVERTING);
8876 end = build_decl (input_location, VAR_DECL,
8877 get_identifier ("__for_end"), iter_type);
8878 TREE_USED (end) = 1;
8879 DECL_ARTIFICIAL (end) = 1;
8881 cp_finish_decl (end, end_expr,
8882 /*is_constant_init*/false, NULL_TREE,
8883 LOOKUP_ONLYCONVERTING);
8885 finish_for_init_stmt (statement);
8887 /* The new for condition */
8888 condition = build_x_binary_op (NE_EXPR,
8891 NULL, tf_warning_or_error);
8892 finish_for_cond (condition, statement);
8894 /* The new increment expression */
8895 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8896 finish_for_expr (expression, statement);
8898 /* The declaration is initialized with *__begin inside the loop body */
8899 cp_finish_decl (range_decl,
8900 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8901 /*is_constant_init*/false, NULL_TREE,
8902 LOOKUP_ONLYCONVERTING);
8908 /* Parse an iteration-statement.
8910 iteration-statement:
8911 while ( condition ) statement
8912 do statement while ( expression ) ;
8913 for ( for-init-statement condition [opt] ; expression [opt] )
8916 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8919 cp_parser_iteration_statement (cp_parser* parser)
8924 unsigned char in_statement;
8926 /* Peek at the next token. */
8927 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8929 return error_mark_node;
8931 /* Remember whether or not we are already within an iteration
8933 in_statement = parser->in_statement;
8935 /* See what kind of keyword it is. */
8936 keyword = token->keyword;
8943 /* Begin the while-statement. */
8944 statement = begin_while_stmt ();
8945 /* Look for the `('. */
8946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8947 /* Parse the condition. */
8948 condition = cp_parser_condition (parser);
8949 finish_while_stmt_cond (condition, statement);
8950 /* Look for the `)'. */
8951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8952 /* Parse the dependent statement. */
8953 parser->in_statement = IN_ITERATION_STMT;
8954 cp_parser_already_scoped_statement (parser);
8955 parser->in_statement = in_statement;
8956 /* We're done with the while-statement. */
8957 finish_while_stmt (statement);
8965 /* Begin the do-statement. */
8966 statement = begin_do_stmt ();
8967 /* Parse the body of the do-statement. */
8968 parser->in_statement = IN_ITERATION_STMT;
8969 cp_parser_implicitly_scoped_statement (parser, NULL);
8970 parser->in_statement = in_statement;
8971 finish_do_body (statement);
8972 /* Look for the `while' keyword. */
8973 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8974 /* Look for the `('. */
8975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8976 /* Parse the expression. */
8977 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8978 /* We're done with the do-statement. */
8979 finish_do_stmt (expression, statement);
8980 /* Look for the `)'. */
8981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8982 /* Look for the `;'. */
8983 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8989 /* Look for the `('. */
8990 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8992 if (cxx_dialect == cxx0x)
8993 statement = cp_parser_range_for (parser);
8995 statement = NULL_TREE;
8996 if (statement == NULL_TREE)
8997 statement = cp_parser_c_for (parser);
8999 /* Look for the `)'. */
9000 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9002 /* Parse the body of the for-statement. */
9003 parser->in_statement = IN_ITERATION_STMT;
9004 cp_parser_already_scoped_statement (parser);
9005 parser->in_statement = in_statement;
9007 /* We're done with the for-statement. */
9008 finish_for_stmt (statement);
9013 cp_parser_error (parser, "expected iteration-statement");
9014 statement = error_mark_node;
9021 /* Parse a for-init-statement.
9024 expression-statement
9025 simple-declaration */
9028 cp_parser_for_init_statement (cp_parser* parser)
9030 /* If the next token is a `;', then we have an empty
9031 expression-statement. Grammatically, this is also a
9032 simple-declaration, but an invalid one, because it does not
9033 declare anything. Therefore, if we did not handle this case
9034 specially, we would issue an error message about an invalid
9036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9038 /* We're going to speculatively look for a declaration, falling back
9039 to an expression, if necessary. */
9040 cp_parser_parse_tentatively (parser);
9041 /* Parse the declaration. */
9042 cp_parser_simple_declaration (parser,
9043 /*function_definition_allowed_p=*/false);
9044 /* If the tentative parse failed, then we shall need to look for an
9045 expression-statement. */
9046 if (cp_parser_parse_definitely (parser))
9050 cp_parser_expression_statement (parser, NULL_TREE);
9053 /* Parse a jump-statement.
9058 return expression [opt] ;
9059 return braced-init-list ;
9067 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9070 cp_parser_jump_statement (cp_parser* parser)
9072 tree statement = error_mark_node;
9075 unsigned char in_statement;
9077 /* Peek at the next token. */
9078 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9080 return error_mark_node;
9082 /* See what kind of keyword it is. */
9083 keyword = token->keyword;
9087 in_statement = parser->in_statement & ~IN_IF_STMT;
9088 switch (in_statement)
9091 error_at (token->location, "break statement not within loop or switch");
9094 gcc_assert ((in_statement & IN_SWITCH_STMT)
9095 || in_statement == IN_ITERATION_STMT);
9096 statement = finish_break_stmt ();
9099 error_at (token->location, "invalid exit from OpenMP structured block");
9102 error_at (token->location, "break statement used with OpenMP for loop");
9105 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9109 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9112 error_at (token->location, "continue statement not within a loop");
9114 case IN_ITERATION_STMT:
9116 statement = finish_continue_stmt ();
9119 error_at (token->location, "invalid exit from OpenMP structured block");
9124 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9130 bool expr_non_constant_p;
9132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9134 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9135 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9137 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9138 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9140 /* If the next token is a `;', then there is no
9143 /* Build the return-statement. */
9144 statement = finish_return_stmt (expr);
9145 /* Look for the final `;'. */
9146 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9151 /* Create the goto-statement. */
9152 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9154 /* Issue a warning about this use of a GNU extension. */
9155 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9156 /* Consume the '*' token. */
9157 cp_lexer_consume_token (parser->lexer);
9158 /* Parse the dependent expression. */
9159 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9162 finish_goto_stmt (cp_parser_identifier (parser));
9163 /* Look for the final `;'. */
9164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9168 cp_parser_error (parser, "expected jump-statement");
9175 /* Parse a declaration-statement.
9177 declaration-statement:
9178 block-declaration */
9181 cp_parser_declaration_statement (cp_parser* parser)
9185 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9186 p = obstack_alloc (&declarator_obstack, 0);
9188 /* Parse the block-declaration. */
9189 cp_parser_block_declaration (parser, /*statement_p=*/true);
9191 /* Free any declarators allocated. */
9192 obstack_free (&declarator_obstack, p);
9194 /* Finish off the statement. */
9198 /* Some dependent statements (like `if (cond) statement'), are
9199 implicitly in their own scope. In other words, if the statement is
9200 a single statement (as opposed to a compound-statement), it is
9201 none-the-less treated as if it were enclosed in braces. Any
9202 declarations appearing in the dependent statement are out of scope
9203 after control passes that point. This function parses a statement,
9204 but ensures that is in its own scope, even if it is not a
9207 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9208 is a (possibly labeled) if statement which is not enclosed in
9209 braces and has an else clause. This is used to implement
9212 Returns the new statement. */
9215 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9222 /* Mark if () ; with a special NOP_EXPR. */
9223 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9225 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9226 cp_lexer_consume_token (parser->lexer);
9227 statement = add_stmt (build_empty_stmt (loc));
9229 /* if a compound is opened, we simply parse the statement directly. */
9230 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9231 statement = cp_parser_compound_statement (parser, NULL, false);
9232 /* If the token is not a `{', then we must take special action. */
9235 /* Create a compound-statement. */
9236 statement = begin_compound_stmt (0);
9237 /* Parse the dependent-statement. */
9238 cp_parser_statement (parser, NULL_TREE, false, if_p);
9239 /* Finish the dummy compound-statement. */
9240 finish_compound_stmt (statement);
9243 /* Return the statement. */
9247 /* For some dependent statements (like `while (cond) statement'), we
9248 have already created a scope. Therefore, even if the dependent
9249 statement is a compound-statement, we do not want to create another
9253 cp_parser_already_scoped_statement (cp_parser* parser)
9255 /* If the token is a `{', then we must take special action. */
9256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9257 cp_parser_statement (parser, NULL_TREE, false, NULL);
9260 /* Avoid calling cp_parser_compound_statement, so that we
9261 don't create a new scope. Do everything else by hand. */
9262 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9263 /* If the next keyword is `__label__' we have a label declaration. */
9264 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9265 cp_parser_label_declaration (parser);
9266 /* Parse an (optional) statement-seq. */
9267 cp_parser_statement_seq_opt (parser, NULL_TREE);
9268 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9272 /* Declarations [gram.dcl.dcl] */
9274 /* Parse an optional declaration-sequence.
9278 declaration-seq declaration */
9281 cp_parser_declaration_seq_opt (cp_parser* parser)
9287 token = cp_lexer_peek_token (parser->lexer);
9289 if (token->type == CPP_CLOSE_BRACE
9290 || token->type == CPP_EOF
9291 || token->type == CPP_PRAGMA_EOL)
9294 if (token->type == CPP_SEMICOLON)
9296 /* A declaration consisting of a single semicolon is
9297 invalid. Allow it unless we're being pedantic. */
9298 cp_lexer_consume_token (parser->lexer);
9299 if (!in_system_header)
9300 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9304 /* If we're entering or exiting a region that's implicitly
9305 extern "C", modify the lang context appropriately. */
9306 if (!parser->implicit_extern_c && token->implicit_extern_c)
9308 push_lang_context (lang_name_c);
9309 parser->implicit_extern_c = true;
9311 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9313 pop_lang_context ();
9314 parser->implicit_extern_c = false;
9317 if (token->type == CPP_PRAGMA)
9319 /* A top-level declaration can consist solely of a #pragma.
9320 A nested declaration cannot, so this is done here and not
9321 in cp_parser_declaration. (A #pragma at block scope is
9322 handled in cp_parser_statement.) */
9323 cp_parser_pragma (parser, pragma_external);
9327 /* Parse the declaration itself. */
9328 cp_parser_declaration (parser);
9332 /* Parse a declaration.
9337 template-declaration
9338 explicit-instantiation
9339 explicit-specialization
9340 linkage-specification
9341 namespace-definition
9346 __extension__ declaration */
9349 cp_parser_declaration (cp_parser* parser)
9355 tree attributes = NULL_TREE;
9357 /* Check for the `__extension__' keyword. */
9358 if (cp_parser_extension_opt (parser, &saved_pedantic))
9360 /* Parse the qualified declaration. */
9361 cp_parser_declaration (parser);
9362 /* Restore the PEDANTIC flag. */
9363 pedantic = saved_pedantic;
9368 /* Try to figure out what kind of declaration is present. */
9369 token1 = *cp_lexer_peek_token (parser->lexer);
9371 if (token1.type != CPP_EOF)
9372 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9375 token2.type = CPP_EOF;
9376 token2.keyword = RID_MAX;
9379 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9380 p = obstack_alloc (&declarator_obstack, 0);
9382 /* If the next token is `extern' and the following token is a string
9383 literal, then we have a linkage specification. */
9384 if (token1.keyword == RID_EXTERN
9385 && cp_parser_is_string_literal (&token2))
9386 cp_parser_linkage_specification (parser);
9387 /* If the next token is `template', then we have either a template
9388 declaration, an explicit instantiation, or an explicit
9390 else if (token1.keyword == RID_TEMPLATE)
9392 /* `template <>' indicates a template specialization. */
9393 if (token2.type == CPP_LESS
9394 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9395 cp_parser_explicit_specialization (parser);
9396 /* `template <' indicates a template declaration. */
9397 else if (token2.type == CPP_LESS)
9398 cp_parser_template_declaration (parser, /*member_p=*/false);
9399 /* Anything else must be an explicit instantiation. */
9401 cp_parser_explicit_instantiation (parser);
9403 /* If the next token is `export', then we have a template
9405 else if (token1.keyword == RID_EXPORT)
9406 cp_parser_template_declaration (parser, /*member_p=*/false);
9407 /* If the next token is `extern', 'static' or 'inline' and the one
9408 after that is `template', we have a GNU extended explicit
9409 instantiation directive. */
9410 else if (cp_parser_allow_gnu_extensions_p (parser)
9411 && (token1.keyword == RID_EXTERN
9412 || token1.keyword == RID_STATIC
9413 || token1.keyword == RID_INLINE)
9414 && token2.keyword == RID_TEMPLATE)
9415 cp_parser_explicit_instantiation (parser);
9416 /* If the next token is `namespace', check for a named or unnamed
9417 namespace definition. */
9418 else if (token1.keyword == RID_NAMESPACE
9419 && (/* A named namespace definition. */
9420 (token2.type == CPP_NAME
9421 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9423 /* An unnamed namespace definition. */
9424 || token2.type == CPP_OPEN_BRACE
9425 || token2.keyword == RID_ATTRIBUTE))
9426 cp_parser_namespace_definition (parser);
9427 /* An inline (associated) namespace definition. */
9428 else if (token1.keyword == RID_INLINE
9429 && token2.keyword == RID_NAMESPACE)
9430 cp_parser_namespace_definition (parser);
9431 /* Objective-C++ declaration/definition. */
9432 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9433 cp_parser_objc_declaration (parser, NULL_TREE);
9434 else if (c_dialect_objc ()
9435 && token1.keyword == RID_ATTRIBUTE
9436 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9437 cp_parser_objc_declaration (parser, attributes);
9438 /* We must have either a block declaration or a function
9441 /* Try to parse a block-declaration, or a function-definition. */
9442 cp_parser_block_declaration (parser, /*statement_p=*/false);
9444 /* Free any declarators allocated. */
9445 obstack_free (&declarator_obstack, p);
9448 /* Parse a block-declaration.
9453 namespace-alias-definition
9460 __extension__ block-declaration
9465 static_assert-declaration
9467 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9468 part of a declaration-statement. */
9471 cp_parser_block_declaration (cp_parser *parser,
9477 /* Check for the `__extension__' keyword. */
9478 if (cp_parser_extension_opt (parser, &saved_pedantic))
9480 /* Parse the qualified declaration. */
9481 cp_parser_block_declaration (parser, statement_p);
9482 /* Restore the PEDANTIC flag. */
9483 pedantic = saved_pedantic;
9488 /* Peek at the next token to figure out which kind of declaration is
9490 token1 = cp_lexer_peek_token (parser->lexer);
9492 /* If the next keyword is `asm', we have an asm-definition. */
9493 if (token1->keyword == RID_ASM)
9496 cp_parser_commit_to_tentative_parse (parser);
9497 cp_parser_asm_definition (parser);
9499 /* If the next keyword is `namespace', we have a
9500 namespace-alias-definition. */
9501 else if (token1->keyword == RID_NAMESPACE)
9502 cp_parser_namespace_alias_definition (parser);
9503 /* If the next keyword is `using', we have either a
9504 using-declaration or a using-directive. */
9505 else if (token1->keyword == RID_USING)
9510 cp_parser_commit_to_tentative_parse (parser);
9511 /* If the token after `using' is `namespace', then we have a
9513 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9514 if (token2->keyword == RID_NAMESPACE)
9515 cp_parser_using_directive (parser);
9516 /* Otherwise, it's a using-declaration. */
9518 cp_parser_using_declaration (parser,
9519 /*access_declaration_p=*/false);
9521 /* If the next keyword is `__label__' we have a misplaced label
9523 else if (token1->keyword == RID_LABEL)
9525 cp_lexer_consume_token (parser->lexer);
9526 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9527 cp_parser_skip_to_end_of_statement (parser);
9528 /* If the next token is now a `;', consume it. */
9529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9530 cp_lexer_consume_token (parser->lexer);
9532 /* If the next token is `static_assert' we have a static assertion. */
9533 else if (token1->keyword == RID_STATIC_ASSERT)
9534 cp_parser_static_assert (parser, /*member_p=*/false);
9535 /* Anything else must be a simple-declaration. */
9537 cp_parser_simple_declaration (parser, !statement_p);
9540 /* Parse a simple-declaration.
9543 decl-specifier-seq [opt] init-declarator-list [opt] ;
9545 init-declarator-list:
9547 init-declarator-list , init-declarator
9549 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9550 function-definition as a simple-declaration. */
9553 cp_parser_simple_declaration (cp_parser* parser,
9554 bool function_definition_allowed_p)
9556 cp_decl_specifier_seq decl_specifiers;
9557 int declares_class_or_enum;
9558 bool saw_declarator;
9560 /* Defer access checks until we know what is being declared; the
9561 checks for names appearing in the decl-specifier-seq should be
9562 done as if we were in the scope of the thing being declared. */
9563 push_deferring_access_checks (dk_deferred);
9565 /* Parse the decl-specifier-seq. We have to keep track of whether
9566 or not the decl-specifier-seq declares a named class or
9567 enumeration type, since that is the only case in which the
9568 init-declarator-list is allowed to be empty.
9572 In a simple-declaration, the optional init-declarator-list can be
9573 omitted only when declaring a class or enumeration, that is when
9574 the decl-specifier-seq contains either a class-specifier, an
9575 elaborated-type-specifier, or an enum-specifier. */
9576 cp_parser_decl_specifier_seq (parser,
9577 CP_PARSER_FLAGS_OPTIONAL,
9579 &declares_class_or_enum);
9580 /* We no longer need to defer access checks. */
9581 stop_deferring_access_checks ();
9583 /* In a block scope, a valid declaration must always have a
9584 decl-specifier-seq. By not trying to parse declarators, we can
9585 resolve the declaration/expression ambiguity more quickly. */
9586 if (!function_definition_allowed_p
9587 && !decl_specifiers.any_specifiers_p)
9589 cp_parser_error (parser, "expected declaration");
9593 /* If the next two tokens are both identifiers, the code is
9594 erroneous. The usual cause of this situation is code like:
9598 where "T" should name a type -- but does not. */
9599 if (!decl_specifiers.any_type_specifiers_p
9600 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9602 /* If parsing tentatively, we should commit; we really are
9603 looking at a declaration. */
9604 cp_parser_commit_to_tentative_parse (parser);
9609 /* If we have seen at least one decl-specifier, and the next token
9610 is not a parenthesis, then we must be looking at a declaration.
9611 (After "int (" we might be looking at a functional cast.) */
9612 if (decl_specifiers.any_specifiers_p
9613 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9614 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9615 && !cp_parser_error_occurred (parser))
9616 cp_parser_commit_to_tentative_parse (parser);
9618 /* Keep going until we hit the `;' at the end of the simple
9620 saw_declarator = false;
9621 while (cp_lexer_next_token_is_not (parser->lexer,
9625 bool function_definition_p;
9630 /* If we are processing next declarator, coma is expected */
9631 token = cp_lexer_peek_token (parser->lexer);
9632 gcc_assert (token->type == CPP_COMMA);
9633 cp_lexer_consume_token (parser->lexer);
9636 saw_declarator = true;
9638 /* Parse the init-declarator. */
9639 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9641 function_definition_allowed_p,
9643 declares_class_or_enum,
9644 &function_definition_p);
9645 /* If an error occurred while parsing tentatively, exit quickly.
9646 (That usually happens when in the body of a function; each
9647 statement is treated as a declaration-statement until proven
9649 if (cp_parser_error_occurred (parser))
9651 /* Handle function definitions specially. */
9652 if (function_definition_p)
9654 /* If the next token is a `,', then we are probably
9655 processing something like:
9659 which is erroneous. */
9660 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9662 cp_token *token = cp_lexer_peek_token (parser->lexer);
9663 error_at (token->location,
9665 " declarations and function-definitions is forbidden");
9667 /* Otherwise, we're done with the list of declarators. */
9670 pop_deferring_access_checks ();
9674 /* The next token should be either a `,' or a `;'. */
9675 token = cp_lexer_peek_token (parser->lexer);
9676 /* If it's a `,', there are more declarators to come. */
9677 if (token->type == CPP_COMMA)
9678 /* will be consumed next time around */;
9679 /* If it's a `;', we are done. */
9680 else if (token->type == CPP_SEMICOLON)
9682 /* Anything else is an error. */
9685 /* If we have already issued an error message we don't need
9686 to issue another one. */
9687 if (decl != error_mark_node
9688 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9689 cp_parser_error (parser, "expected %<,%> or %<;%>");
9690 /* Skip tokens until we reach the end of the statement. */
9691 cp_parser_skip_to_end_of_statement (parser);
9692 /* If the next token is now a `;', consume it. */
9693 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9694 cp_lexer_consume_token (parser->lexer);
9697 /* After the first time around, a function-definition is not
9698 allowed -- even if it was OK at first. For example:
9703 function_definition_allowed_p = false;
9706 /* Issue an error message if no declarators are present, and the
9707 decl-specifier-seq does not itself declare a class or
9709 if (!saw_declarator)
9711 if (cp_parser_declares_only_class_p (parser))
9712 shadow_tag (&decl_specifiers);
9713 /* Perform any deferred access checks. */
9714 perform_deferred_access_checks ();
9717 /* Consume the `;'. */
9718 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9721 pop_deferring_access_checks ();
9724 /* Parse a decl-specifier-seq.
9727 decl-specifier-seq [opt] decl-specifier
9730 storage-class-specifier
9741 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9743 The parser flags FLAGS is used to control type-specifier parsing.
9745 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9748 1: one of the decl-specifiers is an elaborated-type-specifier
9749 (i.e., a type declaration)
9750 2: one of the decl-specifiers is an enum-specifier or a
9751 class-specifier (i.e., a type definition)
9756 cp_parser_decl_specifier_seq (cp_parser* parser,
9757 cp_parser_flags flags,
9758 cp_decl_specifier_seq *decl_specs,
9759 int* declares_class_or_enum)
9761 bool constructor_possible_p = !parser->in_declarator_p;
9762 cp_token *start_token = NULL;
9764 /* Clear DECL_SPECS. */
9765 clear_decl_specs (decl_specs);
9767 /* Assume no class or enumeration type is declared. */
9768 *declares_class_or_enum = 0;
9770 /* Keep reading specifiers until there are no more to read. */
9774 bool found_decl_spec;
9777 /* Peek at the next token. */
9778 token = cp_lexer_peek_token (parser->lexer);
9780 /* Save the first token of the decl spec list for error
9783 start_token = token;
9784 /* Handle attributes. */
9785 if (token->keyword == RID_ATTRIBUTE)
9787 /* Parse the attributes. */
9788 decl_specs->attributes
9789 = chainon (decl_specs->attributes,
9790 cp_parser_attributes_opt (parser));
9793 /* Assume we will find a decl-specifier keyword. */
9794 found_decl_spec = true;
9795 /* If the next token is an appropriate keyword, we can simply
9796 add it to the list. */
9797 switch (token->keyword)
9803 if (!at_class_scope_p ())
9805 error_at (token->location, "%<friend%> used outside of class");
9806 cp_lexer_purge_token (parser->lexer);
9810 ++decl_specs->specs[(int) ds_friend];
9811 /* Consume the token. */
9812 cp_lexer_consume_token (parser->lexer);
9817 ++decl_specs->specs[(int) ds_constexpr];
9818 cp_lexer_consume_token (parser->lexer);
9821 /* function-specifier:
9828 cp_parser_function_specifier_opt (parser, decl_specs);
9834 ++decl_specs->specs[(int) ds_typedef];
9835 /* Consume the token. */
9836 cp_lexer_consume_token (parser->lexer);
9837 /* A constructor declarator cannot appear in a typedef. */
9838 constructor_possible_p = false;
9839 /* The "typedef" keyword can only occur in a declaration; we
9840 may as well commit at this point. */
9841 cp_parser_commit_to_tentative_parse (parser);
9843 if (decl_specs->storage_class != sc_none)
9844 decl_specs->conflicting_specifiers_p = true;
9847 /* storage-class-specifier:
9857 if (cxx_dialect == cxx98)
9859 /* Consume the token. */
9860 cp_lexer_consume_token (parser->lexer);
9862 /* Complain about `auto' as a storage specifier, if
9863 we're complaining about C++0x compatibility. */
9864 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9865 " will change meaning in C++0x; please remove it");
9867 /* Set the storage class anyway. */
9868 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9872 /* C++0x auto type-specifier. */
9873 found_decl_spec = false;
9880 /* Consume the token. */
9881 cp_lexer_consume_token (parser->lexer);
9882 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9886 /* Consume the token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 ++decl_specs->specs[(int) ds_thread];
9892 /* We did not yet find a decl-specifier yet. */
9893 found_decl_spec = false;
9898 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9899 && token->keyword != RID_CONSTEXPR)
9900 error ("decl-specifier invalid in condition");
9902 /* Constructors are a special case. The `S' in `S()' is not a
9903 decl-specifier; it is the beginning of the declarator. */
9906 && constructor_possible_p
9907 && (cp_parser_constructor_declarator_p
9908 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9910 /* If we don't have a DECL_SPEC yet, then we must be looking at
9911 a type-specifier. */
9912 if (!found_decl_spec && !constructor_p)
9914 int decl_spec_declares_class_or_enum;
9915 bool is_cv_qualifier;
9919 = cp_parser_type_specifier (parser, flags,
9921 /*is_declaration=*/true,
9922 &decl_spec_declares_class_or_enum,
9924 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9926 /* If this type-specifier referenced a user-defined type
9927 (a typedef, class-name, etc.), then we can't allow any
9928 more such type-specifiers henceforth.
9932 The longest sequence of decl-specifiers that could
9933 possibly be a type name is taken as the
9934 decl-specifier-seq of a declaration. The sequence shall
9935 be self-consistent as described below.
9939 As a general rule, at most one type-specifier is allowed
9940 in the complete decl-specifier-seq of a declaration. The
9941 only exceptions are the following:
9943 -- const or volatile can be combined with any other
9946 -- signed or unsigned can be combined with char, long,
9954 void g (const int Pc);
9956 Here, Pc is *not* part of the decl-specifier seq; it's
9957 the declarator. Therefore, once we see a type-specifier
9958 (other than a cv-qualifier), we forbid any additional
9959 user-defined types. We *do* still allow things like `int
9960 int' to be considered a decl-specifier-seq, and issue the
9961 error message later. */
9962 if (type_spec && !is_cv_qualifier)
9963 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9964 /* A constructor declarator cannot follow a type-specifier. */
9967 constructor_possible_p = false;
9968 found_decl_spec = true;
9969 if (!is_cv_qualifier)
9970 decl_specs->any_type_specifiers_p = true;
9974 /* If we still do not have a DECL_SPEC, then there are no more
9976 if (!found_decl_spec)
9979 decl_specs->any_specifiers_p = true;
9980 /* After we see one decl-specifier, further decl-specifiers are
9982 flags |= CP_PARSER_FLAGS_OPTIONAL;
9985 cp_parser_check_decl_spec (decl_specs, start_token->location);
9987 /* Don't allow a friend specifier with a class definition. */
9988 if (decl_specs->specs[(int) ds_friend] != 0
9989 && (*declares_class_or_enum & 2))
9990 error_at (start_token->location,
9991 "class definition may not be declared a friend");
9994 /* Parse an (optional) storage-class-specifier.
9996 storage-class-specifier:
10005 storage-class-specifier:
10008 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10011 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10013 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10016 if (cxx_dialect != cxx98)
10018 /* Fall through for C++98. */
10025 /* Consume the token. */
10026 return cp_lexer_consume_token (parser->lexer)->u.value;
10033 /* Parse an (optional) function-specifier.
10035 function-specifier:
10040 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10041 Updates DECL_SPECS, if it is non-NULL. */
10044 cp_parser_function_specifier_opt (cp_parser* parser,
10045 cp_decl_specifier_seq *decl_specs)
10047 cp_token *token = cp_lexer_peek_token (parser->lexer);
10048 switch (token->keyword)
10052 ++decl_specs->specs[(int) ds_inline];
10056 /* 14.5.2.3 [temp.mem]
10058 A member function template shall not be virtual. */
10059 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10060 error_at (token->location, "templates may not be %<virtual%>");
10061 else if (decl_specs)
10062 ++decl_specs->specs[(int) ds_virtual];
10067 ++decl_specs->specs[(int) ds_explicit];
10074 /* Consume the token. */
10075 return cp_lexer_consume_token (parser->lexer)->u.value;
10078 /* Parse a linkage-specification.
10080 linkage-specification:
10081 extern string-literal { declaration-seq [opt] }
10082 extern string-literal declaration */
10085 cp_parser_linkage_specification (cp_parser* parser)
10089 /* Look for the `extern' keyword. */
10090 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10092 /* Look for the string-literal. */
10093 linkage = cp_parser_string_literal (parser, false, false);
10095 /* Transform the literal into an identifier. If the literal is a
10096 wide-character string, or contains embedded NULs, then we can't
10097 handle it as the user wants. */
10098 if (strlen (TREE_STRING_POINTER (linkage))
10099 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10101 cp_parser_error (parser, "invalid linkage-specification");
10102 /* Assume C++ linkage. */
10103 linkage = lang_name_cplusplus;
10106 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10108 /* We're now using the new linkage. */
10109 push_lang_context (linkage);
10111 /* If the next token is a `{', then we're using the first
10113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10115 /* Consume the `{' token. */
10116 cp_lexer_consume_token (parser->lexer);
10117 /* Parse the declarations. */
10118 cp_parser_declaration_seq_opt (parser);
10119 /* Look for the closing `}'. */
10120 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10122 /* Otherwise, there's just one declaration. */
10125 bool saved_in_unbraced_linkage_specification_p;
10127 saved_in_unbraced_linkage_specification_p
10128 = parser->in_unbraced_linkage_specification_p;
10129 parser->in_unbraced_linkage_specification_p = true;
10130 cp_parser_declaration (parser);
10131 parser->in_unbraced_linkage_specification_p
10132 = saved_in_unbraced_linkage_specification_p;
10135 /* We're done with the linkage-specification. */
10136 pop_lang_context ();
10139 /* Parse a static_assert-declaration.
10141 static_assert-declaration:
10142 static_assert ( constant-expression , string-literal ) ;
10144 If MEMBER_P, this static_assert is a class member. */
10147 cp_parser_static_assert(cp_parser *parser, bool member_p)
10152 location_t saved_loc;
10154 /* Peek at the `static_assert' token so we can keep track of exactly
10155 where the static assertion started. */
10156 token = cp_lexer_peek_token (parser->lexer);
10157 saved_loc = token->location;
10159 /* Look for the `static_assert' keyword. */
10160 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10164 /* We know we are in a static assertion; commit to any tentative
10166 if (cp_parser_parsing_tentatively (parser))
10167 cp_parser_commit_to_tentative_parse (parser);
10169 /* Parse the `(' starting the static assertion condition. */
10170 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10172 /* Parse the constant-expression. */
10174 cp_parser_constant_expression (parser,
10175 /*allow_non_constant_p=*/false,
10176 /*non_constant_p=*/NULL);
10178 /* Parse the separating `,'. */
10179 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10181 /* Parse the string-literal message. */
10182 message = cp_parser_string_literal (parser,
10183 /*translate=*/false,
10186 /* A `)' completes the static assertion. */
10187 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10188 cp_parser_skip_to_closing_parenthesis (parser,
10189 /*recovering=*/true,
10190 /*or_comma=*/false,
10191 /*consume_paren=*/true);
10193 /* A semicolon terminates the declaration. */
10194 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10196 /* Complete the static assertion, which may mean either processing
10197 the static assert now or saving it for template instantiation. */
10198 finish_static_assert (condition, message, saved_loc, member_p);
10201 /* Parse a `decltype' type. Returns the type.
10203 simple-type-specifier:
10204 decltype ( expression ) */
10207 cp_parser_decltype (cp_parser *parser)
10210 bool id_expression_or_member_access_p = false;
10211 const char *saved_message;
10212 bool saved_integral_constant_expression_p;
10213 bool saved_non_integral_constant_expression_p;
10214 cp_token *id_expr_start_token;
10216 /* Look for the `decltype' token. */
10217 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10218 return error_mark_node;
10220 /* Types cannot be defined in a `decltype' expression. Save away the
10222 saved_message = parser->type_definition_forbidden_message;
10224 /* And create the new one. */
10225 parser->type_definition_forbidden_message
10226 = G_("types may not be defined in %<decltype%> expressions");
10228 /* The restrictions on constant-expressions do not apply inside
10229 decltype expressions. */
10230 saved_integral_constant_expression_p
10231 = parser->integral_constant_expression_p;
10232 saved_non_integral_constant_expression_p
10233 = parser->non_integral_constant_expression_p;
10234 parser->integral_constant_expression_p = false;
10236 /* Do not actually evaluate the expression. */
10237 ++cp_unevaluated_operand;
10239 /* Do not warn about problems with the expression. */
10240 ++c_inhibit_evaluation_warnings;
10242 /* Parse the opening `('. */
10243 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10244 return error_mark_node;
10246 /* First, try parsing an id-expression. */
10247 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10248 cp_parser_parse_tentatively (parser);
10249 expr = cp_parser_id_expression (parser,
10250 /*template_keyword_p=*/false,
10251 /*check_dependency_p=*/true,
10252 /*template_p=*/NULL,
10253 /*declarator_p=*/false,
10254 /*optional_p=*/false);
10256 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10258 bool non_integral_constant_expression_p = false;
10259 tree id_expression = expr;
10261 const char *error_msg;
10263 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10264 /* Lookup the name we got back from the id-expression. */
10265 expr = cp_parser_lookup_name (parser, expr,
10267 /*is_template=*/false,
10268 /*is_namespace=*/false,
10269 /*check_dependency=*/true,
10270 /*ambiguous_decls=*/NULL,
10271 id_expr_start_token->location);
10274 && expr != error_mark_node
10275 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10276 && TREE_CODE (expr) != TYPE_DECL
10277 && (TREE_CODE (expr) != BIT_NOT_EXPR
10278 || !TYPE_P (TREE_OPERAND (expr, 0)))
10279 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10281 /* Complete lookup of the id-expression. */
10282 expr = (finish_id_expression
10283 (id_expression, expr, parser->scope, &idk,
10284 /*integral_constant_expression_p=*/false,
10285 /*allow_non_integral_constant_expression_p=*/true,
10286 &non_integral_constant_expression_p,
10287 /*template_p=*/false,
10289 /*address_p=*/false,
10290 /*template_arg_p=*/false,
10292 id_expr_start_token->location));
10294 if (expr == error_mark_node)
10295 /* We found an id-expression, but it was something that we
10296 should not have found. This is an error, not something
10297 we can recover from, so note that we found an
10298 id-expression and we'll recover as gracefully as
10300 id_expression_or_member_access_p = true;
10304 && expr != error_mark_node
10305 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10306 /* We have an id-expression. */
10307 id_expression_or_member_access_p = true;
10310 if (!id_expression_or_member_access_p)
10312 /* Abort the id-expression parse. */
10313 cp_parser_abort_tentative_parse (parser);
10315 /* Parsing tentatively, again. */
10316 cp_parser_parse_tentatively (parser);
10318 /* Parse a class member access. */
10319 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10321 /*member_access_only_p=*/true, NULL);
10324 && expr != error_mark_node
10325 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10326 /* We have an id-expression. */
10327 id_expression_or_member_access_p = true;
10330 if (id_expression_or_member_access_p)
10331 /* We have parsed the complete id-expression or member access. */
10332 cp_parser_parse_definitely (parser);
10335 bool saved_greater_than_is_operator_p;
10337 /* Abort our attempt to parse an id-expression or member access
10339 cp_parser_abort_tentative_parse (parser);
10341 /* Within a parenthesized expression, a `>' token is always
10342 the greater-than operator. */
10343 saved_greater_than_is_operator_p
10344 = parser->greater_than_is_operator_p;
10345 parser->greater_than_is_operator_p = true;
10347 /* Parse a full expression. */
10348 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10350 /* The `>' token might be the end of a template-id or
10351 template-parameter-list now. */
10352 parser->greater_than_is_operator_p
10353 = saved_greater_than_is_operator_p;
10356 /* Go back to evaluating expressions. */
10357 --cp_unevaluated_operand;
10358 --c_inhibit_evaluation_warnings;
10360 /* Restore the old message and the integral constant expression
10362 parser->type_definition_forbidden_message = saved_message;
10363 parser->integral_constant_expression_p
10364 = saved_integral_constant_expression_p;
10365 parser->non_integral_constant_expression_p
10366 = saved_non_integral_constant_expression_p;
10368 if (expr == error_mark_node)
10370 /* Skip everything up to the closing `)'. */
10371 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10372 /*consume_paren=*/true);
10373 return error_mark_node;
10376 /* Parse to the closing `)'. */
10377 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10379 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10380 /*consume_paren=*/true);
10381 return error_mark_node;
10384 return finish_decltype_type (expr, id_expression_or_member_access_p);
10387 /* Special member functions [gram.special] */
10389 /* Parse a conversion-function-id.
10391 conversion-function-id:
10392 operator conversion-type-id
10394 Returns an IDENTIFIER_NODE representing the operator. */
10397 cp_parser_conversion_function_id (cp_parser* parser)
10401 tree saved_qualifying_scope;
10402 tree saved_object_scope;
10403 tree pushed_scope = NULL_TREE;
10405 /* Look for the `operator' token. */
10406 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10407 return error_mark_node;
10408 /* When we parse the conversion-type-id, the current scope will be
10409 reset. However, we need that information in able to look up the
10410 conversion function later, so we save it here. */
10411 saved_scope = parser->scope;
10412 saved_qualifying_scope = parser->qualifying_scope;
10413 saved_object_scope = parser->object_scope;
10414 /* We must enter the scope of the class so that the names of
10415 entities declared within the class are available in the
10416 conversion-type-id. For example, consider:
10423 S::operator I() { ... }
10425 In order to see that `I' is a type-name in the definition, we
10426 must be in the scope of `S'. */
10428 pushed_scope = push_scope (saved_scope);
10429 /* Parse the conversion-type-id. */
10430 type = cp_parser_conversion_type_id (parser);
10431 /* Leave the scope of the class, if any. */
10433 pop_scope (pushed_scope);
10434 /* Restore the saved scope. */
10435 parser->scope = saved_scope;
10436 parser->qualifying_scope = saved_qualifying_scope;
10437 parser->object_scope = saved_object_scope;
10438 /* If the TYPE is invalid, indicate failure. */
10439 if (type == error_mark_node)
10440 return error_mark_node;
10441 return mangle_conv_op_name_for_type (type);
10444 /* Parse a conversion-type-id:
10446 conversion-type-id:
10447 type-specifier-seq conversion-declarator [opt]
10449 Returns the TYPE specified. */
10452 cp_parser_conversion_type_id (cp_parser* parser)
10455 cp_decl_specifier_seq type_specifiers;
10456 cp_declarator *declarator;
10457 tree type_specified;
10459 /* Parse the attributes. */
10460 attributes = cp_parser_attributes_opt (parser);
10461 /* Parse the type-specifiers. */
10462 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10463 /*is_trailing_return=*/false,
10465 /* If that didn't work, stop. */
10466 if (type_specifiers.type == error_mark_node)
10467 return error_mark_node;
10468 /* Parse the conversion-declarator. */
10469 declarator = cp_parser_conversion_declarator_opt (parser);
10471 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10472 /*initialized=*/0, &attributes);
10474 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10476 /* Don't give this error when parsing tentatively. This happens to
10477 work because we always parse this definitively once. */
10478 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10479 && type_uses_auto (type_specified))
10481 error ("invalid use of %<auto%> in conversion operator");
10482 return error_mark_node;
10485 return type_specified;
10488 /* Parse an (optional) conversion-declarator.
10490 conversion-declarator:
10491 ptr-operator conversion-declarator [opt]
10495 static cp_declarator *
10496 cp_parser_conversion_declarator_opt (cp_parser* parser)
10498 enum tree_code code;
10500 cp_cv_quals cv_quals;
10502 /* We don't know if there's a ptr-operator next, or not. */
10503 cp_parser_parse_tentatively (parser);
10504 /* Try the ptr-operator. */
10505 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10506 /* If it worked, look for more conversion-declarators. */
10507 if (cp_parser_parse_definitely (parser))
10509 cp_declarator *declarator;
10511 /* Parse another optional declarator. */
10512 declarator = cp_parser_conversion_declarator_opt (parser);
10514 return cp_parser_make_indirect_declarator
10515 (code, class_type, cv_quals, declarator);
10521 /* Parse an (optional) ctor-initializer.
10524 : mem-initializer-list
10526 Returns TRUE iff the ctor-initializer was actually present. */
10529 cp_parser_ctor_initializer_opt (cp_parser* parser)
10531 /* If the next token is not a `:', then there is no
10532 ctor-initializer. */
10533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10535 /* Do default initialization of any bases and members. */
10536 if (DECL_CONSTRUCTOR_P (current_function_decl))
10537 finish_mem_initializers (NULL_TREE);
10542 /* Consume the `:' token. */
10543 cp_lexer_consume_token (parser->lexer);
10544 /* And the mem-initializer-list. */
10545 cp_parser_mem_initializer_list (parser);
10550 /* Parse a mem-initializer-list.
10552 mem-initializer-list:
10553 mem-initializer ... [opt]
10554 mem-initializer ... [opt] , mem-initializer-list */
10557 cp_parser_mem_initializer_list (cp_parser* parser)
10559 tree mem_initializer_list = NULL_TREE;
10560 cp_token *token = cp_lexer_peek_token (parser->lexer);
10562 /* Let the semantic analysis code know that we are starting the
10563 mem-initializer-list. */
10564 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10565 error_at (token->location,
10566 "only constructors take member initializers");
10568 /* Loop through the list. */
10571 tree mem_initializer;
10573 token = cp_lexer_peek_token (parser->lexer);
10574 /* Parse the mem-initializer. */
10575 mem_initializer = cp_parser_mem_initializer (parser);
10576 /* If the next token is a `...', we're expanding member initializers. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10579 /* Consume the `...'. */
10580 cp_lexer_consume_token (parser->lexer);
10582 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10583 can be expanded but members cannot. */
10584 if (mem_initializer != error_mark_node
10585 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10587 error_at (token->location,
10588 "cannot expand initializer for member %<%D%>",
10589 TREE_PURPOSE (mem_initializer));
10590 mem_initializer = error_mark_node;
10593 /* Construct the pack expansion type. */
10594 if (mem_initializer != error_mark_node)
10595 mem_initializer = make_pack_expansion (mem_initializer);
10597 /* Add it to the list, unless it was erroneous. */
10598 if (mem_initializer != error_mark_node)
10600 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10601 mem_initializer_list = mem_initializer;
10603 /* If the next token is not a `,', we're done. */
10604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10606 /* Consume the `,' token. */
10607 cp_lexer_consume_token (parser->lexer);
10610 /* Perform semantic analysis. */
10611 if (DECL_CONSTRUCTOR_P (current_function_decl))
10612 finish_mem_initializers (mem_initializer_list);
10615 /* Parse a mem-initializer.
10618 mem-initializer-id ( expression-list [opt] )
10619 mem-initializer-id braced-init-list
10624 ( expression-list [opt] )
10626 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10627 class) or FIELD_DECL (for a non-static data member) to initialize;
10628 the TREE_VALUE is the expression-list. An empty initialization
10629 list is represented by void_list_node. */
10632 cp_parser_mem_initializer (cp_parser* parser)
10634 tree mem_initializer_id;
10635 tree expression_list;
10637 cp_token *token = cp_lexer_peek_token (parser->lexer);
10639 /* Find out what is being initialized. */
10640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10642 permerror (token->location,
10643 "anachronistic old-style base class initializer");
10644 mem_initializer_id = NULL_TREE;
10648 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10649 if (mem_initializer_id == error_mark_node)
10650 return mem_initializer_id;
10652 member = expand_member_init (mem_initializer_id);
10653 if (member && !DECL_P (member))
10654 in_base_initializer = 1;
10656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10658 bool expr_non_constant_p;
10659 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10660 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10661 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10662 expression_list = build_tree_list (NULL_TREE, expression_list);
10667 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10669 /*allow_expansion_p=*/true,
10670 /*non_constant_p=*/NULL);
10672 return error_mark_node;
10673 expression_list = build_tree_list_vec (vec);
10674 release_tree_vector (vec);
10677 if (expression_list == error_mark_node)
10678 return error_mark_node;
10679 if (!expression_list)
10680 expression_list = void_type_node;
10682 in_base_initializer = 0;
10684 return member ? build_tree_list (member, expression_list) : error_mark_node;
10687 /* Parse a mem-initializer-id.
10689 mem-initializer-id:
10690 :: [opt] nested-name-specifier [opt] class-name
10693 Returns a TYPE indicating the class to be initializer for the first
10694 production. Returns an IDENTIFIER_NODE indicating the data member
10695 to be initialized for the second production. */
10698 cp_parser_mem_initializer_id (cp_parser* parser)
10700 bool global_scope_p;
10701 bool nested_name_specifier_p;
10702 bool template_p = false;
10705 cp_token *token = cp_lexer_peek_token (parser->lexer);
10707 /* `typename' is not allowed in this context ([temp.res]). */
10708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10710 error_at (token->location,
10711 "keyword %<typename%> not allowed in this context (a qualified "
10712 "member initializer is implicitly a type)");
10713 cp_lexer_consume_token (parser->lexer);
10715 /* Look for the optional `::' operator. */
10717 = (cp_parser_global_scope_opt (parser,
10718 /*current_scope_valid_p=*/false)
10720 /* Look for the optional nested-name-specifier. The simplest way to
10725 The keyword `typename' is not permitted in a base-specifier or
10726 mem-initializer; in these contexts a qualified name that
10727 depends on a template-parameter is implicitly assumed to be a
10730 is to assume that we have seen the `typename' keyword at this
10732 nested_name_specifier_p
10733 = (cp_parser_nested_name_specifier_opt (parser,
10734 /*typename_keyword_p=*/true,
10735 /*check_dependency_p=*/true,
10737 /*is_declaration=*/true)
10739 if (nested_name_specifier_p)
10740 template_p = cp_parser_optional_template_keyword (parser);
10741 /* If there is a `::' operator or a nested-name-specifier, then we
10742 are definitely looking for a class-name. */
10743 if (global_scope_p || nested_name_specifier_p)
10744 return cp_parser_class_name (parser,
10745 /*typename_keyword_p=*/true,
10746 /*template_keyword_p=*/template_p,
10748 /*check_dependency_p=*/true,
10749 /*class_head_p=*/false,
10750 /*is_declaration=*/true);
10751 /* Otherwise, we could also be looking for an ordinary identifier. */
10752 cp_parser_parse_tentatively (parser);
10753 /* Try a class-name. */
10754 id = cp_parser_class_name (parser,
10755 /*typename_keyword_p=*/true,
10756 /*template_keyword_p=*/false,
10758 /*check_dependency_p=*/true,
10759 /*class_head_p=*/false,
10760 /*is_declaration=*/true);
10761 /* If we found one, we're done. */
10762 if (cp_parser_parse_definitely (parser))
10764 /* Otherwise, look for an ordinary identifier. */
10765 return cp_parser_identifier (parser);
10768 /* Overloading [gram.over] */
10770 /* Parse an operator-function-id.
10772 operator-function-id:
10775 Returns an IDENTIFIER_NODE for the operator which is a
10776 human-readable spelling of the identifier, e.g., `operator +'. */
10779 cp_parser_operator_function_id (cp_parser* parser)
10781 /* Look for the `operator' keyword. */
10782 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10783 return error_mark_node;
10784 /* And then the name of the operator itself. */
10785 return cp_parser_operator (parser);
10788 /* Parse an operator.
10791 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10792 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10793 || ++ -- , ->* -> () []
10800 Returns an IDENTIFIER_NODE for the operator which is a
10801 human-readable spelling of the identifier, e.g., `operator +'. */
10804 cp_parser_operator (cp_parser* parser)
10806 tree id = NULL_TREE;
10809 /* Peek at the next token. */
10810 token = cp_lexer_peek_token (parser->lexer);
10811 /* Figure out which operator we have. */
10812 switch (token->type)
10818 /* The keyword should be either `new' or `delete'. */
10819 if (token->keyword == RID_NEW)
10821 else if (token->keyword == RID_DELETE)
10826 /* Consume the `new' or `delete' token. */
10827 cp_lexer_consume_token (parser->lexer);
10829 /* Peek at the next token. */
10830 token = cp_lexer_peek_token (parser->lexer);
10831 /* If it's a `[' token then this is the array variant of the
10833 if (token->type == CPP_OPEN_SQUARE)
10835 /* Consume the `[' token. */
10836 cp_lexer_consume_token (parser->lexer);
10837 /* Look for the `]' token. */
10838 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10839 id = ansi_opname (op == NEW_EXPR
10840 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10842 /* Otherwise, we have the non-array variant. */
10844 id = ansi_opname (op);
10850 id = ansi_opname (PLUS_EXPR);
10854 id = ansi_opname (MINUS_EXPR);
10858 id = ansi_opname (MULT_EXPR);
10862 id = ansi_opname (TRUNC_DIV_EXPR);
10866 id = ansi_opname (TRUNC_MOD_EXPR);
10870 id = ansi_opname (BIT_XOR_EXPR);
10874 id = ansi_opname (BIT_AND_EXPR);
10878 id = ansi_opname (BIT_IOR_EXPR);
10882 id = ansi_opname (BIT_NOT_EXPR);
10886 id = ansi_opname (TRUTH_NOT_EXPR);
10890 id = ansi_assopname (NOP_EXPR);
10894 id = ansi_opname (LT_EXPR);
10898 id = ansi_opname (GT_EXPR);
10902 id = ansi_assopname (PLUS_EXPR);
10906 id = ansi_assopname (MINUS_EXPR);
10910 id = ansi_assopname (MULT_EXPR);
10914 id = ansi_assopname (TRUNC_DIV_EXPR);
10918 id = ansi_assopname (TRUNC_MOD_EXPR);
10922 id = ansi_assopname (BIT_XOR_EXPR);
10926 id = ansi_assopname (BIT_AND_EXPR);
10930 id = ansi_assopname (BIT_IOR_EXPR);
10934 id = ansi_opname (LSHIFT_EXPR);
10938 id = ansi_opname (RSHIFT_EXPR);
10941 case CPP_LSHIFT_EQ:
10942 id = ansi_assopname (LSHIFT_EXPR);
10945 case CPP_RSHIFT_EQ:
10946 id = ansi_assopname (RSHIFT_EXPR);
10950 id = ansi_opname (EQ_EXPR);
10954 id = ansi_opname (NE_EXPR);
10958 id = ansi_opname (LE_EXPR);
10961 case CPP_GREATER_EQ:
10962 id = ansi_opname (GE_EXPR);
10966 id = ansi_opname (TRUTH_ANDIF_EXPR);
10970 id = ansi_opname (TRUTH_ORIF_EXPR);
10973 case CPP_PLUS_PLUS:
10974 id = ansi_opname (POSTINCREMENT_EXPR);
10977 case CPP_MINUS_MINUS:
10978 id = ansi_opname (PREDECREMENT_EXPR);
10982 id = ansi_opname (COMPOUND_EXPR);
10985 case CPP_DEREF_STAR:
10986 id = ansi_opname (MEMBER_REF);
10990 id = ansi_opname (COMPONENT_REF);
10993 case CPP_OPEN_PAREN:
10994 /* Consume the `('. */
10995 cp_lexer_consume_token (parser->lexer);
10996 /* Look for the matching `)'. */
10997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10998 return ansi_opname (CALL_EXPR);
11000 case CPP_OPEN_SQUARE:
11001 /* Consume the `['. */
11002 cp_lexer_consume_token (parser->lexer);
11003 /* Look for the matching `]'. */
11004 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11005 return ansi_opname (ARRAY_REF);
11008 /* Anything else is an error. */
11012 /* If we have selected an identifier, we need to consume the
11015 cp_lexer_consume_token (parser->lexer);
11016 /* Otherwise, no valid operator name was present. */
11019 cp_parser_error (parser, "expected operator");
11020 id = error_mark_node;
11026 /* Parse a template-declaration.
11028 template-declaration:
11029 export [opt] template < template-parameter-list > declaration
11031 If MEMBER_P is TRUE, this template-declaration occurs within a
11034 The grammar rule given by the standard isn't correct. What
11035 is really meant is:
11037 template-declaration:
11038 export [opt] template-parameter-list-seq
11039 decl-specifier-seq [opt] init-declarator [opt] ;
11040 export [opt] template-parameter-list-seq
11041 function-definition
11043 template-parameter-list-seq:
11044 template-parameter-list-seq [opt]
11045 template < template-parameter-list > */
11048 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11050 /* Check for `export'. */
11051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11053 /* Consume the `export' token. */
11054 cp_lexer_consume_token (parser->lexer);
11055 /* Warn that we do not support `export'. */
11056 warning (0, "keyword %<export%> not implemented, and will be ignored");
11059 cp_parser_template_declaration_after_export (parser, member_p);
11062 /* Parse a template-parameter-list.
11064 template-parameter-list:
11066 template-parameter-list , template-parameter
11068 Returns a TREE_LIST. Each node represents a template parameter.
11069 The nodes are connected via their TREE_CHAINs. */
11072 cp_parser_template_parameter_list (cp_parser* parser)
11074 tree parameter_list = NULL_TREE;
11076 begin_template_parm_list ();
11078 /* The loop below parses the template parms. We first need to know
11079 the total number of template parms to be able to compute proper
11080 canonical types of each dependent type. So after the loop, when
11081 we know the total number of template parms,
11082 end_template_parm_list computes the proper canonical types and
11083 fixes up the dependent types accordingly. */
11088 bool is_parameter_pack;
11089 location_t parm_loc;
11091 /* Parse the template-parameter. */
11092 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11093 parameter = cp_parser_template_parameter (parser,
11095 &is_parameter_pack);
11096 /* Add it to the list. */
11097 if (parameter != error_mark_node)
11098 parameter_list = process_template_parm (parameter_list,
11106 tree err_parm = build_tree_list (parameter, parameter);
11107 parameter_list = chainon (parameter_list, err_parm);
11110 /* If the next token is not a `,', we're done. */
11111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11113 /* Otherwise, consume the `,' token. */
11114 cp_lexer_consume_token (parser->lexer);
11117 return end_template_parm_list (parameter_list);
11120 /* Parse a template-parameter.
11122 template-parameter:
11124 parameter-declaration
11126 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11127 the parameter. The TREE_PURPOSE is the default value, if any.
11128 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11129 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11130 set to true iff this parameter is a parameter pack. */
11133 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11134 bool *is_parameter_pack)
11137 cp_parameter_declarator *parameter_declarator;
11138 cp_declarator *id_declarator;
11141 /* Assume it is a type parameter or a template parameter. */
11142 *is_non_type = false;
11143 /* Assume it not a parameter pack. */
11144 *is_parameter_pack = false;
11145 /* Peek at the next token. */
11146 token = cp_lexer_peek_token (parser->lexer);
11147 /* If it is `class' or `template', we have a type-parameter. */
11148 if (token->keyword == RID_TEMPLATE)
11149 return cp_parser_type_parameter (parser, is_parameter_pack);
11150 /* If it is `class' or `typename' we do not know yet whether it is a
11151 type parameter or a non-type parameter. Consider:
11153 template <typename T, typename T::X X> ...
11157 template <class C, class D*> ...
11159 Here, the first parameter is a type parameter, and the second is
11160 a non-type parameter. We can tell by looking at the token after
11161 the identifier -- if it is a `,', `=', or `>' then we have a type
11163 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11165 /* Peek at the token after `class' or `typename'. */
11166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11167 /* If it's an ellipsis, we have a template type parameter
11169 if (token->type == CPP_ELLIPSIS)
11170 return cp_parser_type_parameter (parser, is_parameter_pack);
11171 /* If it's an identifier, skip it. */
11172 if (token->type == CPP_NAME)
11173 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11174 /* Now, see if the token looks like the end of a template
11176 if (token->type == CPP_COMMA
11177 || token->type == CPP_EQ
11178 || token->type == CPP_GREATER)
11179 return cp_parser_type_parameter (parser, is_parameter_pack);
11182 /* Otherwise, it is a non-type parameter.
11186 When parsing a default template-argument for a non-type
11187 template-parameter, the first non-nested `>' is taken as the end
11188 of the template parameter-list rather than a greater-than
11190 *is_non_type = true;
11191 parameter_declarator
11192 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11193 /*parenthesized_p=*/NULL);
11195 /* If the parameter declaration is marked as a parameter pack, set
11196 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11197 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11199 if (parameter_declarator
11200 && parameter_declarator->declarator
11201 && parameter_declarator->declarator->parameter_pack_p)
11203 *is_parameter_pack = true;
11204 parameter_declarator->declarator->parameter_pack_p = false;
11207 /* If the next token is an ellipsis, and we don't already have it
11208 marked as a parameter pack, then we have a parameter pack (that
11209 has no declarator). */
11210 if (!*is_parameter_pack
11211 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11212 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11214 /* Consume the `...'. */
11215 cp_lexer_consume_token (parser->lexer);
11216 maybe_warn_variadic_templates ();
11218 *is_parameter_pack = true;
11220 /* We might end up with a pack expansion as the type of the non-type
11221 template parameter, in which case this is a non-type template
11223 else if (parameter_declarator
11224 && parameter_declarator->decl_specifiers.type
11225 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11227 *is_parameter_pack = true;
11228 parameter_declarator->decl_specifiers.type =
11229 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11232 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11234 /* Parameter packs cannot have default arguments. However, a
11235 user may try to do so, so we'll parse them and give an
11236 appropriate diagnostic here. */
11238 /* Consume the `='. */
11239 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11240 cp_lexer_consume_token (parser->lexer);
11242 /* Find the name of the parameter pack. */
11243 id_declarator = parameter_declarator->declarator;
11244 while (id_declarator && id_declarator->kind != cdk_id)
11245 id_declarator = id_declarator->declarator;
11247 if (id_declarator && id_declarator->kind == cdk_id)
11248 error_at (start_token->location,
11249 "template parameter pack %qD cannot have a default argument",
11250 id_declarator->u.id.unqualified_name);
11252 error_at (start_token->location,
11253 "template parameter pack cannot have a default argument");
11255 /* Parse the default argument, but throw away the result. */
11256 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11259 parm = grokdeclarator (parameter_declarator->declarator,
11260 ¶meter_declarator->decl_specifiers,
11261 TPARM, /*initialized=*/0,
11262 /*attrlist=*/NULL);
11263 if (parm == error_mark_node)
11264 return error_mark_node;
11266 return build_tree_list (parameter_declarator->default_argument, parm);
11269 /* Parse a type-parameter.
11272 class identifier [opt]
11273 class identifier [opt] = type-id
11274 typename identifier [opt]
11275 typename identifier [opt] = type-id
11276 template < template-parameter-list > class identifier [opt]
11277 template < template-parameter-list > class identifier [opt]
11280 GNU Extension (variadic templates):
11283 class ... identifier [opt]
11284 typename ... identifier [opt]
11286 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11287 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11288 the declaration of the parameter.
11290 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11293 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11298 /* Look for a keyword to tell us what kind of parameter this is. */
11299 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11301 return error_mark_node;
11303 switch (token->keyword)
11309 tree default_argument;
11311 /* If the next token is an ellipsis, we have a template
11313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11315 /* Consume the `...' token. */
11316 cp_lexer_consume_token (parser->lexer);
11317 maybe_warn_variadic_templates ();
11319 *is_parameter_pack = true;
11322 /* If the next token is an identifier, then it names the
11324 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11325 identifier = cp_parser_identifier (parser);
11327 identifier = NULL_TREE;
11329 /* Create the parameter. */
11330 parameter = finish_template_type_parm (class_type_node, identifier);
11332 /* If the next token is an `=', we have a default argument. */
11333 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11335 /* Consume the `=' token. */
11336 cp_lexer_consume_token (parser->lexer);
11337 /* Parse the default-argument. */
11338 push_deferring_access_checks (dk_no_deferred);
11339 default_argument = cp_parser_type_id (parser);
11341 /* Template parameter packs cannot have default
11343 if (*is_parameter_pack)
11346 error_at (token->location,
11347 "template parameter pack %qD cannot have a "
11348 "default argument", identifier);
11350 error_at (token->location,
11351 "template parameter packs cannot have "
11352 "default arguments");
11353 default_argument = NULL_TREE;
11355 pop_deferring_access_checks ();
11358 default_argument = NULL_TREE;
11360 /* Create the combined representation of the parameter and the
11361 default argument. */
11362 parameter = build_tree_list (default_argument, parameter);
11369 tree default_argument;
11371 /* Look for the `<'. */
11372 cp_parser_require (parser, CPP_LESS, RT_LESS);
11373 /* Parse the template-parameter-list. */
11374 cp_parser_template_parameter_list (parser);
11375 /* Look for the `>'. */
11376 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11377 /* Look for the `class' keyword. */
11378 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11379 /* If the next token is an ellipsis, we have a template
11381 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11383 /* Consume the `...' token. */
11384 cp_lexer_consume_token (parser->lexer);
11385 maybe_warn_variadic_templates ();
11387 *is_parameter_pack = true;
11389 /* If the next token is an `=', then there is a
11390 default-argument. If the next token is a `>', we are at
11391 the end of the parameter-list. If the next token is a `,',
11392 then we are at the end of this parameter. */
11393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11394 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11395 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11397 identifier = cp_parser_identifier (parser);
11398 /* Treat invalid names as if the parameter were nameless. */
11399 if (identifier == error_mark_node)
11400 identifier = NULL_TREE;
11403 identifier = NULL_TREE;
11405 /* Create the template parameter. */
11406 parameter = finish_template_template_parm (class_type_node,
11409 /* If the next token is an `=', then there is a
11410 default-argument. */
11411 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11415 /* Consume the `='. */
11416 cp_lexer_consume_token (parser->lexer);
11417 /* Parse the id-expression. */
11418 push_deferring_access_checks (dk_no_deferred);
11419 /* save token before parsing the id-expression, for error
11421 token = cp_lexer_peek_token (parser->lexer);
11423 = cp_parser_id_expression (parser,
11424 /*template_keyword_p=*/false,
11425 /*check_dependency_p=*/true,
11426 /*template_p=*/&is_template,
11427 /*declarator_p=*/false,
11428 /*optional_p=*/false);
11429 if (TREE_CODE (default_argument) == TYPE_DECL)
11430 /* If the id-expression was a template-id that refers to
11431 a template-class, we already have the declaration here,
11432 so no further lookup is needed. */
11435 /* Look up the name. */
11437 = cp_parser_lookup_name (parser, default_argument,
11439 /*is_template=*/is_template,
11440 /*is_namespace=*/false,
11441 /*check_dependency=*/true,
11442 /*ambiguous_decls=*/NULL,
11444 /* See if the default argument is valid. */
11446 = check_template_template_default_arg (default_argument);
11448 /* Template parameter packs cannot have default
11450 if (*is_parameter_pack)
11453 error_at (token->location,
11454 "template parameter pack %qD cannot "
11455 "have a default argument",
11458 error_at (token->location, "template parameter packs cannot "
11459 "have default arguments");
11460 default_argument = NULL_TREE;
11462 pop_deferring_access_checks ();
11465 default_argument = NULL_TREE;
11467 /* Create the combined representation of the parameter and the
11468 default argument. */
11469 parameter = build_tree_list (default_argument, parameter);
11474 gcc_unreachable ();
11481 /* Parse a template-id.
11484 template-name < template-argument-list [opt] >
11486 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11487 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11488 returned. Otherwise, if the template-name names a function, or set
11489 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11490 names a class, returns a TYPE_DECL for the specialization.
11492 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11493 uninstantiated templates. */
11496 cp_parser_template_id (cp_parser *parser,
11497 bool template_keyword_p,
11498 bool check_dependency_p,
11499 bool is_declaration)
11505 cp_token_position start_of_id = 0;
11506 deferred_access_check *chk;
11507 VEC (deferred_access_check,gc) *access_check;
11508 cp_token *next_token = NULL, *next_token_2 = NULL;
11509 bool is_identifier;
11511 /* If the next token corresponds to a template-id, there is no need
11513 next_token = cp_lexer_peek_token (parser->lexer);
11514 if (next_token->type == CPP_TEMPLATE_ID)
11516 struct tree_check *check_value;
11518 /* Get the stored value. */
11519 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11520 /* Perform any access checks that were deferred. */
11521 access_check = check_value->checks;
11524 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11525 perform_or_defer_access_check (chk->binfo,
11529 /* Return the stored value. */
11530 return check_value->value;
11533 /* Avoid performing name lookup if there is no possibility of
11534 finding a template-id. */
11535 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11536 || (next_token->type == CPP_NAME
11537 && !cp_parser_nth_token_starts_template_argument_list_p
11540 cp_parser_error (parser, "expected template-id");
11541 return error_mark_node;
11544 /* Remember where the template-id starts. */
11545 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11546 start_of_id = cp_lexer_token_position (parser->lexer, false);
11548 push_deferring_access_checks (dk_deferred);
11550 /* Parse the template-name. */
11551 is_identifier = false;
11552 templ = cp_parser_template_name (parser, template_keyword_p,
11553 check_dependency_p,
11556 if (templ == error_mark_node || is_identifier)
11558 pop_deferring_access_checks ();
11562 /* If we find the sequence `[:' after a template-name, it's probably
11563 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11564 parse correctly the argument list. */
11565 next_token = cp_lexer_peek_token (parser->lexer);
11566 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11567 if (next_token->type == CPP_OPEN_SQUARE
11568 && next_token->flags & DIGRAPH
11569 && next_token_2->type == CPP_COLON
11570 && !(next_token_2->flags & PREV_WHITE))
11572 cp_parser_parse_tentatively (parser);
11573 /* Change `:' into `::'. */
11574 next_token_2->type = CPP_SCOPE;
11575 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11577 cp_lexer_consume_token (parser->lexer);
11579 /* Parse the arguments. */
11580 arguments = cp_parser_enclosed_template_argument_list (parser);
11581 if (!cp_parser_parse_definitely (parser))
11583 /* If we couldn't parse an argument list, then we revert our changes
11584 and return simply an error. Maybe this is not a template-id
11586 next_token_2->type = CPP_COLON;
11587 cp_parser_error (parser, "expected %<<%>");
11588 pop_deferring_access_checks ();
11589 return error_mark_node;
11591 /* Otherwise, emit an error about the invalid digraph, but continue
11592 parsing because we got our argument list. */
11593 if (permerror (next_token->location,
11594 "%<<::%> cannot begin a template-argument list"))
11596 static bool hint = false;
11597 inform (next_token->location,
11598 "%<<:%> is an alternate spelling for %<[%>."
11599 " Insert whitespace between %<<%> and %<::%>");
11600 if (!hint && !flag_permissive)
11602 inform (next_token->location, "(if you use %<-fpermissive%>"
11603 " G++ will accept your code)");
11610 /* Look for the `<' that starts the template-argument-list. */
11611 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11613 pop_deferring_access_checks ();
11614 return error_mark_node;
11616 /* Parse the arguments. */
11617 arguments = cp_parser_enclosed_template_argument_list (parser);
11620 /* Build a representation of the specialization. */
11621 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11622 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11623 else if (DECL_CLASS_TEMPLATE_P (templ)
11624 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11626 bool entering_scope;
11627 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11628 template (rather than some instantiation thereof) only if
11629 is not nested within some other construct. For example, in
11630 "template <typename T> void f(T) { A<T>::", A<T> is just an
11631 instantiation of A. */
11632 entering_scope = (template_parm_scope_p ()
11633 && cp_lexer_next_token_is (parser->lexer,
11636 = finish_template_type (templ, arguments, entering_scope);
11640 /* If it's not a class-template or a template-template, it should be
11641 a function-template. */
11642 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11643 || TREE_CODE (templ) == OVERLOAD
11644 || BASELINK_P (templ)));
11646 template_id = lookup_template_function (templ, arguments);
11649 /* If parsing tentatively, replace the sequence of tokens that makes
11650 up the template-id with a CPP_TEMPLATE_ID token. That way,
11651 should we re-parse the token stream, we will not have to repeat
11652 the effort required to do the parse, nor will we issue duplicate
11653 error messages about problems during instantiation of the
11657 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11659 /* Reset the contents of the START_OF_ID token. */
11660 token->type = CPP_TEMPLATE_ID;
11661 /* Retrieve any deferred checks. Do not pop this access checks yet
11662 so the memory will not be reclaimed during token replacing below. */
11663 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11664 token->u.tree_check_value->value = template_id;
11665 token->u.tree_check_value->checks = get_deferred_access_checks ();
11666 token->keyword = RID_MAX;
11668 /* Purge all subsequent tokens. */
11669 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11671 /* ??? Can we actually assume that, if template_id ==
11672 error_mark_node, we will have issued a diagnostic to the
11673 user, as opposed to simply marking the tentative parse as
11675 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11676 error_at (token->location, "parse error in template argument list");
11679 pop_deferring_access_checks ();
11680 return template_id;
11683 /* Parse a template-name.
11688 The standard should actually say:
11692 operator-function-id
11694 A defect report has been filed about this issue.
11696 A conversion-function-id cannot be a template name because they cannot
11697 be part of a template-id. In fact, looking at this code:
11699 a.operator K<int>()
11701 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11702 It is impossible to call a templated conversion-function-id with an
11703 explicit argument list, since the only allowed template parameter is
11704 the type to which it is converting.
11706 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11707 `template' keyword, in a construction like:
11711 In that case `f' is taken to be a template-name, even though there
11712 is no way of knowing for sure.
11714 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11715 name refers to a set of overloaded functions, at least one of which
11716 is a template, or an IDENTIFIER_NODE with the name of the template,
11717 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11718 names are looked up inside uninstantiated templates. */
11721 cp_parser_template_name (cp_parser* parser,
11722 bool template_keyword_p,
11723 bool check_dependency_p,
11724 bool is_declaration,
11725 bool *is_identifier)
11730 cp_token *token = cp_lexer_peek_token (parser->lexer);
11732 /* If the next token is `operator', then we have either an
11733 operator-function-id or a conversion-function-id. */
11734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11736 /* We don't know whether we're looking at an
11737 operator-function-id or a conversion-function-id. */
11738 cp_parser_parse_tentatively (parser);
11739 /* Try an operator-function-id. */
11740 identifier = cp_parser_operator_function_id (parser);
11741 /* If that didn't work, try a conversion-function-id. */
11742 if (!cp_parser_parse_definitely (parser))
11744 cp_parser_error (parser, "expected template-name");
11745 return error_mark_node;
11748 /* Look for the identifier. */
11750 identifier = cp_parser_identifier (parser);
11752 /* If we didn't find an identifier, we don't have a template-id. */
11753 if (identifier == error_mark_node)
11754 return error_mark_node;
11756 /* If the name immediately followed the `template' keyword, then it
11757 is a template-name. However, if the next token is not `<', then
11758 we do not treat it as a template-name, since it is not being used
11759 as part of a template-id. This enables us to handle constructs
11762 template <typename T> struct S { S(); };
11763 template <typename T> S<T>::S();
11765 correctly. We would treat `S' as a template -- if it were `S<T>'
11766 -- but we do not if there is no `<'. */
11768 if (processing_template_decl
11769 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11771 /* In a declaration, in a dependent context, we pretend that the
11772 "template" keyword was present in order to improve error
11773 recovery. For example, given:
11775 template <typename T> void f(T::X<int>);
11777 we want to treat "X<int>" as a template-id. */
11779 && !template_keyword_p
11780 && parser->scope && TYPE_P (parser->scope)
11781 && check_dependency_p
11782 && dependent_scope_p (parser->scope)
11783 /* Do not do this for dtors (or ctors), since they never
11784 need the template keyword before their name. */
11785 && !constructor_name_p (identifier, parser->scope))
11787 cp_token_position start = 0;
11789 /* Explain what went wrong. */
11790 error_at (token->location, "non-template %qD used as template",
11792 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11793 parser->scope, identifier);
11794 /* If parsing tentatively, find the location of the "<" token. */
11795 if (cp_parser_simulate_error (parser))
11796 start = cp_lexer_token_position (parser->lexer, true);
11797 /* Parse the template arguments so that we can issue error
11798 messages about them. */
11799 cp_lexer_consume_token (parser->lexer);
11800 cp_parser_enclosed_template_argument_list (parser);
11801 /* Skip tokens until we find a good place from which to
11802 continue parsing. */
11803 cp_parser_skip_to_closing_parenthesis (parser,
11804 /*recovering=*/true,
11806 /*consume_paren=*/false);
11807 /* If parsing tentatively, permanently remove the
11808 template argument list. That will prevent duplicate
11809 error messages from being issued about the missing
11810 "template" keyword. */
11812 cp_lexer_purge_tokens_after (parser->lexer, start);
11814 *is_identifier = true;
11818 /* If the "template" keyword is present, then there is generally
11819 no point in doing name-lookup, so we just return IDENTIFIER.
11820 But, if the qualifying scope is non-dependent then we can
11821 (and must) do name-lookup normally. */
11822 if (template_keyword_p
11824 || (TYPE_P (parser->scope)
11825 && dependent_type_p (parser->scope))))
11829 /* Look up the name. */
11830 decl = cp_parser_lookup_name (parser, identifier,
11832 /*is_template=*/true,
11833 /*is_namespace=*/false,
11834 check_dependency_p,
11835 /*ambiguous_decls=*/NULL,
11838 /* If DECL is a template, then the name was a template-name. */
11839 if (TREE_CODE (decl) == TEMPLATE_DECL)
11843 tree fn = NULL_TREE;
11845 /* The standard does not explicitly indicate whether a name that
11846 names a set of overloaded declarations, some of which are
11847 templates, is a template-name. However, such a name should
11848 be a template-name; otherwise, there is no way to form a
11849 template-id for the overloaded templates. */
11850 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11851 if (TREE_CODE (fns) == OVERLOAD)
11852 for (fn = fns; fn; fn = OVL_NEXT (fn))
11853 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11858 /* The name does not name a template. */
11859 cp_parser_error (parser, "expected template-name");
11860 return error_mark_node;
11864 /* If DECL is dependent, and refers to a function, then just return
11865 its name; we will look it up again during template instantiation. */
11866 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11868 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11869 if (TYPE_P (scope) && dependent_type_p (scope))
11876 /* Parse a template-argument-list.
11878 template-argument-list:
11879 template-argument ... [opt]
11880 template-argument-list , template-argument ... [opt]
11882 Returns a TREE_VEC containing the arguments. */
11885 cp_parser_template_argument_list (cp_parser* parser)
11887 tree fixed_args[10];
11888 unsigned n_args = 0;
11889 unsigned alloced = 10;
11890 tree *arg_ary = fixed_args;
11892 bool saved_in_template_argument_list_p;
11894 bool saved_non_ice_p;
11896 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11897 parser->in_template_argument_list_p = true;
11898 /* Even if the template-id appears in an integral
11899 constant-expression, the contents of the argument list do
11901 saved_ice_p = parser->integral_constant_expression_p;
11902 parser->integral_constant_expression_p = false;
11903 saved_non_ice_p = parser->non_integral_constant_expression_p;
11904 parser->non_integral_constant_expression_p = false;
11905 /* Parse the arguments. */
11911 /* Consume the comma. */
11912 cp_lexer_consume_token (parser->lexer);
11914 /* Parse the template-argument. */
11915 argument = cp_parser_template_argument (parser);
11917 /* If the next token is an ellipsis, we're expanding a template
11919 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11921 if (argument == error_mark_node)
11923 cp_token *token = cp_lexer_peek_token (parser->lexer);
11924 error_at (token->location,
11925 "expected parameter pack before %<...%>");
11927 /* Consume the `...' token. */
11928 cp_lexer_consume_token (parser->lexer);
11930 /* Make the argument into a TYPE_PACK_EXPANSION or
11931 EXPR_PACK_EXPANSION. */
11932 argument = make_pack_expansion (argument);
11935 if (n_args == alloced)
11939 if (arg_ary == fixed_args)
11941 arg_ary = XNEWVEC (tree, alloced);
11942 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11945 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11947 arg_ary[n_args++] = argument;
11949 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11951 vec = make_tree_vec (n_args);
11954 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11956 if (arg_ary != fixed_args)
11958 parser->non_integral_constant_expression_p = saved_non_ice_p;
11959 parser->integral_constant_expression_p = saved_ice_p;
11960 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11961 #ifdef ENABLE_CHECKING
11962 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11967 /* Parse a template-argument.
11970 assignment-expression
11974 The representation is that of an assignment-expression, type-id, or
11975 id-expression -- except that the qualified id-expression is
11976 evaluated, so that the value returned is either a DECL or an
11979 Although the standard says "assignment-expression", it forbids
11980 throw-expressions or assignments in the template argument.
11981 Therefore, we use "conditional-expression" instead. */
11984 cp_parser_template_argument (cp_parser* parser)
11989 bool maybe_type_id = false;
11990 cp_token *token = NULL, *argument_start_token = NULL;
11993 /* There's really no way to know what we're looking at, so we just
11994 try each alternative in order.
11998 In a template-argument, an ambiguity between a type-id and an
11999 expression is resolved to a type-id, regardless of the form of
12000 the corresponding template-parameter.
12002 Therefore, we try a type-id first. */
12003 cp_parser_parse_tentatively (parser);
12004 argument = cp_parser_template_type_arg (parser);
12005 /* If there was no error parsing the type-id but the next token is a
12006 '>>', our behavior depends on which dialect of C++ we're
12007 parsing. In C++98, we probably found a typo for '> >'. But there
12008 are type-id which are also valid expressions. For instance:
12010 struct X { int operator >> (int); };
12011 template <int V> struct Foo {};
12014 Here 'X()' is a valid type-id of a function type, but the user just
12015 wanted to write the expression "X() >> 5". Thus, we remember that we
12016 found a valid type-id, but we still try to parse the argument as an
12017 expression to see what happens.
12019 In C++0x, the '>>' will be considered two separate '>'
12021 if (!cp_parser_error_occurred (parser)
12022 && cxx_dialect == cxx98
12023 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12025 maybe_type_id = true;
12026 cp_parser_abort_tentative_parse (parser);
12030 /* If the next token isn't a `,' or a `>', then this argument wasn't
12031 really finished. This means that the argument is not a valid
12033 if (!cp_parser_next_token_ends_template_argument_p (parser))
12034 cp_parser_error (parser, "expected template-argument");
12035 /* If that worked, we're done. */
12036 if (cp_parser_parse_definitely (parser))
12039 /* We're still not sure what the argument will be. */
12040 cp_parser_parse_tentatively (parser);
12041 /* Try a template. */
12042 argument_start_token = cp_lexer_peek_token (parser->lexer);
12043 argument = cp_parser_id_expression (parser,
12044 /*template_keyword_p=*/false,
12045 /*check_dependency_p=*/true,
12047 /*declarator_p=*/false,
12048 /*optional_p=*/false);
12049 /* If the next token isn't a `,' or a `>', then this argument wasn't
12050 really finished. */
12051 if (!cp_parser_next_token_ends_template_argument_p (parser))
12052 cp_parser_error (parser, "expected template-argument");
12053 if (!cp_parser_error_occurred (parser))
12055 /* Figure out what is being referred to. If the id-expression
12056 was for a class template specialization, then we will have a
12057 TYPE_DECL at this point. There is no need to do name lookup
12058 at this point in that case. */
12059 if (TREE_CODE (argument) != TYPE_DECL)
12060 argument = cp_parser_lookup_name (parser, argument,
12062 /*is_template=*/template_p,
12063 /*is_namespace=*/false,
12064 /*check_dependency=*/true,
12065 /*ambiguous_decls=*/NULL,
12066 argument_start_token->location);
12067 if (TREE_CODE (argument) != TEMPLATE_DECL
12068 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12069 cp_parser_error (parser, "expected template-name");
12071 if (cp_parser_parse_definitely (parser))
12073 /* It must be a non-type argument. There permitted cases are given
12074 in [temp.arg.nontype]:
12076 -- an integral constant-expression of integral or enumeration
12079 -- the name of a non-type template-parameter; or
12081 -- the name of an object or function with external linkage...
12083 -- the address of an object or function with external linkage...
12085 -- a pointer to member... */
12086 /* Look for a non-type template parameter. */
12087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12089 cp_parser_parse_tentatively (parser);
12090 argument = cp_parser_primary_expression (parser,
12091 /*address_p=*/false,
12093 /*template_arg_p=*/true,
12095 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12096 || !cp_parser_next_token_ends_template_argument_p (parser))
12097 cp_parser_simulate_error (parser);
12098 if (cp_parser_parse_definitely (parser))
12102 /* If the next token is "&", the argument must be the address of an
12103 object or function with external linkage. */
12104 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12106 cp_lexer_consume_token (parser->lexer);
12107 /* See if we might have an id-expression. */
12108 token = cp_lexer_peek_token (parser->lexer);
12109 if (token->type == CPP_NAME
12110 || token->keyword == RID_OPERATOR
12111 || token->type == CPP_SCOPE
12112 || token->type == CPP_TEMPLATE_ID
12113 || token->type == CPP_NESTED_NAME_SPECIFIER)
12115 cp_parser_parse_tentatively (parser);
12116 argument = cp_parser_primary_expression (parser,
12119 /*template_arg_p=*/true,
12121 if (cp_parser_error_occurred (parser)
12122 || !cp_parser_next_token_ends_template_argument_p (parser))
12123 cp_parser_abort_tentative_parse (parser);
12128 if (TREE_CODE (argument) == INDIRECT_REF)
12130 gcc_assert (REFERENCE_REF_P (argument));
12131 argument = TREE_OPERAND (argument, 0);
12134 /* If we're in a template, we represent a qualified-id referring
12135 to a static data member as a SCOPE_REF even if the scope isn't
12136 dependent so that we can check access control later. */
12138 if (TREE_CODE (probe) == SCOPE_REF)
12139 probe = TREE_OPERAND (probe, 1);
12140 if (TREE_CODE (probe) == VAR_DECL)
12142 /* A variable without external linkage might still be a
12143 valid constant-expression, so no error is issued here
12144 if the external-linkage check fails. */
12145 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12146 cp_parser_simulate_error (parser);
12148 else if (is_overloaded_fn (argument))
12149 /* All overloaded functions are allowed; if the external
12150 linkage test does not pass, an error will be issued
12154 && (TREE_CODE (argument) == OFFSET_REF
12155 || TREE_CODE (argument) == SCOPE_REF))
12156 /* A pointer-to-member. */
12158 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12161 cp_parser_simulate_error (parser);
12163 if (cp_parser_parse_definitely (parser))
12166 argument = build_x_unary_op (ADDR_EXPR, argument,
12167 tf_warning_or_error);
12172 /* If the argument started with "&", there are no other valid
12173 alternatives at this point. */
12176 cp_parser_error (parser, "invalid non-type template argument");
12177 return error_mark_node;
12180 /* If the argument wasn't successfully parsed as a type-id followed
12181 by '>>', the argument can only be a constant expression now.
12182 Otherwise, we try parsing the constant-expression tentatively,
12183 because the argument could really be a type-id. */
12185 cp_parser_parse_tentatively (parser);
12186 argument = cp_parser_constant_expression (parser,
12187 /*allow_non_constant_p=*/false,
12188 /*non_constant_p=*/NULL);
12189 argument = fold_non_dependent_expr (argument);
12190 if (!maybe_type_id)
12192 if (!cp_parser_next_token_ends_template_argument_p (parser))
12193 cp_parser_error (parser, "expected template-argument");
12194 if (cp_parser_parse_definitely (parser))
12196 /* We did our best to parse the argument as a non type-id, but that
12197 was the only alternative that matched (albeit with a '>' after
12198 it). We can assume it's just a typo from the user, and a
12199 diagnostic will then be issued. */
12200 return cp_parser_template_type_arg (parser);
12203 /* Parse an explicit-instantiation.
12205 explicit-instantiation:
12206 template declaration
12208 Although the standard says `declaration', what it really means is:
12210 explicit-instantiation:
12211 template decl-specifier-seq [opt] declarator [opt] ;
12213 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12214 supposed to be allowed. A defect report has been filed about this
12219 explicit-instantiation:
12220 storage-class-specifier template
12221 decl-specifier-seq [opt] declarator [opt] ;
12222 function-specifier template
12223 decl-specifier-seq [opt] declarator [opt] ; */
12226 cp_parser_explicit_instantiation (cp_parser* parser)
12228 int declares_class_or_enum;
12229 cp_decl_specifier_seq decl_specifiers;
12230 tree extension_specifier = NULL_TREE;
12232 /* Look for an (optional) storage-class-specifier or
12233 function-specifier. */
12234 if (cp_parser_allow_gnu_extensions_p (parser))
12236 extension_specifier
12237 = cp_parser_storage_class_specifier_opt (parser);
12238 if (!extension_specifier)
12239 extension_specifier
12240 = cp_parser_function_specifier_opt (parser,
12241 /*decl_specs=*/NULL);
12244 /* Look for the `template' keyword. */
12245 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12246 /* Let the front end know that we are processing an explicit
12248 begin_explicit_instantiation ();
12249 /* [temp.explicit] says that we are supposed to ignore access
12250 control while processing explicit instantiation directives. */
12251 push_deferring_access_checks (dk_no_check);
12252 /* Parse a decl-specifier-seq. */
12253 cp_parser_decl_specifier_seq (parser,
12254 CP_PARSER_FLAGS_OPTIONAL,
12256 &declares_class_or_enum);
12257 /* If there was exactly one decl-specifier, and it declared a class,
12258 and there's no declarator, then we have an explicit type
12260 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12264 type = check_tag_decl (&decl_specifiers);
12265 /* Turn access control back on for names used during
12266 template instantiation. */
12267 pop_deferring_access_checks ();
12269 do_type_instantiation (type, extension_specifier,
12270 /*complain=*/tf_error);
12274 cp_declarator *declarator;
12277 /* Parse the declarator. */
12279 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12280 /*ctor_dtor_or_conv_p=*/NULL,
12281 /*parenthesized_p=*/NULL,
12282 /*member_p=*/false);
12283 if (declares_class_or_enum & 2)
12284 cp_parser_check_for_definition_in_return_type (declarator,
12285 decl_specifiers.type,
12286 decl_specifiers.type_location);
12287 if (declarator != cp_error_declarator)
12289 if (decl_specifiers.specs[(int)ds_inline])
12290 permerror (input_location, "explicit instantiation shall not use"
12291 " %<inline%> specifier");
12292 if (decl_specifiers.specs[(int)ds_constexpr])
12293 permerror (input_location, "explicit instantiation shall not use"
12294 " %<constexpr%> specifier");
12296 decl = grokdeclarator (declarator, &decl_specifiers,
12297 NORMAL, 0, &decl_specifiers.attributes);
12298 /* Turn access control back on for names used during
12299 template instantiation. */
12300 pop_deferring_access_checks ();
12301 /* Do the explicit instantiation. */
12302 do_decl_instantiation (decl, extension_specifier);
12306 pop_deferring_access_checks ();
12307 /* Skip the body of the explicit instantiation. */
12308 cp_parser_skip_to_end_of_statement (parser);
12311 /* We're done with the instantiation. */
12312 end_explicit_instantiation ();
12314 cp_parser_consume_semicolon_at_end_of_statement (parser);
12317 /* Parse an explicit-specialization.
12319 explicit-specialization:
12320 template < > declaration
12322 Although the standard says `declaration', what it really means is:
12324 explicit-specialization:
12325 template <> decl-specifier [opt] init-declarator [opt] ;
12326 template <> function-definition
12327 template <> explicit-specialization
12328 template <> template-declaration */
12331 cp_parser_explicit_specialization (cp_parser* parser)
12333 bool need_lang_pop;
12334 cp_token *token = cp_lexer_peek_token (parser->lexer);
12336 /* Look for the `template' keyword. */
12337 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12338 /* Look for the `<'. */
12339 cp_parser_require (parser, CPP_LESS, RT_LESS);
12340 /* Look for the `>'. */
12341 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12342 /* We have processed another parameter list. */
12343 ++parser->num_template_parameter_lists;
12346 A template ... explicit specialization ... shall not have C
12348 if (current_lang_name == lang_name_c)
12350 error_at (token->location, "template specialization with C linkage");
12351 /* Give it C++ linkage to avoid confusing other parts of the
12353 push_lang_context (lang_name_cplusplus);
12354 need_lang_pop = true;
12357 need_lang_pop = false;
12358 /* Let the front end know that we are beginning a specialization. */
12359 if (!begin_specialization ())
12361 end_specialization ();
12365 /* If the next keyword is `template', we need to figure out whether
12366 or not we're looking a template-declaration. */
12367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12369 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12370 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12371 cp_parser_template_declaration_after_export (parser,
12372 /*member_p=*/false);
12374 cp_parser_explicit_specialization (parser);
12377 /* Parse the dependent declaration. */
12378 cp_parser_single_declaration (parser,
12380 /*member_p=*/false,
12381 /*explicit_specialization_p=*/true,
12382 /*friend_p=*/NULL);
12383 /* We're done with the specialization. */
12384 end_specialization ();
12385 /* For the erroneous case of a template with C linkage, we pushed an
12386 implicit C++ linkage scope; exit that scope now. */
12388 pop_lang_context ();
12389 /* We're done with this parameter list. */
12390 --parser->num_template_parameter_lists;
12393 /* Parse a type-specifier.
12396 simple-type-specifier
12399 elaborated-type-specifier
12407 Returns a representation of the type-specifier. For a
12408 class-specifier, enum-specifier, or elaborated-type-specifier, a
12409 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12411 The parser flags FLAGS is used to control type-specifier parsing.
12413 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12414 in a decl-specifier-seq.
12416 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12417 class-specifier, enum-specifier, or elaborated-type-specifier, then
12418 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12419 if a type is declared; 2 if it is defined. Otherwise, it is set to
12422 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12423 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12424 is set to FALSE. */
12427 cp_parser_type_specifier (cp_parser* parser,
12428 cp_parser_flags flags,
12429 cp_decl_specifier_seq *decl_specs,
12430 bool is_declaration,
12431 int* declares_class_or_enum,
12432 bool* is_cv_qualifier)
12434 tree type_spec = NULL_TREE;
12437 cp_decl_spec ds = ds_last;
12439 /* Assume this type-specifier does not declare a new type. */
12440 if (declares_class_or_enum)
12441 *declares_class_or_enum = 0;
12442 /* And that it does not specify a cv-qualifier. */
12443 if (is_cv_qualifier)
12444 *is_cv_qualifier = false;
12445 /* Peek at the next token. */
12446 token = cp_lexer_peek_token (parser->lexer);
12448 /* If we're looking at a keyword, we can use that to guide the
12449 production we choose. */
12450 keyword = token->keyword;
12454 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12455 goto elaborated_type_specifier;
12457 /* Look for the enum-specifier. */
12458 type_spec = cp_parser_enum_specifier (parser);
12459 /* If that worked, we're done. */
12462 if (declares_class_or_enum)
12463 *declares_class_or_enum = 2;
12465 cp_parser_set_decl_spec_type (decl_specs,
12468 /*user_defined_p=*/true);
12472 goto elaborated_type_specifier;
12474 /* Any of these indicate either a class-specifier, or an
12475 elaborated-type-specifier. */
12479 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12480 goto elaborated_type_specifier;
12482 /* Parse tentatively so that we can back up if we don't find a
12483 class-specifier. */
12484 cp_parser_parse_tentatively (parser);
12485 /* Look for the class-specifier. */
12486 type_spec = cp_parser_class_specifier (parser);
12487 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12488 /* If that worked, we're done. */
12489 if (cp_parser_parse_definitely (parser))
12491 if (declares_class_or_enum)
12492 *declares_class_or_enum = 2;
12494 cp_parser_set_decl_spec_type (decl_specs,
12497 /*user_defined_p=*/true);
12501 /* Fall through. */
12502 elaborated_type_specifier:
12503 /* We're declaring (not defining) a class or enum. */
12504 if (declares_class_or_enum)
12505 *declares_class_or_enum = 1;
12507 /* Fall through. */
12509 /* Look for an elaborated-type-specifier. */
12511 = (cp_parser_elaborated_type_specifier
12513 decl_specs && decl_specs->specs[(int) ds_friend],
12516 cp_parser_set_decl_spec_type (decl_specs,
12519 /*user_defined_p=*/true);
12524 if (is_cv_qualifier)
12525 *is_cv_qualifier = true;
12530 if (is_cv_qualifier)
12531 *is_cv_qualifier = true;
12536 if (is_cv_qualifier)
12537 *is_cv_qualifier = true;
12541 /* The `__complex__' keyword is a GNU extension. */
12549 /* Handle simple keywords. */
12554 ++decl_specs->specs[(int)ds];
12555 decl_specs->any_specifiers_p = true;
12557 return cp_lexer_consume_token (parser->lexer)->u.value;
12560 /* If we do not already have a type-specifier, assume we are looking
12561 at a simple-type-specifier. */
12562 type_spec = cp_parser_simple_type_specifier (parser,
12566 /* If we didn't find a type-specifier, and a type-specifier was not
12567 optional in this context, issue an error message. */
12568 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12570 cp_parser_error (parser, "expected type specifier");
12571 return error_mark_node;
12577 /* Parse a simple-type-specifier.
12579 simple-type-specifier:
12580 :: [opt] nested-name-specifier [opt] type-name
12581 :: [opt] nested-name-specifier template template-id
12596 simple-type-specifier:
12598 decltype ( expression )
12604 simple-type-specifier:
12606 __typeof__ unary-expression
12607 __typeof__ ( type-id )
12609 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12610 appropriately updated. */
12613 cp_parser_simple_type_specifier (cp_parser* parser,
12614 cp_decl_specifier_seq *decl_specs,
12615 cp_parser_flags flags)
12617 tree type = NULL_TREE;
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_token (parser->lexer);
12623 /* If we're looking at a keyword, things are easy. */
12624 switch (token->keyword)
12628 decl_specs->explicit_char_p = true;
12629 type = char_type_node;
12632 type = char16_type_node;
12635 type = char32_type_node;
12638 type = wchar_type_node;
12641 type = boolean_type_node;
12645 ++decl_specs->specs[(int) ds_short];
12646 type = short_integer_type_node;
12650 decl_specs->explicit_int_p = true;
12651 type = integer_type_node;
12654 if (!int128_integer_type_node)
12657 decl_specs->explicit_int128_p = true;
12658 type = int128_integer_type_node;
12662 ++decl_specs->specs[(int) ds_long];
12663 type = long_integer_type_node;
12667 ++decl_specs->specs[(int) ds_signed];
12668 type = integer_type_node;
12672 ++decl_specs->specs[(int) ds_unsigned];
12673 type = unsigned_type_node;
12676 type = float_type_node;
12679 type = double_type_node;
12682 type = void_type_node;
12686 maybe_warn_cpp0x (CPP0X_AUTO);
12687 type = make_auto ();
12691 /* Parse the `decltype' type. */
12692 type = cp_parser_decltype (parser);
12695 cp_parser_set_decl_spec_type (decl_specs, type,
12697 /*user_defined_p=*/true);
12702 /* Consume the `typeof' token. */
12703 cp_lexer_consume_token (parser->lexer);
12704 /* Parse the operand to `typeof'. */
12705 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12706 /* If it is not already a TYPE, take its type. */
12707 if (!TYPE_P (type))
12708 type = finish_typeof (type);
12711 cp_parser_set_decl_spec_type (decl_specs, type,
12713 /*user_defined_p=*/true);
12721 /* If the type-specifier was for a built-in type, we're done. */
12724 /* Record the type. */
12726 && (token->keyword != RID_SIGNED
12727 && token->keyword != RID_UNSIGNED
12728 && token->keyword != RID_SHORT
12729 && token->keyword != RID_LONG))
12730 cp_parser_set_decl_spec_type (decl_specs,
12733 /*user_defined=*/false);
12735 decl_specs->any_specifiers_p = true;
12737 /* Consume the token. */
12738 cp_lexer_consume_token (parser->lexer);
12740 /* There is no valid C++ program where a non-template type is
12741 followed by a "<". That usually indicates that the user thought
12742 that the type was a template. */
12743 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12745 return TYPE_NAME (type);
12748 /* The type-specifier must be a user-defined type. */
12749 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12754 /* Don't gobble tokens or issue error messages if this is an
12755 optional type-specifier. */
12756 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12757 cp_parser_parse_tentatively (parser);
12759 /* Look for the optional `::' operator. */
12761 = (cp_parser_global_scope_opt (parser,
12762 /*current_scope_valid_p=*/false)
12764 /* Look for the nested-name specifier. */
12766 = (cp_parser_nested_name_specifier_opt (parser,
12767 /*typename_keyword_p=*/false,
12768 /*check_dependency_p=*/true,
12770 /*is_declaration=*/false)
12772 token = cp_lexer_peek_token (parser->lexer);
12773 /* If we have seen a nested-name-specifier, and the next token
12774 is `template', then we are using the template-id production. */
12776 && cp_parser_optional_template_keyword (parser))
12778 /* Look for the template-id. */
12779 type = cp_parser_template_id (parser,
12780 /*template_keyword_p=*/true,
12781 /*check_dependency_p=*/true,
12782 /*is_declaration=*/false);
12783 /* If the template-id did not name a type, we are out of
12785 if (TREE_CODE (type) != TYPE_DECL)
12787 cp_parser_error (parser, "expected template-id for type");
12791 /* Otherwise, look for a type-name. */
12793 type = cp_parser_type_name (parser);
12794 /* Keep track of all name-lookups performed in class scopes. */
12798 && TREE_CODE (type) == TYPE_DECL
12799 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12800 maybe_note_name_used_in_class (DECL_NAME (type), type);
12801 /* If it didn't work out, we don't have a TYPE. */
12802 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12803 && !cp_parser_parse_definitely (parser))
12805 if (type && decl_specs)
12806 cp_parser_set_decl_spec_type (decl_specs, type,
12808 /*user_defined=*/true);
12811 /* If we didn't get a type-name, issue an error message. */
12812 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12814 cp_parser_error (parser, "expected type-name");
12815 return error_mark_node;
12818 if (type && type != error_mark_node)
12820 /* See if TYPE is an Objective-C type, and if so, parse and
12821 accept any protocol references following it. Do this before
12822 the cp_parser_check_for_invalid_template_id() call, because
12823 Objective-C types can be followed by '<...>' which would
12824 enclose protocol names rather than template arguments, and so
12825 everything is fine. */
12826 if (c_dialect_objc () && !parser->scope
12827 && (objc_is_id (type) || objc_is_class_name (type)))
12829 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12830 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12832 /* Clobber the "unqualified" type previously entered into
12833 DECL_SPECS with the new, improved protocol-qualified version. */
12835 decl_specs->type = qual_type;
12840 /* There is no valid C++ program where a non-template type is
12841 followed by a "<". That usually indicates that the user
12842 thought that the type was a template. */
12843 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12850 /* Parse a type-name.
12863 Returns a TYPE_DECL for the type. */
12866 cp_parser_type_name (cp_parser* parser)
12870 /* We can't know yet whether it is a class-name or not. */
12871 cp_parser_parse_tentatively (parser);
12872 /* Try a class-name. */
12873 type_decl = cp_parser_class_name (parser,
12874 /*typename_keyword_p=*/false,
12875 /*template_keyword_p=*/false,
12877 /*check_dependency_p=*/true,
12878 /*class_head_p=*/false,
12879 /*is_declaration=*/false);
12880 /* If it's not a class-name, keep looking. */
12881 if (!cp_parser_parse_definitely (parser))
12883 /* It must be a typedef-name or an enum-name. */
12884 return cp_parser_nonclass_name (parser);
12890 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12898 Returns a TYPE_DECL for the type. */
12901 cp_parser_nonclass_name (cp_parser* parser)
12906 cp_token *token = cp_lexer_peek_token (parser->lexer);
12907 identifier = cp_parser_identifier (parser);
12908 if (identifier == error_mark_node)
12909 return error_mark_node;
12911 /* Look up the type-name. */
12912 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12914 if (TREE_CODE (type_decl) != TYPE_DECL
12915 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12917 /* See if this is an Objective-C type. */
12918 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12919 tree type = objc_get_protocol_qualified_type (identifier, protos);
12921 type_decl = TYPE_NAME (type);
12924 /* Issue an error if we did not find a type-name. */
12925 if (TREE_CODE (type_decl) != TYPE_DECL
12926 /* In Objective-C, we have the complication that class names are
12927 normally type names and start declarations (eg, the
12928 "NSObject" in "NSObject *object;"), but can be used in an
12929 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12930 is an expression. So, a classname followed by a dot is not a
12931 valid type-name. */
12932 || (objc_is_class_name (TREE_TYPE (type_decl))
12933 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12935 if (!cp_parser_simulate_error (parser))
12936 cp_parser_name_lookup_error (parser, identifier, type_decl,
12937 NLE_TYPE, token->location);
12938 return error_mark_node;
12940 /* Remember that the name was used in the definition of the
12941 current class so that we can check later to see if the
12942 meaning would have been different after the class was
12943 entirely defined. */
12944 else if (type_decl != error_mark_node
12946 maybe_note_name_used_in_class (identifier, type_decl);
12951 /* Parse an elaborated-type-specifier. Note that the grammar given
12952 here incorporates the resolution to DR68.
12954 elaborated-type-specifier:
12955 class-key :: [opt] nested-name-specifier [opt] identifier
12956 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12957 enum-key :: [opt] nested-name-specifier [opt] identifier
12958 typename :: [opt] nested-name-specifier identifier
12959 typename :: [opt] nested-name-specifier template [opt]
12964 elaborated-type-specifier:
12965 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12966 class-key attributes :: [opt] nested-name-specifier [opt]
12967 template [opt] template-id
12968 enum attributes :: [opt] nested-name-specifier [opt] identifier
12970 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12971 declared `friend'. If IS_DECLARATION is TRUE, then this
12972 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12973 something is being declared.
12975 Returns the TYPE specified. */
12978 cp_parser_elaborated_type_specifier (cp_parser* parser,
12980 bool is_declaration)
12982 enum tag_types tag_type;
12984 tree type = NULL_TREE;
12985 tree attributes = NULL_TREE;
12987 cp_token *token = NULL;
12989 /* See if we're looking at the `enum' keyword. */
12990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12992 /* Consume the `enum' token. */
12993 cp_lexer_consume_token (parser->lexer);
12994 /* Remember that it's an enumeration type. */
12995 tag_type = enum_type;
12996 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12997 enums) is used here. */
12998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12999 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13001 pedwarn (input_location, 0, "elaborated-type-specifier "
13002 "for a scoped enum must not use the %<%D%> keyword",
13003 cp_lexer_peek_token (parser->lexer)->u.value);
13004 /* Consume the `struct' or `class' and parse it anyway. */
13005 cp_lexer_consume_token (parser->lexer);
13007 /* Parse the attributes. */
13008 attributes = cp_parser_attributes_opt (parser);
13010 /* Or, it might be `typename'. */
13011 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13014 /* Consume the `typename' token. */
13015 cp_lexer_consume_token (parser->lexer);
13016 /* Remember that it's a `typename' type. */
13017 tag_type = typename_type;
13019 /* Otherwise it must be a class-key. */
13022 tag_type = cp_parser_class_key (parser);
13023 if (tag_type == none_type)
13024 return error_mark_node;
13025 /* Parse the attributes. */
13026 attributes = cp_parser_attributes_opt (parser);
13029 /* Look for the `::' operator. */
13030 globalscope = cp_parser_global_scope_opt (parser,
13031 /*current_scope_valid_p=*/false);
13032 /* Look for the nested-name-specifier. */
13033 if (tag_type == typename_type && !globalscope)
13035 if (!cp_parser_nested_name_specifier (parser,
13036 /*typename_keyword_p=*/true,
13037 /*check_dependency_p=*/true,
13040 return error_mark_node;
13043 /* Even though `typename' is not present, the proposed resolution
13044 to Core Issue 180 says that in `class A<T>::B', `B' should be
13045 considered a type-name, even if `A<T>' is dependent. */
13046 cp_parser_nested_name_specifier_opt (parser,
13047 /*typename_keyword_p=*/true,
13048 /*check_dependency_p=*/true,
13051 /* For everything but enumeration types, consider a template-id.
13052 For an enumeration type, consider only a plain identifier. */
13053 if (tag_type != enum_type)
13055 bool template_p = false;
13058 /* Allow the `template' keyword. */
13059 template_p = cp_parser_optional_template_keyword (parser);
13060 /* If we didn't see `template', we don't know if there's a
13061 template-id or not. */
13063 cp_parser_parse_tentatively (parser);
13064 /* Parse the template-id. */
13065 token = cp_lexer_peek_token (parser->lexer);
13066 decl = cp_parser_template_id (parser, template_p,
13067 /*check_dependency_p=*/true,
13069 /* If we didn't find a template-id, look for an ordinary
13071 if (!template_p && !cp_parser_parse_definitely (parser))
13073 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13074 in effect, then we must assume that, upon instantiation, the
13075 template will correspond to a class. */
13076 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13077 && tag_type == typename_type)
13078 type = make_typename_type (parser->scope, decl,
13080 /*complain=*/tf_error);
13081 /* If the `typename' keyword is in effect and DECL is not a type
13082 decl. Then type is non existant. */
13083 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13086 type = TREE_TYPE (decl);
13091 token = cp_lexer_peek_token (parser->lexer);
13092 identifier = cp_parser_identifier (parser);
13094 if (identifier == error_mark_node)
13096 parser->scope = NULL_TREE;
13097 return error_mark_node;
13100 /* For a `typename', we needn't call xref_tag. */
13101 if (tag_type == typename_type
13102 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13103 return cp_parser_make_typename_type (parser, parser->scope,
13106 /* Look up a qualified name in the usual way. */
13110 tree ambiguous_decls;
13112 decl = cp_parser_lookup_name (parser, identifier,
13114 /*is_template=*/false,
13115 /*is_namespace=*/false,
13116 /*check_dependency=*/true,
13120 /* If the lookup was ambiguous, an error will already have been
13122 if (ambiguous_decls)
13123 return error_mark_node;
13125 /* If we are parsing friend declaration, DECL may be a
13126 TEMPLATE_DECL tree node here. However, we need to check
13127 whether this TEMPLATE_DECL results in valid code. Consider
13128 the following example:
13131 template <class T> class C {};
13134 template <class T> friend class N::C; // #1, valid code
13136 template <class T> class Y {
13137 friend class N::C; // #2, invalid code
13140 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13141 name lookup of `N::C'. We see that friend declaration must
13142 be template for the code to be valid. Note that
13143 processing_template_decl does not work here since it is
13144 always 1 for the above two cases. */
13146 decl = (cp_parser_maybe_treat_template_as_class
13147 (decl, /*tag_name_p=*/is_friend
13148 && parser->num_template_parameter_lists));
13150 if (TREE_CODE (decl) != TYPE_DECL)
13152 cp_parser_diagnose_invalid_type_name (parser,
13156 return error_mark_node;
13159 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13161 bool allow_template = (parser->num_template_parameter_lists
13162 || DECL_SELF_REFERENCE_P (decl));
13163 type = check_elaborated_type_specifier (tag_type, decl,
13166 if (type == error_mark_node)
13167 return error_mark_node;
13170 /* Forward declarations of nested types, such as
13175 are invalid unless all components preceding the final '::'
13176 are complete. If all enclosing types are complete, these
13177 declarations become merely pointless.
13179 Invalid forward declarations of nested types are errors
13180 caught elsewhere in parsing. Those that are pointless arrive
13183 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13184 && !is_friend && !processing_explicit_instantiation)
13185 warning (0, "declaration %qD does not declare anything", decl);
13187 type = TREE_TYPE (decl);
13191 /* An elaborated-type-specifier sometimes introduces a new type and
13192 sometimes names an existing type. Normally, the rule is that it
13193 introduces a new type only if there is not an existing type of
13194 the same name already in scope. For example, given:
13197 void f() { struct S s; }
13199 the `struct S' in the body of `f' is the same `struct S' as in
13200 the global scope; the existing definition is used. However, if
13201 there were no global declaration, this would introduce a new
13202 local class named `S'.
13204 An exception to this rule applies to the following code:
13206 namespace N { struct S; }
13208 Here, the elaborated-type-specifier names a new type
13209 unconditionally; even if there is already an `S' in the
13210 containing scope this declaration names a new type.
13211 This exception only applies if the elaborated-type-specifier
13212 forms the complete declaration:
13216 A declaration consisting solely of `class-key identifier ;' is
13217 either a redeclaration of the name in the current scope or a
13218 forward declaration of the identifier as a class name. It
13219 introduces the name into the current scope.
13221 We are in this situation precisely when the next token is a `;'.
13223 An exception to the exception is that a `friend' declaration does
13224 *not* name a new type; i.e., given:
13226 struct S { friend struct T; };
13228 `T' is not a new type in the scope of `S'.
13230 Also, `new struct S' or `sizeof (struct S)' never results in the
13231 definition of a new type; a new type can only be declared in a
13232 declaration context. */
13238 /* Friends have special name lookup rules. */
13239 ts = ts_within_enclosing_non_class;
13240 else if (is_declaration
13241 && cp_lexer_next_token_is (parser->lexer,
13243 /* This is a `class-key identifier ;' */
13249 (parser->num_template_parameter_lists
13250 && (cp_parser_next_token_starts_class_definition_p (parser)
13251 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13252 /* An unqualified name was used to reference this type, so
13253 there were no qualifying templates. */
13254 if (!cp_parser_check_template_parameters (parser,
13255 /*num_templates=*/0,
13257 /*declarator=*/NULL))
13258 return error_mark_node;
13259 type = xref_tag (tag_type, identifier, ts, template_p);
13263 if (type == error_mark_node)
13264 return error_mark_node;
13266 /* Allow attributes on forward declarations of classes. */
13269 if (TREE_CODE (type) == TYPENAME_TYPE)
13270 warning (OPT_Wattributes,
13271 "attributes ignored on uninstantiated type");
13272 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13273 && ! processing_explicit_instantiation)
13274 warning (OPT_Wattributes,
13275 "attributes ignored on template instantiation");
13276 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13277 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13279 warning (OPT_Wattributes,
13280 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13283 if (tag_type != enum_type)
13284 cp_parser_check_class_key (tag_type, type);
13286 /* A "<" cannot follow an elaborated type specifier. If that
13287 happens, the user was probably trying to form a template-id. */
13288 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13293 /* Parse an enum-specifier.
13296 enum-head { enumerator-list [opt] }
13299 enum-key identifier [opt] enum-base [opt]
13300 enum-key nested-name-specifier identifier enum-base [opt]
13305 enum struct [C++0x]
13308 : type-specifier-seq
13310 opaque-enum-specifier:
13311 enum-key identifier enum-base [opt] ;
13314 enum-key attributes[opt] identifier [opt] enum-base [opt]
13315 { enumerator-list [opt] }attributes[opt]
13317 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13318 if the token stream isn't an enum-specifier after all. */
13321 cp_parser_enum_specifier (cp_parser* parser)
13324 tree type = NULL_TREE;
13326 tree nested_name_specifier = NULL_TREE;
13328 bool scoped_enum_p = false;
13329 bool has_underlying_type = false;
13330 bool nested_being_defined = false;
13331 bool new_value_list = false;
13332 bool is_new_type = false;
13333 bool is_anonymous = false;
13334 tree underlying_type = NULL_TREE;
13335 cp_token *type_start_token = NULL;
13337 /* Parse tentatively so that we can back up if we don't find a
13339 cp_parser_parse_tentatively (parser);
13341 /* Caller guarantees that the current token is 'enum', an identifier
13342 possibly follows, and the token after that is an opening brace.
13343 If we don't have an identifier, fabricate an anonymous name for
13344 the enumeration being defined. */
13345 cp_lexer_consume_token (parser->lexer);
13347 /* Parse the "class" or "struct", which indicates a scoped
13348 enumeration type in C++0x. */
13349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13350 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13352 if (cxx_dialect < cxx0x)
13353 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13355 /* Consume the `struct' or `class' token. */
13356 cp_lexer_consume_token (parser->lexer);
13358 scoped_enum_p = true;
13361 attributes = cp_parser_attributes_opt (parser);
13363 /* Clear the qualification. */
13364 parser->scope = NULL_TREE;
13365 parser->qualifying_scope = NULL_TREE;
13366 parser->object_scope = NULL_TREE;
13368 /* Figure out in what scope the declaration is being placed. */
13369 prev_scope = current_scope ();
13371 type_start_token = cp_lexer_peek_token (parser->lexer);
13373 push_deferring_access_checks (dk_no_check);
13374 nested_name_specifier
13375 = cp_parser_nested_name_specifier_opt (parser,
13376 /*typename_keyword_p=*/true,
13377 /*check_dependency_p=*/false,
13379 /*is_declaration=*/false);
13381 if (nested_name_specifier)
13385 identifier = cp_parser_identifier (parser);
13386 name = cp_parser_lookup_name (parser, identifier,
13388 /*is_template=*/false,
13389 /*is_namespace=*/false,
13390 /*check_dependency=*/true,
13391 /*ambiguous_decls=*/NULL,
13395 type = TREE_TYPE (name);
13396 if (TREE_CODE (type) == TYPENAME_TYPE)
13398 /* Are template enums allowed in ISO? */
13399 if (template_parm_scope_p ())
13400 pedwarn (type_start_token->location, OPT_pedantic,
13401 "%qD is an enumeration template", name);
13402 /* ignore a typename reference, for it will be solved by name
13408 error_at (type_start_token->location,
13409 "%qD is not an enumerator-name", identifier);
13413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13414 identifier = cp_parser_identifier (parser);
13417 identifier = make_anon_name ();
13418 is_anonymous = true;
13421 pop_deferring_access_checks ();
13423 /* Check for the `:' that denotes a specified underlying type in C++0x.
13424 Note that a ':' could also indicate a bitfield width, however. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13427 cp_decl_specifier_seq type_specifiers;
13429 /* Consume the `:'. */
13430 cp_lexer_consume_token (parser->lexer);
13432 /* Parse the type-specifier-seq. */
13433 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13434 /*is_trailing_return=*/false,
13437 /* At this point this is surely not elaborated type specifier. */
13438 if (!cp_parser_parse_definitely (parser))
13441 if (cxx_dialect < cxx0x)
13442 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13444 has_underlying_type = true;
13446 /* If that didn't work, stop. */
13447 if (type_specifiers.type != error_mark_node)
13449 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13450 /*initialized=*/0, NULL);
13451 if (underlying_type == error_mark_node)
13452 underlying_type = NULL_TREE;
13456 /* Look for the `{' but don't consume it yet. */
13457 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13459 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13461 cp_parser_error (parser, "expected %<{%>");
13462 if (has_underlying_type)
13465 /* An opaque-enum-specifier must have a ';' here. */
13466 if ((scoped_enum_p || underlying_type)
13467 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13469 cp_parser_error (parser, "expected %<;%> or %<{%>");
13470 if (has_underlying_type)
13475 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13478 if (nested_name_specifier)
13480 if (CLASS_TYPE_P (nested_name_specifier))
13482 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13483 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13484 push_scope (nested_name_specifier);
13486 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13488 push_nested_namespace (nested_name_specifier);
13492 /* Issue an error message if type-definitions are forbidden here. */
13493 if (!cp_parser_check_type_definition (parser))
13494 type = error_mark_node;
13496 /* Create the new type. We do this before consuming the opening
13497 brace so the enum will be recorded as being on the line of its
13498 tag (or the 'enum' keyword, if there is no tag). */
13499 type = start_enum (identifier, type, underlying_type,
13500 scoped_enum_p, &is_new_type);
13502 /* If the next token is not '{' it is an opaque-enum-specifier or an
13503 elaborated-type-specifier. */
13504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13506 if (nested_name_specifier)
13508 /* The following catches invalid code such as:
13509 enum class S<int>::E { A, B, C }; */
13510 if (!processing_specialization
13511 && CLASS_TYPE_P (nested_name_specifier)
13512 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13513 error_at (type_start_token->location, "cannot add an enumerator "
13514 "list to a template instantiation");
13516 /* If that scope does not contain the scope in which the
13517 class was originally declared, the program is invalid. */
13518 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13520 if (at_namespace_scope_p ())
13521 error_at (type_start_token->location,
13522 "declaration of %qD in namespace %qD which does not "
13524 type, prev_scope, nested_name_specifier);
13526 error_at (type_start_token->location,
13527 "declaration of %qD in %qD which does not enclose %qD",
13528 type, prev_scope, nested_name_specifier);
13529 type = error_mark_node;
13534 begin_scope (sk_scoped_enum, type);
13536 /* Consume the opening brace. */
13537 cp_lexer_consume_token (parser->lexer);
13539 if (type == error_mark_node)
13540 ; /* Nothing to add */
13541 else if (OPAQUE_ENUM_P (type)
13542 || (cxx_dialect > cxx98 && processing_specialization))
13544 new_value_list = true;
13545 SET_OPAQUE_ENUM_P (type, false);
13546 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13550 error_at (type_start_token->location, "multiple definition of %q#T", type);
13551 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13552 "previous definition here");
13553 type = error_mark_node;
13556 if (type == error_mark_node)
13557 cp_parser_skip_to_end_of_block_or_statement (parser);
13558 /* If the next token is not '}', then there are some enumerators. */
13559 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13560 cp_parser_enumerator_list (parser, type);
13562 /* Consume the final '}'. */
13563 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13570 /* If a ';' follows, then it is an opaque-enum-specifier
13571 and additional restrictions apply. */
13572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13575 error_at (type_start_token->location,
13576 "opaque-enum-specifier without name");
13577 else if (nested_name_specifier)
13578 error_at (type_start_token->location,
13579 "opaque-enum-specifier must use a simple identifier");
13583 /* Look for trailing attributes to apply to this enumeration, and
13584 apply them if appropriate. */
13585 if (cp_parser_allow_gnu_extensions_p (parser))
13587 tree trailing_attr = cp_parser_attributes_opt (parser);
13588 trailing_attr = chainon (trailing_attr, attributes);
13589 cplus_decl_attributes (&type,
13591 (int) ATTR_FLAG_TYPE_IN_PLACE);
13594 /* Finish up the enumeration. */
13595 if (type != error_mark_node)
13597 if (new_value_list)
13598 finish_enum_value_list (type);
13600 finish_enum (type);
13603 if (nested_name_specifier)
13605 if (CLASS_TYPE_P (nested_name_specifier))
13607 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13608 pop_scope (nested_name_specifier);
13610 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13612 pop_nested_namespace (nested_name_specifier);
13618 /* Parse an enumerator-list. The enumerators all have the indicated
13622 enumerator-definition
13623 enumerator-list , enumerator-definition */
13626 cp_parser_enumerator_list (cp_parser* parser, tree type)
13630 /* Parse an enumerator-definition. */
13631 cp_parser_enumerator_definition (parser, type);
13633 /* If the next token is not a ',', we've reached the end of
13635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13637 /* Otherwise, consume the `,' and keep going. */
13638 cp_lexer_consume_token (parser->lexer);
13639 /* If the next token is a `}', there is a trailing comma. */
13640 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13642 if (!in_system_header)
13643 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13649 /* Parse an enumerator-definition. The enumerator has the indicated
13652 enumerator-definition:
13654 enumerator = constant-expression
13660 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13666 /* Save the input location because we are interested in the location
13667 of the identifier and not the location of the explicit value. */
13668 loc = cp_lexer_peek_token (parser->lexer)->location;
13670 /* Look for the identifier. */
13671 identifier = cp_parser_identifier (parser);
13672 if (identifier == error_mark_node)
13675 /* If the next token is an '=', then there is an explicit value. */
13676 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13678 /* Consume the `=' token. */
13679 cp_lexer_consume_token (parser->lexer);
13680 /* Parse the value. */
13681 value = cp_parser_constant_expression (parser,
13682 /*allow_non_constant_p=*/false,
13688 /* If we are processing a template, make sure the initializer of the
13689 enumerator doesn't contain any bare template parameter pack. */
13690 if (check_for_bare_parameter_packs (value))
13691 value = error_mark_node;
13693 /* Create the enumerator. */
13694 build_enumerator (identifier, value, type, loc);
13697 /* Parse a namespace-name.
13700 original-namespace-name
13703 Returns the NAMESPACE_DECL for the namespace. */
13706 cp_parser_namespace_name (cp_parser* parser)
13709 tree namespace_decl;
13711 cp_token *token = cp_lexer_peek_token (parser->lexer);
13713 /* Get the name of the namespace. */
13714 identifier = cp_parser_identifier (parser);
13715 if (identifier == error_mark_node)
13716 return error_mark_node;
13718 /* Look up the identifier in the currently active scope. Look only
13719 for namespaces, due to:
13721 [basic.lookup.udir]
13723 When looking up a namespace-name in a using-directive or alias
13724 definition, only namespace names are considered.
13728 [basic.lookup.qual]
13730 During the lookup of a name preceding the :: scope resolution
13731 operator, object, function, and enumerator names are ignored.
13733 (Note that cp_parser_qualifying_entity only calls this
13734 function if the token after the name is the scope resolution
13736 namespace_decl = cp_parser_lookup_name (parser, identifier,
13738 /*is_template=*/false,
13739 /*is_namespace=*/true,
13740 /*check_dependency=*/true,
13741 /*ambiguous_decls=*/NULL,
13743 /* If it's not a namespace, issue an error. */
13744 if (namespace_decl == error_mark_node
13745 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13747 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13748 error_at (token->location, "%qD is not a namespace-name", identifier);
13749 cp_parser_error (parser, "expected namespace-name");
13750 namespace_decl = error_mark_node;
13753 return namespace_decl;
13756 /* Parse a namespace-definition.
13758 namespace-definition:
13759 named-namespace-definition
13760 unnamed-namespace-definition
13762 named-namespace-definition:
13763 original-namespace-definition
13764 extension-namespace-definition
13766 original-namespace-definition:
13767 namespace identifier { namespace-body }
13769 extension-namespace-definition:
13770 namespace original-namespace-name { namespace-body }
13772 unnamed-namespace-definition:
13773 namespace { namespace-body } */
13776 cp_parser_namespace_definition (cp_parser* parser)
13778 tree identifier, attribs;
13779 bool has_visibility;
13782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13784 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13786 cp_lexer_consume_token (parser->lexer);
13791 /* Look for the `namespace' keyword. */
13792 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13794 /* Get the name of the namespace. We do not attempt to distinguish
13795 between an original-namespace-definition and an
13796 extension-namespace-definition at this point. The semantic
13797 analysis routines are responsible for that. */
13798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13799 identifier = cp_parser_identifier (parser);
13801 identifier = NULL_TREE;
13803 /* Parse any specified attributes. */
13804 attribs = cp_parser_attributes_opt (parser);
13806 /* Look for the `{' to start the namespace. */
13807 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13808 /* Start the namespace. */
13809 push_namespace (identifier);
13811 /* "inline namespace" is equivalent to a stub namespace definition
13812 followed by a strong using directive. */
13815 tree name_space = current_namespace;
13816 /* Set up namespace association. */
13817 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13818 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13819 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13820 /* Import the contents of the inline namespace. */
13822 do_using_directive (name_space);
13823 push_namespace (identifier);
13826 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13828 /* Parse the body of the namespace. */
13829 cp_parser_namespace_body (parser);
13831 #ifdef HANDLE_PRAGMA_VISIBILITY
13832 if (has_visibility)
13833 pop_visibility (1);
13836 /* Finish the namespace. */
13838 /* Look for the final `}'. */
13839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13842 /* Parse a namespace-body.
13845 declaration-seq [opt] */
13848 cp_parser_namespace_body (cp_parser* parser)
13850 cp_parser_declaration_seq_opt (parser);
13853 /* Parse a namespace-alias-definition.
13855 namespace-alias-definition:
13856 namespace identifier = qualified-namespace-specifier ; */
13859 cp_parser_namespace_alias_definition (cp_parser* parser)
13862 tree namespace_specifier;
13864 cp_token *token = cp_lexer_peek_token (parser->lexer);
13866 /* Look for the `namespace' keyword. */
13867 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13868 /* Look for the identifier. */
13869 identifier = cp_parser_identifier (parser);
13870 if (identifier == error_mark_node)
13872 /* Look for the `=' token. */
13873 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13874 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13876 error_at (token->location, "%<namespace%> definition is not allowed here");
13877 /* Skip the definition. */
13878 cp_lexer_consume_token (parser->lexer);
13879 if (cp_parser_skip_to_closing_brace (parser))
13880 cp_lexer_consume_token (parser->lexer);
13883 cp_parser_require (parser, CPP_EQ, RT_EQ);
13884 /* Look for the qualified-namespace-specifier. */
13885 namespace_specifier
13886 = cp_parser_qualified_namespace_specifier (parser);
13887 /* Look for the `;' token. */
13888 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13890 /* Register the alias in the symbol table. */
13891 do_namespace_alias (identifier, namespace_specifier);
13894 /* Parse a qualified-namespace-specifier.
13896 qualified-namespace-specifier:
13897 :: [opt] nested-name-specifier [opt] namespace-name
13899 Returns a NAMESPACE_DECL corresponding to the specified
13903 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13905 /* Look for the optional `::'. */
13906 cp_parser_global_scope_opt (parser,
13907 /*current_scope_valid_p=*/false);
13909 /* Look for the optional nested-name-specifier. */
13910 cp_parser_nested_name_specifier_opt (parser,
13911 /*typename_keyword_p=*/false,
13912 /*check_dependency_p=*/true,
13914 /*is_declaration=*/true);
13916 return cp_parser_namespace_name (parser);
13919 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13920 access declaration.
13923 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13924 using :: unqualified-id ;
13926 access-declaration:
13932 cp_parser_using_declaration (cp_parser* parser,
13933 bool access_declaration_p)
13936 bool typename_p = false;
13937 bool global_scope_p;
13942 if (access_declaration_p)
13943 cp_parser_parse_tentatively (parser);
13946 /* Look for the `using' keyword. */
13947 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13949 /* Peek at the next token. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* See if it's `typename'. */
13952 if (token->keyword == RID_TYPENAME)
13954 /* Remember that we've seen it. */
13956 /* Consume the `typename' token. */
13957 cp_lexer_consume_token (parser->lexer);
13961 /* Look for the optional global scope qualification. */
13963 = (cp_parser_global_scope_opt (parser,
13964 /*current_scope_valid_p=*/false)
13967 /* If we saw `typename', or didn't see `::', then there must be a
13968 nested-name-specifier present. */
13969 if (typename_p || !global_scope_p)
13970 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13971 /*check_dependency_p=*/true,
13973 /*is_declaration=*/true);
13974 /* Otherwise, we could be in either of the two productions. In that
13975 case, treat the nested-name-specifier as optional. */
13977 qscope = cp_parser_nested_name_specifier_opt (parser,
13978 /*typename_keyword_p=*/false,
13979 /*check_dependency_p=*/true,
13981 /*is_declaration=*/true);
13983 qscope = global_namespace;
13985 if (access_declaration_p && cp_parser_error_occurred (parser))
13986 /* Something has already gone wrong; there's no need to parse
13987 further. Since an error has occurred, the return value of
13988 cp_parser_parse_definitely will be false, as required. */
13989 return cp_parser_parse_definitely (parser);
13991 token = cp_lexer_peek_token (parser->lexer);
13992 /* Parse the unqualified-id. */
13993 identifier = cp_parser_unqualified_id (parser,
13994 /*template_keyword_p=*/false,
13995 /*check_dependency_p=*/true,
13996 /*declarator_p=*/true,
13997 /*optional_p=*/false);
13999 if (access_declaration_p)
14001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14002 cp_parser_simulate_error (parser);
14003 if (!cp_parser_parse_definitely (parser))
14007 /* The function we call to handle a using-declaration is different
14008 depending on what scope we are in. */
14009 if (qscope == error_mark_node || identifier == error_mark_node)
14011 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14012 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14013 /* [namespace.udecl]
14015 A using declaration shall not name a template-id. */
14016 error_at (token->location,
14017 "a template-id may not appear in a using-declaration");
14020 if (at_class_scope_p ())
14022 /* Create the USING_DECL. */
14023 decl = do_class_using_decl (parser->scope, identifier);
14025 if (check_for_bare_parameter_packs (decl))
14028 /* Add it to the list of members in this class. */
14029 finish_member_declaration (decl);
14033 decl = cp_parser_lookup_name_simple (parser,
14036 if (decl == error_mark_node)
14037 cp_parser_name_lookup_error (parser, identifier,
14040 else if (check_for_bare_parameter_packs (decl))
14042 else if (!at_namespace_scope_p ())
14043 do_local_using_decl (decl, qscope, identifier);
14045 do_toplevel_using_decl (decl, qscope, identifier);
14049 /* Look for the final `;'. */
14050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14055 /* Parse a using-directive.
14058 using namespace :: [opt] nested-name-specifier [opt]
14059 namespace-name ; */
14062 cp_parser_using_directive (cp_parser* parser)
14064 tree namespace_decl;
14067 /* Look for the `using' keyword. */
14068 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14069 /* And the `namespace' keyword. */
14070 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14071 /* Look for the optional `::' operator. */
14072 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14073 /* And the optional nested-name-specifier. */
14074 cp_parser_nested_name_specifier_opt (parser,
14075 /*typename_keyword_p=*/false,
14076 /*check_dependency_p=*/true,
14078 /*is_declaration=*/true);
14079 /* Get the namespace being used. */
14080 namespace_decl = cp_parser_namespace_name (parser);
14081 /* And any specified attributes. */
14082 attribs = cp_parser_attributes_opt (parser);
14083 /* Update the symbol table. */
14084 parse_using_directive (namespace_decl, attribs);
14085 /* Look for the final `;'. */
14086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14089 /* Parse an asm-definition.
14092 asm ( string-literal ) ;
14097 asm volatile [opt] ( string-literal ) ;
14098 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14099 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14100 : asm-operand-list [opt] ) ;
14101 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14102 : asm-operand-list [opt]
14103 : asm-clobber-list [opt] ) ;
14104 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14105 : asm-clobber-list [opt]
14106 : asm-goto-list ) ; */
14109 cp_parser_asm_definition (cp_parser* parser)
14112 tree outputs = NULL_TREE;
14113 tree inputs = NULL_TREE;
14114 tree clobbers = NULL_TREE;
14115 tree labels = NULL_TREE;
14117 bool volatile_p = false;
14118 bool extended_p = false;
14119 bool invalid_inputs_p = false;
14120 bool invalid_outputs_p = false;
14121 bool goto_p = false;
14122 required_token missing = RT_NONE;
14124 /* Look for the `asm' keyword. */
14125 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14126 /* See if the next token is `volatile'. */
14127 if (cp_parser_allow_gnu_extensions_p (parser)
14128 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14130 /* Remember that we saw the `volatile' keyword. */
14132 /* Consume the token. */
14133 cp_lexer_consume_token (parser->lexer);
14135 if (cp_parser_allow_gnu_extensions_p (parser)
14136 && parser->in_function_body
14137 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14139 /* Remember that we saw the `goto' keyword. */
14141 /* Consume the token. */
14142 cp_lexer_consume_token (parser->lexer);
14144 /* Look for the opening `('. */
14145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14147 /* Look for the string. */
14148 string = cp_parser_string_literal (parser, false, false);
14149 if (string == error_mark_node)
14151 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14152 /*consume_paren=*/true);
14156 /* If we're allowing GNU extensions, check for the extended assembly
14157 syntax. Unfortunately, the `:' tokens need not be separated by
14158 a space in C, and so, for compatibility, we tolerate that here
14159 too. Doing that means that we have to treat the `::' operator as
14161 if (cp_parser_allow_gnu_extensions_p (parser)
14162 && parser->in_function_body
14163 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14164 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14166 bool inputs_p = false;
14167 bool clobbers_p = false;
14168 bool labels_p = false;
14170 /* The extended syntax was used. */
14173 /* Look for outputs. */
14174 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14176 /* Consume the `:'. */
14177 cp_lexer_consume_token (parser->lexer);
14178 /* Parse the output-operands. */
14179 if (cp_lexer_next_token_is_not (parser->lexer,
14181 && cp_lexer_next_token_is_not (parser->lexer,
14183 && cp_lexer_next_token_is_not (parser->lexer,
14186 outputs = cp_parser_asm_operand_list (parser);
14188 if (outputs == error_mark_node)
14189 invalid_outputs_p = true;
14191 /* If the next token is `::', there are no outputs, and the
14192 next token is the beginning of the inputs. */
14193 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14194 /* The inputs are coming next. */
14197 /* Look for inputs. */
14199 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14201 /* Consume the `:' or `::'. */
14202 cp_lexer_consume_token (parser->lexer);
14203 /* Parse the output-operands. */
14204 if (cp_lexer_next_token_is_not (parser->lexer,
14206 && cp_lexer_next_token_is_not (parser->lexer,
14208 && cp_lexer_next_token_is_not (parser->lexer,
14210 inputs = cp_parser_asm_operand_list (parser);
14212 if (inputs == error_mark_node)
14213 invalid_inputs_p = true;
14215 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14216 /* The clobbers are coming next. */
14219 /* Look for clobbers. */
14221 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14224 /* Consume the `:' or `::'. */
14225 cp_lexer_consume_token (parser->lexer);
14226 /* Parse the clobbers. */
14227 if (cp_lexer_next_token_is_not (parser->lexer,
14229 && cp_lexer_next_token_is_not (parser->lexer,
14231 clobbers = cp_parser_asm_clobber_list (parser);
14234 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14235 /* The labels are coming next. */
14238 /* Look for labels. */
14240 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14243 /* Consume the `:' or `::'. */
14244 cp_lexer_consume_token (parser->lexer);
14245 /* Parse the labels. */
14246 labels = cp_parser_asm_label_list (parser);
14249 if (goto_p && !labels_p)
14250 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14253 missing = RT_COLON_SCOPE;
14255 /* Look for the closing `)'. */
14256 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14257 missing ? missing : RT_CLOSE_PAREN))
14258 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14259 /*consume_paren=*/true);
14260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14262 if (!invalid_inputs_p && !invalid_outputs_p)
14264 /* Create the ASM_EXPR. */
14265 if (parser->in_function_body)
14267 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14268 inputs, clobbers, labels);
14269 /* If the extended syntax was not used, mark the ASM_EXPR. */
14272 tree temp = asm_stmt;
14273 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14274 temp = TREE_OPERAND (temp, 0);
14276 ASM_INPUT_P (temp) = 1;
14280 cgraph_add_asm_node (string);
14284 /* Declarators [gram.dcl.decl] */
14286 /* Parse an init-declarator.
14289 declarator initializer [opt]
14294 declarator asm-specification [opt] attributes [opt] initializer [opt]
14296 function-definition:
14297 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14299 decl-specifier-seq [opt] declarator function-try-block
14303 function-definition:
14304 __extension__ function-definition
14306 The DECL_SPECIFIERS apply to this declarator. Returns a
14307 representation of the entity declared. If MEMBER_P is TRUE, then
14308 this declarator appears in a class scope. The new DECL created by
14309 this declarator is returned.
14311 The CHECKS are access checks that should be performed once we know
14312 what entity is being declared (and, therefore, what classes have
14315 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14316 for a function-definition here as well. If the declarator is a
14317 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14318 be TRUE upon return. By that point, the function-definition will
14319 have been completely parsed.
14321 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14325 cp_parser_init_declarator (cp_parser* parser,
14326 cp_decl_specifier_seq *decl_specifiers,
14327 VEC (deferred_access_check,gc)* checks,
14328 bool function_definition_allowed_p,
14330 int declares_class_or_enum,
14331 bool* function_definition_p)
14333 cp_token *token = NULL, *asm_spec_start_token = NULL,
14334 *attributes_start_token = NULL;
14335 cp_declarator *declarator;
14336 tree prefix_attributes;
14338 tree asm_specification;
14340 tree decl = NULL_TREE;
14342 int is_initialized;
14343 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14344 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14346 enum cpp_ttype initialization_kind;
14347 bool is_direct_init = false;
14348 bool is_non_constant_init;
14349 int ctor_dtor_or_conv_p;
14351 tree pushed_scope = NULL;
14353 /* Gather the attributes that were provided with the
14354 decl-specifiers. */
14355 prefix_attributes = decl_specifiers->attributes;
14357 /* Assume that this is not the declarator for a function
14359 if (function_definition_p)
14360 *function_definition_p = false;
14362 /* Defer access checks while parsing the declarator; we cannot know
14363 what names are accessible until we know what is being
14365 resume_deferring_access_checks ();
14367 /* Parse the declarator. */
14368 token = cp_lexer_peek_token (parser->lexer);
14370 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14371 &ctor_dtor_or_conv_p,
14372 /*parenthesized_p=*/NULL,
14373 /*member_p=*/false);
14374 /* Gather up the deferred checks. */
14375 stop_deferring_access_checks ();
14377 /* If the DECLARATOR was erroneous, there's no need to go
14379 if (declarator == cp_error_declarator)
14380 return error_mark_node;
14382 /* Check that the number of template-parameter-lists is OK. */
14383 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14385 return error_mark_node;
14387 if (declares_class_or_enum & 2)
14388 cp_parser_check_for_definition_in_return_type (declarator,
14389 decl_specifiers->type,
14390 decl_specifiers->type_location);
14392 /* Figure out what scope the entity declared by the DECLARATOR is
14393 located in. `grokdeclarator' sometimes changes the scope, so
14394 we compute it now. */
14395 scope = get_scope_of_declarator (declarator);
14397 /* Perform any lookups in the declared type which were thought to be
14398 dependent, but are not in the scope of the declarator. */
14399 decl_specifiers->type
14400 = maybe_update_decl_type (decl_specifiers->type, scope);
14402 /* If we're allowing GNU extensions, look for an asm-specification
14404 if (cp_parser_allow_gnu_extensions_p (parser))
14406 /* Look for an asm-specification. */
14407 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14408 asm_specification = cp_parser_asm_specification_opt (parser);
14409 /* And attributes. */
14410 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14411 attributes = cp_parser_attributes_opt (parser);
14415 asm_specification = NULL_TREE;
14416 attributes = NULL_TREE;
14419 /* Peek at the next token. */
14420 token = cp_lexer_peek_token (parser->lexer);
14421 /* Check to see if the token indicates the start of a
14422 function-definition. */
14423 if (function_declarator_p (declarator)
14424 && cp_parser_token_starts_function_definition_p (token))
14426 if (!function_definition_allowed_p)
14428 /* If a function-definition should not appear here, issue an
14430 cp_parser_error (parser,
14431 "a function-definition is not allowed here");
14432 return error_mark_node;
14436 location_t func_brace_location
14437 = cp_lexer_peek_token (parser->lexer)->location;
14439 /* Neither attributes nor an asm-specification are allowed
14440 on a function-definition. */
14441 if (asm_specification)
14442 error_at (asm_spec_start_token->location,
14443 "an asm-specification is not allowed "
14444 "on a function-definition");
14446 error_at (attributes_start_token->location,
14447 "attributes are not allowed on a function-definition");
14448 /* This is a function-definition. */
14449 *function_definition_p = true;
14451 /* Parse the function definition. */
14453 decl = cp_parser_save_member_function_body (parser,
14456 prefix_attributes);
14459 = (cp_parser_function_definition_from_specifiers_and_declarator
14460 (parser, decl_specifiers, prefix_attributes, declarator));
14462 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14464 /* This is where the prologue starts... */
14465 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14466 = func_brace_location;
14475 Only in function declarations for constructors, destructors, and
14476 type conversions can the decl-specifier-seq be omitted.
14478 We explicitly postpone this check past the point where we handle
14479 function-definitions because we tolerate function-definitions
14480 that are missing their return types in some modes. */
14481 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14483 cp_parser_error (parser,
14484 "expected constructor, destructor, or type conversion");
14485 return error_mark_node;
14488 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14489 if (token->type == CPP_EQ
14490 || token->type == CPP_OPEN_PAREN
14491 || token->type == CPP_OPEN_BRACE)
14493 is_initialized = SD_INITIALIZED;
14494 initialization_kind = token->type;
14496 if (token->type == CPP_EQ
14497 && function_declarator_p (declarator))
14499 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14500 if (t2->keyword == RID_DEFAULT)
14501 is_initialized = SD_DEFAULTED;
14502 else if (t2->keyword == RID_DELETE)
14503 is_initialized = SD_DELETED;
14508 /* If the init-declarator isn't initialized and isn't followed by a
14509 `,' or `;', it's not a valid init-declarator. */
14510 if (token->type != CPP_COMMA
14511 && token->type != CPP_SEMICOLON)
14513 cp_parser_error (parser, "expected initializer");
14514 return error_mark_node;
14516 is_initialized = SD_UNINITIALIZED;
14517 initialization_kind = CPP_EOF;
14520 /* Because start_decl has side-effects, we should only call it if we
14521 know we're going ahead. By this point, we know that we cannot
14522 possibly be looking at any other construct. */
14523 cp_parser_commit_to_tentative_parse (parser);
14525 /* If the decl specifiers were bad, issue an error now that we're
14526 sure this was intended to be a declarator. Then continue
14527 declaring the variable(s), as int, to try to cut down on further
14529 if (decl_specifiers->any_specifiers_p
14530 && decl_specifiers->type == error_mark_node)
14532 cp_parser_error (parser, "invalid type in declaration");
14533 decl_specifiers->type = integer_type_node;
14536 /* Check to see whether or not this declaration is a friend. */
14537 friend_p = cp_parser_friend_p (decl_specifiers);
14539 /* Enter the newly declared entry in the symbol table. If we're
14540 processing a declaration in a class-specifier, we wait until
14541 after processing the initializer. */
14544 if (parser->in_unbraced_linkage_specification_p)
14545 decl_specifiers->storage_class = sc_extern;
14546 decl = start_decl (declarator, decl_specifiers,
14547 is_initialized, attributes, prefix_attributes,
14549 /* Adjust location of decl if declarator->id_loc is more appropriate:
14550 set, and decl wasn't merged with another decl, in which case its
14551 location would be different from input_location, and more accurate. */
14553 && declarator->id_loc != UNKNOWN_LOCATION
14554 && DECL_SOURCE_LOCATION (decl) == input_location)
14555 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14558 /* Enter the SCOPE. That way unqualified names appearing in the
14559 initializer will be looked up in SCOPE. */
14560 pushed_scope = push_scope (scope);
14562 /* Perform deferred access control checks, now that we know in which
14563 SCOPE the declared entity resides. */
14564 if (!member_p && decl)
14566 tree saved_current_function_decl = NULL_TREE;
14568 /* If the entity being declared is a function, pretend that we
14569 are in its scope. If it is a `friend', it may have access to
14570 things that would not otherwise be accessible. */
14571 if (TREE_CODE (decl) == FUNCTION_DECL)
14573 saved_current_function_decl = current_function_decl;
14574 current_function_decl = decl;
14577 /* Perform access checks for template parameters. */
14578 cp_parser_perform_template_parameter_access_checks (checks);
14580 /* Perform the access control checks for the declarator and the
14581 decl-specifiers. */
14582 perform_deferred_access_checks ();
14584 /* Restore the saved value. */
14585 if (TREE_CODE (decl) == FUNCTION_DECL)
14586 current_function_decl = saved_current_function_decl;
14589 /* Parse the initializer. */
14590 initializer = NULL_TREE;
14591 is_direct_init = false;
14592 is_non_constant_init = true;
14593 if (is_initialized)
14595 if (function_declarator_p (declarator))
14597 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14598 if (initialization_kind == CPP_EQ)
14599 initializer = cp_parser_pure_specifier (parser);
14602 /* If the declaration was erroneous, we don't really
14603 know what the user intended, so just silently
14604 consume the initializer. */
14605 if (decl != error_mark_node)
14606 error_at (initializer_start_token->location,
14607 "initializer provided for function");
14608 cp_parser_skip_to_closing_parenthesis (parser,
14609 /*recovering=*/true,
14610 /*or_comma=*/false,
14611 /*consume_paren=*/true);
14616 /* We want to record the extra mangling scope for in-class
14617 initializers of class members and initializers of static data
14618 member templates. The former is a C++0x feature which isn't
14619 implemented yet, and I expect it will involve deferring
14620 parsing of the initializer until end of class as with default
14621 arguments. So right here we only handle the latter. */
14622 if (!member_p && processing_template_decl)
14623 start_lambda_scope (decl);
14624 initializer = cp_parser_initializer (parser,
14626 &is_non_constant_init);
14627 if (!member_p && processing_template_decl)
14628 finish_lambda_scope ();
14632 /* The old parser allows attributes to appear after a parenthesized
14633 initializer. Mark Mitchell proposed removing this functionality
14634 on the GCC mailing lists on 2002-08-13. This parser accepts the
14635 attributes -- but ignores them. */
14636 if (cp_parser_allow_gnu_extensions_p (parser)
14637 && initialization_kind == CPP_OPEN_PAREN)
14638 if (cp_parser_attributes_opt (parser))
14639 warning (OPT_Wattributes,
14640 "attributes after parenthesized initializer ignored");
14642 /* For an in-class declaration, use `grokfield' to create the
14648 pop_scope (pushed_scope);
14649 pushed_scope = false;
14651 decl = grokfield (declarator, decl_specifiers,
14652 initializer, !is_non_constant_init,
14653 /*asmspec=*/NULL_TREE,
14654 prefix_attributes);
14655 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14656 cp_parser_save_default_args (parser, decl);
14659 /* Finish processing the declaration. But, skip friend
14661 if (!friend_p && decl && decl != error_mark_node)
14663 cp_finish_decl (decl,
14664 initializer, !is_non_constant_init,
14666 /* If the initializer is in parentheses, then this is
14667 a direct-initialization, which means that an
14668 `explicit' constructor is OK. Otherwise, an
14669 `explicit' constructor cannot be used. */
14670 ((is_direct_init || !is_initialized)
14671 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14673 else if ((cxx_dialect != cxx98) && friend_p
14674 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14675 /* Core issue #226 (C++0x only): A default template-argument
14676 shall not be specified in a friend class template
14678 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14679 /*is_partial=*/0, /*is_friend_decl=*/1);
14681 if (!friend_p && pushed_scope)
14682 pop_scope (pushed_scope);
14687 /* Parse a declarator.
14691 ptr-operator declarator
14693 abstract-declarator:
14694 ptr-operator abstract-declarator [opt]
14695 direct-abstract-declarator
14700 attributes [opt] direct-declarator
14701 attributes [opt] ptr-operator declarator
14703 abstract-declarator:
14704 attributes [opt] ptr-operator abstract-declarator [opt]
14705 attributes [opt] direct-abstract-declarator
14707 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14708 detect constructor, destructor or conversion operators. It is set
14709 to -1 if the declarator is a name, and +1 if it is a
14710 function. Otherwise it is set to zero. Usually you just want to
14711 test for >0, but internally the negative value is used.
14713 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14714 a decl-specifier-seq unless it declares a constructor, destructor,
14715 or conversion. It might seem that we could check this condition in
14716 semantic analysis, rather than parsing, but that makes it difficult
14717 to handle something like `f()'. We want to notice that there are
14718 no decl-specifiers, and therefore realize that this is an
14719 expression, not a declaration.)
14721 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14722 the declarator is a direct-declarator of the form "(...)".
14724 MEMBER_P is true iff this declarator is a member-declarator. */
14726 static cp_declarator *
14727 cp_parser_declarator (cp_parser* parser,
14728 cp_parser_declarator_kind dcl_kind,
14729 int* ctor_dtor_or_conv_p,
14730 bool* parenthesized_p,
14733 cp_declarator *declarator;
14734 enum tree_code code;
14735 cp_cv_quals cv_quals;
14737 tree attributes = NULL_TREE;
14739 /* Assume this is not a constructor, destructor, or type-conversion
14741 if (ctor_dtor_or_conv_p)
14742 *ctor_dtor_or_conv_p = 0;
14744 if (cp_parser_allow_gnu_extensions_p (parser))
14745 attributes = cp_parser_attributes_opt (parser);
14747 /* Check for the ptr-operator production. */
14748 cp_parser_parse_tentatively (parser);
14749 /* Parse the ptr-operator. */
14750 code = cp_parser_ptr_operator (parser,
14753 /* If that worked, then we have a ptr-operator. */
14754 if (cp_parser_parse_definitely (parser))
14756 /* If a ptr-operator was found, then this declarator was not
14758 if (parenthesized_p)
14759 *parenthesized_p = true;
14760 /* The dependent declarator is optional if we are parsing an
14761 abstract-declarator. */
14762 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14763 cp_parser_parse_tentatively (parser);
14765 /* Parse the dependent declarator. */
14766 declarator = cp_parser_declarator (parser, dcl_kind,
14767 /*ctor_dtor_or_conv_p=*/NULL,
14768 /*parenthesized_p=*/NULL,
14769 /*member_p=*/false);
14771 /* If we are parsing an abstract-declarator, we must handle the
14772 case where the dependent declarator is absent. */
14773 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14774 && !cp_parser_parse_definitely (parser))
14777 declarator = cp_parser_make_indirect_declarator
14778 (code, class_type, cv_quals, declarator);
14780 /* Everything else is a direct-declarator. */
14783 if (parenthesized_p)
14784 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14786 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14787 ctor_dtor_or_conv_p,
14791 if (attributes && declarator && declarator != cp_error_declarator)
14792 declarator->attributes = attributes;
14797 /* Parse a direct-declarator or direct-abstract-declarator.
14801 direct-declarator ( parameter-declaration-clause )
14802 cv-qualifier-seq [opt]
14803 exception-specification [opt]
14804 direct-declarator [ constant-expression [opt] ]
14807 direct-abstract-declarator:
14808 direct-abstract-declarator [opt]
14809 ( parameter-declaration-clause )
14810 cv-qualifier-seq [opt]
14811 exception-specification [opt]
14812 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14813 ( abstract-declarator )
14815 Returns a representation of the declarator. DCL_KIND is
14816 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14817 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14818 we are parsing a direct-declarator. It is
14819 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14820 of ambiguity we prefer an abstract declarator, as per
14821 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14822 cp_parser_declarator. */
14824 static cp_declarator *
14825 cp_parser_direct_declarator (cp_parser* parser,
14826 cp_parser_declarator_kind dcl_kind,
14827 int* ctor_dtor_or_conv_p,
14831 cp_declarator *declarator = NULL;
14832 tree scope = NULL_TREE;
14833 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14834 bool saved_in_declarator_p = parser->in_declarator_p;
14836 tree pushed_scope = NULL_TREE;
14840 /* Peek at the next token. */
14841 token = cp_lexer_peek_token (parser->lexer);
14842 if (token->type == CPP_OPEN_PAREN)
14844 /* This is either a parameter-declaration-clause, or a
14845 parenthesized declarator. When we know we are parsing a
14846 named declarator, it must be a parenthesized declarator
14847 if FIRST is true. For instance, `(int)' is a
14848 parameter-declaration-clause, with an omitted
14849 direct-abstract-declarator. But `((*))', is a
14850 parenthesized abstract declarator. Finally, when T is a
14851 template parameter `(T)' is a
14852 parameter-declaration-clause, and not a parenthesized
14855 We first try and parse a parameter-declaration-clause,
14856 and then try a nested declarator (if FIRST is true).
14858 It is not an error for it not to be a
14859 parameter-declaration-clause, even when FIRST is
14865 The first is the declaration of a function while the
14866 second is the definition of a variable, including its
14869 Having seen only the parenthesis, we cannot know which of
14870 these two alternatives should be selected. Even more
14871 complex are examples like:
14876 The former is a function-declaration; the latter is a
14877 variable initialization.
14879 Thus again, we try a parameter-declaration-clause, and if
14880 that fails, we back out and return. */
14882 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14885 unsigned saved_num_template_parameter_lists;
14886 bool is_declarator = false;
14889 /* In a member-declarator, the only valid interpretation
14890 of a parenthesis is the start of a
14891 parameter-declaration-clause. (It is invalid to
14892 initialize a static data member with a parenthesized
14893 initializer; only the "=" form of initialization is
14896 cp_parser_parse_tentatively (parser);
14898 /* Consume the `('. */
14899 cp_lexer_consume_token (parser->lexer);
14902 /* If this is going to be an abstract declarator, we're
14903 in a declarator and we can't have default args. */
14904 parser->default_arg_ok_p = false;
14905 parser->in_declarator_p = true;
14908 /* Inside the function parameter list, surrounding
14909 template-parameter-lists do not apply. */
14910 saved_num_template_parameter_lists
14911 = parser->num_template_parameter_lists;
14912 parser->num_template_parameter_lists = 0;
14914 begin_scope (sk_function_parms, NULL_TREE);
14916 /* Parse the parameter-declaration-clause. */
14917 params = cp_parser_parameter_declaration_clause (parser);
14919 parser->num_template_parameter_lists
14920 = saved_num_template_parameter_lists;
14922 /* If all went well, parse the cv-qualifier-seq and the
14923 exception-specification. */
14924 if (member_p || cp_parser_parse_definitely (parser))
14926 cp_cv_quals cv_quals;
14927 tree exception_specification;
14930 is_declarator = true;
14932 if (ctor_dtor_or_conv_p)
14933 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14935 /* Consume the `)'. */
14936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14938 /* Parse the cv-qualifier-seq. */
14939 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14940 /* And the exception-specification. */
14941 exception_specification
14942 = cp_parser_exception_specification_opt (parser);
14945 = cp_parser_late_return_type_opt (parser);
14947 /* Create the function-declarator. */
14948 declarator = make_call_declarator (declarator,
14951 exception_specification,
14953 /* Any subsequent parameter lists are to do with
14954 return type, so are not those of the declared
14956 parser->default_arg_ok_p = false;
14959 /* Remove the function parms from scope. */
14960 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14961 pop_binding (DECL_NAME (t), t);
14965 /* Repeat the main loop. */
14969 /* If this is the first, we can try a parenthesized
14973 bool saved_in_type_id_in_expr_p;
14975 parser->default_arg_ok_p = saved_default_arg_ok_p;
14976 parser->in_declarator_p = saved_in_declarator_p;
14978 /* Consume the `('. */
14979 cp_lexer_consume_token (parser->lexer);
14980 /* Parse the nested declarator. */
14981 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14982 parser->in_type_id_in_expr_p = true;
14984 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14985 /*parenthesized_p=*/NULL,
14987 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14989 /* Expect a `)'. */
14990 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14991 declarator = cp_error_declarator;
14992 if (declarator == cp_error_declarator)
14995 goto handle_declarator;
14997 /* Otherwise, we must be done. */
15001 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15002 && token->type == CPP_OPEN_SQUARE)
15004 /* Parse an array-declarator. */
15007 if (ctor_dtor_or_conv_p)
15008 *ctor_dtor_or_conv_p = 0;
15011 parser->default_arg_ok_p = false;
15012 parser->in_declarator_p = true;
15013 /* Consume the `['. */
15014 cp_lexer_consume_token (parser->lexer);
15015 /* Peek at the next token. */
15016 token = cp_lexer_peek_token (parser->lexer);
15017 /* If the next token is `]', then there is no
15018 constant-expression. */
15019 if (token->type != CPP_CLOSE_SQUARE)
15021 bool non_constant_p;
15024 = cp_parser_constant_expression (parser,
15025 /*allow_non_constant=*/true,
15027 if (!non_constant_p || cxx_dialect >= cxx0x)
15029 /* Normally, the array bound must be an integral constant
15030 expression. However, as an extension, we allow VLAs
15031 in function scopes as long as they aren't part of a
15032 parameter declaration. */
15033 else if (!parser->in_function_body
15034 || current_binding_level->kind == sk_function_parms)
15036 cp_parser_error (parser,
15037 "array bound is not an integer constant");
15038 bounds = error_mark_node;
15040 else if (processing_template_decl && !error_operand_p (bounds))
15042 /* Remember this wasn't a constant-expression. */
15043 bounds = build_nop (TREE_TYPE (bounds), bounds);
15044 TREE_SIDE_EFFECTS (bounds) = 1;
15048 bounds = NULL_TREE;
15049 /* Look for the closing `]'. */
15050 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15052 declarator = cp_error_declarator;
15056 declarator = make_array_declarator (declarator, bounds);
15058 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15061 tree qualifying_scope;
15062 tree unqualified_name;
15063 special_function_kind sfk;
15065 bool pack_expansion_p = false;
15066 cp_token *declarator_id_start_token;
15068 /* Parse a declarator-id */
15069 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15072 cp_parser_parse_tentatively (parser);
15074 /* If we see an ellipsis, we should be looking at a
15076 if (token->type == CPP_ELLIPSIS)
15078 /* Consume the `...' */
15079 cp_lexer_consume_token (parser->lexer);
15081 pack_expansion_p = true;
15085 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15087 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15088 qualifying_scope = parser->scope;
15093 if (!unqualified_name && pack_expansion_p)
15095 /* Check whether an error occurred. */
15096 okay = !cp_parser_error_occurred (parser);
15098 /* We already consumed the ellipsis to mark a
15099 parameter pack, but we have no way to report it,
15100 so abort the tentative parse. We will be exiting
15101 immediately anyway. */
15102 cp_parser_abort_tentative_parse (parser);
15105 okay = cp_parser_parse_definitely (parser);
15108 unqualified_name = error_mark_node;
15109 else if (unqualified_name
15110 && (qualifying_scope
15111 || (TREE_CODE (unqualified_name)
15112 != IDENTIFIER_NODE)))
15114 cp_parser_error (parser, "expected unqualified-id");
15115 unqualified_name = error_mark_node;
15119 if (!unqualified_name)
15121 if (unqualified_name == error_mark_node)
15123 declarator = cp_error_declarator;
15124 pack_expansion_p = false;
15125 declarator->parameter_pack_p = false;
15129 if (qualifying_scope && at_namespace_scope_p ()
15130 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15132 /* In the declaration of a member of a template class
15133 outside of the class itself, the SCOPE will sometimes
15134 be a TYPENAME_TYPE. For example, given:
15136 template <typename T>
15137 int S<T>::R::i = 3;
15139 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15140 this context, we must resolve S<T>::R to an ordinary
15141 type, rather than a typename type.
15143 The reason we normally avoid resolving TYPENAME_TYPEs
15144 is that a specialization of `S' might render
15145 `S<T>::R' not a type. However, if `S' is
15146 specialized, then this `i' will not be used, so there
15147 is no harm in resolving the types here. */
15150 /* Resolve the TYPENAME_TYPE. */
15151 type = resolve_typename_type (qualifying_scope,
15152 /*only_current_p=*/false);
15153 /* If that failed, the declarator is invalid. */
15154 if (TREE_CODE (type) == TYPENAME_TYPE)
15156 if (typedef_variant_p (type))
15157 error_at (declarator_id_start_token->location,
15158 "cannot define member of dependent typedef "
15161 error_at (declarator_id_start_token->location,
15162 "%<%T::%E%> is not a type",
15163 TYPE_CONTEXT (qualifying_scope),
15164 TYPE_IDENTIFIER (qualifying_scope));
15166 qualifying_scope = type;
15171 if (unqualified_name)
15175 if (qualifying_scope
15176 && CLASS_TYPE_P (qualifying_scope))
15177 class_type = qualifying_scope;
15179 class_type = current_class_type;
15181 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15183 tree name_type = TREE_TYPE (unqualified_name);
15184 if (class_type && same_type_p (name_type, class_type))
15186 if (qualifying_scope
15187 && CLASSTYPE_USE_TEMPLATE (name_type))
15189 error_at (declarator_id_start_token->location,
15190 "invalid use of constructor as a template");
15191 inform (declarator_id_start_token->location,
15192 "use %<%T::%D%> instead of %<%T::%D%> to "
15193 "name the constructor in a qualified name",
15195 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15196 class_type, name_type);
15197 declarator = cp_error_declarator;
15201 unqualified_name = constructor_name (class_type);
15205 /* We do not attempt to print the declarator
15206 here because we do not have enough
15207 information about its original syntactic
15209 cp_parser_error (parser, "invalid declarator");
15210 declarator = cp_error_declarator;
15217 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15218 sfk = sfk_destructor;
15219 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15220 sfk = sfk_conversion;
15221 else if (/* There's no way to declare a constructor
15222 for an anonymous type, even if the type
15223 got a name for linkage purposes. */
15224 !TYPE_WAS_ANONYMOUS (class_type)
15225 && constructor_name_p (unqualified_name,
15228 unqualified_name = constructor_name (class_type);
15229 sfk = sfk_constructor;
15231 else if (is_overloaded_fn (unqualified_name)
15232 && DECL_CONSTRUCTOR_P (get_first_fn
15233 (unqualified_name)))
15234 sfk = sfk_constructor;
15236 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15237 *ctor_dtor_or_conv_p = -1;
15240 declarator = make_id_declarator (qualifying_scope,
15243 declarator->id_loc = token->location;
15244 declarator->parameter_pack_p = pack_expansion_p;
15246 if (pack_expansion_p)
15247 maybe_warn_variadic_templates ();
15250 handle_declarator:;
15251 scope = get_scope_of_declarator (declarator);
15253 /* Any names that appear after the declarator-id for a
15254 member are looked up in the containing scope. */
15255 pushed_scope = push_scope (scope);
15256 parser->in_declarator_p = true;
15257 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15258 || (declarator && declarator->kind == cdk_id))
15259 /* Default args are only allowed on function
15261 parser->default_arg_ok_p = saved_default_arg_ok_p;
15263 parser->default_arg_ok_p = false;
15272 /* For an abstract declarator, we might wind up with nothing at this
15273 point. That's an error; the declarator is not optional. */
15275 cp_parser_error (parser, "expected declarator");
15277 /* If we entered a scope, we must exit it now. */
15279 pop_scope (pushed_scope);
15281 parser->default_arg_ok_p = saved_default_arg_ok_p;
15282 parser->in_declarator_p = saved_in_declarator_p;
15287 /* Parse a ptr-operator.
15290 * cv-qualifier-seq [opt]
15292 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15297 & cv-qualifier-seq [opt]
15299 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15300 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15301 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15302 filled in with the TYPE containing the member. *CV_QUALS is
15303 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15304 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15305 Note that the tree codes returned by this function have nothing
15306 to do with the types of trees that will be eventually be created
15307 to represent the pointer or reference type being parsed. They are
15308 just constants with suggestive names. */
15309 static enum tree_code
15310 cp_parser_ptr_operator (cp_parser* parser,
15312 cp_cv_quals *cv_quals)
15314 enum tree_code code = ERROR_MARK;
15317 /* Assume that it's not a pointer-to-member. */
15319 /* And that there are no cv-qualifiers. */
15320 *cv_quals = TYPE_UNQUALIFIED;
15322 /* Peek at the next token. */
15323 token = cp_lexer_peek_token (parser->lexer);
15325 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15326 if (token->type == CPP_MULT)
15327 code = INDIRECT_REF;
15328 else if (token->type == CPP_AND)
15330 else if ((cxx_dialect != cxx98) &&
15331 token->type == CPP_AND_AND) /* C++0x only */
15332 code = NON_LVALUE_EXPR;
15334 if (code != ERROR_MARK)
15336 /* Consume the `*', `&' or `&&'. */
15337 cp_lexer_consume_token (parser->lexer);
15339 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15340 `&', if we are allowing GNU extensions. (The only qualifier
15341 that can legally appear after `&' is `restrict', but that is
15342 enforced during semantic analysis. */
15343 if (code == INDIRECT_REF
15344 || cp_parser_allow_gnu_extensions_p (parser))
15345 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15349 /* Try the pointer-to-member case. */
15350 cp_parser_parse_tentatively (parser);
15351 /* Look for the optional `::' operator. */
15352 cp_parser_global_scope_opt (parser,
15353 /*current_scope_valid_p=*/false);
15354 /* Look for the nested-name specifier. */
15355 token = cp_lexer_peek_token (parser->lexer);
15356 cp_parser_nested_name_specifier (parser,
15357 /*typename_keyword_p=*/false,
15358 /*check_dependency_p=*/true,
15360 /*is_declaration=*/false);
15361 /* If we found it, and the next token is a `*', then we are
15362 indeed looking at a pointer-to-member operator. */
15363 if (!cp_parser_error_occurred (parser)
15364 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15366 /* Indicate that the `*' operator was used. */
15367 code = INDIRECT_REF;
15369 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15370 error_at (token->location, "%qD is a namespace", parser->scope);
15373 /* The type of which the member is a member is given by the
15375 *type = parser->scope;
15376 /* The next name will not be qualified. */
15377 parser->scope = NULL_TREE;
15378 parser->qualifying_scope = NULL_TREE;
15379 parser->object_scope = NULL_TREE;
15380 /* Look for the optional cv-qualifier-seq. */
15381 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15384 /* If that didn't work we don't have a ptr-operator. */
15385 if (!cp_parser_parse_definitely (parser))
15386 cp_parser_error (parser, "expected ptr-operator");
15392 /* Parse an (optional) cv-qualifier-seq.
15395 cv-qualifier cv-qualifier-seq [opt]
15406 Returns a bitmask representing the cv-qualifiers. */
15409 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15411 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15416 cp_cv_quals cv_qualifier;
15418 /* Peek at the next token. */
15419 token = cp_lexer_peek_token (parser->lexer);
15420 /* See if it's a cv-qualifier. */
15421 switch (token->keyword)
15424 cv_qualifier = TYPE_QUAL_CONST;
15428 cv_qualifier = TYPE_QUAL_VOLATILE;
15432 cv_qualifier = TYPE_QUAL_RESTRICT;
15436 cv_qualifier = TYPE_UNQUALIFIED;
15443 if (cv_quals & cv_qualifier)
15445 error_at (token->location, "duplicate cv-qualifier");
15446 cp_lexer_purge_token (parser->lexer);
15450 cp_lexer_consume_token (parser->lexer);
15451 cv_quals |= cv_qualifier;
15458 /* Parse a late-specified return type, if any. This is not a separate
15459 non-terminal, but part of a function declarator, which looks like
15461 -> trailing-type-specifier-seq abstract-declarator(opt)
15463 Returns the type indicated by the type-id. */
15466 cp_parser_late_return_type_opt (cp_parser* parser)
15470 /* Peek at the next token. */
15471 token = cp_lexer_peek_token (parser->lexer);
15472 /* A late-specified return type is indicated by an initial '->'. */
15473 if (token->type != CPP_DEREF)
15476 /* Consume the ->. */
15477 cp_lexer_consume_token (parser->lexer);
15479 return cp_parser_trailing_type_id (parser);
15482 /* Parse a declarator-id.
15486 :: [opt] nested-name-specifier [opt] type-name
15488 In the `id-expression' case, the value returned is as for
15489 cp_parser_id_expression if the id-expression was an unqualified-id.
15490 If the id-expression was a qualified-id, then a SCOPE_REF is
15491 returned. The first operand is the scope (either a NAMESPACE_DECL
15492 or TREE_TYPE), but the second is still just a representation of an
15496 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15499 /* The expression must be an id-expression. Assume that qualified
15500 names are the names of types so that:
15503 int S<T>::R::i = 3;
15505 will work; we must treat `S<T>::R' as the name of a type.
15506 Similarly, assume that qualified names are templates, where
15510 int S<T>::R<T>::i = 3;
15513 id = cp_parser_id_expression (parser,
15514 /*template_keyword_p=*/false,
15515 /*check_dependency_p=*/false,
15516 /*template_p=*/NULL,
15517 /*declarator_p=*/true,
15519 if (id && BASELINK_P (id))
15520 id = BASELINK_FUNCTIONS (id);
15524 /* Parse a type-id.
15527 type-specifier-seq abstract-declarator [opt]
15529 Returns the TYPE specified. */
15532 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15533 bool is_trailing_return)
15535 cp_decl_specifier_seq type_specifier_seq;
15536 cp_declarator *abstract_declarator;
15538 /* Parse the type-specifier-seq. */
15539 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15540 is_trailing_return,
15541 &type_specifier_seq);
15542 if (type_specifier_seq.type == error_mark_node)
15543 return error_mark_node;
15545 /* There might or might not be an abstract declarator. */
15546 cp_parser_parse_tentatively (parser);
15547 /* Look for the declarator. */
15548 abstract_declarator
15549 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15550 /*parenthesized_p=*/NULL,
15551 /*member_p=*/false);
15552 /* Check to see if there really was a declarator. */
15553 if (!cp_parser_parse_definitely (parser))
15554 abstract_declarator = NULL;
15556 if (type_specifier_seq.type
15557 && type_uses_auto (type_specifier_seq.type))
15559 /* A type-id with type 'auto' is only ok if the abstract declarator
15560 is a function declarator with a late-specified return type. */
15561 if (abstract_declarator
15562 && abstract_declarator->kind == cdk_function
15563 && abstract_declarator->u.function.late_return_type)
15567 error ("invalid use of %<auto%>");
15568 return error_mark_node;
15572 return groktypename (&type_specifier_seq, abstract_declarator,
15576 static tree cp_parser_type_id (cp_parser *parser)
15578 return cp_parser_type_id_1 (parser, false, false);
15581 static tree cp_parser_template_type_arg (cp_parser *parser)
15583 return cp_parser_type_id_1 (parser, true, false);
15586 static tree cp_parser_trailing_type_id (cp_parser *parser)
15588 return cp_parser_type_id_1 (parser, false, true);
15591 /* Parse a type-specifier-seq.
15593 type-specifier-seq:
15594 type-specifier type-specifier-seq [opt]
15598 type-specifier-seq:
15599 attributes type-specifier-seq [opt]
15601 If IS_DECLARATION is true, we are at the start of a "condition" or
15602 exception-declaration, so we might be followed by a declarator-id.
15604 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15605 i.e. we've just seen "->".
15607 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15610 cp_parser_type_specifier_seq (cp_parser* parser,
15611 bool is_declaration,
15612 bool is_trailing_return,
15613 cp_decl_specifier_seq *type_specifier_seq)
15615 bool seen_type_specifier = false;
15616 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15617 cp_token *start_token = NULL;
15619 /* Clear the TYPE_SPECIFIER_SEQ. */
15620 clear_decl_specs (type_specifier_seq);
15622 /* In the context of a trailing return type, enum E { } is an
15623 elaborated-type-specifier followed by a function-body, not an
15625 if (is_trailing_return)
15626 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15628 /* Parse the type-specifiers and attributes. */
15631 tree type_specifier;
15632 bool is_cv_qualifier;
15634 /* Check for attributes first. */
15635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15637 type_specifier_seq->attributes =
15638 chainon (type_specifier_seq->attributes,
15639 cp_parser_attributes_opt (parser));
15643 /* record the token of the beginning of the type specifier seq,
15644 for error reporting purposes*/
15646 start_token = cp_lexer_peek_token (parser->lexer);
15648 /* Look for the type-specifier. */
15649 type_specifier = cp_parser_type_specifier (parser,
15651 type_specifier_seq,
15652 /*is_declaration=*/false,
15655 if (!type_specifier)
15657 /* If the first type-specifier could not be found, this is not a
15658 type-specifier-seq at all. */
15659 if (!seen_type_specifier)
15661 cp_parser_error (parser, "expected type-specifier");
15662 type_specifier_seq->type = error_mark_node;
15665 /* If subsequent type-specifiers could not be found, the
15666 type-specifier-seq is complete. */
15670 seen_type_specifier = true;
15671 /* The standard says that a condition can be:
15673 type-specifier-seq declarator = assignment-expression
15680 we should treat the "S" as a declarator, not as a
15681 type-specifier. The standard doesn't say that explicitly for
15682 type-specifier-seq, but it does say that for
15683 decl-specifier-seq in an ordinary declaration. Perhaps it
15684 would be clearer just to allow a decl-specifier-seq here, and
15685 then add a semantic restriction that if any decl-specifiers
15686 that are not type-specifiers appear, the program is invalid. */
15687 if (is_declaration && !is_cv_qualifier)
15688 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15691 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15694 /* Parse a parameter-declaration-clause.
15696 parameter-declaration-clause:
15697 parameter-declaration-list [opt] ... [opt]
15698 parameter-declaration-list , ...
15700 Returns a representation for the parameter declarations. A return
15701 value of NULL indicates a parameter-declaration-clause consisting
15702 only of an ellipsis. */
15705 cp_parser_parameter_declaration_clause (cp_parser* parser)
15712 /* Peek at the next token. */
15713 token = cp_lexer_peek_token (parser->lexer);
15714 /* Check for trivial parameter-declaration-clauses. */
15715 if (token->type == CPP_ELLIPSIS)
15717 /* Consume the `...' token. */
15718 cp_lexer_consume_token (parser->lexer);
15721 else if (token->type == CPP_CLOSE_PAREN)
15722 /* There are no parameters. */
15724 #ifndef NO_IMPLICIT_EXTERN_C
15725 if (in_system_header && current_class_type == NULL
15726 && current_lang_name == lang_name_c)
15730 return void_list_node;
15732 /* Check for `(void)', too, which is a special case. */
15733 else if (token->keyword == RID_VOID
15734 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15735 == CPP_CLOSE_PAREN))
15737 /* Consume the `void' token. */
15738 cp_lexer_consume_token (parser->lexer);
15739 /* There are no parameters. */
15740 return void_list_node;
15743 /* Parse the parameter-declaration-list. */
15744 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15745 /* If a parse error occurred while parsing the
15746 parameter-declaration-list, then the entire
15747 parameter-declaration-clause is erroneous. */
15751 /* Peek at the next token. */
15752 token = cp_lexer_peek_token (parser->lexer);
15753 /* If it's a `,', the clause should terminate with an ellipsis. */
15754 if (token->type == CPP_COMMA)
15756 /* Consume the `,'. */
15757 cp_lexer_consume_token (parser->lexer);
15758 /* Expect an ellipsis. */
15760 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15762 /* It might also be `...' if the optional trailing `,' was
15764 else if (token->type == CPP_ELLIPSIS)
15766 /* Consume the `...' token. */
15767 cp_lexer_consume_token (parser->lexer);
15768 /* And remember that we saw it. */
15772 ellipsis_p = false;
15774 /* Finish the parameter list. */
15776 parameters = chainon (parameters, void_list_node);
15781 /* Parse a parameter-declaration-list.
15783 parameter-declaration-list:
15784 parameter-declaration
15785 parameter-declaration-list , parameter-declaration
15787 Returns a representation of the parameter-declaration-list, as for
15788 cp_parser_parameter_declaration_clause. However, the
15789 `void_list_node' is never appended to the list. Upon return,
15790 *IS_ERROR will be true iff an error occurred. */
15793 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15795 tree parameters = NULL_TREE;
15796 tree *tail = ¶meters;
15797 bool saved_in_unbraced_linkage_specification_p;
15800 /* Assume all will go well. */
15802 /* The special considerations that apply to a function within an
15803 unbraced linkage specifications do not apply to the parameters
15804 to the function. */
15805 saved_in_unbraced_linkage_specification_p
15806 = parser->in_unbraced_linkage_specification_p;
15807 parser->in_unbraced_linkage_specification_p = false;
15809 /* Look for more parameters. */
15812 cp_parameter_declarator *parameter;
15813 tree decl = error_mark_node;
15814 bool parenthesized_p;
15815 /* Parse the parameter. */
15817 = cp_parser_parameter_declaration (parser,
15818 /*template_parm_p=*/false,
15821 /* We don't know yet if the enclosing context is deprecated, so wait
15822 and warn in grokparms if appropriate. */
15823 deprecated_state = DEPRECATED_SUPPRESS;
15826 decl = grokdeclarator (parameter->declarator,
15827 ¶meter->decl_specifiers,
15829 parameter->default_argument != NULL_TREE,
15830 ¶meter->decl_specifiers.attributes);
15832 deprecated_state = DEPRECATED_NORMAL;
15834 /* If a parse error occurred parsing the parameter declaration,
15835 then the entire parameter-declaration-list is erroneous. */
15836 if (decl == error_mark_node)
15839 parameters = error_mark_node;
15843 if (parameter->decl_specifiers.attributes)
15844 cplus_decl_attributes (&decl,
15845 parameter->decl_specifiers.attributes,
15847 if (DECL_NAME (decl))
15848 decl = pushdecl (decl);
15850 if (decl != error_mark_node)
15852 retrofit_lang_decl (decl);
15853 DECL_PARM_INDEX (decl) = ++index;
15856 /* Add the new parameter to the list. */
15857 *tail = build_tree_list (parameter->default_argument, decl);
15858 tail = &TREE_CHAIN (*tail);
15860 /* Peek at the next token. */
15861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15862 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15863 /* These are for Objective-C++ */
15864 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15865 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15866 /* The parameter-declaration-list is complete. */
15868 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15872 /* Peek at the next token. */
15873 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15874 /* If it's an ellipsis, then the list is complete. */
15875 if (token->type == CPP_ELLIPSIS)
15877 /* Otherwise, there must be more parameters. Consume the
15879 cp_lexer_consume_token (parser->lexer);
15880 /* When parsing something like:
15882 int i(float f, double d)
15884 we can tell after seeing the declaration for "f" that we
15885 are not looking at an initialization of a variable "i",
15886 but rather at the declaration of a function "i".
15888 Due to the fact that the parsing of template arguments
15889 (as specified to a template-id) requires backtracking we
15890 cannot use this technique when inside a template argument
15892 if (!parser->in_template_argument_list_p
15893 && !parser->in_type_id_in_expr_p
15894 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15895 /* However, a parameter-declaration of the form
15896 "foat(f)" (which is a valid declaration of a
15897 parameter "f") can also be interpreted as an
15898 expression (the conversion of "f" to "float"). */
15899 && !parenthesized_p)
15900 cp_parser_commit_to_tentative_parse (parser);
15904 cp_parser_error (parser, "expected %<,%> or %<...%>");
15905 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15906 cp_parser_skip_to_closing_parenthesis (parser,
15907 /*recovering=*/true,
15908 /*or_comma=*/false,
15909 /*consume_paren=*/false);
15914 parser->in_unbraced_linkage_specification_p
15915 = saved_in_unbraced_linkage_specification_p;
15920 /* Parse a parameter declaration.
15922 parameter-declaration:
15923 decl-specifier-seq ... [opt] declarator
15924 decl-specifier-seq declarator = assignment-expression
15925 decl-specifier-seq ... [opt] abstract-declarator [opt]
15926 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15928 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15929 declares a template parameter. (In that case, a non-nested `>'
15930 token encountered during the parsing of the assignment-expression
15931 is not interpreted as a greater-than operator.)
15933 Returns a representation of the parameter, or NULL if an error
15934 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15935 true iff the declarator is of the form "(p)". */
15937 static cp_parameter_declarator *
15938 cp_parser_parameter_declaration (cp_parser *parser,
15939 bool template_parm_p,
15940 bool *parenthesized_p)
15942 int declares_class_or_enum;
15943 cp_decl_specifier_seq decl_specifiers;
15944 cp_declarator *declarator;
15945 tree default_argument;
15946 cp_token *token = NULL, *declarator_token_start = NULL;
15947 const char *saved_message;
15949 /* In a template parameter, `>' is not an operator.
15953 When parsing a default template-argument for a non-type
15954 template-parameter, the first non-nested `>' is taken as the end
15955 of the template parameter-list rather than a greater-than
15958 /* Type definitions may not appear in parameter types. */
15959 saved_message = parser->type_definition_forbidden_message;
15960 parser->type_definition_forbidden_message
15961 = G_("types may not be defined in parameter types");
15963 /* Parse the declaration-specifiers. */
15964 cp_parser_decl_specifier_seq (parser,
15965 CP_PARSER_FLAGS_NONE,
15967 &declares_class_or_enum);
15969 /* Complain about missing 'typename' or other invalid type names. */
15970 if (!decl_specifiers.any_type_specifiers_p)
15971 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15973 /* If an error occurred, there's no reason to attempt to parse the
15974 rest of the declaration. */
15975 if (cp_parser_error_occurred (parser))
15977 parser->type_definition_forbidden_message = saved_message;
15981 /* Peek at the next token. */
15982 token = cp_lexer_peek_token (parser->lexer);
15984 /* If the next token is a `)', `,', `=', `>', or `...', then there
15985 is no declarator. However, when variadic templates are enabled,
15986 there may be a declarator following `...'. */
15987 if (token->type == CPP_CLOSE_PAREN
15988 || token->type == CPP_COMMA
15989 || token->type == CPP_EQ
15990 || token->type == CPP_GREATER)
15993 if (parenthesized_p)
15994 *parenthesized_p = false;
15996 /* Otherwise, there should be a declarator. */
15999 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16000 parser->default_arg_ok_p = false;
16002 /* After seeing a decl-specifier-seq, if the next token is not a
16003 "(", there is no possibility that the code is a valid
16004 expression. Therefore, if parsing tentatively, we commit at
16006 if (!parser->in_template_argument_list_p
16007 /* In an expression context, having seen:
16011 we cannot be sure whether we are looking at a
16012 function-type (taking a "char" as a parameter) or a cast
16013 of some object of type "char" to "int". */
16014 && !parser->in_type_id_in_expr_p
16015 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16016 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16017 cp_parser_commit_to_tentative_parse (parser);
16018 /* Parse the declarator. */
16019 declarator_token_start = token;
16020 declarator = cp_parser_declarator (parser,
16021 CP_PARSER_DECLARATOR_EITHER,
16022 /*ctor_dtor_or_conv_p=*/NULL,
16024 /*member_p=*/false);
16025 parser->default_arg_ok_p = saved_default_arg_ok_p;
16026 /* After the declarator, allow more attributes. */
16027 decl_specifiers.attributes
16028 = chainon (decl_specifiers.attributes,
16029 cp_parser_attributes_opt (parser));
16032 /* If the next token is an ellipsis, and we have not seen a
16033 declarator name, and the type of the declarator contains parameter
16034 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16035 a parameter pack expansion expression. Otherwise, leave the
16036 ellipsis for a C-style variadic function. */
16037 token = cp_lexer_peek_token (parser->lexer);
16038 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16040 tree type = decl_specifiers.type;
16042 if (type && DECL_P (type))
16043 type = TREE_TYPE (type);
16046 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16047 && declarator_can_be_parameter_pack (declarator)
16048 && (!declarator || !declarator->parameter_pack_p)
16049 && uses_parameter_packs (type))
16051 /* Consume the `...'. */
16052 cp_lexer_consume_token (parser->lexer);
16053 maybe_warn_variadic_templates ();
16055 /* Build a pack expansion type */
16057 declarator->parameter_pack_p = true;
16059 decl_specifiers.type = make_pack_expansion (type);
16063 /* The restriction on defining new types applies only to the type
16064 of the parameter, not to the default argument. */
16065 parser->type_definition_forbidden_message = saved_message;
16067 /* If the next token is `=', then process a default argument. */
16068 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16070 /* Consume the `='. */
16071 cp_lexer_consume_token (parser->lexer);
16073 /* If we are defining a class, then the tokens that make up the
16074 default argument must be saved and processed later. */
16075 if (!template_parm_p && at_class_scope_p ()
16076 && TYPE_BEING_DEFINED (current_class_type)
16077 && !LAMBDA_TYPE_P (current_class_type))
16079 unsigned depth = 0;
16080 int maybe_template_id = 0;
16081 cp_token *first_token;
16084 /* Add tokens until we have processed the entire default
16085 argument. We add the range [first_token, token). */
16086 first_token = cp_lexer_peek_token (parser->lexer);
16091 /* Peek at the next token. */
16092 token = cp_lexer_peek_token (parser->lexer);
16093 /* What we do depends on what token we have. */
16094 switch (token->type)
16096 /* In valid code, a default argument must be
16097 immediately followed by a `,' `)', or `...'. */
16099 if (depth == 0 && maybe_template_id)
16101 /* If we've seen a '<', we might be in a
16102 template-argument-list. Until Core issue 325 is
16103 resolved, we don't know how this situation ought
16104 to be handled, so try to DTRT. We check whether
16105 what comes after the comma is a valid parameter
16106 declaration list. If it is, then the comma ends
16107 the default argument; otherwise the default
16108 argument continues. */
16109 bool error = false;
16112 /* Set ITALP so cp_parser_parameter_declaration_list
16113 doesn't decide to commit to this parse. */
16114 bool saved_italp = parser->in_template_argument_list_p;
16115 parser->in_template_argument_list_p = true;
16117 cp_parser_parse_tentatively (parser);
16118 cp_lexer_consume_token (parser->lexer);
16119 begin_scope (sk_function_parms, NULL_TREE);
16120 cp_parser_parameter_declaration_list (parser, &error);
16121 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16122 pop_binding (DECL_NAME (t), t);
16124 if (!cp_parser_error_occurred (parser) && !error)
16126 cp_parser_abort_tentative_parse (parser);
16128 parser->in_template_argument_list_p = saved_italp;
16131 case CPP_CLOSE_PAREN:
16133 /* If we run into a non-nested `;', `}', or `]',
16134 then the code is invalid -- but the default
16135 argument is certainly over. */
16136 case CPP_SEMICOLON:
16137 case CPP_CLOSE_BRACE:
16138 case CPP_CLOSE_SQUARE:
16141 /* Update DEPTH, if necessary. */
16142 else if (token->type == CPP_CLOSE_PAREN
16143 || token->type == CPP_CLOSE_BRACE
16144 || token->type == CPP_CLOSE_SQUARE)
16148 case CPP_OPEN_PAREN:
16149 case CPP_OPEN_SQUARE:
16150 case CPP_OPEN_BRACE:
16156 /* This might be the comparison operator, or it might
16157 start a template argument list. */
16158 ++maybe_template_id;
16162 if (cxx_dialect == cxx98)
16164 /* Fall through for C++0x, which treats the `>>'
16165 operator like two `>' tokens in certain
16171 /* This might be an operator, or it might close a
16172 template argument list. But if a previous '<'
16173 started a template argument list, this will have
16174 closed it, so we can't be in one anymore. */
16175 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16176 if (maybe_template_id < 0)
16177 maybe_template_id = 0;
16181 /* If we run out of tokens, issue an error message. */
16183 case CPP_PRAGMA_EOL:
16184 error_at (token->location, "file ends in default argument");
16190 /* In these cases, we should look for template-ids.
16191 For example, if the default argument is
16192 `X<int, double>()', we need to do name lookup to
16193 figure out whether or not `X' is a template; if
16194 so, the `,' does not end the default argument.
16196 That is not yet done. */
16203 /* If we've reached the end, stop. */
16207 /* Add the token to the token block. */
16208 token = cp_lexer_consume_token (parser->lexer);
16211 /* Create a DEFAULT_ARG to represent the unparsed default
16213 default_argument = make_node (DEFAULT_ARG);
16214 DEFARG_TOKENS (default_argument)
16215 = cp_token_cache_new (first_token, token);
16216 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16218 /* Outside of a class definition, we can just parse the
16219 assignment-expression. */
16222 token = cp_lexer_peek_token (parser->lexer);
16224 = cp_parser_default_argument (parser, template_parm_p);
16227 if (!parser->default_arg_ok_p)
16229 if (flag_permissive)
16230 warning (0, "deprecated use of default argument for parameter of non-function");
16233 error_at (token->location,
16234 "default arguments are only "
16235 "permitted for function parameters");
16236 default_argument = NULL_TREE;
16239 else if ((declarator && declarator->parameter_pack_p)
16240 || (decl_specifiers.type
16241 && PACK_EXPANSION_P (decl_specifiers.type)))
16243 /* Find the name of the parameter pack. */
16244 cp_declarator *id_declarator = declarator;
16245 while (id_declarator && id_declarator->kind != cdk_id)
16246 id_declarator = id_declarator->declarator;
16248 if (id_declarator && id_declarator->kind == cdk_id)
16249 error_at (declarator_token_start->location,
16251 ? "template parameter pack %qD"
16252 " cannot have a default argument"
16253 : "parameter pack %qD cannot have a default argument",
16254 id_declarator->u.id.unqualified_name);
16256 error_at (declarator_token_start->location,
16258 ? "template parameter pack cannot have a default argument"
16259 : "parameter pack cannot have a default argument");
16261 default_argument = NULL_TREE;
16265 default_argument = NULL_TREE;
16267 return make_parameter_declarator (&decl_specifiers,
16272 /* Parse a default argument and return it.
16274 TEMPLATE_PARM_P is true if this is a default argument for a
16275 non-type template parameter. */
16277 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16279 tree default_argument = NULL_TREE;
16280 bool saved_greater_than_is_operator_p;
16281 bool saved_local_variables_forbidden_p;
16283 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16285 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16286 parser->greater_than_is_operator_p = !template_parm_p;
16287 /* Local variable names (and the `this' keyword) may not
16288 appear in a default argument. */
16289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16290 parser->local_variables_forbidden_p = true;
16291 /* Parse the assignment-expression. */
16292 if (template_parm_p)
16293 push_deferring_access_checks (dk_no_deferred);
16295 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16296 if (template_parm_p)
16297 pop_deferring_access_checks ();
16298 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16299 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16301 return default_argument;
16304 /* Parse a function-body.
16307 compound_statement */
16310 cp_parser_function_body (cp_parser *parser)
16312 cp_parser_compound_statement (parser, NULL, false);
16315 /* Parse a ctor-initializer-opt followed by a function-body. Return
16316 true if a ctor-initializer was present. */
16319 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16322 bool ctor_initializer_p;
16323 const bool check_body_p =
16324 DECL_CONSTRUCTOR_P (current_function_decl)
16325 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16328 /* Begin the function body. */
16329 body = begin_function_body ();
16330 /* Parse the optional ctor-initializer. */
16331 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16333 /* If we're parsing a constexpr constructor definition, we need
16334 to check that the constructor body is indeed empty. However,
16335 before we get to cp_parser_function_body lot of junk has been
16336 generated, so we can't just check that we have an empty block.
16337 Rather we take a snapshot of the outermost block, and check whether
16338 cp_parser_function_body changed its state. */
16342 if (TREE_CODE (list) == BIND_EXPR)
16343 list = BIND_EXPR_BODY (list);
16344 if (TREE_CODE (list) == STATEMENT_LIST
16345 && STATEMENT_LIST_TAIL (list) != NULL)
16346 last = STATEMENT_LIST_TAIL (list)->stmt;
16348 /* Parse the function-body. */
16349 cp_parser_function_body (parser);
16351 check_constexpr_ctor_body (last, list);
16352 /* Finish the function body. */
16353 finish_function_body (body);
16355 return ctor_initializer_p;
16358 /* Parse an initializer.
16361 = initializer-clause
16362 ( expression-list )
16364 Returns an expression representing the initializer. If no
16365 initializer is present, NULL_TREE is returned.
16367 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16368 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16369 set to TRUE if there is no initializer present. If there is an
16370 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16371 is set to true; otherwise it is set to false. */
16374 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16375 bool* non_constant_p)
16380 /* Peek at the next token. */
16381 token = cp_lexer_peek_token (parser->lexer);
16383 /* Let our caller know whether or not this initializer was
16385 *is_direct_init = (token->type != CPP_EQ);
16386 /* Assume that the initializer is constant. */
16387 *non_constant_p = false;
16389 if (token->type == CPP_EQ)
16391 /* Consume the `='. */
16392 cp_lexer_consume_token (parser->lexer);
16393 /* Parse the initializer-clause. */
16394 init = cp_parser_initializer_clause (parser, non_constant_p);
16396 else if (token->type == CPP_OPEN_PAREN)
16399 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16401 /*allow_expansion_p=*/true,
16404 return error_mark_node;
16405 init = build_tree_list_vec (vec);
16406 release_tree_vector (vec);
16408 else if (token->type == CPP_OPEN_BRACE)
16410 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16411 init = cp_parser_braced_list (parser, non_constant_p);
16412 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16416 /* Anything else is an error. */
16417 cp_parser_error (parser, "expected initializer");
16418 init = error_mark_node;
16424 /* Parse an initializer-clause.
16426 initializer-clause:
16427 assignment-expression
16430 Returns an expression representing the initializer.
16432 If the `assignment-expression' production is used the value
16433 returned is simply a representation for the expression.
16435 Otherwise, calls cp_parser_braced_list. */
16438 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16442 /* Assume the expression is constant. */
16443 *non_constant_p = false;
16445 /* If it is not a `{', then we are looking at an
16446 assignment-expression. */
16447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16450 = cp_parser_constant_expression (parser,
16451 /*allow_non_constant_p=*/true,
16453 if (!*non_constant_p)
16455 /* We only want to fold if this is really a constant
16456 expression. FIXME Actually, we don't want to fold here, but in
16458 tree folded = fold_non_dependent_expr (initializer);
16459 folded = maybe_constant_value (folded);
16460 if (TREE_CONSTANT (folded))
16461 initializer = folded;
16465 initializer = cp_parser_braced_list (parser, non_constant_p);
16467 return initializer;
16470 /* Parse a brace-enclosed initializer list.
16473 { initializer-list , [opt] }
16476 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16477 the elements of the initializer-list (or NULL, if the last
16478 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16479 NULL_TREE. There is no way to detect whether or not the optional
16480 trailing `,' was provided. NON_CONSTANT_P is as for
16481 cp_parser_initializer. */
16484 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16488 /* Consume the `{' token. */
16489 cp_lexer_consume_token (parser->lexer);
16490 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16491 initializer = make_node (CONSTRUCTOR);
16492 /* If it's not a `}', then there is a non-trivial initializer. */
16493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16495 /* Parse the initializer list. */
16496 CONSTRUCTOR_ELTS (initializer)
16497 = cp_parser_initializer_list (parser, non_constant_p);
16498 /* A trailing `,' token is allowed. */
16499 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16500 cp_lexer_consume_token (parser->lexer);
16502 /* Now, there should be a trailing `}'. */
16503 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16504 TREE_TYPE (initializer) = init_list_type_node;
16505 return initializer;
16508 /* Parse an initializer-list.
16511 initializer-clause ... [opt]
16512 initializer-list , initializer-clause ... [opt]
16517 identifier : initializer-clause
16518 initializer-list, identifier : initializer-clause
16520 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16521 for the initializer. If the INDEX of the elt is non-NULL, it is the
16522 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16523 as for cp_parser_initializer. */
16525 static VEC(constructor_elt,gc) *
16526 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16528 VEC(constructor_elt,gc) *v = NULL;
16530 /* Assume all of the expressions are constant. */
16531 *non_constant_p = false;
16533 /* Parse the rest of the list. */
16539 bool clause_non_constant_p;
16541 /* If the next token is an identifier and the following one is a
16542 colon, we are looking at the GNU designated-initializer
16544 if (cp_parser_allow_gnu_extensions_p (parser)
16545 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16546 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16548 /* Warn the user that they are using an extension. */
16549 pedwarn (input_location, OPT_pedantic,
16550 "ISO C++ does not allow designated initializers");
16551 /* Consume the identifier. */
16552 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16553 /* Consume the `:'. */
16554 cp_lexer_consume_token (parser->lexer);
16557 identifier = NULL_TREE;
16559 /* Parse the initializer. */
16560 initializer = cp_parser_initializer_clause (parser,
16561 &clause_non_constant_p);
16562 /* If any clause is non-constant, so is the entire initializer. */
16563 if (clause_non_constant_p)
16564 *non_constant_p = true;
16566 /* If we have an ellipsis, this is an initializer pack
16568 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16570 /* Consume the `...'. */
16571 cp_lexer_consume_token (parser->lexer);
16573 /* Turn the initializer into an initializer expansion. */
16574 initializer = make_pack_expansion (initializer);
16577 /* Add it to the vector. */
16578 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16580 /* If the next token is not a comma, we have reached the end of
16582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16585 /* Peek at the next token. */
16586 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16587 /* If the next token is a `}', then we're still done. An
16588 initializer-clause can have a trailing `,' after the
16589 initializer-list and before the closing `}'. */
16590 if (token->type == CPP_CLOSE_BRACE)
16593 /* Consume the `,' token. */
16594 cp_lexer_consume_token (parser->lexer);
16600 /* Classes [gram.class] */
16602 /* Parse a class-name.
16608 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16609 to indicate that names looked up in dependent types should be
16610 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16611 keyword has been used to indicate that the name that appears next
16612 is a template. TAG_TYPE indicates the explicit tag given before
16613 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16614 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16615 is the class being defined in a class-head.
16617 Returns the TYPE_DECL representing the class. */
16620 cp_parser_class_name (cp_parser *parser,
16621 bool typename_keyword_p,
16622 bool template_keyword_p,
16623 enum tag_types tag_type,
16624 bool check_dependency_p,
16626 bool is_declaration)
16632 tree identifier = NULL_TREE;
16634 /* All class-names start with an identifier. */
16635 token = cp_lexer_peek_token (parser->lexer);
16636 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16638 cp_parser_error (parser, "expected class-name");
16639 return error_mark_node;
16642 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16643 to a template-id, so we save it here. */
16644 scope = parser->scope;
16645 if (scope == error_mark_node)
16646 return error_mark_node;
16648 /* Any name names a type if we're following the `typename' keyword
16649 in a qualified name where the enclosing scope is type-dependent. */
16650 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16651 && dependent_type_p (scope));
16652 /* Handle the common case (an identifier, but not a template-id)
16654 if (token->type == CPP_NAME
16655 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16657 cp_token *identifier_token;
16660 /* Look for the identifier. */
16661 identifier_token = cp_lexer_peek_token (parser->lexer);
16662 ambiguous_p = identifier_token->ambiguous_p;
16663 identifier = cp_parser_identifier (parser);
16664 /* If the next token isn't an identifier, we are certainly not
16665 looking at a class-name. */
16666 if (identifier == error_mark_node)
16667 decl = error_mark_node;
16668 /* If we know this is a type-name, there's no need to look it
16670 else if (typename_p)
16674 tree ambiguous_decls;
16675 /* If we already know that this lookup is ambiguous, then
16676 we've already issued an error message; there's no reason
16680 cp_parser_simulate_error (parser);
16681 return error_mark_node;
16683 /* If the next token is a `::', then the name must be a type
16686 [basic.lookup.qual]
16688 During the lookup for a name preceding the :: scope
16689 resolution operator, object, function, and enumerator
16690 names are ignored. */
16691 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16692 tag_type = typename_type;
16693 /* Look up the name. */
16694 decl = cp_parser_lookup_name (parser, identifier,
16696 /*is_template=*/false,
16697 /*is_namespace=*/false,
16698 check_dependency_p,
16700 identifier_token->location);
16701 if (ambiguous_decls)
16703 if (cp_parser_parsing_tentatively (parser))
16704 cp_parser_simulate_error (parser);
16705 return error_mark_node;
16711 /* Try a template-id. */
16712 decl = cp_parser_template_id (parser, template_keyword_p,
16713 check_dependency_p,
16715 if (decl == error_mark_node)
16716 return error_mark_node;
16719 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16721 /* If this is a typename, create a TYPENAME_TYPE. */
16722 if (typename_p && decl != error_mark_node)
16724 decl = make_typename_type (scope, decl, typename_type,
16725 /*complain=*/tf_error);
16726 if (decl != error_mark_node)
16727 decl = TYPE_NAME (decl);
16730 /* Check to see that it is really the name of a class. */
16731 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16732 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16733 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16734 /* Situations like this:
16736 template <typename T> struct A {
16737 typename T::template X<int>::I i;
16740 are problematic. Is `T::template X<int>' a class-name? The
16741 standard does not seem to be definitive, but there is no other
16742 valid interpretation of the following `::'. Therefore, those
16743 names are considered class-names. */
16745 decl = make_typename_type (scope, decl, tag_type, tf_error);
16746 if (decl != error_mark_node)
16747 decl = TYPE_NAME (decl);
16749 else if (TREE_CODE (decl) != TYPE_DECL
16750 || TREE_TYPE (decl) == error_mark_node
16751 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16752 /* In Objective-C 2.0, a classname followed by '.' starts a
16753 dot-syntax expression, and it's not a type-name. */
16754 || (c_dialect_objc ()
16755 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16756 && objc_is_class_name (decl)))
16757 decl = error_mark_node;
16759 if (decl == error_mark_node)
16760 cp_parser_error (parser, "expected class-name");
16761 else if (identifier && !parser->scope)
16762 maybe_note_name_used_in_class (identifier, decl);
16767 /* Parse a class-specifier.
16770 class-head { member-specification [opt] }
16772 Returns the TREE_TYPE representing the class. */
16775 cp_parser_class_specifier (cp_parser* parser)
16778 tree attributes = NULL_TREE;
16779 bool nested_name_specifier_p;
16780 unsigned saved_num_template_parameter_lists;
16781 bool saved_in_function_body;
16782 bool saved_in_unbraced_linkage_specification_p;
16783 tree old_scope = NULL_TREE;
16784 tree scope = NULL_TREE;
16787 push_deferring_access_checks (dk_no_deferred);
16789 /* Parse the class-head. */
16790 type = cp_parser_class_head (parser,
16791 &nested_name_specifier_p,
16794 /* If the class-head was a semantic disaster, skip the entire body
16798 cp_parser_skip_to_end_of_block_or_statement (parser);
16799 pop_deferring_access_checks ();
16800 return error_mark_node;
16803 /* Look for the `{'. */
16804 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16806 pop_deferring_access_checks ();
16807 return error_mark_node;
16810 /* Process the base classes. If they're invalid, skip the
16811 entire class body. */
16812 if (!xref_basetypes (type, bases))
16814 /* Consuming the closing brace yields better error messages
16816 if (cp_parser_skip_to_closing_brace (parser))
16817 cp_lexer_consume_token (parser->lexer);
16818 pop_deferring_access_checks ();
16819 return error_mark_node;
16822 /* Issue an error message if type-definitions are forbidden here. */
16823 cp_parser_check_type_definition (parser);
16824 /* Remember that we are defining one more class. */
16825 ++parser->num_classes_being_defined;
16826 /* Inside the class, surrounding template-parameter-lists do not
16828 saved_num_template_parameter_lists
16829 = parser->num_template_parameter_lists;
16830 parser->num_template_parameter_lists = 0;
16831 /* We are not in a function body. */
16832 saved_in_function_body = parser->in_function_body;
16833 parser->in_function_body = false;
16834 /* We are not immediately inside an extern "lang" block. */
16835 saved_in_unbraced_linkage_specification_p
16836 = parser->in_unbraced_linkage_specification_p;
16837 parser->in_unbraced_linkage_specification_p = false;
16839 /* Start the class. */
16840 if (nested_name_specifier_p)
16842 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16843 old_scope = push_inner_scope (scope);
16845 type = begin_class_definition (type, attributes);
16847 if (type == error_mark_node)
16848 /* If the type is erroneous, skip the entire body of the class. */
16849 cp_parser_skip_to_closing_brace (parser);
16851 /* Parse the member-specification. */
16852 cp_parser_member_specification_opt (parser);
16854 /* Look for the trailing `}'. */
16855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16856 /* Look for trailing attributes to apply to this class. */
16857 if (cp_parser_allow_gnu_extensions_p (parser))
16858 attributes = cp_parser_attributes_opt (parser);
16859 if (type != error_mark_node)
16860 type = finish_struct (type, attributes);
16861 if (nested_name_specifier_p)
16862 pop_inner_scope (old_scope, scope);
16863 /* If this class is not itself within the scope of another class,
16864 then we need to parse the bodies of all of the queued function
16865 definitions. Note that the queued functions defined in a class
16866 are not always processed immediately following the
16867 class-specifier for that class. Consider:
16870 struct B { void f() { sizeof (A); } };
16873 If `f' were processed before the processing of `A' were
16874 completed, there would be no way to compute the size of `A'.
16875 Note that the nesting we are interested in here is lexical --
16876 not the semantic nesting given by TYPE_CONTEXT. In particular,
16879 struct A { struct B; };
16880 struct A::B { void f() { } };
16882 there is no need to delay the parsing of `A::B::f'. */
16883 if (--parser->num_classes_being_defined == 0)
16886 tree class_type = NULL_TREE;
16887 tree pushed_scope = NULL_TREE;
16889 cp_default_arg_entry *e;
16891 /* In a first pass, parse default arguments to the functions.
16892 Then, in a second pass, parse the bodies of the functions.
16893 This two-phased approach handles cases like:
16901 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16905 /* If there are default arguments that have not yet been processed,
16906 take care of them now. */
16907 if (class_type != e->class_type)
16910 pop_scope (pushed_scope);
16911 class_type = e->class_type;
16912 pushed_scope = push_scope (class_type);
16914 /* Make sure that any template parameters are in scope. */
16915 maybe_begin_member_template_processing (fn);
16916 /* Parse the default argument expressions. */
16917 cp_parser_late_parsing_default_args (parser, fn);
16918 /* Remove any template parameters from the symbol table. */
16919 maybe_end_member_template_processing ();
16922 pop_scope (pushed_scope);
16923 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16924 /* Now parse the body of the functions. */
16925 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16926 cp_parser_late_parsing_for_member (parser, fn);
16927 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16930 /* Put back any saved access checks. */
16931 pop_deferring_access_checks ();
16933 /* Restore saved state. */
16934 parser->in_function_body = saved_in_function_body;
16935 parser->num_template_parameter_lists
16936 = saved_num_template_parameter_lists;
16937 parser->in_unbraced_linkage_specification_p
16938 = saved_in_unbraced_linkage_specification_p;
16943 /* Parse a class-head.
16946 class-key identifier [opt] base-clause [opt]
16947 class-key nested-name-specifier identifier base-clause [opt]
16948 class-key nested-name-specifier [opt] template-id
16952 class-key attributes identifier [opt] base-clause [opt]
16953 class-key attributes nested-name-specifier identifier base-clause [opt]
16954 class-key attributes nested-name-specifier [opt] template-id
16957 Upon return BASES is initialized to the list of base classes (or
16958 NULL, if there are none) in the same form returned by
16959 cp_parser_base_clause.
16961 Returns the TYPE of the indicated class. Sets
16962 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16963 involving a nested-name-specifier was used, and FALSE otherwise.
16965 Returns error_mark_node if this is not a class-head.
16967 Returns NULL_TREE if the class-head is syntactically valid, but
16968 semantically invalid in a way that means we should skip the entire
16969 body of the class. */
16972 cp_parser_class_head (cp_parser* parser,
16973 bool* nested_name_specifier_p,
16974 tree *attributes_p,
16977 tree nested_name_specifier;
16978 enum tag_types class_key;
16979 tree id = NULL_TREE;
16980 tree type = NULL_TREE;
16982 bool template_id_p = false;
16983 bool qualified_p = false;
16984 bool invalid_nested_name_p = false;
16985 bool invalid_explicit_specialization_p = false;
16986 tree pushed_scope = NULL_TREE;
16987 unsigned num_templates;
16988 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16989 /* Assume no nested-name-specifier will be present. */
16990 *nested_name_specifier_p = false;
16991 /* Assume no template parameter lists will be used in defining the
16995 *bases = NULL_TREE;
16997 /* Look for the class-key. */
16998 class_key = cp_parser_class_key (parser);
16999 if (class_key == none_type)
17000 return error_mark_node;
17002 /* Parse the attributes. */
17003 attributes = cp_parser_attributes_opt (parser);
17005 /* If the next token is `::', that is invalid -- but sometimes
17006 people do try to write:
17010 Handle this gracefully by accepting the extra qualifier, and then
17011 issuing an error about it later if this really is a
17012 class-head. If it turns out just to be an elaborated type
17013 specifier, remain silent. */
17014 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17015 qualified_p = true;
17017 push_deferring_access_checks (dk_no_check);
17019 /* Determine the name of the class. Begin by looking for an
17020 optional nested-name-specifier. */
17021 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17022 nested_name_specifier
17023 = cp_parser_nested_name_specifier_opt (parser,
17024 /*typename_keyword_p=*/false,
17025 /*check_dependency_p=*/false,
17027 /*is_declaration=*/false);
17028 /* If there was a nested-name-specifier, then there *must* be an
17030 if (nested_name_specifier)
17032 type_start_token = cp_lexer_peek_token (parser->lexer);
17033 /* Although the grammar says `identifier', it really means
17034 `class-name' or `template-name'. You are only allowed to
17035 define a class that has already been declared with this
17038 The proposed resolution for Core Issue 180 says that wherever
17039 you see `class T::X' you should treat `X' as a type-name.
17041 It is OK to define an inaccessible class; for example:
17043 class A { class B; };
17046 We do not know if we will see a class-name, or a
17047 template-name. We look for a class-name first, in case the
17048 class-name is a template-id; if we looked for the
17049 template-name first we would stop after the template-name. */
17050 cp_parser_parse_tentatively (parser);
17051 type = cp_parser_class_name (parser,
17052 /*typename_keyword_p=*/false,
17053 /*template_keyword_p=*/false,
17055 /*check_dependency_p=*/false,
17056 /*class_head_p=*/true,
17057 /*is_declaration=*/false);
17058 /* If that didn't work, ignore the nested-name-specifier. */
17059 if (!cp_parser_parse_definitely (parser))
17061 invalid_nested_name_p = true;
17062 type_start_token = cp_lexer_peek_token (parser->lexer);
17063 id = cp_parser_identifier (parser);
17064 if (id == error_mark_node)
17067 /* If we could not find a corresponding TYPE, treat this
17068 declaration like an unqualified declaration. */
17069 if (type == error_mark_node)
17070 nested_name_specifier = NULL_TREE;
17071 /* Otherwise, count the number of templates used in TYPE and its
17072 containing scopes. */
17077 for (scope = TREE_TYPE (type);
17078 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17079 scope = (TYPE_P (scope)
17080 ? TYPE_CONTEXT (scope)
17081 : DECL_CONTEXT (scope)))
17083 && CLASS_TYPE_P (scope)
17084 && CLASSTYPE_TEMPLATE_INFO (scope)
17085 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17086 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17090 /* Otherwise, the identifier is optional. */
17093 /* We don't know whether what comes next is a template-id,
17094 an identifier, or nothing at all. */
17095 cp_parser_parse_tentatively (parser);
17096 /* Check for a template-id. */
17097 type_start_token = cp_lexer_peek_token (parser->lexer);
17098 id = cp_parser_template_id (parser,
17099 /*template_keyword_p=*/false,
17100 /*check_dependency_p=*/true,
17101 /*is_declaration=*/true);
17102 /* If that didn't work, it could still be an identifier. */
17103 if (!cp_parser_parse_definitely (parser))
17105 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17107 type_start_token = cp_lexer_peek_token (parser->lexer);
17108 id = cp_parser_identifier (parser);
17115 template_id_p = true;
17120 pop_deferring_access_checks ();
17123 cp_parser_check_for_invalid_template_id (parser, id,
17124 type_start_token->location);
17126 /* If it's not a `:' or a `{' then we can't really be looking at a
17127 class-head, since a class-head only appears as part of a
17128 class-specifier. We have to detect this situation before calling
17129 xref_tag, since that has irreversible side-effects. */
17130 if (!cp_parser_next_token_starts_class_definition_p (parser))
17132 cp_parser_error (parser, "expected %<{%> or %<:%>");
17133 return error_mark_node;
17136 /* At this point, we're going ahead with the class-specifier, even
17137 if some other problem occurs. */
17138 cp_parser_commit_to_tentative_parse (parser);
17139 /* Issue the error about the overly-qualified name now. */
17142 cp_parser_error (parser,
17143 "global qualification of class name is invalid");
17144 return error_mark_node;
17146 else if (invalid_nested_name_p)
17148 cp_parser_error (parser,
17149 "qualified name does not name a class");
17150 return error_mark_node;
17152 else if (nested_name_specifier)
17156 /* Reject typedef-names in class heads. */
17157 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17159 error_at (type_start_token->location,
17160 "invalid class name in declaration of %qD",
17166 /* Figure out in what scope the declaration is being placed. */
17167 scope = current_scope ();
17168 /* If that scope does not contain the scope in which the
17169 class was originally declared, the program is invalid. */
17170 if (scope && !is_ancestor (scope, nested_name_specifier))
17172 if (at_namespace_scope_p ())
17173 error_at (type_start_token->location,
17174 "declaration of %qD in namespace %qD which does not "
17176 type, scope, nested_name_specifier);
17178 error_at (type_start_token->location,
17179 "declaration of %qD in %qD which does not enclose %qD",
17180 type, scope, nested_name_specifier);
17186 A declarator-id shall not be qualified except for the
17187 definition of a ... nested class outside of its class
17188 ... [or] the definition or explicit instantiation of a
17189 class member of a namespace outside of its namespace. */
17190 if (scope == nested_name_specifier)
17192 permerror (nested_name_specifier_token_start->location,
17193 "extra qualification not allowed");
17194 nested_name_specifier = NULL_TREE;
17198 /* An explicit-specialization must be preceded by "template <>". If
17199 it is not, try to recover gracefully. */
17200 if (at_namespace_scope_p ()
17201 && parser->num_template_parameter_lists == 0
17204 error_at (type_start_token->location,
17205 "an explicit specialization must be preceded by %<template <>%>");
17206 invalid_explicit_specialization_p = true;
17207 /* Take the same action that would have been taken by
17208 cp_parser_explicit_specialization. */
17209 ++parser->num_template_parameter_lists;
17210 begin_specialization ();
17212 /* There must be no "return" statements between this point and the
17213 end of this function; set "type "to the correct return value and
17214 use "goto done;" to return. */
17215 /* Make sure that the right number of template parameters were
17217 if (!cp_parser_check_template_parameters (parser, num_templates,
17218 type_start_token->location,
17219 /*declarator=*/NULL))
17221 /* If something went wrong, there is no point in even trying to
17222 process the class-definition. */
17227 /* Look up the type. */
17230 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17231 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17232 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17234 error_at (type_start_token->location,
17235 "function template %qD redeclared as a class template", id);
17236 type = error_mark_node;
17240 type = TREE_TYPE (id);
17241 type = maybe_process_partial_specialization (type);
17243 if (nested_name_specifier)
17244 pushed_scope = push_scope (nested_name_specifier);
17246 else if (nested_name_specifier)
17252 template <typename T> struct S { struct T };
17253 template <typename T> struct S<T>::T { };
17255 we will get a TYPENAME_TYPE when processing the definition of
17256 `S::T'. We need to resolve it to the actual type before we
17257 try to define it. */
17258 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17260 class_type = resolve_typename_type (TREE_TYPE (type),
17261 /*only_current_p=*/false);
17262 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17263 type = TYPE_NAME (class_type);
17266 cp_parser_error (parser, "could not resolve typename type");
17267 type = error_mark_node;
17271 if (maybe_process_partial_specialization (TREE_TYPE (type))
17272 == error_mark_node)
17278 class_type = current_class_type;
17279 /* Enter the scope indicated by the nested-name-specifier. */
17280 pushed_scope = push_scope (nested_name_specifier);
17281 /* Get the canonical version of this type. */
17282 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17283 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17286 type = push_template_decl (type);
17287 if (type == error_mark_node)
17294 type = TREE_TYPE (type);
17295 *nested_name_specifier_p = true;
17297 else /* The name is not a nested name. */
17299 /* If the class was unnamed, create a dummy name. */
17301 id = make_anon_name ();
17302 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17303 parser->num_template_parameter_lists);
17306 /* Indicate whether this class was declared as a `class' or as a
17308 if (TREE_CODE (type) == RECORD_TYPE)
17309 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17310 cp_parser_check_class_key (class_key, type);
17312 /* If this type was already complete, and we see another definition,
17313 that's an error. */
17314 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17316 error_at (type_start_token->location, "redefinition of %q#T",
17318 error_at (type_start_token->location, "previous definition of %q+#T",
17323 else if (type == error_mark_node)
17326 /* We will have entered the scope containing the class; the names of
17327 base classes should be looked up in that context. For example:
17329 struct A { struct B {}; struct C; };
17330 struct A::C : B {};
17334 /* Get the list of base-classes, if there is one. */
17335 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17336 *bases = cp_parser_base_clause (parser);
17339 /* Leave the scope given by the nested-name-specifier. We will
17340 enter the class scope itself while processing the members. */
17342 pop_scope (pushed_scope);
17344 if (invalid_explicit_specialization_p)
17346 end_specialization ();
17347 --parser->num_template_parameter_lists;
17351 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17352 *attributes_p = attributes;
17356 /* Parse a class-key.
17363 Returns the kind of class-key specified, or none_type to indicate
17366 static enum tag_types
17367 cp_parser_class_key (cp_parser* parser)
17370 enum tag_types tag_type;
17372 /* Look for the class-key. */
17373 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17377 /* Check to see if the TOKEN is a class-key. */
17378 tag_type = cp_parser_token_is_class_key (token);
17380 cp_parser_error (parser, "expected class-key");
17384 /* Parse an (optional) member-specification.
17386 member-specification:
17387 member-declaration member-specification [opt]
17388 access-specifier : member-specification [opt] */
17391 cp_parser_member_specification_opt (cp_parser* parser)
17398 /* Peek at the next token. */
17399 token = cp_lexer_peek_token (parser->lexer);
17400 /* If it's a `}', or EOF then we've seen all the members. */
17401 if (token->type == CPP_CLOSE_BRACE
17402 || token->type == CPP_EOF
17403 || token->type == CPP_PRAGMA_EOL)
17406 /* See if this token is a keyword. */
17407 keyword = token->keyword;
17411 case RID_PROTECTED:
17413 /* Consume the access-specifier. */
17414 cp_lexer_consume_token (parser->lexer);
17415 /* Remember which access-specifier is active. */
17416 current_access_specifier = token->u.value;
17417 /* Look for the `:'. */
17418 cp_parser_require (parser, CPP_COLON, RT_COLON);
17422 /* Accept #pragmas at class scope. */
17423 if (token->type == CPP_PRAGMA)
17425 cp_parser_pragma (parser, pragma_external);
17429 /* Otherwise, the next construction must be a
17430 member-declaration. */
17431 cp_parser_member_declaration (parser);
17436 /* Parse a member-declaration.
17438 member-declaration:
17439 decl-specifier-seq [opt] member-declarator-list [opt] ;
17440 function-definition ; [opt]
17441 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17443 template-declaration
17445 member-declarator-list:
17447 member-declarator-list , member-declarator
17450 declarator pure-specifier [opt]
17451 declarator constant-initializer [opt]
17452 identifier [opt] : constant-expression
17456 member-declaration:
17457 __extension__ member-declaration
17460 declarator attributes [opt] pure-specifier [opt]
17461 declarator attributes [opt] constant-initializer [opt]
17462 identifier [opt] attributes [opt] : constant-expression
17466 member-declaration:
17467 static_assert-declaration */
17470 cp_parser_member_declaration (cp_parser* parser)
17472 cp_decl_specifier_seq decl_specifiers;
17473 tree prefix_attributes;
17475 int declares_class_or_enum;
17477 cp_token *token = NULL;
17478 cp_token *decl_spec_token_start = NULL;
17479 cp_token *initializer_token_start = NULL;
17480 int saved_pedantic;
17482 /* Check for the `__extension__' keyword. */
17483 if (cp_parser_extension_opt (parser, &saved_pedantic))
17486 cp_parser_member_declaration (parser);
17487 /* Restore the old value of the PEDANTIC flag. */
17488 pedantic = saved_pedantic;
17493 /* Check for a template-declaration. */
17494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17496 /* An explicit specialization here is an error condition, and we
17497 expect the specialization handler to detect and report this. */
17498 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17499 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17500 cp_parser_explicit_specialization (parser);
17502 cp_parser_template_declaration (parser, /*member_p=*/true);
17507 /* Check for a using-declaration. */
17508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17510 /* Parse the using-declaration. */
17511 cp_parser_using_declaration (parser,
17512 /*access_declaration_p=*/false);
17516 /* Check for @defs. */
17517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17520 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17521 ivar = ivar_chains;
17525 ivar = TREE_CHAIN (member);
17526 TREE_CHAIN (member) = NULL_TREE;
17527 finish_member_declaration (member);
17532 /* If the next token is `static_assert' we have a static assertion. */
17533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17535 cp_parser_static_assert (parser, /*member_p=*/true);
17539 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17542 /* Parse the decl-specifier-seq. */
17543 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17544 cp_parser_decl_specifier_seq (parser,
17545 CP_PARSER_FLAGS_OPTIONAL,
17547 &declares_class_or_enum);
17548 prefix_attributes = decl_specifiers.attributes;
17549 decl_specifiers.attributes = NULL_TREE;
17550 /* Check for an invalid type-name. */
17551 if (!decl_specifiers.any_type_specifiers_p
17552 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17554 /* If there is no declarator, then the decl-specifier-seq should
17556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17558 /* If there was no decl-specifier-seq, and the next token is a
17559 `;', then we have something like:
17565 Each member-declaration shall declare at least one member
17566 name of the class. */
17567 if (!decl_specifiers.any_specifiers_p)
17569 cp_token *token = cp_lexer_peek_token (parser->lexer);
17570 if (!in_system_header_at (token->location))
17571 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17577 /* See if this declaration is a friend. */
17578 friend_p = cp_parser_friend_p (&decl_specifiers);
17579 /* If there were decl-specifiers, check to see if there was
17580 a class-declaration. */
17581 type = check_tag_decl (&decl_specifiers);
17582 /* Nested classes have already been added to the class, but
17583 a `friend' needs to be explicitly registered. */
17586 /* If the `friend' keyword was present, the friend must
17587 be introduced with a class-key. */
17588 if (!declares_class_or_enum)
17589 error_at (decl_spec_token_start->location,
17590 "a class-key must be used when declaring a friend");
17593 template <typename T> struct A {
17594 friend struct A<T>::B;
17597 A<T>::B will be represented by a TYPENAME_TYPE, and
17598 therefore not recognized by check_tag_decl. */
17600 && decl_specifiers.type
17601 && TYPE_P (decl_specifiers.type))
17602 type = decl_specifiers.type;
17603 if (!type || !TYPE_P (type))
17604 error_at (decl_spec_token_start->location,
17605 "friend declaration does not name a class or "
17608 make_friend_class (current_class_type, type,
17609 /*complain=*/true);
17611 /* If there is no TYPE, an error message will already have
17613 else if (!type || type == error_mark_node)
17615 /* An anonymous aggregate has to be handled specially; such
17616 a declaration really declares a data member (with a
17617 particular type), as opposed to a nested class. */
17618 else if (ANON_AGGR_TYPE_P (type))
17620 /* Remove constructors and such from TYPE, now that we
17621 know it is an anonymous aggregate. */
17622 fixup_anonymous_aggr (type);
17623 /* And make the corresponding data member. */
17624 decl = build_decl (decl_spec_token_start->location,
17625 FIELD_DECL, NULL_TREE, type);
17626 /* Add it to the class. */
17627 finish_member_declaration (decl);
17630 cp_parser_check_access_in_redeclaration
17632 decl_spec_token_start->location);
17637 bool assume_semicolon = false;
17639 /* See if these declarations will be friends. */
17640 friend_p = cp_parser_friend_p (&decl_specifiers);
17642 /* Keep going until we hit the `;' at the end of the
17644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17646 tree attributes = NULL_TREE;
17647 tree first_attribute;
17649 /* Peek at the next token. */
17650 token = cp_lexer_peek_token (parser->lexer);
17652 /* Check for a bitfield declaration. */
17653 if (token->type == CPP_COLON
17654 || (token->type == CPP_NAME
17655 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17661 /* Get the name of the bitfield. Note that we cannot just
17662 check TOKEN here because it may have been invalidated by
17663 the call to cp_lexer_peek_nth_token above. */
17664 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17665 identifier = cp_parser_identifier (parser);
17667 identifier = NULL_TREE;
17669 /* Consume the `:' token. */
17670 cp_lexer_consume_token (parser->lexer);
17671 /* Get the width of the bitfield. */
17673 = cp_parser_constant_expression (parser,
17674 /*allow_non_constant=*/false,
17677 /* Look for attributes that apply to the bitfield. */
17678 attributes = cp_parser_attributes_opt (parser);
17679 /* Remember which attributes are prefix attributes and
17681 first_attribute = attributes;
17682 /* Combine the attributes. */
17683 attributes = chainon (prefix_attributes, attributes);
17685 /* Create the bitfield declaration. */
17686 decl = grokbitfield (identifier
17687 ? make_id_declarator (NULL_TREE,
17697 cp_declarator *declarator;
17699 tree asm_specification;
17700 int ctor_dtor_or_conv_p;
17702 /* Parse the declarator. */
17704 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17705 &ctor_dtor_or_conv_p,
17706 /*parenthesized_p=*/NULL,
17707 /*member_p=*/true);
17709 /* If something went wrong parsing the declarator, make sure
17710 that we at least consume some tokens. */
17711 if (declarator == cp_error_declarator)
17713 /* Skip to the end of the statement. */
17714 cp_parser_skip_to_end_of_statement (parser);
17715 /* If the next token is not a semicolon, that is
17716 probably because we just skipped over the body of
17717 a function. So, we consume a semicolon if
17718 present, but do not issue an error message if it
17720 if (cp_lexer_next_token_is (parser->lexer,
17722 cp_lexer_consume_token (parser->lexer);
17726 if (declares_class_or_enum & 2)
17727 cp_parser_check_for_definition_in_return_type
17728 (declarator, decl_specifiers.type,
17729 decl_specifiers.type_location);
17731 /* Look for an asm-specification. */
17732 asm_specification = cp_parser_asm_specification_opt (parser);
17733 /* Look for attributes that apply to the declaration. */
17734 attributes = cp_parser_attributes_opt (parser);
17735 /* Remember which attributes are prefix attributes and
17737 first_attribute = attributes;
17738 /* Combine the attributes. */
17739 attributes = chainon (prefix_attributes, attributes);
17741 /* If it's an `=', then we have a constant-initializer or a
17742 pure-specifier. It is not correct to parse the
17743 initializer before registering the member declaration
17744 since the member declaration should be in scope while
17745 its initializer is processed. However, the rest of the
17746 front end does not yet provide an interface that allows
17747 us to handle this correctly. */
17748 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17752 A pure-specifier shall be used only in the declaration of
17753 a virtual function.
17755 A member-declarator can contain a constant-initializer
17756 only if it declares a static member of integral or
17759 Therefore, if the DECLARATOR is for a function, we look
17760 for a pure-specifier; otherwise, we look for a
17761 constant-initializer. When we call `grokfield', it will
17762 perform more stringent semantics checks. */
17763 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17764 if (function_declarator_p (declarator))
17765 initializer = cp_parser_pure_specifier (parser);
17767 /* Parse the initializer. */
17768 initializer = cp_parser_constant_initializer (parser);
17770 /* Otherwise, there is no initializer. */
17772 initializer = NULL_TREE;
17774 /* See if we are probably looking at a function
17775 definition. We are certainly not looking at a
17776 member-declarator. Calling `grokfield' has
17777 side-effects, so we must not do it unless we are sure
17778 that we are looking at a member-declarator. */
17779 if (cp_parser_token_starts_function_definition_p
17780 (cp_lexer_peek_token (parser->lexer)))
17782 /* The grammar does not allow a pure-specifier to be
17783 used when a member function is defined. (It is
17784 possible that this fact is an oversight in the
17785 standard, since a pure function may be defined
17786 outside of the class-specifier. */
17788 error_at (initializer_token_start->location,
17789 "pure-specifier on function-definition");
17790 decl = cp_parser_save_member_function_body (parser,
17794 /* If the member was not a friend, declare it here. */
17796 finish_member_declaration (decl);
17797 /* Peek at the next token. */
17798 token = cp_lexer_peek_token (parser->lexer);
17799 /* If the next token is a semicolon, consume it. */
17800 if (token->type == CPP_SEMICOLON)
17801 cp_lexer_consume_token (parser->lexer);
17805 if (declarator->kind == cdk_function)
17806 declarator->id_loc = token->location;
17807 /* Create the declaration. */
17808 decl = grokfield (declarator, &decl_specifiers,
17809 initializer, /*init_const_expr_p=*/true,
17814 /* Reset PREFIX_ATTRIBUTES. */
17815 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17816 attributes = TREE_CHAIN (attributes);
17818 TREE_CHAIN (attributes) = NULL_TREE;
17820 /* If there is any qualification still in effect, clear it
17821 now; we will be starting fresh with the next declarator. */
17822 parser->scope = NULL_TREE;
17823 parser->qualifying_scope = NULL_TREE;
17824 parser->object_scope = NULL_TREE;
17825 /* If it's a `,', then there are more declarators. */
17826 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17827 cp_lexer_consume_token (parser->lexer);
17828 /* If the next token isn't a `;', then we have a parse error. */
17829 else if (cp_lexer_next_token_is_not (parser->lexer,
17832 /* The next token might be a ways away from where the
17833 actual semicolon is missing. Find the previous token
17834 and use that for our error position. */
17835 cp_token *token = cp_lexer_previous_token (parser->lexer);
17836 error_at (token->location,
17837 "expected %<;%> at end of member declaration");
17839 /* Assume that the user meant to provide a semicolon. If
17840 we were to cp_parser_skip_to_end_of_statement, we might
17841 skip to a semicolon inside a member function definition
17842 and issue nonsensical error messages. */
17843 assume_semicolon = true;
17848 /* Add DECL to the list of members. */
17850 finish_member_declaration (decl);
17852 if (TREE_CODE (decl) == FUNCTION_DECL)
17853 cp_parser_save_default_args (parser, decl);
17856 if (assume_semicolon)
17861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17864 /* Parse a pure-specifier.
17869 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17870 Otherwise, ERROR_MARK_NODE is returned. */
17873 cp_parser_pure_specifier (cp_parser* parser)
17877 /* Look for the `=' token. */
17878 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17879 return error_mark_node;
17880 /* Look for the `0' token. */
17881 token = cp_lexer_peek_token (parser->lexer);
17883 if (token->type == CPP_EOF
17884 || token->type == CPP_PRAGMA_EOL)
17885 return error_mark_node;
17887 cp_lexer_consume_token (parser->lexer);
17889 /* Accept = default or = delete in c++0x mode. */
17890 if (token->keyword == RID_DEFAULT
17891 || token->keyword == RID_DELETE)
17893 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17894 return token->u.value;
17897 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17898 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17900 cp_parser_error (parser,
17901 "invalid pure specifier (only %<= 0%> is allowed)");
17902 cp_parser_skip_to_end_of_statement (parser);
17903 return error_mark_node;
17905 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17907 error_at (token->location, "templates may not be %<virtual%>");
17908 return error_mark_node;
17911 return integer_zero_node;
17914 /* Parse a constant-initializer.
17916 constant-initializer:
17917 = constant-expression
17919 Returns a representation of the constant-expression. */
17922 cp_parser_constant_initializer (cp_parser* parser)
17924 /* Look for the `=' token. */
17925 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17926 return error_mark_node;
17928 /* It is invalid to write:
17930 struct S { static const int i = { 7 }; };
17933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17935 cp_parser_error (parser,
17936 "a brace-enclosed initializer is not allowed here");
17937 /* Consume the opening brace. */
17938 cp_lexer_consume_token (parser->lexer);
17939 /* Skip the initializer. */
17940 cp_parser_skip_to_closing_brace (parser);
17941 /* Look for the trailing `}'. */
17942 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17944 return error_mark_node;
17947 return cp_parser_constant_expression (parser,
17948 /*allow_non_constant=*/false,
17952 /* Derived classes [gram.class.derived] */
17954 /* Parse a base-clause.
17957 : base-specifier-list
17959 base-specifier-list:
17960 base-specifier ... [opt]
17961 base-specifier-list , base-specifier ... [opt]
17963 Returns a TREE_LIST representing the base-classes, in the order in
17964 which they were declared. The representation of each node is as
17965 described by cp_parser_base_specifier.
17967 In the case that no bases are specified, this function will return
17968 NULL_TREE, not ERROR_MARK_NODE. */
17971 cp_parser_base_clause (cp_parser* parser)
17973 tree bases = NULL_TREE;
17975 /* Look for the `:' that begins the list. */
17976 cp_parser_require (parser, CPP_COLON, RT_COLON);
17978 /* Scan the base-specifier-list. */
17983 bool pack_expansion_p = false;
17985 /* Look for the base-specifier. */
17986 base = cp_parser_base_specifier (parser);
17987 /* Look for the (optional) ellipsis. */
17988 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17990 /* Consume the `...'. */
17991 cp_lexer_consume_token (parser->lexer);
17993 pack_expansion_p = true;
17996 /* Add BASE to the front of the list. */
17997 if (base != error_mark_node)
17999 if (pack_expansion_p)
18000 /* Make this a pack expansion type. */
18001 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18004 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18006 TREE_CHAIN (base) = bases;
18010 /* Peek at the next token. */
18011 token = cp_lexer_peek_token (parser->lexer);
18012 /* If it's not a comma, then the list is complete. */
18013 if (token->type != CPP_COMMA)
18015 /* Consume the `,'. */
18016 cp_lexer_consume_token (parser->lexer);
18019 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18020 base class had a qualified name. However, the next name that
18021 appears is certainly not qualified. */
18022 parser->scope = NULL_TREE;
18023 parser->qualifying_scope = NULL_TREE;
18024 parser->object_scope = NULL_TREE;
18026 return nreverse (bases);
18029 /* Parse a base-specifier.
18032 :: [opt] nested-name-specifier [opt] class-name
18033 virtual access-specifier [opt] :: [opt] nested-name-specifier
18035 access-specifier virtual [opt] :: [opt] nested-name-specifier
18038 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18039 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18040 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18041 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18044 cp_parser_base_specifier (cp_parser* parser)
18048 bool virtual_p = false;
18049 bool duplicate_virtual_error_issued_p = false;
18050 bool duplicate_access_error_issued_p = false;
18051 bool class_scope_p, template_p;
18052 tree access = access_default_node;
18055 /* Process the optional `virtual' and `access-specifier'. */
18058 /* Peek at the next token. */
18059 token = cp_lexer_peek_token (parser->lexer);
18060 /* Process `virtual'. */
18061 switch (token->keyword)
18064 /* If `virtual' appears more than once, issue an error. */
18065 if (virtual_p && !duplicate_virtual_error_issued_p)
18067 cp_parser_error (parser,
18068 "%<virtual%> specified more than once in base-specified");
18069 duplicate_virtual_error_issued_p = true;
18074 /* Consume the `virtual' token. */
18075 cp_lexer_consume_token (parser->lexer);
18080 case RID_PROTECTED:
18082 /* If more than one access specifier appears, issue an
18084 if (access != access_default_node
18085 && !duplicate_access_error_issued_p)
18087 cp_parser_error (parser,
18088 "more than one access specifier in base-specified");
18089 duplicate_access_error_issued_p = true;
18092 access = ridpointers[(int) token->keyword];
18094 /* Consume the access-specifier. */
18095 cp_lexer_consume_token (parser->lexer);
18104 /* It is not uncommon to see programs mechanically, erroneously, use
18105 the 'typename' keyword to denote (dependent) qualified types
18106 as base classes. */
18107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18109 token = cp_lexer_peek_token (parser->lexer);
18110 if (!processing_template_decl)
18111 error_at (token->location,
18112 "keyword %<typename%> not allowed outside of templates");
18114 error_at (token->location,
18115 "keyword %<typename%> not allowed in this context "
18116 "(the base class is implicitly a type)");
18117 cp_lexer_consume_token (parser->lexer);
18120 /* Look for the optional `::' operator. */
18121 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18122 /* Look for the nested-name-specifier. The simplest way to
18127 The keyword `typename' is not permitted in a base-specifier or
18128 mem-initializer; in these contexts a qualified name that
18129 depends on a template-parameter is implicitly assumed to be a
18132 is to pretend that we have seen the `typename' keyword at this
18134 cp_parser_nested_name_specifier_opt (parser,
18135 /*typename_keyword_p=*/true,
18136 /*check_dependency_p=*/true,
18138 /*is_declaration=*/true);
18139 /* If the base class is given by a qualified name, assume that names
18140 we see are type names or templates, as appropriate. */
18141 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18142 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18144 /* Finally, look for the class-name. */
18145 type = cp_parser_class_name (parser,
18149 /*check_dependency_p=*/true,
18150 /*class_head_p=*/false,
18151 /*is_declaration=*/true);
18153 if (type == error_mark_node)
18154 return error_mark_node;
18156 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18159 /* Exception handling [gram.exception] */
18161 /* Parse an (optional) exception-specification.
18163 exception-specification:
18164 throw ( type-id-list [opt] )
18166 Returns a TREE_LIST representing the exception-specification. The
18167 TREE_VALUE of each node is a type. */
18170 cp_parser_exception_specification_opt (cp_parser* parser)
18174 const char *saved_message;
18176 /* Peek at the next token. */
18177 token = cp_lexer_peek_token (parser->lexer);
18179 /* Is it a noexcept-specification? */
18180 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18183 cp_lexer_consume_token (parser->lexer);
18185 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18187 cp_lexer_consume_token (parser->lexer);
18189 /* Types may not be defined in an exception-specification. */
18190 saved_message = parser->type_definition_forbidden_message;
18191 parser->type_definition_forbidden_message
18192 = G_("types may not be defined in an exception-specification");
18194 expr = cp_parser_constant_expression (parser, false, NULL);
18196 /* Restore the saved message. */
18197 parser->type_definition_forbidden_message = saved_message;
18199 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18202 expr = boolean_true_node;
18204 return build_noexcept_spec (expr, tf_warning_or_error);
18207 /* If it's not `throw', then there's no exception-specification. */
18208 if (!cp_parser_is_keyword (token, RID_THROW))
18212 /* Enable this once a lot of code has transitioned to noexcept? */
18213 if (cxx_dialect == cxx0x && !in_system_header)
18214 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18215 "deprecated in C++0x; use %<noexcept%> instead");
18218 /* Consume the `throw'. */
18219 cp_lexer_consume_token (parser->lexer);
18221 /* Look for the `('. */
18222 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18224 /* Peek at the next token. */
18225 token = cp_lexer_peek_token (parser->lexer);
18226 /* If it's not a `)', then there is a type-id-list. */
18227 if (token->type != CPP_CLOSE_PAREN)
18229 /* Types may not be defined in an exception-specification. */
18230 saved_message = parser->type_definition_forbidden_message;
18231 parser->type_definition_forbidden_message
18232 = G_("types may not be defined in an exception-specification");
18233 /* Parse the type-id-list. */
18234 type_id_list = cp_parser_type_id_list (parser);
18235 /* Restore the saved message. */
18236 parser->type_definition_forbidden_message = saved_message;
18239 type_id_list = empty_except_spec;
18241 /* Look for the `)'. */
18242 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18244 return type_id_list;
18247 /* Parse an (optional) type-id-list.
18251 type-id-list , type-id ... [opt]
18253 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18254 in the order that the types were presented. */
18257 cp_parser_type_id_list (cp_parser* parser)
18259 tree types = NULL_TREE;
18266 /* Get the next type-id. */
18267 type = cp_parser_type_id (parser);
18268 /* Parse the optional ellipsis. */
18269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18271 /* Consume the `...'. */
18272 cp_lexer_consume_token (parser->lexer);
18274 /* Turn the type into a pack expansion expression. */
18275 type = make_pack_expansion (type);
18277 /* Add it to the list. */
18278 types = add_exception_specifier (types, type, /*complain=*/1);
18279 /* Peek at the next token. */
18280 token = cp_lexer_peek_token (parser->lexer);
18281 /* If it is not a `,', we are done. */
18282 if (token->type != CPP_COMMA)
18284 /* Consume the `,'. */
18285 cp_lexer_consume_token (parser->lexer);
18288 return nreverse (types);
18291 /* Parse a try-block.
18294 try compound-statement handler-seq */
18297 cp_parser_try_block (cp_parser* parser)
18301 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18302 try_block = begin_try_block ();
18303 cp_parser_compound_statement (parser, NULL, true);
18304 finish_try_block (try_block);
18305 cp_parser_handler_seq (parser);
18306 finish_handler_sequence (try_block);
18311 /* Parse a function-try-block.
18313 function-try-block:
18314 try ctor-initializer [opt] function-body handler-seq */
18317 cp_parser_function_try_block (cp_parser* parser)
18319 tree compound_stmt;
18321 bool ctor_initializer_p;
18323 /* Look for the `try' keyword. */
18324 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18326 /* Let the rest of the front end know where we are. */
18327 try_block = begin_function_try_block (&compound_stmt);
18328 /* Parse the function-body. */
18330 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18331 /* We're done with the `try' part. */
18332 finish_function_try_block (try_block);
18333 /* Parse the handlers. */
18334 cp_parser_handler_seq (parser);
18335 /* We're done with the handlers. */
18336 finish_function_handler_sequence (try_block, compound_stmt);
18338 return ctor_initializer_p;
18341 /* Parse a handler-seq.
18344 handler handler-seq [opt] */
18347 cp_parser_handler_seq (cp_parser* parser)
18353 /* Parse the handler. */
18354 cp_parser_handler (parser);
18355 /* Peek at the next token. */
18356 token = cp_lexer_peek_token (parser->lexer);
18357 /* If it's not `catch' then there are no more handlers. */
18358 if (!cp_parser_is_keyword (token, RID_CATCH))
18363 /* Parse a handler.
18366 catch ( exception-declaration ) compound-statement */
18369 cp_parser_handler (cp_parser* parser)
18374 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18375 handler = begin_handler ();
18376 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18377 declaration = cp_parser_exception_declaration (parser);
18378 finish_handler_parms (declaration, handler);
18379 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18380 cp_parser_compound_statement (parser, NULL, false);
18381 finish_handler (handler);
18384 /* Parse an exception-declaration.
18386 exception-declaration:
18387 type-specifier-seq declarator
18388 type-specifier-seq abstract-declarator
18392 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18393 ellipsis variant is used. */
18396 cp_parser_exception_declaration (cp_parser* parser)
18398 cp_decl_specifier_seq type_specifiers;
18399 cp_declarator *declarator;
18400 const char *saved_message;
18402 /* If it's an ellipsis, it's easy to handle. */
18403 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18405 /* Consume the `...' token. */
18406 cp_lexer_consume_token (parser->lexer);
18410 /* Types may not be defined in exception-declarations. */
18411 saved_message = parser->type_definition_forbidden_message;
18412 parser->type_definition_forbidden_message
18413 = G_("types may not be defined in exception-declarations");
18415 /* Parse the type-specifier-seq. */
18416 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18417 /*is_trailing_return=*/false,
18419 /* If it's a `)', then there is no declarator. */
18420 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18423 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18424 /*ctor_dtor_or_conv_p=*/NULL,
18425 /*parenthesized_p=*/NULL,
18426 /*member_p=*/false);
18428 /* Restore the saved message. */
18429 parser->type_definition_forbidden_message = saved_message;
18431 if (!type_specifiers.any_specifiers_p)
18432 return error_mark_node;
18434 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18437 /* Parse a throw-expression.
18440 throw assignment-expression [opt]
18442 Returns a THROW_EXPR representing the throw-expression. */
18445 cp_parser_throw_expression (cp_parser* parser)
18450 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18451 token = cp_lexer_peek_token (parser->lexer);
18452 /* Figure out whether or not there is an assignment-expression
18453 following the "throw" keyword. */
18454 if (token->type == CPP_COMMA
18455 || token->type == CPP_SEMICOLON
18456 || token->type == CPP_CLOSE_PAREN
18457 || token->type == CPP_CLOSE_SQUARE
18458 || token->type == CPP_CLOSE_BRACE
18459 || token->type == CPP_COLON)
18460 expression = NULL_TREE;
18462 expression = cp_parser_assignment_expression (parser,
18463 /*cast_p=*/false, NULL);
18465 return build_throw (expression);
18468 /* GNU Extensions */
18470 /* Parse an (optional) asm-specification.
18473 asm ( string-literal )
18475 If the asm-specification is present, returns a STRING_CST
18476 corresponding to the string-literal. Otherwise, returns
18480 cp_parser_asm_specification_opt (cp_parser* parser)
18483 tree asm_specification;
18485 /* Peek at the next token. */
18486 token = cp_lexer_peek_token (parser->lexer);
18487 /* If the next token isn't the `asm' keyword, then there's no
18488 asm-specification. */
18489 if (!cp_parser_is_keyword (token, RID_ASM))
18492 /* Consume the `asm' token. */
18493 cp_lexer_consume_token (parser->lexer);
18494 /* Look for the `('. */
18495 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18497 /* Look for the string-literal. */
18498 asm_specification = cp_parser_string_literal (parser, false, false);
18500 /* Look for the `)'. */
18501 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18503 return asm_specification;
18506 /* Parse an asm-operand-list.
18510 asm-operand-list , asm-operand
18513 string-literal ( expression )
18514 [ string-literal ] string-literal ( expression )
18516 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18517 each node is the expression. The TREE_PURPOSE is itself a
18518 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18519 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18520 is a STRING_CST for the string literal before the parenthesis. Returns
18521 ERROR_MARK_NODE if any of the operands are invalid. */
18524 cp_parser_asm_operand_list (cp_parser* parser)
18526 tree asm_operands = NULL_TREE;
18527 bool invalid_operands = false;
18531 tree string_literal;
18535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18537 /* Consume the `[' token. */
18538 cp_lexer_consume_token (parser->lexer);
18539 /* Read the operand name. */
18540 name = cp_parser_identifier (parser);
18541 if (name != error_mark_node)
18542 name = build_string (IDENTIFIER_LENGTH (name),
18543 IDENTIFIER_POINTER (name));
18544 /* Look for the closing `]'. */
18545 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18549 /* Look for the string-literal. */
18550 string_literal = cp_parser_string_literal (parser, false, false);
18552 /* Look for the `('. */
18553 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18554 /* Parse the expression. */
18555 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18556 /* Look for the `)'. */
18557 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18559 if (name == error_mark_node
18560 || string_literal == error_mark_node
18561 || expression == error_mark_node)
18562 invalid_operands = true;
18564 /* Add this operand to the list. */
18565 asm_operands = tree_cons (build_tree_list (name, string_literal),
18568 /* If the next token is not a `,', there are no more
18570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18572 /* Consume the `,'. */
18573 cp_lexer_consume_token (parser->lexer);
18576 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18579 /* Parse an asm-clobber-list.
18583 asm-clobber-list , string-literal
18585 Returns a TREE_LIST, indicating the clobbers in the order that they
18586 appeared. The TREE_VALUE of each node is a STRING_CST. */
18589 cp_parser_asm_clobber_list (cp_parser* parser)
18591 tree clobbers = NULL_TREE;
18595 tree string_literal;
18597 /* Look for the string literal. */
18598 string_literal = cp_parser_string_literal (parser, false, false);
18599 /* Add it to the list. */
18600 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18601 /* If the next token is not a `,', then the list is
18603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18605 /* Consume the `,' token. */
18606 cp_lexer_consume_token (parser->lexer);
18612 /* Parse an asm-label-list.
18616 asm-label-list , identifier
18618 Returns a TREE_LIST, indicating the labels in the order that they
18619 appeared. The TREE_VALUE of each node is a label. */
18622 cp_parser_asm_label_list (cp_parser* parser)
18624 tree labels = NULL_TREE;
18628 tree identifier, label, name;
18630 /* Look for the identifier. */
18631 identifier = cp_parser_identifier (parser);
18632 if (!error_operand_p (identifier))
18634 label = lookup_label (identifier);
18635 if (TREE_CODE (label) == LABEL_DECL)
18637 TREE_USED (label) = 1;
18638 check_goto (label);
18639 name = build_string (IDENTIFIER_LENGTH (identifier),
18640 IDENTIFIER_POINTER (identifier));
18641 labels = tree_cons (name, label, labels);
18644 /* If the next token is not a `,', then the list is
18646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18648 /* Consume the `,' token. */
18649 cp_lexer_consume_token (parser->lexer);
18652 return nreverse (labels);
18655 /* Parse an (optional) series of attributes.
18658 attributes attribute
18661 __attribute__ (( attribute-list [opt] ))
18663 The return value is as for cp_parser_attribute_list. */
18666 cp_parser_attributes_opt (cp_parser* parser)
18668 tree attributes = NULL_TREE;
18673 tree attribute_list;
18675 /* Peek at the next token. */
18676 token = cp_lexer_peek_token (parser->lexer);
18677 /* If it's not `__attribute__', then we're done. */
18678 if (token->keyword != RID_ATTRIBUTE)
18681 /* Consume the `__attribute__' keyword. */
18682 cp_lexer_consume_token (parser->lexer);
18683 /* Look for the two `(' tokens. */
18684 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18685 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18687 /* Peek at the next token. */
18688 token = cp_lexer_peek_token (parser->lexer);
18689 if (token->type != CPP_CLOSE_PAREN)
18690 /* Parse the attribute-list. */
18691 attribute_list = cp_parser_attribute_list (parser);
18693 /* If the next token is a `)', then there is no attribute
18695 attribute_list = NULL;
18697 /* Look for the two `)' tokens. */
18698 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18699 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18701 /* Add these new attributes to the list. */
18702 attributes = chainon (attributes, attribute_list);
18708 /* Parse an attribute-list.
18712 attribute-list , attribute
18716 identifier ( identifier )
18717 identifier ( identifier , expression-list )
18718 identifier ( expression-list )
18720 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18721 to an attribute. The TREE_PURPOSE of each node is the identifier
18722 indicating which attribute is in use. The TREE_VALUE represents
18723 the arguments, if any. */
18726 cp_parser_attribute_list (cp_parser* parser)
18728 tree attribute_list = NULL_TREE;
18729 bool save_translate_strings_p = parser->translate_strings_p;
18731 parser->translate_strings_p = false;
18738 /* Look for the identifier. We also allow keywords here; for
18739 example `__attribute__ ((const))' is legal. */
18740 token = cp_lexer_peek_token (parser->lexer);
18741 if (token->type == CPP_NAME
18742 || token->type == CPP_KEYWORD)
18744 tree arguments = NULL_TREE;
18746 /* Consume the token. */
18747 token = cp_lexer_consume_token (parser->lexer);
18749 /* Save away the identifier that indicates which attribute
18751 identifier = (token->type == CPP_KEYWORD)
18752 /* For keywords, use the canonical spelling, not the
18753 parsed identifier. */
18754 ? ridpointers[(int) token->keyword]
18757 attribute = build_tree_list (identifier, NULL_TREE);
18759 /* Peek at the next token. */
18760 token = cp_lexer_peek_token (parser->lexer);
18761 /* If it's an `(', then parse the attribute arguments. */
18762 if (token->type == CPP_OPEN_PAREN)
18765 int attr_flag = (attribute_takes_identifier_p (identifier)
18766 ? id_attr : normal_attr);
18767 vec = cp_parser_parenthesized_expression_list
18768 (parser, attr_flag, /*cast_p=*/false,
18769 /*allow_expansion_p=*/false,
18770 /*non_constant_p=*/NULL);
18772 arguments = error_mark_node;
18775 arguments = build_tree_list_vec (vec);
18776 release_tree_vector (vec);
18778 /* Save the arguments away. */
18779 TREE_VALUE (attribute) = arguments;
18782 if (arguments != error_mark_node)
18784 /* Add this attribute to the list. */
18785 TREE_CHAIN (attribute) = attribute_list;
18786 attribute_list = attribute;
18789 token = cp_lexer_peek_token (parser->lexer);
18791 /* Now, look for more attributes. If the next token isn't a
18792 `,', we're done. */
18793 if (token->type != CPP_COMMA)
18796 /* Consume the comma and keep going. */
18797 cp_lexer_consume_token (parser->lexer);
18799 parser->translate_strings_p = save_translate_strings_p;
18801 /* We built up the list in reverse order. */
18802 return nreverse (attribute_list);
18805 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18806 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18807 current value of the PEDANTIC flag, regardless of whether or not
18808 the `__extension__' keyword is present. The caller is responsible
18809 for restoring the value of the PEDANTIC flag. */
18812 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18814 /* Save the old value of the PEDANTIC flag. */
18815 *saved_pedantic = pedantic;
18817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18819 /* Consume the `__extension__' token. */
18820 cp_lexer_consume_token (parser->lexer);
18821 /* We're not being pedantic while the `__extension__' keyword is
18831 /* Parse a label declaration.
18834 __label__ label-declarator-seq ;
18836 label-declarator-seq:
18837 identifier , label-declarator-seq
18841 cp_parser_label_declaration (cp_parser* parser)
18843 /* Look for the `__label__' keyword. */
18844 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18850 /* Look for an identifier. */
18851 identifier = cp_parser_identifier (parser);
18852 /* If we failed, stop. */
18853 if (identifier == error_mark_node)
18855 /* Declare it as a label. */
18856 finish_label_decl (identifier);
18857 /* If the next token is a `;', stop. */
18858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18860 /* Look for the `,' separating the label declarations. */
18861 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18864 /* Look for the final `;'. */
18865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18868 /* Support Functions */
18870 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18871 NAME should have one of the representations used for an
18872 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18873 is returned. If PARSER->SCOPE is a dependent type, then a
18874 SCOPE_REF is returned.
18876 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18877 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18878 was formed. Abstractly, such entities should not be passed to this
18879 function, because they do not need to be looked up, but it is
18880 simpler to check for this special case here, rather than at the
18883 In cases not explicitly covered above, this function returns a
18884 DECL, OVERLOAD, or baselink representing the result of the lookup.
18885 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18888 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18889 (e.g., "struct") that was used. In that case bindings that do not
18890 refer to types are ignored.
18892 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18895 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18898 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18901 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18902 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18903 NULL_TREE otherwise. */
18906 cp_parser_lookup_name (cp_parser *parser, tree name,
18907 enum tag_types tag_type,
18910 bool check_dependency,
18911 tree *ambiguous_decls,
18912 location_t name_location)
18916 tree object_type = parser->context->object_type;
18918 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18919 flags |= LOOKUP_COMPLAIN;
18921 /* Assume that the lookup will be unambiguous. */
18922 if (ambiguous_decls)
18923 *ambiguous_decls = NULL_TREE;
18925 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18926 no longer valid. Note that if we are parsing tentatively, and
18927 the parse fails, OBJECT_TYPE will be automatically restored. */
18928 parser->context->object_type = NULL_TREE;
18930 if (name == error_mark_node)
18931 return error_mark_node;
18933 /* A template-id has already been resolved; there is no lookup to
18935 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18937 if (BASELINK_P (name))
18939 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18940 == TEMPLATE_ID_EXPR);
18944 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18945 it should already have been checked to make sure that the name
18946 used matches the type being destroyed. */
18947 if (TREE_CODE (name) == BIT_NOT_EXPR)
18951 /* Figure out to which type this destructor applies. */
18953 type = parser->scope;
18954 else if (object_type)
18955 type = object_type;
18957 type = current_class_type;
18958 /* If that's not a class type, there is no destructor. */
18959 if (!type || !CLASS_TYPE_P (type))
18960 return error_mark_node;
18961 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18962 lazily_declare_fn (sfk_destructor, type);
18963 if (!CLASSTYPE_DESTRUCTORS (type))
18964 return error_mark_node;
18965 /* If it was a class type, return the destructor. */
18966 return CLASSTYPE_DESTRUCTORS (type);
18969 /* By this point, the NAME should be an ordinary identifier. If
18970 the id-expression was a qualified name, the qualifying scope is
18971 stored in PARSER->SCOPE at this point. */
18972 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18974 /* Perform the lookup. */
18979 if (parser->scope == error_mark_node)
18980 return error_mark_node;
18982 /* If the SCOPE is dependent, the lookup must be deferred until
18983 the template is instantiated -- unless we are explicitly
18984 looking up names in uninstantiated templates. Even then, we
18985 cannot look up the name if the scope is not a class type; it
18986 might, for example, be a template type parameter. */
18987 dependent_p = (TYPE_P (parser->scope)
18988 && dependent_scope_p (parser->scope));
18989 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18991 /* Defer lookup. */
18992 decl = error_mark_node;
18995 tree pushed_scope = NULL_TREE;
18997 /* If PARSER->SCOPE is a dependent type, then it must be a
18998 class type, and we must not be checking dependencies;
18999 otherwise, we would have processed this lookup above. So
19000 that PARSER->SCOPE is not considered a dependent base by
19001 lookup_member, we must enter the scope here. */
19003 pushed_scope = push_scope (parser->scope);
19005 /* If the PARSER->SCOPE is a template specialization, it
19006 may be instantiated during name lookup. In that case,
19007 errors may be issued. Even if we rollback the current
19008 tentative parse, those errors are valid. */
19009 decl = lookup_qualified_name (parser->scope, name,
19010 tag_type != none_type,
19011 /*complain=*/true);
19013 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19014 lookup result and the nested-name-specifier nominates a class C:
19015 * if the name specified after the nested-name-specifier, when
19016 looked up in C, is the injected-class-name of C (Clause 9), or
19017 * if the name specified after the nested-name-specifier is the
19018 same as the identifier or the simple-template-id's template-
19019 name in the last component of the nested-name-specifier,
19020 the name is instead considered to name the constructor of
19021 class C. [ Note: for example, the constructor is not an
19022 acceptable lookup result in an elaborated-type-specifier so
19023 the constructor would not be used in place of the
19024 injected-class-name. --end note ] Such a constructor name
19025 shall be used only in the declarator-id of a declaration that
19026 names a constructor or in a using-declaration. */
19027 if (tag_type == none_type
19028 && DECL_SELF_REFERENCE_P (decl)
19029 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19030 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19031 tag_type != none_type,
19032 /*complain=*/true);
19034 /* If we have a single function from a using decl, pull it out. */
19035 if (TREE_CODE (decl) == OVERLOAD
19036 && !really_overloaded_fn (decl))
19037 decl = OVL_FUNCTION (decl);
19040 pop_scope (pushed_scope);
19043 /* If the scope is a dependent type and either we deferred lookup or
19044 we did lookup but didn't find the name, rememeber the name. */
19045 if (decl == error_mark_node && TYPE_P (parser->scope)
19046 && dependent_type_p (parser->scope))
19052 /* The resolution to Core Issue 180 says that `struct
19053 A::B' should be considered a type-name, even if `A'
19055 type = make_typename_type (parser->scope, name, tag_type,
19056 /*complain=*/tf_error);
19057 decl = TYPE_NAME (type);
19059 else if (is_template
19060 && (cp_parser_next_token_ends_template_argument_p (parser)
19061 || cp_lexer_next_token_is (parser->lexer,
19063 decl = make_unbound_class_template (parser->scope,
19065 /*complain=*/tf_error);
19067 decl = build_qualified_name (/*type=*/NULL_TREE,
19068 parser->scope, name,
19071 parser->qualifying_scope = parser->scope;
19072 parser->object_scope = NULL_TREE;
19074 else if (object_type)
19076 tree object_decl = NULL_TREE;
19077 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19078 OBJECT_TYPE is not a class. */
19079 if (CLASS_TYPE_P (object_type))
19080 /* If the OBJECT_TYPE is a template specialization, it may
19081 be instantiated during name lookup. In that case, errors
19082 may be issued. Even if we rollback the current tentative
19083 parse, those errors are valid. */
19084 object_decl = lookup_member (object_type,
19087 tag_type != none_type);
19088 /* Look it up in the enclosing context, too. */
19089 decl = lookup_name_real (name, tag_type != none_type,
19091 /*block_p=*/true, is_namespace, flags);
19092 parser->object_scope = object_type;
19093 parser->qualifying_scope = NULL_TREE;
19095 decl = object_decl;
19099 decl = lookup_name_real (name, tag_type != none_type,
19101 /*block_p=*/true, is_namespace, flags);
19102 parser->qualifying_scope = NULL_TREE;
19103 parser->object_scope = NULL_TREE;
19106 /* If the lookup failed, let our caller know. */
19107 if (!decl || decl == error_mark_node)
19108 return error_mark_node;
19110 /* Pull out the template from an injected-class-name (or multiple). */
19112 decl = maybe_get_template_decl_from_type_decl (decl);
19114 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19115 if (TREE_CODE (decl) == TREE_LIST)
19117 if (ambiguous_decls)
19118 *ambiguous_decls = decl;
19119 /* The error message we have to print is too complicated for
19120 cp_parser_error, so we incorporate its actions directly. */
19121 if (!cp_parser_simulate_error (parser))
19123 error_at (name_location, "reference to %qD is ambiguous",
19125 print_candidates (decl);
19127 return error_mark_node;
19130 gcc_assert (DECL_P (decl)
19131 || TREE_CODE (decl) == OVERLOAD
19132 || TREE_CODE (decl) == SCOPE_REF
19133 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19134 || BASELINK_P (decl));
19136 /* If we have resolved the name of a member declaration, check to
19137 see if the declaration is accessible. When the name resolves to
19138 set of overloaded functions, accessibility is checked when
19139 overload resolution is done.
19141 During an explicit instantiation, access is not checked at all,
19142 as per [temp.explicit]. */
19144 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19149 /* Like cp_parser_lookup_name, but for use in the typical case where
19150 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19151 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19154 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19156 return cp_parser_lookup_name (parser, name,
19158 /*is_template=*/false,
19159 /*is_namespace=*/false,
19160 /*check_dependency=*/true,
19161 /*ambiguous_decls=*/NULL,
19165 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19166 the current context, return the TYPE_DECL. If TAG_NAME_P is
19167 true, the DECL indicates the class being defined in a class-head,
19168 or declared in an elaborated-type-specifier.
19170 Otherwise, return DECL. */
19173 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19175 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19176 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19179 template <typename T> struct B;
19182 template <typename T> struct A::B {};
19184 Similarly, in an elaborated-type-specifier:
19186 namespace N { struct X{}; }
19189 template <typename T> friend struct N::X;
19192 However, if the DECL refers to a class type, and we are in
19193 the scope of the class, then the name lookup automatically
19194 finds the TYPE_DECL created by build_self_reference rather
19195 than a TEMPLATE_DECL. For example, in:
19197 template <class T> struct S {
19201 there is no need to handle such case. */
19203 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19204 return DECL_TEMPLATE_RESULT (decl);
19209 /* If too many, or too few, template-parameter lists apply to the
19210 declarator, issue an error message. Returns TRUE if all went well,
19211 and FALSE otherwise. */
19214 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19215 cp_declarator *declarator,
19216 location_t declarator_location)
19218 unsigned num_templates;
19220 /* We haven't seen any classes that involve template parameters yet. */
19223 switch (declarator->kind)
19226 if (declarator->u.id.qualifying_scope)
19230 scope = declarator->u.id.qualifying_scope;
19232 while (scope && CLASS_TYPE_P (scope))
19234 /* You're supposed to have one `template <...>'
19235 for every template class, but you don't need one
19236 for a full specialization. For example:
19238 template <class T> struct S{};
19239 template <> struct S<int> { void f(); };
19240 void S<int>::f () {}
19242 is correct; there shouldn't be a `template <>' for
19243 the definition of `S<int>::f'. */
19244 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19245 /* If SCOPE does not have template information of any
19246 kind, then it is not a template, nor is it nested
19247 within a template. */
19249 if (explicit_class_specialization_p (scope))
19251 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19254 scope = TYPE_CONTEXT (scope);
19257 else if (TREE_CODE (declarator->u.id.unqualified_name)
19258 == TEMPLATE_ID_EXPR)
19259 /* If the DECLARATOR has the form `X<y>' then it uses one
19260 additional level of template parameters. */
19263 return cp_parser_check_template_parameters
19264 (parser, num_templates, declarator_location, declarator);
19270 case cdk_reference:
19272 return (cp_parser_check_declarator_template_parameters
19273 (parser, declarator->declarator, declarator_location));
19279 gcc_unreachable ();
19284 /* NUM_TEMPLATES were used in the current declaration. If that is
19285 invalid, return FALSE and issue an error messages. Otherwise,
19286 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19287 declarator and we can print more accurate diagnostics. */
19290 cp_parser_check_template_parameters (cp_parser* parser,
19291 unsigned num_templates,
19292 location_t location,
19293 cp_declarator *declarator)
19295 /* If there are the same number of template classes and parameter
19296 lists, that's OK. */
19297 if (parser->num_template_parameter_lists == num_templates)
19299 /* If there are more, but only one more, then we are referring to a
19300 member template. That's OK too. */
19301 if (parser->num_template_parameter_lists == num_templates + 1)
19303 /* If there are more template classes than parameter lists, we have
19306 template <class T> void S<T>::R<T>::f (); */
19307 if (parser->num_template_parameter_lists < num_templates)
19309 if (declarator && !current_function_decl)
19310 error_at (location, "specializing member %<%T::%E%> "
19311 "requires %<template<>%> syntax",
19312 declarator->u.id.qualifying_scope,
19313 declarator->u.id.unqualified_name);
19314 else if (declarator)
19315 error_at (location, "invalid declaration of %<%T::%E%>",
19316 declarator->u.id.qualifying_scope,
19317 declarator->u.id.unqualified_name);
19319 error_at (location, "too few template-parameter-lists");
19322 /* Otherwise, there are too many template parameter lists. We have
19325 template <class T> template <class U> void S::f(); */
19326 error_at (location, "too many template-parameter-lists");
19330 /* Parse an optional `::' token indicating that the following name is
19331 from the global namespace. If so, PARSER->SCOPE is set to the
19332 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19333 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19334 Returns the new value of PARSER->SCOPE, if the `::' token is
19335 present, and NULL_TREE otherwise. */
19338 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19342 /* Peek at the next token. */
19343 token = cp_lexer_peek_token (parser->lexer);
19344 /* If we're looking at a `::' token then we're starting from the
19345 global namespace, not our current location. */
19346 if (token->type == CPP_SCOPE)
19348 /* Consume the `::' token. */
19349 cp_lexer_consume_token (parser->lexer);
19350 /* Set the SCOPE so that we know where to start the lookup. */
19351 parser->scope = global_namespace;
19352 parser->qualifying_scope = global_namespace;
19353 parser->object_scope = NULL_TREE;
19355 return parser->scope;
19357 else if (!current_scope_valid_p)
19359 parser->scope = NULL_TREE;
19360 parser->qualifying_scope = NULL_TREE;
19361 parser->object_scope = NULL_TREE;
19367 /* Returns TRUE if the upcoming token sequence is the start of a
19368 constructor declarator. If FRIEND_P is true, the declarator is
19369 preceded by the `friend' specifier. */
19372 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19374 bool constructor_p;
19375 tree nested_name_specifier;
19376 cp_token *next_token;
19378 /* The common case is that this is not a constructor declarator, so
19379 try to avoid doing lots of work if at all possible. It's not
19380 valid declare a constructor at function scope. */
19381 if (parser->in_function_body)
19383 /* And only certain tokens can begin a constructor declarator. */
19384 next_token = cp_lexer_peek_token (parser->lexer);
19385 if (next_token->type != CPP_NAME
19386 && next_token->type != CPP_SCOPE
19387 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19388 && next_token->type != CPP_TEMPLATE_ID)
19391 /* Parse tentatively; we are going to roll back all of the tokens
19393 cp_parser_parse_tentatively (parser);
19394 /* Assume that we are looking at a constructor declarator. */
19395 constructor_p = true;
19397 /* Look for the optional `::' operator. */
19398 cp_parser_global_scope_opt (parser,
19399 /*current_scope_valid_p=*/false);
19400 /* Look for the nested-name-specifier. */
19401 nested_name_specifier
19402 = (cp_parser_nested_name_specifier_opt (parser,
19403 /*typename_keyword_p=*/false,
19404 /*check_dependency_p=*/false,
19406 /*is_declaration=*/false));
19407 /* Outside of a class-specifier, there must be a
19408 nested-name-specifier. */
19409 if (!nested_name_specifier &&
19410 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19412 constructor_p = false;
19413 else if (nested_name_specifier == error_mark_node)
19414 constructor_p = false;
19416 /* If we have a class scope, this is easy; DR 147 says that S::S always
19417 names the constructor, and no other qualified name could. */
19418 if (constructor_p && nested_name_specifier
19419 && TYPE_P (nested_name_specifier))
19421 tree id = cp_parser_unqualified_id (parser,
19422 /*template_keyword_p=*/false,
19423 /*check_dependency_p=*/false,
19424 /*declarator_p=*/true,
19425 /*optional_p=*/false);
19426 if (is_overloaded_fn (id))
19427 id = DECL_NAME (get_first_fn (id));
19428 if (!constructor_name_p (id, nested_name_specifier))
19429 constructor_p = false;
19431 /* If we still think that this might be a constructor-declarator,
19432 look for a class-name. */
19433 else if (constructor_p)
19437 template <typename T> struct S {
19441 we must recognize that the nested `S' names a class. */
19443 type_decl = cp_parser_class_name (parser,
19444 /*typename_keyword_p=*/false,
19445 /*template_keyword_p=*/false,
19447 /*check_dependency_p=*/false,
19448 /*class_head_p=*/false,
19449 /*is_declaration=*/false);
19450 /* If there was no class-name, then this is not a constructor. */
19451 constructor_p = !cp_parser_error_occurred (parser);
19453 /* If we're still considering a constructor, we have to see a `(',
19454 to begin the parameter-declaration-clause, followed by either a
19455 `)', an `...', or a decl-specifier. We need to check for a
19456 type-specifier to avoid being fooled into thinking that:
19460 is a constructor. (It is actually a function named `f' that
19461 takes one parameter (of type `int') and returns a value of type
19464 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19465 constructor_p = false;
19468 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19469 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19470 /* A parameter declaration begins with a decl-specifier,
19471 which is either the "attribute" keyword, a storage class
19472 specifier, or (usually) a type-specifier. */
19473 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19476 tree pushed_scope = NULL_TREE;
19477 unsigned saved_num_template_parameter_lists;
19479 /* Names appearing in the type-specifier should be looked up
19480 in the scope of the class. */
19481 if (current_class_type)
19485 type = TREE_TYPE (type_decl);
19486 if (TREE_CODE (type) == TYPENAME_TYPE)
19488 type = resolve_typename_type (type,
19489 /*only_current_p=*/false);
19490 if (TREE_CODE (type) == TYPENAME_TYPE)
19492 cp_parser_abort_tentative_parse (parser);
19496 pushed_scope = push_scope (type);
19499 /* Inside the constructor parameter list, surrounding
19500 template-parameter-lists do not apply. */
19501 saved_num_template_parameter_lists
19502 = parser->num_template_parameter_lists;
19503 parser->num_template_parameter_lists = 0;
19505 /* Look for the type-specifier. */
19506 cp_parser_type_specifier (parser,
19507 CP_PARSER_FLAGS_NONE,
19508 /*decl_specs=*/NULL,
19509 /*is_declarator=*/true,
19510 /*declares_class_or_enum=*/NULL,
19511 /*is_cv_qualifier=*/NULL);
19513 parser->num_template_parameter_lists
19514 = saved_num_template_parameter_lists;
19516 /* Leave the scope of the class. */
19518 pop_scope (pushed_scope);
19520 constructor_p = !cp_parser_error_occurred (parser);
19524 /* We did not really want to consume any tokens. */
19525 cp_parser_abort_tentative_parse (parser);
19527 return constructor_p;
19530 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19531 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19532 they must be performed once we are in the scope of the function.
19534 Returns the function defined. */
19537 cp_parser_function_definition_from_specifiers_and_declarator
19538 (cp_parser* parser,
19539 cp_decl_specifier_seq *decl_specifiers,
19541 const cp_declarator *declarator)
19546 /* Begin the function-definition. */
19547 success_p = start_function (decl_specifiers, declarator, attributes);
19549 /* The things we're about to see are not directly qualified by any
19550 template headers we've seen thus far. */
19551 reset_specialization ();
19553 /* If there were names looked up in the decl-specifier-seq that we
19554 did not check, check them now. We must wait until we are in the
19555 scope of the function to perform the checks, since the function
19556 might be a friend. */
19557 perform_deferred_access_checks ();
19561 /* Skip the entire function. */
19562 cp_parser_skip_to_end_of_block_or_statement (parser);
19563 fn = error_mark_node;
19565 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19567 /* Seen already, skip it. An error message has already been output. */
19568 cp_parser_skip_to_end_of_block_or_statement (parser);
19569 fn = current_function_decl;
19570 current_function_decl = NULL_TREE;
19571 /* If this is a function from a class, pop the nested class. */
19572 if (current_class_name)
19573 pop_nested_class ();
19576 fn = cp_parser_function_definition_after_declarator (parser,
19577 /*inline_p=*/false);
19582 /* Parse the part of a function-definition that follows the
19583 declarator. INLINE_P is TRUE iff this function is an inline
19584 function defined within a class-specifier.
19586 Returns the function defined. */
19589 cp_parser_function_definition_after_declarator (cp_parser* parser,
19593 bool ctor_initializer_p = false;
19594 bool saved_in_unbraced_linkage_specification_p;
19595 bool saved_in_function_body;
19596 unsigned saved_num_template_parameter_lists;
19599 saved_in_function_body = parser->in_function_body;
19600 parser->in_function_body = true;
19601 /* If the next token is `return', then the code may be trying to
19602 make use of the "named return value" extension that G++ used to
19604 token = cp_lexer_peek_token (parser->lexer);
19605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19607 /* Consume the `return' keyword. */
19608 cp_lexer_consume_token (parser->lexer);
19609 /* Look for the identifier that indicates what value is to be
19611 cp_parser_identifier (parser);
19612 /* Issue an error message. */
19613 error_at (token->location,
19614 "named return values are no longer supported");
19615 /* Skip tokens until we reach the start of the function body. */
19618 cp_token *token = cp_lexer_peek_token (parser->lexer);
19619 if (token->type == CPP_OPEN_BRACE
19620 || token->type == CPP_EOF
19621 || token->type == CPP_PRAGMA_EOL)
19623 cp_lexer_consume_token (parser->lexer);
19626 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19627 anything declared inside `f'. */
19628 saved_in_unbraced_linkage_specification_p
19629 = parser->in_unbraced_linkage_specification_p;
19630 parser->in_unbraced_linkage_specification_p = false;
19631 /* Inside the function, surrounding template-parameter-lists do not
19633 saved_num_template_parameter_lists
19634 = parser->num_template_parameter_lists;
19635 parser->num_template_parameter_lists = 0;
19637 start_lambda_scope (current_function_decl);
19639 /* If the next token is `try', then we are looking at a
19640 function-try-block. */
19641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19642 ctor_initializer_p = cp_parser_function_try_block (parser);
19643 /* A function-try-block includes the function-body, so we only do
19644 this next part if we're not processing a function-try-block. */
19647 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19649 finish_lambda_scope ();
19651 /* Finish the function. */
19652 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19653 (inline_p ? 2 : 0));
19654 /* Generate code for it, if necessary. */
19655 expand_or_defer_fn (fn);
19656 /* Restore the saved values. */
19657 parser->in_unbraced_linkage_specification_p
19658 = saved_in_unbraced_linkage_specification_p;
19659 parser->num_template_parameter_lists
19660 = saved_num_template_parameter_lists;
19661 parser->in_function_body = saved_in_function_body;
19666 /* Parse a template-declaration, assuming that the `export' (and
19667 `extern') keywords, if present, has already been scanned. MEMBER_P
19668 is as for cp_parser_template_declaration. */
19671 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19673 tree decl = NULL_TREE;
19674 VEC (deferred_access_check,gc) *checks;
19675 tree parameter_list;
19676 bool friend_p = false;
19677 bool need_lang_pop;
19680 /* Look for the `template' keyword. */
19681 token = cp_lexer_peek_token (parser->lexer);
19682 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19686 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19688 if (at_class_scope_p () && current_function_decl)
19690 /* 14.5.2.2 [temp.mem]
19692 A local class shall not have member templates. */
19693 error_at (token->location,
19694 "invalid declaration of member template in local class");
19695 cp_parser_skip_to_end_of_block_or_statement (parser);
19700 A template ... shall not have C linkage. */
19701 if (current_lang_name == lang_name_c)
19703 error_at (token->location, "template with C linkage");
19704 /* Give it C++ linkage to avoid confusing other parts of the
19706 push_lang_context (lang_name_cplusplus);
19707 need_lang_pop = true;
19710 need_lang_pop = false;
19712 /* We cannot perform access checks on the template parameter
19713 declarations until we know what is being declared, just as we
19714 cannot check the decl-specifier list. */
19715 push_deferring_access_checks (dk_deferred);
19717 /* If the next token is `>', then we have an invalid
19718 specialization. Rather than complain about an invalid template
19719 parameter, issue an error message here. */
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19722 cp_parser_error (parser, "invalid explicit specialization");
19723 begin_specialization ();
19724 parameter_list = NULL_TREE;
19727 /* Parse the template parameters. */
19728 parameter_list = cp_parser_template_parameter_list (parser);
19730 /* Get the deferred access checks from the parameter list. These
19731 will be checked once we know what is being declared, as for a
19732 member template the checks must be performed in the scope of the
19733 class containing the member. */
19734 checks = get_deferred_access_checks ();
19736 /* Look for the `>'. */
19737 cp_parser_skip_to_end_of_template_parameter_list (parser);
19738 /* We just processed one more parameter list. */
19739 ++parser->num_template_parameter_lists;
19740 /* If the next token is `template', there are more template
19742 if (cp_lexer_next_token_is_keyword (parser->lexer,
19744 cp_parser_template_declaration_after_export (parser, member_p);
19747 /* There are no access checks when parsing a template, as we do not
19748 know if a specialization will be a friend. */
19749 push_deferring_access_checks (dk_no_check);
19750 token = cp_lexer_peek_token (parser->lexer);
19751 decl = cp_parser_single_declaration (parser,
19754 /*explicit_specialization_p=*/false,
19756 pop_deferring_access_checks ();
19758 /* If this is a member template declaration, let the front
19760 if (member_p && !friend_p && decl)
19762 if (TREE_CODE (decl) == TYPE_DECL)
19763 cp_parser_check_access_in_redeclaration (decl, token->location);
19765 decl = finish_member_template_decl (decl);
19767 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19768 make_friend_class (current_class_type, TREE_TYPE (decl),
19769 /*complain=*/true);
19771 /* We are done with the current parameter list. */
19772 --parser->num_template_parameter_lists;
19774 pop_deferring_access_checks ();
19777 finish_template_decl (parameter_list);
19779 /* Register member declarations. */
19780 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19781 finish_member_declaration (decl);
19782 /* For the erroneous case of a template with C linkage, we pushed an
19783 implicit C++ linkage scope; exit that scope now. */
19785 pop_lang_context ();
19786 /* If DECL is a function template, we must return to parse it later.
19787 (Even though there is no definition, there might be default
19788 arguments that need handling.) */
19789 if (member_p && decl
19790 && (TREE_CODE (decl) == FUNCTION_DECL
19791 || DECL_FUNCTION_TEMPLATE_P (decl)))
19792 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19795 /* Perform the deferred access checks from a template-parameter-list.
19796 CHECKS is a TREE_LIST of access checks, as returned by
19797 get_deferred_access_checks. */
19800 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19802 ++processing_template_parmlist;
19803 perform_access_checks (checks);
19804 --processing_template_parmlist;
19807 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19808 `function-definition' sequence. MEMBER_P is true, this declaration
19809 appears in a class scope.
19811 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19812 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19815 cp_parser_single_declaration (cp_parser* parser,
19816 VEC (deferred_access_check,gc)* checks,
19818 bool explicit_specialization_p,
19821 int declares_class_or_enum;
19822 tree decl = NULL_TREE;
19823 cp_decl_specifier_seq decl_specifiers;
19824 bool function_definition_p = false;
19825 cp_token *decl_spec_token_start;
19827 /* This function is only used when processing a template
19829 gcc_assert (innermost_scope_kind () == sk_template_parms
19830 || innermost_scope_kind () == sk_template_spec);
19832 /* Defer access checks until we know what is being declared. */
19833 push_deferring_access_checks (dk_deferred);
19835 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19837 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19838 cp_parser_decl_specifier_seq (parser,
19839 CP_PARSER_FLAGS_OPTIONAL,
19841 &declares_class_or_enum);
19843 *friend_p = cp_parser_friend_p (&decl_specifiers);
19845 /* There are no template typedefs. */
19846 if (decl_specifiers.specs[(int) ds_typedef])
19848 error_at (decl_spec_token_start->location,
19849 "template declaration of %<typedef%>");
19850 decl = error_mark_node;
19853 /* Gather up the access checks that occurred the
19854 decl-specifier-seq. */
19855 stop_deferring_access_checks ();
19857 /* Check for the declaration of a template class. */
19858 if (declares_class_or_enum)
19860 if (cp_parser_declares_only_class_p (parser))
19862 decl = shadow_tag (&decl_specifiers);
19867 friend template <typename T> struct A<T>::B;
19870 A<T>::B will be represented by a TYPENAME_TYPE, and
19871 therefore not recognized by shadow_tag. */
19872 if (friend_p && *friend_p
19874 && decl_specifiers.type
19875 && TYPE_P (decl_specifiers.type))
19876 decl = decl_specifiers.type;
19878 if (decl && decl != error_mark_node)
19879 decl = TYPE_NAME (decl);
19881 decl = error_mark_node;
19883 /* Perform access checks for template parameters. */
19884 cp_parser_perform_template_parameter_access_checks (checks);
19888 /* Complain about missing 'typename' or other invalid type names. */
19889 if (!decl_specifiers.any_type_specifiers_p)
19890 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19892 /* If it's not a template class, try for a template function. If
19893 the next token is a `;', then this declaration does not declare
19894 anything. But, if there were errors in the decl-specifiers, then
19895 the error might well have come from an attempted class-specifier.
19896 In that case, there's no need to warn about a missing declarator. */
19898 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19899 || decl_specifiers.type != error_mark_node))
19901 decl = cp_parser_init_declarator (parser,
19904 /*function_definition_allowed_p=*/true,
19906 declares_class_or_enum,
19907 &function_definition_p);
19909 /* 7.1.1-1 [dcl.stc]
19911 A storage-class-specifier shall not be specified in an explicit
19912 specialization... */
19914 && explicit_specialization_p
19915 && decl_specifiers.storage_class != sc_none)
19917 error_at (decl_spec_token_start->location,
19918 "explicit template specialization cannot have a storage class");
19919 decl = error_mark_node;
19923 pop_deferring_access_checks ();
19925 /* Clear any current qualification; whatever comes next is the start
19926 of something new. */
19927 parser->scope = NULL_TREE;
19928 parser->qualifying_scope = NULL_TREE;
19929 parser->object_scope = NULL_TREE;
19930 /* Look for a trailing `;' after the declaration. */
19931 if (!function_definition_p
19932 && (decl == error_mark_node
19933 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19934 cp_parser_skip_to_end_of_block_or_statement (parser);
19939 /* Parse a cast-expression that is not the operand of a unary "&". */
19942 cp_parser_simple_cast_expression (cp_parser *parser)
19944 return cp_parser_cast_expression (parser, /*address_p=*/false,
19945 /*cast_p=*/false, NULL);
19948 /* Parse a functional cast to TYPE. Returns an expression
19949 representing the cast. */
19952 cp_parser_functional_cast (cp_parser* parser, tree type)
19955 tree expression_list;
19959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19961 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19962 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19963 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19964 if (TREE_CODE (type) == TYPE_DECL)
19965 type = TREE_TYPE (type);
19966 return finish_compound_literal (type, expression_list);
19970 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19972 /*allow_expansion_p=*/true,
19973 /*non_constant_p=*/NULL);
19975 expression_list = error_mark_node;
19978 expression_list = build_tree_list_vec (vec);
19979 release_tree_vector (vec);
19982 cast = build_functional_cast (type, expression_list,
19983 tf_warning_or_error);
19984 /* [expr.const]/1: In an integral constant expression "only type
19985 conversions to integral or enumeration type can be used". */
19986 if (TREE_CODE (type) == TYPE_DECL)
19987 type = TREE_TYPE (type);
19988 if (cast != error_mark_node
19989 && !cast_valid_in_integral_constant_expression_p (type)
19990 && cp_parser_non_integral_constant_expression (parser,
19992 return error_mark_node;
19996 /* Save the tokens that make up the body of a member function defined
19997 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19998 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19999 specifiers applied to the declaration. Returns the FUNCTION_DECL
20000 for the member function. */
20003 cp_parser_save_member_function_body (cp_parser* parser,
20004 cp_decl_specifier_seq *decl_specifiers,
20005 cp_declarator *declarator,
20012 /* Create the FUNCTION_DECL. */
20013 fn = grokmethod (decl_specifiers, declarator, attributes);
20014 /* If something went badly wrong, bail out now. */
20015 if (fn == error_mark_node)
20017 /* If there's a function-body, skip it. */
20018 if (cp_parser_token_starts_function_definition_p
20019 (cp_lexer_peek_token (parser->lexer)))
20020 cp_parser_skip_to_end_of_block_or_statement (parser);
20021 return error_mark_node;
20024 /* Remember it, if there default args to post process. */
20025 cp_parser_save_default_args (parser, fn);
20027 /* Save away the tokens that make up the body of the
20029 first = parser->lexer->next_token;
20030 /* We can have braced-init-list mem-initializers before the fn body. */
20031 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20033 cp_lexer_consume_token (parser->lexer);
20034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20035 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20037 /* cache_group will stop after an un-nested { } pair, too. */
20038 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20041 /* variadic mem-inits have ... after the ')'. */
20042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20043 cp_lexer_consume_token (parser->lexer);
20046 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20047 /* Handle function try blocks. */
20048 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20049 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20050 last = parser->lexer->next_token;
20052 /* Save away the inline definition; we will process it when the
20053 class is complete. */
20054 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20055 DECL_PENDING_INLINE_P (fn) = 1;
20057 /* We need to know that this was defined in the class, so that
20058 friend templates are handled correctly. */
20059 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20061 /* Add FN to the queue of functions to be parsed later. */
20062 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20067 /* Parse a template-argument-list, as well as the trailing ">" (but
20068 not the opening ">"). See cp_parser_template_argument_list for the
20072 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20076 tree saved_qualifying_scope;
20077 tree saved_object_scope;
20078 bool saved_greater_than_is_operator_p;
20079 int saved_unevaluated_operand;
20080 int saved_inhibit_evaluation_warnings;
20084 When parsing a template-id, the first non-nested `>' is taken as
20085 the end of the template-argument-list rather than a greater-than
20087 saved_greater_than_is_operator_p
20088 = parser->greater_than_is_operator_p;
20089 parser->greater_than_is_operator_p = false;
20090 /* Parsing the argument list may modify SCOPE, so we save it
20092 saved_scope = parser->scope;
20093 saved_qualifying_scope = parser->qualifying_scope;
20094 saved_object_scope = parser->object_scope;
20095 /* We need to evaluate the template arguments, even though this
20096 template-id may be nested within a "sizeof". */
20097 saved_unevaluated_operand = cp_unevaluated_operand;
20098 cp_unevaluated_operand = 0;
20099 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20100 c_inhibit_evaluation_warnings = 0;
20101 /* Parse the template-argument-list itself. */
20102 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20103 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20104 arguments = NULL_TREE;
20106 arguments = cp_parser_template_argument_list (parser);
20107 /* Look for the `>' that ends the template-argument-list. If we find
20108 a '>>' instead, it's probably just a typo. */
20109 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20111 if (cxx_dialect != cxx98)
20113 /* In C++0x, a `>>' in a template argument list or cast
20114 expression is considered to be two separate `>'
20115 tokens. So, change the current token to a `>', but don't
20116 consume it: it will be consumed later when the outer
20117 template argument list (or cast expression) is parsed.
20118 Note that this replacement of `>' for `>>' is necessary
20119 even if we are parsing tentatively: in the tentative
20120 case, after calling
20121 cp_parser_enclosed_template_argument_list we will always
20122 throw away all of the template arguments and the first
20123 closing `>', either because the template argument list
20124 was erroneous or because we are replacing those tokens
20125 with a CPP_TEMPLATE_ID token. The second `>' (which will
20126 not have been thrown away) is needed either to close an
20127 outer template argument list or to complete a new-style
20129 cp_token *token = cp_lexer_peek_token (parser->lexer);
20130 token->type = CPP_GREATER;
20132 else if (!saved_greater_than_is_operator_p)
20134 /* If we're in a nested template argument list, the '>>' has
20135 to be a typo for '> >'. We emit the error message, but we
20136 continue parsing and we push a '>' as next token, so that
20137 the argument list will be parsed correctly. Note that the
20138 global source location is still on the token before the
20139 '>>', so we need to say explicitly where we want it. */
20140 cp_token *token = cp_lexer_peek_token (parser->lexer);
20141 error_at (token->location, "%<>>%> should be %<> >%> "
20142 "within a nested template argument list");
20144 token->type = CPP_GREATER;
20148 /* If this is not a nested template argument list, the '>>'
20149 is a typo for '>'. Emit an error message and continue.
20150 Same deal about the token location, but here we can get it
20151 right by consuming the '>>' before issuing the diagnostic. */
20152 cp_token *token = cp_lexer_consume_token (parser->lexer);
20153 error_at (token->location,
20154 "spurious %<>>%>, use %<>%> to terminate "
20155 "a template argument list");
20159 cp_parser_skip_to_end_of_template_parameter_list (parser);
20160 /* The `>' token might be a greater-than operator again now. */
20161 parser->greater_than_is_operator_p
20162 = saved_greater_than_is_operator_p;
20163 /* Restore the SAVED_SCOPE. */
20164 parser->scope = saved_scope;
20165 parser->qualifying_scope = saved_qualifying_scope;
20166 parser->object_scope = saved_object_scope;
20167 cp_unevaluated_operand = saved_unevaluated_operand;
20168 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20173 /* MEMBER_FUNCTION is a member function, or a friend. If default
20174 arguments, or the body of the function have not yet been parsed,
20178 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20180 /* If this member is a template, get the underlying
20182 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20183 member_function = DECL_TEMPLATE_RESULT (member_function);
20185 /* There should not be any class definitions in progress at this
20186 point; the bodies of members are only parsed outside of all class
20188 gcc_assert (parser->num_classes_being_defined == 0);
20189 /* While we're parsing the member functions we might encounter more
20190 classes. We want to handle them right away, but we don't want
20191 them getting mixed up with functions that are currently in the
20193 push_unparsed_function_queues (parser);
20195 /* Make sure that any template parameters are in scope. */
20196 maybe_begin_member_template_processing (member_function);
20198 /* If the body of the function has not yet been parsed, parse it
20200 if (DECL_PENDING_INLINE_P (member_function))
20202 tree function_scope;
20203 cp_token_cache *tokens;
20205 /* The function is no longer pending; we are processing it. */
20206 tokens = DECL_PENDING_INLINE_INFO (member_function);
20207 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20208 DECL_PENDING_INLINE_P (member_function) = 0;
20210 /* If this is a local class, enter the scope of the containing
20212 function_scope = current_function_decl;
20213 if (function_scope)
20214 push_function_context ();
20216 /* Push the body of the function onto the lexer stack. */
20217 cp_parser_push_lexer_for_tokens (parser, tokens);
20219 /* Let the front end know that we going to be defining this
20221 start_preparsed_function (member_function, NULL_TREE,
20222 SF_PRE_PARSED | SF_INCLASS_INLINE);
20224 /* Don't do access checking if it is a templated function. */
20225 if (processing_template_decl)
20226 push_deferring_access_checks (dk_no_check);
20228 /* Now, parse the body of the function. */
20229 cp_parser_function_definition_after_declarator (parser,
20230 /*inline_p=*/true);
20232 if (processing_template_decl)
20233 pop_deferring_access_checks ();
20235 /* Leave the scope of the containing function. */
20236 if (function_scope)
20237 pop_function_context ();
20238 cp_parser_pop_lexer (parser);
20241 /* Remove any template parameters from the symbol table. */
20242 maybe_end_member_template_processing ();
20244 /* Restore the queue. */
20245 pop_unparsed_function_queues (parser);
20248 /* If DECL contains any default args, remember it on the unparsed
20249 functions queue. */
20252 cp_parser_save_default_args (cp_parser* parser, tree decl)
20256 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20258 probe = TREE_CHAIN (probe))
20259 if (TREE_PURPOSE (probe))
20261 cp_default_arg_entry *entry
20262 = VEC_safe_push (cp_default_arg_entry, gc,
20263 unparsed_funs_with_default_args, NULL);
20264 entry->class_type = current_class_type;
20265 entry->decl = decl;
20270 /* FN is a FUNCTION_DECL which may contains a parameter with an
20271 unparsed DEFAULT_ARG. Parse the default args now. This function
20272 assumes that the current scope is the scope in which the default
20273 argument should be processed. */
20276 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20278 bool saved_local_variables_forbidden_p;
20279 tree parm, parmdecl;
20281 /* While we're parsing the default args, we might (due to the
20282 statement expression extension) encounter more classes. We want
20283 to handle them right away, but we don't want them getting mixed
20284 up with default args that are currently in the queue. */
20285 push_unparsed_function_queues (parser);
20287 /* Local variable names (and the `this' keyword) may not appear
20288 in a default argument. */
20289 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20290 parser->local_variables_forbidden_p = true;
20292 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20293 parmdecl = DECL_ARGUMENTS (fn);
20294 parm && parm != void_list_node;
20295 parm = TREE_CHAIN (parm),
20296 parmdecl = DECL_CHAIN (parmdecl))
20298 cp_token_cache *tokens;
20299 tree default_arg = TREE_PURPOSE (parm);
20301 VEC(tree,gc) *insts;
20308 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20309 /* This can happen for a friend declaration for a function
20310 already declared with default arguments. */
20313 /* Push the saved tokens for the default argument onto the parser's
20315 tokens = DEFARG_TOKENS (default_arg);
20316 cp_parser_push_lexer_for_tokens (parser, tokens);
20318 start_lambda_scope (parmdecl);
20320 /* Parse the assignment-expression. */
20321 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20322 if (parsed_arg == error_mark_node)
20324 cp_parser_pop_lexer (parser);
20328 if (!processing_template_decl)
20329 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20331 TREE_PURPOSE (parm) = parsed_arg;
20333 /* Update any instantiations we've already created. */
20334 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20335 VEC_iterate (tree, insts, ix, copy); ix++)
20336 TREE_PURPOSE (copy) = parsed_arg;
20338 finish_lambda_scope ();
20340 /* If the token stream has not been completely used up, then
20341 there was extra junk after the end of the default
20343 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20344 cp_parser_error (parser, "expected %<,%>");
20346 /* Revert to the main lexer. */
20347 cp_parser_pop_lexer (parser);
20350 /* Make sure no default arg is missing. */
20351 check_default_args (fn);
20353 /* Restore the state of local_variables_forbidden_p. */
20354 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20356 /* Restore the queue. */
20357 pop_unparsed_function_queues (parser);
20360 /* Parse the operand of `sizeof' (or a similar operator). Returns
20361 either a TYPE or an expression, depending on the form of the
20362 input. The KEYWORD indicates which kind of expression we have
20366 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20368 tree expr = NULL_TREE;
20369 const char *saved_message;
20371 bool saved_integral_constant_expression_p;
20372 bool saved_non_integral_constant_expression_p;
20373 bool pack_expansion_p = false;
20375 /* Types cannot be defined in a `sizeof' expression. Save away the
20377 saved_message = parser->type_definition_forbidden_message;
20378 /* And create the new one. */
20379 tmp = concat ("types may not be defined in %<",
20380 IDENTIFIER_POINTER (ridpointers[keyword]),
20381 "%> expressions", NULL);
20382 parser->type_definition_forbidden_message = tmp;
20384 /* The restrictions on constant-expressions do not apply inside
20385 sizeof expressions. */
20386 saved_integral_constant_expression_p
20387 = parser->integral_constant_expression_p;
20388 saved_non_integral_constant_expression_p
20389 = parser->non_integral_constant_expression_p;
20390 parser->integral_constant_expression_p = false;
20392 /* If it's a `...', then we are computing the length of a parameter
20394 if (keyword == RID_SIZEOF
20395 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20397 /* Consume the `...'. */
20398 cp_lexer_consume_token (parser->lexer);
20399 maybe_warn_variadic_templates ();
20401 /* Note that this is an expansion. */
20402 pack_expansion_p = true;
20405 /* Do not actually evaluate the expression. */
20406 ++cp_unevaluated_operand;
20407 ++c_inhibit_evaluation_warnings;
20408 /* If it's a `(', then we might be looking at the type-id
20410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20413 bool saved_in_type_id_in_expr_p;
20415 /* We can't be sure yet whether we're looking at a type-id or an
20417 cp_parser_parse_tentatively (parser);
20418 /* Consume the `('. */
20419 cp_lexer_consume_token (parser->lexer);
20420 /* Parse the type-id. */
20421 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20422 parser->in_type_id_in_expr_p = true;
20423 type = cp_parser_type_id (parser);
20424 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20425 /* Now, look for the trailing `)'. */
20426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20427 /* If all went well, then we're done. */
20428 if (cp_parser_parse_definitely (parser))
20430 cp_decl_specifier_seq decl_specs;
20432 /* Build a trivial decl-specifier-seq. */
20433 clear_decl_specs (&decl_specs);
20434 decl_specs.type = type;
20436 /* Call grokdeclarator to figure out what type this is. */
20437 expr = grokdeclarator (NULL,
20441 /*attrlist=*/NULL);
20445 /* If the type-id production did not work out, then we must be
20446 looking at the unary-expression production. */
20448 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20449 /*cast_p=*/false, NULL);
20451 if (pack_expansion_p)
20452 /* Build a pack expansion. */
20453 expr = make_pack_expansion (expr);
20455 /* Go back to evaluating expressions. */
20456 --cp_unevaluated_operand;
20457 --c_inhibit_evaluation_warnings;
20459 /* Free the message we created. */
20461 /* And restore the old one. */
20462 parser->type_definition_forbidden_message = saved_message;
20463 parser->integral_constant_expression_p
20464 = saved_integral_constant_expression_p;
20465 parser->non_integral_constant_expression_p
20466 = saved_non_integral_constant_expression_p;
20471 /* If the current declaration has no declarator, return true. */
20474 cp_parser_declares_only_class_p (cp_parser *parser)
20476 /* If the next token is a `;' or a `,' then there is no
20478 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20479 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20482 /* Update the DECL_SPECS to reflect the storage class indicated by
20486 cp_parser_set_storage_class (cp_parser *parser,
20487 cp_decl_specifier_seq *decl_specs,
20489 location_t location)
20491 cp_storage_class storage_class;
20493 if (parser->in_unbraced_linkage_specification_p)
20495 error_at (location, "invalid use of %qD in linkage specification",
20496 ridpointers[keyword]);
20499 else if (decl_specs->storage_class != sc_none)
20501 decl_specs->conflicting_specifiers_p = true;
20505 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20506 && decl_specs->specs[(int) ds_thread])
20508 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20509 decl_specs->specs[(int) ds_thread] = 0;
20515 storage_class = sc_auto;
20518 storage_class = sc_register;
20521 storage_class = sc_static;
20524 storage_class = sc_extern;
20527 storage_class = sc_mutable;
20530 gcc_unreachable ();
20532 decl_specs->storage_class = storage_class;
20534 /* A storage class specifier cannot be applied alongside a typedef
20535 specifier. If there is a typedef specifier present then set
20536 conflicting_specifiers_p which will trigger an error later
20537 on in grokdeclarator. */
20538 if (decl_specs->specs[(int)ds_typedef])
20539 decl_specs->conflicting_specifiers_p = true;
20542 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20543 is true, the type is a user-defined type; otherwise it is a
20544 built-in type specified by a keyword. */
20547 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20549 location_t location,
20550 bool user_defined_p)
20552 decl_specs->any_specifiers_p = true;
20554 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20555 (with, for example, in "typedef int wchar_t;") we remember that
20556 this is what happened. In system headers, we ignore these
20557 declarations so that G++ can work with system headers that are not
20559 if (decl_specs->specs[(int) ds_typedef]
20561 && (type_spec == boolean_type_node
20562 || type_spec == char16_type_node
20563 || type_spec == char32_type_node
20564 || type_spec == wchar_type_node)
20565 && (decl_specs->type
20566 || decl_specs->specs[(int) ds_long]
20567 || decl_specs->specs[(int) ds_short]
20568 || decl_specs->specs[(int) ds_unsigned]
20569 || decl_specs->specs[(int) ds_signed]))
20571 decl_specs->redefined_builtin_type = type_spec;
20572 if (!decl_specs->type)
20574 decl_specs->type = type_spec;
20575 decl_specs->user_defined_type_p = false;
20576 decl_specs->type_location = location;
20579 else if (decl_specs->type)
20580 decl_specs->multiple_types_p = true;
20583 decl_specs->type = type_spec;
20584 decl_specs->user_defined_type_p = user_defined_p;
20585 decl_specs->redefined_builtin_type = NULL_TREE;
20586 decl_specs->type_location = location;
20590 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20591 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20594 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20596 return decl_specifiers->specs[(int) ds_friend] != 0;
20599 /* Issue an error message indicating that TOKEN_DESC was expected.
20600 If KEYWORD is true, it indicated this function is called by
20601 cp_parser_require_keword and the required token can only be
20602 a indicated keyword. */
20605 cp_parser_required_error (cp_parser *parser,
20606 required_token token_desc,
20609 switch (token_desc)
20612 cp_parser_error (parser, "expected %<new%>");
20615 cp_parser_error (parser, "expected %<delete%>");
20618 cp_parser_error (parser, "expected %<return%>");
20621 cp_parser_error (parser, "expected %<while%>");
20624 cp_parser_error (parser, "expected %<extern%>");
20626 case RT_STATIC_ASSERT:
20627 cp_parser_error (parser, "expected %<static_assert%>");
20630 cp_parser_error (parser, "expected %<decltype%>");
20633 cp_parser_error (parser, "expected %<operator%>");
20636 cp_parser_error (parser, "expected %<class%>");
20639 cp_parser_error (parser, "expected %<template%>");
20642 cp_parser_error (parser, "expected %<namespace%>");
20645 cp_parser_error (parser, "expected %<using%>");
20648 cp_parser_error (parser, "expected %<asm%>");
20651 cp_parser_error (parser, "expected %<try%>");
20654 cp_parser_error (parser, "expected %<catch%>");
20657 cp_parser_error (parser, "expected %<throw%>");
20660 cp_parser_error (parser, "expected %<__label__%>");
20663 cp_parser_error (parser, "expected %<@try%>");
20665 case RT_AT_SYNCHRONIZED:
20666 cp_parser_error (parser, "expected %<@synchronized%>");
20669 cp_parser_error (parser, "expected %<@throw%>");
20676 switch (token_desc)
20679 cp_parser_error (parser, "expected %<;%>");
20681 case RT_OPEN_PAREN:
20682 cp_parser_error (parser, "expected %<(%>");
20684 case RT_CLOSE_BRACE:
20685 cp_parser_error (parser, "expected %<}%>");
20687 case RT_OPEN_BRACE:
20688 cp_parser_error (parser, "expected %<{%>");
20690 case RT_CLOSE_SQUARE:
20691 cp_parser_error (parser, "expected %<]%>");
20693 case RT_OPEN_SQUARE:
20694 cp_parser_error (parser, "expected %<[%>");
20697 cp_parser_error (parser, "expected %<,%>");
20700 cp_parser_error (parser, "expected %<::%>");
20703 cp_parser_error (parser, "expected %<<%>");
20706 cp_parser_error (parser, "expected %<>%>");
20709 cp_parser_error (parser, "expected %<=%>");
20712 cp_parser_error (parser, "expected %<...%>");
20715 cp_parser_error (parser, "expected %<*%>");
20718 cp_parser_error (parser, "expected %<~%>");
20721 cp_parser_error (parser, "expected %<:%>");
20723 case RT_COLON_SCOPE:
20724 cp_parser_error (parser, "expected %<:%> or %<::%>");
20726 case RT_CLOSE_PAREN:
20727 cp_parser_error (parser, "expected %<)%>");
20729 case RT_COMMA_CLOSE_PAREN:
20730 cp_parser_error (parser, "expected %<,%> or %<)%>");
20732 case RT_PRAGMA_EOL:
20733 cp_parser_error (parser, "expected end of line");
20736 cp_parser_error (parser, "expected identifier");
20739 cp_parser_error (parser, "expected selection-statement");
20741 case RT_INTERATION:
20742 cp_parser_error (parser, "expected iteration-statement");
20745 cp_parser_error (parser, "expected jump-statement");
20748 cp_parser_error (parser, "expected class-key");
20750 case RT_CLASS_TYPENAME_TEMPLATE:
20751 cp_parser_error (parser,
20752 "expected %<class%>, %<typename%>, or %<template%>");
20755 gcc_unreachable ();
20759 gcc_unreachable ();
20764 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20765 issue an error message indicating that TOKEN_DESC was expected.
20767 Returns the token consumed, if the token had the appropriate type.
20768 Otherwise, returns NULL. */
20771 cp_parser_require (cp_parser* parser,
20772 enum cpp_ttype type,
20773 required_token token_desc)
20775 if (cp_lexer_next_token_is (parser->lexer, type))
20776 return cp_lexer_consume_token (parser->lexer);
20779 /* Output the MESSAGE -- unless we're parsing tentatively. */
20780 if (!cp_parser_simulate_error (parser))
20781 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20786 /* An error message is produced if the next token is not '>'.
20787 All further tokens are skipped until the desired token is
20788 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20791 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20793 /* Current level of '< ... >'. */
20794 unsigned level = 0;
20795 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20796 unsigned nesting_depth = 0;
20798 /* Are we ready, yet? If not, issue error message. */
20799 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20802 /* Skip tokens until the desired token is found. */
20805 /* Peek at the next token. */
20806 switch (cp_lexer_peek_token (parser->lexer)->type)
20809 if (!nesting_depth)
20814 if (cxx_dialect == cxx98)
20815 /* C++0x views the `>>' operator as two `>' tokens, but
20818 else if (!nesting_depth && level-- == 0)
20820 /* We've hit a `>>' where the first `>' closes the
20821 template argument list, and the second `>' is
20822 spurious. Just consume the `>>' and stop; we've
20823 already produced at least one error. */
20824 cp_lexer_consume_token (parser->lexer);
20827 /* Fall through for C++0x, so we handle the second `>' in
20831 if (!nesting_depth && level-- == 0)
20833 /* We've reached the token we want, consume it and stop. */
20834 cp_lexer_consume_token (parser->lexer);
20839 case CPP_OPEN_PAREN:
20840 case CPP_OPEN_SQUARE:
20844 case CPP_CLOSE_PAREN:
20845 case CPP_CLOSE_SQUARE:
20846 if (nesting_depth-- == 0)
20851 case CPP_PRAGMA_EOL:
20852 case CPP_SEMICOLON:
20853 case CPP_OPEN_BRACE:
20854 case CPP_CLOSE_BRACE:
20855 /* The '>' was probably forgotten, don't look further. */
20862 /* Consume this token. */
20863 cp_lexer_consume_token (parser->lexer);
20867 /* If the next token is the indicated keyword, consume it. Otherwise,
20868 issue an error message indicating that TOKEN_DESC was expected.
20870 Returns the token consumed, if the token had the appropriate type.
20871 Otherwise, returns NULL. */
20874 cp_parser_require_keyword (cp_parser* parser,
20876 required_token token_desc)
20878 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20880 if (token && token->keyword != keyword)
20882 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20889 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20890 function-definition. */
20893 cp_parser_token_starts_function_definition_p (cp_token* token)
20895 return (/* An ordinary function-body begins with an `{'. */
20896 token->type == CPP_OPEN_BRACE
20897 /* A ctor-initializer begins with a `:'. */
20898 || token->type == CPP_COLON
20899 /* A function-try-block begins with `try'. */
20900 || token->keyword == RID_TRY
20901 /* The named return value extension begins with `return'. */
20902 || token->keyword == RID_RETURN);
20905 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20909 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20913 token = cp_lexer_peek_token (parser->lexer);
20914 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20917 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20918 C++0x) ending a template-argument. */
20921 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20925 token = cp_lexer_peek_token (parser->lexer);
20926 return (token->type == CPP_COMMA
20927 || token->type == CPP_GREATER
20928 || token->type == CPP_ELLIPSIS
20929 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20932 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20933 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20936 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20941 token = cp_lexer_peek_nth_token (parser->lexer, n);
20942 if (token->type == CPP_LESS)
20944 /* Check for the sequence `<::' in the original code. It would be lexed as
20945 `[:', where `[' is a digraph, and there is no whitespace before
20947 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20950 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20951 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20957 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20958 or none_type otherwise. */
20960 static enum tag_types
20961 cp_parser_token_is_class_key (cp_token* token)
20963 switch (token->keyword)
20968 return record_type;
20977 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20980 cp_parser_check_class_key (enum tag_types class_key, tree type)
20982 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20983 permerror (input_location, "%qs tag used in naming %q#T",
20984 class_key == union_type ? "union"
20985 : class_key == record_type ? "struct" : "class",
20989 /* Issue an error message if DECL is redeclared with different
20990 access than its original declaration [class.access.spec/3].
20991 This applies to nested classes and nested class templates.
20995 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20997 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21000 if ((TREE_PRIVATE (decl)
21001 != (current_access_specifier == access_private_node))
21002 || (TREE_PROTECTED (decl)
21003 != (current_access_specifier == access_protected_node)))
21004 error_at (location, "%qD redeclared with different access", decl);
21007 /* Look for the `template' keyword, as a syntactic disambiguator.
21008 Return TRUE iff it is present, in which case it will be
21012 cp_parser_optional_template_keyword (cp_parser *parser)
21014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21016 /* The `template' keyword can only be used within templates;
21017 outside templates the parser can always figure out what is a
21018 template and what is not. */
21019 if (!processing_template_decl)
21021 cp_token *token = cp_lexer_peek_token (parser->lexer);
21022 error_at (token->location,
21023 "%<template%> (as a disambiguator) is only allowed "
21024 "within templates");
21025 /* If this part of the token stream is rescanned, the same
21026 error message would be generated. So, we purge the token
21027 from the stream. */
21028 cp_lexer_purge_token (parser->lexer);
21033 /* Consume the `template' keyword. */
21034 cp_lexer_consume_token (parser->lexer);
21042 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21043 set PARSER->SCOPE, and perform other related actions. */
21046 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21049 struct tree_check *check_value;
21050 deferred_access_check *chk;
21051 VEC (deferred_access_check,gc) *checks;
21053 /* Get the stored value. */
21054 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21055 /* Perform any access checks that were deferred. */
21056 checks = check_value->checks;
21059 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21060 perform_or_defer_access_check (chk->binfo,
21064 /* Set the scope from the stored value. */
21065 parser->scope = check_value->value;
21066 parser->qualifying_scope = check_value->qualifying_scope;
21067 parser->object_scope = NULL_TREE;
21070 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21071 encounter the end of a block before what we were looking for. */
21074 cp_parser_cache_group (cp_parser *parser,
21075 enum cpp_ttype end,
21080 cp_token *token = cp_lexer_peek_token (parser->lexer);
21082 /* Abort a parenthesized expression if we encounter a semicolon. */
21083 if ((end == CPP_CLOSE_PAREN || depth == 0)
21084 && token->type == CPP_SEMICOLON)
21086 /* If we've reached the end of the file, stop. */
21087 if (token->type == CPP_EOF
21088 || (end != CPP_PRAGMA_EOL
21089 && token->type == CPP_PRAGMA_EOL))
21091 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21092 /* We've hit the end of an enclosing block, so there's been some
21093 kind of syntax error. */
21096 /* Consume the token. */
21097 cp_lexer_consume_token (parser->lexer);
21098 /* See if it starts a new group. */
21099 if (token->type == CPP_OPEN_BRACE)
21101 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21102 /* In theory this should probably check end == '}', but
21103 cp_parser_save_member_function_body needs it to exit
21104 after either '}' or ')' when called with ')'. */
21108 else if (token->type == CPP_OPEN_PAREN)
21110 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21111 if (depth == 0 && end == CPP_CLOSE_PAREN)
21114 else if (token->type == CPP_PRAGMA)
21115 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21116 else if (token->type == end)
21121 /* Begin parsing tentatively. We always save tokens while parsing
21122 tentatively so that if the tentative parsing fails we can restore the
21126 cp_parser_parse_tentatively (cp_parser* parser)
21128 /* Enter a new parsing context. */
21129 parser->context = cp_parser_context_new (parser->context);
21130 /* Begin saving tokens. */
21131 cp_lexer_save_tokens (parser->lexer);
21132 /* In order to avoid repetitive access control error messages,
21133 access checks are queued up until we are no longer parsing
21135 push_deferring_access_checks (dk_deferred);
21138 /* Commit to the currently active tentative parse. */
21141 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21143 cp_parser_context *context;
21146 /* Mark all of the levels as committed. */
21147 lexer = parser->lexer;
21148 for (context = parser->context; context->next; context = context->next)
21150 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21152 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21153 while (!cp_lexer_saving_tokens (lexer))
21154 lexer = lexer->next;
21155 cp_lexer_commit_tokens (lexer);
21159 /* Abort the currently active tentative parse. All consumed tokens
21160 will be rolled back, and no diagnostics will be issued. */
21163 cp_parser_abort_tentative_parse (cp_parser* parser)
21165 cp_parser_simulate_error (parser);
21166 /* Now, pretend that we want to see if the construct was
21167 successfully parsed. */
21168 cp_parser_parse_definitely (parser);
21171 /* Stop parsing tentatively. If a parse error has occurred, restore the
21172 token stream. Otherwise, commit to the tokens we have consumed.
21173 Returns true if no error occurred; false otherwise. */
21176 cp_parser_parse_definitely (cp_parser* parser)
21178 bool error_occurred;
21179 cp_parser_context *context;
21181 /* Remember whether or not an error occurred, since we are about to
21182 destroy that information. */
21183 error_occurred = cp_parser_error_occurred (parser);
21184 /* Remove the topmost context from the stack. */
21185 context = parser->context;
21186 parser->context = context->next;
21187 /* If no parse errors occurred, commit to the tentative parse. */
21188 if (!error_occurred)
21190 /* Commit to the tokens read tentatively, unless that was
21192 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21193 cp_lexer_commit_tokens (parser->lexer);
21195 pop_to_parent_deferring_access_checks ();
21197 /* Otherwise, if errors occurred, roll back our state so that things
21198 are just as they were before we began the tentative parse. */
21201 cp_lexer_rollback_tokens (parser->lexer);
21202 pop_deferring_access_checks ();
21204 /* Add the context to the front of the free list. */
21205 context->next = cp_parser_context_free_list;
21206 cp_parser_context_free_list = context;
21208 return !error_occurred;
21211 /* Returns true if we are parsing tentatively and are not committed to
21212 this tentative parse. */
21215 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21217 return (cp_parser_parsing_tentatively (parser)
21218 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21221 /* Returns nonzero iff an error has occurred during the most recent
21222 tentative parse. */
21225 cp_parser_error_occurred (cp_parser* parser)
21227 return (cp_parser_parsing_tentatively (parser)
21228 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21231 /* Returns nonzero if GNU extensions are allowed. */
21234 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21236 return parser->allow_gnu_extensions_p;
21239 /* Objective-C++ Productions */
21242 /* Parse an Objective-C expression, which feeds into a primary-expression
21246 objc-message-expression
21247 objc-string-literal
21248 objc-encode-expression
21249 objc-protocol-expression
21250 objc-selector-expression
21252 Returns a tree representation of the expression. */
21255 cp_parser_objc_expression (cp_parser* parser)
21257 /* Try to figure out what kind of declaration is present. */
21258 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21262 case CPP_OPEN_SQUARE:
21263 return cp_parser_objc_message_expression (parser);
21265 case CPP_OBJC_STRING:
21266 kwd = cp_lexer_consume_token (parser->lexer);
21267 return objc_build_string_object (kwd->u.value);
21270 switch (kwd->keyword)
21272 case RID_AT_ENCODE:
21273 return cp_parser_objc_encode_expression (parser);
21275 case RID_AT_PROTOCOL:
21276 return cp_parser_objc_protocol_expression (parser);
21278 case RID_AT_SELECTOR:
21279 return cp_parser_objc_selector_expression (parser);
21285 error_at (kwd->location,
21286 "misplaced %<@%D%> Objective-C++ construct",
21288 cp_parser_skip_to_end_of_block_or_statement (parser);
21291 return error_mark_node;
21294 /* Parse an Objective-C message expression.
21296 objc-message-expression:
21297 [ objc-message-receiver objc-message-args ]
21299 Returns a representation of an Objective-C message. */
21302 cp_parser_objc_message_expression (cp_parser* parser)
21304 tree receiver, messageargs;
21306 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21307 receiver = cp_parser_objc_message_receiver (parser);
21308 messageargs = cp_parser_objc_message_args (parser);
21309 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21311 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21314 /* Parse an objc-message-receiver.
21316 objc-message-receiver:
21318 simple-type-specifier
21320 Returns a representation of the type or expression. */
21323 cp_parser_objc_message_receiver (cp_parser* parser)
21327 /* An Objective-C message receiver may be either (1) a type
21328 or (2) an expression. */
21329 cp_parser_parse_tentatively (parser);
21330 rcv = cp_parser_expression (parser, false, NULL);
21332 if (cp_parser_parse_definitely (parser))
21335 rcv = cp_parser_simple_type_specifier (parser,
21336 /*decl_specs=*/NULL,
21337 CP_PARSER_FLAGS_NONE);
21339 return objc_get_class_reference (rcv);
21342 /* Parse the arguments and selectors comprising an Objective-C message.
21347 objc-selector-args , objc-comma-args
21349 objc-selector-args:
21350 objc-selector [opt] : assignment-expression
21351 objc-selector-args objc-selector [opt] : assignment-expression
21354 assignment-expression
21355 objc-comma-args , assignment-expression
21357 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21358 selector arguments and TREE_VALUE containing a list of comma
21362 cp_parser_objc_message_args (cp_parser* parser)
21364 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21365 bool maybe_unary_selector_p = true;
21366 cp_token *token = cp_lexer_peek_token (parser->lexer);
21368 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21370 tree selector = NULL_TREE, arg;
21372 if (token->type != CPP_COLON)
21373 selector = cp_parser_objc_selector (parser);
21375 /* Detect if we have a unary selector. */
21376 if (maybe_unary_selector_p
21377 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21378 return build_tree_list (selector, NULL_TREE);
21380 maybe_unary_selector_p = false;
21381 cp_parser_require (parser, CPP_COLON, RT_COLON);
21382 arg = cp_parser_assignment_expression (parser, false, NULL);
21385 = chainon (sel_args,
21386 build_tree_list (selector, arg));
21388 token = cp_lexer_peek_token (parser->lexer);
21391 /* Handle non-selector arguments, if any. */
21392 while (token->type == CPP_COMMA)
21396 cp_lexer_consume_token (parser->lexer);
21397 arg = cp_parser_assignment_expression (parser, false, NULL);
21400 = chainon (addl_args,
21401 build_tree_list (NULL_TREE, arg));
21403 token = cp_lexer_peek_token (parser->lexer);
21406 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21408 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21409 return build_tree_list (error_mark_node, error_mark_node);
21412 return build_tree_list (sel_args, addl_args);
21415 /* Parse an Objective-C encode expression.
21417 objc-encode-expression:
21418 @encode objc-typename
21420 Returns an encoded representation of the type argument. */
21423 cp_parser_objc_encode_expression (cp_parser* parser)
21428 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21429 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21430 token = cp_lexer_peek_token (parser->lexer);
21431 type = complete_type (cp_parser_type_id (parser));
21432 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21436 error_at (token->location,
21437 "%<@encode%> must specify a type as an argument");
21438 return error_mark_node;
21441 /* This happens if we find @encode(T) (where T is a template
21442 typename or something dependent on a template typename) when
21443 parsing a template. In that case, we can't compile it
21444 immediately, but we rather create an AT_ENCODE_EXPR which will
21445 need to be instantiated when the template is used.
21447 if (dependent_type_p (type))
21449 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21450 TREE_READONLY (value) = 1;
21454 return objc_build_encode_expr (type);
21457 /* Parse an Objective-C @defs expression. */
21460 cp_parser_objc_defs_expression (cp_parser *parser)
21464 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21465 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21466 name = cp_parser_identifier (parser);
21467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21469 return objc_get_class_ivars (name);
21472 /* Parse an Objective-C protocol expression.
21474 objc-protocol-expression:
21475 @protocol ( identifier )
21477 Returns a representation of the protocol expression. */
21480 cp_parser_objc_protocol_expression (cp_parser* parser)
21484 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21485 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21486 proto = cp_parser_identifier (parser);
21487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21489 return objc_build_protocol_expr (proto);
21492 /* Parse an Objective-C selector expression.
21494 objc-selector-expression:
21495 @selector ( objc-method-signature )
21497 objc-method-signature:
21503 objc-selector-seq objc-selector :
21505 Returns a representation of the method selector. */
21508 cp_parser_objc_selector_expression (cp_parser* parser)
21510 tree sel_seq = NULL_TREE;
21511 bool maybe_unary_selector_p = true;
21513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21515 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21516 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21517 token = cp_lexer_peek_token (parser->lexer);
21519 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21520 || token->type == CPP_SCOPE)
21522 tree selector = NULL_TREE;
21524 if (token->type != CPP_COLON
21525 || token->type == CPP_SCOPE)
21526 selector = cp_parser_objc_selector (parser);
21528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21529 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21531 /* Detect if we have a unary selector. */
21532 if (maybe_unary_selector_p)
21534 sel_seq = selector;
21535 goto finish_selector;
21539 cp_parser_error (parser, "expected %<:%>");
21542 maybe_unary_selector_p = false;
21543 token = cp_lexer_consume_token (parser->lexer);
21545 if (token->type == CPP_SCOPE)
21548 = chainon (sel_seq,
21549 build_tree_list (selector, NULL_TREE));
21551 = chainon (sel_seq,
21552 build_tree_list (NULL_TREE, NULL_TREE));
21556 = chainon (sel_seq,
21557 build_tree_list (selector, NULL_TREE));
21559 token = cp_lexer_peek_token (parser->lexer);
21563 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21565 return objc_build_selector_expr (loc, sel_seq);
21568 /* Parse a list of identifiers.
21570 objc-identifier-list:
21572 objc-identifier-list , identifier
21574 Returns a TREE_LIST of identifier nodes. */
21577 cp_parser_objc_identifier_list (cp_parser* parser)
21583 identifier = cp_parser_identifier (parser);
21584 if (identifier == error_mark_node)
21585 return error_mark_node;
21587 list = build_tree_list (NULL_TREE, identifier);
21588 sep = cp_lexer_peek_token (parser->lexer);
21590 while (sep->type == CPP_COMMA)
21592 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21593 identifier = cp_parser_identifier (parser);
21594 if (identifier == error_mark_node)
21597 list = chainon (list, build_tree_list (NULL_TREE,
21599 sep = cp_lexer_peek_token (parser->lexer);
21605 /* Parse an Objective-C alias declaration.
21607 objc-alias-declaration:
21608 @compatibility_alias identifier identifier ;
21610 This function registers the alias mapping with the Objective-C front end.
21611 It returns nothing. */
21614 cp_parser_objc_alias_declaration (cp_parser* parser)
21618 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21619 alias = cp_parser_identifier (parser);
21620 orig = cp_parser_identifier (parser);
21621 objc_declare_alias (alias, orig);
21622 cp_parser_consume_semicolon_at_end_of_statement (parser);
21625 /* Parse an Objective-C class forward-declaration.
21627 objc-class-declaration:
21628 @class objc-identifier-list ;
21630 The function registers the forward declarations with the Objective-C
21631 front end. It returns nothing. */
21634 cp_parser_objc_class_declaration (cp_parser* parser)
21636 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21637 objc_declare_class (cp_parser_objc_identifier_list (parser));
21638 cp_parser_consume_semicolon_at_end_of_statement (parser);
21641 /* Parse a list of Objective-C protocol references.
21643 objc-protocol-refs-opt:
21644 objc-protocol-refs [opt]
21646 objc-protocol-refs:
21647 < objc-identifier-list >
21649 Returns a TREE_LIST of identifiers, if any. */
21652 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21654 tree protorefs = NULL_TREE;
21656 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21658 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21659 protorefs = cp_parser_objc_identifier_list (parser);
21660 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21666 /* Parse a Objective-C visibility specification. */
21669 cp_parser_objc_visibility_spec (cp_parser* parser)
21671 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21673 switch (vis->keyword)
21675 case RID_AT_PRIVATE:
21676 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21678 case RID_AT_PROTECTED:
21679 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21681 case RID_AT_PUBLIC:
21682 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21684 case RID_AT_PACKAGE:
21685 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21691 /* Eat '@private'/'@protected'/'@public'. */
21692 cp_lexer_consume_token (parser->lexer);
21695 /* Parse an Objective-C method type. Return 'true' if it is a class
21696 (+) method, and 'false' if it is an instance (-) method. */
21699 cp_parser_objc_method_type (cp_parser* parser)
21701 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21707 /* Parse an Objective-C protocol qualifier. */
21710 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21712 tree quals = NULL_TREE, node;
21713 cp_token *token = cp_lexer_peek_token (parser->lexer);
21715 node = token->u.value;
21717 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21718 && (node == ridpointers [(int) RID_IN]
21719 || node == ridpointers [(int) RID_OUT]
21720 || node == ridpointers [(int) RID_INOUT]
21721 || node == ridpointers [(int) RID_BYCOPY]
21722 || node == ridpointers [(int) RID_BYREF]
21723 || node == ridpointers [(int) RID_ONEWAY]))
21725 quals = tree_cons (NULL_TREE, node, quals);
21726 cp_lexer_consume_token (parser->lexer);
21727 token = cp_lexer_peek_token (parser->lexer);
21728 node = token->u.value;
21734 /* Parse an Objective-C typename. */
21737 cp_parser_objc_typename (cp_parser* parser)
21739 tree type_name = NULL_TREE;
21741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21743 tree proto_quals, cp_type = NULL_TREE;
21745 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21746 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21748 /* An ObjC type name may consist of just protocol qualifiers, in which
21749 case the type shall default to 'id'. */
21750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21751 cp_type = cp_parser_type_id (parser);
21753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21754 type_name = build_tree_list (proto_quals, cp_type);
21760 /* Check to see if TYPE refers to an Objective-C selector name. */
21763 cp_parser_objc_selector_p (enum cpp_ttype type)
21765 return (type == CPP_NAME || type == CPP_KEYWORD
21766 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21767 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21768 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21769 || type == CPP_XOR || type == CPP_XOR_EQ);
21772 /* Parse an Objective-C selector. */
21775 cp_parser_objc_selector (cp_parser* parser)
21777 cp_token *token = cp_lexer_consume_token (parser->lexer);
21779 if (!cp_parser_objc_selector_p (token->type))
21781 error_at (token->location, "invalid Objective-C++ selector name");
21782 return error_mark_node;
21785 /* C++ operator names are allowed to appear in ObjC selectors. */
21786 switch (token->type)
21788 case CPP_AND_AND: return get_identifier ("and");
21789 case CPP_AND_EQ: return get_identifier ("and_eq");
21790 case CPP_AND: return get_identifier ("bitand");
21791 case CPP_OR: return get_identifier ("bitor");
21792 case CPP_COMPL: return get_identifier ("compl");
21793 case CPP_NOT: return get_identifier ("not");
21794 case CPP_NOT_EQ: return get_identifier ("not_eq");
21795 case CPP_OR_OR: return get_identifier ("or");
21796 case CPP_OR_EQ: return get_identifier ("or_eq");
21797 case CPP_XOR: return get_identifier ("xor");
21798 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21799 default: return token->u.value;
21803 /* Parse an Objective-C params list. */
21806 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21808 tree params = NULL_TREE;
21809 bool maybe_unary_selector_p = true;
21810 cp_token *token = cp_lexer_peek_token (parser->lexer);
21812 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21814 tree selector = NULL_TREE, type_name, identifier;
21815 tree parm_attr = NULL_TREE;
21817 if (token->keyword == RID_ATTRIBUTE)
21820 if (token->type != CPP_COLON)
21821 selector = cp_parser_objc_selector (parser);
21823 /* Detect if we have a unary selector. */
21824 if (maybe_unary_selector_p
21825 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21827 params = selector; /* Might be followed by attributes. */
21831 maybe_unary_selector_p = false;
21832 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21834 /* Something went quite wrong. There should be a colon
21835 here, but there is not. Stop parsing parameters. */
21838 type_name = cp_parser_objc_typename (parser);
21839 /* New ObjC allows attributes on parameters too. */
21840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21841 parm_attr = cp_parser_attributes_opt (parser);
21842 identifier = cp_parser_identifier (parser);
21846 objc_build_keyword_decl (selector,
21851 token = cp_lexer_peek_token (parser->lexer);
21854 if (params == NULL_TREE)
21856 cp_parser_error (parser, "objective-c++ method declaration is expected");
21857 return error_mark_node;
21860 /* We allow tail attributes for the method. */
21861 if (token->keyword == RID_ATTRIBUTE)
21863 *attributes = cp_parser_attributes_opt (parser);
21864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21865 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21867 cp_parser_error (parser,
21868 "method attributes must be specified at the end");
21869 return error_mark_node;
21872 if (params == NULL_TREE)
21874 cp_parser_error (parser, "objective-c++ method declaration is expected");
21875 return error_mark_node;
21880 /* Parse the non-keyword Objective-C params. */
21883 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21886 tree params = make_node (TREE_LIST);
21887 cp_token *token = cp_lexer_peek_token (parser->lexer);
21888 *ellipsisp = false; /* Initially, assume no ellipsis. */
21890 while (token->type == CPP_COMMA)
21892 cp_parameter_declarator *parmdecl;
21895 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21896 token = cp_lexer_peek_token (parser->lexer);
21898 if (token->type == CPP_ELLIPSIS)
21900 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21902 token = cp_lexer_peek_token (parser->lexer);
21906 /* TODO: parse attributes for tail parameters. */
21907 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21908 parm = grokdeclarator (parmdecl->declarator,
21909 &parmdecl->decl_specifiers,
21910 PARM, /*initialized=*/0,
21911 /*attrlist=*/NULL);
21913 chainon (params, build_tree_list (NULL_TREE, parm));
21914 token = cp_lexer_peek_token (parser->lexer);
21917 /* We allow tail attributes for the method. */
21918 if (token->keyword == RID_ATTRIBUTE)
21920 if (*attributes == NULL_TREE)
21922 *attributes = cp_parser_attributes_opt (parser);
21923 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21924 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21928 /* We have an error, but parse the attributes, so that we can
21930 *attributes = cp_parser_attributes_opt (parser);
21932 cp_parser_error (parser,
21933 "method attributes must be specified at the end");
21934 return error_mark_node;
21940 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21943 cp_parser_objc_interstitial_code (cp_parser* parser)
21945 cp_token *token = cp_lexer_peek_token (parser->lexer);
21947 /* If the next token is `extern' and the following token is a string
21948 literal, then we have a linkage specification. */
21949 if (token->keyword == RID_EXTERN
21950 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21951 cp_parser_linkage_specification (parser);
21952 /* Handle #pragma, if any. */
21953 else if (token->type == CPP_PRAGMA)
21954 cp_parser_pragma (parser, pragma_external);
21955 /* Allow stray semicolons. */
21956 else if (token->type == CPP_SEMICOLON)
21957 cp_lexer_consume_token (parser->lexer);
21958 /* Mark methods as optional or required, when building protocols. */
21959 else if (token->keyword == RID_AT_OPTIONAL)
21961 cp_lexer_consume_token (parser->lexer);
21962 objc_set_method_opt (true);
21964 else if (token->keyword == RID_AT_REQUIRED)
21966 cp_lexer_consume_token (parser->lexer);
21967 objc_set_method_opt (false);
21969 else if (token->keyword == RID_NAMESPACE)
21970 cp_parser_namespace_definition (parser);
21971 /* Other stray characters must generate errors. */
21972 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21974 cp_lexer_consume_token (parser->lexer);
21975 error ("stray %qs between Objective-C++ methods",
21976 token->type == CPP_OPEN_BRACE ? "{" : "}");
21978 /* Finally, try to parse a block-declaration, or a function-definition. */
21980 cp_parser_block_declaration (parser, /*statement_p=*/false);
21983 /* Parse a method signature. */
21986 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21988 tree rettype, kwdparms, optparms;
21989 bool ellipsis = false;
21990 bool is_class_method;
21992 is_class_method = cp_parser_objc_method_type (parser);
21993 rettype = cp_parser_objc_typename (parser);
21994 *attributes = NULL_TREE;
21995 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21996 if (kwdparms == error_mark_node)
21997 return error_mark_node;
21998 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21999 if (optparms == error_mark_node)
22000 return error_mark_node;
22002 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22006 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22009 cp_lexer_save_tokens (parser->lexer);
22010 tattr = cp_parser_attributes_opt (parser);
22011 gcc_assert (tattr) ;
22013 /* If the attributes are followed by a method introducer, this is not allowed.
22014 Dump the attributes and flag the situation. */
22015 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22016 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22019 /* Otherwise, the attributes introduce some interstitial code, possibly so
22020 rewind to allow that check. */
22021 cp_lexer_rollback_tokens (parser->lexer);
22025 /* Parse an Objective-C method prototype list. */
22028 cp_parser_objc_method_prototype_list (cp_parser* parser)
22030 cp_token *token = cp_lexer_peek_token (parser->lexer);
22032 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22034 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22036 tree attributes, sig;
22037 bool is_class_method;
22038 if (token->type == CPP_PLUS)
22039 is_class_method = true;
22041 is_class_method = false;
22042 sig = cp_parser_objc_method_signature (parser, &attributes);
22043 if (sig == error_mark_node)
22045 cp_parser_skip_to_end_of_block_or_statement (parser);
22046 token = cp_lexer_peek_token (parser->lexer);
22049 objc_add_method_declaration (is_class_method, sig, attributes);
22050 cp_parser_consume_semicolon_at_end_of_statement (parser);
22052 else if (token->keyword == RID_AT_PROPERTY)
22053 cp_parser_objc_at_property_declaration (parser);
22054 else if (token->keyword == RID_ATTRIBUTE
22055 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22056 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22058 "prefix attributes are ignored for methods");
22060 /* Allow for interspersed non-ObjC++ code. */
22061 cp_parser_objc_interstitial_code (parser);
22063 token = cp_lexer_peek_token (parser->lexer);
22066 if (token->type != CPP_EOF)
22067 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22069 cp_parser_error (parser, "expected %<@end%>");
22071 objc_finish_interface ();
22074 /* Parse an Objective-C method definition list. */
22077 cp_parser_objc_method_definition_list (cp_parser* parser)
22079 cp_token *token = cp_lexer_peek_token (parser->lexer);
22081 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22085 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22088 tree sig, attribute;
22089 bool is_class_method;
22090 if (token->type == CPP_PLUS)
22091 is_class_method = true;
22093 is_class_method = false;
22094 push_deferring_access_checks (dk_deferred);
22095 sig = cp_parser_objc_method_signature (parser, &attribute);
22096 if (sig == error_mark_node)
22098 cp_parser_skip_to_end_of_block_or_statement (parser);
22099 token = cp_lexer_peek_token (parser->lexer);
22102 objc_start_method_definition (is_class_method, sig, attribute);
22104 /* For historical reasons, we accept an optional semicolon. */
22105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22106 cp_lexer_consume_token (parser->lexer);
22108 ptk = cp_lexer_peek_token (parser->lexer);
22109 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22110 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22112 perform_deferred_access_checks ();
22113 stop_deferring_access_checks ();
22114 meth = cp_parser_function_definition_after_declarator (parser,
22116 pop_deferring_access_checks ();
22117 objc_finish_method_definition (meth);
22120 /* The following case will be removed once @synthesize is
22121 completely implemented. */
22122 else if (token->keyword == RID_AT_PROPERTY)
22123 cp_parser_objc_at_property_declaration (parser);
22124 else if (token->keyword == RID_AT_SYNTHESIZE)
22125 cp_parser_objc_at_synthesize_declaration (parser);
22126 else if (token->keyword == RID_AT_DYNAMIC)
22127 cp_parser_objc_at_dynamic_declaration (parser);
22128 else if (token->keyword == RID_ATTRIBUTE
22129 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22130 warning_at (token->location, OPT_Wattributes,
22131 "prefix attributes are ignored for methods");
22133 /* Allow for interspersed non-ObjC++ code. */
22134 cp_parser_objc_interstitial_code (parser);
22136 token = cp_lexer_peek_token (parser->lexer);
22139 if (token->type != CPP_EOF)
22140 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22142 cp_parser_error (parser, "expected %<@end%>");
22144 objc_finish_implementation ();
22147 /* Parse Objective-C ivars. */
22150 cp_parser_objc_class_ivars (cp_parser* parser)
22152 cp_token *token = cp_lexer_peek_token (parser->lexer);
22154 if (token->type != CPP_OPEN_BRACE)
22155 return; /* No ivars specified. */
22157 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22158 token = cp_lexer_peek_token (parser->lexer);
22160 while (token->type != CPP_CLOSE_BRACE
22161 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22163 cp_decl_specifier_seq declspecs;
22164 int decl_class_or_enum_p;
22165 tree prefix_attributes;
22167 cp_parser_objc_visibility_spec (parser);
22169 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22172 cp_parser_decl_specifier_seq (parser,
22173 CP_PARSER_FLAGS_OPTIONAL,
22175 &decl_class_or_enum_p);
22177 /* auto, register, static, extern, mutable. */
22178 if (declspecs.storage_class != sc_none)
22180 cp_parser_error (parser, "invalid type for instance variable");
22181 declspecs.storage_class = sc_none;
22185 if (declspecs.specs[(int) ds_thread])
22187 cp_parser_error (parser, "invalid type for instance variable");
22188 declspecs.specs[(int) ds_thread] = 0;
22192 if (declspecs.specs[(int) ds_typedef])
22194 cp_parser_error (parser, "invalid type for instance variable");
22195 declspecs.specs[(int) ds_typedef] = 0;
22198 prefix_attributes = declspecs.attributes;
22199 declspecs.attributes = NULL_TREE;
22201 /* Keep going until we hit the `;' at the end of the
22203 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22205 tree width = NULL_TREE, attributes, first_attribute, decl;
22206 cp_declarator *declarator = NULL;
22207 int ctor_dtor_or_conv_p;
22209 /* Check for a (possibly unnamed) bitfield declaration. */
22210 token = cp_lexer_peek_token (parser->lexer);
22211 if (token->type == CPP_COLON)
22214 if (token->type == CPP_NAME
22215 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22218 /* Get the name of the bitfield. */
22219 declarator = make_id_declarator (NULL_TREE,
22220 cp_parser_identifier (parser),
22224 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22225 /* Get the width of the bitfield. */
22227 = cp_parser_constant_expression (parser,
22228 /*allow_non_constant=*/false,
22233 /* Parse the declarator. */
22235 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22236 &ctor_dtor_or_conv_p,
22237 /*parenthesized_p=*/NULL,
22238 /*member_p=*/false);
22241 /* Look for attributes that apply to the ivar. */
22242 attributes = cp_parser_attributes_opt (parser);
22243 /* Remember which attributes are prefix attributes and
22245 first_attribute = attributes;
22246 /* Combine the attributes. */
22247 attributes = chainon (prefix_attributes, attributes);
22250 /* Create the bitfield declaration. */
22251 decl = grokbitfield (declarator, &declspecs,
22255 decl = grokfield (declarator, &declspecs,
22256 NULL_TREE, /*init_const_expr_p=*/false,
22257 NULL_TREE, attributes);
22259 /* Add the instance variable. */
22260 objc_add_instance_variable (decl);
22262 /* Reset PREFIX_ATTRIBUTES. */
22263 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22264 attributes = TREE_CHAIN (attributes);
22266 TREE_CHAIN (attributes) = NULL_TREE;
22268 token = cp_lexer_peek_token (parser->lexer);
22270 if (token->type == CPP_COMMA)
22272 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22278 cp_parser_consume_semicolon_at_end_of_statement (parser);
22279 token = cp_lexer_peek_token (parser->lexer);
22282 if (token->keyword == RID_AT_END)
22283 cp_parser_error (parser, "expected %<}%>");
22285 /* Do not consume the RID_AT_END, so it will be read again as terminating
22286 the @interface of @implementation. */
22287 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22288 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22290 /* For historical reasons, we accept an optional semicolon. */
22291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22292 cp_lexer_consume_token (parser->lexer);
22295 /* Parse an Objective-C protocol declaration. */
22298 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22300 tree proto, protorefs;
22303 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22306 tok = cp_lexer_peek_token (parser->lexer);
22307 error_at (tok->location, "identifier expected after %<@protocol%>");
22311 /* See if we have a forward declaration or a definition. */
22312 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22314 /* Try a forward declaration first. */
22315 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22317 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22320 cp_parser_consume_semicolon_at_end_of_statement (parser);
22323 /* Ok, we got a full-fledged definition (or at least should). */
22326 proto = cp_parser_identifier (parser);
22327 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22328 objc_start_protocol (proto, protorefs, attributes);
22329 cp_parser_objc_method_prototype_list (parser);
22333 /* Parse an Objective-C superclass or category. */
22336 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22339 cp_token *next = cp_lexer_peek_token (parser->lexer);
22341 *super = *categ = NULL_TREE;
22342 if (next->type == CPP_COLON)
22344 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22345 *super = cp_parser_identifier (parser);
22347 else if (next->type == CPP_OPEN_PAREN)
22349 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22350 *categ = cp_parser_identifier (parser);
22351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22355 /* Parse an Objective-C class interface. */
22358 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22360 tree name, super, categ, protos;
22362 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22363 name = cp_parser_identifier (parser);
22364 if (name == error_mark_node)
22366 /* It's hard to recover because even if valid @interface stuff
22367 is to follow, we can't compile it (or validate it) if we
22368 don't even know which class it refers to. Let's assume this
22369 was a stray '@interface' token in the stream and skip it.
22373 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22374 protos = cp_parser_objc_protocol_refs_opt (parser);
22376 /* We have either a class or a category on our hands. */
22378 objc_start_category_interface (name, categ, protos, attributes);
22381 objc_start_class_interface (name, super, protos, attributes);
22382 /* Handle instance variable declarations, if any. */
22383 cp_parser_objc_class_ivars (parser);
22384 objc_continue_interface ();
22387 cp_parser_objc_method_prototype_list (parser);
22390 /* Parse an Objective-C class implementation. */
22393 cp_parser_objc_class_implementation (cp_parser* parser)
22395 tree name, super, categ;
22397 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22398 name = cp_parser_identifier (parser);
22399 if (name == error_mark_node)
22401 /* It's hard to recover because even if valid @implementation
22402 stuff is to follow, we can't compile it (or validate it) if
22403 we don't even know which class it refers to. Let's assume
22404 this was a stray '@implementation' token in the stream and
22409 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22411 /* We have either a class or a category on our hands. */
22413 objc_start_category_implementation (name, categ);
22416 objc_start_class_implementation (name, super);
22417 /* Handle instance variable declarations, if any. */
22418 cp_parser_objc_class_ivars (parser);
22419 objc_continue_implementation ();
22422 cp_parser_objc_method_definition_list (parser);
22425 /* Consume the @end token and finish off the implementation. */
22428 cp_parser_objc_end_implementation (cp_parser* parser)
22430 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22431 objc_finish_implementation ();
22434 /* Parse an Objective-C declaration. */
22437 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22439 /* Try to figure out what kind of declaration is present. */
22440 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22443 switch (kwd->keyword)
22448 error_at (kwd->location, "attributes may not be specified before"
22449 " the %<@%D%> Objective-C++ keyword",
22453 case RID_AT_IMPLEMENTATION:
22454 warning_at (kwd->location, OPT_Wattributes,
22455 "prefix attributes are ignored before %<@%D%>",
22462 switch (kwd->keyword)
22465 cp_parser_objc_alias_declaration (parser);
22468 cp_parser_objc_class_declaration (parser);
22470 case RID_AT_PROTOCOL:
22471 cp_parser_objc_protocol_declaration (parser, attributes);
22473 case RID_AT_INTERFACE:
22474 cp_parser_objc_class_interface (parser, attributes);
22476 case RID_AT_IMPLEMENTATION:
22477 cp_parser_objc_class_implementation (parser);
22480 cp_parser_objc_end_implementation (parser);
22483 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22485 cp_parser_skip_to_end_of_block_or_statement (parser);
22489 /* Parse an Objective-C try-catch-finally statement.
22491 objc-try-catch-finally-stmt:
22492 @try compound-statement objc-catch-clause-seq [opt]
22493 objc-finally-clause [opt]
22495 objc-catch-clause-seq:
22496 objc-catch-clause objc-catch-clause-seq [opt]
22499 @catch ( exception-declaration ) compound-statement
22501 objc-finally-clause
22502 @finally compound-statement
22504 Returns NULL_TREE. */
22507 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22508 location_t location;
22511 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22512 location = cp_lexer_peek_token (parser->lexer)->location;
22513 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22514 node, lest it get absorbed into the surrounding block. */
22515 stmt = push_stmt_list ();
22516 cp_parser_compound_statement (parser, NULL, false);
22517 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22519 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22521 cp_parameter_declarator *parmdecl;
22524 cp_lexer_consume_token (parser->lexer);
22525 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22526 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22527 parm = grokdeclarator (parmdecl->declarator,
22528 &parmdecl->decl_specifiers,
22529 PARM, /*initialized=*/0,
22530 /*attrlist=*/NULL);
22531 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22532 objc_begin_catch_clause (parm);
22533 cp_parser_compound_statement (parser, NULL, false);
22534 objc_finish_catch_clause ();
22537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22539 cp_lexer_consume_token (parser->lexer);
22540 location = cp_lexer_peek_token (parser->lexer)->location;
22541 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22542 node, lest it get absorbed into the surrounding block. */
22543 stmt = push_stmt_list ();
22544 cp_parser_compound_statement (parser, NULL, false);
22545 objc_build_finally_clause (location, pop_stmt_list (stmt));
22548 return objc_finish_try_stmt ();
22551 /* Parse an Objective-C synchronized statement.
22553 objc-synchronized-stmt:
22554 @synchronized ( expression ) compound-statement
22556 Returns NULL_TREE. */
22559 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22560 location_t location;
22563 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22565 location = cp_lexer_peek_token (parser->lexer)->location;
22566 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22567 lock = cp_parser_expression (parser, false, NULL);
22568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22570 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22571 node, lest it get absorbed into the surrounding block. */
22572 stmt = push_stmt_list ();
22573 cp_parser_compound_statement (parser, NULL, false);
22575 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22578 /* Parse an Objective-C throw statement.
22581 @throw assignment-expression [opt] ;
22583 Returns a constructed '@throw' statement. */
22586 cp_parser_objc_throw_statement (cp_parser *parser) {
22587 tree expr = NULL_TREE;
22588 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22590 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22593 expr = cp_parser_assignment_expression (parser, false, NULL);
22595 cp_parser_consume_semicolon_at_end_of_statement (parser);
22597 return objc_build_throw_stmt (loc, expr);
22600 /* Parse an Objective-C statement. */
22603 cp_parser_objc_statement (cp_parser * parser) {
22604 /* Try to figure out what kind of declaration is present. */
22605 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22607 switch (kwd->keyword)
22610 return cp_parser_objc_try_catch_finally_statement (parser);
22611 case RID_AT_SYNCHRONIZED:
22612 return cp_parser_objc_synchronized_statement (parser);
22614 return cp_parser_objc_throw_statement (parser);
22616 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22618 cp_parser_skip_to_end_of_block_or_statement (parser);
22621 return error_mark_node;
22624 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22625 look ahead to see if an objc keyword follows the attributes. This
22626 is to detect the use of prefix attributes on ObjC @interface and
22630 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22632 cp_lexer_save_tokens (parser->lexer);
22633 *attrib = cp_parser_attributes_opt (parser);
22634 gcc_assert (*attrib);
22635 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22637 cp_lexer_commit_tokens (parser->lexer);
22640 cp_lexer_rollback_tokens (parser->lexer);
22644 /* This routine is a minimal replacement for
22645 c_parser_struct_declaration () used when parsing the list of
22646 types/names or ObjC++ properties. For example, when parsing the
22649 @property (readonly) int a, b, c;
22651 this function is responsible for parsing "int a, int b, int c" and
22652 returning the declarations as CHAIN of DECLs.
22654 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22655 similar parsing. */
22657 cp_parser_objc_struct_declaration (cp_parser *parser)
22659 tree decls = NULL_TREE;
22660 cp_decl_specifier_seq declspecs;
22661 int decl_class_or_enum_p;
22662 tree prefix_attributes;
22664 cp_parser_decl_specifier_seq (parser,
22665 CP_PARSER_FLAGS_NONE,
22667 &decl_class_or_enum_p);
22669 if (declspecs.type == error_mark_node)
22670 return error_mark_node;
22672 /* auto, register, static, extern, mutable. */
22673 if (declspecs.storage_class != sc_none)
22675 cp_parser_error (parser, "invalid type for property");
22676 declspecs.storage_class = sc_none;
22680 if (declspecs.specs[(int) ds_thread])
22682 cp_parser_error (parser, "invalid type for property");
22683 declspecs.specs[(int) ds_thread] = 0;
22687 if (declspecs.specs[(int) ds_typedef])
22689 cp_parser_error (parser, "invalid type for property");
22690 declspecs.specs[(int) ds_typedef] = 0;
22693 prefix_attributes = declspecs.attributes;
22694 declspecs.attributes = NULL_TREE;
22696 /* Keep going until we hit the `;' at the end of the declaration. */
22697 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22699 tree attributes, first_attribute, decl;
22700 cp_declarator *declarator;
22703 /* Parse the declarator. */
22704 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22705 NULL, NULL, false);
22707 /* Look for attributes that apply to the ivar. */
22708 attributes = cp_parser_attributes_opt (parser);
22709 /* Remember which attributes are prefix attributes and
22711 first_attribute = attributes;
22712 /* Combine the attributes. */
22713 attributes = chainon (prefix_attributes, attributes);
22715 decl = grokfield (declarator, &declspecs,
22716 NULL_TREE, /*init_const_expr_p=*/false,
22717 NULL_TREE, attributes);
22719 if (decl == error_mark_node || decl == NULL_TREE)
22720 return error_mark_node;
22722 /* Reset PREFIX_ATTRIBUTES. */
22723 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22724 attributes = TREE_CHAIN (attributes);
22726 TREE_CHAIN (attributes) = NULL_TREE;
22728 DECL_CHAIN (decl) = decls;
22731 token = cp_lexer_peek_token (parser->lexer);
22732 if (token->type == CPP_COMMA)
22734 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22743 /* Parse an Objective-C @property declaration. The syntax is:
22745 objc-property-declaration:
22746 '@property' objc-property-attributes[opt] struct-declaration ;
22748 objc-property-attributes:
22749 '(' objc-property-attribute-list ')'
22751 objc-property-attribute-list:
22752 objc-property-attribute
22753 objc-property-attribute-list, objc-property-attribute
22755 objc-property-attribute
22756 'getter' = identifier
22757 'setter' = identifier
22766 @property NSString *name;
22767 @property (readonly) id object;
22768 @property (retain, nonatomic, getter=getTheName) id name;
22769 @property int a, b, c;
22771 PS: This function is identical to
22772 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22774 cp_parser_objc_at_property_declaration (cp_parser *parser)
22776 /* The following variables hold the attributes of the properties as
22777 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22778 seen. When we see an attribute, we set them to 'true' (if they
22779 are boolean properties) or to the identifier (if they have an
22780 argument, ie, for getter and setter). Note that here we only
22781 parse the list of attributes, check the syntax and accumulate the
22782 attributes that we find. objc_add_property_declaration() will
22783 then process the information. */
22784 bool property_assign = false;
22785 bool property_copy = false;
22786 tree property_getter_ident = NULL_TREE;
22787 bool property_nonatomic = false;
22788 bool property_readonly = false;
22789 bool property_readwrite = false;
22790 bool property_retain = false;
22791 tree property_setter_ident = NULL_TREE;
22793 /* 'properties' is the list of properties that we read. Usually a
22794 single one, but maybe more (eg, in "@property int a, b, c;" there
22799 loc = cp_lexer_peek_token (parser->lexer)->location;
22801 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22803 /* Parse the optional attribute list... */
22804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22807 cp_lexer_consume_token (parser->lexer);
22811 bool syntax_error = false;
22812 cp_token *token = cp_lexer_peek_token (parser->lexer);
22815 if (token->type != CPP_NAME)
22817 cp_parser_error (parser, "expected identifier");
22820 keyword = C_RID_CODE (token->u.value);
22821 cp_lexer_consume_token (parser->lexer);
22824 case RID_ASSIGN: property_assign = true; break;
22825 case RID_COPY: property_copy = true; break;
22826 case RID_NONATOMIC: property_nonatomic = true; break;
22827 case RID_READONLY: property_readonly = true; break;
22828 case RID_READWRITE: property_readwrite = true; break;
22829 case RID_RETAIN: property_retain = true; break;
22833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22835 cp_parser_error (parser,
22836 "getter/setter/ivar attribute must be followed by %<=%>");
22837 syntax_error = true;
22840 cp_lexer_consume_token (parser->lexer); /* eat the = */
22841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22843 cp_parser_error (parser, "expected identifier");
22844 syntax_error = true;
22847 if (keyword == RID_SETTER)
22849 if (property_setter_ident != NULL_TREE)
22850 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22852 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22853 cp_lexer_consume_token (parser->lexer);
22854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22855 cp_parser_error (parser, "setter name must terminate with %<:%>");
22857 cp_lexer_consume_token (parser->lexer);
22861 if (property_getter_ident != NULL_TREE)
22862 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22864 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22865 cp_lexer_consume_token (parser->lexer);
22869 cp_parser_error (parser, "unknown property attribute");
22870 syntax_error = true;
22877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22878 cp_lexer_consume_token (parser->lexer);
22883 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22885 cp_parser_skip_to_closing_parenthesis (parser,
22886 /*recovering=*/true,
22887 /*or_comma=*/false,
22888 /*consume_paren=*/true);
22892 /* ... and the property declaration(s). */
22893 properties = cp_parser_objc_struct_declaration (parser);
22895 if (properties == error_mark_node)
22897 cp_parser_skip_to_end_of_statement (parser);
22898 /* If the next token is now a `;', consume it. */
22899 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22900 cp_lexer_consume_token (parser->lexer);
22904 if (properties == NULL_TREE)
22905 cp_parser_error (parser, "expected identifier");
22908 /* Comma-separated properties are chained together in
22909 reverse order; add them one by one. */
22910 properties = nreverse (properties);
22912 for (; properties; properties = TREE_CHAIN (properties))
22913 objc_add_property_declaration (loc, copy_node (properties),
22914 property_readonly, property_readwrite,
22915 property_assign, property_retain,
22916 property_copy, property_nonatomic,
22917 property_getter_ident, property_setter_ident);
22920 cp_parser_consume_semicolon_at_end_of_statement (parser);
22923 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22925 objc-synthesize-declaration:
22926 @synthesize objc-synthesize-identifier-list ;
22928 objc-synthesize-identifier-list:
22929 objc-synthesize-identifier
22930 objc-synthesize-identifier-list, objc-synthesize-identifier
22932 objc-synthesize-identifier
22934 identifier = identifier
22937 @synthesize MyProperty;
22938 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22940 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22941 for C. Keep them in sync.
22944 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22946 tree list = NULL_TREE;
22948 loc = cp_lexer_peek_token (parser->lexer)->location;
22950 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
22953 tree property, ivar;
22954 property = cp_parser_identifier (parser);
22955 if (property == error_mark_node)
22957 cp_parser_consume_semicolon_at_end_of_statement (parser);
22960 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22962 cp_lexer_consume_token (parser->lexer);
22963 ivar = cp_parser_identifier (parser);
22964 if (ivar == error_mark_node)
22966 cp_parser_consume_semicolon_at_end_of_statement (parser);
22972 list = chainon (list, build_tree_list (ivar, property));
22973 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22974 cp_lexer_consume_token (parser->lexer);
22978 cp_parser_consume_semicolon_at_end_of_statement (parser);
22979 objc_add_synthesize_declaration (loc, list);
22982 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22984 objc-dynamic-declaration:
22985 @dynamic identifier-list ;
22988 @dynamic MyProperty;
22989 @dynamic MyProperty, AnotherProperty;
22991 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22992 for C. Keep them in sync.
22995 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22997 tree list = NULL_TREE;
22999 loc = cp_lexer_peek_token (parser->lexer)->location;
23001 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23005 property = cp_parser_identifier (parser);
23006 if (property == error_mark_node)
23008 cp_parser_consume_semicolon_at_end_of_statement (parser);
23011 list = chainon (list, build_tree_list (NULL, property));
23012 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23013 cp_lexer_consume_token (parser->lexer);
23017 cp_parser_consume_semicolon_at_end_of_statement (parser);
23018 objc_add_dynamic_declaration (loc, list);
23022 /* OpenMP 2.5 parsing routines. */
23024 /* Returns name of the next clause.
23025 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23026 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23027 returned and the token is consumed. */
23029 static pragma_omp_clause
23030 cp_parser_omp_clause_name (cp_parser *parser)
23032 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23034 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23035 result = PRAGMA_OMP_CLAUSE_IF;
23036 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23037 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23038 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23039 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23040 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23042 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23043 const char *p = IDENTIFIER_POINTER (id);
23048 if (!strcmp ("collapse", p))
23049 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23050 else if (!strcmp ("copyin", p))
23051 result = PRAGMA_OMP_CLAUSE_COPYIN;
23052 else if (!strcmp ("copyprivate", p))
23053 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23056 if (!strcmp ("firstprivate", p))
23057 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23060 if (!strcmp ("lastprivate", p))
23061 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23064 if (!strcmp ("nowait", p))
23065 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23066 else if (!strcmp ("num_threads", p))
23067 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23070 if (!strcmp ("ordered", p))
23071 result = PRAGMA_OMP_CLAUSE_ORDERED;
23074 if (!strcmp ("reduction", p))
23075 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23078 if (!strcmp ("schedule", p))
23079 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23080 else if (!strcmp ("shared", p))
23081 result = PRAGMA_OMP_CLAUSE_SHARED;
23084 if (!strcmp ("untied", p))
23085 result = PRAGMA_OMP_CLAUSE_UNTIED;
23090 if (result != PRAGMA_OMP_CLAUSE_NONE)
23091 cp_lexer_consume_token (parser->lexer);
23096 /* Validate that a clause of the given type does not already exist. */
23099 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23100 const char *name, location_t location)
23104 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23105 if (OMP_CLAUSE_CODE (c) == code)
23107 error_at (location, "too many %qs clauses", name);
23115 variable-list , identifier
23117 In addition, we match a closing parenthesis. An opening parenthesis
23118 will have been consumed by the caller.
23120 If KIND is nonzero, create the appropriate node and install the decl
23121 in OMP_CLAUSE_DECL and add the node to the head of the list.
23123 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23124 return the list created. */
23127 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23135 token = cp_lexer_peek_token (parser->lexer);
23136 name = cp_parser_id_expression (parser, /*template_p=*/false,
23137 /*check_dependency_p=*/true,
23138 /*template_p=*/NULL,
23139 /*declarator_p=*/false,
23140 /*optional_p=*/false);
23141 if (name == error_mark_node)
23144 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23145 if (decl == error_mark_node)
23146 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23148 else if (kind != 0)
23150 tree u = build_omp_clause (token->location, kind);
23151 OMP_CLAUSE_DECL (u) = decl;
23152 OMP_CLAUSE_CHAIN (u) = list;
23156 list = tree_cons (decl, NULL_TREE, list);
23159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23161 cp_lexer_consume_token (parser->lexer);
23164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23168 /* Try to resync to an unnested comma. Copied from
23169 cp_parser_parenthesized_expression_list. */
23171 ending = cp_parser_skip_to_closing_parenthesis (parser,
23172 /*recovering=*/true,
23174 /*consume_paren=*/true);
23182 /* Similarly, but expect leading and trailing parenthesis. This is a very
23183 common case for omp clauses. */
23186 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23188 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23189 return cp_parser_omp_var_list_no_open (parser, kind, list);
23194 collapse ( constant-expression ) */
23197 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23203 loc = cp_lexer_peek_token (parser->lexer)->location;
23204 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23207 num = cp_parser_constant_expression (parser, false, NULL);
23209 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23211 /*or_comma=*/false,
23212 /*consume_paren=*/true);
23214 if (num == error_mark_node)
23216 num = fold_non_dependent_expr (num);
23217 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23218 || !host_integerp (num, 0)
23219 || (n = tree_low_cst (num, 0)) <= 0
23222 error_at (loc, "collapse argument needs positive constant integer expression");
23226 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23227 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23228 OMP_CLAUSE_CHAIN (c) = list;
23229 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23235 default ( shared | none ) */
23238 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23240 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23243 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23247 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23248 const char *p = IDENTIFIER_POINTER (id);
23253 if (strcmp ("none", p) != 0)
23255 kind = OMP_CLAUSE_DEFAULT_NONE;
23259 if (strcmp ("shared", p) != 0)
23261 kind = OMP_CLAUSE_DEFAULT_SHARED;
23268 cp_lexer_consume_token (parser->lexer);
23273 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23276 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23278 /*or_comma=*/false,
23279 /*consume_paren=*/true);
23281 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23284 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23285 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23286 OMP_CLAUSE_CHAIN (c) = list;
23287 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23293 if ( expression ) */
23296 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23300 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23303 t = cp_parser_condition (parser);
23305 if (t == error_mark_node
23306 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23307 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23308 /*or_comma=*/false,
23309 /*consume_paren=*/true);
23311 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23313 c = build_omp_clause (location, OMP_CLAUSE_IF);
23314 OMP_CLAUSE_IF_EXPR (c) = t;
23315 OMP_CLAUSE_CHAIN (c) = list;
23324 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23325 tree list, location_t location)
23329 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23331 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23332 OMP_CLAUSE_CHAIN (c) = list;
23337 num_threads ( expression ) */
23340 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23341 location_t location)
23345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23348 t = cp_parser_expression (parser, false, NULL);
23350 if (t == error_mark_node
23351 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23353 /*or_comma=*/false,
23354 /*consume_paren=*/true);
23356 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23357 "num_threads", location);
23359 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23360 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23361 OMP_CLAUSE_CHAIN (c) = list;
23370 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23371 tree list, location_t location)
23375 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23376 "ordered", location);
23378 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23379 OMP_CLAUSE_CHAIN (c) = list;
23384 reduction ( reduction-operator : variable-list )
23386 reduction-operator:
23387 One of: + * - & ^ | && || */
23390 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23392 enum tree_code code;
23395 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23398 switch (cp_lexer_peek_token (parser->lexer)->type)
23410 code = BIT_AND_EXPR;
23413 code = BIT_XOR_EXPR;
23416 code = BIT_IOR_EXPR;
23419 code = TRUTH_ANDIF_EXPR;
23422 code = TRUTH_ORIF_EXPR;
23425 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23426 "%<|%>, %<&&%>, or %<||%>");
23428 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23429 /*or_comma=*/false,
23430 /*consume_paren=*/true);
23433 cp_lexer_consume_token (parser->lexer);
23435 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23438 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23439 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23440 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23446 schedule ( schedule-kind )
23447 schedule ( schedule-kind , expression )
23450 static | dynamic | guided | runtime | auto */
23453 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23457 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23460 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23462 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23465 const char *p = IDENTIFIER_POINTER (id);
23470 if (strcmp ("dynamic", p) != 0)
23472 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23476 if (strcmp ("guided", p) != 0)
23478 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23482 if (strcmp ("runtime", p) != 0)
23484 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23491 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23492 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23493 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23494 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23497 cp_lexer_consume_token (parser->lexer);
23499 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23502 cp_lexer_consume_token (parser->lexer);
23504 token = cp_lexer_peek_token (parser->lexer);
23505 t = cp_parser_assignment_expression (parser, false, NULL);
23507 if (t == error_mark_node)
23509 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23510 error_at (token->location, "schedule %<runtime%> does not take "
23511 "a %<chunk_size%> parameter");
23512 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23513 error_at (token->location, "schedule %<auto%> does not take "
23514 "a %<chunk_size%> parameter");
23516 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23518 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23521 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23524 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23525 OMP_CLAUSE_CHAIN (c) = list;
23529 cp_parser_error (parser, "invalid schedule kind");
23531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23532 /*or_comma=*/false,
23533 /*consume_paren=*/true);
23541 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23542 tree list, location_t location)
23546 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23548 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23549 OMP_CLAUSE_CHAIN (c) = list;
23553 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23554 is a bitmask in MASK. Return the list of clauses found; the result
23555 of clause default goes in *pdefault. */
23558 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23559 const char *where, cp_token *pragma_tok)
23561 tree clauses = NULL;
23563 cp_token *token = NULL;
23565 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23567 pragma_omp_clause c_kind;
23568 const char *c_name;
23569 tree prev = clauses;
23571 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23572 cp_lexer_consume_token (parser->lexer);
23574 token = cp_lexer_peek_token (parser->lexer);
23575 c_kind = cp_parser_omp_clause_name (parser);
23580 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23581 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23583 c_name = "collapse";
23585 case PRAGMA_OMP_CLAUSE_COPYIN:
23586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23589 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23590 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23592 c_name = "copyprivate";
23594 case PRAGMA_OMP_CLAUSE_DEFAULT:
23595 clauses = cp_parser_omp_clause_default (parser, clauses,
23597 c_name = "default";
23599 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23600 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23602 c_name = "firstprivate";
23604 case PRAGMA_OMP_CLAUSE_IF:
23605 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23608 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23609 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23611 c_name = "lastprivate";
23613 case PRAGMA_OMP_CLAUSE_NOWAIT:
23614 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23617 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23618 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23620 c_name = "num_threads";
23622 case PRAGMA_OMP_CLAUSE_ORDERED:
23623 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23625 c_name = "ordered";
23627 case PRAGMA_OMP_CLAUSE_PRIVATE:
23628 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23630 c_name = "private";
23632 case PRAGMA_OMP_CLAUSE_REDUCTION:
23633 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23634 c_name = "reduction";
23636 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23637 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23639 c_name = "schedule";
23641 case PRAGMA_OMP_CLAUSE_SHARED:
23642 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23646 case PRAGMA_OMP_CLAUSE_UNTIED:
23647 clauses = cp_parser_omp_clause_untied (parser, clauses,
23652 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23656 if (((mask >> c_kind) & 1) == 0)
23658 /* Remove the invalid clause(s) from the list to avoid
23659 confusing the rest of the compiler. */
23661 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23665 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23666 return finish_omp_clauses (clauses);
23673 In practice, we're also interested in adding the statement to an
23674 outer node. So it is convenient if we work around the fact that
23675 cp_parser_statement calls add_stmt. */
23678 cp_parser_begin_omp_structured_block (cp_parser *parser)
23680 unsigned save = parser->in_statement;
23682 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23683 This preserves the "not within loop or switch" style error messages
23684 for nonsense cases like
23690 if (parser->in_statement)
23691 parser->in_statement = IN_OMP_BLOCK;
23697 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23699 parser->in_statement = save;
23703 cp_parser_omp_structured_block (cp_parser *parser)
23705 tree stmt = begin_omp_structured_block ();
23706 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23708 cp_parser_statement (parser, NULL_TREE, false, NULL);
23710 cp_parser_end_omp_structured_block (parser, save);
23711 return finish_omp_structured_block (stmt);
23715 # pragma omp atomic new-line
23719 x binop= expr | x++ | ++x | x-- | --x
23721 +, *, -, /, &, ^, |, <<, >>
23723 where x is an lvalue expression with scalar type. */
23726 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23729 enum tree_code code;
23731 cp_parser_require_pragma_eol (parser, pragma_tok);
23733 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23734 /*cast_p=*/false, NULL);
23735 switch (TREE_CODE (lhs))
23740 case PREINCREMENT_EXPR:
23741 case POSTINCREMENT_EXPR:
23742 lhs = TREE_OPERAND (lhs, 0);
23744 rhs = integer_one_node;
23747 case PREDECREMENT_EXPR:
23748 case POSTDECREMENT_EXPR:
23749 lhs = TREE_OPERAND (lhs, 0);
23751 rhs = integer_one_node;
23754 case COMPOUND_EXPR:
23755 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23756 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23757 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23758 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23759 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23760 (TREE_OPERAND (lhs, 1), 0), 0)))
23762 /* Undo effects of boolean_increment for post {in,de}crement. */
23763 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23766 if (TREE_CODE (lhs) == MODIFY_EXPR
23767 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23769 /* Undo effects of boolean_increment. */
23770 if (integer_onep (TREE_OPERAND (lhs, 1)))
23772 /* This is pre or post increment. */
23773 rhs = TREE_OPERAND (lhs, 1);
23774 lhs = TREE_OPERAND (lhs, 0);
23781 switch (cp_lexer_peek_token (parser->lexer)->type)
23787 code = TRUNC_DIV_EXPR;
23795 case CPP_LSHIFT_EQ:
23796 code = LSHIFT_EXPR;
23798 case CPP_RSHIFT_EQ:
23799 code = RSHIFT_EXPR;
23802 code = BIT_AND_EXPR;
23805 code = BIT_IOR_EXPR;
23808 code = BIT_XOR_EXPR;
23811 cp_parser_error (parser,
23812 "invalid operator for %<#pragma omp atomic%>");
23815 cp_lexer_consume_token (parser->lexer);
23817 rhs = cp_parser_expression (parser, false, NULL);
23818 if (rhs == error_mark_node)
23822 finish_omp_atomic (code, lhs, rhs);
23823 cp_parser_consume_semicolon_at_end_of_statement (parser);
23827 cp_parser_skip_to_end_of_block_or_statement (parser);
23832 # pragma omp barrier new-line */
23835 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23837 cp_parser_require_pragma_eol (parser, pragma_tok);
23838 finish_omp_barrier ();
23842 # pragma omp critical [(name)] new-line
23843 structured-block */
23846 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23848 tree stmt, name = NULL;
23850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23852 cp_lexer_consume_token (parser->lexer);
23854 name = cp_parser_identifier (parser);
23856 if (name == error_mark_node
23857 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23859 /*or_comma=*/false,
23860 /*consume_paren=*/true);
23861 if (name == error_mark_node)
23864 cp_parser_require_pragma_eol (parser, pragma_tok);
23866 stmt = cp_parser_omp_structured_block (parser);
23867 return c_finish_omp_critical (input_location, stmt, name);
23871 # pragma omp flush flush-vars[opt] new-line
23874 ( variable-list ) */
23877 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23880 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23881 cp_parser_require_pragma_eol (parser, pragma_tok);
23883 finish_omp_flush ();
23886 /* Helper function, to parse omp for increment expression. */
23889 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23891 tree cond = cp_parser_binary_expression (parser, false, true,
23892 PREC_NOT_OPERATOR, NULL);
23895 if (cond == error_mark_node
23896 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23898 cp_parser_skip_to_end_of_statement (parser);
23899 return error_mark_node;
23902 switch (TREE_CODE (cond))
23910 return error_mark_node;
23913 /* If decl is an iterator, preserve LHS and RHS of the relational
23914 expr until finish_omp_for. */
23916 && (type_dependent_expression_p (decl)
23917 || CLASS_TYPE_P (TREE_TYPE (decl))))
23920 return build_x_binary_op (TREE_CODE (cond),
23921 TREE_OPERAND (cond, 0), ERROR_MARK,
23922 TREE_OPERAND (cond, 1), ERROR_MARK,
23923 &overloaded_p, tf_warning_or_error);
23926 /* Helper function, to parse omp for increment expression. */
23929 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23931 cp_token *token = cp_lexer_peek_token (parser->lexer);
23937 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23939 op = (token->type == CPP_PLUS_PLUS
23940 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23941 cp_lexer_consume_token (parser->lexer);
23942 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23944 return error_mark_node;
23945 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23948 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23950 return error_mark_node;
23952 token = cp_lexer_peek_token (parser->lexer);
23953 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23955 op = (token->type == CPP_PLUS_PLUS
23956 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23957 cp_lexer_consume_token (parser->lexer);
23958 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23961 op = cp_parser_assignment_operator_opt (parser);
23962 if (op == ERROR_MARK)
23963 return error_mark_node;
23965 if (op != NOP_EXPR)
23967 rhs = cp_parser_assignment_expression (parser, false, NULL);
23968 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23969 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23972 lhs = cp_parser_binary_expression (parser, false, false,
23973 PREC_ADDITIVE_EXPRESSION, NULL);
23974 token = cp_lexer_peek_token (parser->lexer);
23975 decl_first = lhs == decl;
23978 if (token->type != CPP_PLUS
23979 && token->type != CPP_MINUS)
23980 return error_mark_node;
23984 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23985 cp_lexer_consume_token (parser->lexer);
23986 rhs = cp_parser_binary_expression (parser, false, false,
23987 PREC_ADDITIVE_EXPRESSION, NULL);
23988 token = cp_lexer_peek_token (parser->lexer);
23989 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23991 if (lhs == NULL_TREE)
23993 if (op == PLUS_EXPR)
23996 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23999 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24000 NULL, tf_warning_or_error);
24003 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24007 if (rhs != decl || op == MINUS_EXPR)
24008 return error_mark_node;
24009 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24012 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24014 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24017 /* Parse the restricted form of the for statement allowed by OpenMP. */
24020 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24022 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24023 tree real_decl, initv, condv, incrv, declv;
24024 tree this_pre_body, cl;
24025 location_t loc_first;
24026 bool collapse_err = false;
24027 int i, collapse = 1, nbraces = 0;
24028 VEC(tree,gc) *for_block = make_tree_vector ();
24030 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24031 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24032 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24034 gcc_assert (collapse >= 1);
24036 declv = make_tree_vec (collapse);
24037 initv = make_tree_vec (collapse);
24038 condv = make_tree_vec (collapse);
24039 incrv = make_tree_vec (collapse);
24041 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24043 for (i = 0; i < collapse; i++)
24045 int bracecount = 0;
24046 bool add_private_clause = false;
24049 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24051 cp_parser_error (parser, "for statement expected");
24054 loc = cp_lexer_consume_token (parser->lexer)->location;
24056 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24059 init = decl = real_decl = NULL;
24060 this_pre_body = push_stmt_list ();
24061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24063 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24067 integer-type var = lb
24068 random-access-iterator-type var = lb
24069 pointer-type var = lb
24071 cp_decl_specifier_seq type_specifiers;
24073 /* First, try to parse as an initialized declaration. See
24074 cp_parser_condition, from whence the bulk of this is copied. */
24076 cp_parser_parse_tentatively (parser);
24077 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24078 /*is_trailing_return=*/false,
24080 if (cp_parser_parse_definitely (parser))
24082 /* If parsing a type specifier seq succeeded, then this
24083 MUST be a initialized declaration. */
24084 tree asm_specification, attributes;
24085 cp_declarator *declarator;
24087 declarator = cp_parser_declarator (parser,
24088 CP_PARSER_DECLARATOR_NAMED,
24089 /*ctor_dtor_or_conv_p=*/NULL,
24090 /*parenthesized_p=*/NULL,
24091 /*member_p=*/false);
24092 attributes = cp_parser_attributes_opt (parser);
24093 asm_specification = cp_parser_asm_specification_opt (parser);
24095 if (declarator == cp_error_declarator)
24096 cp_parser_skip_to_end_of_statement (parser);
24100 tree pushed_scope, auto_node;
24102 decl = start_decl (declarator, &type_specifiers,
24103 SD_INITIALIZED, attributes,
24104 /*prefix_attributes=*/NULL_TREE,
24107 auto_node = type_uses_auto (TREE_TYPE (decl));
24108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24110 if (cp_lexer_next_token_is (parser->lexer,
24112 error ("parenthesized initialization is not allowed in "
24113 "OpenMP %<for%> loop");
24115 /* Trigger an error. */
24116 cp_parser_require (parser, CPP_EQ, RT_EQ);
24118 init = error_mark_node;
24119 cp_parser_skip_to_end_of_statement (parser);
24121 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24122 || type_dependent_expression_p (decl)
24125 bool is_direct_init, is_non_constant_init;
24127 init = cp_parser_initializer (parser,
24129 &is_non_constant_init);
24131 if (auto_node && describable_type (init))
24134 = do_auto_deduction (TREE_TYPE (decl), init,
24137 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24138 && !type_dependent_expression_p (decl))
24142 cp_finish_decl (decl, init, !is_non_constant_init,
24144 LOOKUP_ONLYCONVERTING);
24145 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24147 VEC_safe_push (tree, gc, for_block, this_pre_body);
24151 init = pop_stmt_list (this_pre_body);
24152 this_pre_body = NULL_TREE;
24157 cp_lexer_consume_token (parser->lexer);
24158 init = cp_parser_assignment_expression (parser, false, NULL);
24161 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24162 init = error_mark_node;
24164 cp_finish_decl (decl, NULL_TREE,
24165 /*init_const_expr_p=*/false,
24167 LOOKUP_ONLYCONVERTING);
24171 pop_scope (pushed_scope);
24177 /* If parsing a type specifier sequence failed, then
24178 this MUST be a simple expression. */
24179 cp_parser_parse_tentatively (parser);
24180 decl = cp_parser_primary_expression (parser, false, false,
24182 if (!cp_parser_error_occurred (parser)
24185 && CLASS_TYPE_P (TREE_TYPE (decl)))
24189 cp_parser_parse_definitely (parser);
24190 cp_parser_require (parser, CPP_EQ, RT_EQ);
24191 rhs = cp_parser_assignment_expression (parser, false, NULL);
24192 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24194 tf_warning_or_error));
24195 add_private_clause = true;
24200 cp_parser_abort_tentative_parse (parser);
24201 init = cp_parser_expression (parser, false, NULL);
24204 if (TREE_CODE (init) == MODIFY_EXPR
24205 || TREE_CODE (init) == MODOP_EXPR)
24206 real_decl = TREE_OPERAND (init, 0);
24211 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24214 this_pre_body = pop_stmt_list (this_pre_body);
24218 pre_body = push_stmt_list ();
24220 add_stmt (this_pre_body);
24221 pre_body = pop_stmt_list (pre_body);
24224 pre_body = this_pre_body;
24229 if (par_clauses != NULL && real_decl != NULL_TREE)
24232 for (c = par_clauses; *c ; )
24233 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24234 && OMP_CLAUSE_DECL (*c) == real_decl)
24236 error_at (loc, "iteration variable %qD"
24237 " should not be firstprivate", real_decl);
24238 *c = OMP_CLAUSE_CHAIN (*c);
24240 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24241 && OMP_CLAUSE_DECL (*c) == real_decl)
24243 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24244 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24245 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24246 OMP_CLAUSE_DECL (l) = real_decl;
24247 OMP_CLAUSE_CHAIN (l) = clauses;
24248 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24250 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24251 CP_OMP_CLAUSE_INFO (*c) = NULL;
24252 add_private_clause = false;
24256 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24257 && OMP_CLAUSE_DECL (*c) == real_decl)
24258 add_private_clause = false;
24259 c = &OMP_CLAUSE_CHAIN (*c);
24263 if (add_private_clause)
24266 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24268 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24269 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24270 && OMP_CLAUSE_DECL (c) == decl)
24272 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24273 && OMP_CLAUSE_DECL (c) == decl)
24274 error_at (loc, "iteration variable %qD "
24275 "should not be firstprivate",
24277 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24278 && OMP_CLAUSE_DECL (c) == decl)
24279 error_at (loc, "iteration variable %qD should not be reduction",
24284 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24285 OMP_CLAUSE_DECL (c) = decl;
24286 c = finish_omp_clauses (c);
24289 OMP_CLAUSE_CHAIN (c) = clauses;
24296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24297 cond = cp_parser_omp_for_cond (parser, decl);
24298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24303 /* If decl is an iterator, preserve the operator on decl
24304 until finish_omp_for. */
24306 && (type_dependent_expression_p (decl)
24307 || CLASS_TYPE_P (TREE_TYPE (decl))))
24308 incr = cp_parser_omp_for_incr (parser, decl);
24310 incr = cp_parser_expression (parser, false, NULL);
24313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24315 /*or_comma=*/false,
24316 /*consume_paren=*/true);
24318 TREE_VEC_ELT (declv, i) = decl;
24319 TREE_VEC_ELT (initv, i) = init;
24320 TREE_VEC_ELT (condv, i) = cond;
24321 TREE_VEC_ELT (incrv, i) = incr;
24323 if (i == collapse - 1)
24326 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24327 in between the collapsed for loops to be still considered perfectly
24328 nested. Hopefully the final version clarifies this.
24329 For now handle (multiple) {'s and empty statements. */
24330 cp_parser_parse_tentatively (parser);
24333 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24335 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24337 cp_lexer_consume_token (parser->lexer);
24340 else if (bracecount
24341 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24342 cp_lexer_consume_token (parser->lexer);
24345 loc = cp_lexer_peek_token (parser->lexer)->location;
24346 error_at (loc, "not enough collapsed for loops");
24347 collapse_err = true;
24348 cp_parser_abort_tentative_parse (parser);
24357 cp_parser_parse_definitely (parser);
24358 nbraces += bracecount;
24362 /* Note that we saved the original contents of this flag when we entered
24363 the structured block, and so we don't need to re-save it here. */
24364 parser->in_statement = IN_OMP_FOR;
24366 /* Note that the grammar doesn't call for a structured block here,
24367 though the loop as a whole is a structured block. */
24368 body = push_stmt_list ();
24369 cp_parser_statement (parser, NULL_TREE, false, NULL);
24370 body = pop_stmt_list (body);
24372 if (declv == NULL_TREE)
24375 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24376 pre_body, clauses);
24380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24382 cp_lexer_consume_token (parser->lexer);
24385 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24386 cp_lexer_consume_token (parser->lexer);
24391 error_at (cp_lexer_peek_token (parser->lexer)->location,
24392 "collapsed loops not perfectly nested");
24394 collapse_err = true;
24395 cp_parser_statement_seq_opt (parser, NULL);
24396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24401 while (!VEC_empty (tree, for_block))
24402 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24403 release_tree_vector (for_block);
24409 #pragma omp for for-clause[optseq] new-line
24412 #define OMP_FOR_CLAUSE_MASK \
24413 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24414 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24415 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24416 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24417 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24418 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24419 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24420 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24423 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24425 tree clauses, sb, ret;
24428 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24429 "#pragma omp for", pragma_tok);
24431 sb = begin_omp_structured_block ();
24432 save = cp_parser_begin_omp_structured_block (parser);
24434 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24436 cp_parser_end_omp_structured_block (parser, save);
24437 add_stmt (finish_omp_structured_block (sb));
24443 # pragma omp master new-line
24444 structured-block */
24447 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24449 cp_parser_require_pragma_eol (parser, pragma_tok);
24450 return c_finish_omp_master (input_location,
24451 cp_parser_omp_structured_block (parser));
24455 # pragma omp ordered new-line
24456 structured-block */
24459 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24461 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24462 cp_parser_require_pragma_eol (parser, pragma_tok);
24463 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24469 { section-sequence }
24472 section-directive[opt] structured-block
24473 section-sequence section-directive structured-block */
24476 cp_parser_omp_sections_scope (cp_parser *parser)
24478 tree stmt, substmt;
24479 bool error_suppress = false;
24482 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24485 stmt = push_stmt_list ();
24487 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24491 substmt = begin_omp_structured_block ();
24492 save = cp_parser_begin_omp_structured_block (parser);
24496 cp_parser_statement (parser, NULL_TREE, false, NULL);
24498 tok = cp_lexer_peek_token (parser->lexer);
24499 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24501 if (tok->type == CPP_CLOSE_BRACE)
24503 if (tok->type == CPP_EOF)
24507 cp_parser_end_omp_structured_block (parser, save);
24508 substmt = finish_omp_structured_block (substmt);
24509 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24510 add_stmt (substmt);
24515 tok = cp_lexer_peek_token (parser->lexer);
24516 if (tok->type == CPP_CLOSE_BRACE)
24518 if (tok->type == CPP_EOF)
24521 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24523 cp_lexer_consume_token (parser->lexer);
24524 cp_parser_require_pragma_eol (parser, tok);
24525 error_suppress = false;
24527 else if (!error_suppress)
24529 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24530 error_suppress = true;
24533 substmt = cp_parser_omp_structured_block (parser);
24534 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24535 add_stmt (substmt);
24537 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24539 substmt = pop_stmt_list (stmt);
24541 stmt = make_node (OMP_SECTIONS);
24542 TREE_TYPE (stmt) = void_type_node;
24543 OMP_SECTIONS_BODY (stmt) = substmt;
24550 # pragma omp sections sections-clause[optseq] newline
24553 #define OMP_SECTIONS_CLAUSE_MASK \
24554 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24555 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24556 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24557 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24558 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24561 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24565 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24566 "#pragma omp sections", pragma_tok);
24568 ret = cp_parser_omp_sections_scope (parser);
24570 OMP_SECTIONS_CLAUSES (ret) = clauses;
24576 # pragma parallel parallel-clause new-line
24577 # pragma parallel for parallel-for-clause new-line
24578 # pragma parallel sections parallel-sections-clause new-line */
24580 #define OMP_PARALLEL_CLAUSE_MASK \
24581 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24582 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24583 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24584 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24585 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24586 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24587 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24588 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24591 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24593 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24594 const char *p_name = "#pragma omp parallel";
24595 tree stmt, clauses, par_clause, ws_clause, block;
24596 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24602 cp_lexer_consume_token (parser->lexer);
24603 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24604 p_name = "#pragma omp parallel for";
24605 mask |= OMP_FOR_CLAUSE_MASK;
24606 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24608 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24611 const char *p = IDENTIFIER_POINTER (id);
24612 if (strcmp (p, "sections") == 0)
24614 cp_lexer_consume_token (parser->lexer);
24615 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24616 p_name = "#pragma omp parallel sections";
24617 mask |= OMP_SECTIONS_CLAUSE_MASK;
24618 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24622 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24623 block = begin_omp_parallel ();
24624 save = cp_parser_begin_omp_structured_block (parser);
24628 case PRAGMA_OMP_PARALLEL:
24629 cp_parser_statement (parser, NULL_TREE, false, NULL);
24630 par_clause = clauses;
24633 case PRAGMA_OMP_PARALLEL_FOR:
24634 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24635 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24638 case PRAGMA_OMP_PARALLEL_SECTIONS:
24639 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24640 stmt = cp_parser_omp_sections_scope (parser);
24642 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24646 gcc_unreachable ();
24649 cp_parser_end_omp_structured_block (parser, save);
24650 stmt = finish_omp_parallel (par_clause, block);
24651 if (p_kind != PRAGMA_OMP_PARALLEL)
24652 OMP_PARALLEL_COMBINED (stmt) = 1;
24657 # pragma omp single single-clause[optseq] new-line
24658 structured-block */
24660 #define OMP_SINGLE_CLAUSE_MASK \
24661 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24662 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24663 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24664 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24667 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24669 tree stmt = make_node (OMP_SINGLE);
24670 TREE_TYPE (stmt) = void_type_node;
24672 OMP_SINGLE_CLAUSES (stmt)
24673 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24674 "#pragma omp single", pragma_tok);
24675 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24677 return add_stmt (stmt);
24681 # pragma omp task task-clause[optseq] new-line
24682 structured-block */
24684 #define OMP_TASK_CLAUSE_MASK \
24685 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24686 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24687 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24688 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24689 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24690 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24693 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24695 tree clauses, block;
24698 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24699 "#pragma omp task", pragma_tok);
24700 block = begin_omp_task ();
24701 save = cp_parser_begin_omp_structured_block (parser);
24702 cp_parser_statement (parser, NULL_TREE, false, NULL);
24703 cp_parser_end_omp_structured_block (parser, save);
24704 return finish_omp_task (clauses, block);
24708 # pragma omp taskwait new-line */
24711 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24713 cp_parser_require_pragma_eol (parser, pragma_tok);
24714 finish_omp_taskwait ();
24718 # pragma omp threadprivate (variable-list) */
24721 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24725 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24726 cp_parser_require_pragma_eol (parser, pragma_tok);
24728 finish_omp_threadprivate (vars);
24731 /* Main entry point to OpenMP statement pragmas. */
24734 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24738 switch (pragma_tok->pragma_kind)
24740 case PRAGMA_OMP_ATOMIC:
24741 cp_parser_omp_atomic (parser, pragma_tok);
24743 case PRAGMA_OMP_CRITICAL:
24744 stmt = cp_parser_omp_critical (parser, pragma_tok);
24746 case PRAGMA_OMP_FOR:
24747 stmt = cp_parser_omp_for (parser, pragma_tok);
24749 case PRAGMA_OMP_MASTER:
24750 stmt = cp_parser_omp_master (parser, pragma_tok);
24752 case PRAGMA_OMP_ORDERED:
24753 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24755 case PRAGMA_OMP_PARALLEL:
24756 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24758 case PRAGMA_OMP_SECTIONS:
24759 stmt = cp_parser_omp_sections (parser, pragma_tok);
24761 case PRAGMA_OMP_SINGLE:
24762 stmt = cp_parser_omp_single (parser, pragma_tok);
24764 case PRAGMA_OMP_TASK:
24765 stmt = cp_parser_omp_task (parser, pragma_tok);
24768 gcc_unreachable ();
24772 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24777 static GTY (()) cp_parser *the_parser;
24780 /* Special handling for the first token or line in the file. The first
24781 thing in the file might be #pragma GCC pch_preprocess, which loads a
24782 PCH file, which is a GC collection point. So we need to handle this
24783 first pragma without benefit of an existing lexer structure.
24785 Always returns one token to the caller in *FIRST_TOKEN. This is
24786 either the true first token of the file, or the first token after
24787 the initial pragma. */
24790 cp_parser_initial_pragma (cp_token *first_token)
24794 cp_lexer_get_preprocessor_token (NULL, first_token);
24795 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24798 cp_lexer_get_preprocessor_token (NULL, first_token);
24799 if (first_token->type == CPP_STRING)
24801 name = first_token->u.value;
24803 cp_lexer_get_preprocessor_token (NULL, first_token);
24804 if (first_token->type != CPP_PRAGMA_EOL)
24805 error_at (first_token->location,
24806 "junk at end of %<#pragma GCC pch_preprocess%>");
24809 error_at (first_token->location, "expected string literal");
24811 /* Skip to the end of the pragma. */
24812 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24813 cp_lexer_get_preprocessor_token (NULL, first_token);
24815 /* Now actually load the PCH file. */
24817 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24819 /* Read one more token to return to our caller. We have to do this
24820 after reading the PCH file in, since its pointers have to be
24822 cp_lexer_get_preprocessor_token (NULL, first_token);
24825 /* Normal parsing of a pragma token. Here we can (and must) use the
24829 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24831 cp_token *pragma_tok;
24834 pragma_tok = cp_lexer_consume_token (parser->lexer);
24835 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24836 parser->lexer->in_pragma = true;
24838 id = pragma_tok->pragma_kind;
24841 case PRAGMA_GCC_PCH_PREPROCESS:
24842 error_at (pragma_tok->location,
24843 "%<#pragma GCC pch_preprocess%> must be first");
24846 case PRAGMA_OMP_BARRIER:
24849 case pragma_compound:
24850 cp_parser_omp_barrier (parser, pragma_tok);
24853 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24854 "used in compound statements");
24861 case PRAGMA_OMP_FLUSH:
24864 case pragma_compound:
24865 cp_parser_omp_flush (parser, pragma_tok);
24868 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24869 "used in compound statements");
24876 case PRAGMA_OMP_TASKWAIT:
24879 case pragma_compound:
24880 cp_parser_omp_taskwait (parser, pragma_tok);
24883 error_at (pragma_tok->location,
24884 "%<#pragma omp taskwait%> may only be "
24885 "used in compound statements");
24892 case PRAGMA_OMP_THREADPRIVATE:
24893 cp_parser_omp_threadprivate (parser, pragma_tok);
24896 case PRAGMA_OMP_ATOMIC:
24897 case PRAGMA_OMP_CRITICAL:
24898 case PRAGMA_OMP_FOR:
24899 case PRAGMA_OMP_MASTER:
24900 case PRAGMA_OMP_ORDERED:
24901 case PRAGMA_OMP_PARALLEL:
24902 case PRAGMA_OMP_SECTIONS:
24903 case PRAGMA_OMP_SINGLE:
24904 case PRAGMA_OMP_TASK:
24905 if (context == pragma_external)
24907 cp_parser_omp_construct (parser, pragma_tok);
24910 case PRAGMA_OMP_SECTION:
24911 error_at (pragma_tok->location,
24912 "%<#pragma omp section%> may only be used in "
24913 "%<#pragma omp sections%> construct");
24917 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24918 c_invoke_pragma_handler (id);
24922 cp_parser_error (parser, "expected declaration specifiers");
24926 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24930 /* The interface the pragma parsers have to the lexer. */
24933 pragma_lex (tree *value)
24936 enum cpp_ttype ret;
24938 tok = cp_lexer_peek_token (the_parser->lexer);
24941 *value = tok->u.value;
24943 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24945 else if (ret == CPP_STRING)
24946 *value = cp_parser_string_literal (the_parser, false, false);
24949 cp_lexer_consume_token (the_parser->lexer);
24950 if (ret == CPP_KEYWORD)
24958 /* External interface. */
24960 /* Parse one entire translation unit. */
24963 c_parse_file (void)
24965 static bool already_called = false;
24967 if (already_called)
24969 sorry ("inter-module optimizations not implemented for C++");
24972 already_called = true;
24974 the_parser = cp_parser_new ();
24975 push_deferring_access_checks (flag_access_control
24976 ? dk_no_deferred : dk_no_check);
24977 cp_parser_translation_unit (the_parser);
24981 #include "gt-cp-parser.h"