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,
1338 /* When parsing a decl-specifier-seq, only allow type-specifier or
1340 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1343 /* This type is used for parameters and variables which hold
1344 combinations of the above flags. */
1345 typedef int cp_parser_flags;
1347 /* The different kinds of declarators we want to parse. */
1349 typedef enum cp_parser_declarator_kind
1351 /* We want an abstract declarator. */
1352 CP_PARSER_DECLARATOR_ABSTRACT,
1353 /* We want a named declarator. */
1354 CP_PARSER_DECLARATOR_NAMED,
1355 /* We don't mind, but the name must be an unqualified-id. */
1356 CP_PARSER_DECLARATOR_EITHER
1357 } cp_parser_declarator_kind;
1359 /* The precedence values used to parse binary expressions. The minimum value
1360 of PREC must be 1, because zero is reserved to quickly discriminate
1361 binary operators from other tokens. */
1366 PREC_LOGICAL_OR_EXPRESSION,
1367 PREC_LOGICAL_AND_EXPRESSION,
1368 PREC_INCLUSIVE_OR_EXPRESSION,
1369 PREC_EXCLUSIVE_OR_EXPRESSION,
1370 PREC_AND_EXPRESSION,
1371 PREC_EQUALITY_EXPRESSION,
1372 PREC_RELATIONAL_EXPRESSION,
1373 PREC_SHIFT_EXPRESSION,
1374 PREC_ADDITIVE_EXPRESSION,
1375 PREC_MULTIPLICATIVE_EXPRESSION,
1377 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1380 /* A mapping from a token type to a corresponding tree node type, with a
1381 precedence value. */
1383 typedef struct cp_parser_binary_operations_map_node
1385 /* The token type. */
1386 enum cpp_ttype token_type;
1387 /* The corresponding tree code. */
1388 enum tree_code tree_type;
1389 /* The precedence of this operator. */
1390 enum cp_parser_prec prec;
1391 } cp_parser_binary_operations_map_node;
1393 /* The status of a tentative parse. */
1395 typedef enum cp_parser_status_kind
1397 /* No errors have occurred. */
1398 CP_PARSER_STATUS_KIND_NO_ERROR,
1399 /* An error has occurred. */
1400 CP_PARSER_STATUS_KIND_ERROR,
1401 /* We are committed to this tentative parse, whether or not an error
1403 CP_PARSER_STATUS_KIND_COMMITTED
1404 } cp_parser_status_kind;
1406 typedef struct cp_parser_expression_stack_entry
1408 /* Left hand side of the binary operation we are currently
1411 /* Original tree code for left hand side, if it was a binary
1412 expression itself (used for -Wparentheses). */
1413 enum tree_code lhs_type;
1414 /* Tree code for the binary operation we are parsing. */
1415 enum tree_code tree_type;
1416 /* Precedence of the binary operation we are parsing. */
1417 enum cp_parser_prec prec;
1418 } cp_parser_expression_stack_entry;
1420 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1421 entries because precedence levels on the stack are monotonically
1423 typedef struct cp_parser_expression_stack_entry
1424 cp_parser_expression_stack[NUM_PREC_VALUES];
1426 /* Context that is saved and restored when parsing tentatively. */
1427 typedef struct GTY (()) cp_parser_context {
1428 /* If this is a tentative parsing context, the status of the
1430 enum cp_parser_status_kind status;
1431 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1432 that are looked up in this context must be looked up both in the
1433 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1434 the context of the containing expression. */
1437 /* The next parsing context in the stack. */
1438 struct cp_parser_context *next;
1439 } cp_parser_context;
1443 /* Constructors and destructors. */
1445 static cp_parser_context *cp_parser_context_new
1446 (cp_parser_context *);
1448 /* Class variables. */
1450 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1452 /* The operator-precedence table used by cp_parser_binary_expression.
1453 Transformed into an associative array (binops_by_token) by
1456 static const cp_parser_binary_operations_map_node binops[] = {
1457 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1458 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1460 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1461 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1462 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1464 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1465 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1467 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1468 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1470 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1471 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1472 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1473 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1475 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1476 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1478 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1480 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1482 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1484 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1486 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1489 /* The same as binops, but initialized by cp_parser_new so that
1490 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1492 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1494 /* Constructors and destructors. */
1496 /* Construct a new context. The context below this one on the stack
1497 is given by NEXT. */
1499 static cp_parser_context *
1500 cp_parser_context_new (cp_parser_context* next)
1502 cp_parser_context *context;
1504 /* Allocate the storage. */
1505 if (cp_parser_context_free_list != NULL)
1507 /* Pull the first entry from the free list. */
1508 context = cp_parser_context_free_list;
1509 cp_parser_context_free_list = context->next;
1510 memset (context, 0, sizeof (*context));
1513 context = ggc_alloc_cleared_cp_parser_context ();
1515 /* No errors have occurred yet in this context. */
1516 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1517 /* If this is not the bottommost context, copy information that we
1518 need from the previous context. */
1521 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1522 expression, then we are parsing one in this context, too. */
1523 context->object_type = next->object_type;
1524 /* Thread the stack. */
1525 context->next = next;
1531 /* An entry in a queue of function arguments that require post-processing. */
1533 typedef struct GTY(()) cp_default_arg_entry_d {
1534 /* The current_class_type when we parsed this arg. */
1537 /* The function decl itself. */
1539 } cp_default_arg_entry;
1541 DEF_VEC_O(cp_default_arg_entry);
1542 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1544 /* An entry in a stack for member functions of local classes. */
1546 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1547 /* Functions with default arguments that require post-processing.
1548 Functions appear in this list in declaration order. */
1549 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1551 /* Functions with defintions that require post-processing. Functions
1552 appear in this list in declaration order. */
1553 VEC(tree,gc) *funs_with_definitions;
1554 } cp_unparsed_functions_entry;
1556 DEF_VEC_O(cp_unparsed_functions_entry);
1557 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1559 /* The cp_parser structure represents the C++ parser. */
1561 typedef struct GTY(()) cp_parser {
1562 /* The lexer from which we are obtaining tokens. */
1565 /* The scope in which names should be looked up. If NULL_TREE, then
1566 we look up names in the scope that is currently open in the
1567 source program. If non-NULL, this is either a TYPE or
1568 NAMESPACE_DECL for the scope in which we should look. It can
1569 also be ERROR_MARK, when we've parsed a bogus scope.
1571 This value is not cleared automatically after a name is looked
1572 up, so we must be careful to clear it before starting a new look
1573 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1574 will look up `Z' in the scope of `X', rather than the current
1575 scope.) Unfortunately, it is difficult to tell when name lookup
1576 is complete, because we sometimes peek at a token, look it up,
1577 and then decide not to consume it. */
1580 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1581 last lookup took place. OBJECT_SCOPE is used if an expression
1582 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1583 respectively. QUALIFYING_SCOPE is used for an expression of the
1584 form "X::Y"; it refers to X. */
1586 tree qualifying_scope;
1588 /* A stack of parsing contexts. All but the bottom entry on the
1589 stack will be tentative contexts.
1591 We parse tentatively in order to determine which construct is in
1592 use in some situations. For example, in order to determine
1593 whether a statement is an expression-statement or a
1594 declaration-statement we parse it tentatively as a
1595 declaration-statement. If that fails, we then reparse the same
1596 token stream as an expression-statement. */
1597 cp_parser_context *context;
1599 /* True if we are parsing GNU C++. If this flag is not set, then
1600 GNU extensions are not recognized. */
1601 bool allow_gnu_extensions_p;
1603 /* TRUE if the `>' token should be interpreted as the greater-than
1604 operator. FALSE if it is the end of a template-id or
1605 template-parameter-list. In C++0x mode, this flag also applies to
1606 `>>' tokens, which are viewed as two consecutive `>' tokens when
1607 this flag is FALSE. */
1608 bool greater_than_is_operator_p;
1610 /* TRUE if default arguments are allowed within a parameter list
1611 that starts at this point. FALSE if only a gnu extension makes
1612 them permissible. */
1613 bool default_arg_ok_p;
1615 /* TRUE if we are parsing an integral constant-expression. See
1616 [expr.const] for a precise definition. */
1617 bool integral_constant_expression_p;
1619 /* TRUE if we are parsing an integral constant-expression -- but a
1620 non-constant expression should be permitted as well. This flag
1621 is used when parsing an array bound so that GNU variable-length
1622 arrays are tolerated. */
1623 bool allow_non_integral_constant_expression_p;
1625 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1626 been seen that makes the expression non-constant. */
1627 bool non_integral_constant_expression_p;
1629 /* TRUE if local variable names and `this' are forbidden in the
1631 bool local_variables_forbidden_p;
1633 /* TRUE if the declaration we are parsing is part of a
1634 linkage-specification of the form `extern string-literal
1636 bool in_unbraced_linkage_specification_p;
1638 /* TRUE if we are presently parsing a declarator, after the
1639 direct-declarator. */
1640 bool in_declarator_p;
1642 /* TRUE if we are presently parsing a template-argument-list. */
1643 bool in_template_argument_list_p;
1645 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1646 to IN_OMP_BLOCK if parsing OpenMP structured block and
1647 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1648 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1649 iteration-statement, OpenMP block or loop within that switch. */
1650 #define IN_SWITCH_STMT 1
1651 #define IN_ITERATION_STMT 2
1652 #define IN_OMP_BLOCK 4
1653 #define IN_OMP_FOR 8
1654 #define IN_IF_STMT 16
1655 unsigned char in_statement;
1657 /* TRUE if we are presently parsing the body of a switch statement.
1658 Note that this doesn't quite overlap with in_statement above.
1659 The difference relates to giving the right sets of error messages:
1660 "case not in switch" vs "break statement used with OpenMP...". */
1661 bool in_switch_statement_p;
1663 /* TRUE if we are parsing a type-id in an expression context. In
1664 such a situation, both "type (expr)" and "type (type)" are valid
1666 bool in_type_id_in_expr_p;
1668 /* TRUE if we are currently in a header file where declarations are
1669 implicitly extern "C". */
1670 bool implicit_extern_c;
1672 /* TRUE if strings in expressions should be translated to the execution
1674 bool translate_strings_p;
1676 /* TRUE if we are presently parsing the body of a function, but not
1678 bool in_function_body;
1680 /* If non-NULL, then we are parsing a construct where new type
1681 definitions are not permitted. The string stored here will be
1682 issued as an error message if a type is defined. */
1683 const char *type_definition_forbidden_message;
1685 /* A stack used for member functions of local classes. The lists
1686 contained in an individual entry can only be processed once the
1687 outermost class being defined is complete. */
1688 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1690 /* The number of classes whose definitions are currently in
1692 unsigned num_classes_being_defined;
1694 /* The number of template parameter lists that apply directly to the
1695 current declaration. */
1696 unsigned num_template_parameter_lists;
1699 /* Managing the unparsed function queues. */
1701 #define unparsed_funs_with_default_args \
1702 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1703 #define unparsed_funs_with_definitions \
1704 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1707 push_unparsed_function_queues (cp_parser *parser)
1709 VEC_safe_push (cp_unparsed_functions_entry, gc,
1710 parser->unparsed_queues, NULL);
1711 unparsed_funs_with_default_args = NULL;
1712 unparsed_funs_with_definitions = make_tree_vector ();
1716 pop_unparsed_function_queues (cp_parser *parser)
1718 release_tree_vector (unparsed_funs_with_definitions);
1719 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1724 /* Constructors and destructors. */
1726 static cp_parser *cp_parser_new
1729 /* Routines to parse various constructs.
1731 Those that return `tree' will return the error_mark_node (rather
1732 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1733 Sometimes, they will return an ordinary node if error-recovery was
1734 attempted, even though a parse error occurred. So, to check
1735 whether or not a parse error occurred, you should always use
1736 cp_parser_error_occurred. If the construct is optional (indicated
1737 either by an `_opt' in the name of the function that does the
1738 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1739 the construct is not present. */
1741 /* Lexical conventions [gram.lex] */
1743 static tree cp_parser_identifier
1745 static tree cp_parser_string_literal
1746 (cp_parser *, bool, bool);
1748 /* Basic concepts [gram.basic] */
1750 static bool cp_parser_translation_unit
1753 /* Expressions [gram.expr] */
1755 static tree cp_parser_primary_expression
1756 (cp_parser *, bool, bool, bool, cp_id_kind *);
1757 static tree cp_parser_id_expression
1758 (cp_parser *, bool, bool, bool *, bool, bool);
1759 static tree cp_parser_unqualified_id
1760 (cp_parser *, bool, bool, bool, bool);
1761 static tree cp_parser_nested_name_specifier_opt
1762 (cp_parser *, bool, bool, bool, bool);
1763 static tree cp_parser_nested_name_specifier
1764 (cp_parser *, bool, bool, bool, bool);
1765 static tree cp_parser_qualifying_entity
1766 (cp_parser *, bool, bool, bool, bool, bool);
1767 static tree cp_parser_postfix_expression
1768 (cp_parser *, bool, bool, bool, cp_id_kind *);
1769 static tree cp_parser_postfix_open_square_expression
1770 (cp_parser *, tree, bool);
1771 static tree cp_parser_postfix_dot_deref_expression
1772 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1773 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1774 (cp_parser *, int, bool, bool, bool *);
1775 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1776 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1777 static void cp_parser_pseudo_destructor_name
1778 (cp_parser *, tree *, tree *);
1779 static tree cp_parser_unary_expression
1780 (cp_parser *, bool, bool, cp_id_kind *);
1781 static enum tree_code cp_parser_unary_operator
1783 static tree cp_parser_new_expression
1785 static VEC(tree,gc) *cp_parser_new_placement
1787 static tree cp_parser_new_type_id
1788 (cp_parser *, tree *);
1789 static cp_declarator *cp_parser_new_declarator_opt
1791 static cp_declarator *cp_parser_direct_new_declarator
1793 static VEC(tree,gc) *cp_parser_new_initializer
1795 static tree cp_parser_delete_expression
1797 static tree cp_parser_cast_expression
1798 (cp_parser *, bool, bool, cp_id_kind *);
1799 static tree cp_parser_binary_expression
1800 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1801 static tree cp_parser_question_colon_clause
1802 (cp_parser *, tree);
1803 static tree cp_parser_assignment_expression
1804 (cp_parser *, bool, cp_id_kind *);
1805 static enum tree_code cp_parser_assignment_operator_opt
1807 static tree cp_parser_expression
1808 (cp_parser *, bool, cp_id_kind *);
1809 static tree cp_parser_constant_expression
1810 (cp_parser *, bool, bool *);
1811 static tree cp_parser_builtin_offsetof
1813 static tree cp_parser_lambda_expression
1815 static void cp_parser_lambda_introducer
1816 (cp_parser *, tree);
1817 static void cp_parser_lambda_declarator_opt
1818 (cp_parser *, tree);
1819 static void cp_parser_lambda_body
1820 (cp_parser *, tree);
1822 /* Statements [gram.stmt.stmt] */
1824 static void cp_parser_statement
1825 (cp_parser *, tree, bool, bool *);
1826 static void cp_parser_label_for_labeled_statement
1828 static tree cp_parser_expression_statement
1829 (cp_parser *, tree);
1830 static tree cp_parser_compound_statement
1831 (cp_parser *, tree, bool);
1832 static void cp_parser_statement_seq_opt
1833 (cp_parser *, tree);
1834 static tree cp_parser_selection_statement
1835 (cp_parser *, bool *);
1836 static tree cp_parser_condition
1838 static tree cp_parser_iteration_statement
1840 static void cp_parser_for_init_statement
1842 static tree cp_parser_c_for
1844 static tree cp_parser_range_for
1846 static tree cp_parser_jump_statement
1848 static void cp_parser_declaration_statement
1851 static tree cp_parser_implicitly_scoped_statement
1852 (cp_parser *, bool *);
1853 static void cp_parser_already_scoped_statement
1856 /* Declarations [gram.dcl.dcl] */
1858 static void cp_parser_declaration_seq_opt
1860 static void cp_parser_declaration
1862 static void cp_parser_block_declaration
1863 (cp_parser *, bool);
1864 static void cp_parser_simple_declaration
1865 (cp_parser *, bool);
1866 static void cp_parser_decl_specifier_seq
1867 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1868 static tree cp_parser_storage_class_specifier_opt
1870 static tree cp_parser_function_specifier_opt
1871 (cp_parser *, cp_decl_specifier_seq *);
1872 static tree cp_parser_type_specifier
1873 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1875 static tree cp_parser_simple_type_specifier
1876 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1877 static tree cp_parser_type_name
1879 static tree cp_parser_nonclass_name
1880 (cp_parser* parser);
1881 static tree cp_parser_elaborated_type_specifier
1882 (cp_parser *, bool, bool);
1883 static tree cp_parser_enum_specifier
1885 static void cp_parser_enumerator_list
1886 (cp_parser *, tree);
1887 static void cp_parser_enumerator_definition
1888 (cp_parser *, tree);
1889 static tree cp_parser_namespace_name
1891 static void cp_parser_namespace_definition
1893 static void cp_parser_namespace_body
1895 static tree cp_parser_qualified_namespace_specifier
1897 static void cp_parser_namespace_alias_definition
1899 static bool cp_parser_using_declaration
1900 (cp_parser *, bool);
1901 static void cp_parser_using_directive
1903 static void cp_parser_asm_definition
1905 static void cp_parser_linkage_specification
1907 static void cp_parser_static_assert
1908 (cp_parser *, bool);
1909 static tree cp_parser_decltype
1912 /* Declarators [gram.dcl.decl] */
1914 static tree cp_parser_init_declarator
1915 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1916 static cp_declarator *cp_parser_declarator
1917 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1918 static cp_declarator *cp_parser_direct_declarator
1919 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1920 static enum tree_code cp_parser_ptr_operator
1921 (cp_parser *, tree *, cp_cv_quals *);
1922 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1924 static tree cp_parser_late_return_type_opt
1926 static tree cp_parser_declarator_id
1927 (cp_parser *, bool);
1928 static tree cp_parser_type_id
1930 static tree cp_parser_template_type_arg
1932 static tree cp_parser_trailing_type_id (cp_parser *);
1933 static tree cp_parser_type_id_1
1934 (cp_parser *, bool, bool);
1935 static void cp_parser_type_specifier_seq
1936 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1937 static tree cp_parser_parameter_declaration_clause
1939 static tree cp_parser_parameter_declaration_list
1940 (cp_parser *, bool *);
1941 static cp_parameter_declarator *cp_parser_parameter_declaration
1942 (cp_parser *, bool, bool *);
1943 static tree cp_parser_default_argument
1944 (cp_parser *, bool);
1945 static void cp_parser_function_body
1947 static tree cp_parser_initializer
1948 (cp_parser *, bool *, bool *);
1949 static tree cp_parser_initializer_clause
1950 (cp_parser *, bool *);
1951 static tree cp_parser_braced_list
1952 (cp_parser*, bool*);
1953 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1954 (cp_parser *, bool *);
1956 static bool cp_parser_ctor_initializer_opt_and_function_body
1959 /* Classes [gram.class] */
1961 static tree cp_parser_class_name
1962 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1963 static tree cp_parser_class_specifier
1965 static tree cp_parser_class_head
1966 (cp_parser *, bool *, tree *, tree *);
1967 static enum tag_types cp_parser_class_key
1969 static void cp_parser_member_specification_opt
1971 static void cp_parser_member_declaration
1973 static tree cp_parser_pure_specifier
1975 static tree cp_parser_constant_initializer
1978 /* Derived classes [gram.class.derived] */
1980 static tree cp_parser_base_clause
1982 static tree cp_parser_base_specifier
1985 /* Special member functions [gram.special] */
1987 static tree cp_parser_conversion_function_id
1989 static tree cp_parser_conversion_type_id
1991 static cp_declarator *cp_parser_conversion_declarator_opt
1993 static bool cp_parser_ctor_initializer_opt
1995 static void cp_parser_mem_initializer_list
1997 static tree cp_parser_mem_initializer
1999 static tree cp_parser_mem_initializer_id
2002 /* Overloading [gram.over] */
2004 static tree cp_parser_operator_function_id
2006 static tree cp_parser_operator
2009 /* Templates [gram.temp] */
2011 static void cp_parser_template_declaration
2012 (cp_parser *, bool);
2013 static tree cp_parser_template_parameter_list
2015 static tree cp_parser_template_parameter
2016 (cp_parser *, bool *, bool *);
2017 static tree cp_parser_type_parameter
2018 (cp_parser *, bool *);
2019 static tree cp_parser_template_id
2020 (cp_parser *, bool, bool, bool);
2021 static tree cp_parser_template_name
2022 (cp_parser *, bool, bool, bool, bool *);
2023 static tree cp_parser_template_argument_list
2025 static tree cp_parser_template_argument
2027 static void cp_parser_explicit_instantiation
2029 static void cp_parser_explicit_specialization
2032 /* Exception handling [gram.exception] */
2034 static tree cp_parser_try_block
2036 static bool cp_parser_function_try_block
2038 static void cp_parser_handler_seq
2040 static void cp_parser_handler
2042 static tree cp_parser_exception_declaration
2044 static tree cp_parser_throw_expression
2046 static tree cp_parser_exception_specification_opt
2048 static tree cp_parser_type_id_list
2051 /* GNU Extensions */
2053 static tree cp_parser_asm_specification_opt
2055 static tree cp_parser_asm_operand_list
2057 static tree cp_parser_asm_clobber_list
2059 static tree cp_parser_asm_label_list
2061 static tree cp_parser_attributes_opt
2063 static tree cp_parser_attribute_list
2065 static bool cp_parser_extension_opt
2066 (cp_parser *, int *);
2067 static void cp_parser_label_declaration
2070 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2071 static bool cp_parser_pragma
2072 (cp_parser *, enum pragma_context);
2074 /* Objective-C++ Productions */
2076 static tree cp_parser_objc_message_receiver
2078 static tree cp_parser_objc_message_args
2080 static tree cp_parser_objc_message_expression
2082 static tree cp_parser_objc_encode_expression
2084 static tree cp_parser_objc_defs_expression
2086 static tree cp_parser_objc_protocol_expression
2088 static tree cp_parser_objc_selector_expression
2090 static tree cp_parser_objc_expression
2092 static bool cp_parser_objc_selector_p
2094 static tree cp_parser_objc_selector
2096 static tree cp_parser_objc_protocol_refs_opt
2098 static void cp_parser_objc_declaration
2099 (cp_parser *, tree);
2100 static tree cp_parser_objc_statement
2102 static bool cp_parser_objc_valid_prefix_attributes
2103 (cp_parser *, tree *);
2104 static void cp_parser_objc_at_property_declaration
2106 static void cp_parser_objc_at_synthesize_declaration
2108 static void cp_parser_objc_at_dynamic_declaration
2110 static tree cp_parser_objc_struct_declaration
2113 /* Utility Routines */
2115 static tree cp_parser_lookup_name
2116 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2117 static tree cp_parser_lookup_name_simple
2118 (cp_parser *, tree, location_t);
2119 static tree cp_parser_maybe_treat_template_as_class
2121 static bool cp_parser_check_declarator_template_parameters
2122 (cp_parser *, cp_declarator *, location_t);
2123 static bool cp_parser_check_template_parameters
2124 (cp_parser *, unsigned, location_t, cp_declarator *);
2125 static tree cp_parser_simple_cast_expression
2127 static tree cp_parser_global_scope_opt
2128 (cp_parser *, bool);
2129 static bool cp_parser_constructor_declarator_p
2130 (cp_parser *, bool);
2131 static tree cp_parser_function_definition_from_specifiers_and_declarator
2132 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2133 static tree cp_parser_function_definition_after_declarator
2134 (cp_parser *, bool);
2135 static void cp_parser_template_declaration_after_export
2136 (cp_parser *, bool);
2137 static void cp_parser_perform_template_parameter_access_checks
2138 (VEC (deferred_access_check,gc)*);
2139 static tree cp_parser_single_declaration
2140 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2141 static tree cp_parser_functional_cast
2142 (cp_parser *, tree);
2143 static tree cp_parser_save_member_function_body
2144 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2145 static tree cp_parser_enclosed_template_argument_list
2147 static void cp_parser_save_default_args
2148 (cp_parser *, tree);
2149 static void cp_parser_late_parsing_for_member
2150 (cp_parser *, tree);
2151 static void cp_parser_late_parsing_default_args
2152 (cp_parser *, tree);
2153 static tree cp_parser_sizeof_operand
2154 (cp_parser *, enum rid);
2155 static tree cp_parser_trait_expr
2156 (cp_parser *, enum rid);
2157 static bool cp_parser_declares_only_class_p
2159 static void cp_parser_set_storage_class
2160 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2161 static void cp_parser_set_decl_spec_type
2162 (cp_decl_specifier_seq *, tree, location_t, bool);
2163 static bool cp_parser_friend_p
2164 (const cp_decl_specifier_seq *);
2165 static void cp_parser_required_error
2166 (cp_parser *, required_token, bool);
2167 static cp_token *cp_parser_require
2168 (cp_parser *, enum cpp_ttype, required_token);
2169 static cp_token *cp_parser_require_keyword
2170 (cp_parser *, enum rid, required_token);
2171 static bool cp_parser_token_starts_function_definition_p
2173 static bool cp_parser_next_token_starts_class_definition_p
2175 static bool cp_parser_next_token_ends_template_argument_p
2177 static bool cp_parser_nth_token_starts_template_argument_list_p
2178 (cp_parser *, size_t);
2179 static enum tag_types cp_parser_token_is_class_key
2181 static void cp_parser_check_class_key
2182 (enum tag_types, tree type);
2183 static void cp_parser_check_access_in_redeclaration
2184 (tree type, location_t location);
2185 static bool cp_parser_optional_template_keyword
2187 static void cp_parser_pre_parsed_nested_name_specifier
2189 static bool cp_parser_cache_group
2190 (cp_parser *, enum cpp_ttype, unsigned);
2191 static void cp_parser_parse_tentatively
2193 static void cp_parser_commit_to_tentative_parse
2195 static void cp_parser_abort_tentative_parse
2197 static bool cp_parser_parse_definitely
2199 static inline bool cp_parser_parsing_tentatively
2201 static bool cp_parser_uncommitted_to_tentative_parse_p
2203 static void cp_parser_error
2204 (cp_parser *, const char *);
2205 static void cp_parser_name_lookup_error
2206 (cp_parser *, tree, tree, name_lookup_error, location_t);
2207 static bool cp_parser_simulate_error
2209 static bool cp_parser_check_type_definition
2211 static void cp_parser_check_for_definition_in_return_type
2212 (cp_declarator *, tree, location_t type_location);
2213 static void cp_parser_check_for_invalid_template_id
2214 (cp_parser *, tree, location_t location);
2215 static bool cp_parser_non_integral_constant_expression
2216 (cp_parser *, non_integral_constant);
2217 static void cp_parser_diagnose_invalid_type_name
2218 (cp_parser *, tree, tree, location_t);
2219 static bool cp_parser_parse_and_diagnose_invalid_type_name
2221 static int cp_parser_skip_to_closing_parenthesis
2222 (cp_parser *, bool, bool, bool);
2223 static void cp_parser_skip_to_end_of_statement
2225 static void cp_parser_consume_semicolon_at_end_of_statement
2227 static void cp_parser_skip_to_end_of_block_or_statement
2229 static bool cp_parser_skip_to_closing_brace
2231 static void cp_parser_skip_to_end_of_template_parameter_list
2233 static void cp_parser_skip_to_pragma_eol
2234 (cp_parser*, cp_token *);
2235 static bool cp_parser_error_occurred
2237 static bool cp_parser_allow_gnu_extensions_p
2239 static bool cp_parser_is_string_literal
2241 static bool cp_parser_is_keyword
2242 (cp_token *, enum rid);
2243 static tree cp_parser_make_typename_type
2244 (cp_parser *, tree, tree, location_t location);
2245 static cp_declarator * cp_parser_make_indirect_declarator
2246 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2248 /* Returns nonzero if we are parsing tentatively. */
2251 cp_parser_parsing_tentatively (cp_parser* parser)
2253 return parser->context->next != NULL;
2256 /* Returns nonzero if TOKEN is a string literal. */
2259 cp_parser_is_string_literal (cp_token* token)
2261 return (token->type == CPP_STRING ||
2262 token->type == CPP_STRING16 ||
2263 token->type == CPP_STRING32 ||
2264 token->type == CPP_WSTRING ||
2265 token->type == CPP_UTF8STRING);
2268 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2271 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2273 return token->keyword == keyword;
2276 /* If not parsing tentatively, issue a diagnostic of the form
2277 FILE:LINE: MESSAGE before TOKEN
2278 where TOKEN is the next token in the input stream. MESSAGE
2279 (specified by the caller) is usually of the form "expected
2283 cp_parser_error (cp_parser* parser, const char* gmsgid)
2285 if (!cp_parser_simulate_error (parser))
2287 cp_token *token = cp_lexer_peek_token (parser->lexer);
2288 /* This diagnostic makes more sense if it is tagged to the line
2289 of the token we just peeked at. */
2290 cp_lexer_set_source_position_from_token (token);
2292 if (token->type == CPP_PRAGMA)
2294 error_at (token->location,
2295 "%<#pragma%> is not allowed here");
2296 cp_parser_skip_to_pragma_eol (parser, token);
2300 c_parse_error (gmsgid,
2301 /* Because c_parser_error does not understand
2302 CPP_KEYWORD, keywords are treated like
2304 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2305 token->u.value, token->flags);
2309 /* Issue an error about name-lookup failing. NAME is the
2310 IDENTIFIER_NODE DECL is the result of
2311 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2312 the thing that we hoped to find. */
2315 cp_parser_name_lookup_error (cp_parser* parser,
2318 name_lookup_error desired,
2319 location_t location)
2321 /* If name lookup completely failed, tell the user that NAME was not
2323 if (decl == error_mark_node)
2325 if (parser->scope && parser->scope != global_namespace)
2326 error_at (location, "%<%E::%E%> has not been declared",
2327 parser->scope, name);
2328 else if (parser->scope == global_namespace)
2329 error_at (location, "%<::%E%> has not been declared", name);
2330 else if (parser->object_scope
2331 && !CLASS_TYPE_P (parser->object_scope))
2332 error_at (location, "request for member %qE in non-class type %qT",
2333 name, parser->object_scope);
2334 else if (parser->object_scope)
2335 error_at (location, "%<%T::%E%> has not been declared",
2336 parser->object_scope, name);
2338 error_at (location, "%qE has not been declared", name);
2340 else if (parser->scope && parser->scope != global_namespace)
2345 error_at (location, "%<%E::%E%> is not a type",
2346 parser->scope, name);
2349 error_at (location, "%<%E::%E%> is not a class or namespace",
2350 parser->scope, name);
2354 "%<%E::%E%> is not a class, namespace, or enumeration",
2355 parser->scope, name);
2362 else if (parser->scope == global_namespace)
2367 error_at (location, "%<::%E%> is not a type", name);
2370 error_at (location, "%<::%E%> is not a class or namespace", name);
2374 "%<::%E%> is not a class, namespace, or enumeration",
2386 error_at (location, "%qE is not a type", name);
2389 error_at (location, "%qE is not a class or namespace", name);
2393 "%qE is not a class, namespace, or enumeration", name);
2401 /* If we are parsing tentatively, remember that an error has occurred
2402 during this tentative parse. Returns true if the error was
2403 simulated; false if a message should be issued by the caller. */
2406 cp_parser_simulate_error (cp_parser* parser)
2408 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2410 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2416 /* Check for repeated decl-specifiers. */
2419 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2420 location_t location)
2424 for (ds = ds_first; ds != ds_last; ++ds)
2426 unsigned count = decl_specs->specs[ds];
2429 /* The "long" specifier is a special case because of "long long". */
2433 error_at (location, "%<long long long%> is too long for GCC");
2435 pedwarn_cxx98 (location, OPT_Wlong_long,
2436 "ISO C++ 1998 does not support %<long long%>");
2440 static const char *const decl_spec_names[] = {
2457 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2462 /* This function is called when a type is defined. If type
2463 definitions are forbidden at this point, an error message is
2467 cp_parser_check_type_definition (cp_parser* parser)
2469 /* If types are forbidden here, issue a message. */
2470 if (parser->type_definition_forbidden_message)
2472 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2473 in the message need to be interpreted. */
2474 error (parser->type_definition_forbidden_message);
2480 /* This function is called when the DECLARATOR is processed. The TYPE
2481 was a type defined in the decl-specifiers. If it is invalid to
2482 define a type in the decl-specifiers for DECLARATOR, an error is
2483 issued. TYPE_LOCATION is the location of TYPE and is used
2484 for error reporting. */
2487 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2488 tree type, location_t type_location)
2490 /* [dcl.fct] forbids type definitions in return types.
2491 Unfortunately, it's not easy to know whether or not we are
2492 processing a return type until after the fact. */
2494 && (declarator->kind == cdk_pointer
2495 || declarator->kind == cdk_reference
2496 || declarator->kind == cdk_ptrmem))
2497 declarator = declarator->declarator;
2499 && declarator->kind == cdk_function)
2501 error_at (type_location,
2502 "new types may not be defined in a return type");
2503 inform (type_location,
2504 "(perhaps a semicolon is missing after the definition of %qT)",
2509 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2510 "<" in any valid C++ program. If the next token is indeed "<",
2511 issue a message warning the user about what appears to be an
2512 invalid attempt to form a template-id. LOCATION is the location
2513 of the type-specifier (TYPE) */
2516 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2517 tree type, location_t location)
2519 cp_token_position start = 0;
2521 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2524 error_at (location, "%qT is not a template", type);
2525 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2526 error_at (location, "%qE is not a template", type);
2528 error_at (location, "invalid template-id");
2529 /* Remember the location of the invalid "<". */
2530 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2531 start = cp_lexer_token_position (parser->lexer, true);
2532 /* Consume the "<". */
2533 cp_lexer_consume_token (parser->lexer);
2534 /* Parse the template arguments. */
2535 cp_parser_enclosed_template_argument_list (parser);
2536 /* Permanently remove the invalid template arguments so that
2537 this error message is not issued again. */
2539 cp_lexer_purge_tokens_after (parser->lexer, start);
2543 /* If parsing an integral constant-expression, issue an error message
2544 about the fact that THING appeared and return true. Otherwise,
2545 return false. In either case, set
2546 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2549 cp_parser_non_integral_constant_expression (cp_parser *parser,
2550 non_integral_constant thing)
2552 parser->non_integral_constant_expression_p = true;
2553 if (parser->integral_constant_expression_p)
2555 if (!parser->allow_non_integral_constant_expression_p)
2557 const char *msg = NULL;
2561 error ("floating-point literal "
2562 "cannot appear in a constant-expression");
2565 error ("a cast to a type other than an integral or "
2566 "enumeration type cannot appear in a "
2567 "constant-expression");
2570 error ("%<typeid%> operator "
2571 "cannot appear in a constant-expression");
2574 error ("non-constant compound literals "
2575 "cannot appear in a constant-expression");
2578 error ("a function call "
2579 "cannot appear in a constant-expression");
2582 error ("an increment "
2583 "cannot appear in a constant-expression");
2586 error ("an decrement "
2587 "cannot appear in a constant-expression");
2590 error ("an array reference "
2591 "cannot appear in a constant-expression");
2593 case NIC_ADDR_LABEL:
2594 error ("the address of a label "
2595 "cannot appear in a constant-expression");
2597 case NIC_OVERLOADED:
2598 error ("calls to overloaded operators "
2599 "cannot appear in a constant-expression");
2601 case NIC_ASSIGNMENT:
2602 error ("an assignment cannot appear in a constant-expression");
2605 error ("a comma operator "
2606 "cannot appear in a constant-expression");
2608 case NIC_CONSTRUCTOR:
2609 error ("a call to a constructor "
2610 "cannot appear in a constant-expression");
2616 msg = "__FUNCTION__";
2618 case NIC_PRETTY_FUNC:
2619 msg = "__PRETTY_FUNCTION__";
2639 case NIC_PREINCREMENT:
2642 case NIC_PREDECREMENT:
2655 error ("%qs cannot appear in a constant-expression", msg);
2662 /* Emit a diagnostic for an invalid type name. SCOPE is the
2663 qualifying scope (or NULL, if none) for ID. This function commits
2664 to the current active tentative parse, if any. (Otherwise, the
2665 problematic construct might be encountered again later, resulting
2666 in duplicate error messages.) LOCATION is the location of ID. */
2669 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2670 tree scope, tree id,
2671 location_t location)
2673 tree decl, old_scope;
2674 /* Try to lookup the identifier. */
2675 old_scope = parser->scope;
2676 parser->scope = scope;
2677 decl = cp_parser_lookup_name_simple (parser, id, location);
2678 parser->scope = old_scope;
2679 /* If the lookup found a template-name, it means that the user forgot
2680 to specify an argument list. Emit a useful error message. */
2681 if (TREE_CODE (decl) == TEMPLATE_DECL)
2683 "invalid use of template-name %qE without an argument list",
2685 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2686 error_at (location, "invalid use of destructor %qD as a type", id);
2687 else if (TREE_CODE (decl) == TYPE_DECL)
2688 /* Something like 'unsigned A a;' */
2689 error_at (location, "invalid combination of multiple type-specifiers");
2690 else if (!parser->scope)
2692 /* Issue an error message. */
2693 error_at (location, "%qE does not name a type", id);
2694 /* If we're in a template class, it's possible that the user was
2695 referring to a type from a base class. For example:
2697 template <typename T> struct A { typedef T X; };
2698 template <typename T> struct B : public A<T> { X x; };
2700 The user should have said "typename A<T>::X". */
2701 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2702 inform (location, "C++0x %<constexpr%> only available with "
2703 "-std=c++0x or -std=gnu++0x");
2704 else if (processing_template_decl && current_class_type
2705 && TYPE_BINFO (current_class_type))
2709 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2713 tree base_type = BINFO_TYPE (b);
2714 if (CLASS_TYPE_P (base_type)
2715 && dependent_type_p (base_type))
2718 /* Go from a particular instantiation of the
2719 template (which will have an empty TYPE_FIELDs),
2720 to the main version. */
2721 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2722 for (field = TYPE_FIELDS (base_type);
2724 field = DECL_CHAIN (field))
2725 if (TREE_CODE (field) == TYPE_DECL
2726 && DECL_NAME (field) == id)
2729 "(perhaps %<typename %T::%E%> was intended)",
2730 BINFO_TYPE (b), id);
2739 /* Here we diagnose qualified-ids where the scope is actually correct,
2740 but the identifier does not resolve to a valid type name. */
2741 else if (parser->scope != error_mark_node)
2743 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2744 error_at (location, "%qE in namespace %qE does not name a type",
2746 else if (CLASS_TYPE_P (parser->scope)
2747 && constructor_name_p (id, parser->scope))
2750 error_at (location, "%<%T::%E%> names the constructor, not"
2751 " the type", parser->scope, id);
2752 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2753 error_at (location, "and %qT has no template constructors",
2756 else if (TYPE_P (parser->scope)
2757 && dependent_scope_p (parser->scope))
2758 error_at (location, "need %<typename%> before %<%T::%E%> because "
2759 "%qT is a dependent scope",
2760 parser->scope, id, parser->scope);
2761 else if (TYPE_P (parser->scope))
2762 error_at (location, "%qE in class %qT does not name a type",
2767 cp_parser_commit_to_tentative_parse (parser);
2770 /* Check for a common situation where a type-name should be present,
2771 but is not, and issue a sensible error message. Returns true if an
2772 invalid type-name was detected.
2774 The situation handled by this function are variable declarations of the
2775 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2776 Usually, `ID' should name a type, but if we got here it means that it
2777 does not. We try to emit the best possible error message depending on
2778 how exactly the id-expression looks like. */
2781 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2784 cp_token *token = cp_lexer_peek_token (parser->lexer);
2786 /* Avoid duplicate error about ambiguous lookup. */
2787 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2789 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2790 if (next->type == CPP_NAME && next->ambiguous_p)
2794 cp_parser_parse_tentatively (parser);
2795 id = cp_parser_id_expression (parser,
2796 /*template_keyword_p=*/false,
2797 /*check_dependency_p=*/true,
2798 /*template_p=*/NULL,
2799 /*declarator_p=*/true,
2800 /*optional_p=*/false);
2801 /* If the next token is a (, this is a function with no explicit return
2802 type, i.e. constructor, destructor or conversion op. */
2803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2804 || TREE_CODE (id) == TYPE_DECL)
2806 cp_parser_abort_tentative_parse (parser);
2809 if (!cp_parser_parse_definitely (parser))
2812 /* Emit a diagnostic for the invalid type. */
2813 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2814 id, token->location);
2816 /* If we aren't in the middle of a declarator (i.e. in a
2817 parameter-declaration-clause), skip to the end of the declaration;
2818 there's no point in trying to process it. */
2819 if (!parser->in_declarator_p)
2820 cp_parser_skip_to_end_of_block_or_statement (parser);
2824 /* Consume tokens up to, and including, the next non-nested closing `)'.
2825 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2826 are doing error recovery. Returns -1 if OR_COMMA is true and we
2827 found an unnested comma. */
2830 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2835 unsigned paren_depth = 0;
2836 unsigned brace_depth = 0;
2837 unsigned square_depth = 0;
2839 if (recovering && !or_comma
2840 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2845 cp_token * token = cp_lexer_peek_token (parser->lexer);
2847 switch (token->type)
2850 case CPP_PRAGMA_EOL:
2851 /* If we've run out of tokens, then there is no closing `)'. */
2854 /* This is good for lambda expression capture-lists. */
2855 case CPP_OPEN_SQUARE:
2858 case CPP_CLOSE_SQUARE:
2859 if (!square_depth--)
2864 /* This matches the processing in skip_to_end_of_statement. */
2869 case CPP_OPEN_BRACE:
2872 case CPP_CLOSE_BRACE:
2878 if (recovering && or_comma && !brace_depth && !paren_depth
2883 case CPP_OPEN_PAREN:
2888 case CPP_CLOSE_PAREN:
2889 if (!brace_depth && !paren_depth--)
2892 cp_lexer_consume_token (parser->lexer);
2901 /* Consume the token. */
2902 cp_lexer_consume_token (parser->lexer);
2906 /* Consume tokens until we reach the end of the current statement.
2907 Normally, that will be just before consuming a `;'. However, if a
2908 non-nested `}' comes first, then we stop before consuming that. */
2911 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2913 unsigned nesting_depth = 0;
2917 cp_token *token = cp_lexer_peek_token (parser->lexer);
2919 switch (token->type)
2922 case CPP_PRAGMA_EOL:
2923 /* If we've run out of tokens, stop. */
2927 /* If the next token is a `;', we have reached the end of the
2933 case CPP_CLOSE_BRACE:
2934 /* If this is a non-nested '}', stop before consuming it.
2935 That way, when confronted with something like:
2939 we stop before consuming the closing '}', even though we
2940 have not yet reached a `;'. */
2941 if (nesting_depth == 0)
2944 /* If it is the closing '}' for a block that we have
2945 scanned, stop -- but only after consuming the token.
2951 we will stop after the body of the erroneously declared
2952 function, but before consuming the following `typedef'
2954 if (--nesting_depth == 0)
2956 cp_lexer_consume_token (parser->lexer);
2960 case CPP_OPEN_BRACE:
2968 /* Consume the token. */
2969 cp_lexer_consume_token (parser->lexer);
2973 /* This function is called at the end of a statement or declaration.
2974 If the next token is a semicolon, it is consumed; otherwise, error
2975 recovery is attempted. */
2978 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2980 /* Look for the trailing `;'. */
2981 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2983 /* If there is additional (erroneous) input, skip to the end of
2985 cp_parser_skip_to_end_of_statement (parser);
2986 /* If the next token is now a `;', consume it. */
2987 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2988 cp_lexer_consume_token (parser->lexer);
2992 /* Skip tokens until we have consumed an entire block, or until we
2993 have consumed a non-nested `;'. */
2996 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2998 int nesting_depth = 0;
3000 while (nesting_depth >= 0)
3002 cp_token *token = cp_lexer_peek_token (parser->lexer);
3004 switch (token->type)
3007 case CPP_PRAGMA_EOL:
3008 /* If we've run out of tokens, stop. */
3012 /* Stop if this is an unnested ';'. */
3017 case CPP_CLOSE_BRACE:
3018 /* Stop if this is an unnested '}', or closes the outermost
3021 if (nesting_depth < 0)
3027 case CPP_OPEN_BRACE:
3036 /* Consume the token. */
3037 cp_lexer_consume_token (parser->lexer);
3041 /* Skip tokens until a non-nested closing curly brace is the next
3042 token, or there are no more tokens. Return true in the first case,
3046 cp_parser_skip_to_closing_brace (cp_parser *parser)
3048 unsigned nesting_depth = 0;
3052 cp_token *token = cp_lexer_peek_token (parser->lexer);
3054 switch (token->type)
3057 case CPP_PRAGMA_EOL:
3058 /* If we've run out of tokens, stop. */
3061 case CPP_CLOSE_BRACE:
3062 /* If the next token is a non-nested `}', then we have reached
3063 the end of the current block. */
3064 if (nesting_depth-- == 0)
3068 case CPP_OPEN_BRACE:
3069 /* If it the next token is a `{', then we are entering a new
3070 block. Consume the entire block. */
3078 /* Consume the token. */
3079 cp_lexer_consume_token (parser->lexer);
3083 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3084 parameter is the PRAGMA token, allowing us to purge the entire pragma
3088 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3092 parser->lexer->in_pragma = false;
3095 token = cp_lexer_consume_token (parser->lexer);
3096 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3098 /* Ensure that the pragma is not parsed again. */
3099 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3102 /* Require pragma end of line, resyncing with it as necessary. The
3103 arguments are as for cp_parser_skip_to_pragma_eol. */
3106 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3108 parser->lexer->in_pragma = false;
3109 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3110 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3113 /* This is a simple wrapper around make_typename_type. When the id is
3114 an unresolved identifier node, we can provide a superior diagnostic
3115 using cp_parser_diagnose_invalid_type_name. */
3118 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3119 tree id, location_t id_location)
3122 if (TREE_CODE (id) == IDENTIFIER_NODE)
3124 result = make_typename_type (scope, id, typename_type,
3125 /*complain=*/tf_none);
3126 if (result == error_mark_node)
3127 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3130 return make_typename_type (scope, id, typename_type, tf_error);
3133 /* This is a wrapper around the
3134 make_{pointer,ptrmem,reference}_declarator functions that decides
3135 which one to call based on the CODE and CLASS_TYPE arguments. The
3136 CODE argument should be one of the values returned by
3137 cp_parser_ptr_operator. */
3138 static cp_declarator *
3139 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3140 cp_cv_quals cv_qualifiers,
3141 cp_declarator *target)
3143 if (code == ERROR_MARK)
3144 return cp_error_declarator;
3146 if (code == INDIRECT_REF)
3147 if (class_type == NULL_TREE)
3148 return make_pointer_declarator (cv_qualifiers, target);
3150 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3151 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3152 return make_reference_declarator (cv_qualifiers, target, false);
3153 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3154 return make_reference_declarator (cv_qualifiers, target, true);
3158 /* Create a new C++ parser. */
3161 cp_parser_new (void)
3167 /* cp_lexer_new_main is called before doing GC allocation because
3168 cp_lexer_new_main might load a PCH file. */
3169 lexer = cp_lexer_new_main ();
3171 /* Initialize the binops_by_token so that we can get the tree
3172 directly from the token. */
3173 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3174 binops_by_token[binops[i].token_type] = binops[i];
3176 parser = ggc_alloc_cleared_cp_parser ();
3177 parser->lexer = lexer;
3178 parser->context = cp_parser_context_new (NULL);
3180 /* For now, we always accept GNU extensions. */
3181 parser->allow_gnu_extensions_p = 1;
3183 /* The `>' token is a greater-than operator, not the end of a
3185 parser->greater_than_is_operator_p = true;
3187 parser->default_arg_ok_p = true;
3189 /* We are not parsing a constant-expression. */
3190 parser->integral_constant_expression_p = false;
3191 parser->allow_non_integral_constant_expression_p = false;
3192 parser->non_integral_constant_expression_p = false;
3194 /* Local variable names are not forbidden. */
3195 parser->local_variables_forbidden_p = false;
3197 /* We are not processing an `extern "C"' declaration. */
3198 parser->in_unbraced_linkage_specification_p = false;
3200 /* We are not processing a declarator. */
3201 parser->in_declarator_p = false;
3203 /* We are not processing a template-argument-list. */
3204 parser->in_template_argument_list_p = false;
3206 /* We are not in an iteration statement. */
3207 parser->in_statement = 0;
3209 /* We are not in a switch statement. */
3210 parser->in_switch_statement_p = false;
3212 /* We are not parsing a type-id inside an expression. */
3213 parser->in_type_id_in_expr_p = false;
3215 /* Declarations aren't implicitly extern "C". */
3216 parser->implicit_extern_c = false;
3218 /* String literals should be translated to the execution character set. */
3219 parser->translate_strings_p = true;
3221 /* We are not parsing a function body. */
3222 parser->in_function_body = false;
3224 /* The unparsed function queue is empty. */
3225 push_unparsed_function_queues (parser);
3227 /* There are no classes being defined. */
3228 parser->num_classes_being_defined = 0;
3230 /* No template parameters apply. */
3231 parser->num_template_parameter_lists = 0;
3236 /* Create a cp_lexer structure which will emit the tokens in CACHE
3237 and push it onto the parser's lexer stack. This is used for delayed
3238 parsing of in-class method bodies and default arguments, and should
3239 not be confused with tentative parsing. */
3241 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3243 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3244 lexer->next = parser->lexer;
3245 parser->lexer = lexer;
3247 /* Move the current source position to that of the first token in the
3249 cp_lexer_set_source_position_from_token (lexer->next_token);
3252 /* Pop the top lexer off the parser stack. This is never used for the
3253 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3255 cp_parser_pop_lexer (cp_parser *parser)
3257 cp_lexer *lexer = parser->lexer;
3258 parser->lexer = lexer->next;
3259 cp_lexer_destroy (lexer);
3261 /* Put the current source position back where it was before this
3262 lexer was pushed. */
3263 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3266 /* Lexical conventions [gram.lex] */
3268 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3272 cp_parser_identifier (cp_parser* parser)
3276 /* Look for the identifier. */
3277 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3278 /* Return the value. */
3279 return token ? token->u.value : error_mark_node;
3282 /* Parse a sequence of adjacent string constants. Returns a
3283 TREE_STRING representing the combined, nul-terminated string
3284 constant. If TRANSLATE is true, translate the string to the
3285 execution character set. If WIDE_OK is true, a wide string is
3288 C++98 [lex.string] says that if a narrow string literal token is
3289 adjacent to a wide string literal token, the behavior is undefined.
3290 However, C99 6.4.5p4 says that this results in a wide string literal.
3291 We follow C99 here, for consistency with the C front end.
3293 This code is largely lifted from lex_string() in c-lex.c.
3295 FUTURE: ObjC++ will need to handle @-strings here. */
3297 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3301 struct obstack str_ob;
3302 cpp_string str, istr, *strs;
3304 enum cpp_ttype type;
3306 tok = cp_lexer_peek_token (parser->lexer);
3307 if (!cp_parser_is_string_literal (tok))
3309 cp_parser_error (parser, "expected string-literal");
3310 return error_mark_node;
3315 /* Try to avoid the overhead of creating and destroying an obstack
3316 for the common case of just one string. */
3317 if (!cp_parser_is_string_literal
3318 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3320 cp_lexer_consume_token (parser->lexer);
3322 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3323 str.len = TREE_STRING_LENGTH (tok->u.value);
3330 gcc_obstack_init (&str_ob);
3335 cp_lexer_consume_token (parser->lexer);
3337 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3338 str.len = TREE_STRING_LENGTH (tok->u.value);
3340 if (type != tok->type)
3342 if (type == CPP_STRING)
3344 else if (tok->type != CPP_STRING)
3345 error_at (tok->location,
3346 "unsupported non-standard concatenation "
3347 "of string literals");
3350 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3352 tok = cp_lexer_peek_token (parser->lexer);
3354 while (cp_parser_is_string_literal (tok));
3356 strs = (cpp_string *) obstack_finish (&str_ob);
3359 if (type != CPP_STRING && !wide_ok)
3361 cp_parser_error (parser, "a wide string is invalid in this context");
3365 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3366 (parse_in, strs, count, &istr, type))
3368 value = build_string (istr.len, (const char *)istr.text);
3369 free (CONST_CAST (unsigned char *, istr.text));
3375 case CPP_UTF8STRING:
3376 TREE_TYPE (value) = char_array_type_node;
3379 TREE_TYPE (value) = char16_array_type_node;
3382 TREE_TYPE (value) = char32_array_type_node;
3385 TREE_TYPE (value) = wchar_array_type_node;
3389 value = fix_string_type (value);
3392 /* cpp_interpret_string has issued an error. */
3393 value = error_mark_node;
3396 obstack_free (&str_ob, 0);
3402 /* Basic concepts [gram.basic] */
3404 /* Parse a translation-unit.
3407 declaration-seq [opt]
3409 Returns TRUE if all went well. */
3412 cp_parser_translation_unit (cp_parser* parser)
3414 /* The address of the first non-permanent object on the declarator
3416 static void *declarator_obstack_base;
3420 /* Create the declarator obstack, if necessary. */
3421 if (!cp_error_declarator)
3423 gcc_obstack_init (&declarator_obstack);
3424 /* Create the error declarator. */
3425 cp_error_declarator = make_declarator (cdk_error);
3426 /* Create the empty parameter list. */
3427 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3428 /* Remember where the base of the declarator obstack lies. */
3429 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3432 cp_parser_declaration_seq_opt (parser);
3434 /* If there are no tokens left then all went well. */
3435 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3437 /* Get rid of the token array; we don't need it any more. */
3438 cp_lexer_destroy (parser->lexer);
3439 parser->lexer = NULL;
3441 /* This file might have been a context that's implicitly extern
3442 "C". If so, pop the lang context. (Only relevant for PCH.) */
3443 if (parser->implicit_extern_c)
3445 pop_lang_context ();
3446 parser->implicit_extern_c = false;
3450 finish_translation_unit ();
3456 cp_parser_error (parser, "expected declaration");
3460 /* Make sure the declarator obstack was fully cleaned up. */
3461 gcc_assert (obstack_next_free (&declarator_obstack)
3462 == declarator_obstack_base);
3464 /* All went well. */
3468 /* Expressions [gram.expr] */
3470 /* Parse a primary-expression.
3481 ( compound-statement )
3482 __builtin_va_arg ( assignment-expression , type-id )
3483 __builtin_offsetof ( type-id , offsetof-expression )
3486 __has_nothrow_assign ( type-id )
3487 __has_nothrow_constructor ( type-id )
3488 __has_nothrow_copy ( type-id )
3489 __has_trivial_assign ( type-id )
3490 __has_trivial_constructor ( type-id )
3491 __has_trivial_copy ( type-id )
3492 __has_trivial_destructor ( type-id )
3493 __has_virtual_destructor ( type-id )
3494 __is_abstract ( type-id )
3495 __is_base_of ( type-id , type-id )
3496 __is_class ( type-id )
3497 __is_convertible_to ( type-id , type-id )
3498 __is_empty ( type-id )
3499 __is_enum ( type-id )
3500 __is_pod ( type-id )
3501 __is_polymorphic ( type-id )
3502 __is_union ( type-id )
3504 Objective-C++ Extension:
3512 ADDRESS_P is true iff this expression was immediately preceded by
3513 "&" and therefore might denote a pointer-to-member. CAST_P is true
3514 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3515 true iff this expression is a template argument.
3517 Returns a representation of the expression. Upon return, *IDK
3518 indicates what kind of id-expression (if any) was present. */
3521 cp_parser_primary_expression (cp_parser *parser,
3524 bool template_arg_p,
3527 cp_token *token = NULL;
3529 /* Assume the primary expression is not an id-expression. */
3530 *idk = CP_ID_KIND_NONE;
3532 /* Peek at the next token. */
3533 token = cp_lexer_peek_token (parser->lexer);
3534 switch (token->type)
3547 token = cp_lexer_consume_token (parser->lexer);
3548 if (TREE_CODE (token->u.value) == FIXED_CST)
3550 error_at (token->location,
3551 "fixed-point types not supported in C++");
3552 return error_mark_node;
3554 /* Floating-point literals are only allowed in an integral
3555 constant expression if they are cast to an integral or
3556 enumeration type. */
3557 if (TREE_CODE (token->u.value) == REAL_CST
3558 && parser->integral_constant_expression_p
3561 /* CAST_P will be set even in invalid code like "int(2.7 +
3562 ...)". Therefore, we have to check that the next token
3563 is sure to end the cast. */
3566 cp_token *next_token;
3568 next_token = cp_lexer_peek_token (parser->lexer);
3569 if (/* The comma at the end of an
3570 enumerator-definition. */
3571 next_token->type != CPP_COMMA
3572 /* The curly brace at the end of an enum-specifier. */
3573 && next_token->type != CPP_CLOSE_BRACE
3574 /* The end of a statement. */
3575 && next_token->type != CPP_SEMICOLON
3576 /* The end of the cast-expression. */
3577 && next_token->type != CPP_CLOSE_PAREN
3578 /* The end of an array bound. */
3579 && next_token->type != CPP_CLOSE_SQUARE
3580 /* The closing ">" in a template-argument-list. */
3581 && (next_token->type != CPP_GREATER
3582 || parser->greater_than_is_operator_p)
3583 /* C++0x only: A ">>" treated like two ">" tokens,
3584 in a template-argument-list. */
3585 && (next_token->type != CPP_RSHIFT
3586 || (cxx_dialect == cxx98)
3587 || parser->greater_than_is_operator_p))
3591 /* If we are within a cast, then the constraint that the
3592 cast is to an integral or enumeration type will be
3593 checked at that point. If we are not within a cast, then
3594 this code is invalid. */
3596 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3598 return token->u.value;
3604 case CPP_UTF8STRING:
3605 /* ??? Should wide strings be allowed when parser->translate_strings_p
3606 is false (i.e. in attributes)? If not, we can kill the third
3607 argument to cp_parser_string_literal. */
3608 return cp_parser_string_literal (parser,
3609 parser->translate_strings_p,
3612 case CPP_OPEN_PAREN:
3615 bool saved_greater_than_is_operator_p;
3617 /* Consume the `('. */
3618 cp_lexer_consume_token (parser->lexer);
3619 /* Within a parenthesized expression, a `>' token is always
3620 the greater-than operator. */
3621 saved_greater_than_is_operator_p
3622 = parser->greater_than_is_operator_p;
3623 parser->greater_than_is_operator_p = true;
3624 /* If we see `( { ' then we are looking at the beginning of
3625 a GNU statement-expression. */
3626 if (cp_parser_allow_gnu_extensions_p (parser)
3627 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3629 /* Statement-expressions are not allowed by the standard. */
3630 pedwarn (token->location, OPT_pedantic,
3631 "ISO C++ forbids braced-groups within expressions");
3633 /* And they're not allowed outside of a function-body; you
3634 cannot, for example, write:
3636 int i = ({ int j = 3; j + 1; });
3638 at class or namespace scope. */
3639 if (!parser->in_function_body
3640 || parser->in_template_argument_list_p)
3642 error_at (token->location,
3643 "statement-expressions are not allowed outside "
3644 "functions nor in template-argument lists");
3645 cp_parser_skip_to_end_of_block_or_statement (parser);
3646 expr = error_mark_node;
3650 /* Start the statement-expression. */
3651 expr = begin_stmt_expr ();
3652 /* Parse the compound-statement. */
3653 cp_parser_compound_statement (parser, expr, false);
3655 expr = finish_stmt_expr (expr, false);
3660 /* Parse the parenthesized expression. */
3661 expr = cp_parser_expression (parser, cast_p, idk);
3662 /* Let the front end know that this expression was
3663 enclosed in parentheses. This matters in case, for
3664 example, the expression is of the form `A::B', since
3665 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3667 finish_parenthesized_expr (expr);
3669 /* The `>' token might be the end of a template-id or
3670 template-parameter-list now. */
3671 parser->greater_than_is_operator_p
3672 = saved_greater_than_is_operator_p;
3673 /* Consume the `)'. */
3674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3675 cp_parser_skip_to_end_of_statement (parser);
3680 case CPP_OPEN_SQUARE:
3681 if (c_dialect_objc ())
3682 /* We have an Objective-C++ message. */
3683 return cp_parser_objc_expression (parser);
3684 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3685 return cp_parser_lambda_expression (parser);
3687 case CPP_OBJC_STRING:
3688 if (c_dialect_objc ())
3689 /* We have an Objective-C++ string literal. */
3690 return cp_parser_objc_expression (parser);
3691 cp_parser_error (parser, "expected primary-expression");
3692 return error_mark_node;
3695 switch (token->keyword)
3697 /* These two are the boolean literals. */
3699 cp_lexer_consume_token (parser->lexer);
3700 return boolean_true_node;
3702 cp_lexer_consume_token (parser->lexer);
3703 return boolean_false_node;
3705 /* The `__null' literal. */
3707 cp_lexer_consume_token (parser->lexer);
3710 /* The `nullptr' literal. */
3712 cp_lexer_consume_token (parser->lexer);
3713 return nullptr_node;
3715 /* Recognize the `this' keyword. */
3717 cp_lexer_consume_token (parser->lexer);
3718 if (parser->local_variables_forbidden_p)
3720 error_at (token->location,
3721 "%<this%> may not be used in this context");
3722 return error_mark_node;
3724 /* Pointers cannot appear in constant-expressions. */
3725 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3726 return error_mark_node;
3727 return finish_this_expr ();
3729 /* The `operator' keyword can be the beginning of an
3734 case RID_FUNCTION_NAME:
3735 case RID_PRETTY_FUNCTION_NAME:
3736 case RID_C99_FUNCTION_NAME:
3738 non_integral_constant name;
3740 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3741 __func__ are the names of variables -- but they are
3742 treated specially. Therefore, they are handled here,
3743 rather than relying on the generic id-expression logic
3744 below. Grammatically, these names are id-expressions.
3746 Consume the token. */
3747 token = cp_lexer_consume_token (parser->lexer);
3749 switch (token->keyword)
3751 case RID_FUNCTION_NAME:
3752 name = NIC_FUNC_NAME;
3754 case RID_PRETTY_FUNCTION_NAME:
3755 name = NIC_PRETTY_FUNC;
3757 case RID_C99_FUNCTION_NAME:
3758 name = NIC_C99_FUNC;
3764 if (cp_parser_non_integral_constant_expression (parser, name))
3765 return error_mark_node;
3767 /* Look up the name. */
3768 return finish_fname (token->u.value);
3776 /* The `__builtin_va_arg' construct is used to handle
3777 `va_arg'. Consume the `__builtin_va_arg' token. */
3778 cp_lexer_consume_token (parser->lexer);
3779 /* Look for the opening `('. */
3780 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3781 /* Now, parse the assignment-expression. */
3782 expression = cp_parser_assignment_expression (parser,
3783 /*cast_p=*/false, NULL);
3784 /* Look for the `,'. */
3785 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3786 /* Parse the type-id. */
3787 type = cp_parser_type_id (parser);
3788 /* Look for the closing `)'. */
3789 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3790 /* Using `va_arg' in a constant-expression is not
3792 if (cp_parser_non_integral_constant_expression (parser,
3794 return error_mark_node;
3795 return build_x_va_arg (expression, type);
3799 return cp_parser_builtin_offsetof (parser);
3801 case RID_HAS_NOTHROW_ASSIGN:
3802 case RID_HAS_NOTHROW_CONSTRUCTOR:
3803 case RID_HAS_NOTHROW_COPY:
3804 case RID_HAS_TRIVIAL_ASSIGN:
3805 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3806 case RID_HAS_TRIVIAL_COPY:
3807 case RID_HAS_TRIVIAL_DESTRUCTOR:
3808 case RID_HAS_VIRTUAL_DESTRUCTOR:
3809 case RID_IS_ABSTRACT:
3810 case RID_IS_BASE_OF:
3812 case RID_IS_CONVERTIBLE_TO:
3816 case RID_IS_POLYMORPHIC:
3817 case RID_IS_STD_LAYOUT:
3818 case RID_IS_TRIVIAL:
3820 case RID_IS_LITERAL_TYPE:
3821 return cp_parser_trait_expr (parser, token->keyword);
3823 /* Objective-C++ expressions. */
3825 case RID_AT_PROTOCOL:
3826 case RID_AT_SELECTOR:
3827 return cp_parser_objc_expression (parser);
3830 if (parser->in_function_body
3831 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3834 error_at (token->location,
3835 "a template declaration cannot appear at block scope");
3836 cp_parser_skip_to_end_of_block_or_statement (parser);
3837 return error_mark_node;
3840 cp_parser_error (parser, "expected primary-expression");
3841 return error_mark_node;
3844 /* An id-expression can start with either an identifier, a
3845 `::' as the beginning of a qualified-id, or the "operator"
3849 case CPP_TEMPLATE_ID:
3850 case CPP_NESTED_NAME_SPECIFIER:
3854 const char *error_msg;
3857 cp_token *id_expr_token;
3860 /* Parse the id-expression. */
3862 = cp_parser_id_expression (parser,
3863 /*template_keyword_p=*/false,
3864 /*check_dependency_p=*/true,
3866 /*declarator_p=*/false,
3867 /*optional_p=*/false);
3868 if (id_expression == error_mark_node)
3869 return error_mark_node;
3870 id_expr_token = token;
3871 token = cp_lexer_peek_token (parser->lexer);
3872 done = (token->type != CPP_OPEN_SQUARE
3873 && token->type != CPP_OPEN_PAREN
3874 && token->type != CPP_DOT
3875 && token->type != CPP_DEREF
3876 && token->type != CPP_PLUS_PLUS
3877 && token->type != CPP_MINUS_MINUS);
3878 /* If we have a template-id, then no further lookup is
3879 required. If the template-id was for a template-class, we
3880 will sometimes have a TYPE_DECL at this point. */
3881 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3882 || TREE_CODE (id_expression) == TYPE_DECL)
3883 decl = id_expression;
3884 /* Look up the name. */
3887 tree ambiguous_decls;
3889 /* If we already know that this lookup is ambiguous, then
3890 we've already issued an error message; there's no reason
3892 if (id_expr_token->type == CPP_NAME
3893 && id_expr_token->ambiguous_p)
3895 cp_parser_simulate_error (parser);
3896 return error_mark_node;
3899 decl = cp_parser_lookup_name (parser, id_expression,
3902 /*is_namespace=*/false,
3903 /*check_dependency=*/true,
3905 id_expr_token->location);
3906 /* If the lookup was ambiguous, an error will already have
3908 if (ambiguous_decls)
3909 return error_mark_node;
3911 /* In Objective-C++, an instance variable (ivar) may be preferred
3912 to whatever cp_parser_lookup_name() found. */
3913 decl = objc_lookup_ivar (decl, id_expression);
3915 /* If name lookup gives us a SCOPE_REF, then the
3916 qualifying scope was dependent. */
3917 if (TREE_CODE (decl) == SCOPE_REF)
3919 /* At this point, we do not know if DECL is a valid
3920 integral constant expression. We assume that it is
3921 in fact such an expression, so that code like:
3923 template <int N> struct A {
3927 is accepted. At template-instantiation time, we
3928 will check that B<N>::i is actually a constant. */
3931 /* Check to see if DECL is a local variable in a context
3932 where that is forbidden. */
3933 if (parser->local_variables_forbidden_p
3934 && local_variable_p (decl))
3936 /* It might be that we only found DECL because we are
3937 trying to be generous with pre-ISO scoping rules.
3938 For example, consider:
3942 for (int i = 0; i < 10; ++i) {}
3943 extern void f(int j = i);
3946 Here, name look up will originally find the out
3947 of scope `i'. We need to issue a warning message,
3948 but then use the global `i'. */
3949 decl = check_for_out_of_scope_variable (decl);
3950 if (local_variable_p (decl))
3952 error_at (id_expr_token->location,
3953 "local variable %qD may not appear in this context",
3955 return error_mark_node;
3960 decl = (finish_id_expression
3961 (id_expression, decl, parser->scope,
3963 parser->integral_constant_expression_p,
3964 parser->allow_non_integral_constant_expression_p,
3965 &parser->non_integral_constant_expression_p,
3966 template_p, done, address_p,
3969 id_expr_token->location));
3971 cp_parser_error (parser, error_msg);
3975 /* Anything else is an error. */
3977 cp_parser_error (parser, "expected primary-expression");
3978 return error_mark_node;
3982 /* Parse an id-expression.
3989 :: [opt] nested-name-specifier template [opt] unqualified-id
3991 :: operator-function-id
3994 Return a representation of the unqualified portion of the
3995 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3996 a `::' or nested-name-specifier.
3998 Often, if the id-expression was a qualified-id, the caller will
3999 want to make a SCOPE_REF to represent the qualified-id. This
4000 function does not do this in order to avoid wastefully creating
4001 SCOPE_REFs when they are not required.
4003 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4006 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4007 uninstantiated templates.
4009 If *TEMPLATE_P is non-NULL, it is set to true iff the
4010 `template' keyword is used to explicitly indicate that the entity
4011 named is a template.
4013 If DECLARATOR_P is true, the id-expression is appearing as part of
4014 a declarator, rather than as part of an expression. */
4017 cp_parser_id_expression (cp_parser *parser,
4018 bool template_keyword_p,
4019 bool check_dependency_p,
4024 bool global_scope_p;
4025 bool nested_name_specifier_p;
4027 /* Assume the `template' keyword was not used. */
4029 *template_p = template_keyword_p;
4031 /* Look for the optional `::' operator. */
4033 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4035 /* Look for the optional nested-name-specifier. */
4036 nested_name_specifier_p
4037 = (cp_parser_nested_name_specifier_opt (parser,
4038 /*typename_keyword_p=*/false,
4043 /* If there is a nested-name-specifier, then we are looking at
4044 the first qualified-id production. */
4045 if (nested_name_specifier_p)
4048 tree saved_object_scope;
4049 tree saved_qualifying_scope;
4050 tree unqualified_id;
4053 /* See if the next token is the `template' keyword. */
4055 template_p = &is_template;
4056 *template_p = cp_parser_optional_template_keyword (parser);
4057 /* Name lookup we do during the processing of the
4058 unqualified-id might obliterate SCOPE. */
4059 saved_scope = parser->scope;
4060 saved_object_scope = parser->object_scope;
4061 saved_qualifying_scope = parser->qualifying_scope;
4062 /* Process the final unqualified-id. */
4063 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4066 /*optional_p=*/false);
4067 /* Restore the SAVED_SCOPE for our caller. */
4068 parser->scope = saved_scope;
4069 parser->object_scope = saved_object_scope;
4070 parser->qualifying_scope = saved_qualifying_scope;
4072 return unqualified_id;
4074 /* Otherwise, if we are in global scope, then we are looking at one
4075 of the other qualified-id productions. */
4076 else if (global_scope_p)
4081 /* Peek at the next token. */
4082 token = cp_lexer_peek_token (parser->lexer);
4084 /* If it's an identifier, and the next token is not a "<", then
4085 we can avoid the template-id case. This is an optimization
4086 for this common case. */
4087 if (token->type == CPP_NAME
4088 && !cp_parser_nth_token_starts_template_argument_list_p
4090 return cp_parser_identifier (parser);
4092 cp_parser_parse_tentatively (parser);
4093 /* Try a template-id. */
4094 id = cp_parser_template_id (parser,
4095 /*template_keyword_p=*/false,
4096 /*check_dependency_p=*/true,
4098 /* If that worked, we're done. */
4099 if (cp_parser_parse_definitely (parser))
4102 /* Peek at the next token. (Changes in the token buffer may
4103 have invalidated the pointer obtained above.) */
4104 token = cp_lexer_peek_token (parser->lexer);
4106 switch (token->type)
4109 return cp_parser_identifier (parser);
4112 if (token->keyword == RID_OPERATOR)
4113 return cp_parser_operator_function_id (parser);
4117 cp_parser_error (parser, "expected id-expression");
4118 return error_mark_node;
4122 return cp_parser_unqualified_id (parser, template_keyword_p,
4123 /*check_dependency_p=*/true,
4128 /* Parse an unqualified-id.
4132 operator-function-id
4133 conversion-function-id
4137 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4138 keyword, in a construct like `A::template ...'.
4140 Returns a representation of unqualified-id. For the `identifier'
4141 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4142 production a BIT_NOT_EXPR is returned; the operand of the
4143 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4144 other productions, see the documentation accompanying the
4145 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4146 names are looked up in uninstantiated templates. If DECLARATOR_P
4147 is true, the unqualified-id is appearing as part of a declarator,
4148 rather than as part of an expression. */
4151 cp_parser_unqualified_id (cp_parser* parser,
4152 bool template_keyword_p,
4153 bool check_dependency_p,
4159 /* Peek at the next token. */
4160 token = cp_lexer_peek_token (parser->lexer);
4162 switch (token->type)
4168 /* We don't know yet whether or not this will be a
4170 cp_parser_parse_tentatively (parser);
4171 /* Try a template-id. */
4172 id = cp_parser_template_id (parser, template_keyword_p,
4175 /* If it worked, we're done. */
4176 if (cp_parser_parse_definitely (parser))
4178 /* Otherwise, it's an ordinary identifier. */
4179 return cp_parser_identifier (parser);
4182 case CPP_TEMPLATE_ID:
4183 return cp_parser_template_id (parser, template_keyword_p,
4190 tree qualifying_scope;
4195 /* Consume the `~' token. */
4196 cp_lexer_consume_token (parser->lexer);
4197 /* Parse the class-name. The standard, as written, seems to
4200 template <typename T> struct S { ~S (); };
4201 template <typename T> S<T>::~S() {}
4203 is invalid, since `~' must be followed by a class-name, but
4204 `S<T>' is dependent, and so not known to be a class.
4205 That's not right; we need to look in uninstantiated
4206 templates. A further complication arises from:
4208 template <typename T> void f(T t) {
4212 Here, it is not possible to look up `T' in the scope of `T'
4213 itself. We must look in both the current scope, and the
4214 scope of the containing complete expression.
4216 Yet another issue is:
4225 The standard does not seem to say that the `S' in `~S'
4226 should refer to the type `S' and not the data member
4229 /* DR 244 says that we look up the name after the "~" in the
4230 same scope as we looked up the qualifying name. That idea
4231 isn't fully worked out; it's more complicated than that. */
4232 scope = parser->scope;
4233 object_scope = parser->object_scope;
4234 qualifying_scope = parser->qualifying_scope;
4236 /* Check for invalid scopes. */
4237 if (scope == error_mark_node)
4239 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4240 cp_lexer_consume_token (parser->lexer);
4241 return error_mark_node;
4243 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4245 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4246 error_at (token->location,
4247 "scope %qT before %<~%> is not a class-name",
4249 cp_parser_simulate_error (parser);
4250 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4251 cp_lexer_consume_token (parser->lexer);
4252 return error_mark_node;
4254 gcc_assert (!scope || TYPE_P (scope));
4256 /* If the name is of the form "X::~X" it's OK even if X is a
4258 token = cp_lexer_peek_token (parser->lexer);
4260 && token->type == CPP_NAME
4261 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4263 && (token->u.value == TYPE_IDENTIFIER (scope)
4264 || constructor_name_p (token->u.value, scope)))
4266 cp_lexer_consume_token (parser->lexer);
4267 return build_nt (BIT_NOT_EXPR, scope);
4270 /* If there was an explicit qualification (S::~T), first look
4271 in the scope given by the qualification (i.e., S).
4273 Note: in the calls to cp_parser_class_name below we pass
4274 typename_type so that lookup finds the injected-class-name
4275 rather than the constructor. */
4277 type_decl = NULL_TREE;
4280 cp_parser_parse_tentatively (parser);
4281 type_decl = cp_parser_class_name (parser,
4282 /*typename_keyword_p=*/false,
4283 /*template_keyword_p=*/false,
4285 /*check_dependency=*/false,
4286 /*class_head_p=*/false,
4288 if (cp_parser_parse_definitely (parser))
4291 /* In "N::S::~S", look in "N" as well. */
4292 if (!done && scope && qualifying_scope)
4294 cp_parser_parse_tentatively (parser);
4295 parser->scope = qualifying_scope;
4296 parser->object_scope = NULL_TREE;
4297 parser->qualifying_scope = NULL_TREE;
4299 = cp_parser_class_name (parser,
4300 /*typename_keyword_p=*/false,
4301 /*template_keyword_p=*/false,
4303 /*check_dependency=*/false,
4304 /*class_head_p=*/false,
4306 if (cp_parser_parse_definitely (parser))
4309 /* In "p->S::~T", look in the scope given by "*p" as well. */
4310 else if (!done && object_scope)
4312 cp_parser_parse_tentatively (parser);
4313 parser->scope = object_scope;
4314 parser->object_scope = NULL_TREE;
4315 parser->qualifying_scope = NULL_TREE;
4317 = cp_parser_class_name (parser,
4318 /*typename_keyword_p=*/false,
4319 /*template_keyword_p=*/false,
4321 /*check_dependency=*/false,
4322 /*class_head_p=*/false,
4324 if (cp_parser_parse_definitely (parser))
4327 /* Look in the surrounding context. */
4330 parser->scope = NULL_TREE;
4331 parser->object_scope = NULL_TREE;
4332 parser->qualifying_scope = NULL_TREE;
4333 if (processing_template_decl)
4334 cp_parser_parse_tentatively (parser);
4336 = cp_parser_class_name (parser,
4337 /*typename_keyword_p=*/false,
4338 /*template_keyword_p=*/false,
4340 /*check_dependency=*/false,
4341 /*class_head_p=*/false,
4343 if (processing_template_decl
4344 && ! cp_parser_parse_definitely (parser))
4346 /* We couldn't find a type with this name, so just accept
4347 it and check for a match at instantiation time. */
4348 type_decl = cp_parser_identifier (parser);
4349 if (type_decl != error_mark_node)
4350 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4354 /* If an error occurred, assume that the name of the
4355 destructor is the same as the name of the qualifying
4356 class. That allows us to keep parsing after running
4357 into ill-formed destructor names. */
4358 if (type_decl == error_mark_node && scope)
4359 return build_nt (BIT_NOT_EXPR, scope);
4360 else if (type_decl == error_mark_node)
4361 return error_mark_node;
4363 /* Check that destructor name and scope match. */
4364 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4366 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4367 error_at (token->location,
4368 "declaration of %<~%T%> as member of %qT",
4370 cp_parser_simulate_error (parser);
4371 return error_mark_node;
4376 A typedef-name that names a class shall not be used as the
4377 identifier in the declarator for a destructor declaration. */
4379 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4380 && !DECL_SELF_REFERENCE_P (type_decl)
4381 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4382 error_at (token->location,
4383 "typedef-name %qD used as destructor declarator",
4386 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4390 if (token->keyword == RID_OPERATOR)
4394 /* This could be a template-id, so we try that first. */
4395 cp_parser_parse_tentatively (parser);
4396 /* Try a template-id. */
4397 id = cp_parser_template_id (parser, template_keyword_p,
4398 /*check_dependency_p=*/true,
4400 /* If that worked, we're done. */
4401 if (cp_parser_parse_definitely (parser))
4403 /* We still don't know whether we're looking at an
4404 operator-function-id or a conversion-function-id. */
4405 cp_parser_parse_tentatively (parser);
4406 /* Try an operator-function-id. */
4407 id = cp_parser_operator_function_id (parser);
4408 /* If that didn't work, try a conversion-function-id. */
4409 if (!cp_parser_parse_definitely (parser))
4410 id = cp_parser_conversion_function_id (parser);
4419 cp_parser_error (parser, "expected unqualified-id");
4420 return error_mark_node;
4424 /* Parse an (optional) nested-name-specifier.
4426 nested-name-specifier: [C++98]
4427 class-or-namespace-name :: nested-name-specifier [opt]
4428 class-or-namespace-name :: template nested-name-specifier [opt]
4430 nested-name-specifier: [C++0x]
4433 nested-name-specifier identifier ::
4434 nested-name-specifier template [opt] simple-template-id ::
4436 PARSER->SCOPE should be set appropriately before this function is
4437 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4438 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4441 Sets PARSER->SCOPE to the class (TYPE) or namespace
4442 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4443 it unchanged if there is no nested-name-specifier. Returns the new
4444 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4446 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4447 part of a declaration and/or decl-specifier. */
4450 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4451 bool typename_keyword_p,
4452 bool check_dependency_p,
4454 bool is_declaration)
4456 bool success = false;
4457 cp_token_position start = 0;
4460 /* Remember where the nested-name-specifier starts. */
4461 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4463 start = cp_lexer_token_position (parser->lexer, false);
4464 push_deferring_access_checks (dk_deferred);
4471 tree saved_qualifying_scope;
4472 bool template_keyword_p;
4474 /* Spot cases that cannot be the beginning of a
4475 nested-name-specifier. */
4476 token = cp_lexer_peek_token (parser->lexer);
4478 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4479 the already parsed nested-name-specifier. */
4480 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4482 /* Grab the nested-name-specifier and continue the loop. */
4483 cp_parser_pre_parsed_nested_name_specifier (parser);
4484 /* If we originally encountered this nested-name-specifier
4485 with IS_DECLARATION set to false, we will not have
4486 resolved TYPENAME_TYPEs, so we must do so here. */
4488 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4490 new_scope = resolve_typename_type (parser->scope,
4491 /*only_current_p=*/false);
4492 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4493 parser->scope = new_scope;
4499 /* Spot cases that cannot be the beginning of a
4500 nested-name-specifier. On the second and subsequent times
4501 through the loop, we look for the `template' keyword. */
4502 if (success && token->keyword == RID_TEMPLATE)
4504 /* A template-id can start a nested-name-specifier. */
4505 else if (token->type == CPP_TEMPLATE_ID)
4509 /* If the next token is not an identifier, then it is
4510 definitely not a type-name or namespace-name. */
4511 if (token->type != CPP_NAME)
4513 /* If the following token is neither a `<' (to begin a
4514 template-id), nor a `::', then we are not looking at a
4515 nested-name-specifier. */
4516 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4517 if (token->type != CPP_SCOPE
4518 && !cp_parser_nth_token_starts_template_argument_list_p
4523 /* The nested-name-specifier is optional, so we parse
4525 cp_parser_parse_tentatively (parser);
4527 /* Look for the optional `template' keyword, if this isn't the
4528 first time through the loop. */
4530 template_keyword_p = cp_parser_optional_template_keyword (parser);
4532 template_keyword_p = false;
4534 /* Save the old scope since the name lookup we are about to do
4535 might destroy it. */
4536 old_scope = parser->scope;
4537 saved_qualifying_scope = parser->qualifying_scope;
4538 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4539 look up names in "X<T>::I" in order to determine that "Y" is
4540 a template. So, if we have a typename at this point, we make
4541 an effort to look through it. */
4543 && !typename_keyword_p
4545 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4546 parser->scope = resolve_typename_type (parser->scope,
4547 /*only_current_p=*/false);
4548 /* Parse the qualifying entity. */
4550 = cp_parser_qualifying_entity (parser,
4556 /* Look for the `::' token. */
4557 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4559 /* If we found what we wanted, we keep going; otherwise, we're
4561 if (!cp_parser_parse_definitely (parser))
4563 bool error_p = false;
4565 /* Restore the OLD_SCOPE since it was valid before the
4566 failed attempt at finding the last
4567 class-or-namespace-name. */
4568 parser->scope = old_scope;
4569 parser->qualifying_scope = saved_qualifying_scope;
4570 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4572 /* If the next token is an identifier, and the one after
4573 that is a `::', then any valid interpretation would have
4574 found a class-or-namespace-name. */
4575 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4576 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4578 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4581 token = cp_lexer_consume_token (parser->lexer);
4584 if (!token->ambiguous_p)
4587 tree ambiguous_decls;
4589 decl = cp_parser_lookup_name (parser, token->u.value,
4591 /*is_template=*/false,
4592 /*is_namespace=*/false,
4593 /*check_dependency=*/true,
4596 if (TREE_CODE (decl) == TEMPLATE_DECL)
4597 error_at (token->location,
4598 "%qD used without template parameters",
4600 else if (ambiguous_decls)
4602 error_at (token->location,
4603 "reference to %qD is ambiguous",
4605 print_candidates (ambiguous_decls);
4606 decl = error_mark_node;
4610 if (cxx_dialect != cxx98)
4611 cp_parser_name_lookup_error
4612 (parser, token->u.value, decl, NLE_NOT_CXX98,
4615 cp_parser_name_lookup_error
4616 (parser, token->u.value, decl, NLE_CXX98,
4620 parser->scope = error_mark_node;
4622 /* Treat this as a successful nested-name-specifier
4627 If the name found is not a class-name (clause
4628 _class_) or namespace-name (_namespace.def_), the
4629 program is ill-formed. */
4632 cp_lexer_consume_token (parser->lexer);
4636 /* We've found one valid nested-name-specifier. */
4638 /* Name lookup always gives us a DECL. */
4639 if (TREE_CODE (new_scope) == TYPE_DECL)
4640 new_scope = TREE_TYPE (new_scope);
4641 /* Uses of "template" must be followed by actual templates. */
4642 if (template_keyword_p
4643 && !(CLASS_TYPE_P (new_scope)
4644 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4645 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4646 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4647 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4648 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4649 == TEMPLATE_ID_EXPR)))
4650 permerror (input_location, TYPE_P (new_scope)
4651 ? "%qT is not a template"
4652 : "%qD is not a template",
4654 /* If it is a class scope, try to complete it; we are about to
4655 be looking up names inside the class. */
4656 if (TYPE_P (new_scope)
4657 /* Since checking types for dependency can be expensive,
4658 avoid doing it if the type is already complete. */
4659 && !COMPLETE_TYPE_P (new_scope)
4660 /* Do not try to complete dependent types. */
4661 && !dependent_type_p (new_scope))
4663 new_scope = complete_type (new_scope);
4664 /* If it is a typedef to current class, use the current
4665 class instead, as the typedef won't have any names inside
4667 if (!COMPLETE_TYPE_P (new_scope)
4668 && currently_open_class (new_scope))
4669 new_scope = TYPE_MAIN_VARIANT (new_scope);
4671 /* Make sure we look in the right scope the next time through
4673 parser->scope = new_scope;
4676 /* If parsing tentatively, replace the sequence of tokens that makes
4677 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4678 token. That way, should we re-parse the token stream, we will
4679 not have to repeat the effort required to do the parse, nor will
4680 we issue duplicate error messages. */
4681 if (success && start)
4685 token = cp_lexer_token_at (parser->lexer, start);
4686 /* Reset the contents of the START token. */
4687 token->type = CPP_NESTED_NAME_SPECIFIER;
4688 /* Retrieve any deferred checks. Do not pop this access checks yet
4689 so the memory will not be reclaimed during token replacing below. */
4690 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4691 token->u.tree_check_value->value = parser->scope;
4692 token->u.tree_check_value->checks = get_deferred_access_checks ();
4693 token->u.tree_check_value->qualifying_scope =
4694 parser->qualifying_scope;
4695 token->keyword = RID_MAX;
4697 /* Purge all subsequent tokens. */
4698 cp_lexer_purge_tokens_after (parser->lexer, start);
4702 pop_to_parent_deferring_access_checks ();
4704 return success ? parser->scope : NULL_TREE;
4707 /* Parse a nested-name-specifier. See
4708 cp_parser_nested_name_specifier_opt for details. This function
4709 behaves identically, except that it will an issue an error if no
4710 nested-name-specifier is present. */
4713 cp_parser_nested_name_specifier (cp_parser *parser,
4714 bool typename_keyword_p,
4715 bool check_dependency_p,
4717 bool is_declaration)
4721 /* Look for the nested-name-specifier. */
4722 scope = cp_parser_nested_name_specifier_opt (parser,
4727 /* If it was not present, issue an error message. */
4730 cp_parser_error (parser, "expected nested-name-specifier");
4731 parser->scope = NULL_TREE;
4737 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4738 this is either a class-name or a namespace-name (which corresponds
4739 to the class-or-namespace-name production in the grammar). For
4740 C++0x, it can also be a type-name that refers to an enumeration
4743 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4744 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4745 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4746 TYPE_P is TRUE iff the next name should be taken as a class-name,
4747 even the same name is declared to be another entity in the same
4750 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4751 specified by the class-or-namespace-name. If neither is found the
4752 ERROR_MARK_NODE is returned. */
4755 cp_parser_qualifying_entity (cp_parser *parser,
4756 bool typename_keyword_p,
4757 bool template_keyword_p,
4758 bool check_dependency_p,
4760 bool is_declaration)
4763 tree saved_qualifying_scope;
4764 tree saved_object_scope;
4767 bool successful_parse_p;
4769 /* Before we try to parse the class-name, we must save away the
4770 current PARSER->SCOPE since cp_parser_class_name will destroy
4772 saved_scope = parser->scope;
4773 saved_qualifying_scope = parser->qualifying_scope;
4774 saved_object_scope = parser->object_scope;
4775 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4776 there is no need to look for a namespace-name. */
4777 only_class_p = template_keyword_p
4778 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4780 cp_parser_parse_tentatively (parser);
4781 scope = cp_parser_class_name (parser,
4784 type_p ? class_type : none_type,
4786 /*class_head_p=*/false,
4788 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4789 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4791 && cxx_dialect != cxx98
4792 && !successful_parse_p)
4794 /* Restore the saved scope. */
4795 parser->scope = saved_scope;
4796 parser->qualifying_scope = saved_qualifying_scope;
4797 parser->object_scope = saved_object_scope;
4799 /* Parse tentatively. */
4800 cp_parser_parse_tentatively (parser);
4802 /* Parse a typedef-name or enum-name. */
4803 scope = cp_parser_nonclass_name (parser);
4805 /* "If the name found does not designate a namespace or a class,
4806 enumeration, or dependent type, the program is ill-formed."
4808 We cover classes and dependent types above and namespaces below,
4809 so this code is only looking for enums. */
4810 if (!scope || TREE_CODE (scope) != TYPE_DECL
4811 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4812 cp_parser_simulate_error (parser);
4814 successful_parse_p = cp_parser_parse_definitely (parser);
4816 /* If that didn't work, try for a namespace-name. */
4817 if (!only_class_p && !successful_parse_p)
4819 /* Restore the saved scope. */
4820 parser->scope = saved_scope;
4821 parser->qualifying_scope = saved_qualifying_scope;
4822 parser->object_scope = saved_object_scope;
4823 /* If we are not looking at an identifier followed by the scope
4824 resolution operator, then this is not part of a
4825 nested-name-specifier. (Note that this function is only used
4826 to parse the components of a nested-name-specifier.) */
4827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4828 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4829 return error_mark_node;
4830 scope = cp_parser_namespace_name (parser);
4836 /* Parse a postfix-expression.
4840 postfix-expression [ expression ]
4841 postfix-expression ( expression-list [opt] )
4842 simple-type-specifier ( expression-list [opt] )
4843 typename :: [opt] nested-name-specifier identifier
4844 ( expression-list [opt] )
4845 typename :: [opt] nested-name-specifier template [opt] template-id
4846 ( expression-list [opt] )
4847 postfix-expression . template [opt] id-expression
4848 postfix-expression -> template [opt] id-expression
4849 postfix-expression . pseudo-destructor-name
4850 postfix-expression -> pseudo-destructor-name
4851 postfix-expression ++
4852 postfix-expression --
4853 dynamic_cast < type-id > ( expression )
4854 static_cast < type-id > ( expression )
4855 reinterpret_cast < type-id > ( expression )
4856 const_cast < type-id > ( expression )
4857 typeid ( expression )
4863 ( type-id ) { initializer-list , [opt] }
4865 This extension is a GNU version of the C99 compound-literal
4866 construct. (The C99 grammar uses `type-name' instead of `type-id',
4867 but they are essentially the same concept.)
4869 If ADDRESS_P is true, the postfix expression is the operand of the
4870 `&' operator. CAST_P is true if this expression is the target of a
4873 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4874 class member access expressions [expr.ref].
4876 Returns a representation of the expression. */
4879 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4880 bool member_access_only_p,
4881 cp_id_kind * pidk_return)
4885 cp_id_kind idk = CP_ID_KIND_NONE;
4886 tree postfix_expression = NULL_TREE;
4887 bool is_member_access = false;
4889 /* Peek at the next token. */
4890 token = cp_lexer_peek_token (parser->lexer);
4891 /* Some of the productions are determined by keywords. */
4892 keyword = token->keyword;
4902 const char *saved_message;
4904 /* All of these can be handled in the same way from the point
4905 of view of parsing. Begin by consuming the token
4906 identifying the cast. */
4907 cp_lexer_consume_token (parser->lexer);
4909 /* New types cannot be defined in the cast. */
4910 saved_message = parser->type_definition_forbidden_message;
4911 parser->type_definition_forbidden_message
4912 = G_("types may not be defined in casts");
4914 /* Look for the opening `<'. */
4915 cp_parser_require (parser, CPP_LESS, RT_LESS);
4916 /* Parse the type to which we are casting. */
4917 type = cp_parser_type_id (parser);
4918 /* Look for the closing `>'. */
4919 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4920 /* Restore the old message. */
4921 parser->type_definition_forbidden_message = saved_message;
4923 /* And the expression which is being cast. */
4924 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4925 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4928 /* Only type conversions to integral or enumeration types
4929 can be used in constant-expressions. */
4930 if (!cast_valid_in_integral_constant_expression_p (type)
4931 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4932 return error_mark_node;
4938 = build_dynamic_cast (type, expression, tf_warning_or_error);
4942 = build_static_cast (type, expression, tf_warning_or_error);
4946 = build_reinterpret_cast (type, expression,
4947 tf_warning_or_error);
4951 = build_const_cast (type, expression, tf_warning_or_error);
4962 const char *saved_message;
4963 bool saved_in_type_id_in_expr_p;
4965 /* Consume the `typeid' token. */
4966 cp_lexer_consume_token (parser->lexer);
4967 /* Look for the `(' token. */
4968 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4969 /* Types cannot be defined in a `typeid' expression. */
4970 saved_message = parser->type_definition_forbidden_message;
4971 parser->type_definition_forbidden_message
4972 = G_("types may not be defined in a %<typeid%> expression");
4973 /* We can't be sure yet whether we're looking at a type-id or an
4975 cp_parser_parse_tentatively (parser);
4976 /* Try a type-id first. */
4977 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4978 parser->in_type_id_in_expr_p = true;
4979 type = cp_parser_type_id (parser);
4980 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4981 /* Look for the `)' token. Otherwise, we can't be sure that
4982 we're not looking at an expression: consider `typeid (int
4983 (3))', for example. */
4984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4985 /* If all went well, simply lookup the type-id. */
4986 if (cp_parser_parse_definitely (parser))
4987 postfix_expression = get_typeid (type);
4988 /* Otherwise, fall back to the expression variant. */
4993 /* Look for an expression. */
4994 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4995 /* Compute its typeid. */
4996 postfix_expression = build_typeid (expression);
4997 /* Look for the `)' token. */
4998 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5000 /* Restore the saved message. */
5001 parser->type_definition_forbidden_message = saved_message;
5002 /* `typeid' may not appear in an integral constant expression. */
5003 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5004 return error_mark_node;
5011 /* The syntax permitted here is the same permitted for an
5012 elaborated-type-specifier. */
5013 type = cp_parser_elaborated_type_specifier (parser,
5014 /*is_friend=*/false,
5015 /*is_declaration=*/false);
5016 postfix_expression = cp_parser_functional_cast (parser, type);
5024 /* If the next thing is a simple-type-specifier, we may be
5025 looking at a functional cast. We could also be looking at
5026 an id-expression. So, we try the functional cast, and if
5027 that doesn't work we fall back to the primary-expression. */
5028 cp_parser_parse_tentatively (parser);
5029 /* Look for the simple-type-specifier. */
5030 type = cp_parser_simple_type_specifier (parser,
5031 /*decl_specs=*/NULL,
5032 CP_PARSER_FLAGS_NONE);
5033 /* Parse the cast itself. */
5034 if (!cp_parser_error_occurred (parser))
5036 = cp_parser_functional_cast (parser, type);
5037 /* If that worked, we're done. */
5038 if (cp_parser_parse_definitely (parser))
5041 /* If the functional-cast didn't work out, try a
5042 compound-literal. */
5043 if (cp_parser_allow_gnu_extensions_p (parser)
5044 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5046 VEC(constructor_elt,gc) *initializer_list = NULL;
5047 bool saved_in_type_id_in_expr_p;
5049 cp_parser_parse_tentatively (parser);
5050 /* Consume the `('. */
5051 cp_lexer_consume_token (parser->lexer);
5052 /* Parse the type. */
5053 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5054 parser->in_type_id_in_expr_p = true;
5055 type = cp_parser_type_id (parser);
5056 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5057 /* Look for the `)'. */
5058 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5059 /* Look for the `{'. */
5060 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5061 /* If things aren't going well, there's no need to
5063 if (!cp_parser_error_occurred (parser))
5065 bool non_constant_p;
5066 /* Parse the initializer-list. */
5068 = cp_parser_initializer_list (parser, &non_constant_p);
5069 /* Allow a trailing `,'. */
5070 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5071 cp_lexer_consume_token (parser->lexer);
5072 /* Look for the final `}'. */
5073 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5075 /* If that worked, we're definitely looking at a
5076 compound-literal expression. */
5077 if (cp_parser_parse_definitely (parser))
5079 /* Warn the user that a compound literal is not
5080 allowed in standard C++. */
5081 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5082 /* For simplicity, we disallow compound literals in
5083 constant-expressions. We could
5084 allow compound literals of integer type, whose
5085 initializer was a constant, in constant
5086 expressions. Permitting that usage, as a further
5087 extension, would not change the meaning of any
5088 currently accepted programs. (Of course, as
5089 compound literals are not part of ISO C++, the
5090 standard has nothing to say.) */
5091 if (cp_parser_non_integral_constant_expression (parser,
5094 postfix_expression = error_mark_node;
5097 /* Form the representation of the compound-literal. */
5099 = (finish_compound_literal
5100 (type, build_constructor (init_list_type_node,
5101 initializer_list)));
5106 /* It must be a primary-expression. */
5108 = cp_parser_primary_expression (parser, address_p, cast_p,
5109 /*template_arg_p=*/false,
5115 /* Keep looping until the postfix-expression is complete. */
5118 if (idk == CP_ID_KIND_UNQUALIFIED
5119 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5120 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5121 /* It is not a Koenig lookup function call. */
5123 = unqualified_name_lookup_error (postfix_expression);
5125 /* Peek at the next token. */
5126 token = cp_lexer_peek_token (parser->lexer);
5128 switch (token->type)
5130 case CPP_OPEN_SQUARE:
5132 = cp_parser_postfix_open_square_expression (parser,
5135 idk = CP_ID_KIND_NONE;
5136 is_member_access = false;
5139 case CPP_OPEN_PAREN:
5140 /* postfix-expression ( expression-list [opt] ) */
5143 bool is_builtin_constant_p;
5144 bool saved_integral_constant_expression_p = false;
5145 bool saved_non_integral_constant_expression_p = false;
5148 is_member_access = false;
5150 is_builtin_constant_p
5151 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5152 if (is_builtin_constant_p)
5154 /* The whole point of __builtin_constant_p is to allow
5155 non-constant expressions to appear as arguments. */
5156 saved_integral_constant_expression_p
5157 = parser->integral_constant_expression_p;
5158 saved_non_integral_constant_expression_p
5159 = parser->non_integral_constant_expression_p;
5160 parser->integral_constant_expression_p = false;
5162 args = (cp_parser_parenthesized_expression_list
5164 /*cast_p=*/false, /*allow_expansion_p=*/true,
5165 /*non_constant_p=*/NULL));
5166 if (is_builtin_constant_p)
5168 parser->integral_constant_expression_p
5169 = saved_integral_constant_expression_p;
5170 parser->non_integral_constant_expression_p
5171 = saved_non_integral_constant_expression_p;
5176 postfix_expression = error_mark_node;
5180 /* Function calls are not permitted in
5181 constant-expressions. */
5182 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5183 && cp_parser_non_integral_constant_expression (parser,
5186 postfix_expression = error_mark_node;
5187 release_tree_vector (args);
5192 if (idk == CP_ID_KIND_UNQUALIFIED
5193 || idk == CP_ID_KIND_TEMPLATE_ID)
5195 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5197 if (!VEC_empty (tree, args))
5200 if (!any_type_dependent_arguments_p (args))
5202 = perform_koenig_lookup (postfix_expression, args,
5203 /*include_std=*/false);
5207 = unqualified_fn_lookup_error (postfix_expression);
5209 /* We do not perform argument-dependent lookup if
5210 normal lookup finds a non-function, in accordance
5211 with the expected resolution of DR 218. */
5212 else if (!VEC_empty (tree, args)
5213 && is_overloaded_fn (postfix_expression))
5215 tree fn = get_first_fn (postfix_expression);
5216 fn = STRIP_TEMPLATE (fn);
5218 /* Do not do argument dependent lookup if regular
5219 lookup finds a member function or a block-scope
5220 function declaration. [basic.lookup.argdep]/3 */
5221 if (!DECL_FUNCTION_MEMBER_P (fn)
5222 && !DECL_LOCAL_FUNCTION_P (fn))
5225 if (!any_type_dependent_arguments_p (args))
5227 = perform_koenig_lookup (postfix_expression, args,
5228 /*include_std=*/false);
5233 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5235 tree instance = TREE_OPERAND (postfix_expression, 0);
5236 tree fn = TREE_OPERAND (postfix_expression, 1);
5238 if (processing_template_decl
5239 && (type_dependent_expression_p (instance)
5240 || (!BASELINK_P (fn)
5241 && TREE_CODE (fn) != FIELD_DECL)
5242 || type_dependent_expression_p (fn)
5243 || any_type_dependent_arguments_p (args)))
5246 = build_nt_call_vec (postfix_expression, args);
5247 release_tree_vector (args);
5251 if (BASELINK_P (fn))
5254 = (build_new_method_call
5255 (instance, fn, &args, NULL_TREE,
5256 (idk == CP_ID_KIND_QUALIFIED
5257 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5259 tf_warning_or_error));
5263 = finish_call_expr (postfix_expression, &args,
5264 /*disallow_virtual=*/false,
5266 tf_warning_or_error);
5268 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5269 || TREE_CODE (postfix_expression) == MEMBER_REF
5270 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5271 postfix_expression = (build_offset_ref_call_from_tree
5272 (postfix_expression, &args));
5273 else if (idk == CP_ID_KIND_QUALIFIED)
5274 /* A call to a static class member, or a namespace-scope
5277 = finish_call_expr (postfix_expression, &args,
5278 /*disallow_virtual=*/true,
5280 tf_warning_or_error);
5282 /* All other function calls. */
5284 = finish_call_expr (postfix_expression, &args,
5285 /*disallow_virtual=*/false,
5287 tf_warning_or_error);
5289 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5290 idk = CP_ID_KIND_NONE;
5292 release_tree_vector (args);
5298 /* postfix-expression . template [opt] id-expression
5299 postfix-expression . pseudo-destructor-name
5300 postfix-expression -> template [opt] id-expression
5301 postfix-expression -> pseudo-destructor-name */
5303 /* Consume the `.' or `->' operator. */
5304 cp_lexer_consume_token (parser->lexer);
5307 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5312 is_member_access = true;
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 POSTINCREMENT_EXPR);
5323 /* Increments may not appear in constant-expressions. */
5324 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5325 postfix_expression = error_mark_node;
5326 idk = CP_ID_KIND_NONE;
5327 is_member_access = false;
5330 case CPP_MINUS_MINUS:
5331 /* postfix-expression -- */
5332 /* Consume the `--' token. */
5333 cp_lexer_consume_token (parser->lexer);
5334 /* Generate a representation for the complete expression. */
5336 = finish_increment_expr (postfix_expression,
5337 POSTDECREMENT_EXPR);
5338 /* Decrements may not appear in constant-expressions. */
5339 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5340 postfix_expression = error_mark_node;
5341 idk = CP_ID_KIND_NONE;
5342 is_member_access = false;
5346 if (pidk_return != NULL)
5347 * pidk_return = idk;
5348 if (member_access_only_p)
5349 return is_member_access? postfix_expression : error_mark_node;
5351 return postfix_expression;
5355 /* We should never get here. */
5357 return error_mark_node;
5360 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5361 by cp_parser_builtin_offsetof. We're looking for
5363 postfix-expression [ expression ]
5365 FOR_OFFSETOF is set if we're being called in that context, which
5366 changes how we deal with integer constant expressions. */
5369 cp_parser_postfix_open_square_expression (cp_parser *parser,
5370 tree postfix_expression,
5375 /* Consume the `[' token. */
5376 cp_lexer_consume_token (parser->lexer);
5378 /* Parse the index expression. */
5379 /* ??? For offsetof, there is a question of what to allow here. If
5380 offsetof is not being used in an integral constant expression context,
5381 then we *could* get the right answer by computing the value at runtime.
5382 If we are in an integral constant expression context, then we might
5383 could accept any constant expression; hard to say without analysis.
5384 Rather than open the barn door too wide right away, allow only integer
5385 constant expressions here. */
5387 index = cp_parser_constant_expression (parser, false, NULL);
5389 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5391 /* Look for the closing `]'. */
5392 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5394 /* Build the ARRAY_REF. */
5395 postfix_expression = grok_array_decl (postfix_expression, index);
5397 /* When not doing offsetof, array references are not permitted in
5398 constant-expressions. */
5400 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5401 postfix_expression = error_mark_node;
5403 return postfix_expression;
5406 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5407 by cp_parser_builtin_offsetof. We're looking for
5409 postfix-expression . template [opt] id-expression
5410 postfix-expression . pseudo-destructor-name
5411 postfix-expression -> template [opt] id-expression
5412 postfix-expression -> pseudo-destructor-name
5414 FOR_OFFSETOF is set if we're being called in that context. That sorta
5415 limits what of the above we'll actually accept, but nevermind.
5416 TOKEN_TYPE is the "." or "->" token, which will already have been
5417 removed from the stream. */
5420 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5421 enum cpp_ttype token_type,
5422 tree postfix_expression,
5423 bool for_offsetof, cp_id_kind *idk,
5424 location_t location)
5428 bool pseudo_destructor_p;
5429 tree scope = NULL_TREE;
5431 /* If this is a `->' operator, dereference the pointer. */
5432 if (token_type == CPP_DEREF)
5433 postfix_expression = build_x_arrow (postfix_expression);
5434 /* Check to see whether or not the expression is type-dependent. */
5435 dependent_p = type_dependent_expression_p (postfix_expression);
5436 /* The identifier following the `->' or `.' is not qualified. */
5437 parser->scope = NULL_TREE;
5438 parser->qualifying_scope = NULL_TREE;
5439 parser->object_scope = NULL_TREE;
5440 *idk = CP_ID_KIND_NONE;
5442 /* Enter the scope corresponding to the type of the object
5443 given by the POSTFIX_EXPRESSION. */
5444 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5446 scope = TREE_TYPE (postfix_expression);
5447 /* According to the standard, no expression should ever have
5448 reference type. Unfortunately, we do not currently match
5449 the standard in this respect in that our internal representation
5450 of an expression may have reference type even when the standard
5451 says it does not. Therefore, we have to manually obtain the
5452 underlying type here. */
5453 scope = non_reference (scope);
5454 /* The type of the POSTFIX_EXPRESSION must be complete. */
5455 if (scope == unknown_type_node)
5457 error_at (location, "%qE does not have class type",
5458 postfix_expression);
5462 scope = complete_type_or_else (scope, NULL_TREE);
5463 /* Let the name lookup machinery know that we are processing a
5464 class member access expression. */
5465 parser->context->object_type = scope;
5466 /* If something went wrong, we want to be able to discern that case,
5467 as opposed to the case where there was no SCOPE due to the type
5468 of expression being dependent. */
5470 scope = error_mark_node;
5471 /* If the SCOPE was erroneous, make the various semantic analysis
5472 functions exit quickly -- and without issuing additional error
5474 if (scope == error_mark_node)
5475 postfix_expression = error_mark_node;
5478 /* Assume this expression is not a pseudo-destructor access. */
5479 pseudo_destructor_p = false;
5481 /* If the SCOPE is a scalar type, then, if this is a valid program,
5482 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5483 is type dependent, it can be pseudo-destructor-name or something else.
5484 Try to parse it as pseudo-destructor-name first. */
5485 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5490 cp_parser_parse_tentatively (parser);
5491 /* Parse the pseudo-destructor-name. */
5493 cp_parser_pseudo_destructor_name (parser, &s, &type);
5495 && (cp_parser_error_occurred (parser)
5496 || TREE_CODE (type) != TYPE_DECL
5497 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5498 cp_parser_abort_tentative_parse (parser);
5499 else if (cp_parser_parse_definitely (parser))
5501 pseudo_destructor_p = true;
5503 = finish_pseudo_destructor_expr (postfix_expression,
5504 s, TREE_TYPE (type));
5508 if (!pseudo_destructor_p)
5510 /* If the SCOPE is not a scalar type, we are looking at an
5511 ordinary class member access expression, rather than a
5512 pseudo-destructor-name. */
5514 cp_token *token = cp_lexer_peek_token (parser->lexer);
5515 /* Parse the id-expression. */
5516 name = (cp_parser_id_expression
5518 cp_parser_optional_template_keyword (parser),
5519 /*check_dependency_p=*/true,
5521 /*declarator_p=*/false,
5522 /*optional_p=*/false));
5523 /* In general, build a SCOPE_REF if the member name is qualified.
5524 However, if the name was not dependent and has already been
5525 resolved; there is no need to build the SCOPE_REF. For example;
5527 struct X { void f(); };
5528 template <typename T> void f(T* t) { t->X::f(); }
5530 Even though "t" is dependent, "X::f" is not and has been resolved
5531 to a BASELINK; there is no need to include scope information. */
5533 /* But we do need to remember that there was an explicit scope for
5534 virtual function calls. */
5536 *idk = CP_ID_KIND_QUALIFIED;
5538 /* If the name is a template-id that names a type, we will get a
5539 TYPE_DECL here. That is invalid code. */
5540 if (TREE_CODE (name) == TYPE_DECL)
5542 error_at (token->location, "invalid use of %qD", name);
5543 postfix_expression = error_mark_node;
5547 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5549 name = build_qualified_name (/*type=*/NULL_TREE,
5553 parser->scope = NULL_TREE;
5554 parser->qualifying_scope = NULL_TREE;
5555 parser->object_scope = NULL_TREE;
5557 if (scope && name && BASELINK_P (name))
5558 adjust_result_of_qualified_name_lookup
5559 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5561 = finish_class_member_access_expr (postfix_expression, name,
5563 tf_warning_or_error);
5567 /* We no longer need to look up names in the scope of the object on
5568 the left-hand side of the `.' or `->' operator. */
5569 parser->context->object_type = NULL_TREE;
5571 /* Outside of offsetof, these operators may not appear in
5572 constant-expressions. */
5574 && (cp_parser_non_integral_constant_expression
5575 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5576 postfix_expression = error_mark_node;
5578 return postfix_expression;
5581 /* Parse a parenthesized expression-list.
5584 assignment-expression
5585 expression-list, assignment-expression
5590 identifier, expression-list
5592 CAST_P is true if this expression is the target of a cast.
5594 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5597 Returns a vector of trees. Each element is a representation of an
5598 assignment-expression. NULL is returned if the ( and or ) are
5599 missing. An empty, but allocated, vector is returned on no
5600 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5601 if we are parsing an attribute list for an attribute that wants a
5602 plain identifier argument, normal_attr for an attribute that wants
5603 an expression, or non_attr if we aren't parsing an attribute list. If
5604 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5605 not all of the expressions in the list were constant. */
5607 static VEC(tree,gc) *
5608 cp_parser_parenthesized_expression_list (cp_parser* parser,
5609 int is_attribute_list,
5611 bool allow_expansion_p,
5612 bool *non_constant_p)
5614 VEC(tree,gc) *expression_list;
5615 bool fold_expr_p = is_attribute_list != non_attr;
5616 tree identifier = NULL_TREE;
5617 bool saved_greater_than_is_operator_p;
5619 /* Assume all the expressions will be constant. */
5621 *non_constant_p = false;
5623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5626 expression_list = make_tree_vector ();
5628 /* Within a parenthesized expression, a `>' token is always
5629 the greater-than operator. */
5630 saved_greater_than_is_operator_p
5631 = parser->greater_than_is_operator_p;
5632 parser->greater_than_is_operator_p = true;
5634 /* Consume expressions until there are no more. */
5635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5640 /* At the beginning of attribute lists, check to see if the
5641 next token is an identifier. */
5642 if (is_attribute_list == id_attr
5643 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5647 /* Consume the identifier. */
5648 token = cp_lexer_consume_token (parser->lexer);
5649 /* Save the identifier. */
5650 identifier = token->u.value;
5654 bool expr_non_constant_p;
5656 /* Parse the next assignment-expression. */
5657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5659 /* A braced-init-list. */
5660 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5661 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5662 if (non_constant_p && expr_non_constant_p)
5663 *non_constant_p = true;
5665 else if (non_constant_p)
5667 expr = (cp_parser_constant_expression
5668 (parser, /*allow_non_constant_p=*/true,
5669 &expr_non_constant_p));
5670 if (expr_non_constant_p)
5671 *non_constant_p = true;
5674 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5677 expr = fold_non_dependent_expr (expr);
5679 /* If we have an ellipsis, then this is an expression
5681 if (allow_expansion_p
5682 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5684 /* Consume the `...'. */
5685 cp_lexer_consume_token (parser->lexer);
5687 /* Build the argument pack. */
5688 expr = make_pack_expansion (expr);
5691 /* Add it to the list. We add error_mark_node
5692 expressions to the list, so that we can still tell if
5693 the correct form for a parenthesized expression-list
5694 is found. That gives better errors. */
5695 VEC_safe_push (tree, gc, expression_list, expr);
5697 if (expr == error_mark_node)
5701 /* After the first item, attribute lists look the same as
5702 expression lists. */
5703 is_attribute_list = non_attr;
5706 /* If the next token isn't a `,', then we are done. */
5707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5710 /* Otherwise, consume the `,' and keep going. */
5711 cp_lexer_consume_token (parser->lexer);
5714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5719 /* We try and resync to an unnested comma, as that will give the
5720 user better diagnostics. */
5721 ending = cp_parser_skip_to_closing_parenthesis (parser,
5722 /*recovering=*/true,
5724 /*consume_paren=*/true);
5729 parser->greater_than_is_operator_p
5730 = saved_greater_than_is_operator_p;
5735 parser->greater_than_is_operator_p
5736 = saved_greater_than_is_operator_p;
5739 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5741 return expression_list;
5744 /* Parse a pseudo-destructor-name.
5746 pseudo-destructor-name:
5747 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5748 :: [opt] nested-name-specifier template template-id :: ~ type-name
5749 :: [opt] nested-name-specifier [opt] ~ type-name
5751 If either of the first two productions is used, sets *SCOPE to the
5752 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5753 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5754 or ERROR_MARK_NODE if the parse fails. */
5757 cp_parser_pseudo_destructor_name (cp_parser* parser,
5761 bool nested_name_specifier_p;
5763 /* Assume that things will not work out. */
5764 *type = error_mark_node;
5766 /* Look for the optional `::' operator. */
5767 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5768 /* Look for the optional nested-name-specifier. */
5769 nested_name_specifier_p
5770 = (cp_parser_nested_name_specifier_opt (parser,
5771 /*typename_keyword_p=*/false,
5772 /*check_dependency_p=*/true,
5774 /*is_declaration=*/false)
5776 /* Now, if we saw a nested-name-specifier, we might be doing the
5777 second production. */
5778 if (nested_name_specifier_p
5779 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5781 /* Consume the `template' keyword. */
5782 cp_lexer_consume_token (parser->lexer);
5783 /* Parse the template-id. */
5784 cp_parser_template_id (parser,
5785 /*template_keyword_p=*/true,
5786 /*check_dependency_p=*/false,
5787 /*is_declaration=*/true);
5788 /* Look for the `::' token. */
5789 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5791 /* If the next token is not a `~', then there might be some
5792 additional qualification. */
5793 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5795 /* At this point, we're looking for "type-name :: ~". The type-name
5796 must not be a class-name, since this is a pseudo-destructor. So,
5797 it must be either an enum-name, or a typedef-name -- both of which
5798 are just identifiers. So, we peek ahead to check that the "::"
5799 and "~" tokens are present; if they are not, then we can avoid
5800 calling type_name. */
5801 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5802 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5803 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5805 cp_parser_error (parser, "non-scalar type");
5809 /* Look for the type-name. */
5810 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5811 if (*scope == error_mark_node)
5814 /* Look for the `::' token. */
5815 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5820 /* Look for the `~'. */
5821 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5822 /* Look for the type-name again. We are not responsible for
5823 checking that it matches the first type-name. */
5824 *type = cp_parser_nonclass_name (parser);
5827 /* Parse a unary-expression.
5833 unary-operator cast-expression
5834 sizeof unary-expression
5842 __extension__ cast-expression
5843 __alignof__ unary-expression
5844 __alignof__ ( type-id )
5845 __real__ cast-expression
5846 __imag__ cast-expression
5849 ADDRESS_P is true iff the unary-expression is appearing as the
5850 operand of the `&' operator. CAST_P is true if this expression is
5851 the target of a cast.
5853 Returns a representation of the expression. */
5856 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5860 enum tree_code unary_operator;
5862 /* Peek at the next token. */
5863 token = cp_lexer_peek_token (parser->lexer);
5864 /* Some keywords give away the kind of expression. */
5865 if (token->type == CPP_KEYWORD)
5867 enum rid keyword = token->keyword;
5877 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5878 /* Consume the token. */
5879 cp_lexer_consume_token (parser->lexer);
5880 /* Parse the operand. */
5881 operand = cp_parser_sizeof_operand (parser, keyword);
5883 if (TYPE_P (operand))
5884 return cxx_sizeof_or_alignof_type (operand, op, true);
5886 return cxx_sizeof_or_alignof_expr (operand, op, true);
5890 return cp_parser_new_expression (parser);
5893 return cp_parser_delete_expression (parser);
5897 /* The saved value of the PEDANTIC flag. */
5901 /* Save away the PEDANTIC flag. */
5902 cp_parser_extension_opt (parser, &saved_pedantic);
5903 /* Parse the cast-expression. */
5904 expr = cp_parser_simple_cast_expression (parser);
5905 /* Restore the PEDANTIC flag. */
5906 pedantic = saved_pedantic;
5916 /* Consume the `__real__' or `__imag__' token. */
5917 cp_lexer_consume_token (parser->lexer);
5918 /* Parse the cast-expression. */
5919 expression = cp_parser_simple_cast_expression (parser);
5920 /* Create the complete representation. */
5921 return build_x_unary_op ((keyword == RID_REALPART
5922 ? REALPART_EXPR : IMAGPART_EXPR),
5924 tf_warning_or_error);
5931 const char *saved_message;
5932 bool saved_integral_constant_expression_p;
5933 bool saved_non_integral_constant_expression_p;
5934 bool saved_greater_than_is_operator_p;
5936 cp_lexer_consume_token (parser->lexer);
5937 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5939 saved_message = parser->type_definition_forbidden_message;
5940 parser->type_definition_forbidden_message
5941 = G_("types may not be defined in %<noexcept%> expressions");
5943 saved_integral_constant_expression_p
5944 = parser->integral_constant_expression_p;
5945 saved_non_integral_constant_expression_p
5946 = parser->non_integral_constant_expression_p;
5947 parser->integral_constant_expression_p = false;
5949 saved_greater_than_is_operator_p
5950 = parser->greater_than_is_operator_p;
5951 parser->greater_than_is_operator_p = true;
5953 ++cp_unevaluated_operand;
5954 ++c_inhibit_evaluation_warnings;
5955 expr = cp_parser_expression (parser, false, NULL);
5956 --c_inhibit_evaluation_warnings;
5957 --cp_unevaluated_operand;
5959 parser->greater_than_is_operator_p
5960 = saved_greater_than_is_operator_p;
5962 parser->integral_constant_expression_p
5963 = saved_integral_constant_expression_p;
5964 parser->non_integral_constant_expression_p
5965 = saved_non_integral_constant_expression_p;
5967 parser->type_definition_forbidden_message = saved_message;
5969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5970 return finish_noexcept_expr (expr, tf_warning_or_error);
5978 /* Look for the `:: new' and `:: delete', which also signal the
5979 beginning of a new-expression, or delete-expression,
5980 respectively. If the next token is `::', then it might be one of
5982 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5986 /* See if the token after the `::' is one of the keywords in
5987 which we're interested. */
5988 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5989 /* If it's `new', we have a new-expression. */
5990 if (keyword == RID_NEW)
5991 return cp_parser_new_expression (parser);
5992 /* Similarly, for `delete'. */
5993 else if (keyword == RID_DELETE)
5994 return cp_parser_delete_expression (parser);
5997 /* Look for a unary operator. */
5998 unary_operator = cp_parser_unary_operator (token);
5999 /* The `++' and `--' operators can be handled similarly, even though
6000 they are not technically unary-operators in the grammar. */
6001 if (unary_operator == ERROR_MARK)
6003 if (token->type == CPP_PLUS_PLUS)
6004 unary_operator = PREINCREMENT_EXPR;
6005 else if (token->type == CPP_MINUS_MINUS)
6006 unary_operator = PREDECREMENT_EXPR;
6007 /* Handle the GNU address-of-label extension. */
6008 else if (cp_parser_allow_gnu_extensions_p (parser)
6009 && token->type == CPP_AND_AND)
6013 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6015 /* Consume the '&&' token. */
6016 cp_lexer_consume_token (parser->lexer);
6017 /* Look for the identifier. */
6018 identifier = cp_parser_identifier (parser);
6019 /* Create an expression representing the address. */
6020 expression = finish_label_address_expr (identifier, loc);
6021 if (cp_parser_non_integral_constant_expression (parser,
6023 expression = error_mark_node;
6027 if (unary_operator != ERROR_MARK)
6029 tree cast_expression;
6030 tree expression = error_mark_node;
6031 non_integral_constant non_constant_p = NIC_NONE;
6033 /* Consume the operator token. */
6034 token = cp_lexer_consume_token (parser->lexer);
6035 /* Parse the cast-expression. */
6037 = cp_parser_cast_expression (parser,
6038 unary_operator == ADDR_EXPR,
6039 /*cast_p=*/false, pidk);
6040 /* Now, build an appropriate representation. */
6041 switch (unary_operator)
6044 non_constant_p = NIC_STAR;
6045 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6046 tf_warning_or_error);
6050 non_constant_p = NIC_ADDR;
6053 expression = build_x_unary_op (unary_operator, cast_expression,
6054 tf_warning_or_error);
6057 case PREINCREMENT_EXPR:
6058 case PREDECREMENT_EXPR:
6059 non_constant_p = unary_operator == PREINCREMENT_EXPR
6060 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6062 case UNARY_PLUS_EXPR:
6064 case TRUTH_NOT_EXPR:
6065 expression = finish_unary_op_expr (unary_operator, cast_expression);
6072 if (non_constant_p != NIC_NONE
6073 && cp_parser_non_integral_constant_expression (parser,
6075 expression = error_mark_node;
6080 return cp_parser_postfix_expression (parser, address_p, cast_p,
6081 /*member_access_only_p=*/false,
6085 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6086 unary-operator, the corresponding tree code is returned. */
6088 static enum tree_code
6089 cp_parser_unary_operator (cp_token* token)
6091 switch (token->type)
6094 return INDIRECT_REF;
6100 return UNARY_PLUS_EXPR;
6106 return TRUTH_NOT_EXPR;
6109 return BIT_NOT_EXPR;
6116 /* Parse a new-expression.
6119 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6120 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6122 Returns a representation of the expression. */
6125 cp_parser_new_expression (cp_parser* parser)
6127 bool global_scope_p;
6128 VEC(tree,gc) *placement;
6130 VEC(tree,gc) *initializer;
6134 /* Look for the optional `::' operator. */
6136 = (cp_parser_global_scope_opt (parser,
6137 /*current_scope_valid_p=*/false)
6139 /* Look for the `new' operator. */
6140 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6141 /* There's no easy way to tell a new-placement from the
6142 `( type-id )' construct. */
6143 cp_parser_parse_tentatively (parser);
6144 /* Look for a new-placement. */
6145 placement = cp_parser_new_placement (parser);
6146 /* If that didn't work out, there's no new-placement. */
6147 if (!cp_parser_parse_definitely (parser))
6149 if (placement != NULL)
6150 release_tree_vector (placement);
6154 /* If the next token is a `(', then we have a parenthesized
6156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6159 /* Consume the `('. */
6160 cp_lexer_consume_token (parser->lexer);
6161 /* Parse the type-id. */
6162 type = cp_parser_type_id (parser);
6163 /* Look for the closing `)'. */
6164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6165 token = cp_lexer_peek_token (parser->lexer);
6166 /* There should not be a direct-new-declarator in this production,
6167 but GCC used to allowed this, so we check and emit a sensible error
6168 message for this case. */
6169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6171 error_at (token->location,
6172 "array bound forbidden after parenthesized type-id");
6173 inform (token->location,
6174 "try removing the parentheses around the type-id");
6175 cp_parser_direct_new_declarator (parser);
6179 /* Otherwise, there must be a new-type-id. */
6181 type = cp_parser_new_type_id (parser, &nelts);
6183 /* If the next token is a `(' or '{', then we have a new-initializer. */
6184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6185 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6186 initializer = cp_parser_new_initializer (parser);
6190 /* A new-expression may not appear in an integral constant
6192 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6193 ret = error_mark_node;
6196 /* Create a representation of the new-expression. */
6197 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6198 tf_warning_or_error);
6201 if (placement != NULL)
6202 release_tree_vector (placement);
6203 if (initializer != NULL)
6204 release_tree_vector (initializer);
6209 /* Parse a new-placement.
6214 Returns the same representation as for an expression-list. */
6216 static VEC(tree,gc) *
6217 cp_parser_new_placement (cp_parser* parser)
6219 VEC(tree,gc) *expression_list;
6221 /* Parse the expression-list. */
6222 expression_list = (cp_parser_parenthesized_expression_list
6223 (parser, non_attr, /*cast_p=*/false,
6224 /*allow_expansion_p=*/true,
6225 /*non_constant_p=*/NULL));
6227 return expression_list;
6230 /* Parse a new-type-id.
6233 type-specifier-seq new-declarator [opt]
6235 Returns the TYPE allocated. If the new-type-id indicates an array
6236 type, *NELTS is set to the number of elements in the last array
6237 bound; the TYPE will not include the last array bound. */
6240 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6242 cp_decl_specifier_seq type_specifier_seq;
6243 cp_declarator *new_declarator;
6244 cp_declarator *declarator;
6245 cp_declarator *outer_declarator;
6246 const char *saved_message;
6249 /* The type-specifier sequence must not contain type definitions.
6250 (It cannot contain declarations of new types either, but if they
6251 are not definitions we will catch that because they are not
6253 saved_message = parser->type_definition_forbidden_message;
6254 parser->type_definition_forbidden_message
6255 = G_("types may not be defined in a new-type-id");
6256 /* Parse the type-specifier-seq. */
6257 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6258 /*is_trailing_return=*/false,
6259 &type_specifier_seq);
6260 /* Restore the old message. */
6261 parser->type_definition_forbidden_message = saved_message;
6262 /* Parse the new-declarator. */
6263 new_declarator = cp_parser_new_declarator_opt (parser);
6265 /* Determine the number of elements in the last array dimension, if
6268 /* Skip down to the last array dimension. */
6269 declarator = new_declarator;
6270 outer_declarator = NULL;
6271 while (declarator && (declarator->kind == cdk_pointer
6272 || declarator->kind == cdk_ptrmem))
6274 outer_declarator = declarator;
6275 declarator = declarator->declarator;
6278 && declarator->kind == cdk_array
6279 && declarator->declarator
6280 && declarator->declarator->kind == cdk_array)
6282 outer_declarator = declarator;
6283 declarator = declarator->declarator;
6286 if (declarator && declarator->kind == cdk_array)
6288 *nelts = declarator->u.array.bounds;
6289 if (*nelts == error_mark_node)
6290 *nelts = integer_one_node;
6292 if (outer_declarator)
6293 outer_declarator->declarator = declarator->declarator;
6295 new_declarator = NULL;
6298 type = groktypename (&type_specifier_seq, new_declarator, false);
6302 /* Parse an (optional) new-declarator.
6305 ptr-operator new-declarator [opt]
6306 direct-new-declarator
6308 Returns the declarator. */
6310 static cp_declarator *
6311 cp_parser_new_declarator_opt (cp_parser* parser)
6313 enum tree_code code;
6315 cp_cv_quals cv_quals;
6317 /* We don't know if there's a ptr-operator next, or not. */
6318 cp_parser_parse_tentatively (parser);
6319 /* Look for a ptr-operator. */
6320 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6321 /* If that worked, look for more new-declarators. */
6322 if (cp_parser_parse_definitely (parser))
6324 cp_declarator *declarator;
6326 /* Parse another optional declarator. */
6327 declarator = cp_parser_new_declarator_opt (parser);
6329 return cp_parser_make_indirect_declarator
6330 (code, type, cv_quals, declarator);
6333 /* If the next token is a `[', there is a direct-new-declarator. */
6334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6335 return cp_parser_direct_new_declarator (parser);
6340 /* Parse a direct-new-declarator.
6342 direct-new-declarator:
6344 direct-new-declarator [constant-expression]
6348 static cp_declarator *
6349 cp_parser_direct_new_declarator (cp_parser* parser)
6351 cp_declarator *declarator = NULL;
6357 /* Look for the opening `['. */
6358 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6359 /* The first expression is not required to be constant. */
6362 cp_token *token = cp_lexer_peek_token (parser->lexer);
6363 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6364 /* The standard requires that the expression have integral
6365 type. DR 74 adds enumeration types. We believe that the
6366 real intent is that these expressions be handled like the
6367 expression in a `switch' condition, which also allows
6368 classes with a single conversion to integral or
6369 enumeration type. */
6370 if (!processing_template_decl)
6373 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6378 error_at (token->location,
6379 "expression in new-declarator must have integral "
6380 "or enumeration type");
6381 expression = error_mark_node;
6385 /* But all the other expressions must be. */
6388 = cp_parser_constant_expression (parser,
6389 /*allow_non_constant=*/false,
6391 /* Look for the closing `]'. */
6392 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6394 /* Add this bound to the declarator. */
6395 declarator = make_array_declarator (declarator, expression);
6397 /* If the next token is not a `[', then there are no more
6399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6406 /* Parse a new-initializer.
6409 ( expression-list [opt] )
6412 Returns a representation of the expression-list. */
6414 static VEC(tree,gc) *
6415 cp_parser_new_initializer (cp_parser* parser)
6417 VEC(tree,gc) *expression_list;
6419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6422 bool expr_non_constant_p;
6423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6424 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6425 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6426 expression_list = make_tree_vector_single (t);
6429 expression_list = (cp_parser_parenthesized_expression_list
6430 (parser, non_attr, /*cast_p=*/false,
6431 /*allow_expansion_p=*/true,
6432 /*non_constant_p=*/NULL));
6434 return expression_list;
6437 /* Parse a delete-expression.
6440 :: [opt] delete cast-expression
6441 :: [opt] delete [ ] cast-expression
6443 Returns a representation of the expression. */
6446 cp_parser_delete_expression (cp_parser* parser)
6448 bool global_scope_p;
6452 /* Look for the optional `::' operator. */
6454 = (cp_parser_global_scope_opt (parser,
6455 /*current_scope_valid_p=*/false)
6457 /* Look for the `delete' keyword. */
6458 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6459 /* See if the array syntax is in use. */
6460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6462 /* Consume the `[' token. */
6463 cp_lexer_consume_token (parser->lexer);
6464 /* Look for the `]' token. */
6465 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6466 /* Remember that this is the `[]' construct. */
6472 /* Parse the cast-expression. */
6473 expression = cp_parser_simple_cast_expression (parser);
6475 /* A delete-expression may not appear in an integral constant
6477 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6478 return error_mark_node;
6480 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6483 /* Returns true if TOKEN may start a cast-expression and false
6487 cp_parser_token_starts_cast_expression (cp_token *token)
6489 switch (token->type)
6495 case CPP_CLOSE_SQUARE:
6496 case CPP_CLOSE_PAREN:
6497 case CPP_CLOSE_BRACE:
6501 case CPP_DEREF_STAR:
6509 case CPP_GREATER_EQ:
6529 /* '[' may start a primary-expression in obj-c++. */
6530 case CPP_OPEN_SQUARE:
6531 return c_dialect_objc ();
6538 /* Parse a cast-expression.
6542 ( type-id ) cast-expression
6544 ADDRESS_P is true iff the unary-expression is appearing as the
6545 operand of the `&' operator. CAST_P is true if this expression is
6546 the target of a cast.
6548 Returns a representation of the expression. */
6551 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6554 /* If it's a `(', then we might be looking at a cast. */
6555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6557 tree type = NULL_TREE;
6558 tree expr = NULL_TREE;
6559 bool compound_literal_p;
6560 const char *saved_message;
6562 /* There's no way to know yet whether or not this is a cast.
6563 For example, `(int (3))' is a unary-expression, while `(int)
6564 3' is a cast. So, we resort to parsing tentatively. */
6565 cp_parser_parse_tentatively (parser);
6566 /* Types may not be defined in a cast. */
6567 saved_message = parser->type_definition_forbidden_message;
6568 parser->type_definition_forbidden_message
6569 = G_("types may not be defined in casts");
6570 /* Consume the `('. */
6571 cp_lexer_consume_token (parser->lexer);
6572 /* A very tricky bit is that `(struct S) { 3 }' is a
6573 compound-literal (which we permit in C++ as an extension).
6574 But, that construct is not a cast-expression -- it is a
6575 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6576 is legal; if the compound-literal were a cast-expression,
6577 you'd need an extra set of parentheses.) But, if we parse
6578 the type-id, and it happens to be a class-specifier, then we
6579 will commit to the parse at that point, because we cannot
6580 undo the action that is done when creating a new class. So,
6581 then we cannot back up and do a postfix-expression.
6583 Therefore, we scan ahead to the closing `)', and check to see
6584 if the token after the `)' is a `{'. If so, we are not
6585 looking at a cast-expression.
6587 Save tokens so that we can put them back. */
6588 cp_lexer_save_tokens (parser->lexer);
6589 /* Skip tokens until the next token is a closing parenthesis.
6590 If we find the closing `)', and the next token is a `{', then
6591 we are looking at a compound-literal. */
6593 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6594 /*consume_paren=*/true)
6595 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6596 /* Roll back the tokens we skipped. */
6597 cp_lexer_rollback_tokens (parser->lexer);
6598 /* If we were looking at a compound-literal, simulate an error
6599 so that the call to cp_parser_parse_definitely below will
6601 if (compound_literal_p)
6602 cp_parser_simulate_error (parser);
6605 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6606 parser->in_type_id_in_expr_p = true;
6607 /* Look for the type-id. */
6608 type = cp_parser_type_id (parser);
6609 /* Look for the closing `)'. */
6610 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6611 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6614 /* Restore the saved message. */
6615 parser->type_definition_forbidden_message = saved_message;
6617 /* At this point this can only be either a cast or a
6618 parenthesized ctor such as `(T ())' that looks like a cast to
6619 function returning T. */
6620 if (!cp_parser_error_occurred (parser)
6621 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6624 cp_parser_parse_definitely (parser);
6625 expr = cp_parser_cast_expression (parser,
6626 /*address_p=*/false,
6627 /*cast_p=*/true, pidk);
6629 /* Warn about old-style casts, if so requested. */
6630 if (warn_old_style_cast
6631 && !in_system_header
6632 && !VOID_TYPE_P (type)
6633 && current_lang_name != lang_name_c)
6634 warning (OPT_Wold_style_cast, "use of old-style cast");
6636 /* Only type conversions to integral or enumeration types
6637 can be used in constant-expressions. */
6638 if (!cast_valid_in_integral_constant_expression_p (type)
6639 && cp_parser_non_integral_constant_expression (parser,
6641 return error_mark_node;
6643 /* Perform the cast. */
6644 expr = build_c_cast (input_location, type, expr);
6648 cp_parser_abort_tentative_parse (parser);
6651 /* If we get here, then it's not a cast, so it must be a
6652 unary-expression. */
6653 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6656 /* Parse a binary expression of the general form:
6660 pm-expression .* cast-expression
6661 pm-expression ->* cast-expression
6663 multiplicative-expression:
6665 multiplicative-expression * pm-expression
6666 multiplicative-expression / pm-expression
6667 multiplicative-expression % pm-expression
6669 additive-expression:
6670 multiplicative-expression
6671 additive-expression + multiplicative-expression
6672 additive-expression - multiplicative-expression
6676 shift-expression << additive-expression
6677 shift-expression >> additive-expression
6679 relational-expression:
6681 relational-expression < shift-expression
6682 relational-expression > shift-expression
6683 relational-expression <= shift-expression
6684 relational-expression >= shift-expression
6688 relational-expression:
6689 relational-expression <? shift-expression
6690 relational-expression >? shift-expression
6692 equality-expression:
6693 relational-expression
6694 equality-expression == relational-expression
6695 equality-expression != relational-expression
6699 and-expression & equality-expression
6701 exclusive-or-expression:
6703 exclusive-or-expression ^ and-expression
6705 inclusive-or-expression:
6706 exclusive-or-expression
6707 inclusive-or-expression | exclusive-or-expression
6709 logical-and-expression:
6710 inclusive-or-expression
6711 logical-and-expression && inclusive-or-expression
6713 logical-or-expression:
6714 logical-and-expression
6715 logical-or-expression || logical-and-expression
6717 All these are implemented with a single function like:
6720 simple-cast-expression
6721 binary-expression <token> binary-expression
6723 CAST_P is true if this expression is the target of a cast.
6725 The binops_by_token map is used to get the tree codes for each <token> type.
6726 binary-expressions are associated according to a precedence table. */
6728 #define TOKEN_PRECEDENCE(token) \
6729 (((token->type == CPP_GREATER \
6730 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6731 && !parser->greater_than_is_operator_p) \
6732 ? PREC_NOT_OPERATOR \
6733 : binops_by_token[token->type].prec)
6736 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6737 bool no_toplevel_fold_p,
6738 enum cp_parser_prec prec,
6741 cp_parser_expression_stack stack;
6742 cp_parser_expression_stack_entry *sp = &stack[0];
6745 enum tree_code tree_type, lhs_type, rhs_type;
6746 enum cp_parser_prec new_prec, lookahead_prec;
6749 /* Parse the first expression. */
6750 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6751 lhs_type = ERROR_MARK;
6755 /* Get an operator token. */
6756 token = cp_lexer_peek_token (parser->lexer);
6758 if (warn_cxx0x_compat
6759 && token->type == CPP_RSHIFT
6760 && !parser->greater_than_is_operator_p)
6762 if (warning_at (token->location, OPT_Wc__0x_compat,
6763 "%<>>%> operator will be treated as"
6764 " two right angle brackets in C++0x"))
6765 inform (token->location,
6766 "suggest parentheses around %<>>%> expression");
6769 new_prec = TOKEN_PRECEDENCE (token);
6771 /* Popping an entry off the stack means we completed a subexpression:
6772 - either we found a token which is not an operator (`>' where it is not
6773 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6774 will happen repeatedly;
6775 - or, we found an operator which has lower priority. This is the case
6776 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6778 if (new_prec <= prec)
6787 tree_type = binops_by_token[token->type].tree_type;
6789 /* We used the operator token. */
6790 cp_lexer_consume_token (parser->lexer);
6792 /* For "false && x" or "true || x", x will never be executed;
6793 disable warnings while evaluating it. */
6794 if (tree_type == TRUTH_ANDIF_EXPR)
6795 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6796 else if (tree_type == TRUTH_ORIF_EXPR)
6797 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6799 /* Extract another operand. It may be the RHS of this expression
6800 or the LHS of a new, higher priority expression. */
6801 rhs = cp_parser_simple_cast_expression (parser);
6802 rhs_type = ERROR_MARK;
6804 /* Get another operator token. Look up its precedence to avoid
6805 building a useless (immediately popped) stack entry for common
6806 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6807 token = cp_lexer_peek_token (parser->lexer);
6808 lookahead_prec = TOKEN_PRECEDENCE (token);
6809 if (lookahead_prec > new_prec)
6811 /* ... and prepare to parse the RHS of the new, higher priority
6812 expression. Since precedence levels on the stack are
6813 monotonically increasing, we do not have to care about
6816 sp->tree_type = tree_type;
6818 sp->lhs_type = lhs_type;
6821 lhs_type = rhs_type;
6823 new_prec = lookahead_prec;
6827 lookahead_prec = new_prec;
6828 /* If the stack is not empty, we have parsed into LHS the right side
6829 (`4' in the example above) of an expression we had suspended.
6830 We can use the information on the stack to recover the LHS (`3')
6831 from the stack together with the tree code (`MULT_EXPR'), and
6832 the precedence of the higher level subexpression
6833 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6834 which will be used to actually build the additive expression. */
6837 tree_type = sp->tree_type;
6839 rhs_type = lhs_type;
6841 lhs_type = sp->lhs_type;
6844 /* Undo the disabling of warnings done above. */
6845 if (tree_type == TRUTH_ANDIF_EXPR)
6846 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6847 else if (tree_type == TRUTH_ORIF_EXPR)
6848 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6850 overloaded_p = false;
6851 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6852 ERROR_MARK for everything that is not a binary expression.
6853 This makes warn_about_parentheses miss some warnings that
6854 involve unary operators. For unary expressions we should
6855 pass the correct tree_code unless the unary expression was
6856 surrounded by parentheses.
6858 if (no_toplevel_fold_p
6859 && lookahead_prec <= prec
6861 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6862 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6864 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6865 &overloaded_p, tf_warning_or_error);
6866 lhs_type = tree_type;
6868 /* If the binary operator required the use of an overloaded operator,
6869 then this expression cannot be an integral constant-expression.
6870 An overloaded operator can be used even if both operands are
6871 otherwise permissible in an integral constant-expression if at
6872 least one of the operands is of enumeration type. */
6875 && cp_parser_non_integral_constant_expression (parser,
6877 return error_mark_node;
6884 /* Parse the `? expression : assignment-expression' part of a
6885 conditional-expression. The LOGICAL_OR_EXPR is the
6886 logical-or-expression that started the conditional-expression.
6887 Returns a representation of the entire conditional-expression.
6889 This routine is used by cp_parser_assignment_expression.
6891 ? expression : assignment-expression
6895 ? : assignment-expression */
6898 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6901 tree assignment_expr;
6902 struct cp_token *token;
6904 /* Consume the `?' token. */
6905 cp_lexer_consume_token (parser->lexer);
6906 token = cp_lexer_peek_token (parser->lexer);
6907 if (cp_parser_allow_gnu_extensions_p (parser)
6908 && token->type == CPP_COLON)
6910 pedwarn (token->location, OPT_pedantic,
6911 "ISO C++ does not allow ?: with omitted middle operand");
6912 /* Implicit true clause. */
6914 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6915 warn_for_omitted_condop (token->location, logical_or_expr);
6919 /* Parse the expression. */
6920 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6921 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6922 c_inhibit_evaluation_warnings +=
6923 ((logical_or_expr == truthvalue_true_node)
6924 - (logical_or_expr == truthvalue_false_node));
6927 /* The next token should be a `:'. */
6928 cp_parser_require (parser, CPP_COLON, RT_COLON);
6929 /* Parse the assignment-expression. */
6930 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6931 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6933 /* Build the conditional-expression. */
6934 return build_x_conditional_expr (logical_or_expr,
6937 tf_warning_or_error);
6940 /* Parse an assignment-expression.
6942 assignment-expression:
6943 conditional-expression
6944 logical-or-expression assignment-operator assignment_expression
6947 CAST_P is true if this expression is the target of a cast.
6949 Returns a representation for the expression. */
6952 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6957 /* If the next token is the `throw' keyword, then we're looking at
6958 a throw-expression. */
6959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6960 expr = cp_parser_throw_expression (parser);
6961 /* Otherwise, it must be that we are looking at a
6962 logical-or-expression. */
6965 /* Parse the binary expressions (logical-or-expression). */
6966 expr = cp_parser_binary_expression (parser, cast_p, false,
6967 PREC_NOT_OPERATOR, pidk);
6968 /* If the next token is a `?' then we're actually looking at a
6969 conditional-expression. */
6970 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6971 return cp_parser_question_colon_clause (parser, expr);
6974 enum tree_code assignment_operator;
6976 /* If it's an assignment-operator, we're using the second
6979 = cp_parser_assignment_operator_opt (parser);
6980 if (assignment_operator != ERROR_MARK)
6982 bool non_constant_p;
6984 /* Parse the right-hand side of the assignment. */
6985 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6987 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6988 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6990 /* An assignment may not appear in a
6991 constant-expression. */
6992 if (cp_parser_non_integral_constant_expression (parser,
6994 return error_mark_node;
6995 /* Build the assignment expression. */
6996 expr = build_x_modify_expr (expr,
6997 assignment_operator,
6999 tf_warning_or_error);
7007 /* Parse an (optional) assignment-operator.
7009 assignment-operator: one of
7010 = *= /= %= += -= >>= <<= &= ^= |=
7014 assignment-operator: one of
7017 If the next token is an assignment operator, the corresponding tree
7018 code is returned, and the token is consumed. For example, for
7019 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7020 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7021 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7022 operator, ERROR_MARK is returned. */
7024 static enum tree_code
7025 cp_parser_assignment_operator_opt (cp_parser* parser)
7030 /* Peek at the next token. */
7031 token = cp_lexer_peek_token (parser->lexer);
7033 switch (token->type)
7044 op = TRUNC_DIV_EXPR;
7048 op = TRUNC_MOD_EXPR;
7080 /* Nothing else is an assignment operator. */
7084 /* If it was an assignment operator, consume it. */
7085 if (op != ERROR_MARK)
7086 cp_lexer_consume_token (parser->lexer);
7091 /* Parse an expression.
7094 assignment-expression
7095 expression , assignment-expression
7097 CAST_P is true if this expression is the target of a cast.
7099 Returns a representation of the expression. */
7102 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7104 tree expression = NULL_TREE;
7108 tree assignment_expression;
7110 /* Parse the next assignment-expression. */
7111 assignment_expression
7112 = cp_parser_assignment_expression (parser, cast_p, pidk);
7113 /* If this is the first assignment-expression, we can just
7116 expression = assignment_expression;
7118 expression = build_x_compound_expr (expression,
7119 assignment_expression,
7120 tf_warning_or_error);
7121 /* If the next token is not a comma, then we are done with the
7123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7125 /* Consume the `,'. */
7126 cp_lexer_consume_token (parser->lexer);
7127 /* A comma operator cannot appear in a constant-expression. */
7128 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7129 expression = error_mark_node;
7135 /* Parse a constant-expression.
7137 constant-expression:
7138 conditional-expression
7140 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7141 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7142 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7143 is false, NON_CONSTANT_P should be NULL. */
7146 cp_parser_constant_expression (cp_parser* parser,
7147 bool allow_non_constant_p,
7148 bool *non_constant_p)
7150 bool saved_integral_constant_expression_p;
7151 bool saved_allow_non_integral_constant_expression_p;
7152 bool saved_non_integral_constant_expression_p;
7155 /* It might seem that we could simply parse the
7156 conditional-expression, and then check to see if it were
7157 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7158 one that the compiler can figure out is constant, possibly after
7159 doing some simplifications or optimizations. The standard has a
7160 precise definition of constant-expression, and we must honor
7161 that, even though it is somewhat more restrictive.
7167 is not a legal declaration, because `(2, 3)' is not a
7168 constant-expression. The `,' operator is forbidden in a
7169 constant-expression. However, GCC's constant-folding machinery
7170 will fold this operation to an INTEGER_CST for `3'. */
7172 /* Save the old settings. */
7173 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7174 saved_allow_non_integral_constant_expression_p
7175 = parser->allow_non_integral_constant_expression_p;
7176 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7177 /* We are now parsing a constant-expression. */
7178 parser->integral_constant_expression_p = true;
7179 parser->allow_non_integral_constant_expression_p
7180 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7181 parser->non_integral_constant_expression_p = false;
7182 /* Although the grammar says "conditional-expression", we parse an
7183 "assignment-expression", which also permits "throw-expression"
7184 and the use of assignment operators. In the case that
7185 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7186 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7187 actually essential that we look for an assignment-expression.
7188 For example, cp_parser_initializer_clauses uses this function to
7189 determine whether a particular assignment-expression is in fact
7191 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7192 /* Restore the old settings. */
7193 parser->integral_constant_expression_p
7194 = saved_integral_constant_expression_p;
7195 parser->allow_non_integral_constant_expression_p
7196 = saved_allow_non_integral_constant_expression_p;
7197 if (allow_non_constant_p)
7198 *non_constant_p = parser->non_integral_constant_expression_p;
7199 else if (parser->non_integral_constant_expression_p
7200 && cxx_dialect < cxx0x)
7201 expression = error_mark_node;
7202 parser->non_integral_constant_expression_p
7203 = saved_non_integral_constant_expression_p;
7208 /* Parse __builtin_offsetof.
7210 offsetof-expression:
7211 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7213 offsetof-member-designator:
7215 | offsetof-member-designator "." id-expression
7216 | offsetof-member-designator "[" expression "]"
7217 | offsetof-member-designator "->" id-expression */
7220 cp_parser_builtin_offsetof (cp_parser *parser)
7222 int save_ice_p, save_non_ice_p;
7227 /* We're about to accept non-integral-constant things, but will
7228 definitely yield an integral constant expression. Save and
7229 restore these values around our local parsing. */
7230 save_ice_p = parser->integral_constant_expression_p;
7231 save_non_ice_p = parser->non_integral_constant_expression_p;
7233 /* Consume the "__builtin_offsetof" token. */
7234 cp_lexer_consume_token (parser->lexer);
7235 /* Consume the opening `('. */
7236 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7237 /* Parse the type-id. */
7238 type = cp_parser_type_id (parser);
7239 /* Look for the `,'. */
7240 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7241 token = cp_lexer_peek_token (parser->lexer);
7243 /* Build the (type *)null that begins the traditional offsetof macro. */
7244 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7245 tf_warning_or_error);
7247 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7248 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7249 true, &dummy, token->location);
7252 token = cp_lexer_peek_token (parser->lexer);
7253 switch (token->type)
7255 case CPP_OPEN_SQUARE:
7256 /* offsetof-member-designator "[" expression "]" */
7257 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7261 /* offsetof-member-designator "->" identifier */
7262 expr = grok_array_decl (expr, integer_zero_node);
7266 /* offsetof-member-designator "." identifier */
7267 cp_lexer_consume_token (parser->lexer);
7268 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7273 case CPP_CLOSE_PAREN:
7274 /* Consume the ")" token. */
7275 cp_lexer_consume_token (parser->lexer);
7279 /* Error. We know the following require will fail, but
7280 that gives the proper error message. */
7281 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7282 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7283 expr = error_mark_node;
7289 /* If we're processing a template, we can't finish the semantics yet.
7290 Otherwise we can fold the entire expression now. */
7291 if (processing_template_decl)
7292 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7294 expr = finish_offsetof (expr);
7297 parser->integral_constant_expression_p = save_ice_p;
7298 parser->non_integral_constant_expression_p = save_non_ice_p;
7303 /* Parse a trait expression. */
7306 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7309 tree type1, type2 = NULL_TREE;
7310 bool binary = false;
7311 cp_decl_specifier_seq decl_specs;
7315 case RID_HAS_NOTHROW_ASSIGN:
7316 kind = CPTK_HAS_NOTHROW_ASSIGN;
7318 case RID_HAS_NOTHROW_CONSTRUCTOR:
7319 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7321 case RID_HAS_NOTHROW_COPY:
7322 kind = CPTK_HAS_NOTHROW_COPY;
7324 case RID_HAS_TRIVIAL_ASSIGN:
7325 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7327 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7328 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7330 case RID_HAS_TRIVIAL_COPY:
7331 kind = CPTK_HAS_TRIVIAL_COPY;
7333 case RID_HAS_TRIVIAL_DESTRUCTOR:
7334 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7336 case RID_HAS_VIRTUAL_DESTRUCTOR:
7337 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7339 case RID_IS_ABSTRACT:
7340 kind = CPTK_IS_ABSTRACT;
7342 case RID_IS_BASE_OF:
7343 kind = CPTK_IS_BASE_OF;
7347 kind = CPTK_IS_CLASS;
7349 case RID_IS_CONVERTIBLE_TO:
7350 kind = CPTK_IS_CONVERTIBLE_TO;
7354 kind = CPTK_IS_EMPTY;
7357 kind = CPTK_IS_ENUM;
7362 case RID_IS_POLYMORPHIC:
7363 kind = CPTK_IS_POLYMORPHIC;
7365 case RID_IS_STD_LAYOUT:
7366 kind = CPTK_IS_STD_LAYOUT;
7368 case RID_IS_TRIVIAL:
7369 kind = CPTK_IS_TRIVIAL;
7372 kind = CPTK_IS_UNION;
7374 case RID_IS_LITERAL_TYPE:
7375 kind = CPTK_IS_LITERAL_TYPE;
7381 /* Consume the token. */
7382 cp_lexer_consume_token (parser->lexer);
7384 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7386 type1 = cp_parser_type_id (parser);
7388 if (type1 == error_mark_node)
7389 return error_mark_node;
7391 /* Build a trivial decl-specifier-seq. */
7392 clear_decl_specs (&decl_specs);
7393 decl_specs.type = type1;
7395 /* Call grokdeclarator to figure out what type this is. */
7396 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7397 /*initialized=*/0, /*attrlist=*/NULL);
7401 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7403 type2 = cp_parser_type_id (parser);
7405 if (type2 == error_mark_node)
7406 return error_mark_node;
7408 /* Build a trivial decl-specifier-seq. */
7409 clear_decl_specs (&decl_specs);
7410 decl_specs.type = type2;
7412 /* Call grokdeclarator to figure out what type this is. */
7413 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7414 /*initialized=*/0, /*attrlist=*/NULL);
7417 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7419 /* Complete the trait expression, which may mean either processing
7420 the trait expr now or saving it for template instantiation. */
7421 return finish_trait_expr (kind, type1, type2);
7424 /* Lambdas that appear in variable initializer or default argument scope
7425 get that in their mangling, so we need to record it. We might as well
7426 use the count for function and namespace scopes as well. */
7427 static GTY(()) tree lambda_scope;
7428 static GTY(()) int lambda_count;
7429 typedef struct GTY(()) tree_int
7434 DEF_VEC_O(tree_int);
7435 DEF_VEC_ALLOC_O(tree_int,gc);
7436 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7439 start_lambda_scope (tree decl)
7443 /* Once we're inside a function, we ignore other scopes and just push
7444 the function again so that popping works properly. */
7445 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7446 decl = current_function_decl;
7447 ti.t = lambda_scope;
7448 ti.i = lambda_count;
7449 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7450 if (lambda_scope != decl)
7452 /* Don't reset the count if we're still in the same function. */
7453 lambda_scope = decl;
7459 record_lambda_scope (tree lambda)
7461 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7462 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7466 finish_lambda_scope (void)
7468 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7469 if (lambda_scope != p->t)
7471 lambda_scope = p->t;
7472 lambda_count = p->i;
7474 VEC_pop (tree_int, lambda_scope_stack);
7477 /* Parse a lambda expression.
7480 lambda-introducer lambda-declarator [opt] compound-statement
7482 Returns a representation of the expression. */
7485 cp_parser_lambda_expression (cp_parser* parser)
7487 tree lambda_expr = build_lambda_expr ();
7490 LAMBDA_EXPR_LOCATION (lambda_expr)
7491 = cp_lexer_peek_token (parser->lexer)->location;
7493 if (cp_unevaluated_operand)
7494 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7495 "lambda-expression in unevaluated context");
7497 /* We may be in the middle of deferred access check. Disable
7499 push_deferring_access_checks (dk_no_deferred);
7501 cp_parser_lambda_introducer (parser, lambda_expr);
7503 type = begin_lambda_type (lambda_expr);
7505 record_lambda_scope (lambda_expr);
7507 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7508 determine_visibility (TYPE_NAME (type));
7510 /* Now that we've started the type, add the capture fields for any
7511 explicit captures. */
7512 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7515 /* Inside the class, surrounding template-parameter-lists do not apply. */
7516 unsigned int saved_num_template_parameter_lists
7517 = parser->num_template_parameter_lists;
7519 parser->num_template_parameter_lists = 0;
7521 /* By virtue of defining a local class, a lambda expression has access to
7522 the private variables of enclosing classes. */
7524 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7526 cp_parser_lambda_body (parser, lambda_expr);
7528 /* The capture list was built up in reverse order; fix that now. */
7530 tree newlist = NULL_TREE;
7533 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7536 tree field = TREE_PURPOSE (elt);
7539 next = TREE_CHAIN (elt);
7540 TREE_CHAIN (elt) = newlist;
7543 /* Also add __ to the beginning of the field name so that code
7544 outside the lambda body can't see the captured name. We could
7545 just remove the name entirely, but this is more useful for
7547 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7548 /* The 'this' capture already starts with __. */
7551 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7552 buf[1] = buf[0] = '_';
7553 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7554 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7555 DECL_NAME (field) = get_identifier (buf);
7557 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7560 maybe_add_lambda_conv_op (type);
7562 type = finish_struct (type, /*attributes=*/NULL_TREE);
7564 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7567 pop_deferring_access_checks ();
7569 return build_lambda_object (lambda_expr);
7572 /* Parse the beginning of a lambda expression.
7575 [ lambda-capture [opt] ]
7577 LAMBDA_EXPR is the current representation of the lambda expression. */
7580 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7582 /* Need commas after the first capture. */
7585 /* Eat the leading `['. */
7586 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7588 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7589 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7590 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7591 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7592 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7593 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7595 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7597 cp_lexer_consume_token (parser->lexer);
7601 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7603 cp_token* capture_token;
7605 tree capture_init_expr;
7606 cp_id_kind idk = CP_ID_KIND_NONE;
7607 bool explicit_init_p = false;
7609 enum capture_kind_type
7614 enum capture_kind_type capture_kind = BY_COPY;
7616 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7618 error ("expected end of capture-list");
7625 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7627 /* Possibly capture `this'. */
7628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7630 cp_lexer_consume_token (parser->lexer);
7631 add_capture (lambda_expr,
7632 /*id=*/get_identifier ("__this"),
7633 /*initializer=*/finish_this_expr(),
7634 /*by_reference_p=*/false,
7639 /* Remember whether we want to capture as a reference or not. */
7640 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7642 capture_kind = BY_REFERENCE;
7643 cp_lexer_consume_token (parser->lexer);
7646 /* Get the identifier. */
7647 capture_token = cp_lexer_peek_token (parser->lexer);
7648 capture_id = cp_parser_identifier (parser);
7650 if (capture_id == error_mark_node)
7651 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7652 delimiters, but I modified this to stop on unnested ']' as well. It
7653 was already changed to stop on unnested '}', so the
7654 "closing_parenthesis" name is no more misleading with my change. */
7656 cp_parser_skip_to_closing_parenthesis (parser,
7657 /*recovering=*/true,
7659 /*consume_paren=*/true);
7663 /* Find the initializer for this capture. */
7664 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7666 /* An explicit expression exists. */
7667 cp_lexer_consume_token (parser->lexer);
7668 pedwarn (input_location, OPT_pedantic,
7669 "ISO C++ does not allow initializers "
7670 "in lambda expression capture lists");
7671 capture_init_expr = cp_parser_assignment_expression (parser,
7674 explicit_init_p = true;
7678 const char* error_msg;
7680 /* Turn the identifier into an id-expression. */
7682 = cp_parser_lookup_name
7686 /*is_template=*/false,
7687 /*is_namespace=*/false,
7688 /*check_dependency=*/true,
7689 /*ambiguous_decls=*/NULL,
7690 capture_token->location);
7693 = finish_id_expression
7698 /*integral_constant_expression_p=*/false,
7699 /*allow_non_integral_constant_expression_p=*/false,
7700 /*non_integral_constant_expression_p=*/NULL,
7701 /*template_p=*/false,
7703 /*address_p=*/false,
7704 /*template_arg_p=*/false,
7706 capture_token->location);
7709 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7711 = unqualified_name_lookup_error (capture_init_expr);
7713 add_capture (lambda_expr,
7716 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7720 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7723 /* Parse the (optional) middle of a lambda expression.
7726 ( parameter-declaration-clause [opt] )
7727 attribute-specifier [opt]
7729 exception-specification [opt]
7730 lambda-return-type-clause [opt]
7732 LAMBDA_EXPR is the current representation of the lambda expression. */
7735 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7737 /* 5.1.1.4 of the standard says:
7738 If a lambda-expression does not include a lambda-declarator, it is as if
7739 the lambda-declarator were ().
7740 This means an empty parameter list, no attributes, and no exception
7742 tree param_list = void_list_node;
7743 tree attributes = NULL_TREE;
7744 tree exception_spec = NULL_TREE;
7747 /* The lambda-declarator is optional, but must begin with an opening
7748 parenthesis if present. */
7749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7751 cp_lexer_consume_token (parser->lexer);
7753 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7755 /* Parse parameters. */
7756 param_list = cp_parser_parameter_declaration_clause (parser);
7758 /* Default arguments shall not be specified in the
7759 parameter-declaration-clause of a lambda-declarator. */
7760 for (t = param_list; t; t = TREE_CHAIN (t))
7761 if (TREE_PURPOSE (t))
7762 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7763 "default argument specified for lambda parameter");
7765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7767 attributes = cp_parser_attributes_opt (parser);
7769 /* Parse optional `mutable' keyword. */
7770 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7772 cp_lexer_consume_token (parser->lexer);
7773 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7776 /* Parse optional exception specification. */
7777 exception_spec = cp_parser_exception_specification_opt (parser);
7779 /* Parse optional trailing return type. */
7780 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7782 cp_lexer_consume_token (parser->lexer);
7783 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7786 /* The function parameters must be in scope all the way until after the
7787 trailing-return-type in case of decltype. */
7788 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7789 pop_binding (DECL_NAME (t), t);
7794 /* Create the function call operator.
7796 Messing with declarators like this is no uglier than building up the
7797 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7800 cp_decl_specifier_seq return_type_specs;
7801 cp_declarator* declarator;
7806 clear_decl_specs (&return_type_specs);
7807 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7808 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7810 /* Maybe we will deduce the return type later, but we can use void
7811 as a placeholder return type anyways. */
7812 return_type_specs.type = void_type_node;
7814 p = obstack_alloc (&declarator_obstack, 0);
7816 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7819 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7820 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7821 declarator = make_call_declarator (declarator, param_list, quals,
7823 /*late_return_type=*/NULL_TREE);
7824 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7826 fco = grokmethod (&return_type_specs,
7829 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7830 DECL_ARTIFICIAL (fco) = 1;
7832 finish_member_declaration (fco);
7834 obstack_free (&declarator_obstack, p);
7838 /* Parse the body of a lambda expression, which is simply
7842 but which requires special handling.
7843 LAMBDA_EXPR is the current representation of the lambda expression. */
7846 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7848 bool nested = (current_function_decl != NULL_TREE);
7850 push_function_context ();
7852 /* Finish the function call operator
7854 + late_parsing_for_member
7855 + function_definition_after_declarator
7856 + ctor_initializer_opt_and_function_body */
7858 tree fco = lambda_function (lambda_expr);
7862 /* Let the front end know that we are going to be defining this
7864 start_preparsed_function (fco,
7866 SF_PRE_PARSED | SF_INCLASS_INLINE);
7868 start_lambda_scope (fco);
7869 body = begin_function_body ();
7871 /* 5.1.1.4 of the standard says:
7872 If a lambda-expression does not include a trailing-return-type, it
7873 is as if the trailing-return-type denotes the following type:
7874 * if the compound-statement is of the form
7875 { return attribute-specifier [opt] expression ; }
7876 the type of the returned expression after lvalue-to-rvalue
7877 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7878 (_conv.array_ 4.2), and function-to-pointer conversion
7880 * otherwise, void. */
7882 /* In a lambda that has neither a lambda-return-type-clause
7883 nor a deducible form, errors should be reported for return statements
7884 in the body. Since we used void as the placeholder return type, parsing
7885 the body as usual will give such desired behavior. */
7886 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7887 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7888 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7889 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7892 tree expr = NULL_TREE;
7893 cp_id_kind idk = CP_ID_KIND_NONE;
7895 /* Parse tentatively in case there's more after the initial return
7897 cp_parser_parse_tentatively (parser);
7899 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7900 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7902 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7905 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7907 if (cp_parser_parse_definitely (parser))
7909 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7911 compound_stmt = begin_compound_stmt (0);
7912 /* Will get error here if type not deduced yet. */
7913 finish_return_stmt (expr);
7914 finish_compound_stmt (compound_stmt);
7922 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7923 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7924 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7925 cp_parser_compound_stmt does not pass it. */
7926 cp_parser_function_body (parser);
7927 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7930 finish_function_body (body);
7931 finish_lambda_scope ();
7933 /* Finish the function and generate code for it if necessary. */
7934 expand_or_defer_fn (finish_function (/*inline*/2));
7938 pop_function_context();
7941 /* Statements [gram.stmt.stmt] */
7943 /* Parse a statement.
7947 expression-statement
7952 declaration-statement
7955 IN_COMPOUND is true when the statement is nested inside a
7956 cp_parser_compound_statement; this matters for certain pragmas.
7958 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7959 is a (possibly labeled) if statement which is not enclosed in braces
7960 and has an else clause. This is used to implement -Wparentheses. */
7963 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7964 bool in_compound, bool *if_p)
7968 location_t statement_location;
7973 /* There is no statement yet. */
7974 statement = NULL_TREE;
7975 /* Peek at the next token. */
7976 token = cp_lexer_peek_token (parser->lexer);
7977 /* Remember the location of the first token in the statement. */
7978 statement_location = token->location;
7979 /* If this is a keyword, then that will often determine what kind of
7980 statement we have. */
7981 if (token->type == CPP_KEYWORD)
7983 enum rid keyword = token->keyword;
7989 /* Looks like a labeled-statement with a case label.
7990 Parse the label, and then use tail recursion to parse
7992 cp_parser_label_for_labeled_statement (parser);
7997 statement = cp_parser_selection_statement (parser, if_p);
8003 statement = cp_parser_iteration_statement (parser);
8010 statement = cp_parser_jump_statement (parser);
8013 /* Objective-C++ exception-handling constructs. */
8016 case RID_AT_FINALLY:
8017 case RID_AT_SYNCHRONIZED:
8019 statement = cp_parser_objc_statement (parser);
8023 statement = cp_parser_try_block (parser);
8027 /* This must be a namespace alias definition. */
8028 cp_parser_declaration_statement (parser);
8032 /* It might be a keyword like `int' that can start a
8033 declaration-statement. */
8037 else if (token->type == CPP_NAME)
8039 /* If the next token is a `:', then we are looking at a
8040 labeled-statement. */
8041 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8042 if (token->type == CPP_COLON)
8044 /* Looks like a labeled-statement with an ordinary label.
8045 Parse the label, and then use tail recursion to parse
8047 cp_parser_label_for_labeled_statement (parser);
8051 /* Anything that starts with a `{' must be a compound-statement. */
8052 else if (token->type == CPP_OPEN_BRACE)
8053 statement = cp_parser_compound_statement (parser, NULL, false);
8054 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8055 a statement all its own. */
8056 else if (token->type == CPP_PRAGMA)
8058 /* Only certain OpenMP pragmas are attached to statements, and thus
8059 are considered statements themselves. All others are not. In
8060 the context of a compound, accept the pragma as a "statement" and
8061 return so that we can check for a close brace. Otherwise we
8062 require a real statement and must go back and read one. */
8064 cp_parser_pragma (parser, pragma_compound);
8065 else if (!cp_parser_pragma (parser, pragma_stmt))
8069 else if (token->type == CPP_EOF)
8071 cp_parser_error (parser, "expected statement");
8075 /* Everything else must be a declaration-statement or an
8076 expression-statement. Try for the declaration-statement
8077 first, unless we are looking at a `;', in which case we know that
8078 we have an expression-statement. */
8081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8083 cp_parser_parse_tentatively (parser);
8084 /* Try to parse the declaration-statement. */
8085 cp_parser_declaration_statement (parser);
8086 /* If that worked, we're done. */
8087 if (cp_parser_parse_definitely (parser))
8090 /* Look for an expression-statement instead. */
8091 statement = cp_parser_expression_statement (parser, in_statement_expr);
8094 /* Set the line number for the statement. */
8095 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8096 SET_EXPR_LOCATION (statement, statement_location);
8099 /* Parse the label for a labeled-statement, i.e.
8102 case constant-expression :
8106 case constant-expression ... constant-expression : statement
8108 When a label is parsed without errors, the label is added to the
8109 parse tree by the finish_* functions, so this function doesn't
8110 have to return the label. */
8113 cp_parser_label_for_labeled_statement (cp_parser* parser)
8116 tree label = NULL_TREE;
8118 /* The next token should be an identifier. */
8119 token = cp_lexer_peek_token (parser->lexer);
8120 if (token->type != CPP_NAME
8121 && token->type != CPP_KEYWORD)
8123 cp_parser_error (parser, "expected labeled-statement");
8127 switch (token->keyword)
8134 /* Consume the `case' token. */
8135 cp_lexer_consume_token (parser->lexer);
8136 /* Parse the constant-expression. */
8137 expr = cp_parser_constant_expression (parser,
8138 /*allow_non_constant_p=*/false,
8141 ellipsis = cp_lexer_peek_token (parser->lexer);
8142 if (ellipsis->type == CPP_ELLIPSIS)
8144 /* Consume the `...' token. */
8145 cp_lexer_consume_token (parser->lexer);
8147 cp_parser_constant_expression (parser,
8148 /*allow_non_constant_p=*/false,
8150 /* We don't need to emit warnings here, as the common code
8151 will do this for us. */
8154 expr_hi = NULL_TREE;
8156 if (parser->in_switch_statement_p)
8157 finish_case_label (token->location, expr, expr_hi);
8159 error_at (token->location,
8160 "case label %qE not within a switch statement",
8166 /* Consume the `default' token. */
8167 cp_lexer_consume_token (parser->lexer);
8169 if (parser->in_switch_statement_p)
8170 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8172 error_at (token->location, "case label not within a switch statement");
8176 /* Anything else must be an ordinary label. */
8177 label = finish_label_stmt (cp_parser_identifier (parser));
8181 /* Require the `:' token. */
8182 cp_parser_require (parser, CPP_COLON, RT_COLON);
8184 /* An ordinary label may optionally be followed by attributes.
8185 However, this is only permitted if the attributes are then
8186 followed by a semicolon. This is because, for backward
8187 compatibility, when parsing
8188 lab: __attribute__ ((unused)) int i;
8189 we want the attribute to attach to "i", not "lab". */
8190 if (label != NULL_TREE
8191 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8195 cp_parser_parse_tentatively (parser);
8196 attrs = cp_parser_attributes_opt (parser);
8197 if (attrs == NULL_TREE
8198 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8199 cp_parser_abort_tentative_parse (parser);
8200 else if (!cp_parser_parse_definitely (parser))
8203 cplus_decl_attributes (&label, attrs, 0);
8207 /* Parse an expression-statement.
8209 expression-statement:
8212 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8213 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8214 indicates whether this expression-statement is part of an
8215 expression statement. */
8218 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8220 tree statement = NULL_TREE;
8221 cp_token *token = cp_lexer_peek_token (parser->lexer);
8223 /* If the next token is a ';', then there is no expression
8225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8226 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8228 /* Give a helpful message for "A<T>::type t;" and the like. */
8229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8230 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8232 if (TREE_CODE (statement) == SCOPE_REF)
8233 error_at (token->location, "need %<typename%> before %qE because "
8234 "%qT is a dependent scope",
8235 statement, TREE_OPERAND (statement, 0));
8236 else if (is_overloaded_fn (statement)
8237 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8240 tree fn = get_first_fn (statement);
8241 error_at (token->location,
8242 "%<%T::%D%> names the constructor, not the type",
8243 DECL_CONTEXT (fn), DECL_NAME (fn));
8247 /* Consume the final `;'. */
8248 cp_parser_consume_semicolon_at_end_of_statement (parser);
8250 if (in_statement_expr
8251 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8252 /* This is the final expression statement of a statement
8254 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8256 statement = finish_expr_stmt (statement);
8263 /* Parse a compound-statement.
8266 { statement-seq [opt] }
8271 { label-declaration-seq [opt] statement-seq [opt] }
8273 label-declaration-seq:
8275 label-declaration-seq label-declaration
8277 Returns a tree representing the statement. */
8280 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8285 /* Consume the `{'. */
8286 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8287 return error_mark_node;
8288 /* Begin the compound-statement. */
8289 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8290 /* If the next keyword is `__label__' we have a label declaration. */
8291 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8292 cp_parser_label_declaration (parser);
8293 /* Parse an (optional) statement-seq. */
8294 cp_parser_statement_seq_opt (parser, in_statement_expr);
8295 /* Finish the compound-statement. */
8296 finish_compound_stmt (compound_stmt);
8297 /* Consume the `}'. */
8298 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8300 return compound_stmt;
8303 /* Parse an (optional) statement-seq.
8307 statement-seq [opt] statement */
8310 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8312 /* Scan statements until there aren't any more. */
8315 cp_token *token = cp_lexer_peek_token (parser->lexer);
8317 /* If we are looking at a `}', then we have run out of
8318 statements; the same is true if we have reached the end
8319 of file, or have stumbled upon a stray '@end'. */
8320 if (token->type == CPP_CLOSE_BRACE
8321 || token->type == CPP_EOF
8322 || token->type == CPP_PRAGMA_EOL
8323 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8326 /* If we are in a compound statement and find 'else' then
8327 something went wrong. */
8328 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8330 if (parser->in_statement & IN_IF_STMT)
8334 token = cp_lexer_consume_token (parser->lexer);
8335 error_at (token->location, "%<else%> without a previous %<if%>");
8339 /* Parse the statement. */
8340 cp_parser_statement (parser, in_statement_expr, true, NULL);
8344 /* Parse a selection-statement.
8346 selection-statement:
8347 if ( condition ) statement
8348 if ( condition ) statement else statement
8349 switch ( condition ) statement
8351 Returns the new IF_STMT or SWITCH_STMT.
8353 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8354 is a (possibly labeled) if statement which is not enclosed in
8355 braces and has an else clause. This is used to implement
8359 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8367 /* Peek at the next token. */
8368 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8370 /* See what kind of keyword it is. */
8371 keyword = token->keyword;
8380 /* Look for the `('. */
8381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8383 cp_parser_skip_to_end_of_statement (parser);
8384 return error_mark_node;
8387 /* Begin the selection-statement. */
8388 if (keyword == RID_IF)
8389 statement = begin_if_stmt ();
8391 statement = begin_switch_stmt ();
8393 /* Parse the condition. */
8394 condition = cp_parser_condition (parser);
8395 /* Look for the `)'. */
8396 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8397 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8398 /*consume_paren=*/true);
8400 if (keyword == RID_IF)
8403 unsigned char in_statement;
8405 /* Add the condition. */
8406 finish_if_stmt_cond (condition, statement);
8408 /* Parse the then-clause. */
8409 in_statement = parser->in_statement;
8410 parser->in_statement |= IN_IF_STMT;
8411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8413 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8414 add_stmt (build_empty_stmt (loc));
8415 cp_lexer_consume_token (parser->lexer);
8416 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8417 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8418 "empty body in an %<if%> statement");
8422 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8423 parser->in_statement = in_statement;
8425 finish_then_clause (statement);
8427 /* If the next token is `else', parse the else-clause. */
8428 if (cp_lexer_next_token_is_keyword (parser->lexer,
8431 /* Consume the `else' keyword. */
8432 cp_lexer_consume_token (parser->lexer);
8433 begin_else_clause (statement);
8434 /* Parse the else-clause. */
8435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8438 loc = cp_lexer_peek_token (parser->lexer)->location;
8440 OPT_Wempty_body, "suggest braces around "
8441 "empty body in an %<else%> statement");
8442 add_stmt (build_empty_stmt (loc));
8443 cp_lexer_consume_token (parser->lexer);
8446 cp_parser_implicitly_scoped_statement (parser, NULL);
8448 finish_else_clause (statement);
8450 /* If we are currently parsing a then-clause, then
8451 IF_P will not be NULL. We set it to true to
8452 indicate that this if statement has an else clause.
8453 This may trigger the Wparentheses warning below
8454 when we get back up to the parent if statement. */
8460 /* This if statement does not have an else clause. If
8461 NESTED_IF is true, then the then-clause is an if
8462 statement which does have an else clause. We warn
8463 about the potential ambiguity. */
8465 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8466 "suggest explicit braces to avoid ambiguous"
8470 /* Now we're all done with the if-statement. */
8471 finish_if_stmt (statement);
8475 bool in_switch_statement_p;
8476 unsigned char in_statement;
8478 /* Add the condition. */
8479 finish_switch_cond (condition, statement);
8481 /* Parse the body of the switch-statement. */
8482 in_switch_statement_p = parser->in_switch_statement_p;
8483 in_statement = parser->in_statement;
8484 parser->in_switch_statement_p = true;
8485 parser->in_statement |= IN_SWITCH_STMT;
8486 cp_parser_implicitly_scoped_statement (parser, NULL);
8487 parser->in_switch_statement_p = in_switch_statement_p;
8488 parser->in_statement = in_statement;
8490 /* Now we're all done with the switch-statement. */
8491 finish_switch_stmt (statement);
8499 cp_parser_error (parser, "expected selection-statement");
8500 return error_mark_node;
8504 /* Parse a condition.
8508 type-specifier-seq declarator = initializer-clause
8509 type-specifier-seq declarator braced-init-list
8514 type-specifier-seq declarator asm-specification [opt]
8515 attributes [opt] = assignment-expression
8517 Returns the expression that should be tested. */
8520 cp_parser_condition (cp_parser* parser)
8522 cp_decl_specifier_seq type_specifiers;
8523 const char *saved_message;
8524 int declares_class_or_enum;
8526 /* Try the declaration first. */
8527 cp_parser_parse_tentatively (parser);
8528 /* New types are not allowed in the type-specifier-seq for a
8530 saved_message = parser->type_definition_forbidden_message;
8531 parser->type_definition_forbidden_message
8532 = G_("types may not be defined in conditions");
8533 /* Parse the type-specifier-seq. */
8534 cp_parser_decl_specifier_seq (parser,
8535 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8537 &declares_class_or_enum);
8538 /* Restore the saved message. */
8539 parser->type_definition_forbidden_message = saved_message;
8540 /* If all is well, we might be looking at a declaration. */
8541 if (!cp_parser_error_occurred (parser))
8544 tree asm_specification;
8546 cp_declarator *declarator;
8547 tree initializer = NULL_TREE;
8549 /* Parse the declarator. */
8550 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8551 /*ctor_dtor_or_conv_p=*/NULL,
8552 /*parenthesized_p=*/NULL,
8553 /*member_p=*/false);
8554 /* Parse the attributes. */
8555 attributes = cp_parser_attributes_opt (parser);
8556 /* Parse the asm-specification. */
8557 asm_specification = cp_parser_asm_specification_opt (parser);
8558 /* If the next token is not an `=' or '{', then we might still be
8559 looking at an expression. For example:
8563 looks like a decl-specifier-seq and a declarator -- but then
8564 there is no `=', so this is an expression. */
8565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8566 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8567 cp_parser_simulate_error (parser);
8569 /* If we did see an `=' or '{', then we are looking at a declaration
8571 if (cp_parser_parse_definitely (parser))
8574 bool non_constant_p;
8575 bool flags = LOOKUP_ONLYCONVERTING;
8577 /* Create the declaration. */
8578 decl = start_decl (declarator, &type_specifiers,
8579 /*initialized_p=*/true,
8580 attributes, /*prefix_attributes=*/NULL_TREE,
8583 /* Parse the initializer. */
8584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8586 initializer = cp_parser_braced_list (parser, &non_constant_p);
8587 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8592 /* Consume the `='. */
8593 cp_parser_require (parser, CPP_EQ, RT_EQ);
8594 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8596 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8599 if (!non_constant_p)
8600 initializer = fold_non_dependent_expr (initializer);
8602 /* Process the initializer. */
8603 cp_finish_decl (decl,
8604 initializer, !non_constant_p,
8609 pop_scope (pushed_scope);
8611 return convert_from_reference (decl);
8614 /* If we didn't even get past the declarator successfully, we are
8615 definitely not looking at a declaration. */
8617 cp_parser_abort_tentative_parse (parser);
8619 /* Otherwise, we are looking at an expression. */
8620 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8623 /* Parses a traditional for-statement until the closing ')', not included. */
8626 cp_parser_c_for (cp_parser *parser)
8628 /* Normal for loop */
8630 tree condition = NULL_TREE;
8631 tree expression = NULL_TREE;
8633 /* Begin the for-statement. */
8634 stmt = begin_for_stmt ();
8636 /* Parse the initialization. */
8637 cp_parser_for_init_statement (parser);
8638 finish_for_init_stmt (stmt);
8640 /* If there's a condition, process it. */
8641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8642 condition = cp_parser_condition (parser);
8643 finish_for_cond (condition, stmt);
8644 /* Look for the `;'. */
8645 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8647 /* If there's an expression, process it. */
8648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8649 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8650 finish_for_expr (expression, stmt);
8655 /* Tries to parse a range-based for-statement:
8658 type-specifier-seq declarator : expression
8660 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8661 expression. Note that the *DECL is returned unfinished, so
8662 later you should call cp_finish_decl().
8664 Returns TRUE iff a range-based for is parsed. */
8667 cp_parser_range_for (cp_parser *parser)
8669 tree stmt, range_decl, range_expr;
8670 cp_decl_specifier_seq type_specifiers;
8671 cp_declarator *declarator;
8672 const char *saved_message;
8673 tree attributes, pushed_scope;
8675 cp_parser_parse_tentatively (parser);
8676 /* New types are not allowed in the type-specifier-seq for a
8677 range-based for loop. */
8678 saved_message = parser->type_definition_forbidden_message;
8679 parser->type_definition_forbidden_message
8680 = G_("types may not be defined in range-based for loops");
8681 /* Parse the type-specifier-seq. */
8682 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8683 /*is_trailing_return=*/false,
8685 /* Restore the saved message. */
8686 parser->type_definition_forbidden_message = saved_message;
8687 /* If all is well, we might be looking at a declaration. */
8688 if (cp_parser_error_occurred (parser))
8690 cp_parser_abort_tentative_parse (parser);
8693 /* Parse the declarator. */
8694 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8695 /*ctor_dtor_or_conv_p=*/NULL,
8696 /*parenthesized_p=*/NULL,
8697 /*member_p=*/false);
8698 /* Parse the attributes. */
8699 attributes = cp_parser_attributes_opt (parser);
8700 /* The next token should be `:'. */
8701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8702 cp_parser_simulate_error (parser);
8704 /* Check if it is a range-based for */
8705 if (!cp_parser_parse_definitely (parser))
8708 cp_parser_require (parser, CPP_COLON, RT_COLON);
8709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8711 bool expr_non_constant_p;
8712 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8715 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8717 /* If in template, STMT is converted to a normal for-statements
8718 at instantiation. If not, it is done just ahead. */
8719 if (processing_template_decl)
8720 stmt = begin_range_for_stmt ();
8722 stmt = begin_for_stmt ();
8724 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8725 range_decl = start_decl (declarator, &type_specifiers,
8726 /*initialized_p=*/SD_INITIALIZED,
8727 attributes, /*prefix_attributes=*/NULL_TREE,
8729 /* No scope allowed here */
8730 pop_scope (pushed_scope);
8732 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8733 finish_range_for_decl (stmt, range_decl, range_expr);
8735 /* Convert the range-based for loop into a normal for-statement. */
8736 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8741 /* Converts a range-based for-statement into a normal
8742 for-statement, as per the definition.
8744 for (RANGE_DECL : RANGE_EXPR)
8747 should be equivalent to:
8750 auto &&__range = RANGE_EXPR;
8751 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8755 RANGE_DECL = *__begin;
8760 If RANGE_EXPR is an array:
8761 BEGIN_EXPR = __range
8762 END_EXPR = __range + ARRAY_SIZE(__range)
8764 BEGIN_EXPR = begin(__range)
8765 END_EXPR = end(__range);
8767 When calling begin()/end() we must use argument dependent
8768 lookup, but always considering 'std' as an associated namespace. */
8771 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8773 tree range_type, range_temp;
8775 tree iter_type, begin_expr, end_expr;
8776 tree condition, expression;
8778 /* Find out the type deduced by the declaration
8779 * `auto &&__range = range_expr' */
8780 range_type = cp_build_reference_type (make_auto (), true);
8781 range_type = do_auto_deduction (range_type, range_expr,
8782 type_uses_auto (range_type));
8784 /* Create the __range variable */
8785 range_temp = build_decl (input_location, VAR_DECL,
8786 get_identifier ("__for_range"), range_type);
8787 TREE_USED (range_temp) = 1;
8788 DECL_ARTIFICIAL (range_temp) = 1;
8789 pushdecl (range_temp);
8790 cp_finish_decl (range_temp, range_expr,
8791 /*is_constant_init*/false, NULL_TREE,
8792 LOOKUP_ONLYCONVERTING);
8794 range_temp = convert_from_reference (range_temp);
8796 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8798 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8799 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8800 begin_expr = range_temp;
8802 = build_binary_op (input_location, PLUS_EXPR,
8804 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8808 /* If it is not an array, we must call begin(__range)/end__range() */
8811 begin_expr = get_identifier ("begin");
8812 vec = make_tree_vector ();
8813 VEC_safe_push (tree, gc, vec, range_temp);
8814 begin_expr = perform_koenig_lookup (begin_expr, vec,
8815 /*include_std=*/true);
8816 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8817 tf_warning_or_error);
8818 release_tree_vector (vec);
8820 end_expr = get_identifier ("end");
8821 vec = make_tree_vector ();
8822 VEC_safe_push (tree, gc, vec, range_temp);
8823 end_expr = perform_koenig_lookup (end_expr, vec,
8824 /*include_std=*/true);
8825 end_expr = finish_call_expr (end_expr, &vec, false, true,
8826 tf_warning_or_error);
8827 release_tree_vector (vec);
8829 /* The unqualified type of the __begin and __end temporaries should
8830 * be the same as required by the multiple auto declaration */
8831 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8832 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8833 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8834 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8837 /* The new for initialization statement */
8838 begin = build_decl (input_location, VAR_DECL,
8839 get_identifier ("__for_begin"), iter_type);
8840 TREE_USED (begin) = 1;
8841 DECL_ARTIFICIAL (begin) = 1;
8843 cp_finish_decl (begin, begin_expr,
8844 /*is_constant_init*/false, NULL_TREE,
8845 LOOKUP_ONLYCONVERTING);
8847 end = build_decl (input_location, VAR_DECL,
8848 get_identifier ("__for_end"), iter_type);
8849 TREE_USED (end) = 1;
8850 DECL_ARTIFICIAL (end) = 1;
8852 cp_finish_decl (end, end_expr,
8853 /*is_constant_init*/false, NULL_TREE,
8854 LOOKUP_ONLYCONVERTING);
8856 finish_for_init_stmt (statement);
8858 /* The new for condition */
8859 condition = build_x_binary_op (NE_EXPR,
8862 NULL, tf_warning_or_error);
8863 finish_for_cond (condition, statement);
8865 /* The new increment expression */
8866 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8867 finish_for_expr (expression, statement);
8869 /* The declaration is initialized with *__begin inside the loop body */
8870 cp_finish_decl (range_decl,
8871 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8872 /*is_constant_init*/false, NULL_TREE,
8873 LOOKUP_ONLYCONVERTING);
8879 /* Parse an iteration-statement.
8881 iteration-statement:
8882 while ( condition ) statement
8883 do statement while ( expression ) ;
8884 for ( for-init-statement condition [opt] ; expression [opt] )
8887 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8890 cp_parser_iteration_statement (cp_parser* parser)
8895 unsigned char in_statement;
8897 /* Peek at the next token. */
8898 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8900 return error_mark_node;
8902 /* Remember whether or not we are already within an iteration
8904 in_statement = parser->in_statement;
8906 /* See what kind of keyword it is. */
8907 keyword = token->keyword;
8914 /* Begin the while-statement. */
8915 statement = begin_while_stmt ();
8916 /* Look for the `('. */
8917 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8918 /* Parse the condition. */
8919 condition = cp_parser_condition (parser);
8920 finish_while_stmt_cond (condition, statement);
8921 /* Look for the `)'. */
8922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8923 /* Parse the dependent statement. */
8924 parser->in_statement = IN_ITERATION_STMT;
8925 cp_parser_already_scoped_statement (parser);
8926 parser->in_statement = in_statement;
8927 /* We're done with the while-statement. */
8928 finish_while_stmt (statement);
8936 /* Begin the do-statement. */
8937 statement = begin_do_stmt ();
8938 /* Parse the body of the do-statement. */
8939 parser->in_statement = IN_ITERATION_STMT;
8940 cp_parser_implicitly_scoped_statement (parser, NULL);
8941 parser->in_statement = in_statement;
8942 finish_do_body (statement);
8943 /* Look for the `while' keyword. */
8944 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8945 /* Look for the `('. */
8946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8947 /* Parse the expression. */
8948 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8949 /* We're done with the do-statement. */
8950 finish_do_stmt (expression, statement);
8951 /* Look for the `)'. */
8952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8953 /* Look for the `;'. */
8954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8960 /* Look for the `('. */
8961 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8963 if (cxx_dialect == cxx0x)
8964 statement = cp_parser_range_for (parser);
8966 statement = NULL_TREE;
8967 if (statement == NULL_TREE)
8968 statement = cp_parser_c_for (parser);
8970 /* Look for the `)'. */
8971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8973 /* Parse the body of the for-statement. */
8974 parser->in_statement = IN_ITERATION_STMT;
8975 cp_parser_already_scoped_statement (parser);
8976 parser->in_statement = in_statement;
8978 /* We're done with the for-statement. */
8979 finish_for_stmt (statement);
8984 cp_parser_error (parser, "expected iteration-statement");
8985 statement = error_mark_node;
8992 /* Parse a for-init-statement.
8995 expression-statement
8996 simple-declaration */
8999 cp_parser_for_init_statement (cp_parser* parser)
9001 /* If the next token is a `;', then we have an empty
9002 expression-statement. Grammatically, this is also a
9003 simple-declaration, but an invalid one, because it does not
9004 declare anything. Therefore, if we did not handle this case
9005 specially, we would issue an error message about an invalid
9007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9009 /* We're going to speculatively look for a declaration, falling back
9010 to an expression, if necessary. */
9011 cp_parser_parse_tentatively (parser);
9012 /* Parse the declaration. */
9013 cp_parser_simple_declaration (parser,
9014 /*function_definition_allowed_p=*/false);
9015 /* If the tentative parse failed, then we shall need to look for an
9016 expression-statement. */
9017 if (cp_parser_parse_definitely (parser))
9021 cp_parser_expression_statement (parser, NULL_TREE);
9024 /* Parse a jump-statement.
9029 return expression [opt] ;
9030 return braced-init-list ;
9038 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9041 cp_parser_jump_statement (cp_parser* parser)
9043 tree statement = error_mark_node;
9046 unsigned char in_statement;
9048 /* Peek at the next token. */
9049 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9051 return error_mark_node;
9053 /* See what kind of keyword it is. */
9054 keyword = token->keyword;
9058 in_statement = parser->in_statement & ~IN_IF_STMT;
9059 switch (in_statement)
9062 error_at (token->location, "break statement not within loop or switch");
9065 gcc_assert ((in_statement & IN_SWITCH_STMT)
9066 || in_statement == IN_ITERATION_STMT);
9067 statement = finish_break_stmt ();
9070 error_at (token->location, "invalid exit from OpenMP structured block");
9073 error_at (token->location, "break statement used with OpenMP for loop");
9076 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9080 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9083 error_at (token->location, "continue statement not within a loop");
9085 case IN_ITERATION_STMT:
9087 statement = finish_continue_stmt ();
9090 error_at (token->location, "invalid exit from OpenMP structured block");
9095 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9101 bool expr_non_constant_p;
9103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9105 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9106 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9108 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9109 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9111 /* If the next token is a `;', then there is no
9114 /* Build the return-statement. */
9115 statement = finish_return_stmt (expr);
9116 /* Look for the final `;'. */
9117 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9122 /* Create the goto-statement. */
9123 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9125 /* Issue a warning about this use of a GNU extension. */
9126 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9127 /* Consume the '*' token. */
9128 cp_lexer_consume_token (parser->lexer);
9129 /* Parse the dependent expression. */
9130 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9133 finish_goto_stmt (cp_parser_identifier (parser));
9134 /* Look for the final `;'. */
9135 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9139 cp_parser_error (parser, "expected jump-statement");
9146 /* Parse a declaration-statement.
9148 declaration-statement:
9149 block-declaration */
9152 cp_parser_declaration_statement (cp_parser* parser)
9156 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9157 p = obstack_alloc (&declarator_obstack, 0);
9159 /* Parse the block-declaration. */
9160 cp_parser_block_declaration (parser, /*statement_p=*/true);
9162 /* Free any declarators allocated. */
9163 obstack_free (&declarator_obstack, p);
9165 /* Finish off the statement. */
9169 /* Some dependent statements (like `if (cond) statement'), are
9170 implicitly in their own scope. In other words, if the statement is
9171 a single statement (as opposed to a compound-statement), it is
9172 none-the-less treated as if it were enclosed in braces. Any
9173 declarations appearing in the dependent statement are out of scope
9174 after control passes that point. This function parses a statement,
9175 but ensures that is in its own scope, even if it is not a
9178 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9179 is a (possibly labeled) if statement which is not enclosed in
9180 braces and has an else clause. This is used to implement
9183 Returns the new statement. */
9186 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9193 /* Mark if () ; with a special NOP_EXPR. */
9194 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9196 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9197 cp_lexer_consume_token (parser->lexer);
9198 statement = add_stmt (build_empty_stmt (loc));
9200 /* if a compound is opened, we simply parse the statement directly. */
9201 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9202 statement = cp_parser_compound_statement (parser, NULL, false);
9203 /* If the token is not a `{', then we must take special action. */
9206 /* Create a compound-statement. */
9207 statement = begin_compound_stmt (0);
9208 /* Parse the dependent-statement. */
9209 cp_parser_statement (parser, NULL_TREE, false, if_p);
9210 /* Finish the dummy compound-statement. */
9211 finish_compound_stmt (statement);
9214 /* Return the statement. */
9218 /* For some dependent statements (like `while (cond) statement'), we
9219 have already created a scope. Therefore, even if the dependent
9220 statement is a compound-statement, we do not want to create another
9224 cp_parser_already_scoped_statement (cp_parser* parser)
9226 /* If the token is a `{', then we must take special action. */
9227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9228 cp_parser_statement (parser, NULL_TREE, false, NULL);
9231 /* Avoid calling cp_parser_compound_statement, so that we
9232 don't create a new scope. Do everything else by hand. */
9233 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9234 /* If the next keyword is `__label__' we have a label declaration. */
9235 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9236 cp_parser_label_declaration (parser);
9237 /* Parse an (optional) statement-seq. */
9238 cp_parser_statement_seq_opt (parser, NULL_TREE);
9239 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9243 /* Declarations [gram.dcl.dcl] */
9245 /* Parse an optional declaration-sequence.
9249 declaration-seq declaration */
9252 cp_parser_declaration_seq_opt (cp_parser* parser)
9258 token = cp_lexer_peek_token (parser->lexer);
9260 if (token->type == CPP_CLOSE_BRACE
9261 || token->type == CPP_EOF
9262 || token->type == CPP_PRAGMA_EOL)
9265 if (token->type == CPP_SEMICOLON)
9267 /* A declaration consisting of a single semicolon is
9268 invalid. Allow it unless we're being pedantic. */
9269 cp_lexer_consume_token (parser->lexer);
9270 if (!in_system_header)
9271 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9275 /* If we're entering or exiting a region that's implicitly
9276 extern "C", modify the lang context appropriately. */
9277 if (!parser->implicit_extern_c && token->implicit_extern_c)
9279 push_lang_context (lang_name_c);
9280 parser->implicit_extern_c = true;
9282 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9284 pop_lang_context ();
9285 parser->implicit_extern_c = false;
9288 if (token->type == CPP_PRAGMA)
9290 /* A top-level declaration can consist solely of a #pragma.
9291 A nested declaration cannot, so this is done here and not
9292 in cp_parser_declaration. (A #pragma at block scope is
9293 handled in cp_parser_statement.) */
9294 cp_parser_pragma (parser, pragma_external);
9298 /* Parse the declaration itself. */
9299 cp_parser_declaration (parser);
9303 /* Parse a declaration.
9308 template-declaration
9309 explicit-instantiation
9310 explicit-specialization
9311 linkage-specification
9312 namespace-definition
9317 __extension__ declaration */
9320 cp_parser_declaration (cp_parser* parser)
9326 tree attributes = NULL_TREE;
9328 /* Check for the `__extension__' keyword. */
9329 if (cp_parser_extension_opt (parser, &saved_pedantic))
9331 /* Parse the qualified declaration. */
9332 cp_parser_declaration (parser);
9333 /* Restore the PEDANTIC flag. */
9334 pedantic = saved_pedantic;
9339 /* Try to figure out what kind of declaration is present. */
9340 token1 = *cp_lexer_peek_token (parser->lexer);
9342 if (token1.type != CPP_EOF)
9343 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9346 token2.type = CPP_EOF;
9347 token2.keyword = RID_MAX;
9350 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9351 p = obstack_alloc (&declarator_obstack, 0);
9353 /* If the next token is `extern' and the following token is a string
9354 literal, then we have a linkage specification. */
9355 if (token1.keyword == RID_EXTERN
9356 && cp_parser_is_string_literal (&token2))
9357 cp_parser_linkage_specification (parser);
9358 /* If the next token is `template', then we have either a template
9359 declaration, an explicit instantiation, or an explicit
9361 else if (token1.keyword == RID_TEMPLATE)
9363 /* `template <>' indicates a template specialization. */
9364 if (token2.type == CPP_LESS
9365 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9366 cp_parser_explicit_specialization (parser);
9367 /* `template <' indicates a template declaration. */
9368 else if (token2.type == CPP_LESS)
9369 cp_parser_template_declaration (parser, /*member_p=*/false);
9370 /* Anything else must be an explicit instantiation. */
9372 cp_parser_explicit_instantiation (parser);
9374 /* If the next token is `export', then we have a template
9376 else if (token1.keyword == RID_EXPORT)
9377 cp_parser_template_declaration (parser, /*member_p=*/false);
9378 /* If the next token is `extern', 'static' or 'inline' and the one
9379 after that is `template', we have a GNU extended explicit
9380 instantiation directive. */
9381 else if (cp_parser_allow_gnu_extensions_p (parser)
9382 && (token1.keyword == RID_EXTERN
9383 || token1.keyword == RID_STATIC
9384 || token1.keyword == RID_INLINE)
9385 && token2.keyword == RID_TEMPLATE)
9386 cp_parser_explicit_instantiation (parser);
9387 /* If the next token is `namespace', check for a named or unnamed
9388 namespace definition. */
9389 else if (token1.keyword == RID_NAMESPACE
9390 && (/* A named namespace definition. */
9391 (token2.type == CPP_NAME
9392 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9394 /* An unnamed namespace definition. */
9395 || token2.type == CPP_OPEN_BRACE
9396 || token2.keyword == RID_ATTRIBUTE))
9397 cp_parser_namespace_definition (parser);
9398 /* An inline (associated) namespace definition. */
9399 else if (token1.keyword == RID_INLINE
9400 && token2.keyword == RID_NAMESPACE)
9401 cp_parser_namespace_definition (parser);
9402 /* Objective-C++ declaration/definition. */
9403 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9404 cp_parser_objc_declaration (parser, NULL_TREE);
9405 else if (c_dialect_objc ()
9406 && token1.keyword == RID_ATTRIBUTE
9407 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9408 cp_parser_objc_declaration (parser, attributes);
9409 /* We must have either a block declaration or a function
9412 /* Try to parse a block-declaration, or a function-definition. */
9413 cp_parser_block_declaration (parser, /*statement_p=*/false);
9415 /* Free any declarators allocated. */
9416 obstack_free (&declarator_obstack, p);
9419 /* Parse a block-declaration.
9424 namespace-alias-definition
9431 __extension__ block-declaration
9436 static_assert-declaration
9438 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9439 part of a declaration-statement. */
9442 cp_parser_block_declaration (cp_parser *parser,
9448 /* Check for the `__extension__' keyword. */
9449 if (cp_parser_extension_opt (parser, &saved_pedantic))
9451 /* Parse the qualified declaration. */
9452 cp_parser_block_declaration (parser, statement_p);
9453 /* Restore the PEDANTIC flag. */
9454 pedantic = saved_pedantic;
9459 /* Peek at the next token to figure out which kind of declaration is
9461 token1 = cp_lexer_peek_token (parser->lexer);
9463 /* If the next keyword is `asm', we have an asm-definition. */
9464 if (token1->keyword == RID_ASM)
9467 cp_parser_commit_to_tentative_parse (parser);
9468 cp_parser_asm_definition (parser);
9470 /* If the next keyword is `namespace', we have a
9471 namespace-alias-definition. */
9472 else if (token1->keyword == RID_NAMESPACE)
9473 cp_parser_namespace_alias_definition (parser);
9474 /* If the next keyword is `using', we have either a
9475 using-declaration or a using-directive. */
9476 else if (token1->keyword == RID_USING)
9481 cp_parser_commit_to_tentative_parse (parser);
9482 /* If the token after `using' is `namespace', then we have a
9484 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9485 if (token2->keyword == RID_NAMESPACE)
9486 cp_parser_using_directive (parser);
9487 /* Otherwise, it's a using-declaration. */
9489 cp_parser_using_declaration (parser,
9490 /*access_declaration_p=*/false);
9492 /* If the next keyword is `__label__' we have a misplaced label
9494 else if (token1->keyword == RID_LABEL)
9496 cp_lexer_consume_token (parser->lexer);
9497 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9498 cp_parser_skip_to_end_of_statement (parser);
9499 /* If the next token is now a `;', consume it. */
9500 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9501 cp_lexer_consume_token (parser->lexer);
9503 /* If the next token is `static_assert' we have a static assertion. */
9504 else if (token1->keyword == RID_STATIC_ASSERT)
9505 cp_parser_static_assert (parser, /*member_p=*/false);
9506 /* Anything else must be a simple-declaration. */
9508 cp_parser_simple_declaration (parser, !statement_p);
9511 /* Parse a simple-declaration.
9514 decl-specifier-seq [opt] init-declarator-list [opt] ;
9516 init-declarator-list:
9518 init-declarator-list , init-declarator
9520 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9521 function-definition as a simple-declaration. */
9524 cp_parser_simple_declaration (cp_parser* parser,
9525 bool function_definition_allowed_p)
9527 cp_decl_specifier_seq decl_specifiers;
9528 int declares_class_or_enum;
9529 bool saw_declarator;
9531 /* Defer access checks until we know what is being declared; the
9532 checks for names appearing in the decl-specifier-seq should be
9533 done as if we were in the scope of the thing being declared. */
9534 push_deferring_access_checks (dk_deferred);
9536 /* Parse the decl-specifier-seq. We have to keep track of whether
9537 or not the decl-specifier-seq declares a named class or
9538 enumeration type, since that is the only case in which the
9539 init-declarator-list is allowed to be empty.
9543 In a simple-declaration, the optional init-declarator-list can be
9544 omitted only when declaring a class or enumeration, that is when
9545 the decl-specifier-seq contains either a class-specifier, an
9546 elaborated-type-specifier, or an enum-specifier. */
9547 cp_parser_decl_specifier_seq (parser,
9548 CP_PARSER_FLAGS_OPTIONAL,
9550 &declares_class_or_enum);
9551 /* We no longer need to defer access checks. */
9552 stop_deferring_access_checks ();
9554 /* In a block scope, a valid declaration must always have a
9555 decl-specifier-seq. By not trying to parse declarators, we can
9556 resolve the declaration/expression ambiguity more quickly. */
9557 if (!function_definition_allowed_p
9558 && !decl_specifiers.any_specifiers_p)
9560 cp_parser_error (parser, "expected declaration");
9564 /* If the next two tokens are both identifiers, the code is
9565 erroneous. The usual cause of this situation is code like:
9569 where "T" should name a type -- but does not. */
9570 if (!decl_specifiers.any_type_specifiers_p
9571 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9573 /* If parsing tentatively, we should commit; we really are
9574 looking at a declaration. */
9575 cp_parser_commit_to_tentative_parse (parser);
9580 /* If we have seen at least one decl-specifier, and the next token
9581 is not a parenthesis, then we must be looking at a declaration.
9582 (After "int (" we might be looking at a functional cast.) */
9583 if (decl_specifiers.any_specifiers_p
9584 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9585 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9586 && !cp_parser_error_occurred (parser))
9587 cp_parser_commit_to_tentative_parse (parser);
9589 /* Keep going until we hit the `;' at the end of the simple
9591 saw_declarator = false;
9592 while (cp_lexer_next_token_is_not (parser->lexer,
9596 bool function_definition_p;
9601 /* If we are processing next declarator, coma is expected */
9602 token = cp_lexer_peek_token (parser->lexer);
9603 gcc_assert (token->type == CPP_COMMA);
9604 cp_lexer_consume_token (parser->lexer);
9607 saw_declarator = true;
9609 /* Parse the init-declarator. */
9610 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9612 function_definition_allowed_p,
9614 declares_class_or_enum,
9615 &function_definition_p);
9616 /* If an error occurred while parsing tentatively, exit quickly.
9617 (That usually happens when in the body of a function; each
9618 statement is treated as a declaration-statement until proven
9620 if (cp_parser_error_occurred (parser))
9622 /* Handle function definitions specially. */
9623 if (function_definition_p)
9625 /* If the next token is a `,', then we are probably
9626 processing something like:
9630 which is erroneous. */
9631 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9633 cp_token *token = cp_lexer_peek_token (parser->lexer);
9634 error_at (token->location,
9636 " declarations and function-definitions is forbidden");
9638 /* Otherwise, we're done with the list of declarators. */
9641 pop_deferring_access_checks ();
9645 /* The next token should be either a `,' or a `;'. */
9646 token = cp_lexer_peek_token (parser->lexer);
9647 /* If it's a `,', there are more declarators to come. */
9648 if (token->type == CPP_COMMA)
9649 /* will be consumed next time around */;
9650 /* If it's a `;', we are done. */
9651 else if (token->type == CPP_SEMICOLON)
9653 /* Anything else is an error. */
9656 /* If we have already issued an error message we don't need
9657 to issue another one. */
9658 if (decl != error_mark_node
9659 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9660 cp_parser_error (parser, "expected %<,%> or %<;%>");
9661 /* Skip tokens until we reach the end of the statement. */
9662 cp_parser_skip_to_end_of_statement (parser);
9663 /* If the next token is now a `;', consume it. */
9664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9665 cp_lexer_consume_token (parser->lexer);
9668 /* After the first time around, a function-definition is not
9669 allowed -- even if it was OK at first. For example:
9674 function_definition_allowed_p = false;
9677 /* Issue an error message if no declarators are present, and the
9678 decl-specifier-seq does not itself declare a class or
9680 if (!saw_declarator)
9682 if (cp_parser_declares_only_class_p (parser))
9683 shadow_tag (&decl_specifiers);
9684 /* Perform any deferred access checks. */
9685 perform_deferred_access_checks ();
9688 /* Consume the `;'. */
9689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9692 pop_deferring_access_checks ();
9695 /* Parse a decl-specifier-seq.
9698 decl-specifier-seq [opt] decl-specifier
9701 storage-class-specifier
9712 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9714 The parser flags FLAGS is used to control type-specifier parsing.
9716 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9719 1: one of the decl-specifiers is an elaborated-type-specifier
9720 (i.e., a type declaration)
9721 2: one of the decl-specifiers is an enum-specifier or a
9722 class-specifier (i.e., a type definition)
9727 cp_parser_decl_specifier_seq (cp_parser* parser,
9728 cp_parser_flags flags,
9729 cp_decl_specifier_seq *decl_specs,
9730 int* declares_class_or_enum)
9732 bool constructor_possible_p = !parser->in_declarator_p;
9733 cp_token *start_token = NULL;
9735 /* Clear DECL_SPECS. */
9736 clear_decl_specs (decl_specs);
9738 /* Assume no class or enumeration type is declared. */
9739 *declares_class_or_enum = 0;
9741 /* Keep reading specifiers until there are no more to read. */
9745 bool found_decl_spec;
9748 /* Peek at the next token. */
9749 token = cp_lexer_peek_token (parser->lexer);
9751 /* Save the first token of the decl spec list for error
9754 start_token = token;
9755 /* Handle attributes. */
9756 if (token->keyword == RID_ATTRIBUTE)
9758 /* Parse the attributes. */
9759 decl_specs->attributes
9760 = chainon (decl_specs->attributes,
9761 cp_parser_attributes_opt (parser));
9764 /* Assume we will find a decl-specifier keyword. */
9765 found_decl_spec = true;
9766 /* If the next token is an appropriate keyword, we can simply
9767 add it to the list. */
9768 switch (token->keyword)
9774 if (!at_class_scope_p ())
9776 error_at (token->location, "%<friend%> used outside of class");
9777 cp_lexer_purge_token (parser->lexer);
9781 ++decl_specs->specs[(int) ds_friend];
9782 /* Consume the token. */
9783 cp_lexer_consume_token (parser->lexer);
9788 ++decl_specs->specs[(int) ds_constexpr];
9789 cp_lexer_consume_token (parser->lexer);
9792 /* function-specifier:
9799 cp_parser_function_specifier_opt (parser, decl_specs);
9805 ++decl_specs->specs[(int) ds_typedef];
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser->lexer);
9808 /* A constructor declarator cannot appear in a typedef. */
9809 constructor_possible_p = false;
9810 /* The "typedef" keyword can only occur in a declaration; we
9811 may as well commit at this point. */
9812 cp_parser_commit_to_tentative_parse (parser);
9814 if (decl_specs->storage_class != sc_none)
9815 decl_specs->conflicting_specifiers_p = true;
9818 /* storage-class-specifier:
9828 if (cxx_dialect == cxx98)
9830 /* Consume the token. */
9831 cp_lexer_consume_token (parser->lexer);
9833 /* Complain about `auto' as a storage specifier, if
9834 we're complaining about C++0x compatibility. */
9835 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9836 " will change meaning in C++0x; please remove it");
9838 /* Set the storage class anyway. */
9839 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9843 /* C++0x auto type-specifier. */
9844 found_decl_spec = false;
9851 /* Consume the token. */
9852 cp_lexer_consume_token (parser->lexer);
9853 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9857 /* Consume the token. */
9858 cp_lexer_consume_token (parser->lexer);
9859 ++decl_specs->specs[(int) ds_thread];
9863 /* We did not yet find a decl-specifier yet. */
9864 found_decl_spec = false;
9869 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9870 && token->keyword != RID_CONSTEXPR)
9871 error ("decl-specifier invalid in condition");
9873 /* Constructors are a special case. The `S' in `S()' is not a
9874 decl-specifier; it is the beginning of the declarator. */
9877 && constructor_possible_p
9878 && (cp_parser_constructor_declarator_p
9879 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9881 /* If we don't have a DECL_SPEC yet, then we must be looking at
9882 a type-specifier. */
9883 if (!found_decl_spec && !constructor_p)
9885 int decl_spec_declares_class_or_enum;
9886 bool is_cv_qualifier;
9890 = cp_parser_type_specifier (parser, flags,
9892 /*is_declaration=*/true,
9893 &decl_spec_declares_class_or_enum,
9895 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9897 /* If this type-specifier referenced a user-defined type
9898 (a typedef, class-name, etc.), then we can't allow any
9899 more such type-specifiers henceforth.
9903 The longest sequence of decl-specifiers that could
9904 possibly be a type name is taken as the
9905 decl-specifier-seq of a declaration. The sequence shall
9906 be self-consistent as described below.
9910 As a general rule, at most one type-specifier is allowed
9911 in the complete decl-specifier-seq of a declaration. The
9912 only exceptions are the following:
9914 -- const or volatile can be combined with any other
9917 -- signed or unsigned can be combined with char, long,
9925 void g (const int Pc);
9927 Here, Pc is *not* part of the decl-specifier seq; it's
9928 the declarator. Therefore, once we see a type-specifier
9929 (other than a cv-qualifier), we forbid any additional
9930 user-defined types. We *do* still allow things like `int
9931 int' to be considered a decl-specifier-seq, and issue the
9932 error message later. */
9933 if (type_spec && !is_cv_qualifier)
9934 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9935 /* A constructor declarator cannot follow a type-specifier. */
9938 constructor_possible_p = false;
9939 found_decl_spec = true;
9940 if (!is_cv_qualifier)
9941 decl_specs->any_type_specifiers_p = true;
9945 /* If we still do not have a DECL_SPEC, then there are no more
9947 if (!found_decl_spec)
9950 decl_specs->any_specifiers_p = true;
9951 /* After we see one decl-specifier, further decl-specifiers are
9953 flags |= CP_PARSER_FLAGS_OPTIONAL;
9956 cp_parser_check_decl_spec (decl_specs, start_token->location);
9958 /* Don't allow a friend specifier with a class definition. */
9959 if (decl_specs->specs[(int) ds_friend] != 0
9960 && (*declares_class_or_enum & 2))
9961 error_at (start_token->location,
9962 "class definition may not be declared a friend");
9965 /* Parse an (optional) storage-class-specifier.
9967 storage-class-specifier:
9976 storage-class-specifier:
9979 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9982 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9984 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9987 if (cxx_dialect != cxx98)
9989 /* Fall through for C++98. */
9996 /* Consume the token. */
9997 return cp_lexer_consume_token (parser->lexer)->u.value;
10004 /* Parse an (optional) function-specifier.
10006 function-specifier:
10011 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10012 Updates DECL_SPECS, if it is non-NULL. */
10015 cp_parser_function_specifier_opt (cp_parser* parser,
10016 cp_decl_specifier_seq *decl_specs)
10018 cp_token *token = cp_lexer_peek_token (parser->lexer);
10019 switch (token->keyword)
10023 ++decl_specs->specs[(int) ds_inline];
10027 /* 14.5.2.3 [temp.mem]
10029 A member function template shall not be virtual. */
10030 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10031 error_at (token->location, "templates may not be %<virtual%>");
10032 else if (decl_specs)
10033 ++decl_specs->specs[(int) ds_virtual];
10038 ++decl_specs->specs[(int) ds_explicit];
10045 /* Consume the token. */
10046 return cp_lexer_consume_token (parser->lexer)->u.value;
10049 /* Parse a linkage-specification.
10051 linkage-specification:
10052 extern string-literal { declaration-seq [opt] }
10053 extern string-literal declaration */
10056 cp_parser_linkage_specification (cp_parser* parser)
10060 /* Look for the `extern' keyword. */
10061 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10063 /* Look for the string-literal. */
10064 linkage = cp_parser_string_literal (parser, false, false);
10066 /* Transform the literal into an identifier. If the literal is a
10067 wide-character string, or contains embedded NULs, then we can't
10068 handle it as the user wants. */
10069 if (strlen (TREE_STRING_POINTER (linkage))
10070 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10072 cp_parser_error (parser, "invalid linkage-specification");
10073 /* Assume C++ linkage. */
10074 linkage = lang_name_cplusplus;
10077 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10079 /* We're now using the new linkage. */
10080 push_lang_context (linkage);
10082 /* If the next token is a `{', then we're using the first
10084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10086 /* Consume the `{' token. */
10087 cp_lexer_consume_token (parser->lexer);
10088 /* Parse the declarations. */
10089 cp_parser_declaration_seq_opt (parser);
10090 /* Look for the closing `}'. */
10091 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10093 /* Otherwise, there's just one declaration. */
10096 bool saved_in_unbraced_linkage_specification_p;
10098 saved_in_unbraced_linkage_specification_p
10099 = parser->in_unbraced_linkage_specification_p;
10100 parser->in_unbraced_linkage_specification_p = true;
10101 cp_parser_declaration (parser);
10102 parser->in_unbraced_linkage_specification_p
10103 = saved_in_unbraced_linkage_specification_p;
10106 /* We're done with the linkage-specification. */
10107 pop_lang_context ();
10110 /* Parse a static_assert-declaration.
10112 static_assert-declaration:
10113 static_assert ( constant-expression , string-literal ) ;
10115 If MEMBER_P, this static_assert is a class member. */
10118 cp_parser_static_assert(cp_parser *parser, bool member_p)
10123 location_t saved_loc;
10125 /* Peek at the `static_assert' token so we can keep track of exactly
10126 where the static assertion started. */
10127 token = cp_lexer_peek_token (parser->lexer);
10128 saved_loc = token->location;
10130 /* Look for the `static_assert' keyword. */
10131 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10135 /* We know we are in a static assertion; commit to any tentative
10137 if (cp_parser_parsing_tentatively (parser))
10138 cp_parser_commit_to_tentative_parse (parser);
10140 /* Parse the `(' starting the static assertion condition. */
10141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10143 /* Parse the constant-expression. */
10145 cp_parser_constant_expression (parser,
10146 /*allow_non_constant_p=*/false,
10147 /*non_constant_p=*/NULL);
10149 /* Parse the separating `,'. */
10150 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10152 /* Parse the string-literal message. */
10153 message = cp_parser_string_literal (parser,
10154 /*translate=*/false,
10157 /* A `)' completes the static assertion. */
10158 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10159 cp_parser_skip_to_closing_parenthesis (parser,
10160 /*recovering=*/true,
10161 /*or_comma=*/false,
10162 /*consume_paren=*/true);
10164 /* A semicolon terminates the declaration. */
10165 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10167 /* Complete the static assertion, which may mean either processing
10168 the static assert now or saving it for template instantiation. */
10169 finish_static_assert (condition, message, saved_loc, member_p);
10172 /* Parse a `decltype' type. Returns the type.
10174 simple-type-specifier:
10175 decltype ( expression ) */
10178 cp_parser_decltype (cp_parser *parser)
10181 bool id_expression_or_member_access_p = false;
10182 const char *saved_message;
10183 bool saved_integral_constant_expression_p;
10184 bool saved_non_integral_constant_expression_p;
10185 cp_token *id_expr_start_token;
10187 /* Look for the `decltype' token. */
10188 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10189 return error_mark_node;
10191 /* Types cannot be defined in a `decltype' expression. Save away the
10193 saved_message = parser->type_definition_forbidden_message;
10195 /* And create the new one. */
10196 parser->type_definition_forbidden_message
10197 = G_("types may not be defined in %<decltype%> expressions");
10199 /* The restrictions on constant-expressions do not apply inside
10200 decltype expressions. */
10201 saved_integral_constant_expression_p
10202 = parser->integral_constant_expression_p;
10203 saved_non_integral_constant_expression_p
10204 = parser->non_integral_constant_expression_p;
10205 parser->integral_constant_expression_p = false;
10207 /* Do not actually evaluate the expression. */
10208 ++cp_unevaluated_operand;
10210 /* Do not warn about problems with the expression. */
10211 ++c_inhibit_evaluation_warnings;
10213 /* Parse the opening `('. */
10214 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10215 return error_mark_node;
10217 /* First, try parsing an id-expression. */
10218 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10219 cp_parser_parse_tentatively (parser);
10220 expr = cp_parser_id_expression (parser,
10221 /*template_keyword_p=*/false,
10222 /*check_dependency_p=*/true,
10223 /*template_p=*/NULL,
10224 /*declarator_p=*/false,
10225 /*optional_p=*/false);
10227 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10229 bool non_integral_constant_expression_p = false;
10230 tree id_expression = expr;
10232 const char *error_msg;
10234 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10235 /* Lookup the name we got back from the id-expression. */
10236 expr = cp_parser_lookup_name (parser, expr,
10238 /*is_template=*/false,
10239 /*is_namespace=*/false,
10240 /*check_dependency=*/true,
10241 /*ambiguous_decls=*/NULL,
10242 id_expr_start_token->location);
10245 && expr != error_mark_node
10246 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10247 && TREE_CODE (expr) != TYPE_DECL
10248 && (TREE_CODE (expr) != BIT_NOT_EXPR
10249 || !TYPE_P (TREE_OPERAND (expr, 0)))
10250 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10252 /* Complete lookup of the id-expression. */
10253 expr = (finish_id_expression
10254 (id_expression, expr, parser->scope, &idk,
10255 /*integral_constant_expression_p=*/false,
10256 /*allow_non_integral_constant_expression_p=*/true,
10257 &non_integral_constant_expression_p,
10258 /*template_p=*/false,
10260 /*address_p=*/false,
10261 /*template_arg_p=*/false,
10263 id_expr_start_token->location));
10265 if (expr == error_mark_node)
10266 /* We found an id-expression, but it was something that we
10267 should not have found. This is an error, not something
10268 we can recover from, so note that we found an
10269 id-expression and we'll recover as gracefully as
10271 id_expression_or_member_access_p = true;
10275 && expr != error_mark_node
10276 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10277 /* We have an id-expression. */
10278 id_expression_or_member_access_p = true;
10281 if (!id_expression_or_member_access_p)
10283 /* Abort the id-expression parse. */
10284 cp_parser_abort_tentative_parse (parser);
10286 /* Parsing tentatively, again. */
10287 cp_parser_parse_tentatively (parser);
10289 /* Parse a class member access. */
10290 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10292 /*member_access_only_p=*/true, NULL);
10295 && expr != error_mark_node
10296 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10297 /* We have an id-expression. */
10298 id_expression_or_member_access_p = true;
10301 if (id_expression_or_member_access_p)
10302 /* We have parsed the complete id-expression or member access. */
10303 cp_parser_parse_definitely (parser);
10306 bool saved_greater_than_is_operator_p;
10308 /* Abort our attempt to parse an id-expression or member access
10310 cp_parser_abort_tentative_parse (parser);
10312 /* Within a parenthesized expression, a `>' token is always
10313 the greater-than operator. */
10314 saved_greater_than_is_operator_p
10315 = parser->greater_than_is_operator_p;
10316 parser->greater_than_is_operator_p = true;
10318 /* Parse a full expression. */
10319 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10321 /* The `>' token might be the end of a template-id or
10322 template-parameter-list now. */
10323 parser->greater_than_is_operator_p
10324 = saved_greater_than_is_operator_p;
10327 /* Go back to evaluating expressions. */
10328 --cp_unevaluated_operand;
10329 --c_inhibit_evaluation_warnings;
10331 /* Restore the old message and the integral constant expression
10333 parser->type_definition_forbidden_message = saved_message;
10334 parser->integral_constant_expression_p
10335 = saved_integral_constant_expression_p;
10336 parser->non_integral_constant_expression_p
10337 = saved_non_integral_constant_expression_p;
10339 if (expr == error_mark_node)
10341 /* Skip everything up to the closing `)'. */
10342 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10343 /*consume_paren=*/true);
10344 return error_mark_node;
10347 /* Parse to the closing `)'. */
10348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10350 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10351 /*consume_paren=*/true);
10352 return error_mark_node;
10355 return finish_decltype_type (expr, id_expression_or_member_access_p);
10358 /* Special member functions [gram.special] */
10360 /* Parse a conversion-function-id.
10362 conversion-function-id:
10363 operator conversion-type-id
10365 Returns an IDENTIFIER_NODE representing the operator. */
10368 cp_parser_conversion_function_id (cp_parser* parser)
10372 tree saved_qualifying_scope;
10373 tree saved_object_scope;
10374 tree pushed_scope = NULL_TREE;
10376 /* Look for the `operator' token. */
10377 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10378 return error_mark_node;
10379 /* When we parse the conversion-type-id, the current scope will be
10380 reset. However, we need that information in able to look up the
10381 conversion function later, so we save it here. */
10382 saved_scope = parser->scope;
10383 saved_qualifying_scope = parser->qualifying_scope;
10384 saved_object_scope = parser->object_scope;
10385 /* We must enter the scope of the class so that the names of
10386 entities declared within the class are available in the
10387 conversion-type-id. For example, consider:
10394 S::operator I() { ... }
10396 In order to see that `I' is a type-name in the definition, we
10397 must be in the scope of `S'. */
10399 pushed_scope = push_scope (saved_scope);
10400 /* Parse the conversion-type-id. */
10401 type = cp_parser_conversion_type_id (parser);
10402 /* Leave the scope of the class, if any. */
10404 pop_scope (pushed_scope);
10405 /* Restore the saved scope. */
10406 parser->scope = saved_scope;
10407 parser->qualifying_scope = saved_qualifying_scope;
10408 parser->object_scope = saved_object_scope;
10409 /* If the TYPE is invalid, indicate failure. */
10410 if (type == error_mark_node)
10411 return error_mark_node;
10412 return mangle_conv_op_name_for_type (type);
10415 /* Parse a conversion-type-id:
10417 conversion-type-id:
10418 type-specifier-seq conversion-declarator [opt]
10420 Returns the TYPE specified. */
10423 cp_parser_conversion_type_id (cp_parser* parser)
10426 cp_decl_specifier_seq type_specifiers;
10427 cp_declarator *declarator;
10428 tree type_specified;
10430 /* Parse the attributes. */
10431 attributes = cp_parser_attributes_opt (parser);
10432 /* Parse the type-specifiers. */
10433 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10434 /*is_trailing_return=*/false,
10436 /* If that didn't work, stop. */
10437 if (type_specifiers.type == error_mark_node)
10438 return error_mark_node;
10439 /* Parse the conversion-declarator. */
10440 declarator = cp_parser_conversion_declarator_opt (parser);
10442 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10443 /*initialized=*/0, &attributes);
10445 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10447 /* Don't give this error when parsing tentatively. This happens to
10448 work because we always parse this definitively once. */
10449 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10450 && type_uses_auto (type_specified))
10452 error ("invalid use of %<auto%> in conversion operator");
10453 return error_mark_node;
10456 return type_specified;
10459 /* Parse an (optional) conversion-declarator.
10461 conversion-declarator:
10462 ptr-operator conversion-declarator [opt]
10466 static cp_declarator *
10467 cp_parser_conversion_declarator_opt (cp_parser* parser)
10469 enum tree_code code;
10471 cp_cv_quals cv_quals;
10473 /* We don't know if there's a ptr-operator next, or not. */
10474 cp_parser_parse_tentatively (parser);
10475 /* Try the ptr-operator. */
10476 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10477 /* If it worked, look for more conversion-declarators. */
10478 if (cp_parser_parse_definitely (parser))
10480 cp_declarator *declarator;
10482 /* Parse another optional declarator. */
10483 declarator = cp_parser_conversion_declarator_opt (parser);
10485 return cp_parser_make_indirect_declarator
10486 (code, class_type, cv_quals, declarator);
10492 /* Parse an (optional) ctor-initializer.
10495 : mem-initializer-list
10497 Returns TRUE iff the ctor-initializer was actually present. */
10500 cp_parser_ctor_initializer_opt (cp_parser* parser)
10502 /* If the next token is not a `:', then there is no
10503 ctor-initializer. */
10504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10506 /* Do default initialization of any bases and members. */
10507 if (DECL_CONSTRUCTOR_P (current_function_decl))
10508 finish_mem_initializers (NULL_TREE);
10513 /* Consume the `:' token. */
10514 cp_lexer_consume_token (parser->lexer);
10515 /* And the mem-initializer-list. */
10516 cp_parser_mem_initializer_list (parser);
10521 /* Parse a mem-initializer-list.
10523 mem-initializer-list:
10524 mem-initializer ... [opt]
10525 mem-initializer ... [opt] , mem-initializer-list */
10528 cp_parser_mem_initializer_list (cp_parser* parser)
10530 tree mem_initializer_list = NULL_TREE;
10531 cp_token *token = cp_lexer_peek_token (parser->lexer);
10533 /* Let the semantic analysis code know that we are starting the
10534 mem-initializer-list. */
10535 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10536 error_at (token->location,
10537 "only constructors take member initializers");
10539 /* Loop through the list. */
10542 tree mem_initializer;
10544 token = cp_lexer_peek_token (parser->lexer);
10545 /* Parse the mem-initializer. */
10546 mem_initializer = cp_parser_mem_initializer (parser);
10547 /* If the next token is a `...', we're expanding member initializers. */
10548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10550 /* Consume the `...'. */
10551 cp_lexer_consume_token (parser->lexer);
10553 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10554 can be expanded but members cannot. */
10555 if (mem_initializer != error_mark_node
10556 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10558 error_at (token->location,
10559 "cannot expand initializer for member %<%D%>",
10560 TREE_PURPOSE (mem_initializer));
10561 mem_initializer = error_mark_node;
10564 /* Construct the pack expansion type. */
10565 if (mem_initializer != error_mark_node)
10566 mem_initializer = make_pack_expansion (mem_initializer);
10568 /* Add it to the list, unless it was erroneous. */
10569 if (mem_initializer != error_mark_node)
10571 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10572 mem_initializer_list = mem_initializer;
10574 /* If the next token is not a `,', we're done. */
10575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10577 /* Consume the `,' token. */
10578 cp_lexer_consume_token (parser->lexer);
10581 /* Perform semantic analysis. */
10582 if (DECL_CONSTRUCTOR_P (current_function_decl))
10583 finish_mem_initializers (mem_initializer_list);
10586 /* Parse a mem-initializer.
10589 mem-initializer-id ( expression-list [opt] )
10590 mem-initializer-id braced-init-list
10595 ( expression-list [opt] )
10597 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10598 class) or FIELD_DECL (for a non-static data member) to initialize;
10599 the TREE_VALUE is the expression-list. An empty initialization
10600 list is represented by void_list_node. */
10603 cp_parser_mem_initializer (cp_parser* parser)
10605 tree mem_initializer_id;
10606 tree expression_list;
10608 cp_token *token = cp_lexer_peek_token (parser->lexer);
10610 /* Find out what is being initialized. */
10611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10613 permerror (token->location,
10614 "anachronistic old-style base class initializer");
10615 mem_initializer_id = NULL_TREE;
10619 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10620 if (mem_initializer_id == error_mark_node)
10621 return mem_initializer_id;
10623 member = expand_member_init (mem_initializer_id);
10624 if (member && !DECL_P (member))
10625 in_base_initializer = 1;
10627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10629 bool expr_non_constant_p;
10630 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10631 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10632 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10633 expression_list = build_tree_list (NULL_TREE, expression_list);
10638 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10640 /*allow_expansion_p=*/true,
10641 /*non_constant_p=*/NULL);
10643 return error_mark_node;
10644 expression_list = build_tree_list_vec (vec);
10645 release_tree_vector (vec);
10648 if (expression_list == error_mark_node)
10649 return error_mark_node;
10650 if (!expression_list)
10651 expression_list = void_type_node;
10653 in_base_initializer = 0;
10655 return member ? build_tree_list (member, expression_list) : error_mark_node;
10658 /* Parse a mem-initializer-id.
10660 mem-initializer-id:
10661 :: [opt] nested-name-specifier [opt] class-name
10664 Returns a TYPE indicating the class to be initializer for the first
10665 production. Returns an IDENTIFIER_NODE indicating the data member
10666 to be initialized for the second production. */
10669 cp_parser_mem_initializer_id (cp_parser* parser)
10671 bool global_scope_p;
10672 bool nested_name_specifier_p;
10673 bool template_p = false;
10676 cp_token *token = cp_lexer_peek_token (parser->lexer);
10678 /* `typename' is not allowed in this context ([temp.res]). */
10679 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10681 error_at (token->location,
10682 "keyword %<typename%> not allowed in this context (a qualified "
10683 "member initializer is implicitly a type)");
10684 cp_lexer_consume_token (parser->lexer);
10686 /* Look for the optional `::' operator. */
10688 = (cp_parser_global_scope_opt (parser,
10689 /*current_scope_valid_p=*/false)
10691 /* Look for the optional nested-name-specifier. The simplest way to
10696 The keyword `typename' is not permitted in a base-specifier or
10697 mem-initializer; in these contexts a qualified name that
10698 depends on a template-parameter is implicitly assumed to be a
10701 is to assume that we have seen the `typename' keyword at this
10703 nested_name_specifier_p
10704 = (cp_parser_nested_name_specifier_opt (parser,
10705 /*typename_keyword_p=*/true,
10706 /*check_dependency_p=*/true,
10708 /*is_declaration=*/true)
10710 if (nested_name_specifier_p)
10711 template_p = cp_parser_optional_template_keyword (parser);
10712 /* If there is a `::' operator or a nested-name-specifier, then we
10713 are definitely looking for a class-name. */
10714 if (global_scope_p || nested_name_specifier_p)
10715 return cp_parser_class_name (parser,
10716 /*typename_keyword_p=*/true,
10717 /*template_keyword_p=*/template_p,
10719 /*check_dependency_p=*/true,
10720 /*class_head_p=*/false,
10721 /*is_declaration=*/true);
10722 /* Otherwise, we could also be looking for an ordinary identifier. */
10723 cp_parser_parse_tentatively (parser);
10724 /* Try a class-name. */
10725 id = cp_parser_class_name (parser,
10726 /*typename_keyword_p=*/true,
10727 /*template_keyword_p=*/false,
10729 /*check_dependency_p=*/true,
10730 /*class_head_p=*/false,
10731 /*is_declaration=*/true);
10732 /* If we found one, we're done. */
10733 if (cp_parser_parse_definitely (parser))
10735 /* Otherwise, look for an ordinary identifier. */
10736 return cp_parser_identifier (parser);
10739 /* Overloading [gram.over] */
10741 /* Parse an operator-function-id.
10743 operator-function-id:
10746 Returns an IDENTIFIER_NODE for the operator which is a
10747 human-readable spelling of the identifier, e.g., `operator +'. */
10750 cp_parser_operator_function_id (cp_parser* parser)
10752 /* Look for the `operator' keyword. */
10753 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10754 return error_mark_node;
10755 /* And then the name of the operator itself. */
10756 return cp_parser_operator (parser);
10759 /* Parse an operator.
10762 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10763 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10764 || ++ -- , ->* -> () []
10771 Returns an IDENTIFIER_NODE for the operator which is a
10772 human-readable spelling of the identifier, e.g., `operator +'. */
10775 cp_parser_operator (cp_parser* parser)
10777 tree id = NULL_TREE;
10780 /* Peek at the next token. */
10781 token = cp_lexer_peek_token (parser->lexer);
10782 /* Figure out which operator we have. */
10783 switch (token->type)
10789 /* The keyword should be either `new' or `delete'. */
10790 if (token->keyword == RID_NEW)
10792 else if (token->keyword == RID_DELETE)
10797 /* Consume the `new' or `delete' token. */
10798 cp_lexer_consume_token (parser->lexer);
10800 /* Peek at the next token. */
10801 token = cp_lexer_peek_token (parser->lexer);
10802 /* If it's a `[' token then this is the array variant of the
10804 if (token->type == CPP_OPEN_SQUARE)
10806 /* Consume the `[' token. */
10807 cp_lexer_consume_token (parser->lexer);
10808 /* Look for the `]' token. */
10809 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10810 id = ansi_opname (op == NEW_EXPR
10811 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10813 /* Otherwise, we have the non-array variant. */
10815 id = ansi_opname (op);
10821 id = ansi_opname (PLUS_EXPR);
10825 id = ansi_opname (MINUS_EXPR);
10829 id = ansi_opname (MULT_EXPR);
10833 id = ansi_opname (TRUNC_DIV_EXPR);
10837 id = ansi_opname (TRUNC_MOD_EXPR);
10841 id = ansi_opname (BIT_XOR_EXPR);
10845 id = ansi_opname (BIT_AND_EXPR);
10849 id = ansi_opname (BIT_IOR_EXPR);
10853 id = ansi_opname (BIT_NOT_EXPR);
10857 id = ansi_opname (TRUTH_NOT_EXPR);
10861 id = ansi_assopname (NOP_EXPR);
10865 id = ansi_opname (LT_EXPR);
10869 id = ansi_opname (GT_EXPR);
10873 id = ansi_assopname (PLUS_EXPR);
10877 id = ansi_assopname (MINUS_EXPR);
10881 id = ansi_assopname (MULT_EXPR);
10885 id = ansi_assopname (TRUNC_DIV_EXPR);
10889 id = ansi_assopname (TRUNC_MOD_EXPR);
10893 id = ansi_assopname (BIT_XOR_EXPR);
10897 id = ansi_assopname (BIT_AND_EXPR);
10901 id = ansi_assopname (BIT_IOR_EXPR);
10905 id = ansi_opname (LSHIFT_EXPR);
10909 id = ansi_opname (RSHIFT_EXPR);
10912 case CPP_LSHIFT_EQ:
10913 id = ansi_assopname (LSHIFT_EXPR);
10916 case CPP_RSHIFT_EQ:
10917 id = ansi_assopname (RSHIFT_EXPR);
10921 id = ansi_opname (EQ_EXPR);
10925 id = ansi_opname (NE_EXPR);
10929 id = ansi_opname (LE_EXPR);
10932 case CPP_GREATER_EQ:
10933 id = ansi_opname (GE_EXPR);
10937 id = ansi_opname (TRUTH_ANDIF_EXPR);
10941 id = ansi_opname (TRUTH_ORIF_EXPR);
10944 case CPP_PLUS_PLUS:
10945 id = ansi_opname (POSTINCREMENT_EXPR);
10948 case CPP_MINUS_MINUS:
10949 id = ansi_opname (PREDECREMENT_EXPR);
10953 id = ansi_opname (COMPOUND_EXPR);
10956 case CPP_DEREF_STAR:
10957 id = ansi_opname (MEMBER_REF);
10961 id = ansi_opname (COMPONENT_REF);
10964 case CPP_OPEN_PAREN:
10965 /* Consume the `('. */
10966 cp_lexer_consume_token (parser->lexer);
10967 /* Look for the matching `)'. */
10968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10969 return ansi_opname (CALL_EXPR);
10971 case CPP_OPEN_SQUARE:
10972 /* Consume the `['. */
10973 cp_lexer_consume_token (parser->lexer);
10974 /* Look for the matching `]'. */
10975 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10976 return ansi_opname (ARRAY_REF);
10979 /* Anything else is an error. */
10983 /* If we have selected an identifier, we need to consume the
10986 cp_lexer_consume_token (parser->lexer);
10987 /* Otherwise, no valid operator name was present. */
10990 cp_parser_error (parser, "expected operator");
10991 id = error_mark_node;
10997 /* Parse a template-declaration.
10999 template-declaration:
11000 export [opt] template < template-parameter-list > declaration
11002 If MEMBER_P is TRUE, this template-declaration occurs within a
11005 The grammar rule given by the standard isn't correct. What
11006 is really meant is:
11008 template-declaration:
11009 export [opt] template-parameter-list-seq
11010 decl-specifier-seq [opt] init-declarator [opt] ;
11011 export [opt] template-parameter-list-seq
11012 function-definition
11014 template-parameter-list-seq:
11015 template-parameter-list-seq [opt]
11016 template < template-parameter-list > */
11019 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11021 /* Check for `export'. */
11022 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11024 /* Consume the `export' token. */
11025 cp_lexer_consume_token (parser->lexer);
11026 /* Warn that we do not support `export'. */
11027 warning (0, "keyword %<export%> not implemented, and will be ignored");
11030 cp_parser_template_declaration_after_export (parser, member_p);
11033 /* Parse a template-parameter-list.
11035 template-parameter-list:
11037 template-parameter-list , template-parameter
11039 Returns a TREE_LIST. Each node represents a template parameter.
11040 The nodes are connected via their TREE_CHAINs. */
11043 cp_parser_template_parameter_list (cp_parser* parser)
11045 tree parameter_list = NULL_TREE;
11047 begin_template_parm_list ();
11049 /* The loop below parses the template parms. We first need to know
11050 the total number of template parms to be able to compute proper
11051 canonical types of each dependent type. So after the loop, when
11052 we know the total number of template parms,
11053 end_template_parm_list computes the proper canonical types and
11054 fixes up the dependent types accordingly. */
11059 bool is_parameter_pack;
11060 location_t parm_loc;
11062 /* Parse the template-parameter. */
11063 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11064 parameter = cp_parser_template_parameter (parser,
11066 &is_parameter_pack);
11067 /* Add it to the list. */
11068 if (parameter != error_mark_node)
11069 parameter_list = process_template_parm (parameter_list,
11077 tree err_parm = build_tree_list (parameter, parameter);
11078 parameter_list = chainon (parameter_list, err_parm);
11081 /* If the next token is not a `,', we're done. */
11082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11084 /* Otherwise, consume the `,' token. */
11085 cp_lexer_consume_token (parser->lexer);
11088 return end_template_parm_list (parameter_list);
11091 /* Parse a template-parameter.
11093 template-parameter:
11095 parameter-declaration
11097 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11098 the parameter. The TREE_PURPOSE is the default value, if any.
11099 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11100 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11101 set to true iff this parameter is a parameter pack. */
11104 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11105 bool *is_parameter_pack)
11108 cp_parameter_declarator *parameter_declarator;
11109 cp_declarator *id_declarator;
11112 /* Assume it is a type parameter or a template parameter. */
11113 *is_non_type = false;
11114 /* Assume it not a parameter pack. */
11115 *is_parameter_pack = false;
11116 /* Peek at the next token. */
11117 token = cp_lexer_peek_token (parser->lexer);
11118 /* If it is `class' or `template', we have a type-parameter. */
11119 if (token->keyword == RID_TEMPLATE)
11120 return cp_parser_type_parameter (parser, is_parameter_pack);
11121 /* If it is `class' or `typename' we do not know yet whether it is a
11122 type parameter or a non-type parameter. Consider:
11124 template <typename T, typename T::X X> ...
11128 template <class C, class D*> ...
11130 Here, the first parameter is a type parameter, and the second is
11131 a non-type parameter. We can tell by looking at the token after
11132 the identifier -- if it is a `,', `=', or `>' then we have a type
11134 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11136 /* Peek at the token after `class' or `typename'. */
11137 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11138 /* If it's an ellipsis, we have a template type parameter
11140 if (token->type == CPP_ELLIPSIS)
11141 return cp_parser_type_parameter (parser, is_parameter_pack);
11142 /* If it's an identifier, skip it. */
11143 if (token->type == CPP_NAME)
11144 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11145 /* Now, see if the token looks like the end of a template
11147 if (token->type == CPP_COMMA
11148 || token->type == CPP_EQ
11149 || token->type == CPP_GREATER)
11150 return cp_parser_type_parameter (parser, is_parameter_pack);
11153 /* Otherwise, it is a non-type parameter.
11157 When parsing a default template-argument for a non-type
11158 template-parameter, the first non-nested `>' is taken as the end
11159 of the template parameter-list rather than a greater-than
11161 *is_non_type = true;
11162 parameter_declarator
11163 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11164 /*parenthesized_p=*/NULL);
11166 /* If the parameter declaration is marked as a parameter pack, set
11167 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11168 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11170 if (parameter_declarator
11171 && parameter_declarator->declarator
11172 && parameter_declarator->declarator->parameter_pack_p)
11174 *is_parameter_pack = true;
11175 parameter_declarator->declarator->parameter_pack_p = false;
11178 /* If the next token is an ellipsis, and we don't already have it
11179 marked as a parameter pack, then we have a parameter pack (that
11180 has no declarator). */
11181 if (!*is_parameter_pack
11182 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11183 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11185 /* Consume the `...'. */
11186 cp_lexer_consume_token (parser->lexer);
11187 maybe_warn_variadic_templates ();
11189 *is_parameter_pack = true;
11191 /* We might end up with a pack expansion as the type of the non-type
11192 template parameter, in which case this is a non-type template
11194 else if (parameter_declarator
11195 && parameter_declarator->decl_specifiers.type
11196 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11198 *is_parameter_pack = true;
11199 parameter_declarator->decl_specifiers.type =
11200 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11203 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11205 /* Parameter packs cannot have default arguments. However, a
11206 user may try to do so, so we'll parse them and give an
11207 appropriate diagnostic here. */
11209 /* Consume the `='. */
11210 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11211 cp_lexer_consume_token (parser->lexer);
11213 /* Find the name of the parameter pack. */
11214 id_declarator = parameter_declarator->declarator;
11215 while (id_declarator && id_declarator->kind != cdk_id)
11216 id_declarator = id_declarator->declarator;
11218 if (id_declarator && id_declarator->kind == cdk_id)
11219 error_at (start_token->location,
11220 "template parameter pack %qD cannot have a default argument",
11221 id_declarator->u.id.unqualified_name);
11223 error_at (start_token->location,
11224 "template parameter pack cannot have a default argument");
11226 /* Parse the default argument, but throw away the result. */
11227 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11230 parm = grokdeclarator (parameter_declarator->declarator,
11231 ¶meter_declarator->decl_specifiers,
11232 TPARM, /*initialized=*/0,
11233 /*attrlist=*/NULL);
11234 if (parm == error_mark_node)
11235 return error_mark_node;
11237 return build_tree_list (parameter_declarator->default_argument, parm);
11240 /* Parse a type-parameter.
11243 class identifier [opt]
11244 class identifier [opt] = type-id
11245 typename identifier [opt]
11246 typename identifier [opt] = type-id
11247 template < template-parameter-list > class identifier [opt]
11248 template < template-parameter-list > class identifier [opt]
11251 GNU Extension (variadic templates):
11254 class ... identifier [opt]
11255 typename ... identifier [opt]
11257 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11258 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11259 the declaration of the parameter.
11261 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11264 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11269 /* Look for a keyword to tell us what kind of parameter this is. */
11270 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11272 return error_mark_node;
11274 switch (token->keyword)
11280 tree default_argument;
11282 /* If the next token is an ellipsis, we have a template
11284 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11286 /* Consume the `...' token. */
11287 cp_lexer_consume_token (parser->lexer);
11288 maybe_warn_variadic_templates ();
11290 *is_parameter_pack = true;
11293 /* If the next token is an identifier, then it names the
11295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11296 identifier = cp_parser_identifier (parser);
11298 identifier = NULL_TREE;
11300 /* Create the parameter. */
11301 parameter = finish_template_type_parm (class_type_node, identifier);
11303 /* If the next token is an `=', we have a default argument. */
11304 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11306 /* Consume the `=' token. */
11307 cp_lexer_consume_token (parser->lexer);
11308 /* Parse the default-argument. */
11309 push_deferring_access_checks (dk_no_deferred);
11310 default_argument = cp_parser_type_id (parser);
11312 /* Template parameter packs cannot have default
11314 if (*is_parameter_pack)
11317 error_at (token->location,
11318 "template parameter pack %qD cannot have a "
11319 "default argument", identifier);
11321 error_at (token->location,
11322 "template parameter packs cannot have "
11323 "default arguments");
11324 default_argument = NULL_TREE;
11326 pop_deferring_access_checks ();
11329 default_argument = NULL_TREE;
11331 /* Create the combined representation of the parameter and the
11332 default argument. */
11333 parameter = build_tree_list (default_argument, parameter);
11340 tree default_argument;
11342 /* Look for the `<'. */
11343 cp_parser_require (parser, CPP_LESS, RT_LESS);
11344 /* Parse the template-parameter-list. */
11345 cp_parser_template_parameter_list (parser);
11346 /* Look for the `>'. */
11347 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11348 /* Look for the `class' keyword. */
11349 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11350 /* If the next token is an ellipsis, we have a template
11352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11354 /* Consume the `...' token. */
11355 cp_lexer_consume_token (parser->lexer);
11356 maybe_warn_variadic_templates ();
11358 *is_parameter_pack = true;
11360 /* If the next token is an `=', then there is a
11361 default-argument. If the next token is a `>', we are at
11362 the end of the parameter-list. If the next token is a `,',
11363 then we are at the end of this parameter. */
11364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11365 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11366 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11368 identifier = cp_parser_identifier (parser);
11369 /* Treat invalid names as if the parameter were nameless. */
11370 if (identifier == error_mark_node)
11371 identifier = NULL_TREE;
11374 identifier = NULL_TREE;
11376 /* Create the template parameter. */
11377 parameter = finish_template_template_parm (class_type_node,
11380 /* If the next token is an `=', then there is a
11381 default-argument. */
11382 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11386 /* Consume the `='. */
11387 cp_lexer_consume_token (parser->lexer);
11388 /* Parse the id-expression. */
11389 push_deferring_access_checks (dk_no_deferred);
11390 /* save token before parsing the id-expression, for error
11392 token = cp_lexer_peek_token (parser->lexer);
11394 = cp_parser_id_expression (parser,
11395 /*template_keyword_p=*/false,
11396 /*check_dependency_p=*/true,
11397 /*template_p=*/&is_template,
11398 /*declarator_p=*/false,
11399 /*optional_p=*/false);
11400 if (TREE_CODE (default_argument) == TYPE_DECL)
11401 /* If the id-expression was a template-id that refers to
11402 a template-class, we already have the declaration here,
11403 so no further lookup is needed. */
11406 /* Look up the name. */
11408 = cp_parser_lookup_name (parser, default_argument,
11410 /*is_template=*/is_template,
11411 /*is_namespace=*/false,
11412 /*check_dependency=*/true,
11413 /*ambiguous_decls=*/NULL,
11415 /* See if the default argument is valid. */
11417 = check_template_template_default_arg (default_argument);
11419 /* Template parameter packs cannot have default
11421 if (*is_parameter_pack)
11424 error_at (token->location,
11425 "template parameter pack %qD cannot "
11426 "have a default argument",
11429 error_at (token->location, "template parameter packs cannot "
11430 "have default arguments");
11431 default_argument = NULL_TREE;
11433 pop_deferring_access_checks ();
11436 default_argument = NULL_TREE;
11438 /* Create the combined representation of the parameter and the
11439 default argument. */
11440 parameter = build_tree_list (default_argument, parameter);
11445 gcc_unreachable ();
11452 /* Parse a template-id.
11455 template-name < template-argument-list [opt] >
11457 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11458 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11459 returned. Otherwise, if the template-name names a function, or set
11460 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11461 names a class, returns a TYPE_DECL for the specialization.
11463 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11464 uninstantiated templates. */
11467 cp_parser_template_id (cp_parser *parser,
11468 bool template_keyword_p,
11469 bool check_dependency_p,
11470 bool is_declaration)
11476 cp_token_position start_of_id = 0;
11477 deferred_access_check *chk;
11478 VEC (deferred_access_check,gc) *access_check;
11479 cp_token *next_token = NULL, *next_token_2 = NULL;
11480 bool is_identifier;
11482 /* If the next token corresponds to a template-id, there is no need
11484 next_token = cp_lexer_peek_token (parser->lexer);
11485 if (next_token->type == CPP_TEMPLATE_ID)
11487 struct tree_check *check_value;
11489 /* Get the stored value. */
11490 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11491 /* Perform any access checks that were deferred. */
11492 access_check = check_value->checks;
11495 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11496 perform_or_defer_access_check (chk->binfo,
11500 /* Return the stored value. */
11501 return check_value->value;
11504 /* Avoid performing name lookup if there is no possibility of
11505 finding a template-id. */
11506 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11507 || (next_token->type == CPP_NAME
11508 && !cp_parser_nth_token_starts_template_argument_list_p
11511 cp_parser_error (parser, "expected template-id");
11512 return error_mark_node;
11515 /* Remember where the template-id starts. */
11516 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11517 start_of_id = cp_lexer_token_position (parser->lexer, false);
11519 push_deferring_access_checks (dk_deferred);
11521 /* Parse the template-name. */
11522 is_identifier = false;
11523 templ = cp_parser_template_name (parser, template_keyword_p,
11524 check_dependency_p,
11527 if (templ == error_mark_node || is_identifier)
11529 pop_deferring_access_checks ();
11533 /* If we find the sequence `[:' after a template-name, it's probably
11534 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11535 parse correctly the argument list. */
11536 next_token = cp_lexer_peek_token (parser->lexer);
11537 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11538 if (next_token->type == CPP_OPEN_SQUARE
11539 && next_token->flags & DIGRAPH
11540 && next_token_2->type == CPP_COLON
11541 && !(next_token_2->flags & PREV_WHITE))
11543 cp_parser_parse_tentatively (parser);
11544 /* Change `:' into `::'. */
11545 next_token_2->type = CPP_SCOPE;
11546 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11548 cp_lexer_consume_token (parser->lexer);
11550 /* Parse the arguments. */
11551 arguments = cp_parser_enclosed_template_argument_list (parser);
11552 if (!cp_parser_parse_definitely (parser))
11554 /* If we couldn't parse an argument list, then we revert our changes
11555 and return simply an error. Maybe this is not a template-id
11557 next_token_2->type = CPP_COLON;
11558 cp_parser_error (parser, "expected %<<%>");
11559 pop_deferring_access_checks ();
11560 return error_mark_node;
11562 /* Otherwise, emit an error about the invalid digraph, but continue
11563 parsing because we got our argument list. */
11564 if (permerror (next_token->location,
11565 "%<<::%> cannot begin a template-argument list"))
11567 static bool hint = false;
11568 inform (next_token->location,
11569 "%<<:%> is an alternate spelling for %<[%>."
11570 " Insert whitespace between %<<%> and %<::%>");
11571 if (!hint && !flag_permissive)
11573 inform (next_token->location, "(if you use %<-fpermissive%>"
11574 " G++ will accept your code)");
11581 /* Look for the `<' that starts the template-argument-list. */
11582 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11584 pop_deferring_access_checks ();
11585 return error_mark_node;
11587 /* Parse the arguments. */
11588 arguments = cp_parser_enclosed_template_argument_list (parser);
11591 /* Build a representation of the specialization. */
11592 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11593 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11594 else if (DECL_CLASS_TEMPLATE_P (templ)
11595 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11597 bool entering_scope;
11598 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11599 template (rather than some instantiation thereof) only if
11600 is not nested within some other construct. For example, in
11601 "template <typename T> void f(T) { A<T>::", A<T> is just an
11602 instantiation of A. */
11603 entering_scope = (template_parm_scope_p ()
11604 && cp_lexer_next_token_is (parser->lexer,
11607 = finish_template_type (templ, arguments, entering_scope);
11611 /* If it's not a class-template or a template-template, it should be
11612 a function-template. */
11613 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11614 || TREE_CODE (templ) == OVERLOAD
11615 || BASELINK_P (templ)));
11617 template_id = lookup_template_function (templ, arguments);
11620 /* If parsing tentatively, replace the sequence of tokens that makes
11621 up the template-id with a CPP_TEMPLATE_ID token. That way,
11622 should we re-parse the token stream, we will not have to repeat
11623 the effort required to do the parse, nor will we issue duplicate
11624 error messages about problems during instantiation of the
11628 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11630 /* Reset the contents of the START_OF_ID token. */
11631 token->type = CPP_TEMPLATE_ID;
11632 /* Retrieve any deferred checks. Do not pop this access checks yet
11633 so the memory will not be reclaimed during token replacing below. */
11634 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11635 token->u.tree_check_value->value = template_id;
11636 token->u.tree_check_value->checks = get_deferred_access_checks ();
11637 token->keyword = RID_MAX;
11639 /* Purge all subsequent tokens. */
11640 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11642 /* ??? Can we actually assume that, if template_id ==
11643 error_mark_node, we will have issued a diagnostic to the
11644 user, as opposed to simply marking the tentative parse as
11646 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11647 error_at (token->location, "parse error in template argument list");
11650 pop_deferring_access_checks ();
11651 return template_id;
11654 /* Parse a template-name.
11659 The standard should actually say:
11663 operator-function-id
11665 A defect report has been filed about this issue.
11667 A conversion-function-id cannot be a template name because they cannot
11668 be part of a template-id. In fact, looking at this code:
11670 a.operator K<int>()
11672 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11673 It is impossible to call a templated conversion-function-id with an
11674 explicit argument list, since the only allowed template parameter is
11675 the type to which it is converting.
11677 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11678 `template' keyword, in a construction like:
11682 In that case `f' is taken to be a template-name, even though there
11683 is no way of knowing for sure.
11685 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11686 name refers to a set of overloaded functions, at least one of which
11687 is a template, or an IDENTIFIER_NODE with the name of the template,
11688 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11689 names are looked up inside uninstantiated templates. */
11692 cp_parser_template_name (cp_parser* parser,
11693 bool template_keyword_p,
11694 bool check_dependency_p,
11695 bool is_declaration,
11696 bool *is_identifier)
11701 cp_token *token = cp_lexer_peek_token (parser->lexer);
11703 /* If the next token is `operator', then we have either an
11704 operator-function-id or a conversion-function-id. */
11705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11707 /* We don't know whether we're looking at an
11708 operator-function-id or a conversion-function-id. */
11709 cp_parser_parse_tentatively (parser);
11710 /* Try an operator-function-id. */
11711 identifier = cp_parser_operator_function_id (parser);
11712 /* If that didn't work, try a conversion-function-id. */
11713 if (!cp_parser_parse_definitely (parser))
11715 cp_parser_error (parser, "expected template-name");
11716 return error_mark_node;
11719 /* Look for the identifier. */
11721 identifier = cp_parser_identifier (parser);
11723 /* If we didn't find an identifier, we don't have a template-id. */
11724 if (identifier == error_mark_node)
11725 return error_mark_node;
11727 /* If the name immediately followed the `template' keyword, then it
11728 is a template-name. However, if the next token is not `<', then
11729 we do not treat it as a template-name, since it is not being used
11730 as part of a template-id. This enables us to handle constructs
11733 template <typename T> struct S { S(); };
11734 template <typename T> S<T>::S();
11736 correctly. We would treat `S' as a template -- if it were `S<T>'
11737 -- but we do not if there is no `<'. */
11739 if (processing_template_decl
11740 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11742 /* In a declaration, in a dependent context, we pretend that the
11743 "template" keyword was present in order to improve error
11744 recovery. For example, given:
11746 template <typename T> void f(T::X<int>);
11748 we want to treat "X<int>" as a template-id. */
11750 && !template_keyword_p
11751 && parser->scope && TYPE_P (parser->scope)
11752 && check_dependency_p
11753 && dependent_scope_p (parser->scope)
11754 /* Do not do this for dtors (or ctors), since they never
11755 need the template keyword before their name. */
11756 && !constructor_name_p (identifier, parser->scope))
11758 cp_token_position start = 0;
11760 /* Explain what went wrong. */
11761 error_at (token->location, "non-template %qD used as template",
11763 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11764 parser->scope, identifier);
11765 /* If parsing tentatively, find the location of the "<" token. */
11766 if (cp_parser_simulate_error (parser))
11767 start = cp_lexer_token_position (parser->lexer, true);
11768 /* Parse the template arguments so that we can issue error
11769 messages about them. */
11770 cp_lexer_consume_token (parser->lexer);
11771 cp_parser_enclosed_template_argument_list (parser);
11772 /* Skip tokens until we find a good place from which to
11773 continue parsing. */
11774 cp_parser_skip_to_closing_parenthesis (parser,
11775 /*recovering=*/true,
11777 /*consume_paren=*/false);
11778 /* If parsing tentatively, permanently remove the
11779 template argument list. That will prevent duplicate
11780 error messages from being issued about the missing
11781 "template" keyword. */
11783 cp_lexer_purge_tokens_after (parser->lexer, start);
11785 *is_identifier = true;
11789 /* If the "template" keyword is present, then there is generally
11790 no point in doing name-lookup, so we just return IDENTIFIER.
11791 But, if the qualifying scope is non-dependent then we can
11792 (and must) do name-lookup normally. */
11793 if (template_keyword_p
11795 || (TYPE_P (parser->scope)
11796 && dependent_type_p (parser->scope))))
11800 /* Look up the name. */
11801 decl = cp_parser_lookup_name (parser, identifier,
11803 /*is_template=*/true,
11804 /*is_namespace=*/false,
11805 check_dependency_p,
11806 /*ambiguous_decls=*/NULL,
11809 /* If DECL is a template, then the name was a template-name. */
11810 if (TREE_CODE (decl) == TEMPLATE_DECL)
11814 tree fn = NULL_TREE;
11816 /* The standard does not explicitly indicate whether a name that
11817 names a set of overloaded declarations, some of which are
11818 templates, is a template-name. However, such a name should
11819 be a template-name; otherwise, there is no way to form a
11820 template-id for the overloaded templates. */
11821 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11822 if (TREE_CODE (fns) == OVERLOAD)
11823 for (fn = fns; fn; fn = OVL_NEXT (fn))
11824 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11829 /* The name does not name a template. */
11830 cp_parser_error (parser, "expected template-name");
11831 return error_mark_node;
11835 /* If DECL is dependent, and refers to a function, then just return
11836 its name; we will look it up again during template instantiation. */
11837 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11839 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11840 if (TYPE_P (scope) && dependent_type_p (scope))
11847 /* Parse a template-argument-list.
11849 template-argument-list:
11850 template-argument ... [opt]
11851 template-argument-list , template-argument ... [opt]
11853 Returns a TREE_VEC containing the arguments. */
11856 cp_parser_template_argument_list (cp_parser* parser)
11858 tree fixed_args[10];
11859 unsigned n_args = 0;
11860 unsigned alloced = 10;
11861 tree *arg_ary = fixed_args;
11863 bool saved_in_template_argument_list_p;
11865 bool saved_non_ice_p;
11867 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11868 parser->in_template_argument_list_p = true;
11869 /* Even if the template-id appears in an integral
11870 constant-expression, the contents of the argument list do
11872 saved_ice_p = parser->integral_constant_expression_p;
11873 parser->integral_constant_expression_p = false;
11874 saved_non_ice_p = parser->non_integral_constant_expression_p;
11875 parser->non_integral_constant_expression_p = false;
11876 /* Parse the arguments. */
11882 /* Consume the comma. */
11883 cp_lexer_consume_token (parser->lexer);
11885 /* Parse the template-argument. */
11886 argument = cp_parser_template_argument (parser);
11888 /* If the next token is an ellipsis, we're expanding a template
11890 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11892 if (argument == error_mark_node)
11894 cp_token *token = cp_lexer_peek_token (parser->lexer);
11895 error_at (token->location,
11896 "expected parameter pack before %<...%>");
11898 /* Consume the `...' token. */
11899 cp_lexer_consume_token (parser->lexer);
11901 /* Make the argument into a TYPE_PACK_EXPANSION or
11902 EXPR_PACK_EXPANSION. */
11903 argument = make_pack_expansion (argument);
11906 if (n_args == alloced)
11910 if (arg_ary == fixed_args)
11912 arg_ary = XNEWVEC (tree, alloced);
11913 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11916 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11918 arg_ary[n_args++] = argument;
11920 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11922 vec = make_tree_vec (n_args);
11925 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11927 if (arg_ary != fixed_args)
11929 parser->non_integral_constant_expression_p = saved_non_ice_p;
11930 parser->integral_constant_expression_p = saved_ice_p;
11931 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11932 #ifdef ENABLE_CHECKING
11933 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11938 /* Parse a template-argument.
11941 assignment-expression
11945 The representation is that of an assignment-expression, type-id, or
11946 id-expression -- except that the qualified id-expression is
11947 evaluated, so that the value returned is either a DECL or an
11950 Although the standard says "assignment-expression", it forbids
11951 throw-expressions or assignments in the template argument.
11952 Therefore, we use "conditional-expression" instead. */
11955 cp_parser_template_argument (cp_parser* parser)
11960 bool maybe_type_id = false;
11961 cp_token *token = NULL, *argument_start_token = NULL;
11964 /* There's really no way to know what we're looking at, so we just
11965 try each alternative in order.
11969 In a template-argument, an ambiguity between a type-id and an
11970 expression is resolved to a type-id, regardless of the form of
11971 the corresponding template-parameter.
11973 Therefore, we try a type-id first. */
11974 cp_parser_parse_tentatively (parser);
11975 argument = cp_parser_template_type_arg (parser);
11976 /* If there was no error parsing the type-id but the next token is a
11977 '>>', our behavior depends on which dialect of C++ we're
11978 parsing. In C++98, we probably found a typo for '> >'. But there
11979 are type-id which are also valid expressions. For instance:
11981 struct X { int operator >> (int); };
11982 template <int V> struct Foo {};
11985 Here 'X()' is a valid type-id of a function type, but the user just
11986 wanted to write the expression "X() >> 5". Thus, we remember that we
11987 found a valid type-id, but we still try to parse the argument as an
11988 expression to see what happens.
11990 In C++0x, the '>>' will be considered two separate '>'
11992 if (!cp_parser_error_occurred (parser)
11993 && cxx_dialect == cxx98
11994 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11996 maybe_type_id = true;
11997 cp_parser_abort_tentative_parse (parser);
12001 /* If the next token isn't a `,' or a `>', then this argument wasn't
12002 really finished. This means that the argument is not a valid
12004 if (!cp_parser_next_token_ends_template_argument_p (parser))
12005 cp_parser_error (parser, "expected template-argument");
12006 /* If that worked, we're done. */
12007 if (cp_parser_parse_definitely (parser))
12010 /* We're still not sure what the argument will be. */
12011 cp_parser_parse_tentatively (parser);
12012 /* Try a template. */
12013 argument_start_token = cp_lexer_peek_token (parser->lexer);
12014 argument = cp_parser_id_expression (parser,
12015 /*template_keyword_p=*/false,
12016 /*check_dependency_p=*/true,
12018 /*declarator_p=*/false,
12019 /*optional_p=*/false);
12020 /* If the next token isn't a `,' or a `>', then this argument wasn't
12021 really finished. */
12022 if (!cp_parser_next_token_ends_template_argument_p (parser))
12023 cp_parser_error (parser, "expected template-argument");
12024 if (!cp_parser_error_occurred (parser))
12026 /* Figure out what is being referred to. If the id-expression
12027 was for a class template specialization, then we will have a
12028 TYPE_DECL at this point. There is no need to do name lookup
12029 at this point in that case. */
12030 if (TREE_CODE (argument) != TYPE_DECL)
12031 argument = cp_parser_lookup_name (parser, argument,
12033 /*is_template=*/template_p,
12034 /*is_namespace=*/false,
12035 /*check_dependency=*/true,
12036 /*ambiguous_decls=*/NULL,
12037 argument_start_token->location);
12038 if (TREE_CODE (argument) != TEMPLATE_DECL
12039 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12040 cp_parser_error (parser, "expected template-name");
12042 if (cp_parser_parse_definitely (parser))
12044 /* It must be a non-type argument. There permitted cases are given
12045 in [temp.arg.nontype]:
12047 -- an integral constant-expression of integral or enumeration
12050 -- the name of a non-type template-parameter; or
12052 -- the name of an object or function with external linkage...
12054 -- the address of an object or function with external linkage...
12056 -- a pointer to member... */
12057 /* Look for a non-type template parameter. */
12058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12060 cp_parser_parse_tentatively (parser);
12061 argument = cp_parser_primary_expression (parser,
12062 /*address_p=*/false,
12064 /*template_arg_p=*/true,
12066 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12067 || !cp_parser_next_token_ends_template_argument_p (parser))
12068 cp_parser_simulate_error (parser);
12069 if (cp_parser_parse_definitely (parser))
12073 /* If the next token is "&", the argument must be the address of an
12074 object or function with external linkage. */
12075 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12077 cp_lexer_consume_token (parser->lexer);
12078 /* See if we might have an id-expression. */
12079 token = cp_lexer_peek_token (parser->lexer);
12080 if (token->type == CPP_NAME
12081 || token->keyword == RID_OPERATOR
12082 || token->type == CPP_SCOPE
12083 || token->type == CPP_TEMPLATE_ID
12084 || token->type == CPP_NESTED_NAME_SPECIFIER)
12086 cp_parser_parse_tentatively (parser);
12087 argument = cp_parser_primary_expression (parser,
12090 /*template_arg_p=*/true,
12092 if (cp_parser_error_occurred (parser)
12093 || !cp_parser_next_token_ends_template_argument_p (parser))
12094 cp_parser_abort_tentative_parse (parser);
12099 if (TREE_CODE (argument) == INDIRECT_REF)
12101 gcc_assert (REFERENCE_REF_P (argument));
12102 argument = TREE_OPERAND (argument, 0);
12105 /* If we're in a template, we represent a qualified-id referring
12106 to a static data member as a SCOPE_REF even if the scope isn't
12107 dependent so that we can check access control later. */
12109 if (TREE_CODE (probe) == SCOPE_REF)
12110 probe = TREE_OPERAND (probe, 1);
12111 if (TREE_CODE (probe) == VAR_DECL)
12113 /* A variable without external linkage might still be a
12114 valid constant-expression, so no error is issued here
12115 if the external-linkage check fails. */
12116 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12117 cp_parser_simulate_error (parser);
12119 else if (is_overloaded_fn (argument))
12120 /* All overloaded functions are allowed; if the external
12121 linkage test does not pass, an error will be issued
12125 && (TREE_CODE (argument) == OFFSET_REF
12126 || TREE_CODE (argument) == SCOPE_REF))
12127 /* A pointer-to-member. */
12129 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12132 cp_parser_simulate_error (parser);
12134 if (cp_parser_parse_definitely (parser))
12137 argument = build_x_unary_op (ADDR_EXPR, argument,
12138 tf_warning_or_error);
12143 /* If the argument started with "&", there are no other valid
12144 alternatives at this point. */
12147 cp_parser_error (parser, "invalid non-type template argument");
12148 return error_mark_node;
12151 /* If the argument wasn't successfully parsed as a type-id followed
12152 by '>>', the argument can only be a constant expression now.
12153 Otherwise, we try parsing the constant-expression tentatively,
12154 because the argument could really be a type-id. */
12156 cp_parser_parse_tentatively (parser);
12157 argument = cp_parser_constant_expression (parser,
12158 /*allow_non_constant_p=*/false,
12159 /*non_constant_p=*/NULL);
12160 argument = fold_non_dependent_expr (argument);
12161 if (!maybe_type_id)
12163 if (!cp_parser_next_token_ends_template_argument_p (parser))
12164 cp_parser_error (parser, "expected template-argument");
12165 if (cp_parser_parse_definitely (parser))
12167 /* We did our best to parse the argument as a non type-id, but that
12168 was the only alternative that matched (albeit with a '>' after
12169 it). We can assume it's just a typo from the user, and a
12170 diagnostic will then be issued. */
12171 return cp_parser_template_type_arg (parser);
12174 /* Parse an explicit-instantiation.
12176 explicit-instantiation:
12177 template declaration
12179 Although the standard says `declaration', what it really means is:
12181 explicit-instantiation:
12182 template decl-specifier-seq [opt] declarator [opt] ;
12184 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12185 supposed to be allowed. A defect report has been filed about this
12190 explicit-instantiation:
12191 storage-class-specifier template
12192 decl-specifier-seq [opt] declarator [opt] ;
12193 function-specifier template
12194 decl-specifier-seq [opt] declarator [opt] ; */
12197 cp_parser_explicit_instantiation (cp_parser* parser)
12199 int declares_class_or_enum;
12200 cp_decl_specifier_seq decl_specifiers;
12201 tree extension_specifier = NULL_TREE;
12203 /* Look for an (optional) storage-class-specifier or
12204 function-specifier. */
12205 if (cp_parser_allow_gnu_extensions_p (parser))
12207 extension_specifier
12208 = cp_parser_storage_class_specifier_opt (parser);
12209 if (!extension_specifier)
12210 extension_specifier
12211 = cp_parser_function_specifier_opt (parser,
12212 /*decl_specs=*/NULL);
12215 /* Look for the `template' keyword. */
12216 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12217 /* Let the front end know that we are processing an explicit
12219 begin_explicit_instantiation ();
12220 /* [temp.explicit] says that we are supposed to ignore access
12221 control while processing explicit instantiation directives. */
12222 push_deferring_access_checks (dk_no_check);
12223 /* Parse a decl-specifier-seq. */
12224 cp_parser_decl_specifier_seq (parser,
12225 CP_PARSER_FLAGS_OPTIONAL,
12227 &declares_class_or_enum);
12228 /* If there was exactly one decl-specifier, and it declared a class,
12229 and there's no declarator, then we have an explicit type
12231 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12235 type = check_tag_decl (&decl_specifiers);
12236 /* Turn access control back on for names used during
12237 template instantiation. */
12238 pop_deferring_access_checks ();
12240 do_type_instantiation (type, extension_specifier,
12241 /*complain=*/tf_error);
12245 cp_declarator *declarator;
12248 /* Parse the declarator. */
12250 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12251 /*ctor_dtor_or_conv_p=*/NULL,
12252 /*parenthesized_p=*/NULL,
12253 /*member_p=*/false);
12254 if (declares_class_or_enum & 2)
12255 cp_parser_check_for_definition_in_return_type (declarator,
12256 decl_specifiers.type,
12257 decl_specifiers.type_location);
12258 if (declarator != cp_error_declarator)
12260 if (decl_specifiers.specs[(int)ds_inline])
12261 permerror (input_location, "explicit instantiation shall not use"
12262 " %<inline%> specifier");
12263 if (decl_specifiers.specs[(int)ds_constexpr])
12264 permerror (input_location, "explicit instantiation shall not use"
12265 " %<constexpr%> specifier");
12267 decl = grokdeclarator (declarator, &decl_specifiers,
12268 NORMAL, 0, &decl_specifiers.attributes);
12269 /* Turn access control back on for names used during
12270 template instantiation. */
12271 pop_deferring_access_checks ();
12272 /* Do the explicit instantiation. */
12273 do_decl_instantiation (decl, extension_specifier);
12277 pop_deferring_access_checks ();
12278 /* Skip the body of the explicit instantiation. */
12279 cp_parser_skip_to_end_of_statement (parser);
12282 /* We're done with the instantiation. */
12283 end_explicit_instantiation ();
12285 cp_parser_consume_semicolon_at_end_of_statement (parser);
12288 /* Parse an explicit-specialization.
12290 explicit-specialization:
12291 template < > declaration
12293 Although the standard says `declaration', what it really means is:
12295 explicit-specialization:
12296 template <> decl-specifier [opt] init-declarator [opt] ;
12297 template <> function-definition
12298 template <> explicit-specialization
12299 template <> template-declaration */
12302 cp_parser_explicit_specialization (cp_parser* parser)
12304 bool need_lang_pop;
12305 cp_token *token = cp_lexer_peek_token (parser->lexer);
12307 /* Look for the `template' keyword. */
12308 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12309 /* Look for the `<'. */
12310 cp_parser_require (parser, CPP_LESS, RT_LESS);
12311 /* Look for the `>'. */
12312 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12313 /* We have processed another parameter list. */
12314 ++parser->num_template_parameter_lists;
12317 A template ... explicit specialization ... shall not have C
12319 if (current_lang_name == lang_name_c)
12321 error_at (token->location, "template specialization with C linkage");
12322 /* Give it C++ linkage to avoid confusing other parts of the
12324 push_lang_context (lang_name_cplusplus);
12325 need_lang_pop = true;
12328 need_lang_pop = false;
12329 /* Let the front end know that we are beginning a specialization. */
12330 if (!begin_specialization ())
12332 end_specialization ();
12336 /* If the next keyword is `template', we need to figure out whether
12337 or not we're looking a template-declaration. */
12338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12340 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12341 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12342 cp_parser_template_declaration_after_export (parser,
12343 /*member_p=*/false);
12345 cp_parser_explicit_specialization (parser);
12348 /* Parse the dependent declaration. */
12349 cp_parser_single_declaration (parser,
12351 /*member_p=*/false,
12352 /*explicit_specialization_p=*/true,
12353 /*friend_p=*/NULL);
12354 /* We're done with the specialization. */
12355 end_specialization ();
12356 /* For the erroneous case of a template with C linkage, we pushed an
12357 implicit C++ linkage scope; exit that scope now. */
12359 pop_lang_context ();
12360 /* We're done with this parameter list. */
12361 --parser->num_template_parameter_lists;
12364 /* Parse a type-specifier.
12367 simple-type-specifier
12370 elaborated-type-specifier
12378 Returns a representation of the type-specifier. For a
12379 class-specifier, enum-specifier, or elaborated-type-specifier, a
12380 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12382 The parser flags FLAGS is used to control type-specifier parsing.
12384 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12385 in a decl-specifier-seq.
12387 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12388 class-specifier, enum-specifier, or elaborated-type-specifier, then
12389 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12390 if a type is declared; 2 if it is defined. Otherwise, it is set to
12393 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12394 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12395 is set to FALSE. */
12398 cp_parser_type_specifier (cp_parser* parser,
12399 cp_parser_flags flags,
12400 cp_decl_specifier_seq *decl_specs,
12401 bool is_declaration,
12402 int* declares_class_or_enum,
12403 bool* is_cv_qualifier)
12405 tree type_spec = NULL_TREE;
12408 cp_decl_spec ds = ds_last;
12410 /* Assume this type-specifier does not declare a new type. */
12411 if (declares_class_or_enum)
12412 *declares_class_or_enum = 0;
12413 /* And that it does not specify a cv-qualifier. */
12414 if (is_cv_qualifier)
12415 *is_cv_qualifier = false;
12416 /* Peek at the next token. */
12417 token = cp_lexer_peek_token (parser->lexer);
12419 /* If we're looking at a keyword, we can use that to guide the
12420 production we choose. */
12421 keyword = token->keyword;
12425 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12426 goto elaborated_type_specifier;
12428 /* Look for the enum-specifier. */
12429 type_spec = cp_parser_enum_specifier (parser);
12430 /* If that worked, we're done. */
12433 if (declares_class_or_enum)
12434 *declares_class_or_enum = 2;
12436 cp_parser_set_decl_spec_type (decl_specs,
12439 /*user_defined_p=*/true);
12443 goto elaborated_type_specifier;
12445 /* Any of these indicate either a class-specifier, or an
12446 elaborated-type-specifier. */
12450 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12451 goto elaborated_type_specifier;
12453 /* Parse tentatively so that we can back up if we don't find a
12454 class-specifier. */
12455 cp_parser_parse_tentatively (parser);
12456 /* Look for the class-specifier. */
12457 type_spec = cp_parser_class_specifier (parser);
12458 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12459 /* If that worked, we're done. */
12460 if (cp_parser_parse_definitely (parser))
12462 if (declares_class_or_enum)
12463 *declares_class_or_enum = 2;
12465 cp_parser_set_decl_spec_type (decl_specs,
12468 /*user_defined_p=*/true);
12472 /* Fall through. */
12473 elaborated_type_specifier:
12474 /* We're declaring (not defining) a class or enum. */
12475 if (declares_class_or_enum)
12476 *declares_class_or_enum = 1;
12478 /* Fall through. */
12480 /* Look for an elaborated-type-specifier. */
12482 = (cp_parser_elaborated_type_specifier
12484 decl_specs && decl_specs->specs[(int) ds_friend],
12487 cp_parser_set_decl_spec_type (decl_specs,
12490 /*user_defined_p=*/true);
12495 if (is_cv_qualifier)
12496 *is_cv_qualifier = true;
12501 if (is_cv_qualifier)
12502 *is_cv_qualifier = true;
12507 if (is_cv_qualifier)
12508 *is_cv_qualifier = true;
12512 /* The `__complex__' keyword is a GNU extension. */
12520 /* Handle simple keywords. */
12525 ++decl_specs->specs[(int)ds];
12526 decl_specs->any_specifiers_p = true;
12528 return cp_lexer_consume_token (parser->lexer)->u.value;
12531 /* If we do not already have a type-specifier, assume we are looking
12532 at a simple-type-specifier. */
12533 type_spec = cp_parser_simple_type_specifier (parser,
12537 /* If we didn't find a type-specifier, and a type-specifier was not
12538 optional in this context, issue an error message. */
12539 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12541 cp_parser_error (parser, "expected type specifier");
12542 return error_mark_node;
12548 /* Parse a simple-type-specifier.
12550 simple-type-specifier:
12551 :: [opt] nested-name-specifier [opt] type-name
12552 :: [opt] nested-name-specifier template template-id
12567 simple-type-specifier:
12569 decltype ( expression )
12575 simple-type-specifier:
12577 __typeof__ unary-expression
12578 __typeof__ ( type-id )
12580 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12581 appropriately updated. */
12584 cp_parser_simple_type_specifier (cp_parser* parser,
12585 cp_decl_specifier_seq *decl_specs,
12586 cp_parser_flags flags)
12588 tree type = NULL_TREE;
12591 /* Peek at the next token. */
12592 token = cp_lexer_peek_token (parser->lexer);
12594 /* If we're looking at a keyword, things are easy. */
12595 switch (token->keyword)
12599 decl_specs->explicit_char_p = true;
12600 type = char_type_node;
12603 type = char16_type_node;
12606 type = char32_type_node;
12609 type = wchar_type_node;
12612 type = boolean_type_node;
12616 ++decl_specs->specs[(int) ds_short];
12617 type = short_integer_type_node;
12621 decl_specs->explicit_int_p = true;
12622 type = integer_type_node;
12625 if (!int128_integer_type_node)
12628 decl_specs->explicit_int128_p = true;
12629 type = int128_integer_type_node;
12633 ++decl_specs->specs[(int) ds_long];
12634 type = long_integer_type_node;
12638 ++decl_specs->specs[(int) ds_signed];
12639 type = integer_type_node;
12643 ++decl_specs->specs[(int) ds_unsigned];
12644 type = unsigned_type_node;
12647 type = float_type_node;
12650 type = double_type_node;
12653 type = void_type_node;
12657 maybe_warn_cpp0x (CPP0X_AUTO);
12658 type = make_auto ();
12662 /* Parse the `decltype' type. */
12663 type = cp_parser_decltype (parser);
12666 cp_parser_set_decl_spec_type (decl_specs, type,
12668 /*user_defined_p=*/true);
12673 /* Consume the `typeof' token. */
12674 cp_lexer_consume_token (parser->lexer);
12675 /* Parse the operand to `typeof'. */
12676 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12677 /* If it is not already a TYPE, take its type. */
12678 if (!TYPE_P (type))
12679 type = finish_typeof (type);
12682 cp_parser_set_decl_spec_type (decl_specs, type,
12684 /*user_defined_p=*/true);
12692 /* If the type-specifier was for a built-in type, we're done. */
12695 /* Record the type. */
12697 && (token->keyword != RID_SIGNED
12698 && token->keyword != RID_UNSIGNED
12699 && token->keyword != RID_SHORT
12700 && token->keyword != RID_LONG))
12701 cp_parser_set_decl_spec_type (decl_specs,
12704 /*user_defined=*/false);
12706 decl_specs->any_specifiers_p = true;
12708 /* Consume the token. */
12709 cp_lexer_consume_token (parser->lexer);
12711 /* There is no valid C++ program where a non-template type is
12712 followed by a "<". That usually indicates that the user thought
12713 that the type was a template. */
12714 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12716 return TYPE_NAME (type);
12719 /* The type-specifier must be a user-defined type. */
12720 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12725 /* Don't gobble tokens or issue error messages if this is an
12726 optional type-specifier. */
12727 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12728 cp_parser_parse_tentatively (parser);
12730 /* Look for the optional `::' operator. */
12732 = (cp_parser_global_scope_opt (parser,
12733 /*current_scope_valid_p=*/false)
12735 /* Look for the nested-name specifier. */
12737 = (cp_parser_nested_name_specifier_opt (parser,
12738 /*typename_keyword_p=*/false,
12739 /*check_dependency_p=*/true,
12741 /*is_declaration=*/false)
12743 token = cp_lexer_peek_token (parser->lexer);
12744 /* If we have seen a nested-name-specifier, and the next token
12745 is `template', then we are using the template-id production. */
12747 && cp_parser_optional_template_keyword (parser))
12749 /* Look for the template-id. */
12750 type = cp_parser_template_id (parser,
12751 /*template_keyword_p=*/true,
12752 /*check_dependency_p=*/true,
12753 /*is_declaration=*/false);
12754 /* If the template-id did not name a type, we are out of
12756 if (TREE_CODE (type) != TYPE_DECL)
12758 cp_parser_error (parser, "expected template-id for type");
12762 /* Otherwise, look for a type-name. */
12764 type = cp_parser_type_name (parser);
12765 /* Keep track of all name-lookups performed in class scopes. */
12769 && TREE_CODE (type) == TYPE_DECL
12770 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12771 maybe_note_name_used_in_class (DECL_NAME (type), type);
12772 /* If it didn't work out, we don't have a TYPE. */
12773 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12774 && !cp_parser_parse_definitely (parser))
12776 if (type && decl_specs)
12777 cp_parser_set_decl_spec_type (decl_specs, type,
12779 /*user_defined=*/true);
12782 /* If we didn't get a type-name, issue an error message. */
12783 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12785 cp_parser_error (parser, "expected type-name");
12786 return error_mark_node;
12789 /* There is no valid C++ program where a non-template type is
12790 followed by a "<". That usually indicates that the user thought
12791 that the type was a template. */
12792 if (type && type != error_mark_node)
12794 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12795 If it is, then the '<'...'>' enclose protocol names rather than
12796 template arguments, and so everything is fine. */
12797 if (c_dialect_objc () && !parser->scope
12798 && (objc_is_id (type) || objc_is_class_name (type)))
12800 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12801 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12803 /* Clobber the "unqualified" type previously entered into
12804 DECL_SPECS with the new, improved protocol-qualified version. */
12806 decl_specs->type = qual_type;
12811 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12818 /* Parse a type-name.
12831 Returns a TYPE_DECL for the type. */
12834 cp_parser_type_name (cp_parser* parser)
12838 /* We can't know yet whether it is a class-name or not. */
12839 cp_parser_parse_tentatively (parser);
12840 /* Try a class-name. */
12841 type_decl = cp_parser_class_name (parser,
12842 /*typename_keyword_p=*/false,
12843 /*template_keyword_p=*/false,
12845 /*check_dependency_p=*/true,
12846 /*class_head_p=*/false,
12847 /*is_declaration=*/false);
12848 /* If it's not a class-name, keep looking. */
12849 if (!cp_parser_parse_definitely (parser))
12851 /* It must be a typedef-name or an enum-name. */
12852 return cp_parser_nonclass_name (parser);
12858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12866 Returns a TYPE_DECL for the type. */
12869 cp_parser_nonclass_name (cp_parser* parser)
12874 cp_token *token = cp_lexer_peek_token (parser->lexer);
12875 identifier = cp_parser_identifier (parser);
12876 if (identifier == error_mark_node)
12877 return error_mark_node;
12879 /* Look up the type-name. */
12880 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12882 if (TREE_CODE (type_decl) != TYPE_DECL
12883 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12885 /* See if this is an Objective-C type. */
12886 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12887 tree type = objc_get_protocol_qualified_type (identifier, protos);
12889 type_decl = TYPE_NAME (type);
12892 /* Issue an error if we did not find a type-name. */
12893 if (TREE_CODE (type_decl) != TYPE_DECL)
12895 if (!cp_parser_simulate_error (parser))
12896 cp_parser_name_lookup_error (parser, identifier, type_decl,
12897 NLE_TYPE, token->location);
12898 return error_mark_node;
12900 /* Remember that the name was used in the definition of the
12901 current class so that we can check later to see if the
12902 meaning would have been different after the class was
12903 entirely defined. */
12904 else if (type_decl != error_mark_node
12906 maybe_note_name_used_in_class (identifier, type_decl);
12911 /* Parse an elaborated-type-specifier. Note that the grammar given
12912 here incorporates the resolution to DR68.
12914 elaborated-type-specifier:
12915 class-key :: [opt] nested-name-specifier [opt] identifier
12916 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12917 enum-key :: [opt] nested-name-specifier [opt] identifier
12918 typename :: [opt] nested-name-specifier identifier
12919 typename :: [opt] nested-name-specifier template [opt]
12924 elaborated-type-specifier:
12925 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12926 class-key attributes :: [opt] nested-name-specifier [opt]
12927 template [opt] template-id
12928 enum attributes :: [opt] nested-name-specifier [opt] identifier
12930 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12931 declared `friend'. If IS_DECLARATION is TRUE, then this
12932 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12933 something is being declared.
12935 Returns the TYPE specified. */
12938 cp_parser_elaborated_type_specifier (cp_parser* parser,
12940 bool is_declaration)
12942 enum tag_types tag_type;
12944 tree type = NULL_TREE;
12945 tree attributes = NULL_TREE;
12947 cp_token *token = NULL;
12949 /* See if we're looking at the `enum' keyword. */
12950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12952 /* Consume the `enum' token. */
12953 cp_lexer_consume_token (parser->lexer);
12954 /* Remember that it's an enumeration type. */
12955 tag_type = enum_type;
12956 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12957 enums) is used here. */
12958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12959 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12961 pedwarn (input_location, 0, "elaborated-type-specifier "
12962 "for a scoped enum must not use the %<%D%> keyword",
12963 cp_lexer_peek_token (parser->lexer)->u.value);
12964 /* Consume the `struct' or `class' and parse it anyway. */
12965 cp_lexer_consume_token (parser->lexer);
12967 /* Parse the attributes. */
12968 attributes = cp_parser_attributes_opt (parser);
12970 /* Or, it might be `typename'. */
12971 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12974 /* Consume the `typename' token. */
12975 cp_lexer_consume_token (parser->lexer);
12976 /* Remember that it's a `typename' type. */
12977 tag_type = typename_type;
12979 /* Otherwise it must be a class-key. */
12982 tag_type = cp_parser_class_key (parser);
12983 if (tag_type == none_type)
12984 return error_mark_node;
12985 /* Parse the attributes. */
12986 attributes = cp_parser_attributes_opt (parser);
12989 /* Look for the `::' operator. */
12990 globalscope = cp_parser_global_scope_opt (parser,
12991 /*current_scope_valid_p=*/false);
12992 /* Look for the nested-name-specifier. */
12993 if (tag_type == typename_type && !globalscope)
12995 if (!cp_parser_nested_name_specifier (parser,
12996 /*typename_keyword_p=*/true,
12997 /*check_dependency_p=*/true,
13000 return error_mark_node;
13003 /* Even though `typename' is not present, the proposed resolution
13004 to Core Issue 180 says that in `class A<T>::B', `B' should be
13005 considered a type-name, even if `A<T>' is dependent. */
13006 cp_parser_nested_name_specifier_opt (parser,
13007 /*typename_keyword_p=*/true,
13008 /*check_dependency_p=*/true,
13011 /* For everything but enumeration types, consider a template-id.
13012 For an enumeration type, consider only a plain identifier. */
13013 if (tag_type != enum_type)
13015 bool template_p = false;
13018 /* Allow the `template' keyword. */
13019 template_p = cp_parser_optional_template_keyword (parser);
13020 /* If we didn't see `template', we don't know if there's a
13021 template-id or not. */
13023 cp_parser_parse_tentatively (parser);
13024 /* Parse the template-id. */
13025 token = cp_lexer_peek_token (parser->lexer);
13026 decl = cp_parser_template_id (parser, template_p,
13027 /*check_dependency_p=*/true,
13029 /* If we didn't find a template-id, look for an ordinary
13031 if (!template_p && !cp_parser_parse_definitely (parser))
13033 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13034 in effect, then we must assume that, upon instantiation, the
13035 template will correspond to a class. */
13036 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13037 && tag_type == typename_type)
13038 type = make_typename_type (parser->scope, decl,
13040 /*complain=*/tf_error);
13041 /* If the `typename' keyword is in effect and DECL is not a type
13042 decl. Then type is non existant. */
13043 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13046 type = TREE_TYPE (decl);
13051 token = cp_lexer_peek_token (parser->lexer);
13052 identifier = cp_parser_identifier (parser);
13054 if (identifier == error_mark_node)
13056 parser->scope = NULL_TREE;
13057 return error_mark_node;
13060 /* For a `typename', we needn't call xref_tag. */
13061 if (tag_type == typename_type
13062 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13063 return cp_parser_make_typename_type (parser, parser->scope,
13066 /* Look up a qualified name in the usual way. */
13070 tree ambiguous_decls;
13072 decl = cp_parser_lookup_name (parser, identifier,
13074 /*is_template=*/false,
13075 /*is_namespace=*/false,
13076 /*check_dependency=*/true,
13080 /* If the lookup was ambiguous, an error will already have been
13082 if (ambiguous_decls)
13083 return error_mark_node;
13085 /* If we are parsing friend declaration, DECL may be a
13086 TEMPLATE_DECL tree node here. However, we need to check
13087 whether this TEMPLATE_DECL results in valid code. Consider
13088 the following example:
13091 template <class T> class C {};
13094 template <class T> friend class N::C; // #1, valid code
13096 template <class T> class Y {
13097 friend class N::C; // #2, invalid code
13100 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13101 name lookup of `N::C'. We see that friend declaration must
13102 be template for the code to be valid. Note that
13103 processing_template_decl does not work here since it is
13104 always 1 for the above two cases. */
13106 decl = (cp_parser_maybe_treat_template_as_class
13107 (decl, /*tag_name_p=*/is_friend
13108 && parser->num_template_parameter_lists));
13110 if (TREE_CODE (decl) != TYPE_DECL)
13112 cp_parser_diagnose_invalid_type_name (parser,
13116 return error_mark_node;
13119 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13121 bool allow_template = (parser->num_template_parameter_lists
13122 || DECL_SELF_REFERENCE_P (decl));
13123 type = check_elaborated_type_specifier (tag_type, decl,
13126 if (type == error_mark_node)
13127 return error_mark_node;
13130 /* Forward declarations of nested types, such as
13135 are invalid unless all components preceding the final '::'
13136 are complete. If all enclosing types are complete, these
13137 declarations become merely pointless.
13139 Invalid forward declarations of nested types are errors
13140 caught elsewhere in parsing. Those that are pointless arrive
13143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13144 && !is_friend && !processing_explicit_instantiation)
13145 warning (0, "declaration %qD does not declare anything", decl);
13147 type = TREE_TYPE (decl);
13151 /* An elaborated-type-specifier sometimes introduces a new type and
13152 sometimes names an existing type. Normally, the rule is that it
13153 introduces a new type only if there is not an existing type of
13154 the same name already in scope. For example, given:
13157 void f() { struct S s; }
13159 the `struct S' in the body of `f' is the same `struct S' as in
13160 the global scope; the existing definition is used. However, if
13161 there were no global declaration, this would introduce a new
13162 local class named `S'.
13164 An exception to this rule applies to the following code:
13166 namespace N { struct S; }
13168 Here, the elaborated-type-specifier names a new type
13169 unconditionally; even if there is already an `S' in the
13170 containing scope this declaration names a new type.
13171 This exception only applies if the elaborated-type-specifier
13172 forms the complete declaration:
13176 A declaration consisting solely of `class-key identifier ;' is
13177 either a redeclaration of the name in the current scope or a
13178 forward declaration of the identifier as a class name. It
13179 introduces the name into the current scope.
13181 We are in this situation precisely when the next token is a `;'.
13183 An exception to the exception is that a `friend' declaration does
13184 *not* name a new type; i.e., given:
13186 struct S { friend struct T; };
13188 `T' is not a new type in the scope of `S'.
13190 Also, `new struct S' or `sizeof (struct S)' never results in the
13191 definition of a new type; a new type can only be declared in a
13192 declaration context. */
13198 /* Friends have special name lookup rules. */
13199 ts = ts_within_enclosing_non_class;
13200 else if (is_declaration
13201 && cp_lexer_next_token_is (parser->lexer,
13203 /* This is a `class-key identifier ;' */
13209 (parser->num_template_parameter_lists
13210 && (cp_parser_next_token_starts_class_definition_p (parser)
13211 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13212 /* An unqualified name was used to reference this type, so
13213 there were no qualifying templates. */
13214 if (!cp_parser_check_template_parameters (parser,
13215 /*num_templates=*/0,
13217 /*declarator=*/NULL))
13218 return error_mark_node;
13219 type = xref_tag (tag_type, identifier, ts, template_p);
13223 if (type == error_mark_node)
13224 return error_mark_node;
13226 /* Allow attributes on forward declarations of classes. */
13229 if (TREE_CODE (type) == TYPENAME_TYPE)
13230 warning (OPT_Wattributes,
13231 "attributes ignored on uninstantiated type");
13232 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13233 && ! processing_explicit_instantiation)
13234 warning (OPT_Wattributes,
13235 "attributes ignored on template instantiation");
13236 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13237 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13239 warning (OPT_Wattributes,
13240 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13243 if (tag_type != enum_type)
13244 cp_parser_check_class_key (tag_type, type);
13246 /* A "<" cannot follow an elaborated type specifier. If that
13247 happens, the user was probably trying to form a template-id. */
13248 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13253 /* Parse an enum-specifier.
13256 enum-head { enumerator-list [opt] }
13259 enum-key identifier [opt] enum-base [opt]
13260 enum-key nested-name-specifier identifier enum-base [opt]
13265 enum struct [C++0x]
13268 : type-specifier-seq
13270 opaque-enum-specifier:
13271 enum-key identifier enum-base [opt] ;
13274 enum-key attributes[opt] identifier [opt] enum-base [opt]
13275 { enumerator-list [opt] }attributes[opt]
13277 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13278 if the token stream isn't an enum-specifier after all. */
13281 cp_parser_enum_specifier (cp_parser* parser)
13284 tree type = NULL_TREE;
13286 tree nested_name_specifier = NULL_TREE;
13288 bool scoped_enum_p = false;
13289 bool has_underlying_type = false;
13290 bool nested_being_defined = false;
13291 bool new_value_list = false;
13292 bool is_new_type = false;
13293 bool is_anonymous = false;
13294 tree underlying_type = NULL_TREE;
13295 cp_token *type_start_token = NULL;
13297 /* Parse tentatively so that we can back up if we don't find a
13299 cp_parser_parse_tentatively (parser);
13301 /* Caller guarantees that the current token is 'enum', an identifier
13302 possibly follows, and the token after that is an opening brace.
13303 If we don't have an identifier, fabricate an anonymous name for
13304 the enumeration being defined. */
13305 cp_lexer_consume_token (parser->lexer);
13307 /* Parse the "class" or "struct", which indicates a scoped
13308 enumeration type in C++0x. */
13309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13310 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13312 if (cxx_dialect < cxx0x)
13313 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13315 /* Consume the `struct' or `class' token. */
13316 cp_lexer_consume_token (parser->lexer);
13318 scoped_enum_p = true;
13321 attributes = cp_parser_attributes_opt (parser);
13323 /* Clear the qualification. */
13324 parser->scope = NULL_TREE;
13325 parser->qualifying_scope = NULL_TREE;
13326 parser->object_scope = NULL_TREE;
13328 /* Figure out in what scope the declaration is being placed. */
13329 prev_scope = current_scope ();
13331 type_start_token = cp_lexer_peek_token (parser->lexer);
13333 push_deferring_access_checks (dk_no_check);
13334 nested_name_specifier
13335 = cp_parser_nested_name_specifier_opt (parser,
13336 /*typename_keyword_p=*/true,
13337 /*check_dependency_p=*/false,
13339 /*is_declaration=*/false);
13341 if (nested_name_specifier)
13345 identifier = cp_parser_identifier (parser);
13346 name = cp_parser_lookup_name (parser, identifier,
13348 /*is_template=*/false,
13349 /*is_namespace=*/false,
13350 /*check_dependency=*/true,
13351 /*ambiguous_decls=*/NULL,
13355 type = TREE_TYPE (name);
13356 if (TREE_CODE (type) == TYPENAME_TYPE)
13358 /* Are template enums allowed in ISO? */
13359 if (template_parm_scope_p ())
13360 pedwarn (type_start_token->location, OPT_pedantic,
13361 "%qD is an enumeration template", name);
13362 /* ignore a typename reference, for it will be solved by name
13368 error_at (type_start_token->location,
13369 "%qD is not an enumerator-name", identifier);
13373 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13374 identifier = cp_parser_identifier (parser);
13377 identifier = make_anon_name ();
13378 is_anonymous = true;
13381 pop_deferring_access_checks ();
13383 /* Check for the `:' that denotes a specified underlying type in C++0x.
13384 Note that a ':' could also indicate a bitfield width, however. */
13385 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13387 cp_decl_specifier_seq type_specifiers;
13389 /* Consume the `:'. */
13390 cp_lexer_consume_token (parser->lexer);
13392 /* Parse the type-specifier-seq. */
13393 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13394 /*is_trailing_return=*/false,
13397 /* At this point this is surely not elaborated type specifier. */
13398 if (!cp_parser_parse_definitely (parser))
13401 if (cxx_dialect < cxx0x)
13402 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13404 has_underlying_type = true;
13406 /* If that didn't work, stop. */
13407 if (type_specifiers.type != error_mark_node)
13409 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13410 /*initialized=*/0, NULL);
13411 if (underlying_type == error_mark_node)
13412 underlying_type = NULL_TREE;
13416 /* Look for the `{' but don't consume it yet. */
13417 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13419 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13421 cp_parser_error (parser, "expected %<{%>");
13422 if (has_underlying_type)
13425 /* An opaque-enum-specifier must have a ';' here. */
13426 if ((scoped_enum_p || underlying_type)
13427 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13429 cp_parser_error (parser, "expected %<;%> or %<{%>");
13430 if (has_underlying_type)
13435 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13438 if (nested_name_specifier)
13440 if (CLASS_TYPE_P (nested_name_specifier))
13442 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13443 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13444 push_scope (nested_name_specifier);
13446 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13448 push_nested_namespace (nested_name_specifier);
13452 /* Issue an error message if type-definitions are forbidden here. */
13453 if (!cp_parser_check_type_definition (parser))
13454 type = error_mark_node;
13456 /* Create the new type. We do this before consuming the opening
13457 brace so the enum will be recorded as being on the line of its
13458 tag (or the 'enum' keyword, if there is no tag). */
13459 type = start_enum (identifier, type, underlying_type,
13460 scoped_enum_p, &is_new_type);
13462 /* If the next token is not '{' it is an opaque-enum-specifier or an
13463 elaborated-type-specifier. */
13464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13466 if (nested_name_specifier)
13468 /* The following catches invalid code such as:
13469 enum class S<int>::E { A, B, C }; */
13470 if (!processing_specialization
13471 && CLASS_TYPE_P (nested_name_specifier)
13472 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13473 error_at (type_start_token->location, "cannot add an enumerator "
13474 "list to a template instantiation");
13476 /* If that scope does not contain the scope in which the
13477 class was originally declared, the program is invalid. */
13478 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13480 if (at_namespace_scope_p ())
13481 error_at (type_start_token->location,
13482 "declaration of %qD in namespace %qD which does not "
13484 type, prev_scope, nested_name_specifier);
13486 error_at (type_start_token->location,
13487 "declaration of %qD in %qD which does not enclose %qD",
13488 type, prev_scope, nested_name_specifier);
13489 type = error_mark_node;
13494 begin_scope (sk_scoped_enum, type);
13496 /* Consume the opening brace. */
13497 cp_lexer_consume_token (parser->lexer);
13499 if (type == error_mark_node)
13500 ; /* Nothing to add */
13501 else if (OPAQUE_ENUM_P (type)
13502 || (cxx_dialect > cxx98 && processing_specialization))
13504 new_value_list = true;
13505 SET_OPAQUE_ENUM_P (type, false);
13506 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13510 error_at (type_start_token->location, "multiple definition of %q#T", type);
13511 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13512 "previous definition here");
13513 type = error_mark_node;
13516 if (type == error_mark_node)
13517 cp_parser_skip_to_end_of_block_or_statement (parser);
13518 /* If the next token is not '}', then there are some enumerators. */
13519 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13520 cp_parser_enumerator_list (parser, type);
13522 /* Consume the final '}'. */
13523 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13530 /* If a ';' follows, then it is an opaque-enum-specifier
13531 and additional restrictions apply. */
13532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13535 error_at (type_start_token->location,
13536 "opaque-enum-specifier without name");
13537 else if (nested_name_specifier)
13538 error_at (type_start_token->location,
13539 "opaque-enum-specifier must use a simple identifier");
13543 /* Look for trailing attributes to apply to this enumeration, and
13544 apply them if appropriate. */
13545 if (cp_parser_allow_gnu_extensions_p (parser))
13547 tree trailing_attr = cp_parser_attributes_opt (parser);
13548 trailing_attr = chainon (trailing_attr, attributes);
13549 cplus_decl_attributes (&type,
13551 (int) ATTR_FLAG_TYPE_IN_PLACE);
13554 /* Finish up the enumeration. */
13555 if (type != error_mark_node)
13557 if (new_value_list)
13558 finish_enum_value_list (type);
13560 finish_enum (type);
13563 if (nested_name_specifier)
13565 if (CLASS_TYPE_P (nested_name_specifier))
13567 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13568 pop_scope (nested_name_specifier);
13570 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13572 pop_nested_namespace (nested_name_specifier);
13578 /* Parse an enumerator-list. The enumerators all have the indicated
13582 enumerator-definition
13583 enumerator-list , enumerator-definition */
13586 cp_parser_enumerator_list (cp_parser* parser, tree type)
13590 /* Parse an enumerator-definition. */
13591 cp_parser_enumerator_definition (parser, type);
13593 /* If the next token is not a ',', we've reached the end of
13595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13597 /* Otherwise, consume the `,' and keep going. */
13598 cp_lexer_consume_token (parser->lexer);
13599 /* If the next token is a `}', there is a trailing comma. */
13600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13602 if (!in_system_header)
13603 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13609 /* Parse an enumerator-definition. The enumerator has the indicated
13612 enumerator-definition:
13614 enumerator = constant-expression
13620 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13626 /* Save the input location because we are interested in the location
13627 of the identifier and not the location of the explicit value. */
13628 loc = cp_lexer_peek_token (parser->lexer)->location;
13630 /* Look for the identifier. */
13631 identifier = cp_parser_identifier (parser);
13632 if (identifier == error_mark_node)
13635 /* If the next token is an '=', then there is an explicit value. */
13636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13638 /* Consume the `=' token. */
13639 cp_lexer_consume_token (parser->lexer);
13640 /* Parse the value. */
13641 value = cp_parser_constant_expression (parser,
13642 /*allow_non_constant_p=*/false,
13648 /* If we are processing a template, make sure the initializer of the
13649 enumerator doesn't contain any bare template parameter pack. */
13650 if (check_for_bare_parameter_packs (value))
13651 value = error_mark_node;
13653 /* Create the enumerator. */
13654 build_enumerator (identifier, value, type, loc);
13657 /* Parse a namespace-name.
13660 original-namespace-name
13663 Returns the NAMESPACE_DECL for the namespace. */
13666 cp_parser_namespace_name (cp_parser* parser)
13669 tree namespace_decl;
13671 cp_token *token = cp_lexer_peek_token (parser->lexer);
13673 /* Get the name of the namespace. */
13674 identifier = cp_parser_identifier (parser);
13675 if (identifier == error_mark_node)
13676 return error_mark_node;
13678 /* Look up the identifier in the currently active scope. Look only
13679 for namespaces, due to:
13681 [basic.lookup.udir]
13683 When looking up a namespace-name in a using-directive or alias
13684 definition, only namespace names are considered.
13688 [basic.lookup.qual]
13690 During the lookup of a name preceding the :: scope resolution
13691 operator, object, function, and enumerator names are ignored.
13693 (Note that cp_parser_qualifying_entity only calls this
13694 function if the token after the name is the scope resolution
13696 namespace_decl = cp_parser_lookup_name (parser, identifier,
13698 /*is_template=*/false,
13699 /*is_namespace=*/true,
13700 /*check_dependency=*/true,
13701 /*ambiguous_decls=*/NULL,
13703 /* If it's not a namespace, issue an error. */
13704 if (namespace_decl == error_mark_node
13705 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13707 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13708 error_at (token->location, "%qD is not a namespace-name", identifier);
13709 cp_parser_error (parser, "expected namespace-name");
13710 namespace_decl = error_mark_node;
13713 return namespace_decl;
13716 /* Parse a namespace-definition.
13718 namespace-definition:
13719 named-namespace-definition
13720 unnamed-namespace-definition
13722 named-namespace-definition:
13723 original-namespace-definition
13724 extension-namespace-definition
13726 original-namespace-definition:
13727 namespace identifier { namespace-body }
13729 extension-namespace-definition:
13730 namespace original-namespace-name { namespace-body }
13732 unnamed-namespace-definition:
13733 namespace { namespace-body } */
13736 cp_parser_namespace_definition (cp_parser* parser)
13738 tree identifier, attribs;
13739 bool has_visibility;
13742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13744 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13746 cp_lexer_consume_token (parser->lexer);
13751 /* Look for the `namespace' keyword. */
13752 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13754 /* Get the name of the namespace. We do not attempt to distinguish
13755 between an original-namespace-definition and an
13756 extension-namespace-definition at this point. The semantic
13757 analysis routines are responsible for that. */
13758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13759 identifier = cp_parser_identifier (parser);
13761 identifier = NULL_TREE;
13763 /* Parse any specified attributes. */
13764 attribs = cp_parser_attributes_opt (parser);
13766 /* Look for the `{' to start the namespace. */
13767 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13768 /* Start the namespace. */
13769 push_namespace (identifier);
13771 /* "inline namespace" is equivalent to a stub namespace definition
13772 followed by a strong using directive. */
13775 tree name_space = current_namespace;
13776 /* Set up namespace association. */
13777 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13778 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13779 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13780 /* Import the contents of the inline namespace. */
13782 do_using_directive (name_space);
13783 push_namespace (identifier);
13786 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13788 /* Parse the body of the namespace. */
13789 cp_parser_namespace_body (parser);
13791 #ifdef HANDLE_PRAGMA_VISIBILITY
13792 if (has_visibility)
13793 pop_visibility (1);
13796 /* Finish the namespace. */
13798 /* Look for the final `}'. */
13799 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13802 /* Parse a namespace-body.
13805 declaration-seq [opt] */
13808 cp_parser_namespace_body (cp_parser* parser)
13810 cp_parser_declaration_seq_opt (parser);
13813 /* Parse a namespace-alias-definition.
13815 namespace-alias-definition:
13816 namespace identifier = qualified-namespace-specifier ; */
13819 cp_parser_namespace_alias_definition (cp_parser* parser)
13822 tree namespace_specifier;
13824 cp_token *token = cp_lexer_peek_token (parser->lexer);
13826 /* Look for the `namespace' keyword. */
13827 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13828 /* Look for the identifier. */
13829 identifier = cp_parser_identifier (parser);
13830 if (identifier == error_mark_node)
13832 /* Look for the `=' token. */
13833 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13834 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13836 error_at (token->location, "%<namespace%> definition is not allowed here");
13837 /* Skip the definition. */
13838 cp_lexer_consume_token (parser->lexer);
13839 if (cp_parser_skip_to_closing_brace (parser))
13840 cp_lexer_consume_token (parser->lexer);
13843 cp_parser_require (parser, CPP_EQ, RT_EQ);
13844 /* Look for the qualified-namespace-specifier. */
13845 namespace_specifier
13846 = cp_parser_qualified_namespace_specifier (parser);
13847 /* Look for the `;' token. */
13848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13850 /* Register the alias in the symbol table. */
13851 do_namespace_alias (identifier, namespace_specifier);
13854 /* Parse a qualified-namespace-specifier.
13856 qualified-namespace-specifier:
13857 :: [opt] nested-name-specifier [opt] namespace-name
13859 Returns a NAMESPACE_DECL corresponding to the specified
13863 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13865 /* Look for the optional `::'. */
13866 cp_parser_global_scope_opt (parser,
13867 /*current_scope_valid_p=*/false);
13869 /* Look for the optional nested-name-specifier. */
13870 cp_parser_nested_name_specifier_opt (parser,
13871 /*typename_keyword_p=*/false,
13872 /*check_dependency_p=*/true,
13874 /*is_declaration=*/true);
13876 return cp_parser_namespace_name (parser);
13879 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13880 access declaration.
13883 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13884 using :: unqualified-id ;
13886 access-declaration:
13892 cp_parser_using_declaration (cp_parser* parser,
13893 bool access_declaration_p)
13896 bool typename_p = false;
13897 bool global_scope_p;
13902 if (access_declaration_p)
13903 cp_parser_parse_tentatively (parser);
13906 /* Look for the `using' keyword. */
13907 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13909 /* Peek at the next token. */
13910 token = cp_lexer_peek_token (parser->lexer);
13911 /* See if it's `typename'. */
13912 if (token->keyword == RID_TYPENAME)
13914 /* Remember that we've seen it. */
13916 /* Consume the `typename' token. */
13917 cp_lexer_consume_token (parser->lexer);
13921 /* Look for the optional global scope qualification. */
13923 = (cp_parser_global_scope_opt (parser,
13924 /*current_scope_valid_p=*/false)
13927 /* If we saw `typename', or didn't see `::', then there must be a
13928 nested-name-specifier present. */
13929 if (typename_p || !global_scope_p)
13930 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13931 /*check_dependency_p=*/true,
13933 /*is_declaration=*/true);
13934 /* Otherwise, we could be in either of the two productions. In that
13935 case, treat the nested-name-specifier as optional. */
13937 qscope = cp_parser_nested_name_specifier_opt (parser,
13938 /*typename_keyword_p=*/false,
13939 /*check_dependency_p=*/true,
13941 /*is_declaration=*/true);
13943 qscope = global_namespace;
13945 if (access_declaration_p && cp_parser_error_occurred (parser))
13946 /* Something has already gone wrong; there's no need to parse
13947 further. Since an error has occurred, the return value of
13948 cp_parser_parse_definitely will be false, as required. */
13949 return cp_parser_parse_definitely (parser);
13951 token = cp_lexer_peek_token (parser->lexer);
13952 /* Parse the unqualified-id. */
13953 identifier = cp_parser_unqualified_id (parser,
13954 /*template_keyword_p=*/false,
13955 /*check_dependency_p=*/true,
13956 /*declarator_p=*/true,
13957 /*optional_p=*/false);
13959 if (access_declaration_p)
13961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13962 cp_parser_simulate_error (parser);
13963 if (!cp_parser_parse_definitely (parser))
13967 /* The function we call to handle a using-declaration is different
13968 depending on what scope we are in. */
13969 if (qscope == error_mark_node || identifier == error_mark_node)
13971 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13972 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13973 /* [namespace.udecl]
13975 A using declaration shall not name a template-id. */
13976 error_at (token->location,
13977 "a template-id may not appear in a using-declaration");
13980 if (at_class_scope_p ())
13982 /* Create the USING_DECL. */
13983 decl = do_class_using_decl (parser->scope, identifier);
13985 if (check_for_bare_parameter_packs (decl))
13988 /* Add it to the list of members in this class. */
13989 finish_member_declaration (decl);
13993 decl = cp_parser_lookup_name_simple (parser,
13996 if (decl == error_mark_node)
13997 cp_parser_name_lookup_error (parser, identifier,
14000 else if (check_for_bare_parameter_packs (decl))
14002 else if (!at_namespace_scope_p ())
14003 do_local_using_decl (decl, qscope, identifier);
14005 do_toplevel_using_decl (decl, qscope, identifier);
14009 /* Look for the final `;'. */
14010 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14015 /* Parse a using-directive.
14018 using namespace :: [opt] nested-name-specifier [opt]
14019 namespace-name ; */
14022 cp_parser_using_directive (cp_parser* parser)
14024 tree namespace_decl;
14027 /* Look for the `using' keyword. */
14028 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14029 /* And the `namespace' keyword. */
14030 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14031 /* Look for the optional `::' operator. */
14032 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14033 /* And the optional nested-name-specifier. */
14034 cp_parser_nested_name_specifier_opt (parser,
14035 /*typename_keyword_p=*/false,
14036 /*check_dependency_p=*/true,
14038 /*is_declaration=*/true);
14039 /* Get the namespace being used. */
14040 namespace_decl = cp_parser_namespace_name (parser);
14041 /* And any specified attributes. */
14042 attribs = cp_parser_attributes_opt (parser);
14043 /* Update the symbol table. */
14044 parse_using_directive (namespace_decl, attribs);
14045 /* Look for the final `;'. */
14046 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14049 /* Parse an asm-definition.
14052 asm ( string-literal ) ;
14057 asm volatile [opt] ( string-literal ) ;
14058 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14059 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14060 : asm-operand-list [opt] ) ;
14061 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14062 : asm-operand-list [opt]
14063 : asm-clobber-list [opt] ) ;
14064 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14065 : asm-clobber-list [opt]
14066 : asm-goto-list ) ; */
14069 cp_parser_asm_definition (cp_parser* parser)
14072 tree outputs = NULL_TREE;
14073 tree inputs = NULL_TREE;
14074 tree clobbers = NULL_TREE;
14075 tree labels = NULL_TREE;
14077 bool volatile_p = false;
14078 bool extended_p = false;
14079 bool invalid_inputs_p = false;
14080 bool invalid_outputs_p = false;
14081 bool goto_p = false;
14082 required_token missing = RT_NONE;
14084 /* Look for the `asm' keyword. */
14085 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14086 /* See if the next token is `volatile'. */
14087 if (cp_parser_allow_gnu_extensions_p (parser)
14088 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14090 /* Remember that we saw the `volatile' keyword. */
14092 /* Consume the token. */
14093 cp_lexer_consume_token (parser->lexer);
14095 if (cp_parser_allow_gnu_extensions_p (parser)
14096 && parser->in_function_body
14097 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14099 /* Remember that we saw the `goto' keyword. */
14101 /* Consume the token. */
14102 cp_lexer_consume_token (parser->lexer);
14104 /* Look for the opening `('. */
14105 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14107 /* Look for the string. */
14108 string = cp_parser_string_literal (parser, false, false);
14109 if (string == error_mark_node)
14111 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14112 /*consume_paren=*/true);
14116 /* If we're allowing GNU extensions, check for the extended assembly
14117 syntax. Unfortunately, the `:' tokens need not be separated by
14118 a space in C, and so, for compatibility, we tolerate that here
14119 too. Doing that means that we have to treat the `::' operator as
14121 if (cp_parser_allow_gnu_extensions_p (parser)
14122 && parser->in_function_body
14123 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14124 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14126 bool inputs_p = false;
14127 bool clobbers_p = false;
14128 bool labels_p = false;
14130 /* The extended syntax was used. */
14133 /* Look for outputs. */
14134 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14136 /* Consume the `:'. */
14137 cp_lexer_consume_token (parser->lexer);
14138 /* Parse the output-operands. */
14139 if (cp_lexer_next_token_is_not (parser->lexer,
14141 && cp_lexer_next_token_is_not (parser->lexer,
14143 && cp_lexer_next_token_is_not (parser->lexer,
14146 outputs = cp_parser_asm_operand_list (parser);
14148 if (outputs == error_mark_node)
14149 invalid_outputs_p = true;
14151 /* If the next token is `::', there are no outputs, and the
14152 next token is the beginning of the inputs. */
14153 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14154 /* The inputs are coming next. */
14157 /* Look for inputs. */
14159 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14161 /* Consume the `:' or `::'. */
14162 cp_lexer_consume_token (parser->lexer);
14163 /* Parse the output-operands. */
14164 if (cp_lexer_next_token_is_not (parser->lexer,
14166 && cp_lexer_next_token_is_not (parser->lexer,
14168 && cp_lexer_next_token_is_not (parser->lexer,
14170 inputs = cp_parser_asm_operand_list (parser);
14172 if (inputs == error_mark_node)
14173 invalid_inputs_p = true;
14175 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14176 /* The clobbers are coming next. */
14179 /* Look for clobbers. */
14181 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14184 /* Consume the `:' or `::'. */
14185 cp_lexer_consume_token (parser->lexer);
14186 /* Parse the clobbers. */
14187 if (cp_lexer_next_token_is_not (parser->lexer,
14189 && cp_lexer_next_token_is_not (parser->lexer,
14191 clobbers = cp_parser_asm_clobber_list (parser);
14194 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14195 /* The labels are coming next. */
14198 /* Look for labels. */
14200 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14203 /* Consume the `:' or `::'. */
14204 cp_lexer_consume_token (parser->lexer);
14205 /* Parse the labels. */
14206 labels = cp_parser_asm_label_list (parser);
14209 if (goto_p && !labels_p)
14210 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14213 missing = RT_COLON_SCOPE;
14215 /* Look for the closing `)'. */
14216 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14217 missing ? missing : RT_CLOSE_PAREN))
14218 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14219 /*consume_paren=*/true);
14220 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14222 if (!invalid_inputs_p && !invalid_outputs_p)
14224 /* Create the ASM_EXPR. */
14225 if (parser->in_function_body)
14227 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14228 inputs, clobbers, labels);
14229 /* If the extended syntax was not used, mark the ASM_EXPR. */
14232 tree temp = asm_stmt;
14233 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14234 temp = TREE_OPERAND (temp, 0);
14236 ASM_INPUT_P (temp) = 1;
14240 cgraph_add_asm_node (string);
14244 /* Declarators [gram.dcl.decl] */
14246 /* Parse an init-declarator.
14249 declarator initializer [opt]
14254 declarator asm-specification [opt] attributes [opt] initializer [opt]
14256 function-definition:
14257 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14259 decl-specifier-seq [opt] declarator function-try-block
14263 function-definition:
14264 __extension__ function-definition
14266 The DECL_SPECIFIERS apply to this declarator. Returns a
14267 representation of the entity declared. If MEMBER_P is TRUE, then
14268 this declarator appears in a class scope. The new DECL created by
14269 this declarator is returned.
14271 The CHECKS are access checks that should be performed once we know
14272 what entity is being declared (and, therefore, what classes have
14275 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14276 for a function-definition here as well. If the declarator is a
14277 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14278 be TRUE upon return. By that point, the function-definition will
14279 have been completely parsed.
14281 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14285 cp_parser_init_declarator (cp_parser* parser,
14286 cp_decl_specifier_seq *decl_specifiers,
14287 VEC (deferred_access_check,gc)* checks,
14288 bool function_definition_allowed_p,
14290 int declares_class_or_enum,
14291 bool* function_definition_p)
14293 cp_token *token = NULL, *asm_spec_start_token = NULL,
14294 *attributes_start_token = NULL;
14295 cp_declarator *declarator;
14296 tree prefix_attributes;
14298 tree asm_specification;
14300 tree decl = NULL_TREE;
14302 int is_initialized;
14303 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14304 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14306 enum cpp_ttype initialization_kind;
14307 bool is_direct_init = false;
14308 bool is_non_constant_init;
14309 int ctor_dtor_or_conv_p;
14311 tree pushed_scope = NULL;
14313 /* Gather the attributes that were provided with the
14314 decl-specifiers. */
14315 prefix_attributes = decl_specifiers->attributes;
14317 /* Assume that this is not the declarator for a function
14319 if (function_definition_p)
14320 *function_definition_p = false;
14322 /* Defer access checks while parsing the declarator; we cannot know
14323 what names are accessible until we know what is being
14325 resume_deferring_access_checks ();
14327 /* Parse the declarator. */
14328 token = cp_lexer_peek_token (parser->lexer);
14330 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14331 &ctor_dtor_or_conv_p,
14332 /*parenthesized_p=*/NULL,
14333 /*member_p=*/false);
14334 /* Gather up the deferred checks. */
14335 stop_deferring_access_checks ();
14337 /* If the DECLARATOR was erroneous, there's no need to go
14339 if (declarator == cp_error_declarator)
14340 return error_mark_node;
14342 /* Check that the number of template-parameter-lists is OK. */
14343 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14345 return error_mark_node;
14347 if (declares_class_or_enum & 2)
14348 cp_parser_check_for_definition_in_return_type (declarator,
14349 decl_specifiers->type,
14350 decl_specifiers->type_location);
14352 /* Figure out what scope the entity declared by the DECLARATOR is
14353 located in. `grokdeclarator' sometimes changes the scope, so
14354 we compute it now. */
14355 scope = get_scope_of_declarator (declarator);
14357 /* Perform any lookups in the declared type which were thought to be
14358 dependent, but are not in the scope of the declarator. */
14359 decl_specifiers->type
14360 = maybe_update_decl_type (decl_specifiers->type, scope);
14362 /* If we're allowing GNU extensions, look for an asm-specification
14364 if (cp_parser_allow_gnu_extensions_p (parser))
14366 /* Look for an asm-specification. */
14367 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14368 asm_specification = cp_parser_asm_specification_opt (parser);
14369 /* And attributes. */
14370 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14371 attributes = cp_parser_attributes_opt (parser);
14375 asm_specification = NULL_TREE;
14376 attributes = NULL_TREE;
14379 /* Peek at the next token. */
14380 token = cp_lexer_peek_token (parser->lexer);
14381 /* Check to see if the token indicates the start of a
14382 function-definition. */
14383 if (function_declarator_p (declarator)
14384 && cp_parser_token_starts_function_definition_p (token))
14386 if (!function_definition_allowed_p)
14388 /* If a function-definition should not appear here, issue an
14390 cp_parser_error (parser,
14391 "a function-definition is not allowed here");
14392 return error_mark_node;
14396 location_t func_brace_location
14397 = cp_lexer_peek_token (parser->lexer)->location;
14399 /* Neither attributes nor an asm-specification are allowed
14400 on a function-definition. */
14401 if (asm_specification)
14402 error_at (asm_spec_start_token->location,
14403 "an asm-specification is not allowed "
14404 "on a function-definition");
14406 error_at (attributes_start_token->location,
14407 "attributes are not allowed on a function-definition");
14408 /* This is a function-definition. */
14409 *function_definition_p = true;
14411 /* Parse the function definition. */
14413 decl = cp_parser_save_member_function_body (parser,
14416 prefix_attributes);
14419 = (cp_parser_function_definition_from_specifiers_and_declarator
14420 (parser, decl_specifiers, prefix_attributes, declarator));
14422 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14424 /* This is where the prologue starts... */
14425 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14426 = func_brace_location;
14435 Only in function declarations for constructors, destructors, and
14436 type conversions can the decl-specifier-seq be omitted.
14438 We explicitly postpone this check past the point where we handle
14439 function-definitions because we tolerate function-definitions
14440 that are missing their return types in some modes. */
14441 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14443 cp_parser_error (parser,
14444 "expected constructor, destructor, or type conversion");
14445 return error_mark_node;
14448 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14449 if (token->type == CPP_EQ
14450 || token->type == CPP_OPEN_PAREN
14451 || token->type == CPP_OPEN_BRACE)
14453 is_initialized = SD_INITIALIZED;
14454 initialization_kind = token->type;
14456 if (token->type == CPP_EQ
14457 && function_declarator_p (declarator))
14459 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14460 if (t2->keyword == RID_DEFAULT)
14461 is_initialized = SD_DEFAULTED;
14462 else if (t2->keyword == RID_DELETE)
14463 is_initialized = SD_DELETED;
14468 /* If the init-declarator isn't initialized and isn't followed by a
14469 `,' or `;', it's not a valid init-declarator. */
14470 if (token->type != CPP_COMMA
14471 && token->type != CPP_SEMICOLON)
14473 cp_parser_error (parser, "expected initializer");
14474 return error_mark_node;
14476 is_initialized = SD_UNINITIALIZED;
14477 initialization_kind = CPP_EOF;
14480 /* Because start_decl has side-effects, we should only call it if we
14481 know we're going ahead. By this point, we know that we cannot
14482 possibly be looking at any other construct. */
14483 cp_parser_commit_to_tentative_parse (parser);
14485 /* If the decl specifiers were bad, issue an error now that we're
14486 sure this was intended to be a declarator. Then continue
14487 declaring the variable(s), as int, to try to cut down on further
14489 if (decl_specifiers->any_specifiers_p
14490 && decl_specifiers->type == error_mark_node)
14492 cp_parser_error (parser, "invalid type in declaration");
14493 decl_specifiers->type = integer_type_node;
14496 /* Check to see whether or not this declaration is a friend. */
14497 friend_p = cp_parser_friend_p (decl_specifiers);
14499 /* Enter the newly declared entry in the symbol table. If we're
14500 processing a declaration in a class-specifier, we wait until
14501 after processing the initializer. */
14504 if (parser->in_unbraced_linkage_specification_p)
14505 decl_specifiers->storage_class = sc_extern;
14506 decl = start_decl (declarator, decl_specifiers,
14507 is_initialized, attributes, prefix_attributes,
14509 /* Adjust location of decl if declarator->id_loc is more appropriate:
14510 set, and decl wasn't merged with another decl, in which case its
14511 location would be different from input_location, and more accurate. */
14513 && declarator->id_loc != UNKNOWN_LOCATION
14514 && DECL_SOURCE_LOCATION (decl) == input_location)
14515 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14518 /* Enter the SCOPE. That way unqualified names appearing in the
14519 initializer will be looked up in SCOPE. */
14520 pushed_scope = push_scope (scope);
14522 /* Perform deferred access control checks, now that we know in which
14523 SCOPE the declared entity resides. */
14524 if (!member_p && decl)
14526 tree saved_current_function_decl = NULL_TREE;
14528 /* If the entity being declared is a function, pretend that we
14529 are in its scope. If it is a `friend', it may have access to
14530 things that would not otherwise be accessible. */
14531 if (TREE_CODE (decl) == FUNCTION_DECL)
14533 saved_current_function_decl = current_function_decl;
14534 current_function_decl = decl;
14537 /* Perform access checks for template parameters. */
14538 cp_parser_perform_template_parameter_access_checks (checks);
14540 /* Perform the access control checks for the declarator and the
14541 decl-specifiers. */
14542 perform_deferred_access_checks ();
14544 /* Restore the saved value. */
14545 if (TREE_CODE (decl) == FUNCTION_DECL)
14546 current_function_decl = saved_current_function_decl;
14549 /* Parse the initializer. */
14550 initializer = NULL_TREE;
14551 is_direct_init = false;
14552 is_non_constant_init = true;
14553 if (is_initialized)
14555 if (function_declarator_p (declarator))
14557 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14558 if (initialization_kind == CPP_EQ)
14559 initializer = cp_parser_pure_specifier (parser);
14562 /* If the declaration was erroneous, we don't really
14563 know what the user intended, so just silently
14564 consume the initializer. */
14565 if (decl != error_mark_node)
14566 error_at (initializer_start_token->location,
14567 "initializer provided for function");
14568 cp_parser_skip_to_closing_parenthesis (parser,
14569 /*recovering=*/true,
14570 /*or_comma=*/false,
14571 /*consume_paren=*/true);
14576 /* We want to record the extra mangling scope for in-class
14577 initializers of class members and initializers of static data
14578 member templates. The former is a C++0x feature which isn't
14579 implemented yet, and I expect it will involve deferring
14580 parsing of the initializer until end of class as with default
14581 arguments. So right here we only handle the latter. */
14582 if (!member_p && processing_template_decl)
14583 start_lambda_scope (decl);
14584 initializer = cp_parser_initializer (parser,
14586 &is_non_constant_init);
14587 if (!member_p && processing_template_decl)
14588 finish_lambda_scope ();
14592 /* The old parser allows attributes to appear after a parenthesized
14593 initializer. Mark Mitchell proposed removing this functionality
14594 on the GCC mailing lists on 2002-08-13. This parser accepts the
14595 attributes -- but ignores them. */
14596 if (cp_parser_allow_gnu_extensions_p (parser)
14597 && initialization_kind == CPP_OPEN_PAREN)
14598 if (cp_parser_attributes_opt (parser))
14599 warning (OPT_Wattributes,
14600 "attributes after parenthesized initializer ignored");
14602 /* For an in-class declaration, use `grokfield' to create the
14608 pop_scope (pushed_scope);
14609 pushed_scope = false;
14611 decl = grokfield (declarator, decl_specifiers,
14612 initializer, !is_non_constant_init,
14613 /*asmspec=*/NULL_TREE,
14614 prefix_attributes);
14615 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14616 cp_parser_save_default_args (parser, decl);
14619 /* Finish processing the declaration. But, skip friend
14621 if (!friend_p && decl && decl != error_mark_node)
14623 cp_finish_decl (decl,
14624 initializer, !is_non_constant_init,
14626 /* If the initializer is in parentheses, then this is
14627 a direct-initialization, which means that an
14628 `explicit' constructor is OK. Otherwise, an
14629 `explicit' constructor cannot be used. */
14630 ((is_direct_init || !is_initialized)
14631 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14633 else if ((cxx_dialect != cxx98) && friend_p
14634 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14635 /* Core issue #226 (C++0x only): A default template-argument
14636 shall not be specified in a friend class template
14638 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14639 /*is_partial=*/0, /*is_friend_decl=*/1);
14641 if (!friend_p && pushed_scope)
14642 pop_scope (pushed_scope);
14647 /* Parse a declarator.
14651 ptr-operator declarator
14653 abstract-declarator:
14654 ptr-operator abstract-declarator [opt]
14655 direct-abstract-declarator
14660 attributes [opt] direct-declarator
14661 attributes [opt] ptr-operator declarator
14663 abstract-declarator:
14664 attributes [opt] ptr-operator abstract-declarator [opt]
14665 attributes [opt] direct-abstract-declarator
14667 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14668 detect constructor, destructor or conversion operators. It is set
14669 to -1 if the declarator is a name, and +1 if it is a
14670 function. Otherwise it is set to zero. Usually you just want to
14671 test for >0, but internally the negative value is used.
14673 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14674 a decl-specifier-seq unless it declares a constructor, destructor,
14675 or conversion. It might seem that we could check this condition in
14676 semantic analysis, rather than parsing, but that makes it difficult
14677 to handle something like `f()'. We want to notice that there are
14678 no decl-specifiers, and therefore realize that this is an
14679 expression, not a declaration.)
14681 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14682 the declarator is a direct-declarator of the form "(...)".
14684 MEMBER_P is true iff this declarator is a member-declarator. */
14686 static cp_declarator *
14687 cp_parser_declarator (cp_parser* parser,
14688 cp_parser_declarator_kind dcl_kind,
14689 int* ctor_dtor_or_conv_p,
14690 bool* parenthesized_p,
14693 cp_declarator *declarator;
14694 enum tree_code code;
14695 cp_cv_quals cv_quals;
14697 tree attributes = NULL_TREE;
14699 /* Assume this is not a constructor, destructor, or type-conversion
14701 if (ctor_dtor_or_conv_p)
14702 *ctor_dtor_or_conv_p = 0;
14704 if (cp_parser_allow_gnu_extensions_p (parser))
14705 attributes = cp_parser_attributes_opt (parser);
14707 /* Check for the ptr-operator production. */
14708 cp_parser_parse_tentatively (parser);
14709 /* Parse the ptr-operator. */
14710 code = cp_parser_ptr_operator (parser,
14713 /* If that worked, then we have a ptr-operator. */
14714 if (cp_parser_parse_definitely (parser))
14716 /* If a ptr-operator was found, then this declarator was not
14718 if (parenthesized_p)
14719 *parenthesized_p = true;
14720 /* The dependent declarator is optional if we are parsing an
14721 abstract-declarator. */
14722 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14723 cp_parser_parse_tentatively (parser);
14725 /* Parse the dependent declarator. */
14726 declarator = cp_parser_declarator (parser, dcl_kind,
14727 /*ctor_dtor_or_conv_p=*/NULL,
14728 /*parenthesized_p=*/NULL,
14729 /*member_p=*/false);
14731 /* If we are parsing an abstract-declarator, we must handle the
14732 case where the dependent declarator is absent. */
14733 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14734 && !cp_parser_parse_definitely (parser))
14737 declarator = cp_parser_make_indirect_declarator
14738 (code, class_type, cv_quals, declarator);
14740 /* Everything else is a direct-declarator. */
14743 if (parenthesized_p)
14744 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14746 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14747 ctor_dtor_or_conv_p,
14751 if (attributes && declarator && declarator != cp_error_declarator)
14752 declarator->attributes = attributes;
14757 /* Parse a direct-declarator or direct-abstract-declarator.
14761 direct-declarator ( parameter-declaration-clause )
14762 cv-qualifier-seq [opt]
14763 exception-specification [opt]
14764 direct-declarator [ constant-expression [opt] ]
14767 direct-abstract-declarator:
14768 direct-abstract-declarator [opt]
14769 ( parameter-declaration-clause )
14770 cv-qualifier-seq [opt]
14771 exception-specification [opt]
14772 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14773 ( abstract-declarator )
14775 Returns a representation of the declarator. DCL_KIND is
14776 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14777 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14778 we are parsing a direct-declarator. It is
14779 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14780 of ambiguity we prefer an abstract declarator, as per
14781 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14782 cp_parser_declarator. */
14784 static cp_declarator *
14785 cp_parser_direct_declarator (cp_parser* parser,
14786 cp_parser_declarator_kind dcl_kind,
14787 int* ctor_dtor_or_conv_p,
14791 cp_declarator *declarator = NULL;
14792 tree scope = NULL_TREE;
14793 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14794 bool saved_in_declarator_p = parser->in_declarator_p;
14796 tree pushed_scope = NULL_TREE;
14800 /* Peek at the next token. */
14801 token = cp_lexer_peek_token (parser->lexer);
14802 if (token->type == CPP_OPEN_PAREN)
14804 /* This is either a parameter-declaration-clause, or a
14805 parenthesized declarator. When we know we are parsing a
14806 named declarator, it must be a parenthesized declarator
14807 if FIRST is true. For instance, `(int)' is a
14808 parameter-declaration-clause, with an omitted
14809 direct-abstract-declarator. But `((*))', is a
14810 parenthesized abstract declarator. Finally, when T is a
14811 template parameter `(T)' is a
14812 parameter-declaration-clause, and not a parenthesized
14815 We first try and parse a parameter-declaration-clause,
14816 and then try a nested declarator (if FIRST is true).
14818 It is not an error for it not to be a
14819 parameter-declaration-clause, even when FIRST is
14825 The first is the declaration of a function while the
14826 second is the definition of a variable, including its
14829 Having seen only the parenthesis, we cannot know which of
14830 these two alternatives should be selected. Even more
14831 complex are examples like:
14836 The former is a function-declaration; the latter is a
14837 variable initialization.
14839 Thus again, we try a parameter-declaration-clause, and if
14840 that fails, we back out and return. */
14842 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14845 unsigned saved_num_template_parameter_lists;
14846 bool is_declarator = false;
14849 /* In a member-declarator, the only valid interpretation
14850 of a parenthesis is the start of a
14851 parameter-declaration-clause. (It is invalid to
14852 initialize a static data member with a parenthesized
14853 initializer; only the "=" form of initialization is
14856 cp_parser_parse_tentatively (parser);
14858 /* Consume the `('. */
14859 cp_lexer_consume_token (parser->lexer);
14862 /* If this is going to be an abstract declarator, we're
14863 in a declarator and we can't have default args. */
14864 parser->default_arg_ok_p = false;
14865 parser->in_declarator_p = true;
14868 /* Inside the function parameter list, surrounding
14869 template-parameter-lists do not apply. */
14870 saved_num_template_parameter_lists
14871 = parser->num_template_parameter_lists;
14872 parser->num_template_parameter_lists = 0;
14874 begin_scope (sk_function_parms, NULL_TREE);
14876 /* Parse the parameter-declaration-clause. */
14877 params = cp_parser_parameter_declaration_clause (parser);
14879 parser->num_template_parameter_lists
14880 = saved_num_template_parameter_lists;
14882 /* If all went well, parse the cv-qualifier-seq and the
14883 exception-specification. */
14884 if (member_p || cp_parser_parse_definitely (parser))
14886 cp_cv_quals cv_quals;
14887 tree exception_specification;
14890 is_declarator = true;
14892 if (ctor_dtor_or_conv_p)
14893 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14895 /* Consume the `)'. */
14896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14898 /* Parse the cv-qualifier-seq. */
14899 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14900 /* And the exception-specification. */
14901 exception_specification
14902 = cp_parser_exception_specification_opt (parser);
14905 = cp_parser_late_return_type_opt (parser);
14907 /* Create the function-declarator. */
14908 declarator = make_call_declarator (declarator,
14911 exception_specification,
14913 /* Any subsequent parameter lists are to do with
14914 return type, so are not those of the declared
14916 parser->default_arg_ok_p = false;
14919 /* Remove the function parms from scope. */
14920 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14921 pop_binding (DECL_NAME (t), t);
14925 /* Repeat the main loop. */
14929 /* If this is the first, we can try a parenthesized
14933 bool saved_in_type_id_in_expr_p;
14935 parser->default_arg_ok_p = saved_default_arg_ok_p;
14936 parser->in_declarator_p = saved_in_declarator_p;
14938 /* Consume the `('. */
14939 cp_lexer_consume_token (parser->lexer);
14940 /* Parse the nested declarator. */
14941 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14942 parser->in_type_id_in_expr_p = true;
14944 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14945 /*parenthesized_p=*/NULL,
14947 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14949 /* Expect a `)'. */
14950 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14951 declarator = cp_error_declarator;
14952 if (declarator == cp_error_declarator)
14955 goto handle_declarator;
14957 /* Otherwise, we must be done. */
14961 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14962 && token->type == CPP_OPEN_SQUARE)
14964 /* Parse an array-declarator. */
14967 if (ctor_dtor_or_conv_p)
14968 *ctor_dtor_or_conv_p = 0;
14971 parser->default_arg_ok_p = false;
14972 parser->in_declarator_p = true;
14973 /* Consume the `['. */
14974 cp_lexer_consume_token (parser->lexer);
14975 /* Peek at the next token. */
14976 token = cp_lexer_peek_token (parser->lexer);
14977 /* If the next token is `]', then there is no
14978 constant-expression. */
14979 if (token->type != CPP_CLOSE_SQUARE)
14981 bool non_constant_p;
14984 = cp_parser_constant_expression (parser,
14985 /*allow_non_constant=*/true,
14987 if (!non_constant_p || cxx_dialect >= cxx0x)
14989 /* Normally, the array bound must be an integral constant
14990 expression. However, as an extension, we allow VLAs
14991 in function scopes as long as they aren't part of a
14992 parameter declaration. */
14993 else if (!parser->in_function_body
14994 || current_binding_level->kind == sk_function_parms)
14996 cp_parser_error (parser,
14997 "array bound is not an integer constant");
14998 bounds = error_mark_node;
15000 else if (processing_template_decl && !error_operand_p (bounds))
15002 /* Remember this wasn't a constant-expression. */
15003 bounds = build_nop (TREE_TYPE (bounds), bounds);
15004 TREE_SIDE_EFFECTS (bounds) = 1;
15008 bounds = NULL_TREE;
15009 /* Look for the closing `]'. */
15010 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15012 declarator = cp_error_declarator;
15016 declarator = make_array_declarator (declarator, bounds);
15018 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15021 tree qualifying_scope;
15022 tree unqualified_name;
15023 special_function_kind sfk;
15025 bool pack_expansion_p = false;
15026 cp_token *declarator_id_start_token;
15028 /* Parse a declarator-id */
15029 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15032 cp_parser_parse_tentatively (parser);
15034 /* If we see an ellipsis, we should be looking at a
15036 if (token->type == CPP_ELLIPSIS)
15038 /* Consume the `...' */
15039 cp_lexer_consume_token (parser->lexer);
15041 pack_expansion_p = true;
15045 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15047 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15048 qualifying_scope = parser->scope;
15053 if (!unqualified_name && pack_expansion_p)
15055 /* Check whether an error occurred. */
15056 okay = !cp_parser_error_occurred (parser);
15058 /* We already consumed the ellipsis to mark a
15059 parameter pack, but we have no way to report it,
15060 so abort the tentative parse. We will be exiting
15061 immediately anyway. */
15062 cp_parser_abort_tentative_parse (parser);
15065 okay = cp_parser_parse_definitely (parser);
15068 unqualified_name = error_mark_node;
15069 else if (unqualified_name
15070 && (qualifying_scope
15071 || (TREE_CODE (unqualified_name)
15072 != IDENTIFIER_NODE)))
15074 cp_parser_error (parser, "expected unqualified-id");
15075 unqualified_name = error_mark_node;
15079 if (!unqualified_name)
15081 if (unqualified_name == error_mark_node)
15083 declarator = cp_error_declarator;
15084 pack_expansion_p = false;
15085 declarator->parameter_pack_p = false;
15089 if (qualifying_scope && at_namespace_scope_p ()
15090 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15092 /* In the declaration of a member of a template class
15093 outside of the class itself, the SCOPE will sometimes
15094 be a TYPENAME_TYPE. For example, given:
15096 template <typename T>
15097 int S<T>::R::i = 3;
15099 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15100 this context, we must resolve S<T>::R to an ordinary
15101 type, rather than a typename type.
15103 The reason we normally avoid resolving TYPENAME_TYPEs
15104 is that a specialization of `S' might render
15105 `S<T>::R' not a type. However, if `S' is
15106 specialized, then this `i' will not be used, so there
15107 is no harm in resolving the types here. */
15110 /* Resolve the TYPENAME_TYPE. */
15111 type = resolve_typename_type (qualifying_scope,
15112 /*only_current_p=*/false);
15113 /* If that failed, the declarator is invalid. */
15114 if (TREE_CODE (type) == TYPENAME_TYPE)
15116 if (typedef_variant_p (type))
15117 error_at (declarator_id_start_token->location,
15118 "cannot define member of dependent typedef "
15121 error_at (declarator_id_start_token->location,
15122 "%<%T::%E%> is not a type",
15123 TYPE_CONTEXT (qualifying_scope),
15124 TYPE_IDENTIFIER (qualifying_scope));
15126 qualifying_scope = type;
15131 if (unqualified_name)
15135 if (qualifying_scope
15136 && CLASS_TYPE_P (qualifying_scope))
15137 class_type = qualifying_scope;
15139 class_type = current_class_type;
15141 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15143 tree name_type = TREE_TYPE (unqualified_name);
15144 if (class_type && same_type_p (name_type, class_type))
15146 if (qualifying_scope
15147 && CLASSTYPE_USE_TEMPLATE (name_type))
15149 error_at (declarator_id_start_token->location,
15150 "invalid use of constructor as a template");
15151 inform (declarator_id_start_token->location,
15152 "use %<%T::%D%> instead of %<%T::%D%> to "
15153 "name the constructor in a qualified name",
15155 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15156 class_type, name_type);
15157 declarator = cp_error_declarator;
15161 unqualified_name = constructor_name (class_type);
15165 /* We do not attempt to print the declarator
15166 here because we do not have enough
15167 information about its original syntactic
15169 cp_parser_error (parser, "invalid declarator");
15170 declarator = cp_error_declarator;
15177 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15178 sfk = sfk_destructor;
15179 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15180 sfk = sfk_conversion;
15181 else if (/* There's no way to declare a constructor
15182 for an anonymous type, even if the type
15183 got a name for linkage purposes. */
15184 !TYPE_WAS_ANONYMOUS (class_type)
15185 && constructor_name_p (unqualified_name,
15188 unqualified_name = constructor_name (class_type);
15189 sfk = sfk_constructor;
15191 else if (is_overloaded_fn (unqualified_name)
15192 && DECL_CONSTRUCTOR_P (get_first_fn
15193 (unqualified_name)))
15194 sfk = sfk_constructor;
15196 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15197 *ctor_dtor_or_conv_p = -1;
15200 declarator = make_id_declarator (qualifying_scope,
15203 declarator->id_loc = token->location;
15204 declarator->parameter_pack_p = pack_expansion_p;
15206 if (pack_expansion_p)
15207 maybe_warn_variadic_templates ();
15210 handle_declarator:;
15211 scope = get_scope_of_declarator (declarator);
15213 /* Any names that appear after the declarator-id for a
15214 member are looked up in the containing scope. */
15215 pushed_scope = push_scope (scope);
15216 parser->in_declarator_p = true;
15217 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15218 || (declarator && declarator->kind == cdk_id))
15219 /* Default args are only allowed on function
15221 parser->default_arg_ok_p = saved_default_arg_ok_p;
15223 parser->default_arg_ok_p = false;
15232 /* For an abstract declarator, we might wind up with nothing at this
15233 point. That's an error; the declarator is not optional. */
15235 cp_parser_error (parser, "expected declarator");
15237 /* If we entered a scope, we must exit it now. */
15239 pop_scope (pushed_scope);
15241 parser->default_arg_ok_p = saved_default_arg_ok_p;
15242 parser->in_declarator_p = saved_in_declarator_p;
15247 /* Parse a ptr-operator.
15250 * cv-qualifier-seq [opt]
15252 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15257 & cv-qualifier-seq [opt]
15259 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15260 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15261 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15262 filled in with the TYPE containing the member. *CV_QUALS is
15263 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15264 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15265 Note that the tree codes returned by this function have nothing
15266 to do with the types of trees that will be eventually be created
15267 to represent the pointer or reference type being parsed. They are
15268 just constants with suggestive names. */
15269 static enum tree_code
15270 cp_parser_ptr_operator (cp_parser* parser,
15272 cp_cv_quals *cv_quals)
15274 enum tree_code code = ERROR_MARK;
15277 /* Assume that it's not a pointer-to-member. */
15279 /* And that there are no cv-qualifiers. */
15280 *cv_quals = TYPE_UNQUALIFIED;
15282 /* Peek at the next token. */
15283 token = cp_lexer_peek_token (parser->lexer);
15285 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15286 if (token->type == CPP_MULT)
15287 code = INDIRECT_REF;
15288 else if (token->type == CPP_AND)
15290 else if ((cxx_dialect != cxx98) &&
15291 token->type == CPP_AND_AND) /* C++0x only */
15292 code = NON_LVALUE_EXPR;
15294 if (code != ERROR_MARK)
15296 /* Consume the `*', `&' or `&&'. */
15297 cp_lexer_consume_token (parser->lexer);
15299 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15300 `&', if we are allowing GNU extensions. (The only qualifier
15301 that can legally appear after `&' is `restrict', but that is
15302 enforced during semantic analysis. */
15303 if (code == INDIRECT_REF
15304 || cp_parser_allow_gnu_extensions_p (parser))
15305 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15309 /* Try the pointer-to-member case. */
15310 cp_parser_parse_tentatively (parser);
15311 /* Look for the optional `::' operator. */
15312 cp_parser_global_scope_opt (parser,
15313 /*current_scope_valid_p=*/false);
15314 /* Look for the nested-name specifier. */
15315 token = cp_lexer_peek_token (parser->lexer);
15316 cp_parser_nested_name_specifier (parser,
15317 /*typename_keyword_p=*/false,
15318 /*check_dependency_p=*/true,
15320 /*is_declaration=*/false);
15321 /* If we found it, and the next token is a `*', then we are
15322 indeed looking at a pointer-to-member operator. */
15323 if (!cp_parser_error_occurred (parser)
15324 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15326 /* Indicate that the `*' operator was used. */
15327 code = INDIRECT_REF;
15329 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15330 error_at (token->location, "%qD is a namespace", parser->scope);
15333 /* The type of which the member is a member is given by the
15335 *type = parser->scope;
15336 /* The next name will not be qualified. */
15337 parser->scope = NULL_TREE;
15338 parser->qualifying_scope = NULL_TREE;
15339 parser->object_scope = NULL_TREE;
15340 /* Look for the optional cv-qualifier-seq. */
15341 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15344 /* If that didn't work we don't have a ptr-operator. */
15345 if (!cp_parser_parse_definitely (parser))
15346 cp_parser_error (parser, "expected ptr-operator");
15352 /* Parse an (optional) cv-qualifier-seq.
15355 cv-qualifier cv-qualifier-seq [opt]
15366 Returns a bitmask representing the cv-qualifiers. */
15369 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15371 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15376 cp_cv_quals cv_qualifier;
15378 /* Peek at the next token. */
15379 token = cp_lexer_peek_token (parser->lexer);
15380 /* See if it's a cv-qualifier. */
15381 switch (token->keyword)
15384 cv_qualifier = TYPE_QUAL_CONST;
15388 cv_qualifier = TYPE_QUAL_VOLATILE;
15392 cv_qualifier = TYPE_QUAL_RESTRICT;
15396 cv_qualifier = TYPE_UNQUALIFIED;
15403 if (cv_quals & cv_qualifier)
15405 error_at (token->location, "duplicate cv-qualifier");
15406 cp_lexer_purge_token (parser->lexer);
15410 cp_lexer_consume_token (parser->lexer);
15411 cv_quals |= cv_qualifier;
15418 /* Parse a late-specified return type, if any. This is not a separate
15419 non-terminal, but part of a function declarator, which looks like
15421 -> trailing-type-specifier-seq abstract-declarator(opt)
15423 Returns the type indicated by the type-id. */
15426 cp_parser_late_return_type_opt (cp_parser* parser)
15430 /* Peek at the next token. */
15431 token = cp_lexer_peek_token (parser->lexer);
15432 /* A late-specified return type is indicated by an initial '->'. */
15433 if (token->type != CPP_DEREF)
15436 /* Consume the ->. */
15437 cp_lexer_consume_token (parser->lexer);
15439 return cp_parser_trailing_type_id (parser);
15442 /* Parse a declarator-id.
15446 :: [opt] nested-name-specifier [opt] type-name
15448 In the `id-expression' case, the value returned is as for
15449 cp_parser_id_expression if the id-expression was an unqualified-id.
15450 If the id-expression was a qualified-id, then a SCOPE_REF is
15451 returned. The first operand is the scope (either a NAMESPACE_DECL
15452 or TREE_TYPE), but the second is still just a representation of an
15456 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15459 /* The expression must be an id-expression. Assume that qualified
15460 names are the names of types so that:
15463 int S<T>::R::i = 3;
15465 will work; we must treat `S<T>::R' as the name of a type.
15466 Similarly, assume that qualified names are templates, where
15470 int S<T>::R<T>::i = 3;
15473 id = cp_parser_id_expression (parser,
15474 /*template_keyword_p=*/false,
15475 /*check_dependency_p=*/false,
15476 /*template_p=*/NULL,
15477 /*declarator_p=*/true,
15479 if (id && BASELINK_P (id))
15480 id = BASELINK_FUNCTIONS (id);
15484 /* Parse a type-id.
15487 type-specifier-seq abstract-declarator [opt]
15489 Returns the TYPE specified. */
15492 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15493 bool is_trailing_return)
15495 cp_decl_specifier_seq type_specifier_seq;
15496 cp_declarator *abstract_declarator;
15498 /* Parse the type-specifier-seq. */
15499 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15500 is_trailing_return,
15501 &type_specifier_seq);
15502 if (type_specifier_seq.type == error_mark_node)
15503 return error_mark_node;
15505 /* There might or might not be an abstract declarator. */
15506 cp_parser_parse_tentatively (parser);
15507 /* Look for the declarator. */
15508 abstract_declarator
15509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15510 /*parenthesized_p=*/NULL,
15511 /*member_p=*/false);
15512 /* Check to see if there really was a declarator. */
15513 if (!cp_parser_parse_definitely (parser))
15514 abstract_declarator = NULL;
15516 if (type_specifier_seq.type
15517 && type_uses_auto (type_specifier_seq.type))
15519 /* A type-id with type 'auto' is only ok if the abstract declarator
15520 is a function declarator with a late-specified return type. */
15521 if (abstract_declarator
15522 && abstract_declarator->kind == cdk_function
15523 && abstract_declarator->u.function.late_return_type)
15527 error ("invalid use of %<auto%>");
15528 return error_mark_node;
15532 return groktypename (&type_specifier_seq, abstract_declarator,
15536 static tree cp_parser_type_id (cp_parser *parser)
15538 return cp_parser_type_id_1 (parser, false, false);
15541 static tree cp_parser_template_type_arg (cp_parser *parser)
15543 return cp_parser_type_id_1 (parser, true, false);
15546 static tree cp_parser_trailing_type_id (cp_parser *parser)
15548 return cp_parser_type_id_1 (parser, false, true);
15551 /* Parse a type-specifier-seq.
15553 type-specifier-seq:
15554 type-specifier type-specifier-seq [opt]
15558 type-specifier-seq:
15559 attributes type-specifier-seq [opt]
15561 If IS_DECLARATION is true, we are at the start of a "condition" or
15562 exception-declaration, so we might be followed by a declarator-id.
15564 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15565 i.e. we've just seen "->".
15567 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15570 cp_parser_type_specifier_seq (cp_parser* parser,
15571 bool is_declaration,
15572 bool is_trailing_return,
15573 cp_decl_specifier_seq *type_specifier_seq)
15575 bool seen_type_specifier = false;
15576 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15577 cp_token *start_token = NULL;
15579 /* Clear the TYPE_SPECIFIER_SEQ. */
15580 clear_decl_specs (type_specifier_seq);
15582 /* In the context of a trailing return type, enum E { } is an
15583 elaborated-type-specifier followed by a function-body, not an
15585 if (is_trailing_return)
15586 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15588 /* Parse the type-specifiers and attributes. */
15591 tree type_specifier;
15592 bool is_cv_qualifier;
15594 /* Check for attributes first. */
15595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15597 type_specifier_seq->attributes =
15598 chainon (type_specifier_seq->attributes,
15599 cp_parser_attributes_opt (parser));
15603 /* record the token of the beginning of the type specifier seq,
15604 for error reporting purposes*/
15606 start_token = cp_lexer_peek_token (parser->lexer);
15608 /* Look for the type-specifier. */
15609 type_specifier = cp_parser_type_specifier (parser,
15611 type_specifier_seq,
15612 /*is_declaration=*/false,
15615 if (!type_specifier)
15617 /* If the first type-specifier could not be found, this is not a
15618 type-specifier-seq at all. */
15619 if (!seen_type_specifier)
15621 cp_parser_error (parser, "expected type-specifier");
15622 type_specifier_seq->type = error_mark_node;
15625 /* If subsequent type-specifiers could not be found, the
15626 type-specifier-seq is complete. */
15630 seen_type_specifier = true;
15631 /* The standard says that a condition can be:
15633 type-specifier-seq declarator = assignment-expression
15640 we should treat the "S" as a declarator, not as a
15641 type-specifier. The standard doesn't say that explicitly for
15642 type-specifier-seq, but it does say that for
15643 decl-specifier-seq in an ordinary declaration. Perhaps it
15644 would be clearer just to allow a decl-specifier-seq here, and
15645 then add a semantic restriction that if any decl-specifiers
15646 that are not type-specifiers appear, the program is invalid. */
15647 if (is_declaration && !is_cv_qualifier)
15648 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15651 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15654 /* Parse a parameter-declaration-clause.
15656 parameter-declaration-clause:
15657 parameter-declaration-list [opt] ... [opt]
15658 parameter-declaration-list , ...
15660 Returns a representation for the parameter declarations. A return
15661 value of NULL indicates a parameter-declaration-clause consisting
15662 only of an ellipsis. */
15665 cp_parser_parameter_declaration_clause (cp_parser* parser)
15672 /* Peek at the next token. */
15673 token = cp_lexer_peek_token (parser->lexer);
15674 /* Check for trivial parameter-declaration-clauses. */
15675 if (token->type == CPP_ELLIPSIS)
15677 /* Consume the `...' token. */
15678 cp_lexer_consume_token (parser->lexer);
15681 else if (token->type == CPP_CLOSE_PAREN)
15682 /* There are no parameters. */
15684 #ifndef NO_IMPLICIT_EXTERN_C
15685 if (in_system_header && current_class_type == NULL
15686 && current_lang_name == lang_name_c)
15690 return void_list_node;
15692 /* Check for `(void)', too, which is a special case. */
15693 else if (token->keyword == RID_VOID
15694 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15695 == CPP_CLOSE_PAREN))
15697 /* Consume the `void' token. */
15698 cp_lexer_consume_token (parser->lexer);
15699 /* There are no parameters. */
15700 return void_list_node;
15703 /* Parse the parameter-declaration-list. */
15704 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15705 /* If a parse error occurred while parsing the
15706 parameter-declaration-list, then the entire
15707 parameter-declaration-clause is erroneous. */
15711 /* Peek at the next token. */
15712 token = cp_lexer_peek_token (parser->lexer);
15713 /* If it's a `,', the clause should terminate with an ellipsis. */
15714 if (token->type == CPP_COMMA)
15716 /* Consume the `,'. */
15717 cp_lexer_consume_token (parser->lexer);
15718 /* Expect an ellipsis. */
15720 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15722 /* It might also be `...' if the optional trailing `,' was
15724 else if (token->type == CPP_ELLIPSIS)
15726 /* Consume the `...' token. */
15727 cp_lexer_consume_token (parser->lexer);
15728 /* And remember that we saw it. */
15732 ellipsis_p = false;
15734 /* Finish the parameter list. */
15736 parameters = chainon (parameters, void_list_node);
15741 /* Parse a parameter-declaration-list.
15743 parameter-declaration-list:
15744 parameter-declaration
15745 parameter-declaration-list , parameter-declaration
15747 Returns a representation of the parameter-declaration-list, as for
15748 cp_parser_parameter_declaration_clause. However, the
15749 `void_list_node' is never appended to the list. Upon return,
15750 *IS_ERROR will be true iff an error occurred. */
15753 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15755 tree parameters = NULL_TREE;
15756 tree *tail = ¶meters;
15757 bool saved_in_unbraced_linkage_specification_p;
15760 /* Assume all will go well. */
15762 /* The special considerations that apply to a function within an
15763 unbraced linkage specifications do not apply to the parameters
15764 to the function. */
15765 saved_in_unbraced_linkage_specification_p
15766 = parser->in_unbraced_linkage_specification_p;
15767 parser->in_unbraced_linkage_specification_p = false;
15769 /* Look for more parameters. */
15772 cp_parameter_declarator *parameter;
15773 tree decl = error_mark_node;
15774 bool parenthesized_p;
15775 /* Parse the parameter. */
15777 = cp_parser_parameter_declaration (parser,
15778 /*template_parm_p=*/false,
15781 /* We don't know yet if the enclosing context is deprecated, so wait
15782 and warn in grokparms if appropriate. */
15783 deprecated_state = DEPRECATED_SUPPRESS;
15786 decl = grokdeclarator (parameter->declarator,
15787 ¶meter->decl_specifiers,
15789 parameter->default_argument != NULL_TREE,
15790 ¶meter->decl_specifiers.attributes);
15792 deprecated_state = DEPRECATED_NORMAL;
15794 /* If a parse error occurred parsing the parameter declaration,
15795 then the entire parameter-declaration-list is erroneous. */
15796 if (decl == error_mark_node)
15799 parameters = error_mark_node;
15803 if (parameter->decl_specifiers.attributes)
15804 cplus_decl_attributes (&decl,
15805 parameter->decl_specifiers.attributes,
15807 if (DECL_NAME (decl))
15808 decl = pushdecl (decl);
15810 if (decl != error_mark_node)
15812 retrofit_lang_decl (decl);
15813 DECL_PARM_INDEX (decl) = ++index;
15816 /* Add the new parameter to the list. */
15817 *tail = build_tree_list (parameter->default_argument, decl);
15818 tail = &TREE_CHAIN (*tail);
15820 /* Peek at the next token. */
15821 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15822 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15823 /* These are for Objective-C++ */
15824 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15825 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15826 /* The parameter-declaration-list is complete. */
15828 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15832 /* Peek at the next token. */
15833 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15834 /* If it's an ellipsis, then the list is complete. */
15835 if (token->type == CPP_ELLIPSIS)
15837 /* Otherwise, there must be more parameters. Consume the
15839 cp_lexer_consume_token (parser->lexer);
15840 /* When parsing something like:
15842 int i(float f, double d)
15844 we can tell after seeing the declaration for "f" that we
15845 are not looking at an initialization of a variable "i",
15846 but rather at the declaration of a function "i".
15848 Due to the fact that the parsing of template arguments
15849 (as specified to a template-id) requires backtracking we
15850 cannot use this technique when inside a template argument
15852 if (!parser->in_template_argument_list_p
15853 && !parser->in_type_id_in_expr_p
15854 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15855 /* However, a parameter-declaration of the form
15856 "foat(f)" (which is a valid declaration of a
15857 parameter "f") can also be interpreted as an
15858 expression (the conversion of "f" to "float"). */
15859 && !parenthesized_p)
15860 cp_parser_commit_to_tentative_parse (parser);
15864 cp_parser_error (parser, "expected %<,%> or %<...%>");
15865 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15866 cp_parser_skip_to_closing_parenthesis (parser,
15867 /*recovering=*/true,
15868 /*or_comma=*/false,
15869 /*consume_paren=*/false);
15874 parser->in_unbraced_linkage_specification_p
15875 = saved_in_unbraced_linkage_specification_p;
15880 /* Parse a parameter declaration.
15882 parameter-declaration:
15883 decl-specifier-seq ... [opt] declarator
15884 decl-specifier-seq declarator = assignment-expression
15885 decl-specifier-seq ... [opt] abstract-declarator [opt]
15886 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15888 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15889 declares a template parameter. (In that case, a non-nested `>'
15890 token encountered during the parsing of the assignment-expression
15891 is not interpreted as a greater-than operator.)
15893 Returns a representation of the parameter, or NULL if an error
15894 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15895 true iff the declarator is of the form "(p)". */
15897 static cp_parameter_declarator *
15898 cp_parser_parameter_declaration (cp_parser *parser,
15899 bool template_parm_p,
15900 bool *parenthesized_p)
15902 int declares_class_or_enum;
15903 cp_decl_specifier_seq decl_specifiers;
15904 cp_declarator *declarator;
15905 tree default_argument;
15906 cp_token *token = NULL, *declarator_token_start = NULL;
15907 const char *saved_message;
15909 /* In a template parameter, `>' is not an operator.
15913 When parsing a default template-argument for a non-type
15914 template-parameter, the first non-nested `>' is taken as the end
15915 of the template parameter-list rather than a greater-than
15918 /* Type definitions may not appear in parameter types. */
15919 saved_message = parser->type_definition_forbidden_message;
15920 parser->type_definition_forbidden_message
15921 = G_("types may not be defined in parameter types");
15923 /* Parse the declaration-specifiers. */
15924 cp_parser_decl_specifier_seq (parser,
15925 CP_PARSER_FLAGS_NONE,
15927 &declares_class_or_enum);
15929 /* Complain about missing 'typename' or other invalid type names. */
15930 if (!decl_specifiers.any_type_specifiers_p)
15931 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15933 /* If an error occurred, there's no reason to attempt to parse the
15934 rest of the declaration. */
15935 if (cp_parser_error_occurred (parser))
15937 parser->type_definition_forbidden_message = saved_message;
15941 /* Peek at the next token. */
15942 token = cp_lexer_peek_token (parser->lexer);
15944 /* If the next token is a `)', `,', `=', `>', or `...', then there
15945 is no declarator. However, when variadic templates are enabled,
15946 there may be a declarator following `...'. */
15947 if (token->type == CPP_CLOSE_PAREN
15948 || token->type == CPP_COMMA
15949 || token->type == CPP_EQ
15950 || token->type == CPP_GREATER)
15953 if (parenthesized_p)
15954 *parenthesized_p = false;
15956 /* Otherwise, there should be a declarator. */
15959 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15960 parser->default_arg_ok_p = false;
15962 /* After seeing a decl-specifier-seq, if the next token is not a
15963 "(", there is no possibility that the code is a valid
15964 expression. Therefore, if parsing tentatively, we commit at
15966 if (!parser->in_template_argument_list_p
15967 /* In an expression context, having seen:
15971 we cannot be sure whether we are looking at a
15972 function-type (taking a "char" as a parameter) or a cast
15973 of some object of type "char" to "int". */
15974 && !parser->in_type_id_in_expr_p
15975 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15976 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15977 cp_parser_commit_to_tentative_parse (parser);
15978 /* Parse the declarator. */
15979 declarator_token_start = token;
15980 declarator = cp_parser_declarator (parser,
15981 CP_PARSER_DECLARATOR_EITHER,
15982 /*ctor_dtor_or_conv_p=*/NULL,
15984 /*member_p=*/false);
15985 parser->default_arg_ok_p = saved_default_arg_ok_p;
15986 /* After the declarator, allow more attributes. */
15987 decl_specifiers.attributes
15988 = chainon (decl_specifiers.attributes,
15989 cp_parser_attributes_opt (parser));
15992 /* If the next token is an ellipsis, and we have not seen a
15993 declarator name, and the type of the declarator contains parameter
15994 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15995 a parameter pack expansion expression. Otherwise, leave the
15996 ellipsis for a C-style variadic function. */
15997 token = cp_lexer_peek_token (parser->lexer);
15998 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16000 tree type = decl_specifiers.type;
16002 if (type && DECL_P (type))
16003 type = TREE_TYPE (type);
16006 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16007 && declarator_can_be_parameter_pack (declarator)
16008 && (!declarator || !declarator->parameter_pack_p)
16009 && uses_parameter_packs (type))
16011 /* Consume the `...'. */
16012 cp_lexer_consume_token (parser->lexer);
16013 maybe_warn_variadic_templates ();
16015 /* Build a pack expansion type */
16017 declarator->parameter_pack_p = true;
16019 decl_specifiers.type = make_pack_expansion (type);
16023 /* The restriction on defining new types applies only to the type
16024 of the parameter, not to the default argument. */
16025 parser->type_definition_forbidden_message = saved_message;
16027 /* If the next token is `=', then process a default argument. */
16028 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16030 /* Consume the `='. */
16031 cp_lexer_consume_token (parser->lexer);
16033 /* If we are defining a class, then the tokens that make up the
16034 default argument must be saved and processed later. */
16035 if (!template_parm_p && at_class_scope_p ()
16036 && TYPE_BEING_DEFINED (current_class_type)
16037 && !LAMBDA_TYPE_P (current_class_type))
16039 unsigned depth = 0;
16040 int maybe_template_id = 0;
16041 cp_token *first_token;
16044 /* Add tokens until we have processed the entire default
16045 argument. We add the range [first_token, token). */
16046 first_token = cp_lexer_peek_token (parser->lexer);
16051 /* Peek at the next token. */
16052 token = cp_lexer_peek_token (parser->lexer);
16053 /* What we do depends on what token we have. */
16054 switch (token->type)
16056 /* In valid code, a default argument must be
16057 immediately followed by a `,' `)', or `...'. */
16059 if (depth == 0 && maybe_template_id)
16061 /* If we've seen a '<', we might be in a
16062 template-argument-list. Until Core issue 325 is
16063 resolved, we don't know how this situation ought
16064 to be handled, so try to DTRT. We check whether
16065 what comes after the comma is a valid parameter
16066 declaration list. If it is, then the comma ends
16067 the default argument; otherwise the default
16068 argument continues. */
16069 bool error = false;
16072 /* Set ITALP so cp_parser_parameter_declaration_list
16073 doesn't decide to commit to this parse. */
16074 bool saved_italp = parser->in_template_argument_list_p;
16075 parser->in_template_argument_list_p = true;
16077 cp_parser_parse_tentatively (parser);
16078 cp_lexer_consume_token (parser->lexer);
16079 begin_scope (sk_function_parms, NULL_TREE);
16080 cp_parser_parameter_declaration_list (parser, &error);
16081 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16082 pop_binding (DECL_NAME (t), t);
16084 if (!cp_parser_error_occurred (parser) && !error)
16086 cp_parser_abort_tentative_parse (parser);
16088 parser->in_template_argument_list_p = saved_italp;
16091 case CPP_CLOSE_PAREN:
16093 /* If we run into a non-nested `;', `}', or `]',
16094 then the code is invalid -- but the default
16095 argument is certainly over. */
16096 case CPP_SEMICOLON:
16097 case CPP_CLOSE_BRACE:
16098 case CPP_CLOSE_SQUARE:
16101 /* Update DEPTH, if necessary. */
16102 else if (token->type == CPP_CLOSE_PAREN
16103 || token->type == CPP_CLOSE_BRACE
16104 || token->type == CPP_CLOSE_SQUARE)
16108 case CPP_OPEN_PAREN:
16109 case CPP_OPEN_SQUARE:
16110 case CPP_OPEN_BRACE:
16116 /* This might be the comparison operator, or it might
16117 start a template argument list. */
16118 ++maybe_template_id;
16122 if (cxx_dialect == cxx98)
16124 /* Fall through for C++0x, which treats the `>>'
16125 operator like two `>' tokens in certain
16131 /* This might be an operator, or it might close a
16132 template argument list. But if a previous '<'
16133 started a template argument list, this will have
16134 closed it, so we can't be in one anymore. */
16135 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16136 if (maybe_template_id < 0)
16137 maybe_template_id = 0;
16141 /* If we run out of tokens, issue an error message. */
16143 case CPP_PRAGMA_EOL:
16144 error_at (token->location, "file ends in default argument");
16150 /* In these cases, we should look for template-ids.
16151 For example, if the default argument is
16152 `X<int, double>()', we need to do name lookup to
16153 figure out whether or not `X' is a template; if
16154 so, the `,' does not end the default argument.
16156 That is not yet done. */
16163 /* If we've reached the end, stop. */
16167 /* Add the token to the token block. */
16168 token = cp_lexer_consume_token (parser->lexer);
16171 /* Create a DEFAULT_ARG to represent the unparsed default
16173 default_argument = make_node (DEFAULT_ARG);
16174 DEFARG_TOKENS (default_argument)
16175 = cp_token_cache_new (first_token, token);
16176 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16178 /* Outside of a class definition, we can just parse the
16179 assignment-expression. */
16182 token = cp_lexer_peek_token (parser->lexer);
16184 = cp_parser_default_argument (parser, template_parm_p);
16187 if (!parser->default_arg_ok_p)
16189 if (flag_permissive)
16190 warning (0, "deprecated use of default argument for parameter of non-function");
16193 error_at (token->location,
16194 "default arguments are only "
16195 "permitted for function parameters");
16196 default_argument = NULL_TREE;
16199 else if ((declarator && declarator->parameter_pack_p)
16200 || (decl_specifiers.type
16201 && PACK_EXPANSION_P (decl_specifiers.type)))
16203 /* Find the name of the parameter pack. */
16204 cp_declarator *id_declarator = declarator;
16205 while (id_declarator && id_declarator->kind != cdk_id)
16206 id_declarator = id_declarator->declarator;
16208 if (id_declarator && id_declarator->kind == cdk_id)
16209 error_at (declarator_token_start->location,
16211 ? "template parameter pack %qD"
16212 " cannot have a default argument"
16213 : "parameter pack %qD cannot have a default argument",
16214 id_declarator->u.id.unqualified_name);
16216 error_at (declarator_token_start->location,
16218 ? "template parameter pack cannot have a default argument"
16219 : "parameter pack cannot have a default argument");
16221 default_argument = NULL_TREE;
16225 default_argument = NULL_TREE;
16227 return make_parameter_declarator (&decl_specifiers,
16232 /* Parse a default argument and return it.
16234 TEMPLATE_PARM_P is true if this is a default argument for a
16235 non-type template parameter. */
16237 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16239 tree default_argument = NULL_TREE;
16240 bool saved_greater_than_is_operator_p;
16241 bool saved_local_variables_forbidden_p;
16243 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16245 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16246 parser->greater_than_is_operator_p = !template_parm_p;
16247 /* Local variable names (and the `this' keyword) may not
16248 appear in a default argument. */
16249 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16250 parser->local_variables_forbidden_p = true;
16251 /* Parse the assignment-expression. */
16252 if (template_parm_p)
16253 push_deferring_access_checks (dk_no_deferred);
16255 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16256 if (template_parm_p)
16257 pop_deferring_access_checks ();
16258 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16259 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16261 return default_argument;
16264 /* Parse a function-body.
16267 compound_statement */
16270 cp_parser_function_body (cp_parser *parser)
16272 cp_parser_compound_statement (parser, NULL, false);
16275 /* Parse a ctor-initializer-opt followed by a function-body. Return
16276 true if a ctor-initializer was present. */
16279 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16282 bool ctor_initializer_p;
16283 const bool check_body_p =
16284 DECL_CONSTRUCTOR_P (current_function_decl)
16285 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16288 /* Begin the function body. */
16289 body = begin_function_body ();
16290 /* Parse the optional ctor-initializer. */
16291 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16293 /* If we're parsing a constexpr constructor definition, we need
16294 to check that the constructor body is indeed empty. However,
16295 before we get to cp_parser_function_body lot of junk has been
16296 generated, so we can't just check that we have an empty block.
16297 Rather we take a snapshot of the outermost block, and check whether
16298 cp_parser_function_body changed its state. */
16302 if (TREE_CODE (list) == BIND_EXPR)
16303 list = BIND_EXPR_BODY (list);
16304 if (TREE_CODE (list) == STATEMENT_LIST
16305 && STATEMENT_LIST_TAIL (list) != NULL)
16306 last = STATEMENT_LIST_TAIL (list)->stmt;
16308 /* Parse the function-body. */
16309 cp_parser_function_body (parser);
16311 && (TREE_CODE (list) != STATEMENT_LIST
16312 || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL)
16313 || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt)))
16315 error ("constexpr constructor does not have empty body");
16316 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
16318 /* Finish the function body. */
16319 finish_function_body (body);
16321 return ctor_initializer_p;
16324 /* Parse an initializer.
16327 = initializer-clause
16328 ( expression-list )
16330 Returns an expression representing the initializer. If no
16331 initializer is present, NULL_TREE is returned.
16333 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16334 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16335 set to TRUE if there is no initializer present. If there is an
16336 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16337 is set to true; otherwise it is set to false. */
16340 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16341 bool* non_constant_p)
16346 /* Peek at the next token. */
16347 token = cp_lexer_peek_token (parser->lexer);
16349 /* Let our caller know whether or not this initializer was
16351 *is_direct_init = (token->type != CPP_EQ);
16352 /* Assume that the initializer is constant. */
16353 *non_constant_p = false;
16355 if (token->type == CPP_EQ)
16357 /* Consume the `='. */
16358 cp_lexer_consume_token (parser->lexer);
16359 /* Parse the initializer-clause. */
16360 init = cp_parser_initializer_clause (parser, non_constant_p);
16362 else if (token->type == CPP_OPEN_PAREN)
16365 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16367 /*allow_expansion_p=*/true,
16370 return error_mark_node;
16371 init = build_tree_list_vec (vec);
16372 release_tree_vector (vec);
16374 else if (token->type == CPP_OPEN_BRACE)
16376 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16377 init = cp_parser_braced_list (parser, non_constant_p);
16378 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16382 /* Anything else is an error. */
16383 cp_parser_error (parser, "expected initializer");
16384 init = error_mark_node;
16390 /* Parse an initializer-clause.
16392 initializer-clause:
16393 assignment-expression
16396 Returns an expression representing the initializer.
16398 If the `assignment-expression' production is used the value
16399 returned is simply a representation for the expression.
16401 Otherwise, calls cp_parser_braced_list. */
16404 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16408 /* Assume the expression is constant. */
16409 *non_constant_p = false;
16411 /* If it is not a `{', then we are looking at an
16412 assignment-expression. */
16413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16416 = cp_parser_constant_expression (parser,
16417 /*allow_non_constant_p=*/true,
16419 if (!*non_constant_p)
16421 /* We only want to fold if this is really a constant
16422 expression. FIXME Actually, we don't want to fold here, but in
16424 tree folded = fold_non_dependent_expr (initializer);
16425 folded = maybe_constant_value (folded);
16426 if (TREE_CONSTANT (folded))
16427 initializer = folded;
16431 initializer = cp_parser_braced_list (parser, non_constant_p);
16433 return initializer;
16436 /* Parse a brace-enclosed initializer list.
16439 { initializer-list , [opt] }
16442 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16443 the elements of the initializer-list (or NULL, if the last
16444 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16445 NULL_TREE. There is no way to detect whether or not the optional
16446 trailing `,' was provided. NON_CONSTANT_P is as for
16447 cp_parser_initializer. */
16450 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16454 /* Consume the `{' token. */
16455 cp_lexer_consume_token (parser->lexer);
16456 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16457 initializer = make_node (CONSTRUCTOR);
16458 /* If it's not a `}', then there is a non-trivial initializer. */
16459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16461 /* Parse the initializer list. */
16462 CONSTRUCTOR_ELTS (initializer)
16463 = cp_parser_initializer_list (parser, non_constant_p);
16464 /* A trailing `,' token is allowed. */
16465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16466 cp_lexer_consume_token (parser->lexer);
16468 /* Now, there should be a trailing `}'. */
16469 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16470 TREE_TYPE (initializer) = init_list_type_node;
16471 return initializer;
16474 /* Parse an initializer-list.
16477 initializer-clause ... [opt]
16478 initializer-list , initializer-clause ... [opt]
16483 identifier : initializer-clause
16484 initializer-list, identifier : initializer-clause
16486 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16487 for the initializer. If the INDEX of the elt is non-NULL, it is the
16488 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16489 as for cp_parser_initializer. */
16491 static VEC(constructor_elt,gc) *
16492 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16494 VEC(constructor_elt,gc) *v = NULL;
16496 /* Assume all of the expressions are constant. */
16497 *non_constant_p = false;
16499 /* Parse the rest of the list. */
16505 bool clause_non_constant_p;
16507 /* If the next token is an identifier and the following one is a
16508 colon, we are looking at the GNU designated-initializer
16510 if (cp_parser_allow_gnu_extensions_p (parser)
16511 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16514 /* Warn the user that they are using an extension. */
16515 pedwarn (input_location, OPT_pedantic,
16516 "ISO C++ does not allow designated initializers");
16517 /* Consume the identifier. */
16518 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16519 /* Consume the `:'. */
16520 cp_lexer_consume_token (parser->lexer);
16523 identifier = NULL_TREE;
16525 /* Parse the initializer. */
16526 initializer = cp_parser_initializer_clause (parser,
16527 &clause_non_constant_p);
16528 /* If any clause is non-constant, so is the entire initializer. */
16529 if (clause_non_constant_p)
16530 *non_constant_p = true;
16532 /* If we have an ellipsis, this is an initializer pack
16534 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16536 /* Consume the `...'. */
16537 cp_lexer_consume_token (parser->lexer);
16539 /* Turn the initializer into an initializer expansion. */
16540 initializer = make_pack_expansion (initializer);
16543 /* Add it to the vector. */
16544 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16546 /* If the next token is not a comma, we have reached the end of
16548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16551 /* Peek at the next token. */
16552 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16553 /* If the next token is a `}', then we're still done. An
16554 initializer-clause can have a trailing `,' after the
16555 initializer-list and before the closing `}'. */
16556 if (token->type == CPP_CLOSE_BRACE)
16559 /* Consume the `,' token. */
16560 cp_lexer_consume_token (parser->lexer);
16566 /* Classes [gram.class] */
16568 /* Parse a class-name.
16574 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16575 to indicate that names looked up in dependent types should be
16576 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16577 keyword has been used to indicate that the name that appears next
16578 is a template. TAG_TYPE indicates the explicit tag given before
16579 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16580 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16581 is the class being defined in a class-head.
16583 Returns the TYPE_DECL representing the class. */
16586 cp_parser_class_name (cp_parser *parser,
16587 bool typename_keyword_p,
16588 bool template_keyword_p,
16589 enum tag_types tag_type,
16590 bool check_dependency_p,
16592 bool is_declaration)
16598 tree identifier = NULL_TREE;
16600 /* All class-names start with an identifier. */
16601 token = cp_lexer_peek_token (parser->lexer);
16602 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16604 cp_parser_error (parser, "expected class-name");
16605 return error_mark_node;
16608 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16609 to a template-id, so we save it here. */
16610 scope = parser->scope;
16611 if (scope == error_mark_node)
16612 return error_mark_node;
16614 /* Any name names a type if we're following the `typename' keyword
16615 in a qualified name where the enclosing scope is type-dependent. */
16616 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16617 && dependent_type_p (scope));
16618 /* Handle the common case (an identifier, but not a template-id)
16620 if (token->type == CPP_NAME
16621 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16623 cp_token *identifier_token;
16626 /* Look for the identifier. */
16627 identifier_token = cp_lexer_peek_token (parser->lexer);
16628 ambiguous_p = identifier_token->ambiguous_p;
16629 identifier = cp_parser_identifier (parser);
16630 /* If the next token isn't an identifier, we are certainly not
16631 looking at a class-name. */
16632 if (identifier == error_mark_node)
16633 decl = error_mark_node;
16634 /* If we know this is a type-name, there's no need to look it
16636 else if (typename_p)
16640 tree ambiguous_decls;
16641 /* If we already know that this lookup is ambiguous, then
16642 we've already issued an error message; there's no reason
16646 cp_parser_simulate_error (parser);
16647 return error_mark_node;
16649 /* If the next token is a `::', then the name must be a type
16652 [basic.lookup.qual]
16654 During the lookup for a name preceding the :: scope
16655 resolution operator, object, function, and enumerator
16656 names are ignored. */
16657 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16658 tag_type = typename_type;
16659 /* Look up the name. */
16660 decl = cp_parser_lookup_name (parser, identifier,
16662 /*is_template=*/false,
16663 /*is_namespace=*/false,
16664 check_dependency_p,
16666 identifier_token->location);
16667 if (ambiguous_decls)
16669 if (cp_parser_parsing_tentatively (parser))
16670 cp_parser_simulate_error (parser);
16671 return error_mark_node;
16677 /* Try a template-id. */
16678 decl = cp_parser_template_id (parser, template_keyword_p,
16679 check_dependency_p,
16681 if (decl == error_mark_node)
16682 return error_mark_node;
16685 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16687 /* If this is a typename, create a TYPENAME_TYPE. */
16688 if (typename_p && decl != error_mark_node)
16690 decl = make_typename_type (scope, decl, typename_type,
16691 /*complain=*/tf_error);
16692 if (decl != error_mark_node)
16693 decl = TYPE_NAME (decl);
16696 /* Check to see that it is really the name of a class. */
16697 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16698 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16699 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16700 /* Situations like this:
16702 template <typename T> struct A {
16703 typename T::template X<int>::I i;
16706 are problematic. Is `T::template X<int>' a class-name? The
16707 standard does not seem to be definitive, but there is no other
16708 valid interpretation of the following `::'. Therefore, those
16709 names are considered class-names. */
16711 decl = make_typename_type (scope, decl, tag_type, tf_error);
16712 if (decl != error_mark_node)
16713 decl = TYPE_NAME (decl);
16715 else if (TREE_CODE (decl) != TYPE_DECL
16716 || TREE_TYPE (decl) == error_mark_node
16717 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16718 decl = error_mark_node;
16720 if (decl == error_mark_node)
16721 cp_parser_error (parser, "expected class-name");
16722 else if (identifier && !parser->scope)
16723 maybe_note_name_used_in_class (identifier, decl);
16728 /* Parse a class-specifier.
16731 class-head { member-specification [opt] }
16733 Returns the TREE_TYPE representing the class. */
16736 cp_parser_class_specifier (cp_parser* parser)
16739 tree attributes = NULL_TREE;
16740 bool nested_name_specifier_p;
16741 unsigned saved_num_template_parameter_lists;
16742 bool saved_in_function_body;
16743 bool saved_in_unbraced_linkage_specification_p;
16744 tree old_scope = NULL_TREE;
16745 tree scope = NULL_TREE;
16748 push_deferring_access_checks (dk_no_deferred);
16750 /* Parse the class-head. */
16751 type = cp_parser_class_head (parser,
16752 &nested_name_specifier_p,
16755 /* If the class-head was a semantic disaster, skip the entire body
16759 cp_parser_skip_to_end_of_block_or_statement (parser);
16760 pop_deferring_access_checks ();
16761 return error_mark_node;
16764 /* Look for the `{'. */
16765 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16767 pop_deferring_access_checks ();
16768 return error_mark_node;
16771 /* Process the base classes. If they're invalid, skip the
16772 entire class body. */
16773 if (!xref_basetypes (type, bases))
16775 /* Consuming the closing brace yields better error messages
16777 if (cp_parser_skip_to_closing_brace (parser))
16778 cp_lexer_consume_token (parser->lexer);
16779 pop_deferring_access_checks ();
16780 return error_mark_node;
16783 /* Issue an error message if type-definitions are forbidden here. */
16784 cp_parser_check_type_definition (parser);
16785 /* Remember that we are defining one more class. */
16786 ++parser->num_classes_being_defined;
16787 /* Inside the class, surrounding template-parameter-lists do not
16789 saved_num_template_parameter_lists
16790 = parser->num_template_parameter_lists;
16791 parser->num_template_parameter_lists = 0;
16792 /* We are not in a function body. */
16793 saved_in_function_body = parser->in_function_body;
16794 parser->in_function_body = false;
16795 /* We are not immediately inside an extern "lang" block. */
16796 saved_in_unbraced_linkage_specification_p
16797 = parser->in_unbraced_linkage_specification_p;
16798 parser->in_unbraced_linkage_specification_p = false;
16800 /* Start the class. */
16801 if (nested_name_specifier_p)
16803 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16804 old_scope = push_inner_scope (scope);
16806 type = begin_class_definition (type, attributes);
16808 if (type == error_mark_node)
16809 /* If the type is erroneous, skip the entire body of the class. */
16810 cp_parser_skip_to_closing_brace (parser);
16812 /* Parse the member-specification. */
16813 cp_parser_member_specification_opt (parser);
16815 /* Look for the trailing `}'. */
16816 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16817 /* Look for trailing attributes to apply to this class. */
16818 if (cp_parser_allow_gnu_extensions_p (parser))
16819 attributes = cp_parser_attributes_opt (parser);
16820 if (type != error_mark_node)
16821 type = finish_struct (type, attributes);
16822 if (nested_name_specifier_p)
16823 pop_inner_scope (old_scope, scope);
16824 /* If this class is not itself within the scope of another class,
16825 then we need to parse the bodies of all of the queued function
16826 definitions. Note that the queued functions defined in a class
16827 are not always processed immediately following the
16828 class-specifier for that class. Consider:
16831 struct B { void f() { sizeof (A); } };
16834 If `f' were processed before the processing of `A' were
16835 completed, there would be no way to compute the size of `A'.
16836 Note that the nesting we are interested in here is lexical --
16837 not the semantic nesting given by TYPE_CONTEXT. In particular,
16840 struct A { struct B; };
16841 struct A::B { void f() { } };
16843 there is no need to delay the parsing of `A::B::f'. */
16844 if (--parser->num_classes_being_defined == 0)
16847 tree class_type = NULL_TREE;
16848 tree pushed_scope = NULL_TREE;
16850 cp_default_arg_entry *e;
16852 /* In a first pass, parse default arguments to the functions.
16853 Then, in a second pass, parse the bodies of the functions.
16854 This two-phased approach handles cases like:
16862 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16866 /* If there are default arguments that have not yet been processed,
16867 take care of them now. */
16868 if (class_type != e->class_type)
16871 pop_scope (pushed_scope);
16872 class_type = e->class_type;
16873 pushed_scope = push_scope (class_type);
16875 /* Make sure that any template parameters are in scope. */
16876 maybe_begin_member_template_processing (fn);
16877 /* Parse the default argument expressions. */
16878 cp_parser_late_parsing_default_args (parser, fn);
16879 /* Remove any template parameters from the symbol table. */
16880 maybe_end_member_template_processing ();
16883 pop_scope (pushed_scope);
16884 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16885 /* Now parse the body of the functions. */
16886 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16887 cp_parser_late_parsing_for_member (parser, fn);
16888 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16891 /* Put back any saved access checks. */
16892 pop_deferring_access_checks ();
16894 /* Restore saved state. */
16895 parser->in_function_body = saved_in_function_body;
16896 parser->num_template_parameter_lists
16897 = saved_num_template_parameter_lists;
16898 parser->in_unbraced_linkage_specification_p
16899 = saved_in_unbraced_linkage_specification_p;
16904 /* Parse a class-head.
16907 class-key identifier [opt] base-clause [opt]
16908 class-key nested-name-specifier identifier base-clause [opt]
16909 class-key nested-name-specifier [opt] template-id
16913 class-key attributes identifier [opt] base-clause [opt]
16914 class-key attributes nested-name-specifier identifier base-clause [opt]
16915 class-key attributes nested-name-specifier [opt] template-id
16918 Upon return BASES is initialized to the list of base classes (or
16919 NULL, if there are none) in the same form returned by
16920 cp_parser_base_clause.
16922 Returns the TYPE of the indicated class. Sets
16923 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16924 involving a nested-name-specifier was used, and FALSE otherwise.
16926 Returns error_mark_node if this is not a class-head.
16928 Returns NULL_TREE if the class-head is syntactically valid, but
16929 semantically invalid in a way that means we should skip the entire
16930 body of the class. */
16933 cp_parser_class_head (cp_parser* parser,
16934 bool* nested_name_specifier_p,
16935 tree *attributes_p,
16938 tree nested_name_specifier;
16939 enum tag_types class_key;
16940 tree id = NULL_TREE;
16941 tree type = NULL_TREE;
16943 bool template_id_p = false;
16944 bool qualified_p = false;
16945 bool invalid_nested_name_p = false;
16946 bool invalid_explicit_specialization_p = false;
16947 tree pushed_scope = NULL_TREE;
16948 unsigned num_templates;
16949 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16950 /* Assume no nested-name-specifier will be present. */
16951 *nested_name_specifier_p = false;
16952 /* Assume no template parameter lists will be used in defining the
16956 *bases = NULL_TREE;
16958 /* Look for the class-key. */
16959 class_key = cp_parser_class_key (parser);
16960 if (class_key == none_type)
16961 return error_mark_node;
16963 /* Parse the attributes. */
16964 attributes = cp_parser_attributes_opt (parser);
16966 /* If the next token is `::', that is invalid -- but sometimes
16967 people do try to write:
16971 Handle this gracefully by accepting the extra qualifier, and then
16972 issuing an error about it later if this really is a
16973 class-head. If it turns out just to be an elaborated type
16974 specifier, remain silent. */
16975 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16976 qualified_p = true;
16978 push_deferring_access_checks (dk_no_check);
16980 /* Determine the name of the class. Begin by looking for an
16981 optional nested-name-specifier. */
16982 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16983 nested_name_specifier
16984 = cp_parser_nested_name_specifier_opt (parser,
16985 /*typename_keyword_p=*/false,
16986 /*check_dependency_p=*/false,
16988 /*is_declaration=*/false);
16989 /* If there was a nested-name-specifier, then there *must* be an
16991 if (nested_name_specifier)
16993 type_start_token = cp_lexer_peek_token (parser->lexer);
16994 /* Although the grammar says `identifier', it really means
16995 `class-name' or `template-name'. You are only allowed to
16996 define a class that has already been declared with this
16999 The proposed resolution for Core Issue 180 says that wherever
17000 you see `class T::X' you should treat `X' as a type-name.
17002 It is OK to define an inaccessible class; for example:
17004 class A { class B; };
17007 We do not know if we will see a class-name, or a
17008 template-name. We look for a class-name first, in case the
17009 class-name is a template-id; if we looked for the
17010 template-name first we would stop after the template-name. */
17011 cp_parser_parse_tentatively (parser);
17012 type = cp_parser_class_name (parser,
17013 /*typename_keyword_p=*/false,
17014 /*template_keyword_p=*/false,
17016 /*check_dependency_p=*/false,
17017 /*class_head_p=*/true,
17018 /*is_declaration=*/false);
17019 /* If that didn't work, ignore the nested-name-specifier. */
17020 if (!cp_parser_parse_definitely (parser))
17022 invalid_nested_name_p = true;
17023 type_start_token = cp_lexer_peek_token (parser->lexer);
17024 id = cp_parser_identifier (parser);
17025 if (id == error_mark_node)
17028 /* If we could not find a corresponding TYPE, treat this
17029 declaration like an unqualified declaration. */
17030 if (type == error_mark_node)
17031 nested_name_specifier = NULL_TREE;
17032 /* Otherwise, count the number of templates used in TYPE and its
17033 containing scopes. */
17038 for (scope = TREE_TYPE (type);
17039 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17040 scope = (TYPE_P (scope)
17041 ? TYPE_CONTEXT (scope)
17042 : DECL_CONTEXT (scope)))
17044 && CLASS_TYPE_P (scope)
17045 && CLASSTYPE_TEMPLATE_INFO (scope)
17046 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17047 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17051 /* Otherwise, the identifier is optional. */
17054 /* We don't know whether what comes next is a template-id,
17055 an identifier, or nothing at all. */
17056 cp_parser_parse_tentatively (parser);
17057 /* Check for a template-id. */
17058 type_start_token = cp_lexer_peek_token (parser->lexer);
17059 id = cp_parser_template_id (parser,
17060 /*template_keyword_p=*/false,
17061 /*check_dependency_p=*/true,
17062 /*is_declaration=*/true);
17063 /* If that didn't work, it could still be an identifier. */
17064 if (!cp_parser_parse_definitely (parser))
17066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17068 type_start_token = cp_lexer_peek_token (parser->lexer);
17069 id = cp_parser_identifier (parser);
17076 template_id_p = true;
17081 pop_deferring_access_checks ();
17084 cp_parser_check_for_invalid_template_id (parser, id,
17085 type_start_token->location);
17087 /* If it's not a `:' or a `{' then we can't really be looking at a
17088 class-head, since a class-head only appears as part of a
17089 class-specifier. We have to detect this situation before calling
17090 xref_tag, since that has irreversible side-effects. */
17091 if (!cp_parser_next_token_starts_class_definition_p (parser))
17093 cp_parser_error (parser, "expected %<{%> or %<:%>");
17094 return error_mark_node;
17097 /* At this point, we're going ahead with the class-specifier, even
17098 if some other problem occurs. */
17099 cp_parser_commit_to_tentative_parse (parser);
17100 /* Issue the error about the overly-qualified name now. */
17103 cp_parser_error (parser,
17104 "global qualification of class name is invalid");
17105 return error_mark_node;
17107 else if (invalid_nested_name_p)
17109 cp_parser_error (parser,
17110 "qualified name does not name a class");
17111 return error_mark_node;
17113 else if (nested_name_specifier)
17117 /* Reject typedef-names in class heads. */
17118 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17120 error_at (type_start_token->location,
17121 "invalid class name in declaration of %qD",
17127 /* Figure out in what scope the declaration is being placed. */
17128 scope = current_scope ();
17129 /* If that scope does not contain the scope in which the
17130 class was originally declared, the program is invalid. */
17131 if (scope && !is_ancestor (scope, nested_name_specifier))
17133 if (at_namespace_scope_p ())
17134 error_at (type_start_token->location,
17135 "declaration of %qD in namespace %qD which does not "
17137 type, scope, nested_name_specifier);
17139 error_at (type_start_token->location,
17140 "declaration of %qD in %qD which does not enclose %qD",
17141 type, scope, nested_name_specifier);
17147 A declarator-id shall not be qualified except for the
17148 definition of a ... nested class outside of its class
17149 ... [or] the definition or explicit instantiation of a
17150 class member of a namespace outside of its namespace. */
17151 if (scope == nested_name_specifier)
17153 permerror (nested_name_specifier_token_start->location,
17154 "extra qualification not allowed");
17155 nested_name_specifier = NULL_TREE;
17159 /* An explicit-specialization must be preceded by "template <>". If
17160 it is not, try to recover gracefully. */
17161 if (at_namespace_scope_p ()
17162 && parser->num_template_parameter_lists == 0
17165 error_at (type_start_token->location,
17166 "an explicit specialization must be preceded by %<template <>%>");
17167 invalid_explicit_specialization_p = true;
17168 /* Take the same action that would have been taken by
17169 cp_parser_explicit_specialization. */
17170 ++parser->num_template_parameter_lists;
17171 begin_specialization ();
17173 /* There must be no "return" statements between this point and the
17174 end of this function; set "type "to the correct return value and
17175 use "goto done;" to return. */
17176 /* Make sure that the right number of template parameters were
17178 if (!cp_parser_check_template_parameters (parser, num_templates,
17179 type_start_token->location,
17180 /*declarator=*/NULL))
17182 /* If something went wrong, there is no point in even trying to
17183 process the class-definition. */
17188 /* Look up the type. */
17191 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17192 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17193 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17195 error_at (type_start_token->location,
17196 "function template %qD redeclared as a class template", id);
17197 type = error_mark_node;
17201 type = TREE_TYPE (id);
17202 type = maybe_process_partial_specialization (type);
17204 if (nested_name_specifier)
17205 pushed_scope = push_scope (nested_name_specifier);
17207 else if (nested_name_specifier)
17213 template <typename T> struct S { struct T };
17214 template <typename T> struct S<T>::T { };
17216 we will get a TYPENAME_TYPE when processing the definition of
17217 `S::T'. We need to resolve it to the actual type before we
17218 try to define it. */
17219 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17221 class_type = resolve_typename_type (TREE_TYPE (type),
17222 /*only_current_p=*/false);
17223 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17224 type = TYPE_NAME (class_type);
17227 cp_parser_error (parser, "could not resolve typename type");
17228 type = error_mark_node;
17232 if (maybe_process_partial_specialization (TREE_TYPE (type))
17233 == error_mark_node)
17239 class_type = current_class_type;
17240 /* Enter the scope indicated by the nested-name-specifier. */
17241 pushed_scope = push_scope (nested_name_specifier);
17242 /* Get the canonical version of this type. */
17243 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17244 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17245 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17247 type = push_template_decl (type);
17248 if (type == error_mark_node)
17255 type = TREE_TYPE (type);
17256 *nested_name_specifier_p = true;
17258 else /* The name is not a nested name. */
17260 /* If the class was unnamed, create a dummy name. */
17262 id = make_anon_name ();
17263 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17264 parser->num_template_parameter_lists);
17267 /* Indicate whether this class was declared as a `class' or as a
17269 if (TREE_CODE (type) == RECORD_TYPE)
17270 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17271 cp_parser_check_class_key (class_key, type);
17273 /* If this type was already complete, and we see another definition,
17274 that's an error. */
17275 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17277 error_at (type_start_token->location, "redefinition of %q#T",
17279 error_at (type_start_token->location, "previous definition of %q+#T",
17284 else if (type == error_mark_node)
17287 /* We will have entered the scope containing the class; the names of
17288 base classes should be looked up in that context. For example:
17290 struct A { struct B {}; struct C; };
17291 struct A::C : B {};
17295 /* Get the list of base-classes, if there is one. */
17296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17297 *bases = cp_parser_base_clause (parser);
17300 /* Leave the scope given by the nested-name-specifier. We will
17301 enter the class scope itself while processing the members. */
17303 pop_scope (pushed_scope);
17305 if (invalid_explicit_specialization_p)
17307 end_specialization ();
17308 --parser->num_template_parameter_lists;
17312 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17313 *attributes_p = attributes;
17317 /* Parse a class-key.
17324 Returns the kind of class-key specified, or none_type to indicate
17327 static enum tag_types
17328 cp_parser_class_key (cp_parser* parser)
17331 enum tag_types tag_type;
17333 /* Look for the class-key. */
17334 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17338 /* Check to see if the TOKEN is a class-key. */
17339 tag_type = cp_parser_token_is_class_key (token);
17341 cp_parser_error (parser, "expected class-key");
17345 /* Parse an (optional) member-specification.
17347 member-specification:
17348 member-declaration member-specification [opt]
17349 access-specifier : member-specification [opt] */
17352 cp_parser_member_specification_opt (cp_parser* parser)
17359 /* Peek at the next token. */
17360 token = cp_lexer_peek_token (parser->lexer);
17361 /* If it's a `}', or EOF then we've seen all the members. */
17362 if (token->type == CPP_CLOSE_BRACE
17363 || token->type == CPP_EOF
17364 || token->type == CPP_PRAGMA_EOL)
17367 /* See if this token is a keyword. */
17368 keyword = token->keyword;
17372 case RID_PROTECTED:
17374 /* Consume the access-specifier. */
17375 cp_lexer_consume_token (parser->lexer);
17376 /* Remember which access-specifier is active. */
17377 current_access_specifier = token->u.value;
17378 /* Look for the `:'. */
17379 cp_parser_require (parser, CPP_COLON, RT_COLON);
17383 /* Accept #pragmas at class scope. */
17384 if (token->type == CPP_PRAGMA)
17386 cp_parser_pragma (parser, pragma_external);
17390 /* Otherwise, the next construction must be a
17391 member-declaration. */
17392 cp_parser_member_declaration (parser);
17397 /* Parse a member-declaration.
17399 member-declaration:
17400 decl-specifier-seq [opt] member-declarator-list [opt] ;
17401 function-definition ; [opt]
17402 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17404 template-declaration
17406 member-declarator-list:
17408 member-declarator-list , member-declarator
17411 declarator pure-specifier [opt]
17412 declarator constant-initializer [opt]
17413 identifier [opt] : constant-expression
17417 member-declaration:
17418 __extension__ member-declaration
17421 declarator attributes [opt] pure-specifier [opt]
17422 declarator attributes [opt] constant-initializer [opt]
17423 identifier [opt] attributes [opt] : constant-expression
17427 member-declaration:
17428 static_assert-declaration */
17431 cp_parser_member_declaration (cp_parser* parser)
17433 cp_decl_specifier_seq decl_specifiers;
17434 tree prefix_attributes;
17436 int declares_class_or_enum;
17438 cp_token *token = NULL;
17439 cp_token *decl_spec_token_start = NULL;
17440 cp_token *initializer_token_start = NULL;
17441 int saved_pedantic;
17443 /* Check for the `__extension__' keyword. */
17444 if (cp_parser_extension_opt (parser, &saved_pedantic))
17447 cp_parser_member_declaration (parser);
17448 /* Restore the old value of the PEDANTIC flag. */
17449 pedantic = saved_pedantic;
17454 /* Check for a template-declaration. */
17455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17457 /* An explicit specialization here is an error condition, and we
17458 expect the specialization handler to detect and report this. */
17459 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17460 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17461 cp_parser_explicit_specialization (parser);
17463 cp_parser_template_declaration (parser, /*member_p=*/true);
17468 /* Check for a using-declaration. */
17469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17471 /* Parse the using-declaration. */
17472 cp_parser_using_declaration (parser,
17473 /*access_declaration_p=*/false);
17477 /* Check for @defs. */
17478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17481 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17482 ivar = ivar_chains;
17486 ivar = TREE_CHAIN (member);
17487 TREE_CHAIN (member) = NULL_TREE;
17488 finish_member_declaration (member);
17493 /* If the next token is `static_assert' we have a static assertion. */
17494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17496 cp_parser_static_assert (parser, /*member_p=*/true);
17500 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17503 /* Parse the decl-specifier-seq. */
17504 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17505 cp_parser_decl_specifier_seq (parser,
17506 CP_PARSER_FLAGS_OPTIONAL,
17508 &declares_class_or_enum);
17509 prefix_attributes = decl_specifiers.attributes;
17510 decl_specifiers.attributes = NULL_TREE;
17511 /* Check for an invalid type-name. */
17512 if (!decl_specifiers.any_type_specifiers_p
17513 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17515 /* If there is no declarator, then the decl-specifier-seq should
17517 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17519 /* If there was no decl-specifier-seq, and the next token is a
17520 `;', then we have something like:
17526 Each member-declaration shall declare at least one member
17527 name of the class. */
17528 if (!decl_specifiers.any_specifiers_p)
17530 cp_token *token = cp_lexer_peek_token (parser->lexer);
17531 if (!in_system_header_at (token->location))
17532 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17538 /* See if this declaration is a friend. */
17539 friend_p = cp_parser_friend_p (&decl_specifiers);
17540 /* If there were decl-specifiers, check to see if there was
17541 a class-declaration. */
17542 type = check_tag_decl (&decl_specifiers);
17543 /* Nested classes have already been added to the class, but
17544 a `friend' needs to be explicitly registered. */
17547 /* If the `friend' keyword was present, the friend must
17548 be introduced with a class-key. */
17549 if (!declares_class_or_enum)
17550 error_at (decl_spec_token_start->location,
17551 "a class-key must be used when declaring a friend");
17554 template <typename T> struct A {
17555 friend struct A<T>::B;
17558 A<T>::B will be represented by a TYPENAME_TYPE, and
17559 therefore not recognized by check_tag_decl. */
17561 && decl_specifiers.type
17562 && TYPE_P (decl_specifiers.type))
17563 type = decl_specifiers.type;
17564 if (!type || !TYPE_P (type))
17565 error_at (decl_spec_token_start->location,
17566 "friend declaration does not name a class or "
17569 make_friend_class (current_class_type, type,
17570 /*complain=*/true);
17572 /* If there is no TYPE, an error message will already have
17574 else if (!type || type == error_mark_node)
17576 /* An anonymous aggregate has to be handled specially; such
17577 a declaration really declares a data member (with a
17578 particular type), as opposed to a nested class. */
17579 else if (ANON_AGGR_TYPE_P (type))
17581 /* Remove constructors and such from TYPE, now that we
17582 know it is an anonymous aggregate. */
17583 fixup_anonymous_aggr (type);
17584 /* And make the corresponding data member. */
17585 decl = build_decl (decl_spec_token_start->location,
17586 FIELD_DECL, NULL_TREE, type);
17587 /* Add it to the class. */
17588 finish_member_declaration (decl);
17591 cp_parser_check_access_in_redeclaration
17593 decl_spec_token_start->location);
17598 /* See if these declarations will be friends. */
17599 friend_p = cp_parser_friend_p (&decl_specifiers);
17601 /* Keep going until we hit the `;' at the end of the
17603 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17605 tree attributes = NULL_TREE;
17606 tree first_attribute;
17608 /* Peek at the next token. */
17609 token = cp_lexer_peek_token (parser->lexer);
17611 /* Check for a bitfield declaration. */
17612 if (token->type == CPP_COLON
17613 || (token->type == CPP_NAME
17614 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17620 /* Get the name of the bitfield. Note that we cannot just
17621 check TOKEN here because it may have been invalidated by
17622 the call to cp_lexer_peek_nth_token above. */
17623 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17624 identifier = cp_parser_identifier (parser);
17626 identifier = NULL_TREE;
17628 /* Consume the `:' token. */
17629 cp_lexer_consume_token (parser->lexer);
17630 /* Get the width of the bitfield. */
17632 = cp_parser_constant_expression (parser,
17633 /*allow_non_constant=*/false,
17636 /* Look for attributes that apply to the bitfield. */
17637 attributes = cp_parser_attributes_opt (parser);
17638 /* Remember which attributes are prefix attributes and
17640 first_attribute = attributes;
17641 /* Combine the attributes. */
17642 attributes = chainon (prefix_attributes, attributes);
17644 /* Create the bitfield declaration. */
17645 decl = grokbitfield (identifier
17646 ? make_id_declarator (NULL_TREE,
17656 cp_declarator *declarator;
17658 tree asm_specification;
17659 int ctor_dtor_or_conv_p;
17661 /* Parse the declarator. */
17663 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17664 &ctor_dtor_or_conv_p,
17665 /*parenthesized_p=*/NULL,
17666 /*member_p=*/true);
17668 /* If something went wrong parsing the declarator, make sure
17669 that we at least consume some tokens. */
17670 if (declarator == cp_error_declarator)
17672 /* Skip to the end of the statement. */
17673 cp_parser_skip_to_end_of_statement (parser);
17674 /* If the next token is not a semicolon, that is
17675 probably because we just skipped over the body of
17676 a function. So, we consume a semicolon if
17677 present, but do not issue an error message if it
17679 if (cp_lexer_next_token_is (parser->lexer,
17681 cp_lexer_consume_token (parser->lexer);
17685 if (declares_class_or_enum & 2)
17686 cp_parser_check_for_definition_in_return_type
17687 (declarator, decl_specifiers.type,
17688 decl_specifiers.type_location);
17690 /* Look for an asm-specification. */
17691 asm_specification = cp_parser_asm_specification_opt (parser);
17692 /* Look for attributes that apply to the declaration. */
17693 attributes = cp_parser_attributes_opt (parser);
17694 /* Remember which attributes are prefix attributes and
17696 first_attribute = attributes;
17697 /* Combine the attributes. */
17698 attributes = chainon (prefix_attributes, attributes);
17700 /* If it's an `=', then we have a constant-initializer or a
17701 pure-specifier. It is not correct to parse the
17702 initializer before registering the member declaration
17703 since the member declaration should be in scope while
17704 its initializer is processed. However, the rest of the
17705 front end does not yet provide an interface that allows
17706 us to handle this correctly. */
17707 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17711 A pure-specifier shall be used only in the declaration of
17712 a virtual function.
17714 A member-declarator can contain a constant-initializer
17715 only if it declares a static member of integral or
17718 Therefore, if the DECLARATOR is for a function, we look
17719 for a pure-specifier; otherwise, we look for a
17720 constant-initializer. When we call `grokfield', it will
17721 perform more stringent semantics checks. */
17722 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17723 if (function_declarator_p (declarator))
17724 initializer = cp_parser_pure_specifier (parser);
17726 /* Parse the initializer. */
17727 initializer = cp_parser_constant_initializer (parser);
17729 /* Otherwise, there is no initializer. */
17731 initializer = NULL_TREE;
17733 /* See if we are probably looking at a function
17734 definition. We are certainly not looking at a
17735 member-declarator. Calling `grokfield' has
17736 side-effects, so we must not do it unless we are sure
17737 that we are looking at a member-declarator. */
17738 if (cp_parser_token_starts_function_definition_p
17739 (cp_lexer_peek_token (parser->lexer)))
17741 /* The grammar does not allow a pure-specifier to be
17742 used when a member function is defined. (It is
17743 possible that this fact is an oversight in the
17744 standard, since a pure function may be defined
17745 outside of the class-specifier. */
17747 error_at (initializer_token_start->location,
17748 "pure-specifier on function-definition");
17749 decl = cp_parser_save_member_function_body (parser,
17753 /* If the member was not a friend, declare it here. */
17755 finish_member_declaration (decl);
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 /* If the next token is a semicolon, consume it. */
17759 if (token->type == CPP_SEMICOLON)
17760 cp_lexer_consume_token (parser->lexer);
17764 if (declarator->kind == cdk_function)
17765 declarator->id_loc = token->location;
17766 /* Create the declaration. */
17767 decl = grokfield (declarator, &decl_specifiers,
17768 initializer, /*init_const_expr_p=*/true,
17773 /* Reset PREFIX_ATTRIBUTES. */
17774 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17775 attributes = TREE_CHAIN (attributes);
17777 TREE_CHAIN (attributes) = NULL_TREE;
17779 /* If there is any qualification still in effect, clear it
17780 now; we will be starting fresh with the next declarator. */
17781 parser->scope = NULL_TREE;
17782 parser->qualifying_scope = NULL_TREE;
17783 parser->object_scope = NULL_TREE;
17784 /* If it's a `,', then there are more declarators. */
17785 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17786 cp_lexer_consume_token (parser->lexer);
17787 /* If the next token isn't a `;', then we have a parse error. */
17788 else if (cp_lexer_next_token_is_not (parser->lexer,
17791 cp_parser_error (parser, "expected %<;%>");
17792 /* Skip tokens until we find a `;'. */
17793 cp_parser_skip_to_end_of_statement (parser);
17800 /* Add DECL to the list of members. */
17802 finish_member_declaration (decl);
17804 if (TREE_CODE (decl) == FUNCTION_DECL)
17805 cp_parser_save_default_args (parser, decl);
17810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17813 /* Parse a pure-specifier.
17818 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17819 Otherwise, ERROR_MARK_NODE is returned. */
17822 cp_parser_pure_specifier (cp_parser* parser)
17826 /* Look for the `=' token. */
17827 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17828 return error_mark_node;
17829 /* Look for the `0' token. */
17830 token = cp_lexer_peek_token (parser->lexer);
17832 if (token->type == CPP_EOF
17833 || token->type == CPP_PRAGMA_EOL)
17834 return error_mark_node;
17836 cp_lexer_consume_token (parser->lexer);
17838 /* Accept = default or = delete in c++0x mode. */
17839 if (token->keyword == RID_DEFAULT
17840 || token->keyword == RID_DELETE)
17842 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17843 return token->u.value;
17846 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17847 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17849 cp_parser_error (parser,
17850 "invalid pure specifier (only %<= 0%> is allowed)");
17851 cp_parser_skip_to_end_of_statement (parser);
17852 return error_mark_node;
17854 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17856 error_at (token->location, "templates may not be %<virtual%>");
17857 return error_mark_node;
17860 return integer_zero_node;
17863 /* Parse a constant-initializer.
17865 constant-initializer:
17866 = constant-expression
17868 Returns a representation of the constant-expression. */
17871 cp_parser_constant_initializer (cp_parser* parser)
17873 /* Look for the `=' token. */
17874 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17875 return error_mark_node;
17877 /* It is invalid to write:
17879 struct S { static const int i = { 7 }; };
17882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17884 cp_parser_error (parser,
17885 "a brace-enclosed initializer is not allowed here");
17886 /* Consume the opening brace. */
17887 cp_lexer_consume_token (parser->lexer);
17888 /* Skip the initializer. */
17889 cp_parser_skip_to_closing_brace (parser);
17890 /* Look for the trailing `}'. */
17891 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17893 return error_mark_node;
17896 return cp_parser_constant_expression (parser,
17897 /*allow_non_constant=*/false,
17901 /* Derived classes [gram.class.derived] */
17903 /* Parse a base-clause.
17906 : base-specifier-list
17908 base-specifier-list:
17909 base-specifier ... [opt]
17910 base-specifier-list , base-specifier ... [opt]
17912 Returns a TREE_LIST representing the base-classes, in the order in
17913 which they were declared. The representation of each node is as
17914 described by cp_parser_base_specifier.
17916 In the case that no bases are specified, this function will return
17917 NULL_TREE, not ERROR_MARK_NODE. */
17920 cp_parser_base_clause (cp_parser* parser)
17922 tree bases = NULL_TREE;
17924 /* Look for the `:' that begins the list. */
17925 cp_parser_require (parser, CPP_COLON, RT_COLON);
17927 /* Scan the base-specifier-list. */
17932 bool pack_expansion_p = false;
17934 /* Look for the base-specifier. */
17935 base = cp_parser_base_specifier (parser);
17936 /* Look for the (optional) ellipsis. */
17937 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17939 /* Consume the `...'. */
17940 cp_lexer_consume_token (parser->lexer);
17942 pack_expansion_p = true;
17945 /* Add BASE to the front of the list. */
17946 if (base != error_mark_node)
17948 if (pack_expansion_p)
17949 /* Make this a pack expansion type. */
17950 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17953 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17955 TREE_CHAIN (base) = bases;
17959 /* Peek at the next token. */
17960 token = cp_lexer_peek_token (parser->lexer);
17961 /* If it's not a comma, then the list is complete. */
17962 if (token->type != CPP_COMMA)
17964 /* Consume the `,'. */
17965 cp_lexer_consume_token (parser->lexer);
17968 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17969 base class had a qualified name. However, the next name that
17970 appears is certainly not qualified. */
17971 parser->scope = NULL_TREE;
17972 parser->qualifying_scope = NULL_TREE;
17973 parser->object_scope = NULL_TREE;
17975 return nreverse (bases);
17978 /* Parse a base-specifier.
17981 :: [opt] nested-name-specifier [opt] class-name
17982 virtual access-specifier [opt] :: [opt] nested-name-specifier
17984 access-specifier virtual [opt] :: [opt] nested-name-specifier
17987 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17988 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17989 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17990 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17993 cp_parser_base_specifier (cp_parser* parser)
17997 bool virtual_p = false;
17998 bool duplicate_virtual_error_issued_p = false;
17999 bool duplicate_access_error_issued_p = false;
18000 bool class_scope_p, template_p;
18001 tree access = access_default_node;
18004 /* Process the optional `virtual' and `access-specifier'. */
18007 /* Peek at the next token. */
18008 token = cp_lexer_peek_token (parser->lexer);
18009 /* Process `virtual'. */
18010 switch (token->keyword)
18013 /* If `virtual' appears more than once, issue an error. */
18014 if (virtual_p && !duplicate_virtual_error_issued_p)
18016 cp_parser_error (parser,
18017 "%<virtual%> specified more than once in base-specified");
18018 duplicate_virtual_error_issued_p = true;
18023 /* Consume the `virtual' token. */
18024 cp_lexer_consume_token (parser->lexer);
18029 case RID_PROTECTED:
18031 /* If more than one access specifier appears, issue an
18033 if (access != access_default_node
18034 && !duplicate_access_error_issued_p)
18036 cp_parser_error (parser,
18037 "more than one access specifier in base-specified");
18038 duplicate_access_error_issued_p = true;
18041 access = ridpointers[(int) token->keyword];
18043 /* Consume the access-specifier. */
18044 cp_lexer_consume_token (parser->lexer);
18053 /* It is not uncommon to see programs mechanically, erroneously, use
18054 the 'typename' keyword to denote (dependent) qualified types
18055 as base classes. */
18056 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18058 token = cp_lexer_peek_token (parser->lexer);
18059 if (!processing_template_decl)
18060 error_at (token->location,
18061 "keyword %<typename%> not allowed outside of templates");
18063 error_at (token->location,
18064 "keyword %<typename%> not allowed in this context "
18065 "(the base class is implicitly a type)");
18066 cp_lexer_consume_token (parser->lexer);
18069 /* Look for the optional `::' operator. */
18070 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18071 /* Look for the nested-name-specifier. The simplest way to
18076 The keyword `typename' is not permitted in a base-specifier or
18077 mem-initializer; in these contexts a qualified name that
18078 depends on a template-parameter is implicitly assumed to be a
18081 is to pretend that we have seen the `typename' keyword at this
18083 cp_parser_nested_name_specifier_opt (parser,
18084 /*typename_keyword_p=*/true,
18085 /*check_dependency_p=*/true,
18087 /*is_declaration=*/true);
18088 /* If the base class is given by a qualified name, assume that names
18089 we see are type names or templates, as appropriate. */
18090 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18091 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18093 /* Finally, look for the class-name. */
18094 type = cp_parser_class_name (parser,
18098 /*check_dependency_p=*/true,
18099 /*class_head_p=*/false,
18100 /*is_declaration=*/true);
18102 if (type == error_mark_node)
18103 return error_mark_node;
18105 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18108 /* Exception handling [gram.exception] */
18110 /* Parse an (optional) exception-specification.
18112 exception-specification:
18113 throw ( type-id-list [opt] )
18115 Returns a TREE_LIST representing the exception-specification. The
18116 TREE_VALUE of each node is a type. */
18119 cp_parser_exception_specification_opt (cp_parser* parser)
18123 const char *saved_message;
18125 /* Peek at the next token. */
18126 token = cp_lexer_peek_token (parser->lexer);
18128 /* Is it a noexcept-specification? */
18129 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18132 cp_lexer_consume_token (parser->lexer);
18134 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18136 cp_lexer_consume_token (parser->lexer);
18138 /* Types may not be defined in an exception-specification. */
18139 saved_message = parser->type_definition_forbidden_message;
18140 parser->type_definition_forbidden_message
18141 = G_("types may not be defined in an exception-specification");
18143 expr = cp_parser_constant_expression (parser, false, NULL);
18145 /* Restore the saved message. */
18146 parser->type_definition_forbidden_message = saved_message;
18148 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18151 expr = boolean_true_node;
18153 return build_noexcept_spec (expr, tf_warning_or_error);
18156 /* If it's not `throw', then there's no exception-specification. */
18157 if (!cp_parser_is_keyword (token, RID_THROW))
18161 /* Enable this once a lot of code has transitioned to noexcept? */
18162 if (cxx_dialect == cxx0x && !in_system_header)
18163 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18164 "deprecated in C++0x; use %<noexcept%> instead.");
18167 /* Consume the `throw'. */
18168 cp_lexer_consume_token (parser->lexer);
18170 /* Look for the `('. */
18171 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18173 /* Peek at the next token. */
18174 token = cp_lexer_peek_token (parser->lexer);
18175 /* If it's not a `)', then there is a type-id-list. */
18176 if (token->type != CPP_CLOSE_PAREN)
18178 /* Types may not be defined in an exception-specification. */
18179 saved_message = parser->type_definition_forbidden_message;
18180 parser->type_definition_forbidden_message
18181 = G_("types may not be defined in an exception-specification");
18182 /* Parse the type-id-list. */
18183 type_id_list = cp_parser_type_id_list (parser);
18184 /* Restore the saved message. */
18185 parser->type_definition_forbidden_message = saved_message;
18188 type_id_list = empty_except_spec;
18190 /* Look for the `)'. */
18191 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18193 return type_id_list;
18196 /* Parse an (optional) type-id-list.
18200 type-id-list , type-id ... [opt]
18202 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18203 in the order that the types were presented. */
18206 cp_parser_type_id_list (cp_parser* parser)
18208 tree types = NULL_TREE;
18215 /* Get the next type-id. */
18216 type = cp_parser_type_id (parser);
18217 /* Parse the optional ellipsis. */
18218 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18220 /* Consume the `...'. */
18221 cp_lexer_consume_token (parser->lexer);
18223 /* Turn the type into a pack expansion expression. */
18224 type = make_pack_expansion (type);
18226 /* Add it to the list. */
18227 types = add_exception_specifier (types, type, /*complain=*/1);
18228 /* Peek at the next token. */
18229 token = cp_lexer_peek_token (parser->lexer);
18230 /* If it is not a `,', we are done. */
18231 if (token->type != CPP_COMMA)
18233 /* Consume the `,'. */
18234 cp_lexer_consume_token (parser->lexer);
18237 return nreverse (types);
18240 /* Parse a try-block.
18243 try compound-statement handler-seq */
18246 cp_parser_try_block (cp_parser* parser)
18250 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18251 try_block = begin_try_block ();
18252 cp_parser_compound_statement (parser, NULL, true);
18253 finish_try_block (try_block);
18254 cp_parser_handler_seq (parser);
18255 finish_handler_sequence (try_block);
18260 /* Parse a function-try-block.
18262 function-try-block:
18263 try ctor-initializer [opt] function-body handler-seq */
18266 cp_parser_function_try_block (cp_parser* parser)
18268 tree compound_stmt;
18270 bool ctor_initializer_p;
18272 /* Look for the `try' keyword. */
18273 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18275 /* Let the rest of the front end know where we are. */
18276 try_block = begin_function_try_block (&compound_stmt);
18277 /* Parse the function-body. */
18279 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18280 /* We're done with the `try' part. */
18281 finish_function_try_block (try_block);
18282 /* Parse the handlers. */
18283 cp_parser_handler_seq (parser);
18284 /* We're done with the handlers. */
18285 finish_function_handler_sequence (try_block, compound_stmt);
18287 return ctor_initializer_p;
18290 /* Parse a handler-seq.
18293 handler handler-seq [opt] */
18296 cp_parser_handler_seq (cp_parser* parser)
18302 /* Parse the handler. */
18303 cp_parser_handler (parser);
18304 /* Peek at the next token. */
18305 token = cp_lexer_peek_token (parser->lexer);
18306 /* If it's not `catch' then there are no more handlers. */
18307 if (!cp_parser_is_keyword (token, RID_CATCH))
18312 /* Parse a handler.
18315 catch ( exception-declaration ) compound-statement */
18318 cp_parser_handler (cp_parser* parser)
18323 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18324 handler = begin_handler ();
18325 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18326 declaration = cp_parser_exception_declaration (parser);
18327 finish_handler_parms (declaration, handler);
18328 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18329 cp_parser_compound_statement (parser, NULL, false);
18330 finish_handler (handler);
18333 /* Parse an exception-declaration.
18335 exception-declaration:
18336 type-specifier-seq declarator
18337 type-specifier-seq abstract-declarator
18341 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18342 ellipsis variant is used. */
18345 cp_parser_exception_declaration (cp_parser* parser)
18347 cp_decl_specifier_seq type_specifiers;
18348 cp_declarator *declarator;
18349 const char *saved_message;
18351 /* If it's an ellipsis, it's easy to handle. */
18352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18354 /* Consume the `...' token. */
18355 cp_lexer_consume_token (parser->lexer);
18359 /* Types may not be defined in exception-declarations. */
18360 saved_message = parser->type_definition_forbidden_message;
18361 parser->type_definition_forbidden_message
18362 = G_("types may not be defined in exception-declarations");
18364 /* Parse the type-specifier-seq. */
18365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18366 /*is_trailing_return=*/false,
18368 /* If it's a `)', then there is no declarator. */
18369 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18372 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18373 /*ctor_dtor_or_conv_p=*/NULL,
18374 /*parenthesized_p=*/NULL,
18375 /*member_p=*/false);
18377 /* Restore the saved message. */
18378 parser->type_definition_forbidden_message = saved_message;
18380 if (!type_specifiers.any_specifiers_p)
18381 return error_mark_node;
18383 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18386 /* Parse a throw-expression.
18389 throw assignment-expression [opt]
18391 Returns a THROW_EXPR representing the throw-expression. */
18394 cp_parser_throw_expression (cp_parser* parser)
18399 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18400 token = cp_lexer_peek_token (parser->lexer);
18401 /* Figure out whether or not there is an assignment-expression
18402 following the "throw" keyword. */
18403 if (token->type == CPP_COMMA
18404 || token->type == CPP_SEMICOLON
18405 || token->type == CPP_CLOSE_PAREN
18406 || token->type == CPP_CLOSE_SQUARE
18407 || token->type == CPP_CLOSE_BRACE
18408 || token->type == CPP_COLON)
18409 expression = NULL_TREE;
18411 expression = cp_parser_assignment_expression (parser,
18412 /*cast_p=*/false, NULL);
18414 return build_throw (expression);
18417 /* GNU Extensions */
18419 /* Parse an (optional) asm-specification.
18422 asm ( string-literal )
18424 If the asm-specification is present, returns a STRING_CST
18425 corresponding to the string-literal. Otherwise, returns
18429 cp_parser_asm_specification_opt (cp_parser* parser)
18432 tree asm_specification;
18434 /* Peek at the next token. */
18435 token = cp_lexer_peek_token (parser->lexer);
18436 /* If the next token isn't the `asm' keyword, then there's no
18437 asm-specification. */
18438 if (!cp_parser_is_keyword (token, RID_ASM))
18441 /* Consume the `asm' token. */
18442 cp_lexer_consume_token (parser->lexer);
18443 /* Look for the `('. */
18444 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18446 /* Look for the string-literal. */
18447 asm_specification = cp_parser_string_literal (parser, false, false);
18449 /* Look for the `)'. */
18450 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18452 return asm_specification;
18455 /* Parse an asm-operand-list.
18459 asm-operand-list , asm-operand
18462 string-literal ( expression )
18463 [ string-literal ] string-literal ( expression )
18465 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18466 each node is the expression. The TREE_PURPOSE is itself a
18467 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18468 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18469 is a STRING_CST for the string literal before the parenthesis. Returns
18470 ERROR_MARK_NODE if any of the operands are invalid. */
18473 cp_parser_asm_operand_list (cp_parser* parser)
18475 tree asm_operands = NULL_TREE;
18476 bool invalid_operands = false;
18480 tree string_literal;
18484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18486 /* Consume the `[' token. */
18487 cp_lexer_consume_token (parser->lexer);
18488 /* Read the operand name. */
18489 name = cp_parser_identifier (parser);
18490 if (name != error_mark_node)
18491 name = build_string (IDENTIFIER_LENGTH (name),
18492 IDENTIFIER_POINTER (name));
18493 /* Look for the closing `]'. */
18494 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18498 /* Look for the string-literal. */
18499 string_literal = cp_parser_string_literal (parser, false, false);
18501 /* Look for the `('. */
18502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18503 /* Parse the expression. */
18504 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18505 /* Look for the `)'. */
18506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18508 if (name == error_mark_node
18509 || string_literal == error_mark_node
18510 || expression == error_mark_node)
18511 invalid_operands = true;
18513 /* Add this operand to the list. */
18514 asm_operands = tree_cons (build_tree_list (name, string_literal),
18517 /* If the next token is not a `,', there are no more
18519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18521 /* Consume the `,'. */
18522 cp_lexer_consume_token (parser->lexer);
18525 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18528 /* Parse an asm-clobber-list.
18532 asm-clobber-list , string-literal
18534 Returns a TREE_LIST, indicating the clobbers in the order that they
18535 appeared. The TREE_VALUE of each node is a STRING_CST. */
18538 cp_parser_asm_clobber_list (cp_parser* parser)
18540 tree clobbers = NULL_TREE;
18544 tree string_literal;
18546 /* Look for the string literal. */
18547 string_literal = cp_parser_string_literal (parser, false, false);
18548 /* Add it to the list. */
18549 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18550 /* If the next token is not a `,', then the list is
18552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18554 /* Consume the `,' token. */
18555 cp_lexer_consume_token (parser->lexer);
18561 /* Parse an asm-label-list.
18565 asm-label-list , identifier
18567 Returns a TREE_LIST, indicating the labels in the order that they
18568 appeared. The TREE_VALUE of each node is a label. */
18571 cp_parser_asm_label_list (cp_parser* parser)
18573 tree labels = NULL_TREE;
18577 tree identifier, label, name;
18579 /* Look for the identifier. */
18580 identifier = cp_parser_identifier (parser);
18581 if (!error_operand_p (identifier))
18583 label = lookup_label (identifier);
18584 if (TREE_CODE (label) == LABEL_DECL)
18586 TREE_USED (label) = 1;
18587 check_goto (label);
18588 name = build_string (IDENTIFIER_LENGTH (identifier),
18589 IDENTIFIER_POINTER (identifier));
18590 labels = tree_cons (name, label, labels);
18593 /* If the next token is not a `,', then the list is
18595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18597 /* Consume the `,' token. */
18598 cp_lexer_consume_token (parser->lexer);
18601 return nreverse (labels);
18604 /* Parse an (optional) series of attributes.
18607 attributes attribute
18610 __attribute__ (( attribute-list [opt] ))
18612 The return value is as for cp_parser_attribute_list. */
18615 cp_parser_attributes_opt (cp_parser* parser)
18617 tree attributes = NULL_TREE;
18622 tree attribute_list;
18624 /* Peek at the next token. */
18625 token = cp_lexer_peek_token (parser->lexer);
18626 /* If it's not `__attribute__', then we're done. */
18627 if (token->keyword != RID_ATTRIBUTE)
18630 /* Consume the `__attribute__' keyword. */
18631 cp_lexer_consume_token (parser->lexer);
18632 /* Look for the two `(' tokens. */
18633 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18634 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18636 /* Peek at the next token. */
18637 token = cp_lexer_peek_token (parser->lexer);
18638 if (token->type != CPP_CLOSE_PAREN)
18639 /* Parse the attribute-list. */
18640 attribute_list = cp_parser_attribute_list (parser);
18642 /* If the next token is a `)', then there is no attribute
18644 attribute_list = NULL;
18646 /* Look for the two `)' tokens. */
18647 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18648 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18650 /* Add these new attributes to the list. */
18651 attributes = chainon (attributes, attribute_list);
18657 /* Parse an attribute-list.
18661 attribute-list , attribute
18665 identifier ( identifier )
18666 identifier ( identifier , expression-list )
18667 identifier ( expression-list )
18669 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18670 to an attribute. The TREE_PURPOSE of each node is the identifier
18671 indicating which attribute is in use. The TREE_VALUE represents
18672 the arguments, if any. */
18675 cp_parser_attribute_list (cp_parser* parser)
18677 tree attribute_list = NULL_TREE;
18678 bool save_translate_strings_p = parser->translate_strings_p;
18680 parser->translate_strings_p = false;
18687 /* Look for the identifier. We also allow keywords here; for
18688 example `__attribute__ ((const))' is legal. */
18689 token = cp_lexer_peek_token (parser->lexer);
18690 if (token->type == CPP_NAME
18691 || token->type == CPP_KEYWORD)
18693 tree arguments = NULL_TREE;
18695 /* Consume the token. */
18696 token = cp_lexer_consume_token (parser->lexer);
18698 /* Save away the identifier that indicates which attribute
18700 identifier = (token->type == CPP_KEYWORD)
18701 /* For keywords, use the canonical spelling, not the
18702 parsed identifier. */
18703 ? ridpointers[(int) token->keyword]
18706 attribute = build_tree_list (identifier, NULL_TREE);
18708 /* Peek at the next token. */
18709 token = cp_lexer_peek_token (parser->lexer);
18710 /* If it's an `(', then parse the attribute arguments. */
18711 if (token->type == CPP_OPEN_PAREN)
18714 int attr_flag = (attribute_takes_identifier_p (identifier)
18715 ? id_attr : normal_attr);
18716 vec = cp_parser_parenthesized_expression_list
18717 (parser, attr_flag, /*cast_p=*/false,
18718 /*allow_expansion_p=*/false,
18719 /*non_constant_p=*/NULL);
18721 arguments = error_mark_node;
18724 arguments = build_tree_list_vec (vec);
18725 release_tree_vector (vec);
18727 /* Save the arguments away. */
18728 TREE_VALUE (attribute) = arguments;
18731 if (arguments != error_mark_node)
18733 /* Add this attribute to the list. */
18734 TREE_CHAIN (attribute) = attribute_list;
18735 attribute_list = attribute;
18738 token = cp_lexer_peek_token (parser->lexer);
18740 /* Now, look for more attributes. If the next token isn't a
18741 `,', we're done. */
18742 if (token->type != CPP_COMMA)
18745 /* Consume the comma and keep going. */
18746 cp_lexer_consume_token (parser->lexer);
18748 parser->translate_strings_p = save_translate_strings_p;
18750 /* We built up the list in reverse order. */
18751 return nreverse (attribute_list);
18754 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18755 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18756 current value of the PEDANTIC flag, regardless of whether or not
18757 the `__extension__' keyword is present. The caller is responsible
18758 for restoring the value of the PEDANTIC flag. */
18761 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18763 /* Save the old value of the PEDANTIC flag. */
18764 *saved_pedantic = pedantic;
18766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18768 /* Consume the `__extension__' token. */
18769 cp_lexer_consume_token (parser->lexer);
18770 /* We're not being pedantic while the `__extension__' keyword is
18780 /* Parse a label declaration.
18783 __label__ label-declarator-seq ;
18785 label-declarator-seq:
18786 identifier , label-declarator-seq
18790 cp_parser_label_declaration (cp_parser* parser)
18792 /* Look for the `__label__' keyword. */
18793 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18799 /* Look for an identifier. */
18800 identifier = cp_parser_identifier (parser);
18801 /* If we failed, stop. */
18802 if (identifier == error_mark_node)
18804 /* Declare it as a label. */
18805 finish_label_decl (identifier);
18806 /* If the next token is a `;', stop. */
18807 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18809 /* Look for the `,' separating the label declarations. */
18810 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18813 /* Look for the final `;'. */
18814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18817 /* Support Functions */
18819 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18820 NAME should have one of the representations used for an
18821 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18822 is returned. If PARSER->SCOPE is a dependent type, then a
18823 SCOPE_REF is returned.
18825 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18826 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18827 was formed. Abstractly, such entities should not be passed to this
18828 function, because they do not need to be looked up, but it is
18829 simpler to check for this special case here, rather than at the
18832 In cases not explicitly covered above, this function returns a
18833 DECL, OVERLOAD, or baselink representing the result of the lookup.
18834 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18837 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18838 (e.g., "struct") that was used. In that case bindings that do not
18839 refer to types are ignored.
18841 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18844 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18847 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18850 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18851 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18852 NULL_TREE otherwise. */
18855 cp_parser_lookup_name (cp_parser *parser, tree name,
18856 enum tag_types tag_type,
18859 bool check_dependency,
18860 tree *ambiguous_decls,
18861 location_t name_location)
18865 tree object_type = parser->context->object_type;
18867 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18868 flags |= LOOKUP_COMPLAIN;
18870 /* Assume that the lookup will be unambiguous. */
18871 if (ambiguous_decls)
18872 *ambiguous_decls = NULL_TREE;
18874 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18875 no longer valid. Note that if we are parsing tentatively, and
18876 the parse fails, OBJECT_TYPE will be automatically restored. */
18877 parser->context->object_type = NULL_TREE;
18879 if (name == error_mark_node)
18880 return error_mark_node;
18882 /* A template-id has already been resolved; there is no lookup to
18884 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18886 if (BASELINK_P (name))
18888 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18889 == TEMPLATE_ID_EXPR);
18893 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18894 it should already have been checked to make sure that the name
18895 used matches the type being destroyed. */
18896 if (TREE_CODE (name) == BIT_NOT_EXPR)
18900 /* Figure out to which type this destructor applies. */
18902 type = parser->scope;
18903 else if (object_type)
18904 type = object_type;
18906 type = current_class_type;
18907 /* If that's not a class type, there is no destructor. */
18908 if (!type || !CLASS_TYPE_P (type))
18909 return error_mark_node;
18910 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18911 lazily_declare_fn (sfk_destructor, type);
18912 if (!CLASSTYPE_DESTRUCTORS (type))
18913 return error_mark_node;
18914 /* If it was a class type, return the destructor. */
18915 return CLASSTYPE_DESTRUCTORS (type);
18918 /* By this point, the NAME should be an ordinary identifier. If
18919 the id-expression was a qualified name, the qualifying scope is
18920 stored in PARSER->SCOPE at this point. */
18921 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18923 /* Perform the lookup. */
18928 if (parser->scope == error_mark_node)
18929 return error_mark_node;
18931 /* If the SCOPE is dependent, the lookup must be deferred until
18932 the template is instantiated -- unless we are explicitly
18933 looking up names in uninstantiated templates. Even then, we
18934 cannot look up the name if the scope is not a class type; it
18935 might, for example, be a template type parameter. */
18936 dependent_p = (TYPE_P (parser->scope)
18937 && dependent_scope_p (parser->scope));
18938 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18940 /* Defer lookup. */
18941 decl = error_mark_node;
18944 tree pushed_scope = NULL_TREE;
18946 /* If PARSER->SCOPE is a dependent type, then it must be a
18947 class type, and we must not be checking dependencies;
18948 otherwise, we would have processed this lookup above. So
18949 that PARSER->SCOPE is not considered a dependent base by
18950 lookup_member, we must enter the scope here. */
18952 pushed_scope = push_scope (parser->scope);
18954 /* If the PARSER->SCOPE is a template specialization, it
18955 may be instantiated during name lookup. In that case,
18956 errors may be issued. Even if we rollback the current
18957 tentative parse, those errors are valid. */
18958 decl = lookup_qualified_name (parser->scope, name,
18959 tag_type != none_type,
18960 /*complain=*/true);
18962 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18963 lookup result and the nested-name-specifier nominates a class C:
18964 * if the name specified after the nested-name-specifier, when
18965 looked up in C, is the injected-class-name of C (Clause 9), or
18966 * if the name specified after the nested-name-specifier is the
18967 same as the identifier or the simple-template-id's template-
18968 name in the last component of the nested-name-specifier,
18969 the name is instead considered to name the constructor of
18970 class C. [ Note: for example, the constructor is not an
18971 acceptable lookup result in an elaborated-type-specifier so
18972 the constructor would not be used in place of the
18973 injected-class-name. --end note ] Such a constructor name
18974 shall be used only in the declarator-id of a declaration that
18975 names a constructor or in a using-declaration. */
18976 if (tag_type == none_type
18977 && DECL_SELF_REFERENCE_P (decl)
18978 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18979 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18980 tag_type != none_type,
18981 /*complain=*/true);
18983 /* If we have a single function from a using decl, pull it out. */
18984 if (TREE_CODE (decl) == OVERLOAD
18985 && !really_overloaded_fn (decl))
18986 decl = OVL_FUNCTION (decl);
18989 pop_scope (pushed_scope);
18992 /* If the scope is a dependent type and either we deferred lookup or
18993 we did lookup but didn't find the name, rememeber the name. */
18994 if (decl == error_mark_node && TYPE_P (parser->scope)
18995 && dependent_type_p (parser->scope))
19001 /* The resolution to Core Issue 180 says that `struct
19002 A::B' should be considered a type-name, even if `A'
19004 type = make_typename_type (parser->scope, name, tag_type,
19005 /*complain=*/tf_error);
19006 decl = TYPE_NAME (type);
19008 else if (is_template
19009 && (cp_parser_next_token_ends_template_argument_p (parser)
19010 || cp_lexer_next_token_is (parser->lexer,
19012 decl = make_unbound_class_template (parser->scope,
19014 /*complain=*/tf_error);
19016 decl = build_qualified_name (/*type=*/NULL_TREE,
19017 parser->scope, name,
19020 parser->qualifying_scope = parser->scope;
19021 parser->object_scope = NULL_TREE;
19023 else if (object_type)
19025 tree object_decl = NULL_TREE;
19026 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19027 OBJECT_TYPE is not a class. */
19028 if (CLASS_TYPE_P (object_type))
19029 /* If the OBJECT_TYPE is a template specialization, it may
19030 be instantiated during name lookup. In that case, errors
19031 may be issued. Even if we rollback the current tentative
19032 parse, those errors are valid. */
19033 object_decl = lookup_member (object_type,
19036 tag_type != none_type);
19037 /* Look it up in the enclosing context, too. */
19038 decl = lookup_name_real (name, tag_type != none_type,
19040 /*block_p=*/true, is_namespace, flags);
19041 parser->object_scope = object_type;
19042 parser->qualifying_scope = NULL_TREE;
19044 decl = object_decl;
19048 decl = lookup_name_real (name, tag_type != none_type,
19050 /*block_p=*/true, is_namespace, flags);
19051 parser->qualifying_scope = NULL_TREE;
19052 parser->object_scope = NULL_TREE;
19055 /* If the lookup failed, let our caller know. */
19056 if (!decl || decl == error_mark_node)
19057 return error_mark_node;
19059 /* Pull out the template from an injected-class-name (or multiple). */
19061 decl = maybe_get_template_decl_from_type_decl (decl);
19063 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19064 if (TREE_CODE (decl) == TREE_LIST)
19066 if (ambiguous_decls)
19067 *ambiguous_decls = decl;
19068 /* The error message we have to print is too complicated for
19069 cp_parser_error, so we incorporate its actions directly. */
19070 if (!cp_parser_simulate_error (parser))
19072 error_at (name_location, "reference to %qD is ambiguous",
19074 print_candidates (decl);
19076 return error_mark_node;
19079 gcc_assert (DECL_P (decl)
19080 || TREE_CODE (decl) == OVERLOAD
19081 || TREE_CODE (decl) == SCOPE_REF
19082 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19083 || BASELINK_P (decl));
19085 /* If we have resolved the name of a member declaration, check to
19086 see if the declaration is accessible. When the name resolves to
19087 set of overloaded functions, accessibility is checked when
19088 overload resolution is done.
19090 During an explicit instantiation, access is not checked at all,
19091 as per [temp.explicit]. */
19093 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19098 /* Like cp_parser_lookup_name, but for use in the typical case where
19099 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19100 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19103 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19105 return cp_parser_lookup_name (parser, name,
19107 /*is_template=*/false,
19108 /*is_namespace=*/false,
19109 /*check_dependency=*/true,
19110 /*ambiguous_decls=*/NULL,
19114 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19115 the current context, return the TYPE_DECL. If TAG_NAME_P is
19116 true, the DECL indicates the class being defined in a class-head,
19117 or declared in an elaborated-type-specifier.
19119 Otherwise, return DECL. */
19122 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19124 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19125 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19128 template <typename T> struct B;
19131 template <typename T> struct A::B {};
19133 Similarly, in an elaborated-type-specifier:
19135 namespace N { struct X{}; }
19138 template <typename T> friend struct N::X;
19141 However, if the DECL refers to a class type, and we are in
19142 the scope of the class, then the name lookup automatically
19143 finds the TYPE_DECL created by build_self_reference rather
19144 than a TEMPLATE_DECL. For example, in:
19146 template <class T> struct S {
19150 there is no need to handle such case. */
19152 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19153 return DECL_TEMPLATE_RESULT (decl);
19158 /* If too many, or too few, template-parameter lists apply to the
19159 declarator, issue an error message. Returns TRUE if all went well,
19160 and FALSE otherwise. */
19163 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19164 cp_declarator *declarator,
19165 location_t declarator_location)
19167 unsigned num_templates;
19169 /* We haven't seen any classes that involve template parameters yet. */
19172 switch (declarator->kind)
19175 if (declarator->u.id.qualifying_scope)
19179 scope = declarator->u.id.qualifying_scope;
19181 while (scope && CLASS_TYPE_P (scope))
19183 /* You're supposed to have one `template <...>'
19184 for every template class, but you don't need one
19185 for a full specialization. For example:
19187 template <class T> struct S{};
19188 template <> struct S<int> { void f(); };
19189 void S<int>::f () {}
19191 is correct; there shouldn't be a `template <>' for
19192 the definition of `S<int>::f'. */
19193 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19194 /* If SCOPE does not have template information of any
19195 kind, then it is not a template, nor is it nested
19196 within a template. */
19198 if (explicit_class_specialization_p (scope))
19200 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19203 scope = TYPE_CONTEXT (scope);
19206 else if (TREE_CODE (declarator->u.id.unqualified_name)
19207 == TEMPLATE_ID_EXPR)
19208 /* If the DECLARATOR has the form `X<y>' then it uses one
19209 additional level of template parameters. */
19212 return cp_parser_check_template_parameters
19213 (parser, num_templates, declarator_location, declarator);
19219 case cdk_reference:
19221 return (cp_parser_check_declarator_template_parameters
19222 (parser, declarator->declarator, declarator_location));
19228 gcc_unreachable ();
19233 /* NUM_TEMPLATES were used in the current declaration. If that is
19234 invalid, return FALSE and issue an error messages. Otherwise,
19235 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19236 declarator and we can print more accurate diagnostics. */
19239 cp_parser_check_template_parameters (cp_parser* parser,
19240 unsigned num_templates,
19241 location_t location,
19242 cp_declarator *declarator)
19244 /* If there are the same number of template classes and parameter
19245 lists, that's OK. */
19246 if (parser->num_template_parameter_lists == num_templates)
19248 /* If there are more, but only one more, then we are referring to a
19249 member template. That's OK too. */
19250 if (parser->num_template_parameter_lists == num_templates + 1)
19252 /* If there are more template classes than parameter lists, we have
19255 template <class T> void S<T>::R<T>::f (); */
19256 if (parser->num_template_parameter_lists < num_templates)
19258 if (declarator && !current_function_decl)
19259 error_at (location, "specializing member %<%T::%E%> "
19260 "requires %<template<>%> syntax",
19261 declarator->u.id.qualifying_scope,
19262 declarator->u.id.unqualified_name);
19263 else if (declarator)
19264 error_at (location, "invalid declaration of %<%T::%E%>",
19265 declarator->u.id.qualifying_scope,
19266 declarator->u.id.unqualified_name);
19268 error_at (location, "too few template-parameter-lists");
19271 /* Otherwise, there are too many template parameter lists. We have
19274 template <class T> template <class U> void S::f(); */
19275 error_at (location, "too many template-parameter-lists");
19279 /* Parse an optional `::' token indicating that the following name is
19280 from the global namespace. If so, PARSER->SCOPE is set to the
19281 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19282 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19283 Returns the new value of PARSER->SCOPE, if the `::' token is
19284 present, and NULL_TREE otherwise. */
19287 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19291 /* Peek at the next token. */
19292 token = cp_lexer_peek_token (parser->lexer);
19293 /* If we're looking at a `::' token then we're starting from the
19294 global namespace, not our current location. */
19295 if (token->type == CPP_SCOPE)
19297 /* Consume the `::' token. */
19298 cp_lexer_consume_token (parser->lexer);
19299 /* Set the SCOPE so that we know where to start the lookup. */
19300 parser->scope = global_namespace;
19301 parser->qualifying_scope = global_namespace;
19302 parser->object_scope = NULL_TREE;
19304 return parser->scope;
19306 else if (!current_scope_valid_p)
19308 parser->scope = NULL_TREE;
19309 parser->qualifying_scope = NULL_TREE;
19310 parser->object_scope = NULL_TREE;
19316 /* Returns TRUE if the upcoming token sequence is the start of a
19317 constructor declarator. If FRIEND_P is true, the declarator is
19318 preceded by the `friend' specifier. */
19321 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19323 bool constructor_p;
19324 tree nested_name_specifier;
19325 cp_token *next_token;
19327 /* The common case is that this is not a constructor declarator, so
19328 try to avoid doing lots of work if at all possible. It's not
19329 valid declare a constructor at function scope. */
19330 if (parser->in_function_body)
19332 /* And only certain tokens can begin a constructor declarator. */
19333 next_token = cp_lexer_peek_token (parser->lexer);
19334 if (next_token->type != CPP_NAME
19335 && next_token->type != CPP_SCOPE
19336 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19337 && next_token->type != CPP_TEMPLATE_ID)
19340 /* Parse tentatively; we are going to roll back all of the tokens
19342 cp_parser_parse_tentatively (parser);
19343 /* Assume that we are looking at a constructor declarator. */
19344 constructor_p = true;
19346 /* Look for the optional `::' operator. */
19347 cp_parser_global_scope_opt (parser,
19348 /*current_scope_valid_p=*/false);
19349 /* Look for the nested-name-specifier. */
19350 nested_name_specifier
19351 = (cp_parser_nested_name_specifier_opt (parser,
19352 /*typename_keyword_p=*/false,
19353 /*check_dependency_p=*/false,
19355 /*is_declaration=*/false));
19356 /* Outside of a class-specifier, there must be a
19357 nested-name-specifier. */
19358 if (!nested_name_specifier &&
19359 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19361 constructor_p = false;
19362 else if (nested_name_specifier == error_mark_node)
19363 constructor_p = false;
19365 /* If we have a class scope, this is easy; DR 147 says that S::S always
19366 names the constructor, and no other qualified name could. */
19367 if (constructor_p && nested_name_specifier
19368 && TYPE_P (nested_name_specifier))
19370 tree id = cp_parser_unqualified_id (parser,
19371 /*template_keyword_p=*/false,
19372 /*check_dependency_p=*/false,
19373 /*declarator_p=*/true,
19374 /*optional_p=*/false);
19375 if (is_overloaded_fn (id))
19376 id = DECL_NAME (get_first_fn (id));
19377 if (!constructor_name_p (id, nested_name_specifier))
19378 constructor_p = false;
19380 /* If we still think that this might be a constructor-declarator,
19381 look for a class-name. */
19382 else if (constructor_p)
19386 template <typename T> struct S {
19390 we must recognize that the nested `S' names a class. */
19392 type_decl = cp_parser_class_name (parser,
19393 /*typename_keyword_p=*/false,
19394 /*template_keyword_p=*/false,
19396 /*check_dependency_p=*/false,
19397 /*class_head_p=*/false,
19398 /*is_declaration=*/false);
19399 /* If there was no class-name, then this is not a constructor. */
19400 constructor_p = !cp_parser_error_occurred (parser);
19402 /* If we're still considering a constructor, we have to see a `(',
19403 to begin the parameter-declaration-clause, followed by either a
19404 `)', an `...', or a decl-specifier. We need to check for a
19405 type-specifier to avoid being fooled into thinking that:
19409 is a constructor. (It is actually a function named `f' that
19410 takes one parameter (of type `int') and returns a value of type
19413 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19414 constructor_p = false;
19417 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19418 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19419 /* A parameter declaration begins with a decl-specifier,
19420 which is either the "attribute" keyword, a storage class
19421 specifier, or (usually) a type-specifier. */
19422 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19425 tree pushed_scope = NULL_TREE;
19426 unsigned saved_num_template_parameter_lists;
19428 /* Names appearing in the type-specifier should be looked up
19429 in the scope of the class. */
19430 if (current_class_type)
19434 type = TREE_TYPE (type_decl);
19435 if (TREE_CODE (type) == TYPENAME_TYPE)
19437 type = resolve_typename_type (type,
19438 /*only_current_p=*/false);
19439 if (TREE_CODE (type) == TYPENAME_TYPE)
19441 cp_parser_abort_tentative_parse (parser);
19445 pushed_scope = push_scope (type);
19448 /* Inside the constructor parameter list, surrounding
19449 template-parameter-lists do not apply. */
19450 saved_num_template_parameter_lists
19451 = parser->num_template_parameter_lists;
19452 parser->num_template_parameter_lists = 0;
19454 /* Look for the type-specifier. */
19455 cp_parser_type_specifier (parser,
19456 CP_PARSER_FLAGS_NONE,
19457 /*decl_specs=*/NULL,
19458 /*is_declarator=*/true,
19459 /*declares_class_or_enum=*/NULL,
19460 /*is_cv_qualifier=*/NULL);
19462 parser->num_template_parameter_lists
19463 = saved_num_template_parameter_lists;
19465 /* Leave the scope of the class. */
19467 pop_scope (pushed_scope);
19469 constructor_p = !cp_parser_error_occurred (parser);
19473 /* We did not really want to consume any tokens. */
19474 cp_parser_abort_tentative_parse (parser);
19476 return constructor_p;
19479 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19480 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19481 they must be performed once we are in the scope of the function.
19483 Returns the function defined. */
19486 cp_parser_function_definition_from_specifiers_and_declarator
19487 (cp_parser* parser,
19488 cp_decl_specifier_seq *decl_specifiers,
19490 const cp_declarator *declarator)
19495 /* Begin the function-definition. */
19496 success_p = start_function (decl_specifiers, declarator, attributes);
19498 /* The things we're about to see are not directly qualified by any
19499 template headers we've seen thus far. */
19500 reset_specialization ();
19502 /* If there were names looked up in the decl-specifier-seq that we
19503 did not check, check them now. We must wait until we are in the
19504 scope of the function to perform the checks, since the function
19505 might be a friend. */
19506 perform_deferred_access_checks ();
19510 /* Skip the entire function. */
19511 cp_parser_skip_to_end_of_block_or_statement (parser);
19512 fn = error_mark_node;
19514 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19516 /* Seen already, skip it. An error message has already been output. */
19517 cp_parser_skip_to_end_of_block_or_statement (parser);
19518 fn = current_function_decl;
19519 current_function_decl = NULL_TREE;
19520 /* If this is a function from a class, pop the nested class. */
19521 if (current_class_name)
19522 pop_nested_class ();
19525 fn = cp_parser_function_definition_after_declarator (parser,
19526 /*inline_p=*/false);
19531 /* Parse the part of a function-definition that follows the
19532 declarator. INLINE_P is TRUE iff this function is an inline
19533 function defined within a class-specifier.
19535 Returns the function defined. */
19538 cp_parser_function_definition_after_declarator (cp_parser* parser,
19542 bool ctor_initializer_p = false;
19543 bool saved_in_unbraced_linkage_specification_p;
19544 bool saved_in_function_body;
19545 unsigned saved_num_template_parameter_lists;
19548 saved_in_function_body = parser->in_function_body;
19549 parser->in_function_body = true;
19550 /* If the next token is `return', then the code may be trying to
19551 make use of the "named return value" extension that G++ used to
19553 token = cp_lexer_peek_token (parser->lexer);
19554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19556 /* Consume the `return' keyword. */
19557 cp_lexer_consume_token (parser->lexer);
19558 /* Look for the identifier that indicates what value is to be
19560 cp_parser_identifier (parser);
19561 /* Issue an error message. */
19562 error_at (token->location,
19563 "named return values are no longer supported");
19564 /* Skip tokens until we reach the start of the function body. */
19567 cp_token *token = cp_lexer_peek_token (parser->lexer);
19568 if (token->type == CPP_OPEN_BRACE
19569 || token->type == CPP_EOF
19570 || token->type == CPP_PRAGMA_EOL)
19572 cp_lexer_consume_token (parser->lexer);
19575 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19576 anything declared inside `f'. */
19577 saved_in_unbraced_linkage_specification_p
19578 = parser->in_unbraced_linkage_specification_p;
19579 parser->in_unbraced_linkage_specification_p = false;
19580 /* Inside the function, surrounding template-parameter-lists do not
19582 saved_num_template_parameter_lists
19583 = parser->num_template_parameter_lists;
19584 parser->num_template_parameter_lists = 0;
19586 start_lambda_scope (current_function_decl);
19588 /* If the next token is `try', then we are looking at a
19589 function-try-block. */
19590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19591 ctor_initializer_p = cp_parser_function_try_block (parser);
19592 /* A function-try-block includes the function-body, so we only do
19593 this next part if we're not processing a function-try-block. */
19596 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19598 finish_lambda_scope ();
19600 /* Finish the function. */
19601 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19602 (inline_p ? 2 : 0));
19603 /* Generate code for it, if necessary. */
19604 expand_or_defer_fn (fn);
19605 /* Restore the saved values. */
19606 parser->in_unbraced_linkage_specification_p
19607 = saved_in_unbraced_linkage_specification_p;
19608 parser->num_template_parameter_lists
19609 = saved_num_template_parameter_lists;
19610 parser->in_function_body = saved_in_function_body;
19615 /* Parse a template-declaration, assuming that the `export' (and
19616 `extern') keywords, if present, has already been scanned. MEMBER_P
19617 is as for cp_parser_template_declaration. */
19620 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19622 tree decl = NULL_TREE;
19623 VEC (deferred_access_check,gc) *checks;
19624 tree parameter_list;
19625 bool friend_p = false;
19626 bool need_lang_pop;
19629 /* Look for the `template' keyword. */
19630 token = cp_lexer_peek_token (parser->lexer);
19631 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19635 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19637 if (at_class_scope_p () && current_function_decl)
19639 /* 14.5.2.2 [temp.mem]
19641 A local class shall not have member templates. */
19642 error_at (token->location,
19643 "invalid declaration of member template in local class");
19644 cp_parser_skip_to_end_of_block_or_statement (parser);
19649 A template ... shall not have C linkage. */
19650 if (current_lang_name == lang_name_c)
19652 error_at (token->location, "template with C linkage");
19653 /* Give it C++ linkage to avoid confusing other parts of the
19655 push_lang_context (lang_name_cplusplus);
19656 need_lang_pop = true;
19659 need_lang_pop = false;
19661 /* We cannot perform access checks on the template parameter
19662 declarations until we know what is being declared, just as we
19663 cannot check the decl-specifier list. */
19664 push_deferring_access_checks (dk_deferred);
19666 /* If the next token is `>', then we have an invalid
19667 specialization. Rather than complain about an invalid template
19668 parameter, issue an error message here. */
19669 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19671 cp_parser_error (parser, "invalid explicit specialization");
19672 begin_specialization ();
19673 parameter_list = NULL_TREE;
19676 /* Parse the template parameters. */
19677 parameter_list = cp_parser_template_parameter_list (parser);
19679 /* Get the deferred access checks from the parameter list. These
19680 will be checked once we know what is being declared, as for a
19681 member template the checks must be performed in the scope of the
19682 class containing the member. */
19683 checks = get_deferred_access_checks ();
19685 /* Look for the `>'. */
19686 cp_parser_skip_to_end_of_template_parameter_list (parser);
19687 /* We just processed one more parameter list. */
19688 ++parser->num_template_parameter_lists;
19689 /* If the next token is `template', there are more template
19691 if (cp_lexer_next_token_is_keyword (parser->lexer,
19693 cp_parser_template_declaration_after_export (parser, member_p);
19696 /* There are no access checks when parsing a template, as we do not
19697 know if a specialization will be a friend. */
19698 push_deferring_access_checks (dk_no_check);
19699 token = cp_lexer_peek_token (parser->lexer);
19700 decl = cp_parser_single_declaration (parser,
19703 /*explicit_specialization_p=*/false,
19705 pop_deferring_access_checks ();
19707 /* If this is a member template declaration, let the front
19709 if (member_p && !friend_p && decl)
19711 if (TREE_CODE (decl) == TYPE_DECL)
19712 cp_parser_check_access_in_redeclaration (decl, token->location);
19714 decl = finish_member_template_decl (decl);
19716 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19717 make_friend_class (current_class_type, TREE_TYPE (decl),
19718 /*complain=*/true);
19720 /* We are done with the current parameter list. */
19721 --parser->num_template_parameter_lists;
19723 pop_deferring_access_checks ();
19726 finish_template_decl (parameter_list);
19728 /* Register member declarations. */
19729 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19730 finish_member_declaration (decl);
19731 /* For the erroneous case of a template with C linkage, we pushed an
19732 implicit C++ linkage scope; exit that scope now. */
19734 pop_lang_context ();
19735 /* If DECL is a function template, we must return to parse it later.
19736 (Even though there is no definition, there might be default
19737 arguments that need handling.) */
19738 if (member_p && decl
19739 && (TREE_CODE (decl) == FUNCTION_DECL
19740 || DECL_FUNCTION_TEMPLATE_P (decl)))
19741 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19744 /* Perform the deferred access checks from a template-parameter-list.
19745 CHECKS is a TREE_LIST of access checks, as returned by
19746 get_deferred_access_checks. */
19749 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19751 ++processing_template_parmlist;
19752 perform_access_checks (checks);
19753 --processing_template_parmlist;
19756 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19757 `function-definition' sequence. MEMBER_P is true, this declaration
19758 appears in a class scope.
19760 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19761 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19764 cp_parser_single_declaration (cp_parser* parser,
19765 VEC (deferred_access_check,gc)* checks,
19767 bool explicit_specialization_p,
19770 int declares_class_or_enum;
19771 tree decl = NULL_TREE;
19772 cp_decl_specifier_seq decl_specifiers;
19773 bool function_definition_p = false;
19774 cp_token *decl_spec_token_start;
19776 /* This function is only used when processing a template
19778 gcc_assert (innermost_scope_kind () == sk_template_parms
19779 || innermost_scope_kind () == sk_template_spec);
19781 /* Defer access checks until we know what is being declared. */
19782 push_deferring_access_checks (dk_deferred);
19784 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19786 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19787 cp_parser_decl_specifier_seq (parser,
19788 CP_PARSER_FLAGS_OPTIONAL,
19790 &declares_class_or_enum);
19792 *friend_p = cp_parser_friend_p (&decl_specifiers);
19794 /* There are no template typedefs. */
19795 if (decl_specifiers.specs[(int) ds_typedef])
19797 error_at (decl_spec_token_start->location,
19798 "template declaration of %<typedef%>");
19799 decl = error_mark_node;
19802 /* Gather up the access checks that occurred the
19803 decl-specifier-seq. */
19804 stop_deferring_access_checks ();
19806 /* Check for the declaration of a template class. */
19807 if (declares_class_or_enum)
19809 if (cp_parser_declares_only_class_p (parser))
19811 decl = shadow_tag (&decl_specifiers);
19816 friend template <typename T> struct A<T>::B;
19819 A<T>::B will be represented by a TYPENAME_TYPE, and
19820 therefore not recognized by shadow_tag. */
19821 if (friend_p && *friend_p
19823 && decl_specifiers.type
19824 && TYPE_P (decl_specifiers.type))
19825 decl = decl_specifiers.type;
19827 if (decl && decl != error_mark_node)
19828 decl = TYPE_NAME (decl);
19830 decl = error_mark_node;
19832 /* Perform access checks for template parameters. */
19833 cp_parser_perform_template_parameter_access_checks (checks);
19837 /* Complain about missing 'typename' or other invalid type names. */
19838 if (!decl_specifiers.any_type_specifiers_p)
19839 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19841 /* If it's not a template class, try for a template function. If
19842 the next token is a `;', then this declaration does not declare
19843 anything. But, if there were errors in the decl-specifiers, then
19844 the error might well have come from an attempted class-specifier.
19845 In that case, there's no need to warn about a missing declarator. */
19847 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19848 || decl_specifiers.type != error_mark_node))
19850 decl = cp_parser_init_declarator (parser,
19853 /*function_definition_allowed_p=*/true,
19855 declares_class_or_enum,
19856 &function_definition_p);
19858 /* 7.1.1-1 [dcl.stc]
19860 A storage-class-specifier shall not be specified in an explicit
19861 specialization... */
19863 && explicit_specialization_p
19864 && decl_specifiers.storage_class != sc_none)
19866 error_at (decl_spec_token_start->location,
19867 "explicit template specialization cannot have a storage class");
19868 decl = error_mark_node;
19872 pop_deferring_access_checks ();
19874 /* Clear any current qualification; whatever comes next is the start
19875 of something new. */
19876 parser->scope = NULL_TREE;
19877 parser->qualifying_scope = NULL_TREE;
19878 parser->object_scope = NULL_TREE;
19879 /* Look for a trailing `;' after the declaration. */
19880 if (!function_definition_p
19881 && (decl == error_mark_node
19882 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19883 cp_parser_skip_to_end_of_block_or_statement (parser);
19888 /* Parse a cast-expression that is not the operand of a unary "&". */
19891 cp_parser_simple_cast_expression (cp_parser *parser)
19893 return cp_parser_cast_expression (parser, /*address_p=*/false,
19894 /*cast_p=*/false, NULL);
19897 /* Parse a functional cast to TYPE. Returns an expression
19898 representing the cast. */
19901 cp_parser_functional_cast (cp_parser* parser, tree type)
19904 tree expression_list;
19908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19910 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19911 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19912 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19913 if (TREE_CODE (type) == TYPE_DECL)
19914 type = TREE_TYPE (type);
19915 return finish_compound_literal (type, expression_list);
19919 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19921 /*allow_expansion_p=*/true,
19922 /*non_constant_p=*/NULL);
19924 expression_list = error_mark_node;
19927 expression_list = build_tree_list_vec (vec);
19928 release_tree_vector (vec);
19931 cast = build_functional_cast (type, expression_list,
19932 tf_warning_or_error);
19933 /* [expr.const]/1: In an integral constant expression "only type
19934 conversions to integral or enumeration type can be used". */
19935 if (TREE_CODE (type) == TYPE_DECL)
19936 type = TREE_TYPE (type);
19937 if (cast != error_mark_node
19938 && !cast_valid_in_integral_constant_expression_p (type)
19939 && cp_parser_non_integral_constant_expression (parser,
19941 return error_mark_node;
19945 /* Save the tokens that make up the body of a member function defined
19946 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19947 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19948 specifiers applied to the declaration. Returns the FUNCTION_DECL
19949 for the member function. */
19952 cp_parser_save_member_function_body (cp_parser* parser,
19953 cp_decl_specifier_seq *decl_specifiers,
19954 cp_declarator *declarator,
19961 /* Create the FUNCTION_DECL. */
19962 fn = grokmethod (decl_specifiers, declarator, attributes);
19963 /* If something went badly wrong, bail out now. */
19964 if (fn == error_mark_node)
19966 /* If there's a function-body, skip it. */
19967 if (cp_parser_token_starts_function_definition_p
19968 (cp_lexer_peek_token (parser->lexer)))
19969 cp_parser_skip_to_end_of_block_or_statement (parser);
19970 return error_mark_node;
19973 /* Remember it, if there default args to post process. */
19974 cp_parser_save_default_args (parser, fn);
19976 /* Save away the tokens that make up the body of the
19978 first = parser->lexer->next_token;
19979 /* We can have braced-init-list mem-initializers before the fn body. */
19980 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19982 cp_lexer_consume_token (parser->lexer);
19983 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19984 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19986 /* cache_group will stop after an un-nested { } pair, too. */
19987 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19990 /* variadic mem-inits have ... after the ')'. */
19991 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19992 cp_lexer_consume_token (parser->lexer);
19995 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19996 /* Handle function try blocks. */
19997 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19998 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19999 last = parser->lexer->next_token;
20001 /* Save away the inline definition; we will process it when the
20002 class is complete. */
20003 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20004 DECL_PENDING_INLINE_P (fn) = 1;
20006 /* We need to know that this was defined in the class, so that
20007 friend templates are handled correctly. */
20008 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20010 /* Add FN to the queue of functions to be parsed later. */
20011 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20016 /* Parse a template-argument-list, as well as the trailing ">" (but
20017 not the opening ">"). See cp_parser_template_argument_list for the
20021 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20025 tree saved_qualifying_scope;
20026 tree saved_object_scope;
20027 bool saved_greater_than_is_operator_p;
20028 int saved_unevaluated_operand;
20029 int saved_inhibit_evaluation_warnings;
20033 When parsing a template-id, the first non-nested `>' is taken as
20034 the end of the template-argument-list rather than a greater-than
20036 saved_greater_than_is_operator_p
20037 = parser->greater_than_is_operator_p;
20038 parser->greater_than_is_operator_p = false;
20039 /* Parsing the argument list may modify SCOPE, so we save it
20041 saved_scope = parser->scope;
20042 saved_qualifying_scope = parser->qualifying_scope;
20043 saved_object_scope = parser->object_scope;
20044 /* We need to evaluate the template arguments, even though this
20045 template-id may be nested within a "sizeof". */
20046 saved_unevaluated_operand = cp_unevaluated_operand;
20047 cp_unevaluated_operand = 0;
20048 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20049 c_inhibit_evaluation_warnings = 0;
20050 /* Parse the template-argument-list itself. */
20051 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20052 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20053 arguments = NULL_TREE;
20055 arguments = cp_parser_template_argument_list (parser);
20056 /* Look for the `>' that ends the template-argument-list. If we find
20057 a '>>' instead, it's probably just a typo. */
20058 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20060 if (cxx_dialect != cxx98)
20062 /* In C++0x, a `>>' in a template argument list or cast
20063 expression is considered to be two separate `>'
20064 tokens. So, change the current token to a `>', but don't
20065 consume it: it will be consumed later when the outer
20066 template argument list (or cast expression) is parsed.
20067 Note that this replacement of `>' for `>>' is necessary
20068 even if we are parsing tentatively: in the tentative
20069 case, after calling
20070 cp_parser_enclosed_template_argument_list we will always
20071 throw away all of the template arguments and the first
20072 closing `>', either because the template argument list
20073 was erroneous or because we are replacing those tokens
20074 with a CPP_TEMPLATE_ID token. The second `>' (which will
20075 not have been thrown away) is needed either to close an
20076 outer template argument list or to complete a new-style
20078 cp_token *token = cp_lexer_peek_token (parser->lexer);
20079 token->type = CPP_GREATER;
20081 else if (!saved_greater_than_is_operator_p)
20083 /* If we're in a nested template argument list, the '>>' has
20084 to be a typo for '> >'. We emit the error message, but we
20085 continue parsing and we push a '>' as next token, so that
20086 the argument list will be parsed correctly. Note that the
20087 global source location is still on the token before the
20088 '>>', so we need to say explicitly where we want it. */
20089 cp_token *token = cp_lexer_peek_token (parser->lexer);
20090 error_at (token->location, "%<>>%> should be %<> >%> "
20091 "within a nested template argument list");
20093 token->type = CPP_GREATER;
20097 /* If this is not a nested template argument list, the '>>'
20098 is a typo for '>'. Emit an error message and continue.
20099 Same deal about the token location, but here we can get it
20100 right by consuming the '>>' before issuing the diagnostic. */
20101 cp_token *token = cp_lexer_consume_token (parser->lexer);
20102 error_at (token->location,
20103 "spurious %<>>%>, use %<>%> to terminate "
20104 "a template argument list");
20108 cp_parser_skip_to_end_of_template_parameter_list (parser);
20109 /* The `>' token might be a greater-than operator again now. */
20110 parser->greater_than_is_operator_p
20111 = saved_greater_than_is_operator_p;
20112 /* Restore the SAVED_SCOPE. */
20113 parser->scope = saved_scope;
20114 parser->qualifying_scope = saved_qualifying_scope;
20115 parser->object_scope = saved_object_scope;
20116 cp_unevaluated_operand = saved_unevaluated_operand;
20117 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20122 /* MEMBER_FUNCTION is a member function, or a friend. If default
20123 arguments, or the body of the function have not yet been parsed,
20127 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20129 /* If this member is a template, get the underlying
20131 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20132 member_function = DECL_TEMPLATE_RESULT (member_function);
20134 /* There should not be any class definitions in progress at this
20135 point; the bodies of members are only parsed outside of all class
20137 gcc_assert (parser->num_classes_being_defined == 0);
20138 /* While we're parsing the member functions we might encounter more
20139 classes. We want to handle them right away, but we don't want
20140 them getting mixed up with functions that are currently in the
20142 push_unparsed_function_queues (parser);
20144 /* Make sure that any template parameters are in scope. */
20145 maybe_begin_member_template_processing (member_function);
20147 /* If the body of the function has not yet been parsed, parse it
20149 if (DECL_PENDING_INLINE_P (member_function))
20151 tree function_scope;
20152 cp_token_cache *tokens;
20154 /* The function is no longer pending; we are processing it. */
20155 tokens = DECL_PENDING_INLINE_INFO (member_function);
20156 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20157 DECL_PENDING_INLINE_P (member_function) = 0;
20159 /* If this is a local class, enter the scope of the containing
20161 function_scope = current_function_decl;
20162 if (function_scope)
20163 push_function_context ();
20165 /* Push the body of the function onto the lexer stack. */
20166 cp_parser_push_lexer_for_tokens (parser, tokens);
20168 /* Let the front end know that we going to be defining this
20170 start_preparsed_function (member_function, NULL_TREE,
20171 SF_PRE_PARSED | SF_INCLASS_INLINE);
20173 /* Don't do access checking if it is a templated function. */
20174 if (processing_template_decl)
20175 push_deferring_access_checks (dk_no_check);
20177 /* Now, parse the body of the function. */
20178 cp_parser_function_definition_after_declarator (parser,
20179 /*inline_p=*/true);
20181 if (processing_template_decl)
20182 pop_deferring_access_checks ();
20184 /* Leave the scope of the containing function. */
20185 if (function_scope)
20186 pop_function_context ();
20187 cp_parser_pop_lexer (parser);
20190 /* Remove any template parameters from the symbol table. */
20191 maybe_end_member_template_processing ();
20193 /* Restore the queue. */
20194 pop_unparsed_function_queues (parser);
20197 /* If DECL contains any default args, remember it on the unparsed
20198 functions queue. */
20201 cp_parser_save_default_args (cp_parser* parser, tree decl)
20205 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20207 probe = TREE_CHAIN (probe))
20208 if (TREE_PURPOSE (probe))
20210 cp_default_arg_entry *entry
20211 = VEC_safe_push (cp_default_arg_entry, gc,
20212 unparsed_funs_with_default_args, NULL);
20213 entry->class_type = current_class_type;
20214 entry->decl = decl;
20219 /* FN is a FUNCTION_DECL which may contains a parameter with an
20220 unparsed DEFAULT_ARG. Parse the default args now. This function
20221 assumes that the current scope is the scope in which the default
20222 argument should be processed. */
20225 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20227 bool saved_local_variables_forbidden_p;
20228 tree parm, parmdecl;
20230 /* While we're parsing the default args, we might (due to the
20231 statement expression extension) encounter more classes. We want
20232 to handle them right away, but we don't want them getting mixed
20233 up with default args that are currently in the queue. */
20234 push_unparsed_function_queues (parser);
20236 /* Local variable names (and the `this' keyword) may not appear
20237 in a default argument. */
20238 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20239 parser->local_variables_forbidden_p = true;
20241 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20242 parmdecl = DECL_ARGUMENTS (fn);
20243 parm && parm != void_list_node;
20244 parm = TREE_CHAIN (parm),
20245 parmdecl = DECL_CHAIN (parmdecl))
20247 cp_token_cache *tokens;
20248 tree default_arg = TREE_PURPOSE (parm);
20250 VEC(tree,gc) *insts;
20257 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20258 /* This can happen for a friend declaration for a function
20259 already declared with default arguments. */
20262 /* Push the saved tokens for the default argument onto the parser's
20264 tokens = DEFARG_TOKENS (default_arg);
20265 cp_parser_push_lexer_for_tokens (parser, tokens);
20267 start_lambda_scope (parmdecl);
20269 /* Parse the assignment-expression. */
20270 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20271 if (parsed_arg == error_mark_node)
20273 cp_parser_pop_lexer (parser);
20277 if (!processing_template_decl)
20278 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20280 TREE_PURPOSE (parm) = parsed_arg;
20282 /* Update any instantiations we've already created. */
20283 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20284 VEC_iterate (tree, insts, ix, copy); ix++)
20285 TREE_PURPOSE (copy) = parsed_arg;
20287 finish_lambda_scope ();
20289 /* If the token stream has not been completely used up, then
20290 there was extra junk after the end of the default
20292 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20293 cp_parser_error (parser, "expected %<,%>");
20295 /* Revert to the main lexer. */
20296 cp_parser_pop_lexer (parser);
20299 /* Make sure no default arg is missing. */
20300 check_default_args (fn);
20302 /* Restore the state of local_variables_forbidden_p. */
20303 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20305 /* Restore the queue. */
20306 pop_unparsed_function_queues (parser);
20309 /* Parse the operand of `sizeof' (or a similar operator). Returns
20310 either a TYPE or an expression, depending on the form of the
20311 input. The KEYWORD indicates which kind of expression we have
20315 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20317 tree expr = NULL_TREE;
20318 const char *saved_message;
20320 bool saved_integral_constant_expression_p;
20321 bool saved_non_integral_constant_expression_p;
20322 bool pack_expansion_p = false;
20324 /* Types cannot be defined in a `sizeof' expression. Save away the
20326 saved_message = parser->type_definition_forbidden_message;
20327 /* And create the new one. */
20328 tmp = concat ("types may not be defined in %<",
20329 IDENTIFIER_POINTER (ridpointers[keyword]),
20330 "%> expressions", NULL);
20331 parser->type_definition_forbidden_message = tmp;
20333 /* The restrictions on constant-expressions do not apply inside
20334 sizeof expressions. */
20335 saved_integral_constant_expression_p
20336 = parser->integral_constant_expression_p;
20337 saved_non_integral_constant_expression_p
20338 = parser->non_integral_constant_expression_p;
20339 parser->integral_constant_expression_p = false;
20341 /* If it's a `...', then we are computing the length of a parameter
20343 if (keyword == RID_SIZEOF
20344 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20346 /* Consume the `...'. */
20347 cp_lexer_consume_token (parser->lexer);
20348 maybe_warn_variadic_templates ();
20350 /* Note that this is an expansion. */
20351 pack_expansion_p = true;
20354 /* Do not actually evaluate the expression. */
20355 ++cp_unevaluated_operand;
20356 ++c_inhibit_evaluation_warnings;
20357 /* If it's a `(', then we might be looking at the type-id
20359 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20362 bool saved_in_type_id_in_expr_p;
20364 /* We can't be sure yet whether we're looking at a type-id or an
20366 cp_parser_parse_tentatively (parser);
20367 /* Consume the `('. */
20368 cp_lexer_consume_token (parser->lexer);
20369 /* Parse the type-id. */
20370 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20371 parser->in_type_id_in_expr_p = true;
20372 type = cp_parser_type_id (parser);
20373 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20374 /* Now, look for the trailing `)'. */
20375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20376 /* If all went well, then we're done. */
20377 if (cp_parser_parse_definitely (parser))
20379 cp_decl_specifier_seq decl_specs;
20381 /* Build a trivial decl-specifier-seq. */
20382 clear_decl_specs (&decl_specs);
20383 decl_specs.type = type;
20385 /* Call grokdeclarator to figure out what type this is. */
20386 expr = grokdeclarator (NULL,
20390 /*attrlist=*/NULL);
20394 /* If the type-id production did not work out, then we must be
20395 looking at the unary-expression production. */
20397 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20398 /*cast_p=*/false, NULL);
20400 if (pack_expansion_p)
20401 /* Build a pack expansion. */
20402 expr = make_pack_expansion (expr);
20404 /* Go back to evaluating expressions. */
20405 --cp_unevaluated_operand;
20406 --c_inhibit_evaluation_warnings;
20408 /* Free the message we created. */
20410 /* And restore the old one. */
20411 parser->type_definition_forbidden_message = saved_message;
20412 parser->integral_constant_expression_p
20413 = saved_integral_constant_expression_p;
20414 parser->non_integral_constant_expression_p
20415 = saved_non_integral_constant_expression_p;
20420 /* If the current declaration has no declarator, return true. */
20423 cp_parser_declares_only_class_p (cp_parser *parser)
20425 /* If the next token is a `;' or a `,' then there is no
20427 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20428 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20431 /* Update the DECL_SPECS to reflect the storage class indicated by
20435 cp_parser_set_storage_class (cp_parser *parser,
20436 cp_decl_specifier_seq *decl_specs,
20438 location_t location)
20440 cp_storage_class storage_class;
20442 if (parser->in_unbraced_linkage_specification_p)
20444 error_at (location, "invalid use of %qD in linkage specification",
20445 ridpointers[keyword]);
20448 else if (decl_specs->storage_class != sc_none)
20450 decl_specs->conflicting_specifiers_p = true;
20454 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20455 && decl_specs->specs[(int) ds_thread])
20457 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20458 decl_specs->specs[(int) ds_thread] = 0;
20464 storage_class = sc_auto;
20467 storage_class = sc_register;
20470 storage_class = sc_static;
20473 storage_class = sc_extern;
20476 storage_class = sc_mutable;
20479 gcc_unreachable ();
20481 decl_specs->storage_class = storage_class;
20483 /* A storage class specifier cannot be applied alongside a typedef
20484 specifier. If there is a typedef specifier present then set
20485 conflicting_specifiers_p which will trigger an error later
20486 on in grokdeclarator. */
20487 if (decl_specs->specs[(int)ds_typedef])
20488 decl_specs->conflicting_specifiers_p = true;
20491 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20492 is true, the type is a user-defined type; otherwise it is a
20493 built-in type specified by a keyword. */
20496 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20498 location_t location,
20499 bool user_defined_p)
20501 decl_specs->any_specifiers_p = true;
20503 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20504 (with, for example, in "typedef int wchar_t;") we remember that
20505 this is what happened. In system headers, we ignore these
20506 declarations so that G++ can work with system headers that are not
20508 if (decl_specs->specs[(int) ds_typedef]
20510 && (type_spec == boolean_type_node
20511 || type_spec == char16_type_node
20512 || type_spec == char32_type_node
20513 || type_spec == wchar_type_node)
20514 && (decl_specs->type
20515 || decl_specs->specs[(int) ds_long]
20516 || decl_specs->specs[(int) ds_short]
20517 || decl_specs->specs[(int) ds_unsigned]
20518 || decl_specs->specs[(int) ds_signed]))
20520 decl_specs->redefined_builtin_type = type_spec;
20521 if (!decl_specs->type)
20523 decl_specs->type = type_spec;
20524 decl_specs->user_defined_type_p = false;
20525 decl_specs->type_location = location;
20528 else if (decl_specs->type)
20529 decl_specs->multiple_types_p = true;
20532 decl_specs->type = type_spec;
20533 decl_specs->user_defined_type_p = user_defined_p;
20534 decl_specs->redefined_builtin_type = NULL_TREE;
20535 decl_specs->type_location = location;
20539 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20540 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20543 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20545 return decl_specifiers->specs[(int) ds_friend] != 0;
20548 /* Issue an error message indicating that TOKEN_DESC was expected.
20549 If KEYWORD is true, it indicated this function is called by
20550 cp_parser_require_keword and the required token can only be
20551 a indicated keyword. */
20554 cp_parser_required_error (cp_parser *parser,
20555 required_token token_desc,
20558 switch (token_desc)
20561 cp_parser_error (parser, "expected %<new%>");
20564 cp_parser_error (parser, "expected %<delete%>");
20567 cp_parser_error (parser, "expected %<return%>");
20570 cp_parser_error (parser, "expected %<while%>");
20573 cp_parser_error (parser, "expected %<extern%>");
20575 case RT_STATIC_ASSERT:
20576 cp_parser_error (parser, "expected %<static_assert%>");
20579 cp_parser_error (parser, "expected %<decltype%>");
20582 cp_parser_error (parser, "expected %<operator%>");
20585 cp_parser_error (parser, "expected %<class%>");
20588 cp_parser_error (parser, "expected %<template%>");
20591 cp_parser_error (parser, "expected %<namespace%>");
20594 cp_parser_error (parser, "expected %<using%>");
20597 cp_parser_error (parser, "expected %<asm%>");
20600 cp_parser_error (parser, "expected %<try%>");
20603 cp_parser_error (parser, "expected %<catch%>");
20606 cp_parser_error (parser, "expected %<throw%>");
20609 cp_parser_error (parser, "expected %<__label__%>");
20612 cp_parser_error (parser, "expected %<@try%>");
20614 case RT_AT_SYNCHRONIZED:
20615 cp_parser_error (parser, "expected %<@synchronized%>");
20618 cp_parser_error (parser, "expected %<@throw%>");
20625 switch (token_desc)
20628 cp_parser_error (parser, "expected %<;%>");
20630 case RT_OPEN_PAREN:
20631 cp_parser_error (parser, "expected %<(%>");
20633 case RT_CLOSE_BRACE:
20634 cp_parser_error (parser, "expected %<}%>");
20636 case RT_OPEN_BRACE:
20637 cp_parser_error (parser, "expected %<{%>");
20639 case RT_CLOSE_SQUARE:
20640 cp_parser_error (parser, "expected %<]%>");
20642 case RT_OPEN_SQUARE:
20643 cp_parser_error (parser, "expected %<[%>");
20646 cp_parser_error (parser, "expected %<,%>");
20649 cp_parser_error (parser, "expected %<::%>");
20652 cp_parser_error (parser, "expected %<<%>");
20655 cp_parser_error (parser, "expected %<>%>");
20658 cp_parser_error (parser, "expected %<=%>");
20661 cp_parser_error (parser, "expected %<...%>");
20664 cp_parser_error (parser, "expected %<*%>");
20667 cp_parser_error (parser, "expected %<~%>");
20670 cp_parser_error (parser, "expected %<:%>");
20672 case RT_COLON_SCOPE:
20673 cp_parser_error (parser, "expected %<:%> or %<::%>");
20675 case RT_CLOSE_PAREN:
20676 cp_parser_error (parser, "expected %<)%>");
20678 case RT_COMMA_CLOSE_PAREN:
20679 cp_parser_error (parser, "expected %<,%> or %<)%>");
20681 case RT_PRAGMA_EOL:
20682 cp_parser_error (parser, "expected end of line");
20685 cp_parser_error (parser, "expected identifier");
20688 cp_parser_error (parser, "expected selection-statement");
20690 case RT_INTERATION:
20691 cp_parser_error (parser, "expected iteration-statement");
20694 cp_parser_error (parser, "expected jump-statement");
20697 cp_parser_error (parser, "expected class-key");
20699 case RT_CLASS_TYPENAME_TEMPLATE:
20700 cp_parser_error (parser,
20701 "expected %<class%>, %<typename%>, or %<template%>");
20704 gcc_unreachable ();
20708 gcc_unreachable ();
20713 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20714 issue an error message indicating that TOKEN_DESC was expected.
20716 Returns the token consumed, if the token had the appropriate type.
20717 Otherwise, returns NULL. */
20720 cp_parser_require (cp_parser* parser,
20721 enum cpp_ttype type,
20722 required_token token_desc)
20724 if (cp_lexer_next_token_is (parser->lexer, type))
20725 return cp_lexer_consume_token (parser->lexer);
20728 /* Output the MESSAGE -- unless we're parsing tentatively. */
20729 if (!cp_parser_simulate_error (parser))
20730 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20735 /* An error message is produced if the next token is not '>'.
20736 All further tokens are skipped until the desired token is
20737 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20740 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20742 /* Current level of '< ... >'. */
20743 unsigned level = 0;
20744 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20745 unsigned nesting_depth = 0;
20747 /* Are we ready, yet? If not, issue error message. */
20748 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20751 /* Skip tokens until the desired token is found. */
20754 /* Peek at the next token. */
20755 switch (cp_lexer_peek_token (parser->lexer)->type)
20758 if (!nesting_depth)
20763 if (cxx_dialect == cxx98)
20764 /* C++0x views the `>>' operator as two `>' tokens, but
20767 else if (!nesting_depth && level-- == 0)
20769 /* We've hit a `>>' where the first `>' closes the
20770 template argument list, and the second `>' is
20771 spurious. Just consume the `>>' and stop; we've
20772 already produced at least one error. */
20773 cp_lexer_consume_token (parser->lexer);
20776 /* Fall through for C++0x, so we handle the second `>' in
20780 if (!nesting_depth && level-- == 0)
20782 /* We've reached the token we want, consume it and stop. */
20783 cp_lexer_consume_token (parser->lexer);
20788 case CPP_OPEN_PAREN:
20789 case CPP_OPEN_SQUARE:
20793 case CPP_CLOSE_PAREN:
20794 case CPP_CLOSE_SQUARE:
20795 if (nesting_depth-- == 0)
20800 case CPP_PRAGMA_EOL:
20801 case CPP_SEMICOLON:
20802 case CPP_OPEN_BRACE:
20803 case CPP_CLOSE_BRACE:
20804 /* The '>' was probably forgotten, don't look further. */
20811 /* Consume this token. */
20812 cp_lexer_consume_token (parser->lexer);
20816 /* If the next token is the indicated keyword, consume it. Otherwise,
20817 issue an error message indicating that TOKEN_DESC was expected.
20819 Returns the token consumed, if the token had the appropriate type.
20820 Otherwise, returns NULL. */
20823 cp_parser_require_keyword (cp_parser* parser,
20825 required_token token_desc)
20827 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20829 if (token && token->keyword != keyword)
20831 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20838 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20839 function-definition. */
20842 cp_parser_token_starts_function_definition_p (cp_token* token)
20844 return (/* An ordinary function-body begins with an `{'. */
20845 token->type == CPP_OPEN_BRACE
20846 /* A ctor-initializer begins with a `:'. */
20847 || token->type == CPP_COLON
20848 /* A function-try-block begins with `try'. */
20849 || token->keyword == RID_TRY
20850 /* The named return value extension begins with `return'. */
20851 || token->keyword == RID_RETURN);
20854 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20858 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20862 token = cp_lexer_peek_token (parser->lexer);
20863 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20866 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20867 C++0x) ending a template-argument. */
20870 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20874 token = cp_lexer_peek_token (parser->lexer);
20875 return (token->type == CPP_COMMA
20876 || token->type == CPP_GREATER
20877 || token->type == CPP_ELLIPSIS
20878 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20881 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20882 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20885 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20890 token = cp_lexer_peek_nth_token (parser->lexer, n);
20891 if (token->type == CPP_LESS)
20893 /* Check for the sequence `<::' in the original code. It would be lexed as
20894 `[:', where `[' is a digraph, and there is no whitespace before
20896 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20899 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20900 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20906 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20907 or none_type otherwise. */
20909 static enum tag_types
20910 cp_parser_token_is_class_key (cp_token* token)
20912 switch (token->keyword)
20917 return record_type;
20926 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20929 cp_parser_check_class_key (enum tag_types class_key, tree type)
20931 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20932 permerror (input_location, "%qs tag used in naming %q#T",
20933 class_key == union_type ? "union"
20934 : class_key == record_type ? "struct" : "class",
20938 /* Issue an error message if DECL is redeclared with different
20939 access than its original declaration [class.access.spec/3].
20940 This applies to nested classes and nested class templates.
20944 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20946 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20949 if ((TREE_PRIVATE (decl)
20950 != (current_access_specifier == access_private_node))
20951 || (TREE_PROTECTED (decl)
20952 != (current_access_specifier == access_protected_node)))
20953 error_at (location, "%qD redeclared with different access", decl);
20956 /* Look for the `template' keyword, as a syntactic disambiguator.
20957 Return TRUE iff it is present, in which case it will be
20961 cp_parser_optional_template_keyword (cp_parser *parser)
20963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20965 /* The `template' keyword can only be used within templates;
20966 outside templates the parser can always figure out what is a
20967 template and what is not. */
20968 if (!processing_template_decl)
20970 cp_token *token = cp_lexer_peek_token (parser->lexer);
20971 error_at (token->location,
20972 "%<template%> (as a disambiguator) is only allowed "
20973 "within templates");
20974 /* If this part of the token stream is rescanned, the same
20975 error message would be generated. So, we purge the token
20976 from the stream. */
20977 cp_lexer_purge_token (parser->lexer);
20982 /* Consume the `template' keyword. */
20983 cp_lexer_consume_token (parser->lexer);
20991 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20992 set PARSER->SCOPE, and perform other related actions. */
20995 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20998 struct tree_check *check_value;
20999 deferred_access_check *chk;
21000 VEC (deferred_access_check,gc) *checks;
21002 /* Get the stored value. */
21003 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21004 /* Perform any access checks that were deferred. */
21005 checks = check_value->checks;
21008 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21009 perform_or_defer_access_check (chk->binfo,
21013 /* Set the scope from the stored value. */
21014 parser->scope = check_value->value;
21015 parser->qualifying_scope = check_value->qualifying_scope;
21016 parser->object_scope = NULL_TREE;
21019 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21020 encounter the end of a block before what we were looking for. */
21023 cp_parser_cache_group (cp_parser *parser,
21024 enum cpp_ttype end,
21029 cp_token *token = cp_lexer_peek_token (parser->lexer);
21031 /* Abort a parenthesized expression if we encounter a semicolon. */
21032 if ((end == CPP_CLOSE_PAREN || depth == 0)
21033 && token->type == CPP_SEMICOLON)
21035 /* If we've reached the end of the file, stop. */
21036 if (token->type == CPP_EOF
21037 || (end != CPP_PRAGMA_EOL
21038 && token->type == CPP_PRAGMA_EOL))
21040 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21041 /* We've hit the end of an enclosing block, so there's been some
21042 kind of syntax error. */
21045 /* Consume the token. */
21046 cp_lexer_consume_token (parser->lexer);
21047 /* See if it starts a new group. */
21048 if (token->type == CPP_OPEN_BRACE)
21050 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21051 /* In theory this should probably check end == '}', but
21052 cp_parser_save_member_function_body needs it to exit
21053 after either '}' or ')' when called with ')'. */
21057 else if (token->type == CPP_OPEN_PAREN)
21059 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21060 if (depth == 0 && end == CPP_CLOSE_PAREN)
21063 else if (token->type == CPP_PRAGMA)
21064 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21065 else if (token->type == end)
21070 /* Begin parsing tentatively. We always save tokens while parsing
21071 tentatively so that if the tentative parsing fails we can restore the
21075 cp_parser_parse_tentatively (cp_parser* parser)
21077 /* Enter a new parsing context. */
21078 parser->context = cp_parser_context_new (parser->context);
21079 /* Begin saving tokens. */
21080 cp_lexer_save_tokens (parser->lexer);
21081 /* In order to avoid repetitive access control error messages,
21082 access checks are queued up until we are no longer parsing
21084 push_deferring_access_checks (dk_deferred);
21087 /* Commit to the currently active tentative parse. */
21090 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21092 cp_parser_context *context;
21095 /* Mark all of the levels as committed. */
21096 lexer = parser->lexer;
21097 for (context = parser->context; context->next; context = context->next)
21099 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21101 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21102 while (!cp_lexer_saving_tokens (lexer))
21103 lexer = lexer->next;
21104 cp_lexer_commit_tokens (lexer);
21108 /* Abort the currently active tentative parse. All consumed tokens
21109 will be rolled back, and no diagnostics will be issued. */
21112 cp_parser_abort_tentative_parse (cp_parser* parser)
21114 cp_parser_simulate_error (parser);
21115 /* Now, pretend that we want to see if the construct was
21116 successfully parsed. */
21117 cp_parser_parse_definitely (parser);
21120 /* Stop parsing tentatively. If a parse error has occurred, restore the
21121 token stream. Otherwise, commit to the tokens we have consumed.
21122 Returns true if no error occurred; false otherwise. */
21125 cp_parser_parse_definitely (cp_parser* parser)
21127 bool error_occurred;
21128 cp_parser_context *context;
21130 /* Remember whether or not an error occurred, since we are about to
21131 destroy that information. */
21132 error_occurred = cp_parser_error_occurred (parser);
21133 /* Remove the topmost context from the stack. */
21134 context = parser->context;
21135 parser->context = context->next;
21136 /* If no parse errors occurred, commit to the tentative parse. */
21137 if (!error_occurred)
21139 /* Commit to the tokens read tentatively, unless that was
21141 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21142 cp_lexer_commit_tokens (parser->lexer);
21144 pop_to_parent_deferring_access_checks ();
21146 /* Otherwise, if errors occurred, roll back our state so that things
21147 are just as they were before we began the tentative parse. */
21150 cp_lexer_rollback_tokens (parser->lexer);
21151 pop_deferring_access_checks ();
21153 /* Add the context to the front of the free list. */
21154 context->next = cp_parser_context_free_list;
21155 cp_parser_context_free_list = context;
21157 return !error_occurred;
21160 /* Returns true if we are parsing tentatively and are not committed to
21161 this tentative parse. */
21164 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21166 return (cp_parser_parsing_tentatively (parser)
21167 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21170 /* Returns nonzero iff an error has occurred during the most recent
21171 tentative parse. */
21174 cp_parser_error_occurred (cp_parser* parser)
21176 return (cp_parser_parsing_tentatively (parser)
21177 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21180 /* Returns nonzero if GNU extensions are allowed. */
21183 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21185 return parser->allow_gnu_extensions_p;
21188 /* Objective-C++ Productions */
21191 /* Parse an Objective-C expression, which feeds into a primary-expression
21195 objc-message-expression
21196 objc-string-literal
21197 objc-encode-expression
21198 objc-protocol-expression
21199 objc-selector-expression
21201 Returns a tree representation of the expression. */
21204 cp_parser_objc_expression (cp_parser* parser)
21206 /* Try to figure out what kind of declaration is present. */
21207 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21211 case CPP_OPEN_SQUARE:
21212 return cp_parser_objc_message_expression (parser);
21214 case CPP_OBJC_STRING:
21215 kwd = cp_lexer_consume_token (parser->lexer);
21216 return objc_build_string_object (kwd->u.value);
21219 switch (kwd->keyword)
21221 case RID_AT_ENCODE:
21222 return cp_parser_objc_encode_expression (parser);
21224 case RID_AT_PROTOCOL:
21225 return cp_parser_objc_protocol_expression (parser);
21227 case RID_AT_SELECTOR:
21228 return cp_parser_objc_selector_expression (parser);
21234 error_at (kwd->location,
21235 "misplaced %<@%D%> Objective-C++ construct",
21237 cp_parser_skip_to_end_of_block_or_statement (parser);
21240 return error_mark_node;
21243 /* Parse an Objective-C message expression.
21245 objc-message-expression:
21246 [ objc-message-receiver objc-message-args ]
21248 Returns a representation of an Objective-C message. */
21251 cp_parser_objc_message_expression (cp_parser* parser)
21253 tree receiver, messageargs;
21255 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21256 receiver = cp_parser_objc_message_receiver (parser);
21257 messageargs = cp_parser_objc_message_args (parser);
21258 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21260 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21263 /* Parse an objc-message-receiver.
21265 objc-message-receiver:
21267 simple-type-specifier
21269 Returns a representation of the type or expression. */
21272 cp_parser_objc_message_receiver (cp_parser* parser)
21276 /* An Objective-C message receiver may be either (1) a type
21277 or (2) an expression. */
21278 cp_parser_parse_tentatively (parser);
21279 rcv = cp_parser_expression (parser, false, NULL);
21281 if (cp_parser_parse_definitely (parser))
21284 rcv = cp_parser_simple_type_specifier (parser,
21285 /*decl_specs=*/NULL,
21286 CP_PARSER_FLAGS_NONE);
21288 return objc_get_class_reference (rcv);
21291 /* Parse the arguments and selectors comprising an Objective-C message.
21296 objc-selector-args , objc-comma-args
21298 objc-selector-args:
21299 objc-selector [opt] : assignment-expression
21300 objc-selector-args objc-selector [opt] : assignment-expression
21303 assignment-expression
21304 objc-comma-args , assignment-expression
21306 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21307 selector arguments and TREE_VALUE containing a list of comma
21311 cp_parser_objc_message_args (cp_parser* parser)
21313 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21314 bool maybe_unary_selector_p = true;
21315 cp_token *token = cp_lexer_peek_token (parser->lexer);
21317 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21319 tree selector = NULL_TREE, arg;
21321 if (token->type != CPP_COLON)
21322 selector = cp_parser_objc_selector (parser);
21324 /* Detect if we have a unary selector. */
21325 if (maybe_unary_selector_p
21326 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21327 return build_tree_list (selector, NULL_TREE);
21329 maybe_unary_selector_p = false;
21330 cp_parser_require (parser, CPP_COLON, RT_COLON);
21331 arg = cp_parser_assignment_expression (parser, false, NULL);
21334 = chainon (sel_args,
21335 build_tree_list (selector, arg));
21337 token = cp_lexer_peek_token (parser->lexer);
21340 /* Handle non-selector arguments, if any. */
21341 while (token->type == CPP_COMMA)
21345 cp_lexer_consume_token (parser->lexer);
21346 arg = cp_parser_assignment_expression (parser, false, NULL);
21349 = chainon (addl_args,
21350 build_tree_list (NULL_TREE, arg));
21352 token = cp_lexer_peek_token (parser->lexer);
21355 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21357 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21358 return build_tree_list (error_mark_node, error_mark_node);
21361 return build_tree_list (sel_args, addl_args);
21364 /* Parse an Objective-C encode expression.
21366 objc-encode-expression:
21367 @encode objc-typename
21369 Returns an encoded representation of the type argument. */
21372 cp_parser_objc_encode_expression (cp_parser* parser)
21377 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21378 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21379 token = cp_lexer_peek_token (parser->lexer);
21380 type = complete_type (cp_parser_type_id (parser));
21381 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21385 error_at (token->location,
21386 "%<@encode%> must specify a type as an argument");
21387 return error_mark_node;
21390 /* This happens if we find @encode(T) (where T is a template
21391 typename or something dependent on a template typename) when
21392 parsing a template. In that case, we can't compile it
21393 immediately, but we rather create an AT_ENCODE_EXPR which will
21394 need to be instantiated when the template is used.
21396 if (dependent_type_p (type))
21398 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21399 TREE_READONLY (value) = 1;
21403 return objc_build_encode_expr (type);
21406 /* Parse an Objective-C @defs expression. */
21409 cp_parser_objc_defs_expression (cp_parser *parser)
21413 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21414 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21415 name = cp_parser_identifier (parser);
21416 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21418 return objc_get_class_ivars (name);
21421 /* Parse an Objective-C protocol expression.
21423 objc-protocol-expression:
21424 @protocol ( identifier )
21426 Returns a representation of the protocol expression. */
21429 cp_parser_objc_protocol_expression (cp_parser* parser)
21433 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21434 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21435 proto = cp_parser_identifier (parser);
21436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21438 return objc_build_protocol_expr (proto);
21441 /* Parse an Objective-C selector expression.
21443 objc-selector-expression:
21444 @selector ( objc-method-signature )
21446 objc-method-signature:
21452 objc-selector-seq objc-selector :
21454 Returns a representation of the method selector. */
21457 cp_parser_objc_selector_expression (cp_parser* parser)
21459 tree sel_seq = NULL_TREE;
21460 bool maybe_unary_selector_p = true;
21462 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21464 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21465 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21466 token = cp_lexer_peek_token (parser->lexer);
21468 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21469 || token->type == CPP_SCOPE)
21471 tree selector = NULL_TREE;
21473 if (token->type != CPP_COLON
21474 || token->type == CPP_SCOPE)
21475 selector = cp_parser_objc_selector (parser);
21477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21478 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21480 /* Detect if we have a unary selector. */
21481 if (maybe_unary_selector_p)
21483 sel_seq = selector;
21484 goto finish_selector;
21488 cp_parser_error (parser, "expected %<:%>");
21491 maybe_unary_selector_p = false;
21492 token = cp_lexer_consume_token (parser->lexer);
21494 if (token->type == CPP_SCOPE)
21497 = chainon (sel_seq,
21498 build_tree_list (selector, NULL_TREE));
21500 = chainon (sel_seq,
21501 build_tree_list (NULL_TREE, NULL_TREE));
21505 = chainon (sel_seq,
21506 build_tree_list (selector, NULL_TREE));
21508 token = cp_lexer_peek_token (parser->lexer);
21512 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21514 return objc_build_selector_expr (loc, sel_seq);
21517 /* Parse a list of identifiers.
21519 objc-identifier-list:
21521 objc-identifier-list , identifier
21523 Returns a TREE_LIST of identifier nodes. */
21526 cp_parser_objc_identifier_list (cp_parser* parser)
21532 identifier = cp_parser_identifier (parser);
21533 if (identifier == error_mark_node)
21534 return error_mark_node;
21536 list = build_tree_list (NULL_TREE, identifier);
21537 sep = cp_lexer_peek_token (parser->lexer);
21539 while (sep->type == CPP_COMMA)
21541 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21542 identifier = cp_parser_identifier (parser);
21543 if (identifier == error_mark_node)
21546 list = chainon (list, build_tree_list (NULL_TREE,
21548 sep = cp_lexer_peek_token (parser->lexer);
21554 /* Parse an Objective-C alias declaration.
21556 objc-alias-declaration:
21557 @compatibility_alias identifier identifier ;
21559 This function registers the alias mapping with the Objective-C front end.
21560 It returns nothing. */
21563 cp_parser_objc_alias_declaration (cp_parser* parser)
21567 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21568 alias = cp_parser_identifier (parser);
21569 orig = cp_parser_identifier (parser);
21570 objc_declare_alias (alias, orig);
21571 cp_parser_consume_semicolon_at_end_of_statement (parser);
21574 /* Parse an Objective-C class forward-declaration.
21576 objc-class-declaration:
21577 @class objc-identifier-list ;
21579 The function registers the forward declarations with the Objective-C
21580 front end. It returns nothing. */
21583 cp_parser_objc_class_declaration (cp_parser* parser)
21585 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21586 objc_declare_class (cp_parser_objc_identifier_list (parser));
21587 cp_parser_consume_semicolon_at_end_of_statement (parser);
21590 /* Parse a list of Objective-C protocol references.
21592 objc-protocol-refs-opt:
21593 objc-protocol-refs [opt]
21595 objc-protocol-refs:
21596 < objc-identifier-list >
21598 Returns a TREE_LIST of identifiers, if any. */
21601 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21603 tree protorefs = NULL_TREE;
21605 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21607 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21608 protorefs = cp_parser_objc_identifier_list (parser);
21609 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21615 /* Parse a Objective-C visibility specification. */
21618 cp_parser_objc_visibility_spec (cp_parser* parser)
21620 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21622 switch (vis->keyword)
21624 case RID_AT_PRIVATE:
21625 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21627 case RID_AT_PROTECTED:
21628 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21630 case RID_AT_PUBLIC:
21631 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21633 case RID_AT_PACKAGE:
21634 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21640 /* Eat '@private'/'@protected'/'@public'. */
21641 cp_lexer_consume_token (parser->lexer);
21644 /* Parse an Objective-C method type. Return 'true' if it is a class
21645 (+) method, and 'false' if it is an instance (-) method. */
21648 cp_parser_objc_method_type (cp_parser* parser)
21650 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21656 /* Parse an Objective-C protocol qualifier. */
21659 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21661 tree quals = NULL_TREE, node;
21662 cp_token *token = cp_lexer_peek_token (parser->lexer);
21664 node = token->u.value;
21666 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21667 && (node == ridpointers [(int) RID_IN]
21668 || node == ridpointers [(int) RID_OUT]
21669 || node == ridpointers [(int) RID_INOUT]
21670 || node == ridpointers [(int) RID_BYCOPY]
21671 || node == ridpointers [(int) RID_BYREF]
21672 || node == ridpointers [(int) RID_ONEWAY]))
21674 quals = tree_cons (NULL_TREE, node, quals);
21675 cp_lexer_consume_token (parser->lexer);
21676 token = cp_lexer_peek_token (parser->lexer);
21677 node = token->u.value;
21683 /* Parse an Objective-C typename. */
21686 cp_parser_objc_typename (cp_parser* parser)
21688 tree type_name = NULL_TREE;
21690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21692 tree proto_quals, cp_type = NULL_TREE;
21694 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21695 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21697 /* An ObjC type name may consist of just protocol qualifiers, in which
21698 case the type shall default to 'id'. */
21699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21700 cp_type = cp_parser_type_id (parser);
21702 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21703 type_name = build_tree_list (proto_quals, cp_type);
21709 /* Check to see if TYPE refers to an Objective-C selector name. */
21712 cp_parser_objc_selector_p (enum cpp_ttype type)
21714 return (type == CPP_NAME || type == CPP_KEYWORD
21715 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21716 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21717 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21718 || type == CPP_XOR || type == CPP_XOR_EQ);
21721 /* Parse an Objective-C selector. */
21724 cp_parser_objc_selector (cp_parser* parser)
21726 cp_token *token = cp_lexer_consume_token (parser->lexer);
21728 if (!cp_parser_objc_selector_p (token->type))
21730 error_at (token->location, "invalid Objective-C++ selector name");
21731 return error_mark_node;
21734 /* C++ operator names are allowed to appear in ObjC selectors. */
21735 switch (token->type)
21737 case CPP_AND_AND: return get_identifier ("and");
21738 case CPP_AND_EQ: return get_identifier ("and_eq");
21739 case CPP_AND: return get_identifier ("bitand");
21740 case CPP_OR: return get_identifier ("bitor");
21741 case CPP_COMPL: return get_identifier ("compl");
21742 case CPP_NOT: return get_identifier ("not");
21743 case CPP_NOT_EQ: return get_identifier ("not_eq");
21744 case CPP_OR_OR: return get_identifier ("or");
21745 case CPP_OR_EQ: return get_identifier ("or_eq");
21746 case CPP_XOR: return get_identifier ("xor");
21747 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21748 default: return token->u.value;
21752 /* Parse an Objective-C params list. */
21755 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21757 tree params = NULL_TREE;
21758 bool maybe_unary_selector_p = true;
21759 cp_token *token = cp_lexer_peek_token (parser->lexer);
21761 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21763 tree selector = NULL_TREE, type_name, identifier;
21764 tree parm_attr = NULL_TREE;
21766 if (token->keyword == RID_ATTRIBUTE)
21769 if (token->type != CPP_COLON)
21770 selector = cp_parser_objc_selector (parser);
21772 /* Detect if we have a unary selector. */
21773 if (maybe_unary_selector_p
21774 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21776 params = selector; /* Might be followed by attributes. */
21780 maybe_unary_selector_p = false;
21781 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21783 /* Something went quite wrong. There should be a colon
21784 here, but there is not. Stop parsing parameters. */
21787 type_name = cp_parser_objc_typename (parser);
21788 /* New ObjC allows attributes on parameters too. */
21789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21790 parm_attr = cp_parser_attributes_opt (parser);
21791 identifier = cp_parser_identifier (parser);
21795 objc_build_keyword_decl (selector,
21800 token = cp_lexer_peek_token (parser->lexer);
21803 if (params == NULL_TREE)
21805 cp_parser_error (parser, "objective-c++ method declaration is expected");
21806 return error_mark_node;
21809 /* We allow tail attributes for the method. */
21810 if (token->keyword == RID_ATTRIBUTE)
21812 *attributes = cp_parser_attributes_opt (parser);
21813 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21814 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21816 cp_parser_error (parser,
21817 "method attributes must be specified at the end");
21818 return error_mark_node;
21821 if (params == NULL_TREE)
21823 cp_parser_error (parser, "objective-c++ method declaration is expected");
21824 return error_mark_node;
21829 /* Parse the non-keyword Objective-C params. */
21832 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21835 tree params = make_node (TREE_LIST);
21836 cp_token *token = cp_lexer_peek_token (parser->lexer);
21837 *ellipsisp = false; /* Initially, assume no ellipsis. */
21839 while (token->type == CPP_COMMA)
21841 cp_parameter_declarator *parmdecl;
21844 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21845 token = cp_lexer_peek_token (parser->lexer);
21847 if (token->type == CPP_ELLIPSIS)
21849 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21851 token = cp_lexer_peek_token (parser->lexer);
21855 /* TODO: parse attributes for tail parameters. */
21856 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21857 parm = grokdeclarator (parmdecl->declarator,
21858 &parmdecl->decl_specifiers,
21859 PARM, /*initialized=*/0,
21860 /*attrlist=*/NULL);
21862 chainon (params, build_tree_list (NULL_TREE, parm));
21863 token = cp_lexer_peek_token (parser->lexer);
21866 /* We allow tail attributes for the method. */
21867 if (token->keyword == RID_ATTRIBUTE)
21869 if (*attributes == NULL_TREE)
21871 *attributes = cp_parser_attributes_opt (parser);
21872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21873 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21877 /* We have an error, but parse the attributes, so that we can
21879 *attributes = cp_parser_attributes_opt (parser);
21881 cp_parser_error (parser,
21882 "method attributes must be specified at the end");
21883 return error_mark_node;
21889 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21892 cp_parser_objc_interstitial_code (cp_parser* parser)
21894 cp_token *token = cp_lexer_peek_token (parser->lexer);
21896 /* If the next token is `extern' and the following token is a string
21897 literal, then we have a linkage specification. */
21898 if (token->keyword == RID_EXTERN
21899 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21900 cp_parser_linkage_specification (parser);
21901 /* Handle #pragma, if any. */
21902 else if (token->type == CPP_PRAGMA)
21903 cp_parser_pragma (parser, pragma_external);
21904 /* Allow stray semicolons. */
21905 else if (token->type == CPP_SEMICOLON)
21906 cp_lexer_consume_token (parser->lexer);
21907 /* Mark methods as optional or required, when building protocols. */
21908 else if (token->keyword == RID_AT_OPTIONAL)
21910 cp_lexer_consume_token (parser->lexer);
21911 objc_set_method_opt (true);
21913 else if (token->keyword == RID_AT_REQUIRED)
21915 cp_lexer_consume_token (parser->lexer);
21916 objc_set_method_opt (false);
21918 else if (token->keyword == RID_NAMESPACE)
21919 cp_parser_namespace_definition (parser);
21920 /* Other stray characters must generate errors. */
21921 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21923 cp_lexer_consume_token (parser->lexer);
21924 error ("stray `%s' between Objective-C++ methods",
21925 token->type == CPP_OPEN_BRACE ? "{" : "}");
21927 /* Finally, try to parse a block-declaration, or a function-definition. */
21929 cp_parser_block_declaration (parser, /*statement_p=*/false);
21932 /* Parse a method signature. */
21935 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21937 tree rettype, kwdparms, optparms;
21938 bool ellipsis = false;
21939 bool is_class_method;
21941 is_class_method = cp_parser_objc_method_type (parser);
21942 rettype = cp_parser_objc_typename (parser);
21943 *attributes = NULL_TREE;
21944 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21945 if (kwdparms == error_mark_node)
21946 return error_mark_node;
21947 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21948 if (optparms == error_mark_node)
21949 return error_mark_node;
21951 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
21955 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21958 cp_lexer_save_tokens (parser->lexer);
21959 tattr = cp_parser_attributes_opt (parser);
21960 gcc_assert (tattr) ;
21962 /* If the attributes are followed by a method introducer, this is not allowed.
21963 Dump the attributes and flag the situation. */
21964 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21965 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21968 /* Otherwise, the attributes introduce some interstitial code, possibly so
21969 rewind to allow that check. */
21970 cp_lexer_rollback_tokens (parser->lexer);
21974 /* Parse an Objective-C method prototype list. */
21977 cp_parser_objc_method_prototype_list (cp_parser* parser)
21979 cp_token *token = cp_lexer_peek_token (parser->lexer);
21981 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21983 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21985 tree attributes, sig;
21986 bool is_class_method;
21987 if (token->type == CPP_PLUS)
21988 is_class_method = true;
21990 is_class_method = false;
21991 sig = cp_parser_objc_method_signature (parser, &attributes);
21992 if (sig == error_mark_node)
21994 cp_parser_skip_to_end_of_block_or_statement (parser);
21995 token = cp_lexer_peek_token (parser->lexer);
21998 objc_add_method_declaration (is_class_method, sig, attributes);
21999 cp_parser_consume_semicolon_at_end_of_statement (parser);
22001 else if (token->keyword == RID_AT_PROPERTY)
22002 cp_parser_objc_at_property_declaration (parser);
22003 else if (token->keyword == RID_ATTRIBUTE
22004 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22005 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22007 "prefix attributes are ignored for methods");
22009 /* Allow for interspersed non-ObjC++ code. */
22010 cp_parser_objc_interstitial_code (parser);
22012 token = cp_lexer_peek_token (parser->lexer);
22015 if (token->type != CPP_EOF)
22016 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22018 cp_parser_error (parser, "expected %<@end%>");
22020 objc_finish_interface ();
22023 /* Parse an Objective-C method definition list. */
22026 cp_parser_objc_method_definition_list (cp_parser* parser)
22028 cp_token *token = cp_lexer_peek_token (parser->lexer);
22030 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22034 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22037 tree sig, attribute;
22038 bool is_class_method;
22039 if (token->type == CPP_PLUS)
22040 is_class_method = true;
22042 is_class_method = false;
22043 push_deferring_access_checks (dk_deferred);
22044 sig = cp_parser_objc_method_signature (parser, &attribute);
22045 if (sig == error_mark_node)
22047 cp_parser_skip_to_end_of_block_or_statement (parser);
22048 token = cp_lexer_peek_token (parser->lexer);
22051 objc_start_method_definition (is_class_method, sig, attribute);
22053 /* For historical reasons, we accept an optional semicolon. */
22054 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22055 cp_lexer_consume_token (parser->lexer);
22057 ptk = cp_lexer_peek_token (parser->lexer);
22058 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22059 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22061 perform_deferred_access_checks ();
22062 stop_deferring_access_checks ();
22063 meth = cp_parser_function_definition_after_declarator (parser,
22065 pop_deferring_access_checks ();
22066 objc_finish_method_definition (meth);
22069 /* The following case will be removed once @synthesize is
22070 completely implemented. */
22071 else if (token->keyword == RID_AT_PROPERTY)
22072 cp_parser_objc_at_property_declaration (parser);
22073 else if (token->keyword == RID_AT_SYNTHESIZE)
22074 cp_parser_objc_at_synthesize_declaration (parser);
22075 else if (token->keyword == RID_AT_DYNAMIC)
22076 cp_parser_objc_at_dynamic_declaration (parser);
22077 else if (token->keyword == RID_ATTRIBUTE
22078 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22079 warning_at (token->location, OPT_Wattributes,
22080 "prefix attributes are ignored for methods");
22082 /* Allow for interspersed non-ObjC++ code. */
22083 cp_parser_objc_interstitial_code (parser);
22085 token = cp_lexer_peek_token (parser->lexer);
22088 if (token->type != CPP_EOF)
22089 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22091 cp_parser_error (parser, "expected %<@end%>");
22093 objc_finish_implementation ();
22096 /* Parse Objective-C ivars. */
22099 cp_parser_objc_class_ivars (cp_parser* parser)
22101 cp_token *token = cp_lexer_peek_token (parser->lexer);
22103 if (token->type != CPP_OPEN_BRACE)
22104 return; /* No ivars specified. */
22106 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22107 token = cp_lexer_peek_token (parser->lexer);
22109 while (token->type != CPP_CLOSE_BRACE
22110 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22112 cp_decl_specifier_seq declspecs;
22113 int decl_class_or_enum_p;
22114 tree prefix_attributes;
22116 cp_parser_objc_visibility_spec (parser);
22118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22121 cp_parser_decl_specifier_seq (parser,
22122 CP_PARSER_FLAGS_OPTIONAL,
22124 &decl_class_or_enum_p);
22126 /* auto, register, static, extern, mutable. */
22127 if (declspecs.storage_class != sc_none)
22129 cp_parser_error (parser, "invalid type for instance variable");
22130 declspecs.storage_class = sc_none;
22134 if (declspecs.specs[(int) ds_thread])
22136 cp_parser_error (parser, "invalid type for instance variable");
22137 declspecs.specs[(int) ds_thread] = 0;
22141 if (declspecs.specs[(int) ds_typedef])
22143 cp_parser_error (parser, "invalid type for instance variable");
22144 declspecs.specs[(int) ds_typedef] = 0;
22147 prefix_attributes = declspecs.attributes;
22148 declspecs.attributes = NULL_TREE;
22150 /* Keep going until we hit the `;' at the end of the
22152 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22154 tree width = NULL_TREE, attributes, first_attribute, decl;
22155 cp_declarator *declarator = NULL;
22156 int ctor_dtor_or_conv_p;
22158 /* Check for a (possibly unnamed) bitfield declaration. */
22159 token = cp_lexer_peek_token (parser->lexer);
22160 if (token->type == CPP_COLON)
22163 if (token->type == CPP_NAME
22164 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22167 /* Get the name of the bitfield. */
22168 declarator = make_id_declarator (NULL_TREE,
22169 cp_parser_identifier (parser),
22173 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22174 /* Get the width of the bitfield. */
22176 = cp_parser_constant_expression (parser,
22177 /*allow_non_constant=*/false,
22182 /* Parse the declarator. */
22184 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22185 &ctor_dtor_or_conv_p,
22186 /*parenthesized_p=*/NULL,
22187 /*member_p=*/false);
22190 /* Look for attributes that apply to the ivar. */
22191 attributes = cp_parser_attributes_opt (parser);
22192 /* Remember which attributes are prefix attributes and
22194 first_attribute = attributes;
22195 /* Combine the attributes. */
22196 attributes = chainon (prefix_attributes, attributes);
22199 /* Create the bitfield declaration. */
22200 decl = grokbitfield (declarator, &declspecs,
22204 decl = grokfield (declarator, &declspecs,
22205 NULL_TREE, /*init_const_expr_p=*/false,
22206 NULL_TREE, attributes);
22208 /* Add the instance variable. */
22209 objc_add_instance_variable (decl);
22211 /* Reset PREFIX_ATTRIBUTES. */
22212 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22213 attributes = TREE_CHAIN (attributes);
22215 TREE_CHAIN (attributes) = NULL_TREE;
22217 token = cp_lexer_peek_token (parser->lexer);
22219 if (token->type == CPP_COMMA)
22221 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22227 cp_parser_consume_semicolon_at_end_of_statement (parser);
22228 token = cp_lexer_peek_token (parser->lexer);
22231 if (token->keyword == RID_AT_END)
22232 cp_parser_error (parser, "expected %<}%>");
22234 /* Do not consume the RID_AT_END, so it will be read again as terminating
22235 the @interface of @implementation. */
22236 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22237 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22239 /* For historical reasons, we accept an optional semicolon. */
22240 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22241 cp_lexer_consume_token (parser->lexer);
22244 /* Parse an Objective-C protocol declaration. */
22247 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22249 tree proto, protorefs;
22252 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22255 tok = cp_lexer_peek_token (parser->lexer);
22256 error_at (tok->location, "identifier expected after %<@protocol%>");
22260 /* See if we have a forward declaration or a definition. */
22261 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22263 /* Try a forward declaration first. */
22264 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22266 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22268 cp_parser_consume_semicolon_at_end_of_statement (parser);
22271 /* Ok, we got a full-fledged definition (or at least should). */
22274 proto = cp_parser_identifier (parser);
22275 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22276 objc_start_protocol (proto, protorefs, attributes);
22277 cp_parser_objc_method_prototype_list (parser);
22281 /* Parse an Objective-C superclass or category. */
22284 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22287 cp_token *next = cp_lexer_peek_token (parser->lexer);
22289 *super = *categ = NULL_TREE;
22290 if (next->type == CPP_COLON)
22292 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22293 *super = cp_parser_identifier (parser);
22295 else if (next->type == CPP_OPEN_PAREN)
22297 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22298 *categ = cp_parser_identifier (parser);
22299 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22303 /* Parse an Objective-C class interface. */
22306 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22308 tree name, super, categ, protos;
22310 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22311 name = cp_parser_identifier (parser);
22312 if (name == error_mark_node)
22314 /* It's hard to recover because even if valid @interface stuff
22315 is to follow, we can't compile it (or validate it) if we
22316 don't even know which class it refers to. Let's assume this
22317 was a stray '@interface' token in the stream and skip it.
22321 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22322 protos = cp_parser_objc_protocol_refs_opt (parser);
22324 /* We have either a class or a category on our hands. */
22326 objc_start_category_interface (name, categ, protos, attributes);
22329 objc_start_class_interface (name, super, protos, attributes);
22330 /* Handle instance variable declarations, if any. */
22331 cp_parser_objc_class_ivars (parser);
22332 objc_continue_interface ();
22335 cp_parser_objc_method_prototype_list (parser);
22338 /* Parse an Objective-C class implementation. */
22341 cp_parser_objc_class_implementation (cp_parser* parser)
22343 tree name, super, categ;
22345 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22346 name = cp_parser_identifier (parser);
22347 if (name == error_mark_node)
22349 /* It's hard to recover because even if valid @implementation
22350 stuff is to follow, we can't compile it (or validate it) if
22351 we don't even know which class it refers to. Let's assume
22352 this was a stray '@implementation' token in the stream and
22357 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22359 /* We have either a class or a category on our hands. */
22361 objc_start_category_implementation (name, categ);
22364 objc_start_class_implementation (name, super);
22365 /* Handle instance variable declarations, if any. */
22366 cp_parser_objc_class_ivars (parser);
22367 objc_continue_implementation ();
22370 cp_parser_objc_method_definition_list (parser);
22373 /* Consume the @end token and finish off the implementation. */
22376 cp_parser_objc_end_implementation (cp_parser* parser)
22378 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22379 objc_finish_implementation ();
22382 /* Parse an Objective-C declaration. */
22385 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22387 /* Try to figure out what kind of declaration is present. */
22388 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22391 switch (kwd->keyword)
22396 error_at (kwd->location, "attributes may not be specified before"
22397 " the %<@%D%> Objective-C++ keyword",
22401 case RID_AT_IMPLEMENTATION:
22402 warning_at (kwd->location, OPT_Wattributes,
22403 "prefix attributes are ignored before %<@%D%>",
22410 switch (kwd->keyword)
22413 cp_parser_objc_alias_declaration (parser);
22416 cp_parser_objc_class_declaration (parser);
22418 case RID_AT_PROTOCOL:
22419 cp_parser_objc_protocol_declaration (parser, attributes);
22421 case RID_AT_INTERFACE:
22422 cp_parser_objc_class_interface (parser, attributes);
22424 case RID_AT_IMPLEMENTATION:
22425 cp_parser_objc_class_implementation (parser);
22428 cp_parser_objc_end_implementation (parser);
22431 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22433 cp_parser_skip_to_end_of_block_or_statement (parser);
22437 /* Parse an Objective-C try-catch-finally statement.
22439 objc-try-catch-finally-stmt:
22440 @try compound-statement objc-catch-clause-seq [opt]
22441 objc-finally-clause [opt]
22443 objc-catch-clause-seq:
22444 objc-catch-clause objc-catch-clause-seq [opt]
22447 @catch ( exception-declaration ) compound-statement
22449 objc-finally-clause
22450 @finally compound-statement
22452 Returns NULL_TREE. */
22455 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22456 location_t location;
22459 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22460 location = cp_lexer_peek_token (parser->lexer)->location;
22461 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22462 node, lest it get absorbed into the surrounding block. */
22463 stmt = push_stmt_list ();
22464 cp_parser_compound_statement (parser, NULL, false);
22465 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22467 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22469 cp_parameter_declarator *parmdecl;
22472 cp_lexer_consume_token (parser->lexer);
22473 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22474 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22475 parm = grokdeclarator (parmdecl->declarator,
22476 &parmdecl->decl_specifiers,
22477 PARM, /*initialized=*/0,
22478 /*attrlist=*/NULL);
22479 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22480 objc_begin_catch_clause (parm);
22481 cp_parser_compound_statement (parser, NULL, false);
22482 objc_finish_catch_clause ();
22485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22487 cp_lexer_consume_token (parser->lexer);
22488 location = cp_lexer_peek_token (parser->lexer)->location;
22489 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22490 node, lest it get absorbed into the surrounding block. */
22491 stmt = push_stmt_list ();
22492 cp_parser_compound_statement (parser, NULL, false);
22493 objc_build_finally_clause (location, pop_stmt_list (stmt));
22496 return objc_finish_try_stmt ();
22499 /* Parse an Objective-C synchronized statement.
22501 objc-synchronized-stmt:
22502 @synchronized ( expression ) compound-statement
22504 Returns NULL_TREE. */
22507 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22508 location_t location;
22511 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22513 location = cp_lexer_peek_token (parser->lexer)->location;
22514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22515 lock = cp_parser_expression (parser, false, NULL);
22516 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22518 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22519 node, lest it get absorbed into the surrounding block. */
22520 stmt = push_stmt_list ();
22521 cp_parser_compound_statement (parser, NULL, false);
22523 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22526 /* Parse an Objective-C throw statement.
22529 @throw assignment-expression [opt] ;
22531 Returns a constructed '@throw' statement. */
22534 cp_parser_objc_throw_statement (cp_parser *parser) {
22535 tree expr = NULL_TREE;
22536 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22538 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22541 expr = cp_parser_assignment_expression (parser, false, NULL);
22543 cp_parser_consume_semicolon_at_end_of_statement (parser);
22545 return objc_build_throw_stmt (loc, expr);
22548 /* Parse an Objective-C statement. */
22551 cp_parser_objc_statement (cp_parser * parser) {
22552 /* Try to figure out what kind of declaration is present. */
22553 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22555 switch (kwd->keyword)
22558 return cp_parser_objc_try_catch_finally_statement (parser);
22559 case RID_AT_SYNCHRONIZED:
22560 return cp_parser_objc_synchronized_statement (parser);
22562 return cp_parser_objc_throw_statement (parser);
22564 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22566 cp_parser_skip_to_end_of_block_or_statement (parser);
22569 return error_mark_node;
22572 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22573 look ahead to see if an objc keyword follows the attributes. This
22574 is to detect the use of prefix attributes on ObjC @interface and
22578 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22580 cp_lexer_save_tokens (parser->lexer);
22581 *attrib = cp_parser_attributes_opt (parser);
22582 gcc_assert (*attrib);
22583 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22585 cp_lexer_commit_tokens (parser->lexer);
22588 cp_lexer_rollback_tokens (parser->lexer);
22592 /* This routine is a minimal replacement for
22593 c_parser_struct_declaration () used when parsing the list of
22594 types/names or ObjC++ properties. For example, when parsing the
22597 @property (readonly) int a, b, c;
22599 this function is responsible for parsing "int a, int b, int c" and
22600 returning the declarations as CHAIN of DECLs.
22602 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22603 similar parsing. */
22605 cp_parser_objc_struct_declaration (cp_parser *parser)
22607 tree decls = NULL_TREE;
22608 cp_decl_specifier_seq declspecs;
22609 int decl_class_or_enum_p;
22610 tree prefix_attributes;
22612 cp_parser_decl_specifier_seq (parser,
22613 CP_PARSER_FLAGS_NONE,
22615 &decl_class_or_enum_p);
22617 if (declspecs.type == error_mark_node)
22618 return error_mark_node;
22620 /* auto, register, static, extern, mutable. */
22621 if (declspecs.storage_class != sc_none)
22623 cp_parser_error (parser, "invalid type for property");
22624 declspecs.storage_class = sc_none;
22628 if (declspecs.specs[(int) ds_thread])
22630 cp_parser_error (parser, "invalid type for property");
22631 declspecs.specs[(int) ds_thread] = 0;
22635 if (declspecs.specs[(int) ds_typedef])
22637 cp_parser_error (parser, "invalid type for property");
22638 declspecs.specs[(int) ds_typedef] = 0;
22641 prefix_attributes = declspecs.attributes;
22642 declspecs.attributes = NULL_TREE;
22644 /* Keep going until we hit the `;' at the end of the declaration. */
22645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22647 tree attributes, first_attribute, decl;
22648 cp_declarator *declarator;
22651 /* Parse the declarator. */
22652 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22653 NULL, NULL, false);
22655 /* Look for attributes that apply to the ivar. */
22656 attributes = cp_parser_attributes_opt (parser);
22657 /* Remember which attributes are prefix attributes and
22659 first_attribute = attributes;
22660 /* Combine the attributes. */
22661 attributes = chainon (prefix_attributes, attributes);
22663 decl = grokfield (declarator, &declspecs,
22664 NULL_TREE, /*init_const_expr_p=*/false,
22665 NULL_TREE, attributes);
22667 if (decl == error_mark_node || decl == NULL_TREE)
22668 return error_mark_node;
22670 /* Reset PREFIX_ATTRIBUTES. */
22671 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22672 attributes = TREE_CHAIN (attributes);
22674 TREE_CHAIN (attributes) = NULL_TREE;
22676 DECL_CHAIN (decl) = decls;
22679 token = cp_lexer_peek_token (parser->lexer);
22680 if (token->type == CPP_COMMA)
22682 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22691 /* Parse an Objective-C @property declaration. The syntax is:
22693 objc-property-declaration:
22694 '@property' objc-property-attributes[opt] struct-declaration ;
22696 objc-property-attributes:
22697 '(' objc-property-attribute-list ')'
22699 objc-property-attribute-list:
22700 objc-property-attribute
22701 objc-property-attribute-list, objc-property-attribute
22703 objc-property-attribute
22704 'getter' = identifier
22705 'setter' = identifier
22714 @property NSString *name;
22715 @property (readonly) id object;
22716 @property (retain, nonatomic, getter=getTheName) id name;
22717 @property int a, b, c;
22719 PS: This function is identical to
22720 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22722 cp_parser_objc_at_property_declaration (cp_parser *parser)
22724 /* The following variables hold the attributes of the properties as
22725 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22726 seen. When we see an attribute, we set them to 'true' (if they
22727 are boolean properties) or to the identifier (if they have an
22728 argument, ie, for getter and setter). Note that here we only
22729 parse the list of attributes, check the syntax and accumulate the
22730 attributes that we find. objc_add_property_declaration() will
22731 then process the information. */
22732 bool property_assign = false;
22733 bool property_copy = false;
22734 tree property_getter_ident = NULL_TREE;
22735 bool property_nonatomic = false;
22736 bool property_readonly = false;
22737 bool property_readwrite = false;
22738 bool property_retain = false;
22739 tree property_setter_ident = NULL_TREE;
22741 /* 'properties' is the list of properties that we read. Usually a
22742 single one, but maybe more (eg, in "@property int a, b, c;" there
22747 loc = cp_lexer_peek_token (parser->lexer)->location;
22749 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22751 /* Parse the optional attribute list... */
22752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22755 cp_lexer_consume_token (parser->lexer);
22759 bool syntax_error = false;
22760 cp_token *token = cp_lexer_peek_token (parser->lexer);
22763 if (token->type != CPP_NAME)
22765 cp_parser_error (parser, "expected identifier");
22768 keyword = C_RID_CODE (token->u.value);
22769 cp_lexer_consume_token (parser->lexer);
22772 case RID_ASSIGN: property_assign = true; break;
22773 case RID_COPY: property_copy = true; break;
22774 case RID_NONATOMIC: property_nonatomic = true; break;
22775 case RID_READONLY: property_readonly = true; break;
22776 case RID_READWRITE: property_readwrite = true; break;
22777 case RID_RETAIN: property_retain = true; break;
22781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22783 cp_parser_error (parser,
22784 "getter/setter/ivar attribute must be followed by %<=%>");
22785 syntax_error = true;
22788 cp_lexer_consume_token (parser->lexer); /* eat the = */
22789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22791 cp_parser_error (parser, "expected identifier");
22792 syntax_error = true;
22795 if (keyword == RID_SETTER)
22797 if (property_setter_ident != NULL_TREE)
22798 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22800 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22801 cp_lexer_consume_token (parser->lexer);
22802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22803 cp_parser_error (parser, "setter name must terminate with %<:%>");
22805 cp_lexer_consume_token (parser->lexer);
22809 if (property_getter_ident != NULL_TREE)
22810 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22812 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22813 cp_lexer_consume_token (parser->lexer);
22817 cp_parser_error (parser, "unknown property attribute");
22818 syntax_error = true;
22825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22826 cp_lexer_consume_token (parser->lexer);
22831 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22833 cp_parser_skip_to_closing_parenthesis (parser,
22834 /*recovering=*/true,
22835 /*or_comma=*/false,
22836 /*consume_paren=*/true);
22840 /* ... and the property declaration(s). */
22841 properties = cp_parser_objc_struct_declaration (parser);
22843 if (properties == error_mark_node)
22845 cp_parser_skip_to_end_of_statement (parser);
22846 /* If the next token is now a `;', consume it. */
22847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22848 cp_lexer_consume_token (parser->lexer);
22852 if (properties == NULL_TREE)
22853 cp_parser_error (parser, "expected identifier");
22856 /* Comma-separated properties are chained together in
22857 reverse order; add them one by one. */
22858 properties = nreverse (properties);
22860 for (; properties; properties = TREE_CHAIN (properties))
22861 objc_add_property_declaration (loc, copy_node (properties),
22862 property_readonly, property_readwrite,
22863 property_assign, property_retain,
22864 property_copy, property_nonatomic,
22865 property_getter_ident, property_setter_ident);
22868 cp_parser_consume_semicolon_at_end_of_statement (parser);
22871 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22873 objc-synthesize-declaration:
22874 @synthesize objc-synthesize-identifier-list ;
22876 objc-synthesize-identifier-list:
22877 objc-synthesize-identifier
22878 objc-synthesize-identifier-list, objc-synthesize-identifier
22880 objc-synthesize-identifier
22882 identifier = identifier
22885 @synthesize MyProperty;
22886 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22888 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22889 for C. Keep them in sync.
22892 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22894 tree list = NULL_TREE;
22896 loc = cp_lexer_peek_token (parser->lexer)->location;
22898 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
22901 tree property, ivar;
22902 property = cp_parser_identifier (parser);
22903 if (property == error_mark_node)
22905 cp_parser_consume_semicolon_at_end_of_statement (parser);
22908 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22910 cp_lexer_consume_token (parser->lexer);
22911 ivar = cp_parser_identifier (parser);
22912 if (ivar == error_mark_node)
22914 cp_parser_consume_semicolon_at_end_of_statement (parser);
22920 list = chainon (list, build_tree_list (ivar, property));
22921 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22922 cp_lexer_consume_token (parser->lexer);
22926 cp_parser_consume_semicolon_at_end_of_statement (parser);
22927 objc_add_synthesize_declaration (loc, list);
22930 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22932 objc-dynamic-declaration:
22933 @dynamic identifier-list ;
22936 @dynamic MyProperty;
22937 @dynamic MyProperty, AnotherProperty;
22939 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22940 for C. Keep them in sync.
22943 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22945 tree list = NULL_TREE;
22947 loc = cp_lexer_peek_token (parser->lexer)->location;
22949 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
22953 property = cp_parser_identifier (parser);
22954 if (property == error_mark_node)
22956 cp_parser_consume_semicolon_at_end_of_statement (parser);
22959 list = chainon (list, build_tree_list (NULL, property));
22960 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22961 cp_lexer_consume_token (parser->lexer);
22965 cp_parser_consume_semicolon_at_end_of_statement (parser);
22966 objc_add_dynamic_declaration (loc, list);
22970 /* OpenMP 2.5 parsing routines. */
22972 /* Returns name of the next clause.
22973 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22974 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22975 returned and the token is consumed. */
22977 static pragma_omp_clause
22978 cp_parser_omp_clause_name (cp_parser *parser)
22980 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22983 result = PRAGMA_OMP_CLAUSE_IF;
22984 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22985 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22986 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22987 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22988 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22990 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22991 const char *p = IDENTIFIER_POINTER (id);
22996 if (!strcmp ("collapse", p))
22997 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22998 else if (!strcmp ("copyin", p))
22999 result = PRAGMA_OMP_CLAUSE_COPYIN;
23000 else if (!strcmp ("copyprivate", p))
23001 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23004 if (!strcmp ("firstprivate", p))
23005 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23008 if (!strcmp ("lastprivate", p))
23009 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23012 if (!strcmp ("nowait", p))
23013 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23014 else if (!strcmp ("num_threads", p))
23015 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23018 if (!strcmp ("ordered", p))
23019 result = PRAGMA_OMP_CLAUSE_ORDERED;
23022 if (!strcmp ("reduction", p))
23023 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23026 if (!strcmp ("schedule", p))
23027 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23028 else if (!strcmp ("shared", p))
23029 result = PRAGMA_OMP_CLAUSE_SHARED;
23032 if (!strcmp ("untied", p))
23033 result = PRAGMA_OMP_CLAUSE_UNTIED;
23038 if (result != PRAGMA_OMP_CLAUSE_NONE)
23039 cp_lexer_consume_token (parser->lexer);
23044 /* Validate that a clause of the given type does not already exist. */
23047 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23048 const char *name, location_t location)
23052 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23053 if (OMP_CLAUSE_CODE (c) == code)
23055 error_at (location, "too many %qs clauses", name);
23063 variable-list , identifier
23065 In addition, we match a closing parenthesis. An opening parenthesis
23066 will have been consumed by the caller.
23068 If KIND is nonzero, create the appropriate node and install the decl
23069 in OMP_CLAUSE_DECL and add the node to the head of the list.
23071 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23072 return the list created. */
23075 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23083 token = cp_lexer_peek_token (parser->lexer);
23084 name = cp_parser_id_expression (parser, /*template_p=*/false,
23085 /*check_dependency_p=*/true,
23086 /*template_p=*/NULL,
23087 /*declarator_p=*/false,
23088 /*optional_p=*/false);
23089 if (name == error_mark_node)
23092 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23093 if (decl == error_mark_node)
23094 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23096 else if (kind != 0)
23098 tree u = build_omp_clause (token->location, kind);
23099 OMP_CLAUSE_DECL (u) = decl;
23100 OMP_CLAUSE_CHAIN (u) = list;
23104 list = tree_cons (decl, NULL_TREE, list);
23107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23109 cp_lexer_consume_token (parser->lexer);
23112 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23116 /* Try to resync to an unnested comma. Copied from
23117 cp_parser_parenthesized_expression_list. */
23119 ending = cp_parser_skip_to_closing_parenthesis (parser,
23120 /*recovering=*/true,
23122 /*consume_paren=*/true);
23130 /* Similarly, but expect leading and trailing parenthesis. This is a very
23131 common case for omp clauses. */
23134 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23136 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23137 return cp_parser_omp_var_list_no_open (parser, kind, list);
23142 collapse ( constant-expression ) */
23145 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23151 loc = cp_lexer_peek_token (parser->lexer)->location;
23152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23155 num = cp_parser_constant_expression (parser, false, NULL);
23157 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23159 /*or_comma=*/false,
23160 /*consume_paren=*/true);
23162 if (num == error_mark_node)
23164 num = fold_non_dependent_expr (num);
23165 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23166 || !host_integerp (num, 0)
23167 || (n = tree_low_cst (num, 0)) <= 0
23170 error_at (loc, "collapse argument needs positive constant integer expression");
23174 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23175 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23176 OMP_CLAUSE_CHAIN (c) = list;
23177 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23183 default ( shared | none ) */
23186 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23188 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23191 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23193 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23195 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23196 const char *p = IDENTIFIER_POINTER (id);
23201 if (strcmp ("none", p) != 0)
23203 kind = OMP_CLAUSE_DEFAULT_NONE;
23207 if (strcmp ("shared", p) != 0)
23209 kind = OMP_CLAUSE_DEFAULT_SHARED;
23216 cp_lexer_consume_token (parser->lexer);
23221 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23224 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23225 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23226 /*or_comma=*/false,
23227 /*consume_paren=*/true);
23229 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23232 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23233 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23234 OMP_CLAUSE_CHAIN (c) = list;
23235 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23241 if ( expression ) */
23244 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23248 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23251 t = cp_parser_condition (parser);
23253 if (t == error_mark_node
23254 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23256 /*or_comma=*/false,
23257 /*consume_paren=*/true);
23259 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23261 c = build_omp_clause (location, OMP_CLAUSE_IF);
23262 OMP_CLAUSE_IF_EXPR (c) = t;
23263 OMP_CLAUSE_CHAIN (c) = list;
23272 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23273 tree list, location_t location)
23277 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23279 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23280 OMP_CLAUSE_CHAIN (c) = list;
23285 num_threads ( expression ) */
23288 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23289 location_t location)
23293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23296 t = cp_parser_expression (parser, false, NULL);
23298 if (t == error_mark_node
23299 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23301 /*or_comma=*/false,
23302 /*consume_paren=*/true);
23304 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23305 "num_threads", location);
23307 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23308 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23309 OMP_CLAUSE_CHAIN (c) = list;
23318 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23319 tree list, location_t location)
23323 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23324 "ordered", location);
23326 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23327 OMP_CLAUSE_CHAIN (c) = list;
23332 reduction ( reduction-operator : variable-list )
23334 reduction-operator:
23335 One of: + * - & ^ | && || */
23338 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23340 enum tree_code code;
23343 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23346 switch (cp_lexer_peek_token (parser->lexer)->type)
23358 code = BIT_AND_EXPR;
23361 code = BIT_XOR_EXPR;
23364 code = BIT_IOR_EXPR;
23367 code = TRUTH_ANDIF_EXPR;
23370 code = TRUTH_ORIF_EXPR;
23373 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23374 "%<|%>, %<&&%>, or %<||%>");
23376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23377 /*or_comma=*/false,
23378 /*consume_paren=*/true);
23381 cp_lexer_consume_token (parser->lexer);
23383 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23386 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23387 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23388 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23394 schedule ( schedule-kind )
23395 schedule ( schedule-kind , expression )
23398 static | dynamic | guided | runtime | auto */
23401 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23408 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23412 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23413 const char *p = IDENTIFIER_POINTER (id);
23418 if (strcmp ("dynamic", p) != 0)
23420 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23424 if (strcmp ("guided", p) != 0)
23426 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23430 if (strcmp ("runtime", p) != 0)
23432 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23439 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23440 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23441 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23442 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23445 cp_lexer_consume_token (parser->lexer);
23447 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23450 cp_lexer_consume_token (parser->lexer);
23452 token = cp_lexer_peek_token (parser->lexer);
23453 t = cp_parser_assignment_expression (parser, false, NULL);
23455 if (t == error_mark_node)
23457 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23458 error_at (token->location, "schedule %<runtime%> does not take "
23459 "a %<chunk_size%> parameter");
23460 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23461 error_at (token->location, "schedule %<auto%> does not take "
23462 "a %<chunk_size%> parameter");
23464 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23466 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23469 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23472 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23473 OMP_CLAUSE_CHAIN (c) = list;
23477 cp_parser_error (parser, "invalid schedule kind");
23479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23480 /*or_comma=*/false,
23481 /*consume_paren=*/true);
23489 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23490 tree list, location_t location)
23494 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23496 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23497 OMP_CLAUSE_CHAIN (c) = list;
23501 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23502 is a bitmask in MASK. Return the list of clauses found; the result
23503 of clause default goes in *pdefault. */
23506 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23507 const char *where, cp_token *pragma_tok)
23509 tree clauses = NULL;
23511 cp_token *token = NULL;
23513 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23515 pragma_omp_clause c_kind;
23516 const char *c_name;
23517 tree prev = clauses;
23519 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23520 cp_lexer_consume_token (parser->lexer);
23522 token = cp_lexer_peek_token (parser->lexer);
23523 c_kind = cp_parser_omp_clause_name (parser);
23528 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23529 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23531 c_name = "collapse";
23533 case PRAGMA_OMP_CLAUSE_COPYIN:
23534 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23537 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23538 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23540 c_name = "copyprivate";
23542 case PRAGMA_OMP_CLAUSE_DEFAULT:
23543 clauses = cp_parser_omp_clause_default (parser, clauses,
23545 c_name = "default";
23547 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23548 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23550 c_name = "firstprivate";
23552 case PRAGMA_OMP_CLAUSE_IF:
23553 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23556 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23557 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23559 c_name = "lastprivate";
23561 case PRAGMA_OMP_CLAUSE_NOWAIT:
23562 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23565 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23566 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23568 c_name = "num_threads";
23570 case PRAGMA_OMP_CLAUSE_ORDERED:
23571 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23573 c_name = "ordered";
23575 case PRAGMA_OMP_CLAUSE_PRIVATE:
23576 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23578 c_name = "private";
23580 case PRAGMA_OMP_CLAUSE_REDUCTION:
23581 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23582 c_name = "reduction";
23584 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23585 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23587 c_name = "schedule";
23589 case PRAGMA_OMP_CLAUSE_SHARED:
23590 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23594 case PRAGMA_OMP_CLAUSE_UNTIED:
23595 clauses = cp_parser_omp_clause_untied (parser, clauses,
23600 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23604 if (((mask >> c_kind) & 1) == 0)
23606 /* Remove the invalid clause(s) from the list to avoid
23607 confusing the rest of the compiler. */
23609 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23613 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23614 return finish_omp_clauses (clauses);
23621 In practice, we're also interested in adding the statement to an
23622 outer node. So it is convenient if we work around the fact that
23623 cp_parser_statement calls add_stmt. */
23626 cp_parser_begin_omp_structured_block (cp_parser *parser)
23628 unsigned save = parser->in_statement;
23630 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23631 This preserves the "not within loop or switch" style error messages
23632 for nonsense cases like
23638 if (parser->in_statement)
23639 parser->in_statement = IN_OMP_BLOCK;
23645 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23647 parser->in_statement = save;
23651 cp_parser_omp_structured_block (cp_parser *parser)
23653 tree stmt = begin_omp_structured_block ();
23654 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23656 cp_parser_statement (parser, NULL_TREE, false, NULL);
23658 cp_parser_end_omp_structured_block (parser, save);
23659 return finish_omp_structured_block (stmt);
23663 # pragma omp atomic new-line
23667 x binop= expr | x++ | ++x | x-- | --x
23669 +, *, -, /, &, ^, |, <<, >>
23671 where x is an lvalue expression with scalar type. */
23674 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23677 enum tree_code code;
23679 cp_parser_require_pragma_eol (parser, pragma_tok);
23681 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23682 /*cast_p=*/false, NULL);
23683 switch (TREE_CODE (lhs))
23688 case PREINCREMENT_EXPR:
23689 case POSTINCREMENT_EXPR:
23690 lhs = TREE_OPERAND (lhs, 0);
23692 rhs = integer_one_node;
23695 case PREDECREMENT_EXPR:
23696 case POSTDECREMENT_EXPR:
23697 lhs = TREE_OPERAND (lhs, 0);
23699 rhs = integer_one_node;
23702 case COMPOUND_EXPR:
23703 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23704 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23705 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23706 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23707 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23708 (TREE_OPERAND (lhs, 1), 0), 0)))
23710 /* Undo effects of boolean_increment for post {in,de}crement. */
23711 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23714 if (TREE_CODE (lhs) == MODIFY_EXPR
23715 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23717 /* Undo effects of boolean_increment. */
23718 if (integer_onep (TREE_OPERAND (lhs, 1)))
23720 /* This is pre or post increment. */
23721 rhs = TREE_OPERAND (lhs, 1);
23722 lhs = TREE_OPERAND (lhs, 0);
23729 switch (cp_lexer_peek_token (parser->lexer)->type)
23735 code = TRUNC_DIV_EXPR;
23743 case CPP_LSHIFT_EQ:
23744 code = LSHIFT_EXPR;
23746 case CPP_RSHIFT_EQ:
23747 code = RSHIFT_EXPR;
23750 code = BIT_AND_EXPR;
23753 code = BIT_IOR_EXPR;
23756 code = BIT_XOR_EXPR;
23759 cp_parser_error (parser,
23760 "invalid operator for %<#pragma omp atomic%>");
23763 cp_lexer_consume_token (parser->lexer);
23765 rhs = cp_parser_expression (parser, false, NULL);
23766 if (rhs == error_mark_node)
23770 finish_omp_atomic (code, lhs, rhs);
23771 cp_parser_consume_semicolon_at_end_of_statement (parser);
23775 cp_parser_skip_to_end_of_block_or_statement (parser);
23780 # pragma omp barrier new-line */
23783 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23785 cp_parser_require_pragma_eol (parser, pragma_tok);
23786 finish_omp_barrier ();
23790 # pragma omp critical [(name)] new-line
23791 structured-block */
23794 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23796 tree stmt, name = NULL;
23798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23800 cp_lexer_consume_token (parser->lexer);
23802 name = cp_parser_identifier (parser);
23804 if (name == error_mark_node
23805 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23806 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23807 /*or_comma=*/false,
23808 /*consume_paren=*/true);
23809 if (name == error_mark_node)
23812 cp_parser_require_pragma_eol (parser, pragma_tok);
23814 stmt = cp_parser_omp_structured_block (parser);
23815 return c_finish_omp_critical (input_location, stmt, name);
23819 # pragma omp flush flush-vars[opt] new-line
23822 ( variable-list ) */
23825 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23827 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23828 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23829 cp_parser_require_pragma_eol (parser, pragma_tok);
23831 finish_omp_flush ();
23834 /* Helper function, to parse omp for increment expression. */
23837 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23839 tree cond = cp_parser_binary_expression (parser, false, true,
23840 PREC_NOT_OPERATOR, NULL);
23843 if (cond == error_mark_node
23844 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23846 cp_parser_skip_to_end_of_statement (parser);
23847 return error_mark_node;
23850 switch (TREE_CODE (cond))
23858 return error_mark_node;
23861 /* If decl is an iterator, preserve LHS and RHS of the relational
23862 expr until finish_omp_for. */
23864 && (type_dependent_expression_p (decl)
23865 || CLASS_TYPE_P (TREE_TYPE (decl))))
23868 return build_x_binary_op (TREE_CODE (cond),
23869 TREE_OPERAND (cond, 0), ERROR_MARK,
23870 TREE_OPERAND (cond, 1), ERROR_MARK,
23871 &overloaded_p, tf_warning_or_error);
23874 /* Helper function, to parse omp for increment expression. */
23877 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23879 cp_token *token = cp_lexer_peek_token (parser->lexer);
23885 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23887 op = (token->type == CPP_PLUS_PLUS
23888 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23889 cp_lexer_consume_token (parser->lexer);
23890 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23892 return error_mark_node;
23893 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23896 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23898 return error_mark_node;
23900 token = cp_lexer_peek_token (parser->lexer);
23901 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23903 op = (token->type == CPP_PLUS_PLUS
23904 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23905 cp_lexer_consume_token (parser->lexer);
23906 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23909 op = cp_parser_assignment_operator_opt (parser);
23910 if (op == ERROR_MARK)
23911 return error_mark_node;
23913 if (op != NOP_EXPR)
23915 rhs = cp_parser_assignment_expression (parser, false, NULL);
23916 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23917 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23920 lhs = cp_parser_binary_expression (parser, false, false,
23921 PREC_ADDITIVE_EXPRESSION, NULL);
23922 token = cp_lexer_peek_token (parser->lexer);
23923 decl_first = lhs == decl;
23926 if (token->type != CPP_PLUS
23927 && token->type != CPP_MINUS)
23928 return error_mark_node;
23932 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23933 cp_lexer_consume_token (parser->lexer);
23934 rhs = cp_parser_binary_expression (parser, false, false,
23935 PREC_ADDITIVE_EXPRESSION, NULL);
23936 token = cp_lexer_peek_token (parser->lexer);
23937 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23939 if (lhs == NULL_TREE)
23941 if (op == PLUS_EXPR)
23944 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23947 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23948 NULL, tf_warning_or_error);
23951 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23955 if (rhs != decl || op == MINUS_EXPR)
23956 return error_mark_node;
23957 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23960 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23962 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23965 /* Parse the restricted form of the for statement allowed by OpenMP. */
23968 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23970 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23971 tree real_decl, initv, condv, incrv, declv;
23972 tree this_pre_body, cl;
23973 location_t loc_first;
23974 bool collapse_err = false;
23975 int i, collapse = 1, nbraces = 0;
23976 VEC(tree,gc) *for_block = make_tree_vector ();
23978 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23979 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23980 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23982 gcc_assert (collapse >= 1);
23984 declv = make_tree_vec (collapse);
23985 initv = make_tree_vec (collapse);
23986 condv = make_tree_vec (collapse);
23987 incrv = make_tree_vec (collapse);
23989 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23991 for (i = 0; i < collapse; i++)
23993 int bracecount = 0;
23994 bool add_private_clause = false;
23997 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23999 cp_parser_error (parser, "for statement expected");
24002 loc = cp_lexer_consume_token (parser->lexer)->location;
24004 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24007 init = decl = real_decl = NULL;
24008 this_pre_body = push_stmt_list ();
24009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24011 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24015 integer-type var = lb
24016 random-access-iterator-type var = lb
24017 pointer-type var = lb
24019 cp_decl_specifier_seq type_specifiers;
24021 /* First, try to parse as an initialized declaration. See
24022 cp_parser_condition, from whence the bulk of this is copied. */
24024 cp_parser_parse_tentatively (parser);
24025 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24026 /*is_trailing_return=*/false,
24028 if (cp_parser_parse_definitely (parser))
24030 /* If parsing a type specifier seq succeeded, then this
24031 MUST be a initialized declaration. */
24032 tree asm_specification, attributes;
24033 cp_declarator *declarator;
24035 declarator = cp_parser_declarator (parser,
24036 CP_PARSER_DECLARATOR_NAMED,
24037 /*ctor_dtor_or_conv_p=*/NULL,
24038 /*parenthesized_p=*/NULL,
24039 /*member_p=*/false);
24040 attributes = cp_parser_attributes_opt (parser);
24041 asm_specification = cp_parser_asm_specification_opt (parser);
24043 if (declarator == cp_error_declarator)
24044 cp_parser_skip_to_end_of_statement (parser);
24048 tree pushed_scope, auto_node;
24050 decl = start_decl (declarator, &type_specifiers,
24051 SD_INITIALIZED, attributes,
24052 /*prefix_attributes=*/NULL_TREE,
24055 auto_node = type_uses_auto (TREE_TYPE (decl));
24056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24058 if (cp_lexer_next_token_is (parser->lexer,
24060 error ("parenthesized initialization is not allowed in "
24061 "OpenMP %<for%> loop");
24063 /* Trigger an error. */
24064 cp_parser_require (parser, CPP_EQ, RT_EQ);
24066 init = error_mark_node;
24067 cp_parser_skip_to_end_of_statement (parser);
24069 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24070 || type_dependent_expression_p (decl)
24073 bool is_direct_init, is_non_constant_init;
24075 init = cp_parser_initializer (parser,
24077 &is_non_constant_init);
24079 if (auto_node && describable_type (init))
24082 = do_auto_deduction (TREE_TYPE (decl), init,
24085 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24086 && !type_dependent_expression_p (decl))
24090 cp_finish_decl (decl, init, !is_non_constant_init,
24092 LOOKUP_ONLYCONVERTING);
24093 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24095 VEC_safe_push (tree, gc, for_block, this_pre_body);
24099 init = pop_stmt_list (this_pre_body);
24100 this_pre_body = NULL_TREE;
24105 cp_lexer_consume_token (parser->lexer);
24106 init = cp_parser_assignment_expression (parser, false, NULL);
24109 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24110 init = error_mark_node;
24112 cp_finish_decl (decl, NULL_TREE,
24113 /*init_const_expr_p=*/false,
24115 LOOKUP_ONLYCONVERTING);
24119 pop_scope (pushed_scope);
24125 /* If parsing a type specifier sequence failed, then
24126 this MUST be a simple expression. */
24127 cp_parser_parse_tentatively (parser);
24128 decl = cp_parser_primary_expression (parser, false, false,
24130 if (!cp_parser_error_occurred (parser)
24133 && CLASS_TYPE_P (TREE_TYPE (decl)))
24137 cp_parser_parse_definitely (parser);
24138 cp_parser_require (parser, CPP_EQ, RT_EQ);
24139 rhs = cp_parser_assignment_expression (parser, false, NULL);
24140 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24142 tf_warning_or_error));
24143 add_private_clause = true;
24148 cp_parser_abort_tentative_parse (parser);
24149 init = cp_parser_expression (parser, false, NULL);
24152 if (TREE_CODE (init) == MODIFY_EXPR
24153 || TREE_CODE (init) == MODOP_EXPR)
24154 real_decl = TREE_OPERAND (init, 0);
24159 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24162 this_pre_body = pop_stmt_list (this_pre_body);
24166 pre_body = push_stmt_list ();
24168 add_stmt (this_pre_body);
24169 pre_body = pop_stmt_list (pre_body);
24172 pre_body = this_pre_body;
24177 if (par_clauses != NULL && real_decl != NULL_TREE)
24180 for (c = par_clauses; *c ; )
24181 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24182 && OMP_CLAUSE_DECL (*c) == real_decl)
24184 error_at (loc, "iteration variable %qD"
24185 " should not be firstprivate", real_decl);
24186 *c = OMP_CLAUSE_CHAIN (*c);
24188 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24189 && OMP_CLAUSE_DECL (*c) == real_decl)
24191 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24192 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24193 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24194 OMP_CLAUSE_DECL (l) = real_decl;
24195 OMP_CLAUSE_CHAIN (l) = clauses;
24196 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24198 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24199 CP_OMP_CLAUSE_INFO (*c) = NULL;
24200 add_private_clause = false;
24204 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24205 && OMP_CLAUSE_DECL (*c) == real_decl)
24206 add_private_clause = false;
24207 c = &OMP_CLAUSE_CHAIN (*c);
24211 if (add_private_clause)
24214 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24216 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24217 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24218 && OMP_CLAUSE_DECL (c) == decl)
24220 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24221 && OMP_CLAUSE_DECL (c) == decl)
24222 error_at (loc, "iteration variable %qD "
24223 "should not be firstprivate",
24225 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24226 && OMP_CLAUSE_DECL (c) == decl)
24227 error_at (loc, "iteration variable %qD should not be reduction",
24232 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24233 OMP_CLAUSE_DECL (c) = decl;
24234 c = finish_omp_clauses (c);
24237 OMP_CLAUSE_CHAIN (c) = clauses;
24244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24245 cond = cp_parser_omp_for_cond (parser, decl);
24246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24251 /* If decl is an iterator, preserve the operator on decl
24252 until finish_omp_for. */
24254 && (type_dependent_expression_p (decl)
24255 || CLASS_TYPE_P (TREE_TYPE (decl))))
24256 incr = cp_parser_omp_for_incr (parser, decl);
24258 incr = cp_parser_expression (parser, false, NULL);
24261 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24263 /*or_comma=*/false,
24264 /*consume_paren=*/true);
24266 TREE_VEC_ELT (declv, i) = decl;
24267 TREE_VEC_ELT (initv, i) = init;
24268 TREE_VEC_ELT (condv, i) = cond;
24269 TREE_VEC_ELT (incrv, i) = incr;
24271 if (i == collapse - 1)
24274 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24275 in between the collapsed for loops to be still considered perfectly
24276 nested. Hopefully the final version clarifies this.
24277 For now handle (multiple) {'s and empty statements. */
24278 cp_parser_parse_tentatively (parser);
24281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24283 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24285 cp_lexer_consume_token (parser->lexer);
24288 else if (bracecount
24289 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24290 cp_lexer_consume_token (parser->lexer);
24293 loc = cp_lexer_peek_token (parser->lexer)->location;
24294 error_at (loc, "not enough collapsed for loops");
24295 collapse_err = true;
24296 cp_parser_abort_tentative_parse (parser);
24305 cp_parser_parse_definitely (parser);
24306 nbraces += bracecount;
24310 /* Note that we saved the original contents of this flag when we entered
24311 the structured block, and so we don't need to re-save it here. */
24312 parser->in_statement = IN_OMP_FOR;
24314 /* Note that the grammar doesn't call for a structured block here,
24315 though the loop as a whole is a structured block. */
24316 body = push_stmt_list ();
24317 cp_parser_statement (parser, NULL_TREE, false, NULL);
24318 body = pop_stmt_list (body);
24320 if (declv == NULL_TREE)
24323 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24324 pre_body, clauses);
24328 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24330 cp_lexer_consume_token (parser->lexer);
24333 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24334 cp_lexer_consume_token (parser->lexer);
24339 error_at (cp_lexer_peek_token (parser->lexer)->location,
24340 "collapsed loops not perfectly nested");
24342 collapse_err = true;
24343 cp_parser_statement_seq_opt (parser, NULL);
24344 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24349 while (!VEC_empty (tree, for_block))
24350 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24351 release_tree_vector (for_block);
24357 #pragma omp for for-clause[optseq] new-line
24360 #define OMP_FOR_CLAUSE_MASK \
24361 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24362 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24363 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24364 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24365 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24366 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24367 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24368 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24371 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24373 tree clauses, sb, ret;
24376 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24377 "#pragma omp for", pragma_tok);
24379 sb = begin_omp_structured_block ();
24380 save = cp_parser_begin_omp_structured_block (parser);
24382 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24384 cp_parser_end_omp_structured_block (parser, save);
24385 add_stmt (finish_omp_structured_block (sb));
24391 # pragma omp master new-line
24392 structured-block */
24395 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24397 cp_parser_require_pragma_eol (parser, pragma_tok);
24398 return c_finish_omp_master (input_location,
24399 cp_parser_omp_structured_block (parser));
24403 # pragma omp ordered new-line
24404 structured-block */
24407 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24409 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24410 cp_parser_require_pragma_eol (parser, pragma_tok);
24411 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24417 { section-sequence }
24420 section-directive[opt] structured-block
24421 section-sequence section-directive structured-block */
24424 cp_parser_omp_sections_scope (cp_parser *parser)
24426 tree stmt, substmt;
24427 bool error_suppress = false;
24430 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24433 stmt = push_stmt_list ();
24435 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24439 substmt = begin_omp_structured_block ();
24440 save = cp_parser_begin_omp_structured_block (parser);
24444 cp_parser_statement (parser, NULL_TREE, false, NULL);
24446 tok = cp_lexer_peek_token (parser->lexer);
24447 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24449 if (tok->type == CPP_CLOSE_BRACE)
24451 if (tok->type == CPP_EOF)
24455 cp_parser_end_omp_structured_block (parser, save);
24456 substmt = finish_omp_structured_block (substmt);
24457 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24458 add_stmt (substmt);
24463 tok = cp_lexer_peek_token (parser->lexer);
24464 if (tok->type == CPP_CLOSE_BRACE)
24466 if (tok->type == CPP_EOF)
24469 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24471 cp_lexer_consume_token (parser->lexer);
24472 cp_parser_require_pragma_eol (parser, tok);
24473 error_suppress = false;
24475 else if (!error_suppress)
24477 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24478 error_suppress = true;
24481 substmt = cp_parser_omp_structured_block (parser);
24482 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24483 add_stmt (substmt);
24485 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24487 substmt = pop_stmt_list (stmt);
24489 stmt = make_node (OMP_SECTIONS);
24490 TREE_TYPE (stmt) = void_type_node;
24491 OMP_SECTIONS_BODY (stmt) = substmt;
24498 # pragma omp sections sections-clause[optseq] newline
24501 #define OMP_SECTIONS_CLAUSE_MASK \
24502 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24503 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24504 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24505 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24506 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24509 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24513 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24514 "#pragma omp sections", pragma_tok);
24516 ret = cp_parser_omp_sections_scope (parser);
24518 OMP_SECTIONS_CLAUSES (ret) = clauses;
24524 # pragma parallel parallel-clause new-line
24525 # pragma parallel for parallel-for-clause new-line
24526 # pragma parallel sections parallel-sections-clause new-line */
24528 #define OMP_PARALLEL_CLAUSE_MASK \
24529 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24530 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24531 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24532 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24533 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24534 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24535 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24536 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24539 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24541 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24542 const char *p_name = "#pragma omp parallel";
24543 tree stmt, clauses, par_clause, ws_clause, block;
24544 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24546 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24548 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24550 cp_lexer_consume_token (parser->lexer);
24551 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24552 p_name = "#pragma omp parallel for";
24553 mask |= OMP_FOR_CLAUSE_MASK;
24554 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24556 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24559 const char *p = IDENTIFIER_POINTER (id);
24560 if (strcmp (p, "sections") == 0)
24562 cp_lexer_consume_token (parser->lexer);
24563 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24564 p_name = "#pragma omp parallel sections";
24565 mask |= OMP_SECTIONS_CLAUSE_MASK;
24566 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24570 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24571 block = begin_omp_parallel ();
24572 save = cp_parser_begin_omp_structured_block (parser);
24576 case PRAGMA_OMP_PARALLEL:
24577 cp_parser_statement (parser, NULL_TREE, false, NULL);
24578 par_clause = clauses;
24581 case PRAGMA_OMP_PARALLEL_FOR:
24582 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24583 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24586 case PRAGMA_OMP_PARALLEL_SECTIONS:
24587 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24588 stmt = cp_parser_omp_sections_scope (parser);
24590 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24594 gcc_unreachable ();
24597 cp_parser_end_omp_structured_block (parser, save);
24598 stmt = finish_omp_parallel (par_clause, block);
24599 if (p_kind != PRAGMA_OMP_PARALLEL)
24600 OMP_PARALLEL_COMBINED (stmt) = 1;
24605 # pragma omp single single-clause[optseq] new-line
24606 structured-block */
24608 #define OMP_SINGLE_CLAUSE_MASK \
24609 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24610 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24611 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24612 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24615 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24617 tree stmt = make_node (OMP_SINGLE);
24618 TREE_TYPE (stmt) = void_type_node;
24620 OMP_SINGLE_CLAUSES (stmt)
24621 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24622 "#pragma omp single", pragma_tok);
24623 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24625 return add_stmt (stmt);
24629 # pragma omp task task-clause[optseq] new-line
24630 structured-block */
24632 #define OMP_TASK_CLAUSE_MASK \
24633 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24634 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24635 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24636 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24637 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24638 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24641 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24643 tree clauses, block;
24646 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24647 "#pragma omp task", pragma_tok);
24648 block = begin_omp_task ();
24649 save = cp_parser_begin_omp_structured_block (parser);
24650 cp_parser_statement (parser, NULL_TREE, false, NULL);
24651 cp_parser_end_omp_structured_block (parser, save);
24652 return finish_omp_task (clauses, block);
24656 # pragma omp taskwait new-line */
24659 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24661 cp_parser_require_pragma_eol (parser, pragma_tok);
24662 finish_omp_taskwait ();
24666 # pragma omp threadprivate (variable-list) */
24669 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24673 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24674 cp_parser_require_pragma_eol (parser, pragma_tok);
24676 finish_omp_threadprivate (vars);
24679 /* Main entry point to OpenMP statement pragmas. */
24682 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24686 switch (pragma_tok->pragma_kind)
24688 case PRAGMA_OMP_ATOMIC:
24689 cp_parser_omp_atomic (parser, pragma_tok);
24691 case PRAGMA_OMP_CRITICAL:
24692 stmt = cp_parser_omp_critical (parser, pragma_tok);
24694 case PRAGMA_OMP_FOR:
24695 stmt = cp_parser_omp_for (parser, pragma_tok);
24697 case PRAGMA_OMP_MASTER:
24698 stmt = cp_parser_omp_master (parser, pragma_tok);
24700 case PRAGMA_OMP_ORDERED:
24701 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24703 case PRAGMA_OMP_PARALLEL:
24704 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24706 case PRAGMA_OMP_SECTIONS:
24707 stmt = cp_parser_omp_sections (parser, pragma_tok);
24709 case PRAGMA_OMP_SINGLE:
24710 stmt = cp_parser_omp_single (parser, pragma_tok);
24712 case PRAGMA_OMP_TASK:
24713 stmt = cp_parser_omp_task (parser, pragma_tok);
24716 gcc_unreachable ();
24720 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24725 static GTY (()) cp_parser *the_parser;
24728 /* Special handling for the first token or line in the file. The first
24729 thing in the file might be #pragma GCC pch_preprocess, which loads a
24730 PCH file, which is a GC collection point. So we need to handle this
24731 first pragma without benefit of an existing lexer structure.
24733 Always returns one token to the caller in *FIRST_TOKEN. This is
24734 either the true first token of the file, or the first token after
24735 the initial pragma. */
24738 cp_parser_initial_pragma (cp_token *first_token)
24742 cp_lexer_get_preprocessor_token (NULL, first_token);
24743 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24746 cp_lexer_get_preprocessor_token (NULL, first_token);
24747 if (first_token->type == CPP_STRING)
24749 name = first_token->u.value;
24751 cp_lexer_get_preprocessor_token (NULL, first_token);
24752 if (first_token->type != CPP_PRAGMA_EOL)
24753 error_at (first_token->location,
24754 "junk at end of %<#pragma GCC pch_preprocess%>");
24757 error_at (first_token->location, "expected string literal");
24759 /* Skip to the end of the pragma. */
24760 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24761 cp_lexer_get_preprocessor_token (NULL, first_token);
24763 /* Now actually load the PCH file. */
24765 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24767 /* Read one more token to return to our caller. We have to do this
24768 after reading the PCH file in, since its pointers have to be
24770 cp_lexer_get_preprocessor_token (NULL, first_token);
24773 /* Normal parsing of a pragma token. Here we can (and must) use the
24777 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24779 cp_token *pragma_tok;
24782 pragma_tok = cp_lexer_consume_token (parser->lexer);
24783 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24784 parser->lexer->in_pragma = true;
24786 id = pragma_tok->pragma_kind;
24789 case PRAGMA_GCC_PCH_PREPROCESS:
24790 error_at (pragma_tok->location,
24791 "%<#pragma GCC pch_preprocess%> must be first");
24794 case PRAGMA_OMP_BARRIER:
24797 case pragma_compound:
24798 cp_parser_omp_barrier (parser, pragma_tok);
24801 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24802 "used in compound statements");
24809 case PRAGMA_OMP_FLUSH:
24812 case pragma_compound:
24813 cp_parser_omp_flush (parser, pragma_tok);
24816 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24817 "used in compound statements");
24824 case PRAGMA_OMP_TASKWAIT:
24827 case pragma_compound:
24828 cp_parser_omp_taskwait (parser, pragma_tok);
24831 error_at (pragma_tok->location,
24832 "%<#pragma omp taskwait%> may only be "
24833 "used in compound statements");
24840 case PRAGMA_OMP_THREADPRIVATE:
24841 cp_parser_omp_threadprivate (parser, pragma_tok);
24844 case PRAGMA_OMP_ATOMIC:
24845 case PRAGMA_OMP_CRITICAL:
24846 case PRAGMA_OMP_FOR:
24847 case PRAGMA_OMP_MASTER:
24848 case PRAGMA_OMP_ORDERED:
24849 case PRAGMA_OMP_PARALLEL:
24850 case PRAGMA_OMP_SECTIONS:
24851 case PRAGMA_OMP_SINGLE:
24852 case PRAGMA_OMP_TASK:
24853 if (context == pragma_external)
24855 cp_parser_omp_construct (parser, pragma_tok);
24858 case PRAGMA_OMP_SECTION:
24859 error_at (pragma_tok->location,
24860 "%<#pragma omp section%> may only be used in "
24861 "%<#pragma omp sections%> construct");
24865 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24866 c_invoke_pragma_handler (id);
24870 cp_parser_error (parser, "expected declaration specifiers");
24874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24878 /* The interface the pragma parsers have to the lexer. */
24881 pragma_lex (tree *value)
24884 enum cpp_ttype ret;
24886 tok = cp_lexer_peek_token (the_parser->lexer);
24889 *value = tok->u.value;
24891 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24893 else if (ret == CPP_STRING)
24894 *value = cp_parser_string_literal (the_parser, false, false);
24897 cp_lexer_consume_token (the_parser->lexer);
24898 if (ret == CPP_KEYWORD)
24906 /* External interface. */
24908 /* Parse one entire translation unit. */
24911 c_parse_file (void)
24913 static bool already_called = false;
24915 if (already_called)
24917 sorry ("inter-module optimizations not implemented for C++");
24920 already_called = true;
24922 the_parser = cp_parser_new ();
24923 push_deferring_access_checks (flag_access_control
24924 ? dk_no_deferred : dk_no_check);
24925 cp_parser_translation_unit (the_parser);
24929 #include "gt-cp-parser.h"