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 /* nonzero if we are presently saving tokens. */
508 cp_lexer_saving_tokens (const cp_lexer* lexer)
510 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
519 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
521 static int is_extern_c = 0;
523 /* Get a new token from the preprocessor. */
525 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
526 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
527 token->keyword = RID_MAX;
528 token->pragma_kind = PRAGMA_NONE;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c += pending_lang_change;
534 pending_lang_change = 0;
535 token->implicit_extern_c = is_extern_c > 0;
537 /* Check to see if this token is a keyword. */
538 if (token->type == CPP_NAME)
540 if (C_IS_RESERVED_WORD (token->u.value))
542 /* Mark this token as a keyword. */
543 token->type = CPP_KEYWORD;
544 /* Record which keyword. */
545 token->keyword = C_RID_CODE (token->u.value);
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
553 /* Warn about the C++0x keyword (but still treat it as
555 warning (OPT_Wc__0x_compat,
556 "identifier %qE will become a keyword in C++0x",
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token->u.value, RID_MAX);
564 token->ambiguous_p = false;
565 token->keyword = RID_MAX;
568 else if (token->type == CPP_AT_NAME)
570 /* This only happens in Objective-C++; it must be a keyword. */
571 token->type = CPP_KEYWORD;
572 switch (C_RID_CODE (token->u.value))
574 /* Replace 'class' with '@class', 'private' with '@private',
575 etc. This prevents confusion with the C++ keyword
576 'class', and makes the tokens consistent with other
577 Objective-C 'AT' keywords. For example '@class' is
578 reported as RID_AT_CLASS which is consistent with
579 '@synchronized', which is reported as
582 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
583 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
584 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
585 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
586 case RID_THROW: token->keyword = RID_AT_THROW; break;
587 case RID_TRY: token->keyword = RID_AT_TRY; break;
588 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
589 default: token->keyword = C_RID_CODE (token->u.value);
592 else if (token->type == CPP_PRAGMA)
594 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
595 token->pragma_kind = ((enum pragma_kind)
596 TREE_INT_CST_LOW (token->u.value));
597 token->u.value = NULL_TREE;
601 /* Update the globals input_location and the input file stack from TOKEN. */
603 cp_lexer_set_source_position_from_token (cp_token *token)
605 if (token->type != CPP_EOF)
607 input_location = token->location;
611 /* Return a pointer to the next token in the token stream, but do not
614 static inline cp_token *
615 cp_lexer_peek_token (cp_lexer *lexer)
617 if (cp_lexer_debugging_p (lexer))
619 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
620 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
621 putc ('\n', cp_lexer_debug_stream);
623 return lexer->next_token;
626 /* Return true if the next token has the indicated TYPE. */
629 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
631 return cp_lexer_peek_token (lexer)->type == type;
634 /* Return true if the next token does not have the indicated TYPE. */
637 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
639 return !cp_lexer_next_token_is (lexer, type);
642 /* Return true if the next token is the indicated KEYWORD. */
645 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
647 return cp_lexer_peek_token (lexer)->keyword == keyword;
650 /* Return true if the next token is not the indicated KEYWORD. */
653 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
655 return cp_lexer_peek_token (lexer)->keyword != keyword;
658 /* Return true if the next token is a keyword for a decl-specifier. */
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
665 token = cp_lexer_peek_token (lexer);
666 switch (token->keyword)
668 /* auto specifier: storage-class-specifier in C++,
669 simple-type-specifier in C++0x. */
671 /* Storage classes. */
677 /* Elaborated type specifiers. */
683 /* Simple type specifiers. */
698 /* GNU extensions. */
701 /* C++0x extensions. */
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token (except
712 that it is not inline). One would like to disallow that case, but
713 there is one case (cp_parser_nth_token_starts_template_id) where
714 the caller passes a variable for N and it might be 1. */
717 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
721 /* N is 1-based, not zero-based. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream,
726 "cp_lexer: peeking ahead %ld at token: ", (long)n);
729 token = lexer->next_token;
730 gcc_assert (!n || token != &eof_token);
734 if (token == lexer->last_token)
740 if (token->type != CPP_PURGED)
744 if (cp_lexer_debugging_p (lexer))
746 cp_lexer_print_token (cp_lexer_debug_stream, token);
747 putc ('\n', cp_lexer_debug_stream);
753 /* Return the next token, and advance the lexer's next_token pointer
754 to point to the next non-purged token. */
757 cp_lexer_consume_token (cp_lexer* lexer)
759 cp_token *token = lexer->next_token;
761 gcc_assert (token != &eof_token);
762 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
767 if (lexer->next_token == lexer->last_token)
769 lexer->next_token = &eof_token;
774 while (lexer->next_token->type == CPP_PURGED);
776 cp_lexer_set_source_position_from_token (token);
778 /* Provide debugging output. */
779 if (cp_lexer_debugging_p (lexer))
781 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
782 cp_lexer_print_token (cp_lexer_debug_stream, token);
783 putc ('\n', cp_lexer_debug_stream);
789 /* Permanently remove the next token from the token stream, and
790 advance the next_token pointer to refer to the next non-purged
794 cp_lexer_purge_token (cp_lexer *lexer)
796 cp_token *tok = lexer->next_token;
798 gcc_assert (tok != &eof_token);
799 tok->type = CPP_PURGED;
800 tok->location = UNKNOWN_LOCATION;
801 tok->u.value = NULL_TREE;
802 tok->keyword = RID_MAX;
807 if (tok == lexer->last_token)
813 while (tok->type == CPP_PURGED);
814 lexer->next_token = tok;
817 /* Permanently remove all tokens after TOK, up to, but not
818 including, the token that will be returned next by
819 cp_lexer_peek_token. */
822 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
824 cp_token *peek = lexer->next_token;
826 if (peek == &eof_token)
827 peek = lexer->last_token;
829 gcc_assert (tok < peek);
831 for ( tok += 1; tok != peek; tok += 1)
833 tok->type = CPP_PURGED;
834 tok->location = UNKNOWN_LOCATION;
835 tok->u.value = NULL_TREE;
836 tok->keyword = RID_MAX;
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 VEC_safe_push (cp_token_position, heap,
851 lexer->saved_tokens, lexer->next_token);
854 /* Commit to the portion of the token stream most recently saved. */
857 cp_lexer_commit_tokens (cp_lexer* lexer)
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer))
861 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
863 VEC_pop (cp_token_position, lexer->saved_tokens);
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867 to the token stream. Stop saving tokens. */
870 cp_lexer_rollback_tokens (cp_lexer* lexer)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
876 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
879 /* Print a representation of the TOKEN on the STREAM. */
881 #ifdef ENABLE_CHECKING
884 cp_lexer_print_token (FILE * stream, cp_token *token)
886 /* We don't use cpp_type2name here because the parser defines
887 a few tokens of its own. */
888 static const char *const token_names[] = {
889 /* cpplib-defined token types */
895 /* C++ parser token types - see "Manifest constants", above. */
898 "NESTED_NAME_SPECIFIER",
902 /* If we have a name for the token, print it out. Otherwise, we
903 simply give the numeric code. */
904 gcc_assert (token->type < ARRAY_SIZE(token_names));
905 fputs (token_names[token->type], stream);
907 /* For some tokens, print the associated data. */
911 /* Some keywords have a value that is not an IDENTIFIER_NODE.
912 For example, `struct' is mapped to an INTEGER_CST. */
913 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
915 /* else fall through */
917 fputs (IDENTIFIER_POINTER (token->u.value), stream);
925 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
933 /* Start emitting debugging information. */
936 cp_lexer_start_debugging (cp_lexer* lexer)
938 lexer->debugging_p = true;
941 /* Stop emitting debugging information. */
944 cp_lexer_stop_debugging (cp_lexer* lexer)
946 lexer->debugging_p = false;
949 #endif /* ENABLE_CHECKING */
951 /* Create a new cp_token_cache, representing a range of tokens. */
953 static cp_token_cache *
954 cp_token_cache_new (cp_token *first, cp_token *last)
956 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
957 cache->first = first;
963 /* Decl-specifiers. */
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
968 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
970 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
975 /* Nothing other than the parser should be creating declarators;
976 declarators are a semi-syntactic representation of C++ entities.
977 Other parts of the front end that need to create entities (like
978 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
980 static cp_declarator *make_call_declarator
981 (cp_declarator *, tree, cp_cv_quals, tree, tree);
982 static cp_declarator *make_array_declarator
983 (cp_declarator *, tree);
984 static cp_declarator *make_pointer_declarator
985 (cp_cv_quals, cp_declarator *);
986 static cp_declarator *make_reference_declarator
987 (cp_cv_quals, cp_declarator *, bool);
988 static cp_parameter_declarator *make_parameter_declarator
989 (cp_decl_specifier_seq *, cp_declarator *, tree);
990 static cp_declarator *make_ptrmem_declarator
991 (cp_cv_quals, tree, cp_declarator *);
993 /* An erroneous declarator. */
994 static cp_declarator *cp_error_declarator;
996 /* The obstack on which declarators and related data structures are
998 static struct obstack declarator_obstack;
1000 /* Alloc BYTES from the declarator memory pool. */
1002 static inline void *
1003 alloc_declarator (size_t bytes)
1005 return obstack_alloc (&declarator_obstack, bytes);
1008 /* Allocate a declarator of the indicated KIND. Clear fields that are
1009 common to all declarators. */
1011 static cp_declarator *
1012 make_declarator (cp_declarator_kind kind)
1014 cp_declarator *declarator;
1016 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1017 declarator->kind = kind;
1018 declarator->attributes = NULL_TREE;
1019 declarator->declarator = NULL;
1020 declarator->parameter_pack_p = false;
1021 declarator->id_loc = UNKNOWN_LOCATION;
1026 /* Make a declarator for a generalized identifier. If
1027 QUALIFYING_SCOPE is non-NULL, the identifier is
1028 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1032 static cp_declarator *
1033 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1034 special_function_kind sfk)
1036 cp_declarator *declarator;
1038 /* It is valid to write:
1040 class C { void f(); };
1044 The standard is not clear about whether `typedef const C D' is
1045 legal; as of 2002-09-15 the committee is considering that
1046 question. EDG 3.0 allows that syntax. Therefore, we do as
1048 if (qualifying_scope && TYPE_P (qualifying_scope))
1049 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1051 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1052 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1053 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1055 declarator = make_declarator (cdk_id);
1056 declarator->u.id.qualifying_scope = qualifying_scope;
1057 declarator->u.id.unqualified_name = unqualified_name;
1058 declarator->u.id.sfk = sfk;
1063 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1064 of modifiers such as const or volatile to apply to the pointer
1065 type, represented as identifiers. */
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1070 cp_declarator *declarator;
1072 declarator = make_declarator (cdk_pointer);
1073 declarator->declarator = target;
1074 declarator->u.pointer.qualifiers = cv_qualifiers;
1075 declarator->u.pointer.class_type = NULL_TREE;
1078 declarator->id_loc = target->id_loc;
1079 declarator->parameter_pack_p = target->parameter_pack_p;
1080 target->parameter_pack_p = false;
1083 declarator->parameter_pack_p = false;
1088 /* Like make_pointer_declarator -- but for references. */
1091 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1094 cp_declarator *declarator;
1096 declarator = make_declarator (cdk_reference);
1097 declarator->declarator = target;
1098 declarator->u.reference.qualifiers = cv_qualifiers;
1099 declarator->u.reference.rvalue_ref = rvalue_ref;
1102 declarator->id_loc = target->id_loc;
1103 declarator->parameter_pack_p = target->parameter_pack_p;
1104 target->parameter_pack_p = false;
1107 declarator->parameter_pack_p = false;
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113 member of CLASS_TYPE. */
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1117 cp_declarator *pointee)
1119 cp_declarator *declarator;
1121 declarator = make_declarator (cdk_ptrmem);
1122 declarator->declarator = pointee;
1123 declarator->u.pointer.qualifiers = cv_qualifiers;
1124 declarator->u.pointer.class_type = class_type;
1128 declarator->parameter_pack_p = pointee->parameter_pack_p;
1129 pointee->parameter_pack_p = false;
1132 declarator->parameter_pack_p = false;
1137 /* Make a declarator for the function given by TARGET, with the
1138 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1139 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1140 indicates what exceptions can be thrown. */
1143 make_call_declarator (cp_declarator *target,
1145 cp_cv_quals cv_qualifiers,
1146 tree exception_specification,
1147 tree late_return_type)
1149 cp_declarator *declarator;
1151 declarator = make_declarator (cdk_function);
1152 declarator->declarator = target;
1153 declarator->u.function.parameters = parms;
1154 declarator->u.function.qualifiers = cv_qualifiers;
1155 declarator->u.function.exception_specification = exception_specification;
1156 declarator->u.function.late_return_type = late_return_type;
1159 declarator->id_loc = target->id_loc;
1160 declarator->parameter_pack_p = target->parameter_pack_p;
1161 target->parameter_pack_p = false;
1164 declarator->parameter_pack_p = false;
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170 defined by ELEMENT. */
1173 make_array_declarator (cp_declarator *element, tree bounds)
1175 cp_declarator *declarator;
1177 declarator = make_declarator (cdk_array);
1178 declarator->declarator = element;
1179 declarator->u.array.bounds = bounds;
1182 declarator->id_loc = element->id_loc;
1183 declarator->parameter_pack_p = element->parameter_pack_p;
1184 element->parameter_pack_p = false;
1187 declarator->parameter_pack_p = false;
1192 /* Determine whether the declarator we've seen so far can be a
1193 parameter pack, when followed by an ellipsis. */
1195 declarator_can_be_parameter_pack (cp_declarator *declarator)
1197 /* Search for a declarator name, or any other declarator that goes
1198 after the point where the ellipsis could appear in a parameter
1199 pack. If we find any of these, then this declarator can not be
1200 made into a parameter pack. */
1202 while (declarator && !found)
1204 switch ((int)declarator->kind)
1215 declarator = declarator->declarator;
1223 cp_parameter_declarator *no_parameters;
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226 DECLARATOR and DEFAULT_ARGUMENT. */
1228 cp_parameter_declarator *
1229 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1230 cp_declarator *declarator,
1231 tree default_argument)
1233 cp_parameter_declarator *parameter;
1235 parameter = ((cp_parameter_declarator *)
1236 alloc_declarator (sizeof (cp_parameter_declarator)));
1237 parameter->next = NULL;
1238 if (decl_specifiers)
1239 parameter->decl_specifiers = *decl_specifiers;
1241 clear_decl_specs (¶meter->decl_specifiers);
1242 parameter->declarator = declarator;
1243 parameter->default_argument = default_argument;
1244 parameter->ellipsis_p = false;
1249 /* Returns true iff DECLARATOR is a declaration for a function. */
1252 function_declarator_p (const cp_declarator *declarator)
1256 if (declarator->kind == cdk_function
1257 && declarator->declarator->kind == cdk_id)
1259 if (declarator->kind == cdk_id
1260 || declarator->kind == cdk_error)
1262 declarator = declarator->declarator;
1272 A cp_parser parses the token stream as specified by the C++
1273 grammar. Its job is purely parsing, not semantic analysis. For
1274 example, the parser breaks the token stream into declarators,
1275 expressions, statements, and other similar syntactic constructs.
1276 It does not check that the types of the expressions on either side
1277 of an assignment-statement are compatible, or that a function is
1278 not declared with a parameter of type `void'.
1280 The parser invokes routines elsewhere in the compiler to perform
1281 semantic analysis and to build up the abstract syntax tree for the
1284 The parser (and the template instantiation code, which is, in a
1285 way, a close relative of parsing) are the only parts of the
1286 compiler that should be calling push_scope and pop_scope, or
1287 related functions. The parser (and template instantiation code)
1288 keeps track of what scope is presently active; everything else
1289 should simply honor that. (The code that generates static
1290 initializers may also need to set the scope, in order to check
1291 access control correctly when emitting the initializers.)
1296 The parser is of the standard recursive-descent variety. Upcoming
1297 tokens in the token stream are examined in order to determine which
1298 production to use when parsing a non-terminal. Some C++ constructs
1299 require arbitrary look ahead to disambiguate. For example, it is
1300 impossible, in the general case, to tell whether a statement is an
1301 expression or declaration without scanning the entire statement.
1302 Therefore, the parser is capable of "parsing tentatively." When the
1303 parser is not sure what construct comes next, it enters this mode.
1304 Then, while we attempt to parse the construct, the parser queues up
1305 error messages, rather than issuing them immediately, and saves the
1306 tokens it consumes. If the construct is parsed successfully, the
1307 parser "commits", i.e., it issues any queued error messages and
1308 the tokens that were being preserved are permanently discarded.
1309 If, however, the construct is not parsed successfully, the parser
1310 rolls back its state completely so that it can resume parsing using
1311 a different alternative.
1316 The performance of the parser could probably be improved substantially.
1317 We could often eliminate the need to parse tentatively by looking ahead
1318 a little bit. In some places, this approach might not entirely eliminate
1319 the need to parse tentatively, but it might still speed up the average
1322 /* Flags that are passed to some parsing functions. These values can
1323 be bitwise-ored together. */
1328 CP_PARSER_FLAGS_NONE = 0x0,
1329 /* The construct is optional. If it is not present, then no error
1330 should be issued. */
1331 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1332 /* When parsing a type-specifier, treat user-defined type-names
1333 as non-type identifiers. */
1334 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1335 /* When parsing a type-specifier, do not try to parse a class-specifier
1336 or enum-specifier. */
1337 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1340 /* This type is used for parameters and variables which hold
1341 combinations of the above flags. */
1342 typedef int cp_parser_flags;
1344 /* The different kinds of declarators we want to parse. */
1346 typedef enum cp_parser_declarator_kind
1348 /* We want an abstract declarator. */
1349 CP_PARSER_DECLARATOR_ABSTRACT,
1350 /* We want a named declarator. */
1351 CP_PARSER_DECLARATOR_NAMED,
1352 /* We don't mind, but the name must be an unqualified-id. */
1353 CP_PARSER_DECLARATOR_EITHER
1354 } cp_parser_declarator_kind;
1356 /* The precedence values used to parse binary expressions. The minimum value
1357 of PREC must be 1, because zero is reserved to quickly discriminate
1358 binary operators from other tokens. */
1363 PREC_LOGICAL_OR_EXPRESSION,
1364 PREC_LOGICAL_AND_EXPRESSION,
1365 PREC_INCLUSIVE_OR_EXPRESSION,
1366 PREC_EXCLUSIVE_OR_EXPRESSION,
1367 PREC_AND_EXPRESSION,
1368 PREC_EQUALITY_EXPRESSION,
1369 PREC_RELATIONAL_EXPRESSION,
1370 PREC_SHIFT_EXPRESSION,
1371 PREC_ADDITIVE_EXPRESSION,
1372 PREC_MULTIPLICATIVE_EXPRESSION,
1374 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1377 /* A mapping from a token type to a corresponding tree node type, with a
1378 precedence value. */
1380 typedef struct cp_parser_binary_operations_map_node
1382 /* The token type. */
1383 enum cpp_ttype token_type;
1384 /* The corresponding tree code. */
1385 enum tree_code tree_type;
1386 /* The precedence of this operator. */
1387 enum cp_parser_prec prec;
1388 } cp_parser_binary_operations_map_node;
1390 /* The status of a tentative parse. */
1392 typedef enum cp_parser_status_kind
1394 /* No errors have occurred. */
1395 CP_PARSER_STATUS_KIND_NO_ERROR,
1396 /* An error has occurred. */
1397 CP_PARSER_STATUS_KIND_ERROR,
1398 /* We are committed to this tentative parse, whether or not an error
1400 CP_PARSER_STATUS_KIND_COMMITTED
1401 } cp_parser_status_kind;
1403 typedef struct cp_parser_expression_stack_entry
1405 /* Left hand side of the binary operation we are currently
1408 /* Original tree code for left hand side, if it was a binary
1409 expression itself (used for -Wparentheses). */
1410 enum tree_code lhs_type;
1411 /* Tree code for the binary operation we are parsing. */
1412 enum tree_code tree_type;
1413 /* Precedence of the binary operation we are parsing. */
1414 enum cp_parser_prec prec;
1415 } cp_parser_expression_stack_entry;
1417 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1418 entries because precedence levels on the stack are monotonically
1420 typedef struct cp_parser_expression_stack_entry
1421 cp_parser_expression_stack[NUM_PREC_VALUES];
1423 /* Context that is saved and restored when parsing tentatively. */
1424 typedef struct GTY (()) cp_parser_context {
1425 /* If this is a tentative parsing context, the status of the
1427 enum cp_parser_status_kind status;
1428 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1429 that are looked up in this context must be looked up both in the
1430 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1431 the context of the containing expression. */
1434 /* The next parsing context in the stack. */
1435 struct cp_parser_context *next;
1436 } cp_parser_context;
1440 /* Constructors and destructors. */
1442 static cp_parser_context *cp_parser_context_new
1443 (cp_parser_context *);
1445 /* Class variables. */
1447 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1449 /* The operator-precedence table used by cp_parser_binary_expression.
1450 Transformed into an associative array (binops_by_token) by
1453 static const cp_parser_binary_operations_map_node binops[] = {
1454 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1455 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1457 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1458 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1459 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1461 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1462 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1464 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1465 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1467 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1468 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1469 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1470 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1472 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1473 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1475 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1477 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1479 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1481 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1483 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1486 /* The same as binops, but initialized by cp_parser_new so that
1487 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1489 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1491 /* Constructors and destructors. */
1493 /* Construct a new context. The context below this one on the stack
1494 is given by NEXT. */
1496 static cp_parser_context *
1497 cp_parser_context_new (cp_parser_context* next)
1499 cp_parser_context *context;
1501 /* Allocate the storage. */
1502 if (cp_parser_context_free_list != NULL)
1504 /* Pull the first entry from the free list. */
1505 context = cp_parser_context_free_list;
1506 cp_parser_context_free_list = context->next;
1507 memset (context, 0, sizeof (*context));
1510 context = ggc_alloc_cleared_cp_parser_context ();
1512 /* No errors have occurred yet in this context. */
1513 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1514 /* If this is not the bottommost context, copy information that we
1515 need from the previous context. */
1518 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1519 expression, then we are parsing one in this context, too. */
1520 context->object_type = next->object_type;
1521 /* Thread the stack. */
1522 context->next = next;
1528 /* An entry in a queue of function arguments that require post-processing. */
1530 typedef struct GTY(()) cp_default_arg_entry_d {
1531 /* The current_class_type when we parsed this arg. */
1534 /* The function decl itself. */
1536 } cp_default_arg_entry;
1538 DEF_VEC_O(cp_default_arg_entry);
1539 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1541 /* An entry in a stack for member functions of local classes. */
1543 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1544 /* Functions with default arguments that require post-processing.
1545 Functions appear in this list in declaration order. */
1546 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1548 /* Functions with defintions that require post-processing. Functions
1549 appear in this list in declaration order. */
1550 VEC(tree,gc) *funs_with_definitions;
1551 } cp_unparsed_functions_entry;
1553 DEF_VEC_O(cp_unparsed_functions_entry);
1554 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1556 /* The cp_parser structure represents the C++ parser. */
1558 typedef struct GTY(()) cp_parser {
1559 /* The lexer from which we are obtaining tokens. */
1562 /* The scope in which names should be looked up. If NULL_TREE, then
1563 we look up names in the scope that is currently open in the
1564 source program. If non-NULL, this is either a TYPE or
1565 NAMESPACE_DECL for the scope in which we should look. It can
1566 also be ERROR_MARK, when we've parsed a bogus scope.
1568 This value is not cleared automatically after a name is looked
1569 up, so we must be careful to clear it before starting a new look
1570 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1571 will look up `Z' in the scope of `X', rather than the current
1572 scope.) Unfortunately, it is difficult to tell when name lookup
1573 is complete, because we sometimes peek at a token, look it up,
1574 and then decide not to consume it. */
1577 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1578 last lookup took place. OBJECT_SCOPE is used if an expression
1579 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1580 respectively. QUALIFYING_SCOPE is used for an expression of the
1581 form "X::Y"; it refers to X. */
1583 tree qualifying_scope;
1585 /* A stack of parsing contexts. All but the bottom entry on the
1586 stack will be tentative contexts.
1588 We parse tentatively in order to determine which construct is in
1589 use in some situations. For example, in order to determine
1590 whether a statement is an expression-statement or a
1591 declaration-statement we parse it tentatively as a
1592 declaration-statement. If that fails, we then reparse the same
1593 token stream as an expression-statement. */
1594 cp_parser_context *context;
1596 /* True if we are parsing GNU C++. If this flag is not set, then
1597 GNU extensions are not recognized. */
1598 bool allow_gnu_extensions_p;
1600 /* TRUE if the `>' token should be interpreted as the greater-than
1601 operator. FALSE if it is the end of a template-id or
1602 template-parameter-list. In C++0x mode, this flag also applies to
1603 `>>' tokens, which are viewed as two consecutive `>' tokens when
1604 this flag is FALSE. */
1605 bool greater_than_is_operator_p;
1607 /* TRUE if default arguments are allowed within a parameter list
1608 that starts at this point. FALSE if only a gnu extension makes
1609 them permissible. */
1610 bool default_arg_ok_p;
1612 /* TRUE if we are parsing an integral constant-expression. See
1613 [expr.const] for a precise definition. */
1614 bool integral_constant_expression_p;
1616 /* TRUE if we are parsing an integral constant-expression -- but a
1617 non-constant expression should be permitted as well. This flag
1618 is used when parsing an array bound so that GNU variable-length
1619 arrays are tolerated. */
1620 bool allow_non_integral_constant_expression_p;
1622 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1623 been seen that makes the expression non-constant. */
1624 bool non_integral_constant_expression_p;
1626 /* TRUE if local variable names and `this' are forbidden in the
1628 bool local_variables_forbidden_p;
1630 /* TRUE if the declaration we are parsing is part of a
1631 linkage-specification of the form `extern string-literal
1633 bool in_unbraced_linkage_specification_p;
1635 /* TRUE if we are presently parsing a declarator, after the
1636 direct-declarator. */
1637 bool in_declarator_p;
1639 /* TRUE if we are presently parsing a template-argument-list. */
1640 bool in_template_argument_list_p;
1642 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1643 to IN_OMP_BLOCK if parsing OpenMP structured block and
1644 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1645 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1646 iteration-statement, OpenMP block or loop within that switch. */
1647 #define IN_SWITCH_STMT 1
1648 #define IN_ITERATION_STMT 2
1649 #define IN_OMP_BLOCK 4
1650 #define IN_OMP_FOR 8
1651 #define IN_IF_STMT 16
1652 unsigned char in_statement;
1654 /* TRUE if we are presently parsing the body of a switch statement.
1655 Note that this doesn't quite overlap with in_statement above.
1656 The difference relates to giving the right sets of error messages:
1657 "case not in switch" vs "break statement used with OpenMP...". */
1658 bool in_switch_statement_p;
1660 /* TRUE if we are parsing a type-id in an expression context. In
1661 such a situation, both "type (expr)" and "type (type)" are valid
1663 bool in_type_id_in_expr_p;
1665 /* TRUE if we are currently in a header file where declarations are
1666 implicitly extern "C". */
1667 bool implicit_extern_c;
1669 /* TRUE if strings in expressions should be translated to the execution
1671 bool translate_strings_p;
1673 /* TRUE if we are presently parsing the body of a function, but not
1675 bool in_function_body;
1677 /* If non-NULL, then we are parsing a construct where new type
1678 definitions are not permitted. The string stored here will be
1679 issued as an error message if a type is defined. */
1680 const char *type_definition_forbidden_message;
1682 /* A stack used for member functions of local classes. The lists
1683 contained in an individual entry can only be processed once the
1684 outermost class being defined is complete. */
1685 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1687 /* The number of classes whose definitions are currently in
1689 unsigned num_classes_being_defined;
1691 /* The number of template parameter lists that apply directly to the
1692 current declaration. */
1693 unsigned num_template_parameter_lists;
1696 /* Managing the unparsed function queues. */
1698 #define unparsed_funs_with_default_args \
1699 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1700 #define unparsed_funs_with_definitions \
1701 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1704 push_unparsed_function_queues (cp_parser *parser)
1706 VEC_safe_push (cp_unparsed_functions_entry, gc,
1707 parser->unparsed_queues, NULL);
1708 unparsed_funs_with_default_args = NULL;
1709 unparsed_funs_with_definitions = make_tree_vector ();
1713 pop_unparsed_function_queues (cp_parser *parser)
1715 release_tree_vector (unparsed_funs_with_definitions);
1716 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1721 /* Constructors and destructors. */
1723 static cp_parser *cp_parser_new
1726 /* Routines to parse various constructs.
1728 Those that return `tree' will return the error_mark_node (rather
1729 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1730 Sometimes, they will return an ordinary node if error-recovery was
1731 attempted, even though a parse error occurred. So, to check
1732 whether or not a parse error occurred, you should always use
1733 cp_parser_error_occurred. If the construct is optional (indicated
1734 either by an `_opt' in the name of the function that does the
1735 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1736 the construct is not present. */
1738 /* Lexical conventions [gram.lex] */
1740 static tree cp_parser_identifier
1742 static tree cp_parser_string_literal
1743 (cp_parser *, bool, bool);
1745 /* Basic concepts [gram.basic] */
1747 static bool cp_parser_translation_unit
1750 /* Expressions [gram.expr] */
1752 static tree cp_parser_primary_expression
1753 (cp_parser *, bool, bool, bool, cp_id_kind *);
1754 static tree cp_parser_id_expression
1755 (cp_parser *, bool, bool, bool *, bool, bool);
1756 static tree cp_parser_unqualified_id
1757 (cp_parser *, bool, bool, bool, bool);
1758 static tree cp_parser_nested_name_specifier_opt
1759 (cp_parser *, bool, bool, bool, bool);
1760 static tree cp_parser_nested_name_specifier
1761 (cp_parser *, bool, bool, bool, bool);
1762 static tree cp_parser_qualifying_entity
1763 (cp_parser *, bool, bool, bool, bool, bool);
1764 static tree cp_parser_postfix_expression
1765 (cp_parser *, bool, bool, bool, cp_id_kind *);
1766 static tree cp_parser_postfix_open_square_expression
1767 (cp_parser *, tree, bool);
1768 static tree cp_parser_postfix_dot_deref_expression
1769 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1770 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1771 (cp_parser *, int, bool, bool, bool *);
1772 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1773 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1774 static void cp_parser_pseudo_destructor_name
1775 (cp_parser *, tree *, tree *);
1776 static tree cp_parser_unary_expression
1777 (cp_parser *, bool, bool, cp_id_kind *);
1778 static enum tree_code cp_parser_unary_operator
1780 static tree cp_parser_new_expression
1782 static VEC(tree,gc) *cp_parser_new_placement
1784 static tree cp_parser_new_type_id
1785 (cp_parser *, tree *);
1786 static cp_declarator *cp_parser_new_declarator_opt
1788 static cp_declarator *cp_parser_direct_new_declarator
1790 static VEC(tree,gc) *cp_parser_new_initializer
1792 static tree cp_parser_delete_expression
1794 static tree cp_parser_cast_expression
1795 (cp_parser *, bool, bool, cp_id_kind *);
1796 static tree cp_parser_binary_expression
1797 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1798 static tree cp_parser_question_colon_clause
1799 (cp_parser *, tree);
1800 static tree cp_parser_assignment_expression
1801 (cp_parser *, bool, cp_id_kind *);
1802 static enum tree_code cp_parser_assignment_operator_opt
1804 static tree cp_parser_expression
1805 (cp_parser *, bool, cp_id_kind *);
1806 static tree cp_parser_constant_expression
1807 (cp_parser *, bool, bool *);
1808 static tree cp_parser_builtin_offsetof
1810 static tree cp_parser_lambda_expression
1812 static void cp_parser_lambda_introducer
1813 (cp_parser *, tree);
1814 static void cp_parser_lambda_declarator_opt
1815 (cp_parser *, tree);
1816 static void cp_parser_lambda_body
1817 (cp_parser *, tree);
1819 /* Statements [gram.stmt.stmt] */
1821 static void cp_parser_statement
1822 (cp_parser *, tree, bool, bool *);
1823 static void cp_parser_label_for_labeled_statement
1825 static tree cp_parser_expression_statement
1826 (cp_parser *, tree);
1827 static tree cp_parser_compound_statement
1828 (cp_parser *, tree, bool);
1829 static void cp_parser_statement_seq_opt
1830 (cp_parser *, tree);
1831 static tree cp_parser_selection_statement
1832 (cp_parser *, bool *);
1833 static tree cp_parser_condition
1835 static tree cp_parser_iteration_statement
1837 static void cp_parser_for_init_statement
1839 static tree cp_parser_c_for
1841 static tree cp_parser_range_for
1843 static tree cp_parser_jump_statement
1845 static void cp_parser_declaration_statement
1848 static tree cp_parser_implicitly_scoped_statement
1849 (cp_parser *, bool *);
1850 static void cp_parser_already_scoped_statement
1853 /* Declarations [gram.dcl.dcl] */
1855 static void cp_parser_declaration_seq_opt
1857 static void cp_parser_declaration
1859 static void cp_parser_block_declaration
1860 (cp_parser *, bool);
1861 static void cp_parser_simple_declaration
1862 (cp_parser *, bool);
1863 static void cp_parser_decl_specifier_seq
1864 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1865 static tree cp_parser_storage_class_specifier_opt
1867 static tree cp_parser_function_specifier_opt
1868 (cp_parser *, cp_decl_specifier_seq *);
1869 static tree cp_parser_type_specifier
1870 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1872 static tree cp_parser_simple_type_specifier
1873 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1874 static tree cp_parser_type_name
1876 static tree cp_parser_nonclass_name
1877 (cp_parser* parser);
1878 static tree cp_parser_elaborated_type_specifier
1879 (cp_parser *, bool, bool);
1880 static tree cp_parser_enum_specifier
1882 static void cp_parser_enumerator_list
1883 (cp_parser *, tree);
1884 static void cp_parser_enumerator_definition
1885 (cp_parser *, tree);
1886 static tree cp_parser_namespace_name
1888 static void cp_parser_namespace_definition
1890 static void cp_parser_namespace_body
1892 static tree cp_parser_qualified_namespace_specifier
1894 static void cp_parser_namespace_alias_definition
1896 static bool cp_parser_using_declaration
1897 (cp_parser *, bool);
1898 static void cp_parser_using_directive
1900 static void cp_parser_asm_definition
1902 static void cp_parser_linkage_specification
1904 static void cp_parser_static_assert
1905 (cp_parser *, bool);
1906 static tree cp_parser_decltype
1909 /* Declarators [gram.dcl.decl] */
1911 static tree cp_parser_init_declarator
1912 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1913 static cp_declarator *cp_parser_declarator
1914 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1915 static cp_declarator *cp_parser_direct_declarator
1916 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1917 static enum tree_code cp_parser_ptr_operator
1918 (cp_parser *, tree *, cp_cv_quals *);
1919 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1921 static tree cp_parser_late_return_type_opt
1923 static tree cp_parser_declarator_id
1924 (cp_parser *, bool);
1925 static tree cp_parser_type_id
1927 static tree cp_parser_template_type_arg
1929 static tree cp_parser_trailing_type_id (cp_parser *);
1930 static tree cp_parser_type_id_1
1931 (cp_parser *, bool, bool);
1932 static void cp_parser_type_specifier_seq
1933 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1934 static tree cp_parser_parameter_declaration_clause
1936 static tree cp_parser_parameter_declaration_list
1937 (cp_parser *, bool *);
1938 static cp_parameter_declarator *cp_parser_parameter_declaration
1939 (cp_parser *, bool, bool *);
1940 static tree cp_parser_default_argument
1941 (cp_parser *, bool);
1942 static void cp_parser_function_body
1944 static tree cp_parser_initializer
1945 (cp_parser *, bool *, bool *);
1946 static tree cp_parser_initializer_clause
1947 (cp_parser *, bool *);
1948 static tree cp_parser_braced_list
1949 (cp_parser*, bool*);
1950 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1951 (cp_parser *, bool *);
1953 static bool cp_parser_ctor_initializer_opt_and_function_body
1956 /* Classes [gram.class] */
1958 static tree cp_parser_class_name
1959 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1960 static tree cp_parser_class_specifier
1962 static tree cp_parser_class_head
1963 (cp_parser *, bool *, tree *, tree *);
1964 static enum tag_types cp_parser_class_key
1966 static void cp_parser_member_specification_opt
1968 static void cp_parser_member_declaration
1970 static tree cp_parser_pure_specifier
1972 static tree cp_parser_constant_initializer
1975 /* Derived classes [gram.class.derived] */
1977 static tree cp_parser_base_clause
1979 static tree cp_parser_base_specifier
1982 /* Special member functions [gram.special] */
1984 static tree cp_parser_conversion_function_id
1986 static tree cp_parser_conversion_type_id
1988 static cp_declarator *cp_parser_conversion_declarator_opt
1990 static bool cp_parser_ctor_initializer_opt
1992 static void cp_parser_mem_initializer_list
1994 static tree cp_parser_mem_initializer
1996 static tree cp_parser_mem_initializer_id
1999 /* Overloading [gram.over] */
2001 static tree cp_parser_operator_function_id
2003 static tree cp_parser_operator
2006 /* Templates [gram.temp] */
2008 static void cp_parser_template_declaration
2009 (cp_parser *, bool);
2010 static tree cp_parser_template_parameter_list
2012 static tree cp_parser_template_parameter
2013 (cp_parser *, bool *, bool *);
2014 static tree cp_parser_type_parameter
2015 (cp_parser *, bool *);
2016 static tree cp_parser_template_id
2017 (cp_parser *, bool, bool, bool);
2018 static tree cp_parser_template_name
2019 (cp_parser *, bool, bool, bool, bool *);
2020 static tree cp_parser_template_argument_list
2022 static tree cp_parser_template_argument
2024 static void cp_parser_explicit_instantiation
2026 static void cp_parser_explicit_specialization
2029 /* Exception handling [gram.exception] */
2031 static tree cp_parser_try_block
2033 static bool cp_parser_function_try_block
2035 static void cp_parser_handler_seq
2037 static void cp_parser_handler
2039 static tree cp_parser_exception_declaration
2041 static tree cp_parser_throw_expression
2043 static tree cp_parser_exception_specification_opt
2045 static tree cp_parser_type_id_list
2048 /* GNU Extensions */
2050 static tree cp_parser_asm_specification_opt
2052 static tree cp_parser_asm_operand_list
2054 static tree cp_parser_asm_clobber_list
2056 static tree cp_parser_asm_label_list
2058 static tree cp_parser_attributes_opt
2060 static tree cp_parser_attribute_list
2062 static bool cp_parser_extension_opt
2063 (cp_parser *, int *);
2064 static void cp_parser_label_declaration
2067 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2068 static bool cp_parser_pragma
2069 (cp_parser *, enum pragma_context);
2071 /* Objective-C++ Productions */
2073 static tree cp_parser_objc_message_receiver
2075 static tree cp_parser_objc_message_args
2077 static tree cp_parser_objc_message_expression
2079 static tree cp_parser_objc_encode_expression
2081 static tree cp_parser_objc_defs_expression
2083 static tree cp_parser_objc_protocol_expression
2085 static tree cp_parser_objc_selector_expression
2087 static tree cp_parser_objc_expression
2089 static bool cp_parser_objc_selector_p
2091 static tree cp_parser_objc_selector
2093 static tree cp_parser_objc_protocol_refs_opt
2095 static void cp_parser_objc_declaration
2096 (cp_parser *, tree);
2097 static tree cp_parser_objc_statement
2099 static bool cp_parser_objc_valid_prefix_attributes
2100 (cp_parser* parser, tree *attrib);
2102 /* Utility Routines */
2104 static tree cp_parser_lookup_name
2105 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2106 static tree cp_parser_lookup_name_simple
2107 (cp_parser *, tree, location_t);
2108 static tree cp_parser_maybe_treat_template_as_class
2110 static bool cp_parser_check_declarator_template_parameters
2111 (cp_parser *, cp_declarator *, location_t);
2112 static bool cp_parser_check_template_parameters
2113 (cp_parser *, unsigned, location_t, cp_declarator *);
2114 static tree cp_parser_simple_cast_expression
2116 static tree cp_parser_global_scope_opt
2117 (cp_parser *, bool);
2118 static bool cp_parser_constructor_declarator_p
2119 (cp_parser *, bool);
2120 static tree cp_parser_function_definition_from_specifiers_and_declarator
2121 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2122 static tree cp_parser_function_definition_after_declarator
2123 (cp_parser *, bool);
2124 static void cp_parser_template_declaration_after_export
2125 (cp_parser *, bool);
2126 static void cp_parser_perform_template_parameter_access_checks
2127 (VEC (deferred_access_check,gc)*);
2128 static tree cp_parser_single_declaration
2129 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2130 static tree cp_parser_functional_cast
2131 (cp_parser *, tree);
2132 static tree cp_parser_save_member_function_body
2133 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2134 static tree cp_parser_enclosed_template_argument_list
2136 static void cp_parser_save_default_args
2137 (cp_parser *, tree);
2138 static void cp_parser_late_parsing_for_member
2139 (cp_parser *, tree);
2140 static void cp_parser_late_parsing_default_args
2141 (cp_parser *, tree);
2142 static tree cp_parser_sizeof_operand
2143 (cp_parser *, enum rid);
2144 static tree cp_parser_trait_expr
2145 (cp_parser *, enum rid);
2146 static bool cp_parser_declares_only_class_p
2148 static void cp_parser_set_storage_class
2149 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2150 static void cp_parser_set_decl_spec_type
2151 (cp_decl_specifier_seq *, tree, location_t, bool);
2152 static bool cp_parser_friend_p
2153 (const cp_decl_specifier_seq *);
2154 static void cp_parser_required_error
2155 (cp_parser *, required_token, bool);
2156 static cp_token *cp_parser_require
2157 (cp_parser *, enum cpp_ttype, required_token);
2158 static cp_token *cp_parser_require_keyword
2159 (cp_parser *, enum rid, required_token);
2160 static bool cp_parser_token_starts_function_definition_p
2162 static bool cp_parser_next_token_starts_class_definition_p
2164 static bool cp_parser_next_token_ends_template_argument_p
2166 static bool cp_parser_nth_token_starts_template_argument_list_p
2167 (cp_parser *, size_t);
2168 static enum tag_types cp_parser_token_is_class_key
2170 static void cp_parser_check_class_key
2171 (enum tag_types, tree type);
2172 static void cp_parser_check_access_in_redeclaration
2173 (tree type, location_t location);
2174 static bool cp_parser_optional_template_keyword
2176 static void cp_parser_pre_parsed_nested_name_specifier
2178 static bool cp_parser_cache_group
2179 (cp_parser *, enum cpp_ttype, unsigned);
2180 static void cp_parser_parse_tentatively
2182 static void cp_parser_commit_to_tentative_parse
2184 static void cp_parser_abort_tentative_parse
2186 static bool cp_parser_parse_definitely
2188 static inline bool cp_parser_parsing_tentatively
2190 static bool cp_parser_uncommitted_to_tentative_parse_p
2192 static void cp_parser_error
2193 (cp_parser *, const char *);
2194 static void cp_parser_name_lookup_error
2195 (cp_parser *, tree, tree, name_lookup_error, location_t);
2196 static bool cp_parser_simulate_error
2198 static bool cp_parser_check_type_definition
2200 static void cp_parser_check_for_definition_in_return_type
2201 (cp_declarator *, tree, location_t type_location);
2202 static void cp_parser_check_for_invalid_template_id
2203 (cp_parser *, tree, location_t location);
2204 static bool cp_parser_non_integral_constant_expression
2205 (cp_parser *, non_integral_constant);
2206 static void cp_parser_diagnose_invalid_type_name
2207 (cp_parser *, tree, tree, location_t);
2208 static bool cp_parser_parse_and_diagnose_invalid_type_name
2210 static int cp_parser_skip_to_closing_parenthesis
2211 (cp_parser *, bool, bool, bool);
2212 static void cp_parser_skip_to_end_of_statement
2214 static void cp_parser_consume_semicolon_at_end_of_statement
2216 static void cp_parser_skip_to_end_of_block_or_statement
2218 static bool cp_parser_skip_to_closing_brace
2220 static void cp_parser_skip_to_end_of_template_parameter_list
2222 static void cp_parser_skip_to_pragma_eol
2223 (cp_parser*, cp_token *);
2224 static bool cp_parser_error_occurred
2226 static bool cp_parser_allow_gnu_extensions_p
2228 static bool cp_parser_is_string_literal
2230 static bool cp_parser_is_keyword
2231 (cp_token *, enum rid);
2232 static tree cp_parser_make_typename_type
2233 (cp_parser *, tree, tree, location_t location);
2234 static cp_declarator * cp_parser_make_indirect_declarator
2235 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2237 /* Returns nonzero if we are parsing tentatively. */
2240 cp_parser_parsing_tentatively (cp_parser* parser)
2242 return parser->context->next != NULL;
2245 /* Returns nonzero if TOKEN is a string literal. */
2248 cp_parser_is_string_literal (cp_token* token)
2250 return (token->type == CPP_STRING ||
2251 token->type == CPP_STRING16 ||
2252 token->type == CPP_STRING32 ||
2253 token->type == CPP_WSTRING ||
2254 token->type == CPP_UTF8STRING);
2257 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2260 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2262 return token->keyword == keyword;
2265 /* If not parsing tentatively, issue a diagnostic of the form
2266 FILE:LINE: MESSAGE before TOKEN
2267 where TOKEN is the next token in the input stream. MESSAGE
2268 (specified by the caller) is usually of the form "expected
2272 cp_parser_error (cp_parser* parser, const char* gmsgid)
2274 if (!cp_parser_simulate_error (parser))
2276 cp_token *token = cp_lexer_peek_token (parser->lexer);
2277 /* This diagnostic makes more sense if it is tagged to the line
2278 of the token we just peeked at. */
2279 cp_lexer_set_source_position_from_token (token);
2281 if (token->type == CPP_PRAGMA)
2283 error_at (token->location,
2284 "%<#pragma%> is not allowed here");
2285 cp_parser_skip_to_pragma_eol (parser, token);
2289 c_parse_error (gmsgid,
2290 /* Because c_parser_error does not understand
2291 CPP_KEYWORD, keywords are treated like
2293 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2294 token->u.value, token->flags);
2298 /* Issue an error about name-lookup failing. NAME is the
2299 IDENTIFIER_NODE DECL is the result of
2300 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2301 the thing that we hoped to find. */
2304 cp_parser_name_lookup_error (cp_parser* parser,
2307 name_lookup_error desired,
2308 location_t location)
2310 /* If name lookup completely failed, tell the user that NAME was not
2312 if (decl == error_mark_node)
2314 if (parser->scope && parser->scope != global_namespace)
2315 error_at (location, "%<%E::%E%> has not been declared",
2316 parser->scope, name);
2317 else if (parser->scope == global_namespace)
2318 error_at (location, "%<::%E%> has not been declared", name);
2319 else if (parser->object_scope
2320 && !CLASS_TYPE_P (parser->object_scope))
2321 error_at (location, "request for member %qE in non-class type %qT",
2322 name, parser->object_scope);
2323 else if (parser->object_scope)
2324 error_at (location, "%<%T::%E%> has not been declared",
2325 parser->object_scope, name);
2327 error_at (location, "%qE has not been declared", name);
2329 else if (parser->scope && parser->scope != global_namespace)
2334 error_at (location, "%<%E::%E%> is not a type",
2335 parser->scope, name);
2338 error_at (location, "%<%E::%E%> is not a class or namespace",
2339 parser->scope, name);
2343 "%<%E::%E%> is not a class, namespace, or enumeration",
2344 parser->scope, name);
2351 else if (parser->scope == global_namespace)
2356 error_at (location, "%<::%E%> is not a type", name);
2359 error_at (location, "%<::%E%> is not a class or namespace", name);
2363 "%<::%E%> is not a class, namespace, or enumeration",
2375 error_at (location, "%qE is not a type", name);
2378 error_at (location, "%qE is not a class or namespace", name);
2382 "%qE is not a class, namespace, or enumeration", name);
2390 /* If we are parsing tentatively, remember that an error has occurred
2391 during this tentative parse. Returns true if the error was
2392 simulated; false if a message should be issued by the caller. */
2395 cp_parser_simulate_error (cp_parser* parser)
2397 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2399 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2405 /* Check for repeated decl-specifiers. */
2408 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2409 location_t location)
2413 for (ds = ds_first; ds != ds_last; ++ds)
2415 unsigned count = decl_specs->specs[ds];
2418 /* The "long" specifier is a special case because of "long long". */
2422 error_at (location, "%<long long long%> is too long for GCC");
2424 pedwarn_cxx98 (location, OPT_Wlong_long,
2425 "ISO C++ 1998 does not support %<long long%>");
2429 static const char *const decl_spec_names[] = {
2446 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2451 /* This function is called when a type is defined. If type
2452 definitions are forbidden at this point, an error message is
2456 cp_parser_check_type_definition (cp_parser* parser)
2458 /* If types are forbidden here, issue a message. */
2459 if (parser->type_definition_forbidden_message)
2461 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2462 in the message need to be interpreted. */
2463 error (parser->type_definition_forbidden_message);
2469 /* This function is called when the DECLARATOR is processed. The TYPE
2470 was a type defined in the decl-specifiers. If it is invalid to
2471 define a type in the decl-specifiers for DECLARATOR, an error is
2472 issued. TYPE_LOCATION is the location of TYPE and is used
2473 for error reporting. */
2476 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2477 tree type, location_t type_location)
2479 /* [dcl.fct] forbids type definitions in return types.
2480 Unfortunately, it's not easy to know whether or not we are
2481 processing a return type until after the fact. */
2483 && (declarator->kind == cdk_pointer
2484 || declarator->kind == cdk_reference
2485 || declarator->kind == cdk_ptrmem))
2486 declarator = declarator->declarator;
2488 && declarator->kind == cdk_function)
2490 error_at (type_location,
2491 "new types may not be defined in a return type");
2492 inform (type_location,
2493 "(perhaps a semicolon is missing after the definition of %qT)",
2498 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2499 "<" in any valid C++ program. If the next token is indeed "<",
2500 issue a message warning the user about what appears to be an
2501 invalid attempt to form a template-id. LOCATION is the location
2502 of the type-specifier (TYPE) */
2505 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2506 tree type, location_t location)
2508 cp_token_position start = 0;
2510 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2513 error_at (location, "%qT is not a template", type);
2514 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2515 error_at (location, "%qE is not a template", type);
2517 error_at (location, "invalid template-id");
2518 /* Remember the location of the invalid "<". */
2519 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2520 start = cp_lexer_token_position (parser->lexer, true);
2521 /* Consume the "<". */
2522 cp_lexer_consume_token (parser->lexer);
2523 /* Parse the template arguments. */
2524 cp_parser_enclosed_template_argument_list (parser);
2525 /* Permanently remove the invalid template arguments so that
2526 this error message is not issued again. */
2528 cp_lexer_purge_tokens_after (parser->lexer, start);
2532 /* If parsing an integral constant-expression, issue an error message
2533 about the fact that THING appeared and return true. Otherwise,
2534 return false. In either case, set
2535 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2538 cp_parser_non_integral_constant_expression (cp_parser *parser,
2539 non_integral_constant thing)
2541 parser->non_integral_constant_expression_p = true;
2542 if (parser->integral_constant_expression_p)
2544 if (!parser->allow_non_integral_constant_expression_p)
2546 const char *msg = NULL;
2550 error ("floating-point literal "
2551 "cannot appear in a constant-expression");
2554 error ("a cast to a type other than an integral or "
2555 "enumeration type cannot appear in a "
2556 "constant-expression");
2559 error ("%<typeid%> operator "
2560 "cannot appear in a constant-expression");
2563 error ("non-constant compound literals "
2564 "cannot appear in a constant-expression");
2567 error ("a function call "
2568 "cannot appear in a constant-expression");
2571 error ("an increment "
2572 "cannot appear in a constant-expression");
2575 error ("an decrement "
2576 "cannot appear in a constant-expression");
2579 error ("an array reference "
2580 "cannot appear in a constant-expression");
2582 case NIC_ADDR_LABEL:
2583 error ("the address of a label "
2584 "cannot appear in a constant-expression");
2586 case NIC_OVERLOADED:
2587 error ("calls to overloaded operators "
2588 "cannot appear in a constant-expression");
2590 case NIC_ASSIGNMENT:
2591 error ("an assignment cannot appear in a constant-expression");
2594 error ("a comma operator "
2595 "cannot appear in a constant-expression");
2597 case NIC_CONSTRUCTOR:
2598 error ("a call to a constructor "
2599 "cannot appear in a constant-expression");
2605 msg = "__FUNCTION__";
2607 case NIC_PRETTY_FUNC:
2608 msg = "__PRETTY_FUNCTION__";
2628 case NIC_PREINCREMENT:
2631 case NIC_PREDECREMENT:
2644 error ("%qs cannot appear in a constant-expression", msg);
2651 /* Emit a diagnostic for an invalid type name. SCOPE is the
2652 qualifying scope (or NULL, if none) for ID. This function commits
2653 to the current active tentative parse, if any. (Otherwise, the
2654 problematic construct might be encountered again later, resulting
2655 in duplicate error messages.) LOCATION is the location of ID. */
2658 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2659 tree scope, tree id,
2660 location_t location)
2662 tree decl, old_scope;
2663 /* Try to lookup the identifier. */
2664 old_scope = parser->scope;
2665 parser->scope = scope;
2666 decl = cp_parser_lookup_name_simple (parser, id, location);
2667 parser->scope = old_scope;
2668 /* If the lookup found a template-name, it means that the user forgot
2669 to specify an argument list. Emit a useful error message. */
2670 if (TREE_CODE (decl) == TEMPLATE_DECL)
2672 "invalid use of template-name %qE without an argument list",
2674 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2675 error_at (location, "invalid use of destructor %qD as a type", id);
2676 else if (TREE_CODE (decl) == TYPE_DECL)
2677 /* Something like 'unsigned A a;' */
2678 error_at (location, "invalid combination of multiple type-specifiers");
2679 else if (!parser->scope)
2681 /* Issue an error message. */
2682 error_at (location, "%qE does not name a type", id);
2683 /* If we're in a template class, it's possible that the user was
2684 referring to a type from a base class. For example:
2686 template <typename T> struct A { typedef T X; };
2687 template <typename T> struct B : public A<T> { X x; };
2689 The user should have said "typename A<T>::X". */
2690 if (processing_template_decl && current_class_type
2691 && TYPE_BINFO (current_class_type))
2695 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2699 tree base_type = BINFO_TYPE (b);
2700 if (CLASS_TYPE_P (base_type)
2701 && dependent_type_p (base_type))
2704 /* Go from a particular instantiation of the
2705 template (which will have an empty TYPE_FIELDs),
2706 to the main version. */
2707 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2708 for (field = TYPE_FIELDS (base_type);
2710 field = DECL_CHAIN (field))
2711 if (TREE_CODE (field) == TYPE_DECL
2712 && DECL_NAME (field) == id)
2715 "(perhaps %<typename %T::%E%> was intended)",
2716 BINFO_TYPE (b), id);
2725 /* Here we diagnose qualified-ids where the scope is actually correct,
2726 but the identifier does not resolve to a valid type name. */
2727 else if (parser->scope != error_mark_node)
2729 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2730 error_at (location, "%qE in namespace %qE does not name a type",
2732 else if (CLASS_TYPE_P (parser->scope)
2733 && constructor_name_p (id, parser->scope))
2736 error_at (location, "%<%T::%E%> names the constructor, not"
2737 " the type", parser->scope, id);
2738 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2739 error_at (location, "and %qT has no template constructors",
2742 else if (TYPE_P (parser->scope)
2743 && dependent_scope_p (parser->scope))
2744 error_at (location, "need %<typename%> before %<%T::%E%> because "
2745 "%qT is a dependent scope",
2746 parser->scope, id, parser->scope);
2747 else if (TYPE_P (parser->scope))
2748 error_at (location, "%qE in class %qT does not name a type",
2753 cp_parser_commit_to_tentative_parse (parser);
2756 /* Check for a common situation where a type-name should be present,
2757 but is not, and issue a sensible error message. Returns true if an
2758 invalid type-name was detected.
2760 The situation handled by this function are variable declarations of the
2761 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2762 Usually, `ID' should name a type, but if we got here it means that it
2763 does not. We try to emit the best possible error message depending on
2764 how exactly the id-expression looks like. */
2767 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2770 cp_token *token = cp_lexer_peek_token (parser->lexer);
2772 /* Avoid duplicate error about ambiguous lookup. */
2773 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2775 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2776 if (next->type == CPP_NAME && next->ambiguous_p)
2780 cp_parser_parse_tentatively (parser);
2781 id = cp_parser_id_expression (parser,
2782 /*template_keyword_p=*/false,
2783 /*check_dependency_p=*/true,
2784 /*template_p=*/NULL,
2785 /*declarator_p=*/true,
2786 /*optional_p=*/false);
2787 /* If the next token is a (, this is a function with no explicit return
2788 type, i.e. constructor, destructor or conversion op. */
2789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2790 || TREE_CODE (id) == TYPE_DECL)
2792 cp_parser_abort_tentative_parse (parser);
2795 if (!cp_parser_parse_definitely (parser))
2798 /* Emit a diagnostic for the invalid type. */
2799 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2800 id, token->location);
2802 /* If we aren't in the middle of a declarator (i.e. in a
2803 parameter-declaration-clause), skip to the end of the declaration;
2804 there's no point in trying to process it. */
2805 if (!parser->in_declarator_p)
2806 cp_parser_skip_to_end_of_block_or_statement (parser);
2810 /* Consume tokens up to, and including, the next non-nested closing `)'.
2811 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2812 are doing error recovery. Returns -1 if OR_COMMA is true and we
2813 found an unnested comma. */
2816 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2821 unsigned paren_depth = 0;
2822 unsigned brace_depth = 0;
2823 unsigned square_depth = 0;
2825 if (recovering && !or_comma
2826 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2831 cp_token * token = cp_lexer_peek_token (parser->lexer);
2833 switch (token->type)
2836 case CPP_PRAGMA_EOL:
2837 /* If we've run out of tokens, then there is no closing `)'. */
2840 /* This is good for lambda expression capture-lists. */
2841 case CPP_OPEN_SQUARE:
2844 case CPP_CLOSE_SQUARE:
2845 if (!square_depth--)
2850 /* This matches the processing in skip_to_end_of_statement. */
2855 case CPP_OPEN_BRACE:
2858 case CPP_CLOSE_BRACE:
2864 if (recovering && or_comma && !brace_depth && !paren_depth
2869 case CPP_OPEN_PAREN:
2874 case CPP_CLOSE_PAREN:
2875 if (!brace_depth && !paren_depth--)
2878 cp_lexer_consume_token (parser->lexer);
2887 /* Consume the token. */
2888 cp_lexer_consume_token (parser->lexer);
2892 /* Consume tokens until we reach the end of the current statement.
2893 Normally, that will be just before consuming a `;'. However, if a
2894 non-nested `}' comes first, then we stop before consuming that. */
2897 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2899 unsigned nesting_depth = 0;
2903 cp_token *token = cp_lexer_peek_token (parser->lexer);
2905 switch (token->type)
2908 case CPP_PRAGMA_EOL:
2909 /* If we've run out of tokens, stop. */
2913 /* If the next token is a `;', we have reached the end of the
2919 case CPP_CLOSE_BRACE:
2920 /* If this is a non-nested '}', stop before consuming it.
2921 That way, when confronted with something like:
2925 we stop before consuming the closing '}', even though we
2926 have not yet reached a `;'. */
2927 if (nesting_depth == 0)
2930 /* If it is the closing '}' for a block that we have
2931 scanned, stop -- but only after consuming the token.
2937 we will stop after the body of the erroneously declared
2938 function, but before consuming the following `typedef'
2940 if (--nesting_depth == 0)
2942 cp_lexer_consume_token (parser->lexer);
2946 case CPP_OPEN_BRACE:
2954 /* Consume the token. */
2955 cp_lexer_consume_token (parser->lexer);
2959 /* This function is called at the end of a statement or declaration.
2960 If the next token is a semicolon, it is consumed; otherwise, error
2961 recovery is attempted. */
2964 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2966 /* Look for the trailing `;'. */
2967 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2969 /* If there is additional (erroneous) input, skip to the end of
2971 cp_parser_skip_to_end_of_statement (parser);
2972 /* If the next token is now a `;', consume it. */
2973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2974 cp_lexer_consume_token (parser->lexer);
2978 /* Skip tokens until we have consumed an entire block, or until we
2979 have consumed a non-nested `;'. */
2982 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2984 int nesting_depth = 0;
2986 while (nesting_depth >= 0)
2988 cp_token *token = cp_lexer_peek_token (parser->lexer);
2990 switch (token->type)
2993 case CPP_PRAGMA_EOL:
2994 /* If we've run out of tokens, stop. */
2998 /* Stop if this is an unnested ';'. */
3003 case CPP_CLOSE_BRACE:
3004 /* Stop if this is an unnested '}', or closes the outermost
3007 if (nesting_depth < 0)
3013 case CPP_OPEN_BRACE:
3022 /* Consume the token. */
3023 cp_lexer_consume_token (parser->lexer);
3027 /* Skip tokens until a non-nested closing curly brace is the next
3028 token, or there are no more tokens. Return true in the first case,
3032 cp_parser_skip_to_closing_brace (cp_parser *parser)
3034 unsigned nesting_depth = 0;
3038 cp_token *token = cp_lexer_peek_token (parser->lexer);
3040 switch (token->type)
3043 case CPP_PRAGMA_EOL:
3044 /* If we've run out of tokens, stop. */
3047 case CPP_CLOSE_BRACE:
3048 /* If the next token is a non-nested `}', then we have reached
3049 the end of the current block. */
3050 if (nesting_depth-- == 0)
3054 case CPP_OPEN_BRACE:
3055 /* If it the next token is a `{', then we are entering a new
3056 block. Consume the entire block. */
3064 /* Consume the token. */
3065 cp_lexer_consume_token (parser->lexer);
3069 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3070 parameter is the PRAGMA token, allowing us to purge the entire pragma
3074 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3078 parser->lexer->in_pragma = false;
3081 token = cp_lexer_consume_token (parser->lexer);
3082 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3084 /* Ensure that the pragma is not parsed again. */
3085 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3088 /* Require pragma end of line, resyncing with it as necessary. The
3089 arguments are as for cp_parser_skip_to_pragma_eol. */
3092 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3094 parser->lexer->in_pragma = false;
3095 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3099 /* This is a simple wrapper around make_typename_type. When the id is
3100 an unresolved identifier node, we can provide a superior diagnostic
3101 using cp_parser_diagnose_invalid_type_name. */
3104 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3105 tree id, location_t id_location)
3108 if (TREE_CODE (id) == IDENTIFIER_NODE)
3110 result = make_typename_type (scope, id, typename_type,
3111 /*complain=*/tf_none);
3112 if (result == error_mark_node)
3113 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3116 return make_typename_type (scope, id, typename_type, tf_error);
3119 /* This is a wrapper around the
3120 make_{pointer,ptrmem,reference}_declarator functions that decides
3121 which one to call based on the CODE and CLASS_TYPE arguments. The
3122 CODE argument should be one of the values returned by
3123 cp_parser_ptr_operator. */
3124 static cp_declarator *
3125 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3126 cp_cv_quals cv_qualifiers,
3127 cp_declarator *target)
3129 if (code == ERROR_MARK)
3130 return cp_error_declarator;
3132 if (code == INDIRECT_REF)
3133 if (class_type == NULL_TREE)
3134 return make_pointer_declarator (cv_qualifiers, target);
3136 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3137 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3138 return make_reference_declarator (cv_qualifiers, target, false);
3139 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3140 return make_reference_declarator (cv_qualifiers, target, true);
3144 /* Create a new C++ parser. */
3147 cp_parser_new (void)
3153 /* cp_lexer_new_main is called before doing GC allocation because
3154 cp_lexer_new_main might load a PCH file. */
3155 lexer = cp_lexer_new_main ();
3157 /* Initialize the binops_by_token so that we can get the tree
3158 directly from the token. */
3159 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3160 binops_by_token[binops[i].token_type] = binops[i];
3162 parser = ggc_alloc_cleared_cp_parser ();
3163 parser->lexer = lexer;
3164 parser->context = cp_parser_context_new (NULL);
3166 /* For now, we always accept GNU extensions. */
3167 parser->allow_gnu_extensions_p = 1;
3169 /* The `>' token is a greater-than operator, not the end of a
3171 parser->greater_than_is_operator_p = true;
3173 parser->default_arg_ok_p = true;
3175 /* We are not parsing a constant-expression. */
3176 parser->integral_constant_expression_p = false;
3177 parser->allow_non_integral_constant_expression_p = false;
3178 parser->non_integral_constant_expression_p = false;
3180 /* Local variable names are not forbidden. */
3181 parser->local_variables_forbidden_p = false;
3183 /* We are not processing an `extern "C"' declaration. */
3184 parser->in_unbraced_linkage_specification_p = false;
3186 /* We are not processing a declarator. */
3187 parser->in_declarator_p = false;
3189 /* We are not processing a template-argument-list. */
3190 parser->in_template_argument_list_p = false;
3192 /* We are not in an iteration statement. */
3193 parser->in_statement = 0;
3195 /* We are not in a switch statement. */
3196 parser->in_switch_statement_p = false;
3198 /* We are not parsing a type-id inside an expression. */
3199 parser->in_type_id_in_expr_p = false;
3201 /* Declarations aren't implicitly extern "C". */
3202 parser->implicit_extern_c = false;
3204 /* String literals should be translated to the execution character set. */
3205 parser->translate_strings_p = true;
3207 /* We are not parsing a function body. */
3208 parser->in_function_body = false;
3210 /* The unparsed function queue is empty. */
3211 push_unparsed_function_queues (parser);
3213 /* There are no classes being defined. */
3214 parser->num_classes_being_defined = 0;
3216 /* No template parameters apply. */
3217 parser->num_template_parameter_lists = 0;
3222 /* Create a cp_lexer structure which will emit the tokens in CACHE
3223 and push it onto the parser's lexer stack. This is used for delayed
3224 parsing of in-class method bodies and default arguments, and should
3225 not be confused with tentative parsing. */
3227 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3229 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3230 lexer->next = parser->lexer;
3231 parser->lexer = lexer;
3233 /* Move the current source position to that of the first token in the
3235 cp_lexer_set_source_position_from_token (lexer->next_token);
3238 /* Pop the top lexer off the parser stack. This is never used for the
3239 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3241 cp_parser_pop_lexer (cp_parser *parser)
3243 cp_lexer *lexer = parser->lexer;
3244 parser->lexer = lexer->next;
3245 cp_lexer_destroy (lexer);
3247 /* Put the current source position back where it was before this
3248 lexer was pushed. */
3249 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3252 /* Lexical conventions [gram.lex] */
3254 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3258 cp_parser_identifier (cp_parser* parser)
3262 /* Look for the identifier. */
3263 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3264 /* Return the value. */
3265 return token ? token->u.value : error_mark_node;
3268 /* Parse a sequence of adjacent string constants. Returns a
3269 TREE_STRING representing the combined, nul-terminated string
3270 constant. If TRANSLATE is true, translate the string to the
3271 execution character set. If WIDE_OK is true, a wide string is
3274 C++98 [lex.string] says that if a narrow string literal token is
3275 adjacent to a wide string literal token, the behavior is undefined.
3276 However, C99 6.4.5p4 says that this results in a wide string literal.
3277 We follow C99 here, for consistency with the C front end.
3279 This code is largely lifted from lex_string() in c-lex.c.
3281 FUTURE: ObjC++ will need to handle @-strings here. */
3283 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3287 struct obstack str_ob;
3288 cpp_string str, istr, *strs;
3290 enum cpp_ttype type;
3292 tok = cp_lexer_peek_token (parser->lexer);
3293 if (!cp_parser_is_string_literal (tok))
3295 cp_parser_error (parser, "expected string-literal");
3296 return error_mark_node;
3301 /* Try to avoid the overhead of creating and destroying an obstack
3302 for the common case of just one string. */
3303 if (!cp_parser_is_string_literal
3304 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3306 cp_lexer_consume_token (parser->lexer);
3308 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3309 str.len = TREE_STRING_LENGTH (tok->u.value);
3316 gcc_obstack_init (&str_ob);
3321 cp_lexer_consume_token (parser->lexer);
3323 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3324 str.len = TREE_STRING_LENGTH (tok->u.value);
3326 if (type != tok->type)
3328 if (type == CPP_STRING)
3330 else if (tok->type != CPP_STRING)
3331 error_at (tok->location,
3332 "unsupported non-standard concatenation "
3333 "of string literals");
3336 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3338 tok = cp_lexer_peek_token (parser->lexer);
3340 while (cp_parser_is_string_literal (tok));
3342 strs = (cpp_string *) obstack_finish (&str_ob);
3345 if (type != CPP_STRING && !wide_ok)
3347 cp_parser_error (parser, "a wide string is invalid in this context");
3351 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3352 (parse_in, strs, count, &istr, type))
3354 value = build_string (istr.len, (const char *)istr.text);
3355 free (CONST_CAST (unsigned char *, istr.text));
3361 case CPP_UTF8STRING:
3362 TREE_TYPE (value) = char_array_type_node;
3365 TREE_TYPE (value) = char16_array_type_node;
3368 TREE_TYPE (value) = char32_array_type_node;
3371 TREE_TYPE (value) = wchar_array_type_node;
3375 value = fix_string_type (value);
3378 /* cpp_interpret_string has issued an error. */
3379 value = error_mark_node;
3382 obstack_free (&str_ob, 0);
3388 /* Basic concepts [gram.basic] */
3390 /* Parse a translation-unit.
3393 declaration-seq [opt]
3395 Returns TRUE if all went well. */
3398 cp_parser_translation_unit (cp_parser* parser)
3400 /* The address of the first non-permanent object on the declarator
3402 static void *declarator_obstack_base;
3406 /* Create the declarator obstack, if necessary. */
3407 if (!cp_error_declarator)
3409 gcc_obstack_init (&declarator_obstack);
3410 /* Create the error declarator. */
3411 cp_error_declarator = make_declarator (cdk_error);
3412 /* Create the empty parameter list. */
3413 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3414 /* Remember where the base of the declarator obstack lies. */
3415 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3418 cp_parser_declaration_seq_opt (parser);
3420 /* If there are no tokens left then all went well. */
3421 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3423 /* Get rid of the token array; we don't need it any more. */
3424 cp_lexer_destroy (parser->lexer);
3425 parser->lexer = NULL;
3427 /* This file might have been a context that's implicitly extern
3428 "C". If so, pop the lang context. (Only relevant for PCH.) */
3429 if (parser->implicit_extern_c)
3431 pop_lang_context ();
3432 parser->implicit_extern_c = false;
3436 finish_translation_unit ();
3442 cp_parser_error (parser, "expected declaration");
3446 /* Make sure the declarator obstack was fully cleaned up. */
3447 gcc_assert (obstack_next_free (&declarator_obstack)
3448 == declarator_obstack_base);
3450 /* All went well. */
3454 /* Expressions [gram.expr] */
3456 /* Parse a primary-expression.
3467 ( compound-statement )
3468 __builtin_va_arg ( assignment-expression , type-id )
3469 __builtin_offsetof ( type-id , offsetof-expression )
3472 __has_nothrow_assign ( type-id )
3473 __has_nothrow_constructor ( type-id )
3474 __has_nothrow_copy ( type-id )
3475 __has_trivial_assign ( type-id )
3476 __has_trivial_constructor ( type-id )
3477 __has_trivial_copy ( type-id )
3478 __has_trivial_destructor ( type-id )
3479 __has_virtual_destructor ( type-id )
3480 __is_abstract ( type-id )
3481 __is_base_of ( type-id , type-id )
3482 __is_class ( type-id )
3483 __is_convertible_to ( type-id , type-id )
3484 __is_empty ( type-id )
3485 __is_enum ( type-id )
3486 __is_pod ( type-id )
3487 __is_polymorphic ( type-id )
3488 __is_union ( type-id )
3490 Objective-C++ Extension:
3498 ADDRESS_P is true iff this expression was immediately preceded by
3499 "&" and therefore might denote a pointer-to-member. CAST_P is true
3500 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3501 true iff this expression is a template argument.
3503 Returns a representation of the expression. Upon return, *IDK
3504 indicates what kind of id-expression (if any) was present. */
3507 cp_parser_primary_expression (cp_parser *parser,
3510 bool template_arg_p,
3513 cp_token *token = NULL;
3515 /* Assume the primary expression is not an id-expression. */
3516 *idk = CP_ID_KIND_NONE;
3518 /* Peek at the next token. */
3519 token = cp_lexer_peek_token (parser->lexer);
3520 switch (token->type)
3533 token = cp_lexer_consume_token (parser->lexer);
3534 if (TREE_CODE (token->u.value) == FIXED_CST)
3536 error_at (token->location,
3537 "fixed-point types not supported in C++");
3538 return error_mark_node;
3540 /* Floating-point literals are only allowed in an integral
3541 constant expression if they are cast to an integral or
3542 enumeration type. */
3543 if (TREE_CODE (token->u.value) == REAL_CST
3544 && parser->integral_constant_expression_p
3547 /* CAST_P will be set even in invalid code like "int(2.7 +
3548 ...)". Therefore, we have to check that the next token
3549 is sure to end the cast. */
3552 cp_token *next_token;
3554 next_token = cp_lexer_peek_token (parser->lexer);
3555 if (/* The comma at the end of an
3556 enumerator-definition. */
3557 next_token->type != CPP_COMMA
3558 /* The curly brace at the end of an enum-specifier. */
3559 && next_token->type != CPP_CLOSE_BRACE
3560 /* The end of a statement. */
3561 && next_token->type != CPP_SEMICOLON
3562 /* The end of the cast-expression. */
3563 && next_token->type != CPP_CLOSE_PAREN
3564 /* The end of an array bound. */
3565 && next_token->type != CPP_CLOSE_SQUARE
3566 /* The closing ">" in a template-argument-list. */
3567 && (next_token->type != CPP_GREATER
3568 || parser->greater_than_is_operator_p)
3569 /* C++0x only: A ">>" treated like two ">" tokens,
3570 in a template-argument-list. */
3571 && (next_token->type != CPP_RSHIFT
3572 || (cxx_dialect == cxx98)
3573 || parser->greater_than_is_operator_p))
3577 /* If we are within a cast, then the constraint that the
3578 cast is to an integral or enumeration type will be
3579 checked at that point. If we are not within a cast, then
3580 this code is invalid. */
3582 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3584 return token->u.value;
3590 case CPP_UTF8STRING:
3591 /* ??? Should wide strings be allowed when parser->translate_strings_p
3592 is false (i.e. in attributes)? If not, we can kill the third
3593 argument to cp_parser_string_literal. */
3594 return cp_parser_string_literal (parser,
3595 parser->translate_strings_p,
3598 case CPP_OPEN_PAREN:
3601 bool saved_greater_than_is_operator_p;
3603 /* Consume the `('. */
3604 cp_lexer_consume_token (parser->lexer);
3605 /* Within a parenthesized expression, a `>' token is always
3606 the greater-than operator. */
3607 saved_greater_than_is_operator_p
3608 = parser->greater_than_is_operator_p;
3609 parser->greater_than_is_operator_p = true;
3610 /* If we see `( { ' then we are looking at the beginning of
3611 a GNU statement-expression. */
3612 if (cp_parser_allow_gnu_extensions_p (parser)
3613 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3615 /* Statement-expressions are not allowed by the standard. */
3616 pedwarn (token->location, OPT_pedantic,
3617 "ISO C++ forbids braced-groups within expressions");
3619 /* And they're not allowed outside of a function-body; you
3620 cannot, for example, write:
3622 int i = ({ int j = 3; j + 1; });
3624 at class or namespace scope. */
3625 if (!parser->in_function_body
3626 || parser->in_template_argument_list_p)
3628 error_at (token->location,
3629 "statement-expressions are not allowed outside "
3630 "functions nor in template-argument lists");
3631 cp_parser_skip_to_end_of_block_or_statement (parser);
3632 expr = error_mark_node;
3636 /* Start the statement-expression. */
3637 expr = begin_stmt_expr ();
3638 /* Parse the compound-statement. */
3639 cp_parser_compound_statement (parser, expr, false);
3641 expr = finish_stmt_expr (expr, false);
3646 /* Parse the parenthesized expression. */
3647 expr = cp_parser_expression (parser, cast_p, idk);
3648 /* Let the front end know that this expression was
3649 enclosed in parentheses. This matters in case, for
3650 example, the expression is of the form `A::B', since
3651 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3653 finish_parenthesized_expr (expr);
3655 /* The `>' token might be the end of a template-id or
3656 template-parameter-list now. */
3657 parser->greater_than_is_operator_p
3658 = saved_greater_than_is_operator_p;
3659 /* Consume the `)'. */
3660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3661 cp_parser_skip_to_end_of_statement (parser);
3666 case CPP_OPEN_SQUARE:
3667 if (c_dialect_objc ())
3668 /* We have an Objective-C++ message. */
3669 return cp_parser_objc_expression (parser);
3670 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3671 return cp_parser_lambda_expression (parser);
3673 case CPP_OBJC_STRING:
3674 if (c_dialect_objc ())
3675 /* We have an Objective-C++ string literal. */
3676 return cp_parser_objc_expression (parser);
3677 cp_parser_error (parser, "expected primary-expression");
3678 return error_mark_node;
3681 switch (token->keyword)
3683 /* These two are the boolean literals. */
3685 cp_lexer_consume_token (parser->lexer);
3686 return boolean_true_node;
3688 cp_lexer_consume_token (parser->lexer);
3689 return boolean_false_node;
3691 /* The `__null' literal. */
3693 cp_lexer_consume_token (parser->lexer);
3696 /* The `nullptr' literal. */
3698 cp_lexer_consume_token (parser->lexer);
3699 return nullptr_node;
3701 /* Recognize the `this' keyword. */
3703 cp_lexer_consume_token (parser->lexer);
3704 if (parser->local_variables_forbidden_p)
3706 error_at (token->location,
3707 "%<this%> may not be used in this context");
3708 return error_mark_node;
3710 /* Pointers cannot appear in constant-expressions. */
3711 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3712 return error_mark_node;
3713 return finish_this_expr ();
3715 /* The `operator' keyword can be the beginning of an
3720 case RID_FUNCTION_NAME:
3721 case RID_PRETTY_FUNCTION_NAME:
3722 case RID_C99_FUNCTION_NAME:
3724 non_integral_constant name;
3726 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3727 __func__ are the names of variables -- but they are
3728 treated specially. Therefore, they are handled here,
3729 rather than relying on the generic id-expression logic
3730 below. Grammatically, these names are id-expressions.
3732 Consume the token. */
3733 token = cp_lexer_consume_token (parser->lexer);
3735 switch (token->keyword)
3737 case RID_FUNCTION_NAME:
3738 name = NIC_FUNC_NAME;
3740 case RID_PRETTY_FUNCTION_NAME:
3741 name = NIC_PRETTY_FUNC;
3743 case RID_C99_FUNCTION_NAME:
3744 name = NIC_C99_FUNC;
3750 if (cp_parser_non_integral_constant_expression (parser, name))
3751 return error_mark_node;
3753 /* Look up the name. */
3754 return finish_fname (token->u.value);
3762 /* The `__builtin_va_arg' construct is used to handle
3763 `va_arg'. Consume the `__builtin_va_arg' token. */
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Look for the opening `('. */
3766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3767 /* Now, parse the assignment-expression. */
3768 expression = cp_parser_assignment_expression (parser,
3769 /*cast_p=*/false, NULL);
3770 /* Look for the `,'. */
3771 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3772 /* Parse the type-id. */
3773 type = cp_parser_type_id (parser);
3774 /* Look for the closing `)'. */
3775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3776 /* Using `va_arg' in a constant-expression is not
3778 if (cp_parser_non_integral_constant_expression (parser,
3780 return error_mark_node;
3781 return build_x_va_arg (expression, type);
3785 return cp_parser_builtin_offsetof (parser);
3787 case RID_HAS_NOTHROW_ASSIGN:
3788 case RID_HAS_NOTHROW_CONSTRUCTOR:
3789 case RID_HAS_NOTHROW_COPY:
3790 case RID_HAS_TRIVIAL_ASSIGN:
3791 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3792 case RID_HAS_TRIVIAL_COPY:
3793 case RID_HAS_TRIVIAL_DESTRUCTOR:
3794 case RID_HAS_VIRTUAL_DESTRUCTOR:
3795 case RID_IS_ABSTRACT:
3796 case RID_IS_BASE_OF:
3798 case RID_IS_CONVERTIBLE_TO:
3802 case RID_IS_POLYMORPHIC:
3803 case RID_IS_STD_LAYOUT:
3804 case RID_IS_TRIVIAL:
3806 return cp_parser_trait_expr (parser, token->keyword);
3808 /* Objective-C++ expressions. */
3810 case RID_AT_PROTOCOL:
3811 case RID_AT_SELECTOR:
3812 return cp_parser_objc_expression (parser);
3815 if (parser->in_function_body
3816 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3819 error_at (token->location,
3820 "a template declaration cannot appear at block scope");
3821 cp_parser_skip_to_end_of_block_or_statement (parser);
3822 return error_mark_node;
3825 cp_parser_error (parser, "expected primary-expression");
3826 return error_mark_node;
3829 /* An id-expression can start with either an identifier, a
3830 `::' as the beginning of a qualified-id, or the "operator"
3834 case CPP_TEMPLATE_ID:
3835 case CPP_NESTED_NAME_SPECIFIER:
3839 const char *error_msg;
3842 cp_token *id_expr_token;
3845 /* Parse the id-expression. */
3847 = cp_parser_id_expression (parser,
3848 /*template_keyword_p=*/false,
3849 /*check_dependency_p=*/true,
3851 /*declarator_p=*/false,
3852 /*optional_p=*/false);
3853 if (id_expression == error_mark_node)
3854 return error_mark_node;
3855 id_expr_token = token;
3856 token = cp_lexer_peek_token (parser->lexer);
3857 done = (token->type != CPP_OPEN_SQUARE
3858 && token->type != CPP_OPEN_PAREN
3859 && token->type != CPP_DOT
3860 && token->type != CPP_DEREF
3861 && token->type != CPP_PLUS_PLUS
3862 && token->type != CPP_MINUS_MINUS);
3863 /* If we have a template-id, then no further lookup is
3864 required. If the template-id was for a template-class, we
3865 will sometimes have a TYPE_DECL at this point. */
3866 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3867 || TREE_CODE (id_expression) == TYPE_DECL)
3868 decl = id_expression;
3869 /* Look up the name. */
3872 tree ambiguous_decls;
3874 /* If we already know that this lookup is ambiguous, then
3875 we've already issued an error message; there's no reason
3877 if (id_expr_token->type == CPP_NAME
3878 && id_expr_token->ambiguous_p)
3880 cp_parser_simulate_error (parser);
3881 return error_mark_node;
3884 decl = cp_parser_lookup_name (parser, id_expression,
3887 /*is_namespace=*/false,
3888 /*check_dependency=*/true,
3890 id_expr_token->location);
3891 /* If the lookup was ambiguous, an error will already have
3893 if (ambiguous_decls)
3894 return error_mark_node;
3896 /* In Objective-C++, an instance variable (ivar) may be preferred
3897 to whatever cp_parser_lookup_name() found. */
3898 decl = objc_lookup_ivar (decl, id_expression);
3900 /* If name lookup gives us a SCOPE_REF, then the
3901 qualifying scope was dependent. */
3902 if (TREE_CODE (decl) == SCOPE_REF)
3904 /* At this point, we do not know if DECL is a valid
3905 integral constant expression. We assume that it is
3906 in fact such an expression, so that code like:
3908 template <int N> struct A {
3912 is accepted. At template-instantiation time, we
3913 will check that B<N>::i is actually a constant. */
3916 /* Check to see if DECL is a local variable in a context
3917 where that is forbidden. */
3918 if (parser->local_variables_forbidden_p
3919 && local_variable_p (decl))
3921 /* It might be that we only found DECL because we are
3922 trying to be generous with pre-ISO scoping rules.
3923 For example, consider:
3927 for (int i = 0; i < 10; ++i) {}
3928 extern void f(int j = i);
3931 Here, name look up will originally find the out
3932 of scope `i'. We need to issue a warning message,
3933 but then use the global `i'. */
3934 decl = check_for_out_of_scope_variable (decl);
3935 if (local_variable_p (decl))
3937 error_at (id_expr_token->location,
3938 "local variable %qD may not appear in this context",
3940 return error_mark_node;
3945 decl = (finish_id_expression
3946 (id_expression, decl, parser->scope,
3948 parser->integral_constant_expression_p,
3949 parser->allow_non_integral_constant_expression_p,
3950 &parser->non_integral_constant_expression_p,
3951 template_p, done, address_p,
3954 id_expr_token->location));
3956 cp_parser_error (parser, error_msg);
3960 /* Anything else is an error. */
3962 cp_parser_error (parser, "expected primary-expression");
3963 return error_mark_node;
3967 /* Parse an id-expression.
3974 :: [opt] nested-name-specifier template [opt] unqualified-id
3976 :: operator-function-id
3979 Return a representation of the unqualified portion of the
3980 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3981 a `::' or nested-name-specifier.
3983 Often, if the id-expression was a qualified-id, the caller will
3984 want to make a SCOPE_REF to represent the qualified-id. This
3985 function does not do this in order to avoid wastefully creating
3986 SCOPE_REFs when they are not required.
3988 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3991 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3992 uninstantiated templates.
3994 If *TEMPLATE_P is non-NULL, it is set to true iff the
3995 `template' keyword is used to explicitly indicate that the entity
3996 named is a template.
3998 If DECLARATOR_P is true, the id-expression is appearing as part of
3999 a declarator, rather than as part of an expression. */
4002 cp_parser_id_expression (cp_parser *parser,
4003 bool template_keyword_p,
4004 bool check_dependency_p,
4009 bool global_scope_p;
4010 bool nested_name_specifier_p;
4012 /* Assume the `template' keyword was not used. */
4014 *template_p = template_keyword_p;
4016 /* Look for the optional `::' operator. */
4018 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4020 /* Look for the optional nested-name-specifier. */
4021 nested_name_specifier_p
4022 = (cp_parser_nested_name_specifier_opt (parser,
4023 /*typename_keyword_p=*/false,
4028 /* If there is a nested-name-specifier, then we are looking at
4029 the first qualified-id production. */
4030 if (nested_name_specifier_p)
4033 tree saved_object_scope;
4034 tree saved_qualifying_scope;
4035 tree unqualified_id;
4038 /* See if the next token is the `template' keyword. */
4040 template_p = &is_template;
4041 *template_p = cp_parser_optional_template_keyword (parser);
4042 /* Name lookup we do during the processing of the
4043 unqualified-id might obliterate SCOPE. */
4044 saved_scope = parser->scope;
4045 saved_object_scope = parser->object_scope;
4046 saved_qualifying_scope = parser->qualifying_scope;
4047 /* Process the final unqualified-id. */
4048 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4051 /*optional_p=*/false);
4052 /* Restore the SAVED_SCOPE for our caller. */
4053 parser->scope = saved_scope;
4054 parser->object_scope = saved_object_scope;
4055 parser->qualifying_scope = saved_qualifying_scope;
4057 return unqualified_id;
4059 /* Otherwise, if we are in global scope, then we are looking at one
4060 of the other qualified-id productions. */
4061 else if (global_scope_p)
4066 /* Peek at the next token. */
4067 token = cp_lexer_peek_token (parser->lexer);
4069 /* If it's an identifier, and the next token is not a "<", then
4070 we can avoid the template-id case. This is an optimization
4071 for this common case. */
4072 if (token->type == CPP_NAME
4073 && !cp_parser_nth_token_starts_template_argument_list_p
4075 return cp_parser_identifier (parser);
4077 cp_parser_parse_tentatively (parser);
4078 /* Try a template-id. */
4079 id = cp_parser_template_id (parser,
4080 /*template_keyword_p=*/false,
4081 /*check_dependency_p=*/true,
4083 /* If that worked, we're done. */
4084 if (cp_parser_parse_definitely (parser))
4087 /* Peek at the next token. (Changes in the token buffer may
4088 have invalidated the pointer obtained above.) */
4089 token = cp_lexer_peek_token (parser->lexer);
4091 switch (token->type)
4094 return cp_parser_identifier (parser);
4097 if (token->keyword == RID_OPERATOR)
4098 return cp_parser_operator_function_id (parser);
4102 cp_parser_error (parser, "expected id-expression");
4103 return error_mark_node;
4107 return cp_parser_unqualified_id (parser, template_keyword_p,
4108 /*check_dependency_p=*/true,
4113 /* Parse an unqualified-id.
4117 operator-function-id
4118 conversion-function-id
4122 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4123 keyword, in a construct like `A::template ...'.
4125 Returns a representation of unqualified-id. For the `identifier'
4126 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4127 production a BIT_NOT_EXPR is returned; the operand of the
4128 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4129 other productions, see the documentation accompanying the
4130 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4131 names are looked up in uninstantiated templates. If DECLARATOR_P
4132 is true, the unqualified-id is appearing as part of a declarator,
4133 rather than as part of an expression. */
4136 cp_parser_unqualified_id (cp_parser* parser,
4137 bool template_keyword_p,
4138 bool check_dependency_p,
4144 /* Peek at the next token. */
4145 token = cp_lexer_peek_token (parser->lexer);
4147 switch (token->type)
4153 /* We don't know yet whether or not this will be a
4155 cp_parser_parse_tentatively (parser);
4156 /* Try a template-id. */
4157 id = cp_parser_template_id (parser, template_keyword_p,
4160 /* If it worked, we're done. */
4161 if (cp_parser_parse_definitely (parser))
4163 /* Otherwise, it's an ordinary identifier. */
4164 return cp_parser_identifier (parser);
4167 case CPP_TEMPLATE_ID:
4168 return cp_parser_template_id (parser, template_keyword_p,
4175 tree qualifying_scope;
4180 /* Consume the `~' token. */
4181 cp_lexer_consume_token (parser->lexer);
4182 /* Parse the class-name. The standard, as written, seems to
4185 template <typename T> struct S { ~S (); };
4186 template <typename T> S<T>::~S() {}
4188 is invalid, since `~' must be followed by a class-name, but
4189 `S<T>' is dependent, and so not known to be a class.
4190 That's not right; we need to look in uninstantiated
4191 templates. A further complication arises from:
4193 template <typename T> void f(T t) {
4197 Here, it is not possible to look up `T' in the scope of `T'
4198 itself. We must look in both the current scope, and the
4199 scope of the containing complete expression.
4201 Yet another issue is:
4210 The standard does not seem to say that the `S' in `~S'
4211 should refer to the type `S' and not the data member
4214 /* DR 244 says that we look up the name after the "~" in the
4215 same scope as we looked up the qualifying name. That idea
4216 isn't fully worked out; it's more complicated than that. */
4217 scope = parser->scope;
4218 object_scope = parser->object_scope;
4219 qualifying_scope = parser->qualifying_scope;
4221 /* Check for invalid scopes. */
4222 if (scope == error_mark_node)
4224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4225 cp_lexer_consume_token (parser->lexer);
4226 return error_mark_node;
4228 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4230 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4231 error_at (token->location,
4232 "scope %qT before %<~%> is not a class-name",
4234 cp_parser_simulate_error (parser);
4235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4236 cp_lexer_consume_token (parser->lexer);
4237 return error_mark_node;
4239 gcc_assert (!scope || TYPE_P (scope));
4241 /* If the name is of the form "X::~X" it's OK even if X is a
4243 token = cp_lexer_peek_token (parser->lexer);
4245 && token->type == CPP_NAME
4246 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4248 && (token->u.value == TYPE_IDENTIFIER (scope)
4249 || constructor_name_p (token->u.value, scope)))
4251 cp_lexer_consume_token (parser->lexer);
4252 return build_nt (BIT_NOT_EXPR, scope);
4255 /* If there was an explicit qualification (S::~T), first look
4256 in the scope given by the qualification (i.e., S).
4258 Note: in the calls to cp_parser_class_name below we pass
4259 typename_type so that lookup finds the injected-class-name
4260 rather than the constructor. */
4262 type_decl = NULL_TREE;
4265 cp_parser_parse_tentatively (parser);
4266 type_decl = cp_parser_class_name (parser,
4267 /*typename_keyword_p=*/false,
4268 /*template_keyword_p=*/false,
4270 /*check_dependency=*/false,
4271 /*class_head_p=*/false,
4273 if (cp_parser_parse_definitely (parser))
4276 /* In "N::S::~S", look in "N" as well. */
4277 if (!done && scope && qualifying_scope)
4279 cp_parser_parse_tentatively (parser);
4280 parser->scope = qualifying_scope;
4281 parser->object_scope = NULL_TREE;
4282 parser->qualifying_scope = NULL_TREE;
4284 = cp_parser_class_name (parser,
4285 /*typename_keyword_p=*/false,
4286 /*template_keyword_p=*/false,
4288 /*check_dependency=*/false,
4289 /*class_head_p=*/false,
4291 if (cp_parser_parse_definitely (parser))
4294 /* In "p->S::~T", look in the scope given by "*p" as well. */
4295 else if (!done && object_scope)
4297 cp_parser_parse_tentatively (parser);
4298 parser->scope = object_scope;
4299 parser->object_scope = NULL_TREE;
4300 parser->qualifying_scope = NULL_TREE;
4302 = cp_parser_class_name (parser,
4303 /*typename_keyword_p=*/false,
4304 /*template_keyword_p=*/false,
4306 /*check_dependency=*/false,
4307 /*class_head_p=*/false,
4309 if (cp_parser_parse_definitely (parser))
4312 /* Look in the surrounding context. */
4315 parser->scope = NULL_TREE;
4316 parser->object_scope = NULL_TREE;
4317 parser->qualifying_scope = NULL_TREE;
4318 if (processing_template_decl)
4319 cp_parser_parse_tentatively (parser);
4321 = cp_parser_class_name (parser,
4322 /*typename_keyword_p=*/false,
4323 /*template_keyword_p=*/false,
4325 /*check_dependency=*/false,
4326 /*class_head_p=*/false,
4328 if (processing_template_decl
4329 && ! cp_parser_parse_definitely (parser))
4331 /* We couldn't find a type with this name, so just accept
4332 it and check for a match at instantiation time. */
4333 type_decl = cp_parser_identifier (parser);
4334 if (type_decl != error_mark_node)
4335 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4339 /* If an error occurred, assume that the name of the
4340 destructor is the same as the name of the qualifying
4341 class. That allows us to keep parsing after running
4342 into ill-formed destructor names. */
4343 if (type_decl == error_mark_node && scope)
4344 return build_nt (BIT_NOT_EXPR, scope);
4345 else if (type_decl == error_mark_node)
4346 return error_mark_node;
4348 /* Check that destructor name and scope match. */
4349 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4351 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4352 error_at (token->location,
4353 "declaration of %<~%T%> as member of %qT",
4355 cp_parser_simulate_error (parser);
4356 return error_mark_node;
4361 A typedef-name that names a class shall not be used as the
4362 identifier in the declarator for a destructor declaration. */
4364 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4365 && !DECL_SELF_REFERENCE_P (type_decl)
4366 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4367 error_at (token->location,
4368 "typedef-name %qD used as destructor declarator",
4371 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4375 if (token->keyword == RID_OPERATOR)
4379 /* This could be a template-id, so we try that first. */
4380 cp_parser_parse_tentatively (parser);
4381 /* Try a template-id. */
4382 id = cp_parser_template_id (parser, template_keyword_p,
4383 /*check_dependency_p=*/true,
4385 /* If that worked, we're done. */
4386 if (cp_parser_parse_definitely (parser))
4388 /* We still don't know whether we're looking at an
4389 operator-function-id or a conversion-function-id. */
4390 cp_parser_parse_tentatively (parser);
4391 /* Try an operator-function-id. */
4392 id = cp_parser_operator_function_id (parser);
4393 /* If that didn't work, try a conversion-function-id. */
4394 if (!cp_parser_parse_definitely (parser))
4395 id = cp_parser_conversion_function_id (parser);
4404 cp_parser_error (parser, "expected unqualified-id");
4405 return error_mark_node;
4409 /* Parse an (optional) nested-name-specifier.
4411 nested-name-specifier: [C++98]
4412 class-or-namespace-name :: nested-name-specifier [opt]
4413 class-or-namespace-name :: template nested-name-specifier [opt]
4415 nested-name-specifier: [C++0x]
4418 nested-name-specifier identifier ::
4419 nested-name-specifier template [opt] simple-template-id ::
4421 PARSER->SCOPE should be set appropriately before this function is
4422 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4423 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4426 Sets PARSER->SCOPE to the class (TYPE) or namespace
4427 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4428 it unchanged if there is no nested-name-specifier. Returns the new
4429 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4431 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4432 part of a declaration and/or decl-specifier. */
4435 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4436 bool typename_keyword_p,
4437 bool check_dependency_p,
4439 bool is_declaration)
4441 bool success = false;
4442 cp_token_position start = 0;
4445 /* Remember where the nested-name-specifier starts. */
4446 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4448 start = cp_lexer_token_position (parser->lexer, false);
4449 push_deferring_access_checks (dk_deferred);
4456 tree saved_qualifying_scope;
4457 bool template_keyword_p;
4459 /* Spot cases that cannot be the beginning of a
4460 nested-name-specifier. */
4461 token = cp_lexer_peek_token (parser->lexer);
4463 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4464 the already parsed nested-name-specifier. */
4465 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4467 /* Grab the nested-name-specifier and continue the loop. */
4468 cp_parser_pre_parsed_nested_name_specifier (parser);
4469 /* If we originally encountered this nested-name-specifier
4470 with IS_DECLARATION set to false, we will not have
4471 resolved TYPENAME_TYPEs, so we must do so here. */
4473 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4475 new_scope = resolve_typename_type (parser->scope,
4476 /*only_current_p=*/false);
4477 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4478 parser->scope = new_scope;
4484 /* Spot cases that cannot be the beginning of a
4485 nested-name-specifier. On the second and subsequent times
4486 through the loop, we look for the `template' keyword. */
4487 if (success && token->keyword == RID_TEMPLATE)
4489 /* A template-id can start a nested-name-specifier. */
4490 else if (token->type == CPP_TEMPLATE_ID)
4494 /* If the next token is not an identifier, then it is
4495 definitely not a type-name or namespace-name. */
4496 if (token->type != CPP_NAME)
4498 /* If the following token is neither a `<' (to begin a
4499 template-id), nor a `::', then we are not looking at a
4500 nested-name-specifier. */
4501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4502 if (token->type != CPP_SCOPE
4503 && !cp_parser_nth_token_starts_template_argument_list_p
4508 /* The nested-name-specifier is optional, so we parse
4510 cp_parser_parse_tentatively (parser);
4512 /* Look for the optional `template' keyword, if this isn't the
4513 first time through the loop. */
4515 template_keyword_p = cp_parser_optional_template_keyword (parser);
4517 template_keyword_p = false;
4519 /* Save the old scope since the name lookup we are about to do
4520 might destroy it. */
4521 old_scope = parser->scope;
4522 saved_qualifying_scope = parser->qualifying_scope;
4523 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4524 look up names in "X<T>::I" in order to determine that "Y" is
4525 a template. So, if we have a typename at this point, we make
4526 an effort to look through it. */
4528 && !typename_keyword_p
4530 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4531 parser->scope = resolve_typename_type (parser->scope,
4532 /*only_current_p=*/false);
4533 /* Parse the qualifying entity. */
4535 = cp_parser_qualifying_entity (parser,
4541 /* Look for the `::' token. */
4542 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4544 /* If we found what we wanted, we keep going; otherwise, we're
4546 if (!cp_parser_parse_definitely (parser))
4548 bool error_p = false;
4550 /* Restore the OLD_SCOPE since it was valid before the
4551 failed attempt at finding the last
4552 class-or-namespace-name. */
4553 parser->scope = old_scope;
4554 parser->qualifying_scope = saved_qualifying_scope;
4555 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4557 /* If the next token is an identifier, and the one after
4558 that is a `::', then any valid interpretation would have
4559 found a class-or-namespace-name. */
4560 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4561 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4563 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4566 token = cp_lexer_consume_token (parser->lexer);
4569 if (!token->ambiguous_p)
4572 tree ambiguous_decls;
4574 decl = cp_parser_lookup_name (parser, token->u.value,
4576 /*is_template=*/false,
4577 /*is_namespace=*/false,
4578 /*check_dependency=*/true,
4581 if (TREE_CODE (decl) == TEMPLATE_DECL)
4582 error_at (token->location,
4583 "%qD used without template parameters",
4585 else if (ambiguous_decls)
4587 error_at (token->location,
4588 "reference to %qD is ambiguous",
4590 print_candidates (ambiguous_decls);
4591 decl = error_mark_node;
4595 if (cxx_dialect != cxx98)
4596 cp_parser_name_lookup_error
4597 (parser, token->u.value, decl, NLE_NOT_CXX98,
4600 cp_parser_name_lookup_error
4601 (parser, token->u.value, decl, NLE_CXX98,
4605 parser->scope = error_mark_node;
4607 /* Treat this as a successful nested-name-specifier
4612 If the name found is not a class-name (clause
4613 _class_) or namespace-name (_namespace.def_), the
4614 program is ill-formed. */
4617 cp_lexer_consume_token (parser->lexer);
4621 /* We've found one valid nested-name-specifier. */
4623 /* Name lookup always gives us a DECL. */
4624 if (TREE_CODE (new_scope) == TYPE_DECL)
4625 new_scope = TREE_TYPE (new_scope);
4626 /* Uses of "template" must be followed by actual templates. */
4627 if (template_keyword_p
4628 && !(CLASS_TYPE_P (new_scope)
4629 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4630 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4631 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4632 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4633 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4634 == TEMPLATE_ID_EXPR)))
4635 permerror (input_location, TYPE_P (new_scope)
4636 ? "%qT is not a template"
4637 : "%qD is not a template",
4639 /* If it is a class scope, try to complete it; we are about to
4640 be looking up names inside the class. */
4641 if (TYPE_P (new_scope)
4642 /* Since checking types for dependency can be expensive,
4643 avoid doing it if the type is already complete. */
4644 && !COMPLETE_TYPE_P (new_scope)
4645 /* Do not try to complete dependent types. */
4646 && !dependent_type_p (new_scope))
4648 new_scope = complete_type (new_scope);
4649 /* If it is a typedef to current class, use the current
4650 class instead, as the typedef won't have any names inside
4652 if (!COMPLETE_TYPE_P (new_scope)
4653 && currently_open_class (new_scope))
4654 new_scope = TYPE_MAIN_VARIANT (new_scope);
4656 /* Make sure we look in the right scope the next time through
4658 parser->scope = new_scope;
4661 /* If parsing tentatively, replace the sequence of tokens that makes
4662 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4663 token. That way, should we re-parse the token stream, we will
4664 not have to repeat the effort required to do the parse, nor will
4665 we issue duplicate error messages. */
4666 if (success && start)
4670 token = cp_lexer_token_at (parser->lexer, start);
4671 /* Reset the contents of the START token. */
4672 token->type = CPP_NESTED_NAME_SPECIFIER;
4673 /* Retrieve any deferred checks. Do not pop this access checks yet
4674 so the memory will not be reclaimed during token replacing below. */
4675 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4676 token->u.tree_check_value->value = parser->scope;
4677 token->u.tree_check_value->checks = get_deferred_access_checks ();
4678 token->u.tree_check_value->qualifying_scope =
4679 parser->qualifying_scope;
4680 token->keyword = RID_MAX;
4682 /* Purge all subsequent tokens. */
4683 cp_lexer_purge_tokens_after (parser->lexer, start);
4687 pop_to_parent_deferring_access_checks ();
4689 return success ? parser->scope : NULL_TREE;
4692 /* Parse a nested-name-specifier. See
4693 cp_parser_nested_name_specifier_opt for details. This function
4694 behaves identically, except that it will an issue an error if no
4695 nested-name-specifier is present. */
4698 cp_parser_nested_name_specifier (cp_parser *parser,
4699 bool typename_keyword_p,
4700 bool check_dependency_p,
4702 bool is_declaration)
4706 /* Look for the nested-name-specifier. */
4707 scope = cp_parser_nested_name_specifier_opt (parser,
4712 /* If it was not present, issue an error message. */
4715 cp_parser_error (parser, "expected nested-name-specifier");
4716 parser->scope = NULL_TREE;
4722 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4723 this is either a class-name or a namespace-name (which corresponds
4724 to the class-or-namespace-name production in the grammar). For
4725 C++0x, it can also be a type-name that refers to an enumeration
4728 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4729 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4730 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4731 TYPE_P is TRUE iff the next name should be taken as a class-name,
4732 even the same name is declared to be another entity in the same
4735 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4736 specified by the class-or-namespace-name. If neither is found the
4737 ERROR_MARK_NODE is returned. */
4740 cp_parser_qualifying_entity (cp_parser *parser,
4741 bool typename_keyword_p,
4742 bool template_keyword_p,
4743 bool check_dependency_p,
4745 bool is_declaration)
4748 tree saved_qualifying_scope;
4749 tree saved_object_scope;
4752 bool successful_parse_p;
4754 /* Before we try to parse the class-name, we must save away the
4755 current PARSER->SCOPE since cp_parser_class_name will destroy
4757 saved_scope = parser->scope;
4758 saved_qualifying_scope = parser->qualifying_scope;
4759 saved_object_scope = parser->object_scope;
4760 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4761 there is no need to look for a namespace-name. */
4762 only_class_p = template_keyword_p
4763 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4765 cp_parser_parse_tentatively (parser);
4766 scope = cp_parser_class_name (parser,
4769 type_p ? class_type : none_type,
4771 /*class_head_p=*/false,
4773 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4774 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4776 && cxx_dialect != cxx98
4777 && !successful_parse_p)
4779 /* Restore the saved scope. */
4780 parser->scope = saved_scope;
4781 parser->qualifying_scope = saved_qualifying_scope;
4782 parser->object_scope = saved_object_scope;
4784 /* Parse tentatively. */
4785 cp_parser_parse_tentatively (parser);
4787 /* Parse a typedef-name or enum-name. */
4788 scope = cp_parser_nonclass_name (parser);
4790 /* "If the name found does not designate a namespace or a class,
4791 enumeration, or dependent type, the program is ill-formed."
4793 We cover classes and dependent types above and namespaces below,
4794 so this code is only looking for enums. */
4795 if (!scope || TREE_CODE (scope) != TYPE_DECL
4796 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4797 cp_parser_simulate_error (parser);
4799 successful_parse_p = cp_parser_parse_definitely (parser);
4801 /* If that didn't work, try for a namespace-name. */
4802 if (!only_class_p && !successful_parse_p)
4804 /* Restore the saved scope. */
4805 parser->scope = saved_scope;
4806 parser->qualifying_scope = saved_qualifying_scope;
4807 parser->object_scope = saved_object_scope;
4808 /* If we are not looking at an identifier followed by the scope
4809 resolution operator, then this is not part of a
4810 nested-name-specifier. (Note that this function is only used
4811 to parse the components of a nested-name-specifier.) */
4812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4813 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4814 return error_mark_node;
4815 scope = cp_parser_namespace_name (parser);
4821 /* Parse a postfix-expression.
4825 postfix-expression [ expression ]
4826 postfix-expression ( expression-list [opt] )
4827 simple-type-specifier ( expression-list [opt] )
4828 typename :: [opt] nested-name-specifier identifier
4829 ( expression-list [opt] )
4830 typename :: [opt] nested-name-specifier template [opt] template-id
4831 ( expression-list [opt] )
4832 postfix-expression . template [opt] id-expression
4833 postfix-expression -> template [opt] id-expression
4834 postfix-expression . pseudo-destructor-name
4835 postfix-expression -> pseudo-destructor-name
4836 postfix-expression ++
4837 postfix-expression --
4838 dynamic_cast < type-id > ( expression )
4839 static_cast < type-id > ( expression )
4840 reinterpret_cast < type-id > ( expression )
4841 const_cast < type-id > ( expression )
4842 typeid ( expression )
4848 ( type-id ) { initializer-list , [opt] }
4850 This extension is a GNU version of the C99 compound-literal
4851 construct. (The C99 grammar uses `type-name' instead of `type-id',
4852 but they are essentially the same concept.)
4854 If ADDRESS_P is true, the postfix expression is the operand of the
4855 `&' operator. CAST_P is true if this expression is the target of a
4858 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4859 class member access expressions [expr.ref].
4861 Returns a representation of the expression. */
4864 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4865 bool member_access_only_p,
4866 cp_id_kind * pidk_return)
4870 cp_id_kind idk = CP_ID_KIND_NONE;
4871 tree postfix_expression = NULL_TREE;
4872 bool is_member_access = false;
4874 /* Peek at the next token. */
4875 token = cp_lexer_peek_token (parser->lexer);
4876 /* Some of the productions are determined by keywords. */
4877 keyword = token->keyword;
4887 const char *saved_message;
4889 /* All of these can be handled in the same way from the point
4890 of view of parsing. Begin by consuming the token
4891 identifying the cast. */
4892 cp_lexer_consume_token (parser->lexer);
4894 /* New types cannot be defined in the cast. */
4895 saved_message = parser->type_definition_forbidden_message;
4896 parser->type_definition_forbidden_message
4897 = G_("types may not be defined in casts");
4899 /* Look for the opening `<'. */
4900 cp_parser_require (parser, CPP_LESS, RT_LESS);
4901 /* Parse the type to which we are casting. */
4902 type = cp_parser_type_id (parser);
4903 /* Look for the closing `>'. */
4904 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4905 /* Restore the old message. */
4906 parser->type_definition_forbidden_message = saved_message;
4908 /* And the expression which is being cast. */
4909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4910 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4913 /* Only type conversions to integral or enumeration types
4914 can be used in constant-expressions. */
4915 if (!cast_valid_in_integral_constant_expression_p (type)
4916 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4917 return error_mark_node;
4923 = build_dynamic_cast (type, expression, tf_warning_or_error);
4927 = build_static_cast (type, expression, tf_warning_or_error);
4931 = build_reinterpret_cast (type, expression,
4932 tf_warning_or_error);
4936 = build_const_cast (type, expression, tf_warning_or_error);
4947 const char *saved_message;
4948 bool saved_in_type_id_in_expr_p;
4950 /* Consume the `typeid' token. */
4951 cp_lexer_consume_token (parser->lexer);
4952 /* Look for the `(' token. */
4953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4954 /* Types cannot be defined in a `typeid' expression. */
4955 saved_message = parser->type_definition_forbidden_message;
4956 parser->type_definition_forbidden_message
4957 = G_("types may not be defined in a %<typeid%> expression");
4958 /* We can't be sure yet whether we're looking at a type-id or an
4960 cp_parser_parse_tentatively (parser);
4961 /* Try a type-id first. */
4962 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4963 parser->in_type_id_in_expr_p = true;
4964 type = cp_parser_type_id (parser);
4965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4966 /* Look for the `)' token. Otherwise, we can't be sure that
4967 we're not looking at an expression: consider `typeid (int
4968 (3))', for example. */
4969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4970 /* If all went well, simply lookup the type-id. */
4971 if (cp_parser_parse_definitely (parser))
4972 postfix_expression = get_typeid (type);
4973 /* Otherwise, fall back to the expression variant. */
4978 /* Look for an expression. */
4979 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4980 /* Compute its typeid. */
4981 postfix_expression = build_typeid (expression);
4982 /* Look for the `)' token. */
4983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4985 /* Restore the saved message. */
4986 parser->type_definition_forbidden_message = saved_message;
4987 /* `typeid' may not appear in an integral constant expression. */
4988 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4989 return error_mark_node;
4996 /* The syntax permitted here is the same permitted for an
4997 elaborated-type-specifier. */
4998 type = cp_parser_elaborated_type_specifier (parser,
4999 /*is_friend=*/false,
5000 /*is_declaration=*/false);
5001 postfix_expression = cp_parser_functional_cast (parser, type);
5009 /* If the next thing is a simple-type-specifier, we may be
5010 looking at a functional cast. We could also be looking at
5011 an id-expression. So, we try the functional cast, and if
5012 that doesn't work we fall back to the primary-expression. */
5013 cp_parser_parse_tentatively (parser);
5014 /* Look for the simple-type-specifier. */
5015 type = cp_parser_simple_type_specifier (parser,
5016 /*decl_specs=*/NULL,
5017 CP_PARSER_FLAGS_NONE);
5018 /* Parse the cast itself. */
5019 if (!cp_parser_error_occurred (parser))
5021 = cp_parser_functional_cast (parser, type);
5022 /* If that worked, we're done. */
5023 if (cp_parser_parse_definitely (parser))
5026 /* If the functional-cast didn't work out, try a
5027 compound-literal. */
5028 if (cp_parser_allow_gnu_extensions_p (parser)
5029 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5031 VEC(constructor_elt,gc) *initializer_list = NULL;
5032 bool saved_in_type_id_in_expr_p;
5034 cp_parser_parse_tentatively (parser);
5035 /* Consume the `('. */
5036 cp_lexer_consume_token (parser->lexer);
5037 /* Parse the type. */
5038 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5039 parser->in_type_id_in_expr_p = true;
5040 type = cp_parser_type_id (parser);
5041 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5042 /* Look for the `)'. */
5043 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5044 /* Look for the `{'. */
5045 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5046 /* If things aren't going well, there's no need to
5048 if (!cp_parser_error_occurred (parser))
5050 bool non_constant_p;
5051 /* Parse the initializer-list. */
5053 = cp_parser_initializer_list (parser, &non_constant_p);
5054 /* Allow a trailing `,'. */
5055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5056 cp_lexer_consume_token (parser->lexer);
5057 /* Look for the final `}'. */
5058 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5060 /* If that worked, we're definitely looking at a
5061 compound-literal expression. */
5062 if (cp_parser_parse_definitely (parser))
5064 /* Warn the user that a compound literal is not
5065 allowed in standard C++. */
5066 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5067 /* For simplicity, we disallow compound literals in
5068 constant-expressions. We could
5069 allow compound literals of integer type, whose
5070 initializer was a constant, in constant
5071 expressions. Permitting that usage, as a further
5072 extension, would not change the meaning of any
5073 currently accepted programs. (Of course, as
5074 compound literals are not part of ISO C++, the
5075 standard has nothing to say.) */
5076 if (cp_parser_non_integral_constant_expression (parser,
5079 postfix_expression = error_mark_node;
5082 /* Form the representation of the compound-literal. */
5084 = (finish_compound_literal
5085 (type, build_constructor (init_list_type_node,
5086 initializer_list)));
5091 /* It must be a primary-expression. */
5093 = cp_parser_primary_expression (parser, address_p, cast_p,
5094 /*template_arg_p=*/false,
5100 /* Keep looping until the postfix-expression is complete. */
5103 if (idk == CP_ID_KIND_UNQUALIFIED
5104 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5105 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5106 /* It is not a Koenig lookup function call. */
5108 = unqualified_name_lookup_error (postfix_expression);
5110 /* Peek at the next token. */
5111 token = cp_lexer_peek_token (parser->lexer);
5113 switch (token->type)
5115 case CPP_OPEN_SQUARE:
5117 = cp_parser_postfix_open_square_expression (parser,
5120 idk = CP_ID_KIND_NONE;
5121 is_member_access = false;
5124 case CPP_OPEN_PAREN:
5125 /* postfix-expression ( expression-list [opt] ) */
5128 bool is_builtin_constant_p;
5129 bool saved_integral_constant_expression_p = false;
5130 bool saved_non_integral_constant_expression_p = false;
5133 is_member_access = false;
5135 is_builtin_constant_p
5136 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5137 if (is_builtin_constant_p)
5139 /* The whole point of __builtin_constant_p is to allow
5140 non-constant expressions to appear as arguments. */
5141 saved_integral_constant_expression_p
5142 = parser->integral_constant_expression_p;
5143 saved_non_integral_constant_expression_p
5144 = parser->non_integral_constant_expression_p;
5145 parser->integral_constant_expression_p = false;
5147 args = (cp_parser_parenthesized_expression_list
5149 /*cast_p=*/false, /*allow_expansion_p=*/true,
5150 /*non_constant_p=*/NULL));
5151 if (is_builtin_constant_p)
5153 parser->integral_constant_expression_p
5154 = saved_integral_constant_expression_p;
5155 parser->non_integral_constant_expression_p
5156 = saved_non_integral_constant_expression_p;
5161 postfix_expression = error_mark_node;
5165 /* Function calls are not permitted in
5166 constant-expressions. */
5167 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5168 && cp_parser_non_integral_constant_expression (parser,
5171 postfix_expression = error_mark_node;
5172 release_tree_vector (args);
5177 if (idk == CP_ID_KIND_UNQUALIFIED
5178 || idk == CP_ID_KIND_TEMPLATE_ID)
5180 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5182 if (!VEC_empty (tree, args))
5185 if (!any_type_dependent_arguments_p (args))
5187 = perform_koenig_lookup (postfix_expression, args,
5188 /*include_std=*/false);
5192 = unqualified_fn_lookup_error (postfix_expression);
5194 /* We do not perform argument-dependent lookup if
5195 normal lookup finds a non-function, in accordance
5196 with the expected resolution of DR 218. */
5197 else if (!VEC_empty (tree, args)
5198 && is_overloaded_fn (postfix_expression))
5200 tree fn = get_first_fn (postfix_expression);
5201 fn = STRIP_TEMPLATE (fn);
5203 /* Do not do argument dependent lookup if regular
5204 lookup finds a member function or a block-scope
5205 function declaration. [basic.lookup.argdep]/3 */
5206 if (!DECL_FUNCTION_MEMBER_P (fn)
5207 && !DECL_LOCAL_FUNCTION_P (fn))
5210 if (!any_type_dependent_arguments_p (args))
5212 = perform_koenig_lookup (postfix_expression, args,
5213 /*include_std=*/false);
5218 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5220 tree instance = TREE_OPERAND (postfix_expression, 0);
5221 tree fn = TREE_OPERAND (postfix_expression, 1);
5223 if (processing_template_decl
5224 && (type_dependent_expression_p (instance)
5225 || (!BASELINK_P (fn)
5226 && TREE_CODE (fn) != FIELD_DECL)
5227 || type_dependent_expression_p (fn)
5228 || any_type_dependent_arguments_p (args)))
5231 = build_nt_call_vec (postfix_expression, args);
5232 release_tree_vector (args);
5236 if (BASELINK_P (fn))
5239 = (build_new_method_call
5240 (instance, fn, &args, NULL_TREE,
5241 (idk == CP_ID_KIND_QUALIFIED
5242 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5244 tf_warning_or_error));
5248 = finish_call_expr (postfix_expression, &args,
5249 /*disallow_virtual=*/false,
5251 tf_warning_or_error);
5253 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5254 || TREE_CODE (postfix_expression) == MEMBER_REF
5255 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5256 postfix_expression = (build_offset_ref_call_from_tree
5257 (postfix_expression, &args));
5258 else if (idk == CP_ID_KIND_QUALIFIED)
5259 /* A call to a static class member, or a namespace-scope
5262 = finish_call_expr (postfix_expression, &args,
5263 /*disallow_virtual=*/true,
5265 tf_warning_or_error);
5267 /* All other function calls. */
5269 = finish_call_expr (postfix_expression, &args,
5270 /*disallow_virtual=*/false,
5272 tf_warning_or_error);
5274 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5275 idk = CP_ID_KIND_NONE;
5277 release_tree_vector (args);
5283 /* postfix-expression . template [opt] id-expression
5284 postfix-expression . pseudo-destructor-name
5285 postfix-expression -> template [opt] id-expression
5286 postfix-expression -> pseudo-destructor-name */
5288 /* Consume the `.' or `->' operator. */
5289 cp_lexer_consume_token (parser->lexer);
5292 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5297 is_member_access = true;
5301 /* postfix-expression ++ */
5302 /* Consume the `++' token. */
5303 cp_lexer_consume_token (parser->lexer);
5304 /* Generate a representation for the complete expression. */
5306 = finish_increment_expr (postfix_expression,
5307 POSTINCREMENT_EXPR);
5308 /* Increments may not appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5310 postfix_expression = error_mark_node;
5311 idk = CP_ID_KIND_NONE;
5312 is_member_access = false;
5315 case CPP_MINUS_MINUS:
5316 /* postfix-expression -- */
5317 /* Consume the `--' token. */
5318 cp_lexer_consume_token (parser->lexer);
5319 /* Generate a representation for the complete expression. */
5321 = finish_increment_expr (postfix_expression,
5322 POSTDECREMENT_EXPR);
5323 /* Decrements may not appear in constant-expressions. */
5324 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5325 postfix_expression = error_mark_node;
5326 idk = CP_ID_KIND_NONE;
5327 is_member_access = false;
5331 if (pidk_return != NULL)
5332 * pidk_return = idk;
5333 if (member_access_only_p)
5334 return is_member_access? postfix_expression : error_mark_node;
5336 return postfix_expression;
5340 /* We should never get here. */
5342 return error_mark_node;
5345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5346 by cp_parser_builtin_offsetof. We're looking for
5348 postfix-expression [ expression ]
5350 FOR_OFFSETOF is set if we're being called in that context, which
5351 changes how we deal with integer constant expressions. */
5354 cp_parser_postfix_open_square_expression (cp_parser *parser,
5355 tree postfix_expression,
5360 /* Consume the `[' token. */
5361 cp_lexer_consume_token (parser->lexer);
5363 /* Parse the index expression. */
5364 /* ??? For offsetof, there is a question of what to allow here. If
5365 offsetof is not being used in an integral constant expression context,
5366 then we *could* get the right answer by computing the value at runtime.
5367 If we are in an integral constant expression context, then we might
5368 could accept any constant expression; hard to say without analysis.
5369 Rather than open the barn door too wide right away, allow only integer
5370 constant expressions here. */
5372 index = cp_parser_constant_expression (parser, false, NULL);
5374 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5376 /* Look for the closing `]'. */
5377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5379 /* Build the ARRAY_REF. */
5380 postfix_expression = grok_array_decl (postfix_expression, index);
5382 /* When not doing offsetof, array references are not permitted in
5383 constant-expressions. */
5385 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5386 postfix_expression = error_mark_node;
5388 return postfix_expression;
5391 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5392 by cp_parser_builtin_offsetof. We're looking for
5394 postfix-expression . template [opt] id-expression
5395 postfix-expression . pseudo-destructor-name
5396 postfix-expression -> template [opt] id-expression
5397 postfix-expression -> pseudo-destructor-name
5399 FOR_OFFSETOF is set if we're being called in that context. That sorta
5400 limits what of the above we'll actually accept, but nevermind.
5401 TOKEN_TYPE is the "." or "->" token, which will already have been
5402 removed from the stream. */
5405 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5406 enum cpp_ttype token_type,
5407 tree postfix_expression,
5408 bool for_offsetof, cp_id_kind *idk,
5409 location_t location)
5413 bool pseudo_destructor_p;
5414 tree scope = NULL_TREE;
5416 /* If this is a `->' operator, dereference the pointer. */
5417 if (token_type == CPP_DEREF)
5418 postfix_expression = build_x_arrow (postfix_expression);
5419 /* Check to see whether or not the expression is type-dependent. */
5420 dependent_p = type_dependent_expression_p (postfix_expression);
5421 /* The identifier following the `->' or `.' is not qualified. */
5422 parser->scope = NULL_TREE;
5423 parser->qualifying_scope = NULL_TREE;
5424 parser->object_scope = NULL_TREE;
5425 *idk = CP_ID_KIND_NONE;
5427 /* Enter the scope corresponding to the type of the object
5428 given by the POSTFIX_EXPRESSION. */
5429 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5431 scope = TREE_TYPE (postfix_expression);
5432 /* According to the standard, no expression should ever have
5433 reference type. Unfortunately, we do not currently match
5434 the standard in this respect in that our internal representation
5435 of an expression may have reference type even when the standard
5436 says it does not. Therefore, we have to manually obtain the
5437 underlying type here. */
5438 scope = non_reference (scope);
5439 /* The type of the POSTFIX_EXPRESSION must be complete. */
5440 if (scope == unknown_type_node)
5442 error_at (location, "%qE does not have class type",
5443 postfix_expression);
5447 scope = complete_type_or_else (scope, NULL_TREE);
5448 /* Let the name lookup machinery know that we are processing a
5449 class member access expression. */
5450 parser->context->object_type = scope;
5451 /* If something went wrong, we want to be able to discern that case,
5452 as opposed to the case where there was no SCOPE due to the type
5453 of expression being dependent. */
5455 scope = error_mark_node;
5456 /* If the SCOPE was erroneous, make the various semantic analysis
5457 functions exit quickly -- and without issuing additional error
5459 if (scope == error_mark_node)
5460 postfix_expression = error_mark_node;
5463 /* Assume this expression is not a pseudo-destructor access. */
5464 pseudo_destructor_p = false;
5466 /* If the SCOPE is a scalar type, then, if this is a valid program,
5467 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5468 is type dependent, it can be pseudo-destructor-name or something else.
5469 Try to parse it as pseudo-destructor-name first. */
5470 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5475 cp_parser_parse_tentatively (parser);
5476 /* Parse the pseudo-destructor-name. */
5478 cp_parser_pseudo_destructor_name (parser, &s, &type);
5480 && (cp_parser_error_occurred (parser)
5481 || TREE_CODE (type) != TYPE_DECL
5482 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5483 cp_parser_abort_tentative_parse (parser);
5484 else if (cp_parser_parse_definitely (parser))
5486 pseudo_destructor_p = true;
5488 = finish_pseudo_destructor_expr (postfix_expression,
5489 s, TREE_TYPE (type));
5493 if (!pseudo_destructor_p)
5495 /* If the SCOPE is not a scalar type, we are looking at an
5496 ordinary class member access expression, rather than a
5497 pseudo-destructor-name. */
5499 cp_token *token = cp_lexer_peek_token (parser->lexer);
5500 /* Parse the id-expression. */
5501 name = (cp_parser_id_expression
5503 cp_parser_optional_template_keyword (parser),
5504 /*check_dependency_p=*/true,
5506 /*declarator_p=*/false,
5507 /*optional_p=*/false));
5508 /* In general, build a SCOPE_REF if the member name is qualified.
5509 However, if the name was not dependent and has already been
5510 resolved; there is no need to build the SCOPE_REF. For example;
5512 struct X { void f(); };
5513 template <typename T> void f(T* t) { t->X::f(); }
5515 Even though "t" is dependent, "X::f" is not and has been resolved
5516 to a BASELINK; there is no need to include scope information. */
5518 /* But we do need to remember that there was an explicit scope for
5519 virtual function calls. */
5521 *idk = CP_ID_KIND_QUALIFIED;
5523 /* If the name is a template-id that names a type, we will get a
5524 TYPE_DECL here. That is invalid code. */
5525 if (TREE_CODE (name) == TYPE_DECL)
5527 error_at (token->location, "invalid use of %qD", name);
5528 postfix_expression = error_mark_node;
5532 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5534 name = build_qualified_name (/*type=*/NULL_TREE,
5538 parser->scope = NULL_TREE;
5539 parser->qualifying_scope = NULL_TREE;
5540 parser->object_scope = NULL_TREE;
5542 if (scope && name && BASELINK_P (name))
5543 adjust_result_of_qualified_name_lookup
5544 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5546 = finish_class_member_access_expr (postfix_expression, name,
5548 tf_warning_or_error);
5552 /* We no longer need to look up names in the scope of the object on
5553 the left-hand side of the `.' or `->' operator. */
5554 parser->context->object_type = NULL_TREE;
5556 /* Outside of offsetof, these operators may not appear in
5557 constant-expressions. */
5559 && (cp_parser_non_integral_constant_expression
5560 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5561 postfix_expression = error_mark_node;
5563 return postfix_expression;
5566 /* Parse a parenthesized expression-list.
5569 assignment-expression
5570 expression-list, assignment-expression
5575 identifier, expression-list
5577 CAST_P is true if this expression is the target of a cast.
5579 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5582 Returns a vector of trees. Each element is a representation of an
5583 assignment-expression. NULL is returned if the ( and or ) are
5584 missing. An empty, but allocated, vector is returned on no
5585 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5586 if we are parsing an attribute list for an attribute that wants a
5587 plain identifier argument, normal_attr for an attribute that wants
5588 an expression, or non_attr if we aren't parsing an attribute list. If
5589 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5590 not all of the expressions in the list were constant. */
5592 static VEC(tree,gc) *
5593 cp_parser_parenthesized_expression_list (cp_parser* parser,
5594 int is_attribute_list,
5596 bool allow_expansion_p,
5597 bool *non_constant_p)
5599 VEC(tree,gc) *expression_list;
5600 bool fold_expr_p = is_attribute_list != non_attr;
5601 tree identifier = NULL_TREE;
5602 bool saved_greater_than_is_operator_p;
5604 /* Assume all the expressions will be constant. */
5606 *non_constant_p = false;
5608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5611 expression_list = make_tree_vector ();
5613 /* Within a parenthesized expression, a `>' token is always
5614 the greater-than operator. */
5615 saved_greater_than_is_operator_p
5616 = parser->greater_than_is_operator_p;
5617 parser->greater_than_is_operator_p = true;
5619 /* Consume expressions until there are no more. */
5620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5625 /* At the beginning of attribute lists, check to see if the
5626 next token is an identifier. */
5627 if (is_attribute_list == id_attr
5628 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5632 /* Consume the identifier. */
5633 token = cp_lexer_consume_token (parser->lexer);
5634 /* Save the identifier. */
5635 identifier = token->u.value;
5639 bool expr_non_constant_p;
5641 /* Parse the next assignment-expression. */
5642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5644 /* A braced-init-list. */
5645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5646 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5647 if (non_constant_p && expr_non_constant_p)
5648 *non_constant_p = true;
5650 else if (non_constant_p)
5652 expr = (cp_parser_constant_expression
5653 (parser, /*allow_non_constant_p=*/true,
5654 &expr_non_constant_p));
5655 if (expr_non_constant_p)
5656 *non_constant_p = true;
5659 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5662 expr = fold_non_dependent_expr (expr);
5664 /* If we have an ellipsis, then this is an expression
5666 if (allow_expansion_p
5667 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5669 /* Consume the `...'. */
5670 cp_lexer_consume_token (parser->lexer);
5672 /* Build the argument pack. */
5673 expr = make_pack_expansion (expr);
5676 /* Add it to the list. We add error_mark_node
5677 expressions to the list, so that we can still tell if
5678 the correct form for a parenthesized expression-list
5679 is found. That gives better errors. */
5680 VEC_safe_push (tree, gc, expression_list, expr);
5682 if (expr == error_mark_node)
5686 /* After the first item, attribute lists look the same as
5687 expression lists. */
5688 is_attribute_list = non_attr;
5691 /* If the next token isn't a `,', then we are done. */
5692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5695 /* Otherwise, consume the `,' and keep going. */
5696 cp_lexer_consume_token (parser->lexer);
5699 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5704 /* We try and resync to an unnested comma, as that will give the
5705 user better diagnostics. */
5706 ending = cp_parser_skip_to_closing_parenthesis (parser,
5707 /*recovering=*/true,
5709 /*consume_paren=*/true);
5714 parser->greater_than_is_operator_p
5715 = saved_greater_than_is_operator_p;
5720 parser->greater_than_is_operator_p
5721 = saved_greater_than_is_operator_p;
5724 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5726 return expression_list;
5729 /* Parse a pseudo-destructor-name.
5731 pseudo-destructor-name:
5732 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5733 :: [opt] nested-name-specifier template template-id :: ~ type-name
5734 :: [opt] nested-name-specifier [opt] ~ type-name
5736 If either of the first two productions is used, sets *SCOPE to the
5737 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5738 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5739 or ERROR_MARK_NODE if the parse fails. */
5742 cp_parser_pseudo_destructor_name (cp_parser* parser,
5746 bool nested_name_specifier_p;
5748 /* Assume that things will not work out. */
5749 *type = error_mark_node;
5751 /* Look for the optional `::' operator. */
5752 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5753 /* Look for the optional nested-name-specifier. */
5754 nested_name_specifier_p
5755 = (cp_parser_nested_name_specifier_opt (parser,
5756 /*typename_keyword_p=*/false,
5757 /*check_dependency_p=*/true,
5759 /*is_declaration=*/false)
5761 /* Now, if we saw a nested-name-specifier, we might be doing the
5762 second production. */
5763 if (nested_name_specifier_p
5764 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5766 /* Consume the `template' keyword. */
5767 cp_lexer_consume_token (parser->lexer);
5768 /* Parse the template-id. */
5769 cp_parser_template_id (parser,
5770 /*template_keyword_p=*/true,
5771 /*check_dependency_p=*/false,
5772 /*is_declaration=*/true);
5773 /* Look for the `::' token. */
5774 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5776 /* If the next token is not a `~', then there might be some
5777 additional qualification. */
5778 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5780 /* At this point, we're looking for "type-name :: ~". The type-name
5781 must not be a class-name, since this is a pseudo-destructor. So,
5782 it must be either an enum-name, or a typedef-name -- both of which
5783 are just identifiers. So, we peek ahead to check that the "::"
5784 and "~" tokens are present; if they are not, then we can avoid
5785 calling type_name. */
5786 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5787 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5788 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5790 cp_parser_error (parser, "non-scalar type");
5794 /* Look for the type-name. */
5795 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5796 if (*scope == error_mark_node)
5799 /* Look for the `::' token. */
5800 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5805 /* Look for the `~'. */
5806 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5807 /* Look for the type-name again. We are not responsible for
5808 checking that it matches the first type-name. */
5809 *type = cp_parser_nonclass_name (parser);
5812 /* Parse a unary-expression.
5818 unary-operator cast-expression
5819 sizeof unary-expression
5827 __extension__ cast-expression
5828 __alignof__ unary-expression
5829 __alignof__ ( type-id )
5830 __real__ cast-expression
5831 __imag__ cast-expression
5834 ADDRESS_P is true iff the unary-expression is appearing as the
5835 operand of the `&' operator. CAST_P is true if this expression is
5836 the target of a cast.
5838 Returns a representation of the expression. */
5841 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5845 enum tree_code unary_operator;
5847 /* Peek at the next token. */
5848 token = cp_lexer_peek_token (parser->lexer);
5849 /* Some keywords give away the kind of expression. */
5850 if (token->type == CPP_KEYWORD)
5852 enum rid keyword = token->keyword;
5862 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5863 /* Consume the token. */
5864 cp_lexer_consume_token (parser->lexer);
5865 /* Parse the operand. */
5866 operand = cp_parser_sizeof_operand (parser, keyword);
5868 if (TYPE_P (operand))
5869 return cxx_sizeof_or_alignof_type (operand, op, true);
5871 return cxx_sizeof_or_alignof_expr (operand, op, true);
5875 return cp_parser_new_expression (parser);
5878 return cp_parser_delete_expression (parser);
5882 /* The saved value of the PEDANTIC flag. */
5886 /* Save away the PEDANTIC flag. */
5887 cp_parser_extension_opt (parser, &saved_pedantic);
5888 /* Parse the cast-expression. */
5889 expr = cp_parser_simple_cast_expression (parser);
5890 /* Restore the PEDANTIC flag. */
5891 pedantic = saved_pedantic;
5901 /* Consume the `__real__' or `__imag__' token. */
5902 cp_lexer_consume_token (parser->lexer);
5903 /* Parse the cast-expression. */
5904 expression = cp_parser_simple_cast_expression (parser);
5905 /* Create the complete representation. */
5906 return build_x_unary_op ((keyword == RID_REALPART
5907 ? REALPART_EXPR : IMAGPART_EXPR),
5909 tf_warning_or_error);
5916 const char *saved_message;
5917 bool saved_integral_constant_expression_p;
5918 bool saved_non_integral_constant_expression_p;
5919 bool saved_greater_than_is_operator_p;
5921 cp_lexer_consume_token (parser->lexer);
5922 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5924 saved_message = parser->type_definition_forbidden_message;
5925 parser->type_definition_forbidden_message
5926 = G_("types may not be defined in %<noexcept%> expressions");
5928 saved_integral_constant_expression_p
5929 = parser->integral_constant_expression_p;
5930 saved_non_integral_constant_expression_p
5931 = parser->non_integral_constant_expression_p;
5932 parser->integral_constant_expression_p = false;
5934 saved_greater_than_is_operator_p
5935 = parser->greater_than_is_operator_p;
5936 parser->greater_than_is_operator_p = true;
5938 ++cp_unevaluated_operand;
5939 ++c_inhibit_evaluation_warnings;
5940 expr = cp_parser_expression (parser, false, NULL);
5941 --c_inhibit_evaluation_warnings;
5942 --cp_unevaluated_operand;
5944 parser->greater_than_is_operator_p
5945 = saved_greater_than_is_operator_p;
5947 parser->integral_constant_expression_p
5948 = saved_integral_constant_expression_p;
5949 parser->non_integral_constant_expression_p
5950 = saved_non_integral_constant_expression_p;
5952 parser->type_definition_forbidden_message = saved_message;
5954 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5955 return finish_noexcept_expr (expr, tf_warning_or_error);
5963 /* Look for the `:: new' and `:: delete', which also signal the
5964 beginning of a new-expression, or delete-expression,
5965 respectively. If the next token is `::', then it might be one of
5967 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5971 /* See if the token after the `::' is one of the keywords in
5972 which we're interested. */
5973 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5974 /* If it's `new', we have a new-expression. */
5975 if (keyword == RID_NEW)
5976 return cp_parser_new_expression (parser);
5977 /* Similarly, for `delete'. */
5978 else if (keyword == RID_DELETE)
5979 return cp_parser_delete_expression (parser);
5982 /* Look for a unary operator. */
5983 unary_operator = cp_parser_unary_operator (token);
5984 /* The `++' and `--' operators can be handled similarly, even though
5985 they are not technically unary-operators in the grammar. */
5986 if (unary_operator == ERROR_MARK)
5988 if (token->type == CPP_PLUS_PLUS)
5989 unary_operator = PREINCREMENT_EXPR;
5990 else if (token->type == CPP_MINUS_MINUS)
5991 unary_operator = PREDECREMENT_EXPR;
5992 /* Handle the GNU address-of-label extension. */
5993 else if (cp_parser_allow_gnu_extensions_p (parser)
5994 && token->type == CPP_AND_AND)
5998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6000 /* Consume the '&&' token. */
6001 cp_lexer_consume_token (parser->lexer);
6002 /* Look for the identifier. */
6003 identifier = cp_parser_identifier (parser);
6004 /* Create an expression representing the address. */
6005 expression = finish_label_address_expr (identifier, loc);
6006 if (cp_parser_non_integral_constant_expression (parser,
6008 expression = error_mark_node;
6012 if (unary_operator != ERROR_MARK)
6014 tree cast_expression;
6015 tree expression = error_mark_node;
6016 non_integral_constant non_constant_p = NIC_NONE;
6018 /* Consume the operator token. */
6019 token = cp_lexer_consume_token (parser->lexer);
6020 /* Parse the cast-expression. */
6022 = cp_parser_cast_expression (parser,
6023 unary_operator == ADDR_EXPR,
6024 /*cast_p=*/false, pidk);
6025 /* Now, build an appropriate representation. */
6026 switch (unary_operator)
6029 non_constant_p = NIC_STAR;
6030 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6031 tf_warning_or_error);
6035 non_constant_p = NIC_ADDR;
6038 expression = build_x_unary_op (unary_operator, cast_expression,
6039 tf_warning_or_error);
6042 case PREINCREMENT_EXPR:
6043 case PREDECREMENT_EXPR:
6044 non_constant_p = unary_operator == PREINCREMENT_EXPR
6045 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6047 case UNARY_PLUS_EXPR:
6049 case TRUTH_NOT_EXPR:
6050 expression = finish_unary_op_expr (unary_operator, cast_expression);
6057 if (non_constant_p != NIC_NONE
6058 && cp_parser_non_integral_constant_expression (parser,
6060 expression = error_mark_node;
6065 return cp_parser_postfix_expression (parser, address_p, cast_p,
6066 /*member_access_only_p=*/false,
6070 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6071 unary-operator, the corresponding tree code is returned. */
6073 static enum tree_code
6074 cp_parser_unary_operator (cp_token* token)
6076 switch (token->type)
6079 return INDIRECT_REF;
6085 return UNARY_PLUS_EXPR;
6091 return TRUTH_NOT_EXPR;
6094 return BIT_NOT_EXPR;
6101 /* Parse a new-expression.
6104 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6105 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6107 Returns a representation of the expression. */
6110 cp_parser_new_expression (cp_parser* parser)
6112 bool global_scope_p;
6113 VEC(tree,gc) *placement;
6115 VEC(tree,gc) *initializer;
6119 /* Look for the optional `::' operator. */
6121 = (cp_parser_global_scope_opt (parser,
6122 /*current_scope_valid_p=*/false)
6124 /* Look for the `new' operator. */
6125 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6126 /* There's no easy way to tell a new-placement from the
6127 `( type-id )' construct. */
6128 cp_parser_parse_tentatively (parser);
6129 /* Look for a new-placement. */
6130 placement = cp_parser_new_placement (parser);
6131 /* If that didn't work out, there's no new-placement. */
6132 if (!cp_parser_parse_definitely (parser))
6134 if (placement != NULL)
6135 release_tree_vector (placement);
6139 /* If the next token is a `(', then we have a parenthesized
6141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6144 /* Consume the `('. */
6145 cp_lexer_consume_token (parser->lexer);
6146 /* Parse the type-id. */
6147 type = cp_parser_type_id (parser);
6148 /* Look for the closing `)'. */
6149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6150 token = cp_lexer_peek_token (parser->lexer);
6151 /* There should not be a direct-new-declarator in this production,
6152 but GCC used to allowed this, so we check and emit a sensible error
6153 message for this case. */
6154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6156 error_at (token->location,
6157 "array bound forbidden after parenthesized type-id");
6158 inform (token->location,
6159 "try removing the parentheses around the type-id");
6160 cp_parser_direct_new_declarator (parser);
6164 /* Otherwise, there must be a new-type-id. */
6166 type = cp_parser_new_type_id (parser, &nelts);
6168 /* If the next token is a `(' or '{', then we have a new-initializer. */
6169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6170 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6171 initializer = cp_parser_new_initializer (parser);
6175 /* A new-expression may not appear in an integral constant
6177 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6178 ret = error_mark_node;
6181 /* Create a representation of the new-expression. */
6182 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6183 tf_warning_or_error);
6186 if (placement != NULL)
6187 release_tree_vector (placement);
6188 if (initializer != NULL)
6189 release_tree_vector (initializer);
6194 /* Parse a new-placement.
6199 Returns the same representation as for an expression-list. */
6201 static VEC(tree,gc) *
6202 cp_parser_new_placement (cp_parser* parser)
6204 VEC(tree,gc) *expression_list;
6206 /* Parse the expression-list. */
6207 expression_list = (cp_parser_parenthesized_expression_list
6208 (parser, non_attr, /*cast_p=*/false,
6209 /*allow_expansion_p=*/true,
6210 /*non_constant_p=*/NULL));
6212 return expression_list;
6215 /* Parse a new-type-id.
6218 type-specifier-seq new-declarator [opt]
6220 Returns the TYPE allocated. If the new-type-id indicates an array
6221 type, *NELTS is set to the number of elements in the last array
6222 bound; the TYPE will not include the last array bound. */
6225 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6227 cp_decl_specifier_seq type_specifier_seq;
6228 cp_declarator *new_declarator;
6229 cp_declarator *declarator;
6230 cp_declarator *outer_declarator;
6231 const char *saved_message;
6234 /* The type-specifier sequence must not contain type definitions.
6235 (It cannot contain declarations of new types either, but if they
6236 are not definitions we will catch that because they are not
6238 saved_message = parser->type_definition_forbidden_message;
6239 parser->type_definition_forbidden_message
6240 = G_("types may not be defined in a new-type-id");
6241 /* Parse the type-specifier-seq. */
6242 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6243 /*is_trailing_return=*/false,
6244 &type_specifier_seq);
6245 /* Restore the old message. */
6246 parser->type_definition_forbidden_message = saved_message;
6247 /* Parse the new-declarator. */
6248 new_declarator = cp_parser_new_declarator_opt (parser);
6250 /* Determine the number of elements in the last array dimension, if
6253 /* Skip down to the last array dimension. */
6254 declarator = new_declarator;
6255 outer_declarator = NULL;
6256 while (declarator && (declarator->kind == cdk_pointer
6257 || declarator->kind == cdk_ptrmem))
6259 outer_declarator = declarator;
6260 declarator = declarator->declarator;
6263 && declarator->kind == cdk_array
6264 && declarator->declarator
6265 && declarator->declarator->kind == cdk_array)
6267 outer_declarator = declarator;
6268 declarator = declarator->declarator;
6271 if (declarator && declarator->kind == cdk_array)
6273 *nelts = declarator->u.array.bounds;
6274 if (*nelts == error_mark_node)
6275 *nelts = integer_one_node;
6277 if (outer_declarator)
6278 outer_declarator->declarator = declarator->declarator;
6280 new_declarator = NULL;
6283 type = groktypename (&type_specifier_seq, new_declarator, false);
6287 /* Parse an (optional) new-declarator.
6290 ptr-operator new-declarator [opt]
6291 direct-new-declarator
6293 Returns the declarator. */
6295 static cp_declarator *
6296 cp_parser_new_declarator_opt (cp_parser* parser)
6298 enum tree_code code;
6300 cp_cv_quals cv_quals;
6302 /* We don't know if there's a ptr-operator next, or not. */
6303 cp_parser_parse_tentatively (parser);
6304 /* Look for a ptr-operator. */
6305 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6306 /* If that worked, look for more new-declarators. */
6307 if (cp_parser_parse_definitely (parser))
6309 cp_declarator *declarator;
6311 /* Parse another optional declarator. */
6312 declarator = cp_parser_new_declarator_opt (parser);
6314 return cp_parser_make_indirect_declarator
6315 (code, type, cv_quals, declarator);
6318 /* If the next token is a `[', there is a direct-new-declarator. */
6319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6320 return cp_parser_direct_new_declarator (parser);
6325 /* Parse a direct-new-declarator.
6327 direct-new-declarator:
6329 direct-new-declarator [constant-expression]
6333 static cp_declarator *
6334 cp_parser_direct_new_declarator (cp_parser* parser)
6336 cp_declarator *declarator = NULL;
6342 /* Look for the opening `['. */
6343 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6344 /* The first expression is not required to be constant. */
6347 cp_token *token = cp_lexer_peek_token (parser->lexer);
6348 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6349 /* The standard requires that the expression have integral
6350 type. DR 74 adds enumeration types. We believe that the
6351 real intent is that these expressions be handled like the
6352 expression in a `switch' condition, which also allows
6353 classes with a single conversion to integral or
6354 enumeration type. */
6355 if (!processing_template_decl)
6358 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6363 error_at (token->location,
6364 "expression in new-declarator must have integral "
6365 "or enumeration type");
6366 expression = error_mark_node;
6370 /* But all the other expressions must be. */
6373 = cp_parser_constant_expression (parser,
6374 /*allow_non_constant=*/false,
6376 /* Look for the closing `]'. */
6377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6379 /* Add this bound to the declarator. */
6380 declarator = make_array_declarator (declarator, expression);
6382 /* If the next token is not a `[', then there are no more
6384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6391 /* Parse a new-initializer.
6394 ( expression-list [opt] )
6397 Returns a representation of the expression-list. */
6399 static VEC(tree,gc) *
6400 cp_parser_new_initializer (cp_parser* parser)
6402 VEC(tree,gc) *expression_list;
6404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6407 bool expr_non_constant_p;
6408 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6409 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6410 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6411 expression_list = make_tree_vector_single (t);
6414 expression_list = (cp_parser_parenthesized_expression_list
6415 (parser, non_attr, /*cast_p=*/false,
6416 /*allow_expansion_p=*/true,
6417 /*non_constant_p=*/NULL));
6419 return expression_list;
6422 /* Parse a delete-expression.
6425 :: [opt] delete cast-expression
6426 :: [opt] delete [ ] cast-expression
6428 Returns a representation of the expression. */
6431 cp_parser_delete_expression (cp_parser* parser)
6433 bool global_scope_p;
6437 /* Look for the optional `::' operator. */
6439 = (cp_parser_global_scope_opt (parser,
6440 /*current_scope_valid_p=*/false)
6442 /* Look for the `delete' keyword. */
6443 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6444 /* See if the array syntax is in use. */
6445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6447 /* Consume the `[' token. */
6448 cp_lexer_consume_token (parser->lexer);
6449 /* Look for the `]' token. */
6450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6451 /* Remember that this is the `[]' construct. */
6457 /* Parse the cast-expression. */
6458 expression = cp_parser_simple_cast_expression (parser);
6460 /* A delete-expression may not appear in an integral constant
6462 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6463 return error_mark_node;
6465 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6468 /* Returns true if TOKEN may start a cast-expression and false
6472 cp_parser_token_starts_cast_expression (cp_token *token)
6474 switch (token->type)
6480 case CPP_CLOSE_SQUARE:
6481 case CPP_CLOSE_PAREN:
6482 case CPP_CLOSE_BRACE:
6486 case CPP_DEREF_STAR:
6494 case CPP_GREATER_EQ:
6514 /* '[' may start a primary-expression in obj-c++. */
6515 case CPP_OPEN_SQUARE:
6516 return c_dialect_objc ();
6523 /* Parse a cast-expression.
6527 ( type-id ) cast-expression
6529 ADDRESS_P is true iff the unary-expression is appearing as the
6530 operand of the `&' operator. CAST_P is true if this expression is
6531 the target of a cast.
6533 Returns a representation of the expression. */
6536 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6539 /* If it's a `(', then we might be looking at a cast. */
6540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6542 tree type = NULL_TREE;
6543 tree expr = NULL_TREE;
6544 bool compound_literal_p;
6545 const char *saved_message;
6547 /* There's no way to know yet whether or not this is a cast.
6548 For example, `(int (3))' is a unary-expression, while `(int)
6549 3' is a cast. So, we resort to parsing tentatively. */
6550 cp_parser_parse_tentatively (parser);
6551 /* Types may not be defined in a cast. */
6552 saved_message = parser->type_definition_forbidden_message;
6553 parser->type_definition_forbidden_message
6554 = G_("types may not be defined in casts");
6555 /* Consume the `('. */
6556 cp_lexer_consume_token (parser->lexer);
6557 /* A very tricky bit is that `(struct S) { 3 }' is a
6558 compound-literal (which we permit in C++ as an extension).
6559 But, that construct is not a cast-expression -- it is a
6560 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6561 is legal; if the compound-literal were a cast-expression,
6562 you'd need an extra set of parentheses.) But, if we parse
6563 the type-id, and it happens to be a class-specifier, then we
6564 will commit to the parse at that point, because we cannot
6565 undo the action that is done when creating a new class. So,
6566 then we cannot back up and do a postfix-expression.
6568 Therefore, we scan ahead to the closing `)', and check to see
6569 if the token after the `)' is a `{'. If so, we are not
6570 looking at a cast-expression.
6572 Save tokens so that we can put them back. */
6573 cp_lexer_save_tokens (parser->lexer);
6574 /* Skip tokens until the next token is a closing parenthesis.
6575 If we find the closing `)', and the next token is a `{', then
6576 we are looking at a compound-literal. */
6578 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6579 /*consume_paren=*/true)
6580 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6581 /* Roll back the tokens we skipped. */
6582 cp_lexer_rollback_tokens (parser->lexer);
6583 /* If we were looking at a compound-literal, simulate an error
6584 so that the call to cp_parser_parse_definitely below will
6586 if (compound_literal_p)
6587 cp_parser_simulate_error (parser);
6590 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6591 parser->in_type_id_in_expr_p = true;
6592 /* Look for the type-id. */
6593 type = cp_parser_type_id (parser);
6594 /* Look for the closing `)'. */
6595 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6596 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6599 /* Restore the saved message. */
6600 parser->type_definition_forbidden_message = saved_message;
6602 /* At this point this can only be either a cast or a
6603 parenthesized ctor such as `(T ())' that looks like a cast to
6604 function returning T. */
6605 if (!cp_parser_error_occurred (parser)
6606 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6609 cp_parser_parse_definitely (parser);
6610 expr = cp_parser_cast_expression (parser,
6611 /*address_p=*/false,
6612 /*cast_p=*/true, pidk);
6614 /* Warn about old-style casts, if so requested. */
6615 if (warn_old_style_cast
6616 && !in_system_header
6617 && !VOID_TYPE_P (type)
6618 && current_lang_name != lang_name_c)
6619 warning (OPT_Wold_style_cast, "use of old-style cast");
6621 /* Only type conversions to integral or enumeration types
6622 can be used in constant-expressions. */
6623 if (!cast_valid_in_integral_constant_expression_p (type)
6624 && cp_parser_non_integral_constant_expression (parser,
6626 return error_mark_node;
6628 /* Perform the cast. */
6629 expr = build_c_cast (input_location, type, expr);
6633 cp_parser_abort_tentative_parse (parser);
6636 /* If we get here, then it's not a cast, so it must be a
6637 unary-expression. */
6638 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6641 /* Parse a binary expression of the general form:
6645 pm-expression .* cast-expression
6646 pm-expression ->* cast-expression
6648 multiplicative-expression:
6650 multiplicative-expression * pm-expression
6651 multiplicative-expression / pm-expression
6652 multiplicative-expression % pm-expression
6654 additive-expression:
6655 multiplicative-expression
6656 additive-expression + multiplicative-expression
6657 additive-expression - multiplicative-expression
6661 shift-expression << additive-expression
6662 shift-expression >> additive-expression
6664 relational-expression:
6666 relational-expression < shift-expression
6667 relational-expression > shift-expression
6668 relational-expression <= shift-expression
6669 relational-expression >= shift-expression
6673 relational-expression:
6674 relational-expression <? shift-expression
6675 relational-expression >? shift-expression
6677 equality-expression:
6678 relational-expression
6679 equality-expression == relational-expression
6680 equality-expression != relational-expression
6684 and-expression & equality-expression
6686 exclusive-or-expression:
6688 exclusive-or-expression ^ and-expression
6690 inclusive-or-expression:
6691 exclusive-or-expression
6692 inclusive-or-expression | exclusive-or-expression
6694 logical-and-expression:
6695 inclusive-or-expression
6696 logical-and-expression && inclusive-or-expression
6698 logical-or-expression:
6699 logical-and-expression
6700 logical-or-expression || logical-and-expression
6702 All these are implemented with a single function like:
6705 simple-cast-expression
6706 binary-expression <token> binary-expression
6708 CAST_P is true if this expression is the target of a cast.
6710 The binops_by_token map is used to get the tree codes for each <token> type.
6711 binary-expressions are associated according to a precedence table. */
6713 #define TOKEN_PRECEDENCE(token) \
6714 (((token->type == CPP_GREATER \
6715 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6716 && !parser->greater_than_is_operator_p) \
6717 ? PREC_NOT_OPERATOR \
6718 : binops_by_token[token->type].prec)
6721 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6722 bool no_toplevel_fold_p,
6723 enum cp_parser_prec prec,
6726 cp_parser_expression_stack stack;
6727 cp_parser_expression_stack_entry *sp = &stack[0];
6730 enum tree_code tree_type, lhs_type, rhs_type;
6731 enum cp_parser_prec new_prec, lookahead_prec;
6734 /* Parse the first expression. */
6735 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6736 lhs_type = ERROR_MARK;
6740 /* Get an operator token. */
6741 token = cp_lexer_peek_token (parser->lexer);
6743 if (warn_cxx0x_compat
6744 && token->type == CPP_RSHIFT
6745 && !parser->greater_than_is_operator_p)
6747 if (warning_at (token->location, OPT_Wc__0x_compat,
6748 "%<>>%> operator will be treated as"
6749 " two right angle brackets in C++0x"))
6750 inform (token->location,
6751 "suggest parentheses around %<>>%> expression");
6754 new_prec = TOKEN_PRECEDENCE (token);
6756 /* Popping an entry off the stack means we completed a subexpression:
6757 - either we found a token which is not an operator (`>' where it is not
6758 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6759 will happen repeatedly;
6760 - or, we found an operator which has lower priority. This is the case
6761 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6763 if (new_prec <= prec)
6772 tree_type = binops_by_token[token->type].tree_type;
6774 /* We used the operator token. */
6775 cp_lexer_consume_token (parser->lexer);
6777 /* For "false && x" or "true || x", x will never be executed;
6778 disable warnings while evaluating it. */
6779 if (tree_type == TRUTH_ANDIF_EXPR)
6780 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6781 else if (tree_type == TRUTH_ORIF_EXPR)
6782 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6784 /* Extract another operand. It may be the RHS of this expression
6785 or the LHS of a new, higher priority expression. */
6786 rhs = cp_parser_simple_cast_expression (parser);
6787 rhs_type = ERROR_MARK;
6789 /* Get another operator token. Look up its precedence to avoid
6790 building a useless (immediately popped) stack entry for common
6791 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 lookahead_prec = TOKEN_PRECEDENCE (token);
6794 if (lookahead_prec > new_prec)
6796 /* ... and prepare to parse the RHS of the new, higher priority
6797 expression. Since precedence levels on the stack are
6798 monotonically increasing, we do not have to care about
6801 sp->tree_type = tree_type;
6803 sp->lhs_type = lhs_type;
6806 lhs_type = rhs_type;
6808 new_prec = lookahead_prec;
6812 lookahead_prec = new_prec;
6813 /* If the stack is not empty, we have parsed into LHS the right side
6814 (`4' in the example above) of an expression we had suspended.
6815 We can use the information on the stack to recover the LHS (`3')
6816 from the stack together with the tree code (`MULT_EXPR'), and
6817 the precedence of the higher level subexpression
6818 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6819 which will be used to actually build the additive expression. */
6822 tree_type = sp->tree_type;
6824 rhs_type = lhs_type;
6826 lhs_type = sp->lhs_type;
6829 /* Undo the disabling of warnings done above. */
6830 if (tree_type == TRUTH_ANDIF_EXPR)
6831 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6832 else if (tree_type == TRUTH_ORIF_EXPR)
6833 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6835 overloaded_p = false;
6836 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6837 ERROR_MARK for everything that is not a binary expression.
6838 This makes warn_about_parentheses miss some warnings that
6839 involve unary operators. For unary expressions we should
6840 pass the correct tree_code unless the unary expression was
6841 surrounded by parentheses.
6843 if (no_toplevel_fold_p
6844 && lookahead_prec <= prec
6846 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6847 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6849 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6850 &overloaded_p, tf_warning_or_error);
6851 lhs_type = tree_type;
6853 /* If the binary operator required the use of an overloaded operator,
6854 then this expression cannot be an integral constant-expression.
6855 An overloaded operator can be used even if both operands are
6856 otherwise permissible in an integral constant-expression if at
6857 least one of the operands is of enumeration type. */
6860 && cp_parser_non_integral_constant_expression (parser,
6862 return error_mark_node;
6869 /* Parse the `? expression : assignment-expression' part of a
6870 conditional-expression. The LOGICAL_OR_EXPR is the
6871 logical-or-expression that started the conditional-expression.
6872 Returns a representation of the entire conditional-expression.
6874 This routine is used by cp_parser_assignment_expression.
6876 ? expression : assignment-expression
6880 ? : assignment-expression */
6883 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6886 tree assignment_expr;
6887 struct cp_token *token;
6889 /* Consume the `?' token. */
6890 cp_lexer_consume_token (parser->lexer);
6891 token = cp_lexer_peek_token (parser->lexer);
6892 if (cp_parser_allow_gnu_extensions_p (parser)
6893 && token->type == CPP_COLON)
6895 pedwarn (token->location, OPT_pedantic,
6896 "ISO C++ does not allow ?: with omitted middle operand");
6897 /* Implicit true clause. */
6899 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6900 warn_for_omitted_condop (token->location, logical_or_expr);
6904 /* Parse the expression. */
6905 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6906 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6907 c_inhibit_evaluation_warnings +=
6908 ((logical_or_expr == truthvalue_true_node)
6909 - (logical_or_expr == truthvalue_false_node));
6912 /* The next token should be a `:'. */
6913 cp_parser_require (parser, CPP_COLON, RT_COLON);
6914 /* Parse the assignment-expression. */
6915 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6916 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6918 /* Build the conditional-expression. */
6919 return build_x_conditional_expr (logical_or_expr,
6922 tf_warning_or_error);
6925 /* Parse an assignment-expression.
6927 assignment-expression:
6928 conditional-expression
6929 logical-or-expression assignment-operator assignment_expression
6932 CAST_P is true if this expression is the target of a cast.
6934 Returns a representation for the expression. */
6937 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6942 /* If the next token is the `throw' keyword, then we're looking at
6943 a throw-expression. */
6944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6945 expr = cp_parser_throw_expression (parser);
6946 /* Otherwise, it must be that we are looking at a
6947 logical-or-expression. */
6950 /* Parse the binary expressions (logical-or-expression). */
6951 expr = cp_parser_binary_expression (parser, cast_p, false,
6952 PREC_NOT_OPERATOR, pidk);
6953 /* If the next token is a `?' then we're actually looking at a
6954 conditional-expression. */
6955 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6956 return cp_parser_question_colon_clause (parser, expr);
6959 enum tree_code assignment_operator;
6961 /* If it's an assignment-operator, we're using the second
6964 = cp_parser_assignment_operator_opt (parser);
6965 if (assignment_operator != ERROR_MARK)
6967 bool non_constant_p;
6969 /* Parse the right-hand side of the assignment. */
6970 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6972 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6975 /* An assignment may not appear in a
6976 constant-expression. */
6977 if (cp_parser_non_integral_constant_expression (parser,
6979 return error_mark_node;
6980 /* Build the assignment expression. */
6981 expr = build_x_modify_expr (expr,
6982 assignment_operator,
6984 tf_warning_or_error);
6992 /* Parse an (optional) assignment-operator.
6994 assignment-operator: one of
6995 = *= /= %= += -= >>= <<= &= ^= |=
6999 assignment-operator: one of
7002 If the next token is an assignment operator, the corresponding tree
7003 code is returned, and the token is consumed. For example, for
7004 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7005 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7006 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7007 operator, ERROR_MARK is returned. */
7009 static enum tree_code
7010 cp_parser_assignment_operator_opt (cp_parser* parser)
7015 /* Peek at the next token. */
7016 token = cp_lexer_peek_token (parser->lexer);
7018 switch (token->type)
7029 op = TRUNC_DIV_EXPR;
7033 op = TRUNC_MOD_EXPR;
7065 /* Nothing else is an assignment operator. */
7069 /* If it was an assignment operator, consume it. */
7070 if (op != ERROR_MARK)
7071 cp_lexer_consume_token (parser->lexer);
7076 /* Parse an expression.
7079 assignment-expression
7080 expression , assignment-expression
7082 CAST_P is true if this expression is the target of a cast.
7084 Returns a representation of the expression. */
7087 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7089 tree expression = NULL_TREE;
7093 tree assignment_expression;
7095 /* Parse the next assignment-expression. */
7096 assignment_expression
7097 = cp_parser_assignment_expression (parser, cast_p, pidk);
7098 /* If this is the first assignment-expression, we can just
7101 expression = assignment_expression;
7103 expression = build_x_compound_expr (expression,
7104 assignment_expression,
7105 tf_warning_or_error);
7106 /* If the next token is not a comma, then we are done with the
7108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7110 /* Consume the `,'. */
7111 cp_lexer_consume_token (parser->lexer);
7112 /* A comma operator cannot appear in a constant-expression. */
7113 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7114 expression = error_mark_node;
7120 /* Parse a constant-expression.
7122 constant-expression:
7123 conditional-expression
7125 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7126 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7127 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7128 is false, NON_CONSTANT_P should be NULL. */
7131 cp_parser_constant_expression (cp_parser* parser,
7132 bool allow_non_constant_p,
7133 bool *non_constant_p)
7135 bool saved_integral_constant_expression_p;
7136 bool saved_allow_non_integral_constant_expression_p;
7137 bool saved_non_integral_constant_expression_p;
7140 /* It might seem that we could simply parse the
7141 conditional-expression, and then check to see if it were
7142 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7143 one that the compiler can figure out is constant, possibly after
7144 doing some simplifications or optimizations. The standard has a
7145 precise definition of constant-expression, and we must honor
7146 that, even though it is somewhat more restrictive.
7152 is not a legal declaration, because `(2, 3)' is not a
7153 constant-expression. The `,' operator is forbidden in a
7154 constant-expression. However, GCC's constant-folding machinery
7155 will fold this operation to an INTEGER_CST for `3'. */
7157 /* Save the old settings. */
7158 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7159 saved_allow_non_integral_constant_expression_p
7160 = parser->allow_non_integral_constant_expression_p;
7161 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7162 /* We are now parsing a constant-expression. */
7163 parser->integral_constant_expression_p = true;
7164 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7165 parser->non_integral_constant_expression_p = false;
7166 /* Although the grammar says "conditional-expression", we parse an
7167 "assignment-expression", which also permits "throw-expression"
7168 and the use of assignment operators. In the case that
7169 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7170 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7171 actually essential that we look for an assignment-expression.
7172 For example, cp_parser_initializer_clauses uses this function to
7173 determine whether a particular assignment-expression is in fact
7175 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7176 /* Restore the old settings. */
7177 parser->integral_constant_expression_p
7178 = saved_integral_constant_expression_p;
7179 parser->allow_non_integral_constant_expression_p
7180 = saved_allow_non_integral_constant_expression_p;
7181 if (allow_non_constant_p)
7182 *non_constant_p = parser->non_integral_constant_expression_p;
7183 else if (parser->non_integral_constant_expression_p)
7184 expression = error_mark_node;
7185 parser->non_integral_constant_expression_p
7186 = saved_non_integral_constant_expression_p;
7191 /* Parse __builtin_offsetof.
7193 offsetof-expression:
7194 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7196 offsetof-member-designator:
7198 | offsetof-member-designator "." id-expression
7199 | offsetof-member-designator "[" expression "]"
7200 | offsetof-member-designator "->" id-expression */
7203 cp_parser_builtin_offsetof (cp_parser *parser)
7205 int save_ice_p, save_non_ice_p;
7210 /* We're about to accept non-integral-constant things, but will
7211 definitely yield an integral constant expression. Save and
7212 restore these values around our local parsing. */
7213 save_ice_p = parser->integral_constant_expression_p;
7214 save_non_ice_p = parser->non_integral_constant_expression_p;
7216 /* Consume the "__builtin_offsetof" token. */
7217 cp_lexer_consume_token (parser->lexer);
7218 /* Consume the opening `('. */
7219 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7220 /* Parse the type-id. */
7221 type = cp_parser_type_id (parser);
7222 /* Look for the `,'. */
7223 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7224 token = cp_lexer_peek_token (parser->lexer);
7226 /* Build the (type *)null that begins the traditional offsetof macro. */
7227 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7228 tf_warning_or_error);
7230 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7231 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7232 true, &dummy, token->location);
7235 token = cp_lexer_peek_token (parser->lexer);
7236 switch (token->type)
7238 case CPP_OPEN_SQUARE:
7239 /* offsetof-member-designator "[" expression "]" */
7240 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7244 /* offsetof-member-designator "->" identifier */
7245 expr = grok_array_decl (expr, integer_zero_node);
7249 /* offsetof-member-designator "." identifier */
7250 cp_lexer_consume_token (parser->lexer);
7251 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7256 case CPP_CLOSE_PAREN:
7257 /* Consume the ")" token. */
7258 cp_lexer_consume_token (parser->lexer);
7262 /* Error. We know the following require will fail, but
7263 that gives the proper error message. */
7264 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7265 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7266 expr = error_mark_node;
7272 /* If we're processing a template, we can't finish the semantics yet.
7273 Otherwise we can fold the entire expression now. */
7274 if (processing_template_decl)
7275 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7277 expr = finish_offsetof (expr);
7280 parser->integral_constant_expression_p = save_ice_p;
7281 parser->non_integral_constant_expression_p = save_non_ice_p;
7286 /* Parse a trait expression. */
7289 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7292 tree type1, type2 = NULL_TREE;
7293 bool binary = false;
7294 cp_decl_specifier_seq decl_specs;
7298 case RID_HAS_NOTHROW_ASSIGN:
7299 kind = CPTK_HAS_NOTHROW_ASSIGN;
7301 case RID_HAS_NOTHROW_CONSTRUCTOR:
7302 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7304 case RID_HAS_NOTHROW_COPY:
7305 kind = CPTK_HAS_NOTHROW_COPY;
7307 case RID_HAS_TRIVIAL_ASSIGN:
7308 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7310 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7311 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7313 case RID_HAS_TRIVIAL_COPY:
7314 kind = CPTK_HAS_TRIVIAL_COPY;
7316 case RID_HAS_TRIVIAL_DESTRUCTOR:
7317 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7319 case RID_HAS_VIRTUAL_DESTRUCTOR:
7320 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7322 case RID_IS_ABSTRACT:
7323 kind = CPTK_IS_ABSTRACT;
7325 case RID_IS_BASE_OF:
7326 kind = CPTK_IS_BASE_OF;
7330 kind = CPTK_IS_CLASS;
7332 case RID_IS_CONVERTIBLE_TO:
7333 kind = CPTK_IS_CONVERTIBLE_TO;
7337 kind = CPTK_IS_EMPTY;
7340 kind = CPTK_IS_ENUM;
7345 case RID_IS_POLYMORPHIC:
7346 kind = CPTK_IS_POLYMORPHIC;
7348 case RID_IS_STD_LAYOUT:
7349 kind = CPTK_IS_STD_LAYOUT;
7351 case RID_IS_TRIVIAL:
7352 kind = CPTK_IS_TRIVIAL;
7355 kind = CPTK_IS_UNION;
7361 /* Consume the token. */
7362 cp_lexer_consume_token (parser->lexer);
7364 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7366 type1 = cp_parser_type_id (parser);
7368 if (type1 == error_mark_node)
7369 return error_mark_node;
7371 /* Build a trivial decl-specifier-seq. */
7372 clear_decl_specs (&decl_specs);
7373 decl_specs.type = type1;
7375 /* Call grokdeclarator to figure out what type this is. */
7376 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7377 /*initialized=*/0, /*attrlist=*/NULL);
7381 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7383 type2 = cp_parser_type_id (parser);
7385 if (type2 == error_mark_node)
7386 return error_mark_node;
7388 /* Build a trivial decl-specifier-seq. */
7389 clear_decl_specs (&decl_specs);
7390 decl_specs.type = type2;
7392 /* Call grokdeclarator to figure out what type this is. */
7393 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7394 /*initialized=*/0, /*attrlist=*/NULL);
7397 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7399 /* Complete the trait expression, which may mean either processing
7400 the trait expr now or saving it for template instantiation. */
7401 return finish_trait_expr (kind, type1, type2);
7404 /* Lambdas that appear in variable initializer or default argument scope
7405 get that in their mangling, so we need to record it. We might as well
7406 use the count for function and namespace scopes as well. */
7407 static GTY(()) tree lambda_scope;
7408 static GTY(()) int lambda_count;
7409 typedef struct GTY(()) tree_int
7414 DEF_VEC_O(tree_int);
7415 DEF_VEC_ALLOC_O(tree_int,gc);
7416 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7419 start_lambda_scope (tree decl)
7423 /* Once we're inside a function, we ignore other scopes and just push
7424 the function again so that popping works properly. */
7425 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7426 decl = current_function_decl;
7427 ti.t = lambda_scope;
7428 ti.i = lambda_count;
7429 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7430 if (lambda_scope != decl)
7432 /* Don't reset the count if we're still in the same function. */
7433 lambda_scope = decl;
7439 record_lambda_scope (tree lambda)
7441 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7442 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7446 finish_lambda_scope (void)
7448 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7449 if (lambda_scope != p->t)
7451 lambda_scope = p->t;
7452 lambda_count = p->i;
7454 VEC_pop (tree_int, lambda_scope_stack);
7457 /* Parse a lambda expression.
7460 lambda-introducer lambda-declarator [opt] compound-statement
7462 Returns a representation of the expression. */
7465 cp_parser_lambda_expression (cp_parser* parser)
7467 tree lambda_expr = build_lambda_expr ();
7470 LAMBDA_EXPR_LOCATION (lambda_expr)
7471 = cp_lexer_peek_token (parser->lexer)->location;
7473 if (cp_unevaluated_operand)
7474 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7475 "lambda-expression in unevaluated context");
7477 /* We may be in the middle of deferred access check. Disable
7479 push_deferring_access_checks (dk_no_deferred);
7481 cp_parser_lambda_introducer (parser, lambda_expr);
7483 type = begin_lambda_type (lambda_expr);
7485 record_lambda_scope (lambda_expr);
7487 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7488 determine_visibility (TYPE_NAME (type));
7490 /* Now that we've started the type, add the capture fields for any
7491 explicit captures. */
7492 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7495 /* Inside the class, surrounding template-parameter-lists do not apply. */
7496 unsigned int saved_num_template_parameter_lists
7497 = parser->num_template_parameter_lists;
7499 parser->num_template_parameter_lists = 0;
7501 /* By virtue of defining a local class, a lambda expression has access to
7502 the private variables of enclosing classes. */
7504 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7506 cp_parser_lambda_body (parser, lambda_expr);
7508 /* The capture list was built up in reverse order; fix that now. */
7510 tree newlist = NULL_TREE;
7513 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7516 tree field = TREE_PURPOSE (elt);
7519 next = TREE_CHAIN (elt);
7520 TREE_CHAIN (elt) = newlist;
7523 /* Also add __ to the beginning of the field name so that code
7524 outside the lambda body can't see the captured name. We could
7525 just remove the name entirely, but this is more useful for
7527 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7528 /* The 'this' capture already starts with __. */
7531 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7532 buf[1] = buf[0] = '_';
7533 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7534 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7535 DECL_NAME (field) = get_identifier (buf);
7537 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7540 maybe_add_lambda_conv_op (type);
7542 type = finish_struct (type, /*attributes=*/NULL_TREE);
7544 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7547 pop_deferring_access_checks ();
7549 return build_lambda_object (lambda_expr);
7552 /* Parse the beginning of a lambda expression.
7555 [ lambda-capture [opt] ]
7557 LAMBDA_EXPR is the current representation of the lambda expression. */
7560 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7562 /* Need commas after the first capture. */
7565 /* Eat the leading `['. */
7566 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7568 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7569 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7570 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7571 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7572 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7573 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7575 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7577 cp_lexer_consume_token (parser->lexer);
7581 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7583 cp_token* capture_token;
7585 tree capture_init_expr;
7586 cp_id_kind idk = CP_ID_KIND_NONE;
7587 bool explicit_init_p = false;
7589 enum capture_kind_type
7594 enum capture_kind_type capture_kind = BY_COPY;
7596 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7598 error ("expected end of capture-list");
7605 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7607 /* Possibly capture `this'. */
7608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7610 cp_lexer_consume_token (parser->lexer);
7611 add_capture (lambda_expr,
7612 /*id=*/get_identifier ("__this"),
7613 /*initializer=*/finish_this_expr(),
7614 /*by_reference_p=*/false,
7619 /* Remember whether we want to capture as a reference or not. */
7620 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7622 capture_kind = BY_REFERENCE;
7623 cp_lexer_consume_token (parser->lexer);
7626 /* Get the identifier. */
7627 capture_token = cp_lexer_peek_token (parser->lexer);
7628 capture_id = cp_parser_identifier (parser);
7630 if (capture_id == error_mark_node)
7631 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7632 delimiters, but I modified this to stop on unnested ']' as well. It
7633 was already changed to stop on unnested '}', so the
7634 "closing_parenthesis" name is no more misleading with my change. */
7636 cp_parser_skip_to_closing_parenthesis (parser,
7637 /*recovering=*/true,
7639 /*consume_paren=*/true);
7643 /* Find the initializer for this capture. */
7644 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7646 /* An explicit expression exists. */
7647 cp_lexer_consume_token (parser->lexer);
7648 pedwarn (input_location, OPT_pedantic,
7649 "ISO C++ does not allow initializers "
7650 "in lambda expression capture lists");
7651 capture_init_expr = cp_parser_assignment_expression (parser,
7654 explicit_init_p = true;
7658 const char* error_msg;
7660 /* Turn the identifier into an id-expression. */
7662 = cp_parser_lookup_name
7666 /*is_template=*/false,
7667 /*is_namespace=*/false,
7668 /*check_dependency=*/true,
7669 /*ambiguous_decls=*/NULL,
7670 capture_token->location);
7673 = finish_id_expression
7678 /*integral_constant_expression_p=*/false,
7679 /*allow_non_integral_constant_expression_p=*/false,
7680 /*non_integral_constant_expression_p=*/NULL,
7681 /*template_p=*/false,
7683 /*address_p=*/false,
7684 /*template_arg_p=*/false,
7686 capture_token->location);
7689 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7691 = unqualified_name_lookup_error (capture_init_expr);
7693 add_capture (lambda_expr,
7696 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7700 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7703 /* Parse the (optional) middle of a lambda expression.
7706 ( parameter-declaration-clause [opt] )
7707 attribute-specifier [opt]
7709 exception-specification [opt]
7710 lambda-return-type-clause [opt]
7712 LAMBDA_EXPR is the current representation of the lambda expression. */
7715 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7717 /* 5.1.1.4 of the standard says:
7718 If a lambda-expression does not include a lambda-declarator, it is as if
7719 the lambda-declarator were ().
7720 This means an empty parameter list, no attributes, and no exception
7722 tree param_list = void_list_node;
7723 tree attributes = NULL_TREE;
7724 tree exception_spec = NULL_TREE;
7727 /* The lambda-declarator is optional, but must begin with an opening
7728 parenthesis if present. */
7729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7731 cp_lexer_consume_token (parser->lexer);
7733 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7735 /* Parse parameters. */
7736 param_list = cp_parser_parameter_declaration_clause (parser);
7738 /* Default arguments shall not be specified in the
7739 parameter-declaration-clause of a lambda-declarator. */
7740 for (t = param_list; t; t = TREE_CHAIN (t))
7741 if (TREE_PURPOSE (t))
7742 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7743 "default argument specified for lambda parameter");
7745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7747 attributes = cp_parser_attributes_opt (parser);
7749 /* Parse optional `mutable' keyword. */
7750 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7752 cp_lexer_consume_token (parser->lexer);
7753 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7756 /* Parse optional exception specification. */
7757 exception_spec = cp_parser_exception_specification_opt (parser);
7759 /* Parse optional trailing return type. */
7760 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7762 cp_lexer_consume_token (parser->lexer);
7763 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7766 /* The function parameters must be in scope all the way until after the
7767 trailing-return-type in case of decltype. */
7768 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7769 pop_binding (DECL_NAME (t), t);
7774 /* Create the function call operator.
7776 Messing with declarators like this is no uglier than building up the
7777 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7780 cp_decl_specifier_seq return_type_specs;
7781 cp_declarator* declarator;
7786 clear_decl_specs (&return_type_specs);
7787 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7788 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7790 /* Maybe we will deduce the return type later, but we can use void
7791 as a placeholder return type anyways. */
7792 return_type_specs.type = void_type_node;
7794 p = obstack_alloc (&declarator_obstack, 0);
7796 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7799 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7800 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7801 declarator = make_call_declarator (declarator, param_list, quals,
7803 /*late_return_type=*/NULL_TREE);
7804 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7806 fco = grokmethod (&return_type_specs,
7809 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7810 DECL_ARTIFICIAL (fco) = 1;
7812 finish_member_declaration (fco);
7814 obstack_free (&declarator_obstack, p);
7818 /* Parse the body of a lambda expression, which is simply
7822 but which requires special handling.
7823 LAMBDA_EXPR is the current representation of the lambda expression. */
7826 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7828 bool nested = (current_function_decl != NULL_TREE);
7830 push_function_context ();
7832 /* Finish the function call operator
7834 + late_parsing_for_member
7835 + function_definition_after_declarator
7836 + ctor_initializer_opt_and_function_body */
7838 tree fco = lambda_function (lambda_expr);
7842 /* Let the front end know that we are going to be defining this
7844 start_preparsed_function (fco,
7846 SF_PRE_PARSED | SF_INCLASS_INLINE);
7848 start_lambda_scope (fco);
7849 body = begin_function_body ();
7851 /* 5.1.1.4 of the standard says:
7852 If a lambda-expression does not include a trailing-return-type, it
7853 is as if the trailing-return-type denotes the following type:
7854 * if the compound-statement is of the form
7855 { return attribute-specifier [opt] expression ; }
7856 the type of the returned expression after lvalue-to-rvalue
7857 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7858 (_conv.array_ 4.2), and function-to-pointer conversion
7860 * otherwise, void. */
7862 /* In a lambda that has neither a lambda-return-type-clause
7863 nor a deducible form, errors should be reported for return statements
7864 in the body. Since we used void as the placeholder return type, parsing
7865 the body as usual will give such desired behavior. */
7866 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7867 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7868 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7869 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7872 tree expr = NULL_TREE;
7873 cp_id_kind idk = CP_ID_KIND_NONE;
7875 /* Parse tentatively in case there's more after the initial return
7877 cp_parser_parse_tentatively (parser);
7879 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7880 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7882 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7885 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7887 if (cp_parser_parse_definitely (parser))
7889 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7891 compound_stmt = begin_compound_stmt (0);
7892 /* Will get error here if type not deduced yet. */
7893 finish_return_stmt (expr);
7894 finish_compound_stmt (compound_stmt);
7902 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7903 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7904 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7905 cp_parser_compound_stmt does not pass it. */
7906 cp_parser_function_body (parser);
7907 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7910 finish_function_body (body);
7911 finish_lambda_scope ();
7913 /* Finish the function and generate code for it if necessary. */
7914 expand_or_defer_fn (finish_function (/*inline*/2));
7918 pop_function_context();
7921 /* Statements [gram.stmt.stmt] */
7923 /* Parse a statement.
7927 expression-statement
7932 declaration-statement
7935 IN_COMPOUND is true when the statement is nested inside a
7936 cp_parser_compound_statement; this matters for certain pragmas.
7938 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7939 is a (possibly labeled) if statement which is not enclosed in braces
7940 and has an else clause. This is used to implement -Wparentheses. */
7943 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7944 bool in_compound, bool *if_p)
7948 location_t statement_location;
7953 /* There is no statement yet. */
7954 statement = NULL_TREE;
7955 /* Peek at the next token. */
7956 token = cp_lexer_peek_token (parser->lexer);
7957 /* Remember the location of the first token in the statement. */
7958 statement_location = token->location;
7959 /* If this is a keyword, then that will often determine what kind of
7960 statement we have. */
7961 if (token->type == CPP_KEYWORD)
7963 enum rid keyword = token->keyword;
7969 /* Looks like a labeled-statement with a case label.
7970 Parse the label, and then use tail recursion to parse
7972 cp_parser_label_for_labeled_statement (parser);
7977 statement = cp_parser_selection_statement (parser, if_p);
7983 statement = cp_parser_iteration_statement (parser);
7990 statement = cp_parser_jump_statement (parser);
7993 /* Objective-C++ exception-handling constructs. */
7996 case RID_AT_FINALLY:
7997 case RID_AT_SYNCHRONIZED:
7999 statement = cp_parser_objc_statement (parser);
8003 statement = cp_parser_try_block (parser);
8007 /* This must be a namespace alias definition. */
8008 cp_parser_declaration_statement (parser);
8012 /* It might be a keyword like `int' that can start a
8013 declaration-statement. */
8017 else if (token->type == CPP_NAME)
8019 /* If the next token is a `:', then we are looking at a
8020 labeled-statement. */
8021 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8022 if (token->type == CPP_COLON)
8024 /* Looks like a labeled-statement with an ordinary label.
8025 Parse the label, and then use tail recursion to parse
8027 cp_parser_label_for_labeled_statement (parser);
8031 /* Anything that starts with a `{' must be a compound-statement. */
8032 else if (token->type == CPP_OPEN_BRACE)
8033 statement = cp_parser_compound_statement (parser, NULL, false);
8034 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8035 a statement all its own. */
8036 else if (token->type == CPP_PRAGMA)
8038 /* Only certain OpenMP pragmas are attached to statements, and thus
8039 are considered statements themselves. All others are not. In
8040 the context of a compound, accept the pragma as a "statement" and
8041 return so that we can check for a close brace. Otherwise we
8042 require a real statement and must go back and read one. */
8044 cp_parser_pragma (parser, pragma_compound);
8045 else if (!cp_parser_pragma (parser, pragma_stmt))
8049 else if (token->type == CPP_EOF)
8051 cp_parser_error (parser, "expected statement");
8055 /* Everything else must be a declaration-statement or an
8056 expression-statement. Try for the declaration-statement
8057 first, unless we are looking at a `;', in which case we know that
8058 we have an expression-statement. */
8061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8063 cp_parser_parse_tentatively (parser);
8064 /* Try to parse the declaration-statement. */
8065 cp_parser_declaration_statement (parser);
8066 /* If that worked, we're done. */
8067 if (cp_parser_parse_definitely (parser))
8070 /* Look for an expression-statement instead. */
8071 statement = cp_parser_expression_statement (parser, in_statement_expr);
8074 /* Set the line number for the statement. */
8075 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8076 SET_EXPR_LOCATION (statement, statement_location);
8079 /* Parse the label for a labeled-statement, i.e.
8082 case constant-expression :
8086 case constant-expression ... constant-expression : statement
8088 When a label is parsed without errors, the label is added to the
8089 parse tree by the finish_* functions, so this function doesn't
8090 have to return the label. */
8093 cp_parser_label_for_labeled_statement (cp_parser* parser)
8096 tree label = NULL_TREE;
8098 /* The next token should be an identifier. */
8099 token = cp_lexer_peek_token (parser->lexer);
8100 if (token->type != CPP_NAME
8101 && token->type != CPP_KEYWORD)
8103 cp_parser_error (parser, "expected labeled-statement");
8107 switch (token->keyword)
8114 /* Consume the `case' token. */
8115 cp_lexer_consume_token (parser->lexer);
8116 /* Parse the constant-expression. */
8117 expr = cp_parser_constant_expression (parser,
8118 /*allow_non_constant_p=*/false,
8121 ellipsis = cp_lexer_peek_token (parser->lexer);
8122 if (ellipsis->type == CPP_ELLIPSIS)
8124 /* Consume the `...' token. */
8125 cp_lexer_consume_token (parser->lexer);
8127 cp_parser_constant_expression (parser,
8128 /*allow_non_constant_p=*/false,
8130 /* We don't need to emit warnings here, as the common code
8131 will do this for us. */
8134 expr_hi = NULL_TREE;
8136 if (parser->in_switch_statement_p)
8137 finish_case_label (token->location, expr, expr_hi);
8139 error_at (token->location,
8140 "case label %qE not within a switch statement",
8146 /* Consume the `default' token. */
8147 cp_lexer_consume_token (parser->lexer);
8149 if (parser->in_switch_statement_p)
8150 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8152 error_at (token->location, "case label not within a switch statement");
8156 /* Anything else must be an ordinary label. */
8157 label = finish_label_stmt (cp_parser_identifier (parser));
8161 /* Require the `:' token. */
8162 cp_parser_require (parser, CPP_COLON, RT_COLON);
8164 /* An ordinary label may optionally be followed by attributes.
8165 However, this is only permitted if the attributes are then
8166 followed by a semicolon. This is because, for backward
8167 compatibility, when parsing
8168 lab: __attribute__ ((unused)) int i;
8169 we want the attribute to attach to "i", not "lab". */
8170 if (label != NULL_TREE
8171 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8175 cp_parser_parse_tentatively (parser);
8176 attrs = cp_parser_attributes_opt (parser);
8177 if (attrs == NULL_TREE
8178 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8179 cp_parser_abort_tentative_parse (parser);
8180 else if (!cp_parser_parse_definitely (parser))
8183 cplus_decl_attributes (&label, attrs, 0);
8187 /* Parse an expression-statement.
8189 expression-statement:
8192 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194 indicates whether this expression-statement is part of an
8195 expression statement. */
8198 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8200 tree statement = NULL_TREE;
8201 cp_token *token = cp_lexer_peek_token (parser->lexer);
8203 /* If the next token is a ';', then there is no expression
8205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8206 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8208 /* Give a helpful message for "A<T>::type t;" and the like. */
8209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8212 if (TREE_CODE (statement) == SCOPE_REF)
8213 error_at (token->location, "need %<typename%> before %qE because "
8214 "%qT is a dependent scope",
8215 statement, TREE_OPERAND (statement, 0));
8216 else if (is_overloaded_fn (statement)
8217 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8220 tree fn = get_first_fn (statement);
8221 error_at (token->location,
8222 "%<%T::%D%> names the constructor, not the type",
8223 DECL_CONTEXT (fn), DECL_NAME (fn));
8227 /* Consume the final `;'. */
8228 cp_parser_consume_semicolon_at_end_of_statement (parser);
8230 if (in_statement_expr
8231 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8232 /* This is the final expression statement of a statement
8234 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8236 statement = finish_expr_stmt (statement);
8243 /* Parse a compound-statement.
8246 { statement-seq [opt] }
8251 { label-declaration-seq [opt] statement-seq [opt] }
8253 label-declaration-seq:
8255 label-declaration-seq label-declaration
8257 Returns a tree representing the statement. */
8260 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8265 /* Consume the `{'. */
8266 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8267 return error_mark_node;
8268 /* Begin the compound-statement. */
8269 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8270 /* If the next keyword is `__label__' we have a label declaration. */
8271 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8272 cp_parser_label_declaration (parser);
8273 /* Parse an (optional) statement-seq. */
8274 cp_parser_statement_seq_opt (parser, in_statement_expr);
8275 /* Finish the compound-statement. */
8276 finish_compound_stmt (compound_stmt);
8277 /* Consume the `}'. */
8278 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8280 return compound_stmt;
8283 /* Parse an (optional) statement-seq.
8287 statement-seq [opt] statement */
8290 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8292 /* Scan statements until there aren't any more. */
8295 cp_token *token = cp_lexer_peek_token (parser->lexer);
8297 /* If we're looking at a `}', then we've run out of statements. */
8298 if (token->type == CPP_CLOSE_BRACE
8299 || token->type == CPP_EOF
8300 || token->type == CPP_PRAGMA_EOL)
8303 /* If we are in a compound statement and find 'else' then
8304 something went wrong. */
8305 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8307 if (parser->in_statement & IN_IF_STMT)
8311 token = cp_lexer_consume_token (parser->lexer);
8312 error_at (token->location, "%<else%> without a previous %<if%>");
8316 /* Parse the statement. */
8317 cp_parser_statement (parser, in_statement_expr, true, NULL);
8321 /* Parse a selection-statement.
8323 selection-statement:
8324 if ( condition ) statement
8325 if ( condition ) statement else statement
8326 switch ( condition ) statement
8328 Returns the new IF_STMT or SWITCH_STMT.
8330 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8331 is a (possibly labeled) if statement which is not enclosed in
8332 braces and has an else clause. This is used to implement
8336 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8344 /* Peek at the next token. */
8345 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8347 /* See what kind of keyword it is. */
8348 keyword = token->keyword;
8357 /* Look for the `('. */
8358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8360 cp_parser_skip_to_end_of_statement (parser);
8361 return error_mark_node;
8364 /* Begin the selection-statement. */
8365 if (keyword == RID_IF)
8366 statement = begin_if_stmt ();
8368 statement = begin_switch_stmt ();
8370 /* Parse the condition. */
8371 condition = cp_parser_condition (parser);
8372 /* Look for the `)'. */
8373 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8374 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8375 /*consume_paren=*/true);
8377 if (keyword == RID_IF)
8380 unsigned char in_statement;
8382 /* Add the condition. */
8383 finish_if_stmt_cond (condition, statement);
8385 /* Parse the then-clause. */
8386 in_statement = parser->in_statement;
8387 parser->in_statement |= IN_IF_STMT;
8388 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8390 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8391 add_stmt (build_empty_stmt (loc));
8392 cp_lexer_consume_token (parser->lexer);
8393 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8394 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8395 "empty body in an %<if%> statement");
8399 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8400 parser->in_statement = in_statement;
8402 finish_then_clause (statement);
8404 /* If the next token is `else', parse the else-clause. */
8405 if (cp_lexer_next_token_is_keyword (parser->lexer,
8408 /* Consume the `else' keyword. */
8409 cp_lexer_consume_token (parser->lexer);
8410 begin_else_clause (statement);
8411 /* Parse the else-clause. */
8412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8415 loc = cp_lexer_peek_token (parser->lexer)->location;
8417 OPT_Wempty_body, "suggest braces around "
8418 "empty body in an %<else%> statement");
8419 add_stmt (build_empty_stmt (loc));
8420 cp_lexer_consume_token (parser->lexer);
8423 cp_parser_implicitly_scoped_statement (parser, NULL);
8425 finish_else_clause (statement);
8427 /* If we are currently parsing a then-clause, then
8428 IF_P will not be NULL. We set it to true to
8429 indicate that this if statement has an else clause.
8430 This may trigger the Wparentheses warning below
8431 when we get back up to the parent if statement. */
8437 /* This if statement does not have an else clause. If
8438 NESTED_IF is true, then the then-clause is an if
8439 statement which does have an else clause. We warn
8440 about the potential ambiguity. */
8442 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8443 "suggest explicit braces to avoid ambiguous"
8447 /* Now we're all done with the if-statement. */
8448 finish_if_stmt (statement);
8452 bool in_switch_statement_p;
8453 unsigned char in_statement;
8455 /* Add the condition. */
8456 finish_switch_cond (condition, statement);
8458 /* Parse the body of the switch-statement. */
8459 in_switch_statement_p = parser->in_switch_statement_p;
8460 in_statement = parser->in_statement;
8461 parser->in_switch_statement_p = true;
8462 parser->in_statement |= IN_SWITCH_STMT;
8463 cp_parser_implicitly_scoped_statement (parser, NULL);
8464 parser->in_switch_statement_p = in_switch_statement_p;
8465 parser->in_statement = in_statement;
8467 /* Now we're all done with the switch-statement. */
8468 finish_switch_stmt (statement);
8476 cp_parser_error (parser, "expected selection-statement");
8477 return error_mark_node;
8481 /* Parse a condition.
8485 type-specifier-seq declarator = initializer-clause
8486 type-specifier-seq declarator braced-init-list
8491 type-specifier-seq declarator asm-specification [opt]
8492 attributes [opt] = assignment-expression
8494 Returns the expression that should be tested. */
8497 cp_parser_condition (cp_parser* parser)
8499 cp_decl_specifier_seq type_specifiers;
8500 const char *saved_message;
8502 /* Try the declaration first. */
8503 cp_parser_parse_tentatively (parser);
8504 /* New types are not allowed in the type-specifier-seq for a
8506 saved_message = parser->type_definition_forbidden_message;
8507 parser->type_definition_forbidden_message
8508 = G_("types may not be defined in conditions");
8509 /* Parse the type-specifier-seq. */
8510 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8511 /*is_trailing_return=*/false,
8513 /* Restore the saved message. */
8514 parser->type_definition_forbidden_message = saved_message;
8515 /* If all is well, we might be looking at a declaration. */
8516 if (!cp_parser_error_occurred (parser))
8519 tree asm_specification;
8521 cp_declarator *declarator;
8522 tree initializer = NULL_TREE;
8524 /* Parse the declarator. */
8525 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8526 /*ctor_dtor_or_conv_p=*/NULL,
8527 /*parenthesized_p=*/NULL,
8528 /*member_p=*/false);
8529 /* Parse the attributes. */
8530 attributes = cp_parser_attributes_opt (parser);
8531 /* Parse the asm-specification. */
8532 asm_specification = cp_parser_asm_specification_opt (parser);
8533 /* If the next token is not an `=' or '{', then we might still be
8534 looking at an expression. For example:
8538 looks like a decl-specifier-seq and a declarator -- but then
8539 there is no `=', so this is an expression. */
8540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8541 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8542 cp_parser_simulate_error (parser);
8544 /* If we did see an `=' or '{', then we are looking at a declaration
8546 if (cp_parser_parse_definitely (parser))
8549 bool non_constant_p;
8550 bool flags = LOOKUP_ONLYCONVERTING;
8552 /* Create the declaration. */
8553 decl = start_decl (declarator, &type_specifiers,
8554 /*initialized_p=*/true,
8555 attributes, /*prefix_attributes=*/NULL_TREE,
8558 /* Parse the initializer. */
8559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8561 initializer = cp_parser_braced_list (parser, &non_constant_p);
8562 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8567 /* Consume the `='. */
8568 cp_parser_require (parser, CPP_EQ, RT_EQ);
8569 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8571 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8572 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8574 if (!non_constant_p)
8575 initializer = fold_non_dependent_expr (initializer);
8577 /* Process the initializer. */
8578 cp_finish_decl (decl,
8579 initializer, !non_constant_p,
8584 pop_scope (pushed_scope);
8586 return convert_from_reference (decl);
8589 /* If we didn't even get past the declarator successfully, we are
8590 definitely not looking at a declaration. */
8592 cp_parser_abort_tentative_parse (parser);
8594 /* Otherwise, we are looking at an expression. */
8595 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8598 /* Parses a traditional for-statement until the closing ')', not included. */
8601 cp_parser_c_for (cp_parser *parser)
8603 /* Normal for loop */
8605 tree condition = NULL_TREE;
8606 tree expression = NULL_TREE;
8608 /* Begin the for-statement. */
8609 stmt = begin_for_stmt ();
8611 /* Parse the initialization. */
8612 cp_parser_for_init_statement (parser);
8613 finish_for_init_stmt (stmt);
8615 /* If there's a condition, process it. */
8616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8617 condition = cp_parser_condition (parser);
8618 finish_for_cond (condition, stmt);
8619 /* Look for the `;'. */
8620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8622 /* If there's an expression, process it. */
8623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8624 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8625 finish_for_expr (expression, stmt);
8630 /* Tries to parse a range-based for-statement:
8633 type-specifier-seq declarator : expression
8635 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8636 expression. Note that the *DECL is returned unfinished, so
8637 later you should call cp_finish_decl().
8639 Returns TRUE iff a range-based for is parsed. */
8642 cp_parser_range_for (cp_parser *parser)
8644 tree stmt, range_decl, range_expr;
8645 cp_decl_specifier_seq type_specifiers;
8646 cp_declarator *declarator;
8647 const char *saved_message;
8648 tree attributes, pushed_scope;
8650 cp_parser_parse_tentatively (parser);
8651 /* New types are not allowed in the type-specifier-seq for a
8652 range-based for loop. */
8653 saved_message = parser->type_definition_forbidden_message;
8654 parser->type_definition_forbidden_message
8655 = G_("types may not be defined in range-based for loops");
8656 /* Parse the type-specifier-seq. */
8657 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8658 /*is_trailing_return=*/false,
8660 /* Restore the saved message. */
8661 parser->type_definition_forbidden_message = saved_message;
8662 /* If all is well, we might be looking at a declaration. */
8663 if (cp_parser_error_occurred (parser))
8665 cp_parser_abort_tentative_parse (parser);
8668 /* Parse the declarator. */
8669 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8670 /*ctor_dtor_or_conv_p=*/NULL,
8671 /*parenthesized_p=*/NULL,
8672 /*member_p=*/false);
8673 /* Parse the attributes. */
8674 attributes = cp_parser_attributes_opt (parser);
8675 /* The next token should be `:'. */
8676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8677 cp_parser_simulate_error (parser);
8679 /* Check if it is a range-based for */
8680 if (!cp_parser_parse_definitely (parser))
8683 cp_parser_require (parser, CPP_COLON, RT_COLON);
8684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8686 bool expr_non_constant_p;
8687 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8690 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8692 /* If in template, STMT is converted to a normal for-statements
8693 at instantiation. If not, it is done just ahead. */
8694 if (processing_template_decl)
8695 stmt = begin_range_for_stmt ();
8697 stmt = begin_for_stmt ();
8699 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8700 range_decl = start_decl (declarator, &type_specifiers,
8701 /*initialized_p=*/SD_INITIALIZED,
8702 attributes, /*prefix_attributes=*/NULL_TREE,
8704 /* No scope allowed here */
8705 pop_scope (pushed_scope);
8707 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8708 finish_range_for_decl (stmt, range_decl, range_expr);
8710 /* Convert the range-based for loop into a normal for-statement. */
8711 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8716 /* Converts a range-based for-statement into a normal
8717 for-statement, as per the definition.
8719 for (RANGE_DECL : RANGE_EXPR)
8722 should be equivalent to:
8725 auto &&__range = RANGE_EXPR;
8726 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8730 RANGE_DECL = *__begin;
8735 If RANGE_EXPR is an array:
8736 BEGIN_EXPR = __range
8737 END_EXPR = __range + ARRAY_SIZE(__range)
8739 BEGIN_EXPR = begin(__range)
8740 END_EXPR = end(__range);
8742 When calling begin()/end() we must use argument dependent
8743 lookup, but always considering 'std' as an associated namespace. */
8746 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8748 tree range_type, range_temp;
8750 tree iter_type, begin_expr, end_expr;
8751 tree condition, expression;
8753 /* Find out the type deduced by the declaration
8754 * `auto &&__range = range_expr' */
8755 range_type = cp_build_reference_type (make_auto (), true);
8756 range_type = do_auto_deduction (range_type, range_expr,
8757 type_uses_auto (range_type));
8759 /* Create the __range variable */
8760 range_temp = build_decl (input_location, VAR_DECL,
8761 get_identifier ("__for_range"), range_type);
8762 TREE_USED (range_temp) = 1;
8763 DECL_ARTIFICIAL (range_temp) = 1;
8764 pushdecl (range_temp);
8765 finish_expr_stmt (cp_build_modify_expr (range_temp, INIT_EXPR, range_expr,
8766 tf_warning_or_error));
8767 range_temp = convert_from_reference (range_temp);
8769 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8771 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8772 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8773 begin_expr = range_temp;
8775 = build_binary_op (input_location, PLUS_EXPR,
8777 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8781 /* If it is not an array, we must call begin(__range)/end__range() */
8784 begin_expr = get_identifier ("begin");
8785 vec = make_tree_vector ();
8786 VEC_safe_push (tree, gc, vec, range_temp);
8787 begin_expr = perform_koenig_lookup (begin_expr, vec,
8788 /*include_std=*/true);
8789 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8790 tf_warning_or_error);
8791 release_tree_vector (vec);
8793 end_expr = get_identifier ("end");
8794 vec = make_tree_vector ();
8795 VEC_safe_push (tree, gc, vec, range_temp);
8796 end_expr = perform_koenig_lookup (end_expr, vec,
8797 /*include_std=*/true);
8798 end_expr = finish_call_expr (end_expr, &vec, false, true,
8799 tf_warning_or_error);
8800 release_tree_vector (vec);
8802 /* The unqualified type of the __begin and __end temporaries should
8803 * be the same as required by the multiple auto declaration */
8804 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8805 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8806 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8807 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8810 /* The new for initialization statement */
8811 begin = build_decl (input_location, VAR_DECL,
8812 get_identifier ("__for_begin"), iter_type);
8813 TREE_USED (begin) = 1;
8814 DECL_ARTIFICIAL (begin) = 1;
8816 finish_expr_stmt (cp_build_modify_expr (begin, INIT_EXPR, begin_expr,
8817 tf_warning_or_error));
8818 end = build_decl (input_location, VAR_DECL,
8819 get_identifier ("__for_end"), iter_type);
8820 TREE_USED (end) = 1;
8821 DECL_ARTIFICIAL (end) = 1;
8824 finish_expr_stmt (cp_build_modify_expr (end, INIT_EXPR, end_expr,
8825 tf_warning_or_error));
8827 finish_for_init_stmt (statement);
8829 /* The new for condition */
8830 condition = build_x_binary_op (NE_EXPR,
8833 NULL, tf_warning_or_error);
8834 finish_for_cond (condition, statement);
8836 /* The new increment expression */
8837 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8838 finish_for_expr (expression, statement);
8840 /* The declaration is initialized with *__begin inside the loop body */
8841 cp_finish_decl (range_decl,
8842 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8843 /*is_constant_init*/false, NULL_TREE,
8844 LOOKUP_ONLYCONVERTING);
8850 /* Parse an iteration-statement.
8852 iteration-statement:
8853 while ( condition ) statement
8854 do statement while ( expression ) ;
8855 for ( for-init-statement condition [opt] ; expression [opt] )
8858 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8861 cp_parser_iteration_statement (cp_parser* parser)
8866 unsigned char in_statement;
8868 /* Peek at the next token. */
8869 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8871 return error_mark_node;
8873 /* Remember whether or not we are already within an iteration
8875 in_statement = parser->in_statement;
8877 /* See what kind of keyword it is. */
8878 keyword = token->keyword;
8885 /* Begin the while-statement. */
8886 statement = begin_while_stmt ();
8887 /* Look for the `('. */
8888 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8889 /* Parse the condition. */
8890 condition = cp_parser_condition (parser);
8891 finish_while_stmt_cond (condition, statement);
8892 /* Look for the `)'. */
8893 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8894 /* Parse the dependent statement. */
8895 parser->in_statement = IN_ITERATION_STMT;
8896 cp_parser_already_scoped_statement (parser);
8897 parser->in_statement = in_statement;
8898 /* We're done with the while-statement. */
8899 finish_while_stmt (statement);
8907 /* Begin the do-statement. */
8908 statement = begin_do_stmt ();
8909 /* Parse the body of the do-statement. */
8910 parser->in_statement = IN_ITERATION_STMT;
8911 cp_parser_implicitly_scoped_statement (parser, NULL);
8912 parser->in_statement = in_statement;
8913 finish_do_body (statement);
8914 /* Look for the `while' keyword. */
8915 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8916 /* Look for the `('. */
8917 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8918 /* Parse the expression. */
8919 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8920 /* We're done with the do-statement. */
8921 finish_do_stmt (expression, statement);
8922 /* Look for the `)'. */
8923 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8924 /* Look for the `;'. */
8925 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8931 /* Look for the `('. */
8932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8934 if (cxx_dialect == cxx0x)
8935 statement = cp_parser_range_for (parser);
8937 statement = NULL_TREE;
8938 if (statement == NULL_TREE)
8939 statement = cp_parser_c_for (parser);
8941 /* Look for the `)'. */
8942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8944 /* Parse the body of the for-statement. */
8945 parser->in_statement = IN_ITERATION_STMT;
8946 cp_parser_already_scoped_statement (parser);
8947 parser->in_statement = in_statement;
8949 /* We're done with the for-statement. */
8950 finish_for_stmt (statement);
8955 cp_parser_error (parser, "expected iteration-statement");
8956 statement = error_mark_node;
8963 /* Parse a for-init-statement.
8966 expression-statement
8967 simple-declaration */
8970 cp_parser_for_init_statement (cp_parser* parser)
8972 /* If the next token is a `;', then we have an empty
8973 expression-statement. Grammatically, this is also a
8974 simple-declaration, but an invalid one, because it does not
8975 declare anything. Therefore, if we did not handle this case
8976 specially, we would issue an error message about an invalid
8978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8980 /* We're going to speculatively look for a declaration, falling back
8981 to an expression, if necessary. */
8982 cp_parser_parse_tentatively (parser);
8983 /* Parse the declaration. */
8984 cp_parser_simple_declaration (parser,
8985 /*function_definition_allowed_p=*/false);
8986 /* If the tentative parse failed, then we shall need to look for an
8987 expression-statement. */
8988 if (cp_parser_parse_definitely (parser))
8992 cp_parser_expression_statement (parser, NULL_TREE);
8995 /* Parse a jump-statement.
9000 return expression [opt] ;
9001 return braced-init-list ;
9009 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9012 cp_parser_jump_statement (cp_parser* parser)
9014 tree statement = error_mark_node;
9017 unsigned char in_statement;
9019 /* Peek at the next token. */
9020 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9022 return error_mark_node;
9024 /* See what kind of keyword it is. */
9025 keyword = token->keyword;
9029 in_statement = parser->in_statement & ~IN_IF_STMT;
9030 switch (in_statement)
9033 error_at (token->location, "break statement not within loop or switch");
9036 gcc_assert ((in_statement & IN_SWITCH_STMT)
9037 || in_statement == IN_ITERATION_STMT);
9038 statement = finish_break_stmt ();
9041 error_at (token->location, "invalid exit from OpenMP structured block");
9044 error_at (token->location, "break statement used with OpenMP for loop");
9047 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9051 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9054 error_at (token->location, "continue statement not within a loop");
9056 case IN_ITERATION_STMT:
9058 statement = finish_continue_stmt ();
9061 error_at (token->location, "invalid exit from OpenMP structured block");
9066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9072 bool expr_non_constant_p;
9074 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9076 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9077 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9079 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9080 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9082 /* If the next token is a `;', then there is no
9085 /* Build the return-statement. */
9086 statement = finish_return_stmt (expr);
9087 /* Look for the final `;'. */
9088 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9093 /* Create the goto-statement. */
9094 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9096 /* Issue a warning about this use of a GNU extension. */
9097 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9098 /* Consume the '*' token. */
9099 cp_lexer_consume_token (parser->lexer);
9100 /* Parse the dependent expression. */
9101 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9104 finish_goto_stmt (cp_parser_identifier (parser));
9105 /* Look for the final `;'. */
9106 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9110 cp_parser_error (parser, "expected jump-statement");
9117 /* Parse a declaration-statement.
9119 declaration-statement:
9120 block-declaration */
9123 cp_parser_declaration_statement (cp_parser* parser)
9127 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9128 p = obstack_alloc (&declarator_obstack, 0);
9130 /* Parse the block-declaration. */
9131 cp_parser_block_declaration (parser, /*statement_p=*/true);
9133 /* Free any declarators allocated. */
9134 obstack_free (&declarator_obstack, p);
9136 /* Finish off the statement. */
9140 /* Some dependent statements (like `if (cond) statement'), are
9141 implicitly in their own scope. In other words, if the statement is
9142 a single statement (as opposed to a compound-statement), it is
9143 none-the-less treated as if it were enclosed in braces. Any
9144 declarations appearing in the dependent statement are out of scope
9145 after control passes that point. This function parses a statement,
9146 but ensures that is in its own scope, even if it is not a
9149 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9150 is a (possibly labeled) if statement which is not enclosed in
9151 braces and has an else clause. This is used to implement
9154 Returns the new statement. */
9157 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9164 /* Mark if () ; with a special NOP_EXPR. */
9165 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9167 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9168 cp_lexer_consume_token (parser->lexer);
9169 statement = add_stmt (build_empty_stmt (loc));
9171 /* if a compound is opened, we simply parse the statement directly. */
9172 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9173 statement = cp_parser_compound_statement (parser, NULL, false);
9174 /* If the token is not a `{', then we must take special action. */
9177 /* Create a compound-statement. */
9178 statement = begin_compound_stmt (0);
9179 /* Parse the dependent-statement. */
9180 cp_parser_statement (parser, NULL_TREE, false, if_p);
9181 /* Finish the dummy compound-statement. */
9182 finish_compound_stmt (statement);
9185 /* Return the statement. */
9189 /* For some dependent statements (like `while (cond) statement'), we
9190 have already created a scope. Therefore, even if the dependent
9191 statement is a compound-statement, we do not want to create another
9195 cp_parser_already_scoped_statement (cp_parser* parser)
9197 /* If the token is a `{', then we must take special action. */
9198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9199 cp_parser_statement (parser, NULL_TREE, false, NULL);
9202 /* Avoid calling cp_parser_compound_statement, so that we
9203 don't create a new scope. Do everything else by hand. */
9204 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9205 /* If the next keyword is `__label__' we have a label declaration. */
9206 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9207 cp_parser_label_declaration (parser);
9208 /* Parse an (optional) statement-seq. */
9209 cp_parser_statement_seq_opt (parser, NULL_TREE);
9210 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9214 /* Declarations [gram.dcl.dcl] */
9216 /* Parse an optional declaration-sequence.
9220 declaration-seq declaration */
9223 cp_parser_declaration_seq_opt (cp_parser* parser)
9229 token = cp_lexer_peek_token (parser->lexer);
9231 if (token->type == CPP_CLOSE_BRACE
9232 || token->type == CPP_EOF
9233 || token->type == CPP_PRAGMA_EOL)
9236 if (token->type == CPP_SEMICOLON)
9238 /* A declaration consisting of a single semicolon is
9239 invalid. Allow it unless we're being pedantic. */
9240 cp_lexer_consume_token (parser->lexer);
9241 if (!in_system_header)
9242 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9246 /* If we're entering or exiting a region that's implicitly
9247 extern "C", modify the lang context appropriately. */
9248 if (!parser->implicit_extern_c && token->implicit_extern_c)
9250 push_lang_context (lang_name_c);
9251 parser->implicit_extern_c = true;
9253 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9255 pop_lang_context ();
9256 parser->implicit_extern_c = false;
9259 if (token->type == CPP_PRAGMA)
9261 /* A top-level declaration can consist solely of a #pragma.
9262 A nested declaration cannot, so this is done here and not
9263 in cp_parser_declaration. (A #pragma at block scope is
9264 handled in cp_parser_statement.) */
9265 cp_parser_pragma (parser, pragma_external);
9269 /* Parse the declaration itself. */
9270 cp_parser_declaration (parser);
9274 /* Parse a declaration.
9279 template-declaration
9280 explicit-instantiation
9281 explicit-specialization
9282 linkage-specification
9283 namespace-definition
9288 __extension__ declaration */
9291 cp_parser_declaration (cp_parser* parser)
9297 tree attributes = NULL_TREE;
9299 /* Check for the `__extension__' keyword. */
9300 if (cp_parser_extension_opt (parser, &saved_pedantic))
9302 /* Parse the qualified declaration. */
9303 cp_parser_declaration (parser);
9304 /* Restore the PEDANTIC flag. */
9305 pedantic = saved_pedantic;
9310 /* Try to figure out what kind of declaration is present. */
9311 token1 = *cp_lexer_peek_token (parser->lexer);
9313 if (token1.type != CPP_EOF)
9314 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9317 token2.type = CPP_EOF;
9318 token2.keyword = RID_MAX;
9321 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9322 p = obstack_alloc (&declarator_obstack, 0);
9324 /* If the next token is `extern' and the following token is a string
9325 literal, then we have a linkage specification. */
9326 if (token1.keyword == RID_EXTERN
9327 && cp_parser_is_string_literal (&token2))
9328 cp_parser_linkage_specification (parser);
9329 /* If the next token is `template', then we have either a template
9330 declaration, an explicit instantiation, or an explicit
9332 else if (token1.keyword == RID_TEMPLATE)
9334 /* `template <>' indicates a template specialization. */
9335 if (token2.type == CPP_LESS
9336 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9337 cp_parser_explicit_specialization (parser);
9338 /* `template <' indicates a template declaration. */
9339 else if (token2.type == CPP_LESS)
9340 cp_parser_template_declaration (parser, /*member_p=*/false);
9341 /* Anything else must be an explicit instantiation. */
9343 cp_parser_explicit_instantiation (parser);
9345 /* If the next token is `export', then we have a template
9347 else if (token1.keyword == RID_EXPORT)
9348 cp_parser_template_declaration (parser, /*member_p=*/false);
9349 /* If the next token is `extern', 'static' or 'inline' and the one
9350 after that is `template', we have a GNU extended explicit
9351 instantiation directive. */
9352 else if (cp_parser_allow_gnu_extensions_p (parser)
9353 && (token1.keyword == RID_EXTERN
9354 || token1.keyword == RID_STATIC
9355 || token1.keyword == RID_INLINE)
9356 && token2.keyword == RID_TEMPLATE)
9357 cp_parser_explicit_instantiation (parser);
9358 /* If the next token is `namespace', check for a named or unnamed
9359 namespace definition. */
9360 else if (token1.keyword == RID_NAMESPACE
9361 && (/* A named namespace definition. */
9362 (token2.type == CPP_NAME
9363 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9365 /* An unnamed namespace definition. */
9366 || token2.type == CPP_OPEN_BRACE
9367 || token2.keyword == RID_ATTRIBUTE))
9368 cp_parser_namespace_definition (parser);
9369 /* An inline (associated) namespace definition. */
9370 else if (token1.keyword == RID_INLINE
9371 && token2.keyword == RID_NAMESPACE)
9372 cp_parser_namespace_definition (parser);
9373 /* Objective-C++ declaration/definition. */
9374 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9375 cp_parser_objc_declaration (parser, NULL_TREE);
9376 else if (c_dialect_objc ()
9377 && token1.keyword == RID_ATTRIBUTE
9378 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9379 cp_parser_objc_declaration (parser, attributes);
9380 /* We must have either a block declaration or a function
9383 /* Try to parse a block-declaration, or a function-definition. */
9384 cp_parser_block_declaration (parser, /*statement_p=*/false);
9386 /* Free any declarators allocated. */
9387 obstack_free (&declarator_obstack, p);
9390 /* Parse a block-declaration.
9395 namespace-alias-definition
9402 __extension__ block-declaration
9407 static_assert-declaration
9409 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9410 part of a declaration-statement. */
9413 cp_parser_block_declaration (cp_parser *parser,
9419 /* Check for the `__extension__' keyword. */
9420 if (cp_parser_extension_opt (parser, &saved_pedantic))
9422 /* Parse the qualified declaration. */
9423 cp_parser_block_declaration (parser, statement_p);
9424 /* Restore the PEDANTIC flag. */
9425 pedantic = saved_pedantic;
9430 /* Peek at the next token to figure out which kind of declaration is
9432 token1 = cp_lexer_peek_token (parser->lexer);
9434 /* If the next keyword is `asm', we have an asm-definition. */
9435 if (token1->keyword == RID_ASM)
9438 cp_parser_commit_to_tentative_parse (parser);
9439 cp_parser_asm_definition (parser);
9441 /* If the next keyword is `namespace', we have a
9442 namespace-alias-definition. */
9443 else if (token1->keyword == RID_NAMESPACE)
9444 cp_parser_namespace_alias_definition (parser);
9445 /* If the next keyword is `using', we have either a
9446 using-declaration or a using-directive. */
9447 else if (token1->keyword == RID_USING)
9452 cp_parser_commit_to_tentative_parse (parser);
9453 /* If the token after `using' is `namespace', then we have a
9455 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9456 if (token2->keyword == RID_NAMESPACE)
9457 cp_parser_using_directive (parser);
9458 /* Otherwise, it's a using-declaration. */
9460 cp_parser_using_declaration (parser,
9461 /*access_declaration_p=*/false);
9463 /* If the next keyword is `__label__' we have a misplaced label
9465 else if (token1->keyword == RID_LABEL)
9467 cp_lexer_consume_token (parser->lexer);
9468 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9469 cp_parser_skip_to_end_of_statement (parser);
9470 /* If the next token is now a `;', consume it. */
9471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9472 cp_lexer_consume_token (parser->lexer);
9474 /* If the next token is `static_assert' we have a static assertion. */
9475 else if (token1->keyword == RID_STATIC_ASSERT)
9476 cp_parser_static_assert (parser, /*member_p=*/false);
9477 /* Anything else must be a simple-declaration. */
9479 cp_parser_simple_declaration (parser, !statement_p);
9482 /* Parse a simple-declaration.
9485 decl-specifier-seq [opt] init-declarator-list [opt] ;
9487 init-declarator-list:
9489 init-declarator-list , init-declarator
9491 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9492 function-definition as a simple-declaration. */
9495 cp_parser_simple_declaration (cp_parser* parser,
9496 bool function_definition_allowed_p)
9498 cp_decl_specifier_seq decl_specifiers;
9499 int declares_class_or_enum;
9500 bool saw_declarator;
9502 /* Defer access checks until we know what is being declared; the
9503 checks for names appearing in the decl-specifier-seq should be
9504 done as if we were in the scope of the thing being declared. */
9505 push_deferring_access_checks (dk_deferred);
9507 /* Parse the decl-specifier-seq. We have to keep track of whether
9508 or not the decl-specifier-seq declares a named class or
9509 enumeration type, since that is the only case in which the
9510 init-declarator-list is allowed to be empty.
9514 In a simple-declaration, the optional init-declarator-list can be
9515 omitted only when declaring a class or enumeration, that is when
9516 the decl-specifier-seq contains either a class-specifier, an
9517 elaborated-type-specifier, or an enum-specifier. */
9518 cp_parser_decl_specifier_seq (parser,
9519 CP_PARSER_FLAGS_OPTIONAL,
9521 &declares_class_or_enum);
9522 /* We no longer need to defer access checks. */
9523 stop_deferring_access_checks ();
9525 /* In a block scope, a valid declaration must always have a
9526 decl-specifier-seq. By not trying to parse declarators, we can
9527 resolve the declaration/expression ambiguity more quickly. */
9528 if (!function_definition_allowed_p
9529 && !decl_specifiers.any_specifiers_p)
9531 cp_parser_error (parser, "expected declaration");
9535 /* If the next two tokens are both identifiers, the code is
9536 erroneous. The usual cause of this situation is code like:
9540 where "T" should name a type -- but does not. */
9541 if (!decl_specifiers.any_type_specifiers_p
9542 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9544 /* If parsing tentatively, we should commit; we really are
9545 looking at a declaration. */
9546 cp_parser_commit_to_tentative_parse (parser);
9551 /* If we have seen at least one decl-specifier, and the next token
9552 is not a parenthesis, then we must be looking at a declaration.
9553 (After "int (" we might be looking at a functional cast.) */
9554 if (decl_specifiers.any_specifiers_p
9555 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9556 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9557 && !cp_parser_error_occurred (parser))
9558 cp_parser_commit_to_tentative_parse (parser);
9560 /* Keep going until we hit the `;' at the end of the simple
9562 saw_declarator = false;
9563 while (cp_lexer_next_token_is_not (parser->lexer,
9567 bool function_definition_p;
9572 /* If we are processing next declarator, coma is expected */
9573 token = cp_lexer_peek_token (parser->lexer);
9574 gcc_assert (token->type == CPP_COMMA);
9575 cp_lexer_consume_token (parser->lexer);
9578 saw_declarator = true;
9580 /* Parse the init-declarator. */
9581 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9583 function_definition_allowed_p,
9585 declares_class_or_enum,
9586 &function_definition_p);
9587 /* If an error occurred while parsing tentatively, exit quickly.
9588 (That usually happens when in the body of a function; each
9589 statement is treated as a declaration-statement until proven
9591 if (cp_parser_error_occurred (parser))
9593 /* Handle function definitions specially. */
9594 if (function_definition_p)
9596 /* If the next token is a `,', then we are probably
9597 processing something like:
9601 which is erroneous. */
9602 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9604 cp_token *token = cp_lexer_peek_token (parser->lexer);
9605 error_at (token->location,
9607 " declarations and function-definitions is forbidden");
9609 /* Otherwise, we're done with the list of declarators. */
9612 pop_deferring_access_checks ();
9616 /* The next token should be either a `,' or a `;'. */
9617 token = cp_lexer_peek_token (parser->lexer);
9618 /* If it's a `,', there are more declarators to come. */
9619 if (token->type == CPP_COMMA)
9620 /* will be consumed next time around */;
9621 /* If it's a `;', we are done. */
9622 else if (token->type == CPP_SEMICOLON)
9624 /* Anything else is an error. */
9627 /* If we have already issued an error message we don't need
9628 to issue another one. */
9629 if (decl != error_mark_node
9630 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9631 cp_parser_error (parser, "expected %<,%> or %<;%>");
9632 /* Skip tokens until we reach the end of the statement. */
9633 cp_parser_skip_to_end_of_statement (parser);
9634 /* If the next token is now a `;', consume it. */
9635 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9636 cp_lexer_consume_token (parser->lexer);
9639 /* After the first time around, a function-definition is not
9640 allowed -- even if it was OK at first. For example:
9645 function_definition_allowed_p = false;
9648 /* Issue an error message if no declarators are present, and the
9649 decl-specifier-seq does not itself declare a class or
9651 if (!saw_declarator)
9653 if (cp_parser_declares_only_class_p (parser))
9654 shadow_tag (&decl_specifiers);
9655 /* Perform any deferred access checks. */
9656 perform_deferred_access_checks ();
9659 /* Consume the `;'. */
9660 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9663 pop_deferring_access_checks ();
9666 /* Parse a decl-specifier-seq.
9669 decl-specifier-seq [opt] decl-specifier
9672 storage-class-specifier
9683 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9685 The parser flags FLAGS is used to control type-specifier parsing.
9687 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9690 1: one of the decl-specifiers is an elaborated-type-specifier
9691 (i.e., a type declaration)
9692 2: one of the decl-specifiers is an enum-specifier or a
9693 class-specifier (i.e., a type definition)
9698 cp_parser_decl_specifier_seq (cp_parser* parser,
9699 cp_parser_flags flags,
9700 cp_decl_specifier_seq *decl_specs,
9701 int* declares_class_or_enum)
9703 bool constructor_possible_p = !parser->in_declarator_p;
9704 cp_token *start_token = NULL;
9706 /* Clear DECL_SPECS. */
9707 clear_decl_specs (decl_specs);
9709 /* Assume no class or enumeration type is declared. */
9710 *declares_class_or_enum = 0;
9712 /* Keep reading specifiers until there are no more to read. */
9716 bool found_decl_spec;
9719 /* Peek at the next token. */
9720 token = cp_lexer_peek_token (parser->lexer);
9722 /* Save the first token of the decl spec list for error
9725 start_token = token;
9726 /* Handle attributes. */
9727 if (token->keyword == RID_ATTRIBUTE)
9729 /* Parse the attributes. */
9730 decl_specs->attributes
9731 = chainon (decl_specs->attributes,
9732 cp_parser_attributes_opt (parser));
9735 /* Assume we will find a decl-specifier keyword. */
9736 found_decl_spec = true;
9737 /* If the next token is an appropriate keyword, we can simply
9738 add it to the list. */
9739 switch (token->keyword)
9745 if (!at_class_scope_p ())
9747 error_at (token->location, "%<friend%> used outside of class");
9748 cp_lexer_purge_token (parser->lexer);
9752 ++decl_specs->specs[(int) ds_friend];
9753 /* Consume the token. */
9754 cp_lexer_consume_token (parser->lexer);
9759 ++decl_specs->specs[(int) ds_constexpr];
9760 cp_lexer_consume_token (parser->lexer);
9763 /* function-specifier:
9770 cp_parser_function_specifier_opt (parser, decl_specs);
9776 ++decl_specs->specs[(int) ds_typedef];
9777 /* Consume the token. */
9778 cp_lexer_consume_token (parser->lexer);
9779 /* A constructor declarator cannot appear in a typedef. */
9780 constructor_possible_p = false;
9781 /* The "typedef" keyword can only occur in a declaration; we
9782 may as well commit at this point. */
9783 cp_parser_commit_to_tentative_parse (parser);
9785 if (decl_specs->storage_class != sc_none)
9786 decl_specs->conflicting_specifiers_p = true;
9789 /* storage-class-specifier:
9799 if (cxx_dialect == cxx98)
9801 /* Consume the token. */
9802 cp_lexer_consume_token (parser->lexer);
9804 /* Complain about `auto' as a storage specifier, if
9805 we're complaining about C++0x compatibility. */
9806 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9807 " will change meaning in C++0x; please remove it");
9809 /* Set the storage class anyway. */
9810 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9814 /* C++0x auto type-specifier. */
9815 found_decl_spec = false;
9822 /* Consume the token. */
9823 cp_lexer_consume_token (parser->lexer);
9824 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9828 /* Consume the token. */
9829 cp_lexer_consume_token (parser->lexer);
9830 ++decl_specs->specs[(int) ds_thread];
9834 /* We did not yet find a decl-specifier yet. */
9835 found_decl_spec = false;
9839 /* Constructors are a special case. The `S' in `S()' is not a
9840 decl-specifier; it is the beginning of the declarator. */
9843 && constructor_possible_p
9844 && (cp_parser_constructor_declarator_p
9845 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9847 /* If we don't have a DECL_SPEC yet, then we must be looking at
9848 a type-specifier. */
9849 if (!found_decl_spec && !constructor_p)
9851 int decl_spec_declares_class_or_enum;
9852 bool is_cv_qualifier;
9856 = cp_parser_type_specifier (parser, flags,
9858 /*is_declaration=*/true,
9859 &decl_spec_declares_class_or_enum,
9861 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9863 /* If this type-specifier referenced a user-defined type
9864 (a typedef, class-name, etc.), then we can't allow any
9865 more such type-specifiers henceforth.
9869 The longest sequence of decl-specifiers that could
9870 possibly be a type name is taken as the
9871 decl-specifier-seq of a declaration. The sequence shall
9872 be self-consistent as described below.
9876 As a general rule, at most one type-specifier is allowed
9877 in the complete decl-specifier-seq of a declaration. The
9878 only exceptions are the following:
9880 -- const or volatile can be combined with any other
9883 -- signed or unsigned can be combined with char, long,
9891 void g (const int Pc);
9893 Here, Pc is *not* part of the decl-specifier seq; it's
9894 the declarator. Therefore, once we see a type-specifier
9895 (other than a cv-qualifier), we forbid any additional
9896 user-defined types. We *do* still allow things like `int
9897 int' to be considered a decl-specifier-seq, and issue the
9898 error message later. */
9899 if (type_spec && !is_cv_qualifier)
9900 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9901 /* A constructor declarator cannot follow a type-specifier. */
9904 constructor_possible_p = false;
9905 found_decl_spec = true;
9906 if (!is_cv_qualifier)
9907 decl_specs->any_type_specifiers_p = true;
9911 /* If we still do not have a DECL_SPEC, then there are no more
9913 if (!found_decl_spec)
9916 decl_specs->any_specifiers_p = true;
9917 /* After we see one decl-specifier, further decl-specifiers are
9919 flags |= CP_PARSER_FLAGS_OPTIONAL;
9922 cp_parser_check_decl_spec (decl_specs, start_token->location);
9924 /* Don't allow a friend specifier with a class definition. */
9925 if (decl_specs->specs[(int) ds_friend] != 0
9926 && (*declares_class_or_enum & 2))
9927 error_at (start_token->location,
9928 "class definition may not be declared a friend");
9931 /* Parse an (optional) storage-class-specifier.
9933 storage-class-specifier:
9942 storage-class-specifier:
9945 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9948 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9950 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9953 if (cxx_dialect != cxx98)
9955 /* Fall through for C++98. */
9962 /* Consume the token. */
9963 return cp_lexer_consume_token (parser->lexer)->u.value;
9970 /* Parse an (optional) function-specifier.
9977 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9978 Updates DECL_SPECS, if it is non-NULL. */
9981 cp_parser_function_specifier_opt (cp_parser* parser,
9982 cp_decl_specifier_seq *decl_specs)
9984 cp_token *token = cp_lexer_peek_token (parser->lexer);
9985 switch (token->keyword)
9989 ++decl_specs->specs[(int) ds_inline];
9993 /* 14.5.2.3 [temp.mem]
9995 A member function template shall not be virtual. */
9996 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9997 error_at (token->location, "templates may not be %<virtual%>");
9998 else if (decl_specs)
9999 ++decl_specs->specs[(int) ds_virtual];
10004 ++decl_specs->specs[(int) ds_explicit];
10011 /* Consume the token. */
10012 return cp_lexer_consume_token (parser->lexer)->u.value;
10015 /* Parse a linkage-specification.
10017 linkage-specification:
10018 extern string-literal { declaration-seq [opt] }
10019 extern string-literal declaration */
10022 cp_parser_linkage_specification (cp_parser* parser)
10026 /* Look for the `extern' keyword. */
10027 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10029 /* Look for the string-literal. */
10030 linkage = cp_parser_string_literal (parser, false, false);
10032 /* Transform the literal into an identifier. If the literal is a
10033 wide-character string, or contains embedded NULs, then we can't
10034 handle it as the user wants. */
10035 if (strlen (TREE_STRING_POINTER (linkage))
10036 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10038 cp_parser_error (parser, "invalid linkage-specification");
10039 /* Assume C++ linkage. */
10040 linkage = lang_name_cplusplus;
10043 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10045 /* We're now using the new linkage. */
10046 push_lang_context (linkage);
10048 /* If the next token is a `{', then we're using the first
10050 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10052 /* Consume the `{' token. */
10053 cp_lexer_consume_token (parser->lexer);
10054 /* Parse the declarations. */
10055 cp_parser_declaration_seq_opt (parser);
10056 /* Look for the closing `}'. */
10057 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10059 /* Otherwise, there's just one declaration. */
10062 bool saved_in_unbraced_linkage_specification_p;
10064 saved_in_unbraced_linkage_specification_p
10065 = parser->in_unbraced_linkage_specification_p;
10066 parser->in_unbraced_linkage_specification_p = true;
10067 cp_parser_declaration (parser);
10068 parser->in_unbraced_linkage_specification_p
10069 = saved_in_unbraced_linkage_specification_p;
10072 /* We're done with the linkage-specification. */
10073 pop_lang_context ();
10076 /* Parse a static_assert-declaration.
10078 static_assert-declaration:
10079 static_assert ( constant-expression , string-literal ) ;
10081 If MEMBER_P, this static_assert is a class member. */
10084 cp_parser_static_assert(cp_parser *parser, bool member_p)
10089 location_t saved_loc;
10091 /* Peek at the `static_assert' token so we can keep track of exactly
10092 where the static assertion started. */
10093 token = cp_lexer_peek_token (parser->lexer);
10094 saved_loc = token->location;
10096 /* Look for the `static_assert' keyword. */
10097 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10101 /* We know we are in a static assertion; commit to any tentative
10103 if (cp_parser_parsing_tentatively (parser))
10104 cp_parser_commit_to_tentative_parse (parser);
10106 /* Parse the `(' starting the static assertion condition. */
10107 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10109 /* Parse the constant-expression. */
10111 cp_parser_constant_expression (parser,
10112 /*allow_non_constant_p=*/false,
10113 /*non_constant_p=*/NULL);
10115 /* Parse the separating `,'. */
10116 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10118 /* Parse the string-literal message. */
10119 message = cp_parser_string_literal (parser,
10120 /*translate=*/false,
10123 /* A `)' completes the static assertion. */
10124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10125 cp_parser_skip_to_closing_parenthesis (parser,
10126 /*recovering=*/true,
10127 /*or_comma=*/false,
10128 /*consume_paren=*/true);
10130 /* A semicolon terminates the declaration. */
10131 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10133 /* Complete the static assertion, which may mean either processing
10134 the static assert now or saving it for template instantiation. */
10135 finish_static_assert (condition, message, saved_loc, member_p);
10138 /* Parse a `decltype' type. Returns the type.
10140 simple-type-specifier:
10141 decltype ( expression ) */
10144 cp_parser_decltype (cp_parser *parser)
10147 bool id_expression_or_member_access_p = false;
10148 const char *saved_message;
10149 bool saved_integral_constant_expression_p;
10150 bool saved_non_integral_constant_expression_p;
10151 cp_token *id_expr_start_token;
10153 /* Look for the `decltype' token. */
10154 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10155 return error_mark_node;
10157 /* Types cannot be defined in a `decltype' expression. Save away the
10159 saved_message = parser->type_definition_forbidden_message;
10161 /* And create the new one. */
10162 parser->type_definition_forbidden_message
10163 = G_("types may not be defined in %<decltype%> expressions");
10165 /* The restrictions on constant-expressions do not apply inside
10166 decltype expressions. */
10167 saved_integral_constant_expression_p
10168 = parser->integral_constant_expression_p;
10169 saved_non_integral_constant_expression_p
10170 = parser->non_integral_constant_expression_p;
10171 parser->integral_constant_expression_p = false;
10173 /* Do not actually evaluate the expression. */
10174 ++cp_unevaluated_operand;
10176 /* Do not warn about problems with the expression. */
10177 ++c_inhibit_evaluation_warnings;
10179 /* Parse the opening `('. */
10180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10181 return error_mark_node;
10183 /* First, try parsing an id-expression. */
10184 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10185 cp_parser_parse_tentatively (parser);
10186 expr = cp_parser_id_expression (parser,
10187 /*template_keyword_p=*/false,
10188 /*check_dependency_p=*/true,
10189 /*template_p=*/NULL,
10190 /*declarator_p=*/false,
10191 /*optional_p=*/false);
10193 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10195 bool non_integral_constant_expression_p = false;
10196 tree id_expression = expr;
10198 const char *error_msg;
10200 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10201 /* Lookup the name we got back from the id-expression. */
10202 expr = cp_parser_lookup_name (parser, expr,
10204 /*is_template=*/false,
10205 /*is_namespace=*/false,
10206 /*check_dependency=*/true,
10207 /*ambiguous_decls=*/NULL,
10208 id_expr_start_token->location);
10211 && expr != error_mark_node
10212 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10213 && TREE_CODE (expr) != TYPE_DECL
10214 && (TREE_CODE (expr) != BIT_NOT_EXPR
10215 || !TYPE_P (TREE_OPERAND (expr, 0)))
10216 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10218 /* Complete lookup of the id-expression. */
10219 expr = (finish_id_expression
10220 (id_expression, expr, parser->scope, &idk,
10221 /*integral_constant_expression_p=*/false,
10222 /*allow_non_integral_constant_expression_p=*/true,
10223 &non_integral_constant_expression_p,
10224 /*template_p=*/false,
10226 /*address_p=*/false,
10227 /*template_arg_p=*/false,
10229 id_expr_start_token->location));
10231 if (expr == error_mark_node)
10232 /* We found an id-expression, but it was something that we
10233 should not have found. This is an error, not something
10234 we can recover from, so note that we found an
10235 id-expression and we'll recover as gracefully as
10237 id_expression_or_member_access_p = true;
10241 && expr != error_mark_node
10242 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10243 /* We have an id-expression. */
10244 id_expression_or_member_access_p = true;
10247 if (!id_expression_or_member_access_p)
10249 /* Abort the id-expression parse. */
10250 cp_parser_abort_tentative_parse (parser);
10252 /* Parsing tentatively, again. */
10253 cp_parser_parse_tentatively (parser);
10255 /* Parse a class member access. */
10256 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10258 /*member_access_only_p=*/true, NULL);
10261 && expr != error_mark_node
10262 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10263 /* We have an id-expression. */
10264 id_expression_or_member_access_p = true;
10267 if (id_expression_or_member_access_p)
10268 /* We have parsed the complete id-expression or member access. */
10269 cp_parser_parse_definitely (parser);
10272 bool saved_greater_than_is_operator_p;
10274 /* Abort our attempt to parse an id-expression or member access
10276 cp_parser_abort_tentative_parse (parser);
10278 /* Within a parenthesized expression, a `>' token is always
10279 the greater-than operator. */
10280 saved_greater_than_is_operator_p
10281 = parser->greater_than_is_operator_p;
10282 parser->greater_than_is_operator_p = true;
10284 /* Parse a full expression. */
10285 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10287 /* The `>' token might be the end of a template-id or
10288 template-parameter-list now. */
10289 parser->greater_than_is_operator_p
10290 = saved_greater_than_is_operator_p;
10293 /* Go back to evaluating expressions. */
10294 --cp_unevaluated_operand;
10295 --c_inhibit_evaluation_warnings;
10297 /* Restore the old message and the integral constant expression
10299 parser->type_definition_forbidden_message = saved_message;
10300 parser->integral_constant_expression_p
10301 = saved_integral_constant_expression_p;
10302 parser->non_integral_constant_expression_p
10303 = saved_non_integral_constant_expression_p;
10305 if (expr == error_mark_node)
10307 /* Skip everything up to the closing `)'. */
10308 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10309 /*consume_paren=*/true);
10310 return error_mark_node;
10313 /* Parse to the closing `)'. */
10314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10316 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10317 /*consume_paren=*/true);
10318 return error_mark_node;
10321 return finish_decltype_type (expr, id_expression_or_member_access_p);
10324 /* Special member functions [gram.special] */
10326 /* Parse a conversion-function-id.
10328 conversion-function-id:
10329 operator conversion-type-id
10331 Returns an IDENTIFIER_NODE representing the operator. */
10334 cp_parser_conversion_function_id (cp_parser* parser)
10338 tree saved_qualifying_scope;
10339 tree saved_object_scope;
10340 tree pushed_scope = NULL_TREE;
10342 /* Look for the `operator' token. */
10343 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10344 return error_mark_node;
10345 /* When we parse the conversion-type-id, the current scope will be
10346 reset. However, we need that information in able to look up the
10347 conversion function later, so we save it here. */
10348 saved_scope = parser->scope;
10349 saved_qualifying_scope = parser->qualifying_scope;
10350 saved_object_scope = parser->object_scope;
10351 /* We must enter the scope of the class so that the names of
10352 entities declared within the class are available in the
10353 conversion-type-id. For example, consider:
10360 S::operator I() { ... }
10362 In order to see that `I' is a type-name in the definition, we
10363 must be in the scope of `S'. */
10365 pushed_scope = push_scope (saved_scope);
10366 /* Parse the conversion-type-id. */
10367 type = cp_parser_conversion_type_id (parser);
10368 /* Leave the scope of the class, if any. */
10370 pop_scope (pushed_scope);
10371 /* Restore the saved scope. */
10372 parser->scope = saved_scope;
10373 parser->qualifying_scope = saved_qualifying_scope;
10374 parser->object_scope = saved_object_scope;
10375 /* If the TYPE is invalid, indicate failure. */
10376 if (type == error_mark_node)
10377 return error_mark_node;
10378 return mangle_conv_op_name_for_type (type);
10381 /* Parse a conversion-type-id:
10383 conversion-type-id:
10384 type-specifier-seq conversion-declarator [opt]
10386 Returns the TYPE specified. */
10389 cp_parser_conversion_type_id (cp_parser* parser)
10392 cp_decl_specifier_seq type_specifiers;
10393 cp_declarator *declarator;
10394 tree type_specified;
10396 /* Parse the attributes. */
10397 attributes = cp_parser_attributes_opt (parser);
10398 /* Parse the type-specifiers. */
10399 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10400 /*is_trailing_return=*/false,
10402 /* If that didn't work, stop. */
10403 if (type_specifiers.type == error_mark_node)
10404 return error_mark_node;
10405 /* Parse the conversion-declarator. */
10406 declarator = cp_parser_conversion_declarator_opt (parser);
10408 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10409 /*initialized=*/0, &attributes);
10411 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10413 /* Don't give this error when parsing tentatively. This happens to
10414 work because we always parse this definitively once. */
10415 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10416 && type_uses_auto (type_specified))
10418 error ("invalid use of %<auto%> in conversion operator");
10419 return error_mark_node;
10422 return type_specified;
10425 /* Parse an (optional) conversion-declarator.
10427 conversion-declarator:
10428 ptr-operator conversion-declarator [opt]
10432 static cp_declarator *
10433 cp_parser_conversion_declarator_opt (cp_parser* parser)
10435 enum tree_code code;
10437 cp_cv_quals cv_quals;
10439 /* We don't know if there's a ptr-operator next, or not. */
10440 cp_parser_parse_tentatively (parser);
10441 /* Try the ptr-operator. */
10442 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10443 /* If it worked, look for more conversion-declarators. */
10444 if (cp_parser_parse_definitely (parser))
10446 cp_declarator *declarator;
10448 /* Parse another optional declarator. */
10449 declarator = cp_parser_conversion_declarator_opt (parser);
10451 return cp_parser_make_indirect_declarator
10452 (code, class_type, cv_quals, declarator);
10458 /* Parse an (optional) ctor-initializer.
10461 : mem-initializer-list
10463 Returns TRUE iff the ctor-initializer was actually present. */
10466 cp_parser_ctor_initializer_opt (cp_parser* parser)
10468 /* If the next token is not a `:', then there is no
10469 ctor-initializer. */
10470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10472 /* Do default initialization of any bases and members. */
10473 if (DECL_CONSTRUCTOR_P (current_function_decl))
10474 finish_mem_initializers (NULL_TREE);
10479 /* Consume the `:' token. */
10480 cp_lexer_consume_token (parser->lexer);
10481 /* And the mem-initializer-list. */
10482 cp_parser_mem_initializer_list (parser);
10487 /* Parse a mem-initializer-list.
10489 mem-initializer-list:
10490 mem-initializer ... [opt]
10491 mem-initializer ... [opt] , mem-initializer-list */
10494 cp_parser_mem_initializer_list (cp_parser* parser)
10496 tree mem_initializer_list = NULL_TREE;
10497 cp_token *token = cp_lexer_peek_token (parser->lexer);
10499 /* Let the semantic analysis code know that we are starting the
10500 mem-initializer-list. */
10501 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10502 error_at (token->location,
10503 "only constructors take member initializers");
10505 /* Loop through the list. */
10508 tree mem_initializer;
10510 token = cp_lexer_peek_token (parser->lexer);
10511 /* Parse the mem-initializer. */
10512 mem_initializer = cp_parser_mem_initializer (parser);
10513 /* If the next token is a `...', we're expanding member initializers. */
10514 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10516 /* Consume the `...'. */
10517 cp_lexer_consume_token (parser->lexer);
10519 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10520 can be expanded but members cannot. */
10521 if (mem_initializer != error_mark_node
10522 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10524 error_at (token->location,
10525 "cannot expand initializer for member %<%D%>",
10526 TREE_PURPOSE (mem_initializer));
10527 mem_initializer = error_mark_node;
10530 /* Construct the pack expansion type. */
10531 if (mem_initializer != error_mark_node)
10532 mem_initializer = make_pack_expansion (mem_initializer);
10534 /* Add it to the list, unless it was erroneous. */
10535 if (mem_initializer != error_mark_node)
10537 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10538 mem_initializer_list = mem_initializer;
10540 /* If the next token is not a `,', we're done. */
10541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10543 /* Consume the `,' token. */
10544 cp_lexer_consume_token (parser->lexer);
10547 /* Perform semantic analysis. */
10548 if (DECL_CONSTRUCTOR_P (current_function_decl))
10549 finish_mem_initializers (mem_initializer_list);
10552 /* Parse a mem-initializer.
10555 mem-initializer-id ( expression-list [opt] )
10556 mem-initializer-id braced-init-list
10561 ( expression-list [opt] )
10563 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10564 class) or FIELD_DECL (for a non-static data member) to initialize;
10565 the TREE_VALUE is the expression-list. An empty initialization
10566 list is represented by void_list_node. */
10569 cp_parser_mem_initializer (cp_parser* parser)
10571 tree mem_initializer_id;
10572 tree expression_list;
10574 cp_token *token = cp_lexer_peek_token (parser->lexer);
10576 /* Find out what is being initialized. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10579 permerror (token->location,
10580 "anachronistic old-style base class initializer");
10581 mem_initializer_id = NULL_TREE;
10585 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10586 if (mem_initializer_id == error_mark_node)
10587 return mem_initializer_id;
10589 member = expand_member_init (mem_initializer_id);
10590 if (member && !DECL_P (member))
10591 in_base_initializer = 1;
10593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10595 bool expr_non_constant_p;
10596 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10597 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10598 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10599 expression_list = build_tree_list (NULL_TREE, expression_list);
10604 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10606 /*allow_expansion_p=*/true,
10607 /*non_constant_p=*/NULL);
10609 return error_mark_node;
10610 expression_list = build_tree_list_vec (vec);
10611 release_tree_vector (vec);
10614 if (expression_list == error_mark_node)
10615 return error_mark_node;
10616 if (!expression_list)
10617 expression_list = void_type_node;
10619 in_base_initializer = 0;
10621 return member ? build_tree_list (member, expression_list) : error_mark_node;
10624 /* Parse a mem-initializer-id.
10626 mem-initializer-id:
10627 :: [opt] nested-name-specifier [opt] class-name
10630 Returns a TYPE indicating the class to be initializer for the first
10631 production. Returns an IDENTIFIER_NODE indicating the data member
10632 to be initialized for the second production. */
10635 cp_parser_mem_initializer_id (cp_parser* parser)
10637 bool global_scope_p;
10638 bool nested_name_specifier_p;
10639 bool template_p = false;
10642 cp_token *token = cp_lexer_peek_token (parser->lexer);
10644 /* `typename' is not allowed in this context ([temp.res]). */
10645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10647 error_at (token->location,
10648 "keyword %<typename%> not allowed in this context (a qualified "
10649 "member initializer is implicitly a type)");
10650 cp_lexer_consume_token (parser->lexer);
10652 /* Look for the optional `::' operator. */
10654 = (cp_parser_global_scope_opt (parser,
10655 /*current_scope_valid_p=*/false)
10657 /* Look for the optional nested-name-specifier. The simplest way to
10662 The keyword `typename' is not permitted in a base-specifier or
10663 mem-initializer; in these contexts a qualified name that
10664 depends on a template-parameter is implicitly assumed to be a
10667 is to assume that we have seen the `typename' keyword at this
10669 nested_name_specifier_p
10670 = (cp_parser_nested_name_specifier_opt (parser,
10671 /*typename_keyword_p=*/true,
10672 /*check_dependency_p=*/true,
10674 /*is_declaration=*/true)
10676 if (nested_name_specifier_p)
10677 template_p = cp_parser_optional_template_keyword (parser);
10678 /* If there is a `::' operator or a nested-name-specifier, then we
10679 are definitely looking for a class-name. */
10680 if (global_scope_p || nested_name_specifier_p)
10681 return cp_parser_class_name (parser,
10682 /*typename_keyword_p=*/true,
10683 /*template_keyword_p=*/template_p,
10685 /*check_dependency_p=*/true,
10686 /*class_head_p=*/false,
10687 /*is_declaration=*/true);
10688 /* Otherwise, we could also be looking for an ordinary identifier. */
10689 cp_parser_parse_tentatively (parser);
10690 /* Try a class-name. */
10691 id = cp_parser_class_name (parser,
10692 /*typename_keyword_p=*/true,
10693 /*template_keyword_p=*/false,
10695 /*check_dependency_p=*/true,
10696 /*class_head_p=*/false,
10697 /*is_declaration=*/true);
10698 /* If we found one, we're done. */
10699 if (cp_parser_parse_definitely (parser))
10701 /* Otherwise, look for an ordinary identifier. */
10702 return cp_parser_identifier (parser);
10705 /* Overloading [gram.over] */
10707 /* Parse an operator-function-id.
10709 operator-function-id:
10712 Returns an IDENTIFIER_NODE for the operator which is a
10713 human-readable spelling of the identifier, e.g., `operator +'. */
10716 cp_parser_operator_function_id (cp_parser* parser)
10718 /* Look for the `operator' keyword. */
10719 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10720 return error_mark_node;
10721 /* And then the name of the operator itself. */
10722 return cp_parser_operator (parser);
10725 /* Parse an operator.
10728 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10729 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10730 || ++ -- , ->* -> () []
10737 Returns an IDENTIFIER_NODE for the operator which is a
10738 human-readable spelling of the identifier, e.g., `operator +'. */
10741 cp_parser_operator (cp_parser* parser)
10743 tree id = NULL_TREE;
10746 /* Peek at the next token. */
10747 token = cp_lexer_peek_token (parser->lexer);
10748 /* Figure out which operator we have. */
10749 switch (token->type)
10755 /* The keyword should be either `new' or `delete'. */
10756 if (token->keyword == RID_NEW)
10758 else if (token->keyword == RID_DELETE)
10763 /* Consume the `new' or `delete' token. */
10764 cp_lexer_consume_token (parser->lexer);
10766 /* Peek at the next token. */
10767 token = cp_lexer_peek_token (parser->lexer);
10768 /* If it's a `[' token then this is the array variant of the
10770 if (token->type == CPP_OPEN_SQUARE)
10772 /* Consume the `[' token. */
10773 cp_lexer_consume_token (parser->lexer);
10774 /* Look for the `]' token. */
10775 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10776 id = ansi_opname (op == NEW_EXPR
10777 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10779 /* Otherwise, we have the non-array variant. */
10781 id = ansi_opname (op);
10787 id = ansi_opname (PLUS_EXPR);
10791 id = ansi_opname (MINUS_EXPR);
10795 id = ansi_opname (MULT_EXPR);
10799 id = ansi_opname (TRUNC_DIV_EXPR);
10803 id = ansi_opname (TRUNC_MOD_EXPR);
10807 id = ansi_opname (BIT_XOR_EXPR);
10811 id = ansi_opname (BIT_AND_EXPR);
10815 id = ansi_opname (BIT_IOR_EXPR);
10819 id = ansi_opname (BIT_NOT_EXPR);
10823 id = ansi_opname (TRUTH_NOT_EXPR);
10827 id = ansi_assopname (NOP_EXPR);
10831 id = ansi_opname (LT_EXPR);
10835 id = ansi_opname (GT_EXPR);
10839 id = ansi_assopname (PLUS_EXPR);
10843 id = ansi_assopname (MINUS_EXPR);
10847 id = ansi_assopname (MULT_EXPR);
10851 id = ansi_assopname (TRUNC_DIV_EXPR);
10855 id = ansi_assopname (TRUNC_MOD_EXPR);
10859 id = ansi_assopname (BIT_XOR_EXPR);
10863 id = ansi_assopname (BIT_AND_EXPR);
10867 id = ansi_assopname (BIT_IOR_EXPR);
10871 id = ansi_opname (LSHIFT_EXPR);
10875 id = ansi_opname (RSHIFT_EXPR);
10878 case CPP_LSHIFT_EQ:
10879 id = ansi_assopname (LSHIFT_EXPR);
10882 case CPP_RSHIFT_EQ:
10883 id = ansi_assopname (RSHIFT_EXPR);
10887 id = ansi_opname (EQ_EXPR);
10891 id = ansi_opname (NE_EXPR);
10895 id = ansi_opname (LE_EXPR);
10898 case CPP_GREATER_EQ:
10899 id = ansi_opname (GE_EXPR);
10903 id = ansi_opname (TRUTH_ANDIF_EXPR);
10907 id = ansi_opname (TRUTH_ORIF_EXPR);
10910 case CPP_PLUS_PLUS:
10911 id = ansi_opname (POSTINCREMENT_EXPR);
10914 case CPP_MINUS_MINUS:
10915 id = ansi_opname (PREDECREMENT_EXPR);
10919 id = ansi_opname (COMPOUND_EXPR);
10922 case CPP_DEREF_STAR:
10923 id = ansi_opname (MEMBER_REF);
10927 id = ansi_opname (COMPONENT_REF);
10930 case CPP_OPEN_PAREN:
10931 /* Consume the `('. */
10932 cp_lexer_consume_token (parser->lexer);
10933 /* Look for the matching `)'. */
10934 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10935 return ansi_opname (CALL_EXPR);
10937 case CPP_OPEN_SQUARE:
10938 /* Consume the `['. */
10939 cp_lexer_consume_token (parser->lexer);
10940 /* Look for the matching `]'. */
10941 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10942 return ansi_opname (ARRAY_REF);
10945 /* Anything else is an error. */
10949 /* If we have selected an identifier, we need to consume the
10952 cp_lexer_consume_token (parser->lexer);
10953 /* Otherwise, no valid operator name was present. */
10956 cp_parser_error (parser, "expected operator");
10957 id = error_mark_node;
10963 /* Parse a template-declaration.
10965 template-declaration:
10966 export [opt] template < template-parameter-list > declaration
10968 If MEMBER_P is TRUE, this template-declaration occurs within a
10971 The grammar rule given by the standard isn't correct. What
10972 is really meant is:
10974 template-declaration:
10975 export [opt] template-parameter-list-seq
10976 decl-specifier-seq [opt] init-declarator [opt] ;
10977 export [opt] template-parameter-list-seq
10978 function-definition
10980 template-parameter-list-seq:
10981 template-parameter-list-seq [opt]
10982 template < template-parameter-list > */
10985 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10987 /* Check for `export'. */
10988 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10990 /* Consume the `export' token. */
10991 cp_lexer_consume_token (parser->lexer);
10992 /* Warn that we do not support `export'. */
10993 warning (0, "keyword %<export%> not implemented, and will be ignored");
10996 cp_parser_template_declaration_after_export (parser, member_p);
10999 /* Parse a template-parameter-list.
11001 template-parameter-list:
11003 template-parameter-list , template-parameter
11005 Returns a TREE_LIST. Each node represents a template parameter.
11006 The nodes are connected via their TREE_CHAINs. */
11009 cp_parser_template_parameter_list (cp_parser* parser)
11011 tree parameter_list = NULL_TREE;
11013 begin_template_parm_list ();
11018 bool is_parameter_pack;
11019 location_t parm_loc;
11021 /* Parse the template-parameter. */
11022 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11023 parameter = cp_parser_template_parameter (parser,
11025 &is_parameter_pack);
11026 /* Add it to the list. */
11027 if (parameter != error_mark_node)
11028 parameter_list = process_template_parm (parameter_list,
11032 is_parameter_pack);
11035 tree err_parm = build_tree_list (parameter, parameter);
11036 TREE_VALUE (err_parm) = error_mark_node;
11037 parameter_list = chainon (parameter_list, err_parm);
11040 /* If the next token is not a `,', we're done. */
11041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11043 /* Otherwise, consume the `,' token. */
11044 cp_lexer_consume_token (parser->lexer);
11047 return end_template_parm_list (parameter_list);
11050 /* Parse a template-parameter.
11052 template-parameter:
11054 parameter-declaration
11056 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11057 the parameter. The TREE_PURPOSE is the default value, if any.
11058 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11059 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11060 set to true iff this parameter is a parameter pack. */
11063 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11064 bool *is_parameter_pack)
11067 cp_parameter_declarator *parameter_declarator;
11068 cp_declarator *id_declarator;
11071 /* Assume it is a type parameter or a template parameter. */
11072 *is_non_type = false;
11073 /* Assume it not a parameter pack. */
11074 *is_parameter_pack = false;
11075 /* Peek at the next token. */
11076 token = cp_lexer_peek_token (parser->lexer);
11077 /* If it is `class' or `template', we have a type-parameter. */
11078 if (token->keyword == RID_TEMPLATE)
11079 return cp_parser_type_parameter (parser, is_parameter_pack);
11080 /* If it is `class' or `typename' we do not know yet whether it is a
11081 type parameter or a non-type parameter. Consider:
11083 template <typename T, typename T::X X> ...
11087 template <class C, class D*> ...
11089 Here, the first parameter is a type parameter, and the second is
11090 a non-type parameter. We can tell by looking at the token after
11091 the identifier -- if it is a `,', `=', or `>' then we have a type
11093 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11095 /* Peek at the token after `class' or `typename'. */
11096 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11097 /* If it's an ellipsis, we have a template type parameter
11099 if (token->type == CPP_ELLIPSIS)
11100 return cp_parser_type_parameter (parser, is_parameter_pack);
11101 /* If it's an identifier, skip it. */
11102 if (token->type == CPP_NAME)
11103 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11104 /* Now, see if the token looks like the end of a template
11106 if (token->type == CPP_COMMA
11107 || token->type == CPP_EQ
11108 || token->type == CPP_GREATER)
11109 return cp_parser_type_parameter (parser, is_parameter_pack);
11112 /* Otherwise, it is a non-type parameter.
11116 When parsing a default template-argument for a non-type
11117 template-parameter, the first non-nested `>' is taken as the end
11118 of the template parameter-list rather than a greater-than
11120 *is_non_type = true;
11121 parameter_declarator
11122 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11123 /*parenthesized_p=*/NULL);
11125 /* If the parameter declaration is marked as a parameter pack, set
11126 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11127 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11129 if (parameter_declarator
11130 && parameter_declarator->declarator
11131 && parameter_declarator->declarator->parameter_pack_p)
11133 *is_parameter_pack = true;
11134 parameter_declarator->declarator->parameter_pack_p = false;
11137 /* If the next token is an ellipsis, and we don't already have it
11138 marked as a parameter pack, then we have a parameter pack (that
11139 has no declarator). */
11140 if (!*is_parameter_pack
11141 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11142 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11144 /* Consume the `...'. */
11145 cp_lexer_consume_token (parser->lexer);
11146 maybe_warn_variadic_templates ();
11148 *is_parameter_pack = true;
11150 /* We might end up with a pack expansion as the type of the non-type
11151 template parameter, in which case this is a non-type template
11153 else if (parameter_declarator
11154 && parameter_declarator->decl_specifiers.type
11155 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11157 *is_parameter_pack = true;
11158 parameter_declarator->decl_specifiers.type =
11159 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11162 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11164 /* Parameter packs cannot have default arguments. However, a
11165 user may try to do so, so we'll parse them and give an
11166 appropriate diagnostic here. */
11168 /* Consume the `='. */
11169 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11170 cp_lexer_consume_token (parser->lexer);
11172 /* Find the name of the parameter pack. */
11173 id_declarator = parameter_declarator->declarator;
11174 while (id_declarator && id_declarator->kind != cdk_id)
11175 id_declarator = id_declarator->declarator;
11177 if (id_declarator && id_declarator->kind == cdk_id)
11178 error_at (start_token->location,
11179 "template parameter pack %qD cannot have a default argument",
11180 id_declarator->u.id.unqualified_name);
11182 error_at (start_token->location,
11183 "template parameter pack cannot have a default argument");
11185 /* Parse the default argument, but throw away the result. */
11186 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11189 parm = grokdeclarator (parameter_declarator->declarator,
11190 ¶meter_declarator->decl_specifiers,
11191 TPARM, /*initialized=*/0,
11192 /*attrlist=*/NULL);
11193 if (parm == error_mark_node)
11194 return error_mark_node;
11196 return build_tree_list (parameter_declarator->default_argument, parm);
11199 /* Parse a type-parameter.
11202 class identifier [opt]
11203 class identifier [opt] = type-id
11204 typename identifier [opt]
11205 typename identifier [opt] = type-id
11206 template < template-parameter-list > class identifier [opt]
11207 template < template-parameter-list > class identifier [opt]
11210 GNU Extension (variadic templates):
11213 class ... identifier [opt]
11214 typename ... identifier [opt]
11216 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11217 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11218 the declaration of the parameter.
11220 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11223 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11228 /* Look for a keyword to tell us what kind of parameter this is. */
11229 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11231 return error_mark_node;
11233 switch (token->keyword)
11239 tree default_argument;
11241 /* If the next token is an ellipsis, we have a template
11243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11245 /* Consume the `...' token. */
11246 cp_lexer_consume_token (parser->lexer);
11247 maybe_warn_variadic_templates ();
11249 *is_parameter_pack = true;
11252 /* If the next token is an identifier, then it names the
11254 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11255 identifier = cp_parser_identifier (parser);
11257 identifier = NULL_TREE;
11259 /* Create the parameter. */
11260 parameter = finish_template_type_parm (class_type_node, identifier);
11262 /* If the next token is an `=', we have a default argument. */
11263 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11265 /* Consume the `=' token. */
11266 cp_lexer_consume_token (parser->lexer);
11267 /* Parse the default-argument. */
11268 push_deferring_access_checks (dk_no_deferred);
11269 default_argument = cp_parser_type_id (parser);
11271 /* Template parameter packs cannot have default
11273 if (*is_parameter_pack)
11276 error_at (token->location,
11277 "template parameter pack %qD cannot have a "
11278 "default argument", identifier);
11280 error_at (token->location,
11281 "template parameter packs cannot have "
11282 "default arguments");
11283 default_argument = NULL_TREE;
11285 pop_deferring_access_checks ();
11288 default_argument = NULL_TREE;
11290 /* Create the combined representation of the parameter and the
11291 default argument. */
11292 parameter = build_tree_list (default_argument, parameter);
11299 tree default_argument;
11301 /* Look for the `<'. */
11302 cp_parser_require (parser, CPP_LESS, RT_LESS);
11303 /* Parse the template-parameter-list. */
11304 cp_parser_template_parameter_list (parser);
11305 /* Look for the `>'. */
11306 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11307 /* Look for the `class' keyword. */
11308 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11309 /* If the next token is an ellipsis, we have a template
11311 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11313 /* Consume the `...' token. */
11314 cp_lexer_consume_token (parser->lexer);
11315 maybe_warn_variadic_templates ();
11317 *is_parameter_pack = true;
11319 /* If the next token is an `=', then there is a
11320 default-argument. If the next token is a `>', we are at
11321 the end of the parameter-list. If the next token is a `,',
11322 then we are at the end of this parameter. */
11323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11324 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11325 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11327 identifier = cp_parser_identifier (parser);
11328 /* Treat invalid names as if the parameter were nameless. */
11329 if (identifier == error_mark_node)
11330 identifier = NULL_TREE;
11333 identifier = NULL_TREE;
11335 /* Create the template parameter. */
11336 parameter = finish_template_template_parm (class_type_node,
11339 /* If the next token is an `=', then there is a
11340 default-argument. */
11341 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11345 /* Consume the `='. */
11346 cp_lexer_consume_token (parser->lexer);
11347 /* Parse the id-expression. */
11348 push_deferring_access_checks (dk_no_deferred);
11349 /* save token before parsing the id-expression, for error
11351 token = cp_lexer_peek_token (parser->lexer);
11353 = cp_parser_id_expression (parser,
11354 /*template_keyword_p=*/false,
11355 /*check_dependency_p=*/true,
11356 /*template_p=*/&is_template,
11357 /*declarator_p=*/false,
11358 /*optional_p=*/false);
11359 if (TREE_CODE (default_argument) == TYPE_DECL)
11360 /* If the id-expression was a template-id that refers to
11361 a template-class, we already have the declaration here,
11362 so no further lookup is needed. */
11365 /* Look up the name. */
11367 = cp_parser_lookup_name (parser, default_argument,
11369 /*is_template=*/is_template,
11370 /*is_namespace=*/false,
11371 /*check_dependency=*/true,
11372 /*ambiguous_decls=*/NULL,
11374 /* See if the default argument is valid. */
11376 = check_template_template_default_arg (default_argument);
11378 /* Template parameter packs cannot have default
11380 if (*is_parameter_pack)
11383 error_at (token->location,
11384 "template parameter pack %qD cannot "
11385 "have a default argument",
11388 error_at (token->location, "template parameter packs cannot "
11389 "have default arguments");
11390 default_argument = NULL_TREE;
11392 pop_deferring_access_checks ();
11395 default_argument = NULL_TREE;
11397 /* Create the combined representation of the parameter and the
11398 default argument. */
11399 parameter = build_tree_list (default_argument, parameter);
11404 gcc_unreachable ();
11411 /* Parse a template-id.
11414 template-name < template-argument-list [opt] >
11416 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11417 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11418 returned. Otherwise, if the template-name names a function, or set
11419 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11420 names a class, returns a TYPE_DECL for the specialization.
11422 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11423 uninstantiated templates. */
11426 cp_parser_template_id (cp_parser *parser,
11427 bool template_keyword_p,
11428 bool check_dependency_p,
11429 bool is_declaration)
11435 cp_token_position start_of_id = 0;
11436 deferred_access_check *chk;
11437 VEC (deferred_access_check,gc) *access_check;
11438 cp_token *next_token = NULL, *next_token_2 = NULL;
11439 bool is_identifier;
11441 /* If the next token corresponds to a template-id, there is no need
11443 next_token = cp_lexer_peek_token (parser->lexer);
11444 if (next_token->type == CPP_TEMPLATE_ID)
11446 struct tree_check *check_value;
11448 /* Get the stored value. */
11449 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11450 /* Perform any access checks that were deferred. */
11451 access_check = check_value->checks;
11454 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11455 perform_or_defer_access_check (chk->binfo,
11459 /* Return the stored value. */
11460 return check_value->value;
11463 /* Avoid performing name lookup if there is no possibility of
11464 finding a template-id. */
11465 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11466 || (next_token->type == CPP_NAME
11467 && !cp_parser_nth_token_starts_template_argument_list_p
11470 cp_parser_error (parser, "expected template-id");
11471 return error_mark_node;
11474 /* Remember where the template-id starts. */
11475 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11476 start_of_id = cp_lexer_token_position (parser->lexer, false);
11478 push_deferring_access_checks (dk_deferred);
11480 /* Parse the template-name. */
11481 is_identifier = false;
11482 templ = cp_parser_template_name (parser, template_keyword_p,
11483 check_dependency_p,
11486 if (templ == error_mark_node || is_identifier)
11488 pop_deferring_access_checks ();
11492 /* If we find the sequence `[:' after a template-name, it's probably
11493 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11494 parse correctly the argument list. */
11495 next_token = cp_lexer_peek_token (parser->lexer);
11496 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11497 if (next_token->type == CPP_OPEN_SQUARE
11498 && next_token->flags & DIGRAPH
11499 && next_token_2->type == CPP_COLON
11500 && !(next_token_2->flags & PREV_WHITE))
11502 cp_parser_parse_tentatively (parser);
11503 /* Change `:' into `::'. */
11504 next_token_2->type = CPP_SCOPE;
11505 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11507 cp_lexer_consume_token (parser->lexer);
11509 /* Parse the arguments. */
11510 arguments = cp_parser_enclosed_template_argument_list (parser);
11511 if (!cp_parser_parse_definitely (parser))
11513 /* If we couldn't parse an argument list, then we revert our changes
11514 and return simply an error. Maybe this is not a template-id
11516 next_token_2->type = CPP_COLON;
11517 cp_parser_error (parser, "expected %<<%>");
11518 pop_deferring_access_checks ();
11519 return error_mark_node;
11521 /* Otherwise, emit an error about the invalid digraph, but continue
11522 parsing because we got our argument list. */
11523 if (permerror (next_token->location,
11524 "%<<::%> cannot begin a template-argument list"))
11526 static bool hint = false;
11527 inform (next_token->location,
11528 "%<<:%> is an alternate spelling for %<[%>."
11529 " Insert whitespace between %<<%> and %<::%>");
11530 if (!hint && !flag_permissive)
11532 inform (next_token->location, "(if you use %<-fpermissive%>"
11533 " G++ will accept your code)");
11540 /* Look for the `<' that starts the template-argument-list. */
11541 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11543 pop_deferring_access_checks ();
11544 return error_mark_node;
11546 /* Parse the arguments. */
11547 arguments = cp_parser_enclosed_template_argument_list (parser);
11550 /* Build a representation of the specialization. */
11551 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11552 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11553 else if (DECL_CLASS_TEMPLATE_P (templ)
11554 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11556 bool entering_scope;
11557 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11558 template (rather than some instantiation thereof) only if
11559 is not nested within some other construct. For example, in
11560 "template <typename T> void f(T) { A<T>::", A<T> is just an
11561 instantiation of A. */
11562 entering_scope = (template_parm_scope_p ()
11563 && cp_lexer_next_token_is (parser->lexer,
11566 = finish_template_type (templ, arguments, entering_scope);
11570 /* If it's not a class-template or a template-template, it should be
11571 a function-template. */
11572 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11573 || TREE_CODE (templ) == OVERLOAD
11574 || BASELINK_P (templ)));
11576 template_id = lookup_template_function (templ, arguments);
11579 /* If parsing tentatively, replace the sequence of tokens that makes
11580 up the template-id with a CPP_TEMPLATE_ID token. That way,
11581 should we re-parse the token stream, we will not have to repeat
11582 the effort required to do the parse, nor will we issue duplicate
11583 error messages about problems during instantiation of the
11587 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11589 /* Reset the contents of the START_OF_ID token. */
11590 token->type = CPP_TEMPLATE_ID;
11591 /* Retrieve any deferred checks. Do not pop this access checks yet
11592 so the memory will not be reclaimed during token replacing below. */
11593 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11594 token->u.tree_check_value->value = template_id;
11595 token->u.tree_check_value->checks = get_deferred_access_checks ();
11596 token->keyword = RID_MAX;
11598 /* Purge all subsequent tokens. */
11599 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11601 /* ??? Can we actually assume that, if template_id ==
11602 error_mark_node, we will have issued a diagnostic to the
11603 user, as opposed to simply marking the tentative parse as
11605 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11606 error_at (token->location, "parse error in template argument list");
11609 pop_deferring_access_checks ();
11610 return template_id;
11613 /* Parse a template-name.
11618 The standard should actually say:
11622 operator-function-id
11624 A defect report has been filed about this issue.
11626 A conversion-function-id cannot be a template name because they cannot
11627 be part of a template-id. In fact, looking at this code:
11629 a.operator K<int>()
11631 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11632 It is impossible to call a templated conversion-function-id with an
11633 explicit argument list, since the only allowed template parameter is
11634 the type to which it is converting.
11636 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11637 `template' keyword, in a construction like:
11641 In that case `f' is taken to be a template-name, even though there
11642 is no way of knowing for sure.
11644 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11645 name refers to a set of overloaded functions, at least one of which
11646 is a template, or an IDENTIFIER_NODE with the name of the template,
11647 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11648 names are looked up inside uninstantiated templates. */
11651 cp_parser_template_name (cp_parser* parser,
11652 bool template_keyword_p,
11653 bool check_dependency_p,
11654 bool is_declaration,
11655 bool *is_identifier)
11660 cp_token *token = cp_lexer_peek_token (parser->lexer);
11662 /* If the next token is `operator', then we have either an
11663 operator-function-id or a conversion-function-id. */
11664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11666 /* We don't know whether we're looking at an
11667 operator-function-id or a conversion-function-id. */
11668 cp_parser_parse_tentatively (parser);
11669 /* Try an operator-function-id. */
11670 identifier = cp_parser_operator_function_id (parser);
11671 /* If that didn't work, try a conversion-function-id. */
11672 if (!cp_parser_parse_definitely (parser))
11674 cp_parser_error (parser, "expected template-name");
11675 return error_mark_node;
11678 /* Look for the identifier. */
11680 identifier = cp_parser_identifier (parser);
11682 /* If we didn't find an identifier, we don't have a template-id. */
11683 if (identifier == error_mark_node)
11684 return error_mark_node;
11686 /* If the name immediately followed the `template' keyword, then it
11687 is a template-name. However, if the next token is not `<', then
11688 we do not treat it as a template-name, since it is not being used
11689 as part of a template-id. This enables us to handle constructs
11692 template <typename T> struct S { S(); };
11693 template <typename T> S<T>::S();
11695 correctly. We would treat `S' as a template -- if it were `S<T>'
11696 -- but we do not if there is no `<'. */
11698 if (processing_template_decl
11699 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11701 /* In a declaration, in a dependent context, we pretend that the
11702 "template" keyword was present in order to improve error
11703 recovery. For example, given:
11705 template <typename T> void f(T::X<int>);
11707 we want to treat "X<int>" as a template-id. */
11709 && !template_keyword_p
11710 && parser->scope && TYPE_P (parser->scope)
11711 && check_dependency_p
11712 && dependent_scope_p (parser->scope)
11713 /* Do not do this for dtors (or ctors), since they never
11714 need the template keyword before their name. */
11715 && !constructor_name_p (identifier, parser->scope))
11717 cp_token_position start = 0;
11719 /* Explain what went wrong. */
11720 error_at (token->location, "non-template %qD used as template",
11722 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11723 parser->scope, identifier);
11724 /* If parsing tentatively, find the location of the "<" token. */
11725 if (cp_parser_simulate_error (parser))
11726 start = cp_lexer_token_position (parser->lexer, true);
11727 /* Parse the template arguments so that we can issue error
11728 messages about them. */
11729 cp_lexer_consume_token (parser->lexer);
11730 cp_parser_enclosed_template_argument_list (parser);
11731 /* Skip tokens until we find a good place from which to
11732 continue parsing. */
11733 cp_parser_skip_to_closing_parenthesis (parser,
11734 /*recovering=*/true,
11736 /*consume_paren=*/false);
11737 /* If parsing tentatively, permanently remove the
11738 template argument list. That will prevent duplicate
11739 error messages from being issued about the missing
11740 "template" keyword. */
11742 cp_lexer_purge_tokens_after (parser->lexer, start);
11744 *is_identifier = true;
11748 /* If the "template" keyword is present, then there is generally
11749 no point in doing name-lookup, so we just return IDENTIFIER.
11750 But, if the qualifying scope is non-dependent then we can
11751 (and must) do name-lookup normally. */
11752 if (template_keyword_p
11754 || (TYPE_P (parser->scope)
11755 && dependent_type_p (parser->scope))))
11759 /* Look up the name. */
11760 decl = cp_parser_lookup_name (parser, identifier,
11762 /*is_template=*/true,
11763 /*is_namespace=*/false,
11764 check_dependency_p,
11765 /*ambiguous_decls=*/NULL,
11768 /* If DECL is a template, then the name was a template-name. */
11769 if (TREE_CODE (decl) == TEMPLATE_DECL)
11773 tree fn = NULL_TREE;
11775 /* The standard does not explicitly indicate whether a name that
11776 names a set of overloaded declarations, some of which are
11777 templates, is a template-name. However, such a name should
11778 be a template-name; otherwise, there is no way to form a
11779 template-id for the overloaded templates. */
11780 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11781 if (TREE_CODE (fns) == OVERLOAD)
11782 for (fn = fns; fn; fn = OVL_NEXT (fn))
11783 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11788 /* The name does not name a template. */
11789 cp_parser_error (parser, "expected template-name");
11790 return error_mark_node;
11794 /* If DECL is dependent, and refers to a function, then just return
11795 its name; we will look it up again during template instantiation. */
11796 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11798 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11799 if (TYPE_P (scope) && dependent_type_p (scope))
11806 /* Parse a template-argument-list.
11808 template-argument-list:
11809 template-argument ... [opt]
11810 template-argument-list , template-argument ... [opt]
11812 Returns a TREE_VEC containing the arguments. */
11815 cp_parser_template_argument_list (cp_parser* parser)
11817 tree fixed_args[10];
11818 unsigned n_args = 0;
11819 unsigned alloced = 10;
11820 tree *arg_ary = fixed_args;
11822 bool saved_in_template_argument_list_p;
11824 bool saved_non_ice_p;
11826 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11827 parser->in_template_argument_list_p = true;
11828 /* Even if the template-id appears in an integral
11829 constant-expression, the contents of the argument list do
11831 saved_ice_p = parser->integral_constant_expression_p;
11832 parser->integral_constant_expression_p = false;
11833 saved_non_ice_p = parser->non_integral_constant_expression_p;
11834 parser->non_integral_constant_expression_p = false;
11835 /* Parse the arguments. */
11841 /* Consume the comma. */
11842 cp_lexer_consume_token (parser->lexer);
11844 /* Parse the template-argument. */
11845 argument = cp_parser_template_argument (parser);
11847 /* If the next token is an ellipsis, we're expanding a template
11849 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11851 if (argument == error_mark_node)
11853 cp_token *token = cp_lexer_peek_token (parser->lexer);
11854 error_at (token->location,
11855 "expected parameter pack before %<...%>");
11857 /* Consume the `...' token. */
11858 cp_lexer_consume_token (parser->lexer);
11860 /* Make the argument into a TYPE_PACK_EXPANSION or
11861 EXPR_PACK_EXPANSION. */
11862 argument = make_pack_expansion (argument);
11865 if (n_args == alloced)
11869 if (arg_ary == fixed_args)
11871 arg_ary = XNEWVEC (tree, alloced);
11872 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11875 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11877 arg_ary[n_args++] = argument;
11879 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11881 vec = make_tree_vec (n_args);
11884 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11886 if (arg_ary != fixed_args)
11888 parser->non_integral_constant_expression_p = saved_non_ice_p;
11889 parser->integral_constant_expression_p = saved_ice_p;
11890 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11891 #ifdef ENABLE_CHECKING
11892 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11897 /* Parse a template-argument.
11900 assignment-expression
11904 The representation is that of an assignment-expression, type-id, or
11905 id-expression -- except that the qualified id-expression is
11906 evaluated, so that the value returned is either a DECL or an
11909 Although the standard says "assignment-expression", it forbids
11910 throw-expressions or assignments in the template argument.
11911 Therefore, we use "conditional-expression" instead. */
11914 cp_parser_template_argument (cp_parser* parser)
11919 bool maybe_type_id = false;
11920 cp_token *token = NULL, *argument_start_token = NULL;
11923 /* There's really no way to know what we're looking at, so we just
11924 try each alternative in order.
11928 In a template-argument, an ambiguity between a type-id and an
11929 expression is resolved to a type-id, regardless of the form of
11930 the corresponding template-parameter.
11932 Therefore, we try a type-id first. */
11933 cp_parser_parse_tentatively (parser);
11934 argument = cp_parser_template_type_arg (parser);
11935 /* If there was no error parsing the type-id but the next token is a
11936 '>>', our behavior depends on which dialect of C++ we're
11937 parsing. In C++98, we probably found a typo for '> >'. But there
11938 are type-id which are also valid expressions. For instance:
11940 struct X { int operator >> (int); };
11941 template <int V> struct Foo {};
11944 Here 'X()' is a valid type-id of a function type, but the user just
11945 wanted to write the expression "X() >> 5". Thus, we remember that we
11946 found a valid type-id, but we still try to parse the argument as an
11947 expression to see what happens.
11949 In C++0x, the '>>' will be considered two separate '>'
11951 if (!cp_parser_error_occurred (parser)
11952 && cxx_dialect == cxx98
11953 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11955 maybe_type_id = true;
11956 cp_parser_abort_tentative_parse (parser);
11960 /* If the next token isn't a `,' or a `>', then this argument wasn't
11961 really finished. This means that the argument is not a valid
11963 if (!cp_parser_next_token_ends_template_argument_p (parser))
11964 cp_parser_error (parser, "expected template-argument");
11965 /* If that worked, we're done. */
11966 if (cp_parser_parse_definitely (parser))
11969 /* We're still not sure what the argument will be. */
11970 cp_parser_parse_tentatively (parser);
11971 /* Try a template. */
11972 argument_start_token = cp_lexer_peek_token (parser->lexer);
11973 argument = cp_parser_id_expression (parser,
11974 /*template_keyword_p=*/false,
11975 /*check_dependency_p=*/true,
11977 /*declarator_p=*/false,
11978 /*optional_p=*/false);
11979 /* If the next token isn't a `,' or a `>', then this argument wasn't
11980 really finished. */
11981 if (!cp_parser_next_token_ends_template_argument_p (parser))
11982 cp_parser_error (parser, "expected template-argument");
11983 if (!cp_parser_error_occurred (parser))
11985 /* Figure out what is being referred to. If the id-expression
11986 was for a class template specialization, then we will have a
11987 TYPE_DECL at this point. There is no need to do name lookup
11988 at this point in that case. */
11989 if (TREE_CODE (argument) != TYPE_DECL)
11990 argument = cp_parser_lookup_name (parser, argument,
11992 /*is_template=*/template_p,
11993 /*is_namespace=*/false,
11994 /*check_dependency=*/true,
11995 /*ambiguous_decls=*/NULL,
11996 argument_start_token->location);
11997 if (TREE_CODE (argument) != TEMPLATE_DECL
11998 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11999 cp_parser_error (parser, "expected template-name");
12001 if (cp_parser_parse_definitely (parser))
12003 /* It must be a non-type argument. There permitted cases are given
12004 in [temp.arg.nontype]:
12006 -- an integral constant-expression of integral or enumeration
12009 -- the name of a non-type template-parameter; or
12011 -- the name of an object or function with external linkage...
12013 -- the address of an object or function with external linkage...
12015 -- a pointer to member... */
12016 /* Look for a non-type template parameter. */
12017 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12019 cp_parser_parse_tentatively (parser);
12020 argument = cp_parser_primary_expression (parser,
12021 /*address_p=*/false,
12023 /*template_arg_p=*/true,
12025 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12026 || !cp_parser_next_token_ends_template_argument_p (parser))
12027 cp_parser_simulate_error (parser);
12028 if (cp_parser_parse_definitely (parser))
12032 /* If the next token is "&", the argument must be the address of an
12033 object or function with external linkage. */
12034 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12036 cp_lexer_consume_token (parser->lexer);
12037 /* See if we might have an id-expression. */
12038 token = cp_lexer_peek_token (parser->lexer);
12039 if (token->type == CPP_NAME
12040 || token->keyword == RID_OPERATOR
12041 || token->type == CPP_SCOPE
12042 || token->type == CPP_TEMPLATE_ID
12043 || token->type == CPP_NESTED_NAME_SPECIFIER)
12045 cp_parser_parse_tentatively (parser);
12046 argument = cp_parser_primary_expression (parser,
12049 /*template_arg_p=*/true,
12051 if (cp_parser_error_occurred (parser)
12052 || !cp_parser_next_token_ends_template_argument_p (parser))
12053 cp_parser_abort_tentative_parse (parser);
12058 if (TREE_CODE (argument) == INDIRECT_REF)
12060 gcc_assert (REFERENCE_REF_P (argument));
12061 argument = TREE_OPERAND (argument, 0);
12064 /* If we're in a template, we represent a qualified-id referring
12065 to a static data member as a SCOPE_REF even if the scope isn't
12066 dependent so that we can check access control later. */
12068 if (TREE_CODE (probe) == SCOPE_REF)
12069 probe = TREE_OPERAND (probe, 1);
12070 if (TREE_CODE (probe) == VAR_DECL)
12072 /* A variable without external linkage might still be a
12073 valid constant-expression, so no error is issued here
12074 if the external-linkage check fails. */
12075 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12076 cp_parser_simulate_error (parser);
12078 else if (is_overloaded_fn (argument))
12079 /* All overloaded functions are allowed; if the external
12080 linkage test does not pass, an error will be issued
12084 && (TREE_CODE (argument) == OFFSET_REF
12085 || TREE_CODE (argument) == SCOPE_REF))
12086 /* A pointer-to-member. */
12088 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12091 cp_parser_simulate_error (parser);
12093 if (cp_parser_parse_definitely (parser))
12096 argument = build_x_unary_op (ADDR_EXPR, argument,
12097 tf_warning_or_error);
12102 /* If the argument started with "&", there are no other valid
12103 alternatives at this point. */
12106 cp_parser_error (parser, "invalid non-type template argument");
12107 return error_mark_node;
12110 /* If the argument wasn't successfully parsed as a type-id followed
12111 by '>>', the argument can only be a constant expression now.
12112 Otherwise, we try parsing the constant-expression tentatively,
12113 because the argument could really be a type-id. */
12115 cp_parser_parse_tentatively (parser);
12116 argument = cp_parser_constant_expression (parser,
12117 /*allow_non_constant_p=*/false,
12118 /*non_constant_p=*/NULL);
12119 argument = fold_non_dependent_expr (argument);
12120 if (!maybe_type_id)
12122 if (!cp_parser_next_token_ends_template_argument_p (parser))
12123 cp_parser_error (parser, "expected template-argument");
12124 if (cp_parser_parse_definitely (parser))
12126 /* We did our best to parse the argument as a non type-id, but that
12127 was the only alternative that matched (albeit with a '>' after
12128 it). We can assume it's just a typo from the user, and a
12129 diagnostic will then be issued. */
12130 return cp_parser_template_type_arg (parser);
12133 /* Parse an explicit-instantiation.
12135 explicit-instantiation:
12136 template declaration
12138 Although the standard says `declaration', what it really means is:
12140 explicit-instantiation:
12141 template decl-specifier-seq [opt] declarator [opt] ;
12143 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12144 supposed to be allowed. A defect report has been filed about this
12149 explicit-instantiation:
12150 storage-class-specifier template
12151 decl-specifier-seq [opt] declarator [opt] ;
12152 function-specifier template
12153 decl-specifier-seq [opt] declarator [opt] ; */
12156 cp_parser_explicit_instantiation (cp_parser* parser)
12158 int declares_class_or_enum;
12159 cp_decl_specifier_seq decl_specifiers;
12160 tree extension_specifier = NULL_TREE;
12162 /* Look for an (optional) storage-class-specifier or
12163 function-specifier. */
12164 if (cp_parser_allow_gnu_extensions_p (parser))
12166 extension_specifier
12167 = cp_parser_storage_class_specifier_opt (parser);
12168 if (!extension_specifier)
12169 extension_specifier
12170 = cp_parser_function_specifier_opt (parser,
12171 /*decl_specs=*/NULL);
12174 /* Look for the `template' keyword. */
12175 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12176 /* Let the front end know that we are processing an explicit
12178 begin_explicit_instantiation ();
12179 /* [temp.explicit] says that we are supposed to ignore access
12180 control while processing explicit instantiation directives. */
12181 push_deferring_access_checks (dk_no_check);
12182 /* Parse a decl-specifier-seq. */
12183 cp_parser_decl_specifier_seq (parser,
12184 CP_PARSER_FLAGS_OPTIONAL,
12186 &declares_class_or_enum);
12187 /* If there was exactly one decl-specifier, and it declared a class,
12188 and there's no declarator, then we have an explicit type
12190 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12194 type = check_tag_decl (&decl_specifiers);
12195 /* Turn access control back on for names used during
12196 template instantiation. */
12197 pop_deferring_access_checks ();
12199 do_type_instantiation (type, extension_specifier,
12200 /*complain=*/tf_error);
12204 cp_declarator *declarator;
12207 /* Parse the declarator. */
12209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12210 /*ctor_dtor_or_conv_p=*/NULL,
12211 /*parenthesized_p=*/NULL,
12212 /*member_p=*/false);
12213 if (declares_class_or_enum & 2)
12214 cp_parser_check_for_definition_in_return_type (declarator,
12215 decl_specifiers.type,
12216 decl_specifiers.type_location);
12217 if (declarator != cp_error_declarator)
12219 decl = grokdeclarator (declarator, &decl_specifiers,
12220 NORMAL, 0, &decl_specifiers.attributes);
12221 /* Turn access control back on for names used during
12222 template instantiation. */
12223 pop_deferring_access_checks ();
12224 /* Do the explicit instantiation. */
12225 do_decl_instantiation (decl, extension_specifier);
12229 pop_deferring_access_checks ();
12230 /* Skip the body of the explicit instantiation. */
12231 cp_parser_skip_to_end_of_statement (parser);
12234 /* We're done with the instantiation. */
12235 end_explicit_instantiation ();
12237 cp_parser_consume_semicolon_at_end_of_statement (parser);
12240 /* Parse an explicit-specialization.
12242 explicit-specialization:
12243 template < > declaration
12245 Although the standard says `declaration', what it really means is:
12247 explicit-specialization:
12248 template <> decl-specifier [opt] init-declarator [opt] ;
12249 template <> function-definition
12250 template <> explicit-specialization
12251 template <> template-declaration */
12254 cp_parser_explicit_specialization (cp_parser* parser)
12256 bool need_lang_pop;
12257 cp_token *token = cp_lexer_peek_token (parser->lexer);
12259 /* Look for the `template' keyword. */
12260 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12261 /* Look for the `<'. */
12262 cp_parser_require (parser, CPP_LESS, RT_LESS);
12263 /* Look for the `>'. */
12264 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12265 /* We have processed another parameter list. */
12266 ++parser->num_template_parameter_lists;
12269 A template ... explicit specialization ... shall not have C
12271 if (current_lang_name == lang_name_c)
12273 error_at (token->location, "template specialization with C linkage");
12274 /* Give it C++ linkage to avoid confusing other parts of the
12276 push_lang_context (lang_name_cplusplus);
12277 need_lang_pop = true;
12280 need_lang_pop = false;
12281 /* Let the front end know that we are beginning a specialization. */
12282 if (!begin_specialization ())
12284 end_specialization ();
12288 /* If the next keyword is `template', we need to figure out whether
12289 or not we're looking a template-declaration. */
12290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12292 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12293 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12294 cp_parser_template_declaration_after_export (parser,
12295 /*member_p=*/false);
12297 cp_parser_explicit_specialization (parser);
12300 /* Parse the dependent declaration. */
12301 cp_parser_single_declaration (parser,
12303 /*member_p=*/false,
12304 /*explicit_specialization_p=*/true,
12305 /*friend_p=*/NULL);
12306 /* We're done with the specialization. */
12307 end_specialization ();
12308 /* For the erroneous case of a template with C linkage, we pushed an
12309 implicit C++ linkage scope; exit that scope now. */
12311 pop_lang_context ();
12312 /* We're done with this parameter list. */
12313 --parser->num_template_parameter_lists;
12316 /* Parse a type-specifier.
12319 simple-type-specifier
12322 elaborated-type-specifier
12330 Returns a representation of the type-specifier. For a
12331 class-specifier, enum-specifier, or elaborated-type-specifier, a
12332 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12334 The parser flags FLAGS is used to control type-specifier parsing.
12336 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12337 in a decl-specifier-seq.
12339 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12340 class-specifier, enum-specifier, or elaborated-type-specifier, then
12341 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12342 if a type is declared; 2 if it is defined. Otherwise, it is set to
12345 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12346 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12347 is set to FALSE. */
12350 cp_parser_type_specifier (cp_parser* parser,
12351 cp_parser_flags flags,
12352 cp_decl_specifier_seq *decl_specs,
12353 bool is_declaration,
12354 int* declares_class_or_enum,
12355 bool* is_cv_qualifier)
12357 tree type_spec = NULL_TREE;
12360 cp_decl_spec ds = ds_last;
12362 /* Assume this type-specifier does not declare a new type. */
12363 if (declares_class_or_enum)
12364 *declares_class_or_enum = 0;
12365 /* And that it does not specify a cv-qualifier. */
12366 if (is_cv_qualifier)
12367 *is_cv_qualifier = false;
12368 /* Peek at the next token. */
12369 token = cp_lexer_peek_token (parser->lexer);
12371 /* If we're looking at a keyword, we can use that to guide the
12372 production we choose. */
12373 keyword = token->keyword;
12377 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12378 goto elaborated_type_specifier;
12380 /* Look for the enum-specifier. */
12381 type_spec = cp_parser_enum_specifier (parser);
12382 /* If that worked, we're done. */
12385 if (declares_class_or_enum)
12386 *declares_class_or_enum = 2;
12388 cp_parser_set_decl_spec_type (decl_specs,
12391 /*user_defined_p=*/true);
12395 goto elaborated_type_specifier;
12397 /* Any of these indicate either a class-specifier, or an
12398 elaborated-type-specifier. */
12402 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12403 goto elaborated_type_specifier;
12405 /* Parse tentatively so that we can back up if we don't find a
12406 class-specifier. */
12407 cp_parser_parse_tentatively (parser);
12408 /* Look for the class-specifier. */
12409 type_spec = cp_parser_class_specifier (parser);
12410 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12411 /* If that worked, we're done. */
12412 if (cp_parser_parse_definitely (parser))
12414 if (declares_class_or_enum)
12415 *declares_class_or_enum = 2;
12417 cp_parser_set_decl_spec_type (decl_specs,
12420 /*user_defined_p=*/true);
12424 /* Fall through. */
12425 elaborated_type_specifier:
12426 /* We're declaring (not defining) a class or enum. */
12427 if (declares_class_or_enum)
12428 *declares_class_or_enum = 1;
12430 /* Fall through. */
12432 /* Look for an elaborated-type-specifier. */
12434 = (cp_parser_elaborated_type_specifier
12436 decl_specs && decl_specs->specs[(int) ds_friend],
12439 cp_parser_set_decl_spec_type (decl_specs,
12442 /*user_defined_p=*/true);
12447 if (is_cv_qualifier)
12448 *is_cv_qualifier = true;
12453 if (is_cv_qualifier)
12454 *is_cv_qualifier = true;
12459 if (is_cv_qualifier)
12460 *is_cv_qualifier = true;
12464 /* The `__complex__' keyword is a GNU extension. */
12472 /* Handle simple keywords. */
12477 ++decl_specs->specs[(int)ds];
12478 decl_specs->any_specifiers_p = true;
12480 return cp_lexer_consume_token (parser->lexer)->u.value;
12483 /* If we do not already have a type-specifier, assume we are looking
12484 at a simple-type-specifier. */
12485 type_spec = cp_parser_simple_type_specifier (parser,
12489 /* If we didn't find a type-specifier, and a type-specifier was not
12490 optional in this context, issue an error message. */
12491 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12493 cp_parser_error (parser, "expected type specifier");
12494 return error_mark_node;
12500 /* Parse a simple-type-specifier.
12502 simple-type-specifier:
12503 :: [opt] nested-name-specifier [opt] type-name
12504 :: [opt] nested-name-specifier template template-id
12519 simple-type-specifier:
12521 decltype ( expression )
12527 simple-type-specifier:
12529 __typeof__ unary-expression
12530 __typeof__ ( type-id )
12532 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12533 appropriately updated. */
12536 cp_parser_simple_type_specifier (cp_parser* parser,
12537 cp_decl_specifier_seq *decl_specs,
12538 cp_parser_flags flags)
12540 tree type = NULL_TREE;
12543 /* Peek at the next token. */
12544 token = cp_lexer_peek_token (parser->lexer);
12546 /* If we're looking at a keyword, things are easy. */
12547 switch (token->keyword)
12551 decl_specs->explicit_char_p = true;
12552 type = char_type_node;
12555 type = char16_type_node;
12558 type = char32_type_node;
12561 type = wchar_type_node;
12564 type = boolean_type_node;
12568 ++decl_specs->specs[(int) ds_short];
12569 type = short_integer_type_node;
12573 decl_specs->explicit_int_p = true;
12574 type = integer_type_node;
12577 if (!int128_integer_type_node)
12580 decl_specs->explicit_int128_p = true;
12581 type = int128_integer_type_node;
12585 ++decl_specs->specs[(int) ds_long];
12586 type = long_integer_type_node;
12590 ++decl_specs->specs[(int) ds_signed];
12591 type = integer_type_node;
12595 ++decl_specs->specs[(int) ds_unsigned];
12596 type = unsigned_type_node;
12599 type = float_type_node;
12602 type = double_type_node;
12605 type = void_type_node;
12609 maybe_warn_cpp0x (CPP0X_AUTO);
12610 type = make_auto ();
12614 /* Parse the `decltype' type. */
12615 type = cp_parser_decltype (parser);
12618 cp_parser_set_decl_spec_type (decl_specs, type,
12620 /*user_defined_p=*/true);
12625 /* Consume the `typeof' token. */
12626 cp_lexer_consume_token (parser->lexer);
12627 /* Parse the operand to `typeof'. */
12628 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12629 /* If it is not already a TYPE, take its type. */
12630 if (!TYPE_P (type))
12631 type = finish_typeof (type);
12634 cp_parser_set_decl_spec_type (decl_specs, type,
12636 /*user_defined_p=*/true);
12644 /* If the type-specifier was for a built-in type, we're done. */
12647 /* Record the type. */
12649 && (token->keyword != RID_SIGNED
12650 && token->keyword != RID_UNSIGNED
12651 && token->keyword != RID_SHORT
12652 && token->keyword != RID_LONG))
12653 cp_parser_set_decl_spec_type (decl_specs,
12656 /*user_defined=*/false);
12658 decl_specs->any_specifiers_p = true;
12660 /* Consume the token. */
12661 cp_lexer_consume_token (parser->lexer);
12663 /* There is no valid C++ program where a non-template type is
12664 followed by a "<". That usually indicates that the user thought
12665 that the type was a template. */
12666 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12668 return TYPE_NAME (type);
12671 /* The type-specifier must be a user-defined type. */
12672 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12677 /* Don't gobble tokens or issue error messages if this is an
12678 optional type-specifier. */
12679 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12680 cp_parser_parse_tentatively (parser);
12682 /* Look for the optional `::' operator. */
12684 = (cp_parser_global_scope_opt (parser,
12685 /*current_scope_valid_p=*/false)
12687 /* Look for the nested-name specifier. */
12689 = (cp_parser_nested_name_specifier_opt (parser,
12690 /*typename_keyword_p=*/false,
12691 /*check_dependency_p=*/true,
12693 /*is_declaration=*/false)
12695 token = cp_lexer_peek_token (parser->lexer);
12696 /* If we have seen a nested-name-specifier, and the next token
12697 is `template', then we are using the template-id production. */
12699 && cp_parser_optional_template_keyword (parser))
12701 /* Look for the template-id. */
12702 type = cp_parser_template_id (parser,
12703 /*template_keyword_p=*/true,
12704 /*check_dependency_p=*/true,
12705 /*is_declaration=*/false);
12706 /* If the template-id did not name a type, we are out of
12708 if (TREE_CODE (type) != TYPE_DECL)
12710 cp_parser_error (parser, "expected template-id for type");
12714 /* Otherwise, look for a type-name. */
12716 type = cp_parser_type_name (parser);
12717 /* Keep track of all name-lookups performed in class scopes. */
12721 && TREE_CODE (type) == TYPE_DECL
12722 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12723 maybe_note_name_used_in_class (DECL_NAME (type), type);
12724 /* If it didn't work out, we don't have a TYPE. */
12725 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12726 && !cp_parser_parse_definitely (parser))
12728 if (type && decl_specs)
12729 cp_parser_set_decl_spec_type (decl_specs, type,
12731 /*user_defined=*/true);
12734 /* If we didn't get a type-name, issue an error message. */
12735 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12737 cp_parser_error (parser, "expected type-name");
12738 return error_mark_node;
12741 /* There is no valid C++ program where a non-template type is
12742 followed by a "<". That usually indicates that the user thought
12743 that the type was a template. */
12744 if (type && type != error_mark_node)
12746 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12747 If it is, then the '<'...'>' enclose protocol names rather than
12748 template arguments, and so everything is fine. */
12749 if (c_dialect_objc ()
12750 && (objc_is_id (type) || objc_is_class_name (type)))
12752 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12753 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12755 /* Clobber the "unqualified" type previously entered into
12756 DECL_SPECS with the new, improved protocol-qualified version. */
12758 decl_specs->type = qual_type;
12763 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12770 /* Parse a type-name.
12783 Returns a TYPE_DECL for the type. */
12786 cp_parser_type_name (cp_parser* parser)
12790 /* We can't know yet whether it is a class-name or not. */
12791 cp_parser_parse_tentatively (parser);
12792 /* Try a class-name. */
12793 type_decl = cp_parser_class_name (parser,
12794 /*typename_keyword_p=*/false,
12795 /*template_keyword_p=*/false,
12797 /*check_dependency_p=*/true,
12798 /*class_head_p=*/false,
12799 /*is_declaration=*/false);
12800 /* If it's not a class-name, keep looking. */
12801 if (!cp_parser_parse_definitely (parser))
12803 /* It must be a typedef-name or an enum-name. */
12804 return cp_parser_nonclass_name (parser);
12810 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12818 Returns a TYPE_DECL for the type. */
12821 cp_parser_nonclass_name (cp_parser* parser)
12826 cp_token *token = cp_lexer_peek_token (parser->lexer);
12827 identifier = cp_parser_identifier (parser);
12828 if (identifier == error_mark_node)
12829 return error_mark_node;
12831 /* Look up the type-name. */
12832 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12834 if (TREE_CODE (type_decl) != TYPE_DECL
12835 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12837 /* See if this is an Objective-C type. */
12838 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12839 tree type = objc_get_protocol_qualified_type (identifier, protos);
12841 type_decl = TYPE_NAME (type);
12844 /* Issue an error if we did not find a type-name. */
12845 if (TREE_CODE (type_decl) != TYPE_DECL)
12847 if (!cp_parser_simulate_error (parser))
12848 cp_parser_name_lookup_error (parser, identifier, type_decl,
12849 NLE_TYPE, token->location);
12850 return error_mark_node;
12852 /* Remember that the name was used in the definition of the
12853 current class so that we can check later to see if the
12854 meaning would have been different after the class was
12855 entirely defined. */
12856 else if (type_decl != error_mark_node
12858 maybe_note_name_used_in_class (identifier, type_decl);
12863 /* Parse an elaborated-type-specifier. Note that the grammar given
12864 here incorporates the resolution to DR68.
12866 elaborated-type-specifier:
12867 class-key :: [opt] nested-name-specifier [opt] identifier
12868 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12869 enum-key :: [opt] nested-name-specifier [opt] identifier
12870 typename :: [opt] nested-name-specifier identifier
12871 typename :: [opt] nested-name-specifier template [opt]
12876 elaborated-type-specifier:
12877 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12878 class-key attributes :: [opt] nested-name-specifier [opt]
12879 template [opt] template-id
12880 enum attributes :: [opt] nested-name-specifier [opt] identifier
12882 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12883 declared `friend'. If IS_DECLARATION is TRUE, then this
12884 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12885 something is being declared.
12887 Returns the TYPE specified. */
12890 cp_parser_elaborated_type_specifier (cp_parser* parser,
12892 bool is_declaration)
12894 enum tag_types tag_type;
12896 tree type = NULL_TREE;
12897 tree attributes = NULL_TREE;
12899 cp_token *token = NULL;
12901 /* See if we're looking at the `enum' keyword. */
12902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12904 /* Consume the `enum' token. */
12905 cp_lexer_consume_token (parser->lexer);
12906 /* Remember that it's an enumeration type. */
12907 tag_type = enum_type;
12908 /* Parse the optional `struct' or `class' key (for C++0x scoped
12910 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12911 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12913 if (cxx_dialect == cxx98)
12914 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12916 /* Consume the `struct' or `class'. */
12917 cp_lexer_consume_token (parser->lexer);
12919 /* Parse the attributes. */
12920 attributes = cp_parser_attributes_opt (parser);
12922 /* Or, it might be `typename'. */
12923 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12926 /* Consume the `typename' token. */
12927 cp_lexer_consume_token (parser->lexer);
12928 /* Remember that it's a `typename' type. */
12929 tag_type = typename_type;
12931 /* Otherwise it must be a class-key. */
12934 tag_type = cp_parser_class_key (parser);
12935 if (tag_type == none_type)
12936 return error_mark_node;
12937 /* Parse the attributes. */
12938 attributes = cp_parser_attributes_opt (parser);
12941 /* Look for the `::' operator. */
12942 globalscope = cp_parser_global_scope_opt (parser,
12943 /*current_scope_valid_p=*/false);
12944 /* Look for the nested-name-specifier. */
12945 if (tag_type == typename_type && !globalscope)
12947 if (!cp_parser_nested_name_specifier (parser,
12948 /*typename_keyword_p=*/true,
12949 /*check_dependency_p=*/true,
12952 return error_mark_node;
12955 /* Even though `typename' is not present, the proposed resolution
12956 to Core Issue 180 says that in `class A<T>::B', `B' should be
12957 considered a type-name, even if `A<T>' is dependent. */
12958 cp_parser_nested_name_specifier_opt (parser,
12959 /*typename_keyword_p=*/true,
12960 /*check_dependency_p=*/true,
12963 /* For everything but enumeration types, consider a template-id.
12964 For an enumeration type, consider only a plain identifier. */
12965 if (tag_type != enum_type)
12967 bool template_p = false;
12970 /* Allow the `template' keyword. */
12971 template_p = cp_parser_optional_template_keyword (parser);
12972 /* If we didn't see `template', we don't know if there's a
12973 template-id or not. */
12975 cp_parser_parse_tentatively (parser);
12976 /* Parse the template-id. */
12977 token = cp_lexer_peek_token (parser->lexer);
12978 decl = cp_parser_template_id (parser, template_p,
12979 /*check_dependency_p=*/true,
12981 /* If we didn't find a template-id, look for an ordinary
12983 if (!template_p && !cp_parser_parse_definitely (parser))
12985 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12986 in effect, then we must assume that, upon instantiation, the
12987 template will correspond to a class. */
12988 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12989 && tag_type == typename_type)
12990 type = make_typename_type (parser->scope, decl,
12992 /*complain=*/tf_error);
12993 /* If the `typename' keyword is in effect and DECL is not a type
12994 decl. Then type is non existant. */
12995 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12998 type = TREE_TYPE (decl);
13003 token = cp_lexer_peek_token (parser->lexer);
13004 identifier = cp_parser_identifier (parser);
13006 if (identifier == error_mark_node)
13008 parser->scope = NULL_TREE;
13009 return error_mark_node;
13012 /* For a `typename', we needn't call xref_tag. */
13013 if (tag_type == typename_type
13014 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13015 return cp_parser_make_typename_type (parser, parser->scope,
13018 /* Look up a qualified name in the usual way. */
13022 tree ambiguous_decls;
13024 decl = cp_parser_lookup_name (parser, identifier,
13026 /*is_template=*/false,
13027 /*is_namespace=*/false,
13028 /*check_dependency=*/true,
13032 /* If the lookup was ambiguous, an error will already have been
13034 if (ambiguous_decls)
13035 return error_mark_node;
13037 /* If we are parsing friend declaration, DECL may be a
13038 TEMPLATE_DECL tree node here. However, we need to check
13039 whether this TEMPLATE_DECL results in valid code. Consider
13040 the following example:
13043 template <class T> class C {};
13046 template <class T> friend class N::C; // #1, valid code
13048 template <class T> class Y {
13049 friend class N::C; // #2, invalid code
13052 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13053 name lookup of `N::C'. We see that friend declaration must
13054 be template for the code to be valid. Note that
13055 processing_template_decl does not work here since it is
13056 always 1 for the above two cases. */
13058 decl = (cp_parser_maybe_treat_template_as_class
13059 (decl, /*tag_name_p=*/is_friend
13060 && parser->num_template_parameter_lists));
13062 if (TREE_CODE (decl) != TYPE_DECL)
13064 cp_parser_diagnose_invalid_type_name (parser,
13068 return error_mark_node;
13071 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13073 bool allow_template = (parser->num_template_parameter_lists
13074 || DECL_SELF_REFERENCE_P (decl));
13075 type = check_elaborated_type_specifier (tag_type, decl,
13078 if (type == error_mark_node)
13079 return error_mark_node;
13082 /* Forward declarations of nested types, such as
13087 are invalid unless all components preceding the final '::'
13088 are complete. If all enclosing types are complete, these
13089 declarations become merely pointless.
13091 Invalid forward declarations of nested types are errors
13092 caught elsewhere in parsing. Those that are pointless arrive
13095 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13096 && !is_friend && !processing_explicit_instantiation)
13097 warning (0, "declaration %qD does not declare anything", decl);
13099 type = TREE_TYPE (decl);
13103 /* An elaborated-type-specifier sometimes introduces a new type and
13104 sometimes names an existing type. Normally, the rule is that it
13105 introduces a new type only if there is not an existing type of
13106 the same name already in scope. For example, given:
13109 void f() { struct S s; }
13111 the `struct S' in the body of `f' is the same `struct S' as in
13112 the global scope; the existing definition is used. However, if
13113 there were no global declaration, this would introduce a new
13114 local class named `S'.
13116 An exception to this rule applies to the following code:
13118 namespace N { struct S; }
13120 Here, the elaborated-type-specifier names a new type
13121 unconditionally; even if there is already an `S' in the
13122 containing scope this declaration names a new type.
13123 This exception only applies if the elaborated-type-specifier
13124 forms the complete declaration:
13128 A declaration consisting solely of `class-key identifier ;' is
13129 either a redeclaration of the name in the current scope or a
13130 forward declaration of the identifier as a class name. It
13131 introduces the name into the current scope.
13133 We are in this situation precisely when the next token is a `;'.
13135 An exception to the exception is that a `friend' declaration does
13136 *not* name a new type; i.e., given:
13138 struct S { friend struct T; };
13140 `T' is not a new type in the scope of `S'.
13142 Also, `new struct S' or `sizeof (struct S)' never results in the
13143 definition of a new type; a new type can only be declared in a
13144 declaration context. */
13150 /* Friends have special name lookup rules. */
13151 ts = ts_within_enclosing_non_class;
13152 else if (is_declaration
13153 && cp_lexer_next_token_is (parser->lexer,
13155 /* This is a `class-key identifier ;' */
13161 (parser->num_template_parameter_lists
13162 && (cp_parser_next_token_starts_class_definition_p (parser)
13163 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13164 /* An unqualified name was used to reference this type, so
13165 there were no qualifying templates. */
13166 if (!cp_parser_check_template_parameters (parser,
13167 /*num_templates=*/0,
13169 /*declarator=*/NULL))
13170 return error_mark_node;
13171 type = xref_tag (tag_type, identifier, ts, template_p);
13175 if (type == error_mark_node)
13176 return error_mark_node;
13178 /* Allow attributes on forward declarations of classes. */
13181 if (TREE_CODE (type) == TYPENAME_TYPE)
13182 warning (OPT_Wattributes,
13183 "attributes ignored on uninstantiated type");
13184 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13185 && ! processing_explicit_instantiation)
13186 warning (OPT_Wattributes,
13187 "attributes ignored on template instantiation");
13188 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13189 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13191 warning (OPT_Wattributes,
13192 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13195 if (tag_type != enum_type)
13196 cp_parser_check_class_key (tag_type, type);
13198 /* A "<" cannot follow an elaborated type specifier. If that
13199 happens, the user was probably trying to form a template-id. */
13200 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13205 /* Parse an enum-specifier.
13208 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13213 enum struct [C++0x]
13216 : type-specifier-seq
13219 enum-key attributes[opt] identifier [opt] enum-base [opt]
13220 { enumerator-list [opt] }attributes[opt]
13222 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13223 if the token stream isn't an enum-specifier after all. */
13226 cp_parser_enum_specifier (cp_parser* parser)
13231 bool scoped_enum_p = false;
13232 bool has_underlying_type = false;
13233 tree underlying_type = NULL_TREE;
13235 /* Parse tentatively so that we can back up if we don't find a
13237 cp_parser_parse_tentatively (parser);
13239 /* Caller guarantees that the current token is 'enum', an identifier
13240 possibly follows, and the token after that is an opening brace.
13241 If we don't have an identifier, fabricate an anonymous name for
13242 the enumeration being defined. */
13243 cp_lexer_consume_token (parser->lexer);
13245 /* Parse the "class" or "struct", which indicates a scoped
13246 enumeration type in C++0x. */
13247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13248 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13250 if (cxx_dialect == cxx98)
13251 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13253 /* Consume the `struct' or `class' token. */
13254 cp_lexer_consume_token (parser->lexer);
13256 scoped_enum_p = true;
13259 attributes = cp_parser_attributes_opt (parser);
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13262 identifier = cp_parser_identifier (parser);
13264 identifier = make_anon_name ();
13266 /* Check for the `:' that denotes a specified underlying type in C++0x.
13267 Note that a ':' could also indicate a bitfield width, however. */
13268 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13270 cp_decl_specifier_seq type_specifiers;
13272 /* Consume the `:'. */
13273 cp_lexer_consume_token (parser->lexer);
13275 /* Parse the type-specifier-seq. */
13276 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13277 /*is_trailing_return=*/false,
13280 /* At this point this is surely not elaborated type specifier. */
13281 if (!cp_parser_parse_definitely (parser))
13284 if (cxx_dialect == cxx98)
13285 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13287 has_underlying_type = true;
13289 /* If that didn't work, stop. */
13290 if (type_specifiers.type != error_mark_node)
13292 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13293 /*initialized=*/0, NULL);
13294 if (underlying_type == error_mark_node)
13295 underlying_type = NULL_TREE;
13299 /* Look for the `{' but don't consume it yet. */
13300 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13302 cp_parser_error (parser, "expected %<{%>");
13303 if (has_underlying_type)
13307 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13310 /* Issue an error message if type-definitions are forbidden here. */
13311 if (!cp_parser_check_type_definition (parser))
13312 type = error_mark_node;
13314 /* Create the new type. We do this before consuming the opening
13315 brace so the enum will be recorded as being on the line of its
13316 tag (or the 'enum' keyword, if there is no tag). */
13317 type = start_enum (identifier, underlying_type, scoped_enum_p);
13319 /* Consume the opening brace. */
13320 cp_lexer_consume_token (parser->lexer);
13322 if (type == error_mark_node)
13324 cp_parser_skip_to_end_of_block_or_statement (parser);
13325 return error_mark_node;
13328 /* If the next token is not '}', then there are some enumerators. */
13329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13330 cp_parser_enumerator_list (parser, type);
13332 /* Consume the final '}'. */
13333 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13335 /* Look for trailing attributes to apply to this enumeration, and
13336 apply them if appropriate. */
13337 if (cp_parser_allow_gnu_extensions_p (parser))
13339 tree trailing_attr = cp_parser_attributes_opt (parser);
13340 trailing_attr = chainon (trailing_attr, attributes);
13341 cplus_decl_attributes (&type,
13343 (int) ATTR_FLAG_TYPE_IN_PLACE);
13346 /* Finish up the enumeration. */
13347 finish_enum (type);
13352 /* Parse an enumerator-list. The enumerators all have the indicated
13356 enumerator-definition
13357 enumerator-list , enumerator-definition */
13360 cp_parser_enumerator_list (cp_parser* parser, tree type)
13364 /* Parse an enumerator-definition. */
13365 cp_parser_enumerator_definition (parser, type);
13367 /* If the next token is not a ',', we've reached the end of
13369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13371 /* Otherwise, consume the `,' and keep going. */
13372 cp_lexer_consume_token (parser->lexer);
13373 /* If the next token is a `}', there is a trailing comma. */
13374 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13376 if (!in_system_header)
13377 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13383 /* Parse an enumerator-definition. The enumerator has the indicated
13386 enumerator-definition:
13388 enumerator = constant-expression
13394 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13400 /* Save the input location because we are interested in the location
13401 of the identifier and not the location of the explicit value. */
13402 loc = cp_lexer_peek_token (parser->lexer)->location;
13404 /* Look for the identifier. */
13405 identifier = cp_parser_identifier (parser);
13406 if (identifier == error_mark_node)
13409 /* If the next token is an '=', then there is an explicit value. */
13410 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13412 /* Consume the `=' token. */
13413 cp_lexer_consume_token (parser->lexer);
13414 /* Parse the value. */
13415 value = cp_parser_constant_expression (parser,
13416 /*allow_non_constant_p=*/false,
13422 /* If we are processing a template, make sure the initializer of the
13423 enumerator doesn't contain any bare template parameter pack. */
13424 if (check_for_bare_parameter_packs (value))
13425 value = error_mark_node;
13427 /* Create the enumerator. */
13428 build_enumerator (identifier, value, type, loc);
13431 /* Parse a namespace-name.
13434 original-namespace-name
13437 Returns the NAMESPACE_DECL for the namespace. */
13440 cp_parser_namespace_name (cp_parser* parser)
13443 tree namespace_decl;
13445 cp_token *token = cp_lexer_peek_token (parser->lexer);
13447 /* Get the name of the namespace. */
13448 identifier = cp_parser_identifier (parser);
13449 if (identifier == error_mark_node)
13450 return error_mark_node;
13452 /* Look up the identifier in the currently active scope. Look only
13453 for namespaces, due to:
13455 [basic.lookup.udir]
13457 When looking up a namespace-name in a using-directive or alias
13458 definition, only namespace names are considered.
13462 [basic.lookup.qual]
13464 During the lookup of a name preceding the :: scope resolution
13465 operator, object, function, and enumerator names are ignored.
13467 (Note that cp_parser_qualifying_entity only calls this
13468 function if the token after the name is the scope resolution
13470 namespace_decl = cp_parser_lookup_name (parser, identifier,
13472 /*is_template=*/false,
13473 /*is_namespace=*/true,
13474 /*check_dependency=*/true,
13475 /*ambiguous_decls=*/NULL,
13477 /* If it's not a namespace, issue an error. */
13478 if (namespace_decl == error_mark_node
13479 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13481 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13482 error_at (token->location, "%qD is not a namespace-name", identifier);
13483 cp_parser_error (parser, "expected namespace-name");
13484 namespace_decl = error_mark_node;
13487 return namespace_decl;
13490 /* Parse a namespace-definition.
13492 namespace-definition:
13493 named-namespace-definition
13494 unnamed-namespace-definition
13496 named-namespace-definition:
13497 original-namespace-definition
13498 extension-namespace-definition
13500 original-namespace-definition:
13501 namespace identifier { namespace-body }
13503 extension-namespace-definition:
13504 namespace original-namespace-name { namespace-body }
13506 unnamed-namespace-definition:
13507 namespace { namespace-body } */
13510 cp_parser_namespace_definition (cp_parser* parser)
13512 tree identifier, attribs;
13513 bool has_visibility;
13516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13518 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13520 cp_lexer_consume_token (parser->lexer);
13525 /* Look for the `namespace' keyword. */
13526 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13528 /* Get the name of the namespace. We do not attempt to distinguish
13529 between an original-namespace-definition and an
13530 extension-namespace-definition at this point. The semantic
13531 analysis routines are responsible for that. */
13532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13533 identifier = cp_parser_identifier (parser);
13535 identifier = NULL_TREE;
13537 /* Parse any specified attributes. */
13538 attribs = cp_parser_attributes_opt (parser);
13540 /* Look for the `{' to start the namespace. */
13541 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13542 /* Start the namespace. */
13543 push_namespace (identifier);
13545 /* "inline namespace" is equivalent to a stub namespace definition
13546 followed by a strong using directive. */
13549 tree name_space = current_namespace;
13550 /* Set up namespace association. */
13551 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13552 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13553 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13554 /* Import the contents of the inline namespace. */
13556 do_using_directive (name_space);
13557 push_namespace (identifier);
13560 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13562 /* Parse the body of the namespace. */
13563 cp_parser_namespace_body (parser);
13565 #ifdef HANDLE_PRAGMA_VISIBILITY
13566 if (has_visibility)
13567 pop_visibility (1);
13570 /* Finish the namespace. */
13572 /* Look for the final `}'. */
13573 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13576 /* Parse a namespace-body.
13579 declaration-seq [opt] */
13582 cp_parser_namespace_body (cp_parser* parser)
13584 cp_parser_declaration_seq_opt (parser);
13587 /* Parse a namespace-alias-definition.
13589 namespace-alias-definition:
13590 namespace identifier = qualified-namespace-specifier ; */
13593 cp_parser_namespace_alias_definition (cp_parser* parser)
13596 tree namespace_specifier;
13598 cp_token *token = cp_lexer_peek_token (parser->lexer);
13600 /* Look for the `namespace' keyword. */
13601 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13602 /* Look for the identifier. */
13603 identifier = cp_parser_identifier (parser);
13604 if (identifier == error_mark_node)
13606 /* Look for the `=' token. */
13607 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13608 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13610 error_at (token->location, "%<namespace%> definition is not allowed here");
13611 /* Skip the definition. */
13612 cp_lexer_consume_token (parser->lexer);
13613 if (cp_parser_skip_to_closing_brace (parser))
13614 cp_lexer_consume_token (parser->lexer);
13617 cp_parser_require (parser, CPP_EQ, RT_EQ);
13618 /* Look for the qualified-namespace-specifier. */
13619 namespace_specifier
13620 = cp_parser_qualified_namespace_specifier (parser);
13621 /* Look for the `;' token. */
13622 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13624 /* Register the alias in the symbol table. */
13625 do_namespace_alias (identifier, namespace_specifier);
13628 /* Parse a qualified-namespace-specifier.
13630 qualified-namespace-specifier:
13631 :: [opt] nested-name-specifier [opt] namespace-name
13633 Returns a NAMESPACE_DECL corresponding to the specified
13637 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13639 /* Look for the optional `::'. */
13640 cp_parser_global_scope_opt (parser,
13641 /*current_scope_valid_p=*/false);
13643 /* Look for the optional nested-name-specifier. */
13644 cp_parser_nested_name_specifier_opt (parser,
13645 /*typename_keyword_p=*/false,
13646 /*check_dependency_p=*/true,
13648 /*is_declaration=*/true);
13650 return cp_parser_namespace_name (parser);
13653 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13654 access declaration.
13657 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13658 using :: unqualified-id ;
13660 access-declaration:
13666 cp_parser_using_declaration (cp_parser* parser,
13667 bool access_declaration_p)
13670 bool typename_p = false;
13671 bool global_scope_p;
13676 if (access_declaration_p)
13677 cp_parser_parse_tentatively (parser);
13680 /* Look for the `using' keyword. */
13681 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13683 /* Peek at the next token. */
13684 token = cp_lexer_peek_token (parser->lexer);
13685 /* See if it's `typename'. */
13686 if (token->keyword == RID_TYPENAME)
13688 /* Remember that we've seen it. */
13690 /* Consume the `typename' token. */
13691 cp_lexer_consume_token (parser->lexer);
13695 /* Look for the optional global scope qualification. */
13697 = (cp_parser_global_scope_opt (parser,
13698 /*current_scope_valid_p=*/false)
13701 /* If we saw `typename', or didn't see `::', then there must be a
13702 nested-name-specifier present. */
13703 if (typename_p || !global_scope_p)
13704 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13705 /*check_dependency_p=*/true,
13707 /*is_declaration=*/true);
13708 /* Otherwise, we could be in either of the two productions. In that
13709 case, treat the nested-name-specifier as optional. */
13711 qscope = cp_parser_nested_name_specifier_opt (parser,
13712 /*typename_keyword_p=*/false,
13713 /*check_dependency_p=*/true,
13715 /*is_declaration=*/true);
13717 qscope = global_namespace;
13719 if (access_declaration_p && cp_parser_error_occurred (parser))
13720 /* Something has already gone wrong; there's no need to parse
13721 further. Since an error has occurred, the return value of
13722 cp_parser_parse_definitely will be false, as required. */
13723 return cp_parser_parse_definitely (parser);
13725 token = cp_lexer_peek_token (parser->lexer);
13726 /* Parse the unqualified-id. */
13727 identifier = cp_parser_unqualified_id (parser,
13728 /*template_keyword_p=*/false,
13729 /*check_dependency_p=*/true,
13730 /*declarator_p=*/true,
13731 /*optional_p=*/false);
13733 if (access_declaration_p)
13735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13736 cp_parser_simulate_error (parser);
13737 if (!cp_parser_parse_definitely (parser))
13741 /* The function we call to handle a using-declaration is different
13742 depending on what scope we are in. */
13743 if (qscope == error_mark_node || identifier == error_mark_node)
13745 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13746 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13747 /* [namespace.udecl]
13749 A using declaration shall not name a template-id. */
13750 error_at (token->location,
13751 "a template-id may not appear in a using-declaration");
13754 if (at_class_scope_p ())
13756 /* Create the USING_DECL. */
13757 decl = do_class_using_decl (parser->scope, identifier);
13759 if (check_for_bare_parameter_packs (decl))
13762 /* Add it to the list of members in this class. */
13763 finish_member_declaration (decl);
13767 decl = cp_parser_lookup_name_simple (parser,
13770 if (decl == error_mark_node)
13771 cp_parser_name_lookup_error (parser, identifier,
13774 else if (check_for_bare_parameter_packs (decl))
13776 else if (!at_namespace_scope_p ())
13777 do_local_using_decl (decl, qscope, identifier);
13779 do_toplevel_using_decl (decl, qscope, identifier);
13783 /* Look for the final `;'. */
13784 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13789 /* Parse a using-directive.
13792 using namespace :: [opt] nested-name-specifier [opt]
13793 namespace-name ; */
13796 cp_parser_using_directive (cp_parser* parser)
13798 tree namespace_decl;
13801 /* Look for the `using' keyword. */
13802 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13803 /* And the `namespace' keyword. */
13804 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13805 /* Look for the optional `::' operator. */
13806 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13807 /* And the optional nested-name-specifier. */
13808 cp_parser_nested_name_specifier_opt (parser,
13809 /*typename_keyword_p=*/false,
13810 /*check_dependency_p=*/true,
13812 /*is_declaration=*/true);
13813 /* Get the namespace being used. */
13814 namespace_decl = cp_parser_namespace_name (parser);
13815 /* And any specified attributes. */
13816 attribs = cp_parser_attributes_opt (parser);
13817 /* Update the symbol table. */
13818 parse_using_directive (namespace_decl, attribs);
13819 /* Look for the final `;'. */
13820 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13823 /* Parse an asm-definition.
13826 asm ( string-literal ) ;
13831 asm volatile [opt] ( string-literal ) ;
13832 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13833 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13834 : asm-operand-list [opt] ) ;
13835 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13836 : asm-operand-list [opt]
13837 : asm-clobber-list [opt] ) ;
13838 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13839 : asm-clobber-list [opt]
13840 : asm-goto-list ) ; */
13843 cp_parser_asm_definition (cp_parser* parser)
13846 tree outputs = NULL_TREE;
13847 tree inputs = NULL_TREE;
13848 tree clobbers = NULL_TREE;
13849 tree labels = NULL_TREE;
13851 bool volatile_p = false;
13852 bool extended_p = false;
13853 bool invalid_inputs_p = false;
13854 bool invalid_outputs_p = false;
13855 bool goto_p = false;
13856 required_token missing = RT_NONE;
13858 /* Look for the `asm' keyword. */
13859 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13860 /* See if the next token is `volatile'. */
13861 if (cp_parser_allow_gnu_extensions_p (parser)
13862 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13864 /* Remember that we saw the `volatile' keyword. */
13866 /* Consume the token. */
13867 cp_lexer_consume_token (parser->lexer);
13869 if (cp_parser_allow_gnu_extensions_p (parser)
13870 && parser->in_function_body
13871 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13873 /* Remember that we saw the `goto' keyword. */
13875 /* Consume the token. */
13876 cp_lexer_consume_token (parser->lexer);
13878 /* Look for the opening `('. */
13879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13881 /* Look for the string. */
13882 string = cp_parser_string_literal (parser, false, false);
13883 if (string == error_mark_node)
13885 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13886 /*consume_paren=*/true);
13890 /* If we're allowing GNU extensions, check for the extended assembly
13891 syntax. Unfortunately, the `:' tokens need not be separated by
13892 a space in C, and so, for compatibility, we tolerate that here
13893 too. Doing that means that we have to treat the `::' operator as
13895 if (cp_parser_allow_gnu_extensions_p (parser)
13896 && parser->in_function_body
13897 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13898 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13900 bool inputs_p = false;
13901 bool clobbers_p = false;
13902 bool labels_p = false;
13904 /* The extended syntax was used. */
13907 /* Look for outputs. */
13908 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13910 /* Consume the `:'. */
13911 cp_lexer_consume_token (parser->lexer);
13912 /* Parse the output-operands. */
13913 if (cp_lexer_next_token_is_not (parser->lexer,
13915 && cp_lexer_next_token_is_not (parser->lexer,
13917 && cp_lexer_next_token_is_not (parser->lexer,
13920 outputs = cp_parser_asm_operand_list (parser);
13922 if (outputs == error_mark_node)
13923 invalid_outputs_p = true;
13925 /* If the next token is `::', there are no outputs, and the
13926 next token is the beginning of the inputs. */
13927 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13928 /* The inputs are coming next. */
13931 /* Look for inputs. */
13933 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13935 /* Consume the `:' or `::'. */
13936 cp_lexer_consume_token (parser->lexer);
13937 /* Parse the output-operands. */
13938 if (cp_lexer_next_token_is_not (parser->lexer,
13940 && cp_lexer_next_token_is_not (parser->lexer,
13942 && cp_lexer_next_token_is_not (parser->lexer,
13944 inputs = cp_parser_asm_operand_list (parser);
13946 if (inputs == error_mark_node)
13947 invalid_inputs_p = true;
13949 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13950 /* The clobbers are coming next. */
13953 /* Look for clobbers. */
13955 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13958 /* Consume the `:' or `::'. */
13959 cp_lexer_consume_token (parser->lexer);
13960 /* Parse the clobbers. */
13961 if (cp_lexer_next_token_is_not (parser->lexer,
13963 && cp_lexer_next_token_is_not (parser->lexer,
13965 clobbers = cp_parser_asm_clobber_list (parser);
13968 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13969 /* The labels are coming next. */
13972 /* Look for labels. */
13974 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13977 /* Consume the `:' or `::'. */
13978 cp_lexer_consume_token (parser->lexer);
13979 /* Parse the labels. */
13980 labels = cp_parser_asm_label_list (parser);
13983 if (goto_p && !labels_p)
13984 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
13987 missing = RT_COLON_SCOPE;
13989 /* Look for the closing `)'. */
13990 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13991 missing ? missing : RT_CLOSE_PAREN))
13992 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13993 /*consume_paren=*/true);
13994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13996 if (!invalid_inputs_p && !invalid_outputs_p)
13998 /* Create the ASM_EXPR. */
13999 if (parser->in_function_body)
14001 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14002 inputs, clobbers, labels);
14003 /* If the extended syntax was not used, mark the ASM_EXPR. */
14006 tree temp = asm_stmt;
14007 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14008 temp = TREE_OPERAND (temp, 0);
14010 ASM_INPUT_P (temp) = 1;
14014 cgraph_add_asm_node (string);
14018 /* Declarators [gram.dcl.decl] */
14020 /* Parse an init-declarator.
14023 declarator initializer [opt]
14028 declarator asm-specification [opt] attributes [opt] initializer [opt]
14030 function-definition:
14031 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14033 decl-specifier-seq [opt] declarator function-try-block
14037 function-definition:
14038 __extension__ function-definition
14040 The DECL_SPECIFIERS apply to this declarator. Returns a
14041 representation of the entity declared. If MEMBER_P is TRUE, then
14042 this declarator appears in a class scope. The new DECL created by
14043 this declarator is returned.
14045 The CHECKS are access checks that should be performed once we know
14046 what entity is being declared (and, therefore, what classes have
14049 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14050 for a function-definition here as well. If the declarator is a
14051 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14052 be TRUE upon return. By that point, the function-definition will
14053 have been completely parsed.
14055 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14059 cp_parser_init_declarator (cp_parser* parser,
14060 cp_decl_specifier_seq *decl_specifiers,
14061 VEC (deferred_access_check,gc)* checks,
14062 bool function_definition_allowed_p,
14064 int declares_class_or_enum,
14065 bool* function_definition_p)
14067 cp_token *token = NULL, *asm_spec_start_token = NULL,
14068 *attributes_start_token = NULL;
14069 cp_declarator *declarator;
14070 tree prefix_attributes;
14072 tree asm_specification;
14074 tree decl = NULL_TREE;
14076 int is_initialized;
14077 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14078 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14080 enum cpp_ttype initialization_kind;
14081 bool is_direct_init = false;
14082 bool is_non_constant_init;
14083 int ctor_dtor_or_conv_p;
14085 tree pushed_scope = NULL;
14087 /* Gather the attributes that were provided with the
14088 decl-specifiers. */
14089 prefix_attributes = decl_specifiers->attributes;
14091 /* Assume that this is not the declarator for a function
14093 if (function_definition_p)
14094 *function_definition_p = false;
14096 /* Defer access checks while parsing the declarator; we cannot know
14097 what names are accessible until we know what is being
14099 resume_deferring_access_checks ();
14101 /* Parse the declarator. */
14102 token = cp_lexer_peek_token (parser->lexer);
14104 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14105 &ctor_dtor_or_conv_p,
14106 /*parenthesized_p=*/NULL,
14107 /*member_p=*/false);
14108 /* Gather up the deferred checks. */
14109 stop_deferring_access_checks ();
14111 /* If the DECLARATOR was erroneous, there's no need to go
14113 if (declarator == cp_error_declarator)
14114 return error_mark_node;
14116 /* Check that the number of template-parameter-lists is OK. */
14117 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14119 return error_mark_node;
14121 if (declares_class_or_enum & 2)
14122 cp_parser_check_for_definition_in_return_type (declarator,
14123 decl_specifiers->type,
14124 decl_specifiers->type_location);
14126 /* Figure out what scope the entity declared by the DECLARATOR is
14127 located in. `grokdeclarator' sometimes changes the scope, so
14128 we compute it now. */
14129 scope = get_scope_of_declarator (declarator);
14131 /* Perform any lookups in the declared type which were thought to be
14132 dependent, but are not in the scope of the declarator. */
14133 decl_specifiers->type
14134 = maybe_update_decl_type (decl_specifiers->type, scope);
14136 /* If we're allowing GNU extensions, look for an asm-specification
14138 if (cp_parser_allow_gnu_extensions_p (parser))
14140 /* Look for an asm-specification. */
14141 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14142 asm_specification = cp_parser_asm_specification_opt (parser);
14143 /* And attributes. */
14144 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14145 attributes = cp_parser_attributes_opt (parser);
14149 asm_specification = NULL_TREE;
14150 attributes = NULL_TREE;
14153 /* Peek at the next token. */
14154 token = cp_lexer_peek_token (parser->lexer);
14155 /* Check to see if the token indicates the start of a
14156 function-definition. */
14157 if (function_declarator_p (declarator)
14158 && cp_parser_token_starts_function_definition_p (token))
14160 if (!function_definition_allowed_p)
14162 /* If a function-definition should not appear here, issue an
14164 cp_parser_error (parser,
14165 "a function-definition is not allowed here");
14166 return error_mark_node;
14170 location_t func_brace_location
14171 = cp_lexer_peek_token (parser->lexer)->location;
14173 /* Neither attributes nor an asm-specification are allowed
14174 on a function-definition. */
14175 if (asm_specification)
14176 error_at (asm_spec_start_token->location,
14177 "an asm-specification is not allowed "
14178 "on a function-definition");
14180 error_at (attributes_start_token->location,
14181 "attributes are not allowed on a function-definition");
14182 /* This is a function-definition. */
14183 *function_definition_p = true;
14185 /* Parse the function definition. */
14187 decl = cp_parser_save_member_function_body (parser,
14190 prefix_attributes);
14193 = (cp_parser_function_definition_from_specifiers_and_declarator
14194 (parser, decl_specifiers, prefix_attributes, declarator));
14196 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14198 /* This is where the prologue starts... */
14199 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14200 = func_brace_location;
14209 Only in function declarations for constructors, destructors, and
14210 type conversions can the decl-specifier-seq be omitted.
14212 We explicitly postpone this check past the point where we handle
14213 function-definitions because we tolerate function-definitions
14214 that are missing their return types in some modes. */
14215 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14217 cp_parser_error (parser,
14218 "expected constructor, destructor, or type conversion");
14219 return error_mark_node;
14222 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14223 if (token->type == CPP_EQ
14224 || token->type == CPP_OPEN_PAREN
14225 || token->type == CPP_OPEN_BRACE)
14227 is_initialized = SD_INITIALIZED;
14228 initialization_kind = token->type;
14230 if (token->type == CPP_EQ
14231 && function_declarator_p (declarator))
14233 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14234 if (t2->keyword == RID_DEFAULT)
14235 is_initialized = SD_DEFAULTED;
14236 else if (t2->keyword == RID_DELETE)
14237 is_initialized = SD_DELETED;
14242 /* If the init-declarator isn't initialized and isn't followed by a
14243 `,' or `;', it's not a valid init-declarator. */
14244 if (token->type != CPP_COMMA
14245 && token->type != CPP_SEMICOLON)
14247 cp_parser_error (parser, "expected initializer");
14248 return error_mark_node;
14250 is_initialized = SD_UNINITIALIZED;
14251 initialization_kind = CPP_EOF;
14254 /* Because start_decl has side-effects, we should only call it if we
14255 know we're going ahead. By this point, we know that we cannot
14256 possibly be looking at any other construct. */
14257 cp_parser_commit_to_tentative_parse (parser);
14259 /* If the decl specifiers were bad, issue an error now that we're
14260 sure this was intended to be a declarator. Then continue
14261 declaring the variable(s), as int, to try to cut down on further
14263 if (decl_specifiers->any_specifiers_p
14264 && decl_specifiers->type == error_mark_node)
14266 cp_parser_error (parser, "invalid type in declaration");
14267 decl_specifiers->type = integer_type_node;
14270 /* Check to see whether or not this declaration is a friend. */
14271 friend_p = cp_parser_friend_p (decl_specifiers);
14273 /* Enter the newly declared entry in the symbol table. If we're
14274 processing a declaration in a class-specifier, we wait until
14275 after processing the initializer. */
14278 if (parser->in_unbraced_linkage_specification_p)
14279 decl_specifiers->storage_class = sc_extern;
14280 decl = start_decl (declarator, decl_specifiers,
14281 is_initialized, attributes, prefix_attributes,
14283 /* Adjust location of decl if declarator->id_loc is more appropriate:
14284 set, and decl wasn't merged with another decl, in which case its
14285 location would be different from input_location, and more accurate. */
14287 && declarator->id_loc != UNKNOWN_LOCATION
14288 && DECL_SOURCE_LOCATION (decl) == input_location)
14289 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14292 /* Enter the SCOPE. That way unqualified names appearing in the
14293 initializer will be looked up in SCOPE. */
14294 pushed_scope = push_scope (scope);
14296 /* Perform deferred access control checks, now that we know in which
14297 SCOPE the declared entity resides. */
14298 if (!member_p && decl)
14300 tree saved_current_function_decl = NULL_TREE;
14302 /* If the entity being declared is a function, pretend that we
14303 are in its scope. If it is a `friend', it may have access to
14304 things that would not otherwise be accessible. */
14305 if (TREE_CODE (decl) == FUNCTION_DECL)
14307 saved_current_function_decl = current_function_decl;
14308 current_function_decl = decl;
14311 /* Perform access checks for template parameters. */
14312 cp_parser_perform_template_parameter_access_checks (checks);
14314 /* Perform the access control checks for the declarator and the
14315 decl-specifiers. */
14316 perform_deferred_access_checks ();
14318 /* Restore the saved value. */
14319 if (TREE_CODE (decl) == FUNCTION_DECL)
14320 current_function_decl = saved_current_function_decl;
14323 /* Parse the initializer. */
14324 initializer = NULL_TREE;
14325 is_direct_init = false;
14326 is_non_constant_init = true;
14327 if (is_initialized)
14329 if (function_declarator_p (declarator))
14331 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14332 if (initialization_kind == CPP_EQ)
14333 initializer = cp_parser_pure_specifier (parser);
14336 /* If the declaration was erroneous, we don't really
14337 know what the user intended, so just silently
14338 consume the initializer. */
14339 if (decl != error_mark_node)
14340 error_at (initializer_start_token->location,
14341 "initializer provided for function");
14342 cp_parser_skip_to_closing_parenthesis (parser,
14343 /*recovering=*/true,
14344 /*or_comma=*/false,
14345 /*consume_paren=*/true);
14350 /* We want to record the extra mangling scope for in-class
14351 initializers of class members and initializers of static data
14352 member templates. The former is a C++0x feature which isn't
14353 implemented yet, and I expect it will involve deferring
14354 parsing of the initializer until end of class as with default
14355 arguments. So right here we only handle the latter. */
14356 if (!member_p && processing_template_decl)
14357 start_lambda_scope (decl);
14358 initializer = cp_parser_initializer (parser,
14360 &is_non_constant_init);
14361 if (!member_p && processing_template_decl)
14362 finish_lambda_scope ();
14366 /* The old parser allows attributes to appear after a parenthesized
14367 initializer. Mark Mitchell proposed removing this functionality
14368 on the GCC mailing lists on 2002-08-13. This parser accepts the
14369 attributes -- but ignores them. */
14370 if (cp_parser_allow_gnu_extensions_p (parser)
14371 && initialization_kind == CPP_OPEN_PAREN)
14372 if (cp_parser_attributes_opt (parser))
14373 warning (OPT_Wattributes,
14374 "attributes after parenthesized initializer ignored");
14376 /* For an in-class declaration, use `grokfield' to create the
14382 pop_scope (pushed_scope);
14383 pushed_scope = false;
14385 decl = grokfield (declarator, decl_specifiers,
14386 initializer, !is_non_constant_init,
14387 /*asmspec=*/NULL_TREE,
14388 prefix_attributes);
14389 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14390 cp_parser_save_default_args (parser, decl);
14393 /* Finish processing the declaration. But, skip friend
14395 if (!friend_p && decl && decl != error_mark_node)
14397 cp_finish_decl (decl,
14398 initializer, !is_non_constant_init,
14400 /* If the initializer is in parentheses, then this is
14401 a direct-initialization, which means that an
14402 `explicit' constructor is OK. Otherwise, an
14403 `explicit' constructor cannot be used. */
14404 ((is_direct_init || !is_initialized)
14405 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14407 else if ((cxx_dialect != cxx98) && friend_p
14408 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14409 /* Core issue #226 (C++0x only): A default template-argument
14410 shall not be specified in a friend class template
14412 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14413 /*is_partial=*/0, /*is_friend_decl=*/1);
14415 if (!friend_p && pushed_scope)
14416 pop_scope (pushed_scope);
14421 /* Parse a declarator.
14425 ptr-operator declarator
14427 abstract-declarator:
14428 ptr-operator abstract-declarator [opt]
14429 direct-abstract-declarator
14434 attributes [opt] direct-declarator
14435 attributes [opt] ptr-operator declarator
14437 abstract-declarator:
14438 attributes [opt] ptr-operator abstract-declarator [opt]
14439 attributes [opt] direct-abstract-declarator
14441 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14442 detect constructor, destructor or conversion operators. It is set
14443 to -1 if the declarator is a name, and +1 if it is a
14444 function. Otherwise it is set to zero. Usually you just want to
14445 test for >0, but internally the negative value is used.
14447 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14448 a decl-specifier-seq unless it declares a constructor, destructor,
14449 or conversion. It might seem that we could check this condition in
14450 semantic analysis, rather than parsing, but that makes it difficult
14451 to handle something like `f()'. We want to notice that there are
14452 no decl-specifiers, and therefore realize that this is an
14453 expression, not a declaration.)
14455 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14456 the declarator is a direct-declarator of the form "(...)".
14458 MEMBER_P is true iff this declarator is a member-declarator. */
14460 static cp_declarator *
14461 cp_parser_declarator (cp_parser* parser,
14462 cp_parser_declarator_kind dcl_kind,
14463 int* ctor_dtor_or_conv_p,
14464 bool* parenthesized_p,
14467 cp_declarator *declarator;
14468 enum tree_code code;
14469 cp_cv_quals cv_quals;
14471 tree attributes = NULL_TREE;
14473 /* Assume this is not a constructor, destructor, or type-conversion
14475 if (ctor_dtor_or_conv_p)
14476 *ctor_dtor_or_conv_p = 0;
14478 if (cp_parser_allow_gnu_extensions_p (parser))
14479 attributes = cp_parser_attributes_opt (parser);
14481 /* Check for the ptr-operator production. */
14482 cp_parser_parse_tentatively (parser);
14483 /* Parse the ptr-operator. */
14484 code = cp_parser_ptr_operator (parser,
14487 /* If that worked, then we have a ptr-operator. */
14488 if (cp_parser_parse_definitely (parser))
14490 /* If a ptr-operator was found, then this declarator was not
14492 if (parenthesized_p)
14493 *parenthesized_p = true;
14494 /* The dependent declarator is optional if we are parsing an
14495 abstract-declarator. */
14496 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14497 cp_parser_parse_tentatively (parser);
14499 /* Parse the dependent declarator. */
14500 declarator = cp_parser_declarator (parser, dcl_kind,
14501 /*ctor_dtor_or_conv_p=*/NULL,
14502 /*parenthesized_p=*/NULL,
14503 /*member_p=*/false);
14505 /* If we are parsing an abstract-declarator, we must handle the
14506 case where the dependent declarator is absent. */
14507 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14508 && !cp_parser_parse_definitely (parser))
14511 declarator = cp_parser_make_indirect_declarator
14512 (code, class_type, cv_quals, declarator);
14514 /* Everything else is a direct-declarator. */
14517 if (parenthesized_p)
14518 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14520 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14521 ctor_dtor_or_conv_p,
14525 if (attributes && declarator && declarator != cp_error_declarator)
14526 declarator->attributes = attributes;
14531 /* Parse a direct-declarator or direct-abstract-declarator.
14535 direct-declarator ( parameter-declaration-clause )
14536 cv-qualifier-seq [opt]
14537 exception-specification [opt]
14538 direct-declarator [ constant-expression [opt] ]
14541 direct-abstract-declarator:
14542 direct-abstract-declarator [opt]
14543 ( parameter-declaration-clause )
14544 cv-qualifier-seq [opt]
14545 exception-specification [opt]
14546 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14547 ( abstract-declarator )
14549 Returns a representation of the declarator. DCL_KIND is
14550 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14551 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14552 we are parsing a direct-declarator. It is
14553 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14554 of ambiguity we prefer an abstract declarator, as per
14555 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14556 cp_parser_declarator. */
14558 static cp_declarator *
14559 cp_parser_direct_declarator (cp_parser* parser,
14560 cp_parser_declarator_kind dcl_kind,
14561 int* ctor_dtor_or_conv_p,
14565 cp_declarator *declarator = NULL;
14566 tree scope = NULL_TREE;
14567 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14568 bool saved_in_declarator_p = parser->in_declarator_p;
14570 tree pushed_scope = NULL_TREE;
14574 /* Peek at the next token. */
14575 token = cp_lexer_peek_token (parser->lexer);
14576 if (token->type == CPP_OPEN_PAREN)
14578 /* This is either a parameter-declaration-clause, or a
14579 parenthesized declarator. When we know we are parsing a
14580 named declarator, it must be a parenthesized declarator
14581 if FIRST is true. For instance, `(int)' is a
14582 parameter-declaration-clause, with an omitted
14583 direct-abstract-declarator. But `((*))', is a
14584 parenthesized abstract declarator. Finally, when T is a
14585 template parameter `(T)' is a
14586 parameter-declaration-clause, and not a parenthesized
14589 We first try and parse a parameter-declaration-clause,
14590 and then try a nested declarator (if FIRST is true).
14592 It is not an error for it not to be a
14593 parameter-declaration-clause, even when FIRST is
14599 The first is the declaration of a function while the
14600 second is the definition of a variable, including its
14603 Having seen only the parenthesis, we cannot know which of
14604 these two alternatives should be selected. Even more
14605 complex are examples like:
14610 The former is a function-declaration; the latter is a
14611 variable initialization.
14613 Thus again, we try a parameter-declaration-clause, and if
14614 that fails, we back out and return. */
14616 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14619 unsigned saved_num_template_parameter_lists;
14620 bool is_declarator = false;
14623 /* In a member-declarator, the only valid interpretation
14624 of a parenthesis is the start of a
14625 parameter-declaration-clause. (It is invalid to
14626 initialize a static data member with a parenthesized
14627 initializer; only the "=" form of initialization is
14630 cp_parser_parse_tentatively (parser);
14632 /* Consume the `('. */
14633 cp_lexer_consume_token (parser->lexer);
14636 /* If this is going to be an abstract declarator, we're
14637 in a declarator and we can't have default args. */
14638 parser->default_arg_ok_p = false;
14639 parser->in_declarator_p = true;
14642 /* Inside the function parameter list, surrounding
14643 template-parameter-lists do not apply. */
14644 saved_num_template_parameter_lists
14645 = parser->num_template_parameter_lists;
14646 parser->num_template_parameter_lists = 0;
14648 begin_scope (sk_function_parms, NULL_TREE);
14650 /* Parse the parameter-declaration-clause. */
14651 params = cp_parser_parameter_declaration_clause (parser);
14653 parser->num_template_parameter_lists
14654 = saved_num_template_parameter_lists;
14656 /* If all went well, parse the cv-qualifier-seq and the
14657 exception-specification. */
14658 if (member_p || cp_parser_parse_definitely (parser))
14660 cp_cv_quals cv_quals;
14661 tree exception_specification;
14664 is_declarator = true;
14666 if (ctor_dtor_or_conv_p)
14667 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14669 /* Consume the `)'. */
14670 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14672 /* Parse the cv-qualifier-seq. */
14673 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14674 /* And the exception-specification. */
14675 exception_specification
14676 = cp_parser_exception_specification_opt (parser);
14679 = cp_parser_late_return_type_opt (parser);
14681 /* Create the function-declarator. */
14682 declarator = make_call_declarator (declarator,
14685 exception_specification,
14687 /* Any subsequent parameter lists are to do with
14688 return type, so are not those of the declared
14690 parser->default_arg_ok_p = false;
14693 /* Remove the function parms from scope. */
14694 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14695 pop_binding (DECL_NAME (t), t);
14699 /* Repeat the main loop. */
14703 /* If this is the first, we can try a parenthesized
14707 bool saved_in_type_id_in_expr_p;
14709 parser->default_arg_ok_p = saved_default_arg_ok_p;
14710 parser->in_declarator_p = saved_in_declarator_p;
14712 /* Consume the `('. */
14713 cp_lexer_consume_token (parser->lexer);
14714 /* Parse the nested declarator. */
14715 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14716 parser->in_type_id_in_expr_p = true;
14718 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14719 /*parenthesized_p=*/NULL,
14721 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14723 /* Expect a `)'. */
14724 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14725 declarator = cp_error_declarator;
14726 if (declarator == cp_error_declarator)
14729 goto handle_declarator;
14731 /* Otherwise, we must be done. */
14735 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14736 && token->type == CPP_OPEN_SQUARE)
14738 /* Parse an array-declarator. */
14741 if (ctor_dtor_or_conv_p)
14742 *ctor_dtor_or_conv_p = 0;
14745 parser->default_arg_ok_p = false;
14746 parser->in_declarator_p = true;
14747 /* Consume the `['. */
14748 cp_lexer_consume_token (parser->lexer);
14749 /* Peek at the next token. */
14750 token = cp_lexer_peek_token (parser->lexer);
14751 /* If the next token is `]', then there is no
14752 constant-expression. */
14753 if (token->type != CPP_CLOSE_SQUARE)
14755 bool non_constant_p;
14758 = cp_parser_constant_expression (parser,
14759 /*allow_non_constant=*/true,
14761 if (!non_constant_p)
14762 bounds = fold_non_dependent_expr (bounds);
14763 /* Normally, the array bound must be an integral constant
14764 expression. However, as an extension, we allow VLAs
14765 in function scopes as long as they aren't part of a
14766 parameter declaration. */
14767 else if (!parser->in_function_body
14768 || current_binding_level->kind == sk_function_parms)
14770 cp_parser_error (parser,
14771 "array bound is not an integer constant");
14772 bounds = error_mark_node;
14774 else if (processing_template_decl && !error_operand_p (bounds))
14776 /* Remember this wasn't a constant-expression. */
14777 bounds = build_nop (TREE_TYPE (bounds), bounds);
14778 TREE_SIDE_EFFECTS (bounds) = 1;
14782 bounds = NULL_TREE;
14783 /* Look for the closing `]'. */
14784 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14786 declarator = cp_error_declarator;
14790 declarator = make_array_declarator (declarator, bounds);
14792 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14795 tree qualifying_scope;
14796 tree unqualified_name;
14797 special_function_kind sfk;
14799 bool pack_expansion_p = false;
14800 cp_token *declarator_id_start_token;
14802 /* Parse a declarator-id */
14803 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14806 cp_parser_parse_tentatively (parser);
14808 /* If we see an ellipsis, we should be looking at a
14810 if (token->type == CPP_ELLIPSIS)
14812 /* Consume the `...' */
14813 cp_lexer_consume_token (parser->lexer);
14815 pack_expansion_p = true;
14819 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14821 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14822 qualifying_scope = parser->scope;
14827 if (!unqualified_name && pack_expansion_p)
14829 /* Check whether an error occurred. */
14830 okay = !cp_parser_error_occurred (parser);
14832 /* We already consumed the ellipsis to mark a
14833 parameter pack, but we have no way to report it,
14834 so abort the tentative parse. We will be exiting
14835 immediately anyway. */
14836 cp_parser_abort_tentative_parse (parser);
14839 okay = cp_parser_parse_definitely (parser);
14842 unqualified_name = error_mark_node;
14843 else if (unqualified_name
14844 && (qualifying_scope
14845 || (TREE_CODE (unqualified_name)
14846 != IDENTIFIER_NODE)))
14848 cp_parser_error (parser, "expected unqualified-id");
14849 unqualified_name = error_mark_node;
14853 if (!unqualified_name)
14855 if (unqualified_name == error_mark_node)
14857 declarator = cp_error_declarator;
14858 pack_expansion_p = false;
14859 declarator->parameter_pack_p = false;
14863 if (qualifying_scope && at_namespace_scope_p ()
14864 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14866 /* In the declaration of a member of a template class
14867 outside of the class itself, the SCOPE will sometimes
14868 be a TYPENAME_TYPE. For example, given:
14870 template <typename T>
14871 int S<T>::R::i = 3;
14873 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14874 this context, we must resolve S<T>::R to an ordinary
14875 type, rather than a typename type.
14877 The reason we normally avoid resolving TYPENAME_TYPEs
14878 is that a specialization of `S' might render
14879 `S<T>::R' not a type. However, if `S' is
14880 specialized, then this `i' will not be used, so there
14881 is no harm in resolving the types here. */
14884 /* Resolve the TYPENAME_TYPE. */
14885 type = resolve_typename_type (qualifying_scope,
14886 /*only_current_p=*/false);
14887 /* If that failed, the declarator is invalid. */
14888 if (TREE_CODE (type) == TYPENAME_TYPE)
14890 if (typedef_variant_p (type))
14891 error_at (declarator_id_start_token->location,
14892 "cannot define member of dependent typedef "
14895 error_at (declarator_id_start_token->location,
14896 "%<%T::%E%> is not a type",
14897 TYPE_CONTEXT (qualifying_scope),
14898 TYPE_IDENTIFIER (qualifying_scope));
14900 qualifying_scope = type;
14905 if (unqualified_name)
14909 if (qualifying_scope
14910 && CLASS_TYPE_P (qualifying_scope))
14911 class_type = qualifying_scope;
14913 class_type = current_class_type;
14915 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14917 tree name_type = TREE_TYPE (unqualified_name);
14918 if (class_type && same_type_p (name_type, class_type))
14920 if (qualifying_scope
14921 && CLASSTYPE_USE_TEMPLATE (name_type))
14923 error_at (declarator_id_start_token->location,
14924 "invalid use of constructor as a template");
14925 inform (declarator_id_start_token->location,
14926 "use %<%T::%D%> instead of %<%T::%D%> to "
14927 "name the constructor in a qualified name",
14929 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14930 class_type, name_type);
14931 declarator = cp_error_declarator;
14935 unqualified_name = constructor_name (class_type);
14939 /* We do not attempt to print the declarator
14940 here because we do not have enough
14941 information about its original syntactic
14943 cp_parser_error (parser, "invalid declarator");
14944 declarator = cp_error_declarator;
14951 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14952 sfk = sfk_destructor;
14953 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14954 sfk = sfk_conversion;
14955 else if (/* There's no way to declare a constructor
14956 for an anonymous type, even if the type
14957 got a name for linkage purposes. */
14958 !TYPE_WAS_ANONYMOUS (class_type)
14959 && constructor_name_p (unqualified_name,
14962 unqualified_name = constructor_name (class_type);
14963 sfk = sfk_constructor;
14965 else if (is_overloaded_fn (unqualified_name)
14966 && DECL_CONSTRUCTOR_P (get_first_fn
14967 (unqualified_name)))
14968 sfk = sfk_constructor;
14970 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14971 *ctor_dtor_or_conv_p = -1;
14974 declarator = make_id_declarator (qualifying_scope,
14977 declarator->id_loc = token->location;
14978 declarator->parameter_pack_p = pack_expansion_p;
14980 if (pack_expansion_p)
14981 maybe_warn_variadic_templates ();
14984 handle_declarator:;
14985 scope = get_scope_of_declarator (declarator);
14987 /* Any names that appear after the declarator-id for a
14988 member are looked up in the containing scope. */
14989 pushed_scope = push_scope (scope);
14990 parser->in_declarator_p = true;
14991 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14992 || (declarator && declarator->kind == cdk_id))
14993 /* Default args are only allowed on function
14995 parser->default_arg_ok_p = saved_default_arg_ok_p;
14997 parser->default_arg_ok_p = false;
15006 /* For an abstract declarator, we might wind up with nothing at this
15007 point. That's an error; the declarator is not optional. */
15009 cp_parser_error (parser, "expected declarator");
15011 /* If we entered a scope, we must exit it now. */
15013 pop_scope (pushed_scope);
15015 parser->default_arg_ok_p = saved_default_arg_ok_p;
15016 parser->in_declarator_p = saved_in_declarator_p;
15021 /* Parse a ptr-operator.
15024 * cv-qualifier-seq [opt]
15026 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15031 & cv-qualifier-seq [opt]
15033 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15034 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15035 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15036 filled in with the TYPE containing the member. *CV_QUALS is
15037 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15038 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15039 Note that the tree codes returned by this function have nothing
15040 to do with the types of trees that will be eventually be created
15041 to represent the pointer or reference type being parsed. They are
15042 just constants with suggestive names. */
15043 static enum tree_code
15044 cp_parser_ptr_operator (cp_parser* parser,
15046 cp_cv_quals *cv_quals)
15048 enum tree_code code = ERROR_MARK;
15051 /* Assume that it's not a pointer-to-member. */
15053 /* And that there are no cv-qualifiers. */
15054 *cv_quals = TYPE_UNQUALIFIED;
15056 /* Peek at the next token. */
15057 token = cp_lexer_peek_token (parser->lexer);
15059 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15060 if (token->type == CPP_MULT)
15061 code = INDIRECT_REF;
15062 else if (token->type == CPP_AND)
15064 else if ((cxx_dialect != cxx98) &&
15065 token->type == CPP_AND_AND) /* C++0x only */
15066 code = NON_LVALUE_EXPR;
15068 if (code != ERROR_MARK)
15070 /* Consume the `*', `&' or `&&'. */
15071 cp_lexer_consume_token (parser->lexer);
15073 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15074 `&', if we are allowing GNU extensions. (The only qualifier
15075 that can legally appear after `&' is `restrict', but that is
15076 enforced during semantic analysis. */
15077 if (code == INDIRECT_REF
15078 || cp_parser_allow_gnu_extensions_p (parser))
15079 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15083 /* Try the pointer-to-member case. */
15084 cp_parser_parse_tentatively (parser);
15085 /* Look for the optional `::' operator. */
15086 cp_parser_global_scope_opt (parser,
15087 /*current_scope_valid_p=*/false);
15088 /* Look for the nested-name specifier. */
15089 token = cp_lexer_peek_token (parser->lexer);
15090 cp_parser_nested_name_specifier (parser,
15091 /*typename_keyword_p=*/false,
15092 /*check_dependency_p=*/true,
15094 /*is_declaration=*/false);
15095 /* If we found it, and the next token is a `*', then we are
15096 indeed looking at a pointer-to-member operator. */
15097 if (!cp_parser_error_occurred (parser)
15098 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15100 /* Indicate that the `*' operator was used. */
15101 code = INDIRECT_REF;
15103 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15104 error_at (token->location, "%qD is a namespace", parser->scope);
15107 /* The type of which the member is a member is given by the
15109 *type = parser->scope;
15110 /* The next name will not be qualified. */
15111 parser->scope = NULL_TREE;
15112 parser->qualifying_scope = NULL_TREE;
15113 parser->object_scope = NULL_TREE;
15114 /* Look for the optional cv-qualifier-seq. */
15115 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15118 /* If that didn't work we don't have a ptr-operator. */
15119 if (!cp_parser_parse_definitely (parser))
15120 cp_parser_error (parser, "expected ptr-operator");
15126 /* Parse an (optional) cv-qualifier-seq.
15129 cv-qualifier cv-qualifier-seq [opt]
15140 Returns a bitmask representing the cv-qualifiers. */
15143 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15145 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15150 cp_cv_quals cv_qualifier;
15152 /* Peek at the next token. */
15153 token = cp_lexer_peek_token (parser->lexer);
15154 /* See if it's a cv-qualifier. */
15155 switch (token->keyword)
15158 cv_qualifier = TYPE_QUAL_CONST;
15162 cv_qualifier = TYPE_QUAL_VOLATILE;
15166 cv_qualifier = TYPE_QUAL_RESTRICT;
15170 cv_qualifier = TYPE_UNQUALIFIED;
15177 if (cv_quals & cv_qualifier)
15179 error_at (token->location, "duplicate cv-qualifier");
15180 cp_lexer_purge_token (parser->lexer);
15184 cp_lexer_consume_token (parser->lexer);
15185 cv_quals |= cv_qualifier;
15192 /* Parse a late-specified return type, if any. This is not a separate
15193 non-terminal, but part of a function declarator, which looks like
15195 -> trailing-type-specifier-seq abstract-declarator(opt)
15197 Returns the type indicated by the type-id. */
15200 cp_parser_late_return_type_opt (cp_parser* parser)
15204 /* Peek at the next token. */
15205 token = cp_lexer_peek_token (parser->lexer);
15206 /* A late-specified return type is indicated by an initial '->'. */
15207 if (token->type != CPP_DEREF)
15210 /* Consume the ->. */
15211 cp_lexer_consume_token (parser->lexer);
15213 return cp_parser_trailing_type_id (parser);
15216 /* Parse a declarator-id.
15220 :: [opt] nested-name-specifier [opt] type-name
15222 In the `id-expression' case, the value returned is as for
15223 cp_parser_id_expression if the id-expression was an unqualified-id.
15224 If the id-expression was a qualified-id, then a SCOPE_REF is
15225 returned. The first operand is the scope (either a NAMESPACE_DECL
15226 or TREE_TYPE), but the second is still just a representation of an
15230 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15233 /* The expression must be an id-expression. Assume that qualified
15234 names are the names of types so that:
15237 int S<T>::R::i = 3;
15239 will work; we must treat `S<T>::R' as the name of a type.
15240 Similarly, assume that qualified names are templates, where
15244 int S<T>::R<T>::i = 3;
15247 id = cp_parser_id_expression (parser,
15248 /*template_keyword_p=*/false,
15249 /*check_dependency_p=*/false,
15250 /*template_p=*/NULL,
15251 /*declarator_p=*/true,
15253 if (id && BASELINK_P (id))
15254 id = BASELINK_FUNCTIONS (id);
15258 /* Parse a type-id.
15261 type-specifier-seq abstract-declarator [opt]
15263 Returns the TYPE specified. */
15266 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15267 bool is_trailing_return)
15269 cp_decl_specifier_seq type_specifier_seq;
15270 cp_declarator *abstract_declarator;
15272 /* Parse the type-specifier-seq. */
15273 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15274 is_trailing_return,
15275 &type_specifier_seq);
15276 if (type_specifier_seq.type == error_mark_node)
15277 return error_mark_node;
15279 /* There might or might not be an abstract declarator. */
15280 cp_parser_parse_tentatively (parser);
15281 /* Look for the declarator. */
15282 abstract_declarator
15283 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15284 /*parenthesized_p=*/NULL,
15285 /*member_p=*/false);
15286 /* Check to see if there really was a declarator. */
15287 if (!cp_parser_parse_definitely (parser))
15288 abstract_declarator = NULL;
15290 if (type_specifier_seq.type
15291 && type_uses_auto (type_specifier_seq.type))
15293 /* A type-id with type 'auto' is only ok if the abstract declarator
15294 is a function declarator with a late-specified return type. */
15295 if (abstract_declarator
15296 && abstract_declarator->kind == cdk_function
15297 && abstract_declarator->u.function.late_return_type)
15301 error ("invalid use of %<auto%>");
15302 return error_mark_node;
15306 return groktypename (&type_specifier_seq, abstract_declarator,
15310 static tree cp_parser_type_id (cp_parser *parser)
15312 return cp_parser_type_id_1 (parser, false, false);
15315 static tree cp_parser_template_type_arg (cp_parser *parser)
15317 return cp_parser_type_id_1 (parser, true, false);
15320 static tree cp_parser_trailing_type_id (cp_parser *parser)
15322 return cp_parser_type_id_1 (parser, false, true);
15325 /* Parse a type-specifier-seq.
15327 type-specifier-seq:
15328 type-specifier type-specifier-seq [opt]
15332 type-specifier-seq:
15333 attributes type-specifier-seq [opt]
15335 If IS_DECLARATION is true, we are at the start of a "condition" or
15336 exception-declaration, so we might be followed by a declarator-id.
15338 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15339 i.e. we've just seen "->".
15341 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15344 cp_parser_type_specifier_seq (cp_parser* parser,
15345 bool is_declaration,
15346 bool is_trailing_return,
15347 cp_decl_specifier_seq *type_specifier_seq)
15349 bool seen_type_specifier = false;
15350 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15351 cp_token *start_token = NULL;
15353 /* Clear the TYPE_SPECIFIER_SEQ. */
15354 clear_decl_specs (type_specifier_seq);
15356 /* In the context of a trailing return type, enum E { } is an
15357 elaborated-type-specifier followed by a function-body, not an
15359 if (is_trailing_return)
15360 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15362 /* Parse the type-specifiers and attributes. */
15365 tree type_specifier;
15366 bool is_cv_qualifier;
15368 /* Check for attributes first. */
15369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15371 type_specifier_seq->attributes =
15372 chainon (type_specifier_seq->attributes,
15373 cp_parser_attributes_opt (parser));
15377 /* record the token of the beginning of the type specifier seq,
15378 for error reporting purposes*/
15380 start_token = cp_lexer_peek_token (parser->lexer);
15382 /* Look for the type-specifier. */
15383 type_specifier = cp_parser_type_specifier (parser,
15385 type_specifier_seq,
15386 /*is_declaration=*/false,
15389 if (!type_specifier)
15391 /* If the first type-specifier could not be found, this is not a
15392 type-specifier-seq at all. */
15393 if (!seen_type_specifier)
15395 cp_parser_error (parser, "expected type-specifier");
15396 type_specifier_seq->type = error_mark_node;
15399 /* If subsequent type-specifiers could not be found, the
15400 type-specifier-seq is complete. */
15404 seen_type_specifier = true;
15405 /* The standard says that a condition can be:
15407 type-specifier-seq declarator = assignment-expression
15414 we should treat the "S" as a declarator, not as a
15415 type-specifier. The standard doesn't say that explicitly for
15416 type-specifier-seq, but it does say that for
15417 decl-specifier-seq in an ordinary declaration. Perhaps it
15418 would be clearer just to allow a decl-specifier-seq here, and
15419 then add a semantic restriction that if any decl-specifiers
15420 that are not type-specifiers appear, the program is invalid. */
15421 if (is_declaration && !is_cv_qualifier)
15422 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15425 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15428 /* Parse a parameter-declaration-clause.
15430 parameter-declaration-clause:
15431 parameter-declaration-list [opt] ... [opt]
15432 parameter-declaration-list , ...
15434 Returns a representation for the parameter declarations. A return
15435 value of NULL indicates a parameter-declaration-clause consisting
15436 only of an ellipsis. */
15439 cp_parser_parameter_declaration_clause (cp_parser* parser)
15446 /* Peek at the next token. */
15447 token = cp_lexer_peek_token (parser->lexer);
15448 /* Check for trivial parameter-declaration-clauses. */
15449 if (token->type == CPP_ELLIPSIS)
15451 /* Consume the `...' token. */
15452 cp_lexer_consume_token (parser->lexer);
15455 else if (token->type == CPP_CLOSE_PAREN)
15456 /* There are no parameters. */
15458 #ifndef NO_IMPLICIT_EXTERN_C
15459 if (in_system_header && current_class_type == NULL
15460 && current_lang_name == lang_name_c)
15464 return void_list_node;
15466 /* Check for `(void)', too, which is a special case. */
15467 else if (token->keyword == RID_VOID
15468 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15469 == CPP_CLOSE_PAREN))
15471 /* Consume the `void' token. */
15472 cp_lexer_consume_token (parser->lexer);
15473 /* There are no parameters. */
15474 return void_list_node;
15477 /* Parse the parameter-declaration-list. */
15478 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15479 /* If a parse error occurred while parsing the
15480 parameter-declaration-list, then the entire
15481 parameter-declaration-clause is erroneous. */
15485 /* Peek at the next token. */
15486 token = cp_lexer_peek_token (parser->lexer);
15487 /* If it's a `,', the clause should terminate with an ellipsis. */
15488 if (token->type == CPP_COMMA)
15490 /* Consume the `,'. */
15491 cp_lexer_consume_token (parser->lexer);
15492 /* Expect an ellipsis. */
15494 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15496 /* It might also be `...' if the optional trailing `,' was
15498 else if (token->type == CPP_ELLIPSIS)
15500 /* Consume the `...' token. */
15501 cp_lexer_consume_token (parser->lexer);
15502 /* And remember that we saw it. */
15506 ellipsis_p = false;
15508 /* Finish the parameter list. */
15510 parameters = chainon (parameters, void_list_node);
15515 /* Parse a parameter-declaration-list.
15517 parameter-declaration-list:
15518 parameter-declaration
15519 parameter-declaration-list , parameter-declaration
15521 Returns a representation of the parameter-declaration-list, as for
15522 cp_parser_parameter_declaration_clause. However, the
15523 `void_list_node' is never appended to the list. Upon return,
15524 *IS_ERROR will be true iff an error occurred. */
15527 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15529 tree parameters = NULL_TREE;
15530 tree *tail = ¶meters;
15531 bool saved_in_unbraced_linkage_specification_p;
15534 /* Assume all will go well. */
15536 /* The special considerations that apply to a function within an
15537 unbraced linkage specifications do not apply to the parameters
15538 to the function. */
15539 saved_in_unbraced_linkage_specification_p
15540 = parser->in_unbraced_linkage_specification_p;
15541 parser->in_unbraced_linkage_specification_p = false;
15543 /* Look for more parameters. */
15546 cp_parameter_declarator *parameter;
15547 tree decl = error_mark_node;
15548 bool parenthesized_p;
15549 /* Parse the parameter. */
15551 = cp_parser_parameter_declaration (parser,
15552 /*template_parm_p=*/false,
15555 /* We don't know yet if the enclosing context is deprecated, so wait
15556 and warn in grokparms if appropriate. */
15557 deprecated_state = DEPRECATED_SUPPRESS;
15560 decl = grokdeclarator (parameter->declarator,
15561 ¶meter->decl_specifiers,
15563 parameter->default_argument != NULL_TREE,
15564 ¶meter->decl_specifiers.attributes);
15566 deprecated_state = DEPRECATED_NORMAL;
15568 /* If a parse error occurred parsing the parameter declaration,
15569 then the entire parameter-declaration-list is erroneous. */
15570 if (decl == error_mark_node)
15573 parameters = error_mark_node;
15577 if (parameter->decl_specifiers.attributes)
15578 cplus_decl_attributes (&decl,
15579 parameter->decl_specifiers.attributes,
15581 if (DECL_NAME (decl))
15582 decl = pushdecl (decl);
15584 if (decl != error_mark_node)
15586 retrofit_lang_decl (decl);
15587 DECL_PARM_INDEX (decl) = ++index;
15590 /* Add the new parameter to the list. */
15591 *tail = build_tree_list (parameter->default_argument, decl);
15592 tail = &TREE_CHAIN (*tail);
15594 /* Peek at the next token. */
15595 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15596 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15597 /* These are for Objective-C++ */
15598 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15599 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15600 /* The parameter-declaration-list is complete. */
15602 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15606 /* Peek at the next token. */
15607 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15608 /* If it's an ellipsis, then the list is complete. */
15609 if (token->type == CPP_ELLIPSIS)
15611 /* Otherwise, there must be more parameters. Consume the
15613 cp_lexer_consume_token (parser->lexer);
15614 /* When parsing something like:
15616 int i(float f, double d)
15618 we can tell after seeing the declaration for "f" that we
15619 are not looking at an initialization of a variable "i",
15620 but rather at the declaration of a function "i".
15622 Due to the fact that the parsing of template arguments
15623 (as specified to a template-id) requires backtracking we
15624 cannot use this technique when inside a template argument
15626 if (!parser->in_template_argument_list_p
15627 && !parser->in_type_id_in_expr_p
15628 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15629 /* However, a parameter-declaration of the form
15630 "foat(f)" (which is a valid declaration of a
15631 parameter "f") can also be interpreted as an
15632 expression (the conversion of "f" to "float"). */
15633 && !parenthesized_p)
15634 cp_parser_commit_to_tentative_parse (parser);
15638 cp_parser_error (parser, "expected %<,%> or %<...%>");
15639 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15640 cp_parser_skip_to_closing_parenthesis (parser,
15641 /*recovering=*/true,
15642 /*or_comma=*/false,
15643 /*consume_paren=*/false);
15648 parser->in_unbraced_linkage_specification_p
15649 = saved_in_unbraced_linkage_specification_p;
15654 /* Parse a parameter declaration.
15656 parameter-declaration:
15657 decl-specifier-seq ... [opt] declarator
15658 decl-specifier-seq declarator = assignment-expression
15659 decl-specifier-seq ... [opt] abstract-declarator [opt]
15660 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15662 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15663 declares a template parameter. (In that case, a non-nested `>'
15664 token encountered during the parsing of the assignment-expression
15665 is not interpreted as a greater-than operator.)
15667 Returns a representation of the parameter, or NULL if an error
15668 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15669 true iff the declarator is of the form "(p)". */
15671 static cp_parameter_declarator *
15672 cp_parser_parameter_declaration (cp_parser *parser,
15673 bool template_parm_p,
15674 bool *parenthesized_p)
15676 int declares_class_or_enum;
15677 cp_decl_specifier_seq decl_specifiers;
15678 cp_declarator *declarator;
15679 tree default_argument;
15680 cp_token *token = NULL, *declarator_token_start = NULL;
15681 const char *saved_message;
15683 /* In a template parameter, `>' is not an operator.
15687 When parsing a default template-argument for a non-type
15688 template-parameter, the first non-nested `>' is taken as the end
15689 of the template parameter-list rather than a greater-than
15692 /* Type definitions may not appear in parameter types. */
15693 saved_message = parser->type_definition_forbidden_message;
15694 parser->type_definition_forbidden_message
15695 = G_("types may not be defined in parameter types");
15697 /* Parse the declaration-specifiers. */
15698 cp_parser_decl_specifier_seq (parser,
15699 CP_PARSER_FLAGS_NONE,
15701 &declares_class_or_enum);
15703 /* Complain about missing 'typename' or other invalid type names. */
15704 if (!decl_specifiers.any_type_specifiers_p)
15705 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15707 /* If an error occurred, there's no reason to attempt to parse the
15708 rest of the declaration. */
15709 if (cp_parser_error_occurred (parser))
15711 parser->type_definition_forbidden_message = saved_message;
15715 /* Peek at the next token. */
15716 token = cp_lexer_peek_token (parser->lexer);
15718 /* If the next token is a `)', `,', `=', `>', or `...', then there
15719 is no declarator. However, when variadic templates are enabled,
15720 there may be a declarator following `...'. */
15721 if (token->type == CPP_CLOSE_PAREN
15722 || token->type == CPP_COMMA
15723 || token->type == CPP_EQ
15724 || token->type == CPP_GREATER)
15727 if (parenthesized_p)
15728 *parenthesized_p = false;
15730 /* Otherwise, there should be a declarator. */
15733 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15734 parser->default_arg_ok_p = false;
15736 /* After seeing a decl-specifier-seq, if the next token is not a
15737 "(", there is no possibility that the code is a valid
15738 expression. Therefore, if parsing tentatively, we commit at
15740 if (!parser->in_template_argument_list_p
15741 /* In an expression context, having seen:
15745 we cannot be sure whether we are looking at a
15746 function-type (taking a "char" as a parameter) or a cast
15747 of some object of type "char" to "int". */
15748 && !parser->in_type_id_in_expr_p
15749 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15750 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15751 cp_parser_commit_to_tentative_parse (parser);
15752 /* Parse the declarator. */
15753 declarator_token_start = token;
15754 declarator = cp_parser_declarator (parser,
15755 CP_PARSER_DECLARATOR_EITHER,
15756 /*ctor_dtor_or_conv_p=*/NULL,
15758 /*member_p=*/false);
15759 parser->default_arg_ok_p = saved_default_arg_ok_p;
15760 /* After the declarator, allow more attributes. */
15761 decl_specifiers.attributes
15762 = chainon (decl_specifiers.attributes,
15763 cp_parser_attributes_opt (parser));
15766 /* If the next token is an ellipsis, and we have not seen a
15767 declarator name, and the type of the declarator contains parameter
15768 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15769 a parameter pack expansion expression. Otherwise, leave the
15770 ellipsis for a C-style variadic function. */
15771 token = cp_lexer_peek_token (parser->lexer);
15772 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15774 tree type = decl_specifiers.type;
15776 if (type && DECL_P (type))
15777 type = TREE_TYPE (type);
15780 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15781 && declarator_can_be_parameter_pack (declarator)
15782 && (!declarator || !declarator->parameter_pack_p)
15783 && uses_parameter_packs (type))
15785 /* Consume the `...'. */
15786 cp_lexer_consume_token (parser->lexer);
15787 maybe_warn_variadic_templates ();
15789 /* Build a pack expansion type */
15791 declarator->parameter_pack_p = true;
15793 decl_specifiers.type = make_pack_expansion (type);
15797 /* The restriction on defining new types applies only to the type
15798 of the parameter, not to the default argument. */
15799 parser->type_definition_forbidden_message = saved_message;
15801 /* If the next token is `=', then process a default argument. */
15802 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15804 /* Consume the `='. */
15805 cp_lexer_consume_token (parser->lexer);
15807 /* If we are defining a class, then the tokens that make up the
15808 default argument must be saved and processed later. */
15809 if (!template_parm_p && at_class_scope_p ()
15810 && TYPE_BEING_DEFINED (current_class_type)
15811 && !LAMBDA_TYPE_P (current_class_type))
15813 unsigned depth = 0;
15814 int maybe_template_id = 0;
15815 cp_token *first_token;
15818 /* Add tokens until we have processed the entire default
15819 argument. We add the range [first_token, token). */
15820 first_token = cp_lexer_peek_token (parser->lexer);
15825 /* Peek at the next token. */
15826 token = cp_lexer_peek_token (parser->lexer);
15827 /* What we do depends on what token we have. */
15828 switch (token->type)
15830 /* In valid code, a default argument must be
15831 immediately followed by a `,' `)', or `...'. */
15833 if (depth == 0 && maybe_template_id)
15835 /* If we've seen a '<', we might be in a
15836 template-argument-list. Until Core issue 325 is
15837 resolved, we don't know how this situation ought
15838 to be handled, so try to DTRT. We check whether
15839 what comes after the comma is a valid parameter
15840 declaration list. If it is, then the comma ends
15841 the default argument; otherwise the default
15842 argument continues. */
15843 bool error = false;
15846 /* Set ITALP so cp_parser_parameter_declaration_list
15847 doesn't decide to commit to this parse. */
15848 bool saved_italp = parser->in_template_argument_list_p;
15849 parser->in_template_argument_list_p = true;
15851 cp_parser_parse_tentatively (parser);
15852 cp_lexer_consume_token (parser->lexer);
15853 begin_scope (sk_function_parms, NULL_TREE);
15854 cp_parser_parameter_declaration_list (parser, &error);
15855 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15856 pop_binding (DECL_NAME (t), t);
15858 if (!cp_parser_error_occurred (parser) && !error)
15860 cp_parser_abort_tentative_parse (parser);
15862 parser->in_template_argument_list_p = saved_italp;
15865 case CPP_CLOSE_PAREN:
15867 /* If we run into a non-nested `;', `}', or `]',
15868 then the code is invalid -- but the default
15869 argument is certainly over. */
15870 case CPP_SEMICOLON:
15871 case CPP_CLOSE_BRACE:
15872 case CPP_CLOSE_SQUARE:
15875 /* Update DEPTH, if necessary. */
15876 else if (token->type == CPP_CLOSE_PAREN
15877 || token->type == CPP_CLOSE_BRACE
15878 || token->type == CPP_CLOSE_SQUARE)
15882 case CPP_OPEN_PAREN:
15883 case CPP_OPEN_SQUARE:
15884 case CPP_OPEN_BRACE:
15890 /* This might be the comparison operator, or it might
15891 start a template argument list. */
15892 ++maybe_template_id;
15896 if (cxx_dialect == cxx98)
15898 /* Fall through for C++0x, which treats the `>>'
15899 operator like two `>' tokens in certain
15905 /* This might be an operator, or it might close a
15906 template argument list. But if a previous '<'
15907 started a template argument list, this will have
15908 closed it, so we can't be in one anymore. */
15909 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15910 if (maybe_template_id < 0)
15911 maybe_template_id = 0;
15915 /* If we run out of tokens, issue an error message. */
15917 case CPP_PRAGMA_EOL:
15918 error_at (token->location, "file ends in default argument");
15924 /* In these cases, we should look for template-ids.
15925 For example, if the default argument is
15926 `X<int, double>()', we need to do name lookup to
15927 figure out whether or not `X' is a template; if
15928 so, the `,' does not end the default argument.
15930 That is not yet done. */
15937 /* If we've reached the end, stop. */
15941 /* Add the token to the token block. */
15942 token = cp_lexer_consume_token (parser->lexer);
15945 /* Create a DEFAULT_ARG to represent the unparsed default
15947 default_argument = make_node (DEFAULT_ARG);
15948 DEFARG_TOKENS (default_argument)
15949 = cp_token_cache_new (first_token, token);
15950 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15952 /* Outside of a class definition, we can just parse the
15953 assignment-expression. */
15956 token = cp_lexer_peek_token (parser->lexer);
15958 = cp_parser_default_argument (parser, template_parm_p);
15961 if (!parser->default_arg_ok_p)
15963 if (flag_permissive)
15964 warning (0, "deprecated use of default argument for parameter of non-function");
15967 error_at (token->location,
15968 "default arguments are only "
15969 "permitted for function parameters");
15970 default_argument = NULL_TREE;
15973 else if ((declarator && declarator->parameter_pack_p)
15974 || (decl_specifiers.type
15975 && PACK_EXPANSION_P (decl_specifiers.type)))
15977 /* Find the name of the parameter pack. */
15978 cp_declarator *id_declarator = declarator;
15979 while (id_declarator && id_declarator->kind != cdk_id)
15980 id_declarator = id_declarator->declarator;
15982 if (id_declarator && id_declarator->kind == cdk_id)
15983 error_at (declarator_token_start->location,
15985 ? "template parameter pack %qD"
15986 " cannot have a default argument"
15987 : "parameter pack %qD cannot have a default argument",
15988 id_declarator->u.id.unqualified_name);
15990 error_at (declarator_token_start->location,
15992 ? "template parameter pack cannot have a default argument"
15993 : "parameter pack cannot have a default argument");
15995 default_argument = NULL_TREE;
15999 default_argument = NULL_TREE;
16001 return make_parameter_declarator (&decl_specifiers,
16006 /* Parse a default argument and return it.
16008 TEMPLATE_PARM_P is true if this is a default argument for a
16009 non-type template parameter. */
16011 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16013 tree default_argument = NULL_TREE;
16014 bool saved_greater_than_is_operator_p;
16015 bool saved_local_variables_forbidden_p;
16017 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16019 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16020 parser->greater_than_is_operator_p = !template_parm_p;
16021 /* Local variable names (and the `this' keyword) may not
16022 appear in a default argument. */
16023 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16024 parser->local_variables_forbidden_p = true;
16025 /* Parse the assignment-expression. */
16026 if (template_parm_p)
16027 push_deferring_access_checks (dk_no_deferred);
16029 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16030 if (template_parm_p)
16031 pop_deferring_access_checks ();
16032 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16033 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16035 return default_argument;
16038 /* Parse a function-body.
16041 compound_statement */
16044 cp_parser_function_body (cp_parser *parser)
16046 cp_parser_compound_statement (parser, NULL, false);
16049 /* Parse a ctor-initializer-opt followed by a function-body. Return
16050 true if a ctor-initializer was present. */
16053 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16056 bool ctor_initializer_p;
16058 /* Begin the function body. */
16059 body = begin_function_body ();
16060 /* Parse the optional ctor-initializer. */
16061 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16062 /* Parse the function-body. */
16063 cp_parser_function_body (parser);
16064 /* Finish the function body. */
16065 finish_function_body (body);
16067 return ctor_initializer_p;
16070 /* Parse an initializer.
16073 = initializer-clause
16074 ( expression-list )
16076 Returns an expression representing the initializer. If no
16077 initializer is present, NULL_TREE is returned.
16079 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16080 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16081 set to TRUE if there is no initializer present. If there is an
16082 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16083 is set to true; otherwise it is set to false. */
16086 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16087 bool* non_constant_p)
16092 /* Peek at the next token. */
16093 token = cp_lexer_peek_token (parser->lexer);
16095 /* Let our caller know whether or not this initializer was
16097 *is_direct_init = (token->type != CPP_EQ);
16098 /* Assume that the initializer is constant. */
16099 *non_constant_p = false;
16101 if (token->type == CPP_EQ)
16103 /* Consume the `='. */
16104 cp_lexer_consume_token (parser->lexer);
16105 /* Parse the initializer-clause. */
16106 init = cp_parser_initializer_clause (parser, non_constant_p);
16108 else if (token->type == CPP_OPEN_PAREN)
16111 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16113 /*allow_expansion_p=*/true,
16116 return error_mark_node;
16117 init = build_tree_list_vec (vec);
16118 release_tree_vector (vec);
16120 else if (token->type == CPP_OPEN_BRACE)
16122 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16123 init = cp_parser_braced_list (parser, non_constant_p);
16124 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16128 /* Anything else is an error. */
16129 cp_parser_error (parser, "expected initializer");
16130 init = error_mark_node;
16136 /* Parse an initializer-clause.
16138 initializer-clause:
16139 assignment-expression
16142 Returns an expression representing the initializer.
16144 If the `assignment-expression' production is used the value
16145 returned is simply a representation for the expression.
16147 Otherwise, calls cp_parser_braced_list. */
16150 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16154 /* Assume the expression is constant. */
16155 *non_constant_p = false;
16157 /* If it is not a `{', then we are looking at an
16158 assignment-expression. */
16159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16162 = cp_parser_constant_expression (parser,
16163 /*allow_non_constant_p=*/true,
16165 if (!*non_constant_p)
16166 initializer = fold_non_dependent_expr (initializer);
16169 initializer = cp_parser_braced_list (parser, non_constant_p);
16171 return initializer;
16174 /* Parse a brace-enclosed initializer list.
16177 { initializer-list , [opt] }
16180 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16181 the elements of the initializer-list (or NULL, if the last
16182 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16183 NULL_TREE. There is no way to detect whether or not the optional
16184 trailing `,' was provided. NON_CONSTANT_P is as for
16185 cp_parser_initializer. */
16188 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16192 /* Consume the `{' token. */
16193 cp_lexer_consume_token (parser->lexer);
16194 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16195 initializer = make_node (CONSTRUCTOR);
16196 /* If it's not a `}', then there is a non-trivial initializer. */
16197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16199 /* Parse the initializer list. */
16200 CONSTRUCTOR_ELTS (initializer)
16201 = cp_parser_initializer_list (parser, non_constant_p);
16202 /* A trailing `,' token is allowed. */
16203 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16204 cp_lexer_consume_token (parser->lexer);
16206 /* Now, there should be a trailing `}'. */
16207 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16208 TREE_TYPE (initializer) = init_list_type_node;
16209 return initializer;
16212 /* Parse an initializer-list.
16215 initializer-clause ... [opt]
16216 initializer-list , initializer-clause ... [opt]
16221 identifier : initializer-clause
16222 initializer-list, identifier : initializer-clause
16224 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16225 for the initializer. If the INDEX of the elt is non-NULL, it is the
16226 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16227 as for cp_parser_initializer. */
16229 static VEC(constructor_elt,gc) *
16230 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16232 VEC(constructor_elt,gc) *v = NULL;
16234 /* Assume all of the expressions are constant. */
16235 *non_constant_p = false;
16237 /* Parse the rest of the list. */
16243 bool clause_non_constant_p;
16245 /* If the next token is an identifier and the following one is a
16246 colon, we are looking at the GNU designated-initializer
16248 if (cp_parser_allow_gnu_extensions_p (parser)
16249 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16250 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16252 /* Warn the user that they are using an extension. */
16253 pedwarn (input_location, OPT_pedantic,
16254 "ISO C++ does not allow designated initializers");
16255 /* Consume the identifier. */
16256 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16257 /* Consume the `:'. */
16258 cp_lexer_consume_token (parser->lexer);
16261 identifier = NULL_TREE;
16263 /* Parse the initializer. */
16264 initializer = cp_parser_initializer_clause (parser,
16265 &clause_non_constant_p);
16266 /* If any clause is non-constant, so is the entire initializer. */
16267 if (clause_non_constant_p)
16268 *non_constant_p = true;
16270 /* If we have an ellipsis, this is an initializer pack
16272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16274 /* Consume the `...'. */
16275 cp_lexer_consume_token (parser->lexer);
16277 /* Turn the initializer into an initializer expansion. */
16278 initializer = make_pack_expansion (initializer);
16281 /* Add it to the vector. */
16282 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16284 /* If the next token is not a comma, we have reached the end of
16286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16289 /* Peek at the next token. */
16290 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16291 /* If the next token is a `}', then we're still done. An
16292 initializer-clause can have a trailing `,' after the
16293 initializer-list and before the closing `}'. */
16294 if (token->type == CPP_CLOSE_BRACE)
16297 /* Consume the `,' token. */
16298 cp_lexer_consume_token (parser->lexer);
16304 /* Classes [gram.class] */
16306 /* Parse a class-name.
16312 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16313 to indicate that names looked up in dependent types should be
16314 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16315 keyword has been used to indicate that the name that appears next
16316 is a template. TAG_TYPE indicates the explicit tag given before
16317 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16318 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16319 is the class being defined in a class-head.
16321 Returns the TYPE_DECL representing the class. */
16324 cp_parser_class_name (cp_parser *parser,
16325 bool typename_keyword_p,
16326 bool template_keyword_p,
16327 enum tag_types tag_type,
16328 bool check_dependency_p,
16330 bool is_declaration)
16336 tree identifier = NULL_TREE;
16338 /* All class-names start with an identifier. */
16339 token = cp_lexer_peek_token (parser->lexer);
16340 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16342 cp_parser_error (parser, "expected class-name");
16343 return error_mark_node;
16346 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16347 to a template-id, so we save it here. */
16348 scope = parser->scope;
16349 if (scope == error_mark_node)
16350 return error_mark_node;
16352 /* Any name names a type if we're following the `typename' keyword
16353 in a qualified name where the enclosing scope is type-dependent. */
16354 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16355 && dependent_type_p (scope));
16356 /* Handle the common case (an identifier, but not a template-id)
16358 if (token->type == CPP_NAME
16359 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16361 cp_token *identifier_token;
16364 /* Look for the identifier. */
16365 identifier_token = cp_lexer_peek_token (parser->lexer);
16366 ambiguous_p = identifier_token->ambiguous_p;
16367 identifier = cp_parser_identifier (parser);
16368 /* If the next token isn't an identifier, we are certainly not
16369 looking at a class-name. */
16370 if (identifier == error_mark_node)
16371 decl = error_mark_node;
16372 /* If we know this is a type-name, there's no need to look it
16374 else if (typename_p)
16378 tree ambiguous_decls;
16379 /* If we already know that this lookup is ambiguous, then
16380 we've already issued an error message; there's no reason
16384 cp_parser_simulate_error (parser);
16385 return error_mark_node;
16387 /* If the next token is a `::', then the name must be a type
16390 [basic.lookup.qual]
16392 During the lookup for a name preceding the :: scope
16393 resolution operator, object, function, and enumerator
16394 names are ignored. */
16395 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16396 tag_type = typename_type;
16397 /* Look up the name. */
16398 decl = cp_parser_lookup_name (parser, identifier,
16400 /*is_template=*/false,
16401 /*is_namespace=*/false,
16402 check_dependency_p,
16404 identifier_token->location);
16405 if (ambiguous_decls)
16407 if (cp_parser_parsing_tentatively (parser))
16408 cp_parser_simulate_error (parser);
16409 return error_mark_node;
16415 /* Try a template-id. */
16416 decl = cp_parser_template_id (parser, template_keyword_p,
16417 check_dependency_p,
16419 if (decl == error_mark_node)
16420 return error_mark_node;
16423 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16425 /* If this is a typename, create a TYPENAME_TYPE. */
16426 if (typename_p && decl != error_mark_node)
16428 decl = make_typename_type (scope, decl, typename_type,
16429 /*complain=*/tf_error);
16430 if (decl != error_mark_node)
16431 decl = TYPE_NAME (decl);
16434 /* Check to see that it is really the name of a class. */
16435 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16436 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16437 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16438 /* Situations like this:
16440 template <typename T> struct A {
16441 typename T::template X<int>::I i;
16444 are problematic. Is `T::template X<int>' a class-name? The
16445 standard does not seem to be definitive, but there is no other
16446 valid interpretation of the following `::'. Therefore, those
16447 names are considered class-names. */
16449 decl = make_typename_type (scope, decl, tag_type, tf_error);
16450 if (decl != error_mark_node)
16451 decl = TYPE_NAME (decl);
16453 else if (TREE_CODE (decl) != TYPE_DECL
16454 || TREE_TYPE (decl) == error_mark_node
16455 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16456 decl = error_mark_node;
16458 if (decl == error_mark_node)
16459 cp_parser_error (parser, "expected class-name");
16460 else if (identifier && !parser->scope)
16461 maybe_note_name_used_in_class (identifier, decl);
16466 /* Parse a class-specifier.
16469 class-head { member-specification [opt] }
16471 Returns the TREE_TYPE representing the class. */
16474 cp_parser_class_specifier (cp_parser* parser)
16477 tree attributes = NULL_TREE;
16478 bool nested_name_specifier_p;
16479 unsigned saved_num_template_parameter_lists;
16480 bool saved_in_function_body;
16481 bool saved_in_unbraced_linkage_specification_p;
16482 tree old_scope = NULL_TREE;
16483 tree scope = NULL_TREE;
16486 push_deferring_access_checks (dk_no_deferred);
16488 /* Parse the class-head. */
16489 type = cp_parser_class_head (parser,
16490 &nested_name_specifier_p,
16493 /* If the class-head was a semantic disaster, skip the entire body
16497 cp_parser_skip_to_end_of_block_or_statement (parser);
16498 pop_deferring_access_checks ();
16499 return error_mark_node;
16502 /* Look for the `{'. */
16503 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16505 pop_deferring_access_checks ();
16506 return error_mark_node;
16509 /* Process the base classes. If they're invalid, skip the
16510 entire class body. */
16511 if (!xref_basetypes (type, bases))
16513 /* Consuming the closing brace yields better error messages
16515 if (cp_parser_skip_to_closing_brace (parser))
16516 cp_lexer_consume_token (parser->lexer);
16517 pop_deferring_access_checks ();
16518 return error_mark_node;
16521 /* Issue an error message if type-definitions are forbidden here. */
16522 cp_parser_check_type_definition (parser);
16523 /* Remember that we are defining one more class. */
16524 ++parser->num_classes_being_defined;
16525 /* Inside the class, surrounding template-parameter-lists do not
16527 saved_num_template_parameter_lists
16528 = parser->num_template_parameter_lists;
16529 parser->num_template_parameter_lists = 0;
16530 /* We are not in a function body. */
16531 saved_in_function_body = parser->in_function_body;
16532 parser->in_function_body = false;
16533 /* We are not immediately inside an extern "lang" block. */
16534 saved_in_unbraced_linkage_specification_p
16535 = parser->in_unbraced_linkage_specification_p;
16536 parser->in_unbraced_linkage_specification_p = false;
16538 /* Start the class. */
16539 if (nested_name_specifier_p)
16541 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16542 old_scope = push_inner_scope (scope);
16544 type = begin_class_definition (type, attributes);
16546 if (type == error_mark_node)
16547 /* If the type is erroneous, skip the entire body of the class. */
16548 cp_parser_skip_to_closing_brace (parser);
16550 /* Parse the member-specification. */
16551 cp_parser_member_specification_opt (parser);
16553 /* Look for the trailing `}'. */
16554 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16555 /* Look for trailing attributes to apply to this class. */
16556 if (cp_parser_allow_gnu_extensions_p (parser))
16557 attributes = cp_parser_attributes_opt (parser);
16558 if (type != error_mark_node)
16559 type = finish_struct (type, attributes);
16560 if (nested_name_specifier_p)
16561 pop_inner_scope (old_scope, scope);
16562 /* If this class is not itself within the scope of another class,
16563 then we need to parse the bodies of all of the queued function
16564 definitions. Note that the queued functions defined in a class
16565 are not always processed immediately following the
16566 class-specifier for that class. Consider:
16569 struct B { void f() { sizeof (A); } };
16572 If `f' were processed before the processing of `A' were
16573 completed, there would be no way to compute the size of `A'.
16574 Note that the nesting we are interested in here is lexical --
16575 not the semantic nesting given by TYPE_CONTEXT. In particular,
16578 struct A { struct B; };
16579 struct A::B { void f() { } };
16581 there is no need to delay the parsing of `A::B::f'. */
16582 if (--parser->num_classes_being_defined == 0)
16585 tree class_type = NULL_TREE;
16586 tree pushed_scope = NULL_TREE;
16588 cp_default_arg_entry *e;
16590 /* In a first pass, parse default arguments to the functions.
16591 Then, in a second pass, parse the bodies of the functions.
16592 This two-phased approach handles cases like:
16600 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16604 /* If there are default arguments that have not yet been processed,
16605 take care of them now. */
16606 if (class_type != e->class_type)
16609 pop_scope (pushed_scope);
16610 class_type = e->class_type;
16611 pushed_scope = push_scope (class_type);
16613 /* Make sure that any template parameters are in scope. */
16614 maybe_begin_member_template_processing (fn);
16615 /* Parse the default argument expressions. */
16616 cp_parser_late_parsing_default_args (parser, fn);
16617 /* Remove any template parameters from the symbol table. */
16618 maybe_end_member_template_processing ();
16621 pop_scope (pushed_scope);
16622 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16623 /* Now parse the body of the functions. */
16624 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16625 cp_parser_late_parsing_for_member (parser, fn);
16626 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16629 /* Put back any saved access checks. */
16630 pop_deferring_access_checks ();
16632 /* Restore saved state. */
16633 parser->in_function_body = saved_in_function_body;
16634 parser->num_template_parameter_lists
16635 = saved_num_template_parameter_lists;
16636 parser->in_unbraced_linkage_specification_p
16637 = saved_in_unbraced_linkage_specification_p;
16642 /* Parse a class-head.
16645 class-key identifier [opt] base-clause [opt]
16646 class-key nested-name-specifier identifier base-clause [opt]
16647 class-key nested-name-specifier [opt] template-id
16651 class-key attributes identifier [opt] base-clause [opt]
16652 class-key attributes nested-name-specifier identifier base-clause [opt]
16653 class-key attributes nested-name-specifier [opt] template-id
16656 Upon return BASES is initialized to the list of base classes (or
16657 NULL, if there are none) in the same form returned by
16658 cp_parser_base_clause.
16660 Returns the TYPE of the indicated class. Sets
16661 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16662 involving a nested-name-specifier was used, and FALSE otherwise.
16664 Returns error_mark_node if this is not a class-head.
16666 Returns NULL_TREE if the class-head is syntactically valid, but
16667 semantically invalid in a way that means we should skip the entire
16668 body of the class. */
16671 cp_parser_class_head (cp_parser* parser,
16672 bool* nested_name_specifier_p,
16673 tree *attributes_p,
16676 tree nested_name_specifier;
16677 enum tag_types class_key;
16678 tree id = NULL_TREE;
16679 tree type = NULL_TREE;
16681 bool template_id_p = false;
16682 bool qualified_p = false;
16683 bool invalid_nested_name_p = false;
16684 bool invalid_explicit_specialization_p = false;
16685 tree pushed_scope = NULL_TREE;
16686 unsigned num_templates;
16687 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16688 /* Assume no nested-name-specifier will be present. */
16689 *nested_name_specifier_p = false;
16690 /* Assume no template parameter lists will be used in defining the
16694 *bases = NULL_TREE;
16696 /* Look for the class-key. */
16697 class_key = cp_parser_class_key (parser);
16698 if (class_key == none_type)
16699 return error_mark_node;
16701 /* Parse the attributes. */
16702 attributes = cp_parser_attributes_opt (parser);
16704 /* If the next token is `::', that is invalid -- but sometimes
16705 people do try to write:
16709 Handle this gracefully by accepting the extra qualifier, and then
16710 issuing an error about it later if this really is a
16711 class-head. If it turns out just to be an elaborated type
16712 specifier, remain silent. */
16713 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16714 qualified_p = true;
16716 push_deferring_access_checks (dk_no_check);
16718 /* Determine the name of the class. Begin by looking for an
16719 optional nested-name-specifier. */
16720 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16721 nested_name_specifier
16722 = cp_parser_nested_name_specifier_opt (parser,
16723 /*typename_keyword_p=*/false,
16724 /*check_dependency_p=*/false,
16726 /*is_declaration=*/false);
16727 /* If there was a nested-name-specifier, then there *must* be an
16729 if (nested_name_specifier)
16731 type_start_token = cp_lexer_peek_token (parser->lexer);
16732 /* Although the grammar says `identifier', it really means
16733 `class-name' or `template-name'. You are only allowed to
16734 define a class that has already been declared with this
16737 The proposed resolution for Core Issue 180 says that wherever
16738 you see `class T::X' you should treat `X' as a type-name.
16740 It is OK to define an inaccessible class; for example:
16742 class A { class B; };
16745 We do not know if we will see a class-name, or a
16746 template-name. We look for a class-name first, in case the
16747 class-name is a template-id; if we looked for the
16748 template-name first we would stop after the template-name. */
16749 cp_parser_parse_tentatively (parser);
16750 type = cp_parser_class_name (parser,
16751 /*typename_keyword_p=*/false,
16752 /*template_keyword_p=*/false,
16754 /*check_dependency_p=*/false,
16755 /*class_head_p=*/true,
16756 /*is_declaration=*/false);
16757 /* If that didn't work, ignore the nested-name-specifier. */
16758 if (!cp_parser_parse_definitely (parser))
16760 invalid_nested_name_p = true;
16761 type_start_token = cp_lexer_peek_token (parser->lexer);
16762 id = cp_parser_identifier (parser);
16763 if (id == error_mark_node)
16766 /* If we could not find a corresponding TYPE, treat this
16767 declaration like an unqualified declaration. */
16768 if (type == error_mark_node)
16769 nested_name_specifier = NULL_TREE;
16770 /* Otherwise, count the number of templates used in TYPE and its
16771 containing scopes. */
16776 for (scope = TREE_TYPE (type);
16777 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16778 scope = (TYPE_P (scope)
16779 ? TYPE_CONTEXT (scope)
16780 : DECL_CONTEXT (scope)))
16782 && CLASS_TYPE_P (scope)
16783 && CLASSTYPE_TEMPLATE_INFO (scope)
16784 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16785 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16789 /* Otherwise, the identifier is optional. */
16792 /* We don't know whether what comes next is a template-id,
16793 an identifier, or nothing at all. */
16794 cp_parser_parse_tentatively (parser);
16795 /* Check for a template-id. */
16796 type_start_token = cp_lexer_peek_token (parser->lexer);
16797 id = cp_parser_template_id (parser,
16798 /*template_keyword_p=*/false,
16799 /*check_dependency_p=*/true,
16800 /*is_declaration=*/true);
16801 /* If that didn't work, it could still be an identifier. */
16802 if (!cp_parser_parse_definitely (parser))
16804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16806 type_start_token = cp_lexer_peek_token (parser->lexer);
16807 id = cp_parser_identifier (parser);
16814 template_id_p = true;
16819 pop_deferring_access_checks ();
16822 cp_parser_check_for_invalid_template_id (parser, id,
16823 type_start_token->location);
16825 /* If it's not a `:' or a `{' then we can't really be looking at a
16826 class-head, since a class-head only appears as part of a
16827 class-specifier. We have to detect this situation before calling
16828 xref_tag, since that has irreversible side-effects. */
16829 if (!cp_parser_next_token_starts_class_definition_p (parser))
16831 cp_parser_error (parser, "expected %<{%> or %<:%>");
16832 return error_mark_node;
16835 /* At this point, we're going ahead with the class-specifier, even
16836 if some other problem occurs. */
16837 cp_parser_commit_to_tentative_parse (parser);
16838 /* Issue the error about the overly-qualified name now. */
16841 cp_parser_error (parser,
16842 "global qualification of class name is invalid");
16843 return error_mark_node;
16845 else if (invalid_nested_name_p)
16847 cp_parser_error (parser,
16848 "qualified name does not name a class");
16849 return error_mark_node;
16851 else if (nested_name_specifier)
16855 /* Reject typedef-names in class heads. */
16856 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16858 error_at (type_start_token->location,
16859 "invalid class name in declaration of %qD",
16865 /* Figure out in what scope the declaration is being placed. */
16866 scope = current_scope ();
16867 /* If that scope does not contain the scope in which the
16868 class was originally declared, the program is invalid. */
16869 if (scope && !is_ancestor (scope, nested_name_specifier))
16871 if (at_namespace_scope_p ())
16872 error_at (type_start_token->location,
16873 "declaration of %qD in namespace %qD which does not "
16875 type, scope, nested_name_specifier);
16877 error_at (type_start_token->location,
16878 "declaration of %qD in %qD which does not enclose %qD",
16879 type, scope, nested_name_specifier);
16885 A declarator-id shall not be qualified except for the
16886 definition of a ... nested class outside of its class
16887 ... [or] the definition or explicit instantiation of a
16888 class member of a namespace outside of its namespace. */
16889 if (scope == nested_name_specifier)
16891 permerror (nested_name_specifier_token_start->location,
16892 "extra qualification not allowed");
16893 nested_name_specifier = NULL_TREE;
16897 /* An explicit-specialization must be preceded by "template <>". If
16898 it is not, try to recover gracefully. */
16899 if (at_namespace_scope_p ()
16900 && parser->num_template_parameter_lists == 0
16903 error_at (type_start_token->location,
16904 "an explicit specialization must be preceded by %<template <>%>");
16905 invalid_explicit_specialization_p = true;
16906 /* Take the same action that would have been taken by
16907 cp_parser_explicit_specialization. */
16908 ++parser->num_template_parameter_lists;
16909 begin_specialization ();
16911 /* There must be no "return" statements between this point and the
16912 end of this function; set "type "to the correct return value and
16913 use "goto done;" to return. */
16914 /* Make sure that the right number of template parameters were
16916 if (!cp_parser_check_template_parameters (parser, num_templates,
16917 type_start_token->location,
16918 /*declarator=*/NULL))
16920 /* If something went wrong, there is no point in even trying to
16921 process the class-definition. */
16926 /* Look up the type. */
16929 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16930 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16931 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16933 error_at (type_start_token->location,
16934 "function template %qD redeclared as a class template", id);
16935 type = error_mark_node;
16939 type = TREE_TYPE (id);
16940 type = maybe_process_partial_specialization (type);
16942 if (nested_name_specifier)
16943 pushed_scope = push_scope (nested_name_specifier);
16945 else if (nested_name_specifier)
16951 template <typename T> struct S { struct T };
16952 template <typename T> struct S<T>::T { };
16954 we will get a TYPENAME_TYPE when processing the definition of
16955 `S::T'. We need to resolve it to the actual type before we
16956 try to define it. */
16957 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16959 class_type = resolve_typename_type (TREE_TYPE (type),
16960 /*only_current_p=*/false);
16961 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16962 type = TYPE_NAME (class_type);
16965 cp_parser_error (parser, "could not resolve typename type");
16966 type = error_mark_node;
16970 if (maybe_process_partial_specialization (TREE_TYPE (type))
16971 == error_mark_node)
16977 class_type = current_class_type;
16978 /* Enter the scope indicated by the nested-name-specifier. */
16979 pushed_scope = push_scope (nested_name_specifier);
16980 /* Get the canonical version of this type. */
16981 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16982 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16983 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16985 type = push_template_decl (type);
16986 if (type == error_mark_node)
16993 type = TREE_TYPE (type);
16994 *nested_name_specifier_p = true;
16996 else /* The name is not a nested name. */
16998 /* If the class was unnamed, create a dummy name. */
17000 id = make_anon_name ();
17001 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17002 parser->num_template_parameter_lists);
17005 /* Indicate whether this class was declared as a `class' or as a
17007 if (TREE_CODE (type) == RECORD_TYPE)
17008 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17009 cp_parser_check_class_key (class_key, type);
17011 /* If this type was already complete, and we see another definition,
17012 that's an error. */
17013 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17015 error_at (type_start_token->location, "redefinition of %q#T",
17017 error_at (type_start_token->location, "previous definition of %q+#T",
17022 else if (type == error_mark_node)
17025 /* We will have entered the scope containing the class; the names of
17026 base classes should be looked up in that context. For example:
17028 struct A { struct B {}; struct C; };
17029 struct A::C : B {};
17033 /* Get the list of base-classes, if there is one. */
17034 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17035 *bases = cp_parser_base_clause (parser);
17038 /* Leave the scope given by the nested-name-specifier. We will
17039 enter the class scope itself while processing the members. */
17041 pop_scope (pushed_scope);
17043 if (invalid_explicit_specialization_p)
17045 end_specialization ();
17046 --parser->num_template_parameter_lists;
17050 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17051 *attributes_p = attributes;
17055 /* Parse a class-key.
17062 Returns the kind of class-key specified, or none_type to indicate
17065 static enum tag_types
17066 cp_parser_class_key (cp_parser* parser)
17069 enum tag_types tag_type;
17071 /* Look for the class-key. */
17072 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17076 /* Check to see if the TOKEN is a class-key. */
17077 tag_type = cp_parser_token_is_class_key (token);
17079 cp_parser_error (parser, "expected class-key");
17083 /* Parse an (optional) member-specification.
17085 member-specification:
17086 member-declaration member-specification [opt]
17087 access-specifier : member-specification [opt] */
17090 cp_parser_member_specification_opt (cp_parser* parser)
17097 /* Peek at the next token. */
17098 token = cp_lexer_peek_token (parser->lexer);
17099 /* If it's a `}', or EOF then we've seen all the members. */
17100 if (token->type == CPP_CLOSE_BRACE
17101 || token->type == CPP_EOF
17102 || token->type == CPP_PRAGMA_EOL)
17105 /* See if this token is a keyword. */
17106 keyword = token->keyword;
17110 case RID_PROTECTED:
17112 /* Consume the access-specifier. */
17113 cp_lexer_consume_token (parser->lexer);
17114 /* Remember which access-specifier is active. */
17115 current_access_specifier = token->u.value;
17116 /* Look for the `:'. */
17117 cp_parser_require (parser, CPP_COLON, RT_COLON);
17121 /* Accept #pragmas at class scope. */
17122 if (token->type == CPP_PRAGMA)
17124 cp_parser_pragma (parser, pragma_external);
17128 /* Otherwise, the next construction must be a
17129 member-declaration. */
17130 cp_parser_member_declaration (parser);
17135 /* Parse a member-declaration.
17137 member-declaration:
17138 decl-specifier-seq [opt] member-declarator-list [opt] ;
17139 function-definition ; [opt]
17140 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17142 template-declaration
17144 member-declarator-list:
17146 member-declarator-list , member-declarator
17149 declarator pure-specifier [opt]
17150 declarator constant-initializer [opt]
17151 identifier [opt] : constant-expression
17155 member-declaration:
17156 __extension__ member-declaration
17159 declarator attributes [opt] pure-specifier [opt]
17160 declarator attributes [opt] constant-initializer [opt]
17161 identifier [opt] attributes [opt] : constant-expression
17165 member-declaration:
17166 static_assert-declaration */
17169 cp_parser_member_declaration (cp_parser* parser)
17171 cp_decl_specifier_seq decl_specifiers;
17172 tree prefix_attributes;
17174 int declares_class_or_enum;
17176 cp_token *token = NULL;
17177 cp_token *decl_spec_token_start = NULL;
17178 cp_token *initializer_token_start = NULL;
17179 int saved_pedantic;
17181 /* Check for the `__extension__' keyword. */
17182 if (cp_parser_extension_opt (parser, &saved_pedantic))
17185 cp_parser_member_declaration (parser);
17186 /* Restore the old value of the PEDANTIC flag. */
17187 pedantic = saved_pedantic;
17192 /* Check for a template-declaration. */
17193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17195 /* An explicit specialization here is an error condition, and we
17196 expect the specialization handler to detect and report this. */
17197 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17198 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17199 cp_parser_explicit_specialization (parser);
17201 cp_parser_template_declaration (parser, /*member_p=*/true);
17206 /* Check for a using-declaration. */
17207 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17209 /* Parse the using-declaration. */
17210 cp_parser_using_declaration (parser,
17211 /*access_declaration_p=*/false);
17215 /* Check for @defs. */
17216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17219 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17220 ivar = ivar_chains;
17224 ivar = TREE_CHAIN (member);
17225 TREE_CHAIN (member) = NULL_TREE;
17226 finish_member_declaration (member);
17231 /* If the next token is `static_assert' we have a static assertion. */
17232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17234 cp_parser_static_assert (parser, /*member_p=*/true);
17238 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17241 /* Parse the decl-specifier-seq. */
17242 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17243 cp_parser_decl_specifier_seq (parser,
17244 CP_PARSER_FLAGS_OPTIONAL,
17246 &declares_class_or_enum);
17247 prefix_attributes = decl_specifiers.attributes;
17248 decl_specifiers.attributes = NULL_TREE;
17249 /* Check for an invalid type-name. */
17250 if (!decl_specifiers.any_type_specifiers_p
17251 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17253 /* If there is no declarator, then the decl-specifier-seq should
17255 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17257 /* If there was no decl-specifier-seq, and the next token is a
17258 `;', then we have something like:
17264 Each member-declaration shall declare at least one member
17265 name of the class. */
17266 if (!decl_specifiers.any_specifiers_p)
17268 cp_token *token = cp_lexer_peek_token (parser->lexer);
17269 if (!in_system_header_at (token->location))
17270 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17276 /* See if this declaration is a friend. */
17277 friend_p = cp_parser_friend_p (&decl_specifiers);
17278 /* If there were decl-specifiers, check to see if there was
17279 a class-declaration. */
17280 type = check_tag_decl (&decl_specifiers);
17281 /* Nested classes have already been added to the class, but
17282 a `friend' needs to be explicitly registered. */
17285 /* If the `friend' keyword was present, the friend must
17286 be introduced with a class-key. */
17287 if (!declares_class_or_enum)
17288 error_at (decl_spec_token_start->location,
17289 "a class-key must be used when declaring a friend");
17292 template <typename T> struct A {
17293 friend struct A<T>::B;
17296 A<T>::B will be represented by a TYPENAME_TYPE, and
17297 therefore not recognized by check_tag_decl. */
17299 && decl_specifiers.type
17300 && TYPE_P (decl_specifiers.type))
17301 type = decl_specifiers.type;
17302 if (!type || !TYPE_P (type))
17303 error_at (decl_spec_token_start->location,
17304 "friend declaration does not name a class or "
17307 make_friend_class (current_class_type, type,
17308 /*complain=*/true);
17310 /* If there is no TYPE, an error message will already have
17312 else if (!type || type == error_mark_node)
17314 /* An anonymous aggregate has to be handled specially; such
17315 a declaration really declares a data member (with a
17316 particular type), as opposed to a nested class. */
17317 else if (ANON_AGGR_TYPE_P (type))
17319 /* Remove constructors and such from TYPE, now that we
17320 know it is an anonymous aggregate. */
17321 fixup_anonymous_aggr (type);
17322 /* And make the corresponding data member. */
17323 decl = build_decl (decl_spec_token_start->location,
17324 FIELD_DECL, NULL_TREE, type);
17325 /* Add it to the class. */
17326 finish_member_declaration (decl);
17329 cp_parser_check_access_in_redeclaration
17331 decl_spec_token_start->location);
17336 /* See if these declarations will be friends. */
17337 friend_p = cp_parser_friend_p (&decl_specifiers);
17339 /* Keep going until we hit the `;' at the end of the
17341 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17343 tree attributes = NULL_TREE;
17344 tree first_attribute;
17346 /* Peek at the next token. */
17347 token = cp_lexer_peek_token (parser->lexer);
17349 /* Check for a bitfield declaration. */
17350 if (token->type == CPP_COLON
17351 || (token->type == CPP_NAME
17352 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17358 /* Get the name of the bitfield. Note that we cannot just
17359 check TOKEN here because it may have been invalidated by
17360 the call to cp_lexer_peek_nth_token above. */
17361 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17362 identifier = cp_parser_identifier (parser);
17364 identifier = NULL_TREE;
17366 /* Consume the `:' token. */
17367 cp_lexer_consume_token (parser->lexer);
17368 /* Get the width of the bitfield. */
17370 = cp_parser_constant_expression (parser,
17371 /*allow_non_constant=*/false,
17374 /* Look for attributes that apply to the bitfield. */
17375 attributes = cp_parser_attributes_opt (parser);
17376 /* Remember which attributes are prefix attributes and
17378 first_attribute = attributes;
17379 /* Combine the attributes. */
17380 attributes = chainon (prefix_attributes, attributes);
17382 /* Create the bitfield declaration. */
17383 decl = grokbitfield (identifier
17384 ? make_id_declarator (NULL_TREE,
17394 cp_declarator *declarator;
17396 tree asm_specification;
17397 int ctor_dtor_or_conv_p;
17399 /* Parse the declarator. */
17401 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17402 &ctor_dtor_or_conv_p,
17403 /*parenthesized_p=*/NULL,
17404 /*member_p=*/true);
17406 /* If something went wrong parsing the declarator, make sure
17407 that we at least consume some tokens. */
17408 if (declarator == cp_error_declarator)
17410 /* Skip to the end of the statement. */
17411 cp_parser_skip_to_end_of_statement (parser);
17412 /* If the next token is not a semicolon, that is
17413 probably because we just skipped over the body of
17414 a function. So, we consume a semicolon if
17415 present, but do not issue an error message if it
17417 if (cp_lexer_next_token_is (parser->lexer,
17419 cp_lexer_consume_token (parser->lexer);
17423 if (declares_class_or_enum & 2)
17424 cp_parser_check_for_definition_in_return_type
17425 (declarator, decl_specifiers.type,
17426 decl_specifiers.type_location);
17428 /* Look for an asm-specification. */
17429 asm_specification = cp_parser_asm_specification_opt (parser);
17430 /* Look for attributes that apply to the declaration. */
17431 attributes = cp_parser_attributes_opt (parser);
17432 /* Remember which attributes are prefix attributes and
17434 first_attribute = attributes;
17435 /* Combine the attributes. */
17436 attributes = chainon (prefix_attributes, attributes);
17438 /* If it's an `=', then we have a constant-initializer or a
17439 pure-specifier. It is not correct to parse the
17440 initializer before registering the member declaration
17441 since the member declaration should be in scope while
17442 its initializer is processed. However, the rest of the
17443 front end does not yet provide an interface that allows
17444 us to handle this correctly. */
17445 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17449 A pure-specifier shall be used only in the declaration of
17450 a virtual function.
17452 A member-declarator can contain a constant-initializer
17453 only if it declares a static member of integral or
17456 Therefore, if the DECLARATOR is for a function, we look
17457 for a pure-specifier; otherwise, we look for a
17458 constant-initializer. When we call `grokfield', it will
17459 perform more stringent semantics checks. */
17460 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17461 if (function_declarator_p (declarator))
17462 initializer = cp_parser_pure_specifier (parser);
17464 /* Parse the initializer. */
17465 initializer = cp_parser_constant_initializer (parser);
17467 /* Otherwise, there is no initializer. */
17469 initializer = NULL_TREE;
17471 /* See if we are probably looking at a function
17472 definition. We are certainly not looking at a
17473 member-declarator. Calling `grokfield' has
17474 side-effects, so we must not do it unless we are sure
17475 that we are looking at a member-declarator. */
17476 if (cp_parser_token_starts_function_definition_p
17477 (cp_lexer_peek_token (parser->lexer)))
17479 /* The grammar does not allow a pure-specifier to be
17480 used when a member function is defined. (It is
17481 possible that this fact is an oversight in the
17482 standard, since a pure function may be defined
17483 outside of the class-specifier. */
17485 error_at (initializer_token_start->location,
17486 "pure-specifier on function-definition");
17487 decl = cp_parser_save_member_function_body (parser,
17491 /* If the member was not a friend, declare it here. */
17493 finish_member_declaration (decl);
17494 /* Peek at the next token. */
17495 token = cp_lexer_peek_token (parser->lexer);
17496 /* If the next token is a semicolon, consume it. */
17497 if (token->type == CPP_SEMICOLON)
17498 cp_lexer_consume_token (parser->lexer);
17502 if (declarator->kind == cdk_function)
17503 declarator->id_loc = token->location;
17504 /* Create the declaration. */
17505 decl = grokfield (declarator, &decl_specifiers,
17506 initializer, /*init_const_expr_p=*/true,
17511 /* Reset PREFIX_ATTRIBUTES. */
17512 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17513 attributes = TREE_CHAIN (attributes);
17515 TREE_CHAIN (attributes) = NULL_TREE;
17517 /* If there is any qualification still in effect, clear it
17518 now; we will be starting fresh with the next declarator. */
17519 parser->scope = NULL_TREE;
17520 parser->qualifying_scope = NULL_TREE;
17521 parser->object_scope = NULL_TREE;
17522 /* If it's a `,', then there are more declarators. */
17523 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17524 cp_lexer_consume_token (parser->lexer);
17525 /* If the next token isn't a `;', then we have a parse error. */
17526 else if (cp_lexer_next_token_is_not (parser->lexer,
17529 cp_parser_error (parser, "expected %<;%>");
17530 /* Skip tokens until we find a `;'. */
17531 cp_parser_skip_to_end_of_statement (parser);
17538 /* Add DECL to the list of members. */
17540 finish_member_declaration (decl);
17542 if (TREE_CODE (decl) == FUNCTION_DECL)
17543 cp_parser_save_default_args (parser, decl);
17548 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17551 /* Parse a pure-specifier.
17556 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17557 Otherwise, ERROR_MARK_NODE is returned. */
17560 cp_parser_pure_specifier (cp_parser* parser)
17564 /* Look for the `=' token. */
17565 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17566 return error_mark_node;
17567 /* Look for the `0' token. */
17568 token = cp_lexer_peek_token (parser->lexer);
17570 if (token->type == CPP_EOF
17571 || token->type == CPP_PRAGMA_EOL)
17572 return error_mark_node;
17574 cp_lexer_consume_token (parser->lexer);
17576 /* Accept = default or = delete in c++0x mode. */
17577 if (token->keyword == RID_DEFAULT
17578 || token->keyword == RID_DELETE)
17580 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17581 return token->u.value;
17584 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17585 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17587 cp_parser_error (parser,
17588 "invalid pure specifier (only %<= 0%> is allowed)");
17589 cp_parser_skip_to_end_of_statement (parser);
17590 return error_mark_node;
17592 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17594 error_at (token->location, "templates may not be %<virtual%>");
17595 return error_mark_node;
17598 return integer_zero_node;
17601 /* Parse a constant-initializer.
17603 constant-initializer:
17604 = constant-expression
17606 Returns a representation of the constant-expression. */
17609 cp_parser_constant_initializer (cp_parser* parser)
17611 /* Look for the `=' token. */
17612 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17613 return error_mark_node;
17615 /* It is invalid to write:
17617 struct S { static const int i = { 7 }; };
17620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17622 cp_parser_error (parser,
17623 "a brace-enclosed initializer is not allowed here");
17624 /* Consume the opening brace. */
17625 cp_lexer_consume_token (parser->lexer);
17626 /* Skip the initializer. */
17627 cp_parser_skip_to_closing_brace (parser);
17628 /* Look for the trailing `}'. */
17629 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17631 return error_mark_node;
17634 return cp_parser_constant_expression (parser,
17635 /*allow_non_constant=*/false,
17639 /* Derived classes [gram.class.derived] */
17641 /* Parse a base-clause.
17644 : base-specifier-list
17646 base-specifier-list:
17647 base-specifier ... [opt]
17648 base-specifier-list , base-specifier ... [opt]
17650 Returns a TREE_LIST representing the base-classes, in the order in
17651 which they were declared. The representation of each node is as
17652 described by cp_parser_base_specifier.
17654 In the case that no bases are specified, this function will return
17655 NULL_TREE, not ERROR_MARK_NODE. */
17658 cp_parser_base_clause (cp_parser* parser)
17660 tree bases = NULL_TREE;
17662 /* Look for the `:' that begins the list. */
17663 cp_parser_require (parser, CPP_COLON, RT_COLON);
17665 /* Scan the base-specifier-list. */
17670 bool pack_expansion_p = false;
17672 /* Look for the base-specifier. */
17673 base = cp_parser_base_specifier (parser);
17674 /* Look for the (optional) ellipsis. */
17675 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17677 /* Consume the `...'. */
17678 cp_lexer_consume_token (parser->lexer);
17680 pack_expansion_p = true;
17683 /* Add BASE to the front of the list. */
17684 if (base != error_mark_node)
17686 if (pack_expansion_p)
17687 /* Make this a pack expansion type. */
17688 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17691 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17693 TREE_CHAIN (base) = bases;
17697 /* Peek at the next token. */
17698 token = cp_lexer_peek_token (parser->lexer);
17699 /* If it's not a comma, then the list is complete. */
17700 if (token->type != CPP_COMMA)
17702 /* Consume the `,'. */
17703 cp_lexer_consume_token (parser->lexer);
17706 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17707 base class had a qualified name. However, the next name that
17708 appears is certainly not qualified. */
17709 parser->scope = NULL_TREE;
17710 parser->qualifying_scope = NULL_TREE;
17711 parser->object_scope = NULL_TREE;
17713 return nreverse (bases);
17716 /* Parse a base-specifier.
17719 :: [opt] nested-name-specifier [opt] class-name
17720 virtual access-specifier [opt] :: [opt] nested-name-specifier
17722 access-specifier virtual [opt] :: [opt] nested-name-specifier
17725 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17726 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17727 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17728 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17731 cp_parser_base_specifier (cp_parser* parser)
17735 bool virtual_p = false;
17736 bool duplicate_virtual_error_issued_p = false;
17737 bool duplicate_access_error_issued_p = false;
17738 bool class_scope_p, template_p;
17739 tree access = access_default_node;
17742 /* Process the optional `virtual' and `access-specifier'. */
17745 /* Peek at the next token. */
17746 token = cp_lexer_peek_token (parser->lexer);
17747 /* Process `virtual'. */
17748 switch (token->keyword)
17751 /* If `virtual' appears more than once, issue an error. */
17752 if (virtual_p && !duplicate_virtual_error_issued_p)
17754 cp_parser_error (parser,
17755 "%<virtual%> specified more than once in base-specified");
17756 duplicate_virtual_error_issued_p = true;
17761 /* Consume the `virtual' token. */
17762 cp_lexer_consume_token (parser->lexer);
17767 case RID_PROTECTED:
17769 /* If more than one access specifier appears, issue an
17771 if (access != access_default_node
17772 && !duplicate_access_error_issued_p)
17774 cp_parser_error (parser,
17775 "more than one access specifier in base-specified");
17776 duplicate_access_error_issued_p = true;
17779 access = ridpointers[(int) token->keyword];
17781 /* Consume the access-specifier. */
17782 cp_lexer_consume_token (parser->lexer);
17791 /* It is not uncommon to see programs mechanically, erroneously, use
17792 the 'typename' keyword to denote (dependent) qualified types
17793 as base classes. */
17794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17796 token = cp_lexer_peek_token (parser->lexer);
17797 if (!processing_template_decl)
17798 error_at (token->location,
17799 "keyword %<typename%> not allowed outside of templates");
17801 error_at (token->location,
17802 "keyword %<typename%> not allowed in this context "
17803 "(the base class is implicitly a type)");
17804 cp_lexer_consume_token (parser->lexer);
17807 /* Look for the optional `::' operator. */
17808 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17809 /* Look for the nested-name-specifier. The simplest way to
17814 The keyword `typename' is not permitted in a base-specifier or
17815 mem-initializer; in these contexts a qualified name that
17816 depends on a template-parameter is implicitly assumed to be a
17819 is to pretend that we have seen the `typename' keyword at this
17821 cp_parser_nested_name_specifier_opt (parser,
17822 /*typename_keyword_p=*/true,
17823 /*check_dependency_p=*/true,
17825 /*is_declaration=*/true);
17826 /* If the base class is given by a qualified name, assume that names
17827 we see are type names or templates, as appropriate. */
17828 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17829 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17831 /* Finally, look for the class-name. */
17832 type = cp_parser_class_name (parser,
17836 /*check_dependency_p=*/true,
17837 /*class_head_p=*/false,
17838 /*is_declaration=*/true);
17840 if (type == error_mark_node)
17841 return error_mark_node;
17843 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17846 /* Exception handling [gram.exception] */
17848 /* Parse an (optional) exception-specification.
17850 exception-specification:
17851 throw ( type-id-list [opt] )
17853 Returns a TREE_LIST representing the exception-specification. The
17854 TREE_VALUE of each node is a type. */
17857 cp_parser_exception_specification_opt (cp_parser* parser)
17861 const char *saved_message;
17863 /* Peek at the next token. */
17864 token = cp_lexer_peek_token (parser->lexer);
17866 /* Is it a noexcept-specification? */
17867 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
17870 cp_lexer_consume_token (parser->lexer);
17872 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
17874 cp_lexer_consume_token (parser->lexer);
17876 /* Types may not be defined in an exception-specification. */
17877 saved_message = parser->type_definition_forbidden_message;
17878 parser->type_definition_forbidden_message
17879 = G_("types may not be defined in an exception-specification");
17881 expr = cp_parser_constant_expression (parser, false, NULL);
17883 /* Restore the saved message. */
17884 parser->type_definition_forbidden_message = saved_message;
17886 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17889 expr = boolean_true_node;
17891 return build_noexcept_spec (expr, tf_warning_or_error);
17894 /* If it's not `throw', then there's no exception-specification. */
17895 if (!cp_parser_is_keyword (token, RID_THROW))
17899 /* Enable this once a lot of code has transitioned to noexcept? */
17900 if (cxx_dialect == cxx0x && !in_system_header)
17901 warning (OPT_Wdeprecated, "dynamic exception specifications are "
17902 "deprecated in C++0x; use %<noexcept%> instead.");
17905 /* Consume the `throw'. */
17906 cp_lexer_consume_token (parser->lexer);
17908 /* Look for the `('. */
17909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
17911 /* Peek at the next token. */
17912 token = cp_lexer_peek_token (parser->lexer);
17913 /* If it's not a `)', then there is a type-id-list. */
17914 if (token->type != CPP_CLOSE_PAREN)
17916 /* Types may not be defined in an exception-specification. */
17917 saved_message = parser->type_definition_forbidden_message;
17918 parser->type_definition_forbidden_message
17919 = G_("types may not be defined in an exception-specification");
17920 /* Parse the type-id-list. */
17921 type_id_list = cp_parser_type_id_list (parser);
17922 /* Restore the saved message. */
17923 parser->type_definition_forbidden_message = saved_message;
17926 type_id_list = empty_except_spec;
17928 /* Look for the `)'. */
17929 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17931 return type_id_list;
17934 /* Parse an (optional) type-id-list.
17938 type-id-list , type-id ... [opt]
17940 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17941 in the order that the types were presented. */
17944 cp_parser_type_id_list (cp_parser* parser)
17946 tree types = NULL_TREE;
17953 /* Get the next type-id. */
17954 type = cp_parser_type_id (parser);
17955 /* Parse the optional ellipsis. */
17956 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17958 /* Consume the `...'. */
17959 cp_lexer_consume_token (parser->lexer);
17961 /* Turn the type into a pack expansion expression. */
17962 type = make_pack_expansion (type);
17964 /* Add it to the list. */
17965 types = add_exception_specifier (types, type, /*complain=*/1);
17966 /* Peek at the next token. */
17967 token = cp_lexer_peek_token (parser->lexer);
17968 /* If it is not a `,', we are done. */
17969 if (token->type != CPP_COMMA)
17971 /* Consume the `,'. */
17972 cp_lexer_consume_token (parser->lexer);
17975 return nreverse (types);
17978 /* Parse a try-block.
17981 try compound-statement handler-seq */
17984 cp_parser_try_block (cp_parser* parser)
17988 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
17989 try_block = begin_try_block ();
17990 cp_parser_compound_statement (parser, NULL, true);
17991 finish_try_block (try_block);
17992 cp_parser_handler_seq (parser);
17993 finish_handler_sequence (try_block);
17998 /* Parse a function-try-block.
18000 function-try-block:
18001 try ctor-initializer [opt] function-body handler-seq */
18004 cp_parser_function_try_block (cp_parser* parser)
18006 tree compound_stmt;
18008 bool ctor_initializer_p;
18010 /* Look for the `try' keyword. */
18011 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18013 /* Let the rest of the front end know where we are. */
18014 try_block = begin_function_try_block (&compound_stmt);
18015 /* Parse the function-body. */
18017 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18018 /* We're done with the `try' part. */
18019 finish_function_try_block (try_block);
18020 /* Parse the handlers. */
18021 cp_parser_handler_seq (parser);
18022 /* We're done with the handlers. */
18023 finish_function_handler_sequence (try_block, compound_stmt);
18025 return ctor_initializer_p;
18028 /* Parse a handler-seq.
18031 handler handler-seq [opt] */
18034 cp_parser_handler_seq (cp_parser* parser)
18040 /* Parse the handler. */
18041 cp_parser_handler (parser);
18042 /* Peek at the next token. */
18043 token = cp_lexer_peek_token (parser->lexer);
18044 /* If it's not `catch' then there are no more handlers. */
18045 if (!cp_parser_is_keyword (token, RID_CATCH))
18050 /* Parse a handler.
18053 catch ( exception-declaration ) compound-statement */
18056 cp_parser_handler (cp_parser* parser)
18061 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18062 handler = begin_handler ();
18063 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18064 declaration = cp_parser_exception_declaration (parser);
18065 finish_handler_parms (declaration, handler);
18066 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18067 cp_parser_compound_statement (parser, NULL, false);
18068 finish_handler (handler);
18071 /* Parse an exception-declaration.
18073 exception-declaration:
18074 type-specifier-seq declarator
18075 type-specifier-seq abstract-declarator
18079 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18080 ellipsis variant is used. */
18083 cp_parser_exception_declaration (cp_parser* parser)
18085 cp_decl_specifier_seq type_specifiers;
18086 cp_declarator *declarator;
18087 const char *saved_message;
18089 /* If it's an ellipsis, it's easy to handle. */
18090 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18092 /* Consume the `...' token. */
18093 cp_lexer_consume_token (parser->lexer);
18097 /* Types may not be defined in exception-declarations. */
18098 saved_message = parser->type_definition_forbidden_message;
18099 parser->type_definition_forbidden_message
18100 = G_("types may not be defined in exception-declarations");
18102 /* Parse the type-specifier-seq. */
18103 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18104 /*is_trailing_return=*/false,
18106 /* If it's a `)', then there is no declarator. */
18107 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18110 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18111 /*ctor_dtor_or_conv_p=*/NULL,
18112 /*parenthesized_p=*/NULL,
18113 /*member_p=*/false);
18115 /* Restore the saved message. */
18116 parser->type_definition_forbidden_message = saved_message;
18118 if (!type_specifiers.any_specifiers_p)
18119 return error_mark_node;
18121 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18124 /* Parse a throw-expression.
18127 throw assignment-expression [opt]
18129 Returns a THROW_EXPR representing the throw-expression. */
18132 cp_parser_throw_expression (cp_parser* parser)
18137 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18138 token = cp_lexer_peek_token (parser->lexer);
18139 /* Figure out whether or not there is an assignment-expression
18140 following the "throw" keyword. */
18141 if (token->type == CPP_COMMA
18142 || token->type == CPP_SEMICOLON
18143 || token->type == CPP_CLOSE_PAREN
18144 || token->type == CPP_CLOSE_SQUARE
18145 || token->type == CPP_CLOSE_BRACE
18146 || token->type == CPP_COLON)
18147 expression = NULL_TREE;
18149 expression = cp_parser_assignment_expression (parser,
18150 /*cast_p=*/false, NULL);
18152 return build_throw (expression);
18155 /* GNU Extensions */
18157 /* Parse an (optional) asm-specification.
18160 asm ( string-literal )
18162 If the asm-specification is present, returns a STRING_CST
18163 corresponding to the string-literal. Otherwise, returns
18167 cp_parser_asm_specification_opt (cp_parser* parser)
18170 tree asm_specification;
18172 /* Peek at the next token. */
18173 token = cp_lexer_peek_token (parser->lexer);
18174 /* If the next token isn't the `asm' keyword, then there's no
18175 asm-specification. */
18176 if (!cp_parser_is_keyword (token, RID_ASM))
18179 /* Consume the `asm' token. */
18180 cp_lexer_consume_token (parser->lexer);
18181 /* Look for the `('. */
18182 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18184 /* Look for the string-literal. */
18185 asm_specification = cp_parser_string_literal (parser, false, false);
18187 /* Look for the `)'. */
18188 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18190 return asm_specification;
18193 /* Parse an asm-operand-list.
18197 asm-operand-list , asm-operand
18200 string-literal ( expression )
18201 [ string-literal ] string-literal ( expression )
18203 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18204 each node is the expression. The TREE_PURPOSE is itself a
18205 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18206 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18207 is a STRING_CST for the string literal before the parenthesis. Returns
18208 ERROR_MARK_NODE if any of the operands are invalid. */
18211 cp_parser_asm_operand_list (cp_parser* parser)
18213 tree asm_operands = NULL_TREE;
18214 bool invalid_operands = false;
18218 tree string_literal;
18222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18224 /* Consume the `[' token. */
18225 cp_lexer_consume_token (parser->lexer);
18226 /* Read the operand name. */
18227 name = cp_parser_identifier (parser);
18228 if (name != error_mark_node)
18229 name = build_string (IDENTIFIER_LENGTH (name),
18230 IDENTIFIER_POINTER (name));
18231 /* Look for the closing `]'. */
18232 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18236 /* Look for the string-literal. */
18237 string_literal = cp_parser_string_literal (parser, false, false);
18239 /* Look for the `('. */
18240 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18241 /* Parse the expression. */
18242 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18243 /* Look for the `)'. */
18244 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18246 if (name == error_mark_node
18247 || string_literal == error_mark_node
18248 || expression == error_mark_node)
18249 invalid_operands = true;
18251 /* Add this operand to the list. */
18252 asm_operands = tree_cons (build_tree_list (name, string_literal),
18255 /* If the next token is not a `,', there are no more
18257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18259 /* Consume the `,'. */
18260 cp_lexer_consume_token (parser->lexer);
18263 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18266 /* Parse an asm-clobber-list.
18270 asm-clobber-list , string-literal
18272 Returns a TREE_LIST, indicating the clobbers in the order that they
18273 appeared. The TREE_VALUE of each node is a STRING_CST. */
18276 cp_parser_asm_clobber_list (cp_parser* parser)
18278 tree clobbers = NULL_TREE;
18282 tree string_literal;
18284 /* Look for the string literal. */
18285 string_literal = cp_parser_string_literal (parser, false, false);
18286 /* Add it to the list. */
18287 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18288 /* If the next token is not a `,', then the list is
18290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18292 /* Consume the `,' token. */
18293 cp_lexer_consume_token (parser->lexer);
18299 /* Parse an asm-label-list.
18303 asm-label-list , identifier
18305 Returns a TREE_LIST, indicating the labels in the order that they
18306 appeared. The TREE_VALUE of each node is a label. */
18309 cp_parser_asm_label_list (cp_parser* parser)
18311 tree labels = NULL_TREE;
18315 tree identifier, label, name;
18317 /* Look for the identifier. */
18318 identifier = cp_parser_identifier (parser);
18319 if (!error_operand_p (identifier))
18321 label = lookup_label (identifier);
18322 if (TREE_CODE (label) == LABEL_DECL)
18324 TREE_USED (label) = 1;
18325 check_goto (label);
18326 name = build_string (IDENTIFIER_LENGTH (identifier),
18327 IDENTIFIER_POINTER (identifier));
18328 labels = tree_cons (name, label, labels);
18331 /* If the next token is not a `,', then the list is
18333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18335 /* Consume the `,' token. */
18336 cp_lexer_consume_token (parser->lexer);
18339 return nreverse (labels);
18342 /* Parse an (optional) series of attributes.
18345 attributes attribute
18348 __attribute__ (( attribute-list [opt] ))
18350 The return value is as for cp_parser_attribute_list. */
18353 cp_parser_attributes_opt (cp_parser* parser)
18355 tree attributes = NULL_TREE;
18360 tree attribute_list;
18362 /* Peek at the next token. */
18363 token = cp_lexer_peek_token (parser->lexer);
18364 /* If it's not `__attribute__', then we're done. */
18365 if (token->keyword != RID_ATTRIBUTE)
18368 /* Consume the `__attribute__' keyword. */
18369 cp_lexer_consume_token (parser->lexer);
18370 /* Look for the two `(' tokens. */
18371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18372 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18374 /* Peek at the next token. */
18375 token = cp_lexer_peek_token (parser->lexer);
18376 if (token->type != CPP_CLOSE_PAREN)
18377 /* Parse the attribute-list. */
18378 attribute_list = cp_parser_attribute_list (parser);
18380 /* If the next token is a `)', then there is no attribute
18382 attribute_list = NULL;
18384 /* Look for the two `)' tokens. */
18385 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18386 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18388 /* Add these new attributes to the list. */
18389 attributes = chainon (attributes, attribute_list);
18395 /* Parse an attribute-list.
18399 attribute-list , attribute
18403 identifier ( identifier )
18404 identifier ( identifier , expression-list )
18405 identifier ( expression-list )
18407 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18408 to an attribute. The TREE_PURPOSE of each node is the identifier
18409 indicating which attribute is in use. The TREE_VALUE represents
18410 the arguments, if any. */
18413 cp_parser_attribute_list (cp_parser* parser)
18415 tree attribute_list = NULL_TREE;
18416 bool save_translate_strings_p = parser->translate_strings_p;
18418 parser->translate_strings_p = false;
18425 /* Look for the identifier. We also allow keywords here; for
18426 example `__attribute__ ((const))' is legal. */
18427 token = cp_lexer_peek_token (parser->lexer);
18428 if (token->type == CPP_NAME
18429 || token->type == CPP_KEYWORD)
18431 tree arguments = NULL_TREE;
18433 /* Consume the token. */
18434 token = cp_lexer_consume_token (parser->lexer);
18436 /* Save away the identifier that indicates which attribute
18438 identifier = (token->type == CPP_KEYWORD)
18439 /* For keywords, use the canonical spelling, not the
18440 parsed identifier. */
18441 ? ridpointers[(int) token->keyword]
18444 attribute = build_tree_list (identifier, NULL_TREE);
18446 /* Peek at the next token. */
18447 token = cp_lexer_peek_token (parser->lexer);
18448 /* If it's an `(', then parse the attribute arguments. */
18449 if (token->type == CPP_OPEN_PAREN)
18452 int attr_flag = (attribute_takes_identifier_p (identifier)
18453 ? id_attr : normal_attr);
18454 vec = cp_parser_parenthesized_expression_list
18455 (parser, attr_flag, /*cast_p=*/false,
18456 /*allow_expansion_p=*/false,
18457 /*non_constant_p=*/NULL);
18459 arguments = error_mark_node;
18462 arguments = build_tree_list_vec (vec);
18463 release_tree_vector (vec);
18465 /* Save the arguments away. */
18466 TREE_VALUE (attribute) = arguments;
18469 if (arguments != error_mark_node)
18471 /* Add this attribute to the list. */
18472 TREE_CHAIN (attribute) = attribute_list;
18473 attribute_list = attribute;
18476 token = cp_lexer_peek_token (parser->lexer);
18478 /* Now, look for more attributes. If the next token isn't a
18479 `,', we're done. */
18480 if (token->type != CPP_COMMA)
18483 /* Consume the comma and keep going. */
18484 cp_lexer_consume_token (parser->lexer);
18486 parser->translate_strings_p = save_translate_strings_p;
18488 /* We built up the list in reverse order. */
18489 return nreverse (attribute_list);
18492 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18493 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18494 current value of the PEDANTIC flag, regardless of whether or not
18495 the `__extension__' keyword is present. The caller is responsible
18496 for restoring the value of the PEDANTIC flag. */
18499 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18501 /* Save the old value of the PEDANTIC flag. */
18502 *saved_pedantic = pedantic;
18504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18506 /* Consume the `__extension__' token. */
18507 cp_lexer_consume_token (parser->lexer);
18508 /* We're not being pedantic while the `__extension__' keyword is
18518 /* Parse a label declaration.
18521 __label__ label-declarator-seq ;
18523 label-declarator-seq:
18524 identifier , label-declarator-seq
18528 cp_parser_label_declaration (cp_parser* parser)
18530 /* Look for the `__label__' keyword. */
18531 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18537 /* Look for an identifier. */
18538 identifier = cp_parser_identifier (parser);
18539 /* If we failed, stop. */
18540 if (identifier == error_mark_node)
18542 /* Declare it as a label. */
18543 finish_label_decl (identifier);
18544 /* If the next token is a `;', stop. */
18545 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18547 /* Look for the `,' separating the label declarations. */
18548 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18551 /* Look for the final `;'. */
18552 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18555 /* Support Functions */
18557 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18558 NAME should have one of the representations used for an
18559 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18560 is returned. If PARSER->SCOPE is a dependent type, then a
18561 SCOPE_REF is returned.
18563 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18564 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18565 was formed. Abstractly, such entities should not be passed to this
18566 function, because they do not need to be looked up, but it is
18567 simpler to check for this special case here, rather than at the
18570 In cases not explicitly covered above, this function returns a
18571 DECL, OVERLOAD, or baselink representing the result of the lookup.
18572 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18575 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18576 (e.g., "struct") that was used. In that case bindings that do not
18577 refer to types are ignored.
18579 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18582 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18585 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18588 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18589 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18590 NULL_TREE otherwise. */
18593 cp_parser_lookup_name (cp_parser *parser, tree name,
18594 enum tag_types tag_type,
18597 bool check_dependency,
18598 tree *ambiguous_decls,
18599 location_t name_location)
18603 tree object_type = parser->context->object_type;
18605 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18606 flags |= LOOKUP_COMPLAIN;
18608 /* Assume that the lookup will be unambiguous. */
18609 if (ambiguous_decls)
18610 *ambiguous_decls = NULL_TREE;
18612 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18613 no longer valid. Note that if we are parsing tentatively, and
18614 the parse fails, OBJECT_TYPE will be automatically restored. */
18615 parser->context->object_type = NULL_TREE;
18617 if (name == error_mark_node)
18618 return error_mark_node;
18620 /* A template-id has already been resolved; there is no lookup to
18622 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18624 if (BASELINK_P (name))
18626 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18627 == TEMPLATE_ID_EXPR);
18631 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18632 it should already have been checked to make sure that the name
18633 used matches the type being destroyed. */
18634 if (TREE_CODE (name) == BIT_NOT_EXPR)
18638 /* Figure out to which type this destructor applies. */
18640 type = parser->scope;
18641 else if (object_type)
18642 type = object_type;
18644 type = current_class_type;
18645 /* If that's not a class type, there is no destructor. */
18646 if (!type || !CLASS_TYPE_P (type))
18647 return error_mark_node;
18648 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18649 lazily_declare_fn (sfk_destructor, type);
18650 if (!CLASSTYPE_DESTRUCTORS (type))
18651 return error_mark_node;
18652 /* If it was a class type, return the destructor. */
18653 return CLASSTYPE_DESTRUCTORS (type);
18656 /* By this point, the NAME should be an ordinary identifier. If
18657 the id-expression was a qualified name, the qualifying scope is
18658 stored in PARSER->SCOPE at this point. */
18659 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18661 /* Perform the lookup. */
18666 if (parser->scope == error_mark_node)
18667 return error_mark_node;
18669 /* If the SCOPE is dependent, the lookup must be deferred until
18670 the template is instantiated -- unless we are explicitly
18671 looking up names in uninstantiated templates. Even then, we
18672 cannot look up the name if the scope is not a class type; it
18673 might, for example, be a template type parameter. */
18674 dependent_p = (TYPE_P (parser->scope)
18675 && dependent_scope_p (parser->scope));
18676 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18678 /* Defer lookup. */
18679 decl = error_mark_node;
18682 tree pushed_scope = NULL_TREE;
18684 /* If PARSER->SCOPE is a dependent type, then it must be a
18685 class type, and we must not be checking dependencies;
18686 otherwise, we would have processed this lookup above. So
18687 that PARSER->SCOPE is not considered a dependent base by
18688 lookup_member, we must enter the scope here. */
18690 pushed_scope = push_scope (parser->scope);
18692 /* If the PARSER->SCOPE is a template specialization, it
18693 may be instantiated during name lookup. In that case,
18694 errors may be issued. Even if we rollback the current
18695 tentative parse, those errors are valid. */
18696 decl = lookup_qualified_name (parser->scope, name,
18697 tag_type != none_type,
18698 /*complain=*/true);
18700 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18701 lookup result and the nested-name-specifier nominates a class C:
18702 * if the name specified after the nested-name-specifier, when
18703 looked up in C, is the injected-class-name of C (Clause 9), or
18704 * if the name specified after the nested-name-specifier is the
18705 same as the identifier or the simple-template-id's template-
18706 name in the last component of the nested-name-specifier,
18707 the name is instead considered to name the constructor of
18708 class C. [ Note: for example, the constructor is not an
18709 acceptable lookup result in an elaborated-type-specifier so
18710 the constructor would not be used in place of the
18711 injected-class-name. --end note ] Such a constructor name
18712 shall be used only in the declarator-id of a declaration that
18713 names a constructor or in a using-declaration. */
18714 if (tag_type == none_type
18715 && DECL_SELF_REFERENCE_P (decl)
18716 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18717 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18718 tag_type != none_type,
18719 /*complain=*/true);
18721 /* If we have a single function from a using decl, pull it out. */
18722 if (TREE_CODE (decl) == OVERLOAD
18723 && !really_overloaded_fn (decl))
18724 decl = OVL_FUNCTION (decl);
18727 pop_scope (pushed_scope);
18730 /* If the scope is a dependent type and either we deferred lookup or
18731 we did lookup but didn't find the name, rememeber the name. */
18732 if (decl == error_mark_node && TYPE_P (parser->scope)
18733 && dependent_type_p (parser->scope))
18739 /* The resolution to Core Issue 180 says that `struct
18740 A::B' should be considered a type-name, even if `A'
18742 type = make_typename_type (parser->scope, name, tag_type,
18743 /*complain=*/tf_error);
18744 decl = TYPE_NAME (type);
18746 else if (is_template
18747 && (cp_parser_next_token_ends_template_argument_p (parser)
18748 || cp_lexer_next_token_is (parser->lexer,
18750 decl = make_unbound_class_template (parser->scope,
18752 /*complain=*/tf_error);
18754 decl = build_qualified_name (/*type=*/NULL_TREE,
18755 parser->scope, name,
18758 parser->qualifying_scope = parser->scope;
18759 parser->object_scope = NULL_TREE;
18761 else if (object_type)
18763 tree object_decl = NULL_TREE;
18764 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18765 OBJECT_TYPE is not a class. */
18766 if (CLASS_TYPE_P (object_type))
18767 /* If the OBJECT_TYPE is a template specialization, it may
18768 be instantiated during name lookup. In that case, errors
18769 may be issued. Even if we rollback the current tentative
18770 parse, those errors are valid. */
18771 object_decl = lookup_member (object_type,
18774 tag_type != none_type);
18775 /* Look it up in the enclosing context, too. */
18776 decl = lookup_name_real (name, tag_type != none_type,
18778 /*block_p=*/true, is_namespace, flags);
18779 parser->object_scope = object_type;
18780 parser->qualifying_scope = NULL_TREE;
18782 decl = object_decl;
18786 decl = lookup_name_real (name, tag_type != none_type,
18788 /*block_p=*/true, is_namespace, flags);
18789 parser->qualifying_scope = NULL_TREE;
18790 parser->object_scope = NULL_TREE;
18793 /* If the lookup failed, let our caller know. */
18794 if (!decl || decl == error_mark_node)
18795 return error_mark_node;
18797 /* Pull out the template from an injected-class-name (or multiple). */
18799 decl = maybe_get_template_decl_from_type_decl (decl);
18801 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18802 if (TREE_CODE (decl) == TREE_LIST)
18804 if (ambiguous_decls)
18805 *ambiguous_decls = decl;
18806 /* The error message we have to print is too complicated for
18807 cp_parser_error, so we incorporate its actions directly. */
18808 if (!cp_parser_simulate_error (parser))
18810 error_at (name_location, "reference to %qD is ambiguous",
18812 print_candidates (decl);
18814 return error_mark_node;
18817 gcc_assert (DECL_P (decl)
18818 || TREE_CODE (decl) == OVERLOAD
18819 || TREE_CODE (decl) == SCOPE_REF
18820 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18821 || BASELINK_P (decl));
18823 /* If we have resolved the name of a member declaration, check to
18824 see if the declaration is accessible. When the name resolves to
18825 set of overloaded functions, accessibility is checked when
18826 overload resolution is done.
18828 During an explicit instantiation, access is not checked at all,
18829 as per [temp.explicit]. */
18831 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18836 /* Like cp_parser_lookup_name, but for use in the typical case where
18837 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18838 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18841 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18843 return cp_parser_lookup_name (parser, name,
18845 /*is_template=*/false,
18846 /*is_namespace=*/false,
18847 /*check_dependency=*/true,
18848 /*ambiguous_decls=*/NULL,
18852 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18853 the current context, return the TYPE_DECL. If TAG_NAME_P is
18854 true, the DECL indicates the class being defined in a class-head,
18855 or declared in an elaborated-type-specifier.
18857 Otherwise, return DECL. */
18860 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18862 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18863 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18866 template <typename T> struct B;
18869 template <typename T> struct A::B {};
18871 Similarly, in an elaborated-type-specifier:
18873 namespace N { struct X{}; }
18876 template <typename T> friend struct N::X;
18879 However, if the DECL refers to a class type, and we are in
18880 the scope of the class, then the name lookup automatically
18881 finds the TYPE_DECL created by build_self_reference rather
18882 than a TEMPLATE_DECL. For example, in:
18884 template <class T> struct S {
18888 there is no need to handle such case. */
18890 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18891 return DECL_TEMPLATE_RESULT (decl);
18896 /* If too many, or too few, template-parameter lists apply to the
18897 declarator, issue an error message. Returns TRUE if all went well,
18898 and FALSE otherwise. */
18901 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18902 cp_declarator *declarator,
18903 location_t declarator_location)
18905 unsigned num_templates;
18907 /* We haven't seen any classes that involve template parameters yet. */
18910 switch (declarator->kind)
18913 if (declarator->u.id.qualifying_scope)
18917 scope = declarator->u.id.qualifying_scope;
18919 while (scope && CLASS_TYPE_P (scope))
18921 /* You're supposed to have one `template <...>'
18922 for every template class, but you don't need one
18923 for a full specialization. For example:
18925 template <class T> struct S{};
18926 template <> struct S<int> { void f(); };
18927 void S<int>::f () {}
18929 is correct; there shouldn't be a `template <>' for
18930 the definition of `S<int>::f'. */
18931 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18932 /* If SCOPE does not have template information of any
18933 kind, then it is not a template, nor is it nested
18934 within a template. */
18936 if (explicit_class_specialization_p (scope))
18938 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18941 scope = TYPE_CONTEXT (scope);
18944 else if (TREE_CODE (declarator->u.id.unqualified_name)
18945 == TEMPLATE_ID_EXPR)
18946 /* If the DECLARATOR has the form `X<y>' then it uses one
18947 additional level of template parameters. */
18950 return cp_parser_check_template_parameters
18951 (parser, num_templates, declarator_location, declarator);
18957 case cdk_reference:
18959 return (cp_parser_check_declarator_template_parameters
18960 (parser, declarator->declarator, declarator_location));
18966 gcc_unreachable ();
18971 /* NUM_TEMPLATES were used in the current declaration. If that is
18972 invalid, return FALSE and issue an error messages. Otherwise,
18973 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18974 declarator and we can print more accurate diagnostics. */
18977 cp_parser_check_template_parameters (cp_parser* parser,
18978 unsigned num_templates,
18979 location_t location,
18980 cp_declarator *declarator)
18982 /* If there are the same number of template classes and parameter
18983 lists, that's OK. */
18984 if (parser->num_template_parameter_lists == num_templates)
18986 /* If there are more, but only one more, then we are referring to a
18987 member template. That's OK too. */
18988 if (parser->num_template_parameter_lists == num_templates + 1)
18990 /* If there are more template classes than parameter lists, we have
18993 template <class T> void S<T>::R<T>::f (); */
18994 if (parser->num_template_parameter_lists < num_templates)
18996 if (declarator && !current_function_decl)
18997 error_at (location, "specializing member %<%T::%E%> "
18998 "requires %<template<>%> syntax",
18999 declarator->u.id.qualifying_scope,
19000 declarator->u.id.unqualified_name);
19001 else if (declarator)
19002 error_at (location, "invalid declaration of %<%T::%E%>",
19003 declarator->u.id.qualifying_scope,
19004 declarator->u.id.unqualified_name);
19006 error_at (location, "too few template-parameter-lists");
19009 /* Otherwise, there are too many template parameter lists. We have
19012 template <class T> template <class U> void S::f(); */
19013 error_at (location, "too many template-parameter-lists");
19017 /* Parse an optional `::' token indicating that the following name is
19018 from the global namespace. If so, PARSER->SCOPE is set to the
19019 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19020 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19021 Returns the new value of PARSER->SCOPE, if the `::' token is
19022 present, and NULL_TREE otherwise. */
19025 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19029 /* Peek at the next token. */
19030 token = cp_lexer_peek_token (parser->lexer);
19031 /* If we're looking at a `::' token then we're starting from the
19032 global namespace, not our current location. */
19033 if (token->type == CPP_SCOPE)
19035 /* Consume the `::' token. */
19036 cp_lexer_consume_token (parser->lexer);
19037 /* Set the SCOPE so that we know where to start the lookup. */
19038 parser->scope = global_namespace;
19039 parser->qualifying_scope = global_namespace;
19040 parser->object_scope = NULL_TREE;
19042 return parser->scope;
19044 else if (!current_scope_valid_p)
19046 parser->scope = NULL_TREE;
19047 parser->qualifying_scope = NULL_TREE;
19048 parser->object_scope = NULL_TREE;
19054 /* Returns TRUE if the upcoming token sequence is the start of a
19055 constructor declarator. If FRIEND_P is true, the declarator is
19056 preceded by the `friend' specifier. */
19059 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19061 bool constructor_p;
19062 tree nested_name_specifier;
19063 cp_token *next_token;
19065 /* The common case is that this is not a constructor declarator, so
19066 try to avoid doing lots of work if at all possible. It's not
19067 valid declare a constructor at function scope. */
19068 if (parser->in_function_body)
19070 /* And only certain tokens can begin a constructor declarator. */
19071 next_token = cp_lexer_peek_token (parser->lexer);
19072 if (next_token->type != CPP_NAME
19073 && next_token->type != CPP_SCOPE
19074 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19075 && next_token->type != CPP_TEMPLATE_ID)
19078 /* Parse tentatively; we are going to roll back all of the tokens
19080 cp_parser_parse_tentatively (parser);
19081 /* Assume that we are looking at a constructor declarator. */
19082 constructor_p = true;
19084 /* Look for the optional `::' operator. */
19085 cp_parser_global_scope_opt (parser,
19086 /*current_scope_valid_p=*/false);
19087 /* Look for the nested-name-specifier. */
19088 nested_name_specifier
19089 = (cp_parser_nested_name_specifier_opt (parser,
19090 /*typename_keyword_p=*/false,
19091 /*check_dependency_p=*/false,
19093 /*is_declaration=*/false));
19094 /* Outside of a class-specifier, there must be a
19095 nested-name-specifier. */
19096 if (!nested_name_specifier &&
19097 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19099 constructor_p = false;
19100 else if (nested_name_specifier == error_mark_node)
19101 constructor_p = false;
19103 /* If we have a class scope, this is easy; DR 147 says that S::S always
19104 names the constructor, and no other qualified name could. */
19105 if (constructor_p && nested_name_specifier
19106 && TYPE_P (nested_name_specifier))
19108 tree id = cp_parser_unqualified_id (parser,
19109 /*template_keyword_p=*/false,
19110 /*check_dependency_p=*/false,
19111 /*declarator_p=*/true,
19112 /*optional_p=*/false);
19113 if (is_overloaded_fn (id))
19114 id = DECL_NAME (get_first_fn (id));
19115 if (!constructor_name_p (id, nested_name_specifier))
19116 constructor_p = false;
19118 /* If we still think that this might be a constructor-declarator,
19119 look for a class-name. */
19120 else if (constructor_p)
19124 template <typename T> struct S {
19128 we must recognize that the nested `S' names a class. */
19130 type_decl = cp_parser_class_name (parser,
19131 /*typename_keyword_p=*/false,
19132 /*template_keyword_p=*/false,
19134 /*check_dependency_p=*/false,
19135 /*class_head_p=*/false,
19136 /*is_declaration=*/false);
19137 /* If there was no class-name, then this is not a constructor. */
19138 constructor_p = !cp_parser_error_occurred (parser);
19140 /* If we're still considering a constructor, we have to see a `(',
19141 to begin the parameter-declaration-clause, followed by either a
19142 `)', an `...', or a decl-specifier. We need to check for a
19143 type-specifier to avoid being fooled into thinking that:
19147 is a constructor. (It is actually a function named `f' that
19148 takes one parameter (of type `int') and returns a value of type
19151 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19152 constructor_p = false;
19155 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19156 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19157 /* A parameter declaration begins with a decl-specifier,
19158 which is either the "attribute" keyword, a storage class
19159 specifier, or (usually) a type-specifier. */
19160 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19163 tree pushed_scope = NULL_TREE;
19164 unsigned saved_num_template_parameter_lists;
19166 /* Names appearing in the type-specifier should be looked up
19167 in the scope of the class. */
19168 if (current_class_type)
19172 type = TREE_TYPE (type_decl);
19173 if (TREE_CODE (type) == TYPENAME_TYPE)
19175 type = resolve_typename_type (type,
19176 /*only_current_p=*/false);
19177 if (TREE_CODE (type) == TYPENAME_TYPE)
19179 cp_parser_abort_tentative_parse (parser);
19183 pushed_scope = push_scope (type);
19186 /* Inside the constructor parameter list, surrounding
19187 template-parameter-lists do not apply. */
19188 saved_num_template_parameter_lists
19189 = parser->num_template_parameter_lists;
19190 parser->num_template_parameter_lists = 0;
19192 /* Look for the type-specifier. */
19193 cp_parser_type_specifier (parser,
19194 CP_PARSER_FLAGS_NONE,
19195 /*decl_specs=*/NULL,
19196 /*is_declarator=*/true,
19197 /*declares_class_or_enum=*/NULL,
19198 /*is_cv_qualifier=*/NULL);
19200 parser->num_template_parameter_lists
19201 = saved_num_template_parameter_lists;
19203 /* Leave the scope of the class. */
19205 pop_scope (pushed_scope);
19207 constructor_p = !cp_parser_error_occurred (parser);
19211 /* We did not really want to consume any tokens. */
19212 cp_parser_abort_tentative_parse (parser);
19214 return constructor_p;
19217 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19218 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19219 they must be performed once we are in the scope of the function.
19221 Returns the function defined. */
19224 cp_parser_function_definition_from_specifiers_and_declarator
19225 (cp_parser* parser,
19226 cp_decl_specifier_seq *decl_specifiers,
19228 const cp_declarator *declarator)
19233 /* Begin the function-definition. */
19234 success_p = start_function (decl_specifiers, declarator, attributes);
19236 /* The things we're about to see are not directly qualified by any
19237 template headers we've seen thus far. */
19238 reset_specialization ();
19240 /* If there were names looked up in the decl-specifier-seq that we
19241 did not check, check them now. We must wait until we are in the
19242 scope of the function to perform the checks, since the function
19243 might be a friend. */
19244 perform_deferred_access_checks ();
19248 /* Skip the entire function. */
19249 cp_parser_skip_to_end_of_block_or_statement (parser);
19250 fn = error_mark_node;
19252 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19254 /* Seen already, skip it. An error message has already been output. */
19255 cp_parser_skip_to_end_of_block_or_statement (parser);
19256 fn = current_function_decl;
19257 current_function_decl = NULL_TREE;
19258 /* If this is a function from a class, pop the nested class. */
19259 if (current_class_name)
19260 pop_nested_class ();
19263 fn = cp_parser_function_definition_after_declarator (parser,
19264 /*inline_p=*/false);
19269 /* Parse the part of a function-definition that follows the
19270 declarator. INLINE_P is TRUE iff this function is an inline
19271 function defined within a class-specifier.
19273 Returns the function defined. */
19276 cp_parser_function_definition_after_declarator (cp_parser* parser,
19280 bool ctor_initializer_p = false;
19281 bool saved_in_unbraced_linkage_specification_p;
19282 bool saved_in_function_body;
19283 unsigned saved_num_template_parameter_lists;
19286 saved_in_function_body = parser->in_function_body;
19287 parser->in_function_body = true;
19288 /* If the next token is `return', then the code may be trying to
19289 make use of the "named return value" extension that G++ used to
19291 token = cp_lexer_peek_token (parser->lexer);
19292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19294 /* Consume the `return' keyword. */
19295 cp_lexer_consume_token (parser->lexer);
19296 /* Look for the identifier that indicates what value is to be
19298 cp_parser_identifier (parser);
19299 /* Issue an error message. */
19300 error_at (token->location,
19301 "named return values are no longer supported");
19302 /* Skip tokens until we reach the start of the function body. */
19305 cp_token *token = cp_lexer_peek_token (parser->lexer);
19306 if (token->type == CPP_OPEN_BRACE
19307 || token->type == CPP_EOF
19308 || token->type == CPP_PRAGMA_EOL)
19310 cp_lexer_consume_token (parser->lexer);
19313 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19314 anything declared inside `f'. */
19315 saved_in_unbraced_linkage_specification_p
19316 = parser->in_unbraced_linkage_specification_p;
19317 parser->in_unbraced_linkage_specification_p = false;
19318 /* Inside the function, surrounding template-parameter-lists do not
19320 saved_num_template_parameter_lists
19321 = parser->num_template_parameter_lists;
19322 parser->num_template_parameter_lists = 0;
19324 start_lambda_scope (current_function_decl);
19326 /* If the next token is `try', then we are looking at a
19327 function-try-block. */
19328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19329 ctor_initializer_p = cp_parser_function_try_block (parser);
19330 /* A function-try-block includes the function-body, so we only do
19331 this next part if we're not processing a function-try-block. */
19334 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19336 finish_lambda_scope ();
19338 /* Finish the function. */
19339 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19340 (inline_p ? 2 : 0));
19341 /* Generate code for it, if necessary. */
19342 expand_or_defer_fn (fn);
19343 /* Restore the saved values. */
19344 parser->in_unbraced_linkage_specification_p
19345 = saved_in_unbraced_linkage_specification_p;
19346 parser->num_template_parameter_lists
19347 = saved_num_template_parameter_lists;
19348 parser->in_function_body = saved_in_function_body;
19353 /* Parse a template-declaration, assuming that the `export' (and
19354 `extern') keywords, if present, has already been scanned. MEMBER_P
19355 is as for cp_parser_template_declaration. */
19358 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19360 tree decl = NULL_TREE;
19361 VEC (deferred_access_check,gc) *checks;
19362 tree parameter_list;
19363 bool friend_p = false;
19364 bool need_lang_pop;
19367 /* Look for the `template' keyword. */
19368 token = cp_lexer_peek_token (parser->lexer);
19369 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19373 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19375 if (at_class_scope_p () && current_function_decl)
19377 /* 14.5.2.2 [temp.mem]
19379 A local class shall not have member templates. */
19380 error_at (token->location,
19381 "invalid declaration of member template in local class");
19382 cp_parser_skip_to_end_of_block_or_statement (parser);
19387 A template ... shall not have C linkage. */
19388 if (current_lang_name == lang_name_c)
19390 error_at (token->location, "template with C linkage");
19391 /* Give it C++ linkage to avoid confusing other parts of the
19393 push_lang_context (lang_name_cplusplus);
19394 need_lang_pop = true;
19397 need_lang_pop = false;
19399 /* We cannot perform access checks on the template parameter
19400 declarations until we know what is being declared, just as we
19401 cannot check the decl-specifier list. */
19402 push_deferring_access_checks (dk_deferred);
19404 /* If the next token is `>', then we have an invalid
19405 specialization. Rather than complain about an invalid template
19406 parameter, issue an error message here. */
19407 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19409 cp_parser_error (parser, "invalid explicit specialization");
19410 begin_specialization ();
19411 parameter_list = NULL_TREE;
19414 /* Parse the template parameters. */
19415 parameter_list = cp_parser_template_parameter_list (parser);
19417 /* Get the deferred access checks from the parameter list. These
19418 will be checked once we know what is being declared, as for a
19419 member template the checks must be performed in the scope of the
19420 class containing the member. */
19421 checks = get_deferred_access_checks ();
19423 /* Look for the `>'. */
19424 cp_parser_skip_to_end_of_template_parameter_list (parser);
19425 /* We just processed one more parameter list. */
19426 ++parser->num_template_parameter_lists;
19427 /* If the next token is `template', there are more template
19429 if (cp_lexer_next_token_is_keyword (parser->lexer,
19431 cp_parser_template_declaration_after_export (parser, member_p);
19434 /* There are no access checks when parsing a template, as we do not
19435 know if a specialization will be a friend. */
19436 push_deferring_access_checks (dk_no_check);
19437 token = cp_lexer_peek_token (parser->lexer);
19438 decl = cp_parser_single_declaration (parser,
19441 /*explicit_specialization_p=*/false,
19443 pop_deferring_access_checks ();
19445 /* If this is a member template declaration, let the front
19447 if (member_p && !friend_p && decl)
19449 if (TREE_CODE (decl) == TYPE_DECL)
19450 cp_parser_check_access_in_redeclaration (decl, token->location);
19452 decl = finish_member_template_decl (decl);
19454 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19455 make_friend_class (current_class_type, TREE_TYPE (decl),
19456 /*complain=*/true);
19458 /* We are done with the current parameter list. */
19459 --parser->num_template_parameter_lists;
19461 pop_deferring_access_checks ();
19464 finish_template_decl (parameter_list);
19466 /* Register member declarations. */
19467 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19468 finish_member_declaration (decl);
19469 /* For the erroneous case of a template with C linkage, we pushed an
19470 implicit C++ linkage scope; exit that scope now. */
19472 pop_lang_context ();
19473 /* If DECL is a function template, we must return to parse it later.
19474 (Even though there is no definition, there might be default
19475 arguments that need handling.) */
19476 if (member_p && decl
19477 && (TREE_CODE (decl) == FUNCTION_DECL
19478 || DECL_FUNCTION_TEMPLATE_P (decl)))
19479 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19482 /* Perform the deferred access checks from a template-parameter-list.
19483 CHECKS is a TREE_LIST of access checks, as returned by
19484 get_deferred_access_checks. */
19487 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19489 ++processing_template_parmlist;
19490 perform_access_checks (checks);
19491 --processing_template_parmlist;
19494 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19495 `function-definition' sequence. MEMBER_P is true, this declaration
19496 appears in a class scope.
19498 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19499 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19502 cp_parser_single_declaration (cp_parser* parser,
19503 VEC (deferred_access_check,gc)* checks,
19505 bool explicit_specialization_p,
19508 int declares_class_or_enum;
19509 tree decl = NULL_TREE;
19510 cp_decl_specifier_seq decl_specifiers;
19511 bool function_definition_p = false;
19512 cp_token *decl_spec_token_start;
19514 /* This function is only used when processing a template
19516 gcc_assert (innermost_scope_kind () == sk_template_parms
19517 || innermost_scope_kind () == sk_template_spec);
19519 /* Defer access checks until we know what is being declared. */
19520 push_deferring_access_checks (dk_deferred);
19522 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19524 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19525 cp_parser_decl_specifier_seq (parser,
19526 CP_PARSER_FLAGS_OPTIONAL,
19528 &declares_class_or_enum);
19530 *friend_p = cp_parser_friend_p (&decl_specifiers);
19532 /* There are no template typedefs. */
19533 if (decl_specifiers.specs[(int) ds_typedef])
19535 error_at (decl_spec_token_start->location,
19536 "template declaration of %<typedef%>");
19537 decl = error_mark_node;
19540 /* Gather up the access checks that occurred the
19541 decl-specifier-seq. */
19542 stop_deferring_access_checks ();
19544 /* Check for the declaration of a template class. */
19545 if (declares_class_or_enum)
19547 if (cp_parser_declares_only_class_p (parser))
19549 decl = shadow_tag (&decl_specifiers);
19554 friend template <typename T> struct A<T>::B;
19557 A<T>::B will be represented by a TYPENAME_TYPE, and
19558 therefore not recognized by shadow_tag. */
19559 if (friend_p && *friend_p
19561 && decl_specifiers.type
19562 && TYPE_P (decl_specifiers.type))
19563 decl = decl_specifiers.type;
19565 if (decl && decl != error_mark_node)
19566 decl = TYPE_NAME (decl);
19568 decl = error_mark_node;
19570 /* Perform access checks for template parameters. */
19571 cp_parser_perform_template_parameter_access_checks (checks);
19575 /* Complain about missing 'typename' or other invalid type names. */
19576 if (!decl_specifiers.any_type_specifiers_p)
19577 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19579 /* If it's not a template class, try for a template function. If
19580 the next token is a `;', then this declaration does not declare
19581 anything. But, if there were errors in the decl-specifiers, then
19582 the error might well have come from an attempted class-specifier.
19583 In that case, there's no need to warn about a missing declarator. */
19585 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19586 || decl_specifiers.type != error_mark_node))
19588 decl = cp_parser_init_declarator (parser,
19591 /*function_definition_allowed_p=*/true,
19593 declares_class_or_enum,
19594 &function_definition_p);
19596 /* 7.1.1-1 [dcl.stc]
19598 A storage-class-specifier shall not be specified in an explicit
19599 specialization... */
19601 && explicit_specialization_p
19602 && decl_specifiers.storage_class != sc_none)
19604 error_at (decl_spec_token_start->location,
19605 "explicit template specialization cannot have a storage class");
19606 decl = error_mark_node;
19610 pop_deferring_access_checks ();
19612 /* Clear any current qualification; whatever comes next is the start
19613 of something new. */
19614 parser->scope = NULL_TREE;
19615 parser->qualifying_scope = NULL_TREE;
19616 parser->object_scope = NULL_TREE;
19617 /* Look for a trailing `;' after the declaration. */
19618 if (!function_definition_p
19619 && (decl == error_mark_node
19620 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19621 cp_parser_skip_to_end_of_block_or_statement (parser);
19626 /* Parse a cast-expression that is not the operand of a unary "&". */
19629 cp_parser_simple_cast_expression (cp_parser *parser)
19631 return cp_parser_cast_expression (parser, /*address_p=*/false,
19632 /*cast_p=*/false, NULL);
19635 /* Parse a functional cast to TYPE. Returns an expression
19636 representing the cast. */
19639 cp_parser_functional_cast (cp_parser* parser, tree type)
19642 tree expression_list;
19646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19648 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19649 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19650 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19651 if (TREE_CODE (type) == TYPE_DECL)
19652 type = TREE_TYPE (type);
19653 return finish_compound_literal (type, expression_list);
19657 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19659 /*allow_expansion_p=*/true,
19660 /*non_constant_p=*/NULL);
19662 expression_list = error_mark_node;
19665 expression_list = build_tree_list_vec (vec);
19666 release_tree_vector (vec);
19669 cast = build_functional_cast (type, expression_list,
19670 tf_warning_or_error);
19671 /* [expr.const]/1: In an integral constant expression "only type
19672 conversions to integral or enumeration type can be used". */
19673 if (TREE_CODE (type) == TYPE_DECL)
19674 type = TREE_TYPE (type);
19675 if (cast != error_mark_node
19676 && !cast_valid_in_integral_constant_expression_p (type)
19677 && cp_parser_non_integral_constant_expression (parser,
19679 return error_mark_node;
19683 /* Save the tokens that make up the body of a member function defined
19684 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19685 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19686 specifiers applied to the declaration. Returns the FUNCTION_DECL
19687 for the member function. */
19690 cp_parser_save_member_function_body (cp_parser* parser,
19691 cp_decl_specifier_seq *decl_specifiers,
19692 cp_declarator *declarator,
19699 /* Create the FUNCTION_DECL. */
19700 fn = grokmethod (decl_specifiers, declarator, attributes);
19701 /* If something went badly wrong, bail out now. */
19702 if (fn == error_mark_node)
19704 /* If there's a function-body, skip it. */
19705 if (cp_parser_token_starts_function_definition_p
19706 (cp_lexer_peek_token (parser->lexer)))
19707 cp_parser_skip_to_end_of_block_or_statement (parser);
19708 return error_mark_node;
19711 /* Remember it, if there default args to post process. */
19712 cp_parser_save_default_args (parser, fn);
19714 /* Save away the tokens that make up the body of the
19716 first = parser->lexer->next_token;
19717 /* We can have braced-init-list mem-initializers before the fn body. */
19718 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19720 cp_lexer_consume_token (parser->lexer);
19721 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19722 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19724 /* cache_group will stop after an un-nested { } pair, too. */
19725 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19728 /* variadic mem-inits have ... after the ')'. */
19729 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19730 cp_lexer_consume_token (parser->lexer);
19733 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19734 /* Handle function try blocks. */
19735 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19736 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19737 last = parser->lexer->next_token;
19739 /* Save away the inline definition; we will process it when the
19740 class is complete. */
19741 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19742 DECL_PENDING_INLINE_P (fn) = 1;
19744 /* We need to know that this was defined in the class, so that
19745 friend templates are handled correctly. */
19746 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19748 /* Add FN to the queue of functions to be parsed later. */
19749 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19754 /* Parse a template-argument-list, as well as the trailing ">" (but
19755 not the opening ">"). See cp_parser_template_argument_list for the
19759 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19763 tree saved_qualifying_scope;
19764 tree saved_object_scope;
19765 bool saved_greater_than_is_operator_p;
19766 int saved_unevaluated_operand;
19767 int saved_inhibit_evaluation_warnings;
19771 When parsing a template-id, the first non-nested `>' is taken as
19772 the end of the template-argument-list rather than a greater-than
19774 saved_greater_than_is_operator_p
19775 = parser->greater_than_is_operator_p;
19776 parser->greater_than_is_operator_p = false;
19777 /* Parsing the argument list may modify SCOPE, so we save it
19779 saved_scope = parser->scope;
19780 saved_qualifying_scope = parser->qualifying_scope;
19781 saved_object_scope = parser->object_scope;
19782 /* We need to evaluate the template arguments, even though this
19783 template-id may be nested within a "sizeof". */
19784 saved_unevaluated_operand = cp_unevaluated_operand;
19785 cp_unevaluated_operand = 0;
19786 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19787 c_inhibit_evaluation_warnings = 0;
19788 /* Parse the template-argument-list itself. */
19789 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19790 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19791 arguments = NULL_TREE;
19793 arguments = cp_parser_template_argument_list (parser);
19794 /* Look for the `>' that ends the template-argument-list. If we find
19795 a '>>' instead, it's probably just a typo. */
19796 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19798 if (cxx_dialect != cxx98)
19800 /* In C++0x, a `>>' in a template argument list or cast
19801 expression is considered to be two separate `>'
19802 tokens. So, change the current token to a `>', but don't
19803 consume it: it will be consumed later when the outer
19804 template argument list (or cast expression) is parsed.
19805 Note that this replacement of `>' for `>>' is necessary
19806 even if we are parsing tentatively: in the tentative
19807 case, after calling
19808 cp_parser_enclosed_template_argument_list we will always
19809 throw away all of the template arguments and the first
19810 closing `>', either because the template argument list
19811 was erroneous or because we are replacing those tokens
19812 with a CPP_TEMPLATE_ID token. The second `>' (which will
19813 not have been thrown away) is needed either to close an
19814 outer template argument list or to complete a new-style
19816 cp_token *token = cp_lexer_peek_token (parser->lexer);
19817 token->type = CPP_GREATER;
19819 else if (!saved_greater_than_is_operator_p)
19821 /* If we're in a nested template argument list, the '>>' has
19822 to be a typo for '> >'. We emit the error message, but we
19823 continue parsing and we push a '>' as next token, so that
19824 the argument list will be parsed correctly. Note that the
19825 global source location is still on the token before the
19826 '>>', so we need to say explicitly where we want it. */
19827 cp_token *token = cp_lexer_peek_token (parser->lexer);
19828 error_at (token->location, "%<>>%> should be %<> >%> "
19829 "within a nested template argument list");
19831 token->type = CPP_GREATER;
19835 /* If this is not a nested template argument list, the '>>'
19836 is a typo for '>'. Emit an error message and continue.
19837 Same deal about the token location, but here we can get it
19838 right by consuming the '>>' before issuing the diagnostic. */
19839 cp_token *token = cp_lexer_consume_token (parser->lexer);
19840 error_at (token->location,
19841 "spurious %<>>%>, use %<>%> to terminate "
19842 "a template argument list");
19846 cp_parser_skip_to_end_of_template_parameter_list (parser);
19847 /* The `>' token might be a greater-than operator again now. */
19848 parser->greater_than_is_operator_p
19849 = saved_greater_than_is_operator_p;
19850 /* Restore the SAVED_SCOPE. */
19851 parser->scope = saved_scope;
19852 parser->qualifying_scope = saved_qualifying_scope;
19853 parser->object_scope = saved_object_scope;
19854 cp_unevaluated_operand = saved_unevaluated_operand;
19855 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19860 /* MEMBER_FUNCTION is a member function, or a friend. If default
19861 arguments, or the body of the function have not yet been parsed,
19865 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19867 /* If this member is a template, get the underlying
19869 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19870 member_function = DECL_TEMPLATE_RESULT (member_function);
19872 /* There should not be any class definitions in progress at this
19873 point; the bodies of members are only parsed outside of all class
19875 gcc_assert (parser->num_classes_being_defined == 0);
19876 /* While we're parsing the member functions we might encounter more
19877 classes. We want to handle them right away, but we don't want
19878 them getting mixed up with functions that are currently in the
19880 push_unparsed_function_queues (parser);
19882 /* Make sure that any template parameters are in scope. */
19883 maybe_begin_member_template_processing (member_function);
19885 /* If the body of the function has not yet been parsed, parse it
19887 if (DECL_PENDING_INLINE_P (member_function))
19889 tree function_scope;
19890 cp_token_cache *tokens;
19892 /* The function is no longer pending; we are processing it. */
19893 tokens = DECL_PENDING_INLINE_INFO (member_function);
19894 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19895 DECL_PENDING_INLINE_P (member_function) = 0;
19897 /* If this is a local class, enter the scope of the containing
19899 function_scope = current_function_decl;
19900 if (function_scope)
19901 push_function_context ();
19903 /* Push the body of the function onto the lexer stack. */
19904 cp_parser_push_lexer_for_tokens (parser, tokens);
19906 /* Let the front end know that we going to be defining this
19908 start_preparsed_function (member_function, NULL_TREE,
19909 SF_PRE_PARSED | SF_INCLASS_INLINE);
19911 /* Don't do access checking if it is a templated function. */
19912 if (processing_template_decl)
19913 push_deferring_access_checks (dk_no_check);
19915 /* Now, parse the body of the function. */
19916 cp_parser_function_definition_after_declarator (parser,
19917 /*inline_p=*/true);
19919 if (processing_template_decl)
19920 pop_deferring_access_checks ();
19922 /* Leave the scope of the containing function. */
19923 if (function_scope)
19924 pop_function_context ();
19925 cp_parser_pop_lexer (parser);
19928 /* Remove any template parameters from the symbol table. */
19929 maybe_end_member_template_processing ();
19931 /* Restore the queue. */
19932 pop_unparsed_function_queues (parser);
19935 /* If DECL contains any default args, remember it on the unparsed
19936 functions queue. */
19939 cp_parser_save_default_args (cp_parser* parser, tree decl)
19943 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19945 probe = TREE_CHAIN (probe))
19946 if (TREE_PURPOSE (probe))
19948 cp_default_arg_entry *entry
19949 = VEC_safe_push (cp_default_arg_entry, gc,
19950 unparsed_funs_with_default_args, NULL);
19951 entry->class_type = current_class_type;
19952 entry->decl = decl;
19957 /* FN is a FUNCTION_DECL which may contains a parameter with an
19958 unparsed DEFAULT_ARG. Parse the default args now. This function
19959 assumes that the current scope is the scope in which the default
19960 argument should be processed. */
19963 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19965 bool saved_local_variables_forbidden_p;
19966 tree parm, parmdecl;
19968 /* While we're parsing the default args, we might (due to the
19969 statement expression extension) encounter more classes. We want
19970 to handle them right away, but we don't want them getting mixed
19971 up with default args that are currently in the queue. */
19972 push_unparsed_function_queues (parser);
19974 /* Local variable names (and the `this' keyword) may not appear
19975 in a default argument. */
19976 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19977 parser->local_variables_forbidden_p = true;
19979 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19980 parmdecl = DECL_ARGUMENTS (fn);
19981 parm && parm != void_list_node;
19982 parm = TREE_CHAIN (parm),
19983 parmdecl = DECL_CHAIN (parmdecl))
19985 cp_token_cache *tokens;
19986 tree default_arg = TREE_PURPOSE (parm);
19988 VEC(tree,gc) *insts;
19995 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19996 /* This can happen for a friend declaration for a function
19997 already declared with default arguments. */
20000 /* Push the saved tokens for the default argument onto the parser's
20002 tokens = DEFARG_TOKENS (default_arg);
20003 cp_parser_push_lexer_for_tokens (parser, tokens);
20005 start_lambda_scope (parmdecl);
20007 /* Parse the assignment-expression. */
20008 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20009 if (parsed_arg == error_mark_node)
20011 cp_parser_pop_lexer (parser);
20015 if (!processing_template_decl)
20016 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20018 TREE_PURPOSE (parm) = parsed_arg;
20020 /* Update any instantiations we've already created. */
20021 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20022 VEC_iterate (tree, insts, ix, copy); ix++)
20023 TREE_PURPOSE (copy) = parsed_arg;
20025 finish_lambda_scope ();
20027 /* If the token stream has not been completely used up, then
20028 there was extra junk after the end of the default
20030 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20031 cp_parser_error (parser, "expected %<,%>");
20033 /* Revert to the main lexer. */
20034 cp_parser_pop_lexer (parser);
20037 /* Make sure no default arg is missing. */
20038 check_default_args (fn);
20040 /* Restore the state of local_variables_forbidden_p. */
20041 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20043 /* Restore the queue. */
20044 pop_unparsed_function_queues (parser);
20047 /* Parse the operand of `sizeof' (or a similar operator). Returns
20048 either a TYPE or an expression, depending on the form of the
20049 input. The KEYWORD indicates which kind of expression we have
20053 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20055 tree expr = NULL_TREE;
20056 const char *saved_message;
20058 bool saved_integral_constant_expression_p;
20059 bool saved_non_integral_constant_expression_p;
20060 bool pack_expansion_p = false;
20062 /* Types cannot be defined in a `sizeof' expression. Save away the
20064 saved_message = parser->type_definition_forbidden_message;
20065 /* And create the new one. */
20066 tmp = concat ("types may not be defined in %<",
20067 IDENTIFIER_POINTER (ridpointers[keyword]),
20068 "%> expressions", NULL);
20069 parser->type_definition_forbidden_message = tmp;
20071 /* The restrictions on constant-expressions do not apply inside
20072 sizeof expressions. */
20073 saved_integral_constant_expression_p
20074 = parser->integral_constant_expression_p;
20075 saved_non_integral_constant_expression_p
20076 = parser->non_integral_constant_expression_p;
20077 parser->integral_constant_expression_p = false;
20079 /* If it's a `...', then we are computing the length of a parameter
20081 if (keyword == RID_SIZEOF
20082 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20084 /* Consume the `...'. */
20085 cp_lexer_consume_token (parser->lexer);
20086 maybe_warn_variadic_templates ();
20088 /* Note that this is an expansion. */
20089 pack_expansion_p = true;
20092 /* Do not actually evaluate the expression. */
20093 ++cp_unevaluated_operand;
20094 ++c_inhibit_evaluation_warnings;
20095 /* If it's a `(', then we might be looking at the type-id
20097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20100 bool saved_in_type_id_in_expr_p;
20102 /* We can't be sure yet whether we're looking at a type-id or an
20104 cp_parser_parse_tentatively (parser);
20105 /* Consume the `('. */
20106 cp_lexer_consume_token (parser->lexer);
20107 /* Parse the type-id. */
20108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20109 parser->in_type_id_in_expr_p = true;
20110 type = cp_parser_type_id (parser);
20111 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20112 /* Now, look for the trailing `)'. */
20113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20114 /* If all went well, then we're done. */
20115 if (cp_parser_parse_definitely (parser))
20117 cp_decl_specifier_seq decl_specs;
20119 /* Build a trivial decl-specifier-seq. */
20120 clear_decl_specs (&decl_specs);
20121 decl_specs.type = type;
20123 /* Call grokdeclarator to figure out what type this is. */
20124 expr = grokdeclarator (NULL,
20128 /*attrlist=*/NULL);
20132 /* If the type-id production did not work out, then we must be
20133 looking at the unary-expression production. */
20135 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20136 /*cast_p=*/false, NULL);
20138 if (pack_expansion_p)
20139 /* Build a pack expansion. */
20140 expr = make_pack_expansion (expr);
20142 /* Go back to evaluating expressions. */
20143 --cp_unevaluated_operand;
20144 --c_inhibit_evaluation_warnings;
20146 /* Free the message we created. */
20148 /* And restore the old one. */
20149 parser->type_definition_forbidden_message = saved_message;
20150 parser->integral_constant_expression_p
20151 = saved_integral_constant_expression_p;
20152 parser->non_integral_constant_expression_p
20153 = saved_non_integral_constant_expression_p;
20158 /* If the current declaration has no declarator, return true. */
20161 cp_parser_declares_only_class_p (cp_parser *parser)
20163 /* If the next token is a `;' or a `,' then there is no
20165 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20166 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20169 /* Update the DECL_SPECS to reflect the storage class indicated by
20173 cp_parser_set_storage_class (cp_parser *parser,
20174 cp_decl_specifier_seq *decl_specs,
20176 location_t location)
20178 cp_storage_class storage_class;
20180 if (parser->in_unbraced_linkage_specification_p)
20182 error_at (location, "invalid use of %qD in linkage specification",
20183 ridpointers[keyword]);
20186 else if (decl_specs->storage_class != sc_none)
20188 decl_specs->conflicting_specifiers_p = true;
20192 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20193 && decl_specs->specs[(int) ds_thread])
20195 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20196 decl_specs->specs[(int) ds_thread] = 0;
20202 storage_class = sc_auto;
20205 storage_class = sc_register;
20208 storage_class = sc_static;
20211 storage_class = sc_extern;
20214 storage_class = sc_mutable;
20217 gcc_unreachable ();
20219 decl_specs->storage_class = storage_class;
20221 /* A storage class specifier cannot be applied alongside a typedef
20222 specifier. If there is a typedef specifier present then set
20223 conflicting_specifiers_p which will trigger an error later
20224 on in grokdeclarator. */
20225 if (decl_specs->specs[(int)ds_typedef])
20226 decl_specs->conflicting_specifiers_p = true;
20229 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20230 is true, the type is a user-defined type; otherwise it is a
20231 built-in type specified by a keyword. */
20234 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20236 location_t location,
20237 bool user_defined_p)
20239 decl_specs->any_specifiers_p = true;
20241 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20242 (with, for example, in "typedef int wchar_t;") we remember that
20243 this is what happened. In system headers, we ignore these
20244 declarations so that G++ can work with system headers that are not
20246 if (decl_specs->specs[(int) ds_typedef]
20248 && (type_spec == boolean_type_node
20249 || type_spec == char16_type_node
20250 || type_spec == char32_type_node
20251 || type_spec == wchar_type_node)
20252 && (decl_specs->type
20253 || decl_specs->specs[(int) ds_long]
20254 || decl_specs->specs[(int) ds_short]
20255 || decl_specs->specs[(int) ds_unsigned]
20256 || decl_specs->specs[(int) ds_signed]))
20258 decl_specs->redefined_builtin_type = type_spec;
20259 if (!decl_specs->type)
20261 decl_specs->type = type_spec;
20262 decl_specs->user_defined_type_p = false;
20263 decl_specs->type_location = location;
20266 else if (decl_specs->type)
20267 decl_specs->multiple_types_p = true;
20270 decl_specs->type = type_spec;
20271 decl_specs->user_defined_type_p = user_defined_p;
20272 decl_specs->redefined_builtin_type = NULL_TREE;
20273 decl_specs->type_location = location;
20277 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20278 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20281 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20283 return decl_specifiers->specs[(int) ds_friend] != 0;
20286 /* Issue an error message indicating that TOKEN_DESC was expected.
20287 If KEYWORD is true, it indicated this function is called by
20288 cp_parser_require_keword and the required token can only be
20289 a indicated keyword. */
20292 cp_parser_required_error (cp_parser *parser,
20293 required_token token_desc,
20296 switch (token_desc)
20299 cp_parser_error (parser, "expected %<new%>");
20302 cp_parser_error (parser, "expected %<delete%>");
20305 cp_parser_error (parser, "expected %<return%>");
20308 cp_parser_error (parser, "expected %<while%>");
20311 cp_parser_error (parser, "expected %<extern%>");
20313 case RT_STATIC_ASSERT:
20314 cp_parser_error (parser, "expected %<static_assert%>");
20317 cp_parser_error (parser, "expected %<decltype%>");
20320 cp_parser_error (parser, "expected %<operator%>");
20323 cp_parser_error (parser, "expected %<class%>");
20326 cp_parser_error (parser, "expected %<template%>");
20329 cp_parser_error (parser, "expected %<namespace%>");
20332 cp_parser_error (parser, "expected %<using%>");
20335 cp_parser_error (parser, "expected %<asm%>");
20338 cp_parser_error (parser, "expected %<try%>");
20341 cp_parser_error (parser, "expected %<catch%>");
20344 cp_parser_error (parser, "expected %<throw%>");
20347 cp_parser_error (parser, "expected %<__label__%>");
20350 cp_parser_error (parser, "expected %<@try%>");
20352 case RT_AT_SYNCHRONIZED:
20353 cp_parser_error (parser, "expected %<@synchronized%>");
20356 cp_parser_error (parser, "expected %<@throw%>");
20363 switch (token_desc)
20366 cp_parser_error (parser, "expected %<;%>");
20368 case RT_OPEN_PAREN:
20369 cp_parser_error (parser, "expected %<(%>");
20371 case RT_CLOSE_BRACE:
20372 cp_parser_error (parser, "expected %<}%>");
20374 case RT_OPEN_BRACE:
20375 cp_parser_error (parser, "expected %<{%>");
20377 case RT_CLOSE_SQUARE:
20378 cp_parser_error (parser, "expected %<]%>");
20380 case RT_OPEN_SQUARE:
20381 cp_parser_error (parser, "expected %<[%>");
20384 cp_parser_error (parser, "expected %<,%>");
20387 cp_parser_error (parser, "expected %<::%>");
20390 cp_parser_error (parser, "expected %<<%>");
20393 cp_parser_error (parser, "expected %<>%>");
20396 cp_parser_error (parser, "expected %<=%>");
20399 cp_parser_error (parser, "expected %<...%>");
20402 cp_parser_error (parser, "expected %<*%>");
20405 cp_parser_error (parser, "expected %<~%>");
20408 cp_parser_error (parser, "expected %<:%>");
20410 case RT_COLON_SCOPE:
20411 cp_parser_error (parser, "expected %<:%> or %<::%>");
20413 case RT_CLOSE_PAREN:
20414 cp_parser_error (parser, "expected %<)%>");
20416 case RT_COMMA_CLOSE_PAREN:
20417 cp_parser_error (parser, "expected %<,%> or %<)%>");
20419 case RT_PRAGMA_EOL:
20420 cp_parser_error (parser, "expected end of line");
20423 cp_parser_error (parser, "expected identifier");
20426 cp_parser_error (parser, "expected selection-statement");
20428 case RT_INTERATION:
20429 cp_parser_error (parser, "expected iteration-statement");
20432 cp_parser_error (parser, "expected jump-statement");
20435 cp_parser_error (parser, "expected class-key");
20437 case RT_CLASS_TYPENAME_TEMPLATE:
20438 cp_parser_error (parser,
20439 "expected %<class%>, %<typename%>, or %<template%>");
20442 gcc_unreachable ();
20446 gcc_unreachable ();
20451 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20452 issue an error message indicating that TOKEN_DESC was expected.
20454 Returns the token consumed, if the token had the appropriate type.
20455 Otherwise, returns NULL. */
20458 cp_parser_require (cp_parser* parser,
20459 enum cpp_ttype type,
20460 required_token token_desc)
20462 if (cp_lexer_next_token_is (parser->lexer, type))
20463 return cp_lexer_consume_token (parser->lexer);
20466 /* Output the MESSAGE -- unless we're parsing tentatively. */
20467 if (!cp_parser_simulate_error (parser))
20468 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20473 /* An error message is produced if the next token is not '>'.
20474 All further tokens are skipped until the desired token is
20475 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20478 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20480 /* Current level of '< ... >'. */
20481 unsigned level = 0;
20482 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20483 unsigned nesting_depth = 0;
20485 /* Are we ready, yet? If not, issue error message. */
20486 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20489 /* Skip tokens until the desired token is found. */
20492 /* Peek at the next token. */
20493 switch (cp_lexer_peek_token (parser->lexer)->type)
20496 if (!nesting_depth)
20501 if (cxx_dialect == cxx98)
20502 /* C++0x views the `>>' operator as two `>' tokens, but
20505 else if (!nesting_depth && level-- == 0)
20507 /* We've hit a `>>' where the first `>' closes the
20508 template argument list, and the second `>' is
20509 spurious. Just consume the `>>' and stop; we've
20510 already produced at least one error. */
20511 cp_lexer_consume_token (parser->lexer);
20514 /* Fall through for C++0x, so we handle the second `>' in
20518 if (!nesting_depth && level-- == 0)
20520 /* We've reached the token we want, consume it and stop. */
20521 cp_lexer_consume_token (parser->lexer);
20526 case CPP_OPEN_PAREN:
20527 case CPP_OPEN_SQUARE:
20531 case CPP_CLOSE_PAREN:
20532 case CPP_CLOSE_SQUARE:
20533 if (nesting_depth-- == 0)
20538 case CPP_PRAGMA_EOL:
20539 case CPP_SEMICOLON:
20540 case CPP_OPEN_BRACE:
20541 case CPP_CLOSE_BRACE:
20542 /* The '>' was probably forgotten, don't look further. */
20549 /* Consume this token. */
20550 cp_lexer_consume_token (parser->lexer);
20554 /* If the next token is the indicated keyword, consume it. Otherwise,
20555 issue an error message indicating that TOKEN_DESC was expected.
20557 Returns the token consumed, if the token had the appropriate type.
20558 Otherwise, returns NULL. */
20561 cp_parser_require_keyword (cp_parser* parser,
20563 required_token token_desc)
20565 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20567 if (token && token->keyword != keyword)
20569 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20577 function-definition. */
20580 cp_parser_token_starts_function_definition_p (cp_token* token)
20582 return (/* An ordinary function-body begins with an `{'. */
20583 token->type == CPP_OPEN_BRACE
20584 /* A ctor-initializer begins with a `:'. */
20585 || token->type == CPP_COLON
20586 /* A function-try-block begins with `try'. */
20587 || token->keyword == RID_TRY
20588 /* The named return value extension begins with `return'. */
20589 || token->keyword == RID_RETURN);
20592 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20596 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20600 token = cp_lexer_peek_token (parser->lexer);
20601 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20604 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20605 C++0x) ending a template-argument. */
20608 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20612 token = cp_lexer_peek_token (parser->lexer);
20613 return (token->type == CPP_COMMA
20614 || token->type == CPP_GREATER
20615 || token->type == CPP_ELLIPSIS
20616 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20619 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20620 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20623 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20628 token = cp_lexer_peek_nth_token (parser->lexer, n);
20629 if (token->type == CPP_LESS)
20631 /* Check for the sequence `<::' in the original code. It would be lexed as
20632 `[:', where `[' is a digraph, and there is no whitespace before
20634 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20637 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20638 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20644 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20645 or none_type otherwise. */
20647 static enum tag_types
20648 cp_parser_token_is_class_key (cp_token* token)
20650 switch (token->keyword)
20655 return record_type;
20664 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20667 cp_parser_check_class_key (enum tag_types class_key, tree type)
20669 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20670 permerror (input_location, "%qs tag used in naming %q#T",
20671 class_key == union_type ? "union"
20672 : class_key == record_type ? "struct" : "class",
20676 /* Issue an error message if DECL is redeclared with different
20677 access than its original declaration [class.access.spec/3].
20678 This applies to nested classes and nested class templates.
20682 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20684 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20687 if ((TREE_PRIVATE (decl)
20688 != (current_access_specifier == access_private_node))
20689 || (TREE_PROTECTED (decl)
20690 != (current_access_specifier == access_protected_node)))
20691 error_at (location, "%qD redeclared with different access", decl);
20694 /* Look for the `template' keyword, as a syntactic disambiguator.
20695 Return TRUE iff it is present, in which case it will be
20699 cp_parser_optional_template_keyword (cp_parser *parser)
20701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20703 /* The `template' keyword can only be used within templates;
20704 outside templates the parser can always figure out what is a
20705 template and what is not. */
20706 if (!processing_template_decl)
20708 cp_token *token = cp_lexer_peek_token (parser->lexer);
20709 error_at (token->location,
20710 "%<template%> (as a disambiguator) is only allowed "
20711 "within templates");
20712 /* If this part of the token stream is rescanned, the same
20713 error message would be generated. So, we purge the token
20714 from the stream. */
20715 cp_lexer_purge_token (parser->lexer);
20720 /* Consume the `template' keyword. */
20721 cp_lexer_consume_token (parser->lexer);
20729 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20730 set PARSER->SCOPE, and perform other related actions. */
20733 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20736 struct tree_check *check_value;
20737 deferred_access_check *chk;
20738 VEC (deferred_access_check,gc) *checks;
20740 /* Get the stored value. */
20741 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20742 /* Perform any access checks that were deferred. */
20743 checks = check_value->checks;
20746 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20747 perform_or_defer_access_check (chk->binfo,
20751 /* Set the scope from the stored value. */
20752 parser->scope = check_value->value;
20753 parser->qualifying_scope = check_value->qualifying_scope;
20754 parser->object_scope = NULL_TREE;
20757 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20758 encounter the end of a block before what we were looking for. */
20761 cp_parser_cache_group (cp_parser *parser,
20762 enum cpp_ttype end,
20767 cp_token *token = cp_lexer_peek_token (parser->lexer);
20769 /* Abort a parenthesized expression if we encounter a semicolon. */
20770 if ((end == CPP_CLOSE_PAREN || depth == 0)
20771 && token->type == CPP_SEMICOLON)
20773 /* If we've reached the end of the file, stop. */
20774 if (token->type == CPP_EOF
20775 || (end != CPP_PRAGMA_EOL
20776 && token->type == CPP_PRAGMA_EOL))
20778 if (token->type == CPP_CLOSE_BRACE && depth == 0)
20779 /* We've hit the end of an enclosing block, so there's been some
20780 kind of syntax error. */
20783 /* Consume the token. */
20784 cp_lexer_consume_token (parser->lexer);
20785 /* See if it starts a new group. */
20786 if (token->type == CPP_OPEN_BRACE)
20788 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
20789 /* In theory this should probably check end == '}', but
20790 cp_parser_save_member_function_body needs it to exit
20791 after either '}' or ')' when called with ')'. */
20795 else if (token->type == CPP_OPEN_PAREN)
20797 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
20798 if (depth == 0 && end == CPP_CLOSE_PAREN)
20801 else if (token->type == CPP_PRAGMA)
20802 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
20803 else if (token->type == end)
20808 /* Begin parsing tentatively. We always save tokens while parsing
20809 tentatively so that if the tentative parsing fails we can restore the
20813 cp_parser_parse_tentatively (cp_parser* parser)
20815 /* Enter a new parsing context. */
20816 parser->context = cp_parser_context_new (parser->context);
20817 /* Begin saving tokens. */
20818 cp_lexer_save_tokens (parser->lexer);
20819 /* In order to avoid repetitive access control error messages,
20820 access checks are queued up until we are no longer parsing
20822 push_deferring_access_checks (dk_deferred);
20825 /* Commit to the currently active tentative parse. */
20828 cp_parser_commit_to_tentative_parse (cp_parser* parser)
20830 cp_parser_context *context;
20833 /* Mark all of the levels as committed. */
20834 lexer = parser->lexer;
20835 for (context = parser->context; context->next; context = context->next)
20837 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20839 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20840 while (!cp_lexer_saving_tokens (lexer))
20841 lexer = lexer->next;
20842 cp_lexer_commit_tokens (lexer);
20846 /* Abort the currently active tentative parse. All consumed tokens
20847 will be rolled back, and no diagnostics will be issued. */
20850 cp_parser_abort_tentative_parse (cp_parser* parser)
20852 cp_parser_simulate_error (parser);
20853 /* Now, pretend that we want to see if the construct was
20854 successfully parsed. */
20855 cp_parser_parse_definitely (parser);
20858 /* Stop parsing tentatively. If a parse error has occurred, restore the
20859 token stream. Otherwise, commit to the tokens we have consumed.
20860 Returns true if no error occurred; false otherwise. */
20863 cp_parser_parse_definitely (cp_parser* parser)
20865 bool error_occurred;
20866 cp_parser_context *context;
20868 /* Remember whether or not an error occurred, since we are about to
20869 destroy that information. */
20870 error_occurred = cp_parser_error_occurred (parser);
20871 /* Remove the topmost context from the stack. */
20872 context = parser->context;
20873 parser->context = context->next;
20874 /* If no parse errors occurred, commit to the tentative parse. */
20875 if (!error_occurred)
20877 /* Commit to the tokens read tentatively, unless that was
20879 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20880 cp_lexer_commit_tokens (parser->lexer);
20882 pop_to_parent_deferring_access_checks ();
20884 /* Otherwise, if errors occurred, roll back our state so that things
20885 are just as they were before we began the tentative parse. */
20888 cp_lexer_rollback_tokens (parser->lexer);
20889 pop_deferring_access_checks ();
20891 /* Add the context to the front of the free list. */
20892 context->next = cp_parser_context_free_list;
20893 cp_parser_context_free_list = context;
20895 return !error_occurred;
20898 /* Returns true if we are parsing tentatively and are not committed to
20899 this tentative parse. */
20902 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20904 return (cp_parser_parsing_tentatively (parser)
20905 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20908 /* Returns nonzero iff an error has occurred during the most recent
20909 tentative parse. */
20912 cp_parser_error_occurred (cp_parser* parser)
20914 return (cp_parser_parsing_tentatively (parser)
20915 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20918 /* Returns nonzero if GNU extensions are allowed. */
20921 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20923 return parser->allow_gnu_extensions_p;
20926 /* Objective-C++ Productions */
20929 /* Parse an Objective-C expression, which feeds into a primary-expression
20933 objc-message-expression
20934 objc-string-literal
20935 objc-encode-expression
20936 objc-protocol-expression
20937 objc-selector-expression
20939 Returns a tree representation of the expression. */
20942 cp_parser_objc_expression (cp_parser* parser)
20944 /* Try to figure out what kind of declaration is present. */
20945 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20949 case CPP_OPEN_SQUARE:
20950 return cp_parser_objc_message_expression (parser);
20952 case CPP_OBJC_STRING:
20953 kwd = cp_lexer_consume_token (parser->lexer);
20954 return objc_build_string_object (kwd->u.value);
20957 switch (kwd->keyword)
20959 case RID_AT_ENCODE:
20960 return cp_parser_objc_encode_expression (parser);
20962 case RID_AT_PROTOCOL:
20963 return cp_parser_objc_protocol_expression (parser);
20965 case RID_AT_SELECTOR:
20966 return cp_parser_objc_selector_expression (parser);
20972 error_at (kwd->location,
20973 "misplaced %<@%D%> Objective-C++ construct",
20975 cp_parser_skip_to_end_of_block_or_statement (parser);
20978 return error_mark_node;
20981 /* Parse an Objective-C message expression.
20983 objc-message-expression:
20984 [ objc-message-receiver objc-message-args ]
20986 Returns a representation of an Objective-C message. */
20989 cp_parser_objc_message_expression (cp_parser* parser)
20991 tree receiver, messageargs;
20993 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20994 receiver = cp_parser_objc_message_receiver (parser);
20995 messageargs = cp_parser_objc_message_args (parser);
20996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20998 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21001 /* Parse an objc-message-receiver.
21003 objc-message-receiver:
21005 simple-type-specifier
21007 Returns a representation of the type or expression. */
21010 cp_parser_objc_message_receiver (cp_parser* parser)
21014 /* An Objective-C message receiver may be either (1) a type
21015 or (2) an expression. */
21016 cp_parser_parse_tentatively (parser);
21017 rcv = cp_parser_expression (parser, false, NULL);
21019 if (cp_parser_parse_definitely (parser))
21022 rcv = cp_parser_simple_type_specifier (parser,
21023 /*decl_specs=*/NULL,
21024 CP_PARSER_FLAGS_NONE);
21026 return objc_get_class_reference (rcv);
21029 /* Parse the arguments and selectors comprising an Objective-C message.
21034 objc-selector-args , objc-comma-args
21036 objc-selector-args:
21037 objc-selector [opt] : assignment-expression
21038 objc-selector-args objc-selector [opt] : assignment-expression
21041 assignment-expression
21042 objc-comma-args , assignment-expression
21044 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21045 selector arguments and TREE_VALUE containing a list of comma
21049 cp_parser_objc_message_args (cp_parser* parser)
21051 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21052 bool maybe_unary_selector_p = true;
21053 cp_token *token = cp_lexer_peek_token (parser->lexer);
21055 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21057 tree selector = NULL_TREE, arg;
21059 if (token->type != CPP_COLON)
21060 selector = cp_parser_objc_selector (parser);
21062 /* Detect if we have a unary selector. */
21063 if (maybe_unary_selector_p
21064 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21065 return build_tree_list (selector, NULL_TREE);
21067 maybe_unary_selector_p = false;
21068 cp_parser_require (parser, CPP_COLON, RT_COLON);
21069 arg = cp_parser_assignment_expression (parser, false, NULL);
21072 = chainon (sel_args,
21073 build_tree_list (selector, arg));
21075 token = cp_lexer_peek_token (parser->lexer);
21078 /* Handle non-selector arguments, if any. */
21079 while (token->type == CPP_COMMA)
21083 cp_lexer_consume_token (parser->lexer);
21084 arg = cp_parser_assignment_expression (parser, false, NULL);
21087 = chainon (addl_args,
21088 build_tree_list (NULL_TREE, arg));
21090 token = cp_lexer_peek_token (parser->lexer);
21093 return build_tree_list (sel_args, addl_args);
21096 /* Parse an Objective-C encode expression.
21098 objc-encode-expression:
21099 @encode objc-typename
21101 Returns an encoded representation of the type argument. */
21104 cp_parser_objc_encode_expression (cp_parser* parser)
21109 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21110 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21111 token = cp_lexer_peek_token (parser->lexer);
21112 type = complete_type (cp_parser_type_id (parser));
21113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21117 error_at (token->location,
21118 "%<@encode%> must specify a type as an argument");
21119 return error_mark_node;
21122 return objc_build_encode_expr (type);
21125 /* Parse an Objective-C @defs expression. */
21128 cp_parser_objc_defs_expression (cp_parser *parser)
21132 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21134 name = cp_parser_identifier (parser);
21135 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21137 return objc_get_class_ivars (name);
21140 /* Parse an Objective-C protocol expression.
21142 objc-protocol-expression:
21143 @protocol ( identifier )
21145 Returns a representation of the protocol expression. */
21148 cp_parser_objc_protocol_expression (cp_parser* parser)
21152 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21153 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21154 proto = cp_parser_identifier (parser);
21155 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21157 return objc_build_protocol_expr (proto);
21160 /* Parse an Objective-C selector expression.
21162 objc-selector-expression:
21163 @selector ( objc-method-signature )
21165 objc-method-signature:
21171 objc-selector-seq objc-selector :
21173 Returns a representation of the method selector. */
21176 cp_parser_objc_selector_expression (cp_parser* parser)
21178 tree sel_seq = NULL_TREE;
21179 bool maybe_unary_selector_p = true;
21181 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21183 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21184 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21185 token = cp_lexer_peek_token (parser->lexer);
21187 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21188 || token->type == CPP_SCOPE)
21190 tree selector = NULL_TREE;
21192 if (token->type != CPP_COLON
21193 || token->type == CPP_SCOPE)
21194 selector = cp_parser_objc_selector (parser);
21196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21197 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21199 /* Detect if we have a unary selector. */
21200 if (maybe_unary_selector_p)
21202 sel_seq = selector;
21203 goto finish_selector;
21207 cp_parser_error (parser, "expected %<:%>");
21210 maybe_unary_selector_p = false;
21211 token = cp_lexer_consume_token (parser->lexer);
21213 if (token->type == CPP_SCOPE)
21216 = chainon (sel_seq,
21217 build_tree_list (selector, NULL_TREE));
21219 = chainon (sel_seq,
21220 build_tree_list (NULL_TREE, NULL_TREE));
21224 = chainon (sel_seq,
21225 build_tree_list (selector, NULL_TREE));
21227 token = cp_lexer_peek_token (parser->lexer);
21231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21233 return objc_build_selector_expr (loc, sel_seq);
21236 /* Parse a list of identifiers.
21238 objc-identifier-list:
21240 objc-identifier-list , identifier
21242 Returns a TREE_LIST of identifier nodes. */
21245 cp_parser_objc_identifier_list (cp_parser* parser)
21247 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
21248 cp_token *sep = cp_lexer_peek_token (parser->lexer);
21250 while (sep->type == CPP_COMMA)
21252 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21253 list = chainon (list,
21254 build_tree_list (NULL_TREE,
21255 cp_parser_identifier (parser)));
21256 sep = cp_lexer_peek_token (parser->lexer);
21262 /* Parse an Objective-C alias declaration.
21264 objc-alias-declaration:
21265 @compatibility_alias identifier identifier ;
21267 This function registers the alias mapping with the Objective-C front end.
21268 It returns nothing. */
21271 cp_parser_objc_alias_declaration (cp_parser* parser)
21275 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21276 alias = cp_parser_identifier (parser);
21277 orig = cp_parser_identifier (parser);
21278 objc_declare_alias (alias, orig);
21279 cp_parser_consume_semicolon_at_end_of_statement (parser);
21282 /* Parse an Objective-C class forward-declaration.
21284 objc-class-declaration:
21285 @class objc-identifier-list ;
21287 The function registers the forward declarations with the Objective-C
21288 front end. It returns nothing. */
21291 cp_parser_objc_class_declaration (cp_parser* parser)
21293 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21294 objc_declare_class (cp_parser_objc_identifier_list (parser));
21295 cp_parser_consume_semicolon_at_end_of_statement (parser);
21298 /* Parse a list of Objective-C protocol references.
21300 objc-protocol-refs-opt:
21301 objc-protocol-refs [opt]
21303 objc-protocol-refs:
21304 < objc-identifier-list >
21306 Returns a TREE_LIST of identifiers, if any. */
21309 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21311 tree protorefs = NULL_TREE;
21313 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21315 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21316 protorefs = cp_parser_objc_identifier_list (parser);
21317 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21323 /* Parse a Objective-C visibility specification. */
21326 cp_parser_objc_visibility_spec (cp_parser* parser)
21328 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21330 switch (vis->keyword)
21332 case RID_AT_PRIVATE:
21333 objc_set_visibility (2);
21335 case RID_AT_PROTECTED:
21336 objc_set_visibility (0);
21338 case RID_AT_PUBLIC:
21339 objc_set_visibility (1);
21345 /* Eat '@private'/'@protected'/'@public'. */
21346 cp_lexer_consume_token (parser->lexer);
21349 /* Parse an Objective-C method type. */
21352 cp_parser_objc_method_type (cp_parser* parser)
21354 objc_set_method_type
21355 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
21360 /* Parse an Objective-C protocol qualifier. */
21363 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21365 tree quals = NULL_TREE, node;
21366 cp_token *token = cp_lexer_peek_token (parser->lexer);
21368 node = token->u.value;
21370 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21371 && (node == ridpointers [(int) RID_IN]
21372 || node == ridpointers [(int) RID_OUT]
21373 || node == ridpointers [(int) RID_INOUT]
21374 || node == ridpointers [(int) RID_BYCOPY]
21375 || node == ridpointers [(int) RID_BYREF]
21376 || node == ridpointers [(int) RID_ONEWAY]))
21378 quals = tree_cons (NULL_TREE, node, quals);
21379 cp_lexer_consume_token (parser->lexer);
21380 token = cp_lexer_peek_token (parser->lexer);
21381 node = token->u.value;
21387 /* Parse an Objective-C typename. */
21390 cp_parser_objc_typename (cp_parser* parser)
21392 tree type_name = NULL_TREE;
21394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21396 tree proto_quals, cp_type = NULL_TREE;
21398 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21399 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21401 /* An ObjC type name may consist of just protocol qualifiers, in which
21402 case the type shall default to 'id'. */
21403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21404 cp_type = cp_parser_type_id (parser);
21406 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21407 type_name = build_tree_list (proto_quals, cp_type);
21413 /* Check to see if TYPE refers to an Objective-C selector name. */
21416 cp_parser_objc_selector_p (enum cpp_ttype type)
21418 return (type == CPP_NAME || type == CPP_KEYWORD
21419 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21420 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21421 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21422 || type == CPP_XOR || type == CPP_XOR_EQ);
21425 /* Parse an Objective-C selector. */
21428 cp_parser_objc_selector (cp_parser* parser)
21430 cp_token *token = cp_lexer_consume_token (parser->lexer);
21432 if (!cp_parser_objc_selector_p (token->type))
21434 error_at (token->location, "invalid Objective-C++ selector name");
21435 return error_mark_node;
21438 /* C++ operator names are allowed to appear in ObjC selectors. */
21439 switch (token->type)
21441 case CPP_AND_AND: return get_identifier ("and");
21442 case CPP_AND_EQ: return get_identifier ("and_eq");
21443 case CPP_AND: return get_identifier ("bitand");
21444 case CPP_OR: return get_identifier ("bitor");
21445 case CPP_COMPL: return get_identifier ("compl");
21446 case CPP_NOT: return get_identifier ("not");
21447 case CPP_NOT_EQ: return get_identifier ("not_eq");
21448 case CPP_OR_OR: return get_identifier ("or");
21449 case CPP_OR_EQ: return get_identifier ("or_eq");
21450 case CPP_XOR: return get_identifier ("xor");
21451 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21452 default: return token->u.value;
21456 /* Parse an Objective-C params list. */
21459 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21461 tree params = NULL_TREE;
21462 bool maybe_unary_selector_p = true;
21463 cp_token *token = cp_lexer_peek_token (parser->lexer);
21465 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21467 tree selector = NULL_TREE, type_name, identifier;
21468 tree parm_attr = NULL_TREE;
21470 if (token->keyword == RID_ATTRIBUTE)
21473 if (token->type != CPP_COLON)
21474 selector = cp_parser_objc_selector (parser);
21476 /* Detect if we have a unary selector. */
21477 if (maybe_unary_selector_p
21478 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21480 params = selector; /* Might be followed by attributes. */
21484 maybe_unary_selector_p = false;
21485 cp_parser_require (parser, CPP_COLON, RT_COLON);
21486 type_name = cp_parser_objc_typename (parser);
21487 /* New ObjC allows attributes on parameters too. */
21488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21489 parm_attr = cp_parser_attributes_opt (parser);
21490 identifier = cp_parser_identifier (parser);
21494 objc_build_keyword_decl (selector,
21499 token = cp_lexer_peek_token (parser->lexer);
21502 if (params == NULL_TREE)
21504 cp_parser_error (parser, "objective-c++ method declaration is expected");
21505 return error_mark_node;
21508 /* We allow tail attributes for the method. */
21509 if (token->keyword == RID_ATTRIBUTE)
21511 *attributes = cp_parser_attributes_opt (parser);
21512 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21513 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21515 cp_parser_error (parser,
21516 "method attributes must be specified at the end");
21517 return error_mark_node;
21523 /* Parse the non-keyword Objective-C params. */
21526 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21529 tree params = make_node (TREE_LIST);
21530 cp_token *token = cp_lexer_peek_token (parser->lexer);
21531 *ellipsisp = false; /* Initially, assume no ellipsis. */
21533 while (token->type == CPP_COMMA)
21535 cp_parameter_declarator *parmdecl;
21538 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21539 token = cp_lexer_peek_token (parser->lexer);
21541 if (token->type == CPP_ELLIPSIS)
21543 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21548 /* TODO: parse attributes for tail parameters. */
21549 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21550 parm = grokdeclarator (parmdecl->declarator,
21551 &parmdecl->decl_specifiers,
21552 PARM, /*initialized=*/0,
21553 /*attrlist=*/NULL);
21555 chainon (params, build_tree_list (NULL_TREE, parm));
21556 token = cp_lexer_peek_token (parser->lexer);
21559 /* We allow tail attributes for the method. */
21560 if (token->keyword == RID_ATTRIBUTE)
21562 if (*attributes == NULL_TREE)
21564 *attributes = cp_parser_attributes_opt (parser);
21565 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21566 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21570 /* We have an error, but parse the attributes, so that we can
21572 *attributes = cp_parser_attributes_opt (parser);
21574 cp_parser_error (parser,
21575 "method attributes must be specified at the end");
21576 return error_mark_node;
21582 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21585 cp_parser_objc_interstitial_code (cp_parser* parser)
21587 cp_token *token = cp_lexer_peek_token (parser->lexer);
21589 /* If the next token is `extern' and the following token is a string
21590 literal, then we have a linkage specification. */
21591 if (token->keyword == RID_EXTERN
21592 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21593 cp_parser_linkage_specification (parser);
21594 /* Handle #pragma, if any. */
21595 else if (token->type == CPP_PRAGMA)
21596 cp_parser_pragma (parser, pragma_external);
21597 /* Allow stray semicolons. */
21598 else if (token->type == CPP_SEMICOLON)
21599 cp_lexer_consume_token (parser->lexer);
21600 /* Finally, try to parse a block-declaration, or a function-definition. */
21602 cp_parser_block_declaration (parser, /*statement_p=*/false);
21605 /* Parse a method signature. */
21608 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21610 tree rettype, kwdparms, optparms;
21611 bool ellipsis = false;
21613 cp_parser_objc_method_type (parser);
21614 rettype = cp_parser_objc_typename (parser);
21615 *attributes = NULL_TREE;
21616 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21617 if (kwdparms == error_mark_node)
21618 return error_mark_node;
21619 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21620 if (optparms == error_mark_node)
21621 return error_mark_node;
21623 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
21627 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21630 cp_lexer_save_tokens (parser->lexer);
21631 tattr = cp_parser_attributes_opt (parser);
21632 gcc_assert (tattr) ;
21634 /* If the attributes are followed by a method introducer, this is not allowed.
21635 Dump the attributes and flag the situation. */
21636 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21637 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21640 /* Otherwise, the attributes introduce some interstitial code, possibly so
21641 rewind to allow that check. */
21642 cp_lexer_rollback_tokens (parser->lexer);
21646 /* Parse an Objective-C method prototype list. */
21649 cp_parser_objc_method_prototype_list (cp_parser* parser)
21651 cp_token *token = cp_lexer_peek_token (parser->lexer);
21653 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21655 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21657 tree attributes, sig;
21658 sig = cp_parser_objc_method_signature (parser, &attributes);
21659 if (sig == error_mark_node)
21661 cp_parser_skip_to_end_of_block_or_statement (parser);
21664 objc_add_method_declaration (sig, attributes);
21665 cp_parser_consume_semicolon_at_end_of_statement (parser);
21667 else if (token->keyword == RID_ATTRIBUTE
21668 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21669 warning_at (cp_lexer_peek_token (parser->lexer)->location,
21671 "prefix attributes are ignored for methods");
21673 /* Allow for interspersed non-ObjC++ code. */
21674 cp_parser_objc_interstitial_code (parser);
21676 token = cp_lexer_peek_token (parser->lexer);
21679 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21680 objc_finish_interface ();
21683 /* Parse an Objective-C method definition list. */
21686 cp_parser_objc_method_definition_list (cp_parser* parser)
21688 cp_token *token = cp_lexer_peek_token (parser->lexer);
21690 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21694 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21697 tree sig, attribute;
21698 push_deferring_access_checks (dk_deferred);
21699 sig = cp_parser_objc_method_signature (parser, &attribute);
21700 if (sig == error_mark_node)
21702 cp_parser_skip_to_end_of_block_or_statement (parser);
21705 objc_start_method_definition (sig, attribute);
21707 /* For historical reasons, we accept an optional semicolon. */
21708 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21709 cp_lexer_consume_token (parser->lexer);
21711 ptk = cp_lexer_peek_token (parser->lexer);
21712 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
21713 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
21715 perform_deferred_access_checks ();
21716 stop_deferring_access_checks ();
21717 meth = cp_parser_function_definition_after_declarator (parser,
21719 pop_deferring_access_checks ();
21720 objc_finish_method_definition (meth);
21723 else if (token->keyword == RID_ATTRIBUTE
21724 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21725 warning_at (token->location, OPT_Wattributes,
21726 "prefix attributes are ignored for methods");
21728 /* Allow for interspersed non-ObjC++ code. */
21729 cp_parser_objc_interstitial_code (parser);
21731 token = cp_lexer_peek_token (parser->lexer);
21734 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21735 objc_finish_implementation ();
21738 /* Parse Objective-C ivars. */
21741 cp_parser_objc_class_ivars (cp_parser* parser)
21743 cp_token *token = cp_lexer_peek_token (parser->lexer);
21745 if (token->type != CPP_OPEN_BRACE)
21746 return; /* No ivars specified. */
21748 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
21749 token = cp_lexer_peek_token (parser->lexer);
21751 while (token->type != CPP_CLOSE_BRACE)
21753 cp_decl_specifier_seq declspecs;
21754 int decl_class_or_enum_p;
21755 tree prefix_attributes;
21757 cp_parser_objc_visibility_spec (parser);
21759 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21762 cp_parser_decl_specifier_seq (parser,
21763 CP_PARSER_FLAGS_OPTIONAL,
21765 &decl_class_or_enum_p);
21766 prefix_attributes = declspecs.attributes;
21767 declspecs.attributes = NULL_TREE;
21769 /* Keep going until we hit the `;' at the end of the
21771 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21773 tree width = NULL_TREE, attributes, first_attribute, decl;
21774 cp_declarator *declarator = NULL;
21775 int ctor_dtor_or_conv_p;
21777 /* Check for a (possibly unnamed) bitfield declaration. */
21778 token = cp_lexer_peek_token (parser->lexer);
21779 if (token->type == CPP_COLON)
21782 if (token->type == CPP_NAME
21783 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21786 /* Get the name of the bitfield. */
21787 declarator = make_id_declarator (NULL_TREE,
21788 cp_parser_identifier (parser),
21792 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21793 /* Get the width of the bitfield. */
21795 = cp_parser_constant_expression (parser,
21796 /*allow_non_constant=*/false,
21801 /* Parse the declarator. */
21803 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21804 &ctor_dtor_or_conv_p,
21805 /*parenthesized_p=*/NULL,
21806 /*member_p=*/false);
21809 /* Look for attributes that apply to the ivar. */
21810 attributes = cp_parser_attributes_opt (parser);
21811 /* Remember which attributes are prefix attributes and
21813 first_attribute = attributes;
21814 /* Combine the attributes. */
21815 attributes = chainon (prefix_attributes, attributes);
21818 /* Create the bitfield declaration. */
21819 decl = grokbitfield (declarator, &declspecs,
21823 decl = grokfield (declarator, &declspecs,
21824 NULL_TREE, /*init_const_expr_p=*/false,
21825 NULL_TREE, attributes);
21827 /* Add the instance variable. */
21828 objc_add_instance_variable (decl);
21830 /* Reset PREFIX_ATTRIBUTES. */
21831 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21832 attributes = TREE_CHAIN (attributes);
21834 TREE_CHAIN (attributes) = NULL_TREE;
21836 token = cp_lexer_peek_token (parser->lexer);
21838 if (token->type == CPP_COMMA)
21840 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21846 cp_parser_consume_semicolon_at_end_of_statement (parser);
21847 token = cp_lexer_peek_token (parser->lexer);
21850 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
21851 /* For historical reasons, we accept an optional semicolon. */
21852 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21853 cp_lexer_consume_token (parser->lexer);
21856 /* Parse an Objective-C protocol declaration. */
21859 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
21861 tree proto, protorefs;
21864 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
21867 tok = cp_lexer_peek_token (parser->lexer);
21868 error_at (tok->location, "identifier expected after %<@protocol%>");
21872 /* See if we have a forward declaration or a definition. */
21873 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
21875 /* Try a forward declaration first. */
21876 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
21878 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
21880 cp_parser_consume_semicolon_at_end_of_statement (parser);
21883 /* Ok, we got a full-fledged definition (or at least should). */
21886 proto = cp_parser_identifier (parser);
21887 protorefs = cp_parser_objc_protocol_refs_opt (parser);
21888 objc_start_protocol (proto, protorefs, attributes);
21889 cp_parser_objc_method_prototype_list (parser);
21893 /* Parse an Objective-C superclass or category. */
21896 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
21899 cp_token *next = cp_lexer_peek_token (parser->lexer);
21901 *super = *categ = NULL_TREE;
21902 if (next->type == CPP_COLON)
21904 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
21905 *super = cp_parser_identifier (parser);
21907 else if (next->type == CPP_OPEN_PAREN)
21909 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21910 *categ = cp_parser_identifier (parser);
21911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21915 /* Parse an Objective-C class interface. */
21918 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
21920 tree name, super, categ, protos;
21922 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
21923 name = cp_parser_identifier (parser);
21924 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21925 protos = cp_parser_objc_protocol_refs_opt (parser);
21927 /* We have either a class or a category on our hands. */
21929 objc_start_category_interface (name, categ, protos, attributes);
21932 objc_start_class_interface (name, super, protos, attributes);
21933 /* Handle instance variable declarations, if any. */
21934 cp_parser_objc_class_ivars (parser);
21935 objc_continue_interface ();
21938 cp_parser_objc_method_prototype_list (parser);
21941 /* Parse an Objective-C class implementation. */
21944 cp_parser_objc_class_implementation (cp_parser* parser)
21946 tree name, super, categ;
21948 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
21949 name = cp_parser_identifier (parser);
21950 cp_parser_objc_superclass_or_category (parser, &super, &categ);
21952 /* We have either a class or a category on our hands. */
21954 objc_start_category_implementation (name, categ);
21957 objc_start_class_implementation (name, super);
21958 /* Handle instance variable declarations, if any. */
21959 cp_parser_objc_class_ivars (parser);
21960 objc_continue_implementation ();
21963 cp_parser_objc_method_definition_list (parser);
21966 /* Consume the @end token and finish off the implementation. */
21969 cp_parser_objc_end_implementation (cp_parser* parser)
21971 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21972 objc_finish_implementation ();
21975 /* Parse an Objective-C declaration. */
21978 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
21980 /* Try to figure out what kind of declaration is present. */
21981 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21984 switch (kwd->keyword)
21989 error_at (kwd->location, "attributes may not be specified before"
21990 " the %<@%D%> Objective-C++ keyword",
21994 case RID_AT_IMPLEMENTATION:
21995 warning_at (kwd->location, OPT_Wattributes,
21996 "prefix attributes are ignored before %<@%D%>",
22003 switch (kwd->keyword)
22006 cp_parser_objc_alias_declaration (parser);
22009 cp_parser_objc_class_declaration (parser);
22011 case RID_AT_PROTOCOL:
22012 cp_parser_objc_protocol_declaration (parser, attributes);
22014 case RID_AT_INTERFACE:
22015 cp_parser_objc_class_interface (parser, attributes);
22017 case RID_AT_IMPLEMENTATION:
22018 cp_parser_objc_class_implementation (parser);
22021 cp_parser_objc_end_implementation (parser);
22024 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22026 cp_parser_skip_to_end_of_block_or_statement (parser);
22030 /* Parse an Objective-C try-catch-finally statement.
22032 objc-try-catch-finally-stmt:
22033 @try compound-statement objc-catch-clause-seq [opt]
22034 objc-finally-clause [opt]
22036 objc-catch-clause-seq:
22037 objc-catch-clause objc-catch-clause-seq [opt]
22040 @catch ( exception-declaration ) compound-statement
22042 objc-finally-clause
22043 @finally compound-statement
22045 Returns NULL_TREE. */
22048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22049 location_t location;
22052 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22053 location = cp_lexer_peek_token (parser->lexer)->location;
22054 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22055 node, lest it get absorbed into the surrounding block. */
22056 stmt = push_stmt_list ();
22057 cp_parser_compound_statement (parser, NULL, false);
22058 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22062 cp_parameter_declarator *parmdecl;
22065 cp_lexer_consume_token (parser->lexer);
22066 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22067 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22068 parm = grokdeclarator (parmdecl->declarator,
22069 &parmdecl->decl_specifiers,
22070 PARM, /*initialized=*/0,
22071 /*attrlist=*/NULL);
22072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22073 objc_begin_catch_clause (parm);
22074 cp_parser_compound_statement (parser, NULL, false);
22075 objc_finish_catch_clause ();
22078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22080 cp_lexer_consume_token (parser->lexer);
22081 location = cp_lexer_peek_token (parser->lexer)->location;
22082 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22083 node, lest it get absorbed into the surrounding block. */
22084 stmt = push_stmt_list ();
22085 cp_parser_compound_statement (parser, NULL, false);
22086 objc_build_finally_clause (location, pop_stmt_list (stmt));
22089 return objc_finish_try_stmt ();
22092 /* Parse an Objective-C synchronized statement.
22094 objc-synchronized-stmt:
22095 @synchronized ( expression ) compound-statement
22097 Returns NULL_TREE. */
22100 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22101 location_t location;
22104 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22106 location = cp_lexer_peek_token (parser->lexer)->location;
22107 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22108 lock = cp_parser_expression (parser, false, NULL);
22109 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22111 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22112 node, lest it get absorbed into the surrounding block. */
22113 stmt = push_stmt_list ();
22114 cp_parser_compound_statement (parser, NULL, false);
22116 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22119 /* Parse an Objective-C throw statement.
22122 @throw assignment-expression [opt] ;
22124 Returns a constructed '@throw' statement. */
22127 cp_parser_objc_throw_statement (cp_parser *parser) {
22128 tree expr = NULL_TREE;
22129 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22131 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22134 expr = cp_parser_assignment_expression (parser, false, NULL);
22136 cp_parser_consume_semicolon_at_end_of_statement (parser);
22138 return objc_build_throw_stmt (loc, expr);
22141 /* Parse an Objective-C statement. */
22144 cp_parser_objc_statement (cp_parser * parser) {
22145 /* Try to figure out what kind of declaration is present. */
22146 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22148 switch (kwd->keyword)
22151 return cp_parser_objc_try_catch_finally_statement (parser);
22152 case RID_AT_SYNCHRONIZED:
22153 return cp_parser_objc_synchronized_statement (parser);
22155 return cp_parser_objc_throw_statement (parser);
22157 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22159 cp_parser_skip_to_end_of_block_or_statement (parser);
22162 return error_mark_node;
22165 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22166 look ahead to see if an objc keyword follows the attributes. This
22167 is to detect the use of prefix attributes on ObjC @interface and
22171 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22173 cp_lexer_save_tokens (parser->lexer);
22174 *attrib = cp_parser_attributes_opt (parser);
22175 gcc_assert (*attrib);
22176 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22178 cp_lexer_commit_tokens (parser->lexer);
22181 cp_lexer_rollback_tokens (parser->lexer);
22185 /* OpenMP 2.5 parsing routines. */
22187 /* Returns name of the next clause.
22188 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22189 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22190 returned and the token is consumed. */
22192 static pragma_omp_clause
22193 cp_parser_omp_clause_name (cp_parser *parser)
22195 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22198 result = PRAGMA_OMP_CLAUSE_IF;
22199 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22200 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22201 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22202 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22203 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22205 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22206 const char *p = IDENTIFIER_POINTER (id);
22211 if (!strcmp ("collapse", p))
22212 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22213 else if (!strcmp ("copyin", p))
22214 result = PRAGMA_OMP_CLAUSE_COPYIN;
22215 else if (!strcmp ("copyprivate", p))
22216 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22219 if (!strcmp ("firstprivate", p))
22220 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
22223 if (!strcmp ("lastprivate", p))
22224 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
22227 if (!strcmp ("nowait", p))
22228 result = PRAGMA_OMP_CLAUSE_NOWAIT;
22229 else if (!strcmp ("num_threads", p))
22230 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
22233 if (!strcmp ("ordered", p))
22234 result = PRAGMA_OMP_CLAUSE_ORDERED;
22237 if (!strcmp ("reduction", p))
22238 result = PRAGMA_OMP_CLAUSE_REDUCTION;
22241 if (!strcmp ("schedule", p))
22242 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
22243 else if (!strcmp ("shared", p))
22244 result = PRAGMA_OMP_CLAUSE_SHARED;
22247 if (!strcmp ("untied", p))
22248 result = PRAGMA_OMP_CLAUSE_UNTIED;
22253 if (result != PRAGMA_OMP_CLAUSE_NONE)
22254 cp_lexer_consume_token (parser->lexer);
22259 /* Validate that a clause of the given type does not already exist. */
22262 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
22263 const char *name, location_t location)
22267 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22268 if (OMP_CLAUSE_CODE (c) == code)
22270 error_at (location, "too many %qs clauses", name);
22278 variable-list , identifier
22280 In addition, we match a closing parenthesis. An opening parenthesis
22281 will have been consumed by the caller.
22283 If KIND is nonzero, create the appropriate node and install the decl
22284 in OMP_CLAUSE_DECL and add the node to the head of the list.
22286 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22287 return the list created. */
22290 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
22298 token = cp_lexer_peek_token (parser->lexer);
22299 name = cp_parser_id_expression (parser, /*template_p=*/false,
22300 /*check_dependency_p=*/true,
22301 /*template_p=*/NULL,
22302 /*declarator_p=*/false,
22303 /*optional_p=*/false);
22304 if (name == error_mark_node)
22307 decl = cp_parser_lookup_name_simple (parser, name, token->location);
22308 if (decl == error_mark_node)
22309 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
22311 else if (kind != 0)
22313 tree u = build_omp_clause (token->location, kind);
22314 OMP_CLAUSE_DECL (u) = decl;
22315 OMP_CLAUSE_CHAIN (u) = list;
22319 list = tree_cons (decl, NULL_TREE, list);
22322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22324 cp_lexer_consume_token (parser->lexer);
22327 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22331 /* Try to resync to an unnested comma. Copied from
22332 cp_parser_parenthesized_expression_list. */
22334 ending = cp_parser_skip_to_closing_parenthesis (parser,
22335 /*recovering=*/true,
22337 /*consume_paren=*/true);
22345 /* Similarly, but expect leading and trailing parenthesis. This is a very
22346 common case for omp clauses. */
22349 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
22351 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22352 return cp_parser_omp_var_list_no_open (parser, kind, list);
22357 collapse ( constant-expression ) */
22360 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
22366 loc = cp_lexer_peek_token (parser->lexer)->location;
22367 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22370 num = cp_parser_constant_expression (parser, false, NULL);
22372 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22374 /*or_comma=*/false,
22375 /*consume_paren=*/true);
22377 if (num == error_mark_node)
22379 num = fold_non_dependent_expr (num);
22380 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
22381 || !host_integerp (num, 0)
22382 || (n = tree_low_cst (num, 0)) <= 0
22385 error_at (loc, "collapse argument needs positive constant integer expression");
22389 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
22390 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
22391 OMP_CLAUSE_CHAIN (c) = list;
22392 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
22398 default ( shared | none ) */
22401 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
22403 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
22406 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22410 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22411 const char *p = IDENTIFIER_POINTER (id);
22416 if (strcmp ("none", p) != 0)
22418 kind = OMP_CLAUSE_DEFAULT_NONE;
22422 if (strcmp ("shared", p) != 0)
22424 kind = OMP_CLAUSE_DEFAULT_SHARED;
22431 cp_lexer_consume_token (parser->lexer);
22436 cp_parser_error (parser, "expected %<none%> or %<shared%>");
22439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22441 /*or_comma=*/false,
22442 /*consume_paren=*/true);
22444 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
22447 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
22448 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
22449 OMP_CLAUSE_CHAIN (c) = list;
22450 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
22456 if ( expression ) */
22459 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
22463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22466 t = cp_parser_condition (parser);
22468 if (t == error_mark_node
22469 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22471 /*or_comma=*/false,
22472 /*consume_paren=*/true);
22474 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
22476 c = build_omp_clause (location, OMP_CLAUSE_IF);
22477 OMP_CLAUSE_IF_EXPR (c) = t;
22478 OMP_CLAUSE_CHAIN (c) = list;
22487 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
22488 tree list, location_t location)
22492 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
22494 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
22495 OMP_CLAUSE_CHAIN (c) = list;
22500 num_threads ( expression ) */
22503 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
22504 location_t location)
22508 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22511 t = cp_parser_expression (parser, false, NULL);
22513 if (t == error_mark_node
22514 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22516 /*or_comma=*/false,
22517 /*consume_paren=*/true);
22519 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
22520 "num_threads", location);
22522 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
22523 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
22524 OMP_CLAUSE_CHAIN (c) = list;
22533 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
22534 tree list, location_t location)
22538 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
22539 "ordered", location);
22541 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
22542 OMP_CLAUSE_CHAIN (c) = list;
22547 reduction ( reduction-operator : variable-list )
22549 reduction-operator:
22550 One of: + * - & ^ | && || */
22553 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
22555 enum tree_code code;
22558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22561 switch (cp_lexer_peek_token (parser->lexer)->type)
22573 code = BIT_AND_EXPR;
22576 code = BIT_XOR_EXPR;
22579 code = BIT_IOR_EXPR;
22582 code = TRUTH_ANDIF_EXPR;
22585 code = TRUTH_ORIF_EXPR;
22588 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22589 "%<|%>, %<&&%>, or %<||%>");
22591 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22592 /*or_comma=*/false,
22593 /*consume_paren=*/true);
22596 cp_lexer_consume_token (parser->lexer);
22598 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22601 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
22602 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
22603 OMP_CLAUSE_REDUCTION_CODE (c) = code;
22609 schedule ( schedule-kind )
22610 schedule ( schedule-kind , expression )
22613 static | dynamic | guided | runtime | auto */
22616 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
22620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22623 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
22625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22628 const char *p = IDENTIFIER_POINTER (id);
22633 if (strcmp ("dynamic", p) != 0)
22635 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
22639 if (strcmp ("guided", p) != 0)
22641 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
22645 if (strcmp ("runtime", p) != 0)
22647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
22654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
22655 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
22656 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
22657 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
22660 cp_lexer_consume_token (parser->lexer);
22662 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22665 cp_lexer_consume_token (parser->lexer);
22667 token = cp_lexer_peek_token (parser->lexer);
22668 t = cp_parser_assignment_expression (parser, false, NULL);
22670 if (t == error_mark_node)
22672 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
22673 error_at (token->location, "schedule %<runtime%> does not take "
22674 "a %<chunk_size%> parameter");
22675 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
22676 error_at (token->location, "schedule %<auto%> does not take "
22677 "a %<chunk_size%> parameter");
22679 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
22681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22684 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
22687 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
22688 OMP_CLAUSE_CHAIN (c) = list;
22692 cp_parser_error (parser, "invalid schedule kind");
22694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22695 /*or_comma=*/false,
22696 /*consume_paren=*/true);
22704 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
22705 tree list, location_t location)
22709 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
22711 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
22712 OMP_CLAUSE_CHAIN (c) = list;
22716 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22717 is a bitmask in MASK. Return the list of clauses found; the result
22718 of clause default goes in *pdefault. */
22721 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
22722 const char *where, cp_token *pragma_tok)
22724 tree clauses = NULL;
22726 cp_token *token = NULL;
22728 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
22730 pragma_omp_clause c_kind;
22731 const char *c_name;
22732 tree prev = clauses;
22734 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22735 cp_lexer_consume_token (parser->lexer);
22737 token = cp_lexer_peek_token (parser->lexer);
22738 c_kind = cp_parser_omp_clause_name (parser);
22743 case PRAGMA_OMP_CLAUSE_COLLAPSE:
22744 clauses = cp_parser_omp_clause_collapse (parser, clauses,
22746 c_name = "collapse";
22748 case PRAGMA_OMP_CLAUSE_COPYIN:
22749 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
22752 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
22753 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
22755 c_name = "copyprivate";
22757 case PRAGMA_OMP_CLAUSE_DEFAULT:
22758 clauses = cp_parser_omp_clause_default (parser, clauses,
22760 c_name = "default";
22762 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
22763 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
22765 c_name = "firstprivate";
22767 case PRAGMA_OMP_CLAUSE_IF:
22768 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
22771 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
22772 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
22774 c_name = "lastprivate";
22776 case PRAGMA_OMP_CLAUSE_NOWAIT:
22777 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
22780 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
22781 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
22783 c_name = "num_threads";
22785 case PRAGMA_OMP_CLAUSE_ORDERED:
22786 clauses = cp_parser_omp_clause_ordered (parser, clauses,
22788 c_name = "ordered";
22790 case PRAGMA_OMP_CLAUSE_PRIVATE:
22791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
22793 c_name = "private";
22795 case PRAGMA_OMP_CLAUSE_REDUCTION:
22796 clauses = cp_parser_omp_clause_reduction (parser, clauses);
22797 c_name = "reduction";
22799 case PRAGMA_OMP_CLAUSE_SCHEDULE:
22800 clauses = cp_parser_omp_clause_schedule (parser, clauses,
22802 c_name = "schedule";
22804 case PRAGMA_OMP_CLAUSE_SHARED:
22805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
22809 case PRAGMA_OMP_CLAUSE_UNTIED:
22810 clauses = cp_parser_omp_clause_untied (parser, clauses,
22815 cp_parser_error (parser, "expected %<#pragma omp%> clause");
22819 if (((mask >> c_kind) & 1) == 0)
22821 /* Remove the invalid clause(s) from the list to avoid
22822 confusing the rest of the compiler. */
22824 error_at (token->location, "%qs is not valid for %qs", c_name, where);
22828 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22829 return finish_omp_clauses (clauses);
22836 In practice, we're also interested in adding the statement to an
22837 outer node. So it is convenient if we work around the fact that
22838 cp_parser_statement calls add_stmt. */
22841 cp_parser_begin_omp_structured_block (cp_parser *parser)
22843 unsigned save = parser->in_statement;
22845 /* Only move the values to IN_OMP_BLOCK if they weren't false.
22846 This preserves the "not within loop or switch" style error messages
22847 for nonsense cases like
22853 if (parser->in_statement)
22854 parser->in_statement = IN_OMP_BLOCK;
22860 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
22862 parser->in_statement = save;
22866 cp_parser_omp_structured_block (cp_parser *parser)
22868 tree stmt = begin_omp_structured_block ();
22869 unsigned int save = cp_parser_begin_omp_structured_block (parser);
22871 cp_parser_statement (parser, NULL_TREE, false, NULL);
22873 cp_parser_end_omp_structured_block (parser, save);
22874 return finish_omp_structured_block (stmt);
22878 # pragma omp atomic new-line
22882 x binop= expr | x++ | ++x | x-- | --x
22884 +, *, -, /, &, ^, |, <<, >>
22886 where x is an lvalue expression with scalar type. */
22889 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
22892 enum tree_code code;
22894 cp_parser_require_pragma_eol (parser, pragma_tok);
22896 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
22897 /*cast_p=*/false, NULL);
22898 switch (TREE_CODE (lhs))
22903 case PREINCREMENT_EXPR:
22904 case POSTINCREMENT_EXPR:
22905 lhs = TREE_OPERAND (lhs, 0);
22907 rhs = integer_one_node;
22910 case PREDECREMENT_EXPR:
22911 case POSTDECREMENT_EXPR:
22912 lhs = TREE_OPERAND (lhs, 0);
22914 rhs = integer_one_node;
22917 case COMPOUND_EXPR:
22918 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
22919 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
22920 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
22921 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
22922 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
22923 (TREE_OPERAND (lhs, 1), 0), 0)))
22925 /* Undo effects of boolean_increment for post {in,de}crement. */
22926 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
22929 if (TREE_CODE (lhs) == MODIFY_EXPR
22930 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
22932 /* Undo effects of boolean_increment. */
22933 if (integer_onep (TREE_OPERAND (lhs, 1)))
22935 /* This is pre or post increment. */
22936 rhs = TREE_OPERAND (lhs, 1);
22937 lhs = TREE_OPERAND (lhs, 0);
22944 switch (cp_lexer_peek_token (parser->lexer)->type)
22950 code = TRUNC_DIV_EXPR;
22958 case CPP_LSHIFT_EQ:
22959 code = LSHIFT_EXPR;
22961 case CPP_RSHIFT_EQ:
22962 code = RSHIFT_EXPR;
22965 code = BIT_AND_EXPR;
22968 code = BIT_IOR_EXPR;
22971 code = BIT_XOR_EXPR;
22974 cp_parser_error (parser,
22975 "invalid operator for %<#pragma omp atomic%>");
22978 cp_lexer_consume_token (parser->lexer);
22980 rhs = cp_parser_expression (parser, false, NULL);
22981 if (rhs == error_mark_node)
22985 finish_omp_atomic (code, lhs, rhs);
22986 cp_parser_consume_semicolon_at_end_of_statement (parser);
22990 cp_parser_skip_to_end_of_block_or_statement (parser);
22995 # pragma omp barrier new-line */
22998 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23000 cp_parser_require_pragma_eol (parser, pragma_tok);
23001 finish_omp_barrier ();
23005 # pragma omp critical [(name)] new-line
23006 structured-block */
23009 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23011 tree stmt, name = NULL;
23013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23015 cp_lexer_consume_token (parser->lexer);
23017 name = cp_parser_identifier (parser);
23019 if (name == error_mark_node
23020 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23021 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23022 /*or_comma=*/false,
23023 /*consume_paren=*/true);
23024 if (name == error_mark_node)
23027 cp_parser_require_pragma_eol (parser, pragma_tok);
23029 stmt = cp_parser_omp_structured_block (parser);
23030 return c_finish_omp_critical (input_location, stmt, name);
23034 # pragma omp flush flush-vars[opt] new-line
23037 ( variable-list ) */
23040 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23042 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23043 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23044 cp_parser_require_pragma_eol (parser, pragma_tok);
23046 finish_omp_flush ();
23049 /* Helper function, to parse omp for increment expression. */
23052 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23054 tree cond = cp_parser_binary_expression (parser, false, true,
23055 PREC_NOT_OPERATOR, NULL);
23058 if (cond == error_mark_node
23059 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23061 cp_parser_skip_to_end_of_statement (parser);
23062 return error_mark_node;
23065 switch (TREE_CODE (cond))
23073 return error_mark_node;
23076 /* If decl is an iterator, preserve LHS and RHS of the relational
23077 expr until finish_omp_for. */
23079 && (type_dependent_expression_p (decl)
23080 || CLASS_TYPE_P (TREE_TYPE (decl))))
23083 return build_x_binary_op (TREE_CODE (cond),
23084 TREE_OPERAND (cond, 0), ERROR_MARK,
23085 TREE_OPERAND (cond, 1), ERROR_MARK,
23086 &overloaded_p, tf_warning_or_error);
23089 /* Helper function, to parse omp for increment expression. */
23092 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23094 cp_token *token = cp_lexer_peek_token (parser->lexer);
23100 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23102 op = (token->type == CPP_PLUS_PLUS
23103 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23104 cp_lexer_consume_token (parser->lexer);
23105 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23107 return error_mark_node;
23108 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23111 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23113 return error_mark_node;
23115 token = cp_lexer_peek_token (parser->lexer);
23116 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23118 op = (token->type == CPP_PLUS_PLUS
23119 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23120 cp_lexer_consume_token (parser->lexer);
23121 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23124 op = cp_parser_assignment_operator_opt (parser);
23125 if (op == ERROR_MARK)
23126 return error_mark_node;
23128 if (op != NOP_EXPR)
23130 rhs = cp_parser_assignment_expression (parser, false, NULL);
23131 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23132 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23135 lhs = cp_parser_binary_expression (parser, false, false,
23136 PREC_ADDITIVE_EXPRESSION, NULL);
23137 token = cp_lexer_peek_token (parser->lexer);
23138 decl_first = lhs == decl;
23141 if (token->type != CPP_PLUS
23142 && token->type != CPP_MINUS)
23143 return error_mark_node;
23147 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23148 cp_lexer_consume_token (parser->lexer);
23149 rhs = cp_parser_binary_expression (parser, false, false,
23150 PREC_ADDITIVE_EXPRESSION, NULL);
23151 token = cp_lexer_peek_token (parser->lexer);
23152 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23154 if (lhs == NULL_TREE)
23156 if (op == PLUS_EXPR)
23159 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23162 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23163 NULL, tf_warning_or_error);
23166 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23170 if (rhs != decl || op == MINUS_EXPR)
23171 return error_mark_node;
23172 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23175 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23177 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23180 /* Parse the restricted form of the for statement allowed by OpenMP. */
23183 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23185 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23186 tree real_decl, initv, condv, incrv, declv;
23187 tree this_pre_body, cl;
23188 location_t loc_first;
23189 bool collapse_err = false;
23190 int i, collapse = 1, nbraces = 0;
23191 VEC(tree,gc) *for_block = make_tree_vector ();
23193 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23194 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23195 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23197 gcc_assert (collapse >= 1);
23199 declv = make_tree_vec (collapse);
23200 initv = make_tree_vec (collapse);
23201 condv = make_tree_vec (collapse);
23202 incrv = make_tree_vec (collapse);
23204 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23206 for (i = 0; i < collapse; i++)
23208 int bracecount = 0;
23209 bool add_private_clause = false;
23212 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23214 cp_parser_error (parser, "for statement expected");
23217 loc = cp_lexer_consume_token (parser->lexer)->location;
23219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23222 init = decl = real_decl = NULL;
23223 this_pre_body = push_stmt_list ();
23224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23226 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23230 integer-type var = lb
23231 random-access-iterator-type var = lb
23232 pointer-type var = lb
23234 cp_decl_specifier_seq type_specifiers;
23236 /* First, try to parse as an initialized declaration. See
23237 cp_parser_condition, from whence the bulk of this is copied. */
23239 cp_parser_parse_tentatively (parser);
23240 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23241 /*is_trailing_return=*/false,
23243 if (cp_parser_parse_definitely (parser))
23245 /* If parsing a type specifier seq succeeded, then this
23246 MUST be a initialized declaration. */
23247 tree asm_specification, attributes;
23248 cp_declarator *declarator;
23250 declarator = cp_parser_declarator (parser,
23251 CP_PARSER_DECLARATOR_NAMED,
23252 /*ctor_dtor_or_conv_p=*/NULL,
23253 /*parenthesized_p=*/NULL,
23254 /*member_p=*/false);
23255 attributes = cp_parser_attributes_opt (parser);
23256 asm_specification = cp_parser_asm_specification_opt (parser);
23258 if (declarator == cp_error_declarator)
23259 cp_parser_skip_to_end_of_statement (parser);
23263 tree pushed_scope, auto_node;
23265 decl = start_decl (declarator, &type_specifiers,
23266 SD_INITIALIZED, attributes,
23267 /*prefix_attributes=*/NULL_TREE,
23270 auto_node = type_uses_auto (TREE_TYPE (decl));
23271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23273 if (cp_lexer_next_token_is (parser->lexer,
23275 error ("parenthesized initialization is not allowed in "
23276 "OpenMP %<for%> loop");
23278 /* Trigger an error. */
23279 cp_parser_require (parser, CPP_EQ, RT_EQ);
23281 init = error_mark_node;
23282 cp_parser_skip_to_end_of_statement (parser);
23284 else if (CLASS_TYPE_P (TREE_TYPE (decl))
23285 || type_dependent_expression_p (decl)
23288 bool is_direct_init, is_non_constant_init;
23290 init = cp_parser_initializer (parser,
23292 &is_non_constant_init);
23294 if (auto_node && describable_type (init))
23297 = do_auto_deduction (TREE_TYPE (decl), init,
23300 if (!CLASS_TYPE_P (TREE_TYPE (decl))
23301 && !type_dependent_expression_p (decl))
23305 cp_finish_decl (decl, init, !is_non_constant_init,
23307 LOOKUP_ONLYCONVERTING);
23308 if (CLASS_TYPE_P (TREE_TYPE (decl)))
23310 VEC_safe_push (tree, gc, for_block, this_pre_body);
23314 init = pop_stmt_list (this_pre_body);
23315 this_pre_body = NULL_TREE;
23320 cp_lexer_consume_token (parser->lexer);
23321 init = cp_parser_assignment_expression (parser, false, NULL);
23324 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
23325 init = error_mark_node;
23327 cp_finish_decl (decl, NULL_TREE,
23328 /*init_const_expr_p=*/false,
23330 LOOKUP_ONLYCONVERTING);
23334 pop_scope (pushed_scope);
23340 /* If parsing a type specifier sequence failed, then
23341 this MUST be a simple expression. */
23342 cp_parser_parse_tentatively (parser);
23343 decl = cp_parser_primary_expression (parser, false, false,
23345 if (!cp_parser_error_occurred (parser)
23348 && CLASS_TYPE_P (TREE_TYPE (decl)))
23352 cp_parser_parse_definitely (parser);
23353 cp_parser_require (parser, CPP_EQ, RT_EQ);
23354 rhs = cp_parser_assignment_expression (parser, false, NULL);
23355 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
23357 tf_warning_or_error));
23358 add_private_clause = true;
23363 cp_parser_abort_tentative_parse (parser);
23364 init = cp_parser_expression (parser, false, NULL);
23367 if (TREE_CODE (init) == MODIFY_EXPR
23368 || TREE_CODE (init) == MODOP_EXPR)
23369 real_decl = TREE_OPERAND (init, 0);
23374 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23377 this_pre_body = pop_stmt_list (this_pre_body);
23381 pre_body = push_stmt_list ();
23383 add_stmt (this_pre_body);
23384 pre_body = pop_stmt_list (pre_body);
23387 pre_body = this_pre_body;
23392 if (par_clauses != NULL && real_decl != NULL_TREE)
23395 for (c = par_clauses; *c ; )
23396 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
23397 && OMP_CLAUSE_DECL (*c) == real_decl)
23399 error_at (loc, "iteration variable %qD"
23400 " should not be firstprivate", real_decl);
23401 *c = OMP_CLAUSE_CHAIN (*c);
23403 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
23404 && OMP_CLAUSE_DECL (*c) == real_decl)
23406 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23407 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23408 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
23409 OMP_CLAUSE_DECL (l) = real_decl;
23410 OMP_CLAUSE_CHAIN (l) = clauses;
23411 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
23413 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
23414 CP_OMP_CLAUSE_INFO (*c) = NULL;
23415 add_private_clause = false;
23419 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
23420 && OMP_CLAUSE_DECL (*c) == real_decl)
23421 add_private_clause = false;
23422 c = &OMP_CLAUSE_CHAIN (*c);
23426 if (add_private_clause)
23429 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23431 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
23432 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
23433 && OMP_CLAUSE_DECL (c) == decl)
23435 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
23436 && OMP_CLAUSE_DECL (c) == decl)
23437 error_at (loc, "iteration variable %qD "
23438 "should not be firstprivate",
23440 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
23441 && OMP_CLAUSE_DECL (c) == decl)
23442 error_at (loc, "iteration variable %qD should not be reduction",
23447 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
23448 OMP_CLAUSE_DECL (c) = decl;
23449 c = finish_omp_clauses (c);
23452 OMP_CLAUSE_CHAIN (c) = clauses;
23459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23460 cond = cp_parser_omp_for_cond (parser, decl);
23461 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23466 /* If decl is an iterator, preserve the operator on decl
23467 until finish_omp_for. */
23469 && (type_dependent_expression_p (decl)
23470 || CLASS_TYPE_P (TREE_TYPE (decl))))
23471 incr = cp_parser_omp_for_incr (parser, decl);
23473 incr = cp_parser_expression (parser, false, NULL);
23476 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23478 /*or_comma=*/false,
23479 /*consume_paren=*/true);
23481 TREE_VEC_ELT (declv, i) = decl;
23482 TREE_VEC_ELT (initv, i) = init;
23483 TREE_VEC_ELT (condv, i) = cond;
23484 TREE_VEC_ELT (incrv, i) = incr;
23486 if (i == collapse - 1)
23489 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23490 in between the collapsed for loops to be still considered perfectly
23491 nested. Hopefully the final version clarifies this.
23492 For now handle (multiple) {'s and empty statements. */
23493 cp_parser_parse_tentatively (parser);
23496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23498 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23500 cp_lexer_consume_token (parser->lexer);
23503 else if (bracecount
23504 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23505 cp_lexer_consume_token (parser->lexer);
23508 loc = cp_lexer_peek_token (parser->lexer)->location;
23509 error_at (loc, "not enough collapsed for loops");
23510 collapse_err = true;
23511 cp_parser_abort_tentative_parse (parser);
23520 cp_parser_parse_definitely (parser);
23521 nbraces += bracecount;
23525 /* Note that we saved the original contents of this flag when we entered
23526 the structured block, and so we don't need to re-save it here. */
23527 parser->in_statement = IN_OMP_FOR;
23529 /* Note that the grammar doesn't call for a structured block here,
23530 though the loop as a whole is a structured block. */
23531 body = push_stmt_list ();
23532 cp_parser_statement (parser, NULL_TREE, false, NULL);
23533 body = pop_stmt_list (body);
23535 if (declv == NULL_TREE)
23538 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
23539 pre_body, clauses);
23543 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23545 cp_lexer_consume_token (parser->lexer);
23548 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23549 cp_lexer_consume_token (parser->lexer);
23554 error_at (cp_lexer_peek_token (parser->lexer)->location,
23555 "collapsed loops not perfectly nested");
23557 collapse_err = true;
23558 cp_parser_statement_seq_opt (parser, NULL);
23559 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23564 while (!VEC_empty (tree, for_block))
23565 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
23566 release_tree_vector (for_block);
23572 #pragma omp for for-clause[optseq] new-line
23575 #define OMP_FOR_CLAUSE_MASK \
23576 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23577 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23578 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23579 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23580 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23581 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23582 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23583 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23586 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
23588 tree clauses, sb, ret;
23591 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
23592 "#pragma omp for", pragma_tok);
23594 sb = begin_omp_structured_block ();
23595 save = cp_parser_begin_omp_structured_block (parser);
23597 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
23599 cp_parser_end_omp_structured_block (parser, save);
23600 add_stmt (finish_omp_structured_block (sb));
23606 # pragma omp master new-line
23607 structured-block */
23610 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
23612 cp_parser_require_pragma_eol (parser, pragma_tok);
23613 return c_finish_omp_master (input_location,
23614 cp_parser_omp_structured_block (parser));
23618 # pragma omp ordered new-line
23619 structured-block */
23622 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
23624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23625 cp_parser_require_pragma_eol (parser, pragma_tok);
23626 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
23632 { section-sequence }
23635 section-directive[opt] structured-block
23636 section-sequence section-directive structured-block */
23639 cp_parser_omp_sections_scope (cp_parser *parser)
23641 tree stmt, substmt;
23642 bool error_suppress = false;
23645 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23648 stmt = push_stmt_list ();
23650 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
23654 substmt = begin_omp_structured_block ();
23655 save = cp_parser_begin_omp_structured_block (parser);
23659 cp_parser_statement (parser, NULL_TREE, false, NULL);
23661 tok = cp_lexer_peek_token (parser->lexer);
23662 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23664 if (tok->type == CPP_CLOSE_BRACE)
23666 if (tok->type == CPP_EOF)
23670 cp_parser_end_omp_structured_block (parser, save);
23671 substmt = finish_omp_structured_block (substmt);
23672 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23673 add_stmt (substmt);
23678 tok = cp_lexer_peek_token (parser->lexer);
23679 if (tok->type == CPP_CLOSE_BRACE)
23681 if (tok->type == CPP_EOF)
23684 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
23686 cp_lexer_consume_token (parser->lexer);
23687 cp_parser_require_pragma_eol (parser, tok);
23688 error_suppress = false;
23690 else if (!error_suppress)
23692 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
23693 error_suppress = true;
23696 substmt = cp_parser_omp_structured_block (parser);
23697 substmt = build1 (OMP_SECTION, void_type_node, substmt);
23698 add_stmt (substmt);
23700 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23702 substmt = pop_stmt_list (stmt);
23704 stmt = make_node (OMP_SECTIONS);
23705 TREE_TYPE (stmt) = void_type_node;
23706 OMP_SECTIONS_BODY (stmt) = substmt;
23713 # pragma omp sections sections-clause[optseq] newline
23716 #define OMP_SECTIONS_CLAUSE_MASK \
23717 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23718 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23719 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23720 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23721 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23724 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
23728 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
23729 "#pragma omp sections", pragma_tok);
23731 ret = cp_parser_omp_sections_scope (parser);
23733 OMP_SECTIONS_CLAUSES (ret) = clauses;
23739 # pragma parallel parallel-clause new-line
23740 # pragma parallel for parallel-for-clause new-line
23741 # pragma parallel sections parallel-sections-clause new-line */
23743 #define OMP_PARALLEL_CLAUSE_MASK \
23744 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23745 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23746 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23747 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23748 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23749 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23750 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23751 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23754 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
23756 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
23757 const char *p_name = "#pragma omp parallel";
23758 tree stmt, clauses, par_clause, ws_clause, block;
23759 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
23761 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23765 cp_lexer_consume_token (parser->lexer);
23766 p_kind = PRAGMA_OMP_PARALLEL_FOR;
23767 p_name = "#pragma omp parallel for";
23768 mask |= OMP_FOR_CLAUSE_MASK;
23769 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23771 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23774 const char *p = IDENTIFIER_POINTER (id);
23775 if (strcmp (p, "sections") == 0)
23777 cp_lexer_consume_token (parser->lexer);
23778 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
23779 p_name = "#pragma omp parallel sections";
23780 mask |= OMP_SECTIONS_CLAUSE_MASK;
23781 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
23785 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
23786 block = begin_omp_parallel ();
23787 save = cp_parser_begin_omp_structured_block (parser);
23791 case PRAGMA_OMP_PARALLEL:
23792 cp_parser_statement (parser, NULL_TREE, false, NULL);
23793 par_clause = clauses;
23796 case PRAGMA_OMP_PARALLEL_FOR:
23797 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23798 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
23801 case PRAGMA_OMP_PARALLEL_SECTIONS:
23802 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
23803 stmt = cp_parser_omp_sections_scope (parser);
23805 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
23809 gcc_unreachable ();
23812 cp_parser_end_omp_structured_block (parser, save);
23813 stmt = finish_omp_parallel (par_clause, block);
23814 if (p_kind != PRAGMA_OMP_PARALLEL)
23815 OMP_PARALLEL_COMBINED (stmt) = 1;
23820 # pragma omp single single-clause[optseq] new-line
23821 structured-block */
23823 #define OMP_SINGLE_CLAUSE_MASK \
23824 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23825 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23826 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
23827 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23830 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
23832 tree stmt = make_node (OMP_SINGLE);
23833 TREE_TYPE (stmt) = void_type_node;
23835 OMP_SINGLE_CLAUSES (stmt)
23836 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
23837 "#pragma omp single", pragma_tok);
23838 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
23840 return add_stmt (stmt);
23844 # pragma omp task task-clause[optseq] new-line
23845 structured-block */
23847 #define OMP_TASK_CLAUSE_MASK \
23848 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23849 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
23850 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23851 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23852 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23853 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
23856 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
23858 tree clauses, block;
23861 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
23862 "#pragma omp task", pragma_tok);
23863 block = begin_omp_task ();
23864 save = cp_parser_begin_omp_structured_block (parser);
23865 cp_parser_statement (parser, NULL_TREE, false, NULL);
23866 cp_parser_end_omp_structured_block (parser, save);
23867 return finish_omp_task (clauses, block);
23871 # pragma omp taskwait new-line */
23874 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
23876 cp_parser_require_pragma_eol (parser, pragma_tok);
23877 finish_omp_taskwait ();
23881 # pragma omp threadprivate (variable-list) */
23884 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
23888 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23889 cp_parser_require_pragma_eol (parser, pragma_tok);
23891 finish_omp_threadprivate (vars);
23894 /* Main entry point to OpenMP statement pragmas. */
23897 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
23901 switch (pragma_tok->pragma_kind)
23903 case PRAGMA_OMP_ATOMIC:
23904 cp_parser_omp_atomic (parser, pragma_tok);
23906 case PRAGMA_OMP_CRITICAL:
23907 stmt = cp_parser_omp_critical (parser, pragma_tok);
23909 case PRAGMA_OMP_FOR:
23910 stmt = cp_parser_omp_for (parser, pragma_tok);
23912 case PRAGMA_OMP_MASTER:
23913 stmt = cp_parser_omp_master (parser, pragma_tok);
23915 case PRAGMA_OMP_ORDERED:
23916 stmt = cp_parser_omp_ordered (parser, pragma_tok);
23918 case PRAGMA_OMP_PARALLEL:
23919 stmt = cp_parser_omp_parallel (parser, pragma_tok);
23921 case PRAGMA_OMP_SECTIONS:
23922 stmt = cp_parser_omp_sections (parser, pragma_tok);
23924 case PRAGMA_OMP_SINGLE:
23925 stmt = cp_parser_omp_single (parser, pragma_tok);
23927 case PRAGMA_OMP_TASK:
23928 stmt = cp_parser_omp_task (parser, pragma_tok);
23931 gcc_unreachable ();
23935 SET_EXPR_LOCATION (stmt, pragma_tok->location);
23940 static GTY (()) cp_parser *the_parser;
23943 /* Special handling for the first token or line in the file. The first
23944 thing in the file might be #pragma GCC pch_preprocess, which loads a
23945 PCH file, which is a GC collection point. So we need to handle this
23946 first pragma without benefit of an existing lexer structure.
23948 Always returns one token to the caller in *FIRST_TOKEN. This is
23949 either the true first token of the file, or the first token after
23950 the initial pragma. */
23953 cp_parser_initial_pragma (cp_token *first_token)
23957 cp_lexer_get_preprocessor_token (NULL, first_token);
23958 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
23961 cp_lexer_get_preprocessor_token (NULL, first_token);
23962 if (first_token->type == CPP_STRING)
23964 name = first_token->u.value;
23966 cp_lexer_get_preprocessor_token (NULL, first_token);
23967 if (first_token->type != CPP_PRAGMA_EOL)
23968 error_at (first_token->location,
23969 "junk at end of %<#pragma GCC pch_preprocess%>");
23972 error_at (first_token->location, "expected string literal");
23974 /* Skip to the end of the pragma. */
23975 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
23976 cp_lexer_get_preprocessor_token (NULL, first_token);
23978 /* Now actually load the PCH file. */
23980 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
23982 /* Read one more token to return to our caller. We have to do this
23983 after reading the PCH file in, since its pointers have to be
23985 cp_lexer_get_preprocessor_token (NULL, first_token);
23988 /* Normal parsing of a pragma token. Here we can (and must) use the
23992 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
23994 cp_token *pragma_tok;
23997 pragma_tok = cp_lexer_consume_token (parser->lexer);
23998 gcc_assert (pragma_tok->type == CPP_PRAGMA);
23999 parser->lexer->in_pragma = true;
24001 id = pragma_tok->pragma_kind;
24004 case PRAGMA_GCC_PCH_PREPROCESS:
24005 error_at (pragma_tok->location,
24006 "%<#pragma GCC pch_preprocess%> must be first");
24009 case PRAGMA_OMP_BARRIER:
24012 case pragma_compound:
24013 cp_parser_omp_barrier (parser, pragma_tok);
24016 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24017 "used in compound statements");
24024 case PRAGMA_OMP_FLUSH:
24027 case pragma_compound:
24028 cp_parser_omp_flush (parser, pragma_tok);
24031 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24032 "used in compound statements");
24039 case PRAGMA_OMP_TASKWAIT:
24042 case pragma_compound:
24043 cp_parser_omp_taskwait (parser, pragma_tok);
24046 error_at (pragma_tok->location,
24047 "%<#pragma omp taskwait%> may only be "
24048 "used in compound statements");
24055 case PRAGMA_OMP_THREADPRIVATE:
24056 cp_parser_omp_threadprivate (parser, pragma_tok);
24059 case PRAGMA_OMP_ATOMIC:
24060 case PRAGMA_OMP_CRITICAL:
24061 case PRAGMA_OMP_FOR:
24062 case PRAGMA_OMP_MASTER:
24063 case PRAGMA_OMP_ORDERED:
24064 case PRAGMA_OMP_PARALLEL:
24065 case PRAGMA_OMP_SECTIONS:
24066 case PRAGMA_OMP_SINGLE:
24067 case PRAGMA_OMP_TASK:
24068 if (context == pragma_external)
24070 cp_parser_omp_construct (parser, pragma_tok);
24073 case PRAGMA_OMP_SECTION:
24074 error_at (pragma_tok->location,
24075 "%<#pragma omp section%> may only be used in "
24076 "%<#pragma omp sections%> construct");
24080 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24081 c_invoke_pragma_handler (id);
24085 cp_parser_error (parser, "expected declaration specifiers");
24089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24093 /* The interface the pragma parsers have to the lexer. */
24096 pragma_lex (tree *value)
24099 enum cpp_ttype ret;
24101 tok = cp_lexer_peek_token (the_parser->lexer);
24104 *value = tok->u.value;
24106 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24108 else if (ret == CPP_STRING)
24109 *value = cp_parser_string_literal (the_parser, false, false);
24112 cp_lexer_consume_token (the_parser->lexer);
24113 if (ret == CPP_KEYWORD)
24121 /* External interface. */
24123 /* Parse one entire translation unit. */
24126 c_parse_file (void)
24128 static bool already_called = false;
24130 if (already_called)
24132 sorry ("inter-module optimizations not implemented for C++");
24135 already_called = true;
24137 the_parser = cp_parser_new ();
24138 push_deferring_access_checks (flag_access_control
24139 ? dk_no_deferred : dk_no_check);
24140 cp_parser_translation_unit (the_parser);
24144 #include "gt-cp-parser.h"