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 (processing_template_decl && current_class_type
2702 && TYPE_BINFO (current_class_type))
2706 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2710 tree base_type = BINFO_TYPE (b);
2711 if (CLASS_TYPE_P (base_type)
2712 && dependent_type_p (base_type))
2715 /* Go from a particular instantiation of the
2716 template (which will have an empty TYPE_FIELDs),
2717 to the main version. */
2718 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2719 for (field = TYPE_FIELDS (base_type);
2721 field = DECL_CHAIN (field))
2722 if (TREE_CODE (field) == TYPE_DECL
2723 && DECL_NAME (field) == id)
2726 "(perhaps %<typename %T::%E%> was intended)",
2727 BINFO_TYPE (b), id);
2736 /* Here we diagnose qualified-ids where the scope is actually correct,
2737 but the identifier does not resolve to a valid type name. */
2738 else if (parser->scope != error_mark_node)
2740 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2741 error_at (location, "%qE in namespace %qE does not name a type",
2743 else if (CLASS_TYPE_P (parser->scope)
2744 && constructor_name_p (id, parser->scope))
2747 error_at (location, "%<%T::%E%> names the constructor, not"
2748 " the type", parser->scope, id);
2749 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2750 error_at (location, "and %qT has no template constructors",
2753 else if (TYPE_P (parser->scope)
2754 && dependent_scope_p (parser->scope))
2755 error_at (location, "need %<typename%> before %<%T::%E%> because "
2756 "%qT is a dependent scope",
2757 parser->scope, id, parser->scope);
2758 else if (TYPE_P (parser->scope))
2759 error_at (location, "%qE in class %qT does not name a type",
2764 cp_parser_commit_to_tentative_parse (parser);
2767 /* Check for a common situation where a type-name should be present,
2768 but is not, and issue a sensible error message. Returns true if an
2769 invalid type-name was detected.
2771 The situation handled by this function are variable declarations of the
2772 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2773 Usually, `ID' should name a type, but if we got here it means that it
2774 does not. We try to emit the best possible error message depending on
2775 how exactly the id-expression looks like. */
2778 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2783 /* Avoid duplicate error about ambiguous lookup. */
2784 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2786 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2787 if (next->type == CPP_NAME && next->ambiguous_p)
2791 cp_parser_parse_tentatively (parser);
2792 id = cp_parser_id_expression (parser,
2793 /*template_keyword_p=*/false,
2794 /*check_dependency_p=*/true,
2795 /*template_p=*/NULL,
2796 /*declarator_p=*/true,
2797 /*optional_p=*/false);
2798 /* If the next token is a (, this is a function with no explicit return
2799 type, i.e. constructor, destructor or conversion op. */
2800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2801 || TREE_CODE (id) == TYPE_DECL)
2803 cp_parser_abort_tentative_parse (parser);
2806 if (!cp_parser_parse_definitely (parser))
2809 /* Emit a diagnostic for the invalid type. */
2810 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2811 id, token->location);
2813 /* If we aren't in the middle of a declarator (i.e. in a
2814 parameter-declaration-clause), skip to the end of the declaration;
2815 there's no point in trying to process it. */
2816 if (!parser->in_declarator_p)
2817 cp_parser_skip_to_end_of_block_or_statement (parser);
2821 /* Consume tokens up to, and including, the next non-nested closing `)'.
2822 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2823 are doing error recovery. Returns -1 if OR_COMMA is true and we
2824 found an unnested comma. */
2827 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2832 unsigned paren_depth = 0;
2833 unsigned brace_depth = 0;
2834 unsigned square_depth = 0;
2836 if (recovering && !or_comma
2837 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2842 cp_token * token = cp_lexer_peek_token (parser->lexer);
2844 switch (token->type)
2847 case CPP_PRAGMA_EOL:
2848 /* If we've run out of tokens, then there is no closing `)'. */
2851 /* This is good for lambda expression capture-lists. */
2852 case CPP_OPEN_SQUARE:
2855 case CPP_CLOSE_SQUARE:
2856 if (!square_depth--)
2861 /* This matches the processing in skip_to_end_of_statement. */
2866 case CPP_OPEN_BRACE:
2869 case CPP_CLOSE_BRACE:
2875 if (recovering && or_comma && !brace_depth && !paren_depth
2880 case CPP_OPEN_PAREN:
2885 case CPP_CLOSE_PAREN:
2886 if (!brace_depth && !paren_depth--)
2889 cp_lexer_consume_token (parser->lexer);
2898 /* Consume the token. */
2899 cp_lexer_consume_token (parser->lexer);
2903 /* Consume tokens until we reach the end of the current statement.
2904 Normally, that will be just before consuming a `;'. However, if a
2905 non-nested `}' comes first, then we stop before consuming that. */
2908 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2910 unsigned nesting_depth = 0;
2914 cp_token *token = cp_lexer_peek_token (parser->lexer);
2916 switch (token->type)
2919 case CPP_PRAGMA_EOL:
2920 /* If we've run out of tokens, stop. */
2924 /* If the next token is a `;', we have reached the end of the
2930 case CPP_CLOSE_BRACE:
2931 /* If this is a non-nested '}', stop before consuming it.
2932 That way, when confronted with something like:
2936 we stop before consuming the closing '}', even though we
2937 have not yet reached a `;'. */
2938 if (nesting_depth == 0)
2941 /* If it is the closing '}' for a block that we have
2942 scanned, stop -- but only after consuming the token.
2948 we will stop after the body of the erroneously declared
2949 function, but before consuming the following `typedef'
2951 if (--nesting_depth == 0)
2953 cp_lexer_consume_token (parser->lexer);
2957 case CPP_OPEN_BRACE:
2965 /* Consume the token. */
2966 cp_lexer_consume_token (parser->lexer);
2970 /* This function is called at the end of a statement or declaration.
2971 If the next token is a semicolon, it is consumed; otherwise, error
2972 recovery is attempted. */
2975 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2977 /* Look for the trailing `;'. */
2978 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2980 /* If there is additional (erroneous) input, skip to the end of
2982 cp_parser_skip_to_end_of_statement (parser);
2983 /* If the next token is now a `;', consume it. */
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2985 cp_lexer_consume_token (parser->lexer);
2989 /* Skip tokens until we have consumed an entire block, or until we
2990 have consumed a non-nested `;'. */
2993 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2995 int nesting_depth = 0;
2997 while (nesting_depth >= 0)
2999 cp_token *token = cp_lexer_peek_token (parser->lexer);
3001 switch (token->type)
3004 case CPP_PRAGMA_EOL:
3005 /* If we've run out of tokens, stop. */
3009 /* Stop if this is an unnested ';'. */
3014 case CPP_CLOSE_BRACE:
3015 /* Stop if this is an unnested '}', or closes the outermost
3018 if (nesting_depth < 0)
3024 case CPP_OPEN_BRACE:
3033 /* Consume the token. */
3034 cp_lexer_consume_token (parser->lexer);
3038 /* Skip tokens until a non-nested closing curly brace is the next
3039 token, or there are no more tokens. Return true in the first case,
3043 cp_parser_skip_to_closing_brace (cp_parser *parser)
3045 unsigned nesting_depth = 0;
3049 cp_token *token = cp_lexer_peek_token (parser->lexer);
3051 switch (token->type)
3054 case CPP_PRAGMA_EOL:
3055 /* If we've run out of tokens, stop. */
3058 case CPP_CLOSE_BRACE:
3059 /* If the next token is a non-nested `}', then we have reached
3060 the end of the current block. */
3061 if (nesting_depth-- == 0)
3065 case CPP_OPEN_BRACE:
3066 /* If it the next token is a `{', then we are entering a new
3067 block. Consume the entire block. */
3075 /* Consume the token. */
3076 cp_lexer_consume_token (parser->lexer);
3080 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3081 parameter is the PRAGMA token, allowing us to purge the entire pragma
3085 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3089 parser->lexer->in_pragma = false;
3092 token = cp_lexer_consume_token (parser->lexer);
3093 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3095 /* Ensure that the pragma is not parsed again. */
3096 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3099 /* Require pragma end of line, resyncing with it as necessary. The
3100 arguments are as for cp_parser_skip_to_pragma_eol. */
3103 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3105 parser->lexer->in_pragma = false;
3106 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3107 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3110 /* This is a simple wrapper around make_typename_type. When the id is
3111 an unresolved identifier node, we can provide a superior diagnostic
3112 using cp_parser_diagnose_invalid_type_name. */
3115 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3116 tree id, location_t id_location)
3119 if (TREE_CODE (id) == IDENTIFIER_NODE)
3121 result = make_typename_type (scope, id, typename_type,
3122 /*complain=*/tf_none);
3123 if (result == error_mark_node)
3124 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3127 return make_typename_type (scope, id, typename_type, tf_error);
3130 /* This is a wrapper around the
3131 make_{pointer,ptrmem,reference}_declarator functions that decides
3132 which one to call based on the CODE and CLASS_TYPE arguments. The
3133 CODE argument should be one of the values returned by
3134 cp_parser_ptr_operator. */
3135 static cp_declarator *
3136 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3137 cp_cv_quals cv_qualifiers,
3138 cp_declarator *target)
3140 if (code == ERROR_MARK)
3141 return cp_error_declarator;
3143 if (code == INDIRECT_REF)
3144 if (class_type == NULL_TREE)
3145 return make_pointer_declarator (cv_qualifiers, target);
3147 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3148 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3149 return make_reference_declarator (cv_qualifiers, target, false);
3150 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3151 return make_reference_declarator (cv_qualifiers, target, true);
3155 /* Create a new C++ parser. */
3158 cp_parser_new (void)
3164 /* cp_lexer_new_main is called before doing GC allocation because
3165 cp_lexer_new_main might load a PCH file. */
3166 lexer = cp_lexer_new_main ();
3168 /* Initialize the binops_by_token so that we can get the tree
3169 directly from the token. */
3170 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3171 binops_by_token[binops[i].token_type] = binops[i];
3173 parser = ggc_alloc_cleared_cp_parser ();
3174 parser->lexer = lexer;
3175 parser->context = cp_parser_context_new (NULL);
3177 /* For now, we always accept GNU extensions. */
3178 parser->allow_gnu_extensions_p = 1;
3180 /* The `>' token is a greater-than operator, not the end of a
3182 parser->greater_than_is_operator_p = true;
3184 parser->default_arg_ok_p = true;
3186 /* We are not parsing a constant-expression. */
3187 parser->integral_constant_expression_p = false;
3188 parser->allow_non_integral_constant_expression_p = false;
3189 parser->non_integral_constant_expression_p = false;
3191 /* Local variable names are not forbidden. */
3192 parser->local_variables_forbidden_p = false;
3194 /* We are not processing an `extern "C"' declaration. */
3195 parser->in_unbraced_linkage_specification_p = false;
3197 /* We are not processing a declarator. */
3198 parser->in_declarator_p = false;
3200 /* We are not processing a template-argument-list. */
3201 parser->in_template_argument_list_p = false;
3203 /* We are not in an iteration statement. */
3204 parser->in_statement = 0;
3206 /* We are not in a switch statement. */
3207 parser->in_switch_statement_p = false;
3209 /* We are not parsing a type-id inside an expression. */
3210 parser->in_type_id_in_expr_p = false;
3212 /* Declarations aren't implicitly extern "C". */
3213 parser->implicit_extern_c = false;
3215 /* String literals should be translated to the execution character set. */
3216 parser->translate_strings_p = true;
3218 /* We are not parsing a function body. */
3219 parser->in_function_body = false;
3221 /* The unparsed function queue is empty. */
3222 push_unparsed_function_queues (parser);
3224 /* There are no classes being defined. */
3225 parser->num_classes_being_defined = 0;
3227 /* No template parameters apply. */
3228 parser->num_template_parameter_lists = 0;
3233 /* Create a cp_lexer structure which will emit the tokens in CACHE
3234 and push it onto the parser's lexer stack. This is used for delayed
3235 parsing of in-class method bodies and default arguments, and should
3236 not be confused with tentative parsing. */
3238 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3240 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3241 lexer->next = parser->lexer;
3242 parser->lexer = lexer;
3244 /* Move the current source position to that of the first token in the
3246 cp_lexer_set_source_position_from_token (lexer->next_token);
3249 /* Pop the top lexer off the parser stack. This is never used for the
3250 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3252 cp_parser_pop_lexer (cp_parser *parser)
3254 cp_lexer *lexer = parser->lexer;
3255 parser->lexer = lexer->next;
3256 cp_lexer_destroy (lexer);
3258 /* Put the current source position back where it was before this
3259 lexer was pushed. */
3260 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3263 /* Lexical conventions [gram.lex] */
3265 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3269 cp_parser_identifier (cp_parser* parser)
3273 /* Look for the identifier. */
3274 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3275 /* Return the value. */
3276 return token ? token->u.value : error_mark_node;
3279 /* Parse a sequence of adjacent string constants. Returns a
3280 TREE_STRING representing the combined, nul-terminated string
3281 constant. If TRANSLATE is true, translate the string to the
3282 execution character set. If WIDE_OK is true, a wide string is
3285 C++98 [lex.string] says that if a narrow string literal token is
3286 adjacent to a wide string literal token, the behavior is undefined.
3287 However, C99 6.4.5p4 says that this results in a wide string literal.
3288 We follow C99 here, for consistency with the C front end.
3290 This code is largely lifted from lex_string() in c-lex.c.
3292 FUTURE: ObjC++ will need to handle @-strings here. */
3294 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3298 struct obstack str_ob;
3299 cpp_string str, istr, *strs;
3301 enum cpp_ttype type;
3303 tok = cp_lexer_peek_token (parser->lexer);
3304 if (!cp_parser_is_string_literal (tok))
3306 cp_parser_error (parser, "expected string-literal");
3307 return error_mark_node;
3312 /* Try to avoid the overhead of creating and destroying an obstack
3313 for the common case of just one string. */
3314 if (!cp_parser_is_string_literal
3315 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3317 cp_lexer_consume_token (parser->lexer);
3319 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3320 str.len = TREE_STRING_LENGTH (tok->u.value);
3327 gcc_obstack_init (&str_ob);
3332 cp_lexer_consume_token (parser->lexer);
3334 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3335 str.len = TREE_STRING_LENGTH (tok->u.value);
3337 if (type != tok->type)
3339 if (type == CPP_STRING)
3341 else if (tok->type != CPP_STRING)
3342 error_at (tok->location,
3343 "unsupported non-standard concatenation "
3344 "of string literals");
3347 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3349 tok = cp_lexer_peek_token (parser->lexer);
3351 while (cp_parser_is_string_literal (tok));
3353 strs = (cpp_string *) obstack_finish (&str_ob);
3356 if (type != CPP_STRING && !wide_ok)
3358 cp_parser_error (parser, "a wide string is invalid in this context");
3362 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3363 (parse_in, strs, count, &istr, type))
3365 value = build_string (istr.len, (const char *)istr.text);
3366 free (CONST_CAST (unsigned char *, istr.text));
3372 case CPP_UTF8STRING:
3373 TREE_TYPE (value) = char_array_type_node;
3376 TREE_TYPE (value) = char16_array_type_node;
3379 TREE_TYPE (value) = char32_array_type_node;
3382 TREE_TYPE (value) = wchar_array_type_node;
3386 value = fix_string_type (value);
3389 /* cpp_interpret_string has issued an error. */
3390 value = error_mark_node;
3393 obstack_free (&str_ob, 0);
3399 /* Basic concepts [gram.basic] */
3401 /* Parse a translation-unit.
3404 declaration-seq [opt]
3406 Returns TRUE if all went well. */
3409 cp_parser_translation_unit (cp_parser* parser)
3411 /* The address of the first non-permanent object on the declarator
3413 static void *declarator_obstack_base;
3417 /* Create the declarator obstack, if necessary. */
3418 if (!cp_error_declarator)
3420 gcc_obstack_init (&declarator_obstack);
3421 /* Create the error declarator. */
3422 cp_error_declarator = make_declarator (cdk_error);
3423 /* Create the empty parameter list. */
3424 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3425 /* Remember where the base of the declarator obstack lies. */
3426 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3429 cp_parser_declaration_seq_opt (parser);
3431 /* If there are no tokens left then all went well. */
3432 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3434 /* Get rid of the token array; we don't need it any more. */
3435 cp_lexer_destroy (parser->lexer);
3436 parser->lexer = NULL;
3438 /* This file might have been a context that's implicitly extern
3439 "C". If so, pop the lang context. (Only relevant for PCH.) */
3440 if (parser->implicit_extern_c)
3442 pop_lang_context ();
3443 parser->implicit_extern_c = false;
3447 finish_translation_unit ();
3453 cp_parser_error (parser, "expected declaration");
3457 /* Make sure the declarator obstack was fully cleaned up. */
3458 gcc_assert (obstack_next_free (&declarator_obstack)
3459 == declarator_obstack_base);
3461 /* All went well. */
3465 /* Expressions [gram.expr] */
3467 /* Parse a primary-expression.
3478 ( compound-statement )
3479 __builtin_va_arg ( assignment-expression , type-id )
3480 __builtin_offsetof ( type-id , offsetof-expression )
3483 __has_nothrow_assign ( type-id )
3484 __has_nothrow_constructor ( type-id )
3485 __has_nothrow_copy ( type-id )
3486 __has_trivial_assign ( type-id )
3487 __has_trivial_constructor ( type-id )
3488 __has_trivial_copy ( type-id )
3489 __has_trivial_destructor ( type-id )
3490 __has_virtual_destructor ( type-id )
3491 __is_abstract ( type-id )
3492 __is_base_of ( type-id , type-id )
3493 __is_class ( type-id )
3494 __is_convertible_to ( type-id , type-id )
3495 __is_empty ( type-id )
3496 __is_enum ( type-id )
3497 __is_pod ( type-id )
3498 __is_polymorphic ( type-id )
3499 __is_union ( type-id )
3501 Objective-C++ Extension:
3509 ADDRESS_P is true iff this expression was immediately preceded by
3510 "&" and therefore might denote a pointer-to-member. CAST_P is true
3511 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3512 true iff this expression is a template argument.
3514 Returns a representation of the expression. Upon return, *IDK
3515 indicates what kind of id-expression (if any) was present. */
3518 cp_parser_primary_expression (cp_parser *parser,
3521 bool template_arg_p,
3524 cp_token *token = NULL;
3526 /* Assume the primary expression is not an id-expression. */
3527 *idk = CP_ID_KIND_NONE;
3529 /* Peek at the next token. */
3530 token = cp_lexer_peek_token (parser->lexer);
3531 switch (token->type)
3544 token = cp_lexer_consume_token (parser->lexer);
3545 if (TREE_CODE (token->u.value) == FIXED_CST)
3547 error_at (token->location,
3548 "fixed-point types not supported in C++");
3549 return error_mark_node;
3551 /* Floating-point literals are only allowed in an integral
3552 constant expression if they are cast to an integral or
3553 enumeration type. */
3554 if (TREE_CODE (token->u.value) == REAL_CST
3555 && parser->integral_constant_expression_p
3558 /* CAST_P will be set even in invalid code like "int(2.7 +
3559 ...)". Therefore, we have to check that the next token
3560 is sure to end the cast. */
3563 cp_token *next_token;
3565 next_token = cp_lexer_peek_token (parser->lexer);
3566 if (/* The comma at the end of an
3567 enumerator-definition. */
3568 next_token->type != CPP_COMMA
3569 /* The curly brace at the end of an enum-specifier. */
3570 && next_token->type != CPP_CLOSE_BRACE
3571 /* The end of a statement. */
3572 && next_token->type != CPP_SEMICOLON
3573 /* The end of the cast-expression. */
3574 && next_token->type != CPP_CLOSE_PAREN
3575 /* The end of an array bound. */
3576 && next_token->type != CPP_CLOSE_SQUARE
3577 /* The closing ">" in a template-argument-list. */
3578 && (next_token->type != CPP_GREATER
3579 || parser->greater_than_is_operator_p)
3580 /* C++0x only: A ">>" treated like two ">" tokens,
3581 in a template-argument-list. */
3582 && (next_token->type != CPP_RSHIFT
3583 || (cxx_dialect == cxx98)
3584 || parser->greater_than_is_operator_p))
3588 /* If we are within a cast, then the constraint that the
3589 cast is to an integral or enumeration type will be
3590 checked at that point. If we are not within a cast, then
3591 this code is invalid. */
3593 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3595 return token->u.value;
3601 case CPP_UTF8STRING:
3602 /* ??? Should wide strings be allowed when parser->translate_strings_p
3603 is false (i.e. in attributes)? If not, we can kill the third
3604 argument to cp_parser_string_literal. */
3605 return cp_parser_string_literal (parser,
3606 parser->translate_strings_p,
3609 case CPP_OPEN_PAREN:
3612 bool saved_greater_than_is_operator_p;
3614 /* Consume the `('. */
3615 cp_lexer_consume_token (parser->lexer);
3616 /* Within a parenthesized expression, a `>' token is always
3617 the greater-than operator. */
3618 saved_greater_than_is_operator_p
3619 = parser->greater_than_is_operator_p;
3620 parser->greater_than_is_operator_p = true;
3621 /* If we see `( { ' then we are looking at the beginning of
3622 a GNU statement-expression. */
3623 if (cp_parser_allow_gnu_extensions_p (parser)
3624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3626 /* Statement-expressions are not allowed by the standard. */
3627 pedwarn (token->location, OPT_pedantic,
3628 "ISO C++ forbids braced-groups within expressions");
3630 /* And they're not allowed outside of a function-body; you
3631 cannot, for example, write:
3633 int i = ({ int j = 3; j + 1; });
3635 at class or namespace scope. */
3636 if (!parser->in_function_body
3637 || parser->in_template_argument_list_p)
3639 error_at (token->location,
3640 "statement-expressions are not allowed outside "
3641 "functions nor in template-argument lists");
3642 cp_parser_skip_to_end_of_block_or_statement (parser);
3643 expr = error_mark_node;
3647 /* Start the statement-expression. */
3648 expr = begin_stmt_expr ();
3649 /* Parse the compound-statement. */
3650 cp_parser_compound_statement (parser, expr, false);
3652 expr = finish_stmt_expr (expr, false);
3657 /* Parse the parenthesized expression. */
3658 expr = cp_parser_expression (parser, cast_p, idk);
3659 /* Let the front end know that this expression was
3660 enclosed in parentheses. This matters in case, for
3661 example, the expression is of the form `A::B', since
3662 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3664 finish_parenthesized_expr (expr);
3666 /* The `>' token might be the end of a template-id or
3667 template-parameter-list now. */
3668 parser->greater_than_is_operator_p
3669 = saved_greater_than_is_operator_p;
3670 /* Consume the `)'. */
3671 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3672 cp_parser_skip_to_end_of_statement (parser);
3677 case CPP_OPEN_SQUARE:
3678 if (c_dialect_objc ())
3679 /* We have an Objective-C++ message. */
3680 return cp_parser_objc_expression (parser);
3681 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3682 return cp_parser_lambda_expression (parser);
3684 case CPP_OBJC_STRING:
3685 if (c_dialect_objc ())
3686 /* We have an Objective-C++ string literal. */
3687 return cp_parser_objc_expression (parser);
3688 cp_parser_error (parser, "expected primary-expression");
3689 return error_mark_node;
3692 switch (token->keyword)
3694 /* These two are the boolean literals. */
3696 cp_lexer_consume_token (parser->lexer);
3697 return boolean_true_node;
3699 cp_lexer_consume_token (parser->lexer);
3700 return boolean_false_node;
3702 /* The `__null' literal. */
3704 cp_lexer_consume_token (parser->lexer);
3707 /* The `nullptr' literal. */
3709 cp_lexer_consume_token (parser->lexer);
3710 return nullptr_node;
3712 /* Recognize the `this' keyword. */
3714 cp_lexer_consume_token (parser->lexer);
3715 if (parser->local_variables_forbidden_p)
3717 error_at (token->location,
3718 "%<this%> may not be used in this context");
3719 return error_mark_node;
3721 /* Pointers cannot appear in constant-expressions. */
3722 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3723 return error_mark_node;
3724 return finish_this_expr ();
3726 /* The `operator' keyword can be the beginning of an
3731 case RID_FUNCTION_NAME:
3732 case RID_PRETTY_FUNCTION_NAME:
3733 case RID_C99_FUNCTION_NAME:
3735 non_integral_constant name;
3737 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3738 __func__ are the names of variables -- but they are
3739 treated specially. Therefore, they are handled here,
3740 rather than relying on the generic id-expression logic
3741 below. Grammatically, these names are id-expressions.
3743 Consume the token. */
3744 token = cp_lexer_consume_token (parser->lexer);
3746 switch (token->keyword)
3748 case RID_FUNCTION_NAME:
3749 name = NIC_FUNC_NAME;
3751 case RID_PRETTY_FUNCTION_NAME:
3752 name = NIC_PRETTY_FUNC;
3754 case RID_C99_FUNCTION_NAME:
3755 name = NIC_C99_FUNC;
3761 if (cp_parser_non_integral_constant_expression (parser, name))
3762 return error_mark_node;
3764 /* Look up the name. */
3765 return finish_fname (token->u.value);
3773 /* The `__builtin_va_arg' construct is used to handle
3774 `va_arg'. Consume the `__builtin_va_arg' token. */
3775 cp_lexer_consume_token (parser->lexer);
3776 /* Look for the opening `('. */
3777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3778 /* Now, parse the assignment-expression. */
3779 expression = cp_parser_assignment_expression (parser,
3780 /*cast_p=*/false, NULL);
3781 /* Look for the `,'. */
3782 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3783 /* Parse the type-id. */
3784 type = cp_parser_type_id (parser);
3785 /* Look for the closing `)'. */
3786 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3787 /* Using `va_arg' in a constant-expression is not
3789 if (cp_parser_non_integral_constant_expression (parser,
3791 return error_mark_node;
3792 return build_x_va_arg (expression, type);
3796 return cp_parser_builtin_offsetof (parser);
3798 case RID_HAS_NOTHROW_ASSIGN:
3799 case RID_HAS_NOTHROW_CONSTRUCTOR:
3800 case RID_HAS_NOTHROW_COPY:
3801 case RID_HAS_TRIVIAL_ASSIGN:
3802 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3803 case RID_HAS_TRIVIAL_COPY:
3804 case RID_HAS_TRIVIAL_DESTRUCTOR:
3805 case RID_HAS_VIRTUAL_DESTRUCTOR:
3806 case RID_IS_ABSTRACT:
3807 case RID_IS_BASE_OF:
3809 case RID_IS_CONVERTIBLE_TO:
3813 case RID_IS_POLYMORPHIC:
3814 case RID_IS_STD_LAYOUT:
3815 case RID_IS_TRIVIAL:
3817 return cp_parser_trait_expr (parser, token->keyword);
3819 /* Objective-C++ expressions. */
3821 case RID_AT_PROTOCOL:
3822 case RID_AT_SELECTOR:
3823 return cp_parser_objc_expression (parser);
3826 if (parser->in_function_body
3827 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3830 error_at (token->location,
3831 "a template declaration cannot appear at block scope");
3832 cp_parser_skip_to_end_of_block_or_statement (parser);
3833 return error_mark_node;
3836 cp_parser_error (parser, "expected primary-expression");
3837 return error_mark_node;
3840 /* An id-expression can start with either an identifier, a
3841 `::' as the beginning of a qualified-id, or the "operator"
3845 case CPP_TEMPLATE_ID:
3846 case CPP_NESTED_NAME_SPECIFIER:
3850 const char *error_msg;
3853 cp_token *id_expr_token;
3856 /* Parse the id-expression. */
3858 = cp_parser_id_expression (parser,
3859 /*template_keyword_p=*/false,
3860 /*check_dependency_p=*/true,
3862 /*declarator_p=*/false,
3863 /*optional_p=*/false);
3864 if (id_expression == error_mark_node)
3865 return error_mark_node;
3866 id_expr_token = token;
3867 token = cp_lexer_peek_token (parser->lexer);
3868 done = (token->type != CPP_OPEN_SQUARE
3869 && token->type != CPP_OPEN_PAREN
3870 && token->type != CPP_DOT
3871 && token->type != CPP_DEREF
3872 && token->type != CPP_PLUS_PLUS
3873 && token->type != CPP_MINUS_MINUS);
3874 /* If we have a template-id, then no further lookup is
3875 required. If the template-id was for a template-class, we
3876 will sometimes have a TYPE_DECL at this point. */
3877 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3878 || TREE_CODE (id_expression) == TYPE_DECL)
3879 decl = id_expression;
3880 /* Look up the name. */
3883 tree ambiguous_decls;
3885 /* If we already know that this lookup is ambiguous, then
3886 we've already issued an error message; there's no reason
3888 if (id_expr_token->type == CPP_NAME
3889 && id_expr_token->ambiguous_p)
3891 cp_parser_simulate_error (parser);
3892 return error_mark_node;
3895 decl = cp_parser_lookup_name (parser, id_expression,
3898 /*is_namespace=*/false,
3899 /*check_dependency=*/true,
3901 id_expr_token->location);
3902 /* If the lookup was ambiguous, an error will already have
3904 if (ambiguous_decls)
3905 return error_mark_node;
3907 /* In Objective-C++, an instance variable (ivar) may be preferred
3908 to whatever cp_parser_lookup_name() found. */
3909 decl = objc_lookup_ivar (decl, id_expression);
3911 /* If name lookup gives us a SCOPE_REF, then the
3912 qualifying scope was dependent. */
3913 if (TREE_CODE (decl) == SCOPE_REF)
3915 /* At this point, we do not know if DECL is a valid
3916 integral constant expression. We assume that it is
3917 in fact such an expression, so that code like:
3919 template <int N> struct A {
3923 is accepted. At template-instantiation time, we
3924 will check that B<N>::i is actually a constant. */
3927 /* Check to see if DECL is a local variable in a context
3928 where that is forbidden. */
3929 if (parser->local_variables_forbidden_p
3930 && local_variable_p (decl))
3932 /* It might be that we only found DECL because we are
3933 trying to be generous with pre-ISO scoping rules.
3934 For example, consider:
3938 for (int i = 0; i < 10; ++i) {}
3939 extern void f(int j = i);
3942 Here, name look up will originally find the out
3943 of scope `i'. We need to issue a warning message,
3944 but then use the global `i'. */
3945 decl = check_for_out_of_scope_variable (decl);
3946 if (local_variable_p (decl))
3948 error_at (id_expr_token->location,
3949 "local variable %qD may not appear in this context",
3951 return error_mark_node;
3956 decl = (finish_id_expression
3957 (id_expression, decl, parser->scope,
3959 parser->integral_constant_expression_p,
3960 parser->allow_non_integral_constant_expression_p,
3961 &parser->non_integral_constant_expression_p,
3962 template_p, done, address_p,
3965 id_expr_token->location));
3967 cp_parser_error (parser, error_msg);
3971 /* Anything else is an error. */
3973 cp_parser_error (parser, "expected primary-expression");
3974 return error_mark_node;
3978 /* Parse an id-expression.
3985 :: [opt] nested-name-specifier template [opt] unqualified-id
3987 :: operator-function-id
3990 Return a representation of the unqualified portion of the
3991 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3992 a `::' or nested-name-specifier.
3994 Often, if the id-expression was a qualified-id, the caller will
3995 want to make a SCOPE_REF to represent the qualified-id. This
3996 function does not do this in order to avoid wastefully creating
3997 SCOPE_REFs when they are not required.
3999 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4002 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4003 uninstantiated templates.
4005 If *TEMPLATE_P is non-NULL, it is set to true iff the
4006 `template' keyword is used to explicitly indicate that the entity
4007 named is a template.
4009 If DECLARATOR_P is true, the id-expression is appearing as part of
4010 a declarator, rather than as part of an expression. */
4013 cp_parser_id_expression (cp_parser *parser,
4014 bool template_keyword_p,
4015 bool check_dependency_p,
4020 bool global_scope_p;
4021 bool nested_name_specifier_p;
4023 /* Assume the `template' keyword was not used. */
4025 *template_p = template_keyword_p;
4027 /* Look for the optional `::' operator. */
4029 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4031 /* Look for the optional nested-name-specifier. */
4032 nested_name_specifier_p
4033 = (cp_parser_nested_name_specifier_opt (parser,
4034 /*typename_keyword_p=*/false,
4039 /* If there is a nested-name-specifier, then we are looking at
4040 the first qualified-id production. */
4041 if (nested_name_specifier_p)
4044 tree saved_object_scope;
4045 tree saved_qualifying_scope;
4046 tree unqualified_id;
4049 /* See if the next token is the `template' keyword. */
4051 template_p = &is_template;
4052 *template_p = cp_parser_optional_template_keyword (parser);
4053 /* Name lookup we do during the processing of the
4054 unqualified-id might obliterate SCOPE. */
4055 saved_scope = parser->scope;
4056 saved_object_scope = parser->object_scope;
4057 saved_qualifying_scope = parser->qualifying_scope;
4058 /* Process the final unqualified-id. */
4059 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4062 /*optional_p=*/false);
4063 /* Restore the SAVED_SCOPE for our caller. */
4064 parser->scope = saved_scope;
4065 parser->object_scope = saved_object_scope;
4066 parser->qualifying_scope = saved_qualifying_scope;
4068 return unqualified_id;
4070 /* Otherwise, if we are in global scope, then we are looking at one
4071 of the other qualified-id productions. */
4072 else if (global_scope_p)
4077 /* Peek at the next token. */
4078 token = cp_lexer_peek_token (parser->lexer);
4080 /* If it's an identifier, and the next token is not a "<", then
4081 we can avoid the template-id case. This is an optimization
4082 for this common case. */
4083 if (token->type == CPP_NAME
4084 && !cp_parser_nth_token_starts_template_argument_list_p
4086 return cp_parser_identifier (parser);
4088 cp_parser_parse_tentatively (parser);
4089 /* Try a template-id. */
4090 id = cp_parser_template_id (parser,
4091 /*template_keyword_p=*/false,
4092 /*check_dependency_p=*/true,
4094 /* If that worked, we're done. */
4095 if (cp_parser_parse_definitely (parser))
4098 /* Peek at the next token. (Changes in the token buffer may
4099 have invalidated the pointer obtained above.) */
4100 token = cp_lexer_peek_token (parser->lexer);
4102 switch (token->type)
4105 return cp_parser_identifier (parser);
4108 if (token->keyword == RID_OPERATOR)
4109 return cp_parser_operator_function_id (parser);
4113 cp_parser_error (parser, "expected id-expression");
4114 return error_mark_node;
4118 return cp_parser_unqualified_id (parser, template_keyword_p,
4119 /*check_dependency_p=*/true,
4124 /* Parse an unqualified-id.
4128 operator-function-id
4129 conversion-function-id
4133 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4134 keyword, in a construct like `A::template ...'.
4136 Returns a representation of unqualified-id. For the `identifier'
4137 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4138 production a BIT_NOT_EXPR is returned; the operand of the
4139 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4140 other productions, see the documentation accompanying the
4141 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4142 names are looked up in uninstantiated templates. If DECLARATOR_P
4143 is true, the unqualified-id is appearing as part of a declarator,
4144 rather than as part of an expression. */
4147 cp_parser_unqualified_id (cp_parser* parser,
4148 bool template_keyword_p,
4149 bool check_dependency_p,
4155 /* Peek at the next token. */
4156 token = cp_lexer_peek_token (parser->lexer);
4158 switch (token->type)
4164 /* We don't know yet whether or not this will be a
4166 cp_parser_parse_tentatively (parser);
4167 /* Try a template-id. */
4168 id = cp_parser_template_id (parser, template_keyword_p,
4171 /* If it worked, we're done. */
4172 if (cp_parser_parse_definitely (parser))
4174 /* Otherwise, it's an ordinary identifier. */
4175 return cp_parser_identifier (parser);
4178 case CPP_TEMPLATE_ID:
4179 return cp_parser_template_id (parser, template_keyword_p,
4186 tree qualifying_scope;
4191 /* Consume the `~' token. */
4192 cp_lexer_consume_token (parser->lexer);
4193 /* Parse the class-name. The standard, as written, seems to
4196 template <typename T> struct S { ~S (); };
4197 template <typename T> S<T>::~S() {}
4199 is invalid, since `~' must be followed by a class-name, but
4200 `S<T>' is dependent, and so not known to be a class.
4201 That's not right; we need to look in uninstantiated
4202 templates. A further complication arises from:
4204 template <typename T> void f(T t) {
4208 Here, it is not possible to look up `T' in the scope of `T'
4209 itself. We must look in both the current scope, and the
4210 scope of the containing complete expression.
4212 Yet another issue is:
4221 The standard does not seem to say that the `S' in `~S'
4222 should refer to the type `S' and not the data member
4225 /* DR 244 says that we look up the name after the "~" in the
4226 same scope as we looked up the qualifying name. That idea
4227 isn't fully worked out; it's more complicated than that. */
4228 scope = parser->scope;
4229 object_scope = parser->object_scope;
4230 qualifying_scope = parser->qualifying_scope;
4232 /* Check for invalid scopes. */
4233 if (scope == error_mark_node)
4235 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4236 cp_lexer_consume_token (parser->lexer);
4237 return error_mark_node;
4239 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4241 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4242 error_at (token->location,
4243 "scope %qT before %<~%> is not a class-name",
4245 cp_parser_simulate_error (parser);
4246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4247 cp_lexer_consume_token (parser->lexer);
4248 return error_mark_node;
4250 gcc_assert (!scope || TYPE_P (scope));
4252 /* If the name is of the form "X::~X" it's OK even if X is a
4254 token = cp_lexer_peek_token (parser->lexer);
4256 && token->type == CPP_NAME
4257 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4259 && (token->u.value == TYPE_IDENTIFIER (scope)
4260 || constructor_name_p (token->u.value, scope)))
4262 cp_lexer_consume_token (parser->lexer);
4263 return build_nt (BIT_NOT_EXPR, scope);
4266 /* If there was an explicit qualification (S::~T), first look
4267 in the scope given by the qualification (i.e., S).
4269 Note: in the calls to cp_parser_class_name below we pass
4270 typename_type so that lookup finds the injected-class-name
4271 rather than the constructor. */
4273 type_decl = NULL_TREE;
4276 cp_parser_parse_tentatively (parser);
4277 type_decl = cp_parser_class_name (parser,
4278 /*typename_keyword_p=*/false,
4279 /*template_keyword_p=*/false,
4281 /*check_dependency=*/false,
4282 /*class_head_p=*/false,
4284 if (cp_parser_parse_definitely (parser))
4287 /* In "N::S::~S", look in "N" as well. */
4288 if (!done && scope && qualifying_scope)
4290 cp_parser_parse_tentatively (parser);
4291 parser->scope = qualifying_scope;
4292 parser->object_scope = NULL_TREE;
4293 parser->qualifying_scope = NULL_TREE;
4295 = cp_parser_class_name (parser,
4296 /*typename_keyword_p=*/false,
4297 /*template_keyword_p=*/false,
4299 /*check_dependency=*/false,
4300 /*class_head_p=*/false,
4302 if (cp_parser_parse_definitely (parser))
4305 /* In "p->S::~T", look in the scope given by "*p" as well. */
4306 else if (!done && object_scope)
4308 cp_parser_parse_tentatively (parser);
4309 parser->scope = object_scope;
4310 parser->object_scope = NULL_TREE;
4311 parser->qualifying_scope = NULL_TREE;
4313 = cp_parser_class_name (parser,
4314 /*typename_keyword_p=*/false,
4315 /*template_keyword_p=*/false,
4317 /*check_dependency=*/false,
4318 /*class_head_p=*/false,
4320 if (cp_parser_parse_definitely (parser))
4323 /* Look in the surrounding context. */
4326 parser->scope = NULL_TREE;
4327 parser->object_scope = NULL_TREE;
4328 parser->qualifying_scope = NULL_TREE;
4329 if (processing_template_decl)
4330 cp_parser_parse_tentatively (parser);
4332 = cp_parser_class_name (parser,
4333 /*typename_keyword_p=*/false,
4334 /*template_keyword_p=*/false,
4336 /*check_dependency=*/false,
4337 /*class_head_p=*/false,
4339 if (processing_template_decl
4340 && ! cp_parser_parse_definitely (parser))
4342 /* We couldn't find a type with this name, so just accept
4343 it and check for a match at instantiation time. */
4344 type_decl = cp_parser_identifier (parser);
4345 if (type_decl != error_mark_node)
4346 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4350 /* If an error occurred, assume that the name of the
4351 destructor is the same as the name of the qualifying
4352 class. That allows us to keep parsing after running
4353 into ill-formed destructor names. */
4354 if (type_decl == error_mark_node && scope)
4355 return build_nt (BIT_NOT_EXPR, scope);
4356 else if (type_decl == error_mark_node)
4357 return error_mark_node;
4359 /* Check that destructor name and scope match. */
4360 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4362 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4363 error_at (token->location,
4364 "declaration of %<~%T%> as member of %qT",
4366 cp_parser_simulate_error (parser);
4367 return error_mark_node;
4372 A typedef-name that names a class shall not be used as the
4373 identifier in the declarator for a destructor declaration. */
4375 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4376 && !DECL_SELF_REFERENCE_P (type_decl)
4377 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4378 error_at (token->location,
4379 "typedef-name %qD used as destructor declarator",
4382 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4386 if (token->keyword == RID_OPERATOR)
4390 /* This could be a template-id, so we try that first. */
4391 cp_parser_parse_tentatively (parser);
4392 /* Try a template-id. */
4393 id = cp_parser_template_id (parser, template_keyword_p,
4394 /*check_dependency_p=*/true,
4396 /* If that worked, we're done. */
4397 if (cp_parser_parse_definitely (parser))
4399 /* We still don't know whether we're looking at an
4400 operator-function-id or a conversion-function-id. */
4401 cp_parser_parse_tentatively (parser);
4402 /* Try an operator-function-id. */
4403 id = cp_parser_operator_function_id (parser);
4404 /* If that didn't work, try a conversion-function-id. */
4405 if (!cp_parser_parse_definitely (parser))
4406 id = cp_parser_conversion_function_id (parser);
4415 cp_parser_error (parser, "expected unqualified-id");
4416 return error_mark_node;
4420 /* Parse an (optional) nested-name-specifier.
4422 nested-name-specifier: [C++98]
4423 class-or-namespace-name :: nested-name-specifier [opt]
4424 class-or-namespace-name :: template nested-name-specifier [opt]
4426 nested-name-specifier: [C++0x]
4429 nested-name-specifier identifier ::
4430 nested-name-specifier template [opt] simple-template-id ::
4432 PARSER->SCOPE should be set appropriately before this function is
4433 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4434 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4437 Sets PARSER->SCOPE to the class (TYPE) or namespace
4438 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4439 it unchanged if there is no nested-name-specifier. Returns the new
4440 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4442 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4443 part of a declaration and/or decl-specifier. */
4446 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4447 bool typename_keyword_p,
4448 bool check_dependency_p,
4450 bool is_declaration)
4452 bool success = false;
4453 cp_token_position start = 0;
4456 /* Remember where the nested-name-specifier starts. */
4457 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4459 start = cp_lexer_token_position (parser->lexer, false);
4460 push_deferring_access_checks (dk_deferred);
4467 tree saved_qualifying_scope;
4468 bool template_keyword_p;
4470 /* Spot cases that cannot be the beginning of a
4471 nested-name-specifier. */
4472 token = cp_lexer_peek_token (parser->lexer);
4474 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4475 the already parsed nested-name-specifier. */
4476 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4478 /* Grab the nested-name-specifier and continue the loop. */
4479 cp_parser_pre_parsed_nested_name_specifier (parser);
4480 /* If we originally encountered this nested-name-specifier
4481 with IS_DECLARATION set to false, we will not have
4482 resolved TYPENAME_TYPEs, so we must do so here. */
4484 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4486 new_scope = resolve_typename_type (parser->scope,
4487 /*only_current_p=*/false);
4488 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4489 parser->scope = new_scope;
4495 /* Spot cases that cannot be the beginning of a
4496 nested-name-specifier. On the second and subsequent times
4497 through the loop, we look for the `template' keyword. */
4498 if (success && token->keyword == RID_TEMPLATE)
4500 /* A template-id can start a nested-name-specifier. */
4501 else if (token->type == CPP_TEMPLATE_ID)
4505 /* If the next token is not an identifier, then it is
4506 definitely not a type-name or namespace-name. */
4507 if (token->type != CPP_NAME)
4509 /* If the following token is neither a `<' (to begin a
4510 template-id), nor a `::', then we are not looking at a
4511 nested-name-specifier. */
4512 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4513 if (token->type != CPP_SCOPE
4514 && !cp_parser_nth_token_starts_template_argument_list_p
4519 /* The nested-name-specifier is optional, so we parse
4521 cp_parser_parse_tentatively (parser);
4523 /* Look for the optional `template' keyword, if this isn't the
4524 first time through the loop. */
4526 template_keyword_p = cp_parser_optional_template_keyword (parser);
4528 template_keyword_p = false;
4530 /* Save the old scope since the name lookup we are about to do
4531 might destroy it. */
4532 old_scope = parser->scope;
4533 saved_qualifying_scope = parser->qualifying_scope;
4534 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4535 look up names in "X<T>::I" in order to determine that "Y" is
4536 a template. So, if we have a typename at this point, we make
4537 an effort to look through it. */
4539 && !typename_keyword_p
4541 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4542 parser->scope = resolve_typename_type (parser->scope,
4543 /*only_current_p=*/false);
4544 /* Parse the qualifying entity. */
4546 = cp_parser_qualifying_entity (parser,
4552 /* Look for the `::' token. */
4553 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4555 /* If we found what we wanted, we keep going; otherwise, we're
4557 if (!cp_parser_parse_definitely (parser))
4559 bool error_p = false;
4561 /* Restore the OLD_SCOPE since it was valid before the
4562 failed attempt at finding the last
4563 class-or-namespace-name. */
4564 parser->scope = old_scope;
4565 parser->qualifying_scope = saved_qualifying_scope;
4566 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4568 /* If the next token is an identifier, and the one after
4569 that is a `::', then any valid interpretation would have
4570 found a class-or-namespace-name. */
4571 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4572 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4574 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4577 token = cp_lexer_consume_token (parser->lexer);
4580 if (!token->ambiguous_p)
4583 tree ambiguous_decls;
4585 decl = cp_parser_lookup_name (parser, token->u.value,
4587 /*is_template=*/false,
4588 /*is_namespace=*/false,
4589 /*check_dependency=*/true,
4592 if (TREE_CODE (decl) == TEMPLATE_DECL)
4593 error_at (token->location,
4594 "%qD used without template parameters",
4596 else if (ambiguous_decls)
4598 error_at (token->location,
4599 "reference to %qD is ambiguous",
4601 print_candidates (ambiguous_decls);
4602 decl = error_mark_node;
4606 if (cxx_dialect != cxx98)
4607 cp_parser_name_lookup_error
4608 (parser, token->u.value, decl, NLE_NOT_CXX98,
4611 cp_parser_name_lookup_error
4612 (parser, token->u.value, decl, NLE_CXX98,
4616 parser->scope = error_mark_node;
4618 /* Treat this as a successful nested-name-specifier
4623 If the name found is not a class-name (clause
4624 _class_) or namespace-name (_namespace.def_), the
4625 program is ill-formed. */
4628 cp_lexer_consume_token (parser->lexer);
4632 /* We've found one valid nested-name-specifier. */
4634 /* Name lookup always gives us a DECL. */
4635 if (TREE_CODE (new_scope) == TYPE_DECL)
4636 new_scope = TREE_TYPE (new_scope);
4637 /* Uses of "template" must be followed by actual templates. */
4638 if (template_keyword_p
4639 && !(CLASS_TYPE_P (new_scope)
4640 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4641 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4642 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4643 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4644 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4645 == TEMPLATE_ID_EXPR)))
4646 permerror (input_location, TYPE_P (new_scope)
4647 ? "%qT is not a template"
4648 : "%qD is not a template",
4650 /* If it is a class scope, try to complete it; we are about to
4651 be looking up names inside the class. */
4652 if (TYPE_P (new_scope)
4653 /* Since checking types for dependency can be expensive,
4654 avoid doing it if the type is already complete. */
4655 && !COMPLETE_TYPE_P (new_scope)
4656 /* Do not try to complete dependent types. */
4657 && !dependent_type_p (new_scope))
4659 new_scope = complete_type (new_scope);
4660 /* If it is a typedef to current class, use the current
4661 class instead, as the typedef won't have any names inside
4663 if (!COMPLETE_TYPE_P (new_scope)
4664 && currently_open_class (new_scope))
4665 new_scope = TYPE_MAIN_VARIANT (new_scope);
4667 /* Make sure we look in the right scope the next time through
4669 parser->scope = new_scope;
4672 /* If parsing tentatively, replace the sequence of tokens that makes
4673 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4674 token. That way, should we re-parse the token stream, we will
4675 not have to repeat the effort required to do the parse, nor will
4676 we issue duplicate error messages. */
4677 if (success && start)
4681 token = cp_lexer_token_at (parser->lexer, start);
4682 /* Reset the contents of the START token. */
4683 token->type = CPP_NESTED_NAME_SPECIFIER;
4684 /* Retrieve any deferred checks. Do not pop this access checks yet
4685 so the memory will not be reclaimed during token replacing below. */
4686 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4687 token->u.tree_check_value->value = parser->scope;
4688 token->u.tree_check_value->checks = get_deferred_access_checks ();
4689 token->u.tree_check_value->qualifying_scope =
4690 parser->qualifying_scope;
4691 token->keyword = RID_MAX;
4693 /* Purge all subsequent tokens. */
4694 cp_lexer_purge_tokens_after (parser->lexer, start);
4698 pop_to_parent_deferring_access_checks ();
4700 return success ? parser->scope : NULL_TREE;
4703 /* Parse a nested-name-specifier. See
4704 cp_parser_nested_name_specifier_opt for details. This function
4705 behaves identically, except that it will an issue an error if no
4706 nested-name-specifier is present. */
4709 cp_parser_nested_name_specifier (cp_parser *parser,
4710 bool typename_keyword_p,
4711 bool check_dependency_p,
4713 bool is_declaration)
4717 /* Look for the nested-name-specifier. */
4718 scope = cp_parser_nested_name_specifier_opt (parser,
4723 /* If it was not present, issue an error message. */
4726 cp_parser_error (parser, "expected nested-name-specifier");
4727 parser->scope = NULL_TREE;
4733 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4734 this is either a class-name or a namespace-name (which corresponds
4735 to the class-or-namespace-name production in the grammar). For
4736 C++0x, it can also be a type-name that refers to an enumeration
4739 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4740 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4741 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4742 TYPE_P is TRUE iff the next name should be taken as a class-name,
4743 even the same name is declared to be another entity in the same
4746 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4747 specified by the class-or-namespace-name. If neither is found the
4748 ERROR_MARK_NODE is returned. */
4751 cp_parser_qualifying_entity (cp_parser *parser,
4752 bool typename_keyword_p,
4753 bool template_keyword_p,
4754 bool check_dependency_p,
4756 bool is_declaration)
4759 tree saved_qualifying_scope;
4760 tree saved_object_scope;
4763 bool successful_parse_p;
4765 /* Before we try to parse the class-name, we must save away the
4766 current PARSER->SCOPE since cp_parser_class_name will destroy
4768 saved_scope = parser->scope;
4769 saved_qualifying_scope = parser->qualifying_scope;
4770 saved_object_scope = parser->object_scope;
4771 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4772 there is no need to look for a namespace-name. */
4773 only_class_p = template_keyword_p
4774 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4776 cp_parser_parse_tentatively (parser);
4777 scope = cp_parser_class_name (parser,
4780 type_p ? class_type : none_type,
4782 /*class_head_p=*/false,
4784 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4785 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4787 && cxx_dialect != cxx98
4788 && !successful_parse_p)
4790 /* Restore the saved scope. */
4791 parser->scope = saved_scope;
4792 parser->qualifying_scope = saved_qualifying_scope;
4793 parser->object_scope = saved_object_scope;
4795 /* Parse tentatively. */
4796 cp_parser_parse_tentatively (parser);
4798 /* Parse a typedef-name or enum-name. */
4799 scope = cp_parser_nonclass_name (parser);
4801 /* "If the name found does not designate a namespace or a class,
4802 enumeration, or dependent type, the program is ill-formed."
4804 We cover classes and dependent types above and namespaces below,
4805 so this code is only looking for enums. */
4806 if (!scope || TREE_CODE (scope) != TYPE_DECL
4807 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4808 cp_parser_simulate_error (parser);
4810 successful_parse_p = cp_parser_parse_definitely (parser);
4812 /* If that didn't work, try for a namespace-name. */
4813 if (!only_class_p && !successful_parse_p)
4815 /* Restore the saved scope. */
4816 parser->scope = saved_scope;
4817 parser->qualifying_scope = saved_qualifying_scope;
4818 parser->object_scope = saved_object_scope;
4819 /* If we are not looking at an identifier followed by the scope
4820 resolution operator, then this is not part of a
4821 nested-name-specifier. (Note that this function is only used
4822 to parse the components of a nested-name-specifier.) */
4823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4824 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4825 return error_mark_node;
4826 scope = cp_parser_namespace_name (parser);
4832 /* Parse a postfix-expression.
4836 postfix-expression [ expression ]
4837 postfix-expression ( expression-list [opt] )
4838 simple-type-specifier ( expression-list [opt] )
4839 typename :: [opt] nested-name-specifier identifier
4840 ( expression-list [opt] )
4841 typename :: [opt] nested-name-specifier template [opt] template-id
4842 ( expression-list [opt] )
4843 postfix-expression . template [opt] id-expression
4844 postfix-expression -> template [opt] id-expression
4845 postfix-expression . pseudo-destructor-name
4846 postfix-expression -> pseudo-destructor-name
4847 postfix-expression ++
4848 postfix-expression --
4849 dynamic_cast < type-id > ( expression )
4850 static_cast < type-id > ( expression )
4851 reinterpret_cast < type-id > ( expression )
4852 const_cast < type-id > ( expression )
4853 typeid ( expression )
4859 ( type-id ) { initializer-list , [opt] }
4861 This extension is a GNU version of the C99 compound-literal
4862 construct. (The C99 grammar uses `type-name' instead of `type-id',
4863 but they are essentially the same concept.)
4865 If ADDRESS_P is true, the postfix expression is the operand of the
4866 `&' operator. CAST_P is true if this expression is the target of a
4869 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4870 class member access expressions [expr.ref].
4872 Returns a representation of the expression. */
4875 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4876 bool member_access_only_p,
4877 cp_id_kind * pidk_return)
4881 cp_id_kind idk = CP_ID_KIND_NONE;
4882 tree postfix_expression = NULL_TREE;
4883 bool is_member_access = false;
4885 /* Peek at the next token. */
4886 token = cp_lexer_peek_token (parser->lexer);
4887 /* Some of the productions are determined by keywords. */
4888 keyword = token->keyword;
4898 const char *saved_message;
4900 /* All of these can be handled in the same way from the point
4901 of view of parsing. Begin by consuming the token
4902 identifying the cast. */
4903 cp_lexer_consume_token (parser->lexer);
4905 /* New types cannot be defined in the cast. */
4906 saved_message = parser->type_definition_forbidden_message;
4907 parser->type_definition_forbidden_message
4908 = G_("types may not be defined in casts");
4910 /* Look for the opening `<'. */
4911 cp_parser_require (parser, CPP_LESS, RT_LESS);
4912 /* Parse the type to which we are casting. */
4913 type = cp_parser_type_id (parser);
4914 /* Look for the closing `>'. */
4915 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4916 /* Restore the old message. */
4917 parser->type_definition_forbidden_message = saved_message;
4919 /* And the expression which is being cast. */
4920 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4921 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4924 /* Only type conversions to integral or enumeration types
4925 can be used in constant-expressions. */
4926 if (!cast_valid_in_integral_constant_expression_p (type)
4927 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4928 return error_mark_node;
4934 = build_dynamic_cast (type, expression, tf_warning_or_error);
4938 = build_static_cast (type, expression, tf_warning_or_error);
4942 = build_reinterpret_cast (type, expression,
4943 tf_warning_or_error);
4947 = build_const_cast (type, expression, tf_warning_or_error);
4958 const char *saved_message;
4959 bool saved_in_type_id_in_expr_p;
4961 /* Consume the `typeid' token. */
4962 cp_lexer_consume_token (parser->lexer);
4963 /* Look for the `(' token. */
4964 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4965 /* Types cannot be defined in a `typeid' expression. */
4966 saved_message = parser->type_definition_forbidden_message;
4967 parser->type_definition_forbidden_message
4968 = G_("types may not be defined in a %<typeid%> expression");
4969 /* We can't be sure yet whether we're looking at a type-id or an
4971 cp_parser_parse_tentatively (parser);
4972 /* Try a type-id first. */
4973 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4974 parser->in_type_id_in_expr_p = true;
4975 type = cp_parser_type_id (parser);
4976 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4977 /* Look for the `)' token. Otherwise, we can't be sure that
4978 we're not looking at an expression: consider `typeid (int
4979 (3))', for example. */
4980 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4981 /* If all went well, simply lookup the type-id. */
4982 if (cp_parser_parse_definitely (parser))
4983 postfix_expression = get_typeid (type);
4984 /* Otherwise, fall back to the expression variant. */
4989 /* Look for an expression. */
4990 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4991 /* Compute its typeid. */
4992 postfix_expression = build_typeid (expression);
4993 /* Look for the `)' token. */
4994 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4996 /* Restore the saved message. */
4997 parser->type_definition_forbidden_message = saved_message;
4998 /* `typeid' may not appear in an integral constant expression. */
4999 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5000 return error_mark_node;
5007 /* The syntax permitted here is the same permitted for an
5008 elaborated-type-specifier. */
5009 type = cp_parser_elaborated_type_specifier (parser,
5010 /*is_friend=*/false,
5011 /*is_declaration=*/false);
5012 postfix_expression = cp_parser_functional_cast (parser, type);
5020 /* If the next thing is a simple-type-specifier, we may be
5021 looking at a functional cast. We could also be looking at
5022 an id-expression. So, we try the functional cast, and if
5023 that doesn't work we fall back to the primary-expression. */
5024 cp_parser_parse_tentatively (parser);
5025 /* Look for the simple-type-specifier. */
5026 type = cp_parser_simple_type_specifier (parser,
5027 /*decl_specs=*/NULL,
5028 CP_PARSER_FLAGS_NONE);
5029 /* Parse the cast itself. */
5030 if (!cp_parser_error_occurred (parser))
5032 = cp_parser_functional_cast (parser, type);
5033 /* If that worked, we're done. */
5034 if (cp_parser_parse_definitely (parser))
5037 /* If the functional-cast didn't work out, try a
5038 compound-literal. */
5039 if (cp_parser_allow_gnu_extensions_p (parser)
5040 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5042 VEC(constructor_elt,gc) *initializer_list = NULL;
5043 bool saved_in_type_id_in_expr_p;
5045 cp_parser_parse_tentatively (parser);
5046 /* Consume the `('. */
5047 cp_lexer_consume_token (parser->lexer);
5048 /* Parse the type. */
5049 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5050 parser->in_type_id_in_expr_p = true;
5051 type = cp_parser_type_id (parser);
5052 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5053 /* Look for the `)'. */
5054 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5055 /* Look for the `{'. */
5056 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5057 /* If things aren't going well, there's no need to
5059 if (!cp_parser_error_occurred (parser))
5061 bool non_constant_p;
5062 /* Parse the initializer-list. */
5064 = cp_parser_initializer_list (parser, &non_constant_p);
5065 /* Allow a trailing `,'. */
5066 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5067 cp_lexer_consume_token (parser->lexer);
5068 /* Look for the final `}'. */
5069 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5071 /* If that worked, we're definitely looking at a
5072 compound-literal expression. */
5073 if (cp_parser_parse_definitely (parser))
5075 /* Warn the user that a compound literal is not
5076 allowed in standard C++. */
5077 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5078 /* For simplicity, we disallow compound literals in
5079 constant-expressions. We could
5080 allow compound literals of integer type, whose
5081 initializer was a constant, in constant
5082 expressions. Permitting that usage, as a further
5083 extension, would not change the meaning of any
5084 currently accepted programs. (Of course, as
5085 compound literals are not part of ISO C++, the
5086 standard has nothing to say.) */
5087 if (cp_parser_non_integral_constant_expression (parser,
5090 postfix_expression = error_mark_node;
5093 /* Form the representation of the compound-literal. */
5095 = (finish_compound_literal
5096 (type, build_constructor (init_list_type_node,
5097 initializer_list)));
5102 /* It must be a primary-expression. */
5104 = cp_parser_primary_expression (parser, address_p, cast_p,
5105 /*template_arg_p=*/false,
5111 /* Keep looping until the postfix-expression is complete. */
5114 if (idk == CP_ID_KIND_UNQUALIFIED
5115 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5116 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5117 /* It is not a Koenig lookup function call. */
5119 = unqualified_name_lookup_error (postfix_expression);
5121 /* Peek at the next token. */
5122 token = cp_lexer_peek_token (parser->lexer);
5124 switch (token->type)
5126 case CPP_OPEN_SQUARE:
5128 = cp_parser_postfix_open_square_expression (parser,
5131 idk = CP_ID_KIND_NONE;
5132 is_member_access = false;
5135 case CPP_OPEN_PAREN:
5136 /* postfix-expression ( expression-list [opt] ) */
5139 bool is_builtin_constant_p;
5140 bool saved_integral_constant_expression_p = false;
5141 bool saved_non_integral_constant_expression_p = false;
5144 is_member_access = false;
5146 is_builtin_constant_p
5147 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5148 if (is_builtin_constant_p)
5150 /* The whole point of __builtin_constant_p is to allow
5151 non-constant expressions to appear as arguments. */
5152 saved_integral_constant_expression_p
5153 = parser->integral_constant_expression_p;
5154 saved_non_integral_constant_expression_p
5155 = parser->non_integral_constant_expression_p;
5156 parser->integral_constant_expression_p = false;
5158 args = (cp_parser_parenthesized_expression_list
5160 /*cast_p=*/false, /*allow_expansion_p=*/true,
5161 /*non_constant_p=*/NULL));
5162 if (is_builtin_constant_p)
5164 parser->integral_constant_expression_p
5165 = saved_integral_constant_expression_p;
5166 parser->non_integral_constant_expression_p
5167 = saved_non_integral_constant_expression_p;
5172 postfix_expression = error_mark_node;
5176 /* Function calls are not permitted in
5177 constant-expressions. */
5178 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5179 && cp_parser_non_integral_constant_expression (parser,
5182 postfix_expression = error_mark_node;
5183 release_tree_vector (args);
5188 if (idk == CP_ID_KIND_UNQUALIFIED
5189 || idk == CP_ID_KIND_TEMPLATE_ID)
5191 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5193 if (!VEC_empty (tree, args))
5196 if (!any_type_dependent_arguments_p (args))
5198 = perform_koenig_lookup (postfix_expression, args,
5199 /*include_std=*/false);
5203 = unqualified_fn_lookup_error (postfix_expression);
5205 /* We do not perform argument-dependent lookup if
5206 normal lookup finds a non-function, in accordance
5207 with the expected resolution of DR 218. */
5208 else if (!VEC_empty (tree, args)
5209 && is_overloaded_fn (postfix_expression))
5211 tree fn = get_first_fn (postfix_expression);
5212 fn = STRIP_TEMPLATE (fn);
5214 /* Do not do argument dependent lookup if regular
5215 lookup finds a member function or a block-scope
5216 function declaration. [basic.lookup.argdep]/3 */
5217 if (!DECL_FUNCTION_MEMBER_P (fn)
5218 && !DECL_LOCAL_FUNCTION_P (fn))
5221 if (!any_type_dependent_arguments_p (args))
5223 = perform_koenig_lookup (postfix_expression, args,
5224 /*include_std=*/false);
5229 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5231 tree instance = TREE_OPERAND (postfix_expression, 0);
5232 tree fn = TREE_OPERAND (postfix_expression, 1);
5234 if (processing_template_decl
5235 && (type_dependent_expression_p (instance)
5236 || (!BASELINK_P (fn)
5237 && TREE_CODE (fn) != FIELD_DECL)
5238 || type_dependent_expression_p (fn)
5239 || any_type_dependent_arguments_p (args)))
5242 = build_nt_call_vec (postfix_expression, args);
5243 release_tree_vector (args);
5247 if (BASELINK_P (fn))
5250 = (build_new_method_call
5251 (instance, fn, &args, NULL_TREE,
5252 (idk == CP_ID_KIND_QUALIFIED
5253 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5255 tf_warning_or_error));
5259 = finish_call_expr (postfix_expression, &args,
5260 /*disallow_virtual=*/false,
5262 tf_warning_or_error);
5264 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5265 || TREE_CODE (postfix_expression) == MEMBER_REF
5266 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5267 postfix_expression = (build_offset_ref_call_from_tree
5268 (postfix_expression, &args));
5269 else if (idk == CP_ID_KIND_QUALIFIED)
5270 /* A call to a static class member, or a namespace-scope
5273 = finish_call_expr (postfix_expression, &args,
5274 /*disallow_virtual=*/true,
5276 tf_warning_or_error);
5278 /* All other function calls. */
5280 = finish_call_expr (postfix_expression, &args,
5281 /*disallow_virtual=*/false,
5283 tf_warning_or_error);
5285 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5286 idk = CP_ID_KIND_NONE;
5288 release_tree_vector (args);
5294 /* postfix-expression . template [opt] id-expression
5295 postfix-expression . pseudo-destructor-name
5296 postfix-expression -> template [opt] id-expression
5297 postfix-expression -> pseudo-destructor-name */
5299 /* Consume the `.' or `->' operator. */
5300 cp_lexer_consume_token (parser->lexer);
5303 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5308 is_member_access = true;
5312 /* postfix-expression ++ */
5313 /* Consume the `++' token. */
5314 cp_lexer_consume_token (parser->lexer);
5315 /* Generate a representation for the complete expression. */
5317 = finish_increment_expr (postfix_expression,
5318 POSTINCREMENT_EXPR);
5319 /* Increments may not appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5321 postfix_expression = error_mark_node;
5322 idk = CP_ID_KIND_NONE;
5323 is_member_access = false;
5326 case CPP_MINUS_MINUS:
5327 /* postfix-expression -- */
5328 /* Consume the `--' token. */
5329 cp_lexer_consume_token (parser->lexer);
5330 /* Generate a representation for the complete expression. */
5332 = finish_increment_expr (postfix_expression,
5333 POSTDECREMENT_EXPR);
5334 /* Decrements may not appear in constant-expressions. */
5335 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5336 postfix_expression = error_mark_node;
5337 idk = CP_ID_KIND_NONE;
5338 is_member_access = false;
5342 if (pidk_return != NULL)
5343 * pidk_return = idk;
5344 if (member_access_only_p)
5345 return is_member_access? postfix_expression : error_mark_node;
5347 return postfix_expression;
5351 /* We should never get here. */
5353 return error_mark_node;
5356 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5357 by cp_parser_builtin_offsetof. We're looking for
5359 postfix-expression [ expression ]
5361 FOR_OFFSETOF is set if we're being called in that context, which
5362 changes how we deal with integer constant expressions. */
5365 cp_parser_postfix_open_square_expression (cp_parser *parser,
5366 tree postfix_expression,
5371 /* Consume the `[' token. */
5372 cp_lexer_consume_token (parser->lexer);
5374 /* Parse the index expression. */
5375 /* ??? For offsetof, there is a question of what to allow here. If
5376 offsetof is not being used in an integral constant expression context,
5377 then we *could* get the right answer by computing the value at runtime.
5378 If we are in an integral constant expression context, then we might
5379 could accept any constant expression; hard to say without analysis.
5380 Rather than open the barn door too wide right away, allow only integer
5381 constant expressions here. */
5383 index = cp_parser_constant_expression (parser, false, NULL);
5385 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5387 /* Look for the closing `]'. */
5388 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5390 /* Build the ARRAY_REF. */
5391 postfix_expression = grok_array_decl (postfix_expression, index);
5393 /* When not doing offsetof, array references are not permitted in
5394 constant-expressions. */
5396 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5397 postfix_expression = error_mark_node;
5399 return postfix_expression;
5402 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5403 by cp_parser_builtin_offsetof. We're looking for
5405 postfix-expression . template [opt] id-expression
5406 postfix-expression . pseudo-destructor-name
5407 postfix-expression -> template [opt] id-expression
5408 postfix-expression -> pseudo-destructor-name
5410 FOR_OFFSETOF is set if we're being called in that context. That sorta
5411 limits what of the above we'll actually accept, but nevermind.
5412 TOKEN_TYPE is the "." or "->" token, which will already have been
5413 removed from the stream. */
5416 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5417 enum cpp_ttype token_type,
5418 tree postfix_expression,
5419 bool for_offsetof, cp_id_kind *idk,
5420 location_t location)
5424 bool pseudo_destructor_p;
5425 tree scope = NULL_TREE;
5427 /* If this is a `->' operator, dereference the pointer. */
5428 if (token_type == CPP_DEREF)
5429 postfix_expression = build_x_arrow (postfix_expression);
5430 /* Check to see whether or not the expression is type-dependent. */
5431 dependent_p = type_dependent_expression_p (postfix_expression);
5432 /* The identifier following the `->' or `.' is not qualified. */
5433 parser->scope = NULL_TREE;
5434 parser->qualifying_scope = NULL_TREE;
5435 parser->object_scope = NULL_TREE;
5436 *idk = CP_ID_KIND_NONE;
5438 /* Enter the scope corresponding to the type of the object
5439 given by the POSTFIX_EXPRESSION. */
5440 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5442 scope = TREE_TYPE (postfix_expression);
5443 /* According to the standard, no expression should ever have
5444 reference type. Unfortunately, we do not currently match
5445 the standard in this respect in that our internal representation
5446 of an expression may have reference type even when the standard
5447 says it does not. Therefore, we have to manually obtain the
5448 underlying type here. */
5449 scope = non_reference (scope);
5450 /* The type of the POSTFIX_EXPRESSION must be complete. */
5451 if (scope == unknown_type_node)
5453 error_at (location, "%qE does not have class type",
5454 postfix_expression);
5458 scope = complete_type_or_else (scope, NULL_TREE);
5459 /* Let the name lookup machinery know that we are processing a
5460 class member access expression. */
5461 parser->context->object_type = scope;
5462 /* If something went wrong, we want to be able to discern that case,
5463 as opposed to the case where there was no SCOPE due to the type
5464 of expression being dependent. */
5466 scope = error_mark_node;
5467 /* If the SCOPE was erroneous, make the various semantic analysis
5468 functions exit quickly -- and without issuing additional error
5470 if (scope == error_mark_node)
5471 postfix_expression = error_mark_node;
5474 /* Assume this expression is not a pseudo-destructor access. */
5475 pseudo_destructor_p = false;
5477 /* If the SCOPE is a scalar type, then, if this is a valid program,
5478 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5479 is type dependent, it can be pseudo-destructor-name or something else.
5480 Try to parse it as pseudo-destructor-name first. */
5481 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5486 cp_parser_parse_tentatively (parser);
5487 /* Parse the pseudo-destructor-name. */
5489 cp_parser_pseudo_destructor_name (parser, &s, &type);
5491 && (cp_parser_error_occurred (parser)
5492 || TREE_CODE (type) != TYPE_DECL
5493 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5494 cp_parser_abort_tentative_parse (parser);
5495 else if (cp_parser_parse_definitely (parser))
5497 pseudo_destructor_p = true;
5499 = finish_pseudo_destructor_expr (postfix_expression,
5500 s, TREE_TYPE (type));
5504 if (!pseudo_destructor_p)
5506 /* If the SCOPE is not a scalar type, we are looking at an
5507 ordinary class member access expression, rather than a
5508 pseudo-destructor-name. */
5510 cp_token *token = cp_lexer_peek_token (parser->lexer);
5511 /* Parse the id-expression. */
5512 name = (cp_parser_id_expression
5514 cp_parser_optional_template_keyword (parser),
5515 /*check_dependency_p=*/true,
5517 /*declarator_p=*/false,
5518 /*optional_p=*/false));
5519 /* In general, build a SCOPE_REF if the member name is qualified.
5520 However, if the name was not dependent and has already been
5521 resolved; there is no need to build the SCOPE_REF. For example;
5523 struct X { void f(); };
5524 template <typename T> void f(T* t) { t->X::f(); }
5526 Even though "t" is dependent, "X::f" is not and has been resolved
5527 to a BASELINK; there is no need to include scope information. */
5529 /* But we do need to remember that there was an explicit scope for
5530 virtual function calls. */
5532 *idk = CP_ID_KIND_QUALIFIED;
5534 /* If the name is a template-id that names a type, we will get a
5535 TYPE_DECL here. That is invalid code. */
5536 if (TREE_CODE (name) == TYPE_DECL)
5538 error_at (token->location, "invalid use of %qD", name);
5539 postfix_expression = error_mark_node;
5543 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5545 name = build_qualified_name (/*type=*/NULL_TREE,
5549 parser->scope = NULL_TREE;
5550 parser->qualifying_scope = NULL_TREE;
5551 parser->object_scope = NULL_TREE;
5553 if (scope && name && BASELINK_P (name))
5554 adjust_result_of_qualified_name_lookup
5555 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5557 = finish_class_member_access_expr (postfix_expression, name,
5559 tf_warning_or_error);
5563 /* We no longer need to look up names in the scope of the object on
5564 the left-hand side of the `.' or `->' operator. */
5565 parser->context->object_type = NULL_TREE;
5567 /* Outside of offsetof, these operators may not appear in
5568 constant-expressions. */
5570 && (cp_parser_non_integral_constant_expression
5571 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5572 postfix_expression = error_mark_node;
5574 return postfix_expression;
5577 /* Parse a parenthesized expression-list.
5580 assignment-expression
5581 expression-list, assignment-expression
5586 identifier, expression-list
5588 CAST_P is true if this expression is the target of a cast.
5590 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5593 Returns a vector of trees. Each element is a representation of an
5594 assignment-expression. NULL is returned if the ( and or ) are
5595 missing. An empty, but allocated, vector is returned on no
5596 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5597 if we are parsing an attribute list for an attribute that wants a
5598 plain identifier argument, normal_attr for an attribute that wants
5599 an expression, or non_attr if we aren't parsing an attribute list. If
5600 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5601 not all of the expressions in the list were constant. */
5603 static VEC(tree,gc) *
5604 cp_parser_parenthesized_expression_list (cp_parser* parser,
5605 int is_attribute_list,
5607 bool allow_expansion_p,
5608 bool *non_constant_p)
5610 VEC(tree,gc) *expression_list;
5611 bool fold_expr_p = is_attribute_list != non_attr;
5612 tree identifier = NULL_TREE;
5613 bool saved_greater_than_is_operator_p;
5615 /* Assume all the expressions will be constant. */
5617 *non_constant_p = false;
5619 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5622 expression_list = make_tree_vector ();
5624 /* Within a parenthesized expression, a `>' token is always
5625 the greater-than operator. */
5626 saved_greater_than_is_operator_p
5627 = parser->greater_than_is_operator_p;
5628 parser->greater_than_is_operator_p = true;
5630 /* Consume expressions until there are no more. */
5631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5636 /* At the beginning of attribute lists, check to see if the
5637 next token is an identifier. */
5638 if (is_attribute_list == id_attr
5639 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5643 /* Consume the identifier. */
5644 token = cp_lexer_consume_token (parser->lexer);
5645 /* Save the identifier. */
5646 identifier = token->u.value;
5650 bool expr_non_constant_p;
5652 /* Parse the next assignment-expression. */
5653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5655 /* A braced-init-list. */
5656 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5657 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5658 if (non_constant_p && expr_non_constant_p)
5659 *non_constant_p = true;
5661 else if (non_constant_p)
5663 expr = (cp_parser_constant_expression
5664 (parser, /*allow_non_constant_p=*/true,
5665 &expr_non_constant_p));
5666 if (expr_non_constant_p)
5667 *non_constant_p = true;
5670 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5673 expr = fold_non_dependent_expr (expr);
5675 /* If we have an ellipsis, then this is an expression
5677 if (allow_expansion_p
5678 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5680 /* Consume the `...'. */
5681 cp_lexer_consume_token (parser->lexer);
5683 /* Build the argument pack. */
5684 expr = make_pack_expansion (expr);
5687 /* Add it to the list. We add error_mark_node
5688 expressions to the list, so that we can still tell if
5689 the correct form for a parenthesized expression-list
5690 is found. That gives better errors. */
5691 VEC_safe_push (tree, gc, expression_list, expr);
5693 if (expr == error_mark_node)
5697 /* After the first item, attribute lists look the same as
5698 expression lists. */
5699 is_attribute_list = non_attr;
5702 /* If the next token isn't a `,', then we are done. */
5703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5706 /* Otherwise, consume the `,' and keep going. */
5707 cp_lexer_consume_token (parser->lexer);
5710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5715 /* We try and resync to an unnested comma, as that will give the
5716 user better diagnostics. */
5717 ending = cp_parser_skip_to_closing_parenthesis (parser,
5718 /*recovering=*/true,
5720 /*consume_paren=*/true);
5725 parser->greater_than_is_operator_p
5726 = saved_greater_than_is_operator_p;
5731 parser->greater_than_is_operator_p
5732 = saved_greater_than_is_operator_p;
5735 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5737 return expression_list;
5740 /* Parse a pseudo-destructor-name.
5742 pseudo-destructor-name:
5743 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5744 :: [opt] nested-name-specifier template template-id :: ~ type-name
5745 :: [opt] nested-name-specifier [opt] ~ type-name
5747 If either of the first two productions is used, sets *SCOPE to the
5748 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5749 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5750 or ERROR_MARK_NODE if the parse fails. */
5753 cp_parser_pseudo_destructor_name (cp_parser* parser,
5757 bool nested_name_specifier_p;
5759 /* Assume that things will not work out. */
5760 *type = error_mark_node;
5762 /* Look for the optional `::' operator. */
5763 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5764 /* Look for the optional nested-name-specifier. */
5765 nested_name_specifier_p
5766 = (cp_parser_nested_name_specifier_opt (parser,
5767 /*typename_keyword_p=*/false,
5768 /*check_dependency_p=*/true,
5770 /*is_declaration=*/false)
5772 /* Now, if we saw a nested-name-specifier, we might be doing the
5773 second production. */
5774 if (nested_name_specifier_p
5775 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5777 /* Consume the `template' keyword. */
5778 cp_lexer_consume_token (parser->lexer);
5779 /* Parse the template-id. */
5780 cp_parser_template_id (parser,
5781 /*template_keyword_p=*/true,
5782 /*check_dependency_p=*/false,
5783 /*is_declaration=*/true);
5784 /* Look for the `::' token. */
5785 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5787 /* If the next token is not a `~', then there might be some
5788 additional qualification. */
5789 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5791 /* At this point, we're looking for "type-name :: ~". The type-name
5792 must not be a class-name, since this is a pseudo-destructor. So,
5793 it must be either an enum-name, or a typedef-name -- both of which
5794 are just identifiers. So, we peek ahead to check that the "::"
5795 and "~" tokens are present; if they are not, then we can avoid
5796 calling type_name. */
5797 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5798 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5799 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5801 cp_parser_error (parser, "non-scalar type");
5805 /* Look for the type-name. */
5806 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5807 if (*scope == error_mark_node)
5810 /* Look for the `::' token. */
5811 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5816 /* Look for the `~'. */
5817 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5818 /* Look for the type-name again. We are not responsible for
5819 checking that it matches the first type-name. */
5820 *type = cp_parser_nonclass_name (parser);
5823 /* Parse a unary-expression.
5829 unary-operator cast-expression
5830 sizeof unary-expression
5838 __extension__ cast-expression
5839 __alignof__ unary-expression
5840 __alignof__ ( type-id )
5841 __real__ cast-expression
5842 __imag__ cast-expression
5845 ADDRESS_P is true iff the unary-expression is appearing as the
5846 operand of the `&' operator. CAST_P is true if this expression is
5847 the target of a cast.
5849 Returns a representation of the expression. */
5852 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5856 enum tree_code unary_operator;
5858 /* Peek at the next token. */
5859 token = cp_lexer_peek_token (parser->lexer);
5860 /* Some keywords give away the kind of expression. */
5861 if (token->type == CPP_KEYWORD)
5863 enum rid keyword = token->keyword;
5873 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5874 /* Consume the token. */
5875 cp_lexer_consume_token (parser->lexer);
5876 /* Parse the operand. */
5877 operand = cp_parser_sizeof_operand (parser, keyword);
5879 if (TYPE_P (operand))
5880 return cxx_sizeof_or_alignof_type (operand, op, true);
5882 return cxx_sizeof_or_alignof_expr (operand, op, true);
5886 return cp_parser_new_expression (parser);
5889 return cp_parser_delete_expression (parser);
5893 /* The saved value of the PEDANTIC flag. */
5897 /* Save away the PEDANTIC flag. */
5898 cp_parser_extension_opt (parser, &saved_pedantic);
5899 /* Parse the cast-expression. */
5900 expr = cp_parser_simple_cast_expression (parser);
5901 /* Restore the PEDANTIC flag. */
5902 pedantic = saved_pedantic;
5912 /* Consume the `__real__' or `__imag__' token. */
5913 cp_lexer_consume_token (parser->lexer);
5914 /* Parse the cast-expression. */
5915 expression = cp_parser_simple_cast_expression (parser);
5916 /* Create the complete representation. */
5917 return build_x_unary_op ((keyword == RID_REALPART
5918 ? REALPART_EXPR : IMAGPART_EXPR),
5920 tf_warning_or_error);
5927 const char *saved_message;
5928 bool saved_integral_constant_expression_p;
5929 bool saved_non_integral_constant_expression_p;
5930 bool saved_greater_than_is_operator_p;
5932 cp_lexer_consume_token (parser->lexer);
5933 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5935 saved_message = parser->type_definition_forbidden_message;
5936 parser->type_definition_forbidden_message
5937 = G_("types may not be defined in %<noexcept%> expressions");
5939 saved_integral_constant_expression_p
5940 = parser->integral_constant_expression_p;
5941 saved_non_integral_constant_expression_p
5942 = parser->non_integral_constant_expression_p;
5943 parser->integral_constant_expression_p = false;
5945 saved_greater_than_is_operator_p
5946 = parser->greater_than_is_operator_p;
5947 parser->greater_than_is_operator_p = true;
5949 ++cp_unevaluated_operand;
5950 ++c_inhibit_evaluation_warnings;
5951 expr = cp_parser_expression (parser, false, NULL);
5952 --c_inhibit_evaluation_warnings;
5953 --cp_unevaluated_operand;
5955 parser->greater_than_is_operator_p
5956 = saved_greater_than_is_operator_p;
5958 parser->integral_constant_expression_p
5959 = saved_integral_constant_expression_p;
5960 parser->non_integral_constant_expression_p
5961 = saved_non_integral_constant_expression_p;
5963 parser->type_definition_forbidden_message = saved_message;
5965 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5966 return finish_noexcept_expr (expr, tf_warning_or_error);
5974 /* Look for the `:: new' and `:: delete', which also signal the
5975 beginning of a new-expression, or delete-expression,
5976 respectively. If the next token is `::', then it might be one of
5978 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5982 /* See if the token after the `::' is one of the keywords in
5983 which we're interested. */
5984 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5985 /* If it's `new', we have a new-expression. */
5986 if (keyword == RID_NEW)
5987 return cp_parser_new_expression (parser);
5988 /* Similarly, for `delete'. */
5989 else if (keyword == RID_DELETE)
5990 return cp_parser_delete_expression (parser);
5993 /* Look for a unary operator. */
5994 unary_operator = cp_parser_unary_operator (token);
5995 /* The `++' and `--' operators can be handled similarly, even though
5996 they are not technically unary-operators in the grammar. */
5997 if (unary_operator == ERROR_MARK)
5999 if (token->type == CPP_PLUS_PLUS)
6000 unary_operator = PREINCREMENT_EXPR;
6001 else if (token->type == CPP_MINUS_MINUS)
6002 unary_operator = PREDECREMENT_EXPR;
6003 /* Handle the GNU address-of-label extension. */
6004 else if (cp_parser_allow_gnu_extensions_p (parser)
6005 && token->type == CPP_AND_AND)
6009 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6011 /* Consume the '&&' token. */
6012 cp_lexer_consume_token (parser->lexer);
6013 /* Look for the identifier. */
6014 identifier = cp_parser_identifier (parser);
6015 /* Create an expression representing the address. */
6016 expression = finish_label_address_expr (identifier, loc);
6017 if (cp_parser_non_integral_constant_expression (parser,
6019 expression = error_mark_node;
6023 if (unary_operator != ERROR_MARK)
6025 tree cast_expression;
6026 tree expression = error_mark_node;
6027 non_integral_constant non_constant_p = NIC_NONE;
6029 /* Consume the operator token. */
6030 token = cp_lexer_consume_token (parser->lexer);
6031 /* Parse the cast-expression. */
6033 = cp_parser_cast_expression (parser,
6034 unary_operator == ADDR_EXPR,
6035 /*cast_p=*/false, pidk);
6036 /* Now, build an appropriate representation. */
6037 switch (unary_operator)
6040 non_constant_p = NIC_STAR;
6041 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6042 tf_warning_or_error);
6046 non_constant_p = NIC_ADDR;
6049 expression = build_x_unary_op (unary_operator, cast_expression,
6050 tf_warning_or_error);
6053 case PREINCREMENT_EXPR:
6054 case PREDECREMENT_EXPR:
6055 non_constant_p = unary_operator == PREINCREMENT_EXPR
6056 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6058 case UNARY_PLUS_EXPR:
6060 case TRUTH_NOT_EXPR:
6061 expression = finish_unary_op_expr (unary_operator, cast_expression);
6068 if (non_constant_p != NIC_NONE
6069 && cp_parser_non_integral_constant_expression (parser,
6071 expression = error_mark_node;
6076 return cp_parser_postfix_expression (parser, address_p, cast_p,
6077 /*member_access_only_p=*/false,
6081 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6082 unary-operator, the corresponding tree code is returned. */
6084 static enum tree_code
6085 cp_parser_unary_operator (cp_token* token)
6087 switch (token->type)
6090 return INDIRECT_REF;
6096 return UNARY_PLUS_EXPR;
6102 return TRUTH_NOT_EXPR;
6105 return BIT_NOT_EXPR;
6112 /* Parse a new-expression.
6115 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6116 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6118 Returns a representation of the expression. */
6121 cp_parser_new_expression (cp_parser* parser)
6123 bool global_scope_p;
6124 VEC(tree,gc) *placement;
6126 VEC(tree,gc) *initializer;
6130 /* Look for the optional `::' operator. */
6132 = (cp_parser_global_scope_opt (parser,
6133 /*current_scope_valid_p=*/false)
6135 /* Look for the `new' operator. */
6136 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6137 /* There's no easy way to tell a new-placement from the
6138 `( type-id )' construct. */
6139 cp_parser_parse_tentatively (parser);
6140 /* Look for a new-placement. */
6141 placement = cp_parser_new_placement (parser);
6142 /* If that didn't work out, there's no new-placement. */
6143 if (!cp_parser_parse_definitely (parser))
6145 if (placement != NULL)
6146 release_tree_vector (placement);
6150 /* If the next token is a `(', then we have a parenthesized
6152 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6155 /* Consume the `('. */
6156 cp_lexer_consume_token (parser->lexer);
6157 /* Parse the type-id. */
6158 type = cp_parser_type_id (parser);
6159 /* Look for the closing `)'. */
6160 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6161 token = cp_lexer_peek_token (parser->lexer);
6162 /* There should not be a direct-new-declarator in this production,
6163 but GCC used to allowed this, so we check and emit a sensible error
6164 message for this case. */
6165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6167 error_at (token->location,
6168 "array bound forbidden after parenthesized type-id");
6169 inform (token->location,
6170 "try removing the parentheses around the type-id");
6171 cp_parser_direct_new_declarator (parser);
6175 /* Otherwise, there must be a new-type-id. */
6177 type = cp_parser_new_type_id (parser, &nelts);
6179 /* If the next token is a `(' or '{', then we have a new-initializer. */
6180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6181 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6182 initializer = cp_parser_new_initializer (parser);
6186 /* A new-expression may not appear in an integral constant
6188 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6189 ret = error_mark_node;
6192 /* Create a representation of the new-expression. */
6193 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6194 tf_warning_or_error);
6197 if (placement != NULL)
6198 release_tree_vector (placement);
6199 if (initializer != NULL)
6200 release_tree_vector (initializer);
6205 /* Parse a new-placement.
6210 Returns the same representation as for an expression-list. */
6212 static VEC(tree,gc) *
6213 cp_parser_new_placement (cp_parser* parser)
6215 VEC(tree,gc) *expression_list;
6217 /* Parse the expression-list. */
6218 expression_list = (cp_parser_parenthesized_expression_list
6219 (parser, non_attr, /*cast_p=*/false,
6220 /*allow_expansion_p=*/true,
6221 /*non_constant_p=*/NULL));
6223 return expression_list;
6226 /* Parse a new-type-id.
6229 type-specifier-seq new-declarator [opt]
6231 Returns the TYPE allocated. If the new-type-id indicates an array
6232 type, *NELTS is set to the number of elements in the last array
6233 bound; the TYPE will not include the last array bound. */
6236 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6238 cp_decl_specifier_seq type_specifier_seq;
6239 cp_declarator *new_declarator;
6240 cp_declarator *declarator;
6241 cp_declarator *outer_declarator;
6242 const char *saved_message;
6245 /* The type-specifier sequence must not contain type definitions.
6246 (It cannot contain declarations of new types either, but if they
6247 are not definitions we will catch that because they are not
6249 saved_message = parser->type_definition_forbidden_message;
6250 parser->type_definition_forbidden_message
6251 = G_("types may not be defined in a new-type-id");
6252 /* Parse the type-specifier-seq. */
6253 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6254 /*is_trailing_return=*/false,
6255 &type_specifier_seq);
6256 /* Restore the old message. */
6257 parser->type_definition_forbidden_message = saved_message;
6258 /* Parse the new-declarator. */
6259 new_declarator = cp_parser_new_declarator_opt (parser);
6261 /* Determine the number of elements in the last array dimension, if
6264 /* Skip down to the last array dimension. */
6265 declarator = new_declarator;
6266 outer_declarator = NULL;
6267 while (declarator && (declarator->kind == cdk_pointer
6268 || declarator->kind == cdk_ptrmem))
6270 outer_declarator = declarator;
6271 declarator = declarator->declarator;
6274 && declarator->kind == cdk_array
6275 && declarator->declarator
6276 && declarator->declarator->kind == cdk_array)
6278 outer_declarator = declarator;
6279 declarator = declarator->declarator;
6282 if (declarator && declarator->kind == cdk_array)
6284 *nelts = declarator->u.array.bounds;
6285 if (*nelts == error_mark_node)
6286 *nelts = integer_one_node;
6288 if (outer_declarator)
6289 outer_declarator->declarator = declarator->declarator;
6291 new_declarator = NULL;
6294 type = groktypename (&type_specifier_seq, new_declarator, false);
6298 /* Parse an (optional) new-declarator.
6301 ptr-operator new-declarator [opt]
6302 direct-new-declarator
6304 Returns the declarator. */
6306 static cp_declarator *
6307 cp_parser_new_declarator_opt (cp_parser* parser)
6309 enum tree_code code;
6311 cp_cv_quals cv_quals;
6313 /* We don't know if there's a ptr-operator next, or not. */
6314 cp_parser_parse_tentatively (parser);
6315 /* Look for a ptr-operator. */
6316 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6317 /* If that worked, look for more new-declarators. */
6318 if (cp_parser_parse_definitely (parser))
6320 cp_declarator *declarator;
6322 /* Parse another optional declarator. */
6323 declarator = cp_parser_new_declarator_opt (parser);
6325 return cp_parser_make_indirect_declarator
6326 (code, type, cv_quals, declarator);
6329 /* If the next token is a `[', there is a direct-new-declarator. */
6330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6331 return cp_parser_direct_new_declarator (parser);
6336 /* Parse a direct-new-declarator.
6338 direct-new-declarator:
6340 direct-new-declarator [constant-expression]
6344 static cp_declarator *
6345 cp_parser_direct_new_declarator (cp_parser* parser)
6347 cp_declarator *declarator = NULL;
6353 /* Look for the opening `['. */
6354 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6355 /* The first expression is not required to be constant. */
6358 cp_token *token = cp_lexer_peek_token (parser->lexer);
6359 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6360 /* The standard requires that the expression have integral
6361 type. DR 74 adds enumeration types. We believe that the
6362 real intent is that these expressions be handled like the
6363 expression in a `switch' condition, which also allows
6364 classes with a single conversion to integral or
6365 enumeration type. */
6366 if (!processing_template_decl)
6369 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6374 error_at (token->location,
6375 "expression in new-declarator must have integral "
6376 "or enumeration type");
6377 expression = error_mark_node;
6381 /* But all the other expressions must be. */
6384 = cp_parser_constant_expression (parser,
6385 /*allow_non_constant=*/false,
6387 /* Look for the closing `]'. */
6388 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6390 /* Add this bound to the declarator. */
6391 declarator = make_array_declarator (declarator, expression);
6393 /* If the next token is not a `[', then there are no more
6395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6402 /* Parse a new-initializer.
6405 ( expression-list [opt] )
6408 Returns a representation of the expression-list. */
6410 static VEC(tree,gc) *
6411 cp_parser_new_initializer (cp_parser* parser)
6413 VEC(tree,gc) *expression_list;
6415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6418 bool expr_non_constant_p;
6419 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6420 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6421 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6422 expression_list = make_tree_vector_single (t);
6425 expression_list = (cp_parser_parenthesized_expression_list
6426 (parser, non_attr, /*cast_p=*/false,
6427 /*allow_expansion_p=*/true,
6428 /*non_constant_p=*/NULL));
6430 return expression_list;
6433 /* Parse a delete-expression.
6436 :: [opt] delete cast-expression
6437 :: [opt] delete [ ] cast-expression
6439 Returns a representation of the expression. */
6442 cp_parser_delete_expression (cp_parser* parser)
6444 bool global_scope_p;
6448 /* Look for the optional `::' operator. */
6450 = (cp_parser_global_scope_opt (parser,
6451 /*current_scope_valid_p=*/false)
6453 /* Look for the `delete' keyword. */
6454 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6455 /* See if the array syntax is in use. */
6456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6458 /* Consume the `[' token. */
6459 cp_lexer_consume_token (parser->lexer);
6460 /* Look for the `]' token. */
6461 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6462 /* Remember that this is the `[]' construct. */
6468 /* Parse the cast-expression. */
6469 expression = cp_parser_simple_cast_expression (parser);
6471 /* A delete-expression may not appear in an integral constant
6473 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6474 return error_mark_node;
6476 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6479 /* Returns true if TOKEN may start a cast-expression and false
6483 cp_parser_token_starts_cast_expression (cp_token *token)
6485 switch (token->type)
6491 case CPP_CLOSE_SQUARE:
6492 case CPP_CLOSE_PAREN:
6493 case CPP_CLOSE_BRACE:
6497 case CPP_DEREF_STAR:
6505 case CPP_GREATER_EQ:
6525 /* '[' may start a primary-expression in obj-c++. */
6526 case CPP_OPEN_SQUARE:
6527 return c_dialect_objc ();
6534 /* Parse a cast-expression.
6538 ( type-id ) cast-expression
6540 ADDRESS_P is true iff the unary-expression is appearing as the
6541 operand of the `&' operator. CAST_P is true if this expression is
6542 the target of a cast.
6544 Returns a representation of the expression. */
6547 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6550 /* If it's a `(', then we might be looking at a cast. */
6551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6553 tree type = NULL_TREE;
6554 tree expr = NULL_TREE;
6555 bool compound_literal_p;
6556 const char *saved_message;
6558 /* There's no way to know yet whether or not this is a cast.
6559 For example, `(int (3))' is a unary-expression, while `(int)
6560 3' is a cast. So, we resort to parsing tentatively. */
6561 cp_parser_parse_tentatively (parser);
6562 /* Types may not be defined in a cast. */
6563 saved_message = parser->type_definition_forbidden_message;
6564 parser->type_definition_forbidden_message
6565 = G_("types may not be defined in casts");
6566 /* Consume the `('. */
6567 cp_lexer_consume_token (parser->lexer);
6568 /* A very tricky bit is that `(struct S) { 3 }' is a
6569 compound-literal (which we permit in C++ as an extension).
6570 But, that construct is not a cast-expression -- it is a
6571 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6572 is legal; if the compound-literal were a cast-expression,
6573 you'd need an extra set of parentheses.) But, if we parse
6574 the type-id, and it happens to be a class-specifier, then we
6575 will commit to the parse at that point, because we cannot
6576 undo the action that is done when creating a new class. So,
6577 then we cannot back up and do a postfix-expression.
6579 Therefore, we scan ahead to the closing `)', and check to see
6580 if the token after the `)' is a `{'. If so, we are not
6581 looking at a cast-expression.
6583 Save tokens so that we can put them back. */
6584 cp_lexer_save_tokens (parser->lexer);
6585 /* Skip tokens until the next token is a closing parenthesis.
6586 If we find the closing `)', and the next token is a `{', then
6587 we are looking at a compound-literal. */
6589 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6590 /*consume_paren=*/true)
6591 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6592 /* Roll back the tokens we skipped. */
6593 cp_lexer_rollback_tokens (parser->lexer);
6594 /* If we were looking at a compound-literal, simulate an error
6595 so that the call to cp_parser_parse_definitely below will
6597 if (compound_literal_p)
6598 cp_parser_simulate_error (parser);
6601 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6602 parser->in_type_id_in_expr_p = true;
6603 /* Look for the type-id. */
6604 type = cp_parser_type_id (parser);
6605 /* Look for the closing `)'. */
6606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6607 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6610 /* Restore the saved message. */
6611 parser->type_definition_forbidden_message = saved_message;
6613 /* At this point this can only be either a cast or a
6614 parenthesized ctor such as `(T ())' that looks like a cast to
6615 function returning T. */
6616 if (!cp_parser_error_occurred (parser)
6617 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6620 cp_parser_parse_definitely (parser);
6621 expr = cp_parser_cast_expression (parser,
6622 /*address_p=*/false,
6623 /*cast_p=*/true, pidk);
6625 /* Warn about old-style casts, if so requested. */
6626 if (warn_old_style_cast
6627 && !in_system_header
6628 && !VOID_TYPE_P (type)
6629 && current_lang_name != lang_name_c)
6630 warning (OPT_Wold_style_cast, "use of old-style cast");
6632 /* Only type conversions to integral or enumeration types
6633 can be used in constant-expressions. */
6634 if (!cast_valid_in_integral_constant_expression_p (type)
6635 && cp_parser_non_integral_constant_expression (parser,
6637 return error_mark_node;
6639 /* Perform the cast. */
6640 expr = build_c_cast (input_location, type, expr);
6644 cp_parser_abort_tentative_parse (parser);
6647 /* If we get here, then it's not a cast, so it must be a
6648 unary-expression. */
6649 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6652 /* Parse a binary expression of the general form:
6656 pm-expression .* cast-expression
6657 pm-expression ->* cast-expression
6659 multiplicative-expression:
6661 multiplicative-expression * pm-expression
6662 multiplicative-expression / pm-expression
6663 multiplicative-expression % pm-expression
6665 additive-expression:
6666 multiplicative-expression
6667 additive-expression + multiplicative-expression
6668 additive-expression - multiplicative-expression
6672 shift-expression << additive-expression
6673 shift-expression >> additive-expression
6675 relational-expression:
6677 relational-expression < shift-expression
6678 relational-expression > shift-expression
6679 relational-expression <= shift-expression
6680 relational-expression >= shift-expression
6684 relational-expression:
6685 relational-expression <? shift-expression
6686 relational-expression >? shift-expression
6688 equality-expression:
6689 relational-expression
6690 equality-expression == relational-expression
6691 equality-expression != relational-expression
6695 and-expression & equality-expression
6697 exclusive-or-expression:
6699 exclusive-or-expression ^ and-expression
6701 inclusive-or-expression:
6702 exclusive-or-expression
6703 inclusive-or-expression | exclusive-or-expression
6705 logical-and-expression:
6706 inclusive-or-expression
6707 logical-and-expression && inclusive-or-expression
6709 logical-or-expression:
6710 logical-and-expression
6711 logical-or-expression || logical-and-expression
6713 All these are implemented with a single function like:
6716 simple-cast-expression
6717 binary-expression <token> binary-expression
6719 CAST_P is true if this expression is the target of a cast.
6721 The binops_by_token map is used to get the tree codes for each <token> type.
6722 binary-expressions are associated according to a precedence table. */
6724 #define TOKEN_PRECEDENCE(token) \
6725 (((token->type == CPP_GREATER \
6726 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6727 && !parser->greater_than_is_operator_p) \
6728 ? PREC_NOT_OPERATOR \
6729 : binops_by_token[token->type].prec)
6732 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6733 bool no_toplevel_fold_p,
6734 enum cp_parser_prec prec,
6737 cp_parser_expression_stack stack;
6738 cp_parser_expression_stack_entry *sp = &stack[0];
6741 enum tree_code tree_type, lhs_type, rhs_type;
6742 enum cp_parser_prec new_prec, lookahead_prec;
6745 /* Parse the first expression. */
6746 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6747 lhs_type = ERROR_MARK;
6751 /* Get an operator token. */
6752 token = cp_lexer_peek_token (parser->lexer);
6754 if (warn_cxx0x_compat
6755 && token->type == CPP_RSHIFT
6756 && !parser->greater_than_is_operator_p)
6758 if (warning_at (token->location, OPT_Wc__0x_compat,
6759 "%<>>%> operator will be treated as"
6760 " two right angle brackets in C++0x"))
6761 inform (token->location,
6762 "suggest parentheses around %<>>%> expression");
6765 new_prec = TOKEN_PRECEDENCE (token);
6767 /* Popping an entry off the stack means we completed a subexpression:
6768 - either we found a token which is not an operator (`>' where it is not
6769 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6770 will happen repeatedly;
6771 - or, we found an operator which has lower priority. This is the case
6772 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6774 if (new_prec <= prec)
6783 tree_type = binops_by_token[token->type].tree_type;
6785 /* We used the operator token. */
6786 cp_lexer_consume_token (parser->lexer);
6788 /* For "false && x" or "true || x", x will never be executed;
6789 disable warnings while evaluating it. */
6790 if (tree_type == TRUTH_ANDIF_EXPR)
6791 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6792 else if (tree_type == TRUTH_ORIF_EXPR)
6793 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6795 /* Extract another operand. It may be the RHS of this expression
6796 or the LHS of a new, higher priority expression. */
6797 rhs = cp_parser_simple_cast_expression (parser);
6798 rhs_type = ERROR_MARK;
6800 /* Get another operator token. Look up its precedence to avoid
6801 building a useless (immediately popped) stack entry for common
6802 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6803 token = cp_lexer_peek_token (parser->lexer);
6804 lookahead_prec = TOKEN_PRECEDENCE (token);
6805 if (lookahead_prec > new_prec)
6807 /* ... and prepare to parse the RHS of the new, higher priority
6808 expression. Since precedence levels on the stack are
6809 monotonically increasing, we do not have to care about
6812 sp->tree_type = tree_type;
6814 sp->lhs_type = lhs_type;
6817 lhs_type = rhs_type;
6819 new_prec = lookahead_prec;
6823 lookahead_prec = new_prec;
6824 /* If the stack is not empty, we have parsed into LHS the right side
6825 (`4' in the example above) of an expression we had suspended.
6826 We can use the information on the stack to recover the LHS (`3')
6827 from the stack together with the tree code (`MULT_EXPR'), and
6828 the precedence of the higher level subexpression
6829 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6830 which will be used to actually build the additive expression. */
6833 tree_type = sp->tree_type;
6835 rhs_type = lhs_type;
6837 lhs_type = sp->lhs_type;
6840 /* Undo the disabling of warnings done above. */
6841 if (tree_type == TRUTH_ANDIF_EXPR)
6842 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6843 else if (tree_type == TRUTH_ORIF_EXPR)
6844 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6846 overloaded_p = false;
6847 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6848 ERROR_MARK for everything that is not a binary expression.
6849 This makes warn_about_parentheses miss some warnings that
6850 involve unary operators. For unary expressions we should
6851 pass the correct tree_code unless the unary expression was
6852 surrounded by parentheses.
6854 if (no_toplevel_fold_p
6855 && lookahead_prec <= prec
6857 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6858 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6860 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6861 &overloaded_p, tf_warning_or_error);
6862 lhs_type = tree_type;
6864 /* If the binary operator required the use of an overloaded operator,
6865 then this expression cannot be an integral constant-expression.
6866 An overloaded operator can be used even if both operands are
6867 otherwise permissible in an integral constant-expression if at
6868 least one of the operands is of enumeration type. */
6871 && cp_parser_non_integral_constant_expression (parser,
6873 return error_mark_node;
6880 /* Parse the `? expression : assignment-expression' part of a
6881 conditional-expression. The LOGICAL_OR_EXPR is the
6882 logical-or-expression that started the conditional-expression.
6883 Returns a representation of the entire conditional-expression.
6885 This routine is used by cp_parser_assignment_expression.
6887 ? expression : assignment-expression
6891 ? : assignment-expression */
6894 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6897 tree assignment_expr;
6898 struct cp_token *token;
6900 /* Consume the `?' token. */
6901 cp_lexer_consume_token (parser->lexer);
6902 token = cp_lexer_peek_token (parser->lexer);
6903 if (cp_parser_allow_gnu_extensions_p (parser)
6904 && token->type == CPP_COLON)
6906 pedwarn (token->location, OPT_pedantic,
6907 "ISO C++ does not allow ?: with omitted middle operand");
6908 /* Implicit true clause. */
6910 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6911 warn_for_omitted_condop (token->location, logical_or_expr);
6915 /* Parse the expression. */
6916 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6917 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6918 c_inhibit_evaluation_warnings +=
6919 ((logical_or_expr == truthvalue_true_node)
6920 - (logical_or_expr == truthvalue_false_node));
6923 /* The next token should be a `:'. */
6924 cp_parser_require (parser, CPP_COLON, RT_COLON);
6925 /* Parse the assignment-expression. */
6926 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6927 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6929 /* Build the conditional-expression. */
6930 return build_x_conditional_expr (logical_or_expr,
6933 tf_warning_or_error);
6936 /* Parse an assignment-expression.
6938 assignment-expression:
6939 conditional-expression
6940 logical-or-expression assignment-operator assignment_expression
6943 CAST_P is true if this expression is the target of a cast.
6945 Returns a representation for the expression. */
6948 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6953 /* If the next token is the `throw' keyword, then we're looking at
6954 a throw-expression. */
6955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6956 expr = cp_parser_throw_expression (parser);
6957 /* Otherwise, it must be that we are looking at a
6958 logical-or-expression. */
6961 /* Parse the binary expressions (logical-or-expression). */
6962 expr = cp_parser_binary_expression (parser, cast_p, false,
6963 PREC_NOT_OPERATOR, pidk);
6964 /* If the next token is a `?' then we're actually looking at a
6965 conditional-expression. */
6966 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6967 return cp_parser_question_colon_clause (parser, expr);
6970 enum tree_code assignment_operator;
6972 /* If it's an assignment-operator, we're using the second
6975 = cp_parser_assignment_operator_opt (parser);
6976 if (assignment_operator != ERROR_MARK)
6978 bool non_constant_p;
6980 /* Parse the right-hand side of the assignment. */
6981 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6983 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6986 /* An assignment may not appear in a
6987 constant-expression. */
6988 if (cp_parser_non_integral_constant_expression (parser,
6990 return error_mark_node;
6991 /* Build the assignment expression. */
6992 expr = build_x_modify_expr (expr,
6993 assignment_operator,
6995 tf_warning_or_error);
7003 /* Parse an (optional) assignment-operator.
7005 assignment-operator: one of
7006 = *= /= %= += -= >>= <<= &= ^= |=
7010 assignment-operator: one of
7013 If the next token is an assignment operator, the corresponding tree
7014 code is returned, and the token is consumed. For example, for
7015 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7016 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7017 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7018 operator, ERROR_MARK is returned. */
7020 static enum tree_code
7021 cp_parser_assignment_operator_opt (cp_parser* parser)
7026 /* Peek at the next token. */
7027 token = cp_lexer_peek_token (parser->lexer);
7029 switch (token->type)
7040 op = TRUNC_DIV_EXPR;
7044 op = TRUNC_MOD_EXPR;
7076 /* Nothing else is an assignment operator. */
7080 /* If it was an assignment operator, consume it. */
7081 if (op != ERROR_MARK)
7082 cp_lexer_consume_token (parser->lexer);
7087 /* Parse an expression.
7090 assignment-expression
7091 expression , assignment-expression
7093 CAST_P is true if this expression is the target of a cast.
7095 Returns a representation of the expression. */
7098 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7100 tree expression = NULL_TREE;
7104 tree assignment_expression;
7106 /* Parse the next assignment-expression. */
7107 assignment_expression
7108 = cp_parser_assignment_expression (parser, cast_p, pidk);
7109 /* If this is the first assignment-expression, we can just
7112 expression = assignment_expression;
7114 expression = build_x_compound_expr (expression,
7115 assignment_expression,
7116 tf_warning_or_error);
7117 /* If the next token is not a comma, then we are done with the
7119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7121 /* Consume the `,'. */
7122 cp_lexer_consume_token (parser->lexer);
7123 /* A comma operator cannot appear in a constant-expression. */
7124 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7125 expression = error_mark_node;
7131 /* Parse a constant-expression.
7133 constant-expression:
7134 conditional-expression
7136 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7137 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7138 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7139 is false, NON_CONSTANT_P should be NULL. */
7142 cp_parser_constant_expression (cp_parser* parser,
7143 bool allow_non_constant_p,
7144 bool *non_constant_p)
7146 bool saved_integral_constant_expression_p;
7147 bool saved_allow_non_integral_constant_expression_p;
7148 bool saved_non_integral_constant_expression_p;
7151 /* It might seem that we could simply parse the
7152 conditional-expression, and then check to see if it were
7153 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7154 one that the compiler can figure out is constant, possibly after
7155 doing some simplifications or optimizations. The standard has a
7156 precise definition of constant-expression, and we must honor
7157 that, even though it is somewhat more restrictive.
7163 is not a legal declaration, because `(2, 3)' is not a
7164 constant-expression. The `,' operator is forbidden in a
7165 constant-expression. However, GCC's constant-folding machinery
7166 will fold this operation to an INTEGER_CST for `3'. */
7168 /* Save the old settings. */
7169 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7170 saved_allow_non_integral_constant_expression_p
7171 = parser->allow_non_integral_constant_expression_p;
7172 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7173 /* We are now parsing a constant-expression. */
7174 parser->integral_constant_expression_p = true;
7175 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
7176 parser->non_integral_constant_expression_p = false;
7177 /* Although the grammar says "conditional-expression", we parse an
7178 "assignment-expression", which also permits "throw-expression"
7179 and the use of assignment operators. In the case that
7180 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7181 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7182 actually essential that we look for an assignment-expression.
7183 For example, cp_parser_initializer_clauses uses this function to
7184 determine whether a particular assignment-expression is in fact
7186 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7187 /* Restore the old settings. */
7188 parser->integral_constant_expression_p
7189 = saved_integral_constant_expression_p;
7190 parser->allow_non_integral_constant_expression_p
7191 = saved_allow_non_integral_constant_expression_p;
7192 if (allow_non_constant_p)
7193 *non_constant_p = parser->non_integral_constant_expression_p;
7194 else if (parser->non_integral_constant_expression_p)
7195 expression = error_mark_node;
7196 parser->non_integral_constant_expression_p
7197 = saved_non_integral_constant_expression_p;
7202 /* Parse __builtin_offsetof.
7204 offsetof-expression:
7205 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7207 offsetof-member-designator:
7209 | offsetof-member-designator "." id-expression
7210 | offsetof-member-designator "[" expression "]"
7211 | offsetof-member-designator "->" id-expression */
7214 cp_parser_builtin_offsetof (cp_parser *parser)
7216 int save_ice_p, save_non_ice_p;
7221 /* We're about to accept non-integral-constant things, but will
7222 definitely yield an integral constant expression. Save and
7223 restore these values around our local parsing. */
7224 save_ice_p = parser->integral_constant_expression_p;
7225 save_non_ice_p = parser->non_integral_constant_expression_p;
7227 /* Consume the "__builtin_offsetof" token. */
7228 cp_lexer_consume_token (parser->lexer);
7229 /* Consume the opening `('. */
7230 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7231 /* Parse the type-id. */
7232 type = cp_parser_type_id (parser);
7233 /* Look for the `,'. */
7234 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7235 token = cp_lexer_peek_token (parser->lexer);
7237 /* Build the (type *)null that begins the traditional offsetof macro. */
7238 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7239 tf_warning_or_error);
7241 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7242 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7243 true, &dummy, token->location);
7246 token = cp_lexer_peek_token (parser->lexer);
7247 switch (token->type)
7249 case CPP_OPEN_SQUARE:
7250 /* offsetof-member-designator "[" expression "]" */
7251 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7255 /* offsetof-member-designator "->" identifier */
7256 expr = grok_array_decl (expr, integer_zero_node);
7260 /* offsetof-member-designator "." identifier */
7261 cp_lexer_consume_token (parser->lexer);
7262 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7267 case CPP_CLOSE_PAREN:
7268 /* Consume the ")" token. */
7269 cp_lexer_consume_token (parser->lexer);
7273 /* Error. We know the following require will fail, but
7274 that gives the proper error message. */
7275 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7276 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7277 expr = error_mark_node;
7283 /* If we're processing a template, we can't finish the semantics yet.
7284 Otherwise we can fold the entire expression now. */
7285 if (processing_template_decl)
7286 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7288 expr = finish_offsetof (expr);
7291 parser->integral_constant_expression_p = save_ice_p;
7292 parser->non_integral_constant_expression_p = save_non_ice_p;
7297 /* Parse a trait expression. */
7300 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7303 tree type1, type2 = NULL_TREE;
7304 bool binary = false;
7305 cp_decl_specifier_seq decl_specs;
7309 case RID_HAS_NOTHROW_ASSIGN:
7310 kind = CPTK_HAS_NOTHROW_ASSIGN;
7312 case RID_HAS_NOTHROW_CONSTRUCTOR:
7313 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7315 case RID_HAS_NOTHROW_COPY:
7316 kind = CPTK_HAS_NOTHROW_COPY;
7318 case RID_HAS_TRIVIAL_ASSIGN:
7319 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7321 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7322 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7324 case RID_HAS_TRIVIAL_COPY:
7325 kind = CPTK_HAS_TRIVIAL_COPY;
7327 case RID_HAS_TRIVIAL_DESTRUCTOR:
7328 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7330 case RID_HAS_VIRTUAL_DESTRUCTOR:
7331 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7333 case RID_IS_ABSTRACT:
7334 kind = CPTK_IS_ABSTRACT;
7336 case RID_IS_BASE_OF:
7337 kind = CPTK_IS_BASE_OF;
7341 kind = CPTK_IS_CLASS;
7343 case RID_IS_CONVERTIBLE_TO:
7344 kind = CPTK_IS_CONVERTIBLE_TO;
7348 kind = CPTK_IS_EMPTY;
7351 kind = CPTK_IS_ENUM;
7356 case RID_IS_POLYMORPHIC:
7357 kind = CPTK_IS_POLYMORPHIC;
7359 case RID_IS_STD_LAYOUT:
7360 kind = CPTK_IS_STD_LAYOUT;
7362 case RID_IS_TRIVIAL:
7363 kind = CPTK_IS_TRIVIAL;
7366 kind = CPTK_IS_UNION;
7372 /* Consume the token. */
7373 cp_lexer_consume_token (parser->lexer);
7375 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7377 type1 = cp_parser_type_id (parser);
7379 if (type1 == error_mark_node)
7380 return error_mark_node;
7382 /* Build a trivial decl-specifier-seq. */
7383 clear_decl_specs (&decl_specs);
7384 decl_specs.type = type1;
7386 /* Call grokdeclarator to figure out what type this is. */
7387 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7388 /*initialized=*/0, /*attrlist=*/NULL);
7392 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7394 type2 = cp_parser_type_id (parser);
7396 if (type2 == error_mark_node)
7397 return error_mark_node;
7399 /* Build a trivial decl-specifier-seq. */
7400 clear_decl_specs (&decl_specs);
7401 decl_specs.type = type2;
7403 /* Call grokdeclarator to figure out what type this is. */
7404 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7405 /*initialized=*/0, /*attrlist=*/NULL);
7408 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7410 /* Complete the trait expression, which may mean either processing
7411 the trait expr now or saving it for template instantiation. */
7412 return finish_trait_expr (kind, type1, type2);
7415 /* Lambdas that appear in variable initializer or default argument scope
7416 get that in their mangling, so we need to record it. We might as well
7417 use the count for function and namespace scopes as well. */
7418 static GTY(()) tree lambda_scope;
7419 static GTY(()) int lambda_count;
7420 typedef struct GTY(()) tree_int
7425 DEF_VEC_O(tree_int);
7426 DEF_VEC_ALLOC_O(tree_int,gc);
7427 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7430 start_lambda_scope (tree decl)
7434 /* Once we're inside a function, we ignore other scopes and just push
7435 the function again so that popping works properly. */
7436 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7437 decl = current_function_decl;
7438 ti.t = lambda_scope;
7439 ti.i = lambda_count;
7440 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7441 if (lambda_scope != decl)
7443 /* Don't reset the count if we're still in the same function. */
7444 lambda_scope = decl;
7450 record_lambda_scope (tree lambda)
7452 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7453 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7457 finish_lambda_scope (void)
7459 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7460 if (lambda_scope != p->t)
7462 lambda_scope = p->t;
7463 lambda_count = p->i;
7465 VEC_pop (tree_int, lambda_scope_stack);
7468 /* Parse a lambda expression.
7471 lambda-introducer lambda-declarator [opt] compound-statement
7473 Returns a representation of the expression. */
7476 cp_parser_lambda_expression (cp_parser* parser)
7478 tree lambda_expr = build_lambda_expr ();
7481 LAMBDA_EXPR_LOCATION (lambda_expr)
7482 = cp_lexer_peek_token (parser->lexer)->location;
7484 if (cp_unevaluated_operand)
7485 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7486 "lambda-expression in unevaluated context");
7488 /* We may be in the middle of deferred access check. Disable
7490 push_deferring_access_checks (dk_no_deferred);
7492 cp_parser_lambda_introducer (parser, lambda_expr);
7494 type = begin_lambda_type (lambda_expr);
7496 record_lambda_scope (lambda_expr);
7498 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7499 determine_visibility (TYPE_NAME (type));
7501 /* Now that we've started the type, add the capture fields for any
7502 explicit captures. */
7503 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7506 /* Inside the class, surrounding template-parameter-lists do not apply. */
7507 unsigned int saved_num_template_parameter_lists
7508 = parser->num_template_parameter_lists;
7510 parser->num_template_parameter_lists = 0;
7512 /* By virtue of defining a local class, a lambda expression has access to
7513 the private variables of enclosing classes. */
7515 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7517 cp_parser_lambda_body (parser, lambda_expr);
7519 /* The capture list was built up in reverse order; fix that now. */
7521 tree newlist = NULL_TREE;
7524 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7527 tree field = TREE_PURPOSE (elt);
7530 next = TREE_CHAIN (elt);
7531 TREE_CHAIN (elt) = newlist;
7534 /* Also add __ to the beginning of the field name so that code
7535 outside the lambda body can't see the captured name. We could
7536 just remove the name entirely, but this is more useful for
7538 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7539 /* The 'this' capture already starts with __. */
7542 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7543 buf[1] = buf[0] = '_';
7544 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7545 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7546 DECL_NAME (field) = get_identifier (buf);
7548 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7551 maybe_add_lambda_conv_op (type);
7553 type = finish_struct (type, /*attributes=*/NULL_TREE);
7555 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7558 pop_deferring_access_checks ();
7560 return build_lambda_object (lambda_expr);
7563 /* Parse the beginning of a lambda expression.
7566 [ lambda-capture [opt] ]
7568 LAMBDA_EXPR is the current representation of the lambda expression. */
7571 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7573 /* Need commas after the first capture. */
7576 /* Eat the leading `['. */
7577 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7579 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7580 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7581 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7582 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7583 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7584 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7586 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7588 cp_lexer_consume_token (parser->lexer);
7592 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7594 cp_token* capture_token;
7596 tree capture_init_expr;
7597 cp_id_kind idk = CP_ID_KIND_NONE;
7598 bool explicit_init_p = false;
7600 enum capture_kind_type
7605 enum capture_kind_type capture_kind = BY_COPY;
7607 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7609 error ("expected end of capture-list");
7616 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7618 /* Possibly capture `this'. */
7619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7621 cp_lexer_consume_token (parser->lexer);
7622 add_capture (lambda_expr,
7623 /*id=*/get_identifier ("__this"),
7624 /*initializer=*/finish_this_expr(),
7625 /*by_reference_p=*/false,
7630 /* Remember whether we want to capture as a reference or not. */
7631 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7633 capture_kind = BY_REFERENCE;
7634 cp_lexer_consume_token (parser->lexer);
7637 /* Get the identifier. */
7638 capture_token = cp_lexer_peek_token (parser->lexer);
7639 capture_id = cp_parser_identifier (parser);
7641 if (capture_id == error_mark_node)
7642 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7643 delimiters, but I modified this to stop on unnested ']' as well. It
7644 was already changed to stop on unnested '}', so the
7645 "closing_parenthesis" name is no more misleading with my change. */
7647 cp_parser_skip_to_closing_parenthesis (parser,
7648 /*recovering=*/true,
7650 /*consume_paren=*/true);
7654 /* Find the initializer for this capture. */
7655 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7657 /* An explicit expression exists. */
7658 cp_lexer_consume_token (parser->lexer);
7659 pedwarn (input_location, OPT_pedantic,
7660 "ISO C++ does not allow initializers "
7661 "in lambda expression capture lists");
7662 capture_init_expr = cp_parser_assignment_expression (parser,
7665 explicit_init_p = true;
7669 const char* error_msg;
7671 /* Turn the identifier into an id-expression. */
7673 = cp_parser_lookup_name
7677 /*is_template=*/false,
7678 /*is_namespace=*/false,
7679 /*check_dependency=*/true,
7680 /*ambiguous_decls=*/NULL,
7681 capture_token->location);
7684 = finish_id_expression
7689 /*integral_constant_expression_p=*/false,
7690 /*allow_non_integral_constant_expression_p=*/false,
7691 /*non_integral_constant_expression_p=*/NULL,
7692 /*template_p=*/false,
7694 /*address_p=*/false,
7695 /*template_arg_p=*/false,
7697 capture_token->location);
7700 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7702 = unqualified_name_lookup_error (capture_init_expr);
7704 add_capture (lambda_expr,
7707 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7711 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7714 /* Parse the (optional) middle of a lambda expression.
7717 ( parameter-declaration-clause [opt] )
7718 attribute-specifier [opt]
7720 exception-specification [opt]
7721 lambda-return-type-clause [opt]
7723 LAMBDA_EXPR is the current representation of the lambda expression. */
7726 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7728 /* 5.1.1.4 of the standard says:
7729 If a lambda-expression does not include a lambda-declarator, it is as if
7730 the lambda-declarator were ().
7731 This means an empty parameter list, no attributes, and no exception
7733 tree param_list = void_list_node;
7734 tree attributes = NULL_TREE;
7735 tree exception_spec = NULL_TREE;
7738 /* The lambda-declarator is optional, but must begin with an opening
7739 parenthesis if present. */
7740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7742 cp_lexer_consume_token (parser->lexer);
7744 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7746 /* Parse parameters. */
7747 param_list = cp_parser_parameter_declaration_clause (parser);
7749 /* Default arguments shall not be specified in the
7750 parameter-declaration-clause of a lambda-declarator. */
7751 for (t = param_list; t; t = TREE_CHAIN (t))
7752 if (TREE_PURPOSE (t))
7753 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7754 "default argument specified for lambda parameter");
7756 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7758 attributes = cp_parser_attributes_opt (parser);
7760 /* Parse optional `mutable' keyword. */
7761 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7763 cp_lexer_consume_token (parser->lexer);
7764 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7767 /* Parse optional exception specification. */
7768 exception_spec = cp_parser_exception_specification_opt (parser);
7770 /* Parse optional trailing return type. */
7771 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7773 cp_lexer_consume_token (parser->lexer);
7774 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7777 /* The function parameters must be in scope all the way until after the
7778 trailing-return-type in case of decltype. */
7779 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7780 pop_binding (DECL_NAME (t), t);
7785 /* Create the function call operator.
7787 Messing with declarators like this is no uglier than building up the
7788 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7791 cp_decl_specifier_seq return_type_specs;
7792 cp_declarator* declarator;
7797 clear_decl_specs (&return_type_specs);
7798 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7799 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7801 /* Maybe we will deduce the return type later, but we can use void
7802 as a placeholder return type anyways. */
7803 return_type_specs.type = void_type_node;
7805 p = obstack_alloc (&declarator_obstack, 0);
7807 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7810 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7811 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7812 declarator = make_call_declarator (declarator, param_list, quals,
7814 /*late_return_type=*/NULL_TREE);
7815 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7817 fco = grokmethod (&return_type_specs,
7820 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7821 DECL_ARTIFICIAL (fco) = 1;
7823 finish_member_declaration (fco);
7825 obstack_free (&declarator_obstack, p);
7829 /* Parse the body of a lambda expression, which is simply
7833 but which requires special handling.
7834 LAMBDA_EXPR is the current representation of the lambda expression. */
7837 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7839 bool nested = (current_function_decl != NULL_TREE);
7841 push_function_context ();
7843 /* Finish the function call operator
7845 + late_parsing_for_member
7846 + function_definition_after_declarator
7847 + ctor_initializer_opt_and_function_body */
7849 tree fco = lambda_function (lambda_expr);
7853 /* Let the front end know that we are going to be defining this
7855 start_preparsed_function (fco,
7857 SF_PRE_PARSED | SF_INCLASS_INLINE);
7859 start_lambda_scope (fco);
7860 body = begin_function_body ();
7862 /* 5.1.1.4 of the standard says:
7863 If a lambda-expression does not include a trailing-return-type, it
7864 is as if the trailing-return-type denotes the following type:
7865 * if the compound-statement is of the form
7866 { return attribute-specifier [opt] expression ; }
7867 the type of the returned expression after lvalue-to-rvalue
7868 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7869 (_conv.array_ 4.2), and function-to-pointer conversion
7871 * otherwise, void. */
7873 /* In a lambda that has neither a lambda-return-type-clause
7874 nor a deducible form, errors should be reported for return statements
7875 in the body. Since we used void as the placeholder return type, parsing
7876 the body as usual will give such desired behavior. */
7877 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7878 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7879 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7880 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7883 tree expr = NULL_TREE;
7884 cp_id_kind idk = CP_ID_KIND_NONE;
7886 /* Parse tentatively in case there's more after the initial return
7888 cp_parser_parse_tentatively (parser);
7890 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7891 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7893 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7895 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7896 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7898 if (cp_parser_parse_definitely (parser))
7900 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7902 compound_stmt = begin_compound_stmt (0);
7903 /* Will get error here if type not deduced yet. */
7904 finish_return_stmt (expr);
7905 finish_compound_stmt (compound_stmt);
7913 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7914 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7915 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7916 cp_parser_compound_stmt does not pass it. */
7917 cp_parser_function_body (parser);
7918 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7921 finish_function_body (body);
7922 finish_lambda_scope ();
7924 /* Finish the function and generate code for it if necessary. */
7925 expand_or_defer_fn (finish_function (/*inline*/2));
7929 pop_function_context();
7932 /* Statements [gram.stmt.stmt] */
7934 /* Parse a statement.
7938 expression-statement
7943 declaration-statement
7946 IN_COMPOUND is true when the statement is nested inside a
7947 cp_parser_compound_statement; this matters for certain pragmas.
7949 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7950 is a (possibly labeled) if statement which is not enclosed in braces
7951 and has an else clause. This is used to implement -Wparentheses. */
7954 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7955 bool in_compound, bool *if_p)
7959 location_t statement_location;
7964 /* There is no statement yet. */
7965 statement = NULL_TREE;
7966 /* Peek at the next token. */
7967 token = cp_lexer_peek_token (parser->lexer);
7968 /* Remember the location of the first token in the statement. */
7969 statement_location = token->location;
7970 /* If this is a keyword, then that will often determine what kind of
7971 statement we have. */
7972 if (token->type == CPP_KEYWORD)
7974 enum rid keyword = token->keyword;
7980 /* Looks like a labeled-statement with a case label.
7981 Parse the label, and then use tail recursion to parse
7983 cp_parser_label_for_labeled_statement (parser);
7988 statement = cp_parser_selection_statement (parser, if_p);
7994 statement = cp_parser_iteration_statement (parser);
8001 statement = cp_parser_jump_statement (parser);
8004 /* Objective-C++ exception-handling constructs. */
8007 case RID_AT_FINALLY:
8008 case RID_AT_SYNCHRONIZED:
8010 statement = cp_parser_objc_statement (parser);
8014 statement = cp_parser_try_block (parser);
8018 /* This must be a namespace alias definition. */
8019 cp_parser_declaration_statement (parser);
8023 /* It might be a keyword like `int' that can start a
8024 declaration-statement. */
8028 else if (token->type == CPP_NAME)
8030 /* If the next token is a `:', then we are looking at a
8031 labeled-statement. */
8032 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8033 if (token->type == CPP_COLON)
8035 /* Looks like a labeled-statement with an ordinary label.
8036 Parse the label, and then use tail recursion to parse
8038 cp_parser_label_for_labeled_statement (parser);
8042 /* Anything that starts with a `{' must be a compound-statement. */
8043 else if (token->type == CPP_OPEN_BRACE)
8044 statement = cp_parser_compound_statement (parser, NULL, false);
8045 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8046 a statement all its own. */
8047 else if (token->type == CPP_PRAGMA)
8049 /* Only certain OpenMP pragmas are attached to statements, and thus
8050 are considered statements themselves. All others are not. In
8051 the context of a compound, accept the pragma as a "statement" and
8052 return so that we can check for a close brace. Otherwise we
8053 require a real statement and must go back and read one. */
8055 cp_parser_pragma (parser, pragma_compound);
8056 else if (!cp_parser_pragma (parser, pragma_stmt))
8060 else if (token->type == CPP_EOF)
8062 cp_parser_error (parser, "expected statement");
8066 /* Everything else must be a declaration-statement or an
8067 expression-statement. Try for the declaration-statement
8068 first, unless we are looking at a `;', in which case we know that
8069 we have an expression-statement. */
8072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8074 cp_parser_parse_tentatively (parser);
8075 /* Try to parse the declaration-statement. */
8076 cp_parser_declaration_statement (parser);
8077 /* If that worked, we're done. */
8078 if (cp_parser_parse_definitely (parser))
8081 /* Look for an expression-statement instead. */
8082 statement = cp_parser_expression_statement (parser, in_statement_expr);
8085 /* Set the line number for the statement. */
8086 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8087 SET_EXPR_LOCATION (statement, statement_location);
8090 /* Parse the label for a labeled-statement, i.e.
8093 case constant-expression :
8097 case constant-expression ... constant-expression : statement
8099 When a label is parsed without errors, the label is added to the
8100 parse tree by the finish_* functions, so this function doesn't
8101 have to return the label. */
8104 cp_parser_label_for_labeled_statement (cp_parser* parser)
8107 tree label = NULL_TREE;
8109 /* The next token should be an identifier. */
8110 token = cp_lexer_peek_token (parser->lexer);
8111 if (token->type != CPP_NAME
8112 && token->type != CPP_KEYWORD)
8114 cp_parser_error (parser, "expected labeled-statement");
8118 switch (token->keyword)
8125 /* Consume the `case' token. */
8126 cp_lexer_consume_token (parser->lexer);
8127 /* Parse the constant-expression. */
8128 expr = cp_parser_constant_expression (parser,
8129 /*allow_non_constant_p=*/false,
8132 ellipsis = cp_lexer_peek_token (parser->lexer);
8133 if (ellipsis->type == CPP_ELLIPSIS)
8135 /* Consume the `...' token. */
8136 cp_lexer_consume_token (parser->lexer);
8138 cp_parser_constant_expression (parser,
8139 /*allow_non_constant_p=*/false,
8141 /* We don't need to emit warnings here, as the common code
8142 will do this for us. */
8145 expr_hi = NULL_TREE;
8147 if (parser->in_switch_statement_p)
8148 finish_case_label (token->location, expr, expr_hi);
8150 error_at (token->location,
8151 "case label %qE not within a switch statement",
8157 /* Consume the `default' token. */
8158 cp_lexer_consume_token (parser->lexer);
8160 if (parser->in_switch_statement_p)
8161 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8163 error_at (token->location, "case label not within a switch statement");
8167 /* Anything else must be an ordinary label. */
8168 label = finish_label_stmt (cp_parser_identifier (parser));
8172 /* Require the `:' token. */
8173 cp_parser_require (parser, CPP_COLON, RT_COLON);
8175 /* An ordinary label may optionally be followed by attributes.
8176 However, this is only permitted if the attributes are then
8177 followed by a semicolon. This is because, for backward
8178 compatibility, when parsing
8179 lab: __attribute__ ((unused)) int i;
8180 we want the attribute to attach to "i", not "lab". */
8181 if (label != NULL_TREE
8182 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8186 cp_parser_parse_tentatively (parser);
8187 attrs = cp_parser_attributes_opt (parser);
8188 if (attrs == NULL_TREE
8189 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8190 cp_parser_abort_tentative_parse (parser);
8191 else if (!cp_parser_parse_definitely (parser))
8194 cplus_decl_attributes (&label, attrs, 0);
8198 /* Parse an expression-statement.
8200 expression-statement:
8203 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8204 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8205 indicates whether this expression-statement is part of an
8206 expression statement. */
8209 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8211 tree statement = NULL_TREE;
8212 cp_token *token = cp_lexer_peek_token (parser->lexer);
8214 /* If the next token is a ';', then there is no expression
8216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8217 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8219 /* Give a helpful message for "A<T>::type t;" and the like. */
8220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8221 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8223 if (TREE_CODE (statement) == SCOPE_REF)
8224 error_at (token->location, "need %<typename%> before %qE because "
8225 "%qT is a dependent scope",
8226 statement, TREE_OPERAND (statement, 0));
8227 else if (is_overloaded_fn (statement)
8228 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8231 tree fn = get_first_fn (statement);
8232 error_at (token->location,
8233 "%<%T::%D%> names the constructor, not the type",
8234 DECL_CONTEXT (fn), DECL_NAME (fn));
8238 /* Consume the final `;'. */
8239 cp_parser_consume_semicolon_at_end_of_statement (parser);
8241 if (in_statement_expr
8242 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8243 /* This is the final expression statement of a statement
8245 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8247 statement = finish_expr_stmt (statement);
8254 /* Parse a compound-statement.
8257 { statement-seq [opt] }
8262 { label-declaration-seq [opt] statement-seq [opt] }
8264 label-declaration-seq:
8266 label-declaration-seq label-declaration
8268 Returns a tree representing the statement. */
8271 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8276 /* Consume the `{'. */
8277 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8278 return error_mark_node;
8279 /* Begin the compound-statement. */
8280 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8281 /* If the next keyword is `__label__' we have a label declaration. */
8282 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8283 cp_parser_label_declaration (parser);
8284 /* Parse an (optional) statement-seq. */
8285 cp_parser_statement_seq_opt (parser, in_statement_expr);
8286 /* Finish the compound-statement. */
8287 finish_compound_stmt (compound_stmt);
8288 /* Consume the `}'. */
8289 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8291 return compound_stmt;
8294 /* Parse an (optional) statement-seq.
8298 statement-seq [opt] statement */
8301 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8303 /* Scan statements until there aren't any more. */
8306 cp_token *token = cp_lexer_peek_token (parser->lexer);
8308 /* If we are looking at a `}', then we have run out of
8309 statements; the same is true if we have reached the end
8310 of file, or have stumbled upon a stray '@end'. */
8311 if (token->type == CPP_CLOSE_BRACE
8312 || token->type == CPP_EOF
8313 || token->type == CPP_PRAGMA_EOL
8314 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8317 /* If we are in a compound statement and find 'else' then
8318 something went wrong. */
8319 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8321 if (parser->in_statement & IN_IF_STMT)
8325 token = cp_lexer_consume_token (parser->lexer);
8326 error_at (token->location, "%<else%> without a previous %<if%>");
8330 /* Parse the statement. */
8331 cp_parser_statement (parser, in_statement_expr, true, NULL);
8335 /* Parse a selection-statement.
8337 selection-statement:
8338 if ( condition ) statement
8339 if ( condition ) statement else statement
8340 switch ( condition ) statement
8342 Returns the new IF_STMT or SWITCH_STMT.
8344 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8345 is a (possibly labeled) if statement which is not enclosed in
8346 braces and has an else clause. This is used to implement
8350 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8358 /* Peek at the next token. */
8359 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8361 /* See what kind of keyword it is. */
8362 keyword = token->keyword;
8371 /* Look for the `('. */
8372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8374 cp_parser_skip_to_end_of_statement (parser);
8375 return error_mark_node;
8378 /* Begin the selection-statement. */
8379 if (keyword == RID_IF)
8380 statement = begin_if_stmt ();
8382 statement = begin_switch_stmt ();
8384 /* Parse the condition. */
8385 condition = cp_parser_condition (parser);
8386 /* Look for the `)'. */
8387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8388 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8389 /*consume_paren=*/true);
8391 if (keyword == RID_IF)
8394 unsigned char in_statement;
8396 /* Add the condition. */
8397 finish_if_stmt_cond (condition, statement);
8399 /* Parse the then-clause. */
8400 in_statement = parser->in_statement;
8401 parser->in_statement |= IN_IF_STMT;
8402 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8405 add_stmt (build_empty_stmt (loc));
8406 cp_lexer_consume_token (parser->lexer);
8407 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8408 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8409 "empty body in an %<if%> statement");
8413 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8414 parser->in_statement = in_statement;
8416 finish_then_clause (statement);
8418 /* If the next token is `else', parse the else-clause. */
8419 if (cp_lexer_next_token_is_keyword (parser->lexer,
8422 /* Consume the `else' keyword. */
8423 cp_lexer_consume_token (parser->lexer);
8424 begin_else_clause (statement);
8425 /* Parse the else-clause. */
8426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8429 loc = cp_lexer_peek_token (parser->lexer)->location;
8431 OPT_Wempty_body, "suggest braces around "
8432 "empty body in an %<else%> statement");
8433 add_stmt (build_empty_stmt (loc));
8434 cp_lexer_consume_token (parser->lexer);
8437 cp_parser_implicitly_scoped_statement (parser, NULL);
8439 finish_else_clause (statement);
8441 /* If we are currently parsing a then-clause, then
8442 IF_P will not be NULL. We set it to true to
8443 indicate that this if statement has an else clause.
8444 This may trigger the Wparentheses warning below
8445 when we get back up to the parent if statement. */
8451 /* This if statement does not have an else clause. If
8452 NESTED_IF is true, then the then-clause is an if
8453 statement which does have an else clause. We warn
8454 about the potential ambiguity. */
8456 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8457 "suggest explicit braces to avoid ambiguous"
8461 /* Now we're all done with the if-statement. */
8462 finish_if_stmt (statement);
8466 bool in_switch_statement_p;
8467 unsigned char in_statement;
8469 /* Add the condition. */
8470 finish_switch_cond (condition, statement);
8472 /* Parse the body of the switch-statement. */
8473 in_switch_statement_p = parser->in_switch_statement_p;
8474 in_statement = parser->in_statement;
8475 parser->in_switch_statement_p = true;
8476 parser->in_statement |= IN_SWITCH_STMT;
8477 cp_parser_implicitly_scoped_statement (parser, NULL);
8478 parser->in_switch_statement_p = in_switch_statement_p;
8479 parser->in_statement = in_statement;
8481 /* Now we're all done with the switch-statement. */
8482 finish_switch_stmt (statement);
8490 cp_parser_error (parser, "expected selection-statement");
8491 return error_mark_node;
8495 /* Parse a condition.
8499 type-specifier-seq declarator = initializer-clause
8500 type-specifier-seq declarator braced-init-list
8505 type-specifier-seq declarator asm-specification [opt]
8506 attributes [opt] = assignment-expression
8508 Returns the expression that should be tested. */
8511 cp_parser_condition (cp_parser* parser)
8513 cp_decl_specifier_seq type_specifiers;
8514 const char *saved_message;
8515 int declares_class_or_enum;
8517 /* Try the declaration first. */
8518 cp_parser_parse_tentatively (parser);
8519 /* New types are not allowed in the type-specifier-seq for a
8521 saved_message = parser->type_definition_forbidden_message;
8522 parser->type_definition_forbidden_message
8523 = G_("types may not be defined in conditions");
8524 /* Parse the type-specifier-seq. */
8525 cp_parser_decl_specifier_seq (parser,
8526 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8528 &declares_class_or_enum);
8529 /* Restore the saved message. */
8530 parser->type_definition_forbidden_message = saved_message;
8531 /* If all is well, we might be looking at a declaration. */
8532 if (!cp_parser_error_occurred (parser))
8535 tree asm_specification;
8537 cp_declarator *declarator;
8538 tree initializer = NULL_TREE;
8540 /* Parse the declarator. */
8541 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8542 /*ctor_dtor_or_conv_p=*/NULL,
8543 /*parenthesized_p=*/NULL,
8544 /*member_p=*/false);
8545 /* Parse the attributes. */
8546 attributes = cp_parser_attributes_opt (parser);
8547 /* Parse the asm-specification. */
8548 asm_specification = cp_parser_asm_specification_opt (parser);
8549 /* If the next token is not an `=' or '{', then we might still be
8550 looking at an expression. For example:
8554 looks like a decl-specifier-seq and a declarator -- but then
8555 there is no `=', so this is an expression. */
8556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8557 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8558 cp_parser_simulate_error (parser);
8560 /* If we did see an `=' or '{', then we are looking at a declaration
8562 if (cp_parser_parse_definitely (parser))
8565 bool non_constant_p;
8566 bool flags = LOOKUP_ONLYCONVERTING;
8568 /* Create the declaration. */
8569 decl = start_decl (declarator, &type_specifiers,
8570 /*initialized_p=*/true,
8571 attributes, /*prefix_attributes=*/NULL_TREE,
8574 /* Parse the initializer. */
8575 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8577 initializer = cp_parser_braced_list (parser, &non_constant_p);
8578 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8583 /* Consume the `='. */
8584 cp_parser_require (parser, CPP_EQ, RT_EQ);
8585 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8587 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8588 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8590 if (!non_constant_p)
8591 initializer = fold_non_dependent_expr (initializer);
8593 /* Process the initializer. */
8594 cp_finish_decl (decl,
8595 initializer, !non_constant_p,
8600 pop_scope (pushed_scope);
8602 return convert_from_reference (decl);
8605 /* If we didn't even get past the declarator successfully, we are
8606 definitely not looking at a declaration. */
8608 cp_parser_abort_tentative_parse (parser);
8610 /* Otherwise, we are looking at an expression. */
8611 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8614 /* Parses a traditional for-statement until the closing ')', not included. */
8617 cp_parser_c_for (cp_parser *parser)
8619 /* Normal for loop */
8621 tree condition = NULL_TREE;
8622 tree expression = NULL_TREE;
8624 /* Begin the for-statement. */
8625 stmt = begin_for_stmt ();
8627 /* Parse the initialization. */
8628 cp_parser_for_init_statement (parser);
8629 finish_for_init_stmt (stmt);
8631 /* If there's a condition, process it. */
8632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8633 condition = cp_parser_condition (parser);
8634 finish_for_cond (condition, stmt);
8635 /* Look for the `;'. */
8636 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8638 /* If there's an expression, process it. */
8639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8640 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8641 finish_for_expr (expression, stmt);
8646 /* Tries to parse a range-based for-statement:
8649 type-specifier-seq declarator : expression
8651 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8652 expression. Note that the *DECL is returned unfinished, so
8653 later you should call cp_finish_decl().
8655 Returns TRUE iff a range-based for is parsed. */
8658 cp_parser_range_for (cp_parser *parser)
8660 tree stmt, range_decl, range_expr;
8661 cp_decl_specifier_seq type_specifiers;
8662 cp_declarator *declarator;
8663 const char *saved_message;
8664 tree attributes, pushed_scope;
8666 cp_parser_parse_tentatively (parser);
8667 /* New types are not allowed in the type-specifier-seq for a
8668 range-based for loop. */
8669 saved_message = parser->type_definition_forbidden_message;
8670 parser->type_definition_forbidden_message
8671 = G_("types may not be defined in range-based for loops");
8672 /* Parse the type-specifier-seq. */
8673 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8674 /*is_trailing_return=*/false,
8676 /* Restore the saved message. */
8677 parser->type_definition_forbidden_message = saved_message;
8678 /* If all is well, we might be looking at a declaration. */
8679 if (cp_parser_error_occurred (parser))
8681 cp_parser_abort_tentative_parse (parser);
8684 /* Parse the declarator. */
8685 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8686 /*ctor_dtor_or_conv_p=*/NULL,
8687 /*parenthesized_p=*/NULL,
8688 /*member_p=*/false);
8689 /* Parse the attributes. */
8690 attributes = cp_parser_attributes_opt (parser);
8691 /* The next token should be `:'. */
8692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8693 cp_parser_simulate_error (parser);
8695 /* Check if it is a range-based for */
8696 if (!cp_parser_parse_definitely (parser))
8699 cp_parser_require (parser, CPP_COLON, RT_COLON);
8700 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8702 bool expr_non_constant_p;
8703 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8706 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8708 /* If in template, STMT is converted to a normal for-statements
8709 at instantiation. If not, it is done just ahead. */
8710 if (processing_template_decl)
8711 stmt = begin_range_for_stmt ();
8713 stmt = begin_for_stmt ();
8715 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8716 range_decl = start_decl (declarator, &type_specifiers,
8717 /*initialized_p=*/SD_INITIALIZED,
8718 attributes, /*prefix_attributes=*/NULL_TREE,
8720 /* No scope allowed here */
8721 pop_scope (pushed_scope);
8723 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8724 finish_range_for_decl (stmt, range_decl, range_expr);
8726 /* Convert the range-based for loop into a normal for-statement. */
8727 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8732 /* Converts a range-based for-statement into a normal
8733 for-statement, as per the definition.
8735 for (RANGE_DECL : RANGE_EXPR)
8738 should be equivalent to:
8741 auto &&__range = RANGE_EXPR;
8742 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8746 RANGE_DECL = *__begin;
8751 If RANGE_EXPR is an array:
8752 BEGIN_EXPR = __range
8753 END_EXPR = __range + ARRAY_SIZE(__range)
8755 BEGIN_EXPR = begin(__range)
8756 END_EXPR = end(__range);
8758 When calling begin()/end() we must use argument dependent
8759 lookup, but always considering 'std' as an associated namespace. */
8762 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8764 tree range_type, range_temp;
8766 tree iter_type, begin_expr, end_expr;
8767 tree condition, expression;
8769 /* Find out the type deduced by the declaration
8770 * `auto &&__range = range_expr' */
8771 range_type = cp_build_reference_type (make_auto (), true);
8772 range_type = do_auto_deduction (range_type, range_expr,
8773 type_uses_auto (range_type));
8775 /* Create the __range variable */
8776 range_temp = build_decl (input_location, VAR_DECL,
8777 get_identifier ("__for_range"), range_type);
8778 TREE_USED (range_temp) = 1;
8779 DECL_ARTIFICIAL (range_temp) = 1;
8780 pushdecl (range_temp);
8781 cp_finish_decl (range_temp, range_expr,
8782 /*is_constant_init*/false, NULL_TREE,
8783 LOOKUP_ONLYCONVERTING);
8785 range_temp = convert_from_reference (range_temp);
8787 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8789 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8790 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8791 begin_expr = range_temp;
8793 = build_binary_op (input_location, PLUS_EXPR,
8795 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8799 /* If it is not an array, we must call begin(__range)/end__range() */
8802 begin_expr = get_identifier ("begin");
8803 vec = make_tree_vector ();
8804 VEC_safe_push (tree, gc, vec, range_temp);
8805 begin_expr = perform_koenig_lookup (begin_expr, vec,
8806 /*include_std=*/true);
8807 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8808 tf_warning_or_error);
8809 release_tree_vector (vec);
8811 end_expr = get_identifier ("end");
8812 vec = make_tree_vector ();
8813 VEC_safe_push (tree, gc, vec, range_temp);
8814 end_expr = perform_koenig_lookup (end_expr, vec,
8815 /*include_std=*/true);
8816 end_expr = finish_call_expr (end_expr, &vec, false, true,
8817 tf_warning_or_error);
8818 release_tree_vector (vec);
8820 /* The unqualified type of the __begin and __end temporaries should
8821 * be the same as required by the multiple auto declaration */
8822 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8823 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8824 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8825 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8828 /* The new for initialization statement */
8829 begin = build_decl (input_location, VAR_DECL,
8830 get_identifier ("__for_begin"), iter_type);
8831 TREE_USED (begin) = 1;
8832 DECL_ARTIFICIAL (begin) = 1;
8834 cp_finish_decl (begin, begin_expr,
8835 /*is_constant_init*/false, NULL_TREE,
8836 LOOKUP_ONLYCONVERTING);
8838 end = build_decl (input_location, VAR_DECL,
8839 get_identifier ("__for_end"), iter_type);
8840 TREE_USED (end) = 1;
8841 DECL_ARTIFICIAL (end) = 1;
8843 cp_finish_decl (end, end_expr,
8844 /*is_constant_init*/false, NULL_TREE,
8845 LOOKUP_ONLYCONVERTING);
8847 finish_for_init_stmt (statement);
8849 /* The new for condition */
8850 condition = build_x_binary_op (NE_EXPR,
8853 NULL, tf_warning_or_error);
8854 finish_for_cond (condition, statement);
8856 /* The new increment expression */
8857 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8858 finish_for_expr (expression, statement);
8860 /* The declaration is initialized with *__begin inside the loop body */
8861 cp_finish_decl (range_decl,
8862 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8863 /*is_constant_init*/false, NULL_TREE,
8864 LOOKUP_ONLYCONVERTING);
8870 /* Parse an iteration-statement.
8872 iteration-statement:
8873 while ( condition ) statement
8874 do statement while ( expression ) ;
8875 for ( for-init-statement condition [opt] ; expression [opt] )
8878 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8881 cp_parser_iteration_statement (cp_parser* parser)
8886 unsigned char in_statement;
8888 /* Peek at the next token. */
8889 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8891 return error_mark_node;
8893 /* Remember whether or not we are already within an iteration
8895 in_statement = parser->in_statement;
8897 /* See what kind of keyword it is. */
8898 keyword = token->keyword;
8905 /* Begin the while-statement. */
8906 statement = begin_while_stmt ();
8907 /* Look for the `('. */
8908 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8909 /* Parse the condition. */
8910 condition = cp_parser_condition (parser);
8911 finish_while_stmt_cond (condition, statement);
8912 /* Look for the `)'. */
8913 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8914 /* Parse the dependent statement. */
8915 parser->in_statement = IN_ITERATION_STMT;
8916 cp_parser_already_scoped_statement (parser);
8917 parser->in_statement = in_statement;
8918 /* We're done with the while-statement. */
8919 finish_while_stmt (statement);
8927 /* Begin the do-statement. */
8928 statement = begin_do_stmt ();
8929 /* Parse the body of the do-statement. */
8930 parser->in_statement = IN_ITERATION_STMT;
8931 cp_parser_implicitly_scoped_statement (parser, NULL);
8932 parser->in_statement = in_statement;
8933 finish_do_body (statement);
8934 /* Look for the `while' keyword. */
8935 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8936 /* Look for the `('. */
8937 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8938 /* Parse the expression. */
8939 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8940 /* We're done with the do-statement. */
8941 finish_do_stmt (expression, statement);
8942 /* Look for the `)'. */
8943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8944 /* Look for the `;'. */
8945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8951 /* Look for the `('. */
8952 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8954 if (cxx_dialect == cxx0x)
8955 statement = cp_parser_range_for (parser);
8957 statement = NULL_TREE;
8958 if (statement == NULL_TREE)
8959 statement = cp_parser_c_for (parser);
8961 /* Look for the `)'. */
8962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8964 /* Parse the body of the for-statement. */
8965 parser->in_statement = IN_ITERATION_STMT;
8966 cp_parser_already_scoped_statement (parser);
8967 parser->in_statement = in_statement;
8969 /* We're done with the for-statement. */
8970 finish_for_stmt (statement);
8975 cp_parser_error (parser, "expected iteration-statement");
8976 statement = error_mark_node;
8983 /* Parse a for-init-statement.
8986 expression-statement
8987 simple-declaration */
8990 cp_parser_for_init_statement (cp_parser* parser)
8992 /* If the next token is a `;', then we have an empty
8993 expression-statement. Grammatically, this is also a
8994 simple-declaration, but an invalid one, because it does not
8995 declare anything. Therefore, if we did not handle this case
8996 specially, we would issue an error message about an invalid
8998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9000 /* We're going to speculatively look for a declaration, falling back
9001 to an expression, if necessary. */
9002 cp_parser_parse_tentatively (parser);
9003 /* Parse the declaration. */
9004 cp_parser_simple_declaration (parser,
9005 /*function_definition_allowed_p=*/false);
9006 /* If the tentative parse failed, then we shall need to look for an
9007 expression-statement. */
9008 if (cp_parser_parse_definitely (parser))
9012 cp_parser_expression_statement (parser, NULL_TREE);
9015 /* Parse a jump-statement.
9020 return expression [opt] ;
9021 return braced-init-list ;
9029 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9032 cp_parser_jump_statement (cp_parser* parser)
9034 tree statement = error_mark_node;
9037 unsigned char in_statement;
9039 /* Peek at the next token. */
9040 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9042 return error_mark_node;
9044 /* See what kind of keyword it is. */
9045 keyword = token->keyword;
9049 in_statement = parser->in_statement & ~IN_IF_STMT;
9050 switch (in_statement)
9053 error_at (token->location, "break statement not within loop or switch");
9056 gcc_assert ((in_statement & IN_SWITCH_STMT)
9057 || in_statement == IN_ITERATION_STMT);
9058 statement = finish_break_stmt ();
9061 error_at (token->location, "invalid exit from OpenMP structured block");
9064 error_at (token->location, "break statement used with OpenMP for loop");
9067 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9071 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9074 error_at (token->location, "continue statement not within a loop");
9076 case IN_ITERATION_STMT:
9078 statement = finish_continue_stmt ();
9081 error_at (token->location, "invalid exit from OpenMP structured block");
9086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9092 bool expr_non_constant_p;
9094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9096 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9097 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9099 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9100 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9102 /* If the next token is a `;', then there is no
9105 /* Build the return-statement. */
9106 statement = finish_return_stmt (expr);
9107 /* Look for the final `;'. */
9108 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9113 /* Create the goto-statement. */
9114 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9116 /* Issue a warning about this use of a GNU extension. */
9117 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9118 /* Consume the '*' token. */
9119 cp_lexer_consume_token (parser->lexer);
9120 /* Parse the dependent expression. */
9121 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9124 finish_goto_stmt (cp_parser_identifier (parser));
9125 /* Look for the final `;'. */
9126 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9130 cp_parser_error (parser, "expected jump-statement");
9137 /* Parse a declaration-statement.
9139 declaration-statement:
9140 block-declaration */
9143 cp_parser_declaration_statement (cp_parser* parser)
9147 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9148 p = obstack_alloc (&declarator_obstack, 0);
9150 /* Parse the block-declaration. */
9151 cp_parser_block_declaration (parser, /*statement_p=*/true);
9153 /* Free any declarators allocated. */
9154 obstack_free (&declarator_obstack, p);
9156 /* Finish off the statement. */
9160 /* Some dependent statements (like `if (cond) statement'), are
9161 implicitly in their own scope. In other words, if the statement is
9162 a single statement (as opposed to a compound-statement), it is
9163 none-the-less treated as if it were enclosed in braces. Any
9164 declarations appearing in the dependent statement are out of scope
9165 after control passes that point. This function parses a statement,
9166 but ensures that is in its own scope, even if it is not a
9169 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9170 is a (possibly labeled) if statement which is not enclosed in
9171 braces and has an else clause. This is used to implement
9174 Returns the new statement. */
9177 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9184 /* Mark if () ; with a special NOP_EXPR. */
9185 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9188 cp_lexer_consume_token (parser->lexer);
9189 statement = add_stmt (build_empty_stmt (loc));
9191 /* if a compound is opened, we simply parse the statement directly. */
9192 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9193 statement = cp_parser_compound_statement (parser, NULL, false);
9194 /* If the token is not a `{', then we must take special action. */
9197 /* Create a compound-statement. */
9198 statement = begin_compound_stmt (0);
9199 /* Parse the dependent-statement. */
9200 cp_parser_statement (parser, NULL_TREE, false, if_p);
9201 /* Finish the dummy compound-statement. */
9202 finish_compound_stmt (statement);
9205 /* Return the statement. */
9209 /* For some dependent statements (like `while (cond) statement'), we
9210 have already created a scope. Therefore, even if the dependent
9211 statement is a compound-statement, we do not want to create another
9215 cp_parser_already_scoped_statement (cp_parser* parser)
9217 /* If the token is a `{', then we must take special action. */
9218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9219 cp_parser_statement (parser, NULL_TREE, false, NULL);
9222 /* Avoid calling cp_parser_compound_statement, so that we
9223 don't create a new scope. Do everything else by hand. */
9224 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9225 /* If the next keyword is `__label__' we have a label declaration. */
9226 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9227 cp_parser_label_declaration (parser);
9228 /* Parse an (optional) statement-seq. */
9229 cp_parser_statement_seq_opt (parser, NULL_TREE);
9230 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9234 /* Declarations [gram.dcl.dcl] */
9236 /* Parse an optional declaration-sequence.
9240 declaration-seq declaration */
9243 cp_parser_declaration_seq_opt (cp_parser* parser)
9249 token = cp_lexer_peek_token (parser->lexer);
9251 if (token->type == CPP_CLOSE_BRACE
9252 || token->type == CPP_EOF
9253 || token->type == CPP_PRAGMA_EOL)
9256 if (token->type == CPP_SEMICOLON)
9258 /* A declaration consisting of a single semicolon is
9259 invalid. Allow it unless we're being pedantic. */
9260 cp_lexer_consume_token (parser->lexer);
9261 if (!in_system_header)
9262 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9266 /* If we're entering or exiting a region that's implicitly
9267 extern "C", modify the lang context appropriately. */
9268 if (!parser->implicit_extern_c && token->implicit_extern_c)
9270 push_lang_context (lang_name_c);
9271 parser->implicit_extern_c = true;
9273 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9275 pop_lang_context ();
9276 parser->implicit_extern_c = false;
9279 if (token->type == CPP_PRAGMA)
9281 /* A top-level declaration can consist solely of a #pragma.
9282 A nested declaration cannot, so this is done here and not
9283 in cp_parser_declaration. (A #pragma at block scope is
9284 handled in cp_parser_statement.) */
9285 cp_parser_pragma (parser, pragma_external);
9289 /* Parse the declaration itself. */
9290 cp_parser_declaration (parser);
9294 /* Parse a declaration.
9299 template-declaration
9300 explicit-instantiation
9301 explicit-specialization
9302 linkage-specification
9303 namespace-definition
9308 __extension__ declaration */
9311 cp_parser_declaration (cp_parser* parser)
9317 tree attributes = NULL_TREE;
9319 /* Check for the `__extension__' keyword. */
9320 if (cp_parser_extension_opt (parser, &saved_pedantic))
9322 /* Parse the qualified declaration. */
9323 cp_parser_declaration (parser);
9324 /* Restore the PEDANTIC flag. */
9325 pedantic = saved_pedantic;
9330 /* Try to figure out what kind of declaration is present. */
9331 token1 = *cp_lexer_peek_token (parser->lexer);
9333 if (token1.type != CPP_EOF)
9334 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9337 token2.type = CPP_EOF;
9338 token2.keyword = RID_MAX;
9341 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9342 p = obstack_alloc (&declarator_obstack, 0);
9344 /* If the next token is `extern' and the following token is a string
9345 literal, then we have a linkage specification. */
9346 if (token1.keyword == RID_EXTERN
9347 && cp_parser_is_string_literal (&token2))
9348 cp_parser_linkage_specification (parser);
9349 /* If the next token is `template', then we have either a template
9350 declaration, an explicit instantiation, or an explicit
9352 else if (token1.keyword == RID_TEMPLATE)
9354 /* `template <>' indicates a template specialization. */
9355 if (token2.type == CPP_LESS
9356 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9357 cp_parser_explicit_specialization (parser);
9358 /* `template <' indicates a template declaration. */
9359 else if (token2.type == CPP_LESS)
9360 cp_parser_template_declaration (parser, /*member_p=*/false);
9361 /* Anything else must be an explicit instantiation. */
9363 cp_parser_explicit_instantiation (parser);
9365 /* If the next token is `export', then we have a template
9367 else if (token1.keyword == RID_EXPORT)
9368 cp_parser_template_declaration (parser, /*member_p=*/false);
9369 /* If the next token is `extern', 'static' or 'inline' and the one
9370 after that is `template', we have a GNU extended explicit
9371 instantiation directive. */
9372 else if (cp_parser_allow_gnu_extensions_p (parser)
9373 && (token1.keyword == RID_EXTERN
9374 || token1.keyword == RID_STATIC
9375 || token1.keyword == RID_INLINE)
9376 && token2.keyword == RID_TEMPLATE)
9377 cp_parser_explicit_instantiation (parser);
9378 /* If the next token is `namespace', check for a named or unnamed
9379 namespace definition. */
9380 else if (token1.keyword == RID_NAMESPACE
9381 && (/* A named namespace definition. */
9382 (token2.type == CPP_NAME
9383 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9385 /* An unnamed namespace definition. */
9386 || token2.type == CPP_OPEN_BRACE
9387 || token2.keyword == RID_ATTRIBUTE))
9388 cp_parser_namespace_definition (parser);
9389 /* An inline (associated) namespace definition. */
9390 else if (token1.keyword == RID_INLINE
9391 && token2.keyword == RID_NAMESPACE)
9392 cp_parser_namespace_definition (parser);
9393 /* Objective-C++ declaration/definition. */
9394 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9395 cp_parser_objc_declaration (parser, NULL_TREE);
9396 else if (c_dialect_objc ()
9397 && token1.keyword == RID_ATTRIBUTE
9398 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9399 cp_parser_objc_declaration (parser, attributes);
9400 /* We must have either a block declaration or a function
9403 /* Try to parse a block-declaration, or a function-definition. */
9404 cp_parser_block_declaration (parser, /*statement_p=*/false);
9406 /* Free any declarators allocated. */
9407 obstack_free (&declarator_obstack, p);
9410 /* Parse a block-declaration.
9415 namespace-alias-definition
9422 __extension__ block-declaration
9427 static_assert-declaration
9429 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9430 part of a declaration-statement. */
9433 cp_parser_block_declaration (cp_parser *parser,
9439 /* Check for the `__extension__' keyword. */
9440 if (cp_parser_extension_opt (parser, &saved_pedantic))
9442 /* Parse the qualified declaration. */
9443 cp_parser_block_declaration (parser, statement_p);
9444 /* Restore the PEDANTIC flag. */
9445 pedantic = saved_pedantic;
9450 /* Peek at the next token to figure out which kind of declaration is
9452 token1 = cp_lexer_peek_token (parser->lexer);
9454 /* If the next keyword is `asm', we have an asm-definition. */
9455 if (token1->keyword == RID_ASM)
9458 cp_parser_commit_to_tentative_parse (parser);
9459 cp_parser_asm_definition (parser);
9461 /* If the next keyword is `namespace', we have a
9462 namespace-alias-definition. */
9463 else if (token1->keyword == RID_NAMESPACE)
9464 cp_parser_namespace_alias_definition (parser);
9465 /* If the next keyword is `using', we have either a
9466 using-declaration or a using-directive. */
9467 else if (token1->keyword == RID_USING)
9472 cp_parser_commit_to_tentative_parse (parser);
9473 /* If the token after `using' is `namespace', then we have a
9475 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9476 if (token2->keyword == RID_NAMESPACE)
9477 cp_parser_using_directive (parser);
9478 /* Otherwise, it's a using-declaration. */
9480 cp_parser_using_declaration (parser,
9481 /*access_declaration_p=*/false);
9483 /* If the next keyword is `__label__' we have a misplaced label
9485 else if (token1->keyword == RID_LABEL)
9487 cp_lexer_consume_token (parser->lexer);
9488 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9489 cp_parser_skip_to_end_of_statement (parser);
9490 /* If the next token is now a `;', consume it. */
9491 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9492 cp_lexer_consume_token (parser->lexer);
9494 /* If the next token is `static_assert' we have a static assertion. */
9495 else if (token1->keyword == RID_STATIC_ASSERT)
9496 cp_parser_static_assert (parser, /*member_p=*/false);
9497 /* Anything else must be a simple-declaration. */
9499 cp_parser_simple_declaration (parser, !statement_p);
9502 /* Parse a simple-declaration.
9505 decl-specifier-seq [opt] init-declarator-list [opt] ;
9507 init-declarator-list:
9509 init-declarator-list , init-declarator
9511 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9512 function-definition as a simple-declaration. */
9515 cp_parser_simple_declaration (cp_parser* parser,
9516 bool function_definition_allowed_p)
9518 cp_decl_specifier_seq decl_specifiers;
9519 int declares_class_or_enum;
9520 bool saw_declarator;
9522 /* Defer access checks until we know what is being declared; the
9523 checks for names appearing in the decl-specifier-seq should be
9524 done as if we were in the scope of the thing being declared. */
9525 push_deferring_access_checks (dk_deferred);
9527 /* Parse the decl-specifier-seq. We have to keep track of whether
9528 or not the decl-specifier-seq declares a named class or
9529 enumeration type, since that is the only case in which the
9530 init-declarator-list is allowed to be empty.
9534 In a simple-declaration, the optional init-declarator-list can be
9535 omitted only when declaring a class or enumeration, that is when
9536 the decl-specifier-seq contains either a class-specifier, an
9537 elaborated-type-specifier, or an enum-specifier. */
9538 cp_parser_decl_specifier_seq (parser,
9539 CP_PARSER_FLAGS_OPTIONAL,
9541 &declares_class_or_enum);
9542 /* We no longer need to defer access checks. */
9543 stop_deferring_access_checks ();
9545 /* In a block scope, a valid declaration must always have a
9546 decl-specifier-seq. By not trying to parse declarators, we can
9547 resolve the declaration/expression ambiguity more quickly. */
9548 if (!function_definition_allowed_p
9549 && !decl_specifiers.any_specifiers_p)
9551 cp_parser_error (parser, "expected declaration");
9555 /* If the next two tokens are both identifiers, the code is
9556 erroneous. The usual cause of this situation is code like:
9560 where "T" should name a type -- but does not. */
9561 if (!decl_specifiers.any_type_specifiers_p
9562 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9564 /* If parsing tentatively, we should commit; we really are
9565 looking at a declaration. */
9566 cp_parser_commit_to_tentative_parse (parser);
9571 /* If we have seen at least one decl-specifier, and the next token
9572 is not a parenthesis, then we must be looking at a declaration.
9573 (After "int (" we might be looking at a functional cast.) */
9574 if (decl_specifiers.any_specifiers_p
9575 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9576 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9577 && !cp_parser_error_occurred (parser))
9578 cp_parser_commit_to_tentative_parse (parser);
9580 /* Keep going until we hit the `;' at the end of the simple
9582 saw_declarator = false;
9583 while (cp_lexer_next_token_is_not (parser->lexer,
9587 bool function_definition_p;
9592 /* If we are processing next declarator, coma is expected */
9593 token = cp_lexer_peek_token (parser->lexer);
9594 gcc_assert (token->type == CPP_COMMA);
9595 cp_lexer_consume_token (parser->lexer);
9598 saw_declarator = true;
9600 /* Parse the init-declarator. */
9601 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9603 function_definition_allowed_p,
9605 declares_class_or_enum,
9606 &function_definition_p);
9607 /* If an error occurred while parsing tentatively, exit quickly.
9608 (That usually happens when in the body of a function; each
9609 statement is treated as a declaration-statement until proven
9611 if (cp_parser_error_occurred (parser))
9613 /* Handle function definitions specially. */
9614 if (function_definition_p)
9616 /* If the next token is a `,', then we are probably
9617 processing something like:
9621 which is erroneous. */
9622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9624 cp_token *token = cp_lexer_peek_token (parser->lexer);
9625 error_at (token->location,
9627 " declarations and function-definitions is forbidden");
9629 /* Otherwise, we're done with the list of declarators. */
9632 pop_deferring_access_checks ();
9636 /* The next token should be either a `,' or a `;'. */
9637 token = cp_lexer_peek_token (parser->lexer);
9638 /* If it's a `,', there are more declarators to come. */
9639 if (token->type == CPP_COMMA)
9640 /* will be consumed next time around */;
9641 /* If it's a `;', we are done. */
9642 else if (token->type == CPP_SEMICOLON)
9644 /* Anything else is an error. */
9647 /* If we have already issued an error message we don't need
9648 to issue another one. */
9649 if (decl != error_mark_node
9650 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9651 cp_parser_error (parser, "expected %<,%> or %<;%>");
9652 /* Skip tokens until we reach the end of the statement. */
9653 cp_parser_skip_to_end_of_statement (parser);
9654 /* If the next token is now a `;', consume it. */
9655 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9656 cp_lexer_consume_token (parser->lexer);
9659 /* After the first time around, a function-definition is not
9660 allowed -- even if it was OK at first. For example:
9665 function_definition_allowed_p = false;
9668 /* Issue an error message if no declarators are present, and the
9669 decl-specifier-seq does not itself declare a class or
9671 if (!saw_declarator)
9673 if (cp_parser_declares_only_class_p (parser))
9674 shadow_tag (&decl_specifiers);
9675 /* Perform any deferred access checks. */
9676 perform_deferred_access_checks ();
9679 /* Consume the `;'. */
9680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9683 pop_deferring_access_checks ();
9686 /* Parse a decl-specifier-seq.
9689 decl-specifier-seq [opt] decl-specifier
9692 storage-class-specifier
9703 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9705 The parser flags FLAGS is used to control type-specifier parsing.
9707 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9710 1: one of the decl-specifiers is an elaborated-type-specifier
9711 (i.e., a type declaration)
9712 2: one of the decl-specifiers is an enum-specifier or a
9713 class-specifier (i.e., a type definition)
9718 cp_parser_decl_specifier_seq (cp_parser* parser,
9719 cp_parser_flags flags,
9720 cp_decl_specifier_seq *decl_specs,
9721 int* declares_class_or_enum)
9723 bool constructor_possible_p = !parser->in_declarator_p;
9724 cp_token *start_token = NULL;
9726 /* Clear DECL_SPECS. */
9727 clear_decl_specs (decl_specs);
9729 /* Assume no class or enumeration type is declared. */
9730 *declares_class_or_enum = 0;
9732 /* Keep reading specifiers until there are no more to read. */
9736 bool found_decl_spec;
9739 /* Peek at the next token. */
9740 token = cp_lexer_peek_token (parser->lexer);
9742 /* Save the first token of the decl spec list for error
9745 start_token = token;
9746 /* Handle attributes. */
9747 if (token->keyword == RID_ATTRIBUTE)
9749 /* Parse the attributes. */
9750 decl_specs->attributes
9751 = chainon (decl_specs->attributes,
9752 cp_parser_attributes_opt (parser));
9755 /* Assume we will find a decl-specifier keyword. */
9756 found_decl_spec = true;
9757 /* If the next token is an appropriate keyword, we can simply
9758 add it to the list. */
9759 switch (token->keyword)
9765 if (!at_class_scope_p ())
9767 error_at (token->location, "%<friend%> used outside of class");
9768 cp_lexer_purge_token (parser->lexer);
9772 ++decl_specs->specs[(int) ds_friend];
9773 /* Consume the token. */
9774 cp_lexer_consume_token (parser->lexer);
9779 ++decl_specs->specs[(int) ds_constexpr];
9780 cp_lexer_consume_token (parser->lexer);
9783 /* function-specifier:
9790 cp_parser_function_specifier_opt (parser, decl_specs);
9796 ++decl_specs->specs[(int) ds_typedef];
9797 /* Consume the token. */
9798 cp_lexer_consume_token (parser->lexer);
9799 /* A constructor declarator cannot appear in a typedef. */
9800 constructor_possible_p = false;
9801 /* The "typedef" keyword can only occur in a declaration; we
9802 may as well commit at this point. */
9803 cp_parser_commit_to_tentative_parse (parser);
9805 if (decl_specs->storage_class != sc_none)
9806 decl_specs->conflicting_specifiers_p = true;
9809 /* storage-class-specifier:
9819 if (cxx_dialect == cxx98)
9821 /* Consume the token. */
9822 cp_lexer_consume_token (parser->lexer);
9824 /* Complain about `auto' as a storage specifier, if
9825 we're complaining about C++0x compatibility. */
9826 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9827 " will change meaning in C++0x; please remove it");
9829 /* Set the storage class anyway. */
9830 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9834 /* C++0x auto type-specifier. */
9835 found_decl_spec = false;
9842 /* Consume the token. */
9843 cp_lexer_consume_token (parser->lexer);
9844 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9848 /* Consume the token. */
9849 cp_lexer_consume_token (parser->lexer);
9850 ++decl_specs->specs[(int) ds_thread];
9854 /* We did not yet find a decl-specifier yet. */
9855 found_decl_spec = false;
9860 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9861 && token->keyword != RID_CONSTEXPR)
9862 error ("decl-specifier invalid in condition");
9864 /* Constructors are a special case. The `S' in `S()' is not a
9865 decl-specifier; it is the beginning of the declarator. */
9868 && constructor_possible_p
9869 && (cp_parser_constructor_declarator_p
9870 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9872 /* If we don't have a DECL_SPEC yet, then we must be looking at
9873 a type-specifier. */
9874 if (!found_decl_spec && !constructor_p)
9876 int decl_spec_declares_class_or_enum;
9877 bool is_cv_qualifier;
9881 = cp_parser_type_specifier (parser, flags,
9883 /*is_declaration=*/true,
9884 &decl_spec_declares_class_or_enum,
9886 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9888 /* If this type-specifier referenced a user-defined type
9889 (a typedef, class-name, etc.), then we can't allow any
9890 more such type-specifiers henceforth.
9894 The longest sequence of decl-specifiers that could
9895 possibly be a type name is taken as the
9896 decl-specifier-seq of a declaration. The sequence shall
9897 be self-consistent as described below.
9901 As a general rule, at most one type-specifier is allowed
9902 in the complete decl-specifier-seq of a declaration. The
9903 only exceptions are the following:
9905 -- const or volatile can be combined with any other
9908 -- signed or unsigned can be combined with char, long,
9916 void g (const int Pc);
9918 Here, Pc is *not* part of the decl-specifier seq; it's
9919 the declarator. Therefore, once we see a type-specifier
9920 (other than a cv-qualifier), we forbid any additional
9921 user-defined types. We *do* still allow things like `int
9922 int' to be considered a decl-specifier-seq, and issue the
9923 error message later. */
9924 if (type_spec && !is_cv_qualifier)
9925 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9926 /* A constructor declarator cannot follow a type-specifier. */
9929 constructor_possible_p = false;
9930 found_decl_spec = true;
9931 if (!is_cv_qualifier)
9932 decl_specs->any_type_specifiers_p = true;
9936 /* If we still do not have a DECL_SPEC, then there are no more
9938 if (!found_decl_spec)
9941 decl_specs->any_specifiers_p = true;
9942 /* After we see one decl-specifier, further decl-specifiers are
9944 flags |= CP_PARSER_FLAGS_OPTIONAL;
9947 cp_parser_check_decl_spec (decl_specs, start_token->location);
9949 /* Don't allow a friend specifier with a class definition. */
9950 if (decl_specs->specs[(int) ds_friend] != 0
9951 && (*declares_class_or_enum & 2))
9952 error_at (start_token->location,
9953 "class definition may not be declared a friend");
9956 /* Parse an (optional) storage-class-specifier.
9958 storage-class-specifier:
9967 storage-class-specifier:
9970 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9973 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9975 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9978 if (cxx_dialect != cxx98)
9980 /* Fall through for C++98. */
9987 /* Consume the token. */
9988 return cp_lexer_consume_token (parser->lexer)->u.value;
9995 /* Parse an (optional) function-specifier.
10002 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10003 Updates DECL_SPECS, if it is non-NULL. */
10006 cp_parser_function_specifier_opt (cp_parser* parser,
10007 cp_decl_specifier_seq *decl_specs)
10009 cp_token *token = cp_lexer_peek_token (parser->lexer);
10010 switch (token->keyword)
10014 ++decl_specs->specs[(int) ds_inline];
10018 /* 14.5.2.3 [temp.mem]
10020 A member function template shall not be virtual. */
10021 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10022 error_at (token->location, "templates may not be %<virtual%>");
10023 else if (decl_specs)
10024 ++decl_specs->specs[(int) ds_virtual];
10029 ++decl_specs->specs[(int) ds_explicit];
10036 /* Consume the token. */
10037 return cp_lexer_consume_token (parser->lexer)->u.value;
10040 /* Parse a linkage-specification.
10042 linkage-specification:
10043 extern string-literal { declaration-seq [opt] }
10044 extern string-literal declaration */
10047 cp_parser_linkage_specification (cp_parser* parser)
10051 /* Look for the `extern' keyword. */
10052 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10054 /* Look for the string-literal. */
10055 linkage = cp_parser_string_literal (parser, false, false);
10057 /* Transform the literal into an identifier. If the literal is a
10058 wide-character string, or contains embedded NULs, then we can't
10059 handle it as the user wants. */
10060 if (strlen (TREE_STRING_POINTER (linkage))
10061 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10063 cp_parser_error (parser, "invalid linkage-specification");
10064 /* Assume C++ linkage. */
10065 linkage = lang_name_cplusplus;
10068 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10070 /* We're now using the new linkage. */
10071 push_lang_context (linkage);
10073 /* If the next token is a `{', then we're using the first
10075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10077 /* Consume the `{' token. */
10078 cp_lexer_consume_token (parser->lexer);
10079 /* Parse the declarations. */
10080 cp_parser_declaration_seq_opt (parser);
10081 /* Look for the closing `}'. */
10082 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10084 /* Otherwise, there's just one declaration. */
10087 bool saved_in_unbraced_linkage_specification_p;
10089 saved_in_unbraced_linkage_specification_p
10090 = parser->in_unbraced_linkage_specification_p;
10091 parser->in_unbraced_linkage_specification_p = true;
10092 cp_parser_declaration (parser);
10093 parser->in_unbraced_linkage_specification_p
10094 = saved_in_unbraced_linkage_specification_p;
10097 /* We're done with the linkage-specification. */
10098 pop_lang_context ();
10101 /* Parse a static_assert-declaration.
10103 static_assert-declaration:
10104 static_assert ( constant-expression , string-literal ) ;
10106 If MEMBER_P, this static_assert is a class member. */
10109 cp_parser_static_assert(cp_parser *parser, bool member_p)
10114 location_t saved_loc;
10116 /* Peek at the `static_assert' token so we can keep track of exactly
10117 where the static assertion started. */
10118 token = cp_lexer_peek_token (parser->lexer);
10119 saved_loc = token->location;
10121 /* Look for the `static_assert' keyword. */
10122 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10126 /* We know we are in a static assertion; commit to any tentative
10128 if (cp_parser_parsing_tentatively (parser))
10129 cp_parser_commit_to_tentative_parse (parser);
10131 /* Parse the `(' starting the static assertion condition. */
10132 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10134 /* Parse the constant-expression. */
10136 cp_parser_constant_expression (parser,
10137 /*allow_non_constant_p=*/false,
10138 /*non_constant_p=*/NULL);
10140 /* Parse the separating `,'. */
10141 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10143 /* Parse the string-literal message. */
10144 message = cp_parser_string_literal (parser,
10145 /*translate=*/false,
10148 /* A `)' completes the static assertion. */
10149 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10150 cp_parser_skip_to_closing_parenthesis (parser,
10151 /*recovering=*/true,
10152 /*or_comma=*/false,
10153 /*consume_paren=*/true);
10155 /* A semicolon terminates the declaration. */
10156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10158 /* Complete the static assertion, which may mean either processing
10159 the static assert now or saving it for template instantiation. */
10160 finish_static_assert (condition, message, saved_loc, member_p);
10163 /* Parse a `decltype' type. Returns the type.
10165 simple-type-specifier:
10166 decltype ( expression ) */
10169 cp_parser_decltype (cp_parser *parser)
10172 bool id_expression_or_member_access_p = false;
10173 const char *saved_message;
10174 bool saved_integral_constant_expression_p;
10175 bool saved_non_integral_constant_expression_p;
10176 cp_token *id_expr_start_token;
10178 /* Look for the `decltype' token. */
10179 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10180 return error_mark_node;
10182 /* Types cannot be defined in a `decltype' expression. Save away the
10184 saved_message = parser->type_definition_forbidden_message;
10186 /* And create the new one. */
10187 parser->type_definition_forbidden_message
10188 = G_("types may not be defined in %<decltype%> expressions");
10190 /* The restrictions on constant-expressions do not apply inside
10191 decltype expressions. */
10192 saved_integral_constant_expression_p
10193 = parser->integral_constant_expression_p;
10194 saved_non_integral_constant_expression_p
10195 = parser->non_integral_constant_expression_p;
10196 parser->integral_constant_expression_p = false;
10198 /* Do not actually evaluate the expression. */
10199 ++cp_unevaluated_operand;
10201 /* Do not warn about problems with the expression. */
10202 ++c_inhibit_evaluation_warnings;
10204 /* Parse the opening `('. */
10205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10206 return error_mark_node;
10208 /* First, try parsing an id-expression. */
10209 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10210 cp_parser_parse_tentatively (parser);
10211 expr = cp_parser_id_expression (parser,
10212 /*template_keyword_p=*/false,
10213 /*check_dependency_p=*/true,
10214 /*template_p=*/NULL,
10215 /*declarator_p=*/false,
10216 /*optional_p=*/false);
10218 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10220 bool non_integral_constant_expression_p = false;
10221 tree id_expression = expr;
10223 const char *error_msg;
10225 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10226 /* Lookup the name we got back from the id-expression. */
10227 expr = cp_parser_lookup_name (parser, expr,
10229 /*is_template=*/false,
10230 /*is_namespace=*/false,
10231 /*check_dependency=*/true,
10232 /*ambiguous_decls=*/NULL,
10233 id_expr_start_token->location);
10236 && expr != error_mark_node
10237 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10238 && TREE_CODE (expr) != TYPE_DECL
10239 && (TREE_CODE (expr) != BIT_NOT_EXPR
10240 || !TYPE_P (TREE_OPERAND (expr, 0)))
10241 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10243 /* Complete lookup of the id-expression. */
10244 expr = (finish_id_expression
10245 (id_expression, expr, parser->scope, &idk,
10246 /*integral_constant_expression_p=*/false,
10247 /*allow_non_integral_constant_expression_p=*/true,
10248 &non_integral_constant_expression_p,
10249 /*template_p=*/false,
10251 /*address_p=*/false,
10252 /*template_arg_p=*/false,
10254 id_expr_start_token->location));
10256 if (expr == error_mark_node)
10257 /* We found an id-expression, but it was something that we
10258 should not have found. This is an error, not something
10259 we can recover from, so note that we found an
10260 id-expression and we'll recover as gracefully as
10262 id_expression_or_member_access_p = true;
10266 && expr != error_mark_node
10267 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10268 /* We have an id-expression. */
10269 id_expression_or_member_access_p = true;
10272 if (!id_expression_or_member_access_p)
10274 /* Abort the id-expression parse. */
10275 cp_parser_abort_tentative_parse (parser);
10277 /* Parsing tentatively, again. */
10278 cp_parser_parse_tentatively (parser);
10280 /* Parse a class member access. */
10281 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10283 /*member_access_only_p=*/true, NULL);
10286 && expr != error_mark_node
10287 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10288 /* We have an id-expression. */
10289 id_expression_or_member_access_p = true;
10292 if (id_expression_or_member_access_p)
10293 /* We have parsed the complete id-expression or member access. */
10294 cp_parser_parse_definitely (parser);
10297 bool saved_greater_than_is_operator_p;
10299 /* Abort our attempt to parse an id-expression or member access
10301 cp_parser_abort_tentative_parse (parser);
10303 /* Within a parenthesized expression, a `>' token is always
10304 the greater-than operator. */
10305 saved_greater_than_is_operator_p
10306 = parser->greater_than_is_operator_p;
10307 parser->greater_than_is_operator_p = true;
10309 /* Parse a full expression. */
10310 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10312 /* The `>' token might be the end of a template-id or
10313 template-parameter-list now. */
10314 parser->greater_than_is_operator_p
10315 = saved_greater_than_is_operator_p;
10318 /* Go back to evaluating expressions. */
10319 --cp_unevaluated_operand;
10320 --c_inhibit_evaluation_warnings;
10322 /* Restore the old message and the integral constant expression
10324 parser->type_definition_forbidden_message = saved_message;
10325 parser->integral_constant_expression_p
10326 = saved_integral_constant_expression_p;
10327 parser->non_integral_constant_expression_p
10328 = saved_non_integral_constant_expression_p;
10330 if (expr == error_mark_node)
10332 /* Skip everything up to the closing `)'. */
10333 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10334 /*consume_paren=*/true);
10335 return error_mark_node;
10338 /* Parse to the closing `)'. */
10339 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10341 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10342 /*consume_paren=*/true);
10343 return error_mark_node;
10346 return finish_decltype_type (expr, id_expression_or_member_access_p);
10349 /* Special member functions [gram.special] */
10351 /* Parse a conversion-function-id.
10353 conversion-function-id:
10354 operator conversion-type-id
10356 Returns an IDENTIFIER_NODE representing the operator. */
10359 cp_parser_conversion_function_id (cp_parser* parser)
10363 tree saved_qualifying_scope;
10364 tree saved_object_scope;
10365 tree pushed_scope = NULL_TREE;
10367 /* Look for the `operator' token. */
10368 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10369 return error_mark_node;
10370 /* When we parse the conversion-type-id, the current scope will be
10371 reset. However, we need that information in able to look up the
10372 conversion function later, so we save it here. */
10373 saved_scope = parser->scope;
10374 saved_qualifying_scope = parser->qualifying_scope;
10375 saved_object_scope = parser->object_scope;
10376 /* We must enter the scope of the class so that the names of
10377 entities declared within the class are available in the
10378 conversion-type-id. For example, consider:
10385 S::operator I() { ... }
10387 In order to see that `I' is a type-name in the definition, we
10388 must be in the scope of `S'. */
10390 pushed_scope = push_scope (saved_scope);
10391 /* Parse the conversion-type-id. */
10392 type = cp_parser_conversion_type_id (parser);
10393 /* Leave the scope of the class, if any. */
10395 pop_scope (pushed_scope);
10396 /* Restore the saved scope. */
10397 parser->scope = saved_scope;
10398 parser->qualifying_scope = saved_qualifying_scope;
10399 parser->object_scope = saved_object_scope;
10400 /* If the TYPE is invalid, indicate failure. */
10401 if (type == error_mark_node)
10402 return error_mark_node;
10403 return mangle_conv_op_name_for_type (type);
10406 /* Parse a conversion-type-id:
10408 conversion-type-id:
10409 type-specifier-seq conversion-declarator [opt]
10411 Returns the TYPE specified. */
10414 cp_parser_conversion_type_id (cp_parser* parser)
10417 cp_decl_specifier_seq type_specifiers;
10418 cp_declarator *declarator;
10419 tree type_specified;
10421 /* Parse the attributes. */
10422 attributes = cp_parser_attributes_opt (parser);
10423 /* Parse the type-specifiers. */
10424 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10425 /*is_trailing_return=*/false,
10427 /* If that didn't work, stop. */
10428 if (type_specifiers.type == error_mark_node)
10429 return error_mark_node;
10430 /* Parse the conversion-declarator. */
10431 declarator = cp_parser_conversion_declarator_opt (parser);
10433 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10434 /*initialized=*/0, &attributes);
10436 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10438 /* Don't give this error when parsing tentatively. This happens to
10439 work because we always parse this definitively once. */
10440 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10441 && type_uses_auto (type_specified))
10443 error ("invalid use of %<auto%> in conversion operator");
10444 return error_mark_node;
10447 return type_specified;
10450 /* Parse an (optional) conversion-declarator.
10452 conversion-declarator:
10453 ptr-operator conversion-declarator [opt]
10457 static cp_declarator *
10458 cp_parser_conversion_declarator_opt (cp_parser* parser)
10460 enum tree_code code;
10462 cp_cv_quals cv_quals;
10464 /* We don't know if there's a ptr-operator next, or not. */
10465 cp_parser_parse_tentatively (parser);
10466 /* Try the ptr-operator. */
10467 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10468 /* If it worked, look for more conversion-declarators. */
10469 if (cp_parser_parse_definitely (parser))
10471 cp_declarator *declarator;
10473 /* Parse another optional declarator. */
10474 declarator = cp_parser_conversion_declarator_opt (parser);
10476 return cp_parser_make_indirect_declarator
10477 (code, class_type, cv_quals, declarator);
10483 /* Parse an (optional) ctor-initializer.
10486 : mem-initializer-list
10488 Returns TRUE iff the ctor-initializer was actually present. */
10491 cp_parser_ctor_initializer_opt (cp_parser* parser)
10493 /* If the next token is not a `:', then there is no
10494 ctor-initializer. */
10495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10497 /* Do default initialization of any bases and members. */
10498 if (DECL_CONSTRUCTOR_P (current_function_decl))
10499 finish_mem_initializers (NULL_TREE);
10504 /* Consume the `:' token. */
10505 cp_lexer_consume_token (parser->lexer);
10506 /* And the mem-initializer-list. */
10507 cp_parser_mem_initializer_list (parser);
10512 /* Parse a mem-initializer-list.
10514 mem-initializer-list:
10515 mem-initializer ... [opt]
10516 mem-initializer ... [opt] , mem-initializer-list */
10519 cp_parser_mem_initializer_list (cp_parser* parser)
10521 tree mem_initializer_list = NULL_TREE;
10522 cp_token *token = cp_lexer_peek_token (parser->lexer);
10524 /* Let the semantic analysis code know that we are starting the
10525 mem-initializer-list. */
10526 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10527 error_at (token->location,
10528 "only constructors take member initializers");
10530 /* Loop through the list. */
10533 tree mem_initializer;
10535 token = cp_lexer_peek_token (parser->lexer);
10536 /* Parse the mem-initializer. */
10537 mem_initializer = cp_parser_mem_initializer (parser);
10538 /* If the next token is a `...', we're expanding member initializers. */
10539 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10541 /* Consume the `...'. */
10542 cp_lexer_consume_token (parser->lexer);
10544 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10545 can be expanded but members cannot. */
10546 if (mem_initializer != error_mark_node
10547 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10549 error_at (token->location,
10550 "cannot expand initializer for member %<%D%>",
10551 TREE_PURPOSE (mem_initializer));
10552 mem_initializer = error_mark_node;
10555 /* Construct the pack expansion type. */
10556 if (mem_initializer != error_mark_node)
10557 mem_initializer = make_pack_expansion (mem_initializer);
10559 /* Add it to the list, unless it was erroneous. */
10560 if (mem_initializer != error_mark_node)
10562 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10563 mem_initializer_list = mem_initializer;
10565 /* If the next token is not a `,', we're done. */
10566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10568 /* Consume the `,' token. */
10569 cp_lexer_consume_token (parser->lexer);
10572 /* Perform semantic analysis. */
10573 if (DECL_CONSTRUCTOR_P (current_function_decl))
10574 finish_mem_initializers (mem_initializer_list);
10577 /* Parse a mem-initializer.
10580 mem-initializer-id ( expression-list [opt] )
10581 mem-initializer-id braced-init-list
10586 ( expression-list [opt] )
10588 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10589 class) or FIELD_DECL (for a non-static data member) to initialize;
10590 the TREE_VALUE is the expression-list. An empty initialization
10591 list is represented by void_list_node. */
10594 cp_parser_mem_initializer (cp_parser* parser)
10596 tree mem_initializer_id;
10597 tree expression_list;
10599 cp_token *token = cp_lexer_peek_token (parser->lexer);
10601 /* Find out what is being initialized. */
10602 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10604 permerror (token->location,
10605 "anachronistic old-style base class initializer");
10606 mem_initializer_id = NULL_TREE;
10610 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10611 if (mem_initializer_id == error_mark_node)
10612 return mem_initializer_id;
10614 member = expand_member_init (mem_initializer_id);
10615 if (member && !DECL_P (member))
10616 in_base_initializer = 1;
10618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10620 bool expr_non_constant_p;
10621 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10622 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10623 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10624 expression_list = build_tree_list (NULL_TREE, expression_list);
10629 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10631 /*allow_expansion_p=*/true,
10632 /*non_constant_p=*/NULL);
10634 return error_mark_node;
10635 expression_list = build_tree_list_vec (vec);
10636 release_tree_vector (vec);
10639 if (expression_list == error_mark_node)
10640 return error_mark_node;
10641 if (!expression_list)
10642 expression_list = void_type_node;
10644 in_base_initializer = 0;
10646 return member ? build_tree_list (member, expression_list) : error_mark_node;
10649 /* Parse a mem-initializer-id.
10651 mem-initializer-id:
10652 :: [opt] nested-name-specifier [opt] class-name
10655 Returns a TYPE indicating the class to be initializer for the first
10656 production. Returns an IDENTIFIER_NODE indicating the data member
10657 to be initialized for the second production. */
10660 cp_parser_mem_initializer_id (cp_parser* parser)
10662 bool global_scope_p;
10663 bool nested_name_specifier_p;
10664 bool template_p = false;
10667 cp_token *token = cp_lexer_peek_token (parser->lexer);
10669 /* `typename' is not allowed in this context ([temp.res]). */
10670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10672 error_at (token->location,
10673 "keyword %<typename%> not allowed in this context (a qualified "
10674 "member initializer is implicitly a type)");
10675 cp_lexer_consume_token (parser->lexer);
10677 /* Look for the optional `::' operator. */
10679 = (cp_parser_global_scope_opt (parser,
10680 /*current_scope_valid_p=*/false)
10682 /* Look for the optional nested-name-specifier. The simplest way to
10687 The keyword `typename' is not permitted in a base-specifier or
10688 mem-initializer; in these contexts a qualified name that
10689 depends on a template-parameter is implicitly assumed to be a
10692 is to assume that we have seen the `typename' keyword at this
10694 nested_name_specifier_p
10695 = (cp_parser_nested_name_specifier_opt (parser,
10696 /*typename_keyword_p=*/true,
10697 /*check_dependency_p=*/true,
10699 /*is_declaration=*/true)
10701 if (nested_name_specifier_p)
10702 template_p = cp_parser_optional_template_keyword (parser);
10703 /* If there is a `::' operator or a nested-name-specifier, then we
10704 are definitely looking for a class-name. */
10705 if (global_scope_p || nested_name_specifier_p)
10706 return cp_parser_class_name (parser,
10707 /*typename_keyword_p=*/true,
10708 /*template_keyword_p=*/template_p,
10710 /*check_dependency_p=*/true,
10711 /*class_head_p=*/false,
10712 /*is_declaration=*/true);
10713 /* Otherwise, we could also be looking for an ordinary identifier. */
10714 cp_parser_parse_tentatively (parser);
10715 /* Try a class-name. */
10716 id = cp_parser_class_name (parser,
10717 /*typename_keyword_p=*/true,
10718 /*template_keyword_p=*/false,
10720 /*check_dependency_p=*/true,
10721 /*class_head_p=*/false,
10722 /*is_declaration=*/true);
10723 /* If we found one, we're done. */
10724 if (cp_parser_parse_definitely (parser))
10726 /* Otherwise, look for an ordinary identifier. */
10727 return cp_parser_identifier (parser);
10730 /* Overloading [gram.over] */
10732 /* Parse an operator-function-id.
10734 operator-function-id:
10737 Returns an IDENTIFIER_NODE for the operator which is a
10738 human-readable spelling of the identifier, e.g., `operator +'. */
10741 cp_parser_operator_function_id (cp_parser* parser)
10743 /* Look for the `operator' keyword. */
10744 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10745 return error_mark_node;
10746 /* And then the name of the operator itself. */
10747 return cp_parser_operator (parser);
10750 /* Parse an operator.
10753 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10754 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10755 || ++ -- , ->* -> () []
10762 Returns an IDENTIFIER_NODE for the operator which is a
10763 human-readable spelling of the identifier, e.g., `operator +'. */
10766 cp_parser_operator (cp_parser* parser)
10768 tree id = NULL_TREE;
10771 /* Peek at the next token. */
10772 token = cp_lexer_peek_token (parser->lexer);
10773 /* Figure out which operator we have. */
10774 switch (token->type)
10780 /* The keyword should be either `new' or `delete'. */
10781 if (token->keyword == RID_NEW)
10783 else if (token->keyword == RID_DELETE)
10788 /* Consume the `new' or `delete' token. */
10789 cp_lexer_consume_token (parser->lexer);
10791 /* Peek at the next token. */
10792 token = cp_lexer_peek_token (parser->lexer);
10793 /* If it's a `[' token then this is the array variant of the
10795 if (token->type == CPP_OPEN_SQUARE)
10797 /* Consume the `[' token. */
10798 cp_lexer_consume_token (parser->lexer);
10799 /* Look for the `]' token. */
10800 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10801 id = ansi_opname (op == NEW_EXPR
10802 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10804 /* Otherwise, we have the non-array variant. */
10806 id = ansi_opname (op);
10812 id = ansi_opname (PLUS_EXPR);
10816 id = ansi_opname (MINUS_EXPR);
10820 id = ansi_opname (MULT_EXPR);
10824 id = ansi_opname (TRUNC_DIV_EXPR);
10828 id = ansi_opname (TRUNC_MOD_EXPR);
10832 id = ansi_opname (BIT_XOR_EXPR);
10836 id = ansi_opname (BIT_AND_EXPR);
10840 id = ansi_opname (BIT_IOR_EXPR);
10844 id = ansi_opname (BIT_NOT_EXPR);
10848 id = ansi_opname (TRUTH_NOT_EXPR);
10852 id = ansi_assopname (NOP_EXPR);
10856 id = ansi_opname (LT_EXPR);
10860 id = ansi_opname (GT_EXPR);
10864 id = ansi_assopname (PLUS_EXPR);
10868 id = ansi_assopname (MINUS_EXPR);
10872 id = ansi_assopname (MULT_EXPR);
10876 id = ansi_assopname (TRUNC_DIV_EXPR);
10880 id = ansi_assopname (TRUNC_MOD_EXPR);
10884 id = ansi_assopname (BIT_XOR_EXPR);
10888 id = ansi_assopname (BIT_AND_EXPR);
10892 id = ansi_assopname (BIT_IOR_EXPR);
10896 id = ansi_opname (LSHIFT_EXPR);
10900 id = ansi_opname (RSHIFT_EXPR);
10903 case CPP_LSHIFT_EQ:
10904 id = ansi_assopname (LSHIFT_EXPR);
10907 case CPP_RSHIFT_EQ:
10908 id = ansi_assopname (RSHIFT_EXPR);
10912 id = ansi_opname (EQ_EXPR);
10916 id = ansi_opname (NE_EXPR);
10920 id = ansi_opname (LE_EXPR);
10923 case CPP_GREATER_EQ:
10924 id = ansi_opname (GE_EXPR);
10928 id = ansi_opname (TRUTH_ANDIF_EXPR);
10932 id = ansi_opname (TRUTH_ORIF_EXPR);
10935 case CPP_PLUS_PLUS:
10936 id = ansi_opname (POSTINCREMENT_EXPR);
10939 case CPP_MINUS_MINUS:
10940 id = ansi_opname (PREDECREMENT_EXPR);
10944 id = ansi_opname (COMPOUND_EXPR);
10947 case CPP_DEREF_STAR:
10948 id = ansi_opname (MEMBER_REF);
10952 id = ansi_opname (COMPONENT_REF);
10955 case CPP_OPEN_PAREN:
10956 /* Consume the `('. */
10957 cp_lexer_consume_token (parser->lexer);
10958 /* Look for the matching `)'. */
10959 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10960 return ansi_opname (CALL_EXPR);
10962 case CPP_OPEN_SQUARE:
10963 /* Consume the `['. */
10964 cp_lexer_consume_token (parser->lexer);
10965 /* Look for the matching `]'. */
10966 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10967 return ansi_opname (ARRAY_REF);
10970 /* Anything else is an error. */
10974 /* If we have selected an identifier, we need to consume the
10977 cp_lexer_consume_token (parser->lexer);
10978 /* Otherwise, no valid operator name was present. */
10981 cp_parser_error (parser, "expected operator");
10982 id = error_mark_node;
10988 /* Parse a template-declaration.
10990 template-declaration:
10991 export [opt] template < template-parameter-list > declaration
10993 If MEMBER_P is TRUE, this template-declaration occurs within a
10996 The grammar rule given by the standard isn't correct. What
10997 is really meant is:
10999 template-declaration:
11000 export [opt] template-parameter-list-seq
11001 decl-specifier-seq [opt] init-declarator [opt] ;
11002 export [opt] template-parameter-list-seq
11003 function-definition
11005 template-parameter-list-seq:
11006 template-parameter-list-seq [opt]
11007 template < template-parameter-list > */
11010 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11012 /* Check for `export'. */
11013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11015 /* Consume the `export' token. */
11016 cp_lexer_consume_token (parser->lexer);
11017 /* Warn that we do not support `export'. */
11018 warning (0, "keyword %<export%> not implemented, and will be ignored");
11021 cp_parser_template_declaration_after_export (parser, member_p);
11024 /* Parse a template-parameter-list.
11026 template-parameter-list:
11028 template-parameter-list , template-parameter
11030 Returns a TREE_LIST. Each node represents a template parameter.
11031 The nodes are connected via their TREE_CHAINs. */
11034 cp_parser_template_parameter_list (cp_parser* parser)
11036 tree parameter_list = NULL_TREE;
11038 begin_template_parm_list ();
11043 bool is_parameter_pack;
11044 location_t parm_loc;
11046 /* Parse the template-parameter. */
11047 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11048 parameter = cp_parser_template_parameter (parser,
11050 &is_parameter_pack);
11051 /* Add it to the list. */
11052 if (parameter != error_mark_node)
11053 parameter_list = process_template_parm (parameter_list,
11057 is_parameter_pack);
11060 tree err_parm = build_tree_list (parameter, parameter);
11061 TREE_VALUE (err_parm) = error_mark_node;
11062 parameter_list = chainon (parameter_list, err_parm);
11065 /* If the next token is not a `,', we're done. */
11066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11068 /* Otherwise, consume the `,' token. */
11069 cp_lexer_consume_token (parser->lexer);
11072 return end_template_parm_list (parameter_list);
11075 /* Parse a template-parameter.
11077 template-parameter:
11079 parameter-declaration
11081 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11082 the parameter. The TREE_PURPOSE is the default value, if any.
11083 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11084 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11085 set to true iff this parameter is a parameter pack. */
11088 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11089 bool *is_parameter_pack)
11092 cp_parameter_declarator *parameter_declarator;
11093 cp_declarator *id_declarator;
11096 /* Assume it is a type parameter or a template parameter. */
11097 *is_non_type = false;
11098 /* Assume it not a parameter pack. */
11099 *is_parameter_pack = false;
11100 /* Peek at the next token. */
11101 token = cp_lexer_peek_token (parser->lexer);
11102 /* If it is `class' or `template', we have a type-parameter. */
11103 if (token->keyword == RID_TEMPLATE)
11104 return cp_parser_type_parameter (parser, is_parameter_pack);
11105 /* If it is `class' or `typename' we do not know yet whether it is a
11106 type parameter or a non-type parameter. Consider:
11108 template <typename T, typename T::X X> ...
11112 template <class C, class D*> ...
11114 Here, the first parameter is a type parameter, and the second is
11115 a non-type parameter. We can tell by looking at the token after
11116 the identifier -- if it is a `,', `=', or `>' then we have a type
11118 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11120 /* Peek at the token after `class' or `typename'. */
11121 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11122 /* If it's an ellipsis, we have a template type parameter
11124 if (token->type == CPP_ELLIPSIS)
11125 return cp_parser_type_parameter (parser, is_parameter_pack);
11126 /* If it's an identifier, skip it. */
11127 if (token->type == CPP_NAME)
11128 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11129 /* Now, see if the token looks like the end of a template
11131 if (token->type == CPP_COMMA
11132 || token->type == CPP_EQ
11133 || token->type == CPP_GREATER)
11134 return cp_parser_type_parameter (parser, is_parameter_pack);
11137 /* Otherwise, it is a non-type parameter.
11141 When parsing a default template-argument for a non-type
11142 template-parameter, the first non-nested `>' is taken as the end
11143 of the template parameter-list rather than a greater-than
11145 *is_non_type = true;
11146 parameter_declarator
11147 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11148 /*parenthesized_p=*/NULL);
11150 /* If the parameter declaration is marked as a parameter pack, set
11151 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11152 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11154 if (parameter_declarator
11155 && parameter_declarator->declarator
11156 && parameter_declarator->declarator->parameter_pack_p)
11158 *is_parameter_pack = true;
11159 parameter_declarator->declarator->parameter_pack_p = false;
11162 /* If the next token is an ellipsis, and we don't already have it
11163 marked as a parameter pack, then we have a parameter pack (that
11164 has no declarator). */
11165 if (!*is_parameter_pack
11166 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11167 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11169 /* Consume the `...'. */
11170 cp_lexer_consume_token (parser->lexer);
11171 maybe_warn_variadic_templates ();
11173 *is_parameter_pack = true;
11175 /* We might end up with a pack expansion as the type of the non-type
11176 template parameter, in which case this is a non-type template
11178 else if (parameter_declarator
11179 && parameter_declarator->decl_specifiers.type
11180 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11182 *is_parameter_pack = true;
11183 parameter_declarator->decl_specifiers.type =
11184 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11187 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11189 /* Parameter packs cannot have default arguments. However, a
11190 user may try to do so, so we'll parse them and give an
11191 appropriate diagnostic here. */
11193 /* Consume the `='. */
11194 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11195 cp_lexer_consume_token (parser->lexer);
11197 /* Find the name of the parameter pack. */
11198 id_declarator = parameter_declarator->declarator;
11199 while (id_declarator && id_declarator->kind != cdk_id)
11200 id_declarator = id_declarator->declarator;
11202 if (id_declarator && id_declarator->kind == cdk_id)
11203 error_at (start_token->location,
11204 "template parameter pack %qD cannot have a default argument",
11205 id_declarator->u.id.unqualified_name);
11207 error_at (start_token->location,
11208 "template parameter pack cannot have a default argument");
11210 /* Parse the default argument, but throw away the result. */
11211 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11214 parm = grokdeclarator (parameter_declarator->declarator,
11215 ¶meter_declarator->decl_specifiers,
11216 TPARM, /*initialized=*/0,
11217 /*attrlist=*/NULL);
11218 if (parm == error_mark_node)
11219 return error_mark_node;
11221 return build_tree_list (parameter_declarator->default_argument, parm);
11224 /* Parse a type-parameter.
11227 class identifier [opt]
11228 class identifier [opt] = type-id
11229 typename identifier [opt]
11230 typename identifier [opt] = type-id
11231 template < template-parameter-list > class identifier [opt]
11232 template < template-parameter-list > class identifier [opt]
11235 GNU Extension (variadic templates):
11238 class ... identifier [opt]
11239 typename ... identifier [opt]
11241 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11242 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11243 the declaration of the parameter.
11245 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11248 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11253 /* Look for a keyword to tell us what kind of parameter this is. */
11254 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11256 return error_mark_node;
11258 switch (token->keyword)
11264 tree default_argument;
11266 /* If the next token is an ellipsis, we have a template
11268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11270 /* Consume the `...' token. */
11271 cp_lexer_consume_token (parser->lexer);
11272 maybe_warn_variadic_templates ();
11274 *is_parameter_pack = true;
11277 /* If the next token is an identifier, then it names the
11279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11280 identifier = cp_parser_identifier (parser);
11282 identifier = NULL_TREE;
11284 /* Create the parameter. */
11285 parameter = finish_template_type_parm (class_type_node, identifier);
11287 /* If the next token is an `=', we have a default argument. */
11288 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11290 /* Consume the `=' token. */
11291 cp_lexer_consume_token (parser->lexer);
11292 /* Parse the default-argument. */
11293 push_deferring_access_checks (dk_no_deferred);
11294 default_argument = cp_parser_type_id (parser);
11296 /* Template parameter packs cannot have default
11298 if (*is_parameter_pack)
11301 error_at (token->location,
11302 "template parameter pack %qD cannot have a "
11303 "default argument", identifier);
11305 error_at (token->location,
11306 "template parameter packs cannot have "
11307 "default arguments");
11308 default_argument = NULL_TREE;
11310 pop_deferring_access_checks ();
11313 default_argument = NULL_TREE;
11315 /* Create the combined representation of the parameter and the
11316 default argument. */
11317 parameter = build_tree_list (default_argument, parameter);
11324 tree default_argument;
11326 /* Look for the `<'. */
11327 cp_parser_require (parser, CPP_LESS, RT_LESS);
11328 /* Parse the template-parameter-list. */
11329 cp_parser_template_parameter_list (parser);
11330 /* Look for the `>'. */
11331 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11332 /* Look for the `class' keyword. */
11333 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11334 /* If the next token is an ellipsis, we have a template
11336 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11338 /* Consume the `...' token. */
11339 cp_lexer_consume_token (parser->lexer);
11340 maybe_warn_variadic_templates ();
11342 *is_parameter_pack = true;
11344 /* If the next token is an `=', then there is a
11345 default-argument. If the next token is a `>', we are at
11346 the end of the parameter-list. If the next token is a `,',
11347 then we are at the end of this parameter. */
11348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11349 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11350 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11352 identifier = cp_parser_identifier (parser);
11353 /* Treat invalid names as if the parameter were nameless. */
11354 if (identifier == error_mark_node)
11355 identifier = NULL_TREE;
11358 identifier = NULL_TREE;
11360 /* Create the template parameter. */
11361 parameter = finish_template_template_parm (class_type_node,
11364 /* If the next token is an `=', then there is a
11365 default-argument. */
11366 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11370 /* Consume the `='. */
11371 cp_lexer_consume_token (parser->lexer);
11372 /* Parse the id-expression. */
11373 push_deferring_access_checks (dk_no_deferred);
11374 /* save token before parsing the id-expression, for error
11376 token = cp_lexer_peek_token (parser->lexer);
11378 = cp_parser_id_expression (parser,
11379 /*template_keyword_p=*/false,
11380 /*check_dependency_p=*/true,
11381 /*template_p=*/&is_template,
11382 /*declarator_p=*/false,
11383 /*optional_p=*/false);
11384 if (TREE_CODE (default_argument) == TYPE_DECL)
11385 /* If the id-expression was a template-id that refers to
11386 a template-class, we already have the declaration here,
11387 so no further lookup is needed. */
11390 /* Look up the name. */
11392 = cp_parser_lookup_name (parser, default_argument,
11394 /*is_template=*/is_template,
11395 /*is_namespace=*/false,
11396 /*check_dependency=*/true,
11397 /*ambiguous_decls=*/NULL,
11399 /* See if the default argument is valid. */
11401 = check_template_template_default_arg (default_argument);
11403 /* Template parameter packs cannot have default
11405 if (*is_parameter_pack)
11408 error_at (token->location,
11409 "template parameter pack %qD cannot "
11410 "have a default argument",
11413 error_at (token->location, "template parameter packs cannot "
11414 "have default arguments");
11415 default_argument = NULL_TREE;
11417 pop_deferring_access_checks ();
11420 default_argument = NULL_TREE;
11422 /* Create the combined representation of the parameter and the
11423 default argument. */
11424 parameter = build_tree_list (default_argument, parameter);
11429 gcc_unreachable ();
11436 /* Parse a template-id.
11439 template-name < template-argument-list [opt] >
11441 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11442 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11443 returned. Otherwise, if the template-name names a function, or set
11444 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11445 names a class, returns a TYPE_DECL for the specialization.
11447 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11448 uninstantiated templates. */
11451 cp_parser_template_id (cp_parser *parser,
11452 bool template_keyword_p,
11453 bool check_dependency_p,
11454 bool is_declaration)
11460 cp_token_position start_of_id = 0;
11461 deferred_access_check *chk;
11462 VEC (deferred_access_check,gc) *access_check;
11463 cp_token *next_token = NULL, *next_token_2 = NULL;
11464 bool is_identifier;
11466 /* If the next token corresponds to a template-id, there is no need
11468 next_token = cp_lexer_peek_token (parser->lexer);
11469 if (next_token->type == CPP_TEMPLATE_ID)
11471 struct tree_check *check_value;
11473 /* Get the stored value. */
11474 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11475 /* Perform any access checks that were deferred. */
11476 access_check = check_value->checks;
11479 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11480 perform_or_defer_access_check (chk->binfo,
11484 /* Return the stored value. */
11485 return check_value->value;
11488 /* Avoid performing name lookup if there is no possibility of
11489 finding a template-id. */
11490 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11491 || (next_token->type == CPP_NAME
11492 && !cp_parser_nth_token_starts_template_argument_list_p
11495 cp_parser_error (parser, "expected template-id");
11496 return error_mark_node;
11499 /* Remember where the template-id starts. */
11500 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11501 start_of_id = cp_lexer_token_position (parser->lexer, false);
11503 push_deferring_access_checks (dk_deferred);
11505 /* Parse the template-name. */
11506 is_identifier = false;
11507 templ = cp_parser_template_name (parser, template_keyword_p,
11508 check_dependency_p,
11511 if (templ == error_mark_node || is_identifier)
11513 pop_deferring_access_checks ();
11517 /* If we find the sequence `[:' after a template-name, it's probably
11518 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11519 parse correctly the argument list. */
11520 next_token = cp_lexer_peek_token (parser->lexer);
11521 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11522 if (next_token->type == CPP_OPEN_SQUARE
11523 && next_token->flags & DIGRAPH
11524 && next_token_2->type == CPP_COLON
11525 && !(next_token_2->flags & PREV_WHITE))
11527 cp_parser_parse_tentatively (parser);
11528 /* Change `:' into `::'. */
11529 next_token_2->type = CPP_SCOPE;
11530 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11532 cp_lexer_consume_token (parser->lexer);
11534 /* Parse the arguments. */
11535 arguments = cp_parser_enclosed_template_argument_list (parser);
11536 if (!cp_parser_parse_definitely (parser))
11538 /* If we couldn't parse an argument list, then we revert our changes
11539 and return simply an error. Maybe this is not a template-id
11541 next_token_2->type = CPP_COLON;
11542 cp_parser_error (parser, "expected %<<%>");
11543 pop_deferring_access_checks ();
11544 return error_mark_node;
11546 /* Otherwise, emit an error about the invalid digraph, but continue
11547 parsing because we got our argument list. */
11548 if (permerror (next_token->location,
11549 "%<<::%> cannot begin a template-argument list"))
11551 static bool hint = false;
11552 inform (next_token->location,
11553 "%<<:%> is an alternate spelling for %<[%>."
11554 " Insert whitespace between %<<%> and %<::%>");
11555 if (!hint && !flag_permissive)
11557 inform (next_token->location, "(if you use %<-fpermissive%>"
11558 " G++ will accept your code)");
11565 /* Look for the `<' that starts the template-argument-list. */
11566 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11568 pop_deferring_access_checks ();
11569 return error_mark_node;
11571 /* Parse the arguments. */
11572 arguments = cp_parser_enclosed_template_argument_list (parser);
11575 /* Build a representation of the specialization. */
11576 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11577 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11578 else if (DECL_CLASS_TEMPLATE_P (templ)
11579 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11581 bool entering_scope;
11582 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11583 template (rather than some instantiation thereof) only if
11584 is not nested within some other construct. For example, in
11585 "template <typename T> void f(T) { A<T>::", A<T> is just an
11586 instantiation of A. */
11587 entering_scope = (template_parm_scope_p ()
11588 && cp_lexer_next_token_is (parser->lexer,
11591 = finish_template_type (templ, arguments, entering_scope);
11595 /* If it's not a class-template or a template-template, it should be
11596 a function-template. */
11597 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11598 || TREE_CODE (templ) == OVERLOAD
11599 || BASELINK_P (templ)));
11601 template_id = lookup_template_function (templ, arguments);
11604 /* If parsing tentatively, replace the sequence of tokens that makes
11605 up the template-id with a CPP_TEMPLATE_ID token. That way,
11606 should we re-parse the token stream, we will not have to repeat
11607 the effort required to do the parse, nor will we issue duplicate
11608 error messages about problems during instantiation of the
11612 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11614 /* Reset the contents of the START_OF_ID token. */
11615 token->type = CPP_TEMPLATE_ID;
11616 /* Retrieve any deferred checks. Do not pop this access checks yet
11617 so the memory will not be reclaimed during token replacing below. */
11618 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11619 token->u.tree_check_value->value = template_id;
11620 token->u.tree_check_value->checks = get_deferred_access_checks ();
11621 token->keyword = RID_MAX;
11623 /* Purge all subsequent tokens. */
11624 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11626 /* ??? Can we actually assume that, if template_id ==
11627 error_mark_node, we will have issued a diagnostic to the
11628 user, as opposed to simply marking the tentative parse as
11630 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11631 error_at (token->location, "parse error in template argument list");
11634 pop_deferring_access_checks ();
11635 return template_id;
11638 /* Parse a template-name.
11643 The standard should actually say:
11647 operator-function-id
11649 A defect report has been filed about this issue.
11651 A conversion-function-id cannot be a template name because they cannot
11652 be part of a template-id. In fact, looking at this code:
11654 a.operator K<int>()
11656 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11657 It is impossible to call a templated conversion-function-id with an
11658 explicit argument list, since the only allowed template parameter is
11659 the type to which it is converting.
11661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11662 `template' keyword, in a construction like:
11666 In that case `f' is taken to be a template-name, even though there
11667 is no way of knowing for sure.
11669 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11670 name refers to a set of overloaded functions, at least one of which
11671 is a template, or an IDENTIFIER_NODE with the name of the template,
11672 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11673 names are looked up inside uninstantiated templates. */
11676 cp_parser_template_name (cp_parser* parser,
11677 bool template_keyword_p,
11678 bool check_dependency_p,
11679 bool is_declaration,
11680 bool *is_identifier)
11685 cp_token *token = cp_lexer_peek_token (parser->lexer);
11687 /* If the next token is `operator', then we have either an
11688 operator-function-id or a conversion-function-id. */
11689 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11691 /* We don't know whether we're looking at an
11692 operator-function-id or a conversion-function-id. */
11693 cp_parser_parse_tentatively (parser);
11694 /* Try an operator-function-id. */
11695 identifier = cp_parser_operator_function_id (parser);
11696 /* If that didn't work, try a conversion-function-id. */
11697 if (!cp_parser_parse_definitely (parser))
11699 cp_parser_error (parser, "expected template-name");
11700 return error_mark_node;
11703 /* Look for the identifier. */
11705 identifier = cp_parser_identifier (parser);
11707 /* If we didn't find an identifier, we don't have a template-id. */
11708 if (identifier == error_mark_node)
11709 return error_mark_node;
11711 /* If the name immediately followed the `template' keyword, then it
11712 is a template-name. However, if the next token is not `<', then
11713 we do not treat it as a template-name, since it is not being used
11714 as part of a template-id. This enables us to handle constructs
11717 template <typename T> struct S { S(); };
11718 template <typename T> S<T>::S();
11720 correctly. We would treat `S' as a template -- if it were `S<T>'
11721 -- but we do not if there is no `<'. */
11723 if (processing_template_decl
11724 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11726 /* In a declaration, in a dependent context, we pretend that the
11727 "template" keyword was present in order to improve error
11728 recovery. For example, given:
11730 template <typename T> void f(T::X<int>);
11732 we want to treat "X<int>" as a template-id. */
11734 && !template_keyword_p
11735 && parser->scope && TYPE_P (parser->scope)
11736 && check_dependency_p
11737 && dependent_scope_p (parser->scope)
11738 /* Do not do this for dtors (or ctors), since they never
11739 need the template keyword before their name. */
11740 && !constructor_name_p (identifier, parser->scope))
11742 cp_token_position start = 0;
11744 /* Explain what went wrong. */
11745 error_at (token->location, "non-template %qD used as template",
11747 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11748 parser->scope, identifier);
11749 /* If parsing tentatively, find the location of the "<" token. */
11750 if (cp_parser_simulate_error (parser))
11751 start = cp_lexer_token_position (parser->lexer, true);
11752 /* Parse the template arguments so that we can issue error
11753 messages about them. */
11754 cp_lexer_consume_token (parser->lexer);
11755 cp_parser_enclosed_template_argument_list (parser);
11756 /* Skip tokens until we find a good place from which to
11757 continue parsing. */
11758 cp_parser_skip_to_closing_parenthesis (parser,
11759 /*recovering=*/true,
11761 /*consume_paren=*/false);
11762 /* If parsing tentatively, permanently remove the
11763 template argument list. That will prevent duplicate
11764 error messages from being issued about the missing
11765 "template" keyword. */
11767 cp_lexer_purge_tokens_after (parser->lexer, start);
11769 *is_identifier = true;
11773 /* If the "template" keyword is present, then there is generally
11774 no point in doing name-lookup, so we just return IDENTIFIER.
11775 But, if the qualifying scope is non-dependent then we can
11776 (and must) do name-lookup normally. */
11777 if (template_keyword_p
11779 || (TYPE_P (parser->scope)
11780 && dependent_type_p (parser->scope))))
11784 /* Look up the name. */
11785 decl = cp_parser_lookup_name (parser, identifier,
11787 /*is_template=*/true,
11788 /*is_namespace=*/false,
11789 check_dependency_p,
11790 /*ambiguous_decls=*/NULL,
11793 /* If DECL is a template, then the name was a template-name. */
11794 if (TREE_CODE (decl) == TEMPLATE_DECL)
11798 tree fn = NULL_TREE;
11800 /* The standard does not explicitly indicate whether a name that
11801 names a set of overloaded declarations, some of which are
11802 templates, is a template-name. However, such a name should
11803 be a template-name; otherwise, there is no way to form a
11804 template-id for the overloaded templates. */
11805 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11806 if (TREE_CODE (fns) == OVERLOAD)
11807 for (fn = fns; fn; fn = OVL_NEXT (fn))
11808 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11813 /* The name does not name a template. */
11814 cp_parser_error (parser, "expected template-name");
11815 return error_mark_node;
11819 /* If DECL is dependent, and refers to a function, then just return
11820 its name; we will look it up again during template instantiation. */
11821 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11823 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11824 if (TYPE_P (scope) && dependent_type_p (scope))
11831 /* Parse a template-argument-list.
11833 template-argument-list:
11834 template-argument ... [opt]
11835 template-argument-list , template-argument ... [opt]
11837 Returns a TREE_VEC containing the arguments. */
11840 cp_parser_template_argument_list (cp_parser* parser)
11842 tree fixed_args[10];
11843 unsigned n_args = 0;
11844 unsigned alloced = 10;
11845 tree *arg_ary = fixed_args;
11847 bool saved_in_template_argument_list_p;
11849 bool saved_non_ice_p;
11851 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11852 parser->in_template_argument_list_p = true;
11853 /* Even if the template-id appears in an integral
11854 constant-expression, the contents of the argument list do
11856 saved_ice_p = parser->integral_constant_expression_p;
11857 parser->integral_constant_expression_p = false;
11858 saved_non_ice_p = parser->non_integral_constant_expression_p;
11859 parser->non_integral_constant_expression_p = false;
11860 /* Parse the arguments. */
11866 /* Consume the comma. */
11867 cp_lexer_consume_token (parser->lexer);
11869 /* Parse the template-argument. */
11870 argument = cp_parser_template_argument (parser);
11872 /* If the next token is an ellipsis, we're expanding a template
11874 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11876 if (argument == error_mark_node)
11878 cp_token *token = cp_lexer_peek_token (parser->lexer);
11879 error_at (token->location,
11880 "expected parameter pack before %<...%>");
11882 /* Consume the `...' token. */
11883 cp_lexer_consume_token (parser->lexer);
11885 /* Make the argument into a TYPE_PACK_EXPANSION or
11886 EXPR_PACK_EXPANSION. */
11887 argument = make_pack_expansion (argument);
11890 if (n_args == alloced)
11894 if (arg_ary == fixed_args)
11896 arg_ary = XNEWVEC (tree, alloced);
11897 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11900 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11902 arg_ary[n_args++] = argument;
11904 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11906 vec = make_tree_vec (n_args);
11909 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11911 if (arg_ary != fixed_args)
11913 parser->non_integral_constant_expression_p = saved_non_ice_p;
11914 parser->integral_constant_expression_p = saved_ice_p;
11915 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11916 #ifdef ENABLE_CHECKING
11917 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11922 /* Parse a template-argument.
11925 assignment-expression
11929 The representation is that of an assignment-expression, type-id, or
11930 id-expression -- except that the qualified id-expression is
11931 evaluated, so that the value returned is either a DECL or an
11934 Although the standard says "assignment-expression", it forbids
11935 throw-expressions or assignments in the template argument.
11936 Therefore, we use "conditional-expression" instead. */
11939 cp_parser_template_argument (cp_parser* parser)
11944 bool maybe_type_id = false;
11945 cp_token *token = NULL, *argument_start_token = NULL;
11948 /* There's really no way to know what we're looking at, so we just
11949 try each alternative in order.
11953 In a template-argument, an ambiguity between a type-id and an
11954 expression is resolved to a type-id, regardless of the form of
11955 the corresponding template-parameter.
11957 Therefore, we try a type-id first. */
11958 cp_parser_parse_tentatively (parser);
11959 argument = cp_parser_template_type_arg (parser);
11960 /* If there was no error parsing the type-id but the next token is a
11961 '>>', our behavior depends on which dialect of C++ we're
11962 parsing. In C++98, we probably found a typo for '> >'. But there
11963 are type-id which are also valid expressions. For instance:
11965 struct X { int operator >> (int); };
11966 template <int V> struct Foo {};
11969 Here 'X()' is a valid type-id of a function type, but the user just
11970 wanted to write the expression "X() >> 5". Thus, we remember that we
11971 found a valid type-id, but we still try to parse the argument as an
11972 expression to see what happens.
11974 In C++0x, the '>>' will be considered two separate '>'
11976 if (!cp_parser_error_occurred (parser)
11977 && cxx_dialect == cxx98
11978 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11980 maybe_type_id = true;
11981 cp_parser_abort_tentative_parse (parser);
11985 /* If the next token isn't a `,' or a `>', then this argument wasn't
11986 really finished. This means that the argument is not a valid
11988 if (!cp_parser_next_token_ends_template_argument_p (parser))
11989 cp_parser_error (parser, "expected template-argument");
11990 /* If that worked, we're done. */
11991 if (cp_parser_parse_definitely (parser))
11994 /* We're still not sure what the argument will be. */
11995 cp_parser_parse_tentatively (parser);
11996 /* Try a template. */
11997 argument_start_token = cp_lexer_peek_token (parser->lexer);
11998 argument = cp_parser_id_expression (parser,
11999 /*template_keyword_p=*/false,
12000 /*check_dependency_p=*/true,
12002 /*declarator_p=*/false,
12003 /*optional_p=*/false);
12004 /* If the next token isn't a `,' or a `>', then this argument wasn't
12005 really finished. */
12006 if (!cp_parser_next_token_ends_template_argument_p (parser))
12007 cp_parser_error (parser, "expected template-argument");
12008 if (!cp_parser_error_occurred (parser))
12010 /* Figure out what is being referred to. If the id-expression
12011 was for a class template specialization, then we will have a
12012 TYPE_DECL at this point. There is no need to do name lookup
12013 at this point in that case. */
12014 if (TREE_CODE (argument) != TYPE_DECL)
12015 argument = cp_parser_lookup_name (parser, argument,
12017 /*is_template=*/template_p,
12018 /*is_namespace=*/false,
12019 /*check_dependency=*/true,
12020 /*ambiguous_decls=*/NULL,
12021 argument_start_token->location);
12022 if (TREE_CODE (argument) != TEMPLATE_DECL
12023 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12024 cp_parser_error (parser, "expected template-name");
12026 if (cp_parser_parse_definitely (parser))
12028 /* It must be a non-type argument. There permitted cases are given
12029 in [temp.arg.nontype]:
12031 -- an integral constant-expression of integral or enumeration
12034 -- the name of a non-type template-parameter; or
12036 -- the name of an object or function with external linkage...
12038 -- the address of an object or function with external linkage...
12040 -- a pointer to member... */
12041 /* Look for a non-type template parameter. */
12042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12044 cp_parser_parse_tentatively (parser);
12045 argument = cp_parser_primary_expression (parser,
12046 /*address_p=*/false,
12048 /*template_arg_p=*/true,
12050 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12051 || !cp_parser_next_token_ends_template_argument_p (parser))
12052 cp_parser_simulate_error (parser);
12053 if (cp_parser_parse_definitely (parser))
12057 /* If the next token is "&", the argument must be the address of an
12058 object or function with external linkage. */
12059 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12061 cp_lexer_consume_token (parser->lexer);
12062 /* See if we might have an id-expression. */
12063 token = cp_lexer_peek_token (parser->lexer);
12064 if (token->type == CPP_NAME
12065 || token->keyword == RID_OPERATOR
12066 || token->type == CPP_SCOPE
12067 || token->type == CPP_TEMPLATE_ID
12068 || token->type == CPP_NESTED_NAME_SPECIFIER)
12070 cp_parser_parse_tentatively (parser);
12071 argument = cp_parser_primary_expression (parser,
12074 /*template_arg_p=*/true,
12076 if (cp_parser_error_occurred (parser)
12077 || !cp_parser_next_token_ends_template_argument_p (parser))
12078 cp_parser_abort_tentative_parse (parser);
12083 if (TREE_CODE (argument) == INDIRECT_REF)
12085 gcc_assert (REFERENCE_REF_P (argument));
12086 argument = TREE_OPERAND (argument, 0);
12089 /* If we're in a template, we represent a qualified-id referring
12090 to a static data member as a SCOPE_REF even if the scope isn't
12091 dependent so that we can check access control later. */
12093 if (TREE_CODE (probe) == SCOPE_REF)
12094 probe = TREE_OPERAND (probe, 1);
12095 if (TREE_CODE (probe) == VAR_DECL)
12097 /* A variable without external linkage might still be a
12098 valid constant-expression, so no error is issued here
12099 if the external-linkage check fails. */
12100 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12101 cp_parser_simulate_error (parser);
12103 else if (is_overloaded_fn (argument))
12104 /* All overloaded functions are allowed; if the external
12105 linkage test does not pass, an error will be issued
12109 && (TREE_CODE (argument) == OFFSET_REF
12110 || TREE_CODE (argument) == SCOPE_REF))
12111 /* A pointer-to-member. */
12113 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12116 cp_parser_simulate_error (parser);
12118 if (cp_parser_parse_definitely (parser))
12121 argument = build_x_unary_op (ADDR_EXPR, argument,
12122 tf_warning_or_error);
12127 /* If the argument started with "&", there are no other valid
12128 alternatives at this point. */
12131 cp_parser_error (parser, "invalid non-type template argument");
12132 return error_mark_node;
12135 /* If the argument wasn't successfully parsed as a type-id followed
12136 by '>>', the argument can only be a constant expression now.
12137 Otherwise, we try parsing the constant-expression tentatively,
12138 because the argument could really be a type-id. */
12140 cp_parser_parse_tentatively (parser);
12141 argument = cp_parser_constant_expression (parser,
12142 /*allow_non_constant_p=*/false,
12143 /*non_constant_p=*/NULL);
12144 argument = fold_non_dependent_expr (argument);
12145 if (!maybe_type_id)
12147 if (!cp_parser_next_token_ends_template_argument_p (parser))
12148 cp_parser_error (parser, "expected template-argument");
12149 if (cp_parser_parse_definitely (parser))
12151 /* We did our best to parse the argument as a non type-id, but that
12152 was the only alternative that matched (albeit with a '>' after
12153 it). We can assume it's just a typo from the user, and a
12154 diagnostic will then be issued. */
12155 return cp_parser_template_type_arg (parser);
12158 /* Parse an explicit-instantiation.
12160 explicit-instantiation:
12161 template declaration
12163 Although the standard says `declaration', what it really means is:
12165 explicit-instantiation:
12166 template decl-specifier-seq [opt] declarator [opt] ;
12168 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12169 supposed to be allowed. A defect report has been filed about this
12174 explicit-instantiation:
12175 storage-class-specifier template
12176 decl-specifier-seq [opt] declarator [opt] ;
12177 function-specifier template
12178 decl-specifier-seq [opt] declarator [opt] ; */
12181 cp_parser_explicit_instantiation (cp_parser* parser)
12183 int declares_class_or_enum;
12184 cp_decl_specifier_seq decl_specifiers;
12185 tree extension_specifier = NULL_TREE;
12187 /* Look for an (optional) storage-class-specifier or
12188 function-specifier. */
12189 if (cp_parser_allow_gnu_extensions_p (parser))
12191 extension_specifier
12192 = cp_parser_storage_class_specifier_opt (parser);
12193 if (!extension_specifier)
12194 extension_specifier
12195 = cp_parser_function_specifier_opt (parser,
12196 /*decl_specs=*/NULL);
12199 /* Look for the `template' keyword. */
12200 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12201 /* Let the front end know that we are processing an explicit
12203 begin_explicit_instantiation ();
12204 /* [temp.explicit] says that we are supposed to ignore access
12205 control while processing explicit instantiation directives. */
12206 push_deferring_access_checks (dk_no_check);
12207 /* Parse a decl-specifier-seq. */
12208 cp_parser_decl_specifier_seq (parser,
12209 CP_PARSER_FLAGS_OPTIONAL,
12211 &declares_class_or_enum);
12212 /* If there was exactly one decl-specifier, and it declared a class,
12213 and there's no declarator, then we have an explicit type
12215 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12219 type = check_tag_decl (&decl_specifiers);
12220 /* Turn access control back on for names used during
12221 template instantiation. */
12222 pop_deferring_access_checks ();
12224 do_type_instantiation (type, extension_specifier,
12225 /*complain=*/tf_error);
12229 cp_declarator *declarator;
12232 /* Parse the declarator. */
12234 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12235 /*ctor_dtor_or_conv_p=*/NULL,
12236 /*parenthesized_p=*/NULL,
12237 /*member_p=*/false);
12238 if (declares_class_or_enum & 2)
12239 cp_parser_check_for_definition_in_return_type (declarator,
12240 decl_specifiers.type,
12241 decl_specifiers.type_location);
12242 if (declarator != cp_error_declarator)
12244 if (decl_specifiers.specs[(int)ds_inline])
12245 permerror (input_location, "explicit instantiation shall not use"
12246 " %<inline%> specifier");
12247 if (decl_specifiers.specs[(int)ds_constexpr])
12248 permerror (input_location, "explicit instantiation shall not use"
12249 " %<constexpr%> specifier");
12251 decl = grokdeclarator (declarator, &decl_specifiers,
12252 NORMAL, 0, &decl_specifiers.attributes);
12253 /* Turn access control back on for names used during
12254 template instantiation. */
12255 pop_deferring_access_checks ();
12256 /* Do the explicit instantiation. */
12257 do_decl_instantiation (decl, extension_specifier);
12261 pop_deferring_access_checks ();
12262 /* Skip the body of the explicit instantiation. */
12263 cp_parser_skip_to_end_of_statement (parser);
12266 /* We're done with the instantiation. */
12267 end_explicit_instantiation ();
12269 cp_parser_consume_semicolon_at_end_of_statement (parser);
12272 /* Parse an explicit-specialization.
12274 explicit-specialization:
12275 template < > declaration
12277 Although the standard says `declaration', what it really means is:
12279 explicit-specialization:
12280 template <> decl-specifier [opt] init-declarator [opt] ;
12281 template <> function-definition
12282 template <> explicit-specialization
12283 template <> template-declaration */
12286 cp_parser_explicit_specialization (cp_parser* parser)
12288 bool need_lang_pop;
12289 cp_token *token = cp_lexer_peek_token (parser->lexer);
12291 /* Look for the `template' keyword. */
12292 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12293 /* Look for the `<'. */
12294 cp_parser_require (parser, CPP_LESS, RT_LESS);
12295 /* Look for the `>'. */
12296 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12297 /* We have processed another parameter list. */
12298 ++parser->num_template_parameter_lists;
12301 A template ... explicit specialization ... shall not have C
12303 if (current_lang_name == lang_name_c)
12305 error_at (token->location, "template specialization with C linkage");
12306 /* Give it C++ linkage to avoid confusing other parts of the
12308 push_lang_context (lang_name_cplusplus);
12309 need_lang_pop = true;
12312 need_lang_pop = false;
12313 /* Let the front end know that we are beginning a specialization. */
12314 if (!begin_specialization ())
12316 end_specialization ();
12320 /* If the next keyword is `template', we need to figure out whether
12321 or not we're looking a template-declaration. */
12322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12324 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12325 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12326 cp_parser_template_declaration_after_export (parser,
12327 /*member_p=*/false);
12329 cp_parser_explicit_specialization (parser);
12332 /* Parse the dependent declaration. */
12333 cp_parser_single_declaration (parser,
12335 /*member_p=*/false,
12336 /*explicit_specialization_p=*/true,
12337 /*friend_p=*/NULL);
12338 /* We're done with the specialization. */
12339 end_specialization ();
12340 /* For the erroneous case of a template with C linkage, we pushed an
12341 implicit C++ linkage scope; exit that scope now. */
12343 pop_lang_context ();
12344 /* We're done with this parameter list. */
12345 --parser->num_template_parameter_lists;
12348 /* Parse a type-specifier.
12351 simple-type-specifier
12354 elaborated-type-specifier
12362 Returns a representation of the type-specifier. For a
12363 class-specifier, enum-specifier, or elaborated-type-specifier, a
12364 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12366 The parser flags FLAGS is used to control type-specifier parsing.
12368 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12369 in a decl-specifier-seq.
12371 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12372 class-specifier, enum-specifier, or elaborated-type-specifier, then
12373 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12374 if a type is declared; 2 if it is defined. Otherwise, it is set to
12377 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12378 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12379 is set to FALSE. */
12382 cp_parser_type_specifier (cp_parser* parser,
12383 cp_parser_flags flags,
12384 cp_decl_specifier_seq *decl_specs,
12385 bool is_declaration,
12386 int* declares_class_or_enum,
12387 bool* is_cv_qualifier)
12389 tree type_spec = NULL_TREE;
12392 cp_decl_spec ds = ds_last;
12394 /* Assume this type-specifier does not declare a new type. */
12395 if (declares_class_or_enum)
12396 *declares_class_or_enum = 0;
12397 /* And that it does not specify a cv-qualifier. */
12398 if (is_cv_qualifier)
12399 *is_cv_qualifier = false;
12400 /* Peek at the next token. */
12401 token = cp_lexer_peek_token (parser->lexer);
12403 /* If we're looking at a keyword, we can use that to guide the
12404 production we choose. */
12405 keyword = token->keyword;
12409 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12410 goto elaborated_type_specifier;
12412 /* Look for the enum-specifier. */
12413 type_spec = cp_parser_enum_specifier (parser);
12414 /* If that worked, we're done. */
12417 if (declares_class_or_enum)
12418 *declares_class_or_enum = 2;
12420 cp_parser_set_decl_spec_type (decl_specs,
12423 /*user_defined_p=*/true);
12427 goto elaborated_type_specifier;
12429 /* Any of these indicate either a class-specifier, or an
12430 elaborated-type-specifier. */
12434 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12435 goto elaborated_type_specifier;
12437 /* Parse tentatively so that we can back up if we don't find a
12438 class-specifier. */
12439 cp_parser_parse_tentatively (parser);
12440 /* Look for the class-specifier. */
12441 type_spec = cp_parser_class_specifier (parser);
12442 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12443 /* If that worked, we're done. */
12444 if (cp_parser_parse_definitely (parser))
12446 if (declares_class_or_enum)
12447 *declares_class_or_enum = 2;
12449 cp_parser_set_decl_spec_type (decl_specs,
12452 /*user_defined_p=*/true);
12456 /* Fall through. */
12457 elaborated_type_specifier:
12458 /* We're declaring (not defining) a class or enum. */
12459 if (declares_class_or_enum)
12460 *declares_class_or_enum = 1;
12462 /* Fall through. */
12464 /* Look for an elaborated-type-specifier. */
12466 = (cp_parser_elaborated_type_specifier
12468 decl_specs && decl_specs->specs[(int) ds_friend],
12471 cp_parser_set_decl_spec_type (decl_specs,
12474 /*user_defined_p=*/true);
12479 if (is_cv_qualifier)
12480 *is_cv_qualifier = true;
12485 if (is_cv_qualifier)
12486 *is_cv_qualifier = true;
12491 if (is_cv_qualifier)
12492 *is_cv_qualifier = true;
12496 /* The `__complex__' keyword is a GNU extension. */
12504 /* Handle simple keywords. */
12509 ++decl_specs->specs[(int)ds];
12510 decl_specs->any_specifiers_p = true;
12512 return cp_lexer_consume_token (parser->lexer)->u.value;
12515 /* If we do not already have a type-specifier, assume we are looking
12516 at a simple-type-specifier. */
12517 type_spec = cp_parser_simple_type_specifier (parser,
12521 /* If we didn't find a type-specifier, and a type-specifier was not
12522 optional in this context, issue an error message. */
12523 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12525 cp_parser_error (parser, "expected type specifier");
12526 return error_mark_node;
12532 /* Parse a simple-type-specifier.
12534 simple-type-specifier:
12535 :: [opt] nested-name-specifier [opt] type-name
12536 :: [opt] nested-name-specifier template template-id
12551 simple-type-specifier:
12553 decltype ( expression )
12559 simple-type-specifier:
12561 __typeof__ unary-expression
12562 __typeof__ ( type-id )
12564 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12565 appropriately updated. */
12568 cp_parser_simple_type_specifier (cp_parser* parser,
12569 cp_decl_specifier_seq *decl_specs,
12570 cp_parser_flags flags)
12572 tree type = NULL_TREE;
12575 /* Peek at the next token. */
12576 token = cp_lexer_peek_token (parser->lexer);
12578 /* If we're looking at a keyword, things are easy. */
12579 switch (token->keyword)
12583 decl_specs->explicit_char_p = true;
12584 type = char_type_node;
12587 type = char16_type_node;
12590 type = char32_type_node;
12593 type = wchar_type_node;
12596 type = boolean_type_node;
12600 ++decl_specs->specs[(int) ds_short];
12601 type = short_integer_type_node;
12605 decl_specs->explicit_int_p = true;
12606 type = integer_type_node;
12609 if (!int128_integer_type_node)
12612 decl_specs->explicit_int128_p = true;
12613 type = int128_integer_type_node;
12617 ++decl_specs->specs[(int) ds_long];
12618 type = long_integer_type_node;
12622 ++decl_specs->specs[(int) ds_signed];
12623 type = integer_type_node;
12627 ++decl_specs->specs[(int) ds_unsigned];
12628 type = unsigned_type_node;
12631 type = float_type_node;
12634 type = double_type_node;
12637 type = void_type_node;
12641 maybe_warn_cpp0x (CPP0X_AUTO);
12642 type = make_auto ();
12646 /* Parse the `decltype' type. */
12647 type = cp_parser_decltype (parser);
12650 cp_parser_set_decl_spec_type (decl_specs, type,
12652 /*user_defined_p=*/true);
12657 /* Consume the `typeof' token. */
12658 cp_lexer_consume_token (parser->lexer);
12659 /* Parse the operand to `typeof'. */
12660 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12661 /* If it is not already a TYPE, take its type. */
12662 if (!TYPE_P (type))
12663 type = finish_typeof (type);
12666 cp_parser_set_decl_spec_type (decl_specs, type,
12668 /*user_defined_p=*/true);
12676 /* If the type-specifier was for a built-in type, we're done. */
12679 /* Record the type. */
12681 && (token->keyword != RID_SIGNED
12682 && token->keyword != RID_UNSIGNED
12683 && token->keyword != RID_SHORT
12684 && token->keyword != RID_LONG))
12685 cp_parser_set_decl_spec_type (decl_specs,
12688 /*user_defined=*/false);
12690 decl_specs->any_specifiers_p = true;
12692 /* Consume the token. */
12693 cp_lexer_consume_token (parser->lexer);
12695 /* There is no valid C++ program where a non-template type is
12696 followed by a "<". That usually indicates that the user thought
12697 that the type was a template. */
12698 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12700 return TYPE_NAME (type);
12703 /* The type-specifier must be a user-defined type. */
12704 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12709 /* Don't gobble tokens or issue error messages if this is an
12710 optional type-specifier. */
12711 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12712 cp_parser_parse_tentatively (parser);
12714 /* Look for the optional `::' operator. */
12716 = (cp_parser_global_scope_opt (parser,
12717 /*current_scope_valid_p=*/false)
12719 /* Look for the nested-name specifier. */
12721 = (cp_parser_nested_name_specifier_opt (parser,
12722 /*typename_keyword_p=*/false,
12723 /*check_dependency_p=*/true,
12725 /*is_declaration=*/false)
12727 token = cp_lexer_peek_token (parser->lexer);
12728 /* If we have seen a nested-name-specifier, and the next token
12729 is `template', then we are using the template-id production. */
12731 && cp_parser_optional_template_keyword (parser))
12733 /* Look for the template-id. */
12734 type = cp_parser_template_id (parser,
12735 /*template_keyword_p=*/true,
12736 /*check_dependency_p=*/true,
12737 /*is_declaration=*/false);
12738 /* If the template-id did not name a type, we are out of
12740 if (TREE_CODE (type) != TYPE_DECL)
12742 cp_parser_error (parser, "expected template-id for type");
12746 /* Otherwise, look for a type-name. */
12748 type = cp_parser_type_name (parser);
12749 /* Keep track of all name-lookups performed in class scopes. */
12753 && TREE_CODE (type) == TYPE_DECL
12754 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12755 maybe_note_name_used_in_class (DECL_NAME (type), type);
12756 /* If it didn't work out, we don't have a TYPE. */
12757 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12758 && !cp_parser_parse_definitely (parser))
12760 if (type && decl_specs)
12761 cp_parser_set_decl_spec_type (decl_specs, type,
12763 /*user_defined=*/true);
12766 /* If we didn't get a type-name, issue an error message. */
12767 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12769 cp_parser_error (parser, "expected type-name");
12770 return error_mark_node;
12773 /* There is no valid C++ program where a non-template type is
12774 followed by a "<". That usually indicates that the user thought
12775 that the type was a template. */
12776 if (type && type != error_mark_node)
12778 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12779 If it is, then the '<'...'>' enclose protocol names rather than
12780 template arguments, and so everything is fine. */
12781 if (c_dialect_objc () && !parser->scope
12782 && (objc_is_id (type) || objc_is_class_name (type)))
12784 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12785 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12787 /* Clobber the "unqualified" type previously entered into
12788 DECL_SPECS with the new, improved protocol-qualified version. */
12790 decl_specs->type = qual_type;
12795 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12802 /* Parse a type-name.
12815 Returns a TYPE_DECL for the type. */
12818 cp_parser_type_name (cp_parser* parser)
12822 /* We can't know yet whether it is a class-name or not. */
12823 cp_parser_parse_tentatively (parser);
12824 /* Try a class-name. */
12825 type_decl = cp_parser_class_name (parser,
12826 /*typename_keyword_p=*/false,
12827 /*template_keyword_p=*/false,
12829 /*check_dependency_p=*/true,
12830 /*class_head_p=*/false,
12831 /*is_declaration=*/false);
12832 /* If it's not a class-name, keep looking. */
12833 if (!cp_parser_parse_definitely (parser))
12835 /* It must be a typedef-name or an enum-name. */
12836 return cp_parser_nonclass_name (parser);
12842 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12850 Returns a TYPE_DECL for the type. */
12853 cp_parser_nonclass_name (cp_parser* parser)
12858 cp_token *token = cp_lexer_peek_token (parser->lexer);
12859 identifier = cp_parser_identifier (parser);
12860 if (identifier == error_mark_node)
12861 return error_mark_node;
12863 /* Look up the type-name. */
12864 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12866 if (TREE_CODE (type_decl) != TYPE_DECL
12867 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12869 /* See if this is an Objective-C type. */
12870 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12871 tree type = objc_get_protocol_qualified_type (identifier, protos);
12873 type_decl = TYPE_NAME (type);
12876 /* Issue an error if we did not find a type-name. */
12877 if (TREE_CODE (type_decl) != TYPE_DECL)
12879 if (!cp_parser_simulate_error (parser))
12880 cp_parser_name_lookup_error (parser, identifier, type_decl,
12881 NLE_TYPE, token->location);
12882 return error_mark_node;
12884 /* Remember that the name was used in the definition of the
12885 current class so that we can check later to see if the
12886 meaning would have been different after the class was
12887 entirely defined. */
12888 else if (type_decl != error_mark_node
12890 maybe_note_name_used_in_class (identifier, type_decl);
12895 /* Parse an elaborated-type-specifier. Note that the grammar given
12896 here incorporates the resolution to DR68.
12898 elaborated-type-specifier:
12899 class-key :: [opt] nested-name-specifier [opt] identifier
12900 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12901 enum-key :: [opt] nested-name-specifier [opt] identifier
12902 typename :: [opt] nested-name-specifier identifier
12903 typename :: [opt] nested-name-specifier template [opt]
12908 elaborated-type-specifier:
12909 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12910 class-key attributes :: [opt] nested-name-specifier [opt]
12911 template [opt] template-id
12912 enum attributes :: [opt] nested-name-specifier [opt] identifier
12914 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12915 declared `friend'. If IS_DECLARATION is TRUE, then this
12916 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12917 something is being declared.
12919 Returns the TYPE specified. */
12922 cp_parser_elaborated_type_specifier (cp_parser* parser,
12924 bool is_declaration)
12926 enum tag_types tag_type;
12928 tree type = NULL_TREE;
12929 tree attributes = NULL_TREE;
12931 cp_token *token = NULL;
12933 /* See if we're looking at the `enum' keyword. */
12934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12936 /* Consume the `enum' token. */
12937 cp_lexer_consume_token (parser->lexer);
12938 /* Remember that it's an enumeration type. */
12939 tag_type = enum_type;
12940 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12941 enums) is used here. */
12942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12943 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12945 pedwarn (input_location, 0, "elaborated-type-specifier "
12946 "for a scoped enum must not use the %<%D%> keyword",
12947 cp_lexer_peek_token (parser->lexer)->u.value);
12948 /* Consume the `struct' or `class' and parse it anyway. */
12949 cp_lexer_consume_token (parser->lexer);
12951 /* Parse the attributes. */
12952 attributes = cp_parser_attributes_opt (parser);
12954 /* Or, it might be `typename'. */
12955 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12958 /* Consume the `typename' token. */
12959 cp_lexer_consume_token (parser->lexer);
12960 /* Remember that it's a `typename' type. */
12961 tag_type = typename_type;
12963 /* Otherwise it must be a class-key. */
12966 tag_type = cp_parser_class_key (parser);
12967 if (tag_type == none_type)
12968 return error_mark_node;
12969 /* Parse the attributes. */
12970 attributes = cp_parser_attributes_opt (parser);
12973 /* Look for the `::' operator. */
12974 globalscope = cp_parser_global_scope_opt (parser,
12975 /*current_scope_valid_p=*/false);
12976 /* Look for the nested-name-specifier. */
12977 if (tag_type == typename_type && !globalscope)
12979 if (!cp_parser_nested_name_specifier (parser,
12980 /*typename_keyword_p=*/true,
12981 /*check_dependency_p=*/true,
12984 return error_mark_node;
12987 /* Even though `typename' is not present, the proposed resolution
12988 to Core Issue 180 says that in `class A<T>::B', `B' should be
12989 considered a type-name, even if `A<T>' is dependent. */
12990 cp_parser_nested_name_specifier_opt (parser,
12991 /*typename_keyword_p=*/true,
12992 /*check_dependency_p=*/true,
12995 /* For everything but enumeration types, consider a template-id.
12996 For an enumeration type, consider only a plain identifier. */
12997 if (tag_type != enum_type)
12999 bool template_p = false;
13002 /* Allow the `template' keyword. */
13003 template_p = cp_parser_optional_template_keyword (parser);
13004 /* If we didn't see `template', we don't know if there's a
13005 template-id or not. */
13007 cp_parser_parse_tentatively (parser);
13008 /* Parse the template-id. */
13009 token = cp_lexer_peek_token (parser->lexer);
13010 decl = cp_parser_template_id (parser, template_p,
13011 /*check_dependency_p=*/true,
13013 /* If we didn't find a template-id, look for an ordinary
13015 if (!template_p && !cp_parser_parse_definitely (parser))
13017 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13018 in effect, then we must assume that, upon instantiation, the
13019 template will correspond to a class. */
13020 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13021 && tag_type == typename_type)
13022 type = make_typename_type (parser->scope, decl,
13024 /*complain=*/tf_error);
13025 /* If the `typename' keyword is in effect and DECL is not a type
13026 decl. Then type is non existant. */
13027 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13030 type = TREE_TYPE (decl);
13035 token = cp_lexer_peek_token (parser->lexer);
13036 identifier = cp_parser_identifier (parser);
13038 if (identifier == error_mark_node)
13040 parser->scope = NULL_TREE;
13041 return error_mark_node;
13044 /* For a `typename', we needn't call xref_tag. */
13045 if (tag_type == typename_type
13046 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13047 return cp_parser_make_typename_type (parser, parser->scope,
13050 /* Look up a qualified name in the usual way. */
13054 tree ambiguous_decls;
13056 decl = cp_parser_lookup_name (parser, identifier,
13058 /*is_template=*/false,
13059 /*is_namespace=*/false,
13060 /*check_dependency=*/true,
13064 /* If the lookup was ambiguous, an error will already have been
13066 if (ambiguous_decls)
13067 return error_mark_node;
13069 /* If we are parsing friend declaration, DECL may be a
13070 TEMPLATE_DECL tree node here. However, we need to check
13071 whether this TEMPLATE_DECL results in valid code. Consider
13072 the following example:
13075 template <class T> class C {};
13078 template <class T> friend class N::C; // #1, valid code
13080 template <class T> class Y {
13081 friend class N::C; // #2, invalid code
13084 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13085 name lookup of `N::C'. We see that friend declaration must
13086 be template for the code to be valid. Note that
13087 processing_template_decl does not work here since it is
13088 always 1 for the above two cases. */
13090 decl = (cp_parser_maybe_treat_template_as_class
13091 (decl, /*tag_name_p=*/is_friend
13092 && parser->num_template_parameter_lists));
13094 if (TREE_CODE (decl) != TYPE_DECL)
13096 cp_parser_diagnose_invalid_type_name (parser,
13100 return error_mark_node;
13103 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13105 bool allow_template = (parser->num_template_parameter_lists
13106 || DECL_SELF_REFERENCE_P (decl));
13107 type = check_elaborated_type_specifier (tag_type, decl,
13110 if (type == error_mark_node)
13111 return error_mark_node;
13114 /* Forward declarations of nested types, such as
13119 are invalid unless all components preceding the final '::'
13120 are complete. If all enclosing types are complete, these
13121 declarations become merely pointless.
13123 Invalid forward declarations of nested types are errors
13124 caught elsewhere in parsing. Those that are pointless arrive
13127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13128 && !is_friend && !processing_explicit_instantiation)
13129 warning (0, "declaration %qD does not declare anything", decl);
13131 type = TREE_TYPE (decl);
13135 /* An elaborated-type-specifier sometimes introduces a new type and
13136 sometimes names an existing type. Normally, the rule is that it
13137 introduces a new type only if there is not an existing type of
13138 the same name already in scope. For example, given:
13141 void f() { struct S s; }
13143 the `struct S' in the body of `f' is the same `struct S' as in
13144 the global scope; the existing definition is used. However, if
13145 there were no global declaration, this would introduce a new
13146 local class named `S'.
13148 An exception to this rule applies to the following code:
13150 namespace N { struct S; }
13152 Here, the elaborated-type-specifier names a new type
13153 unconditionally; even if there is already an `S' in the
13154 containing scope this declaration names a new type.
13155 This exception only applies if the elaborated-type-specifier
13156 forms the complete declaration:
13160 A declaration consisting solely of `class-key identifier ;' is
13161 either a redeclaration of the name in the current scope or a
13162 forward declaration of the identifier as a class name. It
13163 introduces the name into the current scope.
13165 We are in this situation precisely when the next token is a `;'.
13167 An exception to the exception is that a `friend' declaration does
13168 *not* name a new type; i.e., given:
13170 struct S { friend struct T; };
13172 `T' is not a new type in the scope of `S'.
13174 Also, `new struct S' or `sizeof (struct S)' never results in the
13175 definition of a new type; a new type can only be declared in a
13176 declaration context. */
13182 /* Friends have special name lookup rules. */
13183 ts = ts_within_enclosing_non_class;
13184 else if (is_declaration
13185 && cp_lexer_next_token_is (parser->lexer,
13187 /* This is a `class-key identifier ;' */
13193 (parser->num_template_parameter_lists
13194 && (cp_parser_next_token_starts_class_definition_p (parser)
13195 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13196 /* An unqualified name was used to reference this type, so
13197 there were no qualifying templates. */
13198 if (!cp_parser_check_template_parameters (parser,
13199 /*num_templates=*/0,
13201 /*declarator=*/NULL))
13202 return error_mark_node;
13203 type = xref_tag (tag_type, identifier, ts, template_p);
13207 if (type == error_mark_node)
13208 return error_mark_node;
13210 /* Allow attributes on forward declarations of classes. */
13213 if (TREE_CODE (type) == TYPENAME_TYPE)
13214 warning (OPT_Wattributes,
13215 "attributes ignored on uninstantiated type");
13216 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13217 && ! processing_explicit_instantiation)
13218 warning (OPT_Wattributes,
13219 "attributes ignored on template instantiation");
13220 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13221 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13223 warning (OPT_Wattributes,
13224 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13227 if (tag_type != enum_type)
13228 cp_parser_check_class_key (tag_type, type);
13230 /* A "<" cannot follow an elaborated type specifier. If that
13231 happens, the user was probably trying to form a template-id. */
13232 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13237 /* Parse an enum-specifier.
13240 enum-head { enumerator-list [opt] }
13243 enum-key identifier [opt] enum-base [opt]
13244 enum-key nested-name-specifier identifier enum-base [opt]
13249 enum struct [C++0x]
13252 : type-specifier-seq
13254 opaque-enum-specifier:
13255 enum-key identifier enum-base [opt] ;
13258 enum-key attributes[opt] identifier [opt] enum-base [opt]
13259 { enumerator-list [opt] }attributes[opt]
13261 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13262 if the token stream isn't an enum-specifier after all. */
13265 cp_parser_enum_specifier (cp_parser* parser)
13268 tree type = NULL_TREE;
13270 tree nested_name_specifier = NULL_TREE;
13272 bool scoped_enum_p = false;
13273 bool has_underlying_type = false;
13274 bool nested_being_defined = false;
13275 bool new_value_list = false;
13276 bool is_new_type = false;
13277 bool is_anonymous = false;
13278 tree underlying_type = NULL_TREE;
13279 cp_token *type_start_token = NULL;
13281 /* Parse tentatively so that we can back up if we don't find a
13283 cp_parser_parse_tentatively (parser);
13285 /* Caller guarantees that the current token is 'enum', an identifier
13286 possibly follows, and the token after that is an opening brace.
13287 If we don't have an identifier, fabricate an anonymous name for
13288 the enumeration being defined. */
13289 cp_lexer_consume_token (parser->lexer);
13291 /* Parse the "class" or "struct", which indicates a scoped
13292 enumeration type in C++0x. */
13293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13294 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13296 if (cxx_dialect < cxx0x)
13297 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13299 /* Consume the `struct' or `class' token. */
13300 cp_lexer_consume_token (parser->lexer);
13302 scoped_enum_p = true;
13305 attributes = cp_parser_attributes_opt (parser);
13307 /* Clear the qualification. */
13308 parser->scope = NULL_TREE;
13309 parser->qualifying_scope = NULL_TREE;
13310 parser->object_scope = NULL_TREE;
13312 /* Figure out in what scope the declaration is being placed. */
13313 prev_scope = current_scope ();
13315 type_start_token = cp_lexer_peek_token (parser->lexer);
13317 push_deferring_access_checks (dk_no_check);
13318 nested_name_specifier
13319 = cp_parser_nested_name_specifier_opt (parser,
13320 /*typename_keyword_p=*/true,
13321 /*check_dependency_p=*/false,
13323 /*is_declaration=*/false);
13325 if (nested_name_specifier)
13329 identifier = cp_parser_identifier (parser);
13330 name = cp_parser_lookup_name (parser, identifier,
13332 /*is_template=*/false,
13333 /*is_namespace=*/false,
13334 /*check_dependency=*/true,
13335 /*ambiguous_decls=*/NULL,
13339 type = TREE_TYPE (name);
13340 if (TREE_CODE (type) == TYPENAME_TYPE)
13342 /* Are template enums allowed in ISO? */
13343 if (template_parm_scope_p ())
13344 pedwarn (type_start_token->location, OPT_pedantic,
13345 "%qD is an enumeration template", name);
13346 /* ignore a typename reference, for it will be solved by name
13352 error_at (type_start_token->location,
13353 "%qD is not an enumerator-name", identifier);
13357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13358 identifier = cp_parser_identifier (parser);
13361 identifier = make_anon_name ();
13362 is_anonymous = true;
13365 pop_deferring_access_checks ();
13367 /* Check for the `:' that denotes a specified underlying type in C++0x.
13368 Note that a ':' could also indicate a bitfield width, however. */
13369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13371 cp_decl_specifier_seq type_specifiers;
13373 /* Consume the `:'. */
13374 cp_lexer_consume_token (parser->lexer);
13376 /* Parse the type-specifier-seq. */
13377 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13378 /*is_trailing_return=*/false,
13381 /* At this point this is surely not elaborated type specifier. */
13382 if (!cp_parser_parse_definitely (parser))
13385 if (cxx_dialect < cxx0x)
13386 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13388 has_underlying_type = true;
13390 /* If that didn't work, stop. */
13391 if (type_specifiers.type != error_mark_node)
13393 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13394 /*initialized=*/0, NULL);
13395 if (underlying_type == error_mark_node)
13396 underlying_type = NULL_TREE;
13400 /* Look for the `{' but don't consume it yet. */
13401 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13403 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13405 cp_parser_error (parser, "expected %<{%>");
13406 if (has_underlying_type)
13409 /* An opaque-enum-specifier must have a ';' here. */
13410 if ((scoped_enum_p || underlying_type)
13411 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13413 cp_parser_error (parser, "expected %<;%> or %<{%>");
13414 if (has_underlying_type)
13419 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13422 if (nested_name_specifier)
13424 if (CLASS_TYPE_P (nested_name_specifier))
13426 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13427 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13428 push_scope (nested_name_specifier);
13430 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13432 push_nested_namespace (nested_name_specifier);
13436 /* Issue an error message if type-definitions are forbidden here. */
13437 if (!cp_parser_check_type_definition (parser))
13438 type = error_mark_node;
13440 /* Create the new type. We do this before consuming the opening
13441 brace so the enum will be recorded as being on the line of its
13442 tag (or the 'enum' keyword, if there is no tag). */
13443 type = start_enum (identifier, type, underlying_type,
13444 scoped_enum_p, &is_new_type);
13446 /* If the next token is not '{' it is an opaque-enum-specifier or an
13447 elaborated-type-specifier. */
13448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13450 if (nested_name_specifier)
13452 /* The following catches invalid code such as:
13453 enum class S<int>::E { A, B, C }; */
13454 if (!processing_specialization
13455 && CLASS_TYPE_P (nested_name_specifier)
13456 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13457 error_at (type_start_token->location, "cannot add an enumerator "
13458 "list to a template instantiation");
13460 /* If that scope does not contain the scope in which the
13461 class was originally declared, the program is invalid. */
13462 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13464 if (at_namespace_scope_p ())
13465 error_at (type_start_token->location,
13466 "declaration of %qD in namespace %qD which does not "
13468 type, prev_scope, nested_name_specifier);
13470 error_at (type_start_token->location,
13471 "declaration of %qD in %qD which does not enclose %qD",
13472 type, prev_scope, nested_name_specifier);
13473 type = error_mark_node;
13478 begin_scope (sk_scoped_enum, type);
13480 /* Consume the opening brace. */
13481 cp_lexer_consume_token (parser->lexer);
13483 if (type == error_mark_node)
13484 ; /* Nothing to add */
13485 else if (OPAQUE_ENUM_P (type)
13486 || (cxx_dialect > cxx98 && processing_specialization))
13488 new_value_list = true;
13489 SET_OPAQUE_ENUM_P (type, false);
13490 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13494 error_at (type_start_token->location, "multiple definition of %q#T", type);
13495 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13496 "previous definition here");
13497 type = error_mark_node;
13500 if (type == error_mark_node)
13501 cp_parser_skip_to_end_of_block_or_statement (parser);
13502 /* If the next token is not '}', then there are some enumerators. */
13503 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13504 cp_parser_enumerator_list (parser, type);
13506 /* Consume the final '}'. */
13507 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13514 /* If a ';' follows, then it is an opaque-enum-specifier
13515 and additional restrictions apply. */
13516 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13519 error_at (type_start_token->location,
13520 "opaque-enum-specifier without name");
13521 else if (nested_name_specifier)
13522 error_at (type_start_token->location,
13523 "opaque-enum-specifier must use a simple identifier");
13527 /* Look for trailing attributes to apply to this enumeration, and
13528 apply them if appropriate. */
13529 if (cp_parser_allow_gnu_extensions_p (parser))
13531 tree trailing_attr = cp_parser_attributes_opt (parser);
13532 trailing_attr = chainon (trailing_attr, attributes);
13533 cplus_decl_attributes (&type,
13535 (int) ATTR_FLAG_TYPE_IN_PLACE);
13538 /* Finish up the enumeration. */
13539 if (type != error_mark_node)
13541 if (new_value_list)
13542 finish_enum_value_list (type);
13544 finish_enum (type);
13547 if (nested_name_specifier)
13549 if (CLASS_TYPE_P (nested_name_specifier))
13551 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13552 pop_scope (nested_name_specifier);
13554 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13556 pop_nested_namespace (nested_name_specifier);
13562 /* Parse an enumerator-list. The enumerators all have the indicated
13566 enumerator-definition
13567 enumerator-list , enumerator-definition */
13570 cp_parser_enumerator_list (cp_parser* parser, tree type)
13574 /* Parse an enumerator-definition. */
13575 cp_parser_enumerator_definition (parser, type);
13577 /* If the next token is not a ',', we've reached the end of
13579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13581 /* Otherwise, consume the `,' and keep going. */
13582 cp_lexer_consume_token (parser->lexer);
13583 /* If the next token is a `}', there is a trailing comma. */
13584 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13586 if (!in_system_header)
13587 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13593 /* Parse an enumerator-definition. The enumerator has the indicated
13596 enumerator-definition:
13598 enumerator = constant-expression
13604 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13610 /* Save the input location because we are interested in the location
13611 of the identifier and not the location of the explicit value. */
13612 loc = cp_lexer_peek_token (parser->lexer)->location;
13614 /* Look for the identifier. */
13615 identifier = cp_parser_identifier (parser);
13616 if (identifier == error_mark_node)
13619 /* If the next token is an '=', then there is an explicit value. */
13620 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13622 /* Consume the `=' token. */
13623 cp_lexer_consume_token (parser->lexer);
13624 /* Parse the value. */
13625 value = cp_parser_constant_expression (parser,
13626 /*allow_non_constant_p=*/false,
13632 /* If we are processing a template, make sure the initializer of the
13633 enumerator doesn't contain any bare template parameter pack. */
13634 if (check_for_bare_parameter_packs (value))
13635 value = error_mark_node;
13637 /* Create the enumerator. */
13638 build_enumerator (identifier, value, type, loc);
13641 /* Parse a namespace-name.
13644 original-namespace-name
13647 Returns the NAMESPACE_DECL for the namespace. */
13650 cp_parser_namespace_name (cp_parser* parser)
13653 tree namespace_decl;
13655 cp_token *token = cp_lexer_peek_token (parser->lexer);
13657 /* Get the name of the namespace. */
13658 identifier = cp_parser_identifier (parser);
13659 if (identifier == error_mark_node)
13660 return error_mark_node;
13662 /* Look up the identifier in the currently active scope. Look only
13663 for namespaces, due to:
13665 [basic.lookup.udir]
13667 When looking up a namespace-name in a using-directive or alias
13668 definition, only namespace names are considered.
13672 [basic.lookup.qual]
13674 During the lookup of a name preceding the :: scope resolution
13675 operator, object, function, and enumerator names are ignored.
13677 (Note that cp_parser_qualifying_entity only calls this
13678 function if the token after the name is the scope resolution
13680 namespace_decl = cp_parser_lookup_name (parser, identifier,
13682 /*is_template=*/false,
13683 /*is_namespace=*/true,
13684 /*check_dependency=*/true,
13685 /*ambiguous_decls=*/NULL,
13687 /* If it's not a namespace, issue an error. */
13688 if (namespace_decl == error_mark_node
13689 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13691 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13692 error_at (token->location, "%qD is not a namespace-name", identifier);
13693 cp_parser_error (parser, "expected namespace-name");
13694 namespace_decl = error_mark_node;
13697 return namespace_decl;
13700 /* Parse a namespace-definition.
13702 namespace-definition:
13703 named-namespace-definition
13704 unnamed-namespace-definition
13706 named-namespace-definition:
13707 original-namespace-definition
13708 extension-namespace-definition
13710 original-namespace-definition:
13711 namespace identifier { namespace-body }
13713 extension-namespace-definition:
13714 namespace original-namespace-name { namespace-body }
13716 unnamed-namespace-definition:
13717 namespace { namespace-body } */
13720 cp_parser_namespace_definition (cp_parser* parser)
13722 tree identifier, attribs;
13723 bool has_visibility;
13726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13728 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13730 cp_lexer_consume_token (parser->lexer);
13735 /* Look for the `namespace' keyword. */
13736 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13738 /* Get the name of the namespace. We do not attempt to distinguish
13739 between an original-namespace-definition and an
13740 extension-namespace-definition at this point. The semantic
13741 analysis routines are responsible for that. */
13742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13743 identifier = cp_parser_identifier (parser);
13745 identifier = NULL_TREE;
13747 /* Parse any specified attributes. */
13748 attribs = cp_parser_attributes_opt (parser);
13750 /* Look for the `{' to start the namespace. */
13751 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13752 /* Start the namespace. */
13753 push_namespace (identifier);
13755 /* "inline namespace" is equivalent to a stub namespace definition
13756 followed by a strong using directive. */
13759 tree name_space = current_namespace;
13760 /* Set up namespace association. */
13761 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13762 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13763 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13764 /* Import the contents of the inline namespace. */
13766 do_using_directive (name_space);
13767 push_namespace (identifier);
13770 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13772 /* Parse the body of the namespace. */
13773 cp_parser_namespace_body (parser);
13775 #ifdef HANDLE_PRAGMA_VISIBILITY
13776 if (has_visibility)
13777 pop_visibility (1);
13780 /* Finish the namespace. */
13782 /* Look for the final `}'. */
13783 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13786 /* Parse a namespace-body.
13789 declaration-seq [opt] */
13792 cp_parser_namespace_body (cp_parser* parser)
13794 cp_parser_declaration_seq_opt (parser);
13797 /* Parse a namespace-alias-definition.
13799 namespace-alias-definition:
13800 namespace identifier = qualified-namespace-specifier ; */
13803 cp_parser_namespace_alias_definition (cp_parser* parser)
13806 tree namespace_specifier;
13808 cp_token *token = cp_lexer_peek_token (parser->lexer);
13810 /* Look for the `namespace' keyword. */
13811 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13812 /* Look for the identifier. */
13813 identifier = cp_parser_identifier (parser);
13814 if (identifier == error_mark_node)
13816 /* Look for the `=' token. */
13817 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13818 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13820 error_at (token->location, "%<namespace%> definition is not allowed here");
13821 /* Skip the definition. */
13822 cp_lexer_consume_token (parser->lexer);
13823 if (cp_parser_skip_to_closing_brace (parser))
13824 cp_lexer_consume_token (parser->lexer);
13827 cp_parser_require (parser, CPP_EQ, RT_EQ);
13828 /* Look for the qualified-namespace-specifier. */
13829 namespace_specifier
13830 = cp_parser_qualified_namespace_specifier (parser);
13831 /* Look for the `;' token. */
13832 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13834 /* Register the alias in the symbol table. */
13835 do_namespace_alias (identifier, namespace_specifier);
13838 /* Parse a qualified-namespace-specifier.
13840 qualified-namespace-specifier:
13841 :: [opt] nested-name-specifier [opt] namespace-name
13843 Returns a NAMESPACE_DECL corresponding to the specified
13847 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13849 /* Look for the optional `::'. */
13850 cp_parser_global_scope_opt (parser,
13851 /*current_scope_valid_p=*/false);
13853 /* Look for the optional nested-name-specifier. */
13854 cp_parser_nested_name_specifier_opt (parser,
13855 /*typename_keyword_p=*/false,
13856 /*check_dependency_p=*/true,
13858 /*is_declaration=*/true);
13860 return cp_parser_namespace_name (parser);
13863 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13864 access declaration.
13867 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13868 using :: unqualified-id ;
13870 access-declaration:
13876 cp_parser_using_declaration (cp_parser* parser,
13877 bool access_declaration_p)
13880 bool typename_p = false;
13881 bool global_scope_p;
13886 if (access_declaration_p)
13887 cp_parser_parse_tentatively (parser);
13890 /* Look for the `using' keyword. */
13891 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13893 /* Peek at the next token. */
13894 token = cp_lexer_peek_token (parser->lexer);
13895 /* See if it's `typename'. */
13896 if (token->keyword == RID_TYPENAME)
13898 /* Remember that we've seen it. */
13900 /* Consume the `typename' token. */
13901 cp_lexer_consume_token (parser->lexer);
13905 /* Look for the optional global scope qualification. */
13907 = (cp_parser_global_scope_opt (parser,
13908 /*current_scope_valid_p=*/false)
13911 /* If we saw `typename', or didn't see `::', then there must be a
13912 nested-name-specifier present. */
13913 if (typename_p || !global_scope_p)
13914 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13915 /*check_dependency_p=*/true,
13917 /*is_declaration=*/true);
13918 /* Otherwise, we could be in either of the two productions. In that
13919 case, treat the nested-name-specifier as optional. */
13921 qscope = cp_parser_nested_name_specifier_opt (parser,
13922 /*typename_keyword_p=*/false,
13923 /*check_dependency_p=*/true,
13925 /*is_declaration=*/true);
13927 qscope = global_namespace;
13929 if (access_declaration_p && cp_parser_error_occurred (parser))
13930 /* Something has already gone wrong; there's no need to parse
13931 further. Since an error has occurred, the return value of
13932 cp_parser_parse_definitely will be false, as required. */
13933 return cp_parser_parse_definitely (parser);
13935 token = cp_lexer_peek_token (parser->lexer);
13936 /* Parse the unqualified-id. */
13937 identifier = cp_parser_unqualified_id (parser,
13938 /*template_keyword_p=*/false,
13939 /*check_dependency_p=*/true,
13940 /*declarator_p=*/true,
13941 /*optional_p=*/false);
13943 if (access_declaration_p)
13945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13946 cp_parser_simulate_error (parser);
13947 if (!cp_parser_parse_definitely (parser))
13951 /* The function we call to handle a using-declaration is different
13952 depending on what scope we are in. */
13953 if (qscope == error_mark_node || identifier == error_mark_node)
13955 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13956 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13957 /* [namespace.udecl]
13959 A using declaration shall not name a template-id. */
13960 error_at (token->location,
13961 "a template-id may not appear in a using-declaration");
13964 if (at_class_scope_p ())
13966 /* Create the USING_DECL. */
13967 decl = do_class_using_decl (parser->scope, identifier);
13969 if (check_for_bare_parameter_packs (decl))
13972 /* Add it to the list of members in this class. */
13973 finish_member_declaration (decl);
13977 decl = cp_parser_lookup_name_simple (parser,
13980 if (decl == error_mark_node)
13981 cp_parser_name_lookup_error (parser, identifier,
13984 else if (check_for_bare_parameter_packs (decl))
13986 else if (!at_namespace_scope_p ())
13987 do_local_using_decl (decl, qscope, identifier);
13989 do_toplevel_using_decl (decl, qscope, identifier);
13993 /* Look for the final `;'. */
13994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13999 /* Parse a using-directive.
14002 using namespace :: [opt] nested-name-specifier [opt]
14003 namespace-name ; */
14006 cp_parser_using_directive (cp_parser* parser)
14008 tree namespace_decl;
14011 /* Look for the `using' keyword. */
14012 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14013 /* And the `namespace' keyword. */
14014 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14015 /* Look for the optional `::' operator. */
14016 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14017 /* And the optional nested-name-specifier. */
14018 cp_parser_nested_name_specifier_opt (parser,
14019 /*typename_keyword_p=*/false,
14020 /*check_dependency_p=*/true,
14022 /*is_declaration=*/true);
14023 /* Get the namespace being used. */
14024 namespace_decl = cp_parser_namespace_name (parser);
14025 /* And any specified attributes. */
14026 attribs = cp_parser_attributes_opt (parser);
14027 /* Update the symbol table. */
14028 parse_using_directive (namespace_decl, attribs);
14029 /* Look for the final `;'. */
14030 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14033 /* Parse an asm-definition.
14036 asm ( string-literal ) ;
14041 asm volatile [opt] ( string-literal ) ;
14042 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14043 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14044 : asm-operand-list [opt] ) ;
14045 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14046 : asm-operand-list [opt]
14047 : asm-clobber-list [opt] ) ;
14048 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14049 : asm-clobber-list [opt]
14050 : asm-goto-list ) ; */
14053 cp_parser_asm_definition (cp_parser* parser)
14056 tree outputs = NULL_TREE;
14057 tree inputs = NULL_TREE;
14058 tree clobbers = NULL_TREE;
14059 tree labels = NULL_TREE;
14061 bool volatile_p = false;
14062 bool extended_p = false;
14063 bool invalid_inputs_p = false;
14064 bool invalid_outputs_p = false;
14065 bool goto_p = false;
14066 required_token missing = RT_NONE;
14068 /* Look for the `asm' keyword. */
14069 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14070 /* See if the next token is `volatile'. */
14071 if (cp_parser_allow_gnu_extensions_p (parser)
14072 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14074 /* Remember that we saw the `volatile' keyword. */
14076 /* Consume the token. */
14077 cp_lexer_consume_token (parser->lexer);
14079 if (cp_parser_allow_gnu_extensions_p (parser)
14080 && parser->in_function_body
14081 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14083 /* Remember that we saw the `goto' keyword. */
14085 /* Consume the token. */
14086 cp_lexer_consume_token (parser->lexer);
14088 /* Look for the opening `('. */
14089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14091 /* Look for the string. */
14092 string = cp_parser_string_literal (parser, false, false);
14093 if (string == error_mark_node)
14095 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14096 /*consume_paren=*/true);
14100 /* If we're allowing GNU extensions, check for the extended assembly
14101 syntax. Unfortunately, the `:' tokens need not be separated by
14102 a space in C, and so, for compatibility, we tolerate that here
14103 too. Doing that means that we have to treat the `::' operator as
14105 if (cp_parser_allow_gnu_extensions_p (parser)
14106 && parser->in_function_body
14107 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14108 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14110 bool inputs_p = false;
14111 bool clobbers_p = false;
14112 bool labels_p = false;
14114 /* The extended syntax was used. */
14117 /* Look for outputs. */
14118 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14120 /* Consume the `:'. */
14121 cp_lexer_consume_token (parser->lexer);
14122 /* Parse the output-operands. */
14123 if (cp_lexer_next_token_is_not (parser->lexer,
14125 && cp_lexer_next_token_is_not (parser->lexer,
14127 && cp_lexer_next_token_is_not (parser->lexer,
14130 outputs = cp_parser_asm_operand_list (parser);
14132 if (outputs == error_mark_node)
14133 invalid_outputs_p = true;
14135 /* If the next token is `::', there are no outputs, and the
14136 next token is the beginning of the inputs. */
14137 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14138 /* The inputs are coming next. */
14141 /* Look for inputs. */
14143 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14145 /* Consume the `:' or `::'. */
14146 cp_lexer_consume_token (parser->lexer);
14147 /* Parse the output-operands. */
14148 if (cp_lexer_next_token_is_not (parser->lexer,
14150 && cp_lexer_next_token_is_not (parser->lexer,
14152 && cp_lexer_next_token_is_not (parser->lexer,
14154 inputs = cp_parser_asm_operand_list (parser);
14156 if (inputs == error_mark_node)
14157 invalid_inputs_p = true;
14159 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14160 /* The clobbers are coming next. */
14163 /* Look for clobbers. */
14165 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14168 /* Consume the `:' or `::'. */
14169 cp_lexer_consume_token (parser->lexer);
14170 /* Parse the clobbers. */
14171 if (cp_lexer_next_token_is_not (parser->lexer,
14173 && cp_lexer_next_token_is_not (parser->lexer,
14175 clobbers = cp_parser_asm_clobber_list (parser);
14178 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14179 /* The labels are coming next. */
14182 /* Look for labels. */
14184 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14187 /* Consume the `:' or `::'. */
14188 cp_lexer_consume_token (parser->lexer);
14189 /* Parse the labels. */
14190 labels = cp_parser_asm_label_list (parser);
14193 if (goto_p && !labels_p)
14194 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14197 missing = RT_COLON_SCOPE;
14199 /* Look for the closing `)'. */
14200 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14201 missing ? missing : RT_CLOSE_PAREN))
14202 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14203 /*consume_paren=*/true);
14204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14206 if (!invalid_inputs_p && !invalid_outputs_p)
14208 /* Create the ASM_EXPR. */
14209 if (parser->in_function_body)
14211 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14212 inputs, clobbers, labels);
14213 /* If the extended syntax was not used, mark the ASM_EXPR. */
14216 tree temp = asm_stmt;
14217 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14218 temp = TREE_OPERAND (temp, 0);
14220 ASM_INPUT_P (temp) = 1;
14224 cgraph_add_asm_node (string);
14228 /* Declarators [gram.dcl.decl] */
14230 /* Parse an init-declarator.
14233 declarator initializer [opt]
14238 declarator asm-specification [opt] attributes [opt] initializer [opt]
14240 function-definition:
14241 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14243 decl-specifier-seq [opt] declarator function-try-block
14247 function-definition:
14248 __extension__ function-definition
14250 The DECL_SPECIFIERS apply to this declarator. Returns a
14251 representation of the entity declared. If MEMBER_P is TRUE, then
14252 this declarator appears in a class scope. The new DECL created by
14253 this declarator is returned.
14255 The CHECKS are access checks that should be performed once we know
14256 what entity is being declared (and, therefore, what classes have
14259 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14260 for a function-definition here as well. If the declarator is a
14261 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14262 be TRUE upon return. By that point, the function-definition will
14263 have been completely parsed.
14265 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14269 cp_parser_init_declarator (cp_parser* parser,
14270 cp_decl_specifier_seq *decl_specifiers,
14271 VEC (deferred_access_check,gc)* checks,
14272 bool function_definition_allowed_p,
14274 int declares_class_or_enum,
14275 bool* function_definition_p)
14277 cp_token *token = NULL, *asm_spec_start_token = NULL,
14278 *attributes_start_token = NULL;
14279 cp_declarator *declarator;
14280 tree prefix_attributes;
14282 tree asm_specification;
14284 tree decl = NULL_TREE;
14286 int is_initialized;
14287 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14288 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14290 enum cpp_ttype initialization_kind;
14291 bool is_direct_init = false;
14292 bool is_non_constant_init;
14293 int ctor_dtor_or_conv_p;
14295 tree pushed_scope = NULL;
14297 /* Gather the attributes that were provided with the
14298 decl-specifiers. */
14299 prefix_attributes = decl_specifiers->attributes;
14301 /* Assume that this is not the declarator for a function
14303 if (function_definition_p)
14304 *function_definition_p = false;
14306 /* Defer access checks while parsing the declarator; we cannot know
14307 what names are accessible until we know what is being
14309 resume_deferring_access_checks ();
14311 /* Parse the declarator. */
14312 token = cp_lexer_peek_token (parser->lexer);
14314 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14315 &ctor_dtor_or_conv_p,
14316 /*parenthesized_p=*/NULL,
14317 /*member_p=*/false);
14318 /* Gather up the deferred checks. */
14319 stop_deferring_access_checks ();
14321 /* If the DECLARATOR was erroneous, there's no need to go
14323 if (declarator == cp_error_declarator)
14324 return error_mark_node;
14326 /* Check that the number of template-parameter-lists is OK. */
14327 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14329 return error_mark_node;
14331 if (declares_class_or_enum & 2)
14332 cp_parser_check_for_definition_in_return_type (declarator,
14333 decl_specifiers->type,
14334 decl_specifiers->type_location);
14336 /* Figure out what scope the entity declared by the DECLARATOR is
14337 located in. `grokdeclarator' sometimes changes the scope, so
14338 we compute it now. */
14339 scope = get_scope_of_declarator (declarator);
14341 /* Perform any lookups in the declared type which were thought to be
14342 dependent, but are not in the scope of the declarator. */
14343 decl_specifiers->type
14344 = maybe_update_decl_type (decl_specifiers->type, scope);
14346 /* If we're allowing GNU extensions, look for an asm-specification
14348 if (cp_parser_allow_gnu_extensions_p (parser))
14350 /* Look for an asm-specification. */
14351 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14352 asm_specification = cp_parser_asm_specification_opt (parser);
14353 /* And attributes. */
14354 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14355 attributes = cp_parser_attributes_opt (parser);
14359 asm_specification = NULL_TREE;
14360 attributes = NULL_TREE;
14363 /* Peek at the next token. */
14364 token = cp_lexer_peek_token (parser->lexer);
14365 /* Check to see if the token indicates the start of a
14366 function-definition. */
14367 if (function_declarator_p (declarator)
14368 && cp_parser_token_starts_function_definition_p (token))
14370 if (!function_definition_allowed_p)
14372 /* If a function-definition should not appear here, issue an
14374 cp_parser_error (parser,
14375 "a function-definition is not allowed here");
14376 return error_mark_node;
14380 location_t func_brace_location
14381 = cp_lexer_peek_token (parser->lexer)->location;
14383 /* Neither attributes nor an asm-specification are allowed
14384 on a function-definition. */
14385 if (asm_specification)
14386 error_at (asm_spec_start_token->location,
14387 "an asm-specification is not allowed "
14388 "on a function-definition");
14390 error_at (attributes_start_token->location,
14391 "attributes are not allowed on a function-definition");
14392 /* This is a function-definition. */
14393 *function_definition_p = true;
14395 /* Parse the function definition. */
14397 decl = cp_parser_save_member_function_body (parser,
14400 prefix_attributes);
14403 = (cp_parser_function_definition_from_specifiers_and_declarator
14404 (parser, decl_specifiers, prefix_attributes, declarator));
14406 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14408 /* This is where the prologue starts... */
14409 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14410 = func_brace_location;
14419 Only in function declarations for constructors, destructors, and
14420 type conversions can the decl-specifier-seq be omitted.
14422 We explicitly postpone this check past the point where we handle
14423 function-definitions because we tolerate function-definitions
14424 that are missing their return types in some modes. */
14425 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14427 cp_parser_error (parser,
14428 "expected constructor, destructor, or type conversion");
14429 return error_mark_node;
14432 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14433 if (token->type == CPP_EQ
14434 || token->type == CPP_OPEN_PAREN
14435 || token->type == CPP_OPEN_BRACE)
14437 is_initialized = SD_INITIALIZED;
14438 initialization_kind = token->type;
14440 if (token->type == CPP_EQ
14441 && function_declarator_p (declarator))
14443 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14444 if (t2->keyword == RID_DEFAULT)
14445 is_initialized = SD_DEFAULTED;
14446 else if (t2->keyword == RID_DELETE)
14447 is_initialized = SD_DELETED;
14452 /* If the init-declarator isn't initialized and isn't followed by a
14453 `,' or `;', it's not a valid init-declarator. */
14454 if (token->type != CPP_COMMA
14455 && token->type != CPP_SEMICOLON)
14457 cp_parser_error (parser, "expected initializer");
14458 return error_mark_node;
14460 is_initialized = SD_UNINITIALIZED;
14461 initialization_kind = CPP_EOF;
14464 /* Because start_decl has side-effects, we should only call it if we
14465 know we're going ahead. By this point, we know that we cannot
14466 possibly be looking at any other construct. */
14467 cp_parser_commit_to_tentative_parse (parser);
14469 /* If the decl specifiers were bad, issue an error now that we're
14470 sure this was intended to be a declarator. Then continue
14471 declaring the variable(s), as int, to try to cut down on further
14473 if (decl_specifiers->any_specifiers_p
14474 && decl_specifiers->type == error_mark_node)
14476 cp_parser_error (parser, "invalid type in declaration");
14477 decl_specifiers->type = integer_type_node;
14480 /* Check to see whether or not this declaration is a friend. */
14481 friend_p = cp_parser_friend_p (decl_specifiers);
14483 /* Enter the newly declared entry in the symbol table. If we're
14484 processing a declaration in a class-specifier, we wait until
14485 after processing the initializer. */
14488 if (parser->in_unbraced_linkage_specification_p)
14489 decl_specifiers->storage_class = sc_extern;
14490 decl = start_decl (declarator, decl_specifiers,
14491 is_initialized, attributes, prefix_attributes,
14493 /* Adjust location of decl if declarator->id_loc is more appropriate:
14494 set, and decl wasn't merged with another decl, in which case its
14495 location would be different from input_location, and more accurate. */
14497 && declarator->id_loc != UNKNOWN_LOCATION
14498 && DECL_SOURCE_LOCATION (decl) == input_location)
14499 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14502 /* Enter the SCOPE. That way unqualified names appearing in the
14503 initializer will be looked up in SCOPE. */
14504 pushed_scope = push_scope (scope);
14506 /* Perform deferred access control checks, now that we know in which
14507 SCOPE the declared entity resides. */
14508 if (!member_p && decl)
14510 tree saved_current_function_decl = NULL_TREE;
14512 /* If the entity being declared is a function, pretend that we
14513 are in its scope. If it is a `friend', it may have access to
14514 things that would not otherwise be accessible. */
14515 if (TREE_CODE (decl) == FUNCTION_DECL)
14517 saved_current_function_decl = current_function_decl;
14518 current_function_decl = decl;
14521 /* Perform access checks for template parameters. */
14522 cp_parser_perform_template_parameter_access_checks (checks);
14524 /* Perform the access control checks for the declarator and the
14525 decl-specifiers. */
14526 perform_deferred_access_checks ();
14528 /* Restore the saved value. */
14529 if (TREE_CODE (decl) == FUNCTION_DECL)
14530 current_function_decl = saved_current_function_decl;
14533 /* Parse the initializer. */
14534 initializer = NULL_TREE;
14535 is_direct_init = false;
14536 is_non_constant_init = true;
14537 if (is_initialized)
14539 if (function_declarator_p (declarator))
14541 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14542 if (initialization_kind == CPP_EQ)
14543 initializer = cp_parser_pure_specifier (parser);
14546 /* If the declaration was erroneous, we don't really
14547 know what the user intended, so just silently
14548 consume the initializer. */
14549 if (decl != error_mark_node)
14550 error_at (initializer_start_token->location,
14551 "initializer provided for function");
14552 cp_parser_skip_to_closing_parenthesis (parser,
14553 /*recovering=*/true,
14554 /*or_comma=*/false,
14555 /*consume_paren=*/true);
14560 /* We want to record the extra mangling scope for in-class
14561 initializers of class members and initializers of static data
14562 member templates. The former is a C++0x feature which isn't
14563 implemented yet, and I expect it will involve deferring
14564 parsing of the initializer until end of class as with default
14565 arguments. So right here we only handle the latter. */
14566 if (!member_p && processing_template_decl)
14567 start_lambda_scope (decl);
14568 initializer = cp_parser_initializer (parser,
14570 &is_non_constant_init);
14571 if (!member_p && processing_template_decl)
14572 finish_lambda_scope ();
14576 /* The old parser allows attributes to appear after a parenthesized
14577 initializer. Mark Mitchell proposed removing this functionality
14578 on the GCC mailing lists on 2002-08-13. This parser accepts the
14579 attributes -- but ignores them. */
14580 if (cp_parser_allow_gnu_extensions_p (parser)
14581 && initialization_kind == CPP_OPEN_PAREN)
14582 if (cp_parser_attributes_opt (parser))
14583 warning (OPT_Wattributes,
14584 "attributes after parenthesized initializer ignored");
14586 /* For an in-class declaration, use `grokfield' to create the
14592 pop_scope (pushed_scope);
14593 pushed_scope = false;
14595 decl = grokfield (declarator, decl_specifiers,
14596 initializer, !is_non_constant_init,
14597 /*asmspec=*/NULL_TREE,
14598 prefix_attributes);
14599 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14600 cp_parser_save_default_args (parser, decl);
14603 /* Finish processing the declaration. But, skip friend
14605 if (!friend_p && decl && decl != error_mark_node)
14607 cp_finish_decl (decl,
14608 initializer, !is_non_constant_init,
14610 /* If the initializer is in parentheses, then this is
14611 a direct-initialization, which means that an
14612 `explicit' constructor is OK. Otherwise, an
14613 `explicit' constructor cannot be used. */
14614 ((is_direct_init || !is_initialized)
14615 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14617 else if ((cxx_dialect != cxx98) && friend_p
14618 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14619 /* Core issue #226 (C++0x only): A default template-argument
14620 shall not be specified in a friend class template
14622 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14623 /*is_partial=*/0, /*is_friend_decl=*/1);
14625 if (!friend_p && pushed_scope)
14626 pop_scope (pushed_scope);
14631 /* Parse a declarator.
14635 ptr-operator declarator
14637 abstract-declarator:
14638 ptr-operator abstract-declarator [opt]
14639 direct-abstract-declarator
14644 attributes [opt] direct-declarator
14645 attributes [opt] ptr-operator declarator
14647 abstract-declarator:
14648 attributes [opt] ptr-operator abstract-declarator [opt]
14649 attributes [opt] direct-abstract-declarator
14651 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14652 detect constructor, destructor or conversion operators. It is set
14653 to -1 if the declarator is a name, and +1 if it is a
14654 function. Otherwise it is set to zero. Usually you just want to
14655 test for >0, but internally the negative value is used.
14657 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14658 a decl-specifier-seq unless it declares a constructor, destructor,
14659 or conversion. It might seem that we could check this condition in
14660 semantic analysis, rather than parsing, but that makes it difficult
14661 to handle something like `f()'. We want to notice that there are
14662 no decl-specifiers, and therefore realize that this is an
14663 expression, not a declaration.)
14665 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14666 the declarator is a direct-declarator of the form "(...)".
14668 MEMBER_P is true iff this declarator is a member-declarator. */
14670 static cp_declarator *
14671 cp_parser_declarator (cp_parser* parser,
14672 cp_parser_declarator_kind dcl_kind,
14673 int* ctor_dtor_or_conv_p,
14674 bool* parenthesized_p,
14677 cp_declarator *declarator;
14678 enum tree_code code;
14679 cp_cv_quals cv_quals;
14681 tree attributes = NULL_TREE;
14683 /* Assume this is not a constructor, destructor, or type-conversion
14685 if (ctor_dtor_or_conv_p)
14686 *ctor_dtor_or_conv_p = 0;
14688 if (cp_parser_allow_gnu_extensions_p (parser))
14689 attributes = cp_parser_attributes_opt (parser);
14691 /* Check for the ptr-operator production. */
14692 cp_parser_parse_tentatively (parser);
14693 /* Parse the ptr-operator. */
14694 code = cp_parser_ptr_operator (parser,
14697 /* If that worked, then we have a ptr-operator. */
14698 if (cp_parser_parse_definitely (parser))
14700 /* If a ptr-operator was found, then this declarator was not
14702 if (parenthesized_p)
14703 *parenthesized_p = true;
14704 /* The dependent declarator is optional if we are parsing an
14705 abstract-declarator. */
14706 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14707 cp_parser_parse_tentatively (parser);
14709 /* Parse the dependent declarator. */
14710 declarator = cp_parser_declarator (parser, dcl_kind,
14711 /*ctor_dtor_or_conv_p=*/NULL,
14712 /*parenthesized_p=*/NULL,
14713 /*member_p=*/false);
14715 /* If we are parsing an abstract-declarator, we must handle the
14716 case where the dependent declarator is absent. */
14717 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14718 && !cp_parser_parse_definitely (parser))
14721 declarator = cp_parser_make_indirect_declarator
14722 (code, class_type, cv_quals, declarator);
14724 /* Everything else is a direct-declarator. */
14727 if (parenthesized_p)
14728 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14730 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14731 ctor_dtor_or_conv_p,
14735 if (attributes && declarator && declarator != cp_error_declarator)
14736 declarator->attributes = attributes;
14741 /* Parse a direct-declarator or direct-abstract-declarator.
14745 direct-declarator ( parameter-declaration-clause )
14746 cv-qualifier-seq [opt]
14747 exception-specification [opt]
14748 direct-declarator [ constant-expression [opt] ]
14751 direct-abstract-declarator:
14752 direct-abstract-declarator [opt]
14753 ( parameter-declaration-clause )
14754 cv-qualifier-seq [opt]
14755 exception-specification [opt]
14756 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14757 ( abstract-declarator )
14759 Returns a representation of the declarator. DCL_KIND is
14760 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14761 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14762 we are parsing a direct-declarator. It is
14763 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14764 of ambiguity we prefer an abstract declarator, as per
14765 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14766 cp_parser_declarator. */
14768 static cp_declarator *
14769 cp_parser_direct_declarator (cp_parser* parser,
14770 cp_parser_declarator_kind dcl_kind,
14771 int* ctor_dtor_or_conv_p,
14775 cp_declarator *declarator = NULL;
14776 tree scope = NULL_TREE;
14777 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14778 bool saved_in_declarator_p = parser->in_declarator_p;
14780 tree pushed_scope = NULL_TREE;
14784 /* Peek at the next token. */
14785 token = cp_lexer_peek_token (parser->lexer);
14786 if (token->type == CPP_OPEN_PAREN)
14788 /* This is either a parameter-declaration-clause, or a
14789 parenthesized declarator. When we know we are parsing a
14790 named declarator, it must be a parenthesized declarator
14791 if FIRST is true. For instance, `(int)' is a
14792 parameter-declaration-clause, with an omitted
14793 direct-abstract-declarator. But `((*))', is a
14794 parenthesized abstract declarator. Finally, when T is a
14795 template parameter `(T)' is a
14796 parameter-declaration-clause, and not a parenthesized
14799 We first try and parse a parameter-declaration-clause,
14800 and then try a nested declarator (if FIRST is true).
14802 It is not an error for it not to be a
14803 parameter-declaration-clause, even when FIRST is
14809 The first is the declaration of a function while the
14810 second is the definition of a variable, including its
14813 Having seen only the parenthesis, we cannot know which of
14814 these two alternatives should be selected. Even more
14815 complex are examples like:
14820 The former is a function-declaration; the latter is a
14821 variable initialization.
14823 Thus again, we try a parameter-declaration-clause, and if
14824 that fails, we back out and return. */
14826 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14829 unsigned saved_num_template_parameter_lists;
14830 bool is_declarator = false;
14833 /* In a member-declarator, the only valid interpretation
14834 of a parenthesis is the start of a
14835 parameter-declaration-clause. (It is invalid to
14836 initialize a static data member with a parenthesized
14837 initializer; only the "=" form of initialization is
14840 cp_parser_parse_tentatively (parser);
14842 /* Consume the `('. */
14843 cp_lexer_consume_token (parser->lexer);
14846 /* If this is going to be an abstract declarator, we're
14847 in a declarator and we can't have default args. */
14848 parser->default_arg_ok_p = false;
14849 parser->in_declarator_p = true;
14852 /* Inside the function parameter list, surrounding
14853 template-parameter-lists do not apply. */
14854 saved_num_template_parameter_lists
14855 = parser->num_template_parameter_lists;
14856 parser->num_template_parameter_lists = 0;
14858 begin_scope (sk_function_parms, NULL_TREE);
14860 /* Parse the parameter-declaration-clause. */
14861 params = cp_parser_parameter_declaration_clause (parser);
14863 parser->num_template_parameter_lists
14864 = saved_num_template_parameter_lists;
14866 /* If all went well, parse the cv-qualifier-seq and the
14867 exception-specification. */
14868 if (member_p || cp_parser_parse_definitely (parser))
14870 cp_cv_quals cv_quals;
14871 tree exception_specification;
14874 is_declarator = true;
14876 if (ctor_dtor_or_conv_p)
14877 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14879 /* Consume the `)'. */
14880 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14882 /* Parse the cv-qualifier-seq. */
14883 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14884 /* And the exception-specification. */
14885 exception_specification
14886 = cp_parser_exception_specification_opt (parser);
14889 = cp_parser_late_return_type_opt (parser);
14891 /* Create the function-declarator. */
14892 declarator = make_call_declarator (declarator,
14895 exception_specification,
14897 /* Any subsequent parameter lists are to do with
14898 return type, so are not those of the declared
14900 parser->default_arg_ok_p = false;
14903 /* Remove the function parms from scope. */
14904 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14905 pop_binding (DECL_NAME (t), t);
14909 /* Repeat the main loop. */
14913 /* If this is the first, we can try a parenthesized
14917 bool saved_in_type_id_in_expr_p;
14919 parser->default_arg_ok_p = saved_default_arg_ok_p;
14920 parser->in_declarator_p = saved_in_declarator_p;
14922 /* Consume the `('. */
14923 cp_lexer_consume_token (parser->lexer);
14924 /* Parse the nested declarator. */
14925 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14926 parser->in_type_id_in_expr_p = true;
14928 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14929 /*parenthesized_p=*/NULL,
14931 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14933 /* Expect a `)'. */
14934 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14935 declarator = cp_error_declarator;
14936 if (declarator == cp_error_declarator)
14939 goto handle_declarator;
14941 /* Otherwise, we must be done. */
14945 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14946 && token->type == CPP_OPEN_SQUARE)
14948 /* Parse an array-declarator. */
14951 if (ctor_dtor_or_conv_p)
14952 *ctor_dtor_or_conv_p = 0;
14955 parser->default_arg_ok_p = false;
14956 parser->in_declarator_p = true;
14957 /* Consume the `['. */
14958 cp_lexer_consume_token (parser->lexer);
14959 /* Peek at the next token. */
14960 token = cp_lexer_peek_token (parser->lexer);
14961 /* If the next token is `]', then there is no
14962 constant-expression. */
14963 if (token->type != CPP_CLOSE_SQUARE)
14965 bool non_constant_p;
14968 = cp_parser_constant_expression (parser,
14969 /*allow_non_constant=*/true,
14971 if (!non_constant_p)
14972 bounds = fold_non_dependent_expr (bounds);
14973 /* Normally, the array bound must be an integral constant
14974 expression. However, as an extension, we allow VLAs
14975 in function scopes as long as they aren't part of a
14976 parameter declaration. */
14977 else if (!parser->in_function_body
14978 || current_binding_level->kind == sk_function_parms)
14980 cp_parser_error (parser,
14981 "array bound is not an integer constant");
14982 bounds = error_mark_node;
14984 else if (processing_template_decl && !error_operand_p (bounds))
14986 /* Remember this wasn't a constant-expression. */
14987 bounds = build_nop (TREE_TYPE (bounds), bounds);
14988 TREE_SIDE_EFFECTS (bounds) = 1;
14992 bounds = NULL_TREE;
14993 /* Look for the closing `]'. */
14994 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14996 declarator = cp_error_declarator;
15000 declarator = make_array_declarator (declarator, bounds);
15002 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15005 tree qualifying_scope;
15006 tree unqualified_name;
15007 special_function_kind sfk;
15009 bool pack_expansion_p = false;
15010 cp_token *declarator_id_start_token;
15012 /* Parse a declarator-id */
15013 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15016 cp_parser_parse_tentatively (parser);
15018 /* If we see an ellipsis, we should be looking at a
15020 if (token->type == CPP_ELLIPSIS)
15022 /* Consume the `...' */
15023 cp_lexer_consume_token (parser->lexer);
15025 pack_expansion_p = true;
15029 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15031 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15032 qualifying_scope = parser->scope;
15037 if (!unqualified_name && pack_expansion_p)
15039 /* Check whether an error occurred. */
15040 okay = !cp_parser_error_occurred (parser);
15042 /* We already consumed the ellipsis to mark a
15043 parameter pack, but we have no way to report it,
15044 so abort the tentative parse. We will be exiting
15045 immediately anyway. */
15046 cp_parser_abort_tentative_parse (parser);
15049 okay = cp_parser_parse_definitely (parser);
15052 unqualified_name = error_mark_node;
15053 else if (unqualified_name
15054 && (qualifying_scope
15055 || (TREE_CODE (unqualified_name)
15056 != IDENTIFIER_NODE)))
15058 cp_parser_error (parser, "expected unqualified-id");
15059 unqualified_name = error_mark_node;
15063 if (!unqualified_name)
15065 if (unqualified_name == error_mark_node)
15067 declarator = cp_error_declarator;
15068 pack_expansion_p = false;
15069 declarator->parameter_pack_p = false;
15073 if (qualifying_scope && at_namespace_scope_p ()
15074 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15076 /* In the declaration of a member of a template class
15077 outside of the class itself, the SCOPE will sometimes
15078 be a TYPENAME_TYPE. For example, given:
15080 template <typename T>
15081 int S<T>::R::i = 3;
15083 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15084 this context, we must resolve S<T>::R to an ordinary
15085 type, rather than a typename type.
15087 The reason we normally avoid resolving TYPENAME_TYPEs
15088 is that a specialization of `S' might render
15089 `S<T>::R' not a type. However, if `S' is
15090 specialized, then this `i' will not be used, so there
15091 is no harm in resolving the types here. */
15094 /* Resolve the TYPENAME_TYPE. */
15095 type = resolve_typename_type (qualifying_scope,
15096 /*only_current_p=*/false);
15097 /* If that failed, the declarator is invalid. */
15098 if (TREE_CODE (type) == TYPENAME_TYPE)
15100 if (typedef_variant_p (type))
15101 error_at (declarator_id_start_token->location,
15102 "cannot define member of dependent typedef "
15105 error_at (declarator_id_start_token->location,
15106 "%<%T::%E%> is not a type",
15107 TYPE_CONTEXT (qualifying_scope),
15108 TYPE_IDENTIFIER (qualifying_scope));
15110 qualifying_scope = type;
15115 if (unqualified_name)
15119 if (qualifying_scope
15120 && CLASS_TYPE_P (qualifying_scope))
15121 class_type = qualifying_scope;
15123 class_type = current_class_type;
15125 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15127 tree name_type = TREE_TYPE (unqualified_name);
15128 if (class_type && same_type_p (name_type, class_type))
15130 if (qualifying_scope
15131 && CLASSTYPE_USE_TEMPLATE (name_type))
15133 error_at (declarator_id_start_token->location,
15134 "invalid use of constructor as a template");
15135 inform (declarator_id_start_token->location,
15136 "use %<%T::%D%> instead of %<%T::%D%> to "
15137 "name the constructor in a qualified name",
15139 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15140 class_type, name_type);
15141 declarator = cp_error_declarator;
15145 unqualified_name = constructor_name (class_type);
15149 /* We do not attempt to print the declarator
15150 here because we do not have enough
15151 information about its original syntactic
15153 cp_parser_error (parser, "invalid declarator");
15154 declarator = cp_error_declarator;
15161 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15162 sfk = sfk_destructor;
15163 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15164 sfk = sfk_conversion;
15165 else if (/* There's no way to declare a constructor
15166 for an anonymous type, even if the type
15167 got a name for linkage purposes. */
15168 !TYPE_WAS_ANONYMOUS (class_type)
15169 && constructor_name_p (unqualified_name,
15172 unqualified_name = constructor_name (class_type);
15173 sfk = sfk_constructor;
15175 else if (is_overloaded_fn (unqualified_name)
15176 && DECL_CONSTRUCTOR_P (get_first_fn
15177 (unqualified_name)))
15178 sfk = sfk_constructor;
15180 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15181 *ctor_dtor_or_conv_p = -1;
15184 declarator = make_id_declarator (qualifying_scope,
15187 declarator->id_loc = token->location;
15188 declarator->parameter_pack_p = pack_expansion_p;
15190 if (pack_expansion_p)
15191 maybe_warn_variadic_templates ();
15194 handle_declarator:;
15195 scope = get_scope_of_declarator (declarator);
15197 /* Any names that appear after the declarator-id for a
15198 member are looked up in the containing scope. */
15199 pushed_scope = push_scope (scope);
15200 parser->in_declarator_p = true;
15201 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15202 || (declarator && declarator->kind == cdk_id))
15203 /* Default args are only allowed on function
15205 parser->default_arg_ok_p = saved_default_arg_ok_p;
15207 parser->default_arg_ok_p = false;
15216 /* For an abstract declarator, we might wind up with nothing at this
15217 point. That's an error; the declarator is not optional. */
15219 cp_parser_error (parser, "expected declarator");
15221 /* If we entered a scope, we must exit it now. */
15223 pop_scope (pushed_scope);
15225 parser->default_arg_ok_p = saved_default_arg_ok_p;
15226 parser->in_declarator_p = saved_in_declarator_p;
15231 /* Parse a ptr-operator.
15234 * cv-qualifier-seq [opt]
15236 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15241 & cv-qualifier-seq [opt]
15243 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15244 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15245 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15246 filled in with the TYPE containing the member. *CV_QUALS is
15247 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15248 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15249 Note that the tree codes returned by this function have nothing
15250 to do with the types of trees that will be eventually be created
15251 to represent the pointer or reference type being parsed. They are
15252 just constants with suggestive names. */
15253 static enum tree_code
15254 cp_parser_ptr_operator (cp_parser* parser,
15256 cp_cv_quals *cv_quals)
15258 enum tree_code code = ERROR_MARK;
15261 /* Assume that it's not a pointer-to-member. */
15263 /* And that there are no cv-qualifiers. */
15264 *cv_quals = TYPE_UNQUALIFIED;
15266 /* Peek at the next token. */
15267 token = cp_lexer_peek_token (parser->lexer);
15269 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15270 if (token->type == CPP_MULT)
15271 code = INDIRECT_REF;
15272 else if (token->type == CPP_AND)
15274 else if ((cxx_dialect != cxx98) &&
15275 token->type == CPP_AND_AND) /* C++0x only */
15276 code = NON_LVALUE_EXPR;
15278 if (code != ERROR_MARK)
15280 /* Consume the `*', `&' or `&&'. */
15281 cp_lexer_consume_token (parser->lexer);
15283 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15284 `&', if we are allowing GNU extensions. (The only qualifier
15285 that can legally appear after `&' is `restrict', but that is
15286 enforced during semantic analysis. */
15287 if (code == INDIRECT_REF
15288 || cp_parser_allow_gnu_extensions_p (parser))
15289 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15293 /* Try the pointer-to-member case. */
15294 cp_parser_parse_tentatively (parser);
15295 /* Look for the optional `::' operator. */
15296 cp_parser_global_scope_opt (parser,
15297 /*current_scope_valid_p=*/false);
15298 /* Look for the nested-name specifier. */
15299 token = cp_lexer_peek_token (parser->lexer);
15300 cp_parser_nested_name_specifier (parser,
15301 /*typename_keyword_p=*/false,
15302 /*check_dependency_p=*/true,
15304 /*is_declaration=*/false);
15305 /* If we found it, and the next token is a `*', then we are
15306 indeed looking at a pointer-to-member operator. */
15307 if (!cp_parser_error_occurred (parser)
15308 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15310 /* Indicate that the `*' operator was used. */
15311 code = INDIRECT_REF;
15313 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15314 error_at (token->location, "%qD is a namespace", parser->scope);
15317 /* The type of which the member is a member is given by the
15319 *type = parser->scope;
15320 /* The next name will not be qualified. */
15321 parser->scope = NULL_TREE;
15322 parser->qualifying_scope = NULL_TREE;
15323 parser->object_scope = NULL_TREE;
15324 /* Look for the optional cv-qualifier-seq. */
15325 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15328 /* If that didn't work we don't have a ptr-operator. */
15329 if (!cp_parser_parse_definitely (parser))
15330 cp_parser_error (parser, "expected ptr-operator");
15336 /* Parse an (optional) cv-qualifier-seq.
15339 cv-qualifier cv-qualifier-seq [opt]
15350 Returns a bitmask representing the cv-qualifiers. */
15353 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15355 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15360 cp_cv_quals cv_qualifier;
15362 /* Peek at the next token. */
15363 token = cp_lexer_peek_token (parser->lexer);
15364 /* See if it's a cv-qualifier. */
15365 switch (token->keyword)
15368 cv_qualifier = TYPE_QUAL_CONST;
15372 cv_qualifier = TYPE_QUAL_VOLATILE;
15376 cv_qualifier = TYPE_QUAL_RESTRICT;
15380 cv_qualifier = TYPE_UNQUALIFIED;
15387 if (cv_quals & cv_qualifier)
15389 error_at (token->location, "duplicate cv-qualifier");
15390 cp_lexer_purge_token (parser->lexer);
15394 cp_lexer_consume_token (parser->lexer);
15395 cv_quals |= cv_qualifier;
15402 /* Parse a late-specified return type, if any. This is not a separate
15403 non-terminal, but part of a function declarator, which looks like
15405 -> trailing-type-specifier-seq abstract-declarator(opt)
15407 Returns the type indicated by the type-id. */
15410 cp_parser_late_return_type_opt (cp_parser* parser)
15414 /* Peek at the next token. */
15415 token = cp_lexer_peek_token (parser->lexer);
15416 /* A late-specified return type is indicated by an initial '->'. */
15417 if (token->type != CPP_DEREF)
15420 /* Consume the ->. */
15421 cp_lexer_consume_token (parser->lexer);
15423 return cp_parser_trailing_type_id (parser);
15426 /* Parse a declarator-id.
15430 :: [opt] nested-name-specifier [opt] type-name
15432 In the `id-expression' case, the value returned is as for
15433 cp_parser_id_expression if the id-expression was an unqualified-id.
15434 If the id-expression was a qualified-id, then a SCOPE_REF is
15435 returned. The first operand is the scope (either a NAMESPACE_DECL
15436 or TREE_TYPE), but the second is still just a representation of an
15440 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15443 /* The expression must be an id-expression. Assume that qualified
15444 names are the names of types so that:
15447 int S<T>::R::i = 3;
15449 will work; we must treat `S<T>::R' as the name of a type.
15450 Similarly, assume that qualified names are templates, where
15454 int S<T>::R<T>::i = 3;
15457 id = cp_parser_id_expression (parser,
15458 /*template_keyword_p=*/false,
15459 /*check_dependency_p=*/false,
15460 /*template_p=*/NULL,
15461 /*declarator_p=*/true,
15463 if (id && BASELINK_P (id))
15464 id = BASELINK_FUNCTIONS (id);
15468 /* Parse a type-id.
15471 type-specifier-seq abstract-declarator [opt]
15473 Returns the TYPE specified. */
15476 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15477 bool is_trailing_return)
15479 cp_decl_specifier_seq type_specifier_seq;
15480 cp_declarator *abstract_declarator;
15482 /* Parse the type-specifier-seq. */
15483 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15484 is_trailing_return,
15485 &type_specifier_seq);
15486 if (type_specifier_seq.type == error_mark_node)
15487 return error_mark_node;
15489 /* There might or might not be an abstract declarator. */
15490 cp_parser_parse_tentatively (parser);
15491 /* Look for the declarator. */
15492 abstract_declarator
15493 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15494 /*parenthesized_p=*/NULL,
15495 /*member_p=*/false);
15496 /* Check to see if there really was a declarator. */
15497 if (!cp_parser_parse_definitely (parser))
15498 abstract_declarator = NULL;
15500 if (type_specifier_seq.type
15501 && type_uses_auto (type_specifier_seq.type))
15503 /* A type-id with type 'auto' is only ok if the abstract declarator
15504 is a function declarator with a late-specified return type. */
15505 if (abstract_declarator
15506 && abstract_declarator->kind == cdk_function
15507 && abstract_declarator->u.function.late_return_type)
15511 error ("invalid use of %<auto%>");
15512 return error_mark_node;
15516 return groktypename (&type_specifier_seq, abstract_declarator,
15520 static tree cp_parser_type_id (cp_parser *parser)
15522 return cp_parser_type_id_1 (parser, false, false);
15525 static tree cp_parser_template_type_arg (cp_parser *parser)
15527 return cp_parser_type_id_1 (parser, true, false);
15530 static tree cp_parser_trailing_type_id (cp_parser *parser)
15532 return cp_parser_type_id_1 (parser, false, true);
15535 /* Parse a type-specifier-seq.
15537 type-specifier-seq:
15538 type-specifier type-specifier-seq [opt]
15542 type-specifier-seq:
15543 attributes type-specifier-seq [opt]
15545 If IS_DECLARATION is true, we are at the start of a "condition" or
15546 exception-declaration, so we might be followed by a declarator-id.
15548 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15549 i.e. we've just seen "->".
15551 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15554 cp_parser_type_specifier_seq (cp_parser* parser,
15555 bool is_declaration,
15556 bool is_trailing_return,
15557 cp_decl_specifier_seq *type_specifier_seq)
15559 bool seen_type_specifier = false;
15560 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15561 cp_token *start_token = NULL;
15563 /* Clear the TYPE_SPECIFIER_SEQ. */
15564 clear_decl_specs (type_specifier_seq);
15566 /* In the context of a trailing return type, enum E { } is an
15567 elaborated-type-specifier followed by a function-body, not an
15569 if (is_trailing_return)
15570 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15572 /* Parse the type-specifiers and attributes. */
15575 tree type_specifier;
15576 bool is_cv_qualifier;
15578 /* Check for attributes first. */
15579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15581 type_specifier_seq->attributes =
15582 chainon (type_specifier_seq->attributes,
15583 cp_parser_attributes_opt (parser));
15587 /* record the token of the beginning of the type specifier seq,
15588 for error reporting purposes*/
15590 start_token = cp_lexer_peek_token (parser->lexer);
15592 /* Look for the type-specifier. */
15593 type_specifier = cp_parser_type_specifier (parser,
15595 type_specifier_seq,
15596 /*is_declaration=*/false,
15599 if (!type_specifier)
15601 /* If the first type-specifier could not be found, this is not a
15602 type-specifier-seq at all. */
15603 if (!seen_type_specifier)
15605 cp_parser_error (parser, "expected type-specifier");
15606 type_specifier_seq->type = error_mark_node;
15609 /* If subsequent type-specifiers could not be found, the
15610 type-specifier-seq is complete. */
15614 seen_type_specifier = true;
15615 /* The standard says that a condition can be:
15617 type-specifier-seq declarator = assignment-expression
15624 we should treat the "S" as a declarator, not as a
15625 type-specifier. The standard doesn't say that explicitly for
15626 type-specifier-seq, but it does say that for
15627 decl-specifier-seq in an ordinary declaration. Perhaps it
15628 would be clearer just to allow a decl-specifier-seq here, and
15629 then add a semantic restriction that if any decl-specifiers
15630 that are not type-specifiers appear, the program is invalid. */
15631 if (is_declaration && !is_cv_qualifier)
15632 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15635 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15638 /* Parse a parameter-declaration-clause.
15640 parameter-declaration-clause:
15641 parameter-declaration-list [opt] ... [opt]
15642 parameter-declaration-list , ...
15644 Returns a representation for the parameter declarations. A return
15645 value of NULL indicates a parameter-declaration-clause consisting
15646 only of an ellipsis. */
15649 cp_parser_parameter_declaration_clause (cp_parser* parser)
15656 /* Peek at the next token. */
15657 token = cp_lexer_peek_token (parser->lexer);
15658 /* Check for trivial parameter-declaration-clauses. */
15659 if (token->type == CPP_ELLIPSIS)
15661 /* Consume the `...' token. */
15662 cp_lexer_consume_token (parser->lexer);
15665 else if (token->type == CPP_CLOSE_PAREN)
15666 /* There are no parameters. */
15668 #ifndef NO_IMPLICIT_EXTERN_C
15669 if (in_system_header && current_class_type == NULL
15670 && current_lang_name == lang_name_c)
15674 return void_list_node;
15676 /* Check for `(void)', too, which is a special case. */
15677 else if (token->keyword == RID_VOID
15678 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15679 == CPP_CLOSE_PAREN))
15681 /* Consume the `void' token. */
15682 cp_lexer_consume_token (parser->lexer);
15683 /* There are no parameters. */
15684 return void_list_node;
15687 /* Parse the parameter-declaration-list. */
15688 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15689 /* If a parse error occurred while parsing the
15690 parameter-declaration-list, then the entire
15691 parameter-declaration-clause is erroneous. */
15695 /* Peek at the next token. */
15696 token = cp_lexer_peek_token (parser->lexer);
15697 /* If it's a `,', the clause should terminate with an ellipsis. */
15698 if (token->type == CPP_COMMA)
15700 /* Consume the `,'. */
15701 cp_lexer_consume_token (parser->lexer);
15702 /* Expect an ellipsis. */
15704 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15706 /* It might also be `...' if the optional trailing `,' was
15708 else if (token->type == CPP_ELLIPSIS)
15710 /* Consume the `...' token. */
15711 cp_lexer_consume_token (parser->lexer);
15712 /* And remember that we saw it. */
15716 ellipsis_p = false;
15718 /* Finish the parameter list. */
15720 parameters = chainon (parameters, void_list_node);
15725 /* Parse a parameter-declaration-list.
15727 parameter-declaration-list:
15728 parameter-declaration
15729 parameter-declaration-list , parameter-declaration
15731 Returns a representation of the parameter-declaration-list, as for
15732 cp_parser_parameter_declaration_clause. However, the
15733 `void_list_node' is never appended to the list. Upon return,
15734 *IS_ERROR will be true iff an error occurred. */
15737 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15739 tree parameters = NULL_TREE;
15740 tree *tail = ¶meters;
15741 bool saved_in_unbraced_linkage_specification_p;
15744 /* Assume all will go well. */
15746 /* The special considerations that apply to a function within an
15747 unbraced linkage specifications do not apply to the parameters
15748 to the function. */
15749 saved_in_unbraced_linkage_specification_p
15750 = parser->in_unbraced_linkage_specification_p;
15751 parser->in_unbraced_linkage_specification_p = false;
15753 /* Look for more parameters. */
15756 cp_parameter_declarator *parameter;
15757 tree decl = error_mark_node;
15758 bool parenthesized_p;
15759 /* Parse the parameter. */
15761 = cp_parser_parameter_declaration (parser,
15762 /*template_parm_p=*/false,
15765 /* We don't know yet if the enclosing context is deprecated, so wait
15766 and warn in grokparms if appropriate. */
15767 deprecated_state = DEPRECATED_SUPPRESS;
15770 decl = grokdeclarator (parameter->declarator,
15771 ¶meter->decl_specifiers,
15773 parameter->default_argument != NULL_TREE,
15774 ¶meter->decl_specifiers.attributes);
15776 deprecated_state = DEPRECATED_NORMAL;
15778 /* If a parse error occurred parsing the parameter declaration,
15779 then the entire parameter-declaration-list is erroneous. */
15780 if (decl == error_mark_node)
15783 parameters = error_mark_node;
15787 if (parameter->decl_specifiers.attributes)
15788 cplus_decl_attributes (&decl,
15789 parameter->decl_specifiers.attributes,
15791 if (DECL_NAME (decl))
15792 decl = pushdecl (decl);
15794 if (decl != error_mark_node)
15796 retrofit_lang_decl (decl);
15797 DECL_PARM_INDEX (decl) = ++index;
15800 /* Add the new parameter to the list. */
15801 *tail = build_tree_list (parameter->default_argument, decl);
15802 tail = &TREE_CHAIN (*tail);
15804 /* Peek at the next token. */
15805 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15806 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15807 /* These are for Objective-C++ */
15808 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15809 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15810 /* The parameter-declaration-list is complete. */
15812 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15816 /* Peek at the next token. */
15817 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15818 /* If it's an ellipsis, then the list is complete. */
15819 if (token->type == CPP_ELLIPSIS)
15821 /* Otherwise, there must be more parameters. Consume the
15823 cp_lexer_consume_token (parser->lexer);
15824 /* When parsing something like:
15826 int i(float f, double d)
15828 we can tell after seeing the declaration for "f" that we
15829 are not looking at an initialization of a variable "i",
15830 but rather at the declaration of a function "i".
15832 Due to the fact that the parsing of template arguments
15833 (as specified to a template-id) requires backtracking we
15834 cannot use this technique when inside a template argument
15836 if (!parser->in_template_argument_list_p
15837 && !parser->in_type_id_in_expr_p
15838 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15839 /* However, a parameter-declaration of the form
15840 "foat(f)" (which is a valid declaration of a
15841 parameter "f") can also be interpreted as an
15842 expression (the conversion of "f" to "float"). */
15843 && !parenthesized_p)
15844 cp_parser_commit_to_tentative_parse (parser);
15848 cp_parser_error (parser, "expected %<,%> or %<...%>");
15849 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15850 cp_parser_skip_to_closing_parenthesis (parser,
15851 /*recovering=*/true,
15852 /*or_comma=*/false,
15853 /*consume_paren=*/false);
15858 parser->in_unbraced_linkage_specification_p
15859 = saved_in_unbraced_linkage_specification_p;
15864 /* Parse a parameter declaration.
15866 parameter-declaration:
15867 decl-specifier-seq ... [opt] declarator
15868 decl-specifier-seq declarator = assignment-expression
15869 decl-specifier-seq ... [opt] abstract-declarator [opt]
15870 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15872 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15873 declares a template parameter. (In that case, a non-nested `>'
15874 token encountered during the parsing of the assignment-expression
15875 is not interpreted as a greater-than operator.)
15877 Returns a representation of the parameter, or NULL if an error
15878 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15879 true iff the declarator is of the form "(p)". */
15881 static cp_parameter_declarator *
15882 cp_parser_parameter_declaration (cp_parser *parser,
15883 bool template_parm_p,
15884 bool *parenthesized_p)
15886 int declares_class_or_enum;
15887 cp_decl_specifier_seq decl_specifiers;
15888 cp_declarator *declarator;
15889 tree default_argument;
15890 cp_token *token = NULL, *declarator_token_start = NULL;
15891 const char *saved_message;
15893 /* In a template parameter, `>' is not an operator.
15897 When parsing a default template-argument for a non-type
15898 template-parameter, the first non-nested `>' is taken as the end
15899 of the template parameter-list rather than a greater-than
15902 /* Type definitions may not appear in parameter types. */
15903 saved_message = parser->type_definition_forbidden_message;
15904 parser->type_definition_forbidden_message
15905 = G_("types may not be defined in parameter types");
15907 /* Parse the declaration-specifiers. */
15908 cp_parser_decl_specifier_seq (parser,
15909 CP_PARSER_FLAGS_NONE,
15911 &declares_class_or_enum);
15913 /* Complain about missing 'typename' or other invalid type names. */
15914 if (!decl_specifiers.any_type_specifiers_p)
15915 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15917 /* If an error occurred, there's no reason to attempt to parse the
15918 rest of the declaration. */
15919 if (cp_parser_error_occurred (parser))
15921 parser->type_definition_forbidden_message = saved_message;
15925 /* Peek at the next token. */
15926 token = cp_lexer_peek_token (parser->lexer);
15928 /* If the next token is a `)', `,', `=', `>', or `...', then there
15929 is no declarator. However, when variadic templates are enabled,
15930 there may be a declarator following `...'. */
15931 if (token->type == CPP_CLOSE_PAREN
15932 || token->type == CPP_COMMA
15933 || token->type == CPP_EQ
15934 || token->type == CPP_GREATER)
15937 if (parenthesized_p)
15938 *parenthesized_p = false;
15940 /* Otherwise, there should be a declarator. */
15943 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15944 parser->default_arg_ok_p = false;
15946 /* After seeing a decl-specifier-seq, if the next token is not a
15947 "(", there is no possibility that the code is a valid
15948 expression. Therefore, if parsing tentatively, we commit at
15950 if (!parser->in_template_argument_list_p
15951 /* In an expression context, having seen:
15955 we cannot be sure whether we are looking at a
15956 function-type (taking a "char" as a parameter) or a cast
15957 of some object of type "char" to "int". */
15958 && !parser->in_type_id_in_expr_p
15959 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15960 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15961 cp_parser_commit_to_tentative_parse (parser);
15962 /* Parse the declarator. */
15963 declarator_token_start = token;
15964 declarator = cp_parser_declarator (parser,
15965 CP_PARSER_DECLARATOR_EITHER,
15966 /*ctor_dtor_or_conv_p=*/NULL,
15968 /*member_p=*/false);
15969 parser->default_arg_ok_p = saved_default_arg_ok_p;
15970 /* After the declarator, allow more attributes. */
15971 decl_specifiers.attributes
15972 = chainon (decl_specifiers.attributes,
15973 cp_parser_attributes_opt (parser));
15976 /* If the next token is an ellipsis, and we have not seen a
15977 declarator name, and the type of the declarator contains parameter
15978 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15979 a parameter pack expansion expression. Otherwise, leave the
15980 ellipsis for a C-style variadic function. */
15981 token = cp_lexer_peek_token (parser->lexer);
15982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15984 tree type = decl_specifiers.type;
15986 if (type && DECL_P (type))
15987 type = TREE_TYPE (type);
15990 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15991 && declarator_can_be_parameter_pack (declarator)
15992 && (!declarator || !declarator->parameter_pack_p)
15993 && uses_parameter_packs (type))
15995 /* Consume the `...'. */
15996 cp_lexer_consume_token (parser->lexer);
15997 maybe_warn_variadic_templates ();
15999 /* Build a pack expansion type */
16001 declarator->parameter_pack_p = true;
16003 decl_specifiers.type = make_pack_expansion (type);
16007 /* The restriction on defining new types applies only to the type
16008 of the parameter, not to the default argument. */
16009 parser->type_definition_forbidden_message = saved_message;
16011 /* If the next token is `=', then process a default argument. */
16012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16014 /* Consume the `='. */
16015 cp_lexer_consume_token (parser->lexer);
16017 /* If we are defining a class, then the tokens that make up the
16018 default argument must be saved and processed later. */
16019 if (!template_parm_p && at_class_scope_p ()
16020 && TYPE_BEING_DEFINED (current_class_type)
16021 && !LAMBDA_TYPE_P (current_class_type))
16023 unsigned depth = 0;
16024 int maybe_template_id = 0;
16025 cp_token *first_token;
16028 /* Add tokens until we have processed the entire default
16029 argument. We add the range [first_token, token). */
16030 first_token = cp_lexer_peek_token (parser->lexer);
16035 /* Peek at the next token. */
16036 token = cp_lexer_peek_token (parser->lexer);
16037 /* What we do depends on what token we have. */
16038 switch (token->type)
16040 /* In valid code, a default argument must be
16041 immediately followed by a `,' `)', or `...'. */
16043 if (depth == 0 && maybe_template_id)
16045 /* If we've seen a '<', we might be in a
16046 template-argument-list. Until Core issue 325 is
16047 resolved, we don't know how this situation ought
16048 to be handled, so try to DTRT. We check whether
16049 what comes after the comma is a valid parameter
16050 declaration list. If it is, then the comma ends
16051 the default argument; otherwise the default
16052 argument continues. */
16053 bool error = false;
16056 /* Set ITALP so cp_parser_parameter_declaration_list
16057 doesn't decide to commit to this parse. */
16058 bool saved_italp = parser->in_template_argument_list_p;
16059 parser->in_template_argument_list_p = true;
16061 cp_parser_parse_tentatively (parser);
16062 cp_lexer_consume_token (parser->lexer);
16063 begin_scope (sk_function_parms, NULL_TREE);
16064 cp_parser_parameter_declaration_list (parser, &error);
16065 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16066 pop_binding (DECL_NAME (t), t);
16068 if (!cp_parser_error_occurred (parser) && !error)
16070 cp_parser_abort_tentative_parse (parser);
16072 parser->in_template_argument_list_p = saved_italp;
16075 case CPP_CLOSE_PAREN:
16077 /* If we run into a non-nested `;', `}', or `]',
16078 then the code is invalid -- but the default
16079 argument is certainly over. */
16080 case CPP_SEMICOLON:
16081 case CPP_CLOSE_BRACE:
16082 case CPP_CLOSE_SQUARE:
16085 /* Update DEPTH, if necessary. */
16086 else if (token->type == CPP_CLOSE_PAREN
16087 || token->type == CPP_CLOSE_BRACE
16088 || token->type == CPP_CLOSE_SQUARE)
16092 case CPP_OPEN_PAREN:
16093 case CPP_OPEN_SQUARE:
16094 case CPP_OPEN_BRACE:
16100 /* This might be the comparison operator, or it might
16101 start a template argument list. */
16102 ++maybe_template_id;
16106 if (cxx_dialect == cxx98)
16108 /* Fall through for C++0x, which treats the `>>'
16109 operator like two `>' tokens in certain
16115 /* This might be an operator, or it might close a
16116 template argument list. But if a previous '<'
16117 started a template argument list, this will have
16118 closed it, so we can't be in one anymore. */
16119 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16120 if (maybe_template_id < 0)
16121 maybe_template_id = 0;
16125 /* If we run out of tokens, issue an error message. */
16127 case CPP_PRAGMA_EOL:
16128 error_at (token->location, "file ends in default argument");
16134 /* In these cases, we should look for template-ids.
16135 For example, if the default argument is
16136 `X<int, double>()', we need to do name lookup to
16137 figure out whether or not `X' is a template; if
16138 so, the `,' does not end the default argument.
16140 That is not yet done. */
16147 /* If we've reached the end, stop. */
16151 /* Add the token to the token block. */
16152 token = cp_lexer_consume_token (parser->lexer);
16155 /* Create a DEFAULT_ARG to represent the unparsed default
16157 default_argument = make_node (DEFAULT_ARG);
16158 DEFARG_TOKENS (default_argument)
16159 = cp_token_cache_new (first_token, token);
16160 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16162 /* Outside of a class definition, we can just parse the
16163 assignment-expression. */
16166 token = cp_lexer_peek_token (parser->lexer);
16168 = cp_parser_default_argument (parser, template_parm_p);
16171 if (!parser->default_arg_ok_p)
16173 if (flag_permissive)
16174 warning (0, "deprecated use of default argument for parameter of non-function");
16177 error_at (token->location,
16178 "default arguments are only "
16179 "permitted for function parameters");
16180 default_argument = NULL_TREE;
16183 else if ((declarator && declarator->parameter_pack_p)
16184 || (decl_specifiers.type
16185 && PACK_EXPANSION_P (decl_specifiers.type)))
16187 /* Find the name of the parameter pack. */
16188 cp_declarator *id_declarator = declarator;
16189 while (id_declarator && id_declarator->kind != cdk_id)
16190 id_declarator = id_declarator->declarator;
16192 if (id_declarator && id_declarator->kind == cdk_id)
16193 error_at (declarator_token_start->location,
16195 ? "template parameter pack %qD"
16196 " cannot have a default argument"
16197 : "parameter pack %qD cannot have a default argument",
16198 id_declarator->u.id.unqualified_name);
16200 error_at (declarator_token_start->location,
16202 ? "template parameter pack cannot have a default argument"
16203 : "parameter pack cannot have a default argument");
16205 default_argument = NULL_TREE;
16209 default_argument = NULL_TREE;
16211 return make_parameter_declarator (&decl_specifiers,
16216 /* Parse a default argument and return it.
16218 TEMPLATE_PARM_P is true if this is a default argument for a
16219 non-type template parameter. */
16221 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16223 tree default_argument = NULL_TREE;
16224 bool saved_greater_than_is_operator_p;
16225 bool saved_local_variables_forbidden_p;
16227 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16229 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16230 parser->greater_than_is_operator_p = !template_parm_p;
16231 /* Local variable names (and the `this' keyword) may not
16232 appear in a default argument. */
16233 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16234 parser->local_variables_forbidden_p = true;
16235 /* Parse the assignment-expression. */
16236 if (template_parm_p)
16237 push_deferring_access_checks (dk_no_deferred);
16239 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16240 if (template_parm_p)
16241 pop_deferring_access_checks ();
16242 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16243 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16245 return default_argument;
16248 /* Parse a function-body.
16251 compound_statement */
16254 cp_parser_function_body (cp_parser *parser)
16256 cp_parser_compound_statement (parser, NULL, false);
16259 /* Parse a ctor-initializer-opt followed by a function-body. Return
16260 true if a ctor-initializer was present. */
16263 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16266 bool ctor_initializer_p;
16267 const bool check_body_p =
16268 DECL_CONSTRUCTOR_P (current_function_decl)
16269 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16272 /* Begin the function body. */
16273 body = begin_function_body ();
16274 /* Parse the optional ctor-initializer. */
16275 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16277 /* If we're parsing a constexpr constructor definition, we need
16278 to check that the constructor body is indeed empty. However,
16279 before we get to cp_parser_function_body lot of junk has been
16280 generated, so we can't just check that we have an empty block.
16281 Rather we take a snapshot of the outermost block, and check whether
16282 cp_parser_function_body changed its state. */
16286 if (TREE_CODE (list) == BIND_EXPR)
16287 list = BIND_EXPR_BODY (list);
16288 if (TREE_CODE (list) == STATEMENT_LIST
16289 && STATEMENT_LIST_TAIL (list) != NULL)
16290 last = STATEMENT_LIST_TAIL (list)->stmt;
16292 /* Parse the function-body. */
16293 cp_parser_function_body (parser);
16295 && (TREE_CODE (list) != STATEMENT_LIST
16296 || (last == NULL && STATEMENT_LIST_TAIL (list) != NULL)
16297 || (last != NULL && last != STATEMENT_LIST_TAIL (list)->stmt)))
16299 error ("constexpr constructor does not have empty body");
16300 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
16302 /* Finish the function body. */
16303 finish_function_body (body);
16305 return ctor_initializer_p;
16308 /* Parse an initializer.
16311 = initializer-clause
16312 ( expression-list )
16314 Returns an expression representing the initializer. If no
16315 initializer is present, NULL_TREE is returned.
16317 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16318 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16319 set to TRUE if there is no initializer present. If there is an
16320 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16321 is set to true; otherwise it is set to false. */
16324 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16325 bool* non_constant_p)
16330 /* Peek at the next token. */
16331 token = cp_lexer_peek_token (parser->lexer);
16333 /* Let our caller know whether or not this initializer was
16335 *is_direct_init = (token->type != CPP_EQ);
16336 /* Assume that the initializer is constant. */
16337 *non_constant_p = false;
16339 if (token->type == CPP_EQ)
16341 /* Consume the `='. */
16342 cp_lexer_consume_token (parser->lexer);
16343 /* Parse the initializer-clause. */
16344 init = cp_parser_initializer_clause (parser, non_constant_p);
16346 else if (token->type == CPP_OPEN_PAREN)
16349 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16351 /*allow_expansion_p=*/true,
16354 return error_mark_node;
16355 init = build_tree_list_vec (vec);
16356 release_tree_vector (vec);
16358 else if (token->type == CPP_OPEN_BRACE)
16360 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16361 init = cp_parser_braced_list (parser, non_constant_p);
16362 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16366 /* Anything else is an error. */
16367 cp_parser_error (parser, "expected initializer");
16368 init = error_mark_node;
16374 /* Parse an initializer-clause.
16376 initializer-clause:
16377 assignment-expression
16380 Returns an expression representing the initializer.
16382 If the `assignment-expression' production is used the value
16383 returned is simply a representation for the expression.
16385 Otherwise, calls cp_parser_braced_list. */
16388 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16392 /* Assume the expression is constant. */
16393 *non_constant_p = false;
16395 /* If it is not a `{', then we are looking at an
16396 assignment-expression. */
16397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16400 = cp_parser_constant_expression (parser,
16401 /*allow_non_constant_p=*/true,
16403 if (!*non_constant_p)
16404 initializer = fold_non_dependent_expr (initializer);
16407 initializer = cp_parser_braced_list (parser, non_constant_p);
16409 return initializer;
16412 /* Parse a brace-enclosed initializer list.
16415 { initializer-list , [opt] }
16418 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16419 the elements of the initializer-list (or NULL, if the last
16420 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16421 NULL_TREE. There is no way to detect whether or not the optional
16422 trailing `,' was provided. NON_CONSTANT_P is as for
16423 cp_parser_initializer. */
16426 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16430 /* Consume the `{' token. */
16431 cp_lexer_consume_token (parser->lexer);
16432 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16433 initializer = make_node (CONSTRUCTOR);
16434 /* If it's not a `}', then there is a non-trivial initializer. */
16435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16437 /* Parse the initializer list. */
16438 CONSTRUCTOR_ELTS (initializer)
16439 = cp_parser_initializer_list (parser, non_constant_p);
16440 /* A trailing `,' token is allowed. */
16441 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16442 cp_lexer_consume_token (parser->lexer);
16444 /* Now, there should be a trailing `}'. */
16445 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16446 TREE_TYPE (initializer) = init_list_type_node;
16447 return initializer;
16450 /* Parse an initializer-list.
16453 initializer-clause ... [opt]
16454 initializer-list , initializer-clause ... [opt]
16459 identifier : initializer-clause
16460 initializer-list, identifier : initializer-clause
16462 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16463 for the initializer. If the INDEX of the elt is non-NULL, it is the
16464 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16465 as for cp_parser_initializer. */
16467 static VEC(constructor_elt,gc) *
16468 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16470 VEC(constructor_elt,gc) *v = NULL;
16472 /* Assume all of the expressions are constant. */
16473 *non_constant_p = false;
16475 /* Parse the rest of the list. */
16481 bool clause_non_constant_p;
16483 /* If the next token is an identifier and the following one is a
16484 colon, we are looking at the GNU designated-initializer
16486 if (cp_parser_allow_gnu_extensions_p (parser)
16487 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16488 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16490 /* Warn the user that they are using an extension. */
16491 pedwarn (input_location, OPT_pedantic,
16492 "ISO C++ does not allow designated initializers");
16493 /* Consume the identifier. */
16494 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16495 /* Consume the `:'. */
16496 cp_lexer_consume_token (parser->lexer);
16499 identifier = NULL_TREE;
16501 /* Parse the initializer. */
16502 initializer = cp_parser_initializer_clause (parser,
16503 &clause_non_constant_p);
16504 /* If any clause is non-constant, so is the entire initializer. */
16505 if (clause_non_constant_p)
16506 *non_constant_p = true;
16508 /* If we have an ellipsis, this is an initializer pack
16510 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16512 /* Consume the `...'. */
16513 cp_lexer_consume_token (parser->lexer);
16515 /* Turn the initializer into an initializer expansion. */
16516 initializer = make_pack_expansion (initializer);
16519 /* Add it to the vector. */
16520 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16522 /* If the next token is not a comma, we have reached the end of
16524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16527 /* Peek at the next token. */
16528 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16529 /* If the next token is a `}', then we're still done. An
16530 initializer-clause can have a trailing `,' after the
16531 initializer-list and before the closing `}'. */
16532 if (token->type == CPP_CLOSE_BRACE)
16535 /* Consume the `,' token. */
16536 cp_lexer_consume_token (parser->lexer);
16542 /* Classes [gram.class] */
16544 /* Parse a class-name.
16550 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16551 to indicate that names looked up in dependent types should be
16552 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16553 keyword has been used to indicate that the name that appears next
16554 is a template. TAG_TYPE indicates the explicit tag given before
16555 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16556 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16557 is the class being defined in a class-head.
16559 Returns the TYPE_DECL representing the class. */
16562 cp_parser_class_name (cp_parser *parser,
16563 bool typename_keyword_p,
16564 bool template_keyword_p,
16565 enum tag_types tag_type,
16566 bool check_dependency_p,
16568 bool is_declaration)
16574 tree identifier = NULL_TREE;
16576 /* All class-names start with an identifier. */
16577 token = cp_lexer_peek_token (parser->lexer);
16578 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16580 cp_parser_error (parser, "expected class-name");
16581 return error_mark_node;
16584 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16585 to a template-id, so we save it here. */
16586 scope = parser->scope;
16587 if (scope == error_mark_node)
16588 return error_mark_node;
16590 /* Any name names a type if we're following the `typename' keyword
16591 in a qualified name where the enclosing scope is type-dependent. */
16592 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16593 && dependent_type_p (scope));
16594 /* Handle the common case (an identifier, but not a template-id)
16596 if (token->type == CPP_NAME
16597 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16599 cp_token *identifier_token;
16602 /* Look for the identifier. */
16603 identifier_token = cp_lexer_peek_token (parser->lexer);
16604 ambiguous_p = identifier_token->ambiguous_p;
16605 identifier = cp_parser_identifier (parser);
16606 /* If the next token isn't an identifier, we are certainly not
16607 looking at a class-name. */
16608 if (identifier == error_mark_node)
16609 decl = error_mark_node;
16610 /* If we know this is a type-name, there's no need to look it
16612 else if (typename_p)
16616 tree ambiguous_decls;
16617 /* If we already know that this lookup is ambiguous, then
16618 we've already issued an error message; there's no reason
16622 cp_parser_simulate_error (parser);
16623 return error_mark_node;
16625 /* If the next token is a `::', then the name must be a type
16628 [basic.lookup.qual]
16630 During the lookup for a name preceding the :: scope
16631 resolution operator, object, function, and enumerator
16632 names are ignored. */
16633 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16634 tag_type = typename_type;
16635 /* Look up the name. */
16636 decl = cp_parser_lookup_name (parser, identifier,
16638 /*is_template=*/false,
16639 /*is_namespace=*/false,
16640 check_dependency_p,
16642 identifier_token->location);
16643 if (ambiguous_decls)
16645 if (cp_parser_parsing_tentatively (parser))
16646 cp_parser_simulate_error (parser);
16647 return error_mark_node;
16653 /* Try a template-id. */
16654 decl = cp_parser_template_id (parser, template_keyword_p,
16655 check_dependency_p,
16657 if (decl == error_mark_node)
16658 return error_mark_node;
16661 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16663 /* If this is a typename, create a TYPENAME_TYPE. */
16664 if (typename_p && decl != error_mark_node)
16666 decl = make_typename_type (scope, decl, typename_type,
16667 /*complain=*/tf_error);
16668 if (decl != error_mark_node)
16669 decl = TYPE_NAME (decl);
16672 /* Check to see that it is really the name of a class. */
16673 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16674 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16675 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16676 /* Situations like this:
16678 template <typename T> struct A {
16679 typename T::template X<int>::I i;
16682 are problematic. Is `T::template X<int>' a class-name? The
16683 standard does not seem to be definitive, but there is no other
16684 valid interpretation of the following `::'. Therefore, those
16685 names are considered class-names. */
16687 decl = make_typename_type (scope, decl, tag_type, tf_error);
16688 if (decl != error_mark_node)
16689 decl = TYPE_NAME (decl);
16691 else if (TREE_CODE (decl) != TYPE_DECL
16692 || TREE_TYPE (decl) == error_mark_node
16693 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
16694 decl = error_mark_node;
16696 if (decl == error_mark_node)
16697 cp_parser_error (parser, "expected class-name");
16698 else if (identifier && !parser->scope)
16699 maybe_note_name_used_in_class (identifier, decl);
16704 /* Parse a class-specifier.
16707 class-head { member-specification [opt] }
16709 Returns the TREE_TYPE representing the class. */
16712 cp_parser_class_specifier (cp_parser* parser)
16715 tree attributes = NULL_TREE;
16716 bool nested_name_specifier_p;
16717 unsigned saved_num_template_parameter_lists;
16718 bool saved_in_function_body;
16719 bool saved_in_unbraced_linkage_specification_p;
16720 tree old_scope = NULL_TREE;
16721 tree scope = NULL_TREE;
16724 push_deferring_access_checks (dk_no_deferred);
16726 /* Parse the class-head. */
16727 type = cp_parser_class_head (parser,
16728 &nested_name_specifier_p,
16731 /* If the class-head was a semantic disaster, skip the entire body
16735 cp_parser_skip_to_end_of_block_or_statement (parser);
16736 pop_deferring_access_checks ();
16737 return error_mark_node;
16740 /* Look for the `{'. */
16741 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16743 pop_deferring_access_checks ();
16744 return error_mark_node;
16747 /* Process the base classes. If they're invalid, skip the
16748 entire class body. */
16749 if (!xref_basetypes (type, bases))
16751 /* Consuming the closing brace yields better error messages
16753 if (cp_parser_skip_to_closing_brace (parser))
16754 cp_lexer_consume_token (parser->lexer);
16755 pop_deferring_access_checks ();
16756 return error_mark_node;
16759 /* Issue an error message if type-definitions are forbidden here. */
16760 cp_parser_check_type_definition (parser);
16761 /* Remember that we are defining one more class. */
16762 ++parser->num_classes_being_defined;
16763 /* Inside the class, surrounding template-parameter-lists do not
16765 saved_num_template_parameter_lists
16766 = parser->num_template_parameter_lists;
16767 parser->num_template_parameter_lists = 0;
16768 /* We are not in a function body. */
16769 saved_in_function_body = parser->in_function_body;
16770 parser->in_function_body = false;
16771 /* We are not immediately inside an extern "lang" block. */
16772 saved_in_unbraced_linkage_specification_p
16773 = parser->in_unbraced_linkage_specification_p;
16774 parser->in_unbraced_linkage_specification_p = false;
16776 /* Start the class. */
16777 if (nested_name_specifier_p)
16779 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16780 old_scope = push_inner_scope (scope);
16782 type = begin_class_definition (type, attributes);
16784 if (type == error_mark_node)
16785 /* If the type is erroneous, skip the entire body of the class. */
16786 cp_parser_skip_to_closing_brace (parser);
16788 /* Parse the member-specification. */
16789 cp_parser_member_specification_opt (parser);
16791 /* Look for the trailing `}'. */
16792 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16793 /* Look for trailing attributes to apply to this class. */
16794 if (cp_parser_allow_gnu_extensions_p (parser))
16795 attributes = cp_parser_attributes_opt (parser);
16796 if (type != error_mark_node)
16797 type = finish_struct (type, attributes);
16798 if (nested_name_specifier_p)
16799 pop_inner_scope (old_scope, scope);
16800 /* If this class is not itself within the scope of another class,
16801 then we need to parse the bodies of all of the queued function
16802 definitions. Note that the queued functions defined in a class
16803 are not always processed immediately following the
16804 class-specifier for that class. Consider:
16807 struct B { void f() { sizeof (A); } };
16810 If `f' were processed before the processing of `A' were
16811 completed, there would be no way to compute the size of `A'.
16812 Note that the nesting we are interested in here is lexical --
16813 not the semantic nesting given by TYPE_CONTEXT. In particular,
16816 struct A { struct B; };
16817 struct A::B { void f() { } };
16819 there is no need to delay the parsing of `A::B::f'. */
16820 if (--parser->num_classes_being_defined == 0)
16823 tree class_type = NULL_TREE;
16824 tree pushed_scope = NULL_TREE;
16826 cp_default_arg_entry *e;
16828 /* In a first pass, parse default arguments to the functions.
16829 Then, in a second pass, parse the bodies of the functions.
16830 This two-phased approach handles cases like:
16838 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16842 /* If there are default arguments that have not yet been processed,
16843 take care of them now. */
16844 if (class_type != e->class_type)
16847 pop_scope (pushed_scope);
16848 class_type = e->class_type;
16849 pushed_scope = push_scope (class_type);
16851 /* Make sure that any template parameters are in scope. */
16852 maybe_begin_member_template_processing (fn);
16853 /* Parse the default argument expressions. */
16854 cp_parser_late_parsing_default_args (parser, fn);
16855 /* Remove any template parameters from the symbol table. */
16856 maybe_end_member_template_processing ();
16859 pop_scope (pushed_scope);
16860 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16861 /* Now parse the body of the functions. */
16862 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16863 cp_parser_late_parsing_for_member (parser, fn);
16864 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16867 /* Put back any saved access checks. */
16868 pop_deferring_access_checks ();
16870 /* Restore saved state. */
16871 parser->in_function_body = saved_in_function_body;
16872 parser->num_template_parameter_lists
16873 = saved_num_template_parameter_lists;
16874 parser->in_unbraced_linkage_specification_p
16875 = saved_in_unbraced_linkage_specification_p;
16880 /* Parse a class-head.
16883 class-key identifier [opt] base-clause [opt]
16884 class-key nested-name-specifier identifier base-clause [opt]
16885 class-key nested-name-specifier [opt] template-id
16889 class-key attributes identifier [opt] base-clause [opt]
16890 class-key attributes nested-name-specifier identifier base-clause [opt]
16891 class-key attributes nested-name-specifier [opt] template-id
16894 Upon return BASES is initialized to the list of base classes (or
16895 NULL, if there are none) in the same form returned by
16896 cp_parser_base_clause.
16898 Returns the TYPE of the indicated class. Sets
16899 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16900 involving a nested-name-specifier was used, and FALSE otherwise.
16902 Returns error_mark_node if this is not a class-head.
16904 Returns NULL_TREE if the class-head is syntactically valid, but
16905 semantically invalid in a way that means we should skip the entire
16906 body of the class. */
16909 cp_parser_class_head (cp_parser* parser,
16910 bool* nested_name_specifier_p,
16911 tree *attributes_p,
16914 tree nested_name_specifier;
16915 enum tag_types class_key;
16916 tree id = NULL_TREE;
16917 tree type = NULL_TREE;
16919 bool template_id_p = false;
16920 bool qualified_p = false;
16921 bool invalid_nested_name_p = false;
16922 bool invalid_explicit_specialization_p = false;
16923 tree pushed_scope = NULL_TREE;
16924 unsigned num_templates;
16925 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16926 /* Assume no nested-name-specifier will be present. */
16927 *nested_name_specifier_p = false;
16928 /* Assume no template parameter lists will be used in defining the
16932 *bases = NULL_TREE;
16934 /* Look for the class-key. */
16935 class_key = cp_parser_class_key (parser);
16936 if (class_key == none_type)
16937 return error_mark_node;
16939 /* Parse the attributes. */
16940 attributes = cp_parser_attributes_opt (parser);
16942 /* If the next token is `::', that is invalid -- but sometimes
16943 people do try to write:
16947 Handle this gracefully by accepting the extra qualifier, and then
16948 issuing an error about it later if this really is a
16949 class-head. If it turns out just to be an elaborated type
16950 specifier, remain silent. */
16951 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16952 qualified_p = true;
16954 push_deferring_access_checks (dk_no_check);
16956 /* Determine the name of the class. Begin by looking for an
16957 optional nested-name-specifier. */
16958 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16959 nested_name_specifier
16960 = cp_parser_nested_name_specifier_opt (parser,
16961 /*typename_keyword_p=*/false,
16962 /*check_dependency_p=*/false,
16964 /*is_declaration=*/false);
16965 /* If there was a nested-name-specifier, then there *must* be an
16967 if (nested_name_specifier)
16969 type_start_token = cp_lexer_peek_token (parser->lexer);
16970 /* Although the grammar says `identifier', it really means
16971 `class-name' or `template-name'. You are only allowed to
16972 define a class that has already been declared with this
16975 The proposed resolution for Core Issue 180 says that wherever
16976 you see `class T::X' you should treat `X' as a type-name.
16978 It is OK to define an inaccessible class; for example:
16980 class A { class B; };
16983 We do not know if we will see a class-name, or a
16984 template-name. We look for a class-name first, in case the
16985 class-name is a template-id; if we looked for the
16986 template-name first we would stop after the template-name. */
16987 cp_parser_parse_tentatively (parser);
16988 type = cp_parser_class_name (parser,
16989 /*typename_keyword_p=*/false,
16990 /*template_keyword_p=*/false,
16992 /*check_dependency_p=*/false,
16993 /*class_head_p=*/true,
16994 /*is_declaration=*/false);
16995 /* If that didn't work, ignore the nested-name-specifier. */
16996 if (!cp_parser_parse_definitely (parser))
16998 invalid_nested_name_p = true;
16999 type_start_token = cp_lexer_peek_token (parser->lexer);
17000 id = cp_parser_identifier (parser);
17001 if (id == error_mark_node)
17004 /* If we could not find a corresponding TYPE, treat this
17005 declaration like an unqualified declaration. */
17006 if (type == error_mark_node)
17007 nested_name_specifier = NULL_TREE;
17008 /* Otherwise, count the number of templates used in TYPE and its
17009 containing scopes. */
17014 for (scope = TREE_TYPE (type);
17015 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17016 scope = (TYPE_P (scope)
17017 ? TYPE_CONTEXT (scope)
17018 : DECL_CONTEXT (scope)))
17020 && CLASS_TYPE_P (scope)
17021 && CLASSTYPE_TEMPLATE_INFO (scope)
17022 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17023 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17027 /* Otherwise, the identifier is optional. */
17030 /* We don't know whether what comes next is a template-id,
17031 an identifier, or nothing at all. */
17032 cp_parser_parse_tentatively (parser);
17033 /* Check for a template-id. */
17034 type_start_token = cp_lexer_peek_token (parser->lexer);
17035 id = cp_parser_template_id (parser,
17036 /*template_keyword_p=*/false,
17037 /*check_dependency_p=*/true,
17038 /*is_declaration=*/true);
17039 /* If that didn't work, it could still be an identifier. */
17040 if (!cp_parser_parse_definitely (parser))
17042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17044 type_start_token = cp_lexer_peek_token (parser->lexer);
17045 id = cp_parser_identifier (parser);
17052 template_id_p = true;
17057 pop_deferring_access_checks ();
17060 cp_parser_check_for_invalid_template_id (parser, id,
17061 type_start_token->location);
17063 /* If it's not a `:' or a `{' then we can't really be looking at a
17064 class-head, since a class-head only appears as part of a
17065 class-specifier. We have to detect this situation before calling
17066 xref_tag, since that has irreversible side-effects. */
17067 if (!cp_parser_next_token_starts_class_definition_p (parser))
17069 cp_parser_error (parser, "expected %<{%> or %<:%>");
17070 return error_mark_node;
17073 /* At this point, we're going ahead with the class-specifier, even
17074 if some other problem occurs. */
17075 cp_parser_commit_to_tentative_parse (parser);
17076 /* Issue the error about the overly-qualified name now. */
17079 cp_parser_error (parser,
17080 "global qualification of class name is invalid");
17081 return error_mark_node;
17083 else if (invalid_nested_name_p)
17085 cp_parser_error (parser,
17086 "qualified name does not name a class");
17087 return error_mark_node;
17089 else if (nested_name_specifier)
17093 /* Reject typedef-names in class heads. */
17094 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17096 error_at (type_start_token->location,
17097 "invalid class name in declaration of %qD",
17103 /* Figure out in what scope the declaration is being placed. */
17104 scope = current_scope ();
17105 /* If that scope does not contain the scope in which the
17106 class was originally declared, the program is invalid. */
17107 if (scope && !is_ancestor (scope, nested_name_specifier))
17109 if (at_namespace_scope_p ())
17110 error_at (type_start_token->location,
17111 "declaration of %qD in namespace %qD which does not "
17113 type, scope, nested_name_specifier);
17115 error_at (type_start_token->location,
17116 "declaration of %qD in %qD which does not enclose %qD",
17117 type, scope, nested_name_specifier);
17123 A declarator-id shall not be qualified except for the
17124 definition of a ... nested class outside of its class
17125 ... [or] the definition or explicit instantiation of a
17126 class member of a namespace outside of its namespace. */
17127 if (scope == nested_name_specifier)
17129 permerror (nested_name_specifier_token_start->location,
17130 "extra qualification not allowed");
17131 nested_name_specifier = NULL_TREE;
17135 /* An explicit-specialization must be preceded by "template <>". If
17136 it is not, try to recover gracefully. */
17137 if (at_namespace_scope_p ()
17138 && parser->num_template_parameter_lists == 0
17141 error_at (type_start_token->location,
17142 "an explicit specialization must be preceded by %<template <>%>");
17143 invalid_explicit_specialization_p = true;
17144 /* Take the same action that would have been taken by
17145 cp_parser_explicit_specialization. */
17146 ++parser->num_template_parameter_lists;
17147 begin_specialization ();
17149 /* There must be no "return" statements between this point and the
17150 end of this function; set "type "to the correct return value and
17151 use "goto done;" to return. */
17152 /* Make sure that the right number of template parameters were
17154 if (!cp_parser_check_template_parameters (parser, num_templates,
17155 type_start_token->location,
17156 /*declarator=*/NULL))
17158 /* If something went wrong, there is no point in even trying to
17159 process the class-definition. */
17164 /* Look up the type. */
17167 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17168 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17169 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17171 error_at (type_start_token->location,
17172 "function template %qD redeclared as a class template", id);
17173 type = error_mark_node;
17177 type = TREE_TYPE (id);
17178 type = maybe_process_partial_specialization (type);
17180 if (nested_name_specifier)
17181 pushed_scope = push_scope (nested_name_specifier);
17183 else if (nested_name_specifier)
17189 template <typename T> struct S { struct T };
17190 template <typename T> struct S<T>::T { };
17192 we will get a TYPENAME_TYPE when processing the definition of
17193 `S::T'. We need to resolve it to the actual type before we
17194 try to define it. */
17195 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17197 class_type = resolve_typename_type (TREE_TYPE (type),
17198 /*only_current_p=*/false);
17199 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17200 type = TYPE_NAME (class_type);
17203 cp_parser_error (parser, "could not resolve typename type");
17204 type = error_mark_node;
17208 if (maybe_process_partial_specialization (TREE_TYPE (type))
17209 == error_mark_node)
17215 class_type = current_class_type;
17216 /* Enter the scope indicated by the nested-name-specifier. */
17217 pushed_scope = push_scope (nested_name_specifier);
17218 /* Get the canonical version of this type. */
17219 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17220 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17221 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17223 type = push_template_decl (type);
17224 if (type == error_mark_node)
17231 type = TREE_TYPE (type);
17232 *nested_name_specifier_p = true;
17234 else /* The name is not a nested name. */
17236 /* If the class was unnamed, create a dummy name. */
17238 id = make_anon_name ();
17239 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17240 parser->num_template_parameter_lists);
17243 /* Indicate whether this class was declared as a `class' or as a
17245 if (TREE_CODE (type) == RECORD_TYPE)
17246 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17247 cp_parser_check_class_key (class_key, type);
17249 /* If this type was already complete, and we see another definition,
17250 that's an error. */
17251 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17253 error_at (type_start_token->location, "redefinition of %q#T",
17255 error_at (type_start_token->location, "previous definition of %q+#T",
17260 else if (type == error_mark_node)
17263 /* We will have entered the scope containing the class; the names of
17264 base classes should be looked up in that context. For example:
17266 struct A { struct B {}; struct C; };
17267 struct A::C : B {};
17271 /* Get the list of base-classes, if there is one. */
17272 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17273 *bases = cp_parser_base_clause (parser);
17276 /* Leave the scope given by the nested-name-specifier. We will
17277 enter the class scope itself while processing the members. */
17279 pop_scope (pushed_scope);
17281 if (invalid_explicit_specialization_p)
17283 end_specialization ();
17284 --parser->num_template_parameter_lists;
17288 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17289 *attributes_p = attributes;
17293 /* Parse a class-key.
17300 Returns the kind of class-key specified, or none_type to indicate
17303 static enum tag_types
17304 cp_parser_class_key (cp_parser* parser)
17307 enum tag_types tag_type;
17309 /* Look for the class-key. */
17310 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17314 /* Check to see if the TOKEN is a class-key. */
17315 tag_type = cp_parser_token_is_class_key (token);
17317 cp_parser_error (parser, "expected class-key");
17321 /* Parse an (optional) member-specification.
17323 member-specification:
17324 member-declaration member-specification [opt]
17325 access-specifier : member-specification [opt] */
17328 cp_parser_member_specification_opt (cp_parser* parser)
17335 /* Peek at the next token. */
17336 token = cp_lexer_peek_token (parser->lexer);
17337 /* If it's a `}', or EOF then we've seen all the members. */
17338 if (token->type == CPP_CLOSE_BRACE
17339 || token->type == CPP_EOF
17340 || token->type == CPP_PRAGMA_EOL)
17343 /* See if this token is a keyword. */
17344 keyword = token->keyword;
17348 case RID_PROTECTED:
17350 /* Consume the access-specifier. */
17351 cp_lexer_consume_token (parser->lexer);
17352 /* Remember which access-specifier is active. */
17353 current_access_specifier = token->u.value;
17354 /* Look for the `:'. */
17355 cp_parser_require (parser, CPP_COLON, RT_COLON);
17359 /* Accept #pragmas at class scope. */
17360 if (token->type == CPP_PRAGMA)
17362 cp_parser_pragma (parser, pragma_external);
17366 /* Otherwise, the next construction must be a
17367 member-declaration. */
17368 cp_parser_member_declaration (parser);
17373 /* Parse a member-declaration.
17375 member-declaration:
17376 decl-specifier-seq [opt] member-declarator-list [opt] ;
17377 function-definition ; [opt]
17378 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17380 template-declaration
17382 member-declarator-list:
17384 member-declarator-list , member-declarator
17387 declarator pure-specifier [opt]
17388 declarator constant-initializer [opt]
17389 identifier [opt] : constant-expression
17393 member-declaration:
17394 __extension__ member-declaration
17397 declarator attributes [opt] pure-specifier [opt]
17398 declarator attributes [opt] constant-initializer [opt]
17399 identifier [opt] attributes [opt] : constant-expression
17403 member-declaration:
17404 static_assert-declaration */
17407 cp_parser_member_declaration (cp_parser* parser)
17409 cp_decl_specifier_seq decl_specifiers;
17410 tree prefix_attributes;
17412 int declares_class_or_enum;
17414 cp_token *token = NULL;
17415 cp_token *decl_spec_token_start = NULL;
17416 cp_token *initializer_token_start = NULL;
17417 int saved_pedantic;
17419 /* Check for the `__extension__' keyword. */
17420 if (cp_parser_extension_opt (parser, &saved_pedantic))
17423 cp_parser_member_declaration (parser);
17424 /* Restore the old value of the PEDANTIC flag. */
17425 pedantic = saved_pedantic;
17430 /* Check for a template-declaration. */
17431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17433 /* An explicit specialization here is an error condition, and we
17434 expect the specialization handler to detect and report this. */
17435 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17436 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17437 cp_parser_explicit_specialization (parser);
17439 cp_parser_template_declaration (parser, /*member_p=*/true);
17444 /* Check for a using-declaration. */
17445 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17447 /* Parse the using-declaration. */
17448 cp_parser_using_declaration (parser,
17449 /*access_declaration_p=*/false);
17453 /* Check for @defs. */
17454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17457 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17458 ivar = ivar_chains;
17462 ivar = TREE_CHAIN (member);
17463 TREE_CHAIN (member) = NULL_TREE;
17464 finish_member_declaration (member);
17469 /* If the next token is `static_assert' we have a static assertion. */
17470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17472 cp_parser_static_assert (parser, /*member_p=*/true);
17476 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17479 /* Parse the decl-specifier-seq. */
17480 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17481 cp_parser_decl_specifier_seq (parser,
17482 CP_PARSER_FLAGS_OPTIONAL,
17484 &declares_class_or_enum);
17485 prefix_attributes = decl_specifiers.attributes;
17486 decl_specifiers.attributes = NULL_TREE;
17487 /* Check for an invalid type-name. */
17488 if (!decl_specifiers.any_type_specifiers_p
17489 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17491 /* If there is no declarator, then the decl-specifier-seq should
17493 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17495 /* If there was no decl-specifier-seq, and the next token is a
17496 `;', then we have something like:
17502 Each member-declaration shall declare at least one member
17503 name of the class. */
17504 if (!decl_specifiers.any_specifiers_p)
17506 cp_token *token = cp_lexer_peek_token (parser->lexer);
17507 if (!in_system_header_at (token->location))
17508 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17514 /* See if this declaration is a friend. */
17515 friend_p = cp_parser_friend_p (&decl_specifiers);
17516 /* If there were decl-specifiers, check to see if there was
17517 a class-declaration. */
17518 type = check_tag_decl (&decl_specifiers);
17519 /* Nested classes have already been added to the class, but
17520 a `friend' needs to be explicitly registered. */
17523 /* If the `friend' keyword was present, the friend must
17524 be introduced with a class-key. */
17525 if (!declares_class_or_enum)
17526 error_at (decl_spec_token_start->location,
17527 "a class-key must be used when declaring a friend");
17530 template <typename T> struct A {
17531 friend struct A<T>::B;
17534 A<T>::B will be represented by a TYPENAME_TYPE, and
17535 therefore not recognized by check_tag_decl. */
17537 && decl_specifiers.type
17538 && TYPE_P (decl_specifiers.type))
17539 type = decl_specifiers.type;
17540 if (!type || !TYPE_P (type))
17541 error_at (decl_spec_token_start->location,
17542 "friend declaration does not name a class or "
17545 make_friend_class (current_class_type, type,
17546 /*complain=*/true);
17548 /* If there is no TYPE, an error message will already have
17550 else if (!type || type == error_mark_node)
17552 /* An anonymous aggregate has to be handled specially; such
17553 a declaration really declares a data member (with a
17554 particular type), as opposed to a nested class. */
17555 else if (ANON_AGGR_TYPE_P (type))
17557 /* Remove constructors and such from TYPE, now that we
17558 know it is an anonymous aggregate. */
17559 fixup_anonymous_aggr (type);
17560 /* And make the corresponding data member. */
17561 decl = build_decl (decl_spec_token_start->location,
17562 FIELD_DECL, NULL_TREE, type);
17563 /* Add it to the class. */
17564 finish_member_declaration (decl);
17567 cp_parser_check_access_in_redeclaration
17569 decl_spec_token_start->location);
17574 /* See if these declarations will be friends. */
17575 friend_p = cp_parser_friend_p (&decl_specifiers);
17577 /* Keep going until we hit the `;' at the end of the
17579 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17581 tree attributes = NULL_TREE;
17582 tree first_attribute;
17584 /* Peek at the next token. */
17585 token = cp_lexer_peek_token (parser->lexer);
17587 /* Check for a bitfield declaration. */
17588 if (token->type == CPP_COLON
17589 || (token->type == CPP_NAME
17590 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17596 /* Get the name of the bitfield. Note that we cannot just
17597 check TOKEN here because it may have been invalidated by
17598 the call to cp_lexer_peek_nth_token above. */
17599 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17600 identifier = cp_parser_identifier (parser);
17602 identifier = NULL_TREE;
17604 /* Consume the `:' token. */
17605 cp_lexer_consume_token (parser->lexer);
17606 /* Get the width of the bitfield. */
17608 = cp_parser_constant_expression (parser,
17609 /*allow_non_constant=*/false,
17612 /* Look for attributes that apply to the bitfield. */
17613 attributes = cp_parser_attributes_opt (parser);
17614 /* Remember which attributes are prefix attributes and
17616 first_attribute = attributes;
17617 /* Combine the attributes. */
17618 attributes = chainon (prefix_attributes, attributes);
17620 /* Create the bitfield declaration. */
17621 decl = grokbitfield (identifier
17622 ? make_id_declarator (NULL_TREE,
17632 cp_declarator *declarator;
17634 tree asm_specification;
17635 int ctor_dtor_or_conv_p;
17637 /* Parse the declarator. */
17639 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17640 &ctor_dtor_or_conv_p,
17641 /*parenthesized_p=*/NULL,
17642 /*member_p=*/true);
17644 /* If something went wrong parsing the declarator, make sure
17645 that we at least consume some tokens. */
17646 if (declarator == cp_error_declarator)
17648 /* Skip to the end of the statement. */
17649 cp_parser_skip_to_end_of_statement (parser);
17650 /* If the next token is not a semicolon, that is
17651 probably because we just skipped over the body of
17652 a function. So, we consume a semicolon if
17653 present, but do not issue an error message if it
17655 if (cp_lexer_next_token_is (parser->lexer,
17657 cp_lexer_consume_token (parser->lexer);
17661 if (declares_class_or_enum & 2)
17662 cp_parser_check_for_definition_in_return_type
17663 (declarator, decl_specifiers.type,
17664 decl_specifiers.type_location);
17666 /* Look for an asm-specification. */
17667 asm_specification = cp_parser_asm_specification_opt (parser);
17668 /* Look for attributes that apply to the declaration. */
17669 attributes = cp_parser_attributes_opt (parser);
17670 /* Remember which attributes are prefix attributes and
17672 first_attribute = attributes;
17673 /* Combine the attributes. */
17674 attributes = chainon (prefix_attributes, attributes);
17676 /* If it's an `=', then we have a constant-initializer or a
17677 pure-specifier. It is not correct to parse the
17678 initializer before registering the member declaration
17679 since the member declaration should be in scope while
17680 its initializer is processed. However, the rest of the
17681 front end does not yet provide an interface that allows
17682 us to handle this correctly. */
17683 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17687 A pure-specifier shall be used only in the declaration of
17688 a virtual function.
17690 A member-declarator can contain a constant-initializer
17691 only if it declares a static member of integral or
17694 Therefore, if the DECLARATOR is for a function, we look
17695 for a pure-specifier; otherwise, we look for a
17696 constant-initializer. When we call `grokfield', it will
17697 perform more stringent semantics checks. */
17698 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17699 if (function_declarator_p (declarator))
17700 initializer = cp_parser_pure_specifier (parser);
17702 /* Parse the initializer. */
17703 initializer = cp_parser_constant_initializer (parser);
17705 /* Otherwise, there is no initializer. */
17707 initializer = NULL_TREE;
17709 /* See if we are probably looking at a function
17710 definition. We are certainly not looking at a
17711 member-declarator. Calling `grokfield' has
17712 side-effects, so we must not do it unless we are sure
17713 that we are looking at a member-declarator. */
17714 if (cp_parser_token_starts_function_definition_p
17715 (cp_lexer_peek_token (parser->lexer)))
17717 /* The grammar does not allow a pure-specifier to be
17718 used when a member function is defined. (It is
17719 possible that this fact is an oversight in the
17720 standard, since a pure function may be defined
17721 outside of the class-specifier. */
17723 error_at (initializer_token_start->location,
17724 "pure-specifier on function-definition");
17725 decl = cp_parser_save_member_function_body (parser,
17729 /* If the member was not a friend, declare it here. */
17731 finish_member_declaration (decl);
17732 /* Peek at the next token. */
17733 token = cp_lexer_peek_token (parser->lexer);
17734 /* If the next token is a semicolon, consume it. */
17735 if (token->type == CPP_SEMICOLON)
17736 cp_lexer_consume_token (parser->lexer);
17740 if (declarator->kind == cdk_function)
17741 declarator->id_loc = token->location;
17742 /* Create the declaration. */
17743 decl = grokfield (declarator, &decl_specifiers,
17744 initializer, /*init_const_expr_p=*/true,
17749 /* Reset PREFIX_ATTRIBUTES. */
17750 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17751 attributes = TREE_CHAIN (attributes);
17753 TREE_CHAIN (attributes) = NULL_TREE;
17755 /* If there is any qualification still in effect, clear it
17756 now; we will be starting fresh with the next declarator. */
17757 parser->scope = NULL_TREE;
17758 parser->qualifying_scope = NULL_TREE;
17759 parser->object_scope = NULL_TREE;
17760 /* If it's a `,', then there are more declarators. */
17761 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17762 cp_lexer_consume_token (parser->lexer);
17763 /* If the next token isn't a `;', then we have a parse error. */
17764 else if (cp_lexer_next_token_is_not (parser->lexer,
17767 cp_parser_error (parser, "expected %<;%>");
17768 /* Skip tokens until we find a `;'. */
17769 cp_parser_skip_to_end_of_statement (parser);
17776 /* Add DECL to the list of members. */
17778 finish_member_declaration (decl);
17780 if (TREE_CODE (decl) == FUNCTION_DECL)
17781 cp_parser_save_default_args (parser, decl);
17786 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17789 /* Parse a pure-specifier.
17794 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17795 Otherwise, ERROR_MARK_NODE is returned. */
17798 cp_parser_pure_specifier (cp_parser* parser)
17802 /* Look for the `=' token. */
17803 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17804 return error_mark_node;
17805 /* Look for the `0' token. */
17806 token = cp_lexer_peek_token (parser->lexer);
17808 if (token->type == CPP_EOF
17809 || token->type == CPP_PRAGMA_EOL)
17810 return error_mark_node;
17812 cp_lexer_consume_token (parser->lexer);
17814 /* Accept = default or = delete in c++0x mode. */
17815 if (token->keyword == RID_DEFAULT
17816 || token->keyword == RID_DELETE)
17818 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17819 return token->u.value;
17822 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17823 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17825 cp_parser_error (parser,
17826 "invalid pure specifier (only %<= 0%> is allowed)");
17827 cp_parser_skip_to_end_of_statement (parser);
17828 return error_mark_node;
17830 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17832 error_at (token->location, "templates may not be %<virtual%>");
17833 return error_mark_node;
17836 return integer_zero_node;
17839 /* Parse a constant-initializer.
17841 constant-initializer:
17842 = constant-expression
17844 Returns a representation of the constant-expression. */
17847 cp_parser_constant_initializer (cp_parser* parser)
17849 /* Look for the `=' token. */
17850 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17851 return error_mark_node;
17853 /* It is invalid to write:
17855 struct S { static const int i = { 7 }; };
17858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17860 cp_parser_error (parser,
17861 "a brace-enclosed initializer is not allowed here");
17862 /* Consume the opening brace. */
17863 cp_lexer_consume_token (parser->lexer);
17864 /* Skip the initializer. */
17865 cp_parser_skip_to_closing_brace (parser);
17866 /* Look for the trailing `}'. */
17867 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17869 return error_mark_node;
17872 return cp_parser_constant_expression (parser,
17873 /*allow_non_constant=*/false,
17877 /* Derived classes [gram.class.derived] */
17879 /* Parse a base-clause.
17882 : base-specifier-list
17884 base-specifier-list:
17885 base-specifier ... [opt]
17886 base-specifier-list , base-specifier ... [opt]
17888 Returns a TREE_LIST representing the base-classes, in the order in
17889 which they were declared. The representation of each node is as
17890 described by cp_parser_base_specifier.
17892 In the case that no bases are specified, this function will return
17893 NULL_TREE, not ERROR_MARK_NODE. */
17896 cp_parser_base_clause (cp_parser* parser)
17898 tree bases = NULL_TREE;
17900 /* Look for the `:' that begins the list. */
17901 cp_parser_require (parser, CPP_COLON, RT_COLON);
17903 /* Scan the base-specifier-list. */
17908 bool pack_expansion_p = false;
17910 /* Look for the base-specifier. */
17911 base = cp_parser_base_specifier (parser);
17912 /* Look for the (optional) ellipsis. */
17913 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17915 /* Consume the `...'. */
17916 cp_lexer_consume_token (parser->lexer);
17918 pack_expansion_p = true;
17921 /* Add BASE to the front of the list. */
17922 if (base != error_mark_node)
17924 if (pack_expansion_p)
17925 /* Make this a pack expansion type. */
17926 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17929 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17931 TREE_CHAIN (base) = bases;
17935 /* Peek at the next token. */
17936 token = cp_lexer_peek_token (parser->lexer);
17937 /* If it's not a comma, then the list is complete. */
17938 if (token->type != CPP_COMMA)
17940 /* Consume the `,'. */
17941 cp_lexer_consume_token (parser->lexer);
17944 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17945 base class had a qualified name. However, the next name that
17946 appears is certainly not qualified. */
17947 parser->scope = NULL_TREE;
17948 parser->qualifying_scope = NULL_TREE;
17949 parser->object_scope = NULL_TREE;
17951 return nreverse (bases);
17954 /* Parse a base-specifier.
17957 :: [opt] nested-name-specifier [opt] class-name
17958 virtual access-specifier [opt] :: [opt] nested-name-specifier
17960 access-specifier virtual [opt] :: [opt] nested-name-specifier
17963 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17964 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17965 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17966 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17969 cp_parser_base_specifier (cp_parser* parser)
17973 bool virtual_p = false;
17974 bool duplicate_virtual_error_issued_p = false;
17975 bool duplicate_access_error_issued_p = false;
17976 bool class_scope_p, template_p;
17977 tree access = access_default_node;
17980 /* Process the optional `virtual' and `access-specifier'. */
17983 /* Peek at the next token. */
17984 token = cp_lexer_peek_token (parser->lexer);
17985 /* Process `virtual'. */
17986 switch (token->keyword)
17989 /* If `virtual' appears more than once, issue an error. */
17990 if (virtual_p && !duplicate_virtual_error_issued_p)
17992 cp_parser_error (parser,
17993 "%<virtual%> specified more than once in base-specified");
17994 duplicate_virtual_error_issued_p = true;
17999 /* Consume the `virtual' token. */
18000 cp_lexer_consume_token (parser->lexer);
18005 case RID_PROTECTED:
18007 /* If more than one access specifier appears, issue an
18009 if (access != access_default_node
18010 && !duplicate_access_error_issued_p)
18012 cp_parser_error (parser,
18013 "more than one access specifier in base-specified");
18014 duplicate_access_error_issued_p = true;
18017 access = ridpointers[(int) token->keyword];
18019 /* Consume the access-specifier. */
18020 cp_lexer_consume_token (parser->lexer);
18029 /* It is not uncommon to see programs mechanically, erroneously, use
18030 the 'typename' keyword to denote (dependent) qualified types
18031 as base classes. */
18032 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18034 token = cp_lexer_peek_token (parser->lexer);
18035 if (!processing_template_decl)
18036 error_at (token->location,
18037 "keyword %<typename%> not allowed outside of templates");
18039 error_at (token->location,
18040 "keyword %<typename%> not allowed in this context "
18041 "(the base class is implicitly a type)");
18042 cp_lexer_consume_token (parser->lexer);
18045 /* Look for the optional `::' operator. */
18046 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18047 /* Look for the nested-name-specifier. The simplest way to
18052 The keyword `typename' is not permitted in a base-specifier or
18053 mem-initializer; in these contexts a qualified name that
18054 depends on a template-parameter is implicitly assumed to be a
18057 is to pretend that we have seen the `typename' keyword at this
18059 cp_parser_nested_name_specifier_opt (parser,
18060 /*typename_keyword_p=*/true,
18061 /*check_dependency_p=*/true,
18063 /*is_declaration=*/true);
18064 /* If the base class is given by a qualified name, assume that names
18065 we see are type names or templates, as appropriate. */
18066 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18067 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18069 /* Finally, look for the class-name. */
18070 type = cp_parser_class_name (parser,
18074 /*check_dependency_p=*/true,
18075 /*class_head_p=*/false,
18076 /*is_declaration=*/true);
18078 if (type == error_mark_node)
18079 return error_mark_node;
18081 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18084 /* Exception handling [gram.exception] */
18086 /* Parse an (optional) exception-specification.
18088 exception-specification:
18089 throw ( type-id-list [opt] )
18091 Returns a TREE_LIST representing the exception-specification. The
18092 TREE_VALUE of each node is a type. */
18095 cp_parser_exception_specification_opt (cp_parser* parser)
18099 const char *saved_message;
18101 /* Peek at the next token. */
18102 token = cp_lexer_peek_token (parser->lexer);
18104 /* Is it a noexcept-specification? */
18105 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18108 cp_lexer_consume_token (parser->lexer);
18110 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18112 cp_lexer_consume_token (parser->lexer);
18114 /* Types may not be defined in an exception-specification. */
18115 saved_message = parser->type_definition_forbidden_message;
18116 parser->type_definition_forbidden_message
18117 = G_("types may not be defined in an exception-specification");
18119 expr = cp_parser_constant_expression (parser, false, NULL);
18121 /* Restore the saved message. */
18122 parser->type_definition_forbidden_message = saved_message;
18124 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18127 expr = boolean_true_node;
18129 return build_noexcept_spec (expr, tf_warning_or_error);
18132 /* If it's not `throw', then there's no exception-specification. */
18133 if (!cp_parser_is_keyword (token, RID_THROW))
18137 /* Enable this once a lot of code has transitioned to noexcept? */
18138 if (cxx_dialect == cxx0x && !in_system_header)
18139 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18140 "deprecated in C++0x; use %<noexcept%> instead.");
18143 /* Consume the `throw'. */
18144 cp_lexer_consume_token (parser->lexer);
18146 /* Look for the `('. */
18147 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18149 /* Peek at the next token. */
18150 token = cp_lexer_peek_token (parser->lexer);
18151 /* If it's not a `)', then there is a type-id-list. */
18152 if (token->type != CPP_CLOSE_PAREN)
18154 /* Types may not be defined in an exception-specification. */
18155 saved_message = parser->type_definition_forbidden_message;
18156 parser->type_definition_forbidden_message
18157 = G_("types may not be defined in an exception-specification");
18158 /* Parse the type-id-list. */
18159 type_id_list = cp_parser_type_id_list (parser);
18160 /* Restore the saved message. */
18161 parser->type_definition_forbidden_message = saved_message;
18164 type_id_list = empty_except_spec;
18166 /* Look for the `)'. */
18167 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18169 return type_id_list;
18172 /* Parse an (optional) type-id-list.
18176 type-id-list , type-id ... [opt]
18178 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18179 in the order that the types were presented. */
18182 cp_parser_type_id_list (cp_parser* parser)
18184 tree types = NULL_TREE;
18191 /* Get the next type-id. */
18192 type = cp_parser_type_id (parser);
18193 /* Parse the optional ellipsis. */
18194 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18196 /* Consume the `...'. */
18197 cp_lexer_consume_token (parser->lexer);
18199 /* Turn the type into a pack expansion expression. */
18200 type = make_pack_expansion (type);
18202 /* Add it to the list. */
18203 types = add_exception_specifier (types, type, /*complain=*/1);
18204 /* Peek at the next token. */
18205 token = cp_lexer_peek_token (parser->lexer);
18206 /* If it is not a `,', we are done. */
18207 if (token->type != CPP_COMMA)
18209 /* Consume the `,'. */
18210 cp_lexer_consume_token (parser->lexer);
18213 return nreverse (types);
18216 /* Parse a try-block.
18219 try compound-statement handler-seq */
18222 cp_parser_try_block (cp_parser* parser)
18226 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18227 try_block = begin_try_block ();
18228 cp_parser_compound_statement (parser, NULL, true);
18229 finish_try_block (try_block);
18230 cp_parser_handler_seq (parser);
18231 finish_handler_sequence (try_block);
18236 /* Parse a function-try-block.
18238 function-try-block:
18239 try ctor-initializer [opt] function-body handler-seq */
18242 cp_parser_function_try_block (cp_parser* parser)
18244 tree compound_stmt;
18246 bool ctor_initializer_p;
18248 /* Look for the `try' keyword. */
18249 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18251 /* Let the rest of the front end know where we are. */
18252 try_block = begin_function_try_block (&compound_stmt);
18253 /* Parse the function-body. */
18255 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18256 /* We're done with the `try' part. */
18257 finish_function_try_block (try_block);
18258 /* Parse the handlers. */
18259 cp_parser_handler_seq (parser);
18260 /* We're done with the handlers. */
18261 finish_function_handler_sequence (try_block, compound_stmt);
18263 return ctor_initializer_p;
18266 /* Parse a handler-seq.
18269 handler handler-seq [opt] */
18272 cp_parser_handler_seq (cp_parser* parser)
18278 /* Parse the handler. */
18279 cp_parser_handler (parser);
18280 /* Peek at the next token. */
18281 token = cp_lexer_peek_token (parser->lexer);
18282 /* If it's not `catch' then there are no more handlers. */
18283 if (!cp_parser_is_keyword (token, RID_CATCH))
18288 /* Parse a handler.
18291 catch ( exception-declaration ) compound-statement */
18294 cp_parser_handler (cp_parser* parser)
18299 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18300 handler = begin_handler ();
18301 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18302 declaration = cp_parser_exception_declaration (parser);
18303 finish_handler_parms (declaration, handler);
18304 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18305 cp_parser_compound_statement (parser, NULL, false);
18306 finish_handler (handler);
18309 /* Parse an exception-declaration.
18311 exception-declaration:
18312 type-specifier-seq declarator
18313 type-specifier-seq abstract-declarator
18317 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18318 ellipsis variant is used. */
18321 cp_parser_exception_declaration (cp_parser* parser)
18323 cp_decl_specifier_seq type_specifiers;
18324 cp_declarator *declarator;
18325 const char *saved_message;
18327 /* If it's an ellipsis, it's easy to handle. */
18328 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18330 /* Consume the `...' token. */
18331 cp_lexer_consume_token (parser->lexer);
18335 /* Types may not be defined in exception-declarations. */
18336 saved_message = parser->type_definition_forbidden_message;
18337 parser->type_definition_forbidden_message
18338 = G_("types may not be defined in exception-declarations");
18340 /* Parse the type-specifier-seq. */
18341 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18342 /*is_trailing_return=*/false,
18344 /* If it's a `)', then there is no declarator. */
18345 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18348 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18349 /*ctor_dtor_or_conv_p=*/NULL,
18350 /*parenthesized_p=*/NULL,
18351 /*member_p=*/false);
18353 /* Restore the saved message. */
18354 parser->type_definition_forbidden_message = saved_message;
18356 if (!type_specifiers.any_specifiers_p)
18357 return error_mark_node;
18359 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18362 /* Parse a throw-expression.
18365 throw assignment-expression [opt]
18367 Returns a THROW_EXPR representing the throw-expression. */
18370 cp_parser_throw_expression (cp_parser* parser)
18375 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18376 token = cp_lexer_peek_token (parser->lexer);
18377 /* Figure out whether or not there is an assignment-expression
18378 following the "throw" keyword. */
18379 if (token->type == CPP_COMMA
18380 || token->type == CPP_SEMICOLON
18381 || token->type == CPP_CLOSE_PAREN
18382 || token->type == CPP_CLOSE_SQUARE
18383 || token->type == CPP_CLOSE_BRACE
18384 || token->type == CPP_COLON)
18385 expression = NULL_TREE;
18387 expression = cp_parser_assignment_expression (parser,
18388 /*cast_p=*/false, NULL);
18390 return build_throw (expression);
18393 /* GNU Extensions */
18395 /* Parse an (optional) asm-specification.
18398 asm ( string-literal )
18400 If the asm-specification is present, returns a STRING_CST
18401 corresponding to the string-literal. Otherwise, returns
18405 cp_parser_asm_specification_opt (cp_parser* parser)
18408 tree asm_specification;
18410 /* Peek at the next token. */
18411 token = cp_lexer_peek_token (parser->lexer);
18412 /* If the next token isn't the `asm' keyword, then there's no
18413 asm-specification. */
18414 if (!cp_parser_is_keyword (token, RID_ASM))
18417 /* Consume the `asm' token. */
18418 cp_lexer_consume_token (parser->lexer);
18419 /* Look for the `('. */
18420 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18422 /* Look for the string-literal. */
18423 asm_specification = cp_parser_string_literal (parser, false, false);
18425 /* Look for the `)'. */
18426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18428 return asm_specification;
18431 /* Parse an asm-operand-list.
18435 asm-operand-list , asm-operand
18438 string-literal ( expression )
18439 [ string-literal ] string-literal ( expression )
18441 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18442 each node is the expression. The TREE_PURPOSE is itself a
18443 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18444 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18445 is a STRING_CST for the string literal before the parenthesis. Returns
18446 ERROR_MARK_NODE if any of the operands are invalid. */
18449 cp_parser_asm_operand_list (cp_parser* parser)
18451 tree asm_operands = NULL_TREE;
18452 bool invalid_operands = false;
18456 tree string_literal;
18460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18462 /* Consume the `[' token. */
18463 cp_lexer_consume_token (parser->lexer);
18464 /* Read the operand name. */
18465 name = cp_parser_identifier (parser);
18466 if (name != error_mark_node)
18467 name = build_string (IDENTIFIER_LENGTH (name),
18468 IDENTIFIER_POINTER (name));
18469 /* Look for the closing `]'. */
18470 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18474 /* Look for the string-literal. */
18475 string_literal = cp_parser_string_literal (parser, false, false);
18477 /* Look for the `('. */
18478 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18479 /* Parse the expression. */
18480 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18481 /* Look for the `)'. */
18482 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18484 if (name == error_mark_node
18485 || string_literal == error_mark_node
18486 || expression == error_mark_node)
18487 invalid_operands = true;
18489 /* Add this operand to the list. */
18490 asm_operands = tree_cons (build_tree_list (name, string_literal),
18493 /* If the next token is not a `,', there are no more
18495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18497 /* Consume the `,'. */
18498 cp_lexer_consume_token (parser->lexer);
18501 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18504 /* Parse an asm-clobber-list.
18508 asm-clobber-list , string-literal
18510 Returns a TREE_LIST, indicating the clobbers in the order that they
18511 appeared. The TREE_VALUE of each node is a STRING_CST. */
18514 cp_parser_asm_clobber_list (cp_parser* parser)
18516 tree clobbers = NULL_TREE;
18520 tree string_literal;
18522 /* Look for the string literal. */
18523 string_literal = cp_parser_string_literal (parser, false, false);
18524 /* Add it to the list. */
18525 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18526 /* If the next token is not a `,', then the list is
18528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18530 /* Consume the `,' token. */
18531 cp_lexer_consume_token (parser->lexer);
18537 /* Parse an asm-label-list.
18541 asm-label-list , identifier
18543 Returns a TREE_LIST, indicating the labels in the order that they
18544 appeared. The TREE_VALUE of each node is a label. */
18547 cp_parser_asm_label_list (cp_parser* parser)
18549 tree labels = NULL_TREE;
18553 tree identifier, label, name;
18555 /* Look for the identifier. */
18556 identifier = cp_parser_identifier (parser);
18557 if (!error_operand_p (identifier))
18559 label = lookup_label (identifier);
18560 if (TREE_CODE (label) == LABEL_DECL)
18562 TREE_USED (label) = 1;
18563 check_goto (label);
18564 name = build_string (IDENTIFIER_LENGTH (identifier),
18565 IDENTIFIER_POINTER (identifier));
18566 labels = tree_cons (name, label, labels);
18569 /* If the next token is not a `,', then the list is
18571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18573 /* Consume the `,' token. */
18574 cp_lexer_consume_token (parser->lexer);
18577 return nreverse (labels);
18580 /* Parse an (optional) series of attributes.
18583 attributes attribute
18586 __attribute__ (( attribute-list [opt] ))
18588 The return value is as for cp_parser_attribute_list. */
18591 cp_parser_attributes_opt (cp_parser* parser)
18593 tree attributes = NULL_TREE;
18598 tree attribute_list;
18600 /* Peek at the next token. */
18601 token = cp_lexer_peek_token (parser->lexer);
18602 /* If it's not `__attribute__', then we're done. */
18603 if (token->keyword != RID_ATTRIBUTE)
18606 /* Consume the `__attribute__' keyword. */
18607 cp_lexer_consume_token (parser->lexer);
18608 /* Look for the two `(' tokens. */
18609 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18610 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18612 /* Peek at the next token. */
18613 token = cp_lexer_peek_token (parser->lexer);
18614 if (token->type != CPP_CLOSE_PAREN)
18615 /* Parse the attribute-list. */
18616 attribute_list = cp_parser_attribute_list (parser);
18618 /* If the next token is a `)', then there is no attribute
18620 attribute_list = NULL;
18622 /* Look for the two `)' tokens. */
18623 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18624 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18626 /* Add these new attributes to the list. */
18627 attributes = chainon (attributes, attribute_list);
18633 /* Parse an attribute-list.
18637 attribute-list , attribute
18641 identifier ( identifier )
18642 identifier ( identifier , expression-list )
18643 identifier ( expression-list )
18645 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18646 to an attribute. The TREE_PURPOSE of each node is the identifier
18647 indicating which attribute is in use. The TREE_VALUE represents
18648 the arguments, if any. */
18651 cp_parser_attribute_list (cp_parser* parser)
18653 tree attribute_list = NULL_TREE;
18654 bool save_translate_strings_p = parser->translate_strings_p;
18656 parser->translate_strings_p = false;
18663 /* Look for the identifier. We also allow keywords here; for
18664 example `__attribute__ ((const))' is legal. */
18665 token = cp_lexer_peek_token (parser->lexer);
18666 if (token->type == CPP_NAME
18667 || token->type == CPP_KEYWORD)
18669 tree arguments = NULL_TREE;
18671 /* Consume the token. */
18672 token = cp_lexer_consume_token (parser->lexer);
18674 /* Save away the identifier that indicates which attribute
18676 identifier = (token->type == CPP_KEYWORD)
18677 /* For keywords, use the canonical spelling, not the
18678 parsed identifier. */
18679 ? ridpointers[(int) token->keyword]
18682 attribute = build_tree_list (identifier, NULL_TREE);
18684 /* Peek at the next token. */
18685 token = cp_lexer_peek_token (parser->lexer);
18686 /* If it's an `(', then parse the attribute arguments. */
18687 if (token->type == CPP_OPEN_PAREN)
18690 int attr_flag = (attribute_takes_identifier_p (identifier)
18691 ? id_attr : normal_attr);
18692 vec = cp_parser_parenthesized_expression_list
18693 (parser, attr_flag, /*cast_p=*/false,
18694 /*allow_expansion_p=*/false,
18695 /*non_constant_p=*/NULL);
18697 arguments = error_mark_node;
18700 arguments = build_tree_list_vec (vec);
18701 release_tree_vector (vec);
18703 /* Save the arguments away. */
18704 TREE_VALUE (attribute) = arguments;
18707 if (arguments != error_mark_node)
18709 /* Add this attribute to the list. */
18710 TREE_CHAIN (attribute) = attribute_list;
18711 attribute_list = attribute;
18714 token = cp_lexer_peek_token (parser->lexer);
18716 /* Now, look for more attributes. If the next token isn't a
18717 `,', we're done. */
18718 if (token->type != CPP_COMMA)
18721 /* Consume the comma and keep going. */
18722 cp_lexer_consume_token (parser->lexer);
18724 parser->translate_strings_p = save_translate_strings_p;
18726 /* We built up the list in reverse order. */
18727 return nreverse (attribute_list);
18730 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18731 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18732 current value of the PEDANTIC flag, regardless of whether or not
18733 the `__extension__' keyword is present. The caller is responsible
18734 for restoring the value of the PEDANTIC flag. */
18737 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18739 /* Save the old value of the PEDANTIC flag. */
18740 *saved_pedantic = pedantic;
18742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18744 /* Consume the `__extension__' token. */
18745 cp_lexer_consume_token (parser->lexer);
18746 /* We're not being pedantic while the `__extension__' keyword is
18756 /* Parse a label declaration.
18759 __label__ label-declarator-seq ;
18761 label-declarator-seq:
18762 identifier , label-declarator-seq
18766 cp_parser_label_declaration (cp_parser* parser)
18768 /* Look for the `__label__' keyword. */
18769 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18775 /* Look for an identifier. */
18776 identifier = cp_parser_identifier (parser);
18777 /* If we failed, stop. */
18778 if (identifier == error_mark_node)
18780 /* Declare it as a label. */
18781 finish_label_decl (identifier);
18782 /* If the next token is a `;', stop. */
18783 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18785 /* Look for the `,' separating the label declarations. */
18786 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18789 /* Look for the final `;'. */
18790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18793 /* Support Functions */
18795 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18796 NAME should have one of the representations used for an
18797 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18798 is returned. If PARSER->SCOPE is a dependent type, then a
18799 SCOPE_REF is returned.
18801 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18802 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18803 was formed. Abstractly, such entities should not be passed to this
18804 function, because they do not need to be looked up, but it is
18805 simpler to check for this special case here, rather than at the
18808 In cases not explicitly covered above, this function returns a
18809 DECL, OVERLOAD, or baselink representing the result of the lookup.
18810 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18813 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18814 (e.g., "struct") that was used. In that case bindings that do not
18815 refer to types are ignored.
18817 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18820 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18823 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18826 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18827 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18828 NULL_TREE otherwise. */
18831 cp_parser_lookup_name (cp_parser *parser, tree name,
18832 enum tag_types tag_type,
18835 bool check_dependency,
18836 tree *ambiguous_decls,
18837 location_t name_location)
18841 tree object_type = parser->context->object_type;
18843 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18844 flags |= LOOKUP_COMPLAIN;
18846 /* Assume that the lookup will be unambiguous. */
18847 if (ambiguous_decls)
18848 *ambiguous_decls = NULL_TREE;
18850 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18851 no longer valid. Note that if we are parsing tentatively, and
18852 the parse fails, OBJECT_TYPE will be automatically restored. */
18853 parser->context->object_type = NULL_TREE;
18855 if (name == error_mark_node)
18856 return error_mark_node;
18858 /* A template-id has already been resolved; there is no lookup to
18860 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18862 if (BASELINK_P (name))
18864 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18865 == TEMPLATE_ID_EXPR);
18869 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18870 it should already have been checked to make sure that the name
18871 used matches the type being destroyed. */
18872 if (TREE_CODE (name) == BIT_NOT_EXPR)
18876 /* Figure out to which type this destructor applies. */
18878 type = parser->scope;
18879 else if (object_type)
18880 type = object_type;
18882 type = current_class_type;
18883 /* If that's not a class type, there is no destructor. */
18884 if (!type || !CLASS_TYPE_P (type))
18885 return error_mark_node;
18886 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18887 lazily_declare_fn (sfk_destructor, type);
18888 if (!CLASSTYPE_DESTRUCTORS (type))
18889 return error_mark_node;
18890 /* If it was a class type, return the destructor. */
18891 return CLASSTYPE_DESTRUCTORS (type);
18894 /* By this point, the NAME should be an ordinary identifier. If
18895 the id-expression was a qualified name, the qualifying scope is
18896 stored in PARSER->SCOPE at this point. */
18897 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18899 /* Perform the lookup. */
18904 if (parser->scope == error_mark_node)
18905 return error_mark_node;
18907 /* If the SCOPE is dependent, the lookup must be deferred until
18908 the template is instantiated -- unless we are explicitly
18909 looking up names in uninstantiated templates. Even then, we
18910 cannot look up the name if the scope is not a class type; it
18911 might, for example, be a template type parameter. */
18912 dependent_p = (TYPE_P (parser->scope)
18913 && dependent_scope_p (parser->scope));
18914 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18916 /* Defer lookup. */
18917 decl = error_mark_node;
18920 tree pushed_scope = NULL_TREE;
18922 /* If PARSER->SCOPE is a dependent type, then it must be a
18923 class type, and we must not be checking dependencies;
18924 otherwise, we would have processed this lookup above. So
18925 that PARSER->SCOPE is not considered a dependent base by
18926 lookup_member, we must enter the scope here. */
18928 pushed_scope = push_scope (parser->scope);
18930 /* If the PARSER->SCOPE is a template specialization, it
18931 may be instantiated during name lookup. In that case,
18932 errors may be issued. Even if we rollback the current
18933 tentative parse, those errors are valid. */
18934 decl = lookup_qualified_name (parser->scope, name,
18935 tag_type != none_type,
18936 /*complain=*/true);
18938 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18939 lookup result and the nested-name-specifier nominates a class C:
18940 * if the name specified after the nested-name-specifier, when
18941 looked up in C, is the injected-class-name of C (Clause 9), or
18942 * if the name specified after the nested-name-specifier is the
18943 same as the identifier or the simple-template-id's template-
18944 name in the last component of the nested-name-specifier,
18945 the name is instead considered to name the constructor of
18946 class C. [ Note: for example, the constructor is not an
18947 acceptable lookup result in an elaborated-type-specifier so
18948 the constructor would not be used in place of the
18949 injected-class-name. --end note ] Such a constructor name
18950 shall be used only in the declarator-id of a declaration that
18951 names a constructor or in a using-declaration. */
18952 if (tag_type == none_type
18953 && DECL_SELF_REFERENCE_P (decl)
18954 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18955 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18956 tag_type != none_type,
18957 /*complain=*/true);
18959 /* If we have a single function from a using decl, pull it out. */
18960 if (TREE_CODE (decl) == OVERLOAD
18961 && !really_overloaded_fn (decl))
18962 decl = OVL_FUNCTION (decl);
18965 pop_scope (pushed_scope);
18968 /* If the scope is a dependent type and either we deferred lookup or
18969 we did lookup but didn't find the name, rememeber the name. */
18970 if (decl == error_mark_node && TYPE_P (parser->scope)
18971 && dependent_type_p (parser->scope))
18977 /* The resolution to Core Issue 180 says that `struct
18978 A::B' should be considered a type-name, even if `A'
18980 type = make_typename_type (parser->scope, name, tag_type,
18981 /*complain=*/tf_error);
18982 decl = TYPE_NAME (type);
18984 else if (is_template
18985 && (cp_parser_next_token_ends_template_argument_p (parser)
18986 || cp_lexer_next_token_is (parser->lexer,
18988 decl = make_unbound_class_template (parser->scope,
18990 /*complain=*/tf_error);
18992 decl = build_qualified_name (/*type=*/NULL_TREE,
18993 parser->scope, name,
18996 parser->qualifying_scope = parser->scope;
18997 parser->object_scope = NULL_TREE;
18999 else if (object_type)
19001 tree object_decl = NULL_TREE;
19002 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19003 OBJECT_TYPE is not a class. */
19004 if (CLASS_TYPE_P (object_type))
19005 /* If the OBJECT_TYPE is a template specialization, it may
19006 be instantiated during name lookup. In that case, errors
19007 may be issued. Even if we rollback the current tentative
19008 parse, those errors are valid. */
19009 object_decl = lookup_member (object_type,
19012 tag_type != none_type);
19013 /* Look it up in the enclosing context, too. */
19014 decl = lookup_name_real (name, tag_type != none_type,
19016 /*block_p=*/true, is_namespace, flags);
19017 parser->object_scope = object_type;
19018 parser->qualifying_scope = NULL_TREE;
19020 decl = object_decl;
19024 decl = lookup_name_real (name, tag_type != none_type,
19026 /*block_p=*/true, is_namespace, flags);
19027 parser->qualifying_scope = NULL_TREE;
19028 parser->object_scope = NULL_TREE;
19031 /* If the lookup failed, let our caller know. */
19032 if (!decl || decl == error_mark_node)
19033 return error_mark_node;
19035 /* Pull out the template from an injected-class-name (or multiple). */
19037 decl = maybe_get_template_decl_from_type_decl (decl);
19039 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19040 if (TREE_CODE (decl) == TREE_LIST)
19042 if (ambiguous_decls)
19043 *ambiguous_decls = decl;
19044 /* The error message we have to print is too complicated for
19045 cp_parser_error, so we incorporate its actions directly. */
19046 if (!cp_parser_simulate_error (parser))
19048 error_at (name_location, "reference to %qD is ambiguous",
19050 print_candidates (decl);
19052 return error_mark_node;
19055 gcc_assert (DECL_P (decl)
19056 || TREE_CODE (decl) == OVERLOAD
19057 || TREE_CODE (decl) == SCOPE_REF
19058 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19059 || BASELINK_P (decl));
19061 /* If we have resolved the name of a member declaration, check to
19062 see if the declaration is accessible. When the name resolves to
19063 set of overloaded functions, accessibility is checked when
19064 overload resolution is done.
19066 During an explicit instantiation, access is not checked at all,
19067 as per [temp.explicit]. */
19069 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19074 /* Like cp_parser_lookup_name, but for use in the typical case where
19075 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19076 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19079 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19081 return cp_parser_lookup_name (parser, name,
19083 /*is_template=*/false,
19084 /*is_namespace=*/false,
19085 /*check_dependency=*/true,
19086 /*ambiguous_decls=*/NULL,
19090 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19091 the current context, return the TYPE_DECL. If TAG_NAME_P is
19092 true, the DECL indicates the class being defined in a class-head,
19093 or declared in an elaborated-type-specifier.
19095 Otherwise, return DECL. */
19098 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19100 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19101 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19104 template <typename T> struct B;
19107 template <typename T> struct A::B {};
19109 Similarly, in an elaborated-type-specifier:
19111 namespace N { struct X{}; }
19114 template <typename T> friend struct N::X;
19117 However, if the DECL refers to a class type, and we are in
19118 the scope of the class, then the name lookup automatically
19119 finds the TYPE_DECL created by build_self_reference rather
19120 than a TEMPLATE_DECL. For example, in:
19122 template <class T> struct S {
19126 there is no need to handle such case. */
19128 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19129 return DECL_TEMPLATE_RESULT (decl);
19134 /* If too many, or too few, template-parameter lists apply to the
19135 declarator, issue an error message. Returns TRUE if all went well,
19136 and FALSE otherwise. */
19139 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19140 cp_declarator *declarator,
19141 location_t declarator_location)
19143 unsigned num_templates;
19145 /* We haven't seen any classes that involve template parameters yet. */
19148 switch (declarator->kind)
19151 if (declarator->u.id.qualifying_scope)
19155 scope = declarator->u.id.qualifying_scope;
19157 while (scope && CLASS_TYPE_P (scope))
19159 /* You're supposed to have one `template <...>'
19160 for every template class, but you don't need one
19161 for a full specialization. For example:
19163 template <class T> struct S{};
19164 template <> struct S<int> { void f(); };
19165 void S<int>::f () {}
19167 is correct; there shouldn't be a `template <>' for
19168 the definition of `S<int>::f'. */
19169 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19170 /* If SCOPE does not have template information of any
19171 kind, then it is not a template, nor is it nested
19172 within a template. */
19174 if (explicit_class_specialization_p (scope))
19176 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19179 scope = TYPE_CONTEXT (scope);
19182 else if (TREE_CODE (declarator->u.id.unqualified_name)
19183 == TEMPLATE_ID_EXPR)
19184 /* If the DECLARATOR has the form `X<y>' then it uses one
19185 additional level of template parameters. */
19188 return cp_parser_check_template_parameters
19189 (parser, num_templates, declarator_location, declarator);
19195 case cdk_reference:
19197 return (cp_parser_check_declarator_template_parameters
19198 (parser, declarator->declarator, declarator_location));
19204 gcc_unreachable ();
19209 /* NUM_TEMPLATES were used in the current declaration. If that is
19210 invalid, return FALSE and issue an error messages. Otherwise,
19211 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19212 declarator and we can print more accurate diagnostics. */
19215 cp_parser_check_template_parameters (cp_parser* parser,
19216 unsigned num_templates,
19217 location_t location,
19218 cp_declarator *declarator)
19220 /* If there are the same number of template classes and parameter
19221 lists, that's OK. */
19222 if (parser->num_template_parameter_lists == num_templates)
19224 /* If there are more, but only one more, then we are referring to a
19225 member template. That's OK too. */
19226 if (parser->num_template_parameter_lists == num_templates + 1)
19228 /* If there are more template classes than parameter lists, we have
19231 template <class T> void S<T>::R<T>::f (); */
19232 if (parser->num_template_parameter_lists < num_templates)
19234 if (declarator && !current_function_decl)
19235 error_at (location, "specializing member %<%T::%E%> "
19236 "requires %<template<>%> syntax",
19237 declarator->u.id.qualifying_scope,
19238 declarator->u.id.unqualified_name);
19239 else if (declarator)
19240 error_at (location, "invalid declaration of %<%T::%E%>",
19241 declarator->u.id.qualifying_scope,
19242 declarator->u.id.unqualified_name);
19244 error_at (location, "too few template-parameter-lists");
19247 /* Otherwise, there are too many template parameter lists. We have
19250 template <class T> template <class U> void S::f(); */
19251 error_at (location, "too many template-parameter-lists");
19255 /* Parse an optional `::' token indicating that the following name is
19256 from the global namespace. If so, PARSER->SCOPE is set to the
19257 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19258 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19259 Returns the new value of PARSER->SCOPE, if the `::' token is
19260 present, and NULL_TREE otherwise. */
19263 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19267 /* Peek at the next token. */
19268 token = cp_lexer_peek_token (parser->lexer);
19269 /* If we're looking at a `::' token then we're starting from the
19270 global namespace, not our current location. */
19271 if (token->type == CPP_SCOPE)
19273 /* Consume the `::' token. */
19274 cp_lexer_consume_token (parser->lexer);
19275 /* Set the SCOPE so that we know where to start the lookup. */
19276 parser->scope = global_namespace;
19277 parser->qualifying_scope = global_namespace;
19278 parser->object_scope = NULL_TREE;
19280 return parser->scope;
19282 else if (!current_scope_valid_p)
19284 parser->scope = NULL_TREE;
19285 parser->qualifying_scope = NULL_TREE;
19286 parser->object_scope = NULL_TREE;
19292 /* Returns TRUE if the upcoming token sequence is the start of a
19293 constructor declarator. If FRIEND_P is true, the declarator is
19294 preceded by the `friend' specifier. */
19297 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19299 bool constructor_p;
19300 tree nested_name_specifier;
19301 cp_token *next_token;
19303 /* The common case is that this is not a constructor declarator, so
19304 try to avoid doing lots of work if at all possible. It's not
19305 valid declare a constructor at function scope. */
19306 if (parser->in_function_body)
19308 /* And only certain tokens can begin a constructor declarator. */
19309 next_token = cp_lexer_peek_token (parser->lexer);
19310 if (next_token->type != CPP_NAME
19311 && next_token->type != CPP_SCOPE
19312 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19313 && next_token->type != CPP_TEMPLATE_ID)
19316 /* Parse tentatively; we are going to roll back all of the tokens
19318 cp_parser_parse_tentatively (parser);
19319 /* Assume that we are looking at a constructor declarator. */
19320 constructor_p = true;
19322 /* Look for the optional `::' operator. */
19323 cp_parser_global_scope_opt (parser,
19324 /*current_scope_valid_p=*/false);
19325 /* Look for the nested-name-specifier. */
19326 nested_name_specifier
19327 = (cp_parser_nested_name_specifier_opt (parser,
19328 /*typename_keyword_p=*/false,
19329 /*check_dependency_p=*/false,
19331 /*is_declaration=*/false));
19332 /* Outside of a class-specifier, there must be a
19333 nested-name-specifier. */
19334 if (!nested_name_specifier &&
19335 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19337 constructor_p = false;
19338 else if (nested_name_specifier == error_mark_node)
19339 constructor_p = false;
19341 /* If we have a class scope, this is easy; DR 147 says that S::S always
19342 names the constructor, and no other qualified name could. */
19343 if (constructor_p && nested_name_specifier
19344 && TYPE_P (nested_name_specifier))
19346 tree id = cp_parser_unqualified_id (parser,
19347 /*template_keyword_p=*/false,
19348 /*check_dependency_p=*/false,
19349 /*declarator_p=*/true,
19350 /*optional_p=*/false);
19351 if (is_overloaded_fn (id))
19352 id = DECL_NAME (get_first_fn (id));
19353 if (!constructor_name_p (id, nested_name_specifier))
19354 constructor_p = false;
19356 /* If we still think that this might be a constructor-declarator,
19357 look for a class-name. */
19358 else if (constructor_p)
19362 template <typename T> struct S {
19366 we must recognize that the nested `S' names a class. */
19368 type_decl = cp_parser_class_name (parser,
19369 /*typename_keyword_p=*/false,
19370 /*template_keyword_p=*/false,
19372 /*check_dependency_p=*/false,
19373 /*class_head_p=*/false,
19374 /*is_declaration=*/false);
19375 /* If there was no class-name, then this is not a constructor. */
19376 constructor_p = !cp_parser_error_occurred (parser);
19378 /* If we're still considering a constructor, we have to see a `(',
19379 to begin the parameter-declaration-clause, followed by either a
19380 `)', an `...', or a decl-specifier. We need to check for a
19381 type-specifier to avoid being fooled into thinking that:
19385 is a constructor. (It is actually a function named `f' that
19386 takes one parameter (of type `int') and returns a value of type
19389 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19390 constructor_p = false;
19393 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19394 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19395 /* A parameter declaration begins with a decl-specifier,
19396 which is either the "attribute" keyword, a storage class
19397 specifier, or (usually) a type-specifier. */
19398 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19401 tree pushed_scope = NULL_TREE;
19402 unsigned saved_num_template_parameter_lists;
19404 /* Names appearing in the type-specifier should be looked up
19405 in the scope of the class. */
19406 if (current_class_type)
19410 type = TREE_TYPE (type_decl);
19411 if (TREE_CODE (type) == TYPENAME_TYPE)
19413 type = resolve_typename_type (type,
19414 /*only_current_p=*/false);
19415 if (TREE_CODE (type) == TYPENAME_TYPE)
19417 cp_parser_abort_tentative_parse (parser);
19421 pushed_scope = push_scope (type);
19424 /* Inside the constructor parameter list, surrounding
19425 template-parameter-lists do not apply. */
19426 saved_num_template_parameter_lists
19427 = parser->num_template_parameter_lists;
19428 parser->num_template_parameter_lists = 0;
19430 /* Look for the type-specifier. */
19431 cp_parser_type_specifier (parser,
19432 CP_PARSER_FLAGS_NONE,
19433 /*decl_specs=*/NULL,
19434 /*is_declarator=*/true,
19435 /*declares_class_or_enum=*/NULL,
19436 /*is_cv_qualifier=*/NULL);
19438 parser->num_template_parameter_lists
19439 = saved_num_template_parameter_lists;
19441 /* Leave the scope of the class. */
19443 pop_scope (pushed_scope);
19445 constructor_p = !cp_parser_error_occurred (parser);
19449 /* We did not really want to consume any tokens. */
19450 cp_parser_abort_tentative_parse (parser);
19452 return constructor_p;
19455 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19456 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19457 they must be performed once we are in the scope of the function.
19459 Returns the function defined. */
19462 cp_parser_function_definition_from_specifiers_and_declarator
19463 (cp_parser* parser,
19464 cp_decl_specifier_seq *decl_specifiers,
19466 const cp_declarator *declarator)
19471 /* Begin the function-definition. */
19472 success_p = start_function (decl_specifiers, declarator, attributes);
19474 /* The things we're about to see are not directly qualified by any
19475 template headers we've seen thus far. */
19476 reset_specialization ();
19478 /* If there were names looked up in the decl-specifier-seq that we
19479 did not check, check them now. We must wait until we are in the
19480 scope of the function to perform the checks, since the function
19481 might be a friend. */
19482 perform_deferred_access_checks ();
19486 /* Skip the entire function. */
19487 cp_parser_skip_to_end_of_block_or_statement (parser);
19488 fn = error_mark_node;
19490 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19492 /* Seen already, skip it. An error message has already been output. */
19493 cp_parser_skip_to_end_of_block_or_statement (parser);
19494 fn = current_function_decl;
19495 current_function_decl = NULL_TREE;
19496 /* If this is a function from a class, pop the nested class. */
19497 if (current_class_name)
19498 pop_nested_class ();
19501 fn = cp_parser_function_definition_after_declarator (parser,
19502 /*inline_p=*/false);
19507 /* Parse the part of a function-definition that follows the
19508 declarator. INLINE_P is TRUE iff this function is an inline
19509 function defined within a class-specifier.
19511 Returns the function defined. */
19514 cp_parser_function_definition_after_declarator (cp_parser* parser,
19518 bool ctor_initializer_p = false;
19519 bool saved_in_unbraced_linkage_specification_p;
19520 bool saved_in_function_body;
19521 unsigned saved_num_template_parameter_lists;
19524 saved_in_function_body = parser->in_function_body;
19525 parser->in_function_body = true;
19526 /* If the next token is `return', then the code may be trying to
19527 make use of the "named return value" extension that G++ used to
19529 token = cp_lexer_peek_token (parser->lexer);
19530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19532 /* Consume the `return' keyword. */
19533 cp_lexer_consume_token (parser->lexer);
19534 /* Look for the identifier that indicates what value is to be
19536 cp_parser_identifier (parser);
19537 /* Issue an error message. */
19538 error_at (token->location,
19539 "named return values are no longer supported");
19540 /* Skip tokens until we reach the start of the function body. */
19543 cp_token *token = cp_lexer_peek_token (parser->lexer);
19544 if (token->type == CPP_OPEN_BRACE
19545 || token->type == CPP_EOF
19546 || token->type == CPP_PRAGMA_EOL)
19548 cp_lexer_consume_token (parser->lexer);
19551 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19552 anything declared inside `f'. */
19553 saved_in_unbraced_linkage_specification_p
19554 = parser->in_unbraced_linkage_specification_p;
19555 parser->in_unbraced_linkage_specification_p = false;
19556 /* Inside the function, surrounding template-parameter-lists do not
19558 saved_num_template_parameter_lists
19559 = parser->num_template_parameter_lists;
19560 parser->num_template_parameter_lists = 0;
19562 start_lambda_scope (current_function_decl);
19564 /* If the next token is `try', then we are looking at a
19565 function-try-block. */
19566 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19567 ctor_initializer_p = cp_parser_function_try_block (parser);
19568 /* A function-try-block includes the function-body, so we only do
19569 this next part if we're not processing a function-try-block. */
19572 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19574 finish_lambda_scope ();
19576 /* Finish the function. */
19577 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19578 (inline_p ? 2 : 0));
19579 /* Generate code for it, if necessary. */
19580 expand_or_defer_fn (fn);
19581 /* Restore the saved values. */
19582 parser->in_unbraced_linkage_specification_p
19583 = saved_in_unbraced_linkage_specification_p;
19584 parser->num_template_parameter_lists
19585 = saved_num_template_parameter_lists;
19586 parser->in_function_body = saved_in_function_body;
19591 /* Parse a template-declaration, assuming that the `export' (and
19592 `extern') keywords, if present, has already been scanned. MEMBER_P
19593 is as for cp_parser_template_declaration. */
19596 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19598 tree decl = NULL_TREE;
19599 VEC (deferred_access_check,gc) *checks;
19600 tree parameter_list;
19601 bool friend_p = false;
19602 bool need_lang_pop;
19605 /* Look for the `template' keyword. */
19606 token = cp_lexer_peek_token (parser->lexer);
19607 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19611 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19613 if (at_class_scope_p () && current_function_decl)
19615 /* 14.5.2.2 [temp.mem]
19617 A local class shall not have member templates. */
19618 error_at (token->location,
19619 "invalid declaration of member template in local class");
19620 cp_parser_skip_to_end_of_block_or_statement (parser);
19625 A template ... shall not have C linkage. */
19626 if (current_lang_name == lang_name_c)
19628 error_at (token->location, "template with C linkage");
19629 /* Give it C++ linkage to avoid confusing other parts of the
19631 push_lang_context (lang_name_cplusplus);
19632 need_lang_pop = true;
19635 need_lang_pop = false;
19637 /* We cannot perform access checks on the template parameter
19638 declarations until we know what is being declared, just as we
19639 cannot check the decl-specifier list. */
19640 push_deferring_access_checks (dk_deferred);
19642 /* If the next token is `>', then we have an invalid
19643 specialization. Rather than complain about an invalid template
19644 parameter, issue an error message here. */
19645 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19647 cp_parser_error (parser, "invalid explicit specialization");
19648 begin_specialization ();
19649 parameter_list = NULL_TREE;
19652 /* Parse the template parameters. */
19653 parameter_list = cp_parser_template_parameter_list (parser);
19655 /* Get the deferred access checks from the parameter list. These
19656 will be checked once we know what is being declared, as for a
19657 member template the checks must be performed in the scope of the
19658 class containing the member. */
19659 checks = get_deferred_access_checks ();
19661 /* Look for the `>'. */
19662 cp_parser_skip_to_end_of_template_parameter_list (parser);
19663 /* We just processed one more parameter list. */
19664 ++parser->num_template_parameter_lists;
19665 /* If the next token is `template', there are more template
19667 if (cp_lexer_next_token_is_keyword (parser->lexer,
19669 cp_parser_template_declaration_after_export (parser, member_p);
19672 /* There are no access checks when parsing a template, as we do not
19673 know if a specialization will be a friend. */
19674 push_deferring_access_checks (dk_no_check);
19675 token = cp_lexer_peek_token (parser->lexer);
19676 decl = cp_parser_single_declaration (parser,
19679 /*explicit_specialization_p=*/false,
19681 pop_deferring_access_checks ();
19683 /* If this is a member template declaration, let the front
19685 if (member_p && !friend_p && decl)
19687 if (TREE_CODE (decl) == TYPE_DECL)
19688 cp_parser_check_access_in_redeclaration (decl, token->location);
19690 decl = finish_member_template_decl (decl);
19692 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19693 make_friend_class (current_class_type, TREE_TYPE (decl),
19694 /*complain=*/true);
19696 /* We are done with the current parameter list. */
19697 --parser->num_template_parameter_lists;
19699 pop_deferring_access_checks ();
19702 finish_template_decl (parameter_list);
19704 /* Register member declarations. */
19705 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19706 finish_member_declaration (decl);
19707 /* For the erroneous case of a template with C linkage, we pushed an
19708 implicit C++ linkage scope; exit that scope now. */
19710 pop_lang_context ();
19711 /* If DECL is a function template, we must return to parse it later.
19712 (Even though there is no definition, there might be default
19713 arguments that need handling.) */
19714 if (member_p && decl
19715 && (TREE_CODE (decl) == FUNCTION_DECL
19716 || DECL_FUNCTION_TEMPLATE_P (decl)))
19717 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19720 /* Perform the deferred access checks from a template-parameter-list.
19721 CHECKS is a TREE_LIST of access checks, as returned by
19722 get_deferred_access_checks. */
19725 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19727 ++processing_template_parmlist;
19728 perform_access_checks (checks);
19729 --processing_template_parmlist;
19732 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19733 `function-definition' sequence. MEMBER_P is true, this declaration
19734 appears in a class scope.
19736 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19737 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19740 cp_parser_single_declaration (cp_parser* parser,
19741 VEC (deferred_access_check,gc)* checks,
19743 bool explicit_specialization_p,
19746 int declares_class_or_enum;
19747 tree decl = NULL_TREE;
19748 cp_decl_specifier_seq decl_specifiers;
19749 bool function_definition_p = false;
19750 cp_token *decl_spec_token_start;
19752 /* This function is only used when processing a template
19754 gcc_assert (innermost_scope_kind () == sk_template_parms
19755 || innermost_scope_kind () == sk_template_spec);
19757 /* Defer access checks until we know what is being declared. */
19758 push_deferring_access_checks (dk_deferred);
19760 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19762 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19763 cp_parser_decl_specifier_seq (parser,
19764 CP_PARSER_FLAGS_OPTIONAL,
19766 &declares_class_or_enum);
19768 *friend_p = cp_parser_friend_p (&decl_specifiers);
19770 /* There are no template typedefs. */
19771 if (decl_specifiers.specs[(int) ds_typedef])
19773 error_at (decl_spec_token_start->location,
19774 "template declaration of %<typedef%>");
19775 decl = error_mark_node;
19778 /* Gather up the access checks that occurred the
19779 decl-specifier-seq. */
19780 stop_deferring_access_checks ();
19782 /* Check for the declaration of a template class. */
19783 if (declares_class_or_enum)
19785 if (cp_parser_declares_only_class_p (parser))
19787 decl = shadow_tag (&decl_specifiers);
19792 friend template <typename T> struct A<T>::B;
19795 A<T>::B will be represented by a TYPENAME_TYPE, and
19796 therefore not recognized by shadow_tag. */
19797 if (friend_p && *friend_p
19799 && decl_specifiers.type
19800 && TYPE_P (decl_specifiers.type))
19801 decl = decl_specifiers.type;
19803 if (decl && decl != error_mark_node)
19804 decl = TYPE_NAME (decl);
19806 decl = error_mark_node;
19808 /* Perform access checks for template parameters. */
19809 cp_parser_perform_template_parameter_access_checks (checks);
19813 /* Complain about missing 'typename' or other invalid type names. */
19814 if (!decl_specifiers.any_type_specifiers_p)
19815 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19817 /* If it's not a template class, try for a template function. If
19818 the next token is a `;', then this declaration does not declare
19819 anything. But, if there were errors in the decl-specifiers, then
19820 the error might well have come from an attempted class-specifier.
19821 In that case, there's no need to warn about a missing declarator. */
19823 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19824 || decl_specifiers.type != error_mark_node))
19826 decl = cp_parser_init_declarator (parser,
19829 /*function_definition_allowed_p=*/true,
19831 declares_class_or_enum,
19832 &function_definition_p);
19834 /* 7.1.1-1 [dcl.stc]
19836 A storage-class-specifier shall not be specified in an explicit
19837 specialization... */
19839 && explicit_specialization_p
19840 && decl_specifiers.storage_class != sc_none)
19842 error_at (decl_spec_token_start->location,
19843 "explicit template specialization cannot have a storage class");
19844 decl = error_mark_node;
19848 pop_deferring_access_checks ();
19850 /* Clear any current qualification; whatever comes next is the start
19851 of something new. */
19852 parser->scope = NULL_TREE;
19853 parser->qualifying_scope = NULL_TREE;
19854 parser->object_scope = NULL_TREE;
19855 /* Look for a trailing `;' after the declaration. */
19856 if (!function_definition_p
19857 && (decl == error_mark_node
19858 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19859 cp_parser_skip_to_end_of_block_or_statement (parser);
19864 /* Parse a cast-expression that is not the operand of a unary "&". */
19867 cp_parser_simple_cast_expression (cp_parser *parser)
19869 return cp_parser_cast_expression (parser, /*address_p=*/false,
19870 /*cast_p=*/false, NULL);
19873 /* Parse a functional cast to TYPE. Returns an expression
19874 representing the cast. */
19877 cp_parser_functional_cast (cp_parser* parser, tree type)
19880 tree expression_list;
19884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19887 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19888 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19889 if (TREE_CODE (type) == TYPE_DECL)
19890 type = TREE_TYPE (type);
19891 return finish_compound_literal (type, expression_list);
19895 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19897 /*allow_expansion_p=*/true,
19898 /*non_constant_p=*/NULL);
19900 expression_list = error_mark_node;
19903 expression_list = build_tree_list_vec (vec);
19904 release_tree_vector (vec);
19907 cast = build_functional_cast (type, expression_list,
19908 tf_warning_or_error);
19909 /* [expr.const]/1: In an integral constant expression "only type
19910 conversions to integral or enumeration type can be used". */
19911 if (TREE_CODE (type) == TYPE_DECL)
19912 type = TREE_TYPE (type);
19913 if (cast != error_mark_node
19914 && !cast_valid_in_integral_constant_expression_p (type)
19915 && cp_parser_non_integral_constant_expression (parser,
19917 return error_mark_node;
19921 /* Save the tokens that make up the body of a member function defined
19922 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19923 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19924 specifiers applied to the declaration. Returns the FUNCTION_DECL
19925 for the member function. */
19928 cp_parser_save_member_function_body (cp_parser* parser,
19929 cp_decl_specifier_seq *decl_specifiers,
19930 cp_declarator *declarator,
19937 /* Create the FUNCTION_DECL. */
19938 fn = grokmethod (decl_specifiers, declarator, attributes);
19939 /* If something went badly wrong, bail out now. */
19940 if (fn == error_mark_node)
19942 /* If there's a function-body, skip it. */
19943 if (cp_parser_token_starts_function_definition_p
19944 (cp_lexer_peek_token (parser->lexer)))
19945 cp_parser_skip_to_end_of_block_or_statement (parser);
19946 return error_mark_node;
19949 /* Remember it, if there default args to post process. */
19950 cp_parser_save_default_args (parser, fn);
19952 /* Save away the tokens that make up the body of the
19954 first = parser->lexer->next_token;
19955 /* We can have braced-init-list mem-initializers before the fn body. */
19956 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19958 cp_lexer_consume_token (parser->lexer);
19959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19960 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19962 /* cache_group will stop after an un-nested { } pair, too. */
19963 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19966 /* variadic mem-inits have ... after the ')'. */
19967 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19968 cp_lexer_consume_token (parser->lexer);
19971 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19972 /* Handle function try blocks. */
19973 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19974 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19975 last = parser->lexer->next_token;
19977 /* Save away the inline definition; we will process it when the
19978 class is complete. */
19979 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19980 DECL_PENDING_INLINE_P (fn) = 1;
19982 /* We need to know that this was defined in the class, so that
19983 friend templates are handled correctly. */
19984 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19986 /* Add FN to the queue of functions to be parsed later. */
19987 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
19992 /* Parse a template-argument-list, as well as the trailing ">" (but
19993 not the opening ">"). See cp_parser_template_argument_list for the
19997 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20001 tree saved_qualifying_scope;
20002 tree saved_object_scope;
20003 bool saved_greater_than_is_operator_p;
20004 int saved_unevaluated_operand;
20005 int saved_inhibit_evaluation_warnings;
20009 When parsing a template-id, the first non-nested `>' is taken as
20010 the end of the template-argument-list rather than a greater-than
20012 saved_greater_than_is_operator_p
20013 = parser->greater_than_is_operator_p;
20014 parser->greater_than_is_operator_p = false;
20015 /* Parsing the argument list may modify SCOPE, so we save it
20017 saved_scope = parser->scope;
20018 saved_qualifying_scope = parser->qualifying_scope;
20019 saved_object_scope = parser->object_scope;
20020 /* We need to evaluate the template arguments, even though this
20021 template-id may be nested within a "sizeof". */
20022 saved_unevaluated_operand = cp_unevaluated_operand;
20023 cp_unevaluated_operand = 0;
20024 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20025 c_inhibit_evaluation_warnings = 0;
20026 /* Parse the template-argument-list itself. */
20027 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20028 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20029 arguments = NULL_TREE;
20031 arguments = cp_parser_template_argument_list (parser);
20032 /* Look for the `>' that ends the template-argument-list. If we find
20033 a '>>' instead, it's probably just a typo. */
20034 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20036 if (cxx_dialect != cxx98)
20038 /* In C++0x, a `>>' in a template argument list or cast
20039 expression is considered to be two separate `>'
20040 tokens. So, change the current token to a `>', but don't
20041 consume it: it will be consumed later when the outer
20042 template argument list (or cast expression) is parsed.
20043 Note that this replacement of `>' for `>>' is necessary
20044 even if we are parsing tentatively: in the tentative
20045 case, after calling
20046 cp_parser_enclosed_template_argument_list we will always
20047 throw away all of the template arguments and the first
20048 closing `>', either because the template argument list
20049 was erroneous or because we are replacing those tokens
20050 with a CPP_TEMPLATE_ID token. The second `>' (which will
20051 not have been thrown away) is needed either to close an
20052 outer template argument list or to complete a new-style
20054 cp_token *token = cp_lexer_peek_token (parser->lexer);
20055 token->type = CPP_GREATER;
20057 else if (!saved_greater_than_is_operator_p)
20059 /* If we're in a nested template argument list, the '>>' has
20060 to be a typo for '> >'. We emit the error message, but we
20061 continue parsing and we push a '>' as next token, so that
20062 the argument list will be parsed correctly. Note that the
20063 global source location is still on the token before the
20064 '>>', so we need to say explicitly where we want it. */
20065 cp_token *token = cp_lexer_peek_token (parser->lexer);
20066 error_at (token->location, "%<>>%> should be %<> >%> "
20067 "within a nested template argument list");
20069 token->type = CPP_GREATER;
20073 /* If this is not a nested template argument list, the '>>'
20074 is a typo for '>'. Emit an error message and continue.
20075 Same deal about the token location, but here we can get it
20076 right by consuming the '>>' before issuing the diagnostic. */
20077 cp_token *token = cp_lexer_consume_token (parser->lexer);
20078 error_at (token->location,
20079 "spurious %<>>%>, use %<>%> to terminate "
20080 "a template argument list");
20084 cp_parser_skip_to_end_of_template_parameter_list (parser);
20085 /* The `>' token might be a greater-than operator again now. */
20086 parser->greater_than_is_operator_p
20087 = saved_greater_than_is_operator_p;
20088 /* Restore the SAVED_SCOPE. */
20089 parser->scope = saved_scope;
20090 parser->qualifying_scope = saved_qualifying_scope;
20091 parser->object_scope = saved_object_scope;
20092 cp_unevaluated_operand = saved_unevaluated_operand;
20093 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20098 /* MEMBER_FUNCTION is a member function, or a friend. If default
20099 arguments, or the body of the function have not yet been parsed,
20103 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20105 /* If this member is a template, get the underlying
20107 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20108 member_function = DECL_TEMPLATE_RESULT (member_function);
20110 /* There should not be any class definitions in progress at this
20111 point; the bodies of members are only parsed outside of all class
20113 gcc_assert (parser->num_classes_being_defined == 0);
20114 /* While we're parsing the member functions we might encounter more
20115 classes. We want to handle them right away, but we don't want
20116 them getting mixed up with functions that are currently in the
20118 push_unparsed_function_queues (parser);
20120 /* Make sure that any template parameters are in scope. */
20121 maybe_begin_member_template_processing (member_function);
20123 /* If the body of the function has not yet been parsed, parse it
20125 if (DECL_PENDING_INLINE_P (member_function))
20127 tree function_scope;
20128 cp_token_cache *tokens;
20130 /* The function is no longer pending; we are processing it. */
20131 tokens = DECL_PENDING_INLINE_INFO (member_function);
20132 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20133 DECL_PENDING_INLINE_P (member_function) = 0;
20135 /* If this is a local class, enter the scope of the containing
20137 function_scope = current_function_decl;
20138 if (function_scope)
20139 push_function_context ();
20141 /* Push the body of the function onto the lexer stack. */
20142 cp_parser_push_lexer_for_tokens (parser, tokens);
20144 /* Let the front end know that we going to be defining this
20146 start_preparsed_function (member_function, NULL_TREE,
20147 SF_PRE_PARSED | SF_INCLASS_INLINE);
20149 /* Don't do access checking if it is a templated function. */
20150 if (processing_template_decl)
20151 push_deferring_access_checks (dk_no_check);
20153 /* Now, parse the body of the function. */
20154 cp_parser_function_definition_after_declarator (parser,
20155 /*inline_p=*/true);
20157 if (processing_template_decl)
20158 pop_deferring_access_checks ();
20160 /* Leave the scope of the containing function. */
20161 if (function_scope)
20162 pop_function_context ();
20163 cp_parser_pop_lexer (parser);
20166 /* Remove any template parameters from the symbol table. */
20167 maybe_end_member_template_processing ();
20169 /* Restore the queue. */
20170 pop_unparsed_function_queues (parser);
20173 /* If DECL contains any default args, remember it on the unparsed
20174 functions queue. */
20177 cp_parser_save_default_args (cp_parser* parser, tree decl)
20181 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20183 probe = TREE_CHAIN (probe))
20184 if (TREE_PURPOSE (probe))
20186 cp_default_arg_entry *entry
20187 = VEC_safe_push (cp_default_arg_entry, gc,
20188 unparsed_funs_with_default_args, NULL);
20189 entry->class_type = current_class_type;
20190 entry->decl = decl;
20195 /* FN is a FUNCTION_DECL which may contains a parameter with an
20196 unparsed DEFAULT_ARG. Parse the default args now. This function
20197 assumes that the current scope is the scope in which the default
20198 argument should be processed. */
20201 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20203 bool saved_local_variables_forbidden_p;
20204 tree parm, parmdecl;
20206 /* While we're parsing the default args, we might (due to the
20207 statement expression extension) encounter more classes. We want
20208 to handle them right away, but we don't want them getting mixed
20209 up with default args that are currently in the queue. */
20210 push_unparsed_function_queues (parser);
20212 /* Local variable names (and the `this' keyword) may not appear
20213 in a default argument. */
20214 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20215 parser->local_variables_forbidden_p = true;
20217 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20218 parmdecl = DECL_ARGUMENTS (fn);
20219 parm && parm != void_list_node;
20220 parm = TREE_CHAIN (parm),
20221 parmdecl = DECL_CHAIN (parmdecl))
20223 cp_token_cache *tokens;
20224 tree default_arg = TREE_PURPOSE (parm);
20226 VEC(tree,gc) *insts;
20233 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20234 /* This can happen for a friend declaration for a function
20235 already declared with default arguments. */
20238 /* Push the saved tokens for the default argument onto the parser's
20240 tokens = DEFARG_TOKENS (default_arg);
20241 cp_parser_push_lexer_for_tokens (parser, tokens);
20243 start_lambda_scope (parmdecl);
20245 /* Parse the assignment-expression. */
20246 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20247 if (parsed_arg == error_mark_node)
20249 cp_parser_pop_lexer (parser);
20253 if (!processing_template_decl)
20254 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20256 TREE_PURPOSE (parm) = parsed_arg;
20258 /* Update any instantiations we've already created. */
20259 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20260 VEC_iterate (tree, insts, ix, copy); ix++)
20261 TREE_PURPOSE (copy) = parsed_arg;
20263 finish_lambda_scope ();
20265 /* If the token stream has not been completely used up, then
20266 there was extra junk after the end of the default
20268 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20269 cp_parser_error (parser, "expected %<,%>");
20271 /* Revert to the main lexer. */
20272 cp_parser_pop_lexer (parser);
20275 /* Make sure no default arg is missing. */
20276 check_default_args (fn);
20278 /* Restore the state of local_variables_forbidden_p. */
20279 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20281 /* Restore the queue. */
20282 pop_unparsed_function_queues (parser);
20285 /* Parse the operand of `sizeof' (or a similar operator). Returns
20286 either a TYPE or an expression, depending on the form of the
20287 input. The KEYWORD indicates which kind of expression we have
20291 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20293 tree expr = NULL_TREE;
20294 const char *saved_message;
20296 bool saved_integral_constant_expression_p;
20297 bool saved_non_integral_constant_expression_p;
20298 bool pack_expansion_p = false;
20300 /* Types cannot be defined in a `sizeof' expression. Save away the
20302 saved_message = parser->type_definition_forbidden_message;
20303 /* And create the new one. */
20304 tmp = concat ("types may not be defined in %<",
20305 IDENTIFIER_POINTER (ridpointers[keyword]),
20306 "%> expressions", NULL);
20307 parser->type_definition_forbidden_message = tmp;
20309 /* The restrictions on constant-expressions do not apply inside
20310 sizeof expressions. */
20311 saved_integral_constant_expression_p
20312 = parser->integral_constant_expression_p;
20313 saved_non_integral_constant_expression_p
20314 = parser->non_integral_constant_expression_p;
20315 parser->integral_constant_expression_p = false;
20317 /* If it's a `...', then we are computing the length of a parameter
20319 if (keyword == RID_SIZEOF
20320 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20322 /* Consume the `...'. */
20323 cp_lexer_consume_token (parser->lexer);
20324 maybe_warn_variadic_templates ();
20326 /* Note that this is an expansion. */
20327 pack_expansion_p = true;
20330 /* Do not actually evaluate the expression. */
20331 ++cp_unevaluated_operand;
20332 ++c_inhibit_evaluation_warnings;
20333 /* If it's a `(', then we might be looking at the type-id
20335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20338 bool saved_in_type_id_in_expr_p;
20340 /* We can't be sure yet whether we're looking at a type-id or an
20342 cp_parser_parse_tentatively (parser);
20343 /* Consume the `('. */
20344 cp_lexer_consume_token (parser->lexer);
20345 /* Parse the type-id. */
20346 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20347 parser->in_type_id_in_expr_p = true;
20348 type = cp_parser_type_id (parser);
20349 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20350 /* Now, look for the trailing `)'. */
20351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20352 /* If all went well, then we're done. */
20353 if (cp_parser_parse_definitely (parser))
20355 cp_decl_specifier_seq decl_specs;
20357 /* Build a trivial decl-specifier-seq. */
20358 clear_decl_specs (&decl_specs);
20359 decl_specs.type = type;
20361 /* Call grokdeclarator to figure out what type this is. */
20362 expr = grokdeclarator (NULL,
20366 /*attrlist=*/NULL);
20370 /* If the type-id production did not work out, then we must be
20371 looking at the unary-expression production. */
20373 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20374 /*cast_p=*/false, NULL);
20376 if (pack_expansion_p)
20377 /* Build a pack expansion. */
20378 expr = make_pack_expansion (expr);
20380 /* Go back to evaluating expressions. */
20381 --cp_unevaluated_operand;
20382 --c_inhibit_evaluation_warnings;
20384 /* Free the message we created. */
20386 /* And restore the old one. */
20387 parser->type_definition_forbidden_message = saved_message;
20388 parser->integral_constant_expression_p
20389 = saved_integral_constant_expression_p;
20390 parser->non_integral_constant_expression_p
20391 = saved_non_integral_constant_expression_p;
20396 /* If the current declaration has no declarator, return true. */
20399 cp_parser_declares_only_class_p (cp_parser *parser)
20401 /* If the next token is a `;' or a `,' then there is no
20403 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20404 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20407 /* Update the DECL_SPECS to reflect the storage class indicated by
20411 cp_parser_set_storage_class (cp_parser *parser,
20412 cp_decl_specifier_seq *decl_specs,
20414 location_t location)
20416 cp_storage_class storage_class;
20418 if (parser->in_unbraced_linkage_specification_p)
20420 error_at (location, "invalid use of %qD in linkage specification",
20421 ridpointers[keyword]);
20424 else if (decl_specs->storage_class != sc_none)
20426 decl_specs->conflicting_specifiers_p = true;
20430 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20431 && decl_specs->specs[(int) ds_thread])
20433 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20434 decl_specs->specs[(int) ds_thread] = 0;
20440 storage_class = sc_auto;
20443 storage_class = sc_register;
20446 storage_class = sc_static;
20449 storage_class = sc_extern;
20452 storage_class = sc_mutable;
20455 gcc_unreachable ();
20457 decl_specs->storage_class = storage_class;
20459 /* A storage class specifier cannot be applied alongside a typedef
20460 specifier. If there is a typedef specifier present then set
20461 conflicting_specifiers_p which will trigger an error later
20462 on in grokdeclarator. */
20463 if (decl_specs->specs[(int)ds_typedef])
20464 decl_specs->conflicting_specifiers_p = true;
20467 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20468 is true, the type is a user-defined type; otherwise it is a
20469 built-in type specified by a keyword. */
20472 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20474 location_t location,
20475 bool user_defined_p)
20477 decl_specs->any_specifiers_p = true;
20479 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20480 (with, for example, in "typedef int wchar_t;") we remember that
20481 this is what happened. In system headers, we ignore these
20482 declarations so that G++ can work with system headers that are not
20484 if (decl_specs->specs[(int) ds_typedef]
20486 && (type_spec == boolean_type_node
20487 || type_spec == char16_type_node
20488 || type_spec == char32_type_node
20489 || type_spec == wchar_type_node)
20490 && (decl_specs->type
20491 || decl_specs->specs[(int) ds_long]
20492 || decl_specs->specs[(int) ds_short]
20493 || decl_specs->specs[(int) ds_unsigned]
20494 || decl_specs->specs[(int) ds_signed]))
20496 decl_specs->redefined_builtin_type = type_spec;
20497 if (!decl_specs->type)
20499 decl_specs->type = type_spec;
20500 decl_specs->user_defined_type_p = false;
20501 decl_specs->type_location = location;
20504 else if (decl_specs->type)
20505 decl_specs->multiple_types_p = true;
20508 decl_specs->type = type_spec;
20509 decl_specs->user_defined_type_p = user_defined_p;
20510 decl_specs->redefined_builtin_type = NULL_TREE;
20511 decl_specs->type_location = location;
20515 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20516 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20519 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20521 return decl_specifiers->specs[(int) ds_friend] != 0;
20524 /* Issue an error message indicating that TOKEN_DESC was expected.
20525 If KEYWORD is true, it indicated this function is called by
20526 cp_parser_require_keword and the required token can only be
20527 a indicated keyword. */
20530 cp_parser_required_error (cp_parser *parser,
20531 required_token token_desc,
20534 switch (token_desc)
20537 cp_parser_error (parser, "expected %<new%>");
20540 cp_parser_error (parser, "expected %<delete%>");
20543 cp_parser_error (parser, "expected %<return%>");
20546 cp_parser_error (parser, "expected %<while%>");
20549 cp_parser_error (parser, "expected %<extern%>");
20551 case RT_STATIC_ASSERT:
20552 cp_parser_error (parser, "expected %<static_assert%>");
20555 cp_parser_error (parser, "expected %<decltype%>");
20558 cp_parser_error (parser, "expected %<operator%>");
20561 cp_parser_error (parser, "expected %<class%>");
20564 cp_parser_error (parser, "expected %<template%>");
20567 cp_parser_error (parser, "expected %<namespace%>");
20570 cp_parser_error (parser, "expected %<using%>");
20573 cp_parser_error (parser, "expected %<asm%>");
20576 cp_parser_error (parser, "expected %<try%>");
20579 cp_parser_error (parser, "expected %<catch%>");
20582 cp_parser_error (parser, "expected %<throw%>");
20585 cp_parser_error (parser, "expected %<__label__%>");
20588 cp_parser_error (parser, "expected %<@try%>");
20590 case RT_AT_SYNCHRONIZED:
20591 cp_parser_error (parser, "expected %<@synchronized%>");
20594 cp_parser_error (parser, "expected %<@throw%>");
20601 switch (token_desc)
20604 cp_parser_error (parser, "expected %<;%>");
20606 case RT_OPEN_PAREN:
20607 cp_parser_error (parser, "expected %<(%>");
20609 case RT_CLOSE_BRACE:
20610 cp_parser_error (parser, "expected %<}%>");
20612 case RT_OPEN_BRACE:
20613 cp_parser_error (parser, "expected %<{%>");
20615 case RT_CLOSE_SQUARE:
20616 cp_parser_error (parser, "expected %<]%>");
20618 case RT_OPEN_SQUARE:
20619 cp_parser_error (parser, "expected %<[%>");
20622 cp_parser_error (parser, "expected %<,%>");
20625 cp_parser_error (parser, "expected %<::%>");
20628 cp_parser_error (parser, "expected %<<%>");
20631 cp_parser_error (parser, "expected %<>%>");
20634 cp_parser_error (parser, "expected %<=%>");
20637 cp_parser_error (parser, "expected %<...%>");
20640 cp_parser_error (parser, "expected %<*%>");
20643 cp_parser_error (parser, "expected %<~%>");
20646 cp_parser_error (parser, "expected %<:%>");
20648 case RT_COLON_SCOPE:
20649 cp_parser_error (parser, "expected %<:%> or %<::%>");
20651 case RT_CLOSE_PAREN:
20652 cp_parser_error (parser, "expected %<)%>");
20654 case RT_COMMA_CLOSE_PAREN:
20655 cp_parser_error (parser, "expected %<,%> or %<)%>");
20657 case RT_PRAGMA_EOL:
20658 cp_parser_error (parser, "expected end of line");
20661 cp_parser_error (parser, "expected identifier");
20664 cp_parser_error (parser, "expected selection-statement");
20666 case RT_INTERATION:
20667 cp_parser_error (parser, "expected iteration-statement");
20670 cp_parser_error (parser, "expected jump-statement");
20673 cp_parser_error (parser, "expected class-key");
20675 case RT_CLASS_TYPENAME_TEMPLATE:
20676 cp_parser_error (parser,
20677 "expected %<class%>, %<typename%>, or %<template%>");
20680 gcc_unreachable ();
20684 gcc_unreachable ();
20689 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20690 issue an error message indicating that TOKEN_DESC was expected.
20692 Returns the token consumed, if the token had the appropriate type.
20693 Otherwise, returns NULL. */
20696 cp_parser_require (cp_parser* parser,
20697 enum cpp_ttype type,
20698 required_token token_desc)
20700 if (cp_lexer_next_token_is (parser->lexer, type))
20701 return cp_lexer_consume_token (parser->lexer);
20704 /* Output the MESSAGE -- unless we're parsing tentatively. */
20705 if (!cp_parser_simulate_error (parser))
20706 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20711 /* An error message is produced if the next token is not '>'.
20712 All further tokens are skipped until the desired token is
20713 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20716 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20718 /* Current level of '< ... >'. */
20719 unsigned level = 0;
20720 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20721 unsigned nesting_depth = 0;
20723 /* Are we ready, yet? If not, issue error message. */
20724 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20727 /* Skip tokens until the desired token is found. */
20730 /* Peek at the next token. */
20731 switch (cp_lexer_peek_token (parser->lexer)->type)
20734 if (!nesting_depth)
20739 if (cxx_dialect == cxx98)
20740 /* C++0x views the `>>' operator as two `>' tokens, but
20743 else if (!nesting_depth && level-- == 0)
20745 /* We've hit a `>>' where the first `>' closes the
20746 template argument list, and the second `>' is
20747 spurious. Just consume the `>>' and stop; we've
20748 already produced at least one error. */
20749 cp_lexer_consume_token (parser->lexer);
20752 /* Fall through for C++0x, so we handle the second `>' in
20756 if (!nesting_depth && level-- == 0)
20758 /* We've reached the token we want, consume it and stop. */
20759 cp_lexer_consume_token (parser->lexer);
20764 case CPP_OPEN_PAREN:
20765 case CPP_OPEN_SQUARE:
20769 case CPP_CLOSE_PAREN:
20770 case CPP_CLOSE_SQUARE:
20771 if (nesting_depth-- == 0)
20776 case CPP_PRAGMA_EOL:
20777 case CPP_SEMICOLON:
20778 case CPP_OPEN_BRACE:
20779 case CPP_CLOSE_BRACE:
20780 /* The '>' was probably forgotten, don't look further. */
20787 /* Consume this token. */
20788 cp_lexer_consume_token (parser->lexer);
20792 /* If the next token is the indicated keyword, consume it. Otherwise,
20793 issue an error message indicating that TOKEN_DESC was expected.
20795 Returns the token consumed, if the token had the appropriate type.
20796 Otherwise, returns NULL. */
20799 cp_parser_require_keyword (cp_parser* parser,
20801 required_token token_desc)
20803 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20805 if (token && token->keyword != keyword)
20807 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20814 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20815 function-definition. */
20818 cp_parser_token_starts_function_definition_p (cp_token* token)
20820 return (/* An ordinary function-body begins with an `{'. */
20821 token->type == CPP_OPEN_BRACE
20822 /* A ctor-initializer begins with a `:'. */
20823 || token->type == CPP_COLON
20824 /* A function-try-block begins with `try'. */
20825 || token->keyword == RID_TRY
20826 /* The named return value extension begins with `return'. */
20827 || token->keyword == RID_RETURN);
20830 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20834 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20838 token = cp_lexer_peek_token (parser->lexer);
20839 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20842 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20843 C++0x) ending a template-argument. */
20846 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20850 token = cp_lexer_peek_token (parser->lexer);
20851 return (token->type == CPP_COMMA
20852 || token->type == CPP_GREATER
20853 || token->type == CPP_ELLIPSIS
20854 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20857 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20858 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20861 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20866 token = cp_lexer_peek_nth_token (parser->lexer, n);
20867 if (token->type == CPP_LESS)
20869 /* Check for the sequence `<::' in the original code. It would be lexed as
20870 `[:', where `[' is a digraph, and there is no whitespace before
20872 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20875 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20876 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20882 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20883 or none_type otherwise. */
20885 static enum tag_types
20886 cp_parser_token_is_class_key (cp_token* token)
20888 switch (token->keyword)
20893 return record_type;
20902 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20905 cp_parser_check_class_key (enum tag_types class_key, tree type)
20907 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20908 permerror (input_location, "%qs tag used in naming %q#T",
20909 class_key == union_type ? "union"
20910 : class_key == record_type ? "struct" : "class",
20914 /* Issue an error message if DECL is redeclared with different
20915 access than its original declaration [class.access.spec/3].
20916 This applies to nested classes and nested class templates.
20920 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20922 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20925 if ((TREE_PRIVATE (decl)
20926 != (current_access_specifier == access_private_node))
20927 || (TREE_PROTECTED (decl)
20928 != (current_access_specifier == access_protected_node)))
20929 error_at (location, "%qD redeclared with different access", decl);
20932 /* Look for the `template' keyword, as a syntactic disambiguator.
20933 Return TRUE iff it is present, in which case it will be
20937 cp_parser_optional_template_keyword (cp_parser *parser)
20939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20941 /* The `template' keyword can only be used within templates;
20942 outside templates the parser can always figure out what is a
20943 template and what is not. */
20944 if (!processing_template_decl)
20946 cp_token *token = cp_lexer_peek_token (parser->lexer);
20947 error_at (token->location,
20948 "%<template%> (as a disambiguator) is only allowed "
20949 "within templates");
20950 /* If this part of the token stream is rescanned, the same
20951 error message would be generated. So, we purge the token
20952 from the stream. */
20953 cp_lexer_purge_token (parser->lexer);
20958 /* Consume the `template' keyword. */
20959 cp_lexer_consume_token (parser->lexer);
20967 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20968 set PARSER->SCOPE, and perform other related actions. */
20971 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
20974 struct tree_check *check_value;
20975 deferred_access_check *chk;
20976 VEC (deferred_access_check,gc) *checks;
20978 /* Get the stored value. */
20979 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
20980 /* Perform any access checks that were deferred. */
20981 checks = check_value->checks;
20984 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
20985 perform_or_defer_access_check (chk->binfo,
20989 /* Set the scope from the stored value. */
20990 parser->scope = check_value->value;
20991 parser->qualifying_scope = check_value->qualifying_scope;
20992 parser->object_scope = NULL_TREE;
20995 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20996 encounter the end of a block before what we were looking for. */
20999 cp_parser_cache_group (cp_parser *parser,
21000 enum cpp_ttype end,
21005 cp_token *token = cp_lexer_peek_token (parser->lexer);
21007 /* Abort a parenthesized expression if we encounter a semicolon. */
21008 if ((end == CPP_CLOSE_PAREN || depth == 0)
21009 && token->type == CPP_SEMICOLON)
21011 /* If we've reached the end of the file, stop. */
21012 if (token->type == CPP_EOF
21013 || (end != CPP_PRAGMA_EOL
21014 && token->type == CPP_PRAGMA_EOL))
21016 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21017 /* We've hit the end of an enclosing block, so there's been some
21018 kind of syntax error. */
21021 /* Consume the token. */
21022 cp_lexer_consume_token (parser->lexer);
21023 /* See if it starts a new group. */
21024 if (token->type == CPP_OPEN_BRACE)
21026 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21027 /* In theory this should probably check end == '}', but
21028 cp_parser_save_member_function_body needs it to exit
21029 after either '}' or ')' when called with ')'. */
21033 else if (token->type == CPP_OPEN_PAREN)
21035 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21036 if (depth == 0 && end == CPP_CLOSE_PAREN)
21039 else if (token->type == CPP_PRAGMA)
21040 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21041 else if (token->type == end)
21046 /* Begin parsing tentatively. We always save tokens while parsing
21047 tentatively so that if the tentative parsing fails we can restore the
21051 cp_parser_parse_tentatively (cp_parser* parser)
21053 /* Enter a new parsing context. */
21054 parser->context = cp_parser_context_new (parser->context);
21055 /* Begin saving tokens. */
21056 cp_lexer_save_tokens (parser->lexer);
21057 /* In order to avoid repetitive access control error messages,
21058 access checks are queued up until we are no longer parsing
21060 push_deferring_access_checks (dk_deferred);
21063 /* Commit to the currently active tentative parse. */
21066 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21068 cp_parser_context *context;
21071 /* Mark all of the levels as committed. */
21072 lexer = parser->lexer;
21073 for (context = parser->context; context->next; context = context->next)
21075 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21077 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21078 while (!cp_lexer_saving_tokens (lexer))
21079 lexer = lexer->next;
21080 cp_lexer_commit_tokens (lexer);
21084 /* Abort the currently active tentative parse. All consumed tokens
21085 will be rolled back, and no diagnostics will be issued. */
21088 cp_parser_abort_tentative_parse (cp_parser* parser)
21090 cp_parser_simulate_error (parser);
21091 /* Now, pretend that we want to see if the construct was
21092 successfully parsed. */
21093 cp_parser_parse_definitely (parser);
21096 /* Stop parsing tentatively. If a parse error has occurred, restore the
21097 token stream. Otherwise, commit to the tokens we have consumed.
21098 Returns true if no error occurred; false otherwise. */
21101 cp_parser_parse_definitely (cp_parser* parser)
21103 bool error_occurred;
21104 cp_parser_context *context;
21106 /* Remember whether or not an error occurred, since we are about to
21107 destroy that information. */
21108 error_occurred = cp_parser_error_occurred (parser);
21109 /* Remove the topmost context from the stack. */
21110 context = parser->context;
21111 parser->context = context->next;
21112 /* If no parse errors occurred, commit to the tentative parse. */
21113 if (!error_occurred)
21115 /* Commit to the tokens read tentatively, unless that was
21117 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21118 cp_lexer_commit_tokens (parser->lexer);
21120 pop_to_parent_deferring_access_checks ();
21122 /* Otherwise, if errors occurred, roll back our state so that things
21123 are just as they were before we began the tentative parse. */
21126 cp_lexer_rollback_tokens (parser->lexer);
21127 pop_deferring_access_checks ();
21129 /* Add the context to the front of the free list. */
21130 context->next = cp_parser_context_free_list;
21131 cp_parser_context_free_list = context;
21133 return !error_occurred;
21136 /* Returns true if we are parsing tentatively and are not committed to
21137 this tentative parse. */
21140 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21142 return (cp_parser_parsing_tentatively (parser)
21143 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21146 /* Returns nonzero iff an error has occurred during the most recent
21147 tentative parse. */
21150 cp_parser_error_occurred (cp_parser* parser)
21152 return (cp_parser_parsing_tentatively (parser)
21153 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21156 /* Returns nonzero if GNU extensions are allowed. */
21159 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21161 return parser->allow_gnu_extensions_p;
21164 /* Objective-C++ Productions */
21167 /* Parse an Objective-C expression, which feeds into a primary-expression
21171 objc-message-expression
21172 objc-string-literal
21173 objc-encode-expression
21174 objc-protocol-expression
21175 objc-selector-expression
21177 Returns a tree representation of the expression. */
21180 cp_parser_objc_expression (cp_parser* parser)
21182 /* Try to figure out what kind of declaration is present. */
21183 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21187 case CPP_OPEN_SQUARE:
21188 return cp_parser_objc_message_expression (parser);
21190 case CPP_OBJC_STRING:
21191 kwd = cp_lexer_consume_token (parser->lexer);
21192 return objc_build_string_object (kwd->u.value);
21195 switch (kwd->keyword)
21197 case RID_AT_ENCODE:
21198 return cp_parser_objc_encode_expression (parser);
21200 case RID_AT_PROTOCOL:
21201 return cp_parser_objc_protocol_expression (parser);
21203 case RID_AT_SELECTOR:
21204 return cp_parser_objc_selector_expression (parser);
21210 error_at (kwd->location,
21211 "misplaced %<@%D%> Objective-C++ construct",
21213 cp_parser_skip_to_end_of_block_or_statement (parser);
21216 return error_mark_node;
21219 /* Parse an Objective-C message expression.
21221 objc-message-expression:
21222 [ objc-message-receiver objc-message-args ]
21224 Returns a representation of an Objective-C message. */
21227 cp_parser_objc_message_expression (cp_parser* parser)
21229 tree receiver, messageargs;
21231 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21232 receiver = cp_parser_objc_message_receiver (parser);
21233 messageargs = cp_parser_objc_message_args (parser);
21234 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21236 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21239 /* Parse an objc-message-receiver.
21241 objc-message-receiver:
21243 simple-type-specifier
21245 Returns a representation of the type or expression. */
21248 cp_parser_objc_message_receiver (cp_parser* parser)
21252 /* An Objective-C message receiver may be either (1) a type
21253 or (2) an expression. */
21254 cp_parser_parse_tentatively (parser);
21255 rcv = cp_parser_expression (parser, false, NULL);
21257 if (cp_parser_parse_definitely (parser))
21260 rcv = cp_parser_simple_type_specifier (parser,
21261 /*decl_specs=*/NULL,
21262 CP_PARSER_FLAGS_NONE);
21264 return objc_get_class_reference (rcv);
21267 /* Parse the arguments and selectors comprising an Objective-C message.
21272 objc-selector-args , objc-comma-args
21274 objc-selector-args:
21275 objc-selector [opt] : assignment-expression
21276 objc-selector-args objc-selector [opt] : assignment-expression
21279 assignment-expression
21280 objc-comma-args , assignment-expression
21282 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21283 selector arguments and TREE_VALUE containing a list of comma
21287 cp_parser_objc_message_args (cp_parser* parser)
21289 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21290 bool maybe_unary_selector_p = true;
21291 cp_token *token = cp_lexer_peek_token (parser->lexer);
21293 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21295 tree selector = NULL_TREE, arg;
21297 if (token->type != CPP_COLON)
21298 selector = cp_parser_objc_selector (parser);
21300 /* Detect if we have a unary selector. */
21301 if (maybe_unary_selector_p
21302 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21303 return build_tree_list (selector, NULL_TREE);
21305 maybe_unary_selector_p = false;
21306 cp_parser_require (parser, CPP_COLON, RT_COLON);
21307 arg = cp_parser_assignment_expression (parser, false, NULL);
21310 = chainon (sel_args,
21311 build_tree_list (selector, arg));
21313 token = cp_lexer_peek_token (parser->lexer);
21316 /* Handle non-selector arguments, if any. */
21317 while (token->type == CPP_COMMA)
21321 cp_lexer_consume_token (parser->lexer);
21322 arg = cp_parser_assignment_expression (parser, false, NULL);
21325 = chainon (addl_args,
21326 build_tree_list (NULL_TREE, arg));
21328 token = cp_lexer_peek_token (parser->lexer);
21331 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21333 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21334 return build_tree_list (error_mark_node, error_mark_node);
21337 return build_tree_list (sel_args, addl_args);
21340 /* Parse an Objective-C encode expression.
21342 objc-encode-expression:
21343 @encode objc-typename
21345 Returns an encoded representation of the type argument. */
21348 cp_parser_objc_encode_expression (cp_parser* parser)
21353 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21354 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21355 token = cp_lexer_peek_token (parser->lexer);
21356 type = complete_type (cp_parser_type_id (parser));
21357 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21361 error_at (token->location,
21362 "%<@encode%> must specify a type as an argument");
21363 return error_mark_node;
21366 /* This happens if we find @encode(T) (where T is a template
21367 typename or something dependent on a template typename) when
21368 parsing a template. In that case, we can't compile it
21369 immediately, but we rather create an AT_ENCODE_EXPR which will
21370 need to be instantiated when the template is used.
21372 if (dependent_type_p (type))
21374 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21375 TREE_READONLY (value) = 1;
21379 return objc_build_encode_expr (type);
21382 /* Parse an Objective-C @defs expression. */
21385 cp_parser_objc_defs_expression (cp_parser *parser)
21389 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21390 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21391 name = cp_parser_identifier (parser);
21392 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21394 return objc_get_class_ivars (name);
21397 /* Parse an Objective-C protocol expression.
21399 objc-protocol-expression:
21400 @protocol ( identifier )
21402 Returns a representation of the protocol expression. */
21405 cp_parser_objc_protocol_expression (cp_parser* parser)
21409 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21411 proto = cp_parser_identifier (parser);
21412 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21414 return objc_build_protocol_expr (proto);
21417 /* Parse an Objective-C selector expression.
21419 objc-selector-expression:
21420 @selector ( objc-method-signature )
21422 objc-method-signature:
21428 objc-selector-seq objc-selector :
21430 Returns a representation of the method selector. */
21433 cp_parser_objc_selector_expression (cp_parser* parser)
21435 tree sel_seq = NULL_TREE;
21436 bool maybe_unary_selector_p = true;
21438 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21440 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21441 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21442 token = cp_lexer_peek_token (parser->lexer);
21444 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21445 || token->type == CPP_SCOPE)
21447 tree selector = NULL_TREE;
21449 if (token->type != CPP_COLON
21450 || token->type == CPP_SCOPE)
21451 selector = cp_parser_objc_selector (parser);
21453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21454 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21456 /* Detect if we have a unary selector. */
21457 if (maybe_unary_selector_p)
21459 sel_seq = selector;
21460 goto finish_selector;
21464 cp_parser_error (parser, "expected %<:%>");
21467 maybe_unary_selector_p = false;
21468 token = cp_lexer_consume_token (parser->lexer);
21470 if (token->type == CPP_SCOPE)
21473 = chainon (sel_seq,
21474 build_tree_list (selector, NULL_TREE));
21476 = chainon (sel_seq,
21477 build_tree_list (NULL_TREE, NULL_TREE));
21481 = chainon (sel_seq,
21482 build_tree_list (selector, NULL_TREE));
21484 token = cp_lexer_peek_token (parser->lexer);
21488 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21490 return objc_build_selector_expr (loc, sel_seq);
21493 /* Parse a list of identifiers.
21495 objc-identifier-list:
21497 objc-identifier-list , identifier
21499 Returns a TREE_LIST of identifier nodes. */
21502 cp_parser_objc_identifier_list (cp_parser* parser)
21508 identifier = cp_parser_identifier (parser);
21509 if (identifier == error_mark_node)
21510 return error_mark_node;
21512 list = build_tree_list (NULL_TREE, identifier);
21513 sep = cp_lexer_peek_token (parser->lexer);
21515 while (sep->type == CPP_COMMA)
21517 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21518 identifier = cp_parser_identifier (parser);
21519 if (identifier == error_mark_node)
21522 list = chainon (list, build_tree_list (NULL_TREE,
21524 sep = cp_lexer_peek_token (parser->lexer);
21530 /* Parse an Objective-C alias declaration.
21532 objc-alias-declaration:
21533 @compatibility_alias identifier identifier ;
21535 This function registers the alias mapping with the Objective-C front end.
21536 It returns nothing. */
21539 cp_parser_objc_alias_declaration (cp_parser* parser)
21543 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21544 alias = cp_parser_identifier (parser);
21545 orig = cp_parser_identifier (parser);
21546 objc_declare_alias (alias, orig);
21547 cp_parser_consume_semicolon_at_end_of_statement (parser);
21550 /* Parse an Objective-C class forward-declaration.
21552 objc-class-declaration:
21553 @class objc-identifier-list ;
21555 The function registers the forward declarations with the Objective-C
21556 front end. It returns nothing. */
21559 cp_parser_objc_class_declaration (cp_parser* parser)
21561 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21562 objc_declare_class (cp_parser_objc_identifier_list (parser));
21563 cp_parser_consume_semicolon_at_end_of_statement (parser);
21566 /* Parse a list of Objective-C protocol references.
21568 objc-protocol-refs-opt:
21569 objc-protocol-refs [opt]
21571 objc-protocol-refs:
21572 < objc-identifier-list >
21574 Returns a TREE_LIST of identifiers, if any. */
21577 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21579 tree protorefs = NULL_TREE;
21581 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21583 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21584 protorefs = cp_parser_objc_identifier_list (parser);
21585 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21591 /* Parse a Objective-C visibility specification. */
21594 cp_parser_objc_visibility_spec (cp_parser* parser)
21596 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21598 switch (vis->keyword)
21600 case RID_AT_PRIVATE:
21601 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21603 case RID_AT_PROTECTED:
21604 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21606 case RID_AT_PUBLIC:
21607 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21609 case RID_AT_PACKAGE:
21610 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21616 /* Eat '@private'/'@protected'/'@public'. */
21617 cp_lexer_consume_token (parser->lexer);
21620 /* Parse an Objective-C method type. Return 'true' if it is a class
21621 (+) method, and 'false' if it is an instance (-) method. */
21624 cp_parser_objc_method_type (cp_parser* parser)
21626 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21632 /* Parse an Objective-C protocol qualifier. */
21635 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21637 tree quals = NULL_TREE, node;
21638 cp_token *token = cp_lexer_peek_token (parser->lexer);
21640 node = token->u.value;
21642 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21643 && (node == ridpointers [(int) RID_IN]
21644 || node == ridpointers [(int) RID_OUT]
21645 || node == ridpointers [(int) RID_INOUT]
21646 || node == ridpointers [(int) RID_BYCOPY]
21647 || node == ridpointers [(int) RID_BYREF]
21648 || node == ridpointers [(int) RID_ONEWAY]))
21650 quals = tree_cons (NULL_TREE, node, quals);
21651 cp_lexer_consume_token (parser->lexer);
21652 token = cp_lexer_peek_token (parser->lexer);
21653 node = token->u.value;
21659 /* Parse an Objective-C typename. */
21662 cp_parser_objc_typename (cp_parser* parser)
21664 tree type_name = NULL_TREE;
21666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21668 tree proto_quals, cp_type = NULL_TREE;
21670 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21671 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21673 /* An ObjC type name may consist of just protocol qualifiers, in which
21674 case the type shall default to 'id'. */
21675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21676 cp_type = cp_parser_type_id (parser);
21678 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21679 type_name = build_tree_list (proto_quals, cp_type);
21685 /* Check to see if TYPE refers to an Objective-C selector name. */
21688 cp_parser_objc_selector_p (enum cpp_ttype type)
21690 return (type == CPP_NAME || type == CPP_KEYWORD
21691 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21692 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21693 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21694 || type == CPP_XOR || type == CPP_XOR_EQ);
21697 /* Parse an Objective-C selector. */
21700 cp_parser_objc_selector (cp_parser* parser)
21702 cp_token *token = cp_lexer_consume_token (parser->lexer);
21704 if (!cp_parser_objc_selector_p (token->type))
21706 error_at (token->location, "invalid Objective-C++ selector name");
21707 return error_mark_node;
21710 /* C++ operator names are allowed to appear in ObjC selectors. */
21711 switch (token->type)
21713 case CPP_AND_AND: return get_identifier ("and");
21714 case CPP_AND_EQ: return get_identifier ("and_eq");
21715 case CPP_AND: return get_identifier ("bitand");
21716 case CPP_OR: return get_identifier ("bitor");
21717 case CPP_COMPL: return get_identifier ("compl");
21718 case CPP_NOT: return get_identifier ("not");
21719 case CPP_NOT_EQ: return get_identifier ("not_eq");
21720 case CPP_OR_OR: return get_identifier ("or");
21721 case CPP_OR_EQ: return get_identifier ("or_eq");
21722 case CPP_XOR: return get_identifier ("xor");
21723 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21724 default: return token->u.value;
21728 /* Parse an Objective-C params list. */
21731 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21733 tree params = NULL_TREE;
21734 bool maybe_unary_selector_p = true;
21735 cp_token *token = cp_lexer_peek_token (parser->lexer);
21737 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21739 tree selector = NULL_TREE, type_name, identifier;
21740 tree parm_attr = NULL_TREE;
21742 if (token->keyword == RID_ATTRIBUTE)
21745 if (token->type != CPP_COLON)
21746 selector = cp_parser_objc_selector (parser);
21748 /* Detect if we have a unary selector. */
21749 if (maybe_unary_selector_p
21750 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21752 params = selector; /* Might be followed by attributes. */
21756 maybe_unary_selector_p = false;
21757 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21759 /* Something went quite wrong. There should be a colon
21760 here, but there is not. Stop parsing parameters. */
21763 type_name = cp_parser_objc_typename (parser);
21764 /* New ObjC allows attributes on parameters too. */
21765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21766 parm_attr = cp_parser_attributes_opt (parser);
21767 identifier = cp_parser_identifier (parser);
21771 objc_build_keyword_decl (selector,
21776 token = cp_lexer_peek_token (parser->lexer);
21779 if (params == NULL_TREE)
21781 cp_parser_error (parser, "objective-c++ method declaration is expected");
21782 return error_mark_node;
21785 /* We allow tail attributes for the method. */
21786 if (token->keyword == RID_ATTRIBUTE)
21788 *attributes = cp_parser_attributes_opt (parser);
21789 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21790 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21792 cp_parser_error (parser,
21793 "method attributes must be specified at the end");
21794 return error_mark_node;
21797 if (params == NULL_TREE)
21799 cp_parser_error (parser, "objective-c++ method declaration is expected");
21800 return error_mark_node;
21805 /* Parse the non-keyword Objective-C params. */
21808 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21811 tree params = make_node (TREE_LIST);
21812 cp_token *token = cp_lexer_peek_token (parser->lexer);
21813 *ellipsisp = false; /* Initially, assume no ellipsis. */
21815 while (token->type == CPP_COMMA)
21817 cp_parameter_declarator *parmdecl;
21820 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21821 token = cp_lexer_peek_token (parser->lexer);
21823 if (token->type == CPP_ELLIPSIS)
21825 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21827 token = cp_lexer_peek_token (parser->lexer);
21831 /* TODO: parse attributes for tail parameters. */
21832 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21833 parm = grokdeclarator (parmdecl->declarator,
21834 &parmdecl->decl_specifiers,
21835 PARM, /*initialized=*/0,
21836 /*attrlist=*/NULL);
21838 chainon (params, build_tree_list (NULL_TREE, parm));
21839 token = cp_lexer_peek_token (parser->lexer);
21842 /* We allow tail attributes for the method. */
21843 if (token->keyword == RID_ATTRIBUTE)
21845 if (*attributes == NULL_TREE)
21847 *attributes = cp_parser_attributes_opt (parser);
21848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21849 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21853 /* We have an error, but parse the attributes, so that we can
21855 *attributes = cp_parser_attributes_opt (parser);
21857 cp_parser_error (parser,
21858 "method attributes must be specified at the end");
21859 return error_mark_node;
21865 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21868 cp_parser_objc_interstitial_code (cp_parser* parser)
21870 cp_token *token = cp_lexer_peek_token (parser->lexer);
21872 /* If the next token is `extern' and the following token is a string
21873 literal, then we have a linkage specification. */
21874 if (token->keyword == RID_EXTERN
21875 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21876 cp_parser_linkage_specification (parser);
21877 /* Handle #pragma, if any. */
21878 else if (token->type == CPP_PRAGMA)
21879 cp_parser_pragma (parser, pragma_external);
21880 /* Allow stray semicolons. */
21881 else if (token->type == CPP_SEMICOLON)
21882 cp_lexer_consume_token (parser->lexer);
21883 /* Mark methods as optional or required, when building protocols. */
21884 else if (token->keyword == RID_AT_OPTIONAL)
21886 cp_lexer_consume_token (parser->lexer);
21887 objc_set_method_opt (true);
21889 else if (token->keyword == RID_AT_REQUIRED)
21891 cp_lexer_consume_token (parser->lexer);
21892 objc_set_method_opt (false);
21894 else if (token->keyword == RID_NAMESPACE)
21895 cp_parser_namespace_definition (parser);
21896 /* Other stray characters must generate errors. */
21897 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21899 cp_lexer_consume_token (parser->lexer);
21900 error ("stray `%s' between Objective-C++ methods",
21901 token->type == CPP_OPEN_BRACE ? "{" : "}");
21903 /* Finally, try to parse a block-declaration, or a function-definition. */
21905 cp_parser_block_declaration (parser, /*statement_p=*/false);
21908 /* Parse a method signature. */
21911 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
21913 tree rettype, kwdparms, optparms;
21914 bool ellipsis = false;
21915 bool is_class_method;
21917 is_class_method = cp_parser_objc_method_type (parser);
21918 rettype = cp_parser_objc_typename (parser);
21919 *attributes = NULL_TREE;
21920 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
21921 if (kwdparms == error_mark_node)
21922 return error_mark_node;
21923 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
21924 if (optparms == error_mark_node)
21925 return error_mark_node;
21927 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
21931 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
21934 cp_lexer_save_tokens (parser->lexer);
21935 tattr = cp_parser_attributes_opt (parser);
21936 gcc_assert (tattr) ;
21938 /* If the attributes are followed by a method introducer, this is not allowed.
21939 Dump the attributes and flag the situation. */
21940 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
21941 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
21944 /* Otherwise, the attributes introduce some interstitial code, possibly so
21945 rewind to allow that check. */
21946 cp_lexer_rollback_tokens (parser->lexer);
21950 /* Parse an Objective-C method prototype list. */
21953 cp_parser_objc_method_prototype_list (cp_parser* parser)
21955 cp_token *token = cp_lexer_peek_token (parser->lexer);
21957 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
21959 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
21961 tree attributes, sig;
21962 bool is_class_method;
21963 if (token->type == CPP_PLUS)
21964 is_class_method = true;
21966 is_class_method = false;
21967 sig = cp_parser_objc_method_signature (parser, &attributes);
21968 if (sig == error_mark_node)
21970 cp_parser_skip_to_end_of_block_or_statement (parser);
21971 token = cp_lexer_peek_token (parser->lexer);
21974 objc_add_method_declaration (is_class_method, sig, attributes);
21975 cp_parser_consume_semicolon_at_end_of_statement (parser);
21977 else if (token->keyword == RID_AT_PROPERTY)
21978 cp_parser_objc_at_property_declaration (parser);
21979 else if (token->keyword == RID_ATTRIBUTE
21980 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
21981 warning_at (cp_lexer_peek_token (parser->lexer)->location,
21983 "prefix attributes are ignored for methods");
21985 /* Allow for interspersed non-ObjC++ code. */
21986 cp_parser_objc_interstitial_code (parser);
21988 token = cp_lexer_peek_token (parser->lexer);
21991 if (token->type != CPP_EOF)
21992 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
21994 cp_parser_error (parser, "expected %<@end%>");
21996 objc_finish_interface ();
21999 /* Parse an Objective-C method definition list. */
22002 cp_parser_objc_method_definition_list (cp_parser* parser)
22004 cp_token *token = cp_lexer_peek_token (parser->lexer);
22006 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22010 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22013 tree sig, attribute;
22014 bool is_class_method;
22015 if (token->type == CPP_PLUS)
22016 is_class_method = true;
22018 is_class_method = false;
22019 push_deferring_access_checks (dk_deferred);
22020 sig = cp_parser_objc_method_signature (parser, &attribute);
22021 if (sig == error_mark_node)
22023 cp_parser_skip_to_end_of_block_or_statement (parser);
22024 token = cp_lexer_peek_token (parser->lexer);
22027 objc_start_method_definition (is_class_method, sig, attribute);
22029 /* For historical reasons, we accept an optional semicolon. */
22030 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22031 cp_lexer_consume_token (parser->lexer);
22033 ptk = cp_lexer_peek_token (parser->lexer);
22034 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22035 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22037 perform_deferred_access_checks ();
22038 stop_deferring_access_checks ();
22039 meth = cp_parser_function_definition_after_declarator (parser,
22041 pop_deferring_access_checks ();
22042 objc_finish_method_definition (meth);
22045 /* The following case will be removed once @synthesize is
22046 completely implemented. */
22047 else if (token->keyword == RID_AT_PROPERTY)
22048 cp_parser_objc_at_property_declaration (parser);
22049 else if (token->keyword == RID_AT_SYNTHESIZE)
22050 cp_parser_objc_at_synthesize_declaration (parser);
22051 else if (token->keyword == RID_AT_DYNAMIC)
22052 cp_parser_objc_at_dynamic_declaration (parser);
22053 else if (token->keyword == RID_ATTRIBUTE
22054 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22055 warning_at (token->location, OPT_Wattributes,
22056 "prefix attributes are ignored for methods");
22058 /* Allow for interspersed non-ObjC++ code. */
22059 cp_parser_objc_interstitial_code (parser);
22061 token = cp_lexer_peek_token (parser->lexer);
22064 if (token->type != CPP_EOF)
22065 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22067 cp_parser_error (parser, "expected %<@end%>");
22069 objc_finish_implementation ();
22072 /* Parse Objective-C ivars. */
22075 cp_parser_objc_class_ivars (cp_parser* parser)
22077 cp_token *token = cp_lexer_peek_token (parser->lexer);
22079 if (token->type != CPP_OPEN_BRACE)
22080 return; /* No ivars specified. */
22082 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22083 token = cp_lexer_peek_token (parser->lexer);
22085 while (token->type != CPP_CLOSE_BRACE
22086 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22088 cp_decl_specifier_seq declspecs;
22089 int decl_class_or_enum_p;
22090 tree prefix_attributes;
22092 cp_parser_objc_visibility_spec (parser);
22094 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22097 cp_parser_decl_specifier_seq (parser,
22098 CP_PARSER_FLAGS_OPTIONAL,
22100 &decl_class_or_enum_p);
22102 /* auto, register, static, extern, mutable. */
22103 if (declspecs.storage_class != sc_none)
22105 cp_parser_error (parser, "invalid type for instance variable");
22106 declspecs.storage_class = sc_none;
22110 if (declspecs.specs[(int) ds_thread])
22112 cp_parser_error (parser, "invalid type for instance variable");
22113 declspecs.specs[(int) ds_thread] = 0;
22117 if (declspecs.specs[(int) ds_typedef])
22119 cp_parser_error (parser, "invalid type for instance variable");
22120 declspecs.specs[(int) ds_typedef] = 0;
22123 prefix_attributes = declspecs.attributes;
22124 declspecs.attributes = NULL_TREE;
22126 /* Keep going until we hit the `;' at the end of the
22128 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22130 tree width = NULL_TREE, attributes, first_attribute, decl;
22131 cp_declarator *declarator = NULL;
22132 int ctor_dtor_or_conv_p;
22134 /* Check for a (possibly unnamed) bitfield declaration. */
22135 token = cp_lexer_peek_token (parser->lexer);
22136 if (token->type == CPP_COLON)
22139 if (token->type == CPP_NAME
22140 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22143 /* Get the name of the bitfield. */
22144 declarator = make_id_declarator (NULL_TREE,
22145 cp_parser_identifier (parser),
22149 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22150 /* Get the width of the bitfield. */
22152 = cp_parser_constant_expression (parser,
22153 /*allow_non_constant=*/false,
22158 /* Parse the declarator. */
22160 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22161 &ctor_dtor_or_conv_p,
22162 /*parenthesized_p=*/NULL,
22163 /*member_p=*/false);
22166 /* Look for attributes that apply to the ivar. */
22167 attributes = cp_parser_attributes_opt (parser);
22168 /* Remember which attributes are prefix attributes and
22170 first_attribute = attributes;
22171 /* Combine the attributes. */
22172 attributes = chainon (prefix_attributes, attributes);
22175 /* Create the bitfield declaration. */
22176 decl = grokbitfield (declarator, &declspecs,
22180 decl = grokfield (declarator, &declspecs,
22181 NULL_TREE, /*init_const_expr_p=*/false,
22182 NULL_TREE, attributes);
22184 /* Add the instance variable. */
22185 objc_add_instance_variable (decl);
22187 /* Reset PREFIX_ATTRIBUTES. */
22188 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22189 attributes = TREE_CHAIN (attributes);
22191 TREE_CHAIN (attributes) = NULL_TREE;
22193 token = cp_lexer_peek_token (parser->lexer);
22195 if (token->type == CPP_COMMA)
22197 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22203 cp_parser_consume_semicolon_at_end_of_statement (parser);
22204 token = cp_lexer_peek_token (parser->lexer);
22207 if (token->keyword == RID_AT_END)
22208 cp_parser_error (parser, "expected %<}%>");
22210 /* Do not consume the RID_AT_END, so it will be read again as terminating
22211 the @interface of @implementation. */
22212 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22213 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22215 /* For historical reasons, we accept an optional semicolon. */
22216 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22217 cp_lexer_consume_token (parser->lexer);
22220 /* Parse an Objective-C protocol declaration. */
22223 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22225 tree proto, protorefs;
22228 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22231 tok = cp_lexer_peek_token (parser->lexer);
22232 error_at (tok->location, "identifier expected after %<@protocol%>");
22236 /* See if we have a forward declaration or a definition. */
22237 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22239 /* Try a forward declaration first. */
22240 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22242 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
22244 cp_parser_consume_semicolon_at_end_of_statement (parser);
22247 /* Ok, we got a full-fledged definition (or at least should). */
22250 proto = cp_parser_identifier (parser);
22251 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22252 objc_start_protocol (proto, protorefs, attributes);
22253 cp_parser_objc_method_prototype_list (parser);
22257 /* Parse an Objective-C superclass or category. */
22260 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22263 cp_token *next = cp_lexer_peek_token (parser->lexer);
22265 *super = *categ = NULL_TREE;
22266 if (next->type == CPP_COLON)
22268 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22269 *super = cp_parser_identifier (parser);
22271 else if (next->type == CPP_OPEN_PAREN)
22273 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22274 *categ = cp_parser_identifier (parser);
22275 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22279 /* Parse an Objective-C class interface. */
22282 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22284 tree name, super, categ, protos;
22286 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22287 name = cp_parser_identifier (parser);
22288 if (name == error_mark_node)
22290 /* It's hard to recover because even if valid @interface stuff
22291 is to follow, we can't compile it (or validate it) if we
22292 don't even know which class it refers to. Let's assume this
22293 was a stray '@interface' token in the stream and skip it.
22297 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22298 protos = cp_parser_objc_protocol_refs_opt (parser);
22300 /* We have either a class or a category on our hands. */
22302 objc_start_category_interface (name, categ, protos, attributes);
22305 objc_start_class_interface (name, super, protos, attributes);
22306 /* Handle instance variable declarations, if any. */
22307 cp_parser_objc_class_ivars (parser);
22308 objc_continue_interface ();
22311 cp_parser_objc_method_prototype_list (parser);
22314 /* Parse an Objective-C class implementation. */
22317 cp_parser_objc_class_implementation (cp_parser* parser)
22319 tree name, super, categ;
22321 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22322 name = cp_parser_identifier (parser);
22323 if (name == error_mark_node)
22325 /* It's hard to recover because even if valid @implementation
22326 stuff is to follow, we can't compile it (or validate it) if
22327 we don't even know which class it refers to. Let's assume
22328 this was a stray '@implementation' token in the stream and
22333 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22335 /* We have either a class or a category on our hands. */
22337 objc_start_category_implementation (name, categ);
22340 objc_start_class_implementation (name, super);
22341 /* Handle instance variable declarations, if any. */
22342 cp_parser_objc_class_ivars (parser);
22343 objc_continue_implementation ();
22346 cp_parser_objc_method_definition_list (parser);
22349 /* Consume the @end token and finish off the implementation. */
22352 cp_parser_objc_end_implementation (cp_parser* parser)
22354 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22355 objc_finish_implementation ();
22358 /* Parse an Objective-C declaration. */
22361 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22363 /* Try to figure out what kind of declaration is present. */
22364 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22367 switch (kwd->keyword)
22372 error_at (kwd->location, "attributes may not be specified before"
22373 " the %<@%D%> Objective-C++ keyword",
22377 case RID_AT_IMPLEMENTATION:
22378 warning_at (kwd->location, OPT_Wattributes,
22379 "prefix attributes are ignored before %<@%D%>",
22386 switch (kwd->keyword)
22389 cp_parser_objc_alias_declaration (parser);
22392 cp_parser_objc_class_declaration (parser);
22394 case RID_AT_PROTOCOL:
22395 cp_parser_objc_protocol_declaration (parser, attributes);
22397 case RID_AT_INTERFACE:
22398 cp_parser_objc_class_interface (parser, attributes);
22400 case RID_AT_IMPLEMENTATION:
22401 cp_parser_objc_class_implementation (parser);
22404 cp_parser_objc_end_implementation (parser);
22407 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22409 cp_parser_skip_to_end_of_block_or_statement (parser);
22413 /* Parse an Objective-C try-catch-finally statement.
22415 objc-try-catch-finally-stmt:
22416 @try compound-statement objc-catch-clause-seq [opt]
22417 objc-finally-clause [opt]
22419 objc-catch-clause-seq:
22420 objc-catch-clause objc-catch-clause-seq [opt]
22423 @catch ( exception-declaration ) compound-statement
22425 objc-finally-clause
22426 @finally compound-statement
22428 Returns NULL_TREE. */
22431 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22432 location_t location;
22435 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22436 location = cp_lexer_peek_token (parser->lexer)->location;
22437 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22438 node, lest it get absorbed into the surrounding block. */
22439 stmt = push_stmt_list ();
22440 cp_parser_compound_statement (parser, NULL, false);
22441 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22443 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22445 cp_parameter_declarator *parmdecl;
22448 cp_lexer_consume_token (parser->lexer);
22449 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22450 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22451 parm = grokdeclarator (parmdecl->declarator,
22452 &parmdecl->decl_specifiers,
22453 PARM, /*initialized=*/0,
22454 /*attrlist=*/NULL);
22455 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22456 objc_begin_catch_clause (parm);
22457 cp_parser_compound_statement (parser, NULL, false);
22458 objc_finish_catch_clause ();
22461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22463 cp_lexer_consume_token (parser->lexer);
22464 location = cp_lexer_peek_token (parser->lexer)->location;
22465 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22466 node, lest it get absorbed into the surrounding block. */
22467 stmt = push_stmt_list ();
22468 cp_parser_compound_statement (parser, NULL, false);
22469 objc_build_finally_clause (location, pop_stmt_list (stmt));
22472 return objc_finish_try_stmt ();
22475 /* Parse an Objective-C synchronized statement.
22477 objc-synchronized-stmt:
22478 @synchronized ( expression ) compound-statement
22480 Returns NULL_TREE. */
22483 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22484 location_t location;
22487 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22489 location = cp_lexer_peek_token (parser->lexer)->location;
22490 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22491 lock = cp_parser_expression (parser, false, NULL);
22492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22494 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22495 node, lest it get absorbed into the surrounding block. */
22496 stmt = push_stmt_list ();
22497 cp_parser_compound_statement (parser, NULL, false);
22499 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22502 /* Parse an Objective-C throw statement.
22505 @throw assignment-expression [opt] ;
22507 Returns a constructed '@throw' statement. */
22510 cp_parser_objc_throw_statement (cp_parser *parser) {
22511 tree expr = NULL_TREE;
22512 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22514 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22517 expr = cp_parser_assignment_expression (parser, false, NULL);
22519 cp_parser_consume_semicolon_at_end_of_statement (parser);
22521 return objc_build_throw_stmt (loc, expr);
22524 /* Parse an Objective-C statement. */
22527 cp_parser_objc_statement (cp_parser * parser) {
22528 /* Try to figure out what kind of declaration is present. */
22529 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22531 switch (kwd->keyword)
22534 return cp_parser_objc_try_catch_finally_statement (parser);
22535 case RID_AT_SYNCHRONIZED:
22536 return cp_parser_objc_synchronized_statement (parser);
22538 return cp_parser_objc_throw_statement (parser);
22540 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22542 cp_parser_skip_to_end_of_block_or_statement (parser);
22545 return error_mark_node;
22548 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22549 look ahead to see if an objc keyword follows the attributes. This
22550 is to detect the use of prefix attributes on ObjC @interface and
22554 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22556 cp_lexer_save_tokens (parser->lexer);
22557 *attrib = cp_parser_attributes_opt (parser);
22558 gcc_assert (*attrib);
22559 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22561 cp_lexer_commit_tokens (parser->lexer);
22564 cp_lexer_rollback_tokens (parser->lexer);
22568 /* This routine is a minimal replacement for
22569 c_parser_struct_declaration () used when parsing the list of
22570 types/names or ObjC++ properties. For example, when parsing the
22573 @property (readonly) int a, b, c;
22575 this function is responsible for parsing "int a, int b, int c" and
22576 returning the declarations as CHAIN of DECLs.
22578 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22579 similar parsing. */
22581 cp_parser_objc_struct_declaration (cp_parser *parser)
22583 tree decls = NULL_TREE;
22584 cp_decl_specifier_seq declspecs;
22585 int decl_class_or_enum_p;
22586 tree prefix_attributes;
22588 cp_parser_decl_specifier_seq (parser,
22589 CP_PARSER_FLAGS_NONE,
22591 &decl_class_or_enum_p);
22593 if (declspecs.type == error_mark_node)
22594 return error_mark_node;
22596 /* auto, register, static, extern, mutable. */
22597 if (declspecs.storage_class != sc_none)
22599 cp_parser_error (parser, "invalid type for property");
22600 declspecs.storage_class = sc_none;
22604 if (declspecs.specs[(int) ds_thread])
22606 cp_parser_error (parser, "invalid type for property");
22607 declspecs.specs[(int) ds_thread] = 0;
22611 if (declspecs.specs[(int) ds_typedef])
22613 cp_parser_error (parser, "invalid type for property");
22614 declspecs.specs[(int) ds_typedef] = 0;
22617 prefix_attributes = declspecs.attributes;
22618 declspecs.attributes = NULL_TREE;
22620 /* Keep going until we hit the `;' at the end of the declaration. */
22621 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22623 tree attributes, first_attribute, decl;
22624 cp_declarator *declarator;
22627 /* Parse the declarator. */
22628 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22629 NULL, NULL, false);
22631 /* Look for attributes that apply to the ivar. */
22632 attributes = cp_parser_attributes_opt (parser);
22633 /* Remember which attributes are prefix attributes and
22635 first_attribute = attributes;
22636 /* Combine the attributes. */
22637 attributes = chainon (prefix_attributes, attributes);
22639 decl = grokfield (declarator, &declspecs,
22640 NULL_TREE, /*init_const_expr_p=*/false,
22641 NULL_TREE, attributes);
22643 if (decl == error_mark_node || decl == NULL_TREE)
22644 return error_mark_node;
22646 /* Reset PREFIX_ATTRIBUTES. */
22647 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22648 attributes = TREE_CHAIN (attributes);
22650 TREE_CHAIN (attributes) = NULL_TREE;
22652 DECL_CHAIN (decl) = decls;
22655 token = cp_lexer_peek_token (parser->lexer);
22656 if (token->type == CPP_COMMA)
22658 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22667 /* Parse an Objective-C @property declaration. The syntax is:
22669 objc-property-declaration:
22670 '@property' objc-property-attributes[opt] struct-declaration ;
22672 objc-property-attributes:
22673 '(' objc-property-attribute-list ')'
22675 objc-property-attribute-list:
22676 objc-property-attribute
22677 objc-property-attribute-list, objc-property-attribute
22679 objc-property-attribute
22680 'getter' = identifier
22681 'setter' = identifier
22690 @property NSString *name;
22691 @property (readonly) id object;
22692 @property (retain, nonatomic, getter=getTheName) id name;
22693 @property int a, b, c;
22695 PS: This function is identical to
22696 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22698 cp_parser_objc_at_property_declaration (cp_parser *parser)
22700 /* The following variables hold the attributes of the properties as
22701 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22702 seen. When we see an attribute, we set them to 'true' (if they
22703 are boolean properties) or to the identifier (if they have an
22704 argument, ie, for getter and setter). Note that here we only
22705 parse the list of attributes, check the syntax and accumulate the
22706 attributes that we find. objc_add_property_declaration() will
22707 then process the information. */
22708 bool property_assign = false;
22709 bool property_copy = false;
22710 tree property_getter_ident = NULL_TREE;
22711 bool property_nonatomic = false;
22712 bool property_readonly = false;
22713 bool property_readwrite = false;
22714 bool property_retain = false;
22715 tree property_setter_ident = NULL_TREE;
22716 /* The following two will be removed once @synthesize is
22718 bool property_copies = false;
22719 tree property_ivar_ident = NULL_TREE;
22721 /* 'properties' is the list of properties that we read. Usually a
22722 single one, but maybe more (eg, in "@property int a, b, c;" there
22727 loc = cp_lexer_peek_token (parser->lexer)->location;
22729 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22731 /* Parse the optional attribute list... */
22732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22735 cp_lexer_consume_token (parser->lexer);
22739 bool syntax_error = false;
22740 cp_token *token = cp_lexer_peek_token (parser->lexer);
22743 if (token->type != CPP_NAME)
22745 cp_parser_error (parser, "expected identifier");
22748 keyword = C_RID_CODE (token->u.value);
22749 cp_lexer_consume_token (parser->lexer);
22752 case RID_ASSIGN: property_assign = true; break;
22753 case RID_COPIES: property_copies = true; break;
22754 case RID_COPY: property_copy = true; break;
22755 case RID_NONATOMIC: property_nonatomic = true; break;
22756 case RID_READONLY: property_readonly = true; break;
22757 case RID_READWRITE: property_readwrite = true; break;
22758 case RID_RETAIN: property_retain = true; break;
22763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22765 cp_parser_error (parser,
22766 "getter/setter/ivar attribute must be followed by %<=%>");
22767 syntax_error = true;
22770 cp_lexer_consume_token (parser->lexer); /* eat the = */
22771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22773 cp_parser_error (parser, "expected identifier");
22774 syntax_error = true;
22777 if (keyword == RID_SETTER)
22779 if (property_setter_ident != NULL_TREE)
22780 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22782 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22783 cp_lexer_consume_token (parser->lexer);
22784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22785 cp_parser_error (parser, "setter name must terminate with %<:%>");
22787 cp_lexer_consume_token (parser->lexer);
22789 else if (keyword == RID_GETTER)
22791 if (property_getter_ident != NULL_TREE)
22792 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22794 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22795 cp_lexer_consume_token (parser->lexer);
22797 else /* RID_IVAR, this case will go away. */
22799 if (property_ivar_ident != NULL_TREE)
22800 cp_parser_error (parser, "the %<ivar%> attribute may only be specified once");
22802 property_ivar_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22803 cp_lexer_consume_token (parser->lexer);
22807 cp_parser_error (parser, "unknown property attribute");
22808 syntax_error = true;
22815 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22816 cp_lexer_consume_token (parser->lexer);
22821 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22823 cp_parser_skip_to_closing_parenthesis (parser,
22824 /*recovering=*/true,
22825 /*or_comma=*/false,
22826 /*consume_paren=*/true);
22830 /* ... and the property declaration(s). */
22831 properties = cp_parser_objc_struct_declaration (parser);
22833 if (properties == error_mark_node)
22835 cp_parser_skip_to_end_of_statement (parser);
22836 /* If the next token is now a `;', consume it. */
22837 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22838 cp_lexer_consume_token (parser->lexer);
22842 if (properties == NULL_TREE)
22843 cp_parser_error (parser, "expected identifier");
22846 /* Comma-separated properties are chained together in
22847 reverse order; add them one by one. */
22848 properties = nreverse (properties);
22850 for (; properties; properties = TREE_CHAIN (properties))
22851 objc_add_property_declaration (loc, copy_node (properties),
22852 property_readonly, property_readwrite,
22853 property_assign, property_retain,
22854 property_copy, property_nonatomic,
22855 property_getter_ident, property_setter_ident,
22856 /* The following two will be removed. */
22857 property_copies, property_ivar_ident);
22860 cp_parser_consume_semicolon_at_end_of_statement (parser);
22863 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
22865 objc-synthesize-declaration:
22866 @synthesize objc-synthesize-identifier-list ;
22868 objc-synthesize-identifier-list:
22869 objc-synthesize-identifier
22870 objc-synthesize-identifier-list, objc-synthesize-identifier
22872 objc-synthesize-identifier
22874 identifier = identifier
22877 @synthesize MyProperty;
22878 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
22880 PS: This function is identical to c_parser_objc_at_synthesize_declaration
22881 for C. Keep them in sync.
22884 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
22886 tree list = NULL_TREE;
22888 loc = cp_lexer_peek_token (parser->lexer)->location;
22890 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
22893 tree property, ivar;
22894 property = cp_parser_identifier (parser);
22895 if (property == error_mark_node)
22897 cp_parser_consume_semicolon_at_end_of_statement (parser);
22900 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22902 cp_lexer_consume_token (parser->lexer);
22903 ivar = cp_parser_identifier (parser);
22904 if (ivar == error_mark_node)
22906 cp_parser_consume_semicolon_at_end_of_statement (parser);
22912 list = chainon (list, build_tree_list (ivar, property));
22913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22914 cp_lexer_consume_token (parser->lexer);
22918 cp_parser_consume_semicolon_at_end_of_statement (parser);
22919 objc_add_synthesize_declaration (loc, list);
22922 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
22924 objc-dynamic-declaration:
22925 @dynamic identifier-list ;
22928 @dynamic MyProperty;
22929 @dynamic MyProperty, AnotherProperty;
22931 PS: This function is identical to c_parser_objc_at_dynamic_declaration
22932 for C. Keep them in sync.
22935 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
22937 tree list = NULL_TREE;
22939 loc = cp_lexer_peek_token (parser->lexer)->location;
22941 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
22945 property = cp_parser_identifier (parser);
22946 if (property == error_mark_node)
22948 cp_parser_consume_semicolon_at_end_of_statement (parser);
22951 list = chainon (list, build_tree_list (NULL, property));
22952 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22953 cp_lexer_consume_token (parser->lexer);
22957 cp_parser_consume_semicolon_at_end_of_statement (parser);
22958 objc_add_dynamic_declaration (loc, list);
22962 /* OpenMP 2.5 parsing routines. */
22964 /* Returns name of the next clause.
22965 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22966 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22967 returned and the token is consumed. */
22969 static pragma_omp_clause
22970 cp_parser_omp_clause_name (cp_parser *parser)
22972 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
22974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
22975 result = PRAGMA_OMP_CLAUSE_IF;
22976 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
22977 result = PRAGMA_OMP_CLAUSE_DEFAULT;
22978 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
22979 result = PRAGMA_OMP_CLAUSE_PRIVATE;
22980 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22983 const char *p = IDENTIFIER_POINTER (id);
22988 if (!strcmp ("collapse", p))
22989 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
22990 else if (!strcmp ("copyin", p))
22991 result = PRAGMA_OMP_CLAUSE_COPYIN;
22992 else if (!strcmp ("copyprivate", p))
22993 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
22996 if (!strcmp ("firstprivate", p))
22997 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23000 if (!strcmp ("lastprivate", p))
23001 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23004 if (!strcmp ("nowait", p))
23005 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23006 else if (!strcmp ("num_threads", p))
23007 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23010 if (!strcmp ("ordered", p))
23011 result = PRAGMA_OMP_CLAUSE_ORDERED;
23014 if (!strcmp ("reduction", p))
23015 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23018 if (!strcmp ("schedule", p))
23019 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23020 else if (!strcmp ("shared", p))
23021 result = PRAGMA_OMP_CLAUSE_SHARED;
23024 if (!strcmp ("untied", p))
23025 result = PRAGMA_OMP_CLAUSE_UNTIED;
23030 if (result != PRAGMA_OMP_CLAUSE_NONE)
23031 cp_lexer_consume_token (parser->lexer);
23036 /* Validate that a clause of the given type does not already exist. */
23039 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23040 const char *name, location_t location)
23044 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23045 if (OMP_CLAUSE_CODE (c) == code)
23047 error_at (location, "too many %qs clauses", name);
23055 variable-list , identifier
23057 In addition, we match a closing parenthesis. An opening parenthesis
23058 will have been consumed by the caller.
23060 If KIND is nonzero, create the appropriate node and install the decl
23061 in OMP_CLAUSE_DECL and add the node to the head of the list.
23063 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23064 return the list created. */
23067 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23075 token = cp_lexer_peek_token (parser->lexer);
23076 name = cp_parser_id_expression (parser, /*template_p=*/false,
23077 /*check_dependency_p=*/true,
23078 /*template_p=*/NULL,
23079 /*declarator_p=*/false,
23080 /*optional_p=*/false);
23081 if (name == error_mark_node)
23084 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23085 if (decl == error_mark_node)
23086 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23088 else if (kind != 0)
23090 tree u = build_omp_clause (token->location, kind);
23091 OMP_CLAUSE_DECL (u) = decl;
23092 OMP_CLAUSE_CHAIN (u) = list;
23096 list = tree_cons (decl, NULL_TREE, list);
23099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23101 cp_lexer_consume_token (parser->lexer);
23104 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23108 /* Try to resync to an unnested comma. Copied from
23109 cp_parser_parenthesized_expression_list. */
23111 ending = cp_parser_skip_to_closing_parenthesis (parser,
23112 /*recovering=*/true,
23114 /*consume_paren=*/true);
23122 /* Similarly, but expect leading and trailing parenthesis. This is a very
23123 common case for omp clauses. */
23126 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23128 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23129 return cp_parser_omp_var_list_no_open (parser, kind, list);
23134 collapse ( constant-expression ) */
23137 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23143 loc = cp_lexer_peek_token (parser->lexer)->location;
23144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23147 num = cp_parser_constant_expression (parser, false, NULL);
23149 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23151 /*or_comma=*/false,
23152 /*consume_paren=*/true);
23154 if (num == error_mark_node)
23156 num = fold_non_dependent_expr (num);
23157 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23158 || !host_integerp (num, 0)
23159 || (n = tree_low_cst (num, 0)) <= 0
23162 error_at (loc, "collapse argument needs positive constant integer expression");
23166 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23167 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23168 OMP_CLAUSE_CHAIN (c) = list;
23169 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23175 default ( shared | none ) */
23178 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23180 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23183 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23187 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23188 const char *p = IDENTIFIER_POINTER (id);
23193 if (strcmp ("none", p) != 0)
23195 kind = OMP_CLAUSE_DEFAULT_NONE;
23199 if (strcmp ("shared", p) != 0)
23201 kind = OMP_CLAUSE_DEFAULT_SHARED;
23208 cp_lexer_consume_token (parser->lexer);
23213 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23216 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23217 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23218 /*or_comma=*/false,
23219 /*consume_paren=*/true);
23221 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23224 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23225 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23226 OMP_CLAUSE_CHAIN (c) = list;
23227 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23233 if ( expression ) */
23236 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23243 t = cp_parser_condition (parser);
23245 if (t == error_mark_node
23246 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23248 /*or_comma=*/false,
23249 /*consume_paren=*/true);
23251 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23253 c = build_omp_clause (location, OMP_CLAUSE_IF);
23254 OMP_CLAUSE_IF_EXPR (c) = t;
23255 OMP_CLAUSE_CHAIN (c) = list;
23264 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23265 tree list, location_t location)
23269 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23271 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23272 OMP_CLAUSE_CHAIN (c) = list;
23277 num_threads ( expression ) */
23280 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23281 location_t location)
23285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23288 t = cp_parser_expression (parser, false, NULL);
23290 if (t == error_mark_node
23291 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23293 /*or_comma=*/false,
23294 /*consume_paren=*/true);
23296 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23297 "num_threads", location);
23299 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23300 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23301 OMP_CLAUSE_CHAIN (c) = list;
23310 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23311 tree list, location_t location)
23315 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23316 "ordered", location);
23318 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23319 OMP_CLAUSE_CHAIN (c) = list;
23324 reduction ( reduction-operator : variable-list )
23326 reduction-operator:
23327 One of: + * - & ^ | && || */
23330 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23332 enum tree_code code;
23335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23338 switch (cp_lexer_peek_token (parser->lexer)->type)
23350 code = BIT_AND_EXPR;
23353 code = BIT_XOR_EXPR;
23356 code = BIT_IOR_EXPR;
23359 code = TRUTH_ANDIF_EXPR;
23362 code = TRUTH_ORIF_EXPR;
23365 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23366 "%<|%>, %<&&%>, or %<||%>");
23368 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23369 /*or_comma=*/false,
23370 /*consume_paren=*/true);
23373 cp_lexer_consume_token (parser->lexer);
23375 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23378 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23379 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23380 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23386 schedule ( schedule-kind )
23387 schedule ( schedule-kind , expression )
23390 static | dynamic | guided | runtime | auto */
23393 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23400 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23405 const char *p = IDENTIFIER_POINTER (id);
23410 if (strcmp ("dynamic", p) != 0)
23412 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23416 if (strcmp ("guided", p) != 0)
23418 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23422 if (strcmp ("runtime", p) != 0)
23424 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23431 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23432 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23433 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23434 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23437 cp_lexer_consume_token (parser->lexer);
23439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23442 cp_lexer_consume_token (parser->lexer);
23444 token = cp_lexer_peek_token (parser->lexer);
23445 t = cp_parser_assignment_expression (parser, false, NULL);
23447 if (t == error_mark_node)
23449 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23450 error_at (token->location, "schedule %<runtime%> does not take "
23451 "a %<chunk_size%> parameter");
23452 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23453 error_at (token->location, "schedule %<auto%> does not take "
23454 "a %<chunk_size%> parameter");
23456 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23458 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23461 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23464 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23465 OMP_CLAUSE_CHAIN (c) = list;
23469 cp_parser_error (parser, "invalid schedule kind");
23471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23472 /*or_comma=*/false,
23473 /*consume_paren=*/true);
23481 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23482 tree list, location_t location)
23486 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23488 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23489 OMP_CLAUSE_CHAIN (c) = list;
23493 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23494 is a bitmask in MASK. Return the list of clauses found; the result
23495 of clause default goes in *pdefault. */
23498 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23499 const char *where, cp_token *pragma_tok)
23501 tree clauses = NULL;
23503 cp_token *token = NULL;
23505 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23507 pragma_omp_clause c_kind;
23508 const char *c_name;
23509 tree prev = clauses;
23511 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23512 cp_lexer_consume_token (parser->lexer);
23514 token = cp_lexer_peek_token (parser->lexer);
23515 c_kind = cp_parser_omp_clause_name (parser);
23520 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23521 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23523 c_name = "collapse";
23525 case PRAGMA_OMP_CLAUSE_COPYIN:
23526 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23529 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23530 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23532 c_name = "copyprivate";
23534 case PRAGMA_OMP_CLAUSE_DEFAULT:
23535 clauses = cp_parser_omp_clause_default (parser, clauses,
23537 c_name = "default";
23539 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23542 c_name = "firstprivate";
23544 case PRAGMA_OMP_CLAUSE_IF:
23545 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23548 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23549 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23551 c_name = "lastprivate";
23553 case PRAGMA_OMP_CLAUSE_NOWAIT:
23554 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23557 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23558 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23560 c_name = "num_threads";
23562 case PRAGMA_OMP_CLAUSE_ORDERED:
23563 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23565 c_name = "ordered";
23567 case PRAGMA_OMP_CLAUSE_PRIVATE:
23568 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23570 c_name = "private";
23572 case PRAGMA_OMP_CLAUSE_REDUCTION:
23573 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23574 c_name = "reduction";
23576 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23577 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23579 c_name = "schedule";
23581 case PRAGMA_OMP_CLAUSE_SHARED:
23582 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23586 case PRAGMA_OMP_CLAUSE_UNTIED:
23587 clauses = cp_parser_omp_clause_untied (parser, clauses,
23592 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23596 if (((mask >> c_kind) & 1) == 0)
23598 /* Remove the invalid clause(s) from the list to avoid
23599 confusing the rest of the compiler. */
23601 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23605 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23606 return finish_omp_clauses (clauses);
23613 In practice, we're also interested in adding the statement to an
23614 outer node. So it is convenient if we work around the fact that
23615 cp_parser_statement calls add_stmt. */
23618 cp_parser_begin_omp_structured_block (cp_parser *parser)
23620 unsigned save = parser->in_statement;
23622 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23623 This preserves the "not within loop or switch" style error messages
23624 for nonsense cases like
23630 if (parser->in_statement)
23631 parser->in_statement = IN_OMP_BLOCK;
23637 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23639 parser->in_statement = save;
23643 cp_parser_omp_structured_block (cp_parser *parser)
23645 tree stmt = begin_omp_structured_block ();
23646 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23648 cp_parser_statement (parser, NULL_TREE, false, NULL);
23650 cp_parser_end_omp_structured_block (parser, save);
23651 return finish_omp_structured_block (stmt);
23655 # pragma omp atomic new-line
23659 x binop= expr | x++ | ++x | x-- | --x
23661 +, *, -, /, &, ^, |, <<, >>
23663 where x is an lvalue expression with scalar type. */
23666 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23669 enum tree_code code;
23671 cp_parser_require_pragma_eol (parser, pragma_tok);
23673 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23674 /*cast_p=*/false, NULL);
23675 switch (TREE_CODE (lhs))
23680 case PREINCREMENT_EXPR:
23681 case POSTINCREMENT_EXPR:
23682 lhs = TREE_OPERAND (lhs, 0);
23684 rhs = integer_one_node;
23687 case PREDECREMENT_EXPR:
23688 case POSTDECREMENT_EXPR:
23689 lhs = TREE_OPERAND (lhs, 0);
23691 rhs = integer_one_node;
23694 case COMPOUND_EXPR:
23695 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23696 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23697 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23698 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23699 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23700 (TREE_OPERAND (lhs, 1), 0), 0)))
23702 /* Undo effects of boolean_increment for post {in,de}crement. */
23703 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23706 if (TREE_CODE (lhs) == MODIFY_EXPR
23707 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23709 /* Undo effects of boolean_increment. */
23710 if (integer_onep (TREE_OPERAND (lhs, 1)))
23712 /* This is pre or post increment. */
23713 rhs = TREE_OPERAND (lhs, 1);
23714 lhs = TREE_OPERAND (lhs, 0);
23721 switch (cp_lexer_peek_token (parser->lexer)->type)
23727 code = TRUNC_DIV_EXPR;
23735 case CPP_LSHIFT_EQ:
23736 code = LSHIFT_EXPR;
23738 case CPP_RSHIFT_EQ:
23739 code = RSHIFT_EXPR;
23742 code = BIT_AND_EXPR;
23745 code = BIT_IOR_EXPR;
23748 code = BIT_XOR_EXPR;
23751 cp_parser_error (parser,
23752 "invalid operator for %<#pragma omp atomic%>");
23755 cp_lexer_consume_token (parser->lexer);
23757 rhs = cp_parser_expression (parser, false, NULL);
23758 if (rhs == error_mark_node)
23762 finish_omp_atomic (code, lhs, rhs);
23763 cp_parser_consume_semicolon_at_end_of_statement (parser);
23767 cp_parser_skip_to_end_of_block_or_statement (parser);
23772 # pragma omp barrier new-line */
23775 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23777 cp_parser_require_pragma_eol (parser, pragma_tok);
23778 finish_omp_barrier ();
23782 # pragma omp critical [(name)] new-line
23783 structured-block */
23786 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23788 tree stmt, name = NULL;
23790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23792 cp_lexer_consume_token (parser->lexer);
23794 name = cp_parser_identifier (parser);
23796 if (name == error_mark_node
23797 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23798 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23799 /*or_comma=*/false,
23800 /*consume_paren=*/true);
23801 if (name == error_mark_node)
23804 cp_parser_require_pragma_eol (parser, pragma_tok);
23806 stmt = cp_parser_omp_structured_block (parser);
23807 return c_finish_omp_critical (input_location, stmt, name);
23811 # pragma omp flush flush-vars[opt] new-line
23814 ( variable-list ) */
23817 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23820 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23821 cp_parser_require_pragma_eol (parser, pragma_tok);
23823 finish_omp_flush ();
23826 /* Helper function, to parse omp for increment expression. */
23829 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23831 tree cond = cp_parser_binary_expression (parser, false, true,
23832 PREC_NOT_OPERATOR, NULL);
23835 if (cond == error_mark_node
23836 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23838 cp_parser_skip_to_end_of_statement (parser);
23839 return error_mark_node;
23842 switch (TREE_CODE (cond))
23850 return error_mark_node;
23853 /* If decl is an iterator, preserve LHS and RHS of the relational
23854 expr until finish_omp_for. */
23856 && (type_dependent_expression_p (decl)
23857 || CLASS_TYPE_P (TREE_TYPE (decl))))
23860 return build_x_binary_op (TREE_CODE (cond),
23861 TREE_OPERAND (cond, 0), ERROR_MARK,
23862 TREE_OPERAND (cond, 1), ERROR_MARK,
23863 &overloaded_p, tf_warning_or_error);
23866 /* Helper function, to parse omp for increment expression. */
23869 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
23871 cp_token *token = cp_lexer_peek_token (parser->lexer);
23877 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23879 op = (token->type == CPP_PLUS_PLUS
23880 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
23881 cp_lexer_consume_token (parser->lexer);
23882 lhs = cp_parser_cast_expression (parser, false, false, NULL);
23884 return error_mark_node;
23885 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23888 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
23890 return error_mark_node;
23892 token = cp_lexer_peek_token (parser->lexer);
23893 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
23895 op = (token->type == CPP_PLUS_PLUS
23896 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
23897 cp_lexer_consume_token (parser->lexer);
23898 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
23901 op = cp_parser_assignment_operator_opt (parser);
23902 if (op == ERROR_MARK)
23903 return error_mark_node;
23905 if (op != NOP_EXPR)
23907 rhs = cp_parser_assignment_expression (parser, false, NULL);
23908 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
23909 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23912 lhs = cp_parser_binary_expression (parser, false, false,
23913 PREC_ADDITIVE_EXPRESSION, NULL);
23914 token = cp_lexer_peek_token (parser->lexer);
23915 decl_first = lhs == decl;
23918 if (token->type != CPP_PLUS
23919 && token->type != CPP_MINUS)
23920 return error_mark_node;
23924 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
23925 cp_lexer_consume_token (parser->lexer);
23926 rhs = cp_parser_binary_expression (parser, false, false,
23927 PREC_ADDITIVE_EXPRESSION, NULL);
23928 token = cp_lexer_peek_token (parser->lexer);
23929 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
23931 if (lhs == NULL_TREE)
23933 if (op == PLUS_EXPR)
23936 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
23939 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
23940 NULL, tf_warning_or_error);
23943 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
23947 if (rhs != decl || op == MINUS_EXPR)
23948 return error_mark_node;
23949 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
23952 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
23954 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
23957 /* Parse the restricted form of the for statement allowed by OpenMP. */
23960 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
23962 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
23963 tree real_decl, initv, condv, incrv, declv;
23964 tree this_pre_body, cl;
23965 location_t loc_first;
23966 bool collapse_err = false;
23967 int i, collapse = 1, nbraces = 0;
23968 VEC(tree,gc) *for_block = make_tree_vector ();
23970 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
23971 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
23972 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
23974 gcc_assert (collapse >= 1);
23976 declv = make_tree_vec (collapse);
23977 initv = make_tree_vec (collapse);
23978 condv = make_tree_vec (collapse);
23979 incrv = make_tree_vec (collapse);
23981 loc_first = cp_lexer_peek_token (parser->lexer)->location;
23983 for (i = 0; i < collapse; i++)
23985 int bracecount = 0;
23986 bool add_private_clause = false;
23989 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
23991 cp_parser_error (parser, "for statement expected");
23994 loc = cp_lexer_consume_token (parser->lexer)->location;
23996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23999 init = decl = real_decl = NULL;
24000 this_pre_body = push_stmt_list ();
24001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24003 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24007 integer-type var = lb
24008 random-access-iterator-type var = lb
24009 pointer-type var = lb
24011 cp_decl_specifier_seq type_specifiers;
24013 /* First, try to parse as an initialized declaration. See
24014 cp_parser_condition, from whence the bulk of this is copied. */
24016 cp_parser_parse_tentatively (parser);
24017 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24018 /*is_trailing_return=*/false,
24020 if (cp_parser_parse_definitely (parser))
24022 /* If parsing a type specifier seq succeeded, then this
24023 MUST be a initialized declaration. */
24024 tree asm_specification, attributes;
24025 cp_declarator *declarator;
24027 declarator = cp_parser_declarator (parser,
24028 CP_PARSER_DECLARATOR_NAMED,
24029 /*ctor_dtor_or_conv_p=*/NULL,
24030 /*parenthesized_p=*/NULL,
24031 /*member_p=*/false);
24032 attributes = cp_parser_attributes_opt (parser);
24033 asm_specification = cp_parser_asm_specification_opt (parser);
24035 if (declarator == cp_error_declarator)
24036 cp_parser_skip_to_end_of_statement (parser);
24040 tree pushed_scope, auto_node;
24042 decl = start_decl (declarator, &type_specifiers,
24043 SD_INITIALIZED, attributes,
24044 /*prefix_attributes=*/NULL_TREE,
24047 auto_node = type_uses_auto (TREE_TYPE (decl));
24048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24050 if (cp_lexer_next_token_is (parser->lexer,
24052 error ("parenthesized initialization is not allowed in "
24053 "OpenMP %<for%> loop");
24055 /* Trigger an error. */
24056 cp_parser_require (parser, CPP_EQ, RT_EQ);
24058 init = error_mark_node;
24059 cp_parser_skip_to_end_of_statement (parser);
24061 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24062 || type_dependent_expression_p (decl)
24065 bool is_direct_init, is_non_constant_init;
24067 init = cp_parser_initializer (parser,
24069 &is_non_constant_init);
24071 if (auto_node && describable_type (init))
24074 = do_auto_deduction (TREE_TYPE (decl), init,
24077 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24078 && !type_dependent_expression_p (decl))
24082 cp_finish_decl (decl, init, !is_non_constant_init,
24084 LOOKUP_ONLYCONVERTING);
24085 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24087 VEC_safe_push (tree, gc, for_block, this_pre_body);
24091 init = pop_stmt_list (this_pre_body);
24092 this_pre_body = NULL_TREE;
24097 cp_lexer_consume_token (parser->lexer);
24098 init = cp_parser_assignment_expression (parser, false, NULL);
24101 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24102 init = error_mark_node;
24104 cp_finish_decl (decl, NULL_TREE,
24105 /*init_const_expr_p=*/false,
24107 LOOKUP_ONLYCONVERTING);
24111 pop_scope (pushed_scope);
24117 /* If parsing a type specifier sequence failed, then
24118 this MUST be a simple expression. */
24119 cp_parser_parse_tentatively (parser);
24120 decl = cp_parser_primary_expression (parser, false, false,
24122 if (!cp_parser_error_occurred (parser)
24125 && CLASS_TYPE_P (TREE_TYPE (decl)))
24129 cp_parser_parse_definitely (parser);
24130 cp_parser_require (parser, CPP_EQ, RT_EQ);
24131 rhs = cp_parser_assignment_expression (parser, false, NULL);
24132 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24134 tf_warning_or_error));
24135 add_private_clause = true;
24140 cp_parser_abort_tentative_parse (parser);
24141 init = cp_parser_expression (parser, false, NULL);
24144 if (TREE_CODE (init) == MODIFY_EXPR
24145 || TREE_CODE (init) == MODOP_EXPR)
24146 real_decl = TREE_OPERAND (init, 0);
24151 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24154 this_pre_body = pop_stmt_list (this_pre_body);
24158 pre_body = push_stmt_list ();
24160 add_stmt (this_pre_body);
24161 pre_body = pop_stmt_list (pre_body);
24164 pre_body = this_pre_body;
24169 if (par_clauses != NULL && real_decl != NULL_TREE)
24172 for (c = par_clauses; *c ; )
24173 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24174 && OMP_CLAUSE_DECL (*c) == real_decl)
24176 error_at (loc, "iteration variable %qD"
24177 " should not be firstprivate", real_decl);
24178 *c = OMP_CLAUSE_CHAIN (*c);
24180 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24181 && OMP_CLAUSE_DECL (*c) == real_decl)
24183 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24184 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24185 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24186 OMP_CLAUSE_DECL (l) = real_decl;
24187 OMP_CLAUSE_CHAIN (l) = clauses;
24188 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24190 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24191 CP_OMP_CLAUSE_INFO (*c) = NULL;
24192 add_private_clause = false;
24196 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24197 && OMP_CLAUSE_DECL (*c) == real_decl)
24198 add_private_clause = false;
24199 c = &OMP_CLAUSE_CHAIN (*c);
24203 if (add_private_clause)
24206 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24208 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24209 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24210 && OMP_CLAUSE_DECL (c) == decl)
24212 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24213 && OMP_CLAUSE_DECL (c) == decl)
24214 error_at (loc, "iteration variable %qD "
24215 "should not be firstprivate",
24217 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24218 && OMP_CLAUSE_DECL (c) == decl)
24219 error_at (loc, "iteration variable %qD should not be reduction",
24224 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24225 OMP_CLAUSE_DECL (c) = decl;
24226 c = finish_omp_clauses (c);
24229 OMP_CLAUSE_CHAIN (c) = clauses;
24236 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24237 cond = cp_parser_omp_for_cond (parser, decl);
24238 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24243 /* If decl is an iterator, preserve the operator on decl
24244 until finish_omp_for. */
24246 && (type_dependent_expression_p (decl)
24247 || CLASS_TYPE_P (TREE_TYPE (decl))))
24248 incr = cp_parser_omp_for_incr (parser, decl);
24250 incr = cp_parser_expression (parser, false, NULL);
24253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24255 /*or_comma=*/false,
24256 /*consume_paren=*/true);
24258 TREE_VEC_ELT (declv, i) = decl;
24259 TREE_VEC_ELT (initv, i) = init;
24260 TREE_VEC_ELT (condv, i) = cond;
24261 TREE_VEC_ELT (incrv, i) = incr;
24263 if (i == collapse - 1)
24266 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24267 in between the collapsed for loops to be still considered perfectly
24268 nested. Hopefully the final version clarifies this.
24269 For now handle (multiple) {'s and empty statements. */
24270 cp_parser_parse_tentatively (parser);
24273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24275 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24277 cp_lexer_consume_token (parser->lexer);
24280 else if (bracecount
24281 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24282 cp_lexer_consume_token (parser->lexer);
24285 loc = cp_lexer_peek_token (parser->lexer)->location;
24286 error_at (loc, "not enough collapsed for loops");
24287 collapse_err = true;
24288 cp_parser_abort_tentative_parse (parser);
24297 cp_parser_parse_definitely (parser);
24298 nbraces += bracecount;
24302 /* Note that we saved the original contents of this flag when we entered
24303 the structured block, and so we don't need to re-save it here. */
24304 parser->in_statement = IN_OMP_FOR;
24306 /* Note that the grammar doesn't call for a structured block here,
24307 though the loop as a whole is a structured block. */
24308 body = push_stmt_list ();
24309 cp_parser_statement (parser, NULL_TREE, false, NULL);
24310 body = pop_stmt_list (body);
24312 if (declv == NULL_TREE)
24315 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24316 pre_body, clauses);
24320 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24322 cp_lexer_consume_token (parser->lexer);
24325 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24326 cp_lexer_consume_token (parser->lexer);
24331 error_at (cp_lexer_peek_token (parser->lexer)->location,
24332 "collapsed loops not perfectly nested");
24334 collapse_err = true;
24335 cp_parser_statement_seq_opt (parser, NULL);
24336 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24341 while (!VEC_empty (tree, for_block))
24342 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24343 release_tree_vector (for_block);
24349 #pragma omp for for-clause[optseq] new-line
24352 #define OMP_FOR_CLAUSE_MASK \
24353 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24354 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24355 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24356 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24357 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24358 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24359 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24360 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24363 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24365 tree clauses, sb, ret;
24368 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24369 "#pragma omp for", pragma_tok);
24371 sb = begin_omp_structured_block ();
24372 save = cp_parser_begin_omp_structured_block (parser);
24374 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24376 cp_parser_end_omp_structured_block (parser, save);
24377 add_stmt (finish_omp_structured_block (sb));
24383 # pragma omp master new-line
24384 structured-block */
24387 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24389 cp_parser_require_pragma_eol (parser, pragma_tok);
24390 return c_finish_omp_master (input_location,
24391 cp_parser_omp_structured_block (parser));
24395 # pragma omp ordered new-line
24396 structured-block */
24399 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24402 cp_parser_require_pragma_eol (parser, pragma_tok);
24403 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24409 { section-sequence }
24412 section-directive[opt] structured-block
24413 section-sequence section-directive structured-block */
24416 cp_parser_omp_sections_scope (cp_parser *parser)
24418 tree stmt, substmt;
24419 bool error_suppress = false;
24422 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24425 stmt = push_stmt_list ();
24427 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24431 substmt = begin_omp_structured_block ();
24432 save = cp_parser_begin_omp_structured_block (parser);
24436 cp_parser_statement (parser, NULL_TREE, false, NULL);
24438 tok = cp_lexer_peek_token (parser->lexer);
24439 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24441 if (tok->type == CPP_CLOSE_BRACE)
24443 if (tok->type == CPP_EOF)
24447 cp_parser_end_omp_structured_block (parser, save);
24448 substmt = finish_omp_structured_block (substmt);
24449 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24450 add_stmt (substmt);
24455 tok = cp_lexer_peek_token (parser->lexer);
24456 if (tok->type == CPP_CLOSE_BRACE)
24458 if (tok->type == CPP_EOF)
24461 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24463 cp_lexer_consume_token (parser->lexer);
24464 cp_parser_require_pragma_eol (parser, tok);
24465 error_suppress = false;
24467 else if (!error_suppress)
24469 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24470 error_suppress = true;
24473 substmt = cp_parser_omp_structured_block (parser);
24474 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24475 add_stmt (substmt);
24477 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24479 substmt = pop_stmt_list (stmt);
24481 stmt = make_node (OMP_SECTIONS);
24482 TREE_TYPE (stmt) = void_type_node;
24483 OMP_SECTIONS_BODY (stmt) = substmt;
24490 # pragma omp sections sections-clause[optseq] newline
24493 #define OMP_SECTIONS_CLAUSE_MASK \
24494 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24495 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24496 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24497 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24498 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24501 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24505 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24506 "#pragma omp sections", pragma_tok);
24508 ret = cp_parser_omp_sections_scope (parser);
24510 OMP_SECTIONS_CLAUSES (ret) = clauses;
24516 # pragma parallel parallel-clause new-line
24517 # pragma parallel for parallel-for-clause new-line
24518 # pragma parallel sections parallel-sections-clause new-line */
24520 #define OMP_PARALLEL_CLAUSE_MASK \
24521 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24522 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24523 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24524 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24525 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24526 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24527 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24528 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24531 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24533 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24534 const char *p_name = "#pragma omp parallel";
24535 tree stmt, clauses, par_clause, ws_clause, block;
24536 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24538 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24542 cp_lexer_consume_token (parser->lexer);
24543 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24544 p_name = "#pragma omp parallel for";
24545 mask |= OMP_FOR_CLAUSE_MASK;
24546 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24548 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24551 const char *p = IDENTIFIER_POINTER (id);
24552 if (strcmp (p, "sections") == 0)
24554 cp_lexer_consume_token (parser->lexer);
24555 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24556 p_name = "#pragma omp parallel sections";
24557 mask |= OMP_SECTIONS_CLAUSE_MASK;
24558 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24562 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24563 block = begin_omp_parallel ();
24564 save = cp_parser_begin_omp_structured_block (parser);
24568 case PRAGMA_OMP_PARALLEL:
24569 cp_parser_statement (parser, NULL_TREE, false, NULL);
24570 par_clause = clauses;
24573 case PRAGMA_OMP_PARALLEL_FOR:
24574 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24575 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24578 case PRAGMA_OMP_PARALLEL_SECTIONS:
24579 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24580 stmt = cp_parser_omp_sections_scope (parser);
24582 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24586 gcc_unreachable ();
24589 cp_parser_end_omp_structured_block (parser, save);
24590 stmt = finish_omp_parallel (par_clause, block);
24591 if (p_kind != PRAGMA_OMP_PARALLEL)
24592 OMP_PARALLEL_COMBINED (stmt) = 1;
24597 # pragma omp single single-clause[optseq] new-line
24598 structured-block */
24600 #define OMP_SINGLE_CLAUSE_MASK \
24601 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24602 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24603 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24604 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24607 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24609 tree stmt = make_node (OMP_SINGLE);
24610 TREE_TYPE (stmt) = void_type_node;
24612 OMP_SINGLE_CLAUSES (stmt)
24613 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24614 "#pragma omp single", pragma_tok);
24615 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24617 return add_stmt (stmt);
24621 # pragma omp task task-clause[optseq] new-line
24622 structured-block */
24624 #define OMP_TASK_CLAUSE_MASK \
24625 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24626 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24627 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24628 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24629 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24630 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24633 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24635 tree clauses, block;
24638 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24639 "#pragma omp task", pragma_tok);
24640 block = begin_omp_task ();
24641 save = cp_parser_begin_omp_structured_block (parser);
24642 cp_parser_statement (parser, NULL_TREE, false, NULL);
24643 cp_parser_end_omp_structured_block (parser, save);
24644 return finish_omp_task (clauses, block);
24648 # pragma omp taskwait new-line */
24651 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24653 cp_parser_require_pragma_eol (parser, pragma_tok);
24654 finish_omp_taskwait ();
24658 # pragma omp threadprivate (variable-list) */
24661 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24665 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24666 cp_parser_require_pragma_eol (parser, pragma_tok);
24668 finish_omp_threadprivate (vars);
24671 /* Main entry point to OpenMP statement pragmas. */
24674 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24678 switch (pragma_tok->pragma_kind)
24680 case PRAGMA_OMP_ATOMIC:
24681 cp_parser_omp_atomic (parser, pragma_tok);
24683 case PRAGMA_OMP_CRITICAL:
24684 stmt = cp_parser_omp_critical (parser, pragma_tok);
24686 case PRAGMA_OMP_FOR:
24687 stmt = cp_parser_omp_for (parser, pragma_tok);
24689 case PRAGMA_OMP_MASTER:
24690 stmt = cp_parser_omp_master (parser, pragma_tok);
24692 case PRAGMA_OMP_ORDERED:
24693 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24695 case PRAGMA_OMP_PARALLEL:
24696 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24698 case PRAGMA_OMP_SECTIONS:
24699 stmt = cp_parser_omp_sections (parser, pragma_tok);
24701 case PRAGMA_OMP_SINGLE:
24702 stmt = cp_parser_omp_single (parser, pragma_tok);
24704 case PRAGMA_OMP_TASK:
24705 stmt = cp_parser_omp_task (parser, pragma_tok);
24708 gcc_unreachable ();
24712 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24717 static GTY (()) cp_parser *the_parser;
24720 /* Special handling for the first token or line in the file. The first
24721 thing in the file might be #pragma GCC pch_preprocess, which loads a
24722 PCH file, which is a GC collection point. So we need to handle this
24723 first pragma without benefit of an existing lexer structure.
24725 Always returns one token to the caller in *FIRST_TOKEN. This is
24726 either the true first token of the file, or the first token after
24727 the initial pragma. */
24730 cp_parser_initial_pragma (cp_token *first_token)
24734 cp_lexer_get_preprocessor_token (NULL, first_token);
24735 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24738 cp_lexer_get_preprocessor_token (NULL, first_token);
24739 if (first_token->type == CPP_STRING)
24741 name = first_token->u.value;
24743 cp_lexer_get_preprocessor_token (NULL, first_token);
24744 if (first_token->type != CPP_PRAGMA_EOL)
24745 error_at (first_token->location,
24746 "junk at end of %<#pragma GCC pch_preprocess%>");
24749 error_at (first_token->location, "expected string literal");
24751 /* Skip to the end of the pragma. */
24752 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24753 cp_lexer_get_preprocessor_token (NULL, first_token);
24755 /* Now actually load the PCH file. */
24757 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24759 /* Read one more token to return to our caller. We have to do this
24760 after reading the PCH file in, since its pointers have to be
24762 cp_lexer_get_preprocessor_token (NULL, first_token);
24765 /* Normal parsing of a pragma token. Here we can (and must) use the
24769 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24771 cp_token *pragma_tok;
24774 pragma_tok = cp_lexer_consume_token (parser->lexer);
24775 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24776 parser->lexer->in_pragma = true;
24778 id = pragma_tok->pragma_kind;
24781 case PRAGMA_GCC_PCH_PREPROCESS:
24782 error_at (pragma_tok->location,
24783 "%<#pragma GCC pch_preprocess%> must be first");
24786 case PRAGMA_OMP_BARRIER:
24789 case pragma_compound:
24790 cp_parser_omp_barrier (parser, pragma_tok);
24793 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24794 "used in compound statements");
24801 case PRAGMA_OMP_FLUSH:
24804 case pragma_compound:
24805 cp_parser_omp_flush (parser, pragma_tok);
24808 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24809 "used in compound statements");
24816 case PRAGMA_OMP_TASKWAIT:
24819 case pragma_compound:
24820 cp_parser_omp_taskwait (parser, pragma_tok);
24823 error_at (pragma_tok->location,
24824 "%<#pragma omp taskwait%> may only be "
24825 "used in compound statements");
24832 case PRAGMA_OMP_THREADPRIVATE:
24833 cp_parser_omp_threadprivate (parser, pragma_tok);
24836 case PRAGMA_OMP_ATOMIC:
24837 case PRAGMA_OMP_CRITICAL:
24838 case PRAGMA_OMP_FOR:
24839 case PRAGMA_OMP_MASTER:
24840 case PRAGMA_OMP_ORDERED:
24841 case PRAGMA_OMP_PARALLEL:
24842 case PRAGMA_OMP_SECTIONS:
24843 case PRAGMA_OMP_SINGLE:
24844 case PRAGMA_OMP_TASK:
24845 if (context == pragma_external)
24847 cp_parser_omp_construct (parser, pragma_tok);
24850 case PRAGMA_OMP_SECTION:
24851 error_at (pragma_tok->location,
24852 "%<#pragma omp section%> may only be used in "
24853 "%<#pragma omp sections%> construct");
24857 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
24858 c_invoke_pragma_handler (id);
24862 cp_parser_error (parser, "expected declaration specifiers");
24866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24870 /* The interface the pragma parsers have to the lexer. */
24873 pragma_lex (tree *value)
24876 enum cpp_ttype ret;
24878 tok = cp_lexer_peek_token (the_parser->lexer);
24881 *value = tok->u.value;
24883 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
24885 else if (ret == CPP_STRING)
24886 *value = cp_parser_string_literal (the_parser, false, false);
24889 cp_lexer_consume_token (the_parser->lexer);
24890 if (ret == CPP_KEYWORD)
24898 /* External interface. */
24900 /* Parse one entire translation unit. */
24903 c_parse_file (void)
24905 static bool already_called = false;
24907 if (already_called)
24909 sorry ("inter-module optimizations not implemented for C++");
24912 already_called = true;
24914 the_parser = cp_parser_new ();
24915 push_deferring_access_checks (flag_access_control
24916 ? dk_no_deferred : dk_no_check);
24917 cp_parser_translation_unit (the_parser);
24921 #include "gt-cp-parser.h"