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)
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
508 lexer->next_token = cp_lexer_token_at (lexer, pos);
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
514 if (lexer->next_token == &eof_token)
515 return lexer->last_token - 1;
517 return cp_lexer_token_position (lexer, true);
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
523 cp_token_position tp = cp_lexer_previous_token_position (lexer);
525 return cp_lexer_token_at (lexer, tp);
528 /* nonzero if we are presently saving tokens. */
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
533 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
536 /* Store the next token from the preprocessor in *TOKEN. Return true
537 if we reach EOF. If LEXER is NULL, assume we are handling an
538 initial #pragma pch_preprocess, and thus want the lexer to return
539 processed strings. */
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
544 static int is_extern_c = 0;
546 /* Get a new token from the preprocessor. */
548 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550 token->keyword = RID_MAX;
551 token->pragma_kind = PRAGMA_NONE;
553 /* On some systems, some header files are surrounded by an
554 implicit extern "C" block. Set a flag in the token if it
555 comes from such a header. */
556 is_extern_c += pending_lang_change;
557 pending_lang_change = 0;
558 token->implicit_extern_c = is_extern_c > 0;
560 /* Check to see if this token is a keyword. */
561 if (token->type == CPP_NAME)
563 if (C_IS_RESERVED_WORD (token->u.value))
565 /* Mark this token as a keyword. */
566 token->type = CPP_KEYWORD;
567 /* Record which keyword. */
568 token->keyword = C_RID_CODE (token->u.value);
572 if (warn_cxx0x_compat
573 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
576 /* Warn about the C++0x keyword (but still treat it as
578 warning (OPT_Wc__0x_compat,
579 "identifier %qE will become a keyword in C++0x",
582 /* Clear out the C_RID_CODE so we don't warn about this
583 particular identifier-turned-keyword again. */
584 C_SET_RID_CODE (token->u.value, RID_MAX);
587 token->ambiguous_p = false;
588 token->keyword = RID_MAX;
591 else if (token->type == CPP_AT_NAME)
593 /* This only happens in Objective-C++; it must be a keyword. */
594 token->type = CPP_KEYWORD;
595 switch (C_RID_CODE (token->u.value))
597 /* Replace 'class' with '@class', 'private' with '@private',
598 etc. This prevents confusion with the C++ keyword
599 'class', and makes the tokens consistent with other
600 Objective-C 'AT' keywords. For example '@class' is
601 reported as RID_AT_CLASS which is consistent with
602 '@synchronized', which is reported as
605 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
606 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
607 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
609 case RID_THROW: token->keyword = RID_AT_THROW; break;
610 case RID_TRY: token->keyword = RID_AT_TRY; break;
611 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
612 default: token->keyword = C_RID_CODE (token->u.value);
615 else if (token->type == CPP_PRAGMA)
617 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
618 token->pragma_kind = ((enum pragma_kind)
619 TREE_INT_CST_LOW (token->u.value));
620 token->u.value = NULL_TREE;
624 /* Update the globals input_location and the input file stack from TOKEN. */
626 cp_lexer_set_source_position_from_token (cp_token *token)
628 if (token->type != CPP_EOF)
630 input_location = token->location;
634 /* Return a pointer to the next token in the token stream, but do not
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
640 if (cp_lexer_debugging_p (lexer))
642 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644 putc ('\n', cp_lexer_debug_stream);
646 return lexer->next_token;
649 /* Return true if the next token has the indicated TYPE. */
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
654 return cp_lexer_peek_token (lexer)->type == type;
657 /* Return true if the next token does not have the indicated TYPE. */
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
662 return !cp_lexer_next_token_is (lexer, type);
665 /* Return true if the next token is the indicated KEYWORD. */
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
670 return cp_lexer_peek_token (lexer)->keyword == keyword;
673 /* Return true if the next token is not the indicated KEYWORD. */
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
678 return cp_lexer_peek_token (lexer)->keyword != keyword;
681 /* Return true if the next token is a keyword for a decl-specifier. */
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
688 token = cp_lexer_peek_token (lexer);
689 switch (token->keyword)
691 /* auto specifier: storage-class-specifier in C++,
692 simple-type-specifier in C++0x. */
694 /* Storage classes. */
700 /* Elaborated type specifiers. */
706 /* Simple type specifiers. */
721 /* GNU extensions. */
724 /* C++0x extensions. */
733 /* Return a pointer to the Nth token in the token stream. If N is 1,
734 then this is precisely equivalent to cp_lexer_peek_token (except
735 that it is not inline). One would like to disallow that case, but
736 there is one case (cp_parser_nth_token_starts_template_id) where
737 the caller passes a variable for N and it might be 1. */
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
744 /* N is 1-based, not zero-based. */
747 if (cp_lexer_debugging_p (lexer))
748 fprintf (cp_lexer_debug_stream,
749 "cp_lexer: peeking ahead %ld at token: ", (long)n);
752 token = lexer->next_token;
753 gcc_assert (!n || token != &eof_token);
757 if (token == lexer->last_token)
763 if (token->type != CPP_PURGED)
767 if (cp_lexer_debugging_p (lexer))
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 putc ('\n', cp_lexer_debug_stream);
776 /* Return the next token, and advance the lexer's next_token pointer
777 to point to the next non-purged token. */
780 cp_lexer_consume_token (cp_lexer* lexer)
782 cp_token *token = lexer->next_token;
784 gcc_assert (token != &eof_token);
785 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
790 if (lexer->next_token == lexer->last_token)
792 lexer->next_token = &eof_token;
797 while (lexer->next_token->type == CPP_PURGED);
799 cp_lexer_set_source_position_from_token (token);
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer))
804 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805 cp_lexer_print_token (cp_lexer_debug_stream, token);
806 putc ('\n', cp_lexer_debug_stream);
812 /* Permanently remove the next token from the token stream, and
813 advance the next_token pointer to refer to the next non-purged
817 cp_lexer_purge_token (cp_lexer *lexer)
819 cp_token *tok = lexer->next_token;
821 gcc_assert (tok != &eof_token);
822 tok->type = CPP_PURGED;
823 tok->location = UNKNOWN_LOCATION;
824 tok->u.value = NULL_TREE;
825 tok->keyword = RID_MAX;
830 if (tok == lexer->last_token)
836 while (tok->type == CPP_PURGED);
837 lexer->next_token = tok;
840 /* Permanently remove all tokens after TOK, up to, but not
841 including, the token that will be returned next by
842 cp_lexer_peek_token. */
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
847 cp_token *peek = lexer->next_token;
849 if (peek == &eof_token)
850 peek = lexer->last_token;
852 gcc_assert (tok < peek);
854 for ( tok += 1; tok != peek; tok += 1)
856 tok->type = CPP_PURGED;
857 tok->location = UNKNOWN_LOCATION;
858 tok->u.value = NULL_TREE;
859 tok->keyword = RID_MAX;
863 /* Begin saving tokens. All tokens consumed after this point will be
867 cp_lexer_save_tokens (cp_lexer* lexer)
869 /* Provide debugging output. */
870 if (cp_lexer_debugging_p (lexer))
871 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
873 VEC_safe_push (cp_token_position, heap,
874 lexer->saved_tokens, lexer->next_token);
877 /* Commit to the portion of the token stream most recently saved. */
880 cp_lexer_commit_tokens (cp_lexer* lexer)
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
886 VEC_pop (cp_token_position, lexer->saved_tokens);
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890 to the token stream. Stop saving tokens. */
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
895 /* Provide debugging output. */
896 if (cp_lexer_debugging_p (lexer))
897 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
899 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
904 #ifdef ENABLE_CHECKING
907 cp_lexer_print_token (FILE * stream, cp_token *token)
909 /* We don't use cpp_type2name here because the parser defines
910 a few tokens of its own. */
911 static const char *const token_names[] = {
912 /* cpplib-defined token types */
918 /* C++ parser token types - see "Manifest constants", above. */
921 "NESTED_NAME_SPECIFIER",
925 /* If we have a name for the token, print it out. Otherwise, we
926 simply give the numeric code. */
927 gcc_assert (token->type < ARRAY_SIZE(token_names));
928 fputs (token_names[token->type], stream);
930 /* For some tokens, print the associated data. */
934 /* Some keywords have a value that is not an IDENTIFIER_NODE.
935 For example, `struct' is mapped to an INTEGER_CST. */
936 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
938 /* else fall through */
940 fputs (IDENTIFIER_POINTER (token->u.value), stream);
948 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
956 /* Start emitting debugging information. */
959 cp_lexer_start_debugging (cp_lexer* lexer)
961 lexer->debugging_p = true;
964 /* Stop emitting debugging information. */
967 cp_lexer_stop_debugging (cp_lexer* lexer)
969 lexer->debugging_p = false;
972 #endif /* ENABLE_CHECKING */
974 /* Create a new cp_token_cache, representing a range of tokens. */
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
979 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980 cache->first = first;
986 /* Decl-specifiers. */
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
993 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
998 /* Nothing other than the parser should be creating declarators;
999 declarators are a semi-syntactic representation of C++ entities.
1000 Other parts of the front end that need to create entities (like
1001 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1003 static cp_declarator *make_call_declarator
1004 (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006 (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008 (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010 (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012 (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014 (cp_cv_quals, tree, cp_declarator *);
1016 /* An erroneous declarator. */
1017 static cp_declarator *cp_error_declarator;
1019 /* The obstack on which declarators and related data structures are
1021 static struct obstack declarator_obstack;
1023 /* Alloc BYTES from the declarator memory pool. */
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1028 return obstack_alloc (&declarator_obstack, bytes);
1031 /* Allocate a declarator of the indicated KIND. Clear fields that are
1032 common to all declarators. */
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1037 cp_declarator *declarator;
1039 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040 declarator->kind = kind;
1041 declarator->attributes = NULL_TREE;
1042 declarator->declarator = NULL;
1043 declarator->parameter_pack_p = false;
1044 declarator->id_loc = UNKNOWN_LOCATION;
1049 /* Make a declarator for a generalized identifier. If
1050 QUALIFYING_SCOPE is non-NULL, the identifier is
1051 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057 special_function_kind sfk)
1059 cp_declarator *declarator;
1061 /* It is valid to write:
1063 class C { void f(); };
1067 The standard is not clear about whether `typedef const C D' is
1068 legal; as of 2002-09-15 the committee is considering that
1069 question. EDG 3.0 allows that syntax. Therefore, we do as
1071 if (qualifying_scope && TYPE_P (qualifying_scope))
1072 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1074 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1078 declarator = make_declarator (cdk_id);
1079 declarator->u.id.qualifying_scope = qualifying_scope;
1080 declarator->u.id.unqualified_name = unqualified_name;
1081 declarator->u.id.sfk = sfk;
1086 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1087 of modifiers such as const or volatile to apply to the pointer
1088 type, represented as identifiers. */
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1093 cp_declarator *declarator;
1095 declarator = make_declarator (cdk_pointer);
1096 declarator->declarator = target;
1097 declarator->u.pointer.qualifiers = cv_qualifiers;
1098 declarator->u.pointer.class_type = NULL_TREE;
1101 declarator->id_loc = target->id_loc;
1102 declarator->parameter_pack_p = target->parameter_pack_p;
1103 target->parameter_pack_p = false;
1106 declarator->parameter_pack_p = false;
1111 /* Like make_pointer_declarator -- but for references. */
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1117 cp_declarator *declarator;
1119 declarator = make_declarator (cdk_reference);
1120 declarator->declarator = target;
1121 declarator->u.reference.qualifiers = cv_qualifiers;
1122 declarator->u.reference.rvalue_ref = rvalue_ref;
1125 declarator->id_loc = target->id_loc;
1126 declarator->parameter_pack_p = target->parameter_pack_p;
1127 target->parameter_pack_p = false;
1130 declarator->parameter_pack_p = false;
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136 member of CLASS_TYPE. */
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140 cp_declarator *pointee)
1142 cp_declarator *declarator;
1144 declarator = make_declarator (cdk_ptrmem);
1145 declarator->declarator = pointee;
1146 declarator->u.pointer.qualifiers = cv_qualifiers;
1147 declarator->u.pointer.class_type = class_type;
1151 declarator->parameter_pack_p = pointee->parameter_pack_p;
1152 pointee->parameter_pack_p = false;
1155 declarator->parameter_pack_p = false;
1160 /* Make a declarator for the function given by TARGET, with the
1161 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1162 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1163 indicates what exceptions can be thrown. */
1166 make_call_declarator (cp_declarator *target,
1168 cp_cv_quals cv_qualifiers,
1169 tree exception_specification,
1170 tree late_return_type)
1172 cp_declarator *declarator;
1174 declarator = make_declarator (cdk_function);
1175 declarator->declarator = target;
1176 declarator->u.function.parameters = parms;
1177 declarator->u.function.qualifiers = cv_qualifiers;
1178 declarator->u.function.exception_specification = exception_specification;
1179 declarator->u.function.late_return_type = late_return_type;
1182 declarator->id_loc = target->id_loc;
1183 declarator->parameter_pack_p = target->parameter_pack_p;
1184 target->parameter_pack_p = false;
1187 declarator->parameter_pack_p = false;
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193 defined by ELEMENT. */
1196 make_array_declarator (cp_declarator *element, tree bounds)
1198 cp_declarator *declarator;
1200 declarator = make_declarator (cdk_array);
1201 declarator->declarator = element;
1202 declarator->u.array.bounds = bounds;
1205 declarator->id_loc = element->id_loc;
1206 declarator->parameter_pack_p = element->parameter_pack_p;
1207 element->parameter_pack_p = false;
1210 declarator->parameter_pack_p = false;
1215 /* Determine whether the declarator we've seen so far can be a
1216 parameter pack, when followed by an ellipsis. */
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1220 /* Search for a declarator name, or any other declarator that goes
1221 after the point where the ellipsis could appear in a parameter
1222 pack. If we find any of these, then this declarator can not be
1223 made into a parameter pack. */
1225 while (declarator && !found)
1227 switch ((int)declarator->kind)
1238 declarator = declarator->declarator;
1246 cp_parameter_declarator *no_parameters;
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249 DECLARATOR and DEFAULT_ARGUMENT. */
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253 cp_declarator *declarator,
1254 tree default_argument)
1256 cp_parameter_declarator *parameter;
1258 parameter = ((cp_parameter_declarator *)
1259 alloc_declarator (sizeof (cp_parameter_declarator)));
1260 parameter->next = NULL;
1261 if (decl_specifiers)
1262 parameter->decl_specifiers = *decl_specifiers;
1264 clear_decl_specs (¶meter->decl_specifiers);
1265 parameter->declarator = declarator;
1266 parameter->default_argument = default_argument;
1267 parameter->ellipsis_p = false;
1272 /* Returns true iff DECLARATOR is a declaration for a function. */
1275 function_declarator_p (const cp_declarator *declarator)
1279 if (declarator->kind == cdk_function
1280 && declarator->declarator->kind == cdk_id)
1282 if (declarator->kind == cdk_id
1283 || declarator->kind == cdk_error)
1285 declarator = declarator->declarator;
1295 A cp_parser parses the token stream as specified by the C++
1296 grammar. Its job is purely parsing, not semantic analysis. For
1297 example, the parser breaks the token stream into declarators,
1298 expressions, statements, and other similar syntactic constructs.
1299 It does not check that the types of the expressions on either side
1300 of an assignment-statement are compatible, or that a function is
1301 not declared with a parameter of type `void'.
1303 The parser invokes routines elsewhere in the compiler to perform
1304 semantic analysis and to build up the abstract syntax tree for the
1307 The parser (and the template instantiation code, which is, in a
1308 way, a close relative of parsing) are the only parts of the
1309 compiler that should be calling push_scope and pop_scope, or
1310 related functions. The parser (and template instantiation code)
1311 keeps track of what scope is presently active; everything else
1312 should simply honor that. (The code that generates static
1313 initializers may also need to set the scope, in order to check
1314 access control correctly when emitting the initializers.)
1319 The parser is of the standard recursive-descent variety. Upcoming
1320 tokens in the token stream are examined in order to determine which
1321 production to use when parsing a non-terminal. Some C++ constructs
1322 require arbitrary look ahead to disambiguate. For example, it is
1323 impossible, in the general case, to tell whether a statement is an
1324 expression or declaration without scanning the entire statement.
1325 Therefore, the parser is capable of "parsing tentatively." When the
1326 parser is not sure what construct comes next, it enters this mode.
1327 Then, while we attempt to parse the construct, the parser queues up
1328 error messages, rather than issuing them immediately, and saves the
1329 tokens it consumes. If the construct is parsed successfully, the
1330 parser "commits", i.e., it issues any queued error messages and
1331 the tokens that were being preserved are permanently discarded.
1332 If, however, the construct is not parsed successfully, the parser
1333 rolls back its state completely so that it can resume parsing using
1334 a different alternative.
1339 The performance of the parser could probably be improved substantially.
1340 We could often eliminate the need to parse tentatively by looking ahead
1341 a little bit. In some places, this approach might not entirely eliminate
1342 the need to parse tentatively, but it might still speed up the average
1345 /* Flags that are passed to some parsing functions. These values can
1346 be bitwise-ored together. */
1351 CP_PARSER_FLAGS_NONE = 0x0,
1352 /* The construct is optional. If it is not present, then no error
1353 should be issued. */
1354 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355 /* When parsing a type-specifier, treat user-defined type-names
1356 as non-type identifiers. */
1357 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358 /* When parsing a type-specifier, do not try to parse a class-specifier
1359 or enum-specifier. */
1360 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361 /* When parsing a decl-specifier-seq, only allow type-specifier or
1363 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1366 /* This type is used for parameters and variables which hold
1367 combinations of the above flags. */
1368 typedef int cp_parser_flags;
1370 /* The different kinds of declarators we want to parse. */
1372 typedef enum cp_parser_declarator_kind
1374 /* We want an abstract declarator. */
1375 CP_PARSER_DECLARATOR_ABSTRACT,
1376 /* We want a named declarator. */
1377 CP_PARSER_DECLARATOR_NAMED,
1378 /* We don't mind, but the name must be an unqualified-id. */
1379 CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1382 /* The precedence values used to parse binary expressions. The minimum value
1383 of PREC must be 1, because zero is reserved to quickly discriminate
1384 binary operators from other tokens. */
1389 PREC_LOGICAL_OR_EXPRESSION,
1390 PREC_LOGICAL_AND_EXPRESSION,
1391 PREC_INCLUSIVE_OR_EXPRESSION,
1392 PREC_EXCLUSIVE_OR_EXPRESSION,
1393 PREC_AND_EXPRESSION,
1394 PREC_EQUALITY_EXPRESSION,
1395 PREC_RELATIONAL_EXPRESSION,
1396 PREC_SHIFT_EXPRESSION,
1397 PREC_ADDITIVE_EXPRESSION,
1398 PREC_MULTIPLICATIVE_EXPRESSION,
1400 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404 precedence value. */
1406 typedef struct cp_parser_binary_operations_map_node
1408 /* The token type. */
1409 enum cpp_ttype token_type;
1410 /* The corresponding tree code. */
1411 enum tree_code tree_type;
1412 /* The precedence of this operator. */
1413 enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1416 /* The status of a tentative parse. */
1418 typedef enum cp_parser_status_kind
1420 /* No errors have occurred. */
1421 CP_PARSER_STATUS_KIND_NO_ERROR,
1422 /* An error has occurred. */
1423 CP_PARSER_STATUS_KIND_ERROR,
1424 /* We are committed to this tentative parse, whether or not an error
1426 CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1429 typedef struct cp_parser_expression_stack_entry
1431 /* Left hand side of the binary operation we are currently
1434 /* Original tree code for left hand side, if it was a binary
1435 expression itself (used for -Wparentheses). */
1436 enum tree_code lhs_type;
1437 /* Tree code for the binary operation we are parsing. */
1438 enum tree_code tree_type;
1439 /* Precedence of the binary operation we are parsing. */
1440 enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1443 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1444 entries because precedence levels on the stack are monotonically
1446 typedef struct cp_parser_expression_stack_entry
1447 cp_parser_expression_stack[NUM_PREC_VALUES];
1449 /* Context that is saved and restored when parsing tentatively. */
1450 typedef struct GTY (()) cp_parser_context {
1451 /* If this is a tentative parsing context, the status of the
1453 enum cp_parser_status_kind status;
1454 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1455 that are looked up in this context must be looked up both in the
1456 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457 the context of the containing expression. */
1460 /* The next parsing context in the stack. */
1461 struct cp_parser_context *next;
1462 } cp_parser_context;
1466 /* Constructors and destructors. */
1468 static cp_parser_context *cp_parser_context_new
1469 (cp_parser_context *);
1471 /* Class variables. */
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476 Transformed into an associative array (binops_by_token) by
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1483 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1487 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1490 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1493 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1498 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1501 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1503 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1505 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1507 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1509 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1512 /* The same as binops, but initialized by cp_parser_new so that
1513 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1517 /* Constructors and destructors. */
1519 /* Construct a new context. The context below this one on the stack
1520 is given by NEXT. */
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1525 cp_parser_context *context;
1527 /* Allocate the storage. */
1528 if (cp_parser_context_free_list != NULL)
1530 /* Pull the first entry from the free list. */
1531 context = cp_parser_context_free_list;
1532 cp_parser_context_free_list = context->next;
1533 memset (context, 0, sizeof (*context));
1536 context = ggc_alloc_cleared_cp_parser_context ();
1538 /* No errors have occurred yet in this context. */
1539 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540 /* If this is not the bottommost context, copy information that we
1541 need from the previous context. */
1544 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545 expression, then we are parsing one in this context, too. */
1546 context->object_type = next->object_type;
1547 /* Thread the stack. */
1548 context->next = next;
1554 /* An entry in a queue of function arguments that require post-processing. */
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557 /* The current_class_type when we parsed this arg. */
1560 /* The function decl itself. */
1562 } cp_default_arg_entry;
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1567 /* An entry in a stack for member functions of local classes. */
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570 /* Functions with default arguments that require post-processing.
1571 Functions appear in this list in declaration order. */
1572 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1574 /* Functions with defintions that require post-processing. Functions
1575 appear in this list in declaration order. */
1576 VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1582 /* The cp_parser structure represents the C++ parser. */
1584 typedef struct GTY(()) cp_parser {
1585 /* The lexer from which we are obtaining tokens. */
1588 /* The scope in which names should be looked up. If NULL_TREE, then
1589 we look up names in the scope that is currently open in the
1590 source program. If non-NULL, this is either a TYPE or
1591 NAMESPACE_DECL for the scope in which we should look. It can
1592 also be ERROR_MARK, when we've parsed a bogus scope.
1594 This value is not cleared automatically after a name is looked
1595 up, so we must be careful to clear it before starting a new look
1596 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1597 will look up `Z' in the scope of `X', rather than the current
1598 scope.) Unfortunately, it is difficult to tell when name lookup
1599 is complete, because we sometimes peek at a token, look it up,
1600 and then decide not to consume it. */
1603 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604 last lookup took place. OBJECT_SCOPE is used if an expression
1605 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606 respectively. QUALIFYING_SCOPE is used for an expression of the
1607 form "X::Y"; it refers to X. */
1609 tree qualifying_scope;
1611 /* A stack of parsing contexts. All but the bottom entry on the
1612 stack will be tentative contexts.
1614 We parse tentatively in order to determine which construct is in
1615 use in some situations. For example, in order to determine
1616 whether a statement is an expression-statement or a
1617 declaration-statement we parse it tentatively as a
1618 declaration-statement. If that fails, we then reparse the same
1619 token stream as an expression-statement. */
1620 cp_parser_context *context;
1622 /* True if we are parsing GNU C++. If this flag is not set, then
1623 GNU extensions are not recognized. */
1624 bool allow_gnu_extensions_p;
1626 /* TRUE if the `>' token should be interpreted as the greater-than
1627 operator. FALSE if it is the end of a template-id or
1628 template-parameter-list. In C++0x mode, this flag also applies to
1629 `>>' tokens, which are viewed as two consecutive `>' tokens when
1630 this flag is FALSE. */
1631 bool greater_than_is_operator_p;
1633 /* TRUE if default arguments are allowed within a parameter list
1634 that starts at this point. FALSE if only a gnu extension makes
1635 them permissible. */
1636 bool default_arg_ok_p;
1638 /* TRUE if we are parsing an integral constant-expression. See
1639 [expr.const] for a precise definition. */
1640 bool integral_constant_expression_p;
1642 /* TRUE if we are parsing an integral constant-expression -- but a
1643 non-constant expression should be permitted as well. This flag
1644 is used when parsing an array bound so that GNU variable-length
1645 arrays are tolerated. */
1646 bool allow_non_integral_constant_expression_p;
1648 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649 been seen that makes the expression non-constant. */
1650 bool non_integral_constant_expression_p;
1652 /* TRUE if local variable names and `this' are forbidden in the
1654 bool local_variables_forbidden_p;
1656 /* TRUE if the declaration we are parsing is part of a
1657 linkage-specification of the form `extern string-literal
1659 bool in_unbraced_linkage_specification_p;
1661 /* TRUE if we are presently parsing a declarator, after the
1662 direct-declarator. */
1663 bool in_declarator_p;
1665 /* TRUE if we are presently parsing a template-argument-list. */
1666 bool in_template_argument_list_p;
1668 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669 to IN_OMP_BLOCK if parsing OpenMP structured block and
1670 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1671 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672 iteration-statement, OpenMP block or loop within that switch. */
1673 #define IN_SWITCH_STMT 1
1674 #define IN_ITERATION_STMT 2
1675 #define IN_OMP_BLOCK 4
1676 #define IN_OMP_FOR 8
1677 #define IN_IF_STMT 16
1678 unsigned char in_statement;
1680 /* TRUE if we are presently parsing the body of a switch statement.
1681 Note that this doesn't quite overlap with in_statement above.
1682 The difference relates to giving the right sets of error messages:
1683 "case not in switch" vs "break statement used with OpenMP...". */
1684 bool in_switch_statement_p;
1686 /* TRUE if we are parsing a type-id in an expression context. In
1687 such a situation, both "type (expr)" and "type (type)" are valid
1689 bool in_type_id_in_expr_p;
1691 /* TRUE if we are currently in a header file where declarations are
1692 implicitly extern "C". */
1693 bool implicit_extern_c;
1695 /* TRUE if strings in expressions should be translated to the execution
1697 bool translate_strings_p;
1699 /* TRUE if we are presently parsing the body of a function, but not
1701 bool in_function_body;
1703 /* If non-NULL, then we are parsing a construct where new type
1704 definitions are not permitted. The string stored here will be
1705 issued as an error message if a type is defined. */
1706 const char *type_definition_forbidden_message;
1708 /* A stack used for member functions of local classes. The lists
1709 contained in an individual entry can only be processed once the
1710 outermost class being defined is complete. */
1711 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1713 /* The number of classes whose definitions are currently in
1715 unsigned num_classes_being_defined;
1717 /* The number of template parameter lists that apply directly to the
1718 current declaration. */
1719 unsigned num_template_parameter_lists;
1722 /* Managing the unparsed function queues. */
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1730 push_unparsed_function_queues (cp_parser *parser)
1732 VEC_safe_push (cp_unparsed_functions_entry, gc,
1733 parser->unparsed_queues, NULL);
1734 unparsed_funs_with_default_args = NULL;
1735 unparsed_funs_with_definitions = make_tree_vector ();
1739 pop_unparsed_function_queues (cp_parser *parser)
1741 release_tree_vector (unparsed_funs_with_definitions);
1742 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1747 /* Constructors and destructors. */
1749 static cp_parser *cp_parser_new
1752 /* Routines to parse various constructs.
1754 Those that return `tree' will return the error_mark_node (rather
1755 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756 Sometimes, they will return an ordinary node if error-recovery was
1757 attempted, even though a parse error occurred. So, to check
1758 whether or not a parse error occurred, you should always use
1759 cp_parser_error_occurred. If the construct is optional (indicated
1760 either by an `_opt' in the name of the function that does the
1761 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762 the construct is not present. */
1764 /* Lexical conventions [gram.lex] */
1766 static tree cp_parser_identifier
1768 static tree cp_parser_string_literal
1769 (cp_parser *, bool, bool);
1771 /* Basic concepts [gram.basic] */
1773 static bool cp_parser_translation_unit
1776 /* Expressions [gram.expr] */
1778 static tree cp_parser_primary_expression
1779 (cp_parser *, bool, bool, bool, cp_id_kind *);
1780 static tree cp_parser_id_expression
1781 (cp_parser *, bool, bool, bool *, bool, bool);
1782 static tree cp_parser_unqualified_id
1783 (cp_parser *, bool, bool, bool, bool);
1784 static tree cp_parser_nested_name_specifier_opt
1785 (cp_parser *, bool, bool, bool, bool);
1786 static tree cp_parser_nested_name_specifier
1787 (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_qualifying_entity
1789 (cp_parser *, bool, bool, bool, bool, bool);
1790 static tree cp_parser_postfix_expression
1791 (cp_parser *, bool, bool, bool, cp_id_kind *);
1792 static tree cp_parser_postfix_open_square_expression
1793 (cp_parser *, tree, bool);
1794 static tree cp_parser_postfix_dot_deref_expression
1795 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1796 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1797 (cp_parser *, int, bool, bool, bool *);
1798 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1799 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1800 static void cp_parser_pseudo_destructor_name
1801 (cp_parser *, tree *, tree *);
1802 static tree cp_parser_unary_expression
1803 (cp_parser *, bool, bool, cp_id_kind *);
1804 static enum tree_code cp_parser_unary_operator
1806 static tree cp_parser_new_expression
1808 static VEC(tree,gc) *cp_parser_new_placement
1810 static tree cp_parser_new_type_id
1811 (cp_parser *, tree *);
1812 static cp_declarator *cp_parser_new_declarator_opt
1814 static cp_declarator *cp_parser_direct_new_declarator
1816 static VEC(tree,gc) *cp_parser_new_initializer
1818 static tree cp_parser_delete_expression
1820 static tree cp_parser_cast_expression
1821 (cp_parser *, bool, bool, cp_id_kind *);
1822 static tree cp_parser_binary_expression
1823 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1824 static tree cp_parser_question_colon_clause
1825 (cp_parser *, tree);
1826 static tree cp_parser_assignment_expression
1827 (cp_parser *, bool, cp_id_kind *);
1828 static enum tree_code cp_parser_assignment_operator_opt
1830 static tree cp_parser_expression
1831 (cp_parser *, bool, cp_id_kind *);
1832 static tree cp_parser_constant_expression
1833 (cp_parser *, bool, bool *);
1834 static tree cp_parser_builtin_offsetof
1836 static tree cp_parser_lambda_expression
1838 static void cp_parser_lambda_introducer
1839 (cp_parser *, tree);
1840 static void cp_parser_lambda_declarator_opt
1841 (cp_parser *, tree);
1842 static void cp_parser_lambda_body
1843 (cp_parser *, tree);
1845 /* Statements [gram.stmt.stmt] */
1847 static void cp_parser_statement
1848 (cp_parser *, tree, bool, bool *);
1849 static void cp_parser_label_for_labeled_statement
1851 static tree cp_parser_expression_statement
1852 (cp_parser *, tree);
1853 static tree cp_parser_compound_statement
1854 (cp_parser *, tree, bool);
1855 static void cp_parser_statement_seq_opt
1856 (cp_parser *, tree);
1857 static tree cp_parser_selection_statement
1858 (cp_parser *, bool *);
1859 static tree cp_parser_condition
1861 static tree cp_parser_iteration_statement
1863 static void cp_parser_for_init_statement
1865 static tree cp_parser_c_for
1867 static tree cp_parser_range_for
1869 static tree cp_parser_jump_statement
1871 static void cp_parser_declaration_statement
1874 static tree cp_parser_implicitly_scoped_statement
1875 (cp_parser *, bool *);
1876 static void cp_parser_already_scoped_statement
1879 /* Declarations [gram.dcl.dcl] */
1881 static void cp_parser_declaration_seq_opt
1883 static void cp_parser_declaration
1885 static void cp_parser_block_declaration
1886 (cp_parser *, bool);
1887 static void cp_parser_simple_declaration
1888 (cp_parser *, bool);
1889 static void cp_parser_decl_specifier_seq
1890 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1891 static tree cp_parser_storage_class_specifier_opt
1893 static tree cp_parser_function_specifier_opt
1894 (cp_parser *, cp_decl_specifier_seq *);
1895 static tree cp_parser_type_specifier
1896 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1898 static tree cp_parser_simple_type_specifier
1899 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1900 static tree cp_parser_type_name
1902 static tree cp_parser_nonclass_name
1903 (cp_parser* parser);
1904 static tree cp_parser_elaborated_type_specifier
1905 (cp_parser *, bool, bool);
1906 static tree cp_parser_enum_specifier
1908 static void cp_parser_enumerator_list
1909 (cp_parser *, tree);
1910 static void cp_parser_enumerator_definition
1911 (cp_parser *, tree);
1912 static tree cp_parser_namespace_name
1914 static void cp_parser_namespace_definition
1916 static void cp_parser_namespace_body
1918 static tree cp_parser_qualified_namespace_specifier
1920 static void cp_parser_namespace_alias_definition
1922 static bool cp_parser_using_declaration
1923 (cp_parser *, bool);
1924 static void cp_parser_using_directive
1926 static void cp_parser_asm_definition
1928 static void cp_parser_linkage_specification
1930 static void cp_parser_static_assert
1931 (cp_parser *, bool);
1932 static tree cp_parser_decltype
1935 /* Declarators [gram.dcl.decl] */
1937 static tree cp_parser_init_declarator
1938 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1939 static cp_declarator *cp_parser_declarator
1940 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1941 static cp_declarator *cp_parser_direct_declarator
1942 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1943 static enum tree_code cp_parser_ptr_operator
1944 (cp_parser *, tree *, cp_cv_quals *);
1945 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1947 static tree cp_parser_late_return_type_opt
1949 static tree cp_parser_declarator_id
1950 (cp_parser *, bool);
1951 static tree cp_parser_type_id
1953 static tree cp_parser_template_type_arg
1955 static tree cp_parser_trailing_type_id (cp_parser *);
1956 static tree cp_parser_type_id_1
1957 (cp_parser *, bool, bool);
1958 static void cp_parser_type_specifier_seq
1959 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1960 static tree cp_parser_parameter_declaration_clause
1962 static tree cp_parser_parameter_declaration_list
1963 (cp_parser *, bool *);
1964 static cp_parameter_declarator *cp_parser_parameter_declaration
1965 (cp_parser *, bool, bool *);
1966 static tree cp_parser_default_argument
1967 (cp_parser *, bool);
1968 static void cp_parser_function_body
1970 static tree cp_parser_initializer
1971 (cp_parser *, bool *, bool *);
1972 static tree cp_parser_initializer_clause
1973 (cp_parser *, bool *);
1974 static tree cp_parser_braced_list
1975 (cp_parser*, bool*);
1976 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1977 (cp_parser *, bool *);
1979 static bool cp_parser_ctor_initializer_opt_and_function_body
1982 /* Classes [gram.class] */
1984 static tree cp_parser_class_name
1985 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1986 static tree cp_parser_class_specifier
1988 static tree cp_parser_class_head
1989 (cp_parser *, bool *, tree *, tree *);
1990 static enum tag_types cp_parser_class_key
1992 static void cp_parser_member_specification_opt
1994 static void cp_parser_member_declaration
1996 static tree cp_parser_pure_specifier
1998 static tree cp_parser_constant_initializer
2001 /* Derived classes [gram.class.derived] */
2003 static tree cp_parser_base_clause
2005 static tree cp_parser_base_specifier
2008 /* Special member functions [gram.special] */
2010 static tree cp_parser_conversion_function_id
2012 static tree cp_parser_conversion_type_id
2014 static cp_declarator *cp_parser_conversion_declarator_opt
2016 static bool cp_parser_ctor_initializer_opt
2018 static void cp_parser_mem_initializer_list
2020 static tree cp_parser_mem_initializer
2022 static tree cp_parser_mem_initializer_id
2025 /* Overloading [gram.over] */
2027 static tree cp_parser_operator_function_id
2029 static tree cp_parser_operator
2032 /* Templates [gram.temp] */
2034 static void cp_parser_template_declaration
2035 (cp_parser *, bool);
2036 static tree cp_parser_template_parameter_list
2038 static tree cp_parser_template_parameter
2039 (cp_parser *, bool *, bool *);
2040 static tree cp_parser_type_parameter
2041 (cp_parser *, bool *);
2042 static tree cp_parser_template_id
2043 (cp_parser *, bool, bool, bool);
2044 static tree cp_parser_template_name
2045 (cp_parser *, bool, bool, bool, bool *);
2046 static tree cp_parser_template_argument_list
2048 static tree cp_parser_template_argument
2050 static void cp_parser_explicit_instantiation
2052 static void cp_parser_explicit_specialization
2055 /* Exception handling [gram.exception] */
2057 static tree cp_parser_try_block
2059 static bool cp_parser_function_try_block
2061 static void cp_parser_handler_seq
2063 static void cp_parser_handler
2065 static tree cp_parser_exception_declaration
2067 static tree cp_parser_throw_expression
2069 static tree cp_parser_exception_specification_opt
2071 static tree cp_parser_type_id_list
2074 /* GNU Extensions */
2076 static tree cp_parser_asm_specification_opt
2078 static tree cp_parser_asm_operand_list
2080 static tree cp_parser_asm_clobber_list
2082 static tree cp_parser_asm_label_list
2084 static tree cp_parser_attributes_opt
2086 static tree cp_parser_attribute_list
2088 static bool cp_parser_extension_opt
2089 (cp_parser *, int *);
2090 static void cp_parser_label_declaration
2093 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2094 static bool cp_parser_pragma
2095 (cp_parser *, enum pragma_context);
2097 /* Objective-C++ Productions */
2099 static tree cp_parser_objc_message_receiver
2101 static tree cp_parser_objc_message_args
2103 static tree cp_parser_objc_message_expression
2105 static tree cp_parser_objc_encode_expression
2107 static tree cp_parser_objc_defs_expression
2109 static tree cp_parser_objc_protocol_expression
2111 static tree cp_parser_objc_selector_expression
2113 static tree cp_parser_objc_expression
2115 static bool cp_parser_objc_selector_p
2117 static tree cp_parser_objc_selector
2119 static tree cp_parser_objc_protocol_refs_opt
2121 static void cp_parser_objc_declaration
2122 (cp_parser *, tree);
2123 static tree cp_parser_objc_statement
2125 static bool cp_parser_objc_valid_prefix_attributes
2126 (cp_parser *, tree *);
2127 static void cp_parser_objc_at_property_declaration
2129 static void cp_parser_objc_at_synthesize_declaration
2131 static void cp_parser_objc_at_dynamic_declaration
2133 static tree cp_parser_objc_struct_declaration
2136 /* Utility Routines */
2138 static tree cp_parser_lookup_name
2139 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2140 static tree cp_parser_lookup_name_simple
2141 (cp_parser *, tree, location_t);
2142 static tree cp_parser_maybe_treat_template_as_class
2144 static bool cp_parser_check_declarator_template_parameters
2145 (cp_parser *, cp_declarator *, location_t);
2146 static bool cp_parser_check_template_parameters
2147 (cp_parser *, unsigned, location_t, cp_declarator *);
2148 static tree cp_parser_simple_cast_expression
2150 static tree cp_parser_global_scope_opt
2151 (cp_parser *, bool);
2152 static bool cp_parser_constructor_declarator_p
2153 (cp_parser *, bool);
2154 static tree cp_parser_function_definition_from_specifiers_and_declarator
2155 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2156 static tree cp_parser_function_definition_after_declarator
2157 (cp_parser *, bool);
2158 static void cp_parser_template_declaration_after_export
2159 (cp_parser *, bool);
2160 static void cp_parser_perform_template_parameter_access_checks
2161 (VEC (deferred_access_check,gc)*);
2162 static tree cp_parser_single_declaration
2163 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2164 static tree cp_parser_functional_cast
2165 (cp_parser *, tree);
2166 static tree cp_parser_save_member_function_body
2167 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2168 static tree cp_parser_enclosed_template_argument_list
2170 static void cp_parser_save_default_args
2171 (cp_parser *, tree);
2172 static void cp_parser_late_parsing_for_member
2173 (cp_parser *, tree);
2174 static void cp_parser_late_parsing_default_args
2175 (cp_parser *, tree);
2176 static tree cp_parser_sizeof_operand
2177 (cp_parser *, enum rid);
2178 static tree cp_parser_trait_expr
2179 (cp_parser *, enum rid);
2180 static bool cp_parser_declares_only_class_p
2182 static void cp_parser_set_storage_class
2183 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2184 static void cp_parser_set_decl_spec_type
2185 (cp_decl_specifier_seq *, tree, location_t, bool);
2186 static bool cp_parser_friend_p
2187 (const cp_decl_specifier_seq *);
2188 static void cp_parser_required_error
2189 (cp_parser *, required_token, bool);
2190 static cp_token *cp_parser_require
2191 (cp_parser *, enum cpp_ttype, required_token);
2192 static cp_token *cp_parser_require_keyword
2193 (cp_parser *, enum rid, required_token);
2194 static bool cp_parser_token_starts_function_definition_p
2196 static bool cp_parser_next_token_starts_class_definition_p
2198 static bool cp_parser_next_token_ends_template_argument_p
2200 static bool cp_parser_nth_token_starts_template_argument_list_p
2201 (cp_parser *, size_t);
2202 static enum tag_types cp_parser_token_is_class_key
2204 static void cp_parser_check_class_key
2205 (enum tag_types, tree type);
2206 static void cp_parser_check_access_in_redeclaration
2207 (tree type, location_t location);
2208 static bool cp_parser_optional_template_keyword
2210 static void cp_parser_pre_parsed_nested_name_specifier
2212 static bool cp_parser_cache_group
2213 (cp_parser *, enum cpp_ttype, unsigned);
2214 static void cp_parser_parse_tentatively
2216 static void cp_parser_commit_to_tentative_parse
2218 static void cp_parser_abort_tentative_parse
2220 static bool cp_parser_parse_definitely
2222 static inline bool cp_parser_parsing_tentatively
2224 static bool cp_parser_uncommitted_to_tentative_parse_p
2226 static void cp_parser_error
2227 (cp_parser *, const char *);
2228 static void cp_parser_name_lookup_error
2229 (cp_parser *, tree, tree, name_lookup_error, location_t);
2230 static bool cp_parser_simulate_error
2232 static bool cp_parser_check_type_definition
2234 static void cp_parser_check_for_definition_in_return_type
2235 (cp_declarator *, tree, location_t type_location);
2236 static void cp_parser_check_for_invalid_template_id
2237 (cp_parser *, tree, location_t location);
2238 static bool cp_parser_non_integral_constant_expression
2239 (cp_parser *, non_integral_constant);
2240 static void cp_parser_diagnose_invalid_type_name
2241 (cp_parser *, tree, tree, location_t);
2242 static bool cp_parser_parse_and_diagnose_invalid_type_name
2244 static int cp_parser_skip_to_closing_parenthesis
2245 (cp_parser *, bool, bool, bool);
2246 static void cp_parser_skip_to_end_of_statement
2248 static void cp_parser_consume_semicolon_at_end_of_statement
2250 static void cp_parser_skip_to_end_of_block_or_statement
2252 static bool cp_parser_skip_to_closing_brace
2254 static void cp_parser_skip_to_end_of_template_parameter_list
2256 static void cp_parser_skip_to_pragma_eol
2257 (cp_parser*, cp_token *);
2258 static bool cp_parser_error_occurred
2260 static bool cp_parser_allow_gnu_extensions_p
2262 static bool cp_parser_is_string_literal
2264 static bool cp_parser_is_keyword
2265 (cp_token *, enum rid);
2266 static tree cp_parser_make_typename_type
2267 (cp_parser *, tree, tree, location_t location);
2268 static cp_declarator * cp_parser_make_indirect_declarator
2269 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2271 /* Returns nonzero if we are parsing tentatively. */
2274 cp_parser_parsing_tentatively (cp_parser* parser)
2276 return parser->context->next != NULL;
2279 /* Returns nonzero if TOKEN is a string literal. */
2282 cp_parser_is_string_literal (cp_token* token)
2284 return (token->type == CPP_STRING ||
2285 token->type == CPP_STRING16 ||
2286 token->type == CPP_STRING32 ||
2287 token->type == CPP_WSTRING ||
2288 token->type == CPP_UTF8STRING);
2291 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2294 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2296 return token->keyword == keyword;
2299 /* If not parsing tentatively, issue a diagnostic of the form
2300 FILE:LINE: MESSAGE before TOKEN
2301 where TOKEN is the next token in the input stream. MESSAGE
2302 (specified by the caller) is usually of the form "expected
2306 cp_parser_error (cp_parser* parser, const char* gmsgid)
2308 if (!cp_parser_simulate_error (parser))
2310 cp_token *token = cp_lexer_peek_token (parser->lexer);
2311 /* This diagnostic makes more sense if it is tagged to the line
2312 of the token we just peeked at. */
2313 cp_lexer_set_source_position_from_token (token);
2315 if (token->type == CPP_PRAGMA)
2317 error_at (token->location,
2318 "%<#pragma%> is not allowed here");
2319 cp_parser_skip_to_pragma_eol (parser, token);
2323 c_parse_error (gmsgid,
2324 /* Because c_parser_error does not understand
2325 CPP_KEYWORD, keywords are treated like
2327 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2328 token->u.value, token->flags);
2332 /* Issue an error about name-lookup failing. NAME is the
2333 IDENTIFIER_NODE DECL is the result of
2334 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2335 the thing that we hoped to find. */
2338 cp_parser_name_lookup_error (cp_parser* parser,
2341 name_lookup_error desired,
2342 location_t location)
2344 /* If name lookup completely failed, tell the user that NAME was not
2346 if (decl == error_mark_node)
2348 if (parser->scope && parser->scope != global_namespace)
2349 error_at (location, "%<%E::%E%> has not been declared",
2350 parser->scope, name);
2351 else if (parser->scope == global_namespace)
2352 error_at (location, "%<::%E%> has not been declared", name);
2353 else if (parser->object_scope
2354 && !CLASS_TYPE_P (parser->object_scope))
2355 error_at (location, "request for member %qE in non-class type %qT",
2356 name, parser->object_scope);
2357 else if (parser->object_scope)
2358 error_at (location, "%<%T::%E%> has not been declared",
2359 parser->object_scope, name);
2361 error_at (location, "%qE has not been declared", name);
2363 else if (parser->scope && parser->scope != global_namespace)
2368 error_at (location, "%<%E::%E%> is not a type",
2369 parser->scope, name);
2372 error_at (location, "%<%E::%E%> is not a class or namespace",
2373 parser->scope, name);
2377 "%<%E::%E%> is not a class, namespace, or enumeration",
2378 parser->scope, name);
2385 else if (parser->scope == global_namespace)
2390 error_at (location, "%<::%E%> is not a type", name);
2393 error_at (location, "%<::%E%> is not a class or namespace", name);
2397 "%<::%E%> is not a class, namespace, or enumeration",
2409 error_at (location, "%qE is not a type", name);
2412 error_at (location, "%qE is not a class or namespace", name);
2416 "%qE is not a class, namespace, or enumeration", name);
2424 /* If we are parsing tentatively, remember that an error has occurred
2425 during this tentative parse. Returns true if the error was
2426 simulated; false if a message should be issued by the caller. */
2429 cp_parser_simulate_error (cp_parser* parser)
2431 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2433 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2439 /* Check for repeated decl-specifiers. */
2442 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2443 location_t location)
2447 for (ds = ds_first; ds != ds_last; ++ds)
2449 unsigned count = decl_specs->specs[ds];
2452 /* The "long" specifier is a special case because of "long long". */
2456 error_at (location, "%<long long long%> is too long for GCC");
2458 pedwarn_cxx98 (location, OPT_Wlong_long,
2459 "ISO C++ 1998 does not support %<long long%>");
2463 static const char *const decl_spec_names[] = {
2480 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2485 /* This function is called when a type is defined. If type
2486 definitions are forbidden at this point, an error message is
2490 cp_parser_check_type_definition (cp_parser* parser)
2492 /* If types are forbidden here, issue a message. */
2493 if (parser->type_definition_forbidden_message)
2495 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2496 in the message need to be interpreted. */
2497 error (parser->type_definition_forbidden_message);
2503 /* This function is called when the DECLARATOR is processed. The TYPE
2504 was a type defined in the decl-specifiers. If it is invalid to
2505 define a type in the decl-specifiers for DECLARATOR, an error is
2506 issued. TYPE_LOCATION is the location of TYPE and is used
2507 for error reporting. */
2510 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2511 tree type, location_t type_location)
2513 /* [dcl.fct] forbids type definitions in return types.
2514 Unfortunately, it's not easy to know whether or not we are
2515 processing a return type until after the fact. */
2517 && (declarator->kind == cdk_pointer
2518 || declarator->kind == cdk_reference
2519 || declarator->kind == cdk_ptrmem))
2520 declarator = declarator->declarator;
2522 && declarator->kind == cdk_function)
2524 error_at (type_location,
2525 "new types may not be defined in a return type");
2526 inform (type_location,
2527 "(perhaps a semicolon is missing after the definition of %qT)",
2532 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2533 "<" in any valid C++ program. If the next token is indeed "<",
2534 issue a message warning the user about what appears to be an
2535 invalid attempt to form a template-id. LOCATION is the location
2536 of the type-specifier (TYPE) */
2539 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2540 tree type, location_t location)
2542 cp_token_position start = 0;
2544 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2547 error_at (location, "%qT is not a template", type);
2548 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2549 error_at (location, "%qE is not a template", type);
2551 error_at (location, "invalid template-id");
2552 /* Remember the location of the invalid "<". */
2553 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2554 start = cp_lexer_token_position (parser->lexer, true);
2555 /* Consume the "<". */
2556 cp_lexer_consume_token (parser->lexer);
2557 /* Parse the template arguments. */
2558 cp_parser_enclosed_template_argument_list (parser);
2559 /* Permanently remove the invalid template arguments so that
2560 this error message is not issued again. */
2562 cp_lexer_purge_tokens_after (parser->lexer, start);
2566 /* If parsing an integral constant-expression, issue an error message
2567 about the fact that THING appeared and return true. Otherwise,
2568 return false. In either case, set
2569 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2572 cp_parser_non_integral_constant_expression (cp_parser *parser,
2573 non_integral_constant thing)
2575 parser->non_integral_constant_expression_p = true;
2576 if (parser->integral_constant_expression_p)
2578 if (!parser->allow_non_integral_constant_expression_p)
2580 const char *msg = NULL;
2584 error ("floating-point literal "
2585 "cannot appear in a constant-expression");
2588 error ("a cast to a type other than an integral or "
2589 "enumeration type cannot appear in a "
2590 "constant-expression");
2593 error ("%<typeid%> operator "
2594 "cannot appear in a constant-expression");
2597 error ("non-constant compound literals "
2598 "cannot appear in a constant-expression");
2601 error ("a function call "
2602 "cannot appear in a constant-expression");
2605 error ("an increment "
2606 "cannot appear in a constant-expression");
2609 error ("an decrement "
2610 "cannot appear in a constant-expression");
2613 error ("an array reference "
2614 "cannot appear in a constant-expression");
2616 case NIC_ADDR_LABEL:
2617 error ("the address of a label "
2618 "cannot appear in a constant-expression");
2620 case NIC_OVERLOADED:
2621 error ("calls to overloaded operators "
2622 "cannot appear in a constant-expression");
2624 case NIC_ASSIGNMENT:
2625 error ("an assignment cannot appear in a constant-expression");
2628 error ("a comma operator "
2629 "cannot appear in a constant-expression");
2631 case NIC_CONSTRUCTOR:
2632 error ("a call to a constructor "
2633 "cannot appear in a constant-expression");
2639 msg = "__FUNCTION__";
2641 case NIC_PRETTY_FUNC:
2642 msg = "__PRETTY_FUNCTION__";
2662 case NIC_PREINCREMENT:
2665 case NIC_PREDECREMENT:
2678 error ("%qs cannot appear in a constant-expression", msg);
2685 /* Emit a diagnostic for an invalid type name. SCOPE is the
2686 qualifying scope (or NULL, if none) for ID. This function commits
2687 to the current active tentative parse, if any. (Otherwise, the
2688 problematic construct might be encountered again later, resulting
2689 in duplicate error messages.) LOCATION is the location of ID. */
2692 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2693 tree scope, tree id,
2694 location_t location)
2696 tree decl, old_scope;
2697 /* Try to lookup the identifier. */
2698 old_scope = parser->scope;
2699 parser->scope = scope;
2700 decl = cp_parser_lookup_name_simple (parser, id, location);
2701 parser->scope = old_scope;
2702 /* If the lookup found a template-name, it means that the user forgot
2703 to specify an argument list. Emit a useful error message. */
2704 if (TREE_CODE (decl) == TEMPLATE_DECL)
2706 "invalid use of template-name %qE without an argument list",
2708 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2709 error_at (location, "invalid use of destructor %qD as a type", id);
2710 else if (TREE_CODE (decl) == TYPE_DECL)
2711 /* Something like 'unsigned A a;' */
2712 error_at (location, "invalid combination of multiple type-specifiers");
2713 else if (!parser->scope)
2715 /* Issue an error message. */
2716 error_at (location, "%qE does not name a type", id);
2717 /* If we're in a template class, it's possible that the user was
2718 referring to a type from a base class. For example:
2720 template <typename T> struct A { typedef T X; };
2721 template <typename T> struct B : public A<T> { X x; };
2723 The user should have said "typename A<T>::X". */
2724 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2725 inform (location, "C++0x %<constexpr%> only available with "
2726 "-std=c++0x or -std=gnu++0x");
2727 else if (processing_template_decl && current_class_type
2728 && TYPE_BINFO (current_class_type))
2732 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2736 tree base_type = BINFO_TYPE (b);
2737 if (CLASS_TYPE_P (base_type)
2738 && dependent_type_p (base_type))
2741 /* Go from a particular instantiation of the
2742 template (which will have an empty TYPE_FIELDs),
2743 to the main version. */
2744 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2745 for (field = TYPE_FIELDS (base_type);
2747 field = DECL_CHAIN (field))
2748 if (TREE_CODE (field) == TYPE_DECL
2749 && DECL_NAME (field) == id)
2752 "(perhaps %<typename %T::%E%> was intended)",
2753 BINFO_TYPE (b), id);
2762 /* Here we diagnose qualified-ids where the scope is actually correct,
2763 but the identifier does not resolve to a valid type name. */
2764 else if (parser->scope != error_mark_node)
2766 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2767 error_at (location, "%qE in namespace %qE does not name a type",
2769 else if (CLASS_TYPE_P (parser->scope)
2770 && constructor_name_p (id, parser->scope))
2773 error_at (location, "%<%T::%E%> names the constructor, not"
2774 " the type", parser->scope, id);
2775 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2776 error_at (location, "and %qT has no template constructors",
2779 else if (TYPE_P (parser->scope)
2780 && dependent_scope_p (parser->scope))
2781 error_at (location, "need %<typename%> before %<%T::%E%> because "
2782 "%qT is a dependent scope",
2783 parser->scope, id, parser->scope);
2784 else if (TYPE_P (parser->scope))
2785 error_at (location, "%qE in class %qT does not name a type",
2790 cp_parser_commit_to_tentative_parse (parser);
2793 /* Check for a common situation where a type-name should be present,
2794 but is not, and issue a sensible error message. Returns true if an
2795 invalid type-name was detected.
2797 The situation handled by this function are variable declarations of the
2798 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2799 Usually, `ID' should name a type, but if we got here it means that it
2800 does not. We try to emit the best possible error message depending on
2801 how exactly the id-expression looks like. */
2804 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2807 cp_token *token = cp_lexer_peek_token (parser->lexer);
2809 /* Avoid duplicate error about ambiguous lookup. */
2810 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2812 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2813 if (next->type == CPP_NAME && next->ambiguous_p)
2817 cp_parser_parse_tentatively (parser);
2818 id = cp_parser_id_expression (parser,
2819 /*template_keyword_p=*/false,
2820 /*check_dependency_p=*/true,
2821 /*template_p=*/NULL,
2822 /*declarator_p=*/true,
2823 /*optional_p=*/false);
2824 /* If the next token is a (, this is a function with no explicit return
2825 type, i.e. constructor, destructor or conversion op. */
2826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2827 || TREE_CODE (id) == TYPE_DECL)
2829 cp_parser_abort_tentative_parse (parser);
2832 if (!cp_parser_parse_definitely (parser))
2835 /* Emit a diagnostic for the invalid type. */
2836 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2837 id, token->location);
2839 /* If we aren't in the middle of a declarator (i.e. in a
2840 parameter-declaration-clause), skip to the end of the declaration;
2841 there's no point in trying to process it. */
2842 if (!parser->in_declarator_p)
2843 cp_parser_skip_to_end_of_block_or_statement (parser);
2847 /* Consume tokens up to, and including, the next non-nested closing `)'.
2848 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2849 are doing error recovery. Returns -1 if OR_COMMA is true and we
2850 found an unnested comma. */
2853 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2858 unsigned paren_depth = 0;
2859 unsigned brace_depth = 0;
2860 unsigned square_depth = 0;
2862 if (recovering && !or_comma
2863 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2868 cp_token * token = cp_lexer_peek_token (parser->lexer);
2870 switch (token->type)
2873 case CPP_PRAGMA_EOL:
2874 /* If we've run out of tokens, then there is no closing `)'. */
2877 /* This is good for lambda expression capture-lists. */
2878 case CPP_OPEN_SQUARE:
2881 case CPP_CLOSE_SQUARE:
2882 if (!square_depth--)
2887 /* This matches the processing in skip_to_end_of_statement. */
2892 case CPP_OPEN_BRACE:
2895 case CPP_CLOSE_BRACE:
2901 if (recovering && or_comma && !brace_depth && !paren_depth
2906 case CPP_OPEN_PAREN:
2911 case CPP_CLOSE_PAREN:
2912 if (!brace_depth && !paren_depth--)
2915 cp_lexer_consume_token (parser->lexer);
2924 /* Consume the token. */
2925 cp_lexer_consume_token (parser->lexer);
2929 /* Consume tokens until we reach the end of the current statement.
2930 Normally, that will be just before consuming a `;'. However, if a
2931 non-nested `}' comes first, then we stop before consuming that. */
2934 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2936 unsigned nesting_depth = 0;
2940 cp_token *token = cp_lexer_peek_token (parser->lexer);
2942 switch (token->type)
2945 case CPP_PRAGMA_EOL:
2946 /* If we've run out of tokens, stop. */
2950 /* If the next token is a `;', we have reached the end of the
2956 case CPP_CLOSE_BRACE:
2957 /* If this is a non-nested '}', stop before consuming it.
2958 That way, when confronted with something like:
2962 we stop before consuming the closing '}', even though we
2963 have not yet reached a `;'. */
2964 if (nesting_depth == 0)
2967 /* If it is the closing '}' for a block that we have
2968 scanned, stop -- but only after consuming the token.
2974 we will stop after the body of the erroneously declared
2975 function, but before consuming the following `typedef'
2977 if (--nesting_depth == 0)
2979 cp_lexer_consume_token (parser->lexer);
2983 case CPP_OPEN_BRACE:
2991 /* Consume the token. */
2992 cp_lexer_consume_token (parser->lexer);
2996 /* This function is called at the end of a statement or declaration.
2997 If the next token is a semicolon, it is consumed; otherwise, error
2998 recovery is attempted. */
3001 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3003 /* Look for the trailing `;'. */
3004 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3006 /* If there is additional (erroneous) input, skip to the end of
3008 cp_parser_skip_to_end_of_statement (parser);
3009 /* If the next token is now a `;', consume it. */
3010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3011 cp_lexer_consume_token (parser->lexer);
3015 /* Skip tokens until we have consumed an entire block, or until we
3016 have consumed a non-nested `;'. */
3019 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3021 int nesting_depth = 0;
3023 while (nesting_depth >= 0)
3025 cp_token *token = cp_lexer_peek_token (parser->lexer);
3027 switch (token->type)
3030 case CPP_PRAGMA_EOL:
3031 /* If we've run out of tokens, stop. */
3035 /* Stop if this is an unnested ';'. */
3040 case CPP_CLOSE_BRACE:
3041 /* Stop if this is an unnested '}', or closes the outermost
3044 if (nesting_depth < 0)
3050 case CPP_OPEN_BRACE:
3059 /* Consume the token. */
3060 cp_lexer_consume_token (parser->lexer);
3064 /* Skip tokens until a non-nested closing curly brace is the next
3065 token, or there are no more tokens. Return true in the first case,
3069 cp_parser_skip_to_closing_brace (cp_parser *parser)
3071 unsigned nesting_depth = 0;
3075 cp_token *token = cp_lexer_peek_token (parser->lexer);
3077 switch (token->type)
3080 case CPP_PRAGMA_EOL:
3081 /* If we've run out of tokens, stop. */
3084 case CPP_CLOSE_BRACE:
3085 /* If the next token is a non-nested `}', then we have reached
3086 the end of the current block. */
3087 if (nesting_depth-- == 0)
3091 case CPP_OPEN_BRACE:
3092 /* If it the next token is a `{', then we are entering a new
3093 block. Consume the entire block. */
3101 /* Consume the token. */
3102 cp_lexer_consume_token (parser->lexer);
3106 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3107 parameter is the PRAGMA token, allowing us to purge the entire pragma
3111 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3115 parser->lexer->in_pragma = false;
3118 token = cp_lexer_consume_token (parser->lexer);
3119 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3121 /* Ensure that the pragma is not parsed again. */
3122 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3125 /* Require pragma end of line, resyncing with it as necessary. The
3126 arguments are as for cp_parser_skip_to_pragma_eol. */
3129 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3131 parser->lexer->in_pragma = false;
3132 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3133 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3136 /* This is a simple wrapper around make_typename_type. When the id is
3137 an unresolved identifier node, we can provide a superior diagnostic
3138 using cp_parser_diagnose_invalid_type_name. */
3141 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3142 tree id, location_t id_location)
3145 if (TREE_CODE (id) == IDENTIFIER_NODE)
3147 result = make_typename_type (scope, id, typename_type,
3148 /*complain=*/tf_none);
3149 if (result == error_mark_node)
3150 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3153 return make_typename_type (scope, id, typename_type, tf_error);
3156 /* This is a wrapper around the
3157 make_{pointer,ptrmem,reference}_declarator functions that decides
3158 which one to call based on the CODE and CLASS_TYPE arguments. The
3159 CODE argument should be one of the values returned by
3160 cp_parser_ptr_operator. */
3161 static cp_declarator *
3162 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3163 cp_cv_quals cv_qualifiers,
3164 cp_declarator *target)
3166 if (code == ERROR_MARK)
3167 return cp_error_declarator;
3169 if (code == INDIRECT_REF)
3170 if (class_type == NULL_TREE)
3171 return make_pointer_declarator (cv_qualifiers, target);
3173 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3174 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3175 return make_reference_declarator (cv_qualifiers, target, false);
3176 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3177 return make_reference_declarator (cv_qualifiers, target, true);
3181 /* Create a new C++ parser. */
3184 cp_parser_new (void)
3190 /* cp_lexer_new_main is called before doing GC allocation because
3191 cp_lexer_new_main might load a PCH file. */
3192 lexer = cp_lexer_new_main ();
3194 /* Initialize the binops_by_token so that we can get the tree
3195 directly from the token. */
3196 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3197 binops_by_token[binops[i].token_type] = binops[i];
3199 parser = ggc_alloc_cleared_cp_parser ();
3200 parser->lexer = lexer;
3201 parser->context = cp_parser_context_new (NULL);
3203 /* For now, we always accept GNU extensions. */
3204 parser->allow_gnu_extensions_p = 1;
3206 /* The `>' token is a greater-than operator, not the end of a
3208 parser->greater_than_is_operator_p = true;
3210 parser->default_arg_ok_p = true;
3212 /* We are not parsing a constant-expression. */
3213 parser->integral_constant_expression_p = false;
3214 parser->allow_non_integral_constant_expression_p = false;
3215 parser->non_integral_constant_expression_p = false;
3217 /* Local variable names are not forbidden. */
3218 parser->local_variables_forbidden_p = false;
3220 /* We are not processing an `extern "C"' declaration. */
3221 parser->in_unbraced_linkage_specification_p = false;
3223 /* We are not processing a declarator. */
3224 parser->in_declarator_p = false;
3226 /* We are not processing a template-argument-list. */
3227 parser->in_template_argument_list_p = false;
3229 /* We are not in an iteration statement. */
3230 parser->in_statement = 0;
3232 /* We are not in a switch statement. */
3233 parser->in_switch_statement_p = false;
3235 /* We are not parsing a type-id inside an expression. */
3236 parser->in_type_id_in_expr_p = false;
3238 /* Declarations aren't implicitly extern "C". */
3239 parser->implicit_extern_c = false;
3241 /* String literals should be translated to the execution character set. */
3242 parser->translate_strings_p = true;
3244 /* We are not parsing a function body. */
3245 parser->in_function_body = false;
3247 /* The unparsed function queue is empty. */
3248 push_unparsed_function_queues (parser);
3250 /* There are no classes being defined. */
3251 parser->num_classes_being_defined = 0;
3253 /* No template parameters apply. */
3254 parser->num_template_parameter_lists = 0;
3259 /* Create a cp_lexer structure which will emit the tokens in CACHE
3260 and push it onto the parser's lexer stack. This is used for delayed
3261 parsing of in-class method bodies and default arguments, and should
3262 not be confused with tentative parsing. */
3264 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3266 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3267 lexer->next = parser->lexer;
3268 parser->lexer = lexer;
3270 /* Move the current source position to that of the first token in the
3272 cp_lexer_set_source_position_from_token (lexer->next_token);
3275 /* Pop the top lexer off the parser stack. This is never used for the
3276 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3278 cp_parser_pop_lexer (cp_parser *parser)
3280 cp_lexer *lexer = parser->lexer;
3281 parser->lexer = lexer->next;
3282 cp_lexer_destroy (lexer);
3284 /* Put the current source position back where it was before this
3285 lexer was pushed. */
3286 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3289 /* Lexical conventions [gram.lex] */
3291 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3295 cp_parser_identifier (cp_parser* parser)
3299 /* Look for the identifier. */
3300 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3301 /* Return the value. */
3302 return token ? token->u.value : error_mark_node;
3305 /* Parse a sequence of adjacent string constants. Returns a
3306 TREE_STRING representing the combined, nul-terminated string
3307 constant. If TRANSLATE is true, translate the string to the
3308 execution character set. If WIDE_OK is true, a wide string is
3311 C++98 [lex.string] says that if a narrow string literal token is
3312 adjacent to a wide string literal token, the behavior is undefined.
3313 However, C99 6.4.5p4 says that this results in a wide string literal.
3314 We follow C99 here, for consistency with the C front end.
3316 This code is largely lifted from lex_string() in c-lex.c.
3318 FUTURE: ObjC++ will need to handle @-strings here. */
3320 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3324 struct obstack str_ob;
3325 cpp_string str, istr, *strs;
3327 enum cpp_ttype type;
3329 tok = cp_lexer_peek_token (parser->lexer);
3330 if (!cp_parser_is_string_literal (tok))
3332 cp_parser_error (parser, "expected string-literal");
3333 return error_mark_node;
3338 /* Try to avoid the overhead of creating and destroying an obstack
3339 for the common case of just one string. */
3340 if (!cp_parser_is_string_literal
3341 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3343 cp_lexer_consume_token (parser->lexer);
3345 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3346 str.len = TREE_STRING_LENGTH (tok->u.value);
3353 gcc_obstack_init (&str_ob);
3358 cp_lexer_consume_token (parser->lexer);
3360 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3361 str.len = TREE_STRING_LENGTH (tok->u.value);
3363 if (type != tok->type)
3365 if (type == CPP_STRING)
3367 else if (tok->type != CPP_STRING)
3368 error_at (tok->location,
3369 "unsupported non-standard concatenation "
3370 "of string literals");
3373 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3375 tok = cp_lexer_peek_token (parser->lexer);
3377 while (cp_parser_is_string_literal (tok));
3379 strs = (cpp_string *) obstack_finish (&str_ob);
3382 if (type != CPP_STRING && !wide_ok)
3384 cp_parser_error (parser, "a wide string is invalid in this context");
3388 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3389 (parse_in, strs, count, &istr, type))
3391 value = build_string (istr.len, (const char *)istr.text);
3392 free (CONST_CAST (unsigned char *, istr.text));
3398 case CPP_UTF8STRING:
3399 TREE_TYPE (value) = char_array_type_node;
3402 TREE_TYPE (value) = char16_array_type_node;
3405 TREE_TYPE (value) = char32_array_type_node;
3408 TREE_TYPE (value) = wchar_array_type_node;
3412 value = fix_string_type (value);
3415 /* cpp_interpret_string has issued an error. */
3416 value = error_mark_node;
3419 obstack_free (&str_ob, 0);
3425 /* Basic concepts [gram.basic] */
3427 /* Parse a translation-unit.
3430 declaration-seq [opt]
3432 Returns TRUE if all went well. */
3435 cp_parser_translation_unit (cp_parser* parser)
3437 /* The address of the first non-permanent object on the declarator
3439 static void *declarator_obstack_base;
3443 /* Create the declarator obstack, if necessary. */
3444 if (!cp_error_declarator)
3446 gcc_obstack_init (&declarator_obstack);
3447 /* Create the error declarator. */
3448 cp_error_declarator = make_declarator (cdk_error);
3449 /* Create the empty parameter list. */
3450 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3451 /* Remember where the base of the declarator obstack lies. */
3452 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3455 cp_parser_declaration_seq_opt (parser);
3457 /* If there are no tokens left then all went well. */
3458 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3460 /* Get rid of the token array; we don't need it any more. */
3461 cp_lexer_destroy (parser->lexer);
3462 parser->lexer = NULL;
3464 /* This file might have been a context that's implicitly extern
3465 "C". If so, pop the lang context. (Only relevant for PCH.) */
3466 if (parser->implicit_extern_c)
3468 pop_lang_context ();
3469 parser->implicit_extern_c = false;
3473 finish_translation_unit ();
3479 cp_parser_error (parser, "expected declaration");
3483 /* Make sure the declarator obstack was fully cleaned up. */
3484 gcc_assert (obstack_next_free (&declarator_obstack)
3485 == declarator_obstack_base);
3487 /* All went well. */
3491 /* Expressions [gram.expr] */
3493 /* Parse a primary-expression.
3504 ( compound-statement )
3505 __builtin_va_arg ( assignment-expression , type-id )
3506 __builtin_offsetof ( type-id , offsetof-expression )
3509 __has_nothrow_assign ( type-id )
3510 __has_nothrow_constructor ( type-id )
3511 __has_nothrow_copy ( type-id )
3512 __has_trivial_assign ( type-id )
3513 __has_trivial_constructor ( type-id )
3514 __has_trivial_copy ( type-id )
3515 __has_trivial_destructor ( type-id )
3516 __has_virtual_destructor ( type-id )
3517 __is_abstract ( type-id )
3518 __is_base_of ( type-id , type-id )
3519 __is_class ( type-id )
3520 __is_convertible_to ( type-id , type-id )
3521 __is_empty ( type-id )
3522 __is_enum ( type-id )
3523 __is_pod ( type-id )
3524 __is_polymorphic ( type-id )
3525 __is_union ( type-id )
3527 Objective-C++ Extension:
3535 ADDRESS_P is true iff this expression was immediately preceded by
3536 "&" and therefore might denote a pointer-to-member. CAST_P is true
3537 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3538 true iff this expression is a template argument.
3540 Returns a representation of the expression. Upon return, *IDK
3541 indicates what kind of id-expression (if any) was present. */
3544 cp_parser_primary_expression (cp_parser *parser,
3547 bool template_arg_p,
3550 cp_token *token = NULL;
3552 /* Assume the primary expression is not an id-expression. */
3553 *idk = CP_ID_KIND_NONE;
3555 /* Peek at the next token. */
3556 token = cp_lexer_peek_token (parser->lexer);
3557 switch (token->type)
3570 token = cp_lexer_consume_token (parser->lexer);
3571 if (TREE_CODE (token->u.value) == FIXED_CST)
3573 error_at (token->location,
3574 "fixed-point types not supported in C++");
3575 return error_mark_node;
3577 /* Floating-point literals are only allowed in an integral
3578 constant expression if they are cast to an integral or
3579 enumeration type. */
3580 if (TREE_CODE (token->u.value) == REAL_CST
3581 && parser->integral_constant_expression_p
3584 /* CAST_P will be set even in invalid code like "int(2.7 +
3585 ...)". Therefore, we have to check that the next token
3586 is sure to end the cast. */
3589 cp_token *next_token;
3591 next_token = cp_lexer_peek_token (parser->lexer);
3592 if (/* The comma at the end of an
3593 enumerator-definition. */
3594 next_token->type != CPP_COMMA
3595 /* The curly brace at the end of an enum-specifier. */
3596 && next_token->type != CPP_CLOSE_BRACE
3597 /* The end of a statement. */
3598 && next_token->type != CPP_SEMICOLON
3599 /* The end of the cast-expression. */
3600 && next_token->type != CPP_CLOSE_PAREN
3601 /* The end of an array bound. */
3602 && next_token->type != CPP_CLOSE_SQUARE
3603 /* The closing ">" in a template-argument-list. */
3604 && (next_token->type != CPP_GREATER
3605 || parser->greater_than_is_operator_p)
3606 /* C++0x only: A ">>" treated like two ">" tokens,
3607 in a template-argument-list. */
3608 && (next_token->type != CPP_RSHIFT
3609 || (cxx_dialect == cxx98)
3610 || parser->greater_than_is_operator_p))
3614 /* If we are within a cast, then the constraint that the
3615 cast is to an integral or enumeration type will be
3616 checked at that point. If we are not within a cast, then
3617 this code is invalid. */
3619 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3621 return token->u.value;
3627 case CPP_UTF8STRING:
3628 /* ??? Should wide strings be allowed when parser->translate_strings_p
3629 is false (i.e. in attributes)? If not, we can kill the third
3630 argument to cp_parser_string_literal. */
3631 return cp_parser_string_literal (parser,
3632 parser->translate_strings_p,
3635 case CPP_OPEN_PAREN:
3638 bool saved_greater_than_is_operator_p;
3640 /* Consume the `('. */
3641 cp_lexer_consume_token (parser->lexer);
3642 /* Within a parenthesized expression, a `>' token is always
3643 the greater-than operator. */
3644 saved_greater_than_is_operator_p
3645 = parser->greater_than_is_operator_p;
3646 parser->greater_than_is_operator_p = true;
3647 /* If we see `( { ' then we are looking at the beginning of
3648 a GNU statement-expression. */
3649 if (cp_parser_allow_gnu_extensions_p (parser)
3650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3652 /* Statement-expressions are not allowed by the standard. */
3653 pedwarn (token->location, OPT_pedantic,
3654 "ISO C++ forbids braced-groups within expressions");
3656 /* And they're not allowed outside of a function-body; you
3657 cannot, for example, write:
3659 int i = ({ int j = 3; j + 1; });
3661 at class or namespace scope. */
3662 if (!parser->in_function_body
3663 || parser->in_template_argument_list_p)
3665 error_at (token->location,
3666 "statement-expressions are not allowed outside "
3667 "functions nor in template-argument lists");
3668 cp_parser_skip_to_end_of_block_or_statement (parser);
3669 expr = error_mark_node;
3673 /* Start the statement-expression. */
3674 expr = begin_stmt_expr ();
3675 /* Parse the compound-statement. */
3676 cp_parser_compound_statement (parser, expr, false);
3678 expr = finish_stmt_expr (expr, false);
3683 /* Parse the parenthesized expression. */
3684 expr = cp_parser_expression (parser, cast_p, idk);
3685 /* Let the front end know that this expression was
3686 enclosed in parentheses. This matters in case, for
3687 example, the expression is of the form `A::B', since
3688 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3690 finish_parenthesized_expr (expr);
3692 /* The `>' token might be the end of a template-id or
3693 template-parameter-list now. */
3694 parser->greater_than_is_operator_p
3695 = saved_greater_than_is_operator_p;
3696 /* Consume the `)'. */
3697 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3698 cp_parser_skip_to_end_of_statement (parser);
3703 case CPP_OPEN_SQUARE:
3704 if (c_dialect_objc ())
3705 /* We have an Objective-C++ message. */
3706 return cp_parser_objc_expression (parser);
3707 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3708 return cp_parser_lambda_expression (parser);
3710 case CPP_OBJC_STRING:
3711 if (c_dialect_objc ())
3712 /* We have an Objective-C++ string literal. */
3713 return cp_parser_objc_expression (parser);
3714 cp_parser_error (parser, "expected primary-expression");
3715 return error_mark_node;
3718 switch (token->keyword)
3720 /* These two are the boolean literals. */
3722 cp_lexer_consume_token (parser->lexer);
3723 return boolean_true_node;
3725 cp_lexer_consume_token (parser->lexer);
3726 return boolean_false_node;
3728 /* The `__null' literal. */
3730 cp_lexer_consume_token (parser->lexer);
3733 /* The `nullptr' literal. */
3735 cp_lexer_consume_token (parser->lexer);
3736 return nullptr_node;
3738 /* Recognize the `this' keyword. */
3740 cp_lexer_consume_token (parser->lexer);
3741 if (parser->local_variables_forbidden_p)
3743 error_at (token->location,
3744 "%<this%> may not be used in this context");
3745 return error_mark_node;
3747 /* Pointers cannot appear in constant-expressions. */
3748 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3749 return error_mark_node;
3750 return finish_this_expr ();
3752 /* The `operator' keyword can be the beginning of an
3757 case RID_FUNCTION_NAME:
3758 case RID_PRETTY_FUNCTION_NAME:
3759 case RID_C99_FUNCTION_NAME:
3761 non_integral_constant name;
3763 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3764 __func__ are the names of variables -- but they are
3765 treated specially. Therefore, they are handled here,
3766 rather than relying on the generic id-expression logic
3767 below. Grammatically, these names are id-expressions.
3769 Consume the token. */
3770 token = cp_lexer_consume_token (parser->lexer);
3772 switch (token->keyword)
3774 case RID_FUNCTION_NAME:
3775 name = NIC_FUNC_NAME;
3777 case RID_PRETTY_FUNCTION_NAME:
3778 name = NIC_PRETTY_FUNC;
3780 case RID_C99_FUNCTION_NAME:
3781 name = NIC_C99_FUNC;
3787 if (cp_parser_non_integral_constant_expression (parser, name))
3788 return error_mark_node;
3790 /* Look up the name. */
3791 return finish_fname (token->u.value);
3799 /* The `__builtin_va_arg' construct is used to handle
3800 `va_arg'. Consume the `__builtin_va_arg' token. */
3801 cp_lexer_consume_token (parser->lexer);
3802 /* Look for the opening `('. */
3803 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3804 /* Now, parse the assignment-expression. */
3805 expression = cp_parser_assignment_expression (parser,
3806 /*cast_p=*/false, NULL);
3807 /* Look for the `,'. */
3808 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3809 /* Parse the type-id. */
3810 type = cp_parser_type_id (parser);
3811 /* Look for the closing `)'. */
3812 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3813 /* Using `va_arg' in a constant-expression is not
3815 if (cp_parser_non_integral_constant_expression (parser,
3817 return error_mark_node;
3818 return build_x_va_arg (expression, type);
3822 return cp_parser_builtin_offsetof (parser);
3824 case RID_HAS_NOTHROW_ASSIGN:
3825 case RID_HAS_NOTHROW_CONSTRUCTOR:
3826 case RID_HAS_NOTHROW_COPY:
3827 case RID_HAS_TRIVIAL_ASSIGN:
3828 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3829 case RID_HAS_TRIVIAL_COPY:
3830 case RID_HAS_TRIVIAL_DESTRUCTOR:
3831 case RID_HAS_VIRTUAL_DESTRUCTOR:
3832 case RID_IS_ABSTRACT:
3833 case RID_IS_BASE_OF:
3835 case RID_IS_CONVERTIBLE_TO:
3839 case RID_IS_POLYMORPHIC:
3840 case RID_IS_STD_LAYOUT:
3841 case RID_IS_TRIVIAL:
3843 case RID_IS_LITERAL_TYPE:
3844 return cp_parser_trait_expr (parser, token->keyword);
3846 /* Objective-C++ expressions. */
3848 case RID_AT_PROTOCOL:
3849 case RID_AT_SELECTOR:
3850 return cp_parser_objc_expression (parser);
3853 if (parser->in_function_body
3854 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3857 error_at (token->location,
3858 "a template declaration cannot appear at block scope");
3859 cp_parser_skip_to_end_of_block_or_statement (parser);
3860 return error_mark_node;
3863 cp_parser_error (parser, "expected primary-expression");
3864 return error_mark_node;
3867 /* An id-expression can start with either an identifier, a
3868 `::' as the beginning of a qualified-id, or the "operator"
3872 case CPP_TEMPLATE_ID:
3873 case CPP_NESTED_NAME_SPECIFIER:
3877 const char *error_msg;
3880 cp_token *id_expr_token;
3883 /* Parse the id-expression. */
3885 = cp_parser_id_expression (parser,
3886 /*template_keyword_p=*/false,
3887 /*check_dependency_p=*/true,
3889 /*declarator_p=*/false,
3890 /*optional_p=*/false);
3891 if (id_expression == error_mark_node)
3892 return error_mark_node;
3893 id_expr_token = token;
3894 token = cp_lexer_peek_token (parser->lexer);
3895 done = (token->type != CPP_OPEN_SQUARE
3896 && token->type != CPP_OPEN_PAREN
3897 && token->type != CPP_DOT
3898 && token->type != CPP_DEREF
3899 && token->type != CPP_PLUS_PLUS
3900 && token->type != CPP_MINUS_MINUS);
3901 /* If we have a template-id, then no further lookup is
3902 required. If the template-id was for a template-class, we
3903 will sometimes have a TYPE_DECL at this point. */
3904 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3905 || TREE_CODE (id_expression) == TYPE_DECL)
3906 decl = id_expression;
3907 /* Look up the name. */
3910 tree ambiguous_decls;
3912 /* If we already know that this lookup is ambiguous, then
3913 we've already issued an error message; there's no reason
3915 if (id_expr_token->type == CPP_NAME
3916 && id_expr_token->ambiguous_p)
3918 cp_parser_simulate_error (parser);
3919 return error_mark_node;
3922 decl = cp_parser_lookup_name (parser, id_expression,
3925 /*is_namespace=*/false,
3926 /*check_dependency=*/true,
3928 id_expr_token->location);
3929 /* If the lookup was ambiguous, an error will already have
3931 if (ambiguous_decls)
3932 return error_mark_node;
3934 /* In Objective-C++, we may have an Objective-C 2.0
3935 dot-syntax for classes here. */
3936 if (c_dialect_objc ()
3937 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3938 && TREE_CODE (decl) == TYPE_DECL
3939 && objc_is_class_name (decl))
3942 cp_lexer_consume_token (parser->lexer);
3943 component = cp_parser_identifier (parser);
3944 if (component == error_mark_node)
3945 return error_mark_node;
3947 return objc_build_class_component_ref (id_expression, component);
3950 /* In Objective-C++, an instance variable (ivar) may be preferred
3951 to whatever cp_parser_lookup_name() found. */
3952 decl = objc_lookup_ivar (decl, id_expression);
3954 /* If name lookup gives us a SCOPE_REF, then the
3955 qualifying scope was dependent. */
3956 if (TREE_CODE (decl) == SCOPE_REF)
3958 /* At this point, we do not know if DECL is a valid
3959 integral constant expression. We assume that it is
3960 in fact such an expression, so that code like:
3962 template <int N> struct A {
3966 is accepted. At template-instantiation time, we
3967 will check that B<N>::i is actually a constant. */
3970 /* Check to see if DECL is a local variable in a context
3971 where that is forbidden. */
3972 if (parser->local_variables_forbidden_p
3973 && local_variable_p (decl))
3975 /* It might be that we only found DECL because we are
3976 trying to be generous with pre-ISO scoping rules.
3977 For example, consider:
3981 for (int i = 0; i < 10; ++i) {}
3982 extern void f(int j = i);
3985 Here, name look up will originally find the out
3986 of scope `i'. We need to issue a warning message,
3987 but then use the global `i'. */
3988 decl = check_for_out_of_scope_variable (decl);
3989 if (local_variable_p (decl))
3991 error_at (id_expr_token->location,
3992 "local variable %qD may not appear in this context",
3994 return error_mark_node;
3999 decl = (finish_id_expression
4000 (id_expression, decl, parser->scope,
4002 parser->integral_constant_expression_p,
4003 parser->allow_non_integral_constant_expression_p,
4004 &parser->non_integral_constant_expression_p,
4005 template_p, done, address_p,
4008 id_expr_token->location));
4010 cp_parser_error (parser, error_msg);
4014 /* Anything else is an error. */
4016 cp_parser_error (parser, "expected primary-expression");
4017 return error_mark_node;
4021 /* Parse an id-expression.
4028 :: [opt] nested-name-specifier template [opt] unqualified-id
4030 :: operator-function-id
4033 Return a representation of the unqualified portion of the
4034 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4035 a `::' or nested-name-specifier.
4037 Often, if the id-expression was a qualified-id, the caller will
4038 want to make a SCOPE_REF to represent the qualified-id. This
4039 function does not do this in order to avoid wastefully creating
4040 SCOPE_REFs when they are not required.
4042 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4045 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4046 uninstantiated templates.
4048 If *TEMPLATE_P is non-NULL, it is set to true iff the
4049 `template' keyword is used to explicitly indicate that the entity
4050 named is a template.
4052 If DECLARATOR_P is true, the id-expression is appearing as part of
4053 a declarator, rather than as part of an expression. */
4056 cp_parser_id_expression (cp_parser *parser,
4057 bool template_keyword_p,
4058 bool check_dependency_p,
4063 bool global_scope_p;
4064 bool nested_name_specifier_p;
4066 /* Assume the `template' keyword was not used. */
4068 *template_p = template_keyword_p;
4070 /* Look for the optional `::' operator. */
4072 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4074 /* Look for the optional nested-name-specifier. */
4075 nested_name_specifier_p
4076 = (cp_parser_nested_name_specifier_opt (parser,
4077 /*typename_keyword_p=*/false,
4082 /* If there is a nested-name-specifier, then we are looking at
4083 the first qualified-id production. */
4084 if (nested_name_specifier_p)
4087 tree saved_object_scope;
4088 tree saved_qualifying_scope;
4089 tree unqualified_id;
4092 /* See if the next token is the `template' keyword. */
4094 template_p = &is_template;
4095 *template_p = cp_parser_optional_template_keyword (parser);
4096 /* Name lookup we do during the processing of the
4097 unqualified-id might obliterate SCOPE. */
4098 saved_scope = parser->scope;
4099 saved_object_scope = parser->object_scope;
4100 saved_qualifying_scope = parser->qualifying_scope;
4101 /* Process the final unqualified-id. */
4102 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4105 /*optional_p=*/false);
4106 /* Restore the SAVED_SCOPE for our caller. */
4107 parser->scope = saved_scope;
4108 parser->object_scope = saved_object_scope;
4109 parser->qualifying_scope = saved_qualifying_scope;
4111 return unqualified_id;
4113 /* Otherwise, if we are in global scope, then we are looking at one
4114 of the other qualified-id productions. */
4115 else if (global_scope_p)
4120 /* Peek at the next token. */
4121 token = cp_lexer_peek_token (parser->lexer);
4123 /* If it's an identifier, and the next token is not a "<", then
4124 we can avoid the template-id case. This is an optimization
4125 for this common case. */
4126 if (token->type == CPP_NAME
4127 && !cp_parser_nth_token_starts_template_argument_list_p
4129 return cp_parser_identifier (parser);
4131 cp_parser_parse_tentatively (parser);
4132 /* Try a template-id. */
4133 id = cp_parser_template_id (parser,
4134 /*template_keyword_p=*/false,
4135 /*check_dependency_p=*/true,
4137 /* If that worked, we're done. */
4138 if (cp_parser_parse_definitely (parser))
4141 /* Peek at the next token. (Changes in the token buffer may
4142 have invalidated the pointer obtained above.) */
4143 token = cp_lexer_peek_token (parser->lexer);
4145 switch (token->type)
4148 return cp_parser_identifier (parser);
4151 if (token->keyword == RID_OPERATOR)
4152 return cp_parser_operator_function_id (parser);
4156 cp_parser_error (parser, "expected id-expression");
4157 return error_mark_node;
4161 return cp_parser_unqualified_id (parser, template_keyword_p,
4162 /*check_dependency_p=*/true,
4167 /* Parse an unqualified-id.
4171 operator-function-id
4172 conversion-function-id
4176 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4177 keyword, in a construct like `A::template ...'.
4179 Returns a representation of unqualified-id. For the `identifier'
4180 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4181 production a BIT_NOT_EXPR is returned; the operand of the
4182 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4183 other productions, see the documentation accompanying the
4184 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4185 names are looked up in uninstantiated templates. If DECLARATOR_P
4186 is true, the unqualified-id is appearing as part of a declarator,
4187 rather than as part of an expression. */
4190 cp_parser_unqualified_id (cp_parser* parser,
4191 bool template_keyword_p,
4192 bool check_dependency_p,
4198 /* Peek at the next token. */
4199 token = cp_lexer_peek_token (parser->lexer);
4201 switch (token->type)
4207 /* We don't know yet whether or not this will be a
4209 cp_parser_parse_tentatively (parser);
4210 /* Try a template-id. */
4211 id = cp_parser_template_id (parser, template_keyword_p,
4214 /* If it worked, we're done. */
4215 if (cp_parser_parse_definitely (parser))
4217 /* Otherwise, it's an ordinary identifier. */
4218 return cp_parser_identifier (parser);
4221 case CPP_TEMPLATE_ID:
4222 return cp_parser_template_id (parser, template_keyword_p,
4229 tree qualifying_scope;
4234 /* Consume the `~' token. */
4235 cp_lexer_consume_token (parser->lexer);
4236 /* Parse the class-name. The standard, as written, seems to
4239 template <typename T> struct S { ~S (); };
4240 template <typename T> S<T>::~S() {}
4242 is invalid, since `~' must be followed by a class-name, but
4243 `S<T>' is dependent, and so not known to be a class.
4244 That's not right; we need to look in uninstantiated
4245 templates. A further complication arises from:
4247 template <typename T> void f(T t) {
4251 Here, it is not possible to look up `T' in the scope of `T'
4252 itself. We must look in both the current scope, and the
4253 scope of the containing complete expression.
4255 Yet another issue is:
4264 The standard does not seem to say that the `S' in `~S'
4265 should refer to the type `S' and not the data member
4268 /* DR 244 says that we look up the name after the "~" in the
4269 same scope as we looked up the qualifying name. That idea
4270 isn't fully worked out; it's more complicated than that. */
4271 scope = parser->scope;
4272 object_scope = parser->object_scope;
4273 qualifying_scope = parser->qualifying_scope;
4275 /* Check for invalid scopes. */
4276 if (scope == error_mark_node)
4278 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4279 cp_lexer_consume_token (parser->lexer);
4280 return error_mark_node;
4282 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4284 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4285 error_at (token->location,
4286 "scope %qT before %<~%> is not a class-name",
4288 cp_parser_simulate_error (parser);
4289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4290 cp_lexer_consume_token (parser->lexer);
4291 return error_mark_node;
4293 gcc_assert (!scope || TYPE_P (scope));
4295 /* If the name is of the form "X::~X" it's OK even if X is a
4297 token = cp_lexer_peek_token (parser->lexer);
4299 && token->type == CPP_NAME
4300 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4302 && (token->u.value == TYPE_IDENTIFIER (scope)
4303 || constructor_name_p (token->u.value, scope)))
4305 cp_lexer_consume_token (parser->lexer);
4306 return build_nt (BIT_NOT_EXPR, scope);
4309 /* If there was an explicit qualification (S::~T), first look
4310 in the scope given by the qualification (i.e., S).
4312 Note: in the calls to cp_parser_class_name below we pass
4313 typename_type so that lookup finds the injected-class-name
4314 rather than the constructor. */
4316 type_decl = NULL_TREE;
4319 cp_parser_parse_tentatively (parser);
4320 type_decl = cp_parser_class_name (parser,
4321 /*typename_keyword_p=*/false,
4322 /*template_keyword_p=*/false,
4324 /*check_dependency=*/false,
4325 /*class_head_p=*/false,
4327 if (cp_parser_parse_definitely (parser))
4330 /* In "N::S::~S", look in "N" as well. */
4331 if (!done && scope && qualifying_scope)
4333 cp_parser_parse_tentatively (parser);
4334 parser->scope = qualifying_scope;
4335 parser->object_scope = NULL_TREE;
4336 parser->qualifying_scope = NULL_TREE;
4338 = cp_parser_class_name (parser,
4339 /*typename_keyword_p=*/false,
4340 /*template_keyword_p=*/false,
4342 /*check_dependency=*/false,
4343 /*class_head_p=*/false,
4345 if (cp_parser_parse_definitely (parser))
4348 /* In "p->S::~T", look in the scope given by "*p" as well. */
4349 else if (!done && object_scope)
4351 cp_parser_parse_tentatively (parser);
4352 parser->scope = object_scope;
4353 parser->object_scope = NULL_TREE;
4354 parser->qualifying_scope = NULL_TREE;
4356 = cp_parser_class_name (parser,
4357 /*typename_keyword_p=*/false,
4358 /*template_keyword_p=*/false,
4360 /*check_dependency=*/false,
4361 /*class_head_p=*/false,
4363 if (cp_parser_parse_definitely (parser))
4366 /* Look in the surrounding context. */
4369 parser->scope = NULL_TREE;
4370 parser->object_scope = NULL_TREE;
4371 parser->qualifying_scope = NULL_TREE;
4372 if (processing_template_decl)
4373 cp_parser_parse_tentatively (parser);
4375 = cp_parser_class_name (parser,
4376 /*typename_keyword_p=*/false,
4377 /*template_keyword_p=*/false,
4379 /*check_dependency=*/false,
4380 /*class_head_p=*/false,
4382 if (processing_template_decl
4383 && ! cp_parser_parse_definitely (parser))
4385 /* We couldn't find a type with this name, so just accept
4386 it and check for a match at instantiation time. */
4387 type_decl = cp_parser_identifier (parser);
4388 if (type_decl != error_mark_node)
4389 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4393 /* If an error occurred, assume that the name of the
4394 destructor is the same as the name of the qualifying
4395 class. That allows us to keep parsing after running
4396 into ill-formed destructor names. */
4397 if (type_decl == error_mark_node && scope)
4398 return build_nt (BIT_NOT_EXPR, scope);
4399 else if (type_decl == error_mark_node)
4400 return error_mark_node;
4402 /* Check that destructor name and scope match. */
4403 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4405 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4406 error_at (token->location,
4407 "declaration of %<~%T%> as member of %qT",
4409 cp_parser_simulate_error (parser);
4410 return error_mark_node;
4415 A typedef-name that names a class shall not be used as the
4416 identifier in the declarator for a destructor declaration. */
4418 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4419 && !DECL_SELF_REFERENCE_P (type_decl)
4420 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4421 error_at (token->location,
4422 "typedef-name %qD used as destructor declarator",
4425 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4429 if (token->keyword == RID_OPERATOR)
4433 /* This could be a template-id, so we try that first. */
4434 cp_parser_parse_tentatively (parser);
4435 /* Try a template-id. */
4436 id = cp_parser_template_id (parser, template_keyword_p,
4437 /*check_dependency_p=*/true,
4439 /* If that worked, we're done. */
4440 if (cp_parser_parse_definitely (parser))
4442 /* We still don't know whether we're looking at an
4443 operator-function-id or a conversion-function-id. */
4444 cp_parser_parse_tentatively (parser);
4445 /* Try an operator-function-id. */
4446 id = cp_parser_operator_function_id (parser);
4447 /* If that didn't work, try a conversion-function-id. */
4448 if (!cp_parser_parse_definitely (parser))
4449 id = cp_parser_conversion_function_id (parser);
4458 cp_parser_error (parser, "expected unqualified-id");
4459 return error_mark_node;
4463 /* Parse an (optional) nested-name-specifier.
4465 nested-name-specifier: [C++98]
4466 class-or-namespace-name :: nested-name-specifier [opt]
4467 class-or-namespace-name :: template nested-name-specifier [opt]
4469 nested-name-specifier: [C++0x]
4472 nested-name-specifier identifier ::
4473 nested-name-specifier template [opt] simple-template-id ::
4475 PARSER->SCOPE should be set appropriately before this function is
4476 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4477 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4480 Sets PARSER->SCOPE to the class (TYPE) or namespace
4481 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4482 it unchanged if there is no nested-name-specifier. Returns the new
4483 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4485 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4486 part of a declaration and/or decl-specifier. */
4489 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4490 bool typename_keyword_p,
4491 bool check_dependency_p,
4493 bool is_declaration)
4495 bool success = false;
4496 cp_token_position start = 0;
4499 /* Remember where the nested-name-specifier starts. */
4500 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4502 start = cp_lexer_token_position (parser->lexer, false);
4503 push_deferring_access_checks (dk_deferred);
4510 tree saved_qualifying_scope;
4511 bool template_keyword_p;
4513 /* Spot cases that cannot be the beginning of a
4514 nested-name-specifier. */
4515 token = cp_lexer_peek_token (parser->lexer);
4517 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4518 the already parsed nested-name-specifier. */
4519 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4521 /* Grab the nested-name-specifier and continue the loop. */
4522 cp_parser_pre_parsed_nested_name_specifier (parser);
4523 /* If we originally encountered this nested-name-specifier
4524 with IS_DECLARATION set to false, we will not have
4525 resolved TYPENAME_TYPEs, so we must do so here. */
4527 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4529 new_scope = resolve_typename_type (parser->scope,
4530 /*only_current_p=*/false);
4531 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4532 parser->scope = new_scope;
4538 /* Spot cases that cannot be the beginning of a
4539 nested-name-specifier. On the second and subsequent times
4540 through the loop, we look for the `template' keyword. */
4541 if (success && token->keyword == RID_TEMPLATE)
4543 /* A template-id can start a nested-name-specifier. */
4544 else if (token->type == CPP_TEMPLATE_ID)
4548 /* If the next token is not an identifier, then it is
4549 definitely not a type-name or namespace-name. */
4550 if (token->type != CPP_NAME)
4552 /* If the following token is neither a `<' (to begin a
4553 template-id), nor a `::', then we are not looking at a
4554 nested-name-specifier. */
4555 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4556 if (token->type != CPP_SCOPE
4557 && !cp_parser_nth_token_starts_template_argument_list_p
4562 /* The nested-name-specifier is optional, so we parse
4564 cp_parser_parse_tentatively (parser);
4566 /* Look for the optional `template' keyword, if this isn't the
4567 first time through the loop. */
4569 template_keyword_p = cp_parser_optional_template_keyword (parser);
4571 template_keyword_p = false;
4573 /* Save the old scope since the name lookup we are about to do
4574 might destroy it. */
4575 old_scope = parser->scope;
4576 saved_qualifying_scope = parser->qualifying_scope;
4577 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4578 look up names in "X<T>::I" in order to determine that "Y" is
4579 a template. So, if we have a typename at this point, we make
4580 an effort to look through it. */
4582 && !typename_keyword_p
4584 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4585 parser->scope = resolve_typename_type (parser->scope,
4586 /*only_current_p=*/false);
4587 /* Parse the qualifying entity. */
4589 = cp_parser_qualifying_entity (parser,
4595 /* Look for the `::' token. */
4596 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4598 /* If we found what we wanted, we keep going; otherwise, we're
4600 if (!cp_parser_parse_definitely (parser))
4602 bool error_p = false;
4604 /* Restore the OLD_SCOPE since it was valid before the
4605 failed attempt at finding the last
4606 class-or-namespace-name. */
4607 parser->scope = old_scope;
4608 parser->qualifying_scope = saved_qualifying_scope;
4609 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4611 /* If the next token is an identifier, and the one after
4612 that is a `::', then any valid interpretation would have
4613 found a class-or-namespace-name. */
4614 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4615 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4617 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4620 token = cp_lexer_consume_token (parser->lexer);
4623 if (!token->ambiguous_p)
4626 tree ambiguous_decls;
4628 decl = cp_parser_lookup_name (parser, token->u.value,
4630 /*is_template=*/false,
4631 /*is_namespace=*/false,
4632 /*check_dependency=*/true,
4635 if (TREE_CODE (decl) == TEMPLATE_DECL)
4636 error_at (token->location,
4637 "%qD used without template parameters",
4639 else if (ambiguous_decls)
4641 error_at (token->location,
4642 "reference to %qD is ambiguous",
4644 print_candidates (ambiguous_decls);
4645 decl = error_mark_node;
4649 if (cxx_dialect != cxx98)
4650 cp_parser_name_lookup_error
4651 (parser, token->u.value, decl, NLE_NOT_CXX98,
4654 cp_parser_name_lookup_error
4655 (parser, token->u.value, decl, NLE_CXX98,
4659 parser->scope = error_mark_node;
4661 /* Treat this as a successful nested-name-specifier
4666 If the name found is not a class-name (clause
4667 _class_) or namespace-name (_namespace.def_), the
4668 program is ill-formed. */
4671 cp_lexer_consume_token (parser->lexer);
4675 /* We've found one valid nested-name-specifier. */
4677 /* Name lookup always gives us a DECL. */
4678 if (TREE_CODE (new_scope) == TYPE_DECL)
4679 new_scope = TREE_TYPE (new_scope);
4680 /* Uses of "template" must be followed by actual templates. */
4681 if (template_keyword_p
4682 && !(CLASS_TYPE_P (new_scope)
4683 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4684 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4685 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4686 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4687 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4688 == TEMPLATE_ID_EXPR)))
4689 permerror (input_location, TYPE_P (new_scope)
4690 ? "%qT is not a template"
4691 : "%qD is not a template",
4693 /* If it is a class scope, try to complete it; we are about to
4694 be looking up names inside the class. */
4695 if (TYPE_P (new_scope)
4696 /* Since checking types for dependency can be expensive,
4697 avoid doing it if the type is already complete. */
4698 && !COMPLETE_TYPE_P (new_scope)
4699 /* Do not try to complete dependent types. */
4700 && !dependent_type_p (new_scope))
4702 new_scope = complete_type (new_scope);
4703 /* If it is a typedef to current class, use the current
4704 class instead, as the typedef won't have any names inside
4706 if (!COMPLETE_TYPE_P (new_scope)
4707 && currently_open_class (new_scope))
4708 new_scope = TYPE_MAIN_VARIANT (new_scope);
4710 /* Make sure we look in the right scope the next time through
4712 parser->scope = new_scope;
4715 /* If parsing tentatively, replace the sequence of tokens that makes
4716 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4717 token. That way, should we re-parse the token stream, we will
4718 not have to repeat the effort required to do the parse, nor will
4719 we issue duplicate error messages. */
4720 if (success && start)
4724 token = cp_lexer_token_at (parser->lexer, start);
4725 /* Reset the contents of the START token. */
4726 token->type = CPP_NESTED_NAME_SPECIFIER;
4727 /* Retrieve any deferred checks. Do not pop this access checks yet
4728 so the memory will not be reclaimed during token replacing below. */
4729 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4730 token->u.tree_check_value->value = parser->scope;
4731 token->u.tree_check_value->checks = get_deferred_access_checks ();
4732 token->u.tree_check_value->qualifying_scope =
4733 parser->qualifying_scope;
4734 token->keyword = RID_MAX;
4736 /* Purge all subsequent tokens. */
4737 cp_lexer_purge_tokens_after (parser->lexer, start);
4741 pop_to_parent_deferring_access_checks ();
4743 return success ? parser->scope : NULL_TREE;
4746 /* Parse a nested-name-specifier. See
4747 cp_parser_nested_name_specifier_opt for details. This function
4748 behaves identically, except that it will an issue an error if no
4749 nested-name-specifier is present. */
4752 cp_parser_nested_name_specifier (cp_parser *parser,
4753 bool typename_keyword_p,
4754 bool check_dependency_p,
4756 bool is_declaration)
4760 /* Look for the nested-name-specifier. */
4761 scope = cp_parser_nested_name_specifier_opt (parser,
4766 /* If it was not present, issue an error message. */
4769 cp_parser_error (parser, "expected nested-name-specifier");
4770 parser->scope = NULL_TREE;
4776 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4777 this is either a class-name or a namespace-name (which corresponds
4778 to the class-or-namespace-name production in the grammar). For
4779 C++0x, it can also be a type-name that refers to an enumeration
4782 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4783 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4784 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4785 TYPE_P is TRUE iff the next name should be taken as a class-name,
4786 even the same name is declared to be another entity in the same
4789 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4790 specified by the class-or-namespace-name. If neither is found the
4791 ERROR_MARK_NODE is returned. */
4794 cp_parser_qualifying_entity (cp_parser *parser,
4795 bool typename_keyword_p,
4796 bool template_keyword_p,
4797 bool check_dependency_p,
4799 bool is_declaration)
4802 tree saved_qualifying_scope;
4803 tree saved_object_scope;
4806 bool successful_parse_p;
4808 /* Before we try to parse the class-name, we must save away the
4809 current PARSER->SCOPE since cp_parser_class_name will destroy
4811 saved_scope = parser->scope;
4812 saved_qualifying_scope = parser->qualifying_scope;
4813 saved_object_scope = parser->object_scope;
4814 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4815 there is no need to look for a namespace-name. */
4816 only_class_p = template_keyword_p
4817 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4819 cp_parser_parse_tentatively (parser);
4820 scope = cp_parser_class_name (parser,
4823 type_p ? class_type : none_type,
4825 /*class_head_p=*/false,
4827 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4828 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4830 && cxx_dialect != cxx98
4831 && !successful_parse_p)
4833 /* Restore the saved scope. */
4834 parser->scope = saved_scope;
4835 parser->qualifying_scope = saved_qualifying_scope;
4836 parser->object_scope = saved_object_scope;
4838 /* Parse tentatively. */
4839 cp_parser_parse_tentatively (parser);
4841 /* Parse a typedef-name or enum-name. */
4842 scope = cp_parser_nonclass_name (parser);
4844 /* "If the name found does not designate a namespace or a class,
4845 enumeration, or dependent type, the program is ill-formed."
4847 We cover classes and dependent types above and namespaces below,
4848 so this code is only looking for enums. */
4849 if (!scope || TREE_CODE (scope) != TYPE_DECL
4850 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4851 cp_parser_simulate_error (parser);
4853 successful_parse_p = cp_parser_parse_definitely (parser);
4855 /* If that didn't work, try for a namespace-name. */
4856 if (!only_class_p && !successful_parse_p)
4858 /* Restore the saved scope. */
4859 parser->scope = saved_scope;
4860 parser->qualifying_scope = saved_qualifying_scope;
4861 parser->object_scope = saved_object_scope;
4862 /* If we are not looking at an identifier followed by the scope
4863 resolution operator, then this is not part of a
4864 nested-name-specifier. (Note that this function is only used
4865 to parse the components of a nested-name-specifier.) */
4866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4867 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4868 return error_mark_node;
4869 scope = cp_parser_namespace_name (parser);
4875 /* Parse a postfix-expression.
4879 postfix-expression [ expression ]
4880 postfix-expression ( expression-list [opt] )
4881 simple-type-specifier ( expression-list [opt] )
4882 typename :: [opt] nested-name-specifier identifier
4883 ( expression-list [opt] )
4884 typename :: [opt] nested-name-specifier template [opt] template-id
4885 ( expression-list [opt] )
4886 postfix-expression . template [opt] id-expression
4887 postfix-expression -> template [opt] id-expression
4888 postfix-expression . pseudo-destructor-name
4889 postfix-expression -> pseudo-destructor-name
4890 postfix-expression ++
4891 postfix-expression --
4892 dynamic_cast < type-id > ( expression )
4893 static_cast < type-id > ( expression )
4894 reinterpret_cast < type-id > ( expression )
4895 const_cast < type-id > ( expression )
4896 typeid ( expression )
4902 ( type-id ) { initializer-list , [opt] }
4904 This extension is a GNU version of the C99 compound-literal
4905 construct. (The C99 grammar uses `type-name' instead of `type-id',
4906 but they are essentially the same concept.)
4908 If ADDRESS_P is true, the postfix expression is the operand of the
4909 `&' operator. CAST_P is true if this expression is the target of a
4912 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4913 class member access expressions [expr.ref].
4915 Returns a representation of the expression. */
4918 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4919 bool member_access_only_p,
4920 cp_id_kind * pidk_return)
4924 cp_id_kind idk = CP_ID_KIND_NONE;
4925 tree postfix_expression = NULL_TREE;
4926 bool is_member_access = false;
4928 /* Peek at the next token. */
4929 token = cp_lexer_peek_token (parser->lexer);
4930 /* Some of the productions are determined by keywords. */
4931 keyword = token->keyword;
4941 const char *saved_message;
4943 /* All of these can be handled in the same way from the point
4944 of view of parsing. Begin by consuming the token
4945 identifying the cast. */
4946 cp_lexer_consume_token (parser->lexer);
4948 /* New types cannot be defined in the cast. */
4949 saved_message = parser->type_definition_forbidden_message;
4950 parser->type_definition_forbidden_message
4951 = G_("types may not be defined in casts");
4953 /* Look for the opening `<'. */
4954 cp_parser_require (parser, CPP_LESS, RT_LESS);
4955 /* Parse the type to which we are casting. */
4956 type = cp_parser_type_id (parser);
4957 /* Look for the closing `>'. */
4958 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4959 /* Restore the old message. */
4960 parser->type_definition_forbidden_message = saved_message;
4962 /* And the expression which is being cast. */
4963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4964 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4965 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4967 /* Only type conversions to integral or enumeration types
4968 can be used in constant-expressions. */
4969 if (!cast_valid_in_integral_constant_expression_p (type)
4970 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4971 return error_mark_node;
4977 = build_dynamic_cast (type, expression, tf_warning_or_error);
4981 = build_static_cast (type, expression, tf_warning_or_error);
4985 = build_reinterpret_cast (type, expression,
4986 tf_warning_or_error);
4990 = build_const_cast (type, expression, tf_warning_or_error);
5001 const char *saved_message;
5002 bool saved_in_type_id_in_expr_p;
5004 /* Consume the `typeid' token. */
5005 cp_lexer_consume_token (parser->lexer);
5006 /* Look for the `(' token. */
5007 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5008 /* Types cannot be defined in a `typeid' expression. */
5009 saved_message = parser->type_definition_forbidden_message;
5010 parser->type_definition_forbidden_message
5011 = G_("types may not be defined in a %<typeid%> expression");
5012 /* We can't be sure yet whether we're looking at a type-id or an
5014 cp_parser_parse_tentatively (parser);
5015 /* Try a type-id first. */
5016 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5017 parser->in_type_id_in_expr_p = true;
5018 type = cp_parser_type_id (parser);
5019 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5020 /* Look for the `)' token. Otherwise, we can't be sure that
5021 we're not looking at an expression: consider `typeid (int
5022 (3))', for example. */
5023 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5024 /* If all went well, simply lookup the type-id. */
5025 if (cp_parser_parse_definitely (parser))
5026 postfix_expression = get_typeid (type);
5027 /* Otherwise, fall back to the expression variant. */
5032 /* Look for an expression. */
5033 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5034 /* Compute its typeid. */
5035 postfix_expression = build_typeid (expression);
5036 /* Look for the `)' token. */
5037 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5039 /* Restore the saved message. */
5040 parser->type_definition_forbidden_message = saved_message;
5041 /* `typeid' may not appear in an integral constant expression. */
5042 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5043 return error_mark_node;
5050 /* The syntax permitted here is the same permitted for an
5051 elaborated-type-specifier. */
5052 type = cp_parser_elaborated_type_specifier (parser,
5053 /*is_friend=*/false,
5054 /*is_declaration=*/false);
5055 postfix_expression = cp_parser_functional_cast (parser, type);
5063 /* If the next thing is a simple-type-specifier, we may be
5064 looking at a functional cast. We could also be looking at
5065 an id-expression. So, we try the functional cast, and if
5066 that doesn't work we fall back to the primary-expression. */
5067 cp_parser_parse_tentatively (parser);
5068 /* Look for the simple-type-specifier. */
5069 type = cp_parser_simple_type_specifier (parser,
5070 /*decl_specs=*/NULL,
5071 CP_PARSER_FLAGS_NONE);
5072 /* Parse the cast itself. */
5073 if (!cp_parser_error_occurred (parser))
5075 = cp_parser_functional_cast (parser, type);
5076 /* If that worked, we're done. */
5077 if (cp_parser_parse_definitely (parser))
5080 /* If the functional-cast didn't work out, try a
5081 compound-literal. */
5082 if (cp_parser_allow_gnu_extensions_p (parser)
5083 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5085 VEC(constructor_elt,gc) *initializer_list = NULL;
5086 bool saved_in_type_id_in_expr_p;
5088 cp_parser_parse_tentatively (parser);
5089 /* Consume the `('. */
5090 cp_lexer_consume_token (parser->lexer);
5091 /* Parse the type. */
5092 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5093 parser->in_type_id_in_expr_p = true;
5094 type = cp_parser_type_id (parser);
5095 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5096 /* Look for the `)'. */
5097 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5098 /* Look for the `{'. */
5099 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5100 /* If things aren't going well, there's no need to
5102 if (!cp_parser_error_occurred (parser))
5104 bool non_constant_p;
5105 /* Parse the initializer-list. */
5107 = cp_parser_initializer_list (parser, &non_constant_p);
5108 /* Allow a trailing `,'. */
5109 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5110 cp_lexer_consume_token (parser->lexer);
5111 /* Look for the final `}'. */
5112 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5114 /* If that worked, we're definitely looking at a
5115 compound-literal expression. */
5116 if (cp_parser_parse_definitely (parser))
5118 /* Warn the user that a compound literal is not
5119 allowed in standard C++. */
5120 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5121 /* For simplicity, we disallow compound literals in
5122 constant-expressions. We could
5123 allow compound literals of integer type, whose
5124 initializer was a constant, in constant
5125 expressions. Permitting that usage, as a further
5126 extension, would not change the meaning of any
5127 currently accepted programs. (Of course, as
5128 compound literals are not part of ISO C++, the
5129 standard has nothing to say.) */
5130 if (cp_parser_non_integral_constant_expression (parser,
5133 postfix_expression = error_mark_node;
5136 /* Form the representation of the compound-literal. */
5138 = (finish_compound_literal
5139 (type, build_constructor (init_list_type_node,
5140 initializer_list)));
5145 /* It must be a primary-expression. */
5147 = cp_parser_primary_expression (parser, address_p, cast_p,
5148 /*template_arg_p=*/false,
5154 /* Keep looping until the postfix-expression is complete. */
5157 if (idk == CP_ID_KIND_UNQUALIFIED
5158 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5159 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5160 /* It is not a Koenig lookup function call. */
5162 = unqualified_name_lookup_error (postfix_expression);
5164 /* Peek at the next token. */
5165 token = cp_lexer_peek_token (parser->lexer);
5167 switch (token->type)
5169 case CPP_OPEN_SQUARE:
5171 = cp_parser_postfix_open_square_expression (parser,
5174 idk = CP_ID_KIND_NONE;
5175 is_member_access = false;
5178 case CPP_OPEN_PAREN:
5179 /* postfix-expression ( expression-list [opt] ) */
5182 bool is_builtin_constant_p;
5183 bool saved_integral_constant_expression_p = false;
5184 bool saved_non_integral_constant_expression_p = false;
5187 is_member_access = false;
5189 is_builtin_constant_p
5190 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5191 if (is_builtin_constant_p)
5193 /* The whole point of __builtin_constant_p is to allow
5194 non-constant expressions to appear as arguments. */
5195 saved_integral_constant_expression_p
5196 = parser->integral_constant_expression_p;
5197 saved_non_integral_constant_expression_p
5198 = parser->non_integral_constant_expression_p;
5199 parser->integral_constant_expression_p = false;
5201 args = (cp_parser_parenthesized_expression_list
5203 /*cast_p=*/false, /*allow_expansion_p=*/true,
5204 /*non_constant_p=*/NULL));
5205 if (is_builtin_constant_p)
5207 parser->integral_constant_expression_p
5208 = saved_integral_constant_expression_p;
5209 parser->non_integral_constant_expression_p
5210 = saved_non_integral_constant_expression_p;
5215 postfix_expression = error_mark_node;
5219 /* Function calls are not permitted in
5220 constant-expressions. */
5221 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5222 && cp_parser_non_integral_constant_expression (parser,
5225 postfix_expression = error_mark_node;
5226 release_tree_vector (args);
5231 if (idk == CP_ID_KIND_UNQUALIFIED
5232 || idk == CP_ID_KIND_TEMPLATE_ID)
5234 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5236 if (!VEC_empty (tree, args))
5239 if (!any_type_dependent_arguments_p (args))
5241 = perform_koenig_lookup (postfix_expression, args,
5242 /*include_std=*/false);
5246 = unqualified_fn_lookup_error (postfix_expression);
5248 /* We do not perform argument-dependent lookup if
5249 normal lookup finds a non-function, in accordance
5250 with the expected resolution of DR 218. */
5251 else if (!VEC_empty (tree, args)
5252 && is_overloaded_fn (postfix_expression))
5254 tree fn = get_first_fn (postfix_expression);
5255 fn = STRIP_TEMPLATE (fn);
5257 /* Do not do argument dependent lookup if regular
5258 lookup finds a member function or a block-scope
5259 function declaration. [basic.lookup.argdep]/3 */
5260 if (!DECL_FUNCTION_MEMBER_P (fn)
5261 && !DECL_LOCAL_FUNCTION_P (fn))
5264 if (!any_type_dependent_arguments_p (args))
5266 = perform_koenig_lookup (postfix_expression, args,
5267 /*include_std=*/false);
5272 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5274 tree instance = TREE_OPERAND (postfix_expression, 0);
5275 tree fn = TREE_OPERAND (postfix_expression, 1);
5277 if (processing_template_decl
5278 && (type_dependent_expression_p (instance)
5279 || (!BASELINK_P (fn)
5280 && TREE_CODE (fn) != FIELD_DECL)
5281 || type_dependent_expression_p (fn)
5282 || any_type_dependent_arguments_p (args)))
5285 = build_nt_call_vec (postfix_expression, args);
5286 release_tree_vector (args);
5290 if (BASELINK_P (fn))
5293 = (build_new_method_call
5294 (instance, fn, &args, NULL_TREE,
5295 (idk == CP_ID_KIND_QUALIFIED
5296 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5298 tf_warning_or_error));
5302 = finish_call_expr (postfix_expression, &args,
5303 /*disallow_virtual=*/false,
5305 tf_warning_or_error);
5307 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5308 || TREE_CODE (postfix_expression) == MEMBER_REF
5309 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5310 postfix_expression = (build_offset_ref_call_from_tree
5311 (postfix_expression, &args));
5312 else if (idk == CP_ID_KIND_QUALIFIED)
5313 /* A call to a static class member, or a namespace-scope
5316 = finish_call_expr (postfix_expression, &args,
5317 /*disallow_virtual=*/true,
5319 tf_warning_or_error);
5321 /* All other function calls. */
5323 = finish_call_expr (postfix_expression, &args,
5324 /*disallow_virtual=*/false,
5326 tf_warning_or_error);
5328 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5329 idk = CP_ID_KIND_NONE;
5331 release_tree_vector (args);
5337 /* postfix-expression . template [opt] id-expression
5338 postfix-expression . pseudo-destructor-name
5339 postfix-expression -> template [opt] id-expression
5340 postfix-expression -> pseudo-destructor-name */
5342 /* Consume the `.' or `->' operator. */
5343 cp_lexer_consume_token (parser->lexer);
5346 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5351 is_member_access = true;
5355 /* postfix-expression ++ */
5356 /* Consume the `++' token. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* Generate a representation for the complete expression. */
5360 = finish_increment_expr (postfix_expression,
5361 POSTINCREMENT_EXPR);
5362 /* Increments may not appear in constant-expressions. */
5363 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5364 postfix_expression = error_mark_node;
5365 idk = CP_ID_KIND_NONE;
5366 is_member_access = false;
5369 case CPP_MINUS_MINUS:
5370 /* postfix-expression -- */
5371 /* Consume the `--' token. */
5372 cp_lexer_consume_token (parser->lexer);
5373 /* Generate a representation for the complete expression. */
5375 = finish_increment_expr (postfix_expression,
5376 POSTDECREMENT_EXPR);
5377 /* Decrements may not appear in constant-expressions. */
5378 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5379 postfix_expression = error_mark_node;
5380 idk = CP_ID_KIND_NONE;
5381 is_member_access = false;
5385 if (pidk_return != NULL)
5386 * pidk_return = idk;
5387 if (member_access_only_p)
5388 return is_member_access? postfix_expression : error_mark_node;
5390 return postfix_expression;
5394 /* We should never get here. */
5396 return error_mark_node;
5399 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5400 by cp_parser_builtin_offsetof. We're looking for
5402 postfix-expression [ expression ]
5404 FOR_OFFSETOF is set if we're being called in that context, which
5405 changes how we deal with integer constant expressions. */
5408 cp_parser_postfix_open_square_expression (cp_parser *parser,
5409 tree postfix_expression,
5414 /* Consume the `[' token. */
5415 cp_lexer_consume_token (parser->lexer);
5417 /* Parse the index expression. */
5418 /* ??? For offsetof, there is a question of what to allow here. If
5419 offsetof is not being used in an integral constant expression context,
5420 then we *could* get the right answer by computing the value at runtime.
5421 If we are in an integral constant expression context, then we might
5422 could accept any constant expression; hard to say without analysis.
5423 Rather than open the barn door too wide right away, allow only integer
5424 constant expressions here. */
5426 index = cp_parser_constant_expression (parser, false, NULL);
5428 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5430 /* Look for the closing `]'. */
5431 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5433 /* Build the ARRAY_REF. */
5434 postfix_expression = grok_array_decl (postfix_expression, index);
5436 /* When not doing offsetof, array references are not permitted in
5437 constant-expressions. */
5439 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5440 postfix_expression = error_mark_node;
5442 return postfix_expression;
5445 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5446 by cp_parser_builtin_offsetof. We're looking for
5448 postfix-expression . template [opt] id-expression
5449 postfix-expression . pseudo-destructor-name
5450 postfix-expression -> template [opt] id-expression
5451 postfix-expression -> pseudo-destructor-name
5453 FOR_OFFSETOF is set if we're being called in that context. That sorta
5454 limits what of the above we'll actually accept, but nevermind.
5455 TOKEN_TYPE is the "." or "->" token, which will already have been
5456 removed from the stream. */
5459 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5460 enum cpp_ttype token_type,
5461 tree postfix_expression,
5462 bool for_offsetof, cp_id_kind *idk,
5463 location_t location)
5467 bool pseudo_destructor_p;
5468 tree scope = NULL_TREE;
5470 /* If this is a `->' operator, dereference the pointer. */
5471 if (token_type == CPP_DEREF)
5472 postfix_expression = build_x_arrow (postfix_expression);
5473 /* Check to see whether or not the expression is type-dependent. */
5474 dependent_p = type_dependent_expression_p (postfix_expression);
5475 /* The identifier following the `->' or `.' is not qualified. */
5476 parser->scope = NULL_TREE;
5477 parser->qualifying_scope = NULL_TREE;
5478 parser->object_scope = NULL_TREE;
5479 *idk = CP_ID_KIND_NONE;
5481 /* Enter the scope corresponding to the type of the object
5482 given by the POSTFIX_EXPRESSION. */
5483 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5485 scope = TREE_TYPE (postfix_expression);
5486 /* According to the standard, no expression should ever have
5487 reference type. Unfortunately, we do not currently match
5488 the standard in this respect in that our internal representation
5489 of an expression may have reference type even when the standard
5490 says it does not. Therefore, we have to manually obtain the
5491 underlying type here. */
5492 scope = non_reference (scope);
5493 /* The type of the POSTFIX_EXPRESSION must be complete. */
5494 if (scope == unknown_type_node)
5496 error_at (location, "%qE does not have class type",
5497 postfix_expression);
5501 scope = complete_type_or_else (scope, NULL_TREE);
5502 /* Let the name lookup machinery know that we are processing a
5503 class member access expression. */
5504 parser->context->object_type = scope;
5505 /* If something went wrong, we want to be able to discern that case,
5506 as opposed to the case where there was no SCOPE due to the type
5507 of expression being dependent. */
5509 scope = error_mark_node;
5510 /* If the SCOPE was erroneous, make the various semantic analysis
5511 functions exit quickly -- and without issuing additional error
5513 if (scope == error_mark_node)
5514 postfix_expression = error_mark_node;
5517 /* Assume this expression is not a pseudo-destructor access. */
5518 pseudo_destructor_p = false;
5520 /* If the SCOPE is a scalar type, then, if this is a valid program,
5521 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5522 is type dependent, it can be pseudo-destructor-name or something else.
5523 Try to parse it as pseudo-destructor-name first. */
5524 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5529 cp_parser_parse_tentatively (parser);
5530 /* Parse the pseudo-destructor-name. */
5532 cp_parser_pseudo_destructor_name (parser, &s, &type);
5534 && (cp_parser_error_occurred (parser)
5535 || TREE_CODE (type) != TYPE_DECL
5536 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5537 cp_parser_abort_tentative_parse (parser);
5538 else if (cp_parser_parse_definitely (parser))
5540 pseudo_destructor_p = true;
5542 = finish_pseudo_destructor_expr (postfix_expression,
5543 s, TREE_TYPE (type));
5547 if (!pseudo_destructor_p)
5549 /* If the SCOPE is not a scalar type, we are looking at an
5550 ordinary class member access expression, rather than a
5551 pseudo-destructor-name. */
5553 cp_token *token = cp_lexer_peek_token (parser->lexer);
5554 /* Parse the id-expression. */
5555 name = (cp_parser_id_expression
5557 cp_parser_optional_template_keyword (parser),
5558 /*check_dependency_p=*/true,
5560 /*declarator_p=*/false,
5561 /*optional_p=*/false));
5562 /* In general, build a SCOPE_REF if the member name is qualified.
5563 However, if the name was not dependent and has already been
5564 resolved; there is no need to build the SCOPE_REF. For example;
5566 struct X { void f(); };
5567 template <typename T> void f(T* t) { t->X::f(); }
5569 Even though "t" is dependent, "X::f" is not and has been resolved
5570 to a BASELINK; there is no need to include scope information. */
5572 /* But we do need to remember that there was an explicit scope for
5573 virtual function calls. */
5575 *idk = CP_ID_KIND_QUALIFIED;
5577 /* If the name is a template-id that names a type, we will get a
5578 TYPE_DECL here. That is invalid code. */
5579 if (TREE_CODE (name) == TYPE_DECL)
5581 error_at (token->location, "invalid use of %qD", name);
5582 postfix_expression = error_mark_node;
5586 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5588 name = build_qualified_name (/*type=*/NULL_TREE,
5592 parser->scope = NULL_TREE;
5593 parser->qualifying_scope = NULL_TREE;
5594 parser->object_scope = NULL_TREE;
5596 if (scope && name && BASELINK_P (name))
5597 adjust_result_of_qualified_name_lookup
5598 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5600 = finish_class_member_access_expr (postfix_expression, name,
5602 tf_warning_or_error);
5606 /* We no longer need to look up names in the scope of the object on
5607 the left-hand side of the `.' or `->' operator. */
5608 parser->context->object_type = NULL_TREE;
5610 /* Outside of offsetof, these operators may not appear in
5611 constant-expressions. */
5613 && (cp_parser_non_integral_constant_expression
5614 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5615 postfix_expression = error_mark_node;
5617 return postfix_expression;
5620 /* Parse a parenthesized expression-list.
5623 assignment-expression
5624 expression-list, assignment-expression
5629 identifier, expression-list
5631 CAST_P is true if this expression is the target of a cast.
5633 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5636 Returns a vector of trees. Each element is a representation of an
5637 assignment-expression. NULL is returned if the ( and or ) are
5638 missing. An empty, but allocated, vector is returned on no
5639 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5640 if we are parsing an attribute list for an attribute that wants a
5641 plain identifier argument, normal_attr for an attribute that wants
5642 an expression, or non_attr if we aren't parsing an attribute list. If
5643 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5644 not all of the expressions in the list were constant. */
5646 static VEC(tree,gc) *
5647 cp_parser_parenthesized_expression_list (cp_parser* parser,
5648 int is_attribute_list,
5650 bool allow_expansion_p,
5651 bool *non_constant_p)
5653 VEC(tree,gc) *expression_list;
5654 bool fold_expr_p = is_attribute_list != non_attr;
5655 tree identifier = NULL_TREE;
5656 bool saved_greater_than_is_operator_p;
5658 /* Assume all the expressions will be constant. */
5660 *non_constant_p = false;
5662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5665 expression_list = make_tree_vector ();
5667 /* Within a parenthesized expression, a `>' token is always
5668 the greater-than operator. */
5669 saved_greater_than_is_operator_p
5670 = parser->greater_than_is_operator_p;
5671 parser->greater_than_is_operator_p = true;
5673 /* Consume expressions until there are no more. */
5674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5679 /* At the beginning of attribute lists, check to see if the
5680 next token is an identifier. */
5681 if (is_attribute_list == id_attr
5682 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5686 /* Consume the identifier. */
5687 token = cp_lexer_consume_token (parser->lexer);
5688 /* Save the identifier. */
5689 identifier = token->u.value;
5693 bool expr_non_constant_p;
5695 /* Parse the next assignment-expression. */
5696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5698 /* A braced-init-list. */
5699 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5700 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5701 if (non_constant_p && expr_non_constant_p)
5702 *non_constant_p = true;
5704 else if (non_constant_p)
5706 expr = (cp_parser_constant_expression
5707 (parser, /*allow_non_constant_p=*/true,
5708 &expr_non_constant_p));
5709 if (expr_non_constant_p)
5710 *non_constant_p = true;
5713 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5716 expr = fold_non_dependent_expr (expr);
5718 /* If we have an ellipsis, then this is an expression
5720 if (allow_expansion_p
5721 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5723 /* Consume the `...'. */
5724 cp_lexer_consume_token (parser->lexer);
5726 /* Build the argument pack. */
5727 expr = make_pack_expansion (expr);
5730 /* Add it to the list. We add error_mark_node
5731 expressions to the list, so that we can still tell if
5732 the correct form for a parenthesized expression-list
5733 is found. That gives better errors. */
5734 VEC_safe_push (tree, gc, expression_list, expr);
5736 if (expr == error_mark_node)
5740 /* After the first item, attribute lists look the same as
5741 expression lists. */
5742 is_attribute_list = non_attr;
5745 /* If the next token isn't a `,', then we are done. */
5746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5749 /* Otherwise, consume the `,' and keep going. */
5750 cp_lexer_consume_token (parser->lexer);
5753 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5758 /* We try and resync to an unnested comma, as that will give the
5759 user better diagnostics. */
5760 ending = cp_parser_skip_to_closing_parenthesis (parser,
5761 /*recovering=*/true,
5763 /*consume_paren=*/true);
5768 parser->greater_than_is_operator_p
5769 = saved_greater_than_is_operator_p;
5774 parser->greater_than_is_operator_p
5775 = saved_greater_than_is_operator_p;
5778 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5780 return expression_list;
5783 /* Parse a pseudo-destructor-name.
5785 pseudo-destructor-name:
5786 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5787 :: [opt] nested-name-specifier template template-id :: ~ type-name
5788 :: [opt] nested-name-specifier [opt] ~ type-name
5790 If either of the first two productions is used, sets *SCOPE to the
5791 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5792 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5793 or ERROR_MARK_NODE if the parse fails. */
5796 cp_parser_pseudo_destructor_name (cp_parser* parser,
5800 bool nested_name_specifier_p;
5802 /* Assume that things will not work out. */
5803 *type = error_mark_node;
5805 /* Look for the optional `::' operator. */
5806 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5807 /* Look for the optional nested-name-specifier. */
5808 nested_name_specifier_p
5809 = (cp_parser_nested_name_specifier_opt (parser,
5810 /*typename_keyword_p=*/false,
5811 /*check_dependency_p=*/true,
5813 /*is_declaration=*/false)
5815 /* Now, if we saw a nested-name-specifier, we might be doing the
5816 second production. */
5817 if (nested_name_specifier_p
5818 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5820 /* Consume the `template' keyword. */
5821 cp_lexer_consume_token (parser->lexer);
5822 /* Parse the template-id. */
5823 cp_parser_template_id (parser,
5824 /*template_keyword_p=*/true,
5825 /*check_dependency_p=*/false,
5826 /*is_declaration=*/true);
5827 /* Look for the `::' token. */
5828 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5830 /* If the next token is not a `~', then there might be some
5831 additional qualification. */
5832 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5834 /* At this point, we're looking for "type-name :: ~". The type-name
5835 must not be a class-name, since this is a pseudo-destructor. So,
5836 it must be either an enum-name, or a typedef-name -- both of which
5837 are just identifiers. So, we peek ahead to check that the "::"
5838 and "~" tokens are present; if they are not, then we can avoid
5839 calling type_name. */
5840 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5841 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5842 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5844 cp_parser_error (parser, "non-scalar type");
5848 /* Look for the type-name. */
5849 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5850 if (*scope == error_mark_node)
5853 /* Look for the `::' token. */
5854 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5859 /* Look for the `~'. */
5860 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5861 /* Look for the type-name again. We are not responsible for
5862 checking that it matches the first type-name. */
5863 *type = cp_parser_nonclass_name (parser);
5866 /* Parse a unary-expression.
5872 unary-operator cast-expression
5873 sizeof unary-expression
5881 __extension__ cast-expression
5882 __alignof__ unary-expression
5883 __alignof__ ( type-id )
5884 __real__ cast-expression
5885 __imag__ cast-expression
5888 ADDRESS_P is true iff the unary-expression is appearing as the
5889 operand of the `&' operator. CAST_P is true if this expression is
5890 the target of a cast.
5892 Returns a representation of the expression. */
5895 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5899 enum tree_code unary_operator;
5901 /* Peek at the next token. */
5902 token = cp_lexer_peek_token (parser->lexer);
5903 /* Some keywords give away the kind of expression. */
5904 if (token->type == CPP_KEYWORD)
5906 enum rid keyword = token->keyword;
5916 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5917 /* Consume the token. */
5918 cp_lexer_consume_token (parser->lexer);
5919 /* Parse the operand. */
5920 operand = cp_parser_sizeof_operand (parser, keyword);
5922 if (TYPE_P (operand))
5923 return cxx_sizeof_or_alignof_type (operand, op, true);
5925 return cxx_sizeof_or_alignof_expr (operand, op, true);
5929 return cp_parser_new_expression (parser);
5932 return cp_parser_delete_expression (parser);
5936 /* The saved value of the PEDANTIC flag. */
5940 /* Save away the PEDANTIC flag. */
5941 cp_parser_extension_opt (parser, &saved_pedantic);
5942 /* Parse the cast-expression. */
5943 expr = cp_parser_simple_cast_expression (parser);
5944 /* Restore the PEDANTIC flag. */
5945 pedantic = saved_pedantic;
5955 /* Consume the `__real__' or `__imag__' token. */
5956 cp_lexer_consume_token (parser->lexer);
5957 /* Parse the cast-expression. */
5958 expression = cp_parser_simple_cast_expression (parser);
5959 /* Create the complete representation. */
5960 return build_x_unary_op ((keyword == RID_REALPART
5961 ? REALPART_EXPR : IMAGPART_EXPR),
5963 tf_warning_or_error);
5970 const char *saved_message;
5971 bool saved_integral_constant_expression_p;
5972 bool saved_non_integral_constant_expression_p;
5973 bool saved_greater_than_is_operator_p;
5975 cp_lexer_consume_token (parser->lexer);
5976 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5978 saved_message = parser->type_definition_forbidden_message;
5979 parser->type_definition_forbidden_message
5980 = G_("types may not be defined in %<noexcept%> expressions");
5982 saved_integral_constant_expression_p
5983 = parser->integral_constant_expression_p;
5984 saved_non_integral_constant_expression_p
5985 = parser->non_integral_constant_expression_p;
5986 parser->integral_constant_expression_p = false;
5988 saved_greater_than_is_operator_p
5989 = parser->greater_than_is_operator_p;
5990 parser->greater_than_is_operator_p = true;
5992 ++cp_unevaluated_operand;
5993 ++c_inhibit_evaluation_warnings;
5994 expr = cp_parser_expression (parser, false, NULL);
5995 --c_inhibit_evaluation_warnings;
5996 --cp_unevaluated_operand;
5998 parser->greater_than_is_operator_p
5999 = saved_greater_than_is_operator_p;
6001 parser->integral_constant_expression_p
6002 = saved_integral_constant_expression_p;
6003 parser->non_integral_constant_expression_p
6004 = saved_non_integral_constant_expression_p;
6006 parser->type_definition_forbidden_message = saved_message;
6008 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6009 return finish_noexcept_expr (expr, tf_warning_or_error);
6017 /* Look for the `:: new' and `:: delete', which also signal the
6018 beginning of a new-expression, or delete-expression,
6019 respectively. If the next token is `::', then it might be one of
6021 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6025 /* See if the token after the `::' is one of the keywords in
6026 which we're interested. */
6027 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6028 /* If it's `new', we have a new-expression. */
6029 if (keyword == RID_NEW)
6030 return cp_parser_new_expression (parser);
6031 /* Similarly, for `delete'. */
6032 else if (keyword == RID_DELETE)
6033 return cp_parser_delete_expression (parser);
6036 /* Look for a unary operator. */
6037 unary_operator = cp_parser_unary_operator (token);
6038 /* The `++' and `--' operators can be handled similarly, even though
6039 they are not technically unary-operators in the grammar. */
6040 if (unary_operator == ERROR_MARK)
6042 if (token->type == CPP_PLUS_PLUS)
6043 unary_operator = PREINCREMENT_EXPR;
6044 else if (token->type == CPP_MINUS_MINUS)
6045 unary_operator = PREDECREMENT_EXPR;
6046 /* Handle the GNU address-of-label extension. */
6047 else if (cp_parser_allow_gnu_extensions_p (parser)
6048 && token->type == CPP_AND_AND)
6052 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6054 /* Consume the '&&' token. */
6055 cp_lexer_consume_token (parser->lexer);
6056 /* Look for the identifier. */
6057 identifier = cp_parser_identifier (parser);
6058 /* Create an expression representing the address. */
6059 expression = finish_label_address_expr (identifier, loc);
6060 if (cp_parser_non_integral_constant_expression (parser,
6062 expression = error_mark_node;
6066 if (unary_operator != ERROR_MARK)
6068 tree cast_expression;
6069 tree expression = error_mark_node;
6070 non_integral_constant non_constant_p = NIC_NONE;
6072 /* Consume the operator token. */
6073 token = cp_lexer_consume_token (parser->lexer);
6074 /* Parse the cast-expression. */
6076 = cp_parser_cast_expression (parser,
6077 unary_operator == ADDR_EXPR,
6078 /*cast_p=*/false, pidk);
6079 /* Now, build an appropriate representation. */
6080 switch (unary_operator)
6083 non_constant_p = NIC_STAR;
6084 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6085 tf_warning_or_error);
6089 non_constant_p = NIC_ADDR;
6092 expression = build_x_unary_op (unary_operator, cast_expression,
6093 tf_warning_or_error);
6096 case PREINCREMENT_EXPR:
6097 case PREDECREMENT_EXPR:
6098 non_constant_p = unary_operator == PREINCREMENT_EXPR
6099 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6101 case UNARY_PLUS_EXPR:
6103 case TRUTH_NOT_EXPR:
6104 expression = finish_unary_op_expr (unary_operator, cast_expression);
6111 if (non_constant_p != NIC_NONE
6112 && cp_parser_non_integral_constant_expression (parser,
6114 expression = error_mark_node;
6119 return cp_parser_postfix_expression (parser, address_p, cast_p,
6120 /*member_access_only_p=*/false,
6124 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6125 unary-operator, the corresponding tree code is returned. */
6127 static enum tree_code
6128 cp_parser_unary_operator (cp_token* token)
6130 switch (token->type)
6133 return INDIRECT_REF;
6139 return UNARY_PLUS_EXPR;
6145 return TRUTH_NOT_EXPR;
6148 return BIT_NOT_EXPR;
6155 /* Parse a new-expression.
6158 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6159 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6161 Returns a representation of the expression. */
6164 cp_parser_new_expression (cp_parser* parser)
6166 bool global_scope_p;
6167 VEC(tree,gc) *placement;
6169 VEC(tree,gc) *initializer;
6173 /* Look for the optional `::' operator. */
6175 = (cp_parser_global_scope_opt (parser,
6176 /*current_scope_valid_p=*/false)
6178 /* Look for the `new' operator. */
6179 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6180 /* There's no easy way to tell a new-placement from the
6181 `( type-id )' construct. */
6182 cp_parser_parse_tentatively (parser);
6183 /* Look for a new-placement. */
6184 placement = cp_parser_new_placement (parser);
6185 /* If that didn't work out, there's no new-placement. */
6186 if (!cp_parser_parse_definitely (parser))
6188 if (placement != NULL)
6189 release_tree_vector (placement);
6193 /* If the next token is a `(', then we have a parenthesized
6195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6198 /* Consume the `('. */
6199 cp_lexer_consume_token (parser->lexer);
6200 /* Parse the type-id. */
6201 type = cp_parser_type_id (parser);
6202 /* Look for the closing `)'. */
6203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6204 token = cp_lexer_peek_token (parser->lexer);
6205 /* There should not be a direct-new-declarator in this production,
6206 but GCC used to allowed this, so we check and emit a sensible error
6207 message for this case. */
6208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6210 error_at (token->location,
6211 "array bound forbidden after parenthesized type-id");
6212 inform (token->location,
6213 "try removing the parentheses around the type-id");
6214 cp_parser_direct_new_declarator (parser);
6218 /* Otherwise, there must be a new-type-id. */
6220 type = cp_parser_new_type_id (parser, &nelts);
6222 /* If the next token is a `(' or '{', then we have a new-initializer. */
6223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6224 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6225 initializer = cp_parser_new_initializer (parser);
6229 /* A new-expression may not appear in an integral constant
6231 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6232 ret = error_mark_node;
6235 /* Create a representation of the new-expression. */
6236 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6237 tf_warning_or_error);
6240 if (placement != NULL)
6241 release_tree_vector (placement);
6242 if (initializer != NULL)
6243 release_tree_vector (initializer);
6248 /* Parse a new-placement.
6253 Returns the same representation as for an expression-list. */
6255 static VEC(tree,gc) *
6256 cp_parser_new_placement (cp_parser* parser)
6258 VEC(tree,gc) *expression_list;
6260 /* Parse the expression-list. */
6261 expression_list = (cp_parser_parenthesized_expression_list
6262 (parser, non_attr, /*cast_p=*/false,
6263 /*allow_expansion_p=*/true,
6264 /*non_constant_p=*/NULL));
6266 return expression_list;
6269 /* Parse a new-type-id.
6272 type-specifier-seq new-declarator [opt]
6274 Returns the TYPE allocated. If the new-type-id indicates an array
6275 type, *NELTS is set to the number of elements in the last array
6276 bound; the TYPE will not include the last array bound. */
6279 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6281 cp_decl_specifier_seq type_specifier_seq;
6282 cp_declarator *new_declarator;
6283 cp_declarator *declarator;
6284 cp_declarator *outer_declarator;
6285 const char *saved_message;
6288 /* The type-specifier sequence must not contain type definitions.
6289 (It cannot contain declarations of new types either, but if they
6290 are not definitions we will catch that because they are not
6292 saved_message = parser->type_definition_forbidden_message;
6293 parser->type_definition_forbidden_message
6294 = G_("types may not be defined in a new-type-id");
6295 /* Parse the type-specifier-seq. */
6296 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6297 /*is_trailing_return=*/false,
6298 &type_specifier_seq);
6299 /* Restore the old message. */
6300 parser->type_definition_forbidden_message = saved_message;
6301 /* Parse the new-declarator. */
6302 new_declarator = cp_parser_new_declarator_opt (parser);
6304 /* Determine the number of elements in the last array dimension, if
6307 /* Skip down to the last array dimension. */
6308 declarator = new_declarator;
6309 outer_declarator = NULL;
6310 while (declarator && (declarator->kind == cdk_pointer
6311 || declarator->kind == cdk_ptrmem))
6313 outer_declarator = declarator;
6314 declarator = declarator->declarator;
6317 && declarator->kind == cdk_array
6318 && declarator->declarator
6319 && declarator->declarator->kind == cdk_array)
6321 outer_declarator = declarator;
6322 declarator = declarator->declarator;
6325 if (declarator && declarator->kind == cdk_array)
6327 *nelts = declarator->u.array.bounds;
6328 if (*nelts == error_mark_node)
6329 *nelts = integer_one_node;
6331 if (outer_declarator)
6332 outer_declarator->declarator = declarator->declarator;
6334 new_declarator = NULL;
6337 type = groktypename (&type_specifier_seq, new_declarator, false);
6341 /* Parse an (optional) new-declarator.
6344 ptr-operator new-declarator [opt]
6345 direct-new-declarator
6347 Returns the declarator. */
6349 static cp_declarator *
6350 cp_parser_new_declarator_opt (cp_parser* parser)
6352 enum tree_code code;
6354 cp_cv_quals cv_quals;
6356 /* We don't know if there's a ptr-operator next, or not. */
6357 cp_parser_parse_tentatively (parser);
6358 /* Look for a ptr-operator. */
6359 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6360 /* If that worked, look for more new-declarators. */
6361 if (cp_parser_parse_definitely (parser))
6363 cp_declarator *declarator;
6365 /* Parse another optional declarator. */
6366 declarator = cp_parser_new_declarator_opt (parser);
6368 return cp_parser_make_indirect_declarator
6369 (code, type, cv_quals, declarator);
6372 /* If the next token is a `[', there is a direct-new-declarator. */
6373 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6374 return cp_parser_direct_new_declarator (parser);
6379 /* Parse a direct-new-declarator.
6381 direct-new-declarator:
6383 direct-new-declarator [constant-expression]
6387 static cp_declarator *
6388 cp_parser_direct_new_declarator (cp_parser* parser)
6390 cp_declarator *declarator = NULL;
6396 /* Look for the opening `['. */
6397 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6398 /* The first expression is not required to be constant. */
6401 cp_token *token = cp_lexer_peek_token (parser->lexer);
6402 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6403 /* The standard requires that the expression have integral
6404 type. DR 74 adds enumeration types. We believe that the
6405 real intent is that these expressions be handled like the
6406 expression in a `switch' condition, which also allows
6407 classes with a single conversion to integral or
6408 enumeration type. */
6409 if (!processing_template_decl)
6412 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6417 error_at (token->location,
6418 "expression in new-declarator must have integral "
6419 "or enumeration type");
6420 expression = error_mark_node;
6424 /* But all the other expressions must be. */
6427 = cp_parser_constant_expression (parser,
6428 /*allow_non_constant=*/false,
6430 /* Look for the closing `]'. */
6431 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6433 /* Add this bound to the declarator. */
6434 declarator = make_array_declarator (declarator, expression);
6436 /* If the next token is not a `[', then there are no more
6438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6445 /* Parse a new-initializer.
6448 ( expression-list [opt] )
6451 Returns a representation of the expression-list. */
6453 static VEC(tree,gc) *
6454 cp_parser_new_initializer (cp_parser* parser)
6456 VEC(tree,gc) *expression_list;
6458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6461 bool expr_non_constant_p;
6462 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6463 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6464 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6465 expression_list = make_tree_vector_single (t);
6468 expression_list = (cp_parser_parenthesized_expression_list
6469 (parser, non_attr, /*cast_p=*/false,
6470 /*allow_expansion_p=*/true,
6471 /*non_constant_p=*/NULL));
6473 return expression_list;
6476 /* Parse a delete-expression.
6479 :: [opt] delete cast-expression
6480 :: [opt] delete [ ] cast-expression
6482 Returns a representation of the expression. */
6485 cp_parser_delete_expression (cp_parser* parser)
6487 bool global_scope_p;
6491 /* Look for the optional `::' operator. */
6493 = (cp_parser_global_scope_opt (parser,
6494 /*current_scope_valid_p=*/false)
6496 /* Look for the `delete' keyword. */
6497 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6498 /* See if the array syntax is in use. */
6499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6501 /* Consume the `[' token. */
6502 cp_lexer_consume_token (parser->lexer);
6503 /* Look for the `]' token. */
6504 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6505 /* Remember that this is the `[]' construct. */
6511 /* Parse the cast-expression. */
6512 expression = cp_parser_simple_cast_expression (parser);
6514 /* A delete-expression may not appear in an integral constant
6516 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6517 return error_mark_node;
6519 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6522 /* Returns true if TOKEN may start a cast-expression and false
6526 cp_parser_token_starts_cast_expression (cp_token *token)
6528 switch (token->type)
6534 case CPP_CLOSE_SQUARE:
6535 case CPP_CLOSE_PAREN:
6536 case CPP_CLOSE_BRACE:
6540 case CPP_DEREF_STAR:
6548 case CPP_GREATER_EQ:
6568 /* '[' may start a primary-expression in obj-c++. */
6569 case CPP_OPEN_SQUARE:
6570 return c_dialect_objc ();
6577 /* Parse a cast-expression.
6581 ( type-id ) cast-expression
6583 ADDRESS_P is true iff the unary-expression is appearing as the
6584 operand of the `&' operator. CAST_P is true if this expression is
6585 the target of a cast.
6587 Returns a representation of the expression. */
6590 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6593 /* If it's a `(', then we might be looking at a cast. */
6594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6596 tree type = NULL_TREE;
6597 tree expr = NULL_TREE;
6598 bool compound_literal_p;
6599 const char *saved_message;
6601 /* There's no way to know yet whether or not this is a cast.
6602 For example, `(int (3))' is a unary-expression, while `(int)
6603 3' is a cast. So, we resort to parsing tentatively. */
6604 cp_parser_parse_tentatively (parser);
6605 /* Types may not be defined in a cast. */
6606 saved_message = parser->type_definition_forbidden_message;
6607 parser->type_definition_forbidden_message
6608 = G_("types may not be defined in casts");
6609 /* Consume the `('. */
6610 cp_lexer_consume_token (parser->lexer);
6611 /* A very tricky bit is that `(struct S) { 3 }' is a
6612 compound-literal (which we permit in C++ as an extension).
6613 But, that construct is not a cast-expression -- it is a
6614 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6615 is legal; if the compound-literal were a cast-expression,
6616 you'd need an extra set of parentheses.) But, if we parse
6617 the type-id, and it happens to be a class-specifier, then we
6618 will commit to the parse at that point, because we cannot
6619 undo the action that is done when creating a new class. So,
6620 then we cannot back up and do a postfix-expression.
6622 Therefore, we scan ahead to the closing `)', and check to see
6623 if the token after the `)' is a `{'. If so, we are not
6624 looking at a cast-expression.
6626 Save tokens so that we can put them back. */
6627 cp_lexer_save_tokens (parser->lexer);
6628 /* Skip tokens until the next token is a closing parenthesis.
6629 If we find the closing `)', and the next token is a `{', then
6630 we are looking at a compound-literal. */
6632 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6633 /*consume_paren=*/true)
6634 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6635 /* Roll back the tokens we skipped. */
6636 cp_lexer_rollback_tokens (parser->lexer);
6637 /* If we were looking at a compound-literal, simulate an error
6638 so that the call to cp_parser_parse_definitely below will
6640 if (compound_literal_p)
6641 cp_parser_simulate_error (parser);
6644 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6645 parser->in_type_id_in_expr_p = true;
6646 /* Look for the type-id. */
6647 type = cp_parser_type_id (parser);
6648 /* Look for the closing `)'. */
6649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6650 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6653 /* Restore the saved message. */
6654 parser->type_definition_forbidden_message = saved_message;
6656 /* At this point this can only be either a cast or a
6657 parenthesized ctor such as `(T ())' that looks like a cast to
6658 function returning T. */
6659 if (!cp_parser_error_occurred (parser)
6660 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6663 cp_parser_parse_definitely (parser);
6664 expr = cp_parser_cast_expression (parser,
6665 /*address_p=*/false,
6666 /*cast_p=*/true, pidk);
6668 /* Warn about old-style casts, if so requested. */
6669 if (warn_old_style_cast
6670 && !in_system_header
6671 && !VOID_TYPE_P (type)
6672 && current_lang_name != lang_name_c)
6673 warning (OPT_Wold_style_cast, "use of old-style cast");
6675 /* Only type conversions to integral or enumeration types
6676 can be used in constant-expressions. */
6677 if (!cast_valid_in_integral_constant_expression_p (type)
6678 && cp_parser_non_integral_constant_expression (parser,
6680 return error_mark_node;
6682 /* Perform the cast. */
6683 expr = build_c_cast (input_location, type, expr);
6687 cp_parser_abort_tentative_parse (parser);
6690 /* If we get here, then it's not a cast, so it must be a
6691 unary-expression. */
6692 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6695 /* Parse a binary expression of the general form:
6699 pm-expression .* cast-expression
6700 pm-expression ->* cast-expression
6702 multiplicative-expression:
6704 multiplicative-expression * pm-expression
6705 multiplicative-expression / pm-expression
6706 multiplicative-expression % pm-expression
6708 additive-expression:
6709 multiplicative-expression
6710 additive-expression + multiplicative-expression
6711 additive-expression - multiplicative-expression
6715 shift-expression << additive-expression
6716 shift-expression >> additive-expression
6718 relational-expression:
6720 relational-expression < shift-expression
6721 relational-expression > shift-expression
6722 relational-expression <= shift-expression
6723 relational-expression >= shift-expression
6727 relational-expression:
6728 relational-expression <? shift-expression
6729 relational-expression >? shift-expression
6731 equality-expression:
6732 relational-expression
6733 equality-expression == relational-expression
6734 equality-expression != relational-expression
6738 and-expression & equality-expression
6740 exclusive-or-expression:
6742 exclusive-or-expression ^ and-expression
6744 inclusive-or-expression:
6745 exclusive-or-expression
6746 inclusive-or-expression | exclusive-or-expression
6748 logical-and-expression:
6749 inclusive-or-expression
6750 logical-and-expression && inclusive-or-expression
6752 logical-or-expression:
6753 logical-and-expression
6754 logical-or-expression || logical-and-expression
6756 All these are implemented with a single function like:
6759 simple-cast-expression
6760 binary-expression <token> binary-expression
6762 CAST_P is true if this expression is the target of a cast.
6764 The binops_by_token map is used to get the tree codes for each <token> type.
6765 binary-expressions are associated according to a precedence table. */
6767 #define TOKEN_PRECEDENCE(token) \
6768 (((token->type == CPP_GREATER \
6769 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6770 && !parser->greater_than_is_operator_p) \
6771 ? PREC_NOT_OPERATOR \
6772 : binops_by_token[token->type].prec)
6775 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6776 bool no_toplevel_fold_p,
6777 enum cp_parser_prec prec,
6780 cp_parser_expression_stack stack;
6781 cp_parser_expression_stack_entry *sp = &stack[0];
6784 enum tree_code tree_type, lhs_type, rhs_type;
6785 enum cp_parser_prec new_prec, lookahead_prec;
6788 /* Parse the first expression. */
6789 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6790 lhs_type = ERROR_MARK;
6794 /* Get an operator token. */
6795 token = cp_lexer_peek_token (parser->lexer);
6797 if (warn_cxx0x_compat
6798 && token->type == CPP_RSHIFT
6799 && !parser->greater_than_is_operator_p)
6801 if (warning_at (token->location, OPT_Wc__0x_compat,
6802 "%<>>%> operator will be treated as"
6803 " two right angle brackets in C++0x"))
6804 inform (token->location,
6805 "suggest parentheses around %<>>%> expression");
6808 new_prec = TOKEN_PRECEDENCE (token);
6810 /* Popping an entry off the stack means we completed a subexpression:
6811 - either we found a token which is not an operator (`>' where it is not
6812 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6813 will happen repeatedly;
6814 - or, we found an operator which has lower priority. This is the case
6815 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6817 if (new_prec <= prec)
6826 tree_type = binops_by_token[token->type].tree_type;
6828 /* We used the operator token. */
6829 cp_lexer_consume_token (parser->lexer);
6831 /* For "false && x" or "true || x", x will never be executed;
6832 disable warnings while evaluating it. */
6833 if (tree_type == TRUTH_ANDIF_EXPR)
6834 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6835 else if (tree_type == TRUTH_ORIF_EXPR)
6836 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6838 /* Extract another operand. It may be the RHS of this expression
6839 or the LHS of a new, higher priority expression. */
6840 rhs = cp_parser_simple_cast_expression (parser);
6841 rhs_type = ERROR_MARK;
6843 /* Get another operator token. Look up its precedence to avoid
6844 building a useless (immediately popped) stack entry for common
6845 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6846 token = cp_lexer_peek_token (parser->lexer);
6847 lookahead_prec = TOKEN_PRECEDENCE (token);
6848 if (lookahead_prec > new_prec)
6850 /* ... and prepare to parse the RHS of the new, higher priority
6851 expression. Since precedence levels on the stack are
6852 monotonically increasing, we do not have to care about
6855 sp->tree_type = tree_type;
6857 sp->lhs_type = lhs_type;
6860 lhs_type = rhs_type;
6862 new_prec = lookahead_prec;
6866 lookahead_prec = new_prec;
6867 /* If the stack is not empty, we have parsed into LHS the right side
6868 (`4' in the example above) of an expression we had suspended.
6869 We can use the information on the stack to recover the LHS (`3')
6870 from the stack together with the tree code (`MULT_EXPR'), and
6871 the precedence of the higher level subexpression
6872 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6873 which will be used to actually build the additive expression. */
6876 tree_type = sp->tree_type;
6878 rhs_type = lhs_type;
6880 lhs_type = sp->lhs_type;
6883 /* Undo the disabling of warnings done above. */
6884 if (tree_type == TRUTH_ANDIF_EXPR)
6885 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6886 else if (tree_type == TRUTH_ORIF_EXPR)
6887 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6889 overloaded_p = false;
6890 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6891 ERROR_MARK for everything that is not a binary expression.
6892 This makes warn_about_parentheses miss some warnings that
6893 involve unary operators. For unary expressions we should
6894 pass the correct tree_code unless the unary expression was
6895 surrounded by parentheses.
6897 if (no_toplevel_fold_p
6898 && lookahead_prec <= prec
6900 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6901 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6903 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6904 &overloaded_p, tf_warning_or_error);
6905 lhs_type = tree_type;
6907 /* If the binary operator required the use of an overloaded operator,
6908 then this expression cannot be an integral constant-expression.
6909 An overloaded operator can be used even if both operands are
6910 otherwise permissible in an integral constant-expression if at
6911 least one of the operands is of enumeration type. */
6914 && cp_parser_non_integral_constant_expression (parser,
6916 return error_mark_node;
6923 /* Parse the `? expression : assignment-expression' part of a
6924 conditional-expression. The LOGICAL_OR_EXPR is the
6925 logical-or-expression that started the conditional-expression.
6926 Returns a representation of the entire conditional-expression.
6928 This routine is used by cp_parser_assignment_expression.
6930 ? expression : assignment-expression
6934 ? : assignment-expression */
6937 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6940 tree assignment_expr;
6941 struct cp_token *token;
6943 /* Consume the `?' token. */
6944 cp_lexer_consume_token (parser->lexer);
6945 token = cp_lexer_peek_token (parser->lexer);
6946 if (cp_parser_allow_gnu_extensions_p (parser)
6947 && token->type == CPP_COLON)
6949 pedwarn (token->location, OPT_pedantic,
6950 "ISO C++ does not allow ?: with omitted middle operand");
6951 /* Implicit true clause. */
6953 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6954 warn_for_omitted_condop (token->location, logical_or_expr);
6958 /* Parse the expression. */
6959 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6960 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6961 c_inhibit_evaluation_warnings +=
6962 ((logical_or_expr == truthvalue_true_node)
6963 - (logical_or_expr == truthvalue_false_node));
6966 /* The next token should be a `:'. */
6967 cp_parser_require (parser, CPP_COLON, RT_COLON);
6968 /* Parse the assignment-expression. */
6969 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6970 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6972 /* Build the conditional-expression. */
6973 return build_x_conditional_expr (logical_or_expr,
6976 tf_warning_or_error);
6979 /* Parse an assignment-expression.
6981 assignment-expression:
6982 conditional-expression
6983 logical-or-expression assignment-operator assignment_expression
6986 CAST_P is true if this expression is the target of a cast.
6988 Returns a representation for the expression. */
6991 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6996 /* If the next token is the `throw' keyword, then we're looking at
6997 a throw-expression. */
6998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6999 expr = cp_parser_throw_expression (parser);
7000 /* Otherwise, it must be that we are looking at a
7001 logical-or-expression. */
7004 /* Parse the binary expressions (logical-or-expression). */
7005 expr = cp_parser_binary_expression (parser, cast_p, false,
7006 PREC_NOT_OPERATOR, pidk);
7007 /* If the next token is a `?' then we're actually looking at a
7008 conditional-expression. */
7009 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7010 return cp_parser_question_colon_clause (parser, expr);
7013 enum tree_code assignment_operator;
7015 /* If it's an assignment-operator, we're using the second
7018 = cp_parser_assignment_operator_opt (parser);
7019 if (assignment_operator != ERROR_MARK)
7021 bool non_constant_p;
7023 /* Parse the right-hand side of the assignment. */
7024 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7026 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7027 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7029 /* An assignment may not appear in a
7030 constant-expression. */
7031 if (cp_parser_non_integral_constant_expression (parser,
7033 return error_mark_node;
7034 /* Build the assignment expression. */
7035 expr = build_x_modify_expr (expr,
7036 assignment_operator,
7038 tf_warning_or_error);
7046 /* Parse an (optional) assignment-operator.
7048 assignment-operator: one of
7049 = *= /= %= += -= >>= <<= &= ^= |=
7053 assignment-operator: one of
7056 If the next token is an assignment operator, the corresponding tree
7057 code is returned, and the token is consumed. For example, for
7058 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7059 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7060 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7061 operator, ERROR_MARK is returned. */
7063 static enum tree_code
7064 cp_parser_assignment_operator_opt (cp_parser* parser)
7069 /* Peek at the next token. */
7070 token = cp_lexer_peek_token (parser->lexer);
7072 switch (token->type)
7083 op = TRUNC_DIV_EXPR;
7087 op = TRUNC_MOD_EXPR;
7119 /* Nothing else is an assignment operator. */
7123 /* If it was an assignment operator, consume it. */
7124 if (op != ERROR_MARK)
7125 cp_lexer_consume_token (parser->lexer);
7130 /* Parse an expression.
7133 assignment-expression
7134 expression , assignment-expression
7136 CAST_P is true if this expression is the target of a cast.
7138 Returns a representation of the expression. */
7141 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7143 tree expression = NULL_TREE;
7147 tree assignment_expression;
7149 /* Parse the next assignment-expression. */
7150 assignment_expression
7151 = cp_parser_assignment_expression (parser, cast_p, pidk);
7152 /* If this is the first assignment-expression, we can just
7155 expression = assignment_expression;
7157 expression = build_x_compound_expr (expression,
7158 assignment_expression,
7159 tf_warning_or_error);
7160 /* If the next token is not a comma, then we are done with the
7162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7164 /* Consume the `,'. */
7165 cp_lexer_consume_token (parser->lexer);
7166 /* A comma operator cannot appear in a constant-expression. */
7167 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7168 expression = error_mark_node;
7174 /* Parse a constant-expression.
7176 constant-expression:
7177 conditional-expression
7179 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7180 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7181 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7182 is false, NON_CONSTANT_P should be NULL. */
7185 cp_parser_constant_expression (cp_parser* parser,
7186 bool allow_non_constant_p,
7187 bool *non_constant_p)
7189 bool saved_integral_constant_expression_p;
7190 bool saved_allow_non_integral_constant_expression_p;
7191 bool saved_non_integral_constant_expression_p;
7194 /* It might seem that we could simply parse the
7195 conditional-expression, and then check to see if it were
7196 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7197 one that the compiler can figure out is constant, possibly after
7198 doing some simplifications or optimizations. The standard has a
7199 precise definition of constant-expression, and we must honor
7200 that, even though it is somewhat more restrictive.
7206 is not a legal declaration, because `(2, 3)' is not a
7207 constant-expression. The `,' operator is forbidden in a
7208 constant-expression. However, GCC's constant-folding machinery
7209 will fold this operation to an INTEGER_CST for `3'. */
7211 /* Save the old settings. */
7212 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7213 saved_allow_non_integral_constant_expression_p
7214 = parser->allow_non_integral_constant_expression_p;
7215 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7216 /* We are now parsing a constant-expression. */
7217 parser->integral_constant_expression_p = true;
7218 parser->allow_non_integral_constant_expression_p
7219 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7220 parser->non_integral_constant_expression_p = false;
7221 /* Although the grammar says "conditional-expression", we parse an
7222 "assignment-expression", which also permits "throw-expression"
7223 and the use of assignment operators. In the case that
7224 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7225 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7226 actually essential that we look for an assignment-expression.
7227 For example, cp_parser_initializer_clauses uses this function to
7228 determine whether a particular assignment-expression is in fact
7230 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7231 /* Restore the old settings. */
7232 parser->integral_constant_expression_p
7233 = saved_integral_constant_expression_p;
7234 parser->allow_non_integral_constant_expression_p
7235 = saved_allow_non_integral_constant_expression_p;
7236 if (allow_non_constant_p)
7237 *non_constant_p = parser->non_integral_constant_expression_p;
7238 else if (parser->non_integral_constant_expression_p
7239 && cxx_dialect < cxx0x)
7240 expression = error_mark_node;
7241 parser->non_integral_constant_expression_p
7242 = saved_non_integral_constant_expression_p;
7247 /* Parse __builtin_offsetof.
7249 offsetof-expression:
7250 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7252 offsetof-member-designator:
7254 | offsetof-member-designator "." id-expression
7255 | offsetof-member-designator "[" expression "]"
7256 | offsetof-member-designator "->" id-expression */
7259 cp_parser_builtin_offsetof (cp_parser *parser)
7261 int save_ice_p, save_non_ice_p;
7266 /* We're about to accept non-integral-constant things, but will
7267 definitely yield an integral constant expression. Save and
7268 restore these values around our local parsing. */
7269 save_ice_p = parser->integral_constant_expression_p;
7270 save_non_ice_p = parser->non_integral_constant_expression_p;
7272 /* Consume the "__builtin_offsetof" token. */
7273 cp_lexer_consume_token (parser->lexer);
7274 /* Consume the opening `('. */
7275 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7276 /* Parse the type-id. */
7277 type = cp_parser_type_id (parser);
7278 /* Look for the `,'. */
7279 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7280 token = cp_lexer_peek_token (parser->lexer);
7282 /* Build the (type *)null that begins the traditional offsetof macro. */
7283 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7284 tf_warning_or_error);
7286 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7287 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7288 true, &dummy, token->location);
7291 token = cp_lexer_peek_token (parser->lexer);
7292 switch (token->type)
7294 case CPP_OPEN_SQUARE:
7295 /* offsetof-member-designator "[" expression "]" */
7296 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7300 /* offsetof-member-designator "->" identifier */
7301 expr = grok_array_decl (expr, integer_zero_node);
7305 /* offsetof-member-designator "." identifier */
7306 cp_lexer_consume_token (parser->lexer);
7307 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7312 case CPP_CLOSE_PAREN:
7313 /* Consume the ")" token. */
7314 cp_lexer_consume_token (parser->lexer);
7318 /* Error. We know the following require will fail, but
7319 that gives the proper error message. */
7320 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7321 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7322 expr = error_mark_node;
7328 /* If we're processing a template, we can't finish the semantics yet.
7329 Otherwise we can fold the entire expression now. */
7330 if (processing_template_decl)
7331 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7333 expr = finish_offsetof (expr);
7336 parser->integral_constant_expression_p = save_ice_p;
7337 parser->non_integral_constant_expression_p = save_non_ice_p;
7342 /* Parse a trait expression. */
7345 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7348 tree type1, type2 = NULL_TREE;
7349 bool binary = false;
7350 cp_decl_specifier_seq decl_specs;
7354 case RID_HAS_NOTHROW_ASSIGN:
7355 kind = CPTK_HAS_NOTHROW_ASSIGN;
7357 case RID_HAS_NOTHROW_CONSTRUCTOR:
7358 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7360 case RID_HAS_NOTHROW_COPY:
7361 kind = CPTK_HAS_NOTHROW_COPY;
7363 case RID_HAS_TRIVIAL_ASSIGN:
7364 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7366 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7367 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7369 case RID_HAS_TRIVIAL_COPY:
7370 kind = CPTK_HAS_TRIVIAL_COPY;
7372 case RID_HAS_TRIVIAL_DESTRUCTOR:
7373 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7375 case RID_HAS_VIRTUAL_DESTRUCTOR:
7376 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7378 case RID_IS_ABSTRACT:
7379 kind = CPTK_IS_ABSTRACT;
7381 case RID_IS_BASE_OF:
7382 kind = CPTK_IS_BASE_OF;
7386 kind = CPTK_IS_CLASS;
7388 case RID_IS_CONVERTIBLE_TO:
7389 kind = CPTK_IS_CONVERTIBLE_TO;
7393 kind = CPTK_IS_EMPTY;
7396 kind = CPTK_IS_ENUM;
7401 case RID_IS_POLYMORPHIC:
7402 kind = CPTK_IS_POLYMORPHIC;
7404 case RID_IS_STD_LAYOUT:
7405 kind = CPTK_IS_STD_LAYOUT;
7407 case RID_IS_TRIVIAL:
7408 kind = CPTK_IS_TRIVIAL;
7411 kind = CPTK_IS_UNION;
7413 case RID_IS_LITERAL_TYPE:
7414 kind = CPTK_IS_LITERAL_TYPE;
7420 /* Consume the token. */
7421 cp_lexer_consume_token (parser->lexer);
7423 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7425 type1 = cp_parser_type_id (parser);
7427 if (type1 == error_mark_node)
7428 return error_mark_node;
7430 /* Build a trivial decl-specifier-seq. */
7431 clear_decl_specs (&decl_specs);
7432 decl_specs.type = type1;
7434 /* Call grokdeclarator to figure out what type this is. */
7435 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7436 /*initialized=*/0, /*attrlist=*/NULL);
7440 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7442 type2 = cp_parser_type_id (parser);
7444 if (type2 == error_mark_node)
7445 return error_mark_node;
7447 /* Build a trivial decl-specifier-seq. */
7448 clear_decl_specs (&decl_specs);
7449 decl_specs.type = type2;
7451 /* Call grokdeclarator to figure out what type this is. */
7452 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7453 /*initialized=*/0, /*attrlist=*/NULL);
7456 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7458 /* Complete the trait expression, which may mean either processing
7459 the trait expr now or saving it for template instantiation. */
7460 return finish_trait_expr (kind, type1, type2);
7463 /* Lambdas that appear in variable initializer or default argument scope
7464 get that in their mangling, so we need to record it. We might as well
7465 use the count for function and namespace scopes as well. */
7466 static GTY(()) tree lambda_scope;
7467 static GTY(()) int lambda_count;
7468 typedef struct GTY(()) tree_int
7473 DEF_VEC_O(tree_int);
7474 DEF_VEC_ALLOC_O(tree_int,gc);
7475 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7478 start_lambda_scope (tree decl)
7482 /* Once we're inside a function, we ignore other scopes and just push
7483 the function again so that popping works properly. */
7484 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7485 decl = current_function_decl;
7486 ti.t = lambda_scope;
7487 ti.i = lambda_count;
7488 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7489 if (lambda_scope != decl)
7491 /* Don't reset the count if we're still in the same function. */
7492 lambda_scope = decl;
7498 record_lambda_scope (tree lambda)
7500 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7501 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7505 finish_lambda_scope (void)
7507 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7508 if (lambda_scope != p->t)
7510 lambda_scope = p->t;
7511 lambda_count = p->i;
7513 VEC_pop (tree_int, lambda_scope_stack);
7516 /* Parse a lambda expression.
7519 lambda-introducer lambda-declarator [opt] compound-statement
7521 Returns a representation of the expression. */
7524 cp_parser_lambda_expression (cp_parser* parser)
7526 tree lambda_expr = build_lambda_expr ();
7529 LAMBDA_EXPR_LOCATION (lambda_expr)
7530 = cp_lexer_peek_token (parser->lexer)->location;
7532 if (cp_unevaluated_operand)
7533 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7534 "lambda-expression in unevaluated context");
7536 /* We may be in the middle of deferred access check. Disable
7538 push_deferring_access_checks (dk_no_deferred);
7540 cp_parser_lambda_introducer (parser, lambda_expr);
7542 type = begin_lambda_type (lambda_expr);
7544 record_lambda_scope (lambda_expr);
7546 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7547 determine_visibility (TYPE_NAME (type));
7549 /* Now that we've started the type, add the capture fields for any
7550 explicit captures. */
7551 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7554 /* Inside the class, surrounding template-parameter-lists do not apply. */
7555 unsigned int saved_num_template_parameter_lists
7556 = parser->num_template_parameter_lists;
7558 parser->num_template_parameter_lists = 0;
7560 /* By virtue of defining a local class, a lambda expression has access to
7561 the private variables of enclosing classes. */
7563 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7565 cp_parser_lambda_body (parser, lambda_expr);
7567 /* The capture list was built up in reverse order; fix that now. */
7569 tree newlist = NULL_TREE;
7572 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7575 tree field = TREE_PURPOSE (elt);
7578 next = TREE_CHAIN (elt);
7579 TREE_CHAIN (elt) = newlist;
7582 /* Also add __ to the beginning of the field name so that code
7583 outside the lambda body can't see the captured name. We could
7584 just remove the name entirely, but this is more useful for
7586 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7587 /* The 'this' capture already starts with __. */
7590 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7591 buf[1] = buf[0] = '_';
7592 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7593 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7594 DECL_NAME (field) = get_identifier (buf);
7596 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7599 maybe_add_lambda_conv_op (type);
7601 type = finish_struct (type, /*attributes=*/NULL_TREE);
7603 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7606 pop_deferring_access_checks ();
7608 return build_lambda_object (lambda_expr);
7611 /* Parse the beginning of a lambda expression.
7614 [ lambda-capture [opt] ]
7616 LAMBDA_EXPR is the current representation of the lambda expression. */
7619 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7621 /* Need commas after the first capture. */
7624 /* Eat the leading `['. */
7625 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7627 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7628 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7629 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7630 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7631 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7632 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7634 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7636 cp_lexer_consume_token (parser->lexer);
7640 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7642 cp_token* capture_token;
7644 tree capture_init_expr;
7645 cp_id_kind idk = CP_ID_KIND_NONE;
7646 bool explicit_init_p = false;
7648 enum capture_kind_type
7653 enum capture_kind_type capture_kind = BY_COPY;
7655 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7657 error ("expected end of capture-list");
7664 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7666 /* Possibly capture `this'. */
7667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7669 cp_lexer_consume_token (parser->lexer);
7670 add_capture (lambda_expr,
7671 /*id=*/get_identifier ("__this"),
7672 /*initializer=*/finish_this_expr(),
7673 /*by_reference_p=*/false,
7678 /* Remember whether we want to capture as a reference or not. */
7679 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7681 capture_kind = BY_REFERENCE;
7682 cp_lexer_consume_token (parser->lexer);
7685 /* Get the identifier. */
7686 capture_token = cp_lexer_peek_token (parser->lexer);
7687 capture_id = cp_parser_identifier (parser);
7689 if (capture_id == error_mark_node)
7690 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7691 delimiters, but I modified this to stop on unnested ']' as well. It
7692 was already changed to stop on unnested '}', so the
7693 "closing_parenthesis" name is no more misleading with my change. */
7695 cp_parser_skip_to_closing_parenthesis (parser,
7696 /*recovering=*/true,
7698 /*consume_paren=*/true);
7702 /* Find the initializer for this capture. */
7703 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7705 /* An explicit expression exists. */
7706 cp_lexer_consume_token (parser->lexer);
7707 pedwarn (input_location, OPT_pedantic,
7708 "ISO C++ does not allow initializers "
7709 "in lambda expression capture lists");
7710 capture_init_expr = cp_parser_assignment_expression (parser,
7713 explicit_init_p = true;
7717 const char* error_msg;
7719 /* Turn the identifier into an id-expression. */
7721 = cp_parser_lookup_name
7725 /*is_template=*/false,
7726 /*is_namespace=*/false,
7727 /*check_dependency=*/true,
7728 /*ambiguous_decls=*/NULL,
7729 capture_token->location);
7732 = finish_id_expression
7737 /*integral_constant_expression_p=*/false,
7738 /*allow_non_integral_constant_expression_p=*/false,
7739 /*non_integral_constant_expression_p=*/NULL,
7740 /*template_p=*/false,
7742 /*address_p=*/false,
7743 /*template_arg_p=*/false,
7745 capture_token->location);
7748 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7750 = unqualified_name_lookup_error (capture_init_expr);
7752 add_capture (lambda_expr,
7755 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7759 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7762 /* Parse the (optional) middle of a lambda expression.
7765 ( parameter-declaration-clause [opt] )
7766 attribute-specifier [opt]
7768 exception-specification [opt]
7769 lambda-return-type-clause [opt]
7771 LAMBDA_EXPR is the current representation of the lambda expression. */
7774 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7776 /* 5.1.1.4 of the standard says:
7777 If a lambda-expression does not include a lambda-declarator, it is as if
7778 the lambda-declarator were ().
7779 This means an empty parameter list, no attributes, and no exception
7781 tree param_list = void_list_node;
7782 tree attributes = NULL_TREE;
7783 tree exception_spec = NULL_TREE;
7786 /* The lambda-declarator is optional, but must begin with an opening
7787 parenthesis if present. */
7788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7790 cp_lexer_consume_token (parser->lexer);
7792 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7794 /* Parse parameters. */
7795 param_list = cp_parser_parameter_declaration_clause (parser);
7797 /* Default arguments shall not be specified in the
7798 parameter-declaration-clause of a lambda-declarator. */
7799 for (t = param_list; t; t = TREE_CHAIN (t))
7800 if (TREE_PURPOSE (t))
7801 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7802 "default argument specified for lambda parameter");
7804 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7806 attributes = cp_parser_attributes_opt (parser);
7808 /* Parse optional `mutable' keyword. */
7809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7811 cp_lexer_consume_token (parser->lexer);
7812 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7815 /* Parse optional exception specification. */
7816 exception_spec = cp_parser_exception_specification_opt (parser);
7818 /* Parse optional trailing return type. */
7819 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7821 cp_lexer_consume_token (parser->lexer);
7822 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7825 /* The function parameters must be in scope all the way until after the
7826 trailing-return-type in case of decltype. */
7827 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7828 pop_binding (DECL_NAME (t), t);
7833 /* Create the function call operator.
7835 Messing with declarators like this is no uglier than building up the
7836 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7839 cp_decl_specifier_seq return_type_specs;
7840 cp_declarator* declarator;
7845 clear_decl_specs (&return_type_specs);
7846 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7847 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7849 /* Maybe we will deduce the return type later, but we can use void
7850 as a placeholder return type anyways. */
7851 return_type_specs.type = void_type_node;
7853 p = obstack_alloc (&declarator_obstack, 0);
7855 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7858 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7859 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7860 declarator = make_call_declarator (declarator, param_list, quals,
7862 /*late_return_type=*/NULL_TREE);
7863 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7865 fco = grokmethod (&return_type_specs,
7868 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7869 DECL_ARTIFICIAL (fco) = 1;
7871 finish_member_declaration (fco);
7873 obstack_free (&declarator_obstack, p);
7877 /* Parse the body of a lambda expression, which is simply
7881 but which requires special handling.
7882 LAMBDA_EXPR is the current representation of the lambda expression. */
7885 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7887 bool nested = (current_function_decl != NULL_TREE);
7889 push_function_context ();
7891 /* Finish the function call operator
7893 + late_parsing_for_member
7894 + function_definition_after_declarator
7895 + ctor_initializer_opt_and_function_body */
7897 tree fco = lambda_function (lambda_expr);
7901 /* Let the front end know that we are going to be defining this
7903 start_preparsed_function (fco,
7905 SF_PRE_PARSED | SF_INCLASS_INLINE);
7907 start_lambda_scope (fco);
7908 body = begin_function_body ();
7910 /* 5.1.1.4 of the standard says:
7911 If a lambda-expression does not include a trailing-return-type, it
7912 is as if the trailing-return-type denotes the following type:
7913 * if the compound-statement is of the form
7914 { return attribute-specifier [opt] expression ; }
7915 the type of the returned expression after lvalue-to-rvalue
7916 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7917 (_conv.array_ 4.2), and function-to-pointer conversion
7919 * otherwise, void. */
7921 /* In a lambda that has neither a lambda-return-type-clause
7922 nor a deducible form, errors should be reported for return statements
7923 in the body. Since we used void as the placeholder return type, parsing
7924 the body as usual will give such desired behavior. */
7925 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7926 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7927 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7928 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7931 tree expr = NULL_TREE;
7932 cp_id_kind idk = CP_ID_KIND_NONE;
7934 /* Parse tentatively in case there's more after the initial return
7936 cp_parser_parse_tentatively (parser);
7938 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7939 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7941 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7943 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7944 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7946 if (cp_parser_parse_definitely (parser))
7948 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7950 compound_stmt = begin_compound_stmt (0);
7951 /* Will get error here if type not deduced yet. */
7952 finish_return_stmt (expr);
7953 finish_compound_stmt (compound_stmt);
7961 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7962 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7963 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7964 cp_parser_compound_stmt does not pass it. */
7965 cp_parser_function_body (parser);
7966 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7969 finish_function_body (body);
7970 finish_lambda_scope ();
7972 /* Finish the function and generate code for it if necessary. */
7973 expand_or_defer_fn (finish_function (/*inline*/2));
7977 pop_function_context();
7980 /* Statements [gram.stmt.stmt] */
7982 /* Parse a statement.
7986 expression-statement
7991 declaration-statement
7994 IN_COMPOUND is true when the statement is nested inside a
7995 cp_parser_compound_statement; this matters for certain pragmas.
7997 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7998 is a (possibly labeled) if statement which is not enclosed in braces
7999 and has an else clause. This is used to implement -Wparentheses. */
8002 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8003 bool in_compound, bool *if_p)
8007 location_t statement_location;
8012 /* There is no statement yet. */
8013 statement = NULL_TREE;
8014 /* Peek at the next token. */
8015 token = cp_lexer_peek_token (parser->lexer);
8016 /* Remember the location of the first token in the statement. */
8017 statement_location = token->location;
8018 /* If this is a keyword, then that will often determine what kind of
8019 statement we have. */
8020 if (token->type == CPP_KEYWORD)
8022 enum rid keyword = token->keyword;
8028 /* Looks like a labeled-statement with a case label.
8029 Parse the label, and then use tail recursion to parse
8031 cp_parser_label_for_labeled_statement (parser);
8036 statement = cp_parser_selection_statement (parser, if_p);
8042 statement = cp_parser_iteration_statement (parser);
8049 statement = cp_parser_jump_statement (parser);
8052 /* Objective-C++ exception-handling constructs. */
8055 case RID_AT_FINALLY:
8056 case RID_AT_SYNCHRONIZED:
8058 statement = cp_parser_objc_statement (parser);
8062 statement = cp_parser_try_block (parser);
8066 /* This must be a namespace alias definition. */
8067 cp_parser_declaration_statement (parser);
8071 /* It might be a keyword like `int' that can start a
8072 declaration-statement. */
8076 else if (token->type == CPP_NAME)
8078 /* If the next token is a `:', then we are looking at a
8079 labeled-statement. */
8080 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8081 if (token->type == CPP_COLON)
8083 /* Looks like a labeled-statement with an ordinary label.
8084 Parse the label, and then use tail recursion to parse
8086 cp_parser_label_for_labeled_statement (parser);
8090 /* Anything that starts with a `{' must be a compound-statement. */
8091 else if (token->type == CPP_OPEN_BRACE)
8092 statement = cp_parser_compound_statement (parser, NULL, false);
8093 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8094 a statement all its own. */
8095 else if (token->type == CPP_PRAGMA)
8097 /* Only certain OpenMP pragmas are attached to statements, and thus
8098 are considered statements themselves. All others are not. In
8099 the context of a compound, accept the pragma as a "statement" and
8100 return so that we can check for a close brace. Otherwise we
8101 require a real statement and must go back and read one. */
8103 cp_parser_pragma (parser, pragma_compound);
8104 else if (!cp_parser_pragma (parser, pragma_stmt))
8108 else if (token->type == CPP_EOF)
8110 cp_parser_error (parser, "expected statement");
8114 /* Everything else must be a declaration-statement or an
8115 expression-statement. Try for the declaration-statement
8116 first, unless we are looking at a `;', in which case we know that
8117 we have an expression-statement. */
8120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8122 cp_parser_parse_tentatively (parser);
8123 /* Try to parse the declaration-statement. */
8124 cp_parser_declaration_statement (parser);
8125 /* If that worked, we're done. */
8126 if (cp_parser_parse_definitely (parser))
8129 /* Look for an expression-statement instead. */
8130 statement = cp_parser_expression_statement (parser, in_statement_expr);
8133 /* Set the line number for the statement. */
8134 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8135 SET_EXPR_LOCATION (statement, statement_location);
8138 /* Parse the label for a labeled-statement, i.e.
8141 case constant-expression :
8145 case constant-expression ... constant-expression : statement
8147 When a label is parsed without errors, the label is added to the
8148 parse tree by the finish_* functions, so this function doesn't
8149 have to return the label. */
8152 cp_parser_label_for_labeled_statement (cp_parser* parser)
8155 tree label = NULL_TREE;
8157 /* The next token should be an identifier. */
8158 token = cp_lexer_peek_token (parser->lexer);
8159 if (token->type != CPP_NAME
8160 && token->type != CPP_KEYWORD)
8162 cp_parser_error (parser, "expected labeled-statement");
8166 switch (token->keyword)
8173 /* Consume the `case' token. */
8174 cp_lexer_consume_token (parser->lexer);
8175 /* Parse the constant-expression. */
8176 expr = cp_parser_constant_expression (parser,
8177 /*allow_non_constant_p=*/false,
8180 ellipsis = cp_lexer_peek_token (parser->lexer);
8181 if (ellipsis->type == CPP_ELLIPSIS)
8183 /* Consume the `...' token. */
8184 cp_lexer_consume_token (parser->lexer);
8186 cp_parser_constant_expression (parser,
8187 /*allow_non_constant_p=*/false,
8189 /* We don't need to emit warnings here, as the common code
8190 will do this for us. */
8193 expr_hi = NULL_TREE;
8195 if (parser->in_switch_statement_p)
8196 finish_case_label (token->location, expr, expr_hi);
8198 error_at (token->location,
8199 "case label %qE not within a switch statement",
8205 /* Consume the `default' token. */
8206 cp_lexer_consume_token (parser->lexer);
8208 if (parser->in_switch_statement_p)
8209 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8211 error_at (token->location, "case label not within a switch statement");
8215 /* Anything else must be an ordinary label. */
8216 label = finish_label_stmt (cp_parser_identifier (parser));
8220 /* Require the `:' token. */
8221 cp_parser_require (parser, CPP_COLON, RT_COLON);
8223 /* An ordinary label may optionally be followed by attributes.
8224 However, this is only permitted if the attributes are then
8225 followed by a semicolon. This is because, for backward
8226 compatibility, when parsing
8227 lab: __attribute__ ((unused)) int i;
8228 we want the attribute to attach to "i", not "lab". */
8229 if (label != NULL_TREE
8230 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8234 cp_parser_parse_tentatively (parser);
8235 attrs = cp_parser_attributes_opt (parser);
8236 if (attrs == NULL_TREE
8237 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8238 cp_parser_abort_tentative_parse (parser);
8239 else if (!cp_parser_parse_definitely (parser))
8242 cplus_decl_attributes (&label, attrs, 0);
8246 /* Parse an expression-statement.
8248 expression-statement:
8251 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8252 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8253 indicates whether this expression-statement is part of an
8254 expression statement. */
8257 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8259 tree statement = NULL_TREE;
8260 cp_token *token = cp_lexer_peek_token (parser->lexer);
8262 /* If the next token is a ';', then there is no expression
8264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8265 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8267 /* Give a helpful message for "A<T>::type t;" and the like. */
8268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8269 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8271 if (TREE_CODE (statement) == SCOPE_REF)
8272 error_at (token->location, "need %<typename%> before %qE because "
8273 "%qT is a dependent scope",
8274 statement, TREE_OPERAND (statement, 0));
8275 else if (is_overloaded_fn (statement)
8276 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8279 tree fn = get_first_fn (statement);
8280 error_at (token->location,
8281 "%<%T::%D%> names the constructor, not the type",
8282 DECL_CONTEXT (fn), DECL_NAME (fn));
8286 /* Consume the final `;'. */
8287 cp_parser_consume_semicolon_at_end_of_statement (parser);
8289 if (in_statement_expr
8290 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8291 /* This is the final expression statement of a statement
8293 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8295 statement = finish_expr_stmt (statement);
8302 /* Parse a compound-statement.
8305 { statement-seq [opt] }
8310 { label-declaration-seq [opt] statement-seq [opt] }
8312 label-declaration-seq:
8314 label-declaration-seq label-declaration
8316 Returns a tree representing the statement. */
8319 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8324 /* Consume the `{'. */
8325 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8326 return error_mark_node;
8327 /* Begin the compound-statement. */
8328 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8329 /* If the next keyword is `__label__' we have a label declaration. */
8330 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8331 cp_parser_label_declaration (parser);
8332 /* Parse an (optional) statement-seq. */
8333 cp_parser_statement_seq_opt (parser, in_statement_expr);
8334 /* Finish the compound-statement. */
8335 finish_compound_stmt (compound_stmt);
8336 /* Consume the `}'. */
8337 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8339 return compound_stmt;
8342 /* Parse an (optional) statement-seq.
8346 statement-seq [opt] statement */
8349 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8351 /* Scan statements until there aren't any more. */
8354 cp_token *token = cp_lexer_peek_token (parser->lexer);
8356 /* If we are looking at a `}', then we have run out of
8357 statements; the same is true if we have reached the end
8358 of file, or have stumbled upon a stray '@end'. */
8359 if (token->type == CPP_CLOSE_BRACE
8360 || token->type == CPP_EOF
8361 || token->type == CPP_PRAGMA_EOL
8362 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8365 /* If we are in a compound statement and find 'else' then
8366 something went wrong. */
8367 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8369 if (parser->in_statement & IN_IF_STMT)
8373 token = cp_lexer_consume_token (parser->lexer);
8374 error_at (token->location, "%<else%> without a previous %<if%>");
8378 /* Parse the statement. */
8379 cp_parser_statement (parser, in_statement_expr, true, NULL);
8383 /* Parse a selection-statement.
8385 selection-statement:
8386 if ( condition ) statement
8387 if ( condition ) statement else statement
8388 switch ( condition ) statement
8390 Returns the new IF_STMT or SWITCH_STMT.
8392 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8393 is a (possibly labeled) if statement which is not enclosed in
8394 braces and has an else clause. This is used to implement
8398 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8406 /* Peek at the next token. */
8407 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8409 /* See what kind of keyword it is. */
8410 keyword = token->keyword;
8419 /* Look for the `('. */
8420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8422 cp_parser_skip_to_end_of_statement (parser);
8423 return error_mark_node;
8426 /* Begin the selection-statement. */
8427 if (keyword == RID_IF)
8428 statement = begin_if_stmt ();
8430 statement = begin_switch_stmt ();
8432 /* Parse the condition. */
8433 condition = cp_parser_condition (parser);
8434 /* Look for the `)'. */
8435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8436 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8437 /*consume_paren=*/true);
8439 if (keyword == RID_IF)
8442 unsigned char in_statement;
8444 /* Add the condition. */
8445 finish_if_stmt_cond (condition, statement);
8447 /* Parse the then-clause. */
8448 in_statement = parser->in_statement;
8449 parser->in_statement |= IN_IF_STMT;
8450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8452 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8453 add_stmt (build_empty_stmt (loc));
8454 cp_lexer_consume_token (parser->lexer);
8455 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8456 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8457 "empty body in an %<if%> statement");
8461 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8462 parser->in_statement = in_statement;
8464 finish_then_clause (statement);
8466 /* If the next token is `else', parse the else-clause. */
8467 if (cp_lexer_next_token_is_keyword (parser->lexer,
8470 /* Consume the `else' keyword. */
8471 cp_lexer_consume_token (parser->lexer);
8472 begin_else_clause (statement);
8473 /* Parse the else-clause. */
8474 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8477 loc = cp_lexer_peek_token (parser->lexer)->location;
8479 OPT_Wempty_body, "suggest braces around "
8480 "empty body in an %<else%> statement");
8481 add_stmt (build_empty_stmt (loc));
8482 cp_lexer_consume_token (parser->lexer);
8485 cp_parser_implicitly_scoped_statement (parser, NULL);
8487 finish_else_clause (statement);
8489 /* If we are currently parsing a then-clause, then
8490 IF_P will not be NULL. We set it to true to
8491 indicate that this if statement has an else clause.
8492 This may trigger the Wparentheses warning below
8493 when we get back up to the parent if statement. */
8499 /* This if statement does not have an else clause. If
8500 NESTED_IF is true, then the then-clause is an if
8501 statement which does have an else clause. We warn
8502 about the potential ambiguity. */
8504 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8505 "suggest explicit braces to avoid ambiguous"
8509 /* Now we're all done with the if-statement. */
8510 finish_if_stmt (statement);
8514 bool in_switch_statement_p;
8515 unsigned char in_statement;
8517 /* Add the condition. */
8518 finish_switch_cond (condition, statement);
8520 /* Parse the body of the switch-statement. */
8521 in_switch_statement_p = parser->in_switch_statement_p;
8522 in_statement = parser->in_statement;
8523 parser->in_switch_statement_p = true;
8524 parser->in_statement |= IN_SWITCH_STMT;
8525 cp_parser_implicitly_scoped_statement (parser, NULL);
8526 parser->in_switch_statement_p = in_switch_statement_p;
8527 parser->in_statement = in_statement;
8529 /* Now we're all done with the switch-statement. */
8530 finish_switch_stmt (statement);
8538 cp_parser_error (parser, "expected selection-statement");
8539 return error_mark_node;
8543 /* Parse a condition.
8547 type-specifier-seq declarator = initializer-clause
8548 type-specifier-seq declarator braced-init-list
8553 type-specifier-seq declarator asm-specification [opt]
8554 attributes [opt] = assignment-expression
8556 Returns the expression that should be tested. */
8559 cp_parser_condition (cp_parser* parser)
8561 cp_decl_specifier_seq type_specifiers;
8562 const char *saved_message;
8563 int declares_class_or_enum;
8565 /* Try the declaration first. */
8566 cp_parser_parse_tentatively (parser);
8567 /* New types are not allowed in the type-specifier-seq for a
8569 saved_message = parser->type_definition_forbidden_message;
8570 parser->type_definition_forbidden_message
8571 = G_("types may not be defined in conditions");
8572 /* Parse the type-specifier-seq. */
8573 cp_parser_decl_specifier_seq (parser,
8574 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8576 &declares_class_or_enum);
8577 /* Restore the saved message. */
8578 parser->type_definition_forbidden_message = saved_message;
8579 /* If all is well, we might be looking at a declaration. */
8580 if (!cp_parser_error_occurred (parser))
8583 tree asm_specification;
8585 cp_declarator *declarator;
8586 tree initializer = NULL_TREE;
8588 /* Parse the declarator. */
8589 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8590 /*ctor_dtor_or_conv_p=*/NULL,
8591 /*parenthesized_p=*/NULL,
8592 /*member_p=*/false);
8593 /* Parse the attributes. */
8594 attributes = cp_parser_attributes_opt (parser);
8595 /* Parse the asm-specification. */
8596 asm_specification = cp_parser_asm_specification_opt (parser);
8597 /* If the next token is not an `=' or '{', then we might still be
8598 looking at an expression. For example:
8602 looks like a decl-specifier-seq and a declarator -- but then
8603 there is no `=', so this is an expression. */
8604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8605 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8606 cp_parser_simulate_error (parser);
8608 /* If we did see an `=' or '{', then we are looking at a declaration
8610 if (cp_parser_parse_definitely (parser))
8613 bool non_constant_p;
8614 bool flags = LOOKUP_ONLYCONVERTING;
8616 /* Create the declaration. */
8617 decl = start_decl (declarator, &type_specifiers,
8618 /*initialized_p=*/true,
8619 attributes, /*prefix_attributes=*/NULL_TREE,
8622 /* Parse the initializer. */
8623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8625 initializer = cp_parser_braced_list (parser, &non_constant_p);
8626 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8631 /* Consume the `='. */
8632 cp_parser_require (parser, CPP_EQ, RT_EQ);
8633 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8635 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8638 if (!non_constant_p)
8639 initializer = fold_non_dependent_expr (initializer);
8641 /* Process the initializer. */
8642 cp_finish_decl (decl,
8643 initializer, !non_constant_p,
8648 pop_scope (pushed_scope);
8650 return convert_from_reference (decl);
8653 /* If we didn't even get past the declarator successfully, we are
8654 definitely not looking at a declaration. */
8656 cp_parser_abort_tentative_parse (parser);
8658 /* Otherwise, we are looking at an expression. */
8659 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8662 /* Parses a traditional for-statement until the closing ')', not included. */
8665 cp_parser_c_for (cp_parser *parser)
8667 /* Normal for loop */
8669 tree condition = NULL_TREE;
8670 tree expression = NULL_TREE;
8672 /* Begin the for-statement. */
8673 stmt = begin_for_stmt ();
8675 /* Parse the initialization. */
8676 cp_parser_for_init_statement (parser);
8677 finish_for_init_stmt (stmt);
8679 /* If there's a condition, process it. */
8680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8681 condition = cp_parser_condition (parser);
8682 finish_for_cond (condition, stmt);
8683 /* Look for the `;'. */
8684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8686 /* If there's an expression, process it. */
8687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8688 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8689 finish_for_expr (expression, stmt);
8694 /* Tries to parse a range-based for-statement:
8697 type-specifier-seq declarator : expression
8699 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8700 expression. Note that the *DECL is returned unfinished, so
8701 later you should call cp_finish_decl().
8703 Returns TRUE iff a range-based for is parsed. */
8706 cp_parser_range_for (cp_parser *parser)
8708 tree stmt, range_decl, range_expr;
8709 cp_decl_specifier_seq type_specifiers;
8710 cp_declarator *declarator;
8711 const char *saved_message;
8712 tree attributes, pushed_scope;
8714 cp_parser_parse_tentatively (parser);
8715 /* New types are not allowed in the type-specifier-seq for a
8716 range-based for loop. */
8717 saved_message = parser->type_definition_forbidden_message;
8718 parser->type_definition_forbidden_message
8719 = G_("types may not be defined in range-based for loops");
8720 /* Parse the type-specifier-seq. */
8721 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8722 /*is_trailing_return=*/false,
8724 /* Restore the saved message. */
8725 parser->type_definition_forbidden_message = saved_message;
8726 /* If all is well, we might be looking at a declaration. */
8727 if (cp_parser_error_occurred (parser))
8729 cp_parser_abort_tentative_parse (parser);
8732 /* Parse the declarator. */
8733 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8734 /*ctor_dtor_or_conv_p=*/NULL,
8735 /*parenthesized_p=*/NULL,
8736 /*member_p=*/false);
8737 /* Parse the attributes. */
8738 attributes = cp_parser_attributes_opt (parser);
8739 /* The next token should be `:'. */
8740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8741 cp_parser_simulate_error (parser);
8743 /* Check if it is a range-based for */
8744 if (!cp_parser_parse_definitely (parser))
8747 cp_parser_require (parser, CPP_COLON, RT_COLON);
8748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8750 bool expr_non_constant_p;
8751 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8754 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8756 /* If in template, STMT is converted to a normal for-statements
8757 at instantiation. If not, it is done just ahead. */
8758 if (processing_template_decl)
8759 stmt = begin_range_for_stmt ();
8761 stmt = begin_for_stmt ();
8763 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8764 range_decl = start_decl (declarator, &type_specifiers,
8765 /*initialized_p=*/SD_INITIALIZED,
8766 attributes, /*prefix_attributes=*/NULL_TREE,
8768 /* No scope allowed here */
8769 pop_scope (pushed_scope);
8771 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8772 finish_range_for_decl (stmt, range_decl, range_expr);
8774 /* Convert the range-based for loop into a normal for-statement. */
8775 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8780 /* Converts a range-based for-statement into a normal
8781 for-statement, as per the definition.
8783 for (RANGE_DECL : RANGE_EXPR)
8786 should be equivalent to:
8789 auto &&__range = RANGE_EXPR;
8790 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8794 RANGE_DECL = *__begin;
8799 If RANGE_EXPR is an array:
8800 BEGIN_EXPR = __range
8801 END_EXPR = __range + ARRAY_SIZE(__range)
8803 BEGIN_EXPR = begin(__range)
8804 END_EXPR = end(__range);
8806 When calling begin()/end() we must use argument dependent
8807 lookup, but always considering 'std' as an associated namespace. */
8810 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8812 tree range_type, range_temp;
8814 tree iter_type, begin_expr, end_expr;
8815 tree condition, expression;
8817 /* Find out the type deduced by the declaration
8818 * `auto &&__range = range_expr' */
8819 range_type = cp_build_reference_type (make_auto (), true);
8820 range_type = do_auto_deduction (range_type, range_expr,
8821 type_uses_auto (range_type));
8823 /* Create the __range variable */
8824 range_temp = build_decl (input_location, VAR_DECL,
8825 get_identifier ("__for_range"), range_type);
8826 TREE_USED (range_temp) = 1;
8827 DECL_ARTIFICIAL (range_temp) = 1;
8828 pushdecl (range_temp);
8829 cp_finish_decl (range_temp, range_expr,
8830 /*is_constant_init*/false, NULL_TREE,
8831 LOOKUP_ONLYCONVERTING);
8833 range_temp = convert_from_reference (range_temp);
8835 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8837 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8838 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8839 begin_expr = range_temp;
8841 = build_binary_op (input_location, PLUS_EXPR,
8843 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8847 /* If it is not an array, we must call begin(__range)/end__range() */
8850 begin_expr = get_identifier ("begin");
8851 vec = make_tree_vector ();
8852 VEC_safe_push (tree, gc, vec, range_temp);
8853 begin_expr = perform_koenig_lookup (begin_expr, vec,
8854 /*include_std=*/true);
8855 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8856 tf_warning_or_error);
8857 release_tree_vector (vec);
8859 end_expr = get_identifier ("end");
8860 vec = make_tree_vector ();
8861 VEC_safe_push (tree, gc, vec, range_temp);
8862 end_expr = perform_koenig_lookup (end_expr, vec,
8863 /*include_std=*/true);
8864 end_expr = finish_call_expr (end_expr, &vec, false, true,
8865 tf_warning_or_error);
8866 release_tree_vector (vec);
8868 /* The unqualified type of the __begin and __end temporaries should
8869 * be the same as required by the multiple auto declaration */
8870 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8871 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8872 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8873 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8876 /* The new for initialization statement */
8877 begin = build_decl (input_location, VAR_DECL,
8878 get_identifier ("__for_begin"), iter_type);
8879 TREE_USED (begin) = 1;
8880 DECL_ARTIFICIAL (begin) = 1;
8882 cp_finish_decl (begin, begin_expr,
8883 /*is_constant_init*/false, NULL_TREE,
8884 LOOKUP_ONLYCONVERTING);
8886 end = build_decl (input_location, VAR_DECL,
8887 get_identifier ("__for_end"), iter_type);
8888 TREE_USED (end) = 1;
8889 DECL_ARTIFICIAL (end) = 1;
8891 cp_finish_decl (end, end_expr,
8892 /*is_constant_init*/false, NULL_TREE,
8893 LOOKUP_ONLYCONVERTING);
8895 finish_for_init_stmt (statement);
8897 /* The new for condition */
8898 condition = build_x_binary_op (NE_EXPR,
8901 NULL, tf_warning_or_error);
8902 finish_for_cond (condition, statement);
8904 /* The new increment expression */
8905 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8906 finish_for_expr (expression, statement);
8908 /* The declaration is initialized with *__begin inside the loop body */
8909 cp_finish_decl (range_decl,
8910 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8911 /*is_constant_init*/false, NULL_TREE,
8912 LOOKUP_ONLYCONVERTING);
8918 /* Parse an iteration-statement.
8920 iteration-statement:
8921 while ( condition ) statement
8922 do statement while ( expression ) ;
8923 for ( for-init-statement condition [opt] ; expression [opt] )
8926 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8929 cp_parser_iteration_statement (cp_parser* parser)
8934 unsigned char in_statement;
8936 /* Peek at the next token. */
8937 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8939 return error_mark_node;
8941 /* Remember whether or not we are already within an iteration
8943 in_statement = parser->in_statement;
8945 /* See what kind of keyword it is. */
8946 keyword = token->keyword;
8953 /* Begin the while-statement. */
8954 statement = begin_while_stmt ();
8955 /* Look for the `('. */
8956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8957 /* Parse the condition. */
8958 condition = cp_parser_condition (parser);
8959 finish_while_stmt_cond (condition, statement);
8960 /* Look for the `)'. */
8961 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8962 /* Parse the dependent statement. */
8963 parser->in_statement = IN_ITERATION_STMT;
8964 cp_parser_already_scoped_statement (parser);
8965 parser->in_statement = in_statement;
8966 /* We're done with the while-statement. */
8967 finish_while_stmt (statement);
8975 /* Begin the do-statement. */
8976 statement = begin_do_stmt ();
8977 /* Parse the body of the do-statement. */
8978 parser->in_statement = IN_ITERATION_STMT;
8979 cp_parser_implicitly_scoped_statement (parser, NULL);
8980 parser->in_statement = in_statement;
8981 finish_do_body (statement);
8982 /* Look for the `while' keyword. */
8983 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8984 /* Look for the `('. */
8985 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8986 /* Parse the expression. */
8987 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8988 /* We're done with the do-statement. */
8989 finish_do_stmt (expression, statement);
8990 /* Look for the `)'. */
8991 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8992 /* Look for the `;'. */
8993 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8999 /* Look for the `('. */
9000 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9002 if (cxx_dialect == cxx0x)
9003 statement = cp_parser_range_for (parser);
9005 statement = NULL_TREE;
9006 if (statement == NULL_TREE)
9007 statement = cp_parser_c_for (parser);
9009 /* Look for the `)'. */
9010 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9012 /* Parse the body of the for-statement. */
9013 parser->in_statement = IN_ITERATION_STMT;
9014 cp_parser_already_scoped_statement (parser);
9015 parser->in_statement = in_statement;
9017 /* We're done with the for-statement. */
9018 finish_for_stmt (statement);
9023 cp_parser_error (parser, "expected iteration-statement");
9024 statement = error_mark_node;
9031 /* Parse a for-init-statement.
9034 expression-statement
9035 simple-declaration */
9038 cp_parser_for_init_statement (cp_parser* parser)
9040 /* If the next token is a `;', then we have an empty
9041 expression-statement. Grammatically, this is also a
9042 simple-declaration, but an invalid one, because it does not
9043 declare anything. Therefore, if we did not handle this case
9044 specially, we would issue an error message about an invalid
9046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9048 /* We're going to speculatively look for a declaration, falling back
9049 to an expression, if necessary. */
9050 cp_parser_parse_tentatively (parser);
9051 /* Parse the declaration. */
9052 cp_parser_simple_declaration (parser,
9053 /*function_definition_allowed_p=*/false);
9054 /* If the tentative parse failed, then we shall need to look for an
9055 expression-statement. */
9056 if (cp_parser_parse_definitely (parser))
9060 cp_parser_expression_statement (parser, NULL_TREE);
9063 /* Parse a jump-statement.
9068 return expression [opt] ;
9069 return braced-init-list ;
9077 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9080 cp_parser_jump_statement (cp_parser* parser)
9082 tree statement = error_mark_node;
9085 unsigned char in_statement;
9087 /* Peek at the next token. */
9088 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9090 return error_mark_node;
9092 /* See what kind of keyword it is. */
9093 keyword = token->keyword;
9097 in_statement = parser->in_statement & ~IN_IF_STMT;
9098 switch (in_statement)
9101 error_at (token->location, "break statement not within loop or switch");
9104 gcc_assert ((in_statement & IN_SWITCH_STMT)
9105 || in_statement == IN_ITERATION_STMT);
9106 statement = finish_break_stmt ();
9109 error_at (token->location, "invalid exit from OpenMP structured block");
9112 error_at (token->location, "break statement used with OpenMP for loop");
9115 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9119 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9122 error_at (token->location, "continue statement not within a loop");
9124 case IN_ITERATION_STMT:
9126 statement = finish_continue_stmt ();
9129 error_at (token->location, "invalid exit from OpenMP structured block");
9134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9140 bool expr_non_constant_p;
9142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9144 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9145 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9147 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9148 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9150 /* If the next token is a `;', then there is no
9153 /* Build the return-statement. */
9154 statement = finish_return_stmt (expr);
9155 /* Look for the final `;'. */
9156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9161 /* Create the goto-statement. */
9162 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9164 /* Issue a warning about this use of a GNU extension. */
9165 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9166 /* Consume the '*' token. */
9167 cp_lexer_consume_token (parser->lexer);
9168 /* Parse the dependent expression. */
9169 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9172 finish_goto_stmt (cp_parser_identifier (parser));
9173 /* Look for the final `;'. */
9174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9178 cp_parser_error (parser, "expected jump-statement");
9185 /* Parse a declaration-statement.
9187 declaration-statement:
9188 block-declaration */
9191 cp_parser_declaration_statement (cp_parser* parser)
9195 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9196 p = obstack_alloc (&declarator_obstack, 0);
9198 /* Parse the block-declaration. */
9199 cp_parser_block_declaration (parser, /*statement_p=*/true);
9201 /* Free any declarators allocated. */
9202 obstack_free (&declarator_obstack, p);
9204 /* Finish off the statement. */
9208 /* Some dependent statements (like `if (cond) statement'), are
9209 implicitly in their own scope. In other words, if the statement is
9210 a single statement (as opposed to a compound-statement), it is
9211 none-the-less treated as if it were enclosed in braces. Any
9212 declarations appearing in the dependent statement are out of scope
9213 after control passes that point. This function parses a statement,
9214 but ensures that is in its own scope, even if it is not a
9217 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9218 is a (possibly labeled) if statement which is not enclosed in
9219 braces and has an else clause. This is used to implement
9222 Returns the new statement. */
9225 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9232 /* Mark if () ; with a special NOP_EXPR. */
9233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9235 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9236 cp_lexer_consume_token (parser->lexer);
9237 statement = add_stmt (build_empty_stmt (loc));
9239 /* if a compound is opened, we simply parse the statement directly. */
9240 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9241 statement = cp_parser_compound_statement (parser, NULL, false);
9242 /* If the token is not a `{', then we must take special action. */
9245 /* Create a compound-statement. */
9246 statement = begin_compound_stmt (0);
9247 /* Parse the dependent-statement. */
9248 cp_parser_statement (parser, NULL_TREE, false, if_p);
9249 /* Finish the dummy compound-statement. */
9250 finish_compound_stmt (statement);
9253 /* Return the statement. */
9257 /* For some dependent statements (like `while (cond) statement'), we
9258 have already created a scope. Therefore, even if the dependent
9259 statement is a compound-statement, we do not want to create another
9263 cp_parser_already_scoped_statement (cp_parser* parser)
9265 /* If the token is a `{', then we must take special action. */
9266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9267 cp_parser_statement (parser, NULL_TREE, false, NULL);
9270 /* Avoid calling cp_parser_compound_statement, so that we
9271 don't create a new scope. Do everything else by hand. */
9272 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9273 /* If the next keyword is `__label__' we have a label declaration. */
9274 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9275 cp_parser_label_declaration (parser);
9276 /* Parse an (optional) statement-seq. */
9277 cp_parser_statement_seq_opt (parser, NULL_TREE);
9278 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9282 /* Declarations [gram.dcl.dcl] */
9284 /* Parse an optional declaration-sequence.
9288 declaration-seq declaration */
9291 cp_parser_declaration_seq_opt (cp_parser* parser)
9297 token = cp_lexer_peek_token (parser->lexer);
9299 if (token->type == CPP_CLOSE_BRACE
9300 || token->type == CPP_EOF
9301 || token->type == CPP_PRAGMA_EOL)
9304 if (token->type == CPP_SEMICOLON)
9306 /* A declaration consisting of a single semicolon is
9307 invalid. Allow it unless we're being pedantic. */
9308 cp_lexer_consume_token (parser->lexer);
9309 if (!in_system_header)
9310 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9314 /* If we're entering or exiting a region that's implicitly
9315 extern "C", modify the lang context appropriately. */
9316 if (!parser->implicit_extern_c && token->implicit_extern_c)
9318 push_lang_context (lang_name_c);
9319 parser->implicit_extern_c = true;
9321 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9323 pop_lang_context ();
9324 parser->implicit_extern_c = false;
9327 if (token->type == CPP_PRAGMA)
9329 /* A top-level declaration can consist solely of a #pragma.
9330 A nested declaration cannot, so this is done here and not
9331 in cp_parser_declaration. (A #pragma at block scope is
9332 handled in cp_parser_statement.) */
9333 cp_parser_pragma (parser, pragma_external);
9337 /* Parse the declaration itself. */
9338 cp_parser_declaration (parser);
9342 /* Parse a declaration.
9347 template-declaration
9348 explicit-instantiation
9349 explicit-specialization
9350 linkage-specification
9351 namespace-definition
9356 __extension__ declaration */
9359 cp_parser_declaration (cp_parser* parser)
9365 tree attributes = NULL_TREE;
9367 /* Check for the `__extension__' keyword. */
9368 if (cp_parser_extension_opt (parser, &saved_pedantic))
9370 /* Parse the qualified declaration. */
9371 cp_parser_declaration (parser);
9372 /* Restore the PEDANTIC flag. */
9373 pedantic = saved_pedantic;
9378 /* Try to figure out what kind of declaration is present. */
9379 token1 = *cp_lexer_peek_token (parser->lexer);
9381 if (token1.type != CPP_EOF)
9382 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9385 token2.type = CPP_EOF;
9386 token2.keyword = RID_MAX;
9389 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9390 p = obstack_alloc (&declarator_obstack, 0);
9392 /* If the next token is `extern' and the following token is a string
9393 literal, then we have a linkage specification. */
9394 if (token1.keyword == RID_EXTERN
9395 && cp_parser_is_string_literal (&token2))
9396 cp_parser_linkage_specification (parser);
9397 /* If the next token is `template', then we have either a template
9398 declaration, an explicit instantiation, or an explicit
9400 else if (token1.keyword == RID_TEMPLATE)
9402 /* `template <>' indicates a template specialization. */
9403 if (token2.type == CPP_LESS
9404 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9405 cp_parser_explicit_specialization (parser);
9406 /* `template <' indicates a template declaration. */
9407 else if (token2.type == CPP_LESS)
9408 cp_parser_template_declaration (parser, /*member_p=*/false);
9409 /* Anything else must be an explicit instantiation. */
9411 cp_parser_explicit_instantiation (parser);
9413 /* If the next token is `export', then we have a template
9415 else if (token1.keyword == RID_EXPORT)
9416 cp_parser_template_declaration (parser, /*member_p=*/false);
9417 /* If the next token is `extern', 'static' or 'inline' and the one
9418 after that is `template', we have a GNU extended explicit
9419 instantiation directive. */
9420 else if (cp_parser_allow_gnu_extensions_p (parser)
9421 && (token1.keyword == RID_EXTERN
9422 || token1.keyword == RID_STATIC
9423 || token1.keyword == RID_INLINE)
9424 && token2.keyword == RID_TEMPLATE)
9425 cp_parser_explicit_instantiation (parser);
9426 /* If the next token is `namespace', check for a named or unnamed
9427 namespace definition. */
9428 else if (token1.keyword == RID_NAMESPACE
9429 && (/* A named namespace definition. */
9430 (token2.type == CPP_NAME
9431 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9433 /* An unnamed namespace definition. */
9434 || token2.type == CPP_OPEN_BRACE
9435 || token2.keyword == RID_ATTRIBUTE))
9436 cp_parser_namespace_definition (parser);
9437 /* An inline (associated) namespace definition. */
9438 else if (token1.keyword == RID_INLINE
9439 && token2.keyword == RID_NAMESPACE)
9440 cp_parser_namespace_definition (parser);
9441 /* Objective-C++ declaration/definition. */
9442 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9443 cp_parser_objc_declaration (parser, NULL_TREE);
9444 else if (c_dialect_objc ()
9445 && token1.keyword == RID_ATTRIBUTE
9446 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9447 cp_parser_objc_declaration (parser, attributes);
9448 /* We must have either a block declaration or a function
9451 /* Try to parse a block-declaration, or a function-definition. */
9452 cp_parser_block_declaration (parser, /*statement_p=*/false);
9454 /* Free any declarators allocated. */
9455 obstack_free (&declarator_obstack, p);
9458 /* Parse a block-declaration.
9463 namespace-alias-definition
9470 __extension__ block-declaration
9475 static_assert-declaration
9477 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9478 part of a declaration-statement. */
9481 cp_parser_block_declaration (cp_parser *parser,
9487 /* Check for the `__extension__' keyword. */
9488 if (cp_parser_extension_opt (parser, &saved_pedantic))
9490 /* Parse the qualified declaration. */
9491 cp_parser_block_declaration (parser, statement_p);
9492 /* Restore the PEDANTIC flag. */
9493 pedantic = saved_pedantic;
9498 /* Peek at the next token to figure out which kind of declaration is
9500 token1 = cp_lexer_peek_token (parser->lexer);
9502 /* If the next keyword is `asm', we have an asm-definition. */
9503 if (token1->keyword == RID_ASM)
9506 cp_parser_commit_to_tentative_parse (parser);
9507 cp_parser_asm_definition (parser);
9509 /* If the next keyword is `namespace', we have a
9510 namespace-alias-definition. */
9511 else if (token1->keyword == RID_NAMESPACE)
9512 cp_parser_namespace_alias_definition (parser);
9513 /* If the next keyword is `using', we have either a
9514 using-declaration or a using-directive. */
9515 else if (token1->keyword == RID_USING)
9520 cp_parser_commit_to_tentative_parse (parser);
9521 /* If the token after `using' is `namespace', then we have a
9523 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9524 if (token2->keyword == RID_NAMESPACE)
9525 cp_parser_using_directive (parser);
9526 /* Otherwise, it's a using-declaration. */
9528 cp_parser_using_declaration (parser,
9529 /*access_declaration_p=*/false);
9531 /* If the next keyword is `__label__' we have a misplaced label
9533 else if (token1->keyword == RID_LABEL)
9535 cp_lexer_consume_token (parser->lexer);
9536 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9537 cp_parser_skip_to_end_of_statement (parser);
9538 /* If the next token is now a `;', consume it. */
9539 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9540 cp_lexer_consume_token (parser->lexer);
9542 /* If the next token is `static_assert' we have a static assertion. */
9543 else if (token1->keyword == RID_STATIC_ASSERT)
9544 cp_parser_static_assert (parser, /*member_p=*/false);
9545 /* Anything else must be a simple-declaration. */
9547 cp_parser_simple_declaration (parser, !statement_p);
9550 /* Parse a simple-declaration.
9553 decl-specifier-seq [opt] init-declarator-list [opt] ;
9555 init-declarator-list:
9557 init-declarator-list , init-declarator
9559 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9560 function-definition as a simple-declaration. */
9563 cp_parser_simple_declaration (cp_parser* parser,
9564 bool function_definition_allowed_p)
9566 cp_decl_specifier_seq decl_specifiers;
9567 int declares_class_or_enum;
9568 bool saw_declarator;
9570 /* Defer access checks until we know what is being declared; the
9571 checks for names appearing in the decl-specifier-seq should be
9572 done as if we were in the scope of the thing being declared. */
9573 push_deferring_access_checks (dk_deferred);
9575 /* Parse the decl-specifier-seq. We have to keep track of whether
9576 or not the decl-specifier-seq declares a named class or
9577 enumeration type, since that is the only case in which the
9578 init-declarator-list is allowed to be empty.
9582 In a simple-declaration, the optional init-declarator-list can be
9583 omitted only when declaring a class or enumeration, that is when
9584 the decl-specifier-seq contains either a class-specifier, an
9585 elaborated-type-specifier, or an enum-specifier. */
9586 cp_parser_decl_specifier_seq (parser,
9587 CP_PARSER_FLAGS_OPTIONAL,
9589 &declares_class_or_enum);
9590 /* We no longer need to defer access checks. */
9591 stop_deferring_access_checks ();
9593 /* In a block scope, a valid declaration must always have a
9594 decl-specifier-seq. By not trying to parse declarators, we can
9595 resolve the declaration/expression ambiguity more quickly. */
9596 if (!function_definition_allowed_p
9597 && !decl_specifiers.any_specifiers_p)
9599 cp_parser_error (parser, "expected declaration");
9603 /* If the next two tokens are both identifiers, the code is
9604 erroneous. The usual cause of this situation is code like:
9608 where "T" should name a type -- but does not. */
9609 if (!decl_specifiers.any_type_specifiers_p
9610 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9612 /* If parsing tentatively, we should commit; we really are
9613 looking at a declaration. */
9614 cp_parser_commit_to_tentative_parse (parser);
9619 /* If we have seen at least one decl-specifier, and the next token
9620 is not a parenthesis, then we must be looking at a declaration.
9621 (After "int (" we might be looking at a functional cast.) */
9622 if (decl_specifiers.any_specifiers_p
9623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9624 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9625 && !cp_parser_error_occurred (parser))
9626 cp_parser_commit_to_tentative_parse (parser);
9628 /* Keep going until we hit the `;' at the end of the simple
9630 saw_declarator = false;
9631 while (cp_lexer_next_token_is_not (parser->lexer,
9635 bool function_definition_p;
9640 /* If we are processing next declarator, coma is expected */
9641 token = cp_lexer_peek_token (parser->lexer);
9642 gcc_assert (token->type == CPP_COMMA);
9643 cp_lexer_consume_token (parser->lexer);
9646 saw_declarator = true;
9648 /* Parse the init-declarator. */
9649 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9651 function_definition_allowed_p,
9653 declares_class_or_enum,
9654 &function_definition_p);
9655 /* If an error occurred while parsing tentatively, exit quickly.
9656 (That usually happens when in the body of a function; each
9657 statement is treated as a declaration-statement until proven
9659 if (cp_parser_error_occurred (parser))
9661 /* Handle function definitions specially. */
9662 if (function_definition_p)
9664 /* If the next token is a `,', then we are probably
9665 processing something like:
9669 which is erroneous. */
9670 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9672 cp_token *token = cp_lexer_peek_token (parser->lexer);
9673 error_at (token->location,
9675 " declarations and function-definitions is forbidden");
9677 /* Otherwise, we're done with the list of declarators. */
9680 pop_deferring_access_checks ();
9684 /* The next token should be either a `,' or a `;'. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686 /* If it's a `,', there are more declarators to come. */
9687 if (token->type == CPP_COMMA)
9688 /* will be consumed next time around */;
9689 /* If it's a `;', we are done. */
9690 else if (token->type == CPP_SEMICOLON)
9692 /* Anything else is an error. */
9695 /* If we have already issued an error message we don't need
9696 to issue another one. */
9697 if (decl != error_mark_node
9698 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9699 cp_parser_error (parser, "expected %<,%> or %<;%>");
9700 /* Skip tokens until we reach the end of the statement. */
9701 cp_parser_skip_to_end_of_statement (parser);
9702 /* If the next token is now a `;', consume it. */
9703 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9704 cp_lexer_consume_token (parser->lexer);
9707 /* After the first time around, a function-definition is not
9708 allowed -- even if it was OK at first. For example:
9713 function_definition_allowed_p = false;
9716 /* Issue an error message if no declarators are present, and the
9717 decl-specifier-seq does not itself declare a class or
9719 if (!saw_declarator)
9721 if (cp_parser_declares_only_class_p (parser))
9722 shadow_tag (&decl_specifiers);
9723 /* Perform any deferred access checks. */
9724 perform_deferred_access_checks ();
9727 /* Consume the `;'. */
9728 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9731 pop_deferring_access_checks ();
9734 /* Parse a decl-specifier-seq.
9737 decl-specifier-seq [opt] decl-specifier
9740 storage-class-specifier
9751 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9753 The parser flags FLAGS is used to control type-specifier parsing.
9755 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9758 1: one of the decl-specifiers is an elaborated-type-specifier
9759 (i.e., a type declaration)
9760 2: one of the decl-specifiers is an enum-specifier or a
9761 class-specifier (i.e., a type definition)
9766 cp_parser_decl_specifier_seq (cp_parser* parser,
9767 cp_parser_flags flags,
9768 cp_decl_specifier_seq *decl_specs,
9769 int* declares_class_or_enum)
9771 bool constructor_possible_p = !parser->in_declarator_p;
9772 cp_token *start_token = NULL;
9774 /* Clear DECL_SPECS. */
9775 clear_decl_specs (decl_specs);
9777 /* Assume no class or enumeration type is declared. */
9778 *declares_class_or_enum = 0;
9780 /* Keep reading specifiers until there are no more to read. */
9784 bool found_decl_spec;
9787 /* Peek at the next token. */
9788 token = cp_lexer_peek_token (parser->lexer);
9790 /* Save the first token of the decl spec list for error
9793 start_token = token;
9794 /* Handle attributes. */
9795 if (token->keyword == RID_ATTRIBUTE)
9797 /* Parse the attributes. */
9798 decl_specs->attributes
9799 = chainon (decl_specs->attributes,
9800 cp_parser_attributes_opt (parser));
9803 /* Assume we will find a decl-specifier keyword. */
9804 found_decl_spec = true;
9805 /* If the next token is an appropriate keyword, we can simply
9806 add it to the list. */
9807 switch (token->keyword)
9813 if (!at_class_scope_p ())
9815 error_at (token->location, "%<friend%> used outside of class");
9816 cp_lexer_purge_token (parser->lexer);
9820 ++decl_specs->specs[(int) ds_friend];
9821 /* Consume the token. */
9822 cp_lexer_consume_token (parser->lexer);
9827 ++decl_specs->specs[(int) ds_constexpr];
9828 cp_lexer_consume_token (parser->lexer);
9831 /* function-specifier:
9838 cp_parser_function_specifier_opt (parser, decl_specs);
9844 ++decl_specs->specs[(int) ds_typedef];
9845 /* Consume the token. */
9846 cp_lexer_consume_token (parser->lexer);
9847 /* A constructor declarator cannot appear in a typedef. */
9848 constructor_possible_p = false;
9849 /* The "typedef" keyword can only occur in a declaration; we
9850 may as well commit at this point. */
9851 cp_parser_commit_to_tentative_parse (parser);
9853 if (decl_specs->storage_class != sc_none)
9854 decl_specs->conflicting_specifiers_p = true;
9857 /* storage-class-specifier:
9867 if (cxx_dialect == cxx98)
9869 /* Consume the token. */
9870 cp_lexer_consume_token (parser->lexer);
9872 /* Complain about `auto' as a storage specifier, if
9873 we're complaining about C++0x compatibility. */
9874 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9875 " will change meaning in C++0x; please remove it");
9877 /* Set the storage class anyway. */
9878 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9882 /* C++0x auto type-specifier. */
9883 found_decl_spec = false;
9890 /* Consume the token. */
9891 cp_lexer_consume_token (parser->lexer);
9892 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9896 /* Consume the token. */
9897 cp_lexer_consume_token (parser->lexer);
9898 ++decl_specs->specs[(int) ds_thread];
9902 /* We did not yet find a decl-specifier yet. */
9903 found_decl_spec = false;
9908 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9909 && token->keyword != RID_CONSTEXPR)
9910 error ("decl-specifier invalid in condition");
9912 /* Constructors are a special case. The `S' in `S()' is not a
9913 decl-specifier; it is the beginning of the declarator. */
9916 && constructor_possible_p
9917 && (cp_parser_constructor_declarator_p
9918 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9920 /* If we don't have a DECL_SPEC yet, then we must be looking at
9921 a type-specifier. */
9922 if (!found_decl_spec && !constructor_p)
9924 int decl_spec_declares_class_or_enum;
9925 bool is_cv_qualifier;
9929 = cp_parser_type_specifier (parser, flags,
9931 /*is_declaration=*/true,
9932 &decl_spec_declares_class_or_enum,
9934 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9936 /* If this type-specifier referenced a user-defined type
9937 (a typedef, class-name, etc.), then we can't allow any
9938 more such type-specifiers henceforth.
9942 The longest sequence of decl-specifiers that could
9943 possibly be a type name is taken as the
9944 decl-specifier-seq of a declaration. The sequence shall
9945 be self-consistent as described below.
9949 As a general rule, at most one type-specifier is allowed
9950 in the complete decl-specifier-seq of a declaration. The
9951 only exceptions are the following:
9953 -- const or volatile can be combined with any other
9956 -- signed or unsigned can be combined with char, long,
9964 void g (const int Pc);
9966 Here, Pc is *not* part of the decl-specifier seq; it's
9967 the declarator. Therefore, once we see a type-specifier
9968 (other than a cv-qualifier), we forbid any additional
9969 user-defined types. We *do* still allow things like `int
9970 int' to be considered a decl-specifier-seq, and issue the
9971 error message later. */
9972 if (type_spec && !is_cv_qualifier)
9973 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9974 /* A constructor declarator cannot follow a type-specifier. */
9977 constructor_possible_p = false;
9978 found_decl_spec = true;
9979 if (!is_cv_qualifier)
9980 decl_specs->any_type_specifiers_p = true;
9984 /* If we still do not have a DECL_SPEC, then there are no more
9986 if (!found_decl_spec)
9989 decl_specs->any_specifiers_p = true;
9990 /* After we see one decl-specifier, further decl-specifiers are
9992 flags |= CP_PARSER_FLAGS_OPTIONAL;
9995 cp_parser_check_decl_spec (decl_specs, start_token->location);
9997 /* Don't allow a friend specifier with a class definition. */
9998 if (decl_specs->specs[(int) ds_friend] != 0
9999 && (*declares_class_or_enum & 2))
10000 error_at (start_token->location,
10001 "class definition may not be declared a friend");
10004 /* Parse an (optional) storage-class-specifier.
10006 storage-class-specifier:
10015 storage-class-specifier:
10018 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10021 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10023 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10026 if (cxx_dialect != cxx98)
10028 /* Fall through for C++98. */
10035 /* Consume the token. */
10036 return cp_lexer_consume_token (parser->lexer)->u.value;
10043 /* Parse an (optional) function-specifier.
10045 function-specifier:
10050 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10051 Updates DECL_SPECS, if it is non-NULL. */
10054 cp_parser_function_specifier_opt (cp_parser* parser,
10055 cp_decl_specifier_seq *decl_specs)
10057 cp_token *token = cp_lexer_peek_token (parser->lexer);
10058 switch (token->keyword)
10062 ++decl_specs->specs[(int) ds_inline];
10066 /* 14.5.2.3 [temp.mem]
10068 A member function template shall not be virtual. */
10069 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10070 error_at (token->location, "templates may not be %<virtual%>");
10071 else if (decl_specs)
10072 ++decl_specs->specs[(int) ds_virtual];
10077 ++decl_specs->specs[(int) ds_explicit];
10084 /* Consume the token. */
10085 return cp_lexer_consume_token (parser->lexer)->u.value;
10088 /* Parse a linkage-specification.
10090 linkage-specification:
10091 extern string-literal { declaration-seq [opt] }
10092 extern string-literal declaration */
10095 cp_parser_linkage_specification (cp_parser* parser)
10099 /* Look for the `extern' keyword. */
10100 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10102 /* Look for the string-literal. */
10103 linkage = cp_parser_string_literal (parser, false, false);
10105 /* Transform the literal into an identifier. If the literal is a
10106 wide-character string, or contains embedded NULs, then we can't
10107 handle it as the user wants. */
10108 if (strlen (TREE_STRING_POINTER (linkage))
10109 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10111 cp_parser_error (parser, "invalid linkage-specification");
10112 /* Assume C++ linkage. */
10113 linkage = lang_name_cplusplus;
10116 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10118 /* We're now using the new linkage. */
10119 push_lang_context (linkage);
10121 /* If the next token is a `{', then we're using the first
10123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10125 /* Consume the `{' token. */
10126 cp_lexer_consume_token (parser->lexer);
10127 /* Parse the declarations. */
10128 cp_parser_declaration_seq_opt (parser);
10129 /* Look for the closing `}'. */
10130 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10132 /* Otherwise, there's just one declaration. */
10135 bool saved_in_unbraced_linkage_specification_p;
10137 saved_in_unbraced_linkage_specification_p
10138 = parser->in_unbraced_linkage_specification_p;
10139 parser->in_unbraced_linkage_specification_p = true;
10140 cp_parser_declaration (parser);
10141 parser->in_unbraced_linkage_specification_p
10142 = saved_in_unbraced_linkage_specification_p;
10145 /* We're done with the linkage-specification. */
10146 pop_lang_context ();
10149 /* Parse a static_assert-declaration.
10151 static_assert-declaration:
10152 static_assert ( constant-expression , string-literal ) ;
10154 If MEMBER_P, this static_assert is a class member. */
10157 cp_parser_static_assert(cp_parser *parser, bool member_p)
10162 location_t saved_loc;
10164 /* Peek at the `static_assert' token so we can keep track of exactly
10165 where the static assertion started. */
10166 token = cp_lexer_peek_token (parser->lexer);
10167 saved_loc = token->location;
10169 /* Look for the `static_assert' keyword. */
10170 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10174 /* We know we are in a static assertion; commit to any tentative
10176 if (cp_parser_parsing_tentatively (parser))
10177 cp_parser_commit_to_tentative_parse (parser);
10179 /* Parse the `(' starting the static assertion condition. */
10180 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10182 /* Parse the constant-expression. */
10184 cp_parser_constant_expression (parser,
10185 /*allow_non_constant_p=*/false,
10186 /*non_constant_p=*/NULL);
10188 /* Parse the separating `,'. */
10189 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10191 /* Parse the string-literal message. */
10192 message = cp_parser_string_literal (parser,
10193 /*translate=*/false,
10196 /* A `)' completes the static assertion. */
10197 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10198 cp_parser_skip_to_closing_parenthesis (parser,
10199 /*recovering=*/true,
10200 /*or_comma=*/false,
10201 /*consume_paren=*/true);
10203 /* A semicolon terminates the declaration. */
10204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10206 /* Complete the static assertion, which may mean either processing
10207 the static assert now or saving it for template instantiation. */
10208 finish_static_assert (condition, message, saved_loc, member_p);
10211 /* Parse a `decltype' type. Returns the type.
10213 simple-type-specifier:
10214 decltype ( expression ) */
10217 cp_parser_decltype (cp_parser *parser)
10220 bool id_expression_or_member_access_p = false;
10221 const char *saved_message;
10222 bool saved_integral_constant_expression_p;
10223 bool saved_non_integral_constant_expression_p;
10224 cp_token *id_expr_start_token;
10226 /* Look for the `decltype' token. */
10227 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10228 return error_mark_node;
10230 /* Types cannot be defined in a `decltype' expression. Save away the
10232 saved_message = parser->type_definition_forbidden_message;
10234 /* And create the new one. */
10235 parser->type_definition_forbidden_message
10236 = G_("types may not be defined in %<decltype%> expressions");
10238 /* The restrictions on constant-expressions do not apply inside
10239 decltype expressions. */
10240 saved_integral_constant_expression_p
10241 = parser->integral_constant_expression_p;
10242 saved_non_integral_constant_expression_p
10243 = parser->non_integral_constant_expression_p;
10244 parser->integral_constant_expression_p = false;
10246 /* Do not actually evaluate the expression. */
10247 ++cp_unevaluated_operand;
10249 /* Do not warn about problems with the expression. */
10250 ++c_inhibit_evaluation_warnings;
10252 /* Parse the opening `('. */
10253 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10254 return error_mark_node;
10256 /* First, try parsing an id-expression. */
10257 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10258 cp_parser_parse_tentatively (parser);
10259 expr = cp_parser_id_expression (parser,
10260 /*template_keyword_p=*/false,
10261 /*check_dependency_p=*/true,
10262 /*template_p=*/NULL,
10263 /*declarator_p=*/false,
10264 /*optional_p=*/false);
10266 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10268 bool non_integral_constant_expression_p = false;
10269 tree id_expression = expr;
10271 const char *error_msg;
10273 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10274 /* Lookup the name we got back from the id-expression. */
10275 expr = cp_parser_lookup_name (parser, expr,
10277 /*is_template=*/false,
10278 /*is_namespace=*/false,
10279 /*check_dependency=*/true,
10280 /*ambiguous_decls=*/NULL,
10281 id_expr_start_token->location);
10284 && expr != error_mark_node
10285 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10286 && TREE_CODE (expr) != TYPE_DECL
10287 && (TREE_CODE (expr) != BIT_NOT_EXPR
10288 || !TYPE_P (TREE_OPERAND (expr, 0)))
10289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10291 /* Complete lookup of the id-expression. */
10292 expr = (finish_id_expression
10293 (id_expression, expr, parser->scope, &idk,
10294 /*integral_constant_expression_p=*/false,
10295 /*allow_non_integral_constant_expression_p=*/true,
10296 &non_integral_constant_expression_p,
10297 /*template_p=*/false,
10299 /*address_p=*/false,
10300 /*template_arg_p=*/false,
10302 id_expr_start_token->location));
10304 if (expr == error_mark_node)
10305 /* We found an id-expression, but it was something that we
10306 should not have found. This is an error, not something
10307 we can recover from, so note that we found an
10308 id-expression and we'll recover as gracefully as
10310 id_expression_or_member_access_p = true;
10314 && expr != error_mark_node
10315 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10316 /* We have an id-expression. */
10317 id_expression_or_member_access_p = true;
10320 if (!id_expression_or_member_access_p)
10322 /* Abort the id-expression parse. */
10323 cp_parser_abort_tentative_parse (parser);
10325 /* Parsing tentatively, again. */
10326 cp_parser_parse_tentatively (parser);
10328 /* Parse a class member access. */
10329 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10331 /*member_access_only_p=*/true, NULL);
10334 && expr != error_mark_node
10335 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10336 /* We have an id-expression. */
10337 id_expression_or_member_access_p = true;
10340 if (id_expression_or_member_access_p)
10341 /* We have parsed the complete id-expression or member access. */
10342 cp_parser_parse_definitely (parser);
10345 bool saved_greater_than_is_operator_p;
10347 /* Abort our attempt to parse an id-expression or member access
10349 cp_parser_abort_tentative_parse (parser);
10351 /* Within a parenthesized expression, a `>' token is always
10352 the greater-than operator. */
10353 saved_greater_than_is_operator_p
10354 = parser->greater_than_is_operator_p;
10355 parser->greater_than_is_operator_p = true;
10357 /* Parse a full expression. */
10358 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10360 /* The `>' token might be the end of a template-id or
10361 template-parameter-list now. */
10362 parser->greater_than_is_operator_p
10363 = saved_greater_than_is_operator_p;
10366 /* Go back to evaluating expressions. */
10367 --cp_unevaluated_operand;
10368 --c_inhibit_evaluation_warnings;
10370 /* Restore the old message and the integral constant expression
10372 parser->type_definition_forbidden_message = saved_message;
10373 parser->integral_constant_expression_p
10374 = saved_integral_constant_expression_p;
10375 parser->non_integral_constant_expression_p
10376 = saved_non_integral_constant_expression_p;
10378 if (expr == error_mark_node)
10380 /* Skip everything up to the closing `)'. */
10381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10382 /*consume_paren=*/true);
10383 return error_mark_node;
10386 /* Parse to the closing `)'. */
10387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10389 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10390 /*consume_paren=*/true);
10391 return error_mark_node;
10394 return finish_decltype_type (expr, id_expression_or_member_access_p);
10397 /* Special member functions [gram.special] */
10399 /* Parse a conversion-function-id.
10401 conversion-function-id:
10402 operator conversion-type-id
10404 Returns an IDENTIFIER_NODE representing the operator. */
10407 cp_parser_conversion_function_id (cp_parser* parser)
10411 tree saved_qualifying_scope;
10412 tree saved_object_scope;
10413 tree pushed_scope = NULL_TREE;
10415 /* Look for the `operator' token. */
10416 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10417 return error_mark_node;
10418 /* When we parse the conversion-type-id, the current scope will be
10419 reset. However, we need that information in able to look up the
10420 conversion function later, so we save it here. */
10421 saved_scope = parser->scope;
10422 saved_qualifying_scope = parser->qualifying_scope;
10423 saved_object_scope = parser->object_scope;
10424 /* We must enter the scope of the class so that the names of
10425 entities declared within the class are available in the
10426 conversion-type-id. For example, consider:
10433 S::operator I() { ... }
10435 In order to see that `I' is a type-name in the definition, we
10436 must be in the scope of `S'. */
10438 pushed_scope = push_scope (saved_scope);
10439 /* Parse the conversion-type-id. */
10440 type = cp_parser_conversion_type_id (parser);
10441 /* Leave the scope of the class, if any. */
10443 pop_scope (pushed_scope);
10444 /* Restore the saved scope. */
10445 parser->scope = saved_scope;
10446 parser->qualifying_scope = saved_qualifying_scope;
10447 parser->object_scope = saved_object_scope;
10448 /* If the TYPE is invalid, indicate failure. */
10449 if (type == error_mark_node)
10450 return error_mark_node;
10451 return mangle_conv_op_name_for_type (type);
10454 /* Parse a conversion-type-id:
10456 conversion-type-id:
10457 type-specifier-seq conversion-declarator [opt]
10459 Returns the TYPE specified. */
10462 cp_parser_conversion_type_id (cp_parser* parser)
10465 cp_decl_specifier_seq type_specifiers;
10466 cp_declarator *declarator;
10467 tree type_specified;
10469 /* Parse the attributes. */
10470 attributes = cp_parser_attributes_opt (parser);
10471 /* Parse the type-specifiers. */
10472 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10473 /*is_trailing_return=*/false,
10475 /* If that didn't work, stop. */
10476 if (type_specifiers.type == error_mark_node)
10477 return error_mark_node;
10478 /* Parse the conversion-declarator. */
10479 declarator = cp_parser_conversion_declarator_opt (parser);
10481 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10482 /*initialized=*/0, &attributes);
10484 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10486 /* Don't give this error when parsing tentatively. This happens to
10487 work because we always parse this definitively once. */
10488 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10489 && type_uses_auto (type_specified))
10491 error ("invalid use of %<auto%> in conversion operator");
10492 return error_mark_node;
10495 return type_specified;
10498 /* Parse an (optional) conversion-declarator.
10500 conversion-declarator:
10501 ptr-operator conversion-declarator [opt]
10505 static cp_declarator *
10506 cp_parser_conversion_declarator_opt (cp_parser* parser)
10508 enum tree_code code;
10510 cp_cv_quals cv_quals;
10512 /* We don't know if there's a ptr-operator next, or not. */
10513 cp_parser_parse_tentatively (parser);
10514 /* Try the ptr-operator. */
10515 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10516 /* If it worked, look for more conversion-declarators. */
10517 if (cp_parser_parse_definitely (parser))
10519 cp_declarator *declarator;
10521 /* Parse another optional declarator. */
10522 declarator = cp_parser_conversion_declarator_opt (parser);
10524 return cp_parser_make_indirect_declarator
10525 (code, class_type, cv_quals, declarator);
10531 /* Parse an (optional) ctor-initializer.
10534 : mem-initializer-list
10536 Returns TRUE iff the ctor-initializer was actually present. */
10539 cp_parser_ctor_initializer_opt (cp_parser* parser)
10541 /* If the next token is not a `:', then there is no
10542 ctor-initializer. */
10543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10545 /* Do default initialization of any bases and members. */
10546 if (DECL_CONSTRUCTOR_P (current_function_decl))
10547 finish_mem_initializers (NULL_TREE);
10552 /* Consume the `:' token. */
10553 cp_lexer_consume_token (parser->lexer);
10554 /* And the mem-initializer-list. */
10555 cp_parser_mem_initializer_list (parser);
10560 /* Parse a mem-initializer-list.
10562 mem-initializer-list:
10563 mem-initializer ... [opt]
10564 mem-initializer ... [opt] , mem-initializer-list */
10567 cp_parser_mem_initializer_list (cp_parser* parser)
10569 tree mem_initializer_list = NULL_TREE;
10570 cp_token *token = cp_lexer_peek_token (parser->lexer);
10572 /* Let the semantic analysis code know that we are starting the
10573 mem-initializer-list. */
10574 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10575 error_at (token->location,
10576 "only constructors take member initializers");
10578 /* Loop through the list. */
10581 tree mem_initializer;
10583 token = cp_lexer_peek_token (parser->lexer);
10584 /* Parse the mem-initializer. */
10585 mem_initializer = cp_parser_mem_initializer (parser);
10586 /* If the next token is a `...', we're expanding member initializers. */
10587 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10589 /* Consume the `...'. */
10590 cp_lexer_consume_token (parser->lexer);
10592 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10593 can be expanded but members cannot. */
10594 if (mem_initializer != error_mark_node
10595 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10597 error_at (token->location,
10598 "cannot expand initializer for member %<%D%>",
10599 TREE_PURPOSE (mem_initializer));
10600 mem_initializer = error_mark_node;
10603 /* Construct the pack expansion type. */
10604 if (mem_initializer != error_mark_node)
10605 mem_initializer = make_pack_expansion (mem_initializer);
10607 /* Add it to the list, unless it was erroneous. */
10608 if (mem_initializer != error_mark_node)
10610 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10611 mem_initializer_list = mem_initializer;
10613 /* If the next token is not a `,', we're done. */
10614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10616 /* Consume the `,' token. */
10617 cp_lexer_consume_token (parser->lexer);
10620 /* Perform semantic analysis. */
10621 if (DECL_CONSTRUCTOR_P (current_function_decl))
10622 finish_mem_initializers (mem_initializer_list);
10625 /* Parse a mem-initializer.
10628 mem-initializer-id ( expression-list [opt] )
10629 mem-initializer-id braced-init-list
10634 ( expression-list [opt] )
10636 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10637 class) or FIELD_DECL (for a non-static data member) to initialize;
10638 the TREE_VALUE is the expression-list. An empty initialization
10639 list is represented by void_list_node. */
10642 cp_parser_mem_initializer (cp_parser* parser)
10644 tree mem_initializer_id;
10645 tree expression_list;
10647 cp_token *token = cp_lexer_peek_token (parser->lexer);
10649 /* Find out what is being initialized. */
10650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10652 permerror (token->location,
10653 "anachronistic old-style base class initializer");
10654 mem_initializer_id = NULL_TREE;
10658 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10659 if (mem_initializer_id == error_mark_node)
10660 return mem_initializer_id;
10662 member = expand_member_init (mem_initializer_id);
10663 if (member && !DECL_P (member))
10664 in_base_initializer = 1;
10666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10668 bool expr_non_constant_p;
10669 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10670 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10671 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10672 expression_list = build_tree_list (NULL_TREE, expression_list);
10677 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10679 /*allow_expansion_p=*/true,
10680 /*non_constant_p=*/NULL);
10682 return error_mark_node;
10683 expression_list = build_tree_list_vec (vec);
10684 release_tree_vector (vec);
10687 if (expression_list == error_mark_node)
10688 return error_mark_node;
10689 if (!expression_list)
10690 expression_list = void_type_node;
10692 in_base_initializer = 0;
10694 return member ? build_tree_list (member, expression_list) : error_mark_node;
10697 /* Parse a mem-initializer-id.
10699 mem-initializer-id:
10700 :: [opt] nested-name-specifier [opt] class-name
10703 Returns a TYPE indicating the class to be initializer for the first
10704 production. Returns an IDENTIFIER_NODE indicating the data member
10705 to be initialized for the second production. */
10708 cp_parser_mem_initializer_id (cp_parser* parser)
10710 bool global_scope_p;
10711 bool nested_name_specifier_p;
10712 bool template_p = false;
10715 cp_token *token = cp_lexer_peek_token (parser->lexer);
10717 /* `typename' is not allowed in this context ([temp.res]). */
10718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10720 error_at (token->location,
10721 "keyword %<typename%> not allowed in this context (a qualified "
10722 "member initializer is implicitly a type)");
10723 cp_lexer_consume_token (parser->lexer);
10725 /* Look for the optional `::' operator. */
10727 = (cp_parser_global_scope_opt (parser,
10728 /*current_scope_valid_p=*/false)
10730 /* Look for the optional nested-name-specifier. The simplest way to
10735 The keyword `typename' is not permitted in a base-specifier or
10736 mem-initializer; in these contexts a qualified name that
10737 depends on a template-parameter is implicitly assumed to be a
10740 is to assume that we have seen the `typename' keyword at this
10742 nested_name_specifier_p
10743 = (cp_parser_nested_name_specifier_opt (parser,
10744 /*typename_keyword_p=*/true,
10745 /*check_dependency_p=*/true,
10747 /*is_declaration=*/true)
10749 if (nested_name_specifier_p)
10750 template_p = cp_parser_optional_template_keyword (parser);
10751 /* If there is a `::' operator or a nested-name-specifier, then we
10752 are definitely looking for a class-name. */
10753 if (global_scope_p || nested_name_specifier_p)
10754 return cp_parser_class_name (parser,
10755 /*typename_keyword_p=*/true,
10756 /*template_keyword_p=*/template_p,
10758 /*check_dependency_p=*/true,
10759 /*class_head_p=*/false,
10760 /*is_declaration=*/true);
10761 /* Otherwise, we could also be looking for an ordinary identifier. */
10762 cp_parser_parse_tentatively (parser);
10763 /* Try a class-name. */
10764 id = cp_parser_class_name (parser,
10765 /*typename_keyword_p=*/true,
10766 /*template_keyword_p=*/false,
10768 /*check_dependency_p=*/true,
10769 /*class_head_p=*/false,
10770 /*is_declaration=*/true);
10771 /* If we found one, we're done. */
10772 if (cp_parser_parse_definitely (parser))
10774 /* Otherwise, look for an ordinary identifier. */
10775 return cp_parser_identifier (parser);
10778 /* Overloading [gram.over] */
10780 /* Parse an operator-function-id.
10782 operator-function-id:
10785 Returns an IDENTIFIER_NODE for the operator which is a
10786 human-readable spelling of the identifier, e.g., `operator +'. */
10789 cp_parser_operator_function_id (cp_parser* parser)
10791 /* Look for the `operator' keyword. */
10792 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10793 return error_mark_node;
10794 /* And then the name of the operator itself. */
10795 return cp_parser_operator (parser);
10798 /* Parse an operator.
10801 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10802 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10803 || ++ -- , ->* -> () []
10810 Returns an IDENTIFIER_NODE for the operator which is a
10811 human-readable spelling of the identifier, e.g., `operator +'. */
10814 cp_parser_operator (cp_parser* parser)
10816 tree id = NULL_TREE;
10819 /* Peek at the next token. */
10820 token = cp_lexer_peek_token (parser->lexer);
10821 /* Figure out which operator we have. */
10822 switch (token->type)
10828 /* The keyword should be either `new' or `delete'. */
10829 if (token->keyword == RID_NEW)
10831 else if (token->keyword == RID_DELETE)
10836 /* Consume the `new' or `delete' token. */
10837 cp_lexer_consume_token (parser->lexer);
10839 /* Peek at the next token. */
10840 token = cp_lexer_peek_token (parser->lexer);
10841 /* If it's a `[' token then this is the array variant of the
10843 if (token->type == CPP_OPEN_SQUARE)
10845 /* Consume the `[' token. */
10846 cp_lexer_consume_token (parser->lexer);
10847 /* Look for the `]' token. */
10848 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10849 id = ansi_opname (op == NEW_EXPR
10850 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10852 /* Otherwise, we have the non-array variant. */
10854 id = ansi_opname (op);
10860 id = ansi_opname (PLUS_EXPR);
10864 id = ansi_opname (MINUS_EXPR);
10868 id = ansi_opname (MULT_EXPR);
10872 id = ansi_opname (TRUNC_DIV_EXPR);
10876 id = ansi_opname (TRUNC_MOD_EXPR);
10880 id = ansi_opname (BIT_XOR_EXPR);
10884 id = ansi_opname (BIT_AND_EXPR);
10888 id = ansi_opname (BIT_IOR_EXPR);
10892 id = ansi_opname (BIT_NOT_EXPR);
10896 id = ansi_opname (TRUTH_NOT_EXPR);
10900 id = ansi_assopname (NOP_EXPR);
10904 id = ansi_opname (LT_EXPR);
10908 id = ansi_opname (GT_EXPR);
10912 id = ansi_assopname (PLUS_EXPR);
10916 id = ansi_assopname (MINUS_EXPR);
10920 id = ansi_assopname (MULT_EXPR);
10924 id = ansi_assopname (TRUNC_DIV_EXPR);
10928 id = ansi_assopname (TRUNC_MOD_EXPR);
10932 id = ansi_assopname (BIT_XOR_EXPR);
10936 id = ansi_assopname (BIT_AND_EXPR);
10940 id = ansi_assopname (BIT_IOR_EXPR);
10944 id = ansi_opname (LSHIFT_EXPR);
10948 id = ansi_opname (RSHIFT_EXPR);
10951 case CPP_LSHIFT_EQ:
10952 id = ansi_assopname (LSHIFT_EXPR);
10955 case CPP_RSHIFT_EQ:
10956 id = ansi_assopname (RSHIFT_EXPR);
10960 id = ansi_opname (EQ_EXPR);
10964 id = ansi_opname (NE_EXPR);
10968 id = ansi_opname (LE_EXPR);
10971 case CPP_GREATER_EQ:
10972 id = ansi_opname (GE_EXPR);
10976 id = ansi_opname (TRUTH_ANDIF_EXPR);
10980 id = ansi_opname (TRUTH_ORIF_EXPR);
10983 case CPP_PLUS_PLUS:
10984 id = ansi_opname (POSTINCREMENT_EXPR);
10987 case CPP_MINUS_MINUS:
10988 id = ansi_opname (PREDECREMENT_EXPR);
10992 id = ansi_opname (COMPOUND_EXPR);
10995 case CPP_DEREF_STAR:
10996 id = ansi_opname (MEMBER_REF);
11000 id = ansi_opname (COMPONENT_REF);
11003 case CPP_OPEN_PAREN:
11004 /* Consume the `('. */
11005 cp_lexer_consume_token (parser->lexer);
11006 /* Look for the matching `)'. */
11007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11008 return ansi_opname (CALL_EXPR);
11010 case CPP_OPEN_SQUARE:
11011 /* Consume the `['. */
11012 cp_lexer_consume_token (parser->lexer);
11013 /* Look for the matching `]'. */
11014 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11015 return ansi_opname (ARRAY_REF);
11018 /* Anything else is an error. */
11022 /* If we have selected an identifier, we need to consume the
11025 cp_lexer_consume_token (parser->lexer);
11026 /* Otherwise, no valid operator name was present. */
11029 cp_parser_error (parser, "expected operator");
11030 id = error_mark_node;
11036 /* Parse a template-declaration.
11038 template-declaration:
11039 export [opt] template < template-parameter-list > declaration
11041 If MEMBER_P is TRUE, this template-declaration occurs within a
11044 The grammar rule given by the standard isn't correct. What
11045 is really meant is:
11047 template-declaration:
11048 export [opt] template-parameter-list-seq
11049 decl-specifier-seq [opt] init-declarator [opt] ;
11050 export [opt] template-parameter-list-seq
11051 function-definition
11053 template-parameter-list-seq:
11054 template-parameter-list-seq [opt]
11055 template < template-parameter-list > */
11058 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11060 /* Check for `export'. */
11061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11063 /* Consume the `export' token. */
11064 cp_lexer_consume_token (parser->lexer);
11065 /* Warn that we do not support `export'. */
11066 warning (0, "keyword %<export%> not implemented, and will be ignored");
11069 cp_parser_template_declaration_after_export (parser, member_p);
11072 /* Parse a template-parameter-list.
11074 template-parameter-list:
11076 template-parameter-list , template-parameter
11078 Returns a TREE_LIST. Each node represents a template parameter.
11079 The nodes are connected via their TREE_CHAINs. */
11082 cp_parser_template_parameter_list (cp_parser* parser)
11084 tree parameter_list = NULL_TREE;
11086 begin_template_parm_list ();
11088 /* The loop below parses the template parms. We first need to know
11089 the total number of template parms to be able to compute proper
11090 canonical types of each dependent type. So after the loop, when
11091 we know the total number of template parms,
11092 end_template_parm_list computes the proper canonical types and
11093 fixes up the dependent types accordingly. */
11098 bool is_parameter_pack;
11099 location_t parm_loc;
11101 /* Parse the template-parameter. */
11102 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11103 parameter = cp_parser_template_parameter (parser,
11105 &is_parameter_pack);
11106 /* Add it to the list. */
11107 if (parameter != error_mark_node)
11108 parameter_list = process_template_parm (parameter_list,
11116 tree err_parm = build_tree_list (parameter, parameter);
11117 parameter_list = chainon (parameter_list, err_parm);
11120 /* If the next token is not a `,', we're done. */
11121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11123 /* Otherwise, consume the `,' token. */
11124 cp_lexer_consume_token (parser->lexer);
11127 return end_template_parm_list (parameter_list);
11130 /* Parse a template-parameter.
11132 template-parameter:
11134 parameter-declaration
11136 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11137 the parameter. The TREE_PURPOSE is the default value, if any.
11138 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11139 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11140 set to true iff this parameter is a parameter pack. */
11143 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11144 bool *is_parameter_pack)
11147 cp_parameter_declarator *parameter_declarator;
11148 cp_declarator *id_declarator;
11151 /* Assume it is a type parameter or a template parameter. */
11152 *is_non_type = false;
11153 /* Assume it not a parameter pack. */
11154 *is_parameter_pack = false;
11155 /* Peek at the next token. */
11156 token = cp_lexer_peek_token (parser->lexer);
11157 /* If it is `class' or `template', we have a type-parameter. */
11158 if (token->keyword == RID_TEMPLATE)
11159 return cp_parser_type_parameter (parser, is_parameter_pack);
11160 /* If it is `class' or `typename' we do not know yet whether it is a
11161 type parameter or a non-type parameter. Consider:
11163 template <typename T, typename T::X X> ...
11167 template <class C, class D*> ...
11169 Here, the first parameter is a type parameter, and the second is
11170 a non-type parameter. We can tell by looking at the token after
11171 the identifier -- if it is a `,', `=', or `>' then we have a type
11173 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11175 /* Peek at the token after `class' or `typename'. */
11176 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11177 /* If it's an ellipsis, we have a template type parameter
11179 if (token->type == CPP_ELLIPSIS)
11180 return cp_parser_type_parameter (parser, is_parameter_pack);
11181 /* If it's an identifier, skip it. */
11182 if (token->type == CPP_NAME)
11183 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11184 /* Now, see if the token looks like the end of a template
11186 if (token->type == CPP_COMMA
11187 || token->type == CPP_EQ
11188 || token->type == CPP_GREATER)
11189 return cp_parser_type_parameter (parser, is_parameter_pack);
11192 /* Otherwise, it is a non-type parameter.
11196 When parsing a default template-argument for a non-type
11197 template-parameter, the first non-nested `>' is taken as the end
11198 of the template parameter-list rather than a greater-than
11200 *is_non_type = true;
11201 parameter_declarator
11202 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11203 /*parenthesized_p=*/NULL);
11205 /* If the parameter declaration is marked as a parameter pack, set
11206 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11207 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11209 if (parameter_declarator
11210 && parameter_declarator->declarator
11211 && parameter_declarator->declarator->parameter_pack_p)
11213 *is_parameter_pack = true;
11214 parameter_declarator->declarator->parameter_pack_p = false;
11217 /* If the next token is an ellipsis, and we don't already have it
11218 marked as a parameter pack, then we have a parameter pack (that
11219 has no declarator). */
11220 if (!*is_parameter_pack
11221 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11222 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11224 /* Consume the `...'. */
11225 cp_lexer_consume_token (parser->lexer);
11226 maybe_warn_variadic_templates ();
11228 *is_parameter_pack = true;
11230 /* We might end up with a pack expansion as the type of the non-type
11231 template parameter, in which case this is a non-type template
11233 else if (parameter_declarator
11234 && parameter_declarator->decl_specifiers.type
11235 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11237 *is_parameter_pack = true;
11238 parameter_declarator->decl_specifiers.type =
11239 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11242 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11244 /* Parameter packs cannot have default arguments. However, a
11245 user may try to do so, so we'll parse them and give an
11246 appropriate diagnostic here. */
11248 /* Consume the `='. */
11249 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11250 cp_lexer_consume_token (parser->lexer);
11252 /* Find the name of the parameter pack. */
11253 id_declarator = parameter_declarator->declarator;
11254 while (id_declarator && id_declarator->kind != cdk_id)
11255 id_declarator = id_declarator->declarator;
11257 if (id_declarator && id_declarator->kind == cdk_id)
11258 error_at (start_token->location,
11259 "template parameter pack %qD cannot have a default argument",
11260 id_declarator->u.id.unqualified_name);
11262 error_at (start_token->location,
11263 "template parameter pack cannot have a default argument");
11265 /* Parse the default argument, but throw away the result. */
11266 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11269 parm = grokdeclarator (parameter_declarator->declarator,
11270 ¶meter_declarator->decl_specifiers,
11271 TPARM, /*initialized=*/0,
11272 /*attrlist=*/NULL);
11273 if (parm == error_mark_node)
11274 return error_mark_node;
11276 return build_tree_list (parameter_declarator->default_argument, parm);
11279 /* Parse a type-parameter.
11282 class identifier [opt]
11283 class identifier [opt] = type-id
11284 typename identifier [opt]
11285 typename identifier [opt] = type-id
11286 template < template-parameter-list > class identifier [opt]
11287 template < template-parameter-list > class identifier [opt]
11290 GNU Extension (variadic templates):
11293 class ... identifier [opt]
11294 typename ... identifier [opt]
11296 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11297 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11298 the declaration of the parameter.
11300 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11303 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11308 /* Look for a keyword to tell us what kind of parameter this is. */
11309 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11311 return error_mark_node;
11313 switch (token->keyword)
11319 tree default_argument;
11321 /* If the next token is an ellipsis, we have a template
11323 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11325 /* Consume the `...' token. */
11326 cp_lexer_consume_token (parser->lexer);
11327 maybe_warn_variadic_templates ();
11329 *is_parameter_pack = true;
11332 /* If the next token is an identifier, then it names the
11334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11335 identifier = cp_parser_identifier (parser);
11337 identifier = NULL_TREE;
11339 /* Create the parameter. */
11340 parameter = finish_template_type_parm (class_type_node, identifier);
11342 /* If the next token is an `=', we have a default argument. */
11343 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11345 /* Consume the `=' token. */
11346 cp_lexer_consume_token (parser->lexer);
11347 /* Parse the default-argument. */
11348 push_deferring_access_checks (dk_no_deferred);
11349 default_argument = cp_parser_type_id (parser);
11351 /* Template parameter packs cannot have default
11353 if (*is_parameter_pack)
11356 error_at (token->location,
11357 "template parameter pack %qD cannot have a "
11358 "default argument", identifier);
11360 error_at (token->location,
11361 "template parameter packs cannot have "
11362 "default arguments");
11363 default_argument = NULL_TREE;
11365 pop_deferring_access_checks ();
11368 default_argument = NULL_TREE;
11370 /* Create the combined representation of the parameter and the
11371 default argument. */
11372 parameter = build_tree_list (default_argument, parameter);
11379 tree default_argument;
11381 /* Look for the `<'. */
11382 cp_parser_require (parser, CPP_LESS, RT_LESS);
11383 /* Parse the template-parameter-list. */
11384 cp_parser_template_parameter_list (parser);
11385 /* Look for the `>'. */
11386 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11387 /* Look for the `class' keyword. */
11388 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11389 /* If the next token is an ellipsis, we have a template
11391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11393 /* Consume the `...' token. */
11394 cp_lexer_consume_token (parser->lexer);
11395 maybe_warn_variadic_templates ();
11397 *is_parameter_pack = true;
11399 /* If the next token is an `=', then there is a
11400 default-argument. If the next token is a `>', we are at
11401 the end of the parameter-list. If the next token is a `,',
11402 then we are at the end of this parameter. */
11403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11404 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11405 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11407 identifier = cp_parser_identifier (parser);
11408 /* Treat invalid names as if the parameter were nameless. */
11409 if (identifier == error_mark_node)
11410 identifier = NULL_TREE;
11413 identifier = NULL_TREE;
11415 /* Create the template parameter. */
11416 parameter = finish_template_template_parm (class_type_node,
11419 /* If the next token is an `=', then there is a
11420 default-argument. */
11421 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11425 /* Consume the `='. */
11426 cp_lexer_consume_token (parser->lexer);
11427 /* Parse the id-expression. */
11428 push_deferring_access_checks (dk_no_deferred);
11429 /* save token before parsing the id-expression, for error
11431 token = cp_lexer_peek_token (parser->lexer);
11433 = cp_parser_id_expression (parser,
11434 /*template_keyword_p=*/false,
11435 /*check_dependency_p=*/true,
11436 /*template_p=*/&is_template,
11437 /*declarator_p=*/false,
11438 /*optional_p=*/false);
11439 if (TREE_CODE (default_argument) == TYPE_DECL)
11440 /* If the id-expression was a template-id that refers to
11441 a template-class, we already have the declaration here,
11442 so no further lookup is needed. */
11445 /* Look up the name. */
11447 = cp_parser_lookup_name (parser, default_argument,
11449 /*is_template=*/is_template,
11450 /*is_namespace=*/false,
11451 /*check_dependency=*/true,
11452 /*ambiguous_decls=*/NULL,
11454 /* See if the default argument is valid. */
11456 = check_template_template_default_arg (default_argument);
11458 /* Template parameter packs cannot have default
11460 if (*is_parameter_pack)
11463 error_at (token->location,
11464 "template parameter pack %qD cannot "
11465 "have a default argument",
11468 error_at (token->location, "template parameter packs cannot "
11469 "have default arguments");
11470 default_argument = NULL_TREE;
11472 pop_deferring_access_checks ();
11475 default_argument = NULL_TREE;
11477 /* Create the combined representation of the parameter and the
11478 default argument. */
11479 parameter = build_tree_list (default_argument, parameter);
11484 gcc_unreachable ();
11491 /* Parse a template-id.
11494 template-name < template-argument-list [opt] >
11496 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11497 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11498 returned. Otherwise, if the template-name names a function, or set
11499 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11500 names a class, returns a TYPE_DECL for the specialization.
11502 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11503 uninstantiated templates. */
11506 cp_parser_template_id (cp_parser *parser,
11507 bool template_keyword_p,
11508 bool check_dependency_p,
11509 bool is_declaration)
11515 cp_token_position start_of_id = 0;
11516 deferred_access_check *chk;
11517 VEC (deferred_access_check,gc) *access_check;
11518 cp_token *next_token = NULL, *next_token_2 = NULL;
11519 bool is_identifier;
11521 /* If the next token corresponds to a template-id, there is no need
11523 next_token = cp_lexer_peek_token (parser->lexer);
11524 if (next_token->type == CPP_TEMPLATE_ID)
11526 struct tree_check *check_value;
11528 /* Get the stored value. */
11529 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11530 /* Perform any access checks that were deferred. */
11531 access_check = check_value->checks;
11534 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11535 perform_or_defer_access_check (chk->binfo,
11539 /* Return the stored value. */
11540 return check_value->value;
11543 /* Avoid performing name lookup if there is no possibility of
11544 finding a template-id. */
11545 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11546 || (next_token->type == CPP_NAME
11547 && !cp_parser_nth_token_starts_template_argument_list_p
11550 cp_parser_error (parser, "expected template-id");
11551 return error_mark_node;
11554 /* Remember where the template-id starts. */
11555 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11556 start_of_id = cp_lexer_token_position (parser->lexer, false);
11558 push_deferring_access_checks (dk_deferred);
11560 /* Parse the template-name. */
11561 is_identifier = false;
11562 templ = cp_parser_template_name (parser, template_keyword_p,
11563 check_dependency_p,
11566 if (templ == error_mark_node || is_identifier)
11568 pop_deferring_access_checks ();
11572 /* If we find the sequence `[:' after a template-name, it's probably
11573 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11574 parse correctly the argument list. */
11575 next_token = cp_lexer_peek_token (parser->lexer);
11576 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11577 if (next_token->type == CPP_OPEN_SQUARE
11578 && next_token->flags & DIGRAPH
11579 && next_token_2->type == CPP_COLON
11580 && !(next_token_2->flags & PREV_WHITE))
11582 cp_parser_parse_tentatively (parser);
11583 /* Change `:' into `::'. */
11584 next_token_2->type = CPP_SCOPE;
11585 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11587 cp_lexer_consume_token (parser->lexer);
11589 /* Parse the arguments. */
11590 arguments = cp_parser_enclosed_template_argument_list (parser);
11591 if (!cp_parser_parse_definitely (parser))
11593 /* If we couldn't parse an argument list, then we revert our changes
11594 and return simply an error. Maybe this is not a template-id
11596 next_token_2->type = CPP_COLON;
11597 cp_parser_error (parser, "expected %<<%>");
11598 pop_deferring_access_checks ();
11599 return error_mark_node;
11601 /* Otherwise, emit an error about the invalid digraph, but continue
11602 parsing because we got our argument list. */
11603 if (permerror (next_token->location,
11604 "%<<::%> cannot begin a template-argument list"))
11606 static bool hint = false;
11607 inform (next_token->location,
11608 "%<<:%> is an alternate spelling for %<[%>."
11609 " Insert whitespace between %<<%> and %<::%>");
11610 if (!hint && !flag_permissive)
11612 inform (next_token->location, "(if you use %<-fpermissive%>"
11613 " G++ will accept your code)");
11620 /* Look for the `<' that starts the template-argument-list. */
11621 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11623 pop_deferring_access_checks ();
11624 return error_mark_node;
11626 /* Parse the arguments. */
11627 arguments = cp_parser_enclosed_template_argument_list (parser);
11630 /* Build a representation of the specialization. */
11631 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11632 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11633 else if (DECL_CLASS_TEMPLATE_P (templ)
11634 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11636 bool entering_scope;
11637 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11638 template (rather than some instantiation thereof) only if
11639 is not nested within some other construct. For example, in
11640 "template <typename T> void f(T) { A<T>::", A<T> is just an
11641 instantiation of A. */
11642 entering_scope = (template_parm_scope_p ()
11643 && cp_lexer_next_token_is (parser->lexer,
11646 = finish_template_type (templ, arguments, entering_scope);
11650 /* If it's not a class-template or a template-template, it should be
11651 a function-template. */
11652 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11653 || TREE_CODE (templ) == OVERLOAD
11654 || BASELINK_P (templ)));
11656 template_id = lookup_template_function (templ, arguments);
11659 /* If parsing tentatively, replace the sequence of tokens that makes
11660 up the template-id with a CPP_TEMPLATE_ID token. That way,
11661 should we re-parse the token stream, we will not have to repeat
11662 the effort required to do the parse, nor will we issue duplicate
11663 error messages about problems during instantiation of the
11667 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11669 /* Reset the contents of the START_OF_ID token. */
11670 token->type = CPP_TEMPLATE_ID;
11671 /* Retrieve any deferred checks. Do not pop this access checks yet
11672 so the memory will not be reclaimed during token replacing below. */
11673 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11674 token->u.tree_check_value->value = template_id;
11675 token->u.tree_check_value->checks = get_deferred_access_checks ();
11676 token->keyword = RID_MAX;
11678 /* Purge all subsequent tokens. */
11679 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11681 /* ??? Can we actually assume that, if template_id ==
11682 error_mark_node, we will have issued a diagnostic to the
11683 user, as opposed to simply marking the tentative parse as
11685 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11686 error_at (token->location, "parse error in template argument list");
11689 pop_deferring_access_checks ();
11690 return template_id;
11693 /* Parse a template-name.
11698 The standard should actually say:
11702 operator-function-id
11704 A defect report has been filed about this issue.
11706 A conversion-function-id cannot be a template name because they cannot
11707 be part of a template-id. In fact, looking at this code:
11709 a.operator K<int>()
11711 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11712 It is impossible to call a templated conversion-function-id with an
11713 explicit argument list, since the only allowed template parameter is
11714 the type to which it is converting.
11716 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11717 `template' keyword, in a construction like:
11721 In that case `f' is taken to be a template-name, even though there
11722 is no way of knowing for sure.
11724 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11725 name refers to a set of overloaded functions, at least one of which
11726 is a template, or an IDENTIFIER_NODE with the name of the template,
11727 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11728 names are looked up inside uninstantiated templates. */
11731 cp_parser_template_name (cp_parser* parser,
11732 bool template_keyword_p,
11733 bool check_dependency_p,
11734 bool is_declaration,
11735 bool *is_identifier)
11740 cp_token *token = cp_lexer_peek_token (parser->lexer);
11742 /* If the next token is `operator', then we have either an
11743 operator-function-id or a conversion-function-id. */
11744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11746 /* We don't know whether we're looking at an
11747 operator-function-id or a conversion-function-id. */
11748 cp_parser_parse_tentatively (parser);
11749 /* Try an operator-function-id. */
11750 identifier = cp_parser_operator_function_id (parser);
11751 /* If that didn't work, try a conversion-function-id. */
11752 if (!cp_parser_parse_definitely (parser))
11754 cp_parser_error (parser, "expected template-name");
11755 return error_mark_node;
11758 /* Look for the identifier. */
11760 identifier = cp_parser_identifier (parser);
11762 /* If we didn't find an identifier, we don't have a template-id. */
11763 if (identifier == error_mark_node)
11764 return error_mark_node;
11766 /* If the name immediately followed the `template' keyword, then it
11767 is a template-name. However, if the next token is not `<', then
11768 we do not treat it as a template-name, since it is not being used
11769 as part of a template-id. This enables us to handle constructs
11772 template <typename T> struct S { S(); };
11773 template <typename T> S<T>::S();
11775 correctly. We would treat `S' as a template -- if it were `S<T>'
11776 -- but we do not if there is no `<'. */
11778 if (processing_template_decl
11779 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11781 /* In a declaration, in a dependent context, we pretend that the
11782 "template" keyword was present in order to improve error
11783 recovery. For example, given:
11785 template <typename T> void f(T::X<int>);
11787 we want to treat "X<int>" as a template-id. */
11789 && !template_keyword_p
11790 && parser->scope && TYPE_P (parser->scope)
11791 && check_dependency_p
11792 && dependent_scope_p (parser->scope)
11793 /* Do not do this for dtors (or ctors), since they never
11794 need the template keyword before their name. */
11795 && !constructor_name_p (identifier, parser->scope))
11797 cp_token_position start = 0;
11799 /* Explain what went wrong. */
11800 error_at (token->location, "non-template %qD used as template",
11802 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11803 parser->scope, identifier);
11804 /* If parsing tentatively, find the location of the "<" token. */
11805 if (cp_parser_simulate_error (parser))
11806 start = cp_lexer_token_position (parser->lexer, true);
11807 /* Parse the template arguments so that we can issue error
11808 messages about them. */
11809 cp_lexer_consume_token (parser->lexer);
11810 cp_parser_enclosed_template_argument_list (parser);
11811 /* Skip tokens until we find a good place from which to
11812 continue parsing. */
11813 cp_parser_skip_to_closing_parenthesis (parser,
11814 /*recovering=*/true,
11816 /*consume_paren=*/false);
11817 /* If parsing tentatively, permanently remove the
11818 template argument list. That will prevent duplicate
11819 error messages from being issued about the missing
11820 "template" keyword. */
11822 cp_lexer_purge_tokens_after (parser->lexer, start);
11824 *is_identifier = true;
11828 /* If the "template" keyword is present, then there is generally
11829 no point in doing name-lookup, so we just return IDENTIFIER.
11830 But, if the qualifying scope is non-dependent then we can
11831 (and must) do name-lookup normally. */
11832 if (template_keyword_p
11834 || (TYPE_P (parser->scope)
11835 && dependent_type_p (parser->scope))))
11839 /* Look up the name. */
11840 decl = cp_parser_lookup_name (parser, identifier,
11842 /*is_template=*/true,
11843 /*is_namespace=*/false,
11844 check_dependency_p,
11845 /*ambiguous_decls=*/NULL,
11848 /* If DECL is a template, then the name was a template-name. */
11849 if (TREE_CODE (decl) == TEMPLATE_DECL)
11853 tree fn = NULL_TREE;
11855 /* The standard does not explicitly indicate whether a name that
11856 names a set of overloaded declarations, some of which are
11857 templates, is a template-name. However, such a name should
11858 be a template-name; otherwise, there is no way to form a
11859 template-id for the overloaded templates. */
11860 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11861 if (TREE_CODE (fns) == OVERLOAD)
11862 for (fn = fns; fn; fn = OVL_NEXT (fn))
11863 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11868 /* The name does not name a template. */
11869 cp_parser_error (parser, "expected template-name");
11870 return error_mark_node;
11874 /* If DECL is dependent, and refers to a function, then just return
11875 its name; we will look it up again during template instantiation. */
11876 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11878 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11879 if (TYPE_P (scope) && dependent_type_p (scope))
11886 /* Parse a template-argument-list.
11888 template-argument-list:
11889 template-argument ... [opt]
11890 template-argument-list , template-argument ... [opt]
11892 Returns a TREE_VEC containing the arguments. */
11895 cp_parser_template_argument_list (cp_parser* parser)
11897 tree fixed_args[10];
11898 unsigned n_args = 0;
11899 unsigned alloced = 10;
11900 tree *arg_ary = fixed_args;
11902 bool saved_in_template_argument_list_p;
11904 bool saved_non_ice_p;
11906 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11907 parser->in_template_argument_list_p = true;
11908 /* Even if the template-id appears in an integral
11909 constant-expression, the contents of the argument list do
11911 saved_ice_p = parser->integral_constant_expression_p;
11912 parser->integral_constant_expression_p = false;
11913 saved_non_ice_p = parser->non_integral_constant_expression_p;
11914 parser->non_integral_constant_expression_p = false;
11915 /* Parse the arguments. */
11921 /* Consume the comma. */
11922 cp_lexer_consume_token (parser->lexer);
11924 /* Parse the template-argument. */
11925 argument = cp_parser_template_argument (parser);
11927 /* If the next token is an ellipsis, we're expanding a template
11929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11931 if (argument == error_mark_node)
11933 cp_token *token = cp_lexer_peek_token (parser->lexer);
11934 error_at (token->location,
11935 "expected parameter pack before %<...%>");
11937 /* Consume the `...' token. */
11938 cp_lexer_consume_token (parser->lexer);
11940 /* Make the argument into a TYPE_PACK_EXPANSION or
11941 EXPR_PACK_EXPANSION. */
11942 argument = make_pack_expansion (argument);
11945 if (n_args == alloced)
11949 if (arg_ary == fixed_args)
11951 arg_ary = XNEWVEC (tree, alloced);
11952 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11955 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11957 arg_ary[n_args++] = argument;
11959 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11961 vec = make_tree_vec (n_args);
11964 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11966 if (arg_ary != fixed_args)
11968 parser->non_integral_constant_expression_p = saved_non_ice_p;
11969 parser->integral_constant_expression_p = saved_ice_p;
11970 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11971 #ifdef ENABLE_CHECKING
11972 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11977 /* Parse a template-argument.
11980 assignment-expression
11984 The representation is that of an assignment-expression, type-id, or
11985 id-expression -- except that the qualified id-expression is
11986 evaluated, so that the value returned is either a DECL or an
11989 Although the standard says "assignment-expression", it forbids
11990 throw-expressions or assignments in the template argument.
11991 Therefore, we use "conditional-expression" instead. */
11994 cp_parser_template_argument (cp_parser* parser)
11999 bool maybe_type_id = false;
12000 cp_token *token = NULL, *argument_start_token = NULL;
12003 /* There's really no way to know what we're looking at, so we just
12004 try each alternative in order.
12008 In a template-argument, an ambiguity between a type-id and an
12009 expression is resolved to a type-id, regardless of the form of
12010 the corresponding template-parameter.
12012 Therefore, we try a type-id first. */
12013 cp_parser_parse_tentatively (parser);
12014 argument = cp_parser_template_type_arg (parser);
12015 /* If there was no error parsing the type-id but the next token is a
12016 '>>', our behavior depends on which dialect of C++ we're
12017 parsing. In C++98, we probably found a typo for '> >'. But there
12018 are type-id which are also valid expressions. For instance:
12020 struct X { int operator >> (int); };
12021 template <int V> struct Foo {};
12024 Here 'X()' is a valid type-id of a function type, but the user just
12025 wanted to write the expression "X() >> 5". Thus, we remember that we
12026 found a valid type-id, but we still try to parse the argument as an
12027 expression to see what happens.
12029 In C++0x, the '>>' will be considered two separate '>'
12031 if (!cp_parser_error_occurred (parser)
12032 && cxx_dialect == cxx98
12033 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12035 maybe_type_id = true;
12036 cp_parser_abort_tentative_parse (parser);
12040 /* If the next token isn't a `,' or a `>', then this argument wasn't
12041 really finished. This means that the argument is not a valid
12043 if (!cp_parser_next_token_ends_template_argument_p (parser))
12044 cp_parser_error (parser, "expected template-argument");
12045 /* If that worked, we're done. */
12046 if (cp_parser_parse_definitely (parser))
12049 /* We're still not sure what the argument will be. */
12050 cp_parser_parse_tentatively (parser);
12051 /* Try a template. */
12052 argument_start_token = cp_lexer_peek_token (parser->lexer);
12053 argument = cp_parser_id_expression (parser,
12054 /*template_keyword_p=*/false,
12055 /*check_dependency_p=*/true,
12057 /*declarator_p=*/false,
12058 /*optional_p=*/false);
12059 /* If the next token isn't a `,' or a `>', then this argument wasn't
12060 really finished. */
12061 if (!cp_parser_next_token_ends_template_argument_p (parser))
12062 cp_parser_error (parser, "expected template-argument");
12063 if (!cp_parser_error_occurred (parser))
12065 /* Figure out what is being referred to. If the id-expression
12066 was for a class template specialization, then we will have a
12067 TYPE_DECL at this point. There is no need to do name lookup
12068 at this point in that case. */
12069 if (TREE_CODE (argument) != TYPE_DECL)
12070 argument = cp_parser_lookup_name (parser, argument,
12072 /*is_template=*/template_p,
12073 /*is_namespace=*/false,
12074 /*check_dependency=*/true,
12075 /*ambiguous_decls=*/NULL,
12076 argument_start_token->location);
12077 if (TREE_CODE (argument) != TEMPLATE_DECL
12078 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12079 cp_parser_error (parser, "expected template-name");
12081 if (cp_parser_parse_definitely (parser))
12083 /* It must be a non-type argument. There permitted cases are given
12084 in [temp.arg.nontype]:
12086 -- an integral constant-expression of integral or enumeration
12089 -- the name of a non-type template-parameter; or
12091 -- the name of an object or function with external linkage...
12093 -- the address of an object or function with external linkage...
12095 -- a pointer to member... */
12096 /* Look for a non-type template parameter. */
12097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12099 cp_parser_parse_tentatively (parser);
12100 argument = cp_parser_primary_expression (parser,
12101 /*address_p=*/false,
12103 /*template_arg_p=*/true,
12105 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12106 || !cp_parser_next_token_ends_template_argument_p (parser))
12107 cp_parser_simulate_error (parser);
12108 if (cp_parser_parse_definitely (parser))
12112 /* If the next token is "&", the argument must be the address of an
12113 object or function with external linkage. */
12114 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12116 cp_lexer_consume_token (parser->lexer);
12117 /* See if we might have an id-expression. */
12118 token = cp_lexer_peek_token (parser->lexer);
12119 if (token->type == CPP_NAME
12120 || token->keyword == RID_OPERATOR
12121 || token->type == CPP_SCOPE
12122 || token->type == CPP_TEMPLATE_ID
12123 || token->type == CPP_NESTED_NAME_SPECIFIER)
12125 cp_parser_parse_tentatively (parser);
12126 argument = cp_parser_primary_expression (parser,
12129 /*template_arg_p=*/true,
12131 if (cp_parser_error_occurred (parser)
12132 || !cp_parser_next_token_ends_template_argument_p (parser))
12133 cp_parser_abort_tentative_parse (parser);
12138 if (TREE_CODE (argument) == INDIRECT_REF)
12140 gcc_assert (REFERENCE_REF_P (argument));
12141 argument = TREE_OPERAND (argument, 0);
12144 /* If we're in a template, we represent a qualified-id referring
12145 to a static data member as a SCOPE_REF even if the scope isn't
12146 dependent so that we can check access control later. */
12148 if (TREE_CODE (probe) == SCOPE_REF)
12149 probe = TREE_OPERAND (probe, 1);
12150 if (TREE_CODE (probe) == VAR_DECL)
12152 /* A variable without external linkage might still be a
12153 valid constant-expression, so no error is issued here
12154 if the external-linkage check fails. */
12155 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12156 cp_parser_simulate_error (parser);
12158 else if (is_overloaded_fn (argument))
12159 /* All overloaded functions are allowed; if the external
12160 linkage test does not pass, an error will be issued
12164 && (TREE_CODE (argument) == OFFSET_REF
12165 || TREE_CODE (argument) == SCOPE_REF))
12166 /* A pointer-to-member. */
12168 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12171 cp_parser_simulate_error (parser);
12173 if (cp_parser_parse_definitely (parser))
12176 argument = build_x_unary_op (ADDR_EXPR, argument,
12177 tf_warning_or_error);
12182 /* If the argument started with "&", there are no other valid
12183 alternatives at this point. */
12186 cp_parser_error (parser, "invalid non-type template argument");
12187 return error_mark_node;
12190 /* If the argument wasn't successfully parsed as a type-id followed
12191 by '>>', the argument can only be a constant expression now.
12192 Otherwise, we try parsing the constant-expression tentatively,
12193 because the argument could really be a type-id. */
12195 cp_parser_parse_tentatively (parser);
12196 argument = cp_parser_constant_expression (parser,
12197 /*allow_non_constant_p=*/false,
12198 /*non_constant_p=*/NULL);
12199 argument = fold_non_dependent_expr (argument);
12200 if (!maybe_type_id)
12202 if (!cp_parser_next_token_ends_template_argument_p (parser))
12203 cp_parser_error (parser, "expected template-argument");
12204 if (cp_parser_parse_definitely (parser))
12206 /* We did our best to parse the argument as a non type-id, but that
12207 was the only alternative that matched (albeit with a '>' after
12208 it). We can assume it's just a typo from the user, and a
12209 diagnostic will then be issued. */
12210 return cp_parser_template_type_arg (parser);
12213 /* Parse an explicit-instantiation.
12215 explicit-instantiation:
12216 template declaration
12218 Although the standard says `declaration', what it really means is:
12220 explicit-instantiation:
12221 template decl-specifier-seq [opt] declarator [opt] ;
12223 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12224 supposed to be allowed. A defect report has been filed about this
12229 explicit-instantiation:
12230 storage-class-specifier template
12231 decl-specifier-seq [opt] declarator [opt] ;
12232 function-specifier template
12233 decl-specifier-seq [opt] declarator [opt] ; */
12236 cp_parser_explicit_instantiation (cp_parser* parser)
12238 int declares_class_or_enum;
12239 cp_decl_specifier_seq decl_specifiers;
12240 tree extension_specifier = NULL_TREE;
12242 /* Look for an (optional) storage-class-specifier or
12243 function-specifier. */
12244 if (cp_parser_allow_gnu_extensions_p (parser))
12246 extension_specifier
12247 = cp_parser_storage_class_specifier_opt (parser);
12248 if (!extension_specifier)
12249 extension_specifier
12250 = cp_parser_function_specifier_opt (parser,
12251 /*decl_specs=*/NULL);
12254 /* Look for the `template' keyword. */
12255 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12256 /* Let the front end know that we are processing an explicit
12258 begin_explicit_instantiation ();
12259 /* [temp.explicit] says that we are supposed to ignore access
12260 control while processing explicit instantiation directives. */
12261 push_deferring_access_checks (dk_no_check);
12262 /* Parse a decl-specifier-seq. */
12263 cp_parser_decl_specifier_seq (parser,
12264 CP_PARSER_FLAGS_OPTIONAL,
12266 &declares_class_or_enum);
12267 /* If there was exactly one decl-specifier, and it declared a class,
12268 and there's no declarator, then we have an explicit type
12270 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12274 type = check_tag_decl (&decl_specifiers);
12275 /* Turn access control back on for names used during
12276 template instantiation. */
12277 pop_deferring_access_checks ();
12279 do_type_instantiation (type, extension_specifier,
12280 /*complain=*/tf_error);
12284 cp_declarator *declarator;
12287 /* Parse the declarator. */
12289 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12290 /*ctor_dtor_or_conv_p=*/NULL,
12291 /*parenthesized_p=*/NULL,
12292 /*member_p=*/false);
12293 if (declares_class_or_enum & 2)
12294 cp_parser_check_for_definition_in_return_type (declarator,
12295 decl_specifiers.type,
12296 decl_specifiers.type_location);
12297 if (declarator != cp_error_declarator)
12299 if (decl_specifiers.specs[(int)ds_inline])
12300 permerror (input_location, "explicit instantiation shall not use"
12301 " %<inline%> specifier");
12302 if (decl_specifiers.specs[(int)ds_constexpr])
12303 permerror (input_location, "explicit instantiation shall not use"
12304 " %<constexpr%> specifier");
12306 decl = grokdeclarator (declarator, &decl_specifiers,
12307 NORMAL, 0, &decl_specifiers.attributes);
12308 /* Turn access control back on for names used during
12309 template instantiation. */
12310 pop_deferring_access_checks ();
12311 /* Do the explicit instantiation. */
12312 do_decl_instantiation (decl, extension_specifier);
12316 pop_deferring_access_checks ();
12317 /* Skip the body of the explicit instantiation. */
12318 cp_parser_skip_to_end_of_statement (parser);
12321 /* We're done with the instantiation. */
12322 end_explicit_instantiation ();
12324 cp_parser_consume_semicolon_at_end_of_statement (parser);
12327 /* Parse an explicit-specialization.
12329 explicit-specialization:
12330 template < > declaration
12332 Although the standard says `declaration', what it really means is:
12334 explicit-specialization:
12335 template <> decl-specifier [opt] init-declarator [opt] ;
12336 template <> function-definition
12337 template <> explicit-specialization
12338 template <> template-declaration */
12341 cp_parser_explicit_specialization (cp_parser* parser)
12343 bool need_lang_pop;
12344 cp_token *token = cp_lexer_peek_token (parser->lexer);
12346 /* Look for the `template' keyword. */
12347 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12348 /* Look for the `<'. */
12349 cp_parser_require (parser, CPP_LESS, RT_LESS);
12350 /* Look for the `>'. */
12351 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12352 /* We have processed another parameter list. */
12353 ++parser->num_template_parameter_lists;
12356 A template ... explicit specialization ... shall not have C
12358 if (current_lang_name == lang_name_c)
12360 error_at (token->location, "template specialization with C linkage");
12361 /* Give it C++ linkage to avoid confusing other parts of the
12363 push_lang_context (lang_name_cplusplus);
12364 need_lang_pop = true;
12367 need_lang_pop = false;
12368 /* Let the front end know that we are beginning a specialization. */
12369 if (!begin_specialization ())
12371 end_specialization ();
12375 /* If the next keyword is `template', we need to figure out whether
12376 or not we're looking a template-declaration. */
12377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12379 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12380 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12381 cp_parser_template_declaration_after_export (parser,
12382 /*member_p=*/false);
12384 cp_parser_explicit_specialization (parser);
12387 /* Parse the dependent declaration. */
12388 cp_parser_single_declaration (parser,
12390 /*member_p=*/false,
12391 /*explicit_specialization_p=*/true,
12392 /*friend_p=*/NULL);
12393 /* We're done with the specialization. */
12394 end_specialization ();
12395 /* For the erroneous case of a template with C linkage, we pushed an
12396 implicit C++ linkage scope; exit that scope now. */
12398 pop_lang_context ();
12399 /* We're done with this parameter list. */
12400 --parser->num_template_parameter_lists;
12403 /* Parse a type-specifier.
12406 simple-type-specifier
12409 elaborated-type-specifier
12417 Returns a representation of the type-specifier. For a
12418 class-specifier, enum-specifier, or elaborated-type-specifier, a
12419 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12421 The parser flags FLAGS is used to control type-specifier parsing.
12423 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12424 in a decl-specifier-seq.
12426 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12427 class-specifier, enum-specifier, or elaborated-type-specifier, then
12428 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12429 if a type is declared; 2 if it is defined. Otherwise, it is set to
12432 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12433 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12434 is set to FALSE. */
12437 cp_parser_type_specifier (cp_parser* parser,
12438 cp_parser_flags flags,
12439 cp_decl_specifier_seq *decl_specs,
12440 bool is_declaration,
12441 int* declares_class_or_enum,
12442 bool* is_cv_qualifier)
12444 tree type_spec = NULL_TREE;
12447 cp_decl_spec ds = ds_last;
12449 /* Assume this type-specifier does not declare a new type. */
12450 if (declares_class_or_enum)
12451 *declares_class_or_enum = 0;
12452 /* And that it does not specify a cv-qualifier. */
12453 if (is_cv_qualifier)
12454 *is_cv_qualifier = false;
12455 /* Peek at the next token. */
12456 token = cp_lexer_peek_token (parser->lexer);
12458 /* If we're looking at a keyword, we can use that to guide the
12459 production we choose. */
12460 keyword = token->keyword;
12464 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12465 goto elaborated_type_specifier;
12467 /* Look for the enum-specifier. */
12468 type_spec = cp_parser_enum_specifier (parser);
12469 /* If that worked, we're done. */
12472 if (declares_class_or_enum)
12473 *declares_class_or_enum = 2;
12475 cp_parser_set_decl_spec_type (decl_specs,
12478 /*user_defined_p=*/true);
12482 goto elaborated_type_specifier;
12484 /* Any of these indicate either a class-specifier, or an
12485 elaborated-type-specifier. */
12489 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12490 goto elaborated_type_specifier;
12492 /* Parse tentatively so that we can back up if we don't find a
12493 class-specifier. */
12494 cp_parser_parse_tentatively (parser);
12495 /* Look for the class-specifier. */
12496 type_spec = cp_parser_class_specifier (parser);
12497 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12498 /* If that worked, we're done. */
12499 if (cp_parser_parse_definitely (parser))
12501 if (declares_class_or_enum)
12502 *declares_class_or_enum = 2;
12504 cp_parser_set_decl_spec_type (decl_specs,
12507 /*user_defined_p=*/true);
12511 /* Fall through. */
12512 elaborated_type_specifier:
12513 /* We're declaring (not defining) a class or enum. */
12514 if (declares_class_or_enum)
12515 *declares_class_or_enum = 1;
12517 /* Fall through. */
12519 /* Look for an elaborated-type-specifier. */
12521 = (cp_parser_elaborated_type_specifier
12523 decl_specs && decl_specs->specs[(int) ds_friend],
12526 cp_parser_set_decl_spec_type (decl_specs,
12529 /*user_defined_p=*/true);
12534 if (is_cv_qualifier)
12535 *is_cv_qualifier = true;
12540 if (is_cv_qualifier)
12541 *is_cv_qualifier = true;
12546 if (is_cv_qualifier)
12547 *is_cv_qualifier = true;
12551 /* The `__complex__' keyword is a GNU extension. */
12559 /* Handle simple keywords. */
12564 ++decl_specs->specs[(int)ds];
12565 decl_specs->any_specifiers_p = true;
12567 return cp_lexer_consume_token (parser->lexer)->u.value;
12570 /* If we do not already have a type-specifier, assume we are looking
12571 at a simple-type-specifier. */
12572 type_spec = cp_parser_simple_type_specifier (parser,
12576 /* If we didn't find a type-specifier, and a type-specifier was not
12577 optional in this context, issue an error message. */
12578 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12580 cp_parser_error (parser, "expected type specifier");
12581 return error_mark_node;
12587 /* Parse a simple-type-specifier.
12589 simple-type-specifier:
12590 :: [opt] nested-name-specifier [opt] type-name
12591 :: [opt] nested-name-specifier template template-id
12606 simple-type-specifier:
12608 decltype ( expression )
12614 simple-type-specifier:
12616 __typeof__ unary-expression
12617 __typeof__ ( type-id )
12619 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12620 appropriately updated. */
12623 cp_parser_simple_type_specifier (cp_parser* parser,
12624 cp_decl_specifier_seq *decl_specs,
12625 cp_parser_flags flags)
12627 tree type = NULL_TREE;
12630 /* Peek at the next token. */
12631 token = cp_lexer_peek_token (parser->lexer);
12633 /* If we're looking at a keyword, things are easy. */
12634 switch (token->keyword)
12638 decl_specs->explicit_char_p = true;
12639 type = char_type_node;
12642 type = char16_type_node;
12645 type = char32_type_node;
12648 type = wchar_type_node;
12651 type = boolean_type_node;
12655 ++decl_specs->specs[(int) ds_short];
12656 type = short_integer_type_node;
12660 decl_specs->explicit_int_p = true;
12661 type = integer_type_node;
12664 if (!int128_integer_type_node)
12667 decl_specs->explicit_int128_p = true;
12668 type = int128_integer_type_node;
12672 ++decl_specs->specs[(int) ds_long];
12673 type = long_integer_type_node;
12677 ++decl_specs->specs[(int) ds_signed];
12678 type = integer_type_node;
12682 ++decl_specs->specs[(int) ds_unsigned];
12683 type = unsigned_type_node;
12686 type = float_type_node;
12689 type = double_type_node;
12692 type = void_type_node;
12696 maybe_warn_cpp0x (CPP0X_AUTO);
12697 type = make_auto ();
12701 /* Parse the `decltype' type. */
12702 type = cp_parser_decltype (parser);
12705 cp_parser_set_decl_spec_type (decl_specs, type,
12707 /*user_defined_p=*/true);
12712 /* Consume the `typeof' token. */
12713 cp_lexer_consume_token (parser->lexer);
12714 /* Parse the operand to `typeof'. */
12715 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12716 /* If it is not already a TYPE, take its type. */
12717 if (!TYPE_P (type))
12718 type = finish_typeof (type);
12721 cp_parser_set_decl_spec_type (decl_specs, type,
12723 /*user_defined_p=*/true);
12731 /* If the type-specifier was for a built-in type, we're done. */
12734 /* Record the type. */
12736 && (token->keyword != RID_SIGNED
12737 && token->keyword != RID_UNSIGNED
12738 && token->keyword != RID_SHORT
12739 && token->keyword != RID_LONG))
12740 cp_parser_set_decl_spec_type (decl_specs,
12743 /*user_defined=*/false);
12745 decl_specs->any_specifiers_p = true;
12747 /* Consume the token. */
12748 cp_lexer_consume_token (parser->lexer);
12750 /* There is no valid C++ program where a non-template type is
12751 followed by a "<". That usually indicates that the user thought
12752 that the type was a template. */
12753 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12755 return TYPE_NAME (type);
12758 /* The type-specifier must be a user-defined type. */
12759 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12764 /* Don't gobble tokens or issue error messages if this is an
12765 optional type-specifier. */
12766 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12767 cp_parser_parse_tentatively (parser);
12769 /* Look for the optional `::' operator. */
12771 = (cp_parser_global_scope_opt (parser,
12772 /*current_scope_valid_p=*/false)
12774 /* Look for the nested-name specifier. */
12776 = (cp_parser_nested_name_specifier_opt (parser,
12777 /*typename_keyword_p=*/false,
12778 /*check_dependency_p=*/true,
12780 /*is_declaration=*/false)
12782 token = cp_lexer_peek_token (parser->lexer);
12783 /* If we have seen a nested-name-specifier, and the next token
12784 is `template', then we are using the template-id production. */
12786 && cp_parser_optional_template_keyword (parser))
12788 /* Look for the template-id. */
12789 type = cp_parser_template_id (parser,
12790 /*template_keyword_p=*/true,
12791 /*check_dependency_p=*/true,
12792 /*is_declaration=*/false);
12793 /* If the template-id did not name a type, we are out of
12795 if (TREE_CODE (type) != TYPE_DECL)
12797 cp_parser_error (parser, "expected template-id for type");
12801 /* Otherwise, look for a type-name. */
12803 type = cp_parser_type_name (parser);
12804 /* Keep track of all name-lookups performed in class scopes. */
12808 && TREE_CODE (type) == TYPE_DECL
12809 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12810 maybe_note_name_used_in_class (DECL_NAME (type), type);
12811 /* If it didn't work out, we don't have a TYPE. */
12812 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12813 && !cp_parser_parse_definitely (parser))
12815 if (type && decl_specs)
12816 cp_parser_set_decl_spec_type (decl_specs, type,
12818 /*user_defined=*/true);
12821 /* If we didn't get a type-name, issue an error message. */
12822 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12824 cp_parser_error (parser, "expected type-name");
12825 return error_mark_node;
12828 if (type && type != error_mark_node)
12830 /* See if TYPE is an Objective-C type, and if so, parse and
12831 accept any protocol references following it. Do this before
12832 the cp_parser_check_for_invalid_template_id() call, because
12833 Objective-C types can be followed by '<...>' which would
12834 enclose protocol names rather than template arguments, and so
12835 everything is fine. */
12836 if (c_dialect_objc () && !parser->scope
12837 && (objc_is_id (type) || objc_is_class_name (type)))
12839 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12840 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12842 /* Clobber the "unqualified" type previously entered into
12843 DECL_SPECS with the new, improved protocol-qualified version. */
12845 decl_specs->type = qual_type;
12850 /* There is no valid C++ program where a non-template type is
12851 followed by a "<". That usually indicates that the user
12852 thought that the type was a template. */
12853 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12860 /* Parse a type-name.
12873 Returns a TYPE_DECL for the type. */
12876 cp_parser_type_name (cp_parser* parser)
12880 /* We can't know yet whether it is a class-name or not. */
12881 cp_parser_parse_tentatively (parser);
12882 /* Try a class-name. */
12883 type_decl = cp_parser_class_name (parser,
12884 /*typename_keyword_p=*/false,
12885 /*template_keyword_p=*/false,
12887 /*check_dependency_p=*/true,
12888 /*class_head_p=*/false,
12889 /*is_declaration=*/false);
12890 /* If it's not a class-name, keep looking. */
12891 if (!cp_parser_parse_definitely (parser))
12893 /* It must be a typedef-name or an enum-name. */
12894 return cp_parser_nonclass_name (parser);
12900 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12908 Returns a TYPE_DECL for the type. */
12911 cp_parser_nonclass_name (cp_parser* parser)
12916 cp_token *token = cp_lexer_peek_token (parser->lexer);
12917 identifier = cp_parser_identifier (parser);
12918 if (identifier == error_mark_node)
12919 return error_mark_node;
12921 /* Look up the type-name. */
12922 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12924 if (TREE_CODE (type_decl) != TYPE_DECL
12925 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12927 /* See if this is an Objective-C type. */
12928 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12929 tree type = objc_get_protocol_qualified_type (identifier, protos);
12931 type_decl = TYPE_NAME (type);
12934 /* Issue an error if we did not find a type-name. */
12935 if (TREE_CODE (type_decl) != TYPE_DECL
12936 /* In Objective-C, we have the complication that class names are
12937 normally type names and start declarations (eg, the
12938 "NSObject" in "NSObject *object;"), but can be used in an
12939 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12940 is an expression. So, a classname followed by a dot is not a
12941 valid type-name. */
12942 || (objc_is_class_name (TREE_TYPE (type_decl))
12943 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12945 if (!cp_parser_simulate_error (parser))
12946 cp_parser_name_lookup_error (parser, identifier, type_decl,
12947 NLE_TYPE, token->location);
12948 return error_mark_node;
12950 /* Remember that the name was used in the definition of the
12951 current class so that we can check later to see if the
12952 meaning would have been different after the class was
12953 entirely defined. */
12954 else if (type_decl != error_mark_node
12956 maybe_note_name_used_in_class (identifier, type_decl);
12961 /* Parse an elaborated-type-specifier. Note that the grammar given
12962 here incorporates the resolution to DR68.
12964 elaborated-type-specifier:
12965 class-key :: [opt] nested-name-specifier [opt] identifier
12966 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12967 enum-key :: [opt] nested-name-specifier [opt] identifier
12968 typename :: [opt] nested-name-specifier identifier
12969 typename :: [opt] nested-name-specifier template [opt]
12974 elaborated-type-specifier:
12975 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12976 class-key attributes :: [opt] nested-name-specifier [opt]
12977 template [opt] template-id
12978 enum attributes :: [opt] nested-name-specifier [opt] identifier
12980 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12981 declared `friend'. If IS_DECLARATION is TRUE, then this
12982 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12983 something is being declared.
12985 Returns the TYPE specified. */
12988 cp_parser_elaborated_type_specifier (cp_parser* parser,
12990 bool is_declaration)
12992 enum tag_types tag_type;
12994 tree type = NULL_TREE;
12995 tree attributes = NULL_TREE;
12997 cp_token *token = NULL;
12999 /* See if we're looking at the `enum' keyword. */
13000 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13002 /* Consume the `enum' token. */
13003 cp_lexer_consume_token (parser->lexer);
13004 /* Remember that it's an enumeration type. */
13005 tag_type = enum_type;
13006 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13007 enums) is used here. */
13008 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13009 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13011 pedwarn (input_location, 0, "elaborated-type-specifier "
13012 "for a scoped enum must not use the %<%D%> keyword",
13013 cp_lexer_peek_token (parser->lexer)->u.value);
13014 /* Consume the `struct' or `class' and parse it anyway. */
13015 cp_lexer_consume_token (parser->lexer);
13017 /* Parse the attributes. */
13018 attributes = cp_parser_attributes_opt (parser);
13020 /* Or, it might be `typename'. */
13021 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13024 /* Consume the `typename' token. */
13025 cp_lexer_consume_token (parser->lexer);
13026 /* Remember that it's a `typename' type. */
13027 tag_type = typename_type;
13029 /* Otherwise it must be a class-key. */
13032 tag_type = cp_parser_class_key (parser);
13033 if (tag_type == none_type)
13034 return error_mark_node;
13035 /* Parse the attributes. */
13036 attributes = cp_parser_attributes_opt (parser);
13039 /* Look for the `::' operator. */
13040 globalscope = cp_parser_global_scope_opt (parser,
13041 /*current_scope_valid_p=*/false);
13042 /* Look for the nested-name-specifier. */
13043 if (tag_type == typename_type && !globalscope)
13045 if (!cp_parser_nested_name_specifier (parser,
13046 /*typename_keyword_p=*/true,
13047 /*check_dependency_p=*/true,
13050 return error_mark_node;
13053 /* Even though `typename' is not present, the proposed resolution
13054 to Core Issue 180 says that in `class A<T>::B', `B' should be
13055 considered a type-name, even if `A<T>' is dependent. */
13056 cp_parser_nested_name_specifier_opt (parser,
13057 /*typename_keyword_p=*/true,
13058 /*check_dependency_p=*/true,
13061 /* For everything but enumeration types, consider a template-id.
13062 For an enumeration type, consider only a plain identifier. */
13063 if (tag_type != enum_type)
13065 bool template_p = false;
13068 /* Allow the `template' keyword. */
13069 template_p = cp_parser_optional_template_keyword (parser);
13070 /* If we didn't see `template', we don't know if there's a
13071 template-id or not. */
13073 cp_parser_parse_tentatively (parser);
13074 /* Parse the template-id. */
13075 token = cp_lexer_peek_token (parser->lexer);
13076 decl = cp_parser_template_id (parser, template_p,
13077 /*check_dependency_p=*/true,
13079 /* If we didn't find a template-id, look for an ordinary
13081 if (!template_p && !cp_parser_parse_definitely (parser))
13083 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13084 in effect, then we must assume that, upon instantiation, the
13085 template will correspond to a class. */
13086 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13087 && tag_type == typename_type)
13088 type = make_typename_type (parser->scope, decl,
13090 /*complain=*/tf_error);
13091 /* If the `typename' keyword is in effect and DECL is not a type
13092 decl. Then type is non existant. */
13093 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13096 type = TREE_TYPE (decl);
13101 token = cp_lexer_peek_token (parser->lexer);
13102 identifier = cp_parser_identifier (parser);
13104 if (identifier == error_mark_node)
13106 parser->scope = NULL_TREE;
13107 return error_mark_node;
13110 /* For a `typename', we needn't call xref_tag. */
13111 if (tag_type == typename_type
13112 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13113 return cp_parser_make_typename_type (parser, parser->scope,
13116 /* Look up a qualified name in the usual way. */
13120 tree ambiguous_decls;
13122 decl = cp_parser_lookup_name (parser, identifier,
13124 /*is_template=*/false,
13125 /*is_namespace=*/false,
13126 /*check_dependency=*/true,
13130 /* If the lookup was ambiguous, an error will already have been
13132 if (ambiguous_decls)
13133 return error_mark_node;
13135 /* If we are parsing friend declaration, DECL may be a
13136 TEMPLATE_DECL tree node here. However, we need to check
13137 whether this TEMPLATE_DECL results in valid code. Consider
13138 the following example:
13141 template <class T> class C {};
13144 template <class T> friend class N::C; // #1, valid code
13146 template <class T> class Y {
13147 friend class N::C; // #2, invalid code
13150 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13151 name lookup of `N::C'. We see that friend declaration must
13152 be template for the code to be valid. Note that
13153 processing_template_decl does not work here since it is
13154 always 1 for the above two cases. */
13156 decl = (cp_parser_maybe_treat_template_as_class
13157 (decl, /*tag_name_p=*/is_friend
13158 && parser->num_template_parameter_lists));
13160 if (TREE_CODE (decl) != TYPE_DECL)
13162 cp_parser_diagnose_invalid_type_name (parser,
13166 return error_mark_node;
13169 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13171 bool allow_template = (parser->num_template_parameter_lists
13172 || DECL_SELF_REFERENCE_P (decl));
13173 type = check_elaborated_type_specifier (tag_type, decl,
13176 if (type == error_mark_node)
13177 return error_mark_node;
13180 /* Forward declarations of nested types, such as
13185 are invalid unless all components preceding the final '::'
13186 are complete. If all enclosing types are complete, these
13187 declarations become merely pointless.
13189 Invalid forward declarations of nested types are errors
13190 caught elsewhere in parsing. Those that are pointless arrive
13193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13194 && !is_friend && !processing_explicit_instantiation)
13195 warning (0, "declaration %qD does not declare anything", decl);
13197 type = TREE_TYPE (decl);
13201 /* An elaborated-type-specifier sometimes introduces a new type and
13202 sometimes names an existing type. Normally, the rule is that it
13203 introduces a new type only if there is not an existing type of
13204 the same name already in scope. For example, given:
13207 void f() { struct S s; }
13209 the `struct S' in the body of `f' is the same `struct S' as in
13210 the global scope; the existing definition is used. However, if
13211 there were no global declaration, this would introduce a new
13212 local class named `S'.
13214 An exception to this rule applies to the following code:
13216 namespace N { struct S; }
13218 Here, the elaborated-type-specifier names a new type
13219 unconditionally; even if there is already an `S' in the
13220 containing scope this declaration names a new type.
13221 This exception only applies if the elaborated-type-specifier
13222 forms the complete declaration:
13226 A declaration consisting solely of `class-key identifier ;' is
13227 either a redeclaration of the name in the current scope or a
13228 forward declaration of the identifier as a class name. It
13229 introduces the name into the current scope.
13231 We are in this situation precisely when the next token is a `;'.
13233 An exception to the exception is that a `friend' declaration does
13234 *not* name a new type; i.e., given:
13236 struct S { friend struct T; };
13238 `T' is not a new type in the scope of `S'.
13240 Also, `new struct S' or `sizeof (struct S)' never results in the
13241 definition of a new type; a new type can only be declared in a
13242 declaration context. */
13248 /* Friends have special name lookup rules. */
13249 ts = ts_within_enclosing_non_class;
13250 else if (is_declaration
13251 && cp_lexer_next_token_is (parser->lexer,
13253 /* This is a `class-key identifier ;' */
13259 (parser->num_template_parameter_lists
13260 && (cp_parser_next_token_starts_class_definition_p (parser)
13261 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13262 /* An unqualified name was used to reference this type, so
13263 there were no qualifying templates. */
13264 if (!cp_parser_check_template_parameters (parser,
13265 /*num_templates=*/0,
13267 /*declarator=*/NULL))
13268 return error_mark_node;
13269 type = xref_tag (tag_type, identifier, ts, template_p);
13273 if (type == error_mark_node)
13274 return error_mark_node;
13276 /* Allow attributes on forward declarations of classes. */
13279 if (TREE_CODE (type) == TYPENAME_TYPE)
13280 warning (OPT_Wattributes,
13281 "attributes ignored on uninstantiated type");
13282 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13283 && ! processing_explicit_instantiation)
13284 warning (OPT_Wattributes,
13285 "attributes ignored on template instantiation");
13286 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13287 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13289 warning (OPT_Wattributes,
13290 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13293 if (tag_type != enum_type)
13294 cp_parser_check_class_key (tag_type, type);
13296 /* A "<" cannot follow an elaborated type specifier. If that
13297 happens, the user was probably trying to form a template-id. */
13298 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13303 /* Parse an enum-specifier.
13306 enum-head { enumerator-list [opt] }
13309 enum-key identifier [opt] enum-base [opt]
13310 enum-key nested-name-specifier identifier enum-base [opt]
13315 enum struct [C++0x]
13318 : type-specifier-seq
13320 opaque-enum-specifier:
13321 enum-key identifier enum-base [opt] ;
13324 enum-key attributes[opt] identifier [opt] enum-base [opt]
13325 { enumerator-list [opt] }attributes[opt]
13327 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13328 if the token stream isn't an enum-specifier after all. */
13331 cp_parser_enum_specifier (cp_parser* parser)
13334 tree type = NULL_TREE;
13336 tree nested_name_specifier = NULL_TREE;
13338 bool scoped_enum_p = false;
13339 bool has_underlying_type = false;
13340 bool nested_being_defined = false;
13341 bool new_value_list = false;
13342 bool is_new_type = false;
13343 bool is_anonymous = false;
13344 tree underlying_type = NULL_TREE;
13345 cp_token *type_start_token = NULL;
13347 /* Parse tentatively so that we can back up if we don't find a
13349 cp_parser_parse_tentatively (parser);
13351 /* Caller guarantees that the current token is 'enum', an identifier
13352 possibly follows, and the token after that is an opening brace.
13353 If we don't have an identifier, fabricate an anonymous name for
13354 the enumeration being defined. */
13355 cp_lexer_consume_token (parser->lexer);
13357 /* Parse the "class" or "struct", which indicates a scoped
13358 enumeration type in C++0x. */
13359 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13360 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13362 if (cxx_dialect < cxx0x)
13363 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13365 /* Consume the `struct' or `class' token. */
13366 cp_lexer_consume_token (parser->lexer);
13368 scoped_enum_p = true;
13371 attributes = cp_parser_attributes_opt (parser);
13373 /* Clear the qualification. */
13374 parser->scope = NULL_TREE;
13375 parser->qualifying_scope = NULL_TREE;
13376 parser->object_scope = NULL_TREE;
13378 /* Figure out in what scope the declaration is being placed. */
13379 prev_scope = current_scope ();
13381 type_start_token = cp_lexer_peek_token (parser->lexer);
13383 push_deferring_access_checks (dk_no_check);
13384 nested_name_specifier
13385 = cp_parser_nested_name_specifier_opt (parser,
13386 /*typename_keyword_p=*/true,
13387 /*check_dependency_p=*/false,
13389 /*is_declaration=*/false);
13391 if (nested_name_specifier)
13395 identifier = cp_parser_identifier (parser);
13396 name = cp_parser_lookup_name (parser, identifier,
13398 /*is_template=*/false,
13399 /*is_namespace=*/false,
13400 /*check_dependency=*/true,
13401 /*ambiguous_decls=*/NULL,
13405 type = TREE_TYPE (name);
13406 if (TREE_CODE (type) == TYPENAME_TYPE)
13408 /* Are template enums allowed in ISO? */
13409 if (template_parm_scope_p ())
13410 pedwarn (type_start_token->location, OPT_pedantic,
13411 "%qD is an enumeration template", name);
13412 /* ignore a typename reference, for it will be solved by name
13418 error_at (type_start_token->location,
13419 "%qD is not an enumerator-name", identifier);
13423 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13424 identifier = cp_parser_identifier (parser);
13427 identifier = make_anon_name ();
13428 is_anonymous = true;
13431 pop_deferring_access_checks ();
13433 /* Check for the `:' that denotes a specified underlying type in C++0x.
13434 Note that a ':' could also indicate a bitfield width, however. */
13435 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13437 cp_decl_specifier_seq type_specifiers;
13439 /* Consume the `:'. */
13440 cp_lexer_consume_token (parser->lexer);
13442 /* Parse the type-specifier-seq. */
13443 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13444 /*is_trailing_return=*/false,
13447 /* At this point this is surely not elaborated type specifier. */
13448 if (!cp_parser_parse_definitely (parser))
13451 if (cxx_dialect < cxx0x)
13452 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13454 has_underlying_type = true;
13456 /* If that didn't work, stop. */
13457 if (type_specifiers.type != error_mark_node)
13459 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13460 /*initialized=*/0, NULL);
13461 if (underlying_type == error_mark_node)
13462 underlying_type = NULL_TREE;
13466 /* Look for the `{' but don't consume it yet. */
13467 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13469 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13471 cp_parser_error (parser, "expected %<{%>");
13472 if (has_underlying_type)
13475 /* An opaque-enum-specifier must have a ';' here. */
13476 if ((scoped_enum_p || underlying_type)
13477 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13479 cp_parser_error (parser, "expected %<;%> or %<{%>");
13480 if (has_underlying_type)
13485 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13488 if (nested_name_specifier)
13490 if (CLASS_TYPE_P (nested_name_specifier))
13492 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13493 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13494 push_scope (nested_name_specifier);
13496 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13498 push_nested_namespace (nested_name_specifier);
13502 /* Issue an error message if type-definitions are forbidden here. */
13503 if (!cp_parser_check_type_definition (parser))
13504 type = error_mark_node;
13506 /* Create the new type. We do this before consuming the opening
13507 brace so the enum will be recorded as being on the line of its
13508 tag (or the 'enum' keyword, if there is no tag). */
13509 type = start_enum (identifier, type, underlying_type,
13510 scoped_enum_p, &is_new_type);
13512 /* If the next token is not '{' it is an opaque-enum-specifier or an
13513 elaborated-type-specifier. */
13514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13516 if (nested_name_specifier)
13518 /* The following catches invalid code such as:
13519 enum class S<int>::E { A, B, C }; */
13520 if (!processing_specialization
13521 && CLASS_TYPE_P (nested_name_specifier)
13522 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13523 error_at (type_start_token->location, "cannot add an enumerator "
13524 "list to a template instantiation");
13526 /* If that scope does not contain the scope in which the
13527 class was originally declared, the program is invalid. */
13528 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13530 if (at_namespace_scope_p ())
13531 error_at (type_start_token->location,
13532 "declaration of %qD in namespace %qD which does not "
13534 type, prev_scope, nested_name_specifier);
13536 error_at (type_start_token->location,
13537 "declaration of %qD in %qD which does not enclose %qD",
13538 type, prev_scope, nested_name_specifier);
13539 type = error_mark_node;
13544 begin_scope (sk_scoped_enum, type);
13546 /* Consume the opening brace. */
13547 cp_lexer_consume_token (parser->lexer);
13549 if (type == error_mark_node)
13550 ; /* Nothing to add */
13551 else if (OPAQUE_ENUM_P (type)
13552 || (cxx_dialect > cxx98 && processing_specialization))
13554 new_value_list = true;
13555 SET_OPAQUE_ENUM_P (type, false);
13556 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13560 error_at (type_start_token->location, "multiple definition of %q#T", type);
13561 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13562 "previous definition here");
13563 type = error_mark_node;
13566 if (type == error_mark_node)
13567 cp_parser_skip_to_end_of_block_or_statement (parser);
13568 /* If the next token is not '}', then there are some enumerators. */
13569 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13570 cp_parser_enumerator_list (parser, type);
13572 /* Consume the final '}'. */
13573 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13580 /* If a ';' follows, then it is an opaque-enum-specifier
13581 and additional restrictions apply. */
13582 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13585 error_at (type_start_token->location,
13586 "opaque-enum-specifier without name");
13587 else if (nested_name_specifier)
13588 error_at (type_start_token->location,
13589 "opaque-enum-specifier must use a simple identifier");
13593 /* Look for trailing attributes to apply to this enumeration, and
13594 apply them if appropriate. */
13595 if (cp_parser_allow_gnu_extensions_p (parser))
13597 tree trailing_attr = cp_parser_attributes_opt (parser);
13598 trailing_attr = chainon (trailing_attr, attributes);
13599 cplus_decl_attributes (&type,
13601 (int) ATTR_FLAG_TYPE_IN_PLACE);
13604 /* Finish up the enumeration. */
13605 if (type != error_mark_node)
13607 if (new_value_list)
13608 finish_enum_value_list (type);
13610 finish_enum (type);
13613 if (nested_name_specifier)
13615 if (CLASS_TYPE_P (nested_name_specifier))
13617 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13618 pop_scope (nested_name_specifier);
13620 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13622 pop_nested_namespace (nested_name_specifier);
13628 /* Parse an enumerator-list. The enumerators all have the indicated
13632 enumerator-definition
13633 enumerator-list , enumerator-definition */
13636 cp_parser_enumerator_list (cp_parser* parser, tree type)
13640 /* Parse an enumerator-definition. */
13641 cp_parser_enumerator_definition (parser, type);
13643 /* If the next token is not a ',', we've reached the end of
13645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13647 /* Otherwise, consume the `,' and keep going. */
13648 cp_lexer_consume_token (parser->lexer);
13649 /* If the next token is a `}', there is a trailing comma. */
13650 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13652 if (!in_system_header)
13653 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13659 /* Parse an enumerator-definition. The enumerator has the indicated
13662 enumerator-definition:
13664 enumerator = constant-expression
13670 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13676 /* Save the input location because we are interested in the location
13677 of the identifier and not the location of the explicit value. */
13678 loc = cp_lexer_peek_token (parser->lexer)->location;
13680 /* Look for the identifier. */
13681 identifier = cp_parser_identifier (parser);
13682 if (identifier == error_mark_node)
13685 /* If the next token is an '=', then there is an explicit value. */
13686 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13688 /* Consume the `=' token. */
13689 cp_lexer_consume_token (parser->lexer);
13690 /* Parse the value. */
13691 value = cp_parser_constant_expression (parser,
13692 /*allow_non_constant_p=*/false,
13698 /* If we are processing a template, make sure the initializer of the
13699 enumerator doesn't contain any bare template parameter pack. */
13700 if (check_for_bare_parameter_packs (value))
13701 value = error_mark_node;
13703 /* Create the enumerator. */
13704 build_enumerator (identifier, value, type, loc);
13707 /* Parse a namespace-name.
13710 original-namespace-name
13713 Returns the NAMESPACE_DECL for the namespace. */
13716 cp_parser_namespace_name (cp_parser* parser)
13719 tree namespace_decl;
13721 cp_token *token = cp_lexer_peek_token (parser->lexer);
13723 /* Get the name of the namespace. */
13724 identifier = cp_parser_identifier (parser);
13725 if (identifier == error_mark_node)
13726 return error_mark_node;
13728 /* Look up the identifier in the currently active scope. Look only
13729 for namespaces, due to:
13731 [basic.lookup.udir]
13733 When looking up a namespace-name in a using-directive or alias
13734 definition, only namespace names are considered.
13738 [basic.lookup.qual]
13740 During the lookup of a name preceding the :: scope resolution
13741 operator, object, function, and enumerator names are ignored.
13743 (Note that cp_parser_qualifying_entity only calls this
13744 function if the token after the name is the scope resolution
13746 namespace_decl = cp_parser_lookup_name (parser, identifier,
13748 /*is_template=*/false,
13749 /*is_namespace=*/true,
13750 /*check_dependency=*/true,
13751 /*ambiguous_decls=*/NULL,
13753 /* If it's not a namespace, issue an error. */
13754 if (namespace_decl == error_mark_node
13755 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13757 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13758 error_at (token->location, "%qD is not a namespace-name", identifier);
13759 cp_parser_error (parser, "expected namespace-name");
13760 namespace_decl = error_mark_node;
13763 return namespace_decl;
13766 /* Parse a namespace-definition.
13768 namespace-definition:
13769 named-namespace-definition
13770 unnamed-namespace-definition
13772 named-namespace-definition:
13773 original-namespace-definition
13774 extension-namespace-definition
13776 original-namespace-definition:
13777 namespace identifier { namespace-body }
13779 extension-namespace-definition:
13780 namespace original-namespace-name { namespace-body }
13782 unnamed-namespace-definition:
13783 namespace { namespace-body } */
13786 cp_parser_namespace_definition (cp_parser* parser)
13788 tree identifier, attribs;
13789 bool has_visibility;
13792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13794 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13796 cp_lexer_consume_token (parser->lexer);
13801 /* Look for the `namespace' keyword. */
13802 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13804 /* Get the name of the namespace. We do not attempt to distinguish
13805 between an original-namespace-definition and an
13806 extension-namespace-definition at this point. The semantic
13807 analysis routines are responsible for that. */
13808 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13809 identifier = cp_parser_identifier (parser);
13811 identifier = NULL_TREE;
13813 /* Parse any specified attributes. */
13814 attribs = cp_parser_attributes_opt (parser);
13816 /* Look for the `{' to start the namespace. */
13817 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13818 /* Start the namespace. */
13819 push_namespace (identifier);
13821 /* "inline namespace" is equivalent to a stub namespace definition
13822 followed by a strong using directive. */
13825 tree name_space = current_namespace;
13826 /* Set up namespace association. */
13827 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13828 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13829 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13830 /* Import the contents of the inline namespace. */
13832 do_using_directive (name_space);
13833 push_namespace (identifier);
13836 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13838 /* Parse the body of the namespace. */
13839 cp_parser_namespace_body (parser);
13841 if (has_visibility)
13842 pop_visibility (1);
13844 /* Finish the namespace. */
13846 /* Look for the final `}'. */
13847 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13850 /* Parse a namespace-body.
13853 declaration-seq [opt] */
13856 cp_parser_namespace_body (cp_parser* parser)
13858 cp_parser_declaration_seq_opt (parser);
13861 /* Parse a namespace-alias-definition.
13863 namespace-alias-definition:
13864 namespace identifier = qualified-namespace-specifier ; */
13867 cp_parser_namespace_alias_definition (cp_parser* parser)
13870 tree namespace_specifier;
13872 cp_token *token = cp_lexer_peek_token (parser->lexer);
13874 /* Look for the `namespace' keyword. */
13875 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13876 /* Look for the identifier. */
13877 identifier = cp_parser_identifier (parser);
13878 if (identifier == error_mark_node)
13880 /* Look for the `=' token. */
13881 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13882 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13884 error_at (token->location, "%<namespace%> definition is not allowed here");
13885 /* Skip the definition. */
13886 cp_lexer_consume_token (parser->lexer);
13887 if (cp_parser_skip_to_closing_brace (parser))
13888 cp_lexer_consume_token (parser->lexer);
13891 cp_parser_require (parser, CPP_EQ, RT_EQ);
13892 /* Look for the qualified-namespace-specifier. */
13893 namespace_specifier
13894 = cp_parser_qualified_namespace_specifier (parser);
13895 /* Look for the `;' token. */
13896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13898 /* Register the alias in the symbol table. */
13899 do_namespace_alias (identifier, namespace_specifier);
13902 /* Parse a qualified-namespace-specifier.
13904 qualified-namespace-specifier:
13905 :: [opt] nested-name-specifier [opt] namespace-name
13907 Returns a NAMESPACE_DECL corresponding to the specified
13911 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13913 /* Look for the optional `::'. */
13914 cp_parser_global_scope_opt (parser,
13915 /*current_scope_valid_p=*/false);
13917 /* Look for the optional nested-name-specifier. */
13918 cp_parser_nested_name_specifier_opt (parser,
13919 /*typename_keyword_p=*/false,
13920 /*check_dependency_p=*/true,
13922 /*is_declaration=*/true);
13924 return cp_parser_namespace_name (parser);
13927 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13928 access declaration.
13931 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13932 using :: unqualified-id ;
13934 access-declaration:
13940 cp_parser_using_declaration (cp_parser* parser,
13941 bool access_declaration_p)
13944 bool typename_p = false;
13945 bool global_scope_p;
13950 if (access_declaration_p)
13951 cp_parser_parse_tentatively (parser);
13954 /* Look for the `using' keyword. */
13955 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13957 /* Peek at the next token. */
13958 token = cp_lexer_peek_token (parser->lexer);
13959 /* See if it's `typename'. */
13960 if (token->keyword == RID_TYPENAME)
13962 /* Remember that we've seen it. */
13964 /* Consume the `typename' token. */
13965 cp_lexer_consume_token (parser->lexer);
13969 /* Look for the optional global scope qualification. */
13971 = (cp_parser_global_scope_opt (parser,
13972 /*current_scope_valid_p=*/false)
13975 /* If we saw `typename', or didn't see `::', then there must be a
13976 nested-name-specifier present. */
13977 if (typename_p || !global_scope_p)
13978 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13979 /*check_dependency_p=*/true,
13981 /*is_declaration=*/true);
13982 /* Otherwise, we could be in either of the two productions. In that
13983 case, treat the nested-name-specifier as optional. */
13985 qscope = cp_parser_nested_name_specifier_opt (parser,
13986 /*typename_keyword_p=*/false,
13987 /*check_dependency_p=*/true,
13989 /*is_declaration=*/true);
13991 qscope = global_namespace;
13993 if (access_declaration_p && cp_parser_error_occurred (parser))
13994 /* Something has already gone wrong; there's no need to parse
13995 further. Since an error has occurred, the return value of
13996 cp_parser_parse_definitely will be false, as required. */
13997 return cp_parser_parse_definitely (parser);
13999 token = cp_lexer_peek_token (parser->lexer);
14000 /* Parse the unqualified-id. */
14001 identifier = cp_parser_unqualified_id (parser,
14002 /*template_keyword_p=*/false,
14003 /*check_dependency_p=*/true,
14004 /*declarator_p=*/true,
14005 /*optional_p=*/false);
14007 if (access_declaration_p)
14009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14010 cp_parser_simulate_error (parser);
14011 if (!cp_parser_parse_definitely (parser))
14015 /* The function we call to handle a using-declaration is different
14016 depending on what scope we are in. */
14017 if (qscope == error_mark_node || identifier == error_mark_node)
14019 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14020 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14021 /* [namespace.udecl]
14023 A using declaration shall not name a template-id. */
14024 error_at (token->location,
14025 "a template-id may not appear in a using-declaration");
14028 if (at_class_scope_p ())
14030 /* Create the USING_DECL. */
14031 decl = do_class_using_decl (parser->scope, identifier);
14033 if (check_for_bare_parameter_packs (decl))
14036 /* Add it to the list of members in this class. */
14037 finish_member_declaration (decl);
14041 decl = cp_parser_lookup_name_simple (parser,
14044 if (decl == error_mark_node)
14045 cp_parser_name_lookup_error (parser, identifier,
14048 else if (check_for_bare_parameter_packs (decl))
14050 else if (!at_namespace_scope_p ())
14051 do_local_using_decl (decl, qscope, identifier);
14053 do_toplevel_using_decl (decl, qscope, identifier);
14057 /* Look for the final `;'. */
14058 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14063 /* Parse a using-directive.
14066 using namespace :: [opt] nested-name-specifier [opt]
14067 namespace-name ; */
14070 cp_parser_using_directive (cp_parser* parser)
14072 tree namespace_decl;
14075 /* Look for the `using' keyword. */
14076 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14077 /* And the `namespace' keyword. */
14078 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14079 /* Look for the optional `::' operator. */
14080 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14081 /* And the optional nested-name-specifier. */
14082 cp_parser_nested_name_specifier_opt (parser,
14083 /*typename_keyword_p=*/false,
14084 /*check_dependency_p=*/true,
14086 /*is_declaration=*/true);
14087 /* Get the namespace being used. */
14088 namespace_decl = cp_parser_namespace_name (parser);
14089 /* And any specified attributes. */
14090 attribs = cp_parser_attributes_opt (parser);
14091 /* Update the symbol table. */
14092 parse_using_directive (namespace_decl, attribs);
14093 /* Look for the final `;'. */
14094 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14097 /* Parse an asm-definition.
14100 asm ( string-literal ) ;
14105 asm volatile [opt] ( string-literal ) ;
14106 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14107 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14108 : asm-operand-list [opt] ) ;
14109 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14110 : asm-operand-list [opt]
14111 : asm-clobber-list [opt] ) ;
14112 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14113 : asm-clobber-list [opt]
14114 : asm-goto-list ) ; */
14117 cp_parser_asm_definition (cp_parser* parser)
14120 tree outputs = NULL_TREE;
14121 tree inputs = NULL_TREE;
14122 tree clobbers = NULL_TREE;
14123 tree labels = NULL_TREE;
14125 bool volatile_p = false;
14126 bool extended_p = false;
14127 bool invalid_inputs_p = false;
14128 bool invalid_outputs_p = false;
14129 bool goto_p = false;
14130 required_token missing = RT_NONE;
14132 /* Look for the `asm' keyword. */
14133 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14134 /* See if the next token is `volatile'. */
14135 if (cp_parser_allow_gnu_extensions_p (parser)
14136 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14138 /* Remember that we saw the `volatile' keyword. */
14140 /* Consume the token. */
14141 cp_lexer_consume_token (parser->lexer);
14143 if (cp_parser_allow_gnu_extensions_p (parser)
14144 && parser->in_function_body
14145 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14147 /* Remember that we saw the `goto' keyword. */
14149 /* Consume the token. */
14150 cp_lexer_consume_token (parser->lexer);
14152 /* Look for the opening `('. */
14153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14155 /* Look for the string. */
14156 string = cp_parser_string_literal (parser, false, false);
14157 if (string == error_mark_node)
14159 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160 /*consume_paren=*/true);
14164 /* If we're allowing GNU extensions, check for the extended assembly
14165 syntax. Unfortunately, the `:' tokens need not be separated by
14166 a space in C, and so, for compatibility, we tolerate that here
14167 too. Doing that means that we have to treat the `::' operator as
14169 if (cp_parser_allow_gnu_extensions_p (parser)
14170 && parser->in_function_body
14171 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14172 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14174 bool inputs_p = false;
14175 bool clobbers_p = false;
14176 bool labels_p = false;
14178 /* The extended syntax was used. */
14181 /* Look for outputs. */
14182 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14184 /* Consume the `:'. */
14185 cp_lexer_consume_token (parser->lexer);
14186 /* Parse the output-operands. */
14187 if (cp_lexer_next_token_is_not (parser->lexer,
14189 && cp_lexer_next_token_is_not (parser->lexer,
14191 && cp_lexer_next_token_is_not (parser->lexer,
14194 outputs = cp_parser_asm_operand_list (parser);
14196 if (outputs == error_mark_node)
14197 invalid_outputs_p = true;
14199 /* If the next token is `::', there are no outputs, and the
14200 next token is the beginning of the inputs. */
14201 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14202 /* The inputs are coming next. */
14205 /* Look for inputs. */
14207 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14209 /* Consume the `:' or `::'. */
14210 cp_lexer_consume_token (parser->lexer);
14211 /* Parse the output-operands. */
14212 if (cp_lexer_next_token_is_not (parser->lexer,
14214 && cp_lexer_next_token_is_not (parser->lexer,
14216 && cp_lexer_next_token_is_not (parser->lexer,
14218 inputs = cp_parser_asm_operand_list (parser);
14220 if (inputs == error_mark_node)
14221 invalid_inputs_p = true;
14223 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14224 /* The clobbers are coming next. */
14227 /* Look for clobbers. */
14229 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14232 /* Consume the `:' or `::'. */
14233 cp_lexer_consume_token (parser->lexer);
14234 /* Parse the clobbers. */
14235 if (cp_lexer_next_token_is_not (parser->lexer,
14237 && cp_lexer_next_token_is_not (parser->lexer,
14239 clobbers = cp_parser_asm_clobber_list (parser);
14242 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14243 /* The labels are coming next. */
14246 /* Look for labels. */
14248 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14251 /* Consume the `:' or `::'. */
14252 cp_lexer_consume_token (parser->lexer);
14253 /* Parse the labels. */
14254 labels = cp_parser_asm_label_list (parser);
14257 if (goto_p && !labels_p)
14258 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14261 missing = RT_COLON_SCOPE;
14263 /* Look for the closing `)'. */
14264 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14265 missing ? missing : RT_CLOSE_PAREN))
14266 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14267 /*consume_paren=*/true);
14268 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14270 if (!invalid_inputs_p && !invalid_outputs_p)
14272 /* Create the ASM_EXPR. */
14273 if (parser->in_function_body)
14275 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14276 inputs, clobbers, labels);
14277 /* If the extended syntax was not used, mark the ASM_EXPR. */
14280 tree temp = asm_stmt;
14281 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14282 temp = TREE_OPERAND (temp, 0);
14284 ASM_INPUT_P (temp) = 1;
14288 cgraph_add_asm_node (string);
14292 /* Declarators [gram.dcl.decl] */
14294 /* Parse an init-declarator.
14297 declarator initializer [opt]
14302 declarator asm-specification [opt] attributes [opt] initializer [opt]
14304 function-definition:
14305 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14307 decl-specifier-seq [opt] declarator function-try-block
14311 function-definition:
14312 __extension__ function-definition
14314 The DECL_SPECIFIERS apply to this declarator. Returns a
14315 representation of the entity declared. If MEMBER_P is TRUE, then
14316 this declarator appears in a class scope. The new DECL created by
14317 this declarator is returned.
14319 The CHECKS are access checks that should be performed once we know
14320 what entity is being declared (and, therefore, what classes have
14323 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14324 for a function-definition here as well. If the declarator is a
14325 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14326 be TRUE upon return. By that point, the function-definition will
14327 have been completely parsed.
14329 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14333 cp_parser_init_declarator (cp_parser* parser,
14334 cp_decl_specifier_seq *decl_specifiers,
14335 VEC (deferred_access_check,gc)* checks,
14336 bool function_definition_allowed_p,
14338 int declares_class_or_enum,
14339 bool* function_definition_p)
14341 cp_token *token = NULL, *asm_spec_start_token = NULL,
14342 *attributes_start_token = NULL;
14343 cp_declarator *declarator;
14344 tree prefix_attributes;
14346 tree asm_specification;
14348 tree decl = NULL_TREE;
14350 int is_initialized;
14351 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14352 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14354 enum cpp_ttype initialization_kind;
14355 bool is_direct_init = false;
14356 bool is_non_constant_init;
14357 int ctor_dtor_or_conv_p;
14359 tree pushed_scope = NULL;
14361 /* Gather the attributes that were provided with the
14362 decl-specifiers. */
14363 prefix_attributes = decl_specifiers->attributes;
14365 /* Assume that this is not the declarator for a function
14367 if (function_definition_p)
14368 *function_definition_p = false;
14370 /* Defer access checks while parsing the declarator; we cannot know
14371 what names are accessible until we know what is being
14373 resume_deferring_access_checks ();
14375 /* Parse the declarator. */
14376 token = cp_lexer_peek_token (parser->lexer);
14378 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14379 &ctor_dtor_or_conv_p,
14380 /*parenthesized_p=*/NULL,
14381 /*member_p=*/false);
14382 /* Gather up the deferred checks. */
14383 stop_deferring_access_checks ();
14385 /* If the DECLARATOR was erroneous, there's no need to go
14387 if (declarator == cp_error_declarator)
14388 return error_mark_node;
14390 /* Check that the number of template-parameter-lists is OK. */
14391 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14393 return error_mark_node;
14395 if (declares_class_or_enum & 2)
14396 cp_parser_check_for_definition_in_return_type (declarator,
14397 decl_specifiers->type,
14398 decl_specifiers->type_location);
14400 /* Figure out what scope the entity declared by the DECLARATOR is
14401 located in. `grokdeclarator' sometimes changes the scope, so
14402 we compute it now. */
14403 scope = get_scope_of_declarator (declarator);
14405 /* Perform any lookups in the declared type which were thought to be
14406 dependent, but are not in the scope of the declarator. */
14407 decl_specifiers->type
14408 = maybe_update_decl_type (decl_specifiers->type, scope);
14410 /* If we're allowing GNU extensions, look for an asm-specification
14412 if (cp_parser_allow_gnu_extensions_p (parser))
14414 /* Look for an asm-specification. */
14415 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14416 asm_specification = cp_parser_asm_specification_opt (parser);
14417 /* And attributes. */
14418 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14419 attributes = cp_parser_attributes_opt (parser);
14423 asm_specification = NULL_TREE;
14424 attributes = NULL_TREE;
14427 /* Peek at the next token. */
14428 token = cp_lexer_peek_token (parser->lexer);
14429 /* Check to see if the token indicates the start of a
14430 function-definition. */
14431 if (function_declarator_p (declarator)
14432 && cp_parser_token_starts_function_definition_p (token))
14434 if (!function_definition_allowed_p)
14436 /* If a function-definition should not appear here, issue an
14438 cp_parser_error (parser,
14439 "a function-definition is not allowed here");
14440 return error_mark_node;
14444 location_t func_brace_location
14445 = cp_lexer_peek_token (parser->lexer)->location;
14447 /* Neither attributes nor an asm-specification are allowed
14448 on a function-definition. */
14449 if (asm_specification)
14450 error_at (asm_spec_start_token->location,
14451 "an asm-specification is not allowed "
14452 "on a function-definition");
14454 error_at (attributes_start_token->location,
14455 "attributes are not allowed on a function-definition");
14456 /* This is a function-definition. */
14457 *function_definition_p = true;
14459 /* Parse the function definition. */
14461 decl = cp_parser_save_member_function_body (parser,
14464 prefix_attributes);
14467 = (cp_parser_function_definition_from_specifiers_and_declarator
14468 (parser, decl_specifiers, prefix_attributes, declarator));
14470 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14472 /* This is where the prologue starts... */
14473 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14474 = func_brace_location;
14483 Only in function declarations for constructors, destructors, and
14484 type conversions can the decl-specifier-seq be omitted.
14486 We explicitly postpone this check past the point where we handle
14487 function-definitions because we tolerate function-definitions
14488 that are missing their return types in some modes. */
14489 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14491 cp_parser_error (parser,
14492 "expected constructor, destructor, or type conversion");
14493 return error_mark_node;
14496 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14497 if (token->type == CPP_EQ
14498 || token->type == CPP_OPEN_PAREN
14499 || token->type == CPP_OPEN_BRACE)
14501 is_initialized = SD_INITIALIZED;
14502 initialization_kind = token->type;
14504 if (token->type == CPP_EQ
14505 && function_declarator_p (declarator))
14507 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14508 if (t2->keyword == RID_DEFAULT)
14509 is_initialized = SD_DEFAULTED;
14510 else if (t2->keyword == RID_DELETE)
14511 is_initialized = SD_DELETED;
14516 /* If the init-declarator isn't initialized and isn't followed by a
14517 `,' or `;', it's not a valid init-declarator. */
14518 if (token->type != CPP_COMMA
14519 && token->type != CPP_SEMICOLON)
14521 cp_parser_error (parser, "expected initializer");
14522 return error_mark_node;
14524 is_initialized = SD_UNINITIALIZED;
14525 initialization_kind = CPP_EOF;
14528 /* Because start_decl has side-effects, we should only call it if we
14529 know we're going ahead. By this point, we know that we cannot
14530 possibly be looking at any other construct. */
14531 cp_parser_commit_to_tentative_parse (parser);
14533 /* If the decl specifiers were bad, issue an error now that we're
14534 sure this was intended to be a declarator. Then continue
14535 declaring the variable(s), as int, to try to cut down on further
14537 if (decl_specifiers->any_specifiers_p
14538 && decl_specifiers->type == error_mark_node)
14540 cp_parser_error (parser, "invalid type in declaration");
14541 decl_specifiers->type = integer_type_node;
14544 /* Check to see whether or not this declaration is a friend. */
14545 friend_p = cp_parser_friend_p (decl_specifiers);
14547 /* Enter the newly declared entry in the symbol table. If we're
14548 processing a declaration in a class-specifier, we wait until
14549 after processing the initializer. */
14552 if (parser->in_unbraced_linkage_specification_p)
14553 decl_specifiers->storage_class = sc_extern;
14554 decl = start_decl (declarator, decl_specifiers,
14555 is_initialized, attributes, prefix_attributes,
14557 /* Adjust location of decl if declarator->id_loc is more appropriate:
14558 set, and decl wasn't merged with another decl, in which case its
14559 location would be different from input_location, and more accurate. */
14561 && declarator->id_loc != UNKNOWN_LOCATION
14562 && DECL_SOURCE_LOCATION (decl) == input_location)
14563 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14566 /* Enter the SCOPE. That way unqualified names appearing in the
14567 initializer will be looked up in SCOPE. */
14568 pushed_scope = push_scope (scope);
14570 /* Perform deferred access control checks, now that we know in which
14571 SCOPE the declared entity resides. */
14572 if (!member_p && decl)
14574 tree saved_current_function_decl = NULL_TREE;
14576 /* If the entity being declared is a function, pretend that we
14577 are in its scope. If it is a `friend', it may have access to
14578 things that would not otherwise be accessible. */
14579 if (TREE_CODE (decl) == FUNCTION_DECL)
14581 saved_current_function_decl = current_function_decl;
14582 current_function_decl = decl;
14585 /* Perform access checks for template parameters. */
14586 cp_parser_perform_template_parameter_access_checks (checks);
14588 /* Perform the access control checks for the declarator and the
14589 decl-specifiers. */
14590 perform_deferred_access_checks ();
14592 /* Restore the saved value. */
14593 if (TREE_CODE (decl) == FUNCTION_DECL)
14594 current_function_decl = saved_current_function_decl;
14597 /* Parse the initializer. */
14598 initializer = NULL_TREE;
14599 is_direct_init = false;
14600 is_non_constant_init = true;
14601 if (is_initialized)
14603 if (function_declarator_p (declarator))
14605 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14606 if (initialization_kind == CPP_EQ)
14607 initializer = cp_parser_pure_specifier (parser);
14610 /* If the declaration was erroneous, we don't really
14611 know what the user intended, so just silently
14612 consume the initializer. */
14613 if (decl != error_mark_node)
14614 error_at (initializer_start_token->location,
14615 "initializer provided for function");
14616 cp_parser_skip_to_closing_parenthesis (parser,
14617 /*recovering=*/true,
14618 /*or_comma=*/false,
14619 /*consume_paren=*/true);
14624 /* We want to record the extra mangling scope for in-class
14625 initializers of class members and initializers of static data
14626 member templates. The former is a C++0x feature which isn't
14627 implemented yet, and I expect it will involve deferring
14628 parsing of the initializer until end of class as with default
14629 arguments. So right here we only handle the latter. */
14630 if (!member_p && processing_template_decl)
14631 start_lambda_scope (decl);
14632 initializer = cp_parser_initializer (parser,
14634 &is_non_constant_init);
14635 if (!member_p && processing_template_decl)
14636 finish_lambda_scope ();
14640 /* The old parser allows attributes to appear after a parenthesized
14641 initializer. Mark Mitchell proposed removing this functionality
14642 on the GCC mailing lists on 2002-08-13. This parser accepts the
14643 attributes -- but ignores them. */
14644 if (cp_parser_allow_gnu_extensions_p (parser)
14645 && initialization_kind == CPP_OPEN_PAREN)
14646 if (cp_parser_attributes_opt (parser))
14647 warning (OPT_Wattributes,
14648 "attributes after parenthesized initializer ignored");
14650 /* For an in-class declaration, use `grokfield' to create the
14656 pop_scope (pushed_scope);
14657 pushed_scope = false;
14659 decl = grokfield (declarator, decl_specifiers,
14660 initializer, !is_non_constant_init,
14661 /*asmspec=*/NULL_TREE,
14662 prefix_attributes);
14663 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14664 cp_parser_save_default_args (parser, decl);
14667 /* Finish processing the declaration. But, skip friend
14669 if (!friend_p && decl && decl != error_mark_node)
14671 cp_finish_decl (decl,
14672 initializer, !is_non_constant_init,
14674 /* If the initializer is in parentheses, then this is
14675 a direct-initialization, which means that an
14676 `explicit' constructor is OK. Otherwise, an
14677 `explicit' constructor cannot be used. */
14678 ((is_direct_init || !is_initialized)
14679 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14681 else if ((cxx_dialect != cxx98) && friend_p
14682 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14683 /* Core issue #226 (C++0x only): A default template-argument
14684 shall not be specified in a friend class template
14686 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14687 /*is_partial=*/0, /*is_friend_decl=*/1);
14689 if (!friend_p && pushed_scope)
14690 pop_scope (pushed_scope);
14695 /* Parse a declarator.
14699 ptr-operator declarator
14701 abstract-declarator:
14702 ptr-operator abstract-declarator [opt]
14703 direct-abstract-declarator
14708 attributes [opt] direct-declarator
14709 attributes [opt] ptr-operator declarator
14711 abstract-declarator:
14712 attributes [opt] ptr-operator abstract-declarator [opt]
14713 attributes [opt] direct-abstract-declarator
14715 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14716 detect constructor, destructor or conversion operators. It is set
14717 to -1 if the declarator is a name, and +1 if it is a
14718 function. Otherwise it is set to zero. Usually you just want to
14719 test for >0, but internally the negative value is used.
14721 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14722 a decl-specifier-seq unless it declares a constructor, destructor,
14723 or conversion. It might seem that we could check this condition in
14724 semantic analysis, rather than parsing, but that makes it difficult
14725 to handle something like `f()'. We want to notice that there are
14726 no decl-specifiers, and therefore realize that this is an
14727 expression, not a declaration.)
14729 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14730 the declarator is a direct-declarator of the form "(...)".
14732 MEMBER_P is true iff this declarator is a member-declarator. */
14734 static cp_declarator *
14735 cp_parser_declarator (cp_parser* parser,
14736 cp_parser_declarator_kind dcl_kind,
14737 int* ctor_dtor_or_conv_p,
14738 bool* parenthesized_p,
14741 cp_declarator *declarator;
14742 enum tree_code code;
14743 cp_cv_quals cv_quals;
14745 tree attributes = NULL_TREE;
14747 /* Assume this is not a constructor, destructor, or type-conversion
14749 if (ctor_dtor_or_conv_p)
14750 *ctor_dtor_or_conv_p = 0;
14752 if (cp_parser_allow_gnu_extensions_p (parser))
14753 attributes = cp_parser_attributes_opt (parser);
14755 /* Check for the ptr-operator production. */
14756 cp_parser_parse_tentatively (parser);
14757 /* Parse the ptr-operator. */
14758 code = cp_parser_ptr_operator (parser,
14761 /* If that worked, then we have a ptr-operator. */
14762 if (cp_parser_parse_definitely (parser))
14764 /* If a ptr-operator was found, then this declarator was not
14766 if (parenthesized_p)
14767 *parenthesized_p = true;
14768 /* The dependent declarator is optional if we are parsing an
14769 abstract-declarator. */
14770 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14771 cp_parser_parse_tentatively (parser);
14773 /* Parse the dependent declarator. */
14774 declarator = cp_parser_declarator (parser, dcl_kind,
14775 /*ctor_dtor_or_conv_p=*/NULL,
14776 /*parenthesized_p=*/NULL,
14777 /*member_p=*/false);
14779 /* If we are parsing an abstract-declarator, we must handle the
14780 case where the dependent declarator is absent. */
14781 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14782 && !cp_parser_parse_definitely (parser))
14785 declarator = cp_parser_make_indirect_declarator
14786 (code, class_type, cv_quals, declarator);
14788 /* Everything else is a direct-declarator. */
14791 if (parenthesized_p)
14792 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14794 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14795 ctor_dtor_or_conv_p,
14799 if (attributes && declarator && declarator != cp_error_declarator)
14800 declarator->attributes = attributes;
14805 /* Parse a direct-declarator or direct-abstract-declarator.
14809 direct-declarator ( parameter-declaration-clause )
14810 cv-qualifier-seq [opt]
14811 exception-specification [opt]
14812 direct-declarator [ constant-expression [opt] ]
14815 direct-abstract-declarator:
14816 direct-abstract-declarator [opt]
14817 ( parameter-declaration-clause )
14818 cv-qualifier-seq [opt]
14819 exception-specification [opt]
14820 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14821 ( abstract-declarator )
14823 Returns a representation of the declarator. DCL_KIND is
14824 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14825 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14826 we are parsing a direct-declarator. It is
14827 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14828 of ambiguity we prefer an abstract declarator, as per
14829 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14830 cp_parser_declarator. */
14832 static cp_declarator *
14833 cp_parser_direct_declarator (cp_parser* parser,
14834 cp_parser_declarator_kind dcl_kind,
14835 int* ctor_dtor_or_conv_p,
14839 cp_declarator *declarator = NULL;
14840 tree scope = NULL_TREE;
14841 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14842 bool saved_in_declarator_p = parser->in_declarator_p;
14844 tree pushed_scope = NULL_TREE;
14848 /* Peek at the next token. */
14849 token = cp_lexer_peek_token (parser->lexer);
14850 if (token->type == CPP_OPEN_PAREN)
14852 /* This is either a parameter-declaration-clause, or a
14853 parenthesized declarator. When we know we are parsing a
14854 named declarator, it must be a parenthesized declarator
14855 if FIRST is true. For instance, `(int)' is a
14856 parameter-declaration-clause, with an omitted
14857 direct-abstract-declarator. But `((*))', is a
14858 parenthesized abstract declarator. Finally, when T is a
14859 template parameter `(T)' is a
14860 parameter-declaration-clause, and not a parenthesized
14863 We first try and parse a parameter-declaration-clause,
14864 and then try a nested declarator (if FIRST is true).
14866 It is not an error for it not to be a
14867 parameter-declaration-clause, even when FIRST is
14873 The first is the declaration of a function while the
14874 second is the definition of a variable, including its
14877 Having seen only the parenthesis, we cannot know which of
14878 these two alternatives should be selected. Even more
14879 complex are examples like:
14884 The former is a function-declaration; the latter is a
14885 variable initialization.
14887 Thus again, we try a parameter-declaration-clause, and if
14888 that fails, we back out and return. */
14890 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14893 unsigned saved_num_template_parameter_lists;
14894 bool is_declarator = false;
14897 /* In a member-declarator, the only valid interpretation
14898 of a parenthesis is the start of a
14899 parameter-declaration-clause. (It is invalid to
14900 initialize a static data member with a parenthesized
14901 initializer; only the "=" form of initialization is
14904 cp_parser_parse_tentatively (parser);
14906 /* Consume the `('. */
14907 cp_lexer_consume_token (parser->lexer);
14910 /* If this is going to be an abstract declarator, we're
14911 in a declarator and we can't have default args. */
14912 parser->default_arg_ok_p = false;
14913 parser->in_declarator_p = true;
14916 /* Inside the function parameter list, surrounding
14917 template-parameter-lists do not apply. */
14918 saved_num_template_parameter_lists
14919 = parser->num_template_parameter_lists;
14920 parser->num_template_parameter_lists = 0;
14922 begin_scope (sk_function_parms, NULL_TREE);
14924 /* Parse the parameter-declaration-clause. */
14925 params = cp_parser_parameter_declaration_clause (parser);
14927 parser->num_template_parameter_lists
14928 = saved_num_template_parameter_lists;
14930 /* If all went well, parse the cv-qualifier-seq and the
14931 exception-specification. */
14932 if (member_p || cp_parser_parse_definitely (parser))
14934 cp_cv_quals cv_quals;
14935 tree exception_specification;
14938 is_declarator = true;
14940 if (ctor_dtor_or_conv_p)
14941 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14943 /* Consume the `)'. */
14944 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14946 /* Parse the cv-qualifier-seq. */
14947 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14948 /* And the exception-specification. */
14949 exception_specification
14950 = cp_parser_exception_specification_opt (parser);
14953 = cp_parser_late_return_type_opt (parser);
14955 /* Create the function-declarator. */
14956 declarator = make_call_declarator (declarator,
14959 exception_specification,
14961 /* Any subsequent parameter lists are to do with
14962 return type, so are not those of the declared
14964 parser->default_arg_ok_p = false;
14967 /* Remove the function parms from scope. */
14968 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14969 pop_binding (DECL_NAME (t), t);
14973 /* Repeat the main loop. */
14977 /* If this is the first, we can try a parenthesized
14981 bool saved_in_type_id_in_expr_p;
14983 parser->default_arg_ok_p = saved_default_arg_ok_p;
14984 parser->in_declarator_p = saved_in_declarator_p;
14986 /* Consume the `('. */
14987 cp_lexer_consume_token (parser->lexer);
14988 /* Parse the nested declarator. */
14989 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14990 parser->in_type_id_in_expr_p = true;
14992 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14993 /*parenthesized_p=*/NULL,
14995 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14997 /* Expect a `)'. */
14998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14999 declarator = cp_error_declarator;
15000 if (declarator == cp_error_declarator)
15003 goto handle_declarator;
15005 /* Otherwise, we must be done. */
15009 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15010 && token->type == CPP_OPEN_SQUARE)
15012 /* Parse an array-declarator. */
15015 if (ctor_dtor_or_conv_p)
15016 *ctor_dtor_or_conv_p = 0;
15019 parser->default_arg_ok_p = false;
15020 parser->in_declarator_p = true;
15021 /* Consume the `['. */
15022 cp_lexer_consume_token (parser->lexer);
15023 /* Peek at the next token. */
15024 token = cp_lexer_peek_token (parser->lexer);
15025 /* If the next token is `]', then there is no
15026 constant-expression. */
15027 if (token->type != CPP_CLOSE_SQUARE)
15029 bool non_constant_p;
15032 = cp_parser_constant_expression (parser,
15033 /*allow_non_constant=*/true,
15035 if (!non_constant_p || cxx_dialect >= cxx0x)
15037 /* Normally, the array bound must be an integral constant
15038 expression. However, as an extension, we allow VLAs
15039 in function scopes as long as they aren't part of a
15040 parameter declaration. */
15041 else if (!parser->in_function_body
15042 || current_binding_level->kind == sk_function_parms)
15044 cp_parser_error (parser,
15045 "array bound is not an integer constant");
15046 bounds = error_mark_node;
15048 else if (processing_template_decl && !error_operand_p (bounds))
15050 /* Remember this wasn't a constant-expression. */
15051 bounds = build_nop (TREE_TYPE (bounds), bounds);
15052 TREE_SIDE_EFFECTS (bounds) = 1;
15056 bounds = NULL_TREE;
15057 /* Look for the closing `]'. */
15058 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15060 declarator = cp_error_declarator;
15064 declarator = make_array_declarator (declarator, bounds);
15066 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15069 tree qualifying_scope;
15070 tree unqualified_name;
15071 special_function_kind sfk;
15073 bool pack_expansion_p = false;
15074 cp_token *declarator_id_start_token;
15076 /* Parse a declarator-id */
15077 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15080 cp_parser_parse_tentatively (parser);
15082 /* If we see an ellipsis, we should be looking at a
15084 if (token->type == CPP_ELLIPSIS)
15086 /* Consume the `...' */
15087 cp_lexer_consume_token (parser->lexer);
15089 pack_expansion_p = true;
15093 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15095 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15096 qualifying_scope = parser->scope;
15101 if (!unqualified_name && pack_expansion_p)
15103 /* Check whether an error occurred. */
15104 okay = !cp_parser_error_occurred (parser);
15106 /* We already consumed the ellipsis to mark a
15107 parameter pack, but we have no way to report it,
15108 so abort the tentative parse. We will be exiting
15109 immediately anyway. */
15110 cp_parser_abort_tentative_parse (parser);
15113 okay = cp_parser_parse_definitely (parser);
15116 unqualified_name = error_mark_node;
15117 else if (unqualified_name
15118 && (qualifying_scope
15119 || (TREE_CODE (unqualified_name)
15120 != IDENTIFIER_NODE)))
15122 cp_parser_error (parser, "expected unqualified-id");
15123 unqualified_name = error_mark_node;
15127 if (!unqualified_name)
15129 if (unqualified_name == error_mark_node)
15131 declarator = cp_error_declarator;
15132 pack_expansion_p = false;
15133 declarator->parameter_pack_p = false;
15137 if (qualifying_scope && at_namespace_scope_p ()
15138 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15140 /* In the declaration of a member of a template class
15141 outside of the class itself, the SCOPE will sometimes
15142 be a TYPENAME_TYPE. For example, given:
15144 template <typename T>
15145 int S<T>::R::i = 3;
15147 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15148 this context, we must resolve S<T>::R to an ordinary
15149 type, rather than a typename type.
15151 The reason we normally avoid resolving TYPENAME_TYPEs
15152 is that a specialization of `S' might render
15153 `S<T>::R' not a type. However, if `S' is
15154 specialized, then this `i' will not be used, so there
15155 is no harm in resolving the types here. */
15158 /* Resolve the TYPENAME_TYPE. */
15159 type = resolve_typename_type (qualifying_scope,
15160 /*only_current_p=*/false);
15161 /* If that failed, the declarator is invalid. */
15162 if (TREE_CODE (type) == TYPENAME_TYPE)
15164 if (typedef_variant_p (type))
15165 error_at (declarator_id_start_token->location,
15166 "cannot define member of dependent typedef "
15169 error_at (declarator_id_start_token->location,
15170 "%<%T::%E%> is not a type",
15171 TYPE_CONTEXT (qualifying_scope),
15172 TYPE_IDENTIFIER (qualifying_scope));
15174 qualifying_scope = type;
15179 if (unqualified_name)
15183 if (qualifying_scope
15184 && CLASS_TYPE_P (qualifying_scope))
15185 class_type = qualifying_scope;
15187 class_type = current_class_type;
15189 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15191 tree name_type = TREE_TYPE (unqualified_name);
15192 if (class_type && same_type_p (name_type, class_type))
15194 if (qualifying_scope
15195 && CLASSTYPE_USE_TEMPLATE (name_type))
15197 error_at (declarator_id_start_token->location,
15198 "invalid use of constructor as a template");
15199 inform (declarator_id_start_token->location,
15200 "use %<%T::%D%> instead of %<%T::%D%> to "
15201 "name the constructor in a qualified name",
15203 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15204 class_type, name_type);
15205 declarator = cp_error_declarator;
15209 unqualified_name = constructor_name (class_type);
15213 /* We do not attempt to print the declarator
15214 here because we do not have enough
15215 information about its original syntactic
15217 cp_parser_error (parser, "invalid declarator");
15218 declarator = cp_error_declarator;
15225 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15226 sfk = sfk_destructor;
15227 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15228 sfk = sfk_conversion;
15229 else if (/* There's no way to declare a constructor
15230 for an anonymous type, even if the type
15231 got a name for linkage purposes. */
15232 !TYPE_WAS_ANONYMOUS (class_type)
15233 && constructor_name_p (unqualified_name,
15236 unqualified_name = constructor_name (class_type);
15237 sfk = sfk_constructor;
15239 else if (is_overloaded_fn (unqualified_name)
15240 && DECL_CONSTRUCTOR_P (get_first_fn
15241 (unqualified_name)))
15242 sfk = sfk_constructor;
15244 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15245 *ctor_dtor_or_conv_p = -1;
15248 declarator = make_id_declarator (qualifying_scope,
15251 declarator->id_loc = token->location;
15252 declarator->parameter_pack_p = pack_expansion_p;
15254 if (pack_expansion_p)
15255 maybe_warn_variadic_templates ();
15258 handle_declarator:;
15259 scope = get_scope_of_declarator (declarator);
15261 /* Any names that appear after the declarator-id for a
15262 member are looked up in the containing scope. */
15263 pushed_scope = push_scope (scope);
15264 parser->in_declarator_p = true;
15265 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15266 || (declarator && declarator->kind == cdk_id))
15267 /* Default args are only allowed on function
15269 parser->default_arg_ok_p = saved_default_arg_ok_p;
15271 parser->default_arg_ok_p = false;
15280 /* For an abstract declarator, we might wind up with nothing at this
15281 point. That's an error; the declarator is not optional. */
15283 cp_parser_error (parser, "expected declarator");
15285 /* If we entered a scope, we must exit it now. */
15287 pop_scope (pushed_scope);
15289 parser->default_arg_ok_p = saved_default_arg_ok_p;
15290 parser->in_declarator_p = saved_in_declarator_p;
15295 /* Parse a ptr-operator.
15298 * cv-qualifier-seq [opt]
15300 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15305 & cv-qualifier-seq [opt]
15307 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15308 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15309 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15310 filled in with the TYPE containing the member. *CV_QUALS is
15311 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15312 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15313 Note that the tree codes returned by this function have nothing
15314 to do with the types of trees that will be eventually be created
15315 to represent the pointer or reference type being parsed. They are
15316 just constants with suggestive names. */
15317 static enum tree_code
15318 cp_parser_ptr_operator (cp_parser* parser,
15320 cp_cv_quals *cv_quals)
15322 enum tree_code code = ERROR_MARK;
15325 /* Assume that it's not a pointer-to-member. */
15327 /* And that there are no cv-qualifiers. */
15328 *cv_quals = TYPE_UNQUALIFIED;
15330 /* Peek at the next token. */
15331 token = cp_lexer_peek_token (parser->lexer);
15333 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15334 if (token->type == CPP_MULT)
15335 code = INDIRECT_REF;
15336 else if (token->type == CPP_AND)
15338 else if ((cxx_dialect != cxx98) &&
15339 token->type == CPP_AND_AND) /* C++0x only */
15340 code = NON_LVALUE_EXPR;
15342 if (code != ERROR_MARK)
15344 /* Consume the `*', `&' or `&&'. */
15345 cp_lexer_consume_token (parser->lexer);
15347 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15348 `&', if we are allowing GNU extensions. (The only qualifier
15349 that can legally appear after `&' is `restrict', but that is
15350 enforced during semantic analysis. */
15351 if (code == INDIRECT_REF
15352 || cp_parser_allow_gnu_extensions_p (parser))
15353 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15357 /* Try the pointer-to-member case. */
15358 cp_parser_parse_tentatively (parser);
15359 /* Look for the optional `::' operator. */
15360 cp_parser_global_scope_opt (parser,
15361 /*current_scope_valid_p=*/false);
15362 /* Look for the nested-name specifier. */
15363 token = cp_lexer_peek_token (parser->lexer);
15364 cp_parser_nested_name_specifier (parser,
15365 /*typename_keyword_p=*/false,
15366 /*check_dependency_p=*/true,
15368 /*is_declaration=*/false);
15369 /* If we found it, and the next token is a `*', then we are
15370 indeed looking at a pointer-to-member operator. */
15371 if (!cp_parser_error_occurred (parser)
15372 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15374 /* Indicate that the `*' operator was used. */
15375 code = INDIRECT_REF;
15377 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15378 error_at (token->location, "%qD is a namespace", parser->scope);
15381 /* The type of which the member is a member is given by the
15383 *type = parser->scope;
15384 /* The next name will not be qualified. */
15385 parser->scope = NULL_TREE;
15386 parser->qualifying_scope = NULL_TREE;
15387 parser->object_scope = NULL_TREE;
15388 /* Look for the optional cv-qualifier-seq. */
15389 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15392 /* If that didn't work we don't have a ptr-operator. */
15393 if (!cp_parser_parse_definitely (parser))
15394 cp_parser_error (parser, "expected ptr-operator");
15400 /* Parse an (optional) cv-qualifier-seq.
15403 cv-qualifier cv-qualifier-seq [opt]
15414 Returns a bitmask representing the cv-qualifiers. */
15417 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15419 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15424 cp_cv_quals cv_qualifier;
15426 /* Peek at the next token. */
15427 token = cp_lexer_peek_token (parser->lexer);
15428 /* See if it's a cv-qualifier. */
15429 switch (token->keyword)
15432 cv_qualifier = TYPE_QUAL_CONST;
15436 cv_qualifier = TYPE_QUAL_VOLATILE;
15440 cv_qualifier = TYPE_QUAL_RESTRICT;
15444 cv_qualifier = TYPE_UNQUALIFIED;
15451 if (cv_quals & cv_qualifier)
15453 error_at (token->location, "duplicate cv-qualifier");
15454 cp_lexer_purge_token (parser->lexer);
15458 cp_lexer_consume_token (parser->lexer);
15459 cv_quals |= cv_qualifier;
15466 /* Parse a late-specified return type, if any. This is not a separate
15467 non-terminal, but part of a function declarator, which looks like
15469 -> trailing-type-specifier-seq abstract-declarator(opt)
15471 Returns the type indicated by the type-id. */
15474 cp_parser_late_return_type_opt (cp_parser* parser)
15478 /* Peek at the next token. */
15479 token = cp_lexer_peek_token (parser->lexer);
15480 /* A late-specified return type is indicated by an initial '->'. */
15481 if (token->type != CPP_DEREF)
15484 /* Consume the ->. */
15485 cp_lexer_consume_token (parser->lexer);
15487 return cp_parser_trailing_type_id (parser);
15490 /* Parse a declarator-id.
15494 :: [opt] nested-name-specifier [opt] type-name
15496 In the `id-expression' case, the value returned is as for
15497 cp_parser_id_expression if the id-expression was an unqualified-id.
15498 If the id-expression was a qualified-id, then a SCOPE_REF is
15499 returned. The first operand is the scope (either a NAMESPACE_DECL
15500 or TREE_TYPE), but the second is still just a representation of an
15504 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15507 /* The expression must be an id-expression. Assume that qualified
15508 names are the names of types so that:
15511 int S<T>::R::i = 3;
15513 will work; we must treat `S<T>::R' as the name of a type.
15514 Similarly, assume that qualified names are templates, where
15518 int S<T>::R<T>::i = 3;
15521 id = cp_parser_id_expression (parser,
15522 /*template_keyword_p=*/false,
15523 /*check_dependency_p=*/false,
15524 /*template_p=*/NULL,
15525 /*declarator_p=*/true,
15527 if (id && BASELINK_P (id))
15528 id = BASELINK_FUNCTIONS (id);
15532 /* Parse a type-id.
15535 type-specifier-seq abstract-declarator [opt]
15537 Returns the TYPE specified. */
15540 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15541 bool is_trailing_return)
15543 cp_decl_specifier_seq type_specifier_seq;
15544 cp_declarator *abstract_declarator;
15546 /* Parse the type-specifier-seq. */
15547 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15548 is_trailing_return,
15549 &type_specifier_seq);
15550 if (type_specifier_seq.type == error_mark_node)
15551 return error_mark_node;
15553 /* There might or might not be an abstract declarator. */
15554 cp_parser_parse_tentatively (parser);
15555 /* Look for the declarator. */
15556 abstract_declarator
15557 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15558 /*parenthesized_p=*/NULL,
15559 /*member_p=*/false);
15560 /* Check to see if there really was a declarator. */
15561 if (!cp_parser_parse_definitely (parser))
15562 abstract_declarator = NULL;
15564 if (type_specifier_seq.type
15565 && type_uses_auto (type_specifier_seq.type))
15567 /* A type-id with type 'auto' is only ok if the abstract declarator
15568 is a function declarator with a late-specified return type. */
15569 if (abstract_declarator
15570 && abstract_declarator->kind == cdk_function
15571 && abstract_declarator->u.function.late_return_type)
15575 error ("invalid use of %<auto%>");
15576 return error_mark_node;
15580 return groktypename (&type_specifier_seq, abstract_declarator,
15584 static tree cp_parser_type_id (cp_parser *parser)
15586 return cp_parser_type_id_1 (parser, false, false);
15589 static tree cp_parser_template_type_arg (cp_parser *parser)
15591 return cp_parser_type_id_1 (parser, true, false);
15594 static tree cp_parser_trailing_type_id (cp_parser *parser)
15596 return cp_parser_type_id_1 (parser, false, true);
15599 /* Parse a type-specifier-seq.
15601 type-specifier-seq:
15602 type-specifier type-specifier-seq [opt]
15606 type-specifier-seq:
15607 attributes type-specifier-seq [opt]
15609 If IS_DECLARATION is true, we are at the start of a "condition" or
15610 exception-declaration, so we might be followed by a declarator-id.
15612 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15613 i.e. we've just seen "->".
15615 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15618 cp_parser_type_specifier_seq (cp_parser* parser,
15619 bool is_declaration,
15620 bool is_trailing_return,
15621 cp_decl_specifier_seq *type_specifier_seq)
15623 bool seen_type_specifier = false;
15624 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15625 cp_token *start_token = NULL;
15627 /* Clear the TYPE_SPECIFIER_SEQ. */
15628 clear_decl_specs (type_specifier_seq);
15630 /* In the context of a trailing return type, enum E { } is an
15631 elaborated-type-specifier followed by a function-body, not an
15633 if (is_trailing_return)
15634 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15636 /* Parse the type-specifiers and attributes. */
15639 tree type_specifier;
15640 bool is_cv_qualifier;
15642 /* Check for attributes first. */
15643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15645 type_specifier_seq->attributes =
15646 chainon (type_specifier_seq->attributes,
15647 cp_parser_attributes_opt (parser));
15651 /* record the token of the beginning of the type specifier seq,
15652 for error reporting purposes*/
15654 start_token = cp_lexer_peek_token (parser->lexer);
15656 /* Look for the type-specifier. */
15657 type_specifier = cp_parser_type_specifier (parser,
15659 type_specifier_seq,
15660 /*is_declaration=*/false,
15663 if (!type_specifier)
15665 /* If the first type-specifier could not be found, this is not a
15666 type-specifier-seq at all. */
15667 if (!seen_type_specifier)
15669 cp_parser_error (parser, "expected type-specifier");
15670 type_specifier_seq->type = error_mark_node;
15673 /* If subsequent type-specifiers could not be found, the
15674 type-specifier-seq is complete. */
15678 seen_type_specifier = true;
15679 /* The standard says that a condition can be:
15681 type-specifier-seq declarator = assignment-expression
15688 we should treat the "S" as a declarator, not as a
15689 type-specifier. The standard doesn't say that explicitly for
15690 type-specifier-seq, but it does say that for
15691 decl-specifier-seq in an ordinary declaration. Perhaps it
15692 would be clearer just to allow a decl-specifier-seq here, and
15693 then add a semantic restriction that if any decl-specifiers
15694 that are not type-specifiers appear, the program is invalid. */
15695 if (is_declaration && !is_cv_qualifier)
15696 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15699 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15702 /* Parse a parameter-declaration-clause.
15704 parameter-declaration-clause:
15705 parameter-declaration-list [opt] ... [opt]
15706 parameter-declaration-list , ...
15708 Returns a representation for the parameter declarations. A return
15709 value of NULL indicates a parameter-declaration-clause consisting
15710 only of an ellipsis. */
15713 cp_parser_parameter_declaration_clause (cp_parser* parser)
15720 /* Peek at the next token. */
15721 token = cp_lexer_peek_token (parser->lexer);
15722 /* Check for trivial parameter-declaration-clauses. */
15723 if (token->type == CPP_ELLIPSIS)
15725 /* Consume the `...' token. */
15726 cp_lexer_consume_token (parser->lexer);
15729 else if (token->type == CPP_CLOSE_PAREN)
15730 /* There are no parameters. */
15732 #ifndef NO_IMPLICIT_EXTERN_C
15733 if (in_system_header && current_class_type == NULL
15734 && current_lang_name == lang_name_c)
15738 return void_list_node;
15740 /* Check for `(void)', too, which is a special case. */
15741 else if (token->keyword == RID_VOID
15742 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15743 == CPP_CLOSE_PAREN))
15745 /* Consume the `void' token. */
15746 cp_lexer_consume_token (parser->lexer);
15747 /* There are no parameters. */
15748 return void_list_node;
15751 /* Parse the parameter-declaration-list. */
15752 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15753 /* If a parse error occurred while parsing the
15754 parameter-declaration-list, then the entire
15755 parameter-declaration-clause is erroneous. */
15759 /* Peek at the next token. */
15760 token = cp_lexer_peek_token (parser->lexer);
15761 /* If it's a `,', the clause should terminate with an ellipsis. */
15762 if (token->type == CPP_COMMA)
15764 /* Consume the `,'. */
15765 cp_lexer_consume_token (parser->lexer);
15766 /* Expect an ellipsis. */
15768 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15770 /* It might also be `...' if the optional trailing `,' was
15772 else if (token->type == CPP_ELLIPSIS)
15774 /* Consume the `...' token. */
15775 cp_lexer_consume_token (parser->lexer);
15776 /* And remember that we saw it. */
15780 ellipsis_p = false;
15782 /* Finish the parameter list. */
15784 parameters = chainon (parameters, void_list_node);
15789 /* Parse a parameter-declaration-list.
15791 parameter-declaration-list:
15792 parameter-declaration
15793 parameter-declaration-list , parameter-declaration
15795 Returns a representation of the parameter-declaration-list, as for
15796 cp_parser_parameter_declaration_clause. However, the
15797 `void_list_node' is never appended to the list. Upon return,
15798 *IS_ERROR will be true iff an error occurred. */
15801 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15803 tree parameters = NULL_TREE;
15804 tree *tail = ¶meters;
15805 bool saved_in_unbraced_linkage_specification_p;
15808 /* Assume all will go well. */
15810 /* The special considerations that apply to a function within an
15811 unbraced linkage specifications do not apply to the parameters
15812 to the function. */
15813 saved_in_unbraced_linkage_specification_p
15814 = parser->in_unbraced_linkage_specification_p;
15815 parser->in_unbraced_linkage_specification_p = false;
15817 /* Look for more parameters. */
15820 cp_parameter_declarator *parameter;
15821 tree decl = error_mark_node;
15822 bool parenthesized_p;
15823 /* Parse the parameter. */
15825 = cp_parser_parameter_declaration (parser,
15826 /*template_parm_p=*/false,
15829 /* We don't know yet if the enclosing context is deprecated, so wait
15830 and warn in grokparms if appropriate. */
15831 deprecated_state = DEPRECATED_SUPPRESS;
15834 decl = grokdeclarator (parameter->declarator,
15835 ¶meter->decl_specifiers,
15837 parameter->default_argument != NULL_TREE,
15838 ¶meter->decl_specifiers.attributes);
15840 deprecated_state = DEPRECATED_NORMAL;
15842 /* If a parse error occurred parsing the parameter declaration,
15843 then the entire parameter-declaration-list is erroneous. */
15844 if (decl == error_mark_node)
15847 parameters = error_mark_node;
15851 if (parameter->decl_specifiers.attributes)
15852 cplus_decl_attributes (&decl,
15853 parameter->decl_specifiers.attributes,
15855 if (DECL_NAME (decl))
15856 decl = pushdecl (decl);
15858 if (decl != error_mark_node)
15860 retrofit_lang_decl (decl);
15861 DECL_PARM_INDEX (decl) = ++index;
15864 /* Add the new parameter to the list. */
15865 *tail = build_tree_list (parameter->default_argument, decl);
15866 tail = &TREE_CHAIN (*tail);
15868 /* Peek at the next token. */
15869 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15870 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15871 /* These are for Objective-C++ */
15872 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15873 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15874 /* The parameter-declaration-list is complete. */
15876 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15880 /* Peek at the next token. */
15881 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15882 /* If it's an ellipsis, then the list is complete. */
15883 if (token->type == CPP_ELLIPSIS)
15885 /* Otherwise, there must be more parameters. Consume the
15887 cp_lexer_consume_token (parser->lexer);
15888 /* When parsing something like:
15890 int i(float f, double d)
15892 we can tell after seeing the declaration for "f" that we
15893 are not looking at an initialization of a variable "i",
15894 but rather at the declaration of a function "i".
15896 Due to the fact that the parsing of template arguments
15897 (as specified to a template-id) requires backtracking we
15898 cannot use this technique when inside a template argument
15900 if (!parser->in_template_argument_list_p
15901 && !parser->in_type_id_in_expr_p
15902 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15903 /* However, a parameter-declaration of the form
15904 "foat(f)" (which is a valid declaration of a
15905 parameter "f") can also be interpreted as an
15906 expression (the conversion of "f" to "float"). */
15907 && !parenthesized_p)
15908 cp_parser_commit_to_tentative_parse (parser);
15912 cp_parser_error (parser, "expected %<,%> or %<...%>");
15913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15914 cp_parser_skip_to_closing_parenthesis (parser,
15915 /*recovering=*/true,
15916 /*or_comma=*/false,
15917 /*consume_paren=*/false);
15922 parser->in_unbraced_linkage_specification_p
15923 = saved_in_unbraced_linkage_specification_p;
15928 /* Parse a parameter declaration.
15930 parameter-declaration:
15931 decl-specifier-seq ... [opt] declarator
15932 decl-specifier-seq declarator = assignment-expression
15933 decl-specifier-seq ... [opt] abstract-declarator [opt]
15934 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15936 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15937 declares a template parameter. (In that case, a non-nested `>'
15938 token encountered during the parsing of the assignment-expression
15939 is not interpreted as a greater-than operator.)
15941 Returns a representation of the parameter, or NULL if an error
15942 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15943 true iff the declarator is of the form "(p)". */
15945 static cp_parameter_declarator *
15946 cp_parser_parameter_declaration (cp_parser *parser,
15947 bool template_parm_p,
15948 bool *parenthesized_p)
15950 int declares_class_or_enum;
15951 cp_decl_specifier_seq decl_specifiers;
15952 cp_declarator *declarator;
15953 tree default_argument;
15954 cp_token *token = NULL, *declarator_token_start = NULL;
15955 const char *saved_message;
15957 /* In a template parameter, `>' is not an operator.
15961 When parsing a default template-argument for a non-type
15962 template-parameter, the first non-nested `>' is taken as the end
15963 of the template parameter-list rather than a greater-than
15966 /* Type definitions may not appear in parameter types. */
15967 saved_message = parser->type_definition_forbidden_message;
15968 parser->type_definition_forbidden_message
15969 = G_("types may not be defined in parameter types");
15971 /* Parse the declaration-specifiers. */
15972 cp_parser_decl_specifier_seq (parser,
15973 CP_PARSER_FLAGS_NONE,
15975 &declares_class_or_enum);
15977 /* Complain about missing 'typename' or other invalid type names. */
15978 if (!decl_specifiers.any_type_specifiers_p)
15979 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15981 /* If an error occurred, there's no reason to attempt to parse the
15982 rest of the declaration. */
15983 if (cp_parser_error_occurred (parser))
15985 parser->type_definition_forbidden_message = saved_message;
15989 /* Peek at the next token. */
15990 token = cp_lexer_peek_token (parser->lexer);
15992 /* If the next token is a `)', `,', `=', `>', or `...', then there
15993 is no declarator. However, when variadic templates are enabled,
15994 there may be a declarator following `...'. */
15995 if (token->type == CPP_CLOSE_PAREN
15996 || token->type == CPP_COMMA
15997 || token->type == CPP_EQ
15998 || token->type == CPP_GREATER)
16001 if (parenthesized_p)
16002 *parenthesized_p = false;
16004 /* Otherwise, there should be a declarator. */
16007 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16008 parser->default_arg_ok_p = false;
16010 /* After seeing a decl-specifier-seq, if the next token is not a
16011 "(", there is no possibility that the code is a valid
16012 expression. Therefore, if parsing tentatively, we commit at
16014 if (!parser->in_template_argument_list_p
16015 /* In an expression context, having seen:
16019 we cannot be sure whether we are looking at a
16020 function-type (taking a "char" as a parameter) or a cast
16021 of some object of type "char" to "int". */
16022 && !parser->in_type_id_in_expr_p
16023 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16024 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16025 cp_parser_commit_to_tentative_parse (parser);
16026 /* Parse the declarator. */
16027 declarator_token_start = token;
16028 declarator = cp_parser_declarator (parser,
16029 CP_PARSER_DECLARATOR_EITHER,
16030 /*ctor_dtor_or_conv_p=*/NULL,
16032 /*member_p=*/false);
16033 parser->default_arg_ok_p = saved_default_arg_ok_p;
16034 /* After the declarator, allow more attributes. */
16035 decl_specifiers.attributes
16036 = chainon (decl_specifiers.attributes,
16037 cp_parser_attributes_opt (parser));
16040 /* If the next token is an ellipsis, and we have not seen a
16041 declarator name, and the type of the declarator contains parameter
16042 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16043 a parameter pack expansion expression. Otherwise, leave the
16044 ellipsis for a C-style variadic function. */
16045 token = cp_lexer_peek_token (parser->lexer);
16046 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16048 tree type = decl_specifiers.type;
16050 if (type && DECL_P (type))
16051 type = TREE_TYPE (type);
16054 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16055 && declarator_can_be_parameter_pack (declarator)
16056 && (!declarator || !declarator->parameter_pack_p)
16057 && uses_parameter_packs (type))
16059 /* Consume the `...'. */
16060 cp_lexer_consume_token (parser->lexer);
16061 maybe_warn_variadic_templates ();
16063 /* Build a pack expansion type */
16065 declarator->parameter_pack_p = true;
16067 decl_specifiers.type = make_pack_expansion (type);
16071 /* The restriction on defining new types applies only to the type
16072 of the parameter, not to the default argument. */
16073 parser->type_definition_forbidden_message = saved_message;
16075 /* If the next token is `=', then process a default argument. */
16076 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16078 /* Consume the `='. */
16079 cp_lexer_consume_token (parser->lexer);
16081 /* If we are defining a class, then the tokens that make up the
16082 default argument must be saved and processed later. */
16083 if (!template_parm_p && at_class_scope_p ()
16084 && TYPE_BEING_DEFINED (current_class_type)
16085 && !LAMBDA_TYPE_P (current_class_type))
16087 unsigned depth = 0;
16088 int maybe_template_id = 0;
16089 cp_token *first_token;
16092 /* Add tokens until we have processed the entire default
16093 argument. We add the range [first_token, token). */
16094 first_token = cp_lexer_peek_token (parser->lexer);
16099 /* Peek at the next token. */
16100 token = cp_lexer_peek_token (parser->lexer);
16101 /* What we do depends on what token we have. */
16102 switch (token->type)
16104 /* In valid code, a default argument must be
16105 immediately followed by a `,' `)', or `...'. */
16107 if (depth == 0 && maybe_template_id)
16109 /* If we've seen a '<', we might be in a
16110 template-argument-list. Until Core issue 325 is
16111 resolved, we don't know how this situation ought
16112 to be handled, so try to DTRT. We check whether
16113 what comes after the comma is a valid parameter
16114 declaration list. If it is, then the comma ends
16115 the default argument; otherwise the default
16116 argument continues. */
16117 bool error = false;
16120 /* Set ITALP so cp_parser_parameter_declaration_list
16121 doesn't decide to commit to this parse. */
16122 bool saved_italp = parser->in_template_argument_list_p;
16123 parser->in_template_argument_list_p = true;
16125 cp_parser_parse_tentatively (parser);
16126 cp_lexer_consume_token (parser->lexer);
16127 begin_scope (sk_function_parms, NULL_TREE);
16128 cp_parser_parameter_declaration_list (parser, &error);
16129 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16130 pop_binding (DECL_NAME (t), t);
16132 if (!cp_parser_error_occurred (parser) && !error)
16134 cp_parser_abort_tentative_parse (parser);
16136 parser->in_template_argument_list_p = saved_italp;
16139 case CPP_CLOSE_PAREN:
16141 /* If we run into a non-nested `;', `}', or `]',
16142 then the code is invalid -- but the default
16143 argument is certainly over. */
16144 case CPP_SEMICOLON:
16145 case CPP_CLOSE_BRACE:
16146 case CPP_CLOSE_SQUARE:
16149 /* Update DEPTH, if necessary. */
16150 else if (token->type == CPP_CLOSE_PAREN
16151 || token->type == CPP_CLOSE_BRACE
16152 || token->type == CPP_CLOSE_SQUARE)
16156 case CPP_OPEN_PAREN:
16157 case CPP_OPEN_SQUARE:
16158 case CPP_OPEN_BRACE:
16164 /* This might be the comparison operator, or it might
16165 start a template argument list. */
16166 ++maybe_template_id;
16170 if (cxx_dialect == cxx98)
16172 /* Fall through for C++0x, which treats the `>>'
16173 operator like two `>' tokens in certain
16179 /* This might be an operator, or it might close a
16180 template argument list. But if a previous '<'
16181 started a template argument list, this will have
16182 closed it, so we can't be in one anymore. */
16183 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16184 if (maybe_template_id < 0)
16185 maybe_template_id = 0;
16189 /* If we run out of tokens, issue an error message. */
16191 case CPP_PRAGMA_EOL:
16192 error_at (token->location, "file ends in default argument");
16198 /* In these cases, we should look for template-ids.
16199 For example, if the default argument is
16200 `X<int, double>()', we need to do name lookup to
16201 figure out whether or not `X' is a template; if
16202 so, the `,' does not end the default argument.
16204 That is not yet done. */
16211 /* If we've reached the end, stop. */
16215 /* Add the token to the token block. */
16216 token = cp_lexer_consume_token (parser->lexer);
16219 /* Create a DEFAULT_ARG to represent the unparsed default
16221 default_argument = make_node (DEFAULT_ARG);
16222 DEFARG_TOKENS (default_argument)
16223 = cp_token_cache_new (first_token, token);
16224 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16226 /* Outside of a class definition, we can just parse the
16227 assignment-expression. */
16230 token = cp_lexer_peek_token (parser->lexer);
16232 = cp_parser_default_argument (parser, template_parm_p);
16235 if (!parser->default_arg_ok_p)
16237 if (flag_permissive)
16238 warning (0, "deprecated use of default argument for parameter of non-function");
16241 error_at (token->location,
16242 "default arguments are only "
16243 "permitted for function parameters");
16244 default_argument = NULL_TREE;
16247 else if ((declarator && declarator->parameter_pack_p)
16248 || (decl_specifiers.type
16249 && PACK_EXPANSION_P (decl_specifiers.type)))
16251 /* Find the name of the parameter pack. */
16252 cp_declarator *id_declarator = declarator;
16253 while (id_declarator && id_declarator->kind != cdk_id)
16254 id_declarator = id_declarator->declarator;
16256 if (id_declarator && id_declarator->kind == cdk_id)
16257 error_at (declarator_token_start->location,
16259 ? "template parameter pack %qD"
16260 " cannot have a default argument"
16261 : "parameter pack %qD cannot have a default argument",
16262 id_declarator->u.id.unqualified_name);
16264 error_at (declarator_token_start->location,
16266 ? "template parameter pack cannot have a default argument"
16267 : "parameter pack cannot have a default argument");
16269 default_argument = NULL_TREE;
16273 default_argument = NULL_TREE;
16275 return make_parameter_declarator (&decl_specifiers,
16280 /* Parse a default argument and return it.
16282 TEMPLATE_PARM_P is true if this is a default argument for a
16283 non-type template parameter. */
16285 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16287 tree default_argument = NULL_TREE;
16288 bool saved_greater_than_is_operator_p;
16289 bool saved_local_variables_forbidden_p;
16291 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16293 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16294 parser->greater_than_is_operator_p = !template_parm_p;
16295 /* Local variable names (and the `this' keyword) may not
16296 appear in a default argument. */
16297 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16298 parser->local_variables_forbidden_p = true;
16299 /* Parse the assignment-expression. */
16300 if (template_parm_p)
16301 push_deferring_access_checks (dk_no_deferred);
16303 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16304 if (template_parm_p)
16305 pop_deferring_access_checks ();
16306 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16307 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16309 return default_argument;
16312 /* Parse a function-body.
16315 compound_statement */
16318 cp_parser_function_body (cp_parser *parser)
16320 cp_parser_compound_statement (parser, NULL, false);
16323 /* Parse a ctor-initializer-opt followed by a function-body. Return
16324 true if a ctor-initializer was present. */
16327 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16330 bool ctor_initializer_p;
16331 const bool check_body_p =
16332 DECL_CONSTRUCTOR_P (current_function_decl)
16333 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16336 /* Begin the function body. */
16337 body = begin_function_body ();
16338 /* Parse the optional ctor-initializer. */
16339 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16341 /* If we're parsing a constexpr constructor definition, we need
16342 to check that the constructor body is indeed empty. However,
16343 before we get to cp_parser_function_body lot of junk has been
16344 generated, so we can't just check that we have an empty block.
16345 Rather we take a snapshot of the outermost block, and check whether
16346 cp_parser_function_body changed its state. */
16350 if (TREE_CODE (list) == BIND_EXPR)
16351 list = BIND_EXPR_BODY (list);
16352 if (TREE_CODE (list) == STATEMENT_LIST
16353 && STATEMENT_LIST_TAIL (list) != NULL)
16354 last = STATEMENT_LIST_TAIL (list)->stmt;
16356 /* Parse the function-body. */
16357 cp_parser_function_body (parser);
16359 check_constexpr_ctor_body (last, list);
16360 /* Finish the function body. */
16361 finish_function_body (body);
16363 return ctor_initializer_p;
16366 /* Parse an initializer.
16369 = initializer-clause
16370 ( expression-list )
16372 Returns an expression representing the initializer. If no
16373 initializer is present, NULL_TREE is returned.
16375 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16376 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16377 set to TRUE if there is no initializer present. If there is an
16378 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16379 is set to true; otherwise it is set to false. */
16382 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16383 bool* non_constant_p)
16388 /* Peek at the next token. */
16389 token = cp_lexer_peek_token (parser->lexer);
16391 /* Let our caller know whether or not this initializer was
16393 *is_direct_init = (token->type != CPP_EQ);
16394 /* Assume that the initializer is constant. */
16395 *non_constant_p = false;
16397 if (token->type == CPP_EQ)
16399 /* Consume the `='. */
16400 cp_lexer_consume_token (parser->lexer);
16401 /* Parse the initializer-clause. */
16402 init = cp_parser_initializer_clause (parser, non_constant_p);
16404 else if (token->type == CPP_OPEN_PAREN)
16407 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16409 /*allow_expansion_p=*/true,
16412 return error_mark_node;
16413 init = build_tree_list_vec (vec);
16414 release_tree_vector (vec);
16416 else if (token->type == CPP_OPEN_BRACE)
16418 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16419 init = cp_parser_braced_list (parser, non_constant_p);
16420 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16424 /* Anything else is an error. */
16425 cp_parser_error (parser, "expected initializer");
16426 init = error_mark_node;
16432 /* Parse an initializer-clause.
16434 initializer-clause:
16435 assignment-expression
16438 Returns an expression representing the initializer.
16440 If the `assignment-expression' production is used the value
16441 returned is simply a representation for the expression.
16443 Otherwise, calls cp_parser_braced_list. */
16446 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16450 /* Assume the expression is constant. */
16451 *non_constant_p = false;
16453 /* If it is not a `{', then we are looking at an
16454 assignment-expression. */
16455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16458 = cp_parser_constant_expression (parser,
16459 /*allow_non_constant_p=*/true,
16461 if (!*non_constant_p)
16463 /* We only want to fold if this is really a constant
16464 expression. FIXME Actually, we don't want to fold here, but in
16466 tree folded = fold_non_dependent_expr (initializer);
16467 folded = maybe_constant_value (folded);
16468 if (TREE_CONSTANT (folded))
16469 initializer = folded;
16473 initializer = cp_parser_braced_list (parser, non_constant_p);
16475 return initializer;
16478 /* Parse a brace-enclosed initializer list.
16481 { initializer-list , [opt] }
16484 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16485 the elements of the initializer-list (or NULL, if the last
16486 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16487 NULL_TREE. There is no way to detect whether or not the optional
16488 trailing `,' was provided. NON_CONSTANT_P is as for
16489 cp_parser_initializer. */
16492 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16496 /* Consume the `{' token. */
16497 cp_lexer_consume_token (parser->lexer);
16498 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16499 initializer = make_node (CONSTRUCTOR);
16500 /* If it's not a `}', then there is a non-trivial initializer. */
16501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16503 /* Parse the initializer list. */
16504 CONSTRUCTOR_ELTS (initializer)
16505 = cp_parser_initializer_list (parser, non_constant_p);
16506 /* A trailing `,' token is allowed. */
16507 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16508 cp_lexer_consume_token (parser->lexer);
16510 /* Now, there should be a trailing `}'. */
16511 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16512 TREE_TYPE (initializer) = init_list_type_node;
16513 return initializer;
16516 /* Parse an initializer-list.
16519 initializer-clause ... [opt]
16520 initializer-list , initializer-clause ... [opt]
16525 identifier : initializer-clause
16526 initializer-list, identifier : initializer-clause
16528 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16529 for the initializer. If the INDEX of the elt is non-NULL, it is the
16530 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16531 as for cp_parser_initializer. */
16533 static VEC(constructor_elt,gc) *
16534 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16536 VEC(constructor_elt,gc) *v = NULL;
16538 /* Assume all of the expressions are constant. */
16539 *non_constant_p = false;
16541 /* Parse the rest of the list. */
16547 bool clause_non_constant_p;
16549 /* If the next token is an identifier and the following one is a
16550 colon, we are looking at the GNU designated-initializer
16552 if (cp_parser_allow_gnu_extensions_p (parser)
16553 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16554 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16556 /* Warn the user that they are using an extension. */
16557 pedwarn (input_location, OPT_pedantic,
16558 "ISO C++ does not allow designated initializers");
16559 /* Consume the identifier. */
16560 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16561 /* Consume the `:'. */
16562 cp_lexer_consume_token (parser->lexer);
16565 identifier = NULL_TREE;
16567 /* Parse the initializer. */
16568 initializer = cp_parser_initializer_clause (parser,
16569 &clause_non_constant_p);
16570 /* If any clause is non-constant, so is the entire initializer. */
16571 if (clause_non_constant_p)
16572 *non_constant_p = true;
16574 /* If we have an ellipsis, this is an initializer pack
16576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16578 /* Consume the `...'. */
16579 cp_lexer_consume_token (parser->lexer);
16581 /* Turn the initializer into an initializer expansion. */
16582 initializer = make_pack_expansion (initializer);
16585 /* Add it to the vector. */
16586 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16588 /* If the next token is not a comma, we have reached the end of
16590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16593 /* Peek at the next token. */
16594 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16595 /* If the next token is a `}', then we're still done. An
16596 initializer-clause can have a trailing `,' after the
16597 initializer-list and before the closing `}'. */
16598 if (token->type == CPP_CLOSE_BRACE)
16601 /* Consume the `,' token. */
16602 cp_lexer_consume_token (parser->lexer);
16608 /* Classes [gram.class] */
16610 /* Parse a class-name.
16616 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16617 to indicate that names looked up in dependent types should be
16618 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16619 keyword has been used to indicate that the name that appears next
16620 is a template. TAG_TYPE indicates the explicit tag given before
16621 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16622 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16623 is the class being defined in a class-head.
16625 Returns the TYPE_DECL representing the class. */
16628 cp_parser_class_name (cp_parser *parser,
16629 bool typename_keyword_p,
16630 bool template_keyword_p,
16631 enum tag_types tag_type,
16632 bool check_dependency_p,
16634 bool is_declaration)
16640 tree identifier = NULL_TREE;
16642 /* All class-names start with an identifier. */
16643 token = cp_lexer_peek_token (parser->lexer);
16644 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16646 cp_parser_error (parser, "expected class-name");
16647 return error_mark_node;
16650 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16651 to a template-id, so we save it here. */
16652 scope = parser->scope;
16653 if (scope == error_mark_node)
16654 return error_mark_node;
16656 /* Any name names a type if we're following the `typename' keyword
16657 in a qualified name where the enclosing scope is type-dependent. */
16658 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16659 && dependent_type_p (scope));
16660 /* Handle the common case (an identifier, but not a template-id)
16662 if (token->type == CPP_NAME
16663 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16665 cp_token *identifier_token;
16668 /* Look for the identifier. */
16669 identifier_token = cp_lexer_peek_token (parser->lexer);
16670 ambiguous_p = identifier_token->ambiguous_p;
16671 identifier = cp_parser_identifier (parser);
16672 /* If the next token isn't an identifier, we are certainly not
16673 looking at a class-name. */
16674 if (identifier == error_mark_node)
16675 decl = error_mark_node;
16676 /* If we know this is a type-name, there's no need to look it
16678 else if (typename_p)
16682 tree ambiguous_decls;
16683 /* If we already know that this lookup is ambiguous, then
16684 we've already issued an error message; there's no reason
16688 cp_parser_simulate_error (parser);
16689 return error_mark_node;
16691 /* If the next token is a `::', then the name must be a type
16694 [basic.lookup.qual]
16696 During the lookup for a name preceding the :: scope
16697 resolution operator, object, function, and enumerator
16698 names are ignored. */
16699 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16700 tag_type = typename_type;
16701 /* Look up the name. */
16702 decl = cp_parser_lookup_name (parser, identifier,
16704 /*is_template=*/false,
16705 /*is_namespace=*/false,
16706 check_dependency_p,
16708 identifier_token->location);
16709 if (ambiguous_decls)
16711 if (cp_parser_parsing_tentatively (parser))
16712 cp_parser_simulate_error (parser);
16713 return error_mark_node;
16719 /* Try a template-id. */
16720 decl = cp_parser_template_id (parser, template_keyword_p,
16721 check_dependency_p,
16723 if (decl == error_mark_node)
16724 return error_mark_node;
16727 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16729 /* If this is a typename, create a TYPENAME_TYPE. */
16730 if (typename_p && decl != error_mark_node)
16732 decl = make_typename_type (scope, decl, typename_type,
16733 /*complain=*/tf_error);
16734 if (decl != error_mark_node)
16735 decl = TYPE_NAME (decl);
16738 /* Check to see that it is really the name of a class. */
16739 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16740 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16741 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16742 /* Situations like this:
16744 template <typename T> struct A {
16745 typename T::template X<int>::I i;
16748 are problematic. Is `T::template X<int>' a class-name? The
16749 standard does not seem to be definitive, but there is no other
16750 valid interpretation of the following `::'. Therefore, those
16751 names are considered class-names. */
16753 decl = make_typename_type (scope, decl, tag_type, tf_error);
16754 if (decl != error_mark_node)
16755 decl = TYPE_NAME (decl);
16757 else if (TREE_CODE (decl) != TYPE_DECL
16758 || TREE_TYPE (decl) == error_mark_node
16759 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16760 /* In Objective-C 2.0, a classname followed by '.' starts a
16761 dot-syntax expression, and it's not a type-name. */
16762 || (c_dialect_objc ()
16763 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16764 && objc_is_class_name (decl)))
16765 decl = error_mark_node;
16767 if (decl == error_mark_node)
16768 cp_parser_error (parser, "expected class-name");
16769 else if (identifier && !parser->scope)
16770 maybe_note_name_used_in_class (identifier, decl);
16775 /* Parse a class-specifier.
16778 class-head { member-specification [opt] }
16780 Returns the TREE_TYPE representing the class. */
16783 cp_parser_class_specifier (cp_parser* parser)
16786 tree attributes = NULL_TREE;
16787 bool nested_name_specifier_p;
16788 unsigned saved_num_template_parameter_lists;
16789 bool saved_in_function_body;
16790 bool saved_in_unbraced_linkage_specification_p;
16791 tree old_scope = NULL_TREE;
16792 tree scope = NULL_TREE;
16795 push_deferring_access_checks (dk_no_deferred);
16797 /* Parse the class-head. */
16798 type = cp_parser_class_head (parser,
16799 &nested_name_specifier_p,
16802 /* If the class-head was a semantic disaster, skip the entire body
16806 cp_parser_skip_to_end_of_block_or_statement (parser);
16807 pop_deferring_access_checks ();
16808 return error_mark_node;
16811 /* Look for the `{'. */
16812 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16814 pop_deferring_access_checks ();
16815 return error_mark_node;
16818 /* Process the base classes. If they're invalid, skip the
16819 entire class body. */
16820 if (!xref_basetypes (type, bases))
16822 /* Consuming the closing brace yields better error messages
16824 if (cp_parser_skip_to_closing_brace (parser))
16825 cp_lexer_consume_token (parser->lexer);
16826 pop_deferring_access_checks ();
16827 return error_mark_node;
16830 /* Issue an error message if type-definitions are forbidden here. */
16831 cp_parser_check_type_definition (parser);
16832 /* Remember that we are defining one more class. */
16833 ++parser->num_classes_being_defined;
16834 /* Inside the class, surrounding template-parameter-lists do not
16836 saved_num_template_parameter_lists
16837 = parser->num_template_parameter_lists;
16838 parser->num_template_parameter_lists = 0;
16839 /* We are not in a function body. */
16840 saved_in_function_body = parser->in_function_body;
16841 parser->in_function_body = false;
16842 /* We are not immediately inside an extern "lang" block. */
16843 saved_in_unbraced_linkage_specification_p
16844 = parser->in_unbraced_linkage_specification_p;
16845 parser->in_unbraced_linkage_specification_p = false;
16847 /* Start the class. */
16848 if (nested_name_specifier_p)
16850 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16851 old_scope = push_inner_scope (scope);
16853 type = begin_class_definition (type, attributes);
16855 if (type == error_mark_node)
16856 /* If the type is erroneous, skip the entire body of the class. */
16857 cp_parser_skip_to_closing_brace (parser);
16859 /* Parse the member-specification. */
16860 cp_parser_member_specification_opt (parser);
16862 /* Look for the trailing `}'. */
16863 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16864 /* Look for trailing attributes to apply to this class. */
16865 if (cp_parser_allow_gnu_extensions_p (parser))
16866 attributes = cp_parser_attributes_opt (parser);
16867 if (type != error_mark_node)
16868 type = finish_struct (type, attributes);
16869 if (nested_name_specifier_p)
16870 pop_inner_scope (old_scope, scope);
16872 /* We've finished a type definition. Check for the common syntax
16873 error of forgetting a semicolon after the definition. We need to
16874 be careful, as we can't just check for not-a-semicolon and be done
16875 with it; the user might have typed:
16877 class X { } c = ...;
16878 class X { } *p = ...;
16880 and so forth. Instead, enumerate all the possible tokens that
16881 might follow this production; if we don't see one of them, then
16882 complain and silently insert the semicolon. */
16884 cp_token *token = cp_lexer_peek_token (parser->lexer);
16885 bool want_semicolon = true;
16887 switch (token->type)
16890 case CPP_SEMICOLON:
16893 case CPP_OPEN_PAREN:
16894 case CPP_CLOSE_PAREN:
16896 want_semicolon = false;
16899 /* While it's legal for type qualifiers and storage class
16900 specifiers to follow type definitions in the grammar, only
16901 compiler testsuites contain code like that. Assume that if
16902 we see such code, then what we're really seeing is a case
16906 const <type> var = ...;
16911 static <type> func (...) ...
16913 i.e. the qualifier or specifier applies to the next
16914 declaration. To do so, however, we need to look ahead one
16915 more token to see if *that* token is a type specifier.
16917 This code could be improved to handle:
16920 static const <type> var = ...; */
16922 if (keyword_is_storage_class_specifier (token->keyword)
16923 || keyword_is_type_qualifier (token->keyword))
16925 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16927 if (lookahead->type == CPP_KEYWORD
16928 && !keyword_begins_type_specifier (lookahead->keyword))
16929 want_semicolon = false;
16930 else if (lookahead->type == CPP_NAME)
16931 /* Handling user-defined types here would be nice, but
16933 want_semicolon = false;
16940 if (want_semicolon)
16942 cp_token_position prev
16943 = cp_lexer_previous_token_position (parser->lexer);
16944 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16945 location_t loc = prev_token->location;
16947 if (CLASSTYPE_DECLARED_CLASS (type))
16948 error_at (loc, "expected %<;%> after class definition");
16949 else if (TREE_CODE (type) == RECORD_TYPE)
16950 error_at (loc, "expected %<;%> after struct definition");
16951 else if (TREE_CODE (type) == UNION_TYPE)
16952 error_at (loc, "expected %<;%> after union definition");
16954 gcc_unreachable ();
16956 /* Unget one token and smash it to look as though we encountered
16957 a semicolon in the input stream. */
16958 cp_lexer_set_token_position (parser->lexer, prev);
16959 token = cp_lexer_peek_token (parser->lexer);
16960 token->type = CPP_SEMICOLON;
16961 token->keyword = RID_MAX;
16965 /* If this class is not itself within the scope of another class,
16966 then we need to parse the bodies of all of the queued function
16967 definitions. Note that the queued functions defined in a class
16968 are not always processed immediately following the
16969 class-specifier for that class. Consider:
16972 struct B { void f() { sizeof (A); } };
16975 If `f' were processed before the processing of `A' were
16976 completed, there would be no way to compute the size of `A'.
16977 Note that the nesting we are interested in here is lexical --
16978 not the semantic nesting given by TYPE_CONTEXT. In particular,
16981 struct A { struct B; };
16982 struct A::B { void f() { } };
16984 there is no need to delay the parsing of `A::B::f'. */
16985 if (--parser->num_classes_being_defined == 0)
16988 tree class_type = NULL_TREE;
16989 tree pushed_scope = NULL_TREE;
16991 cp_default_arg_entry *e;
16993 /* In a first pass, parse default arguments to the functions.
16994 Then, in a second pass, parse the bodies of the functions.
16995 This two-phased approach handles cases like:
17003 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17007 /* If there are default arguments that have not yet been processed,
17008 take care of them now. */
17009 if (class_type != e->class_type)
17012 pop_scope (pushed_scope);
17013 class_type = e->class_type;
17014 pushed_scope = push_scope (class_type);
17016 /* Make sure that any template parameters are in scope. */
17017 maybe_begin_member_template_processing (fn);
17018 /* Parse the default argument expressions. */
17019 cp_parser_late_parsing_default_args (parser, fn);
17020 /* Remove any template parameters from the symbol table. */
17021 maybe_end_member_template_processing ();
17024 pop_scope (pushed_scope);
17025 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17026 /* Now parse the body of the functions. */
17027 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17028 cp_parser_late_parsing_for_member (parser, fn);
17029 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17032 /* Put back any saved access checks. */
17033 pop_deferring_access_checks ();
17035 /* Restore saved state. */
17036 parser->in_function_body = saved_in_function_body;
17037 parser->num_template_parameter_lists
17038 = saved_num_template_parameter_lists;
17039 parser->in_unbraced_linkage_specification_p
17040 = saved_in_unbraced_linkage_specification_p;
17045 /* Parse a class-head.
17048 class-key identifier [opt] base-clause [opt]
17049 class-key nested-name-specifier identifier base-clause [opt]
17050 class-key nested-name-specifier [opt] template-id
17054 class-key attributes identifier [opt] base-clause [opt]
17055 class-key attributes nested-name-specifier identifier base-clause [opt]
17056 class-key attributes nested-name-specifier [opt] template-id
17059 Upon return BASES is initialized to the list of base classes (or
17060 NULL, if there are none) in the same form returned by
17061 cp_parser_base_clause.
17063 Returns the TYPE of the indicated class. Sets
17064 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17065 involving a nested-name-specifier was used, and FALSE otherwise.
17067 Returns error_mark_node if this is not a class-head.
17069 Returns NULL_TREE if the class-head is syntactically valid, but
17070 semantically invalid in a way that means we should skip the entire
17071 body of the class. */
17074 cp_parser_class_head (cp_parser* parser,
17075 bool* nested_name_specifier_p,
17076 tree *attributes_p,
17079 tree nested_name_specifier;
17080 enum tag_types class_key;
17081 tree id = NULL_TREE;
17082 tree type = NULL_TREE;
17084 bool template_id_p = false;
17085 bool qualified_p = false;
17086 bool invalid_nested_name_p = false;
17087 bool invalid_explicit_specialization_p = false;
17088 tree pushed_scope = NULL_TREE;
17089 unsigned num_templates;
17090 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17091 /* Assume no nested-name-specifier will be present. */
17092 *nested_name_specifier_p = false;
17093 /* Assume no template parameter lists will be used in defining the
17097 *bases = NULL_TREE;
17099 /* Look for the class-key. */
17100 class_key = cp_parser_class_key (parser);
17101 if (class_key == none_type)
17102 return error_mark_node;
17104 /* Parse the attributes. */
17105 attributes = cp_parser_attributes_opt (parser);
17107 /* If the next token is `::', that is invalid -- but sometimes
17108 people do try to write:
17112 Handle this gracefully by accepting the extra qualifier, and then
17113 issuing an error about it later if this really is a
17114 class-head. If it turns out just to be an elaborated type
17115 specifier, remain silent. */
17116 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17117 qualified_p = true;
17119 push_deferring_access_checks (dk_no_check);
17121 /* Determine the name of the class. Begin by looking for an
17122 optional nested-name-specifier. */
17123 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17124 nested_name_specifier
17125 = cp_parser_nested_name_specifier_opt (parser,
17126 /*typename_keyword_p=*/false,
17127 /*check_dependency_p=*/false,
17129 /*is_declaration=*/false);
17130 /* If there was a nested-name-specifier, then there *must* be an
17132 if (nested_name_specifier)
17134 type_start_token = cp_lexer_peek_token (parser->lexer);
17135 /* Although the grammar says `identifier', it really means
17136 `class-name' or `template-name'. You are only allowed to
17137 define a class that has already been declared with this
17140 The proposed resolution for Core Issue 180 says that wherever
17141 you see `class T::X' you should treat `X' as a type-name.
17143 It is OK to define an inaccessible class; for example:
17145 class A { class B; };
17148 We do not know if we will see a class-name, or a
17149 template-name. We look for a class-name first, in case the
17150 class-name is a template-id; if we looked for the
17151 template-name first we would stop after the template-name. */
17152 cp_parser_parse_tentatively (parser);
17153 type = cp_parser_class_name (parser,
17154 /*typename_keyword_p=*/false,
17155 /*template_keyword_p=*/false,
17157 /*check_dependency_p=*/false,
17158 /*class_head_p=*/true,
17159 /*is_declaration=*/false);
17160 /* If that didn't work, ignore the nested-name-specifier. */
17161 if (!cp_parser_parse_definitely (parser))
17163 invalid_nested_name_p = true;
17164 type_start_token = cp_lexer_peek_token (parser->lexer);
17165 id = cp_parser_identifier (parser);
17166 if (id == error_mark_node)
17169 /* If we could not find a corresponding TYPE, treat this
17170 declaration like an unqualified declaration. */
17171 if (type == error_mark_node)
17172 nested_name_specifier = NULL_TREE;
17173 /* Otherwise, count the number of templates used in TYPE and its
17174 containing scopes. */
17179 for (scope = TREE_TYPE (type);
17180 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17181 scope = (TYPE_P (scope)
17182 ? TYPE_CONTEXT (scope)
17183 : DECL_CONTEXT (scope)))
17185 && CLASS_TYPE_P (scope)
17186 && CLASSTYPE_TEMPLATE_INFO (scope)
17187 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17188 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17192 /* Otherwise, the identifier is optional. */
17195 /* We don't know whether what comes next is a template-id,
17196 an identifier, or nothing at all. */
17197 cp_parser_parse_tentatively (parser);
17198 /* Check for a template-id. */
17199 type_start_token = cp_lexer_peek_token (parser->lexer);
17200 id = cp_parser_template_id (parser,
17201 /*template_keyword_p=*/false,
17202 /*check_dependency_p=*/true,
17203 /*is_declaration=*/true);
17204 /* If that didn't work, it could still be an identifier. */
17205 if (!cp_parser_parse_definitely (parser))
17207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17209 type_start_token = cp_lexer_peek_token (parser->lexer);
17210 id = cp_parser_identifier (parser);
17217 template_id_p = true;
17222 pop_deferring_access_checks ();
17225 cp_parser_check_for_invalid_template_id (parser, id,
17226 type_start_token->location);
17228 /* If it's not a `:' or a `{' then we can't really be looking at a
17229 class-head, since a class-head only appears as part of a
17230 class-specifier. We have to detect this situation before calling
17231 xref_tag, since that has irreversible side-effects. */
17232 if (!cp_parser_next_token_starts_class_definition_p (parser))
17234 cp_parser_error (parser, "expected %<{%> or %<:%>");
17235 return error_mark_node;
17238 /* At this point, we're going ahead with the class-specifier, even
17239 if some other problem occurs. */
17240 cp_parser_commit_to_tentative_parse (parser);
17241 /* Issue the error about the overly-qualified name now. */
17244 cp_parser_error (parser,
17245 "global qualification of class name is invalid");
17246 return error_mark_node;
17248 else if (invalid_nested_name_p)
17250 cp_parser_error (parser,
17251 "qualified name does not name a class");
17252 return error_mark_node;
17254 else if (nested_name_specifier)
17258 /* Reject typedef-names in class heads. */
17259 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17261 error_at (type_start_token->location,
17262 "invalid class name in declaration of %qD",
17268 /* Figure out in what scope the declaration is being placed. */
17269 scope = current_scope ();
17270 /* If that scope does not contain the scope in which the
17271 class was originally declared, the program is invalid. */
17272 if (scope && !is_ancestor (scope, nested_name_specifier))
17274 if (at_namespace_scope_p ())
17275 error_at (type_start_token->location,
17276 "declaration of %qD in namespace %qD which does not "
17278 type, scope, nested_name_specifier);
17280 error_at (type_start_token->location,
17281 "declaration of %qD in %qD which does not enclose %qD",
17282 type, scope, nested_name_specifier);
17288 A declarator-id shall not be qualified except for the
17289 definition of a ... nested class outside of its class
17290 ... [or] the definition or explicit instantiation of a
17291 class member of a namespace outside of its namespace. */
17292 if (scope == nested_name_specifier)
17294 permerror (nested_name_specifier_token_start->location,
17295 "extra qualification not allowed");
17296 nested_name_specifier = NULL_TREE;
17300 /* An explicit-specialization must be preceded by "template <>". If
17301 it is not, try to recover gracefully. */
17302 if (at_namespace_scope_p ()
17303 && parser->num_template_parameter_lists == 0
17306 error_at (type_start_token->location,
17307 "an explicit specialization must be preceded by %<template <>%>");
17308 invalid_explicit_specialization_p = true;
17309 /* Take the same action that would have been taken by
17310 cp_parser_explicit_specialization. */
17311 ++parser->num_template_parameter_lists;
17312 begin_specialization ();
17314 /* There must be no "return" statements between this point and the
17315 end of this function; set "type "to the correct return value and
17316 use "goto done;" to return. */
17317 /* Make sure that the right number of template parameters were
17319 if (!cp_parser_check_template_parameters (parser, num_templates,
17320 type_start_token->location,
17321 /*declarator=*/NULL))
17323 /* If something went wrong, there is no point in even trying to
17324 process the class-definition. */
17329 /* Look up the type. */
17332 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17333 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17334 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17336 error_at (type_start_token->location,
17337 "function template %qD redeclared as a class template", id);
17338 type = error_mark_node;
17342 type = TREE_TYPE (id);
17343 type = maybe_process_partial_specialization (type);
17345 if (nested_name_specifier)
17346 pushed_scope = push_scope (nested_name_specifier);
17348 else if (nested_name_specifier)
17354 template <typename T> struct S { struct T };
17355 template <typename T> struct S<T>::T { };
17357 we will get a TYPENAME_TYPE when processing the definition of
17358 `S::T'. We need to resolve it to the actual type before we
17359 try to define it. */
17360 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17362 class_type = resolve_typename_type (TREE_TYPE (type),
17363 /*only_current_p=*/false);
17364 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17365 type = TYPE_NAME (class_type);
17368 cp_parser_error (parser, "could not resolve typename type");
17369 type = error_mark_node;
17373 if (maybe_process_partial_specialization (TREE_TYPE (type))
17374 == error_mark_node)
17380 class_type = current_class_type;
17381 /* Enter the scope indicated by the nested-name-specifier. */
17382 pushed_scope = push_scope (nested_name_specifier);
17383 /* Get the canonical version of this type. */
17384 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17385 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17386 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17388 type = push_template_decl (type);
17389 if (type == error_mark_node)
17396 type = TREE_TYPE (type);
17397 *nested_name_specifier_p = true;
17399 else /* The name is not a nested name. */
17401 /* If the class was unnamed, create a dummy name. */
17403 id = make_anon_name ();
17404 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17405 parser->num_template_parameter_lists);
17408 /* Indicate whether this class was declared as a `class' or as a
17410 if (TREE_CODE (type) == RECORD_TYPE)
17411 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17412 cp_parser_check_class_key (class_key, type);
17414 /* If this type was already complete, and we see another definition,
17415 that's an error. */
17416 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17418 error_at (type_start_token->location, "redefinition of %q#T",
17420 error_at (type_start_token->location, "previous definition of %q+#T",
17425 else if (type == error_mark_node)
17428 /* We will have entered the scope containing the class; the names of
17429 base classes should be looked up in that context. For example:
17431 struct A { struct B {}; struct C; };
17432 struct A::C : B {};
17436 /* Get the list of base-classes, if there is one. */
17437 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17438 *bases = cp_parser_base_clause (parser);
17441 /* Leave the scope given by the nested-name-specifier. We will
17442 enter the class scope itself while processing the members. */
17444 pop_scope (pushed_scope);
17446 if (invalid_explicit_specialization_p)
17448 end_specialization ();
17449 --parser->num_template_parameter_lists;
17453 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17454 *attributes_p = attributes;
17458 /* Parse a class-key.
17465 Returns the kind of class-key specified, or none_type to indicate
17468 static enum tag_types
17469 cp_parser_class_key (cp_parser* parser)
17472 enum tag_types tag_type;
17474 /* Look for the class-key. */
17475 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17479 /* Check to see if the TOKEN is a class-key. */
17480 tag_type = cp_parser_token_is_class_key (token);
17482 cp_parser_error (parser, "expected class-key");
17486 /* Parse an (optional) member-specification.
17488 member-specification:
17489 member-declaration member-specification [opt]
17490 access-specifier : member-specification [opt] */
17493 cp_parser_member_specification_opt (cp_parser* parser)
17500 /* Peek at the next token. */
17501 token = cp_lexer_peek_token (parser->lexer);
17502 /* If it's a `}', or EOF then we've seen all the members. */
17503 if (token->type == CPP_CLOSE_BRACE
17504 || token->type == CPP_EOF
17505 || token->type == CPP_PRAGMA_EOL)
17508 /* See if this token is a keyword. */
17509 keyword = token->keyword;
17513 case RID_PROTECTED:
17515 /* Consume the access-specifier. */
17516 cp_lexer_consume_token (parser->lexer);
17517 /* Remember which access-specifier is active. */
17518 current_access_specifier = token->u.value;
17519 /* Look for the `:'. */
17520 cp_parser_require (parser, CPP_COLON, RT_COLON);
17524 /* Accept #pragmas at class scope. */
17525 if (token->type == CPP_PRAGMA)
17527 cp_parser_pragma (parser, pragma_external);
17531 /* Otherwise, the next construction must be a
17532 member-declaration. */
17533 cp_parser_member_declaration (parser);
17538 /* Parse a member-declaration.
17540 member-declaration:
17541 decl-specifier-seq [opt] member-declarator-list [opt] ;
17542 function-definition ; [opt]
17543 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17545 template-declaration
17547 member-declarator-list:
17549 member-declarator-list , member-declarator
17552 declarator pure-specifier [opt]
17553 declarator constant-initializer [opt]
17554 identifier [opt] : constant-expression
17558 member-declaration:
17559 __extension__ member-declaration
17562 declarator attributes [opt] pure-specifier [opt]
17563 declarator attributes [opt] constant-initializer [opt]
17564 identifier [opt] attributes [opt] : constant-expression
17568 member-declaration:
17569 static_assert-declaration */
17572 cp_parser_member_declaration (cp_parser* parser)
17574 cp_decl_specifier_seq decl_specifiers;
17575 tree prefix_attributes;
17577 int declares_class_or_enum;
17579 cp_token *token = NULL;
17580 cp_token *decl_spec_token_start = NULL;
17581 cp_token *initializer_token_start = NULL;
17582 int saved_pedantic;
17584 /* Check for the `__extension__' keyword. */
17585 if (cp_parser_extension_opt (parser, &saved_pedantic))
17588 cp_parser_member_declaration (parser);
17589 /* Restore the old value of the PEDANTIC flag. */
17590 pedantic = saved_pedantic;
17595 /* Check for a template-declaration. */
17596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17598 /* An explicit specialization here is an error condition, and we
17599 expect the specialization handler to detect and report this. */
17600 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17601 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17602 cp_parser_explicit_specialization (parser);
17604 cp_parser_template_declaration (parser, /*member_p=*/true);
17609 /* Check for a using-declaration. */
17610 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17612 /* Parse the using-declaration. */
17613 cp_parser_using_declaration (parser,
17614 /*access_declaration_p=*/false);
17618 /* Check for @defs. */
17619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17622 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17623 ivar = ivar_chains;
17627 ivar = TREE_CHAIN (member);
17628 TREE_CHAIN (member) = NULL_TREE;
17629 finish_member_declaration (member);
17634 /* If the next token is `static_assert' we have a static assertion. */
17635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17637 cp_parser_static_assert (parser, /*member_p=*/true);
17641 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17644 /* Parse the decl-specifier-seq. */
17645 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17646 cp_parser_decl_specifier_seq (parser,
17647 CP_PARSER_FLAGS_OPTIONAL,
17649 &declares_class_or_enum);
17650 prefix_attributes = decl_specifiers.attributes;
17651 decl_specifiers.attributes = NULL_TREE;
17652 /* Check for an invalid type-name. */
17653 if (!decl_specifiers.any_type_specifiers_p
17654 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17656 /* If there is no declarator, then the decl-specifier-seq should
17658 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17660 /* If there was no decl-specifier-seq, and the next token is a
17661 `;', then we have something like:
17667 Each member-declaration shall declare at least one member
17668 name of the class. */
17669 if (!decl_specifiers.any_specifiers_p)
17671 cp_token *token = cp_lexer_peek_token (parser->lexer);
17672 if (!in_system_header_at (token->location))
17673 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17679 /* See if this declaration is a friend. */
17680 friend_p = cp_parser_friend_p (&decl_specifiers);
17681 /* If there were decl-specifiers, check to see if there was
17682 a class-declaration. */
17683 type = check_tag_decl (&decl_specifiers);
17684 /* Nested classes have already been added to the class, but
17685 a `friend' needs to be explicitly registered. */
17688 /* If the `friend' keyword was present, the friend must
17689 be introduced with a class-key. */
17690 if (!declares_class_or_enum)
17691 error_at (decl_spec_token_start->location,
17692 "a class-key must be used when declaring a friend");
17695 template <typename T> struct A {
17696 friend struct A<T>::B;
17699 A<T>::B will be represented by a TYPENAME_TYPE, and
17700 therefore not recognized by check_tag_decl. */
17702 && decl_specifiers.type
17703 && TYPE_P (decl_specifiers.type))
17704 type = decl_specifiers.type;
17705 if (!type || !TYPE_P (type))
17706 error_at (decl_spec_token_start->location,
17707 "friend declaration does not name a class or "
17710 make_friend_class (current_class_type, type,
17711 /*complain=*/true);
17713 /* If there is no TYPE, an error message will already have
17715 else if (!type || type == error_mark_node)
17717 /* An anonymous aggregate has to be handled specially; such
17718 a declaration really declares a data member (with a
17719 particular type), as opposed to a nested class. */
17720 else if (ANON_AGGR_TYPE_P (type))
17722 /* Remove constructors and such from TYPE, now that we
17723 know it is an anonymous aggregate. */
17724 fixup_anonymous_aggr (type);
17725 /* And make the corresponding data member. */
17726 decl = build_decl (decl_spec_token_start->location,
17727 FIELD_DECL, NULL_TREE, type);
17728 /* Add it to the class. */
17729 finish_member_declaration (decl);
17732 cp_parser_check_access_in_redeclaration
17734 decl_spec_token_start->location);
17739 bool assume_semicolon = false;
17741 /* See if these declarations will be friends. */
17742 friend_p = cp_parser_friend_p (&decl_specifiers);
17744 /* Keep going until we hit the `;' at the end of the
17746 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17748 tree attributes = NULL_TREE;
17749 tree first_attribute;
17751 /* Peek at the next token. */
17752 token = cp_lexer_peek_token (parser->lexer);
17754 /* Check for a bitfield declaration. */
17755 if (token->type == CPP_COLON
17756 || (token->type == CPP_NAME
17757 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17763 /* Get the name of the bitfield. Note that we cannot just
17764 check TOKEN here because it may have been invalidated by
17765 the call to cp_lexer_peek_nth_token above. */
17766 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17767 identifier = cp_parser_identifier (parser);
17769 identifier = NULL_TREE;
17771 /* Consume the `:' token. */
17772 cp_lexer_consume_token (parser->lexer);
17773 /* Get the width of the bitfield. */
17775 = cp_parser_constant_expression (parser,
17776 /*allow_non_constant=*/false,
17779 /* Look for attributes that apply to the bitfield. */
17780 attributes = cp_parser_attributes_opt (parser);
17781 /* Remember which attributes are prefix attributes and
17783 first_attribute = attributes;
17784 /* Combine the attributes. */
17785 attributes = chainon (prefix_attributes, attributes);
17787 /* Create the bitfield declaration. */
17788 decl = grokbitfield (identifier
17789 ? make_id_declarator (NULL_TREE,
17799 cp_declarator *declarator;
17801 tree asm_specification;
17802 int ctor_dtor_or_conv_p;
17804 /* Parse the declarator. */
17806 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17807 &ctor_dtor_or_conv_p,
17808 /*parenthesized_p=*/NULL,
17809 /*member_p=*/true);
17811 /* If something went wrong parsing the declarator, make sure
17812 that we at least consume some tokens. */
17813 if (declarator == cp_error_declarator)
17815 /* Skip to the end of the statement. */
17816 cp_parser_skip_to_end_of_statement (parser);
17817 /* If the next token is not a semicolon, that is
17818 probably because we just skipped over the body of
17819 a function. So, we consume a semicolon if
17820 present, but do not issue an error message if it
17822 if (cp_lexer_next_token_is (parser->lexer,
17824 cp_lexer_consume_token (parser->lexer);
17828 if (declares_class_or_enum & 2)
17829 cp_parser_check_for_definition_in_return_type
17830 (declarator, decl_specifiers.type,
17831 decl_specifiers.type_location);
17833 /* Look for an asm-specification. */
17834 asm_specification = cp_parser_asm_specification_opt (parser);
17835 /* Look for attributes that apply to the declaration. */
17836 attributes = cp_parser_attributes_opt (parser);
17837 /* Remember which attributes are prefix attributes and
17839 first_attribute = attributes;
17840 /* Combine the attributes. */
17841 attributes = chainon (prefix_attributes, attributes);
17843 /* If it's an `=', then we have a constant-initializer or a
17844 pure-specifier. It is not correct to parse the
17845 initializer before registering the member declaration
17846 since the member declaration should be in scope while
17847 its initializer is processed. However, the rest of the
17848 front end does not yet provide an interface that allows
17849 us to handle this correctly. */
17850 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17854 A pure-specifier shall be used only in the declaration of
17855 a virtual function.
17857 A member-declarator can contain a constant-initializer
17858 only if it declares a static member of integral or
17861 Therefore, if the DECLARATOR is for a function, we look
17862 for a pure-specifier; otherwise, we look for a
17863 constant-initializer. When we call `grokfield', it will
17864 perform more stringent semantics checks. */
17865 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17866 if (function_declarator_p (declarator))
17867 initializer = cp_parser_pure_specifier (parser);
17869 /* Parse the initializer. */
17870 initializer = cp_parser_constant_initializer (parser);
17872 /* Otherwise, there is no initializer. */
17874 initializer = NULL_TREE;
17876 /* See if we are probably looking at a function
17877 definition. We are certainly not looking at a
17878 member-declarator. Calling `grokfield' has
17879 side-effects, so we must not do it unless we are sure
17880 that we are looking at a member-declarator. */
17881 if (cp_parser_token_starts_function_definition_p
17882 (cp_lexer_peek_token (parser->lexer)))
17884 /* The grammar does not allow a pure-specifier to be
17885 used when a member function is defined. (It is
17886 possible that this fact is an oversight in the
17887 standard, since a pure function may be defined
17888 outside of the class-specifier. */
17890 error_at (initializer_token_start->location,
17891 "pure-specifier on function-definition");
17892 decl = cp_parser_save_member_function_body (parser,
17896 /* If the member was not a friend, declare it here. */
17898 finish_member_declaration (decl);
17899 /* Peek at the next token. */
17900 token = cp_lexer_peek_token (parser->lexer);
17901 /* If the next token is a semicolon, consume it. */
17902 if (token->type == CPP_SEMICOLON)
17903 cp_lexer_consume_token (parser->lexer);
17907 if (declarator->kind == cdk_function)
17908 declarator->id_loc = token->location;
17909 /* Create the declaration. */
17910 decl = grokfield (declarator, &decl_specifiers,
17911 initializer, /*init_const_expr_p=*/true,
17916 /* Reset PREFIX_ATTRIBUTES. */
17917 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17918 attributes = TREE_CHAIN (attributes);
17920 TREE_CHAIN (attributes) = NULL_TREE;
17922 /* If there is any qualification still in effect, clear it
17923 now; we will be starting fresh with the next declarator. */
17924 parser->scope = NULL_TREE;
17925 parser->qualifying_scope = NULL_TREE;
17926 parser->object_scope = NULL_TREE;
17927 /* If it's a `,', then there are more declarators. */
17928 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17929 cp_lexer_consume_token (parser->lexer);
17930 /* If the next token isn't a `;', then we have a parse error. */
17931 else if (cp_lexer_next_token_is_not (parser->lexer,
17934 /* The next token might be a ways away from where the
17935 actual semicolon is missing. Find the previous token
17936 and use that for our error position. */
17937 cp_token *token = cp_lexer_previous_token (parser->lexer);
17938 error_at (token->location,
17939 "expected %<;%> at end of member declaration");
17941 /* Assume that the user meant to provide a semicolon. If
17942 we were to cp_parser_skip_to_end_of_statement, we might
17943 skip to a semicolon inside a member function definition
17944 and issue nonsensical error messages. */
17945 assume_semicolon = true;
17950 /* Add DECL to the list of members. */
17952 finish_member_declaration (decl);
17954 if (TREE_CODE (decl) == FUNCTION_DECL)
17955 cp_parser_save_default_args (parser, decl);
17958 if (assume_semicolon)
17963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17966 /* Parse a pure-specifier.
17971 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17972 Otherwise, ERROR_MARK_NODE is returned. */
17975 cp_parser_pure_specifier (cp_parser* parser)
17979 /* Look for the `=' token. */
17980 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17981 return error_mark_node;
17982 /* Look for the `0' token. */
17983 token = cp_lexer_peek_token (parser->lexer);
17985 if (token->type == CPP_EOF
17986 || token->type == CPP_PRAGMA_EOL)
17987 return error_mark_node;
17989 cp_lexer_consume_token (parser->lexer);
17991 /* Accept = default or = delete in c++0x mode. */
17992 if (token->keyword == RID_DEFAULT
17993 || token->keyword == RID_DELETE)
17995 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17996 return token->u.value;
17999 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18000 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18002 cp_parser_error (parser,
18003 "invalid pure specifier (only %<= 0%> is allowed)");
18004 cp_parser_skip_to_end_of_statement (parser);
18005 return error_mark_node;
18007 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18009 error_at (token->location, "templates may not be %<virtual%>");
18010 return error_mark_node;
18013 return integer_zero_node;
18016 /* Parse a constant-initializer.
18018 constant-initializer:
18019 = constant-expression
18021 Returns a representation of the constant-expression. */
18024 cp_parser_constant_initializer (cp_parser* parser)
18026 /* Look for the `=' token. */
18027 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18028 return error_mark_node;
18030 /* It is invalid to write:
18032 struct S { static const int i = { 7 }; };
18035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18037 cp_parser_error (parser,
18038 "a brace-enclosed initializer is not allowed here");
18039 /* Consume the opening brace. */
18040 cp_lexer_consume_token (parser->lexer);
18041 /* Skip the initializer. */
18042 cp_parser_skip_to_closing_brace (parser);
18043 /* Look for the trailing `}'. */
18044 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18046 return error_mark_node;
18049 return cp_parser_constant_expression (parser,
18050 /*allow_non_constant=*/false,
18054 /* Derived classes [gram.class.derived] */
18056 /* Parse a base-clause.
18059 : base-specifier-list
18061 base-specifier-list:
18062 base-specifier ... [opt]
18063 base-specifier-list , base-specifier ... [opt]
18065 Returns a TREE_LIST representing the base-classes, in the order in
18066 which they were declared. The representation of each node is as
18067 described by cp_parser_base_specifier.
18069 In the case that no bases are specified, this function will return
18070 NULL_TREE, not ERROR_MARK_NODE. */
18073 cp_parser_base_clause (cp_parser* parser)
18075 tree bases = NULL_TREE;
18077 /* Look for the `:' that begins the list. */
18078 cp_parser_require (parser, CPP_COLON, RT_COLON);
18080 /* Scan the base-specifier-list. */
18085 bool pack_expansion_p = false;
18087 /* Look for the base-specifier. */
18088 base = cp_parser_base_specifier (parser);
18089 /* Look for the (optional) ellipsis. */
18090 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18092 /* Consume the `...'. */
18093 cp_lexer_consume_token (parser->lexer);
18095 pack_expansion_p = true;
18098 /* Add BASE to the front of the list. */
18099 if (base != error_mark_node)
18101 if (pack_expansion_p)
18102 /* Make this a pack expansion type. */
18103 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18106 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18108 TREE_CHAIN (base) = bases;
18112 /* Peek at the next token. */
18113 token = cp_lexer_peek_token (parser->lexer);
18114 /* If it's not a comma, then the list is complete. */
18115 if (token->type != CPP_COMMA)
18117 /* Consume the `,'. */
18118 cp_lexer_consume_token (parser->lexer);
18121 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18122 base class had a qualified name. However, the next name that
18123 appears is certainly not qualified. */
18124 parser->scope = NULL_TREE;
18125 parser->qualifying_scope = NULL_TREE;
18126 parser->object_scope = NULL_TREE;
18128 return nreverse (bases);
18131 /* Parse a base-specifier.
18134 :: [opt] nested-name-specifier [opt] class-name
18135 virtual access-specifier [opt] :: [opt] nested-name-specifier
18137 access-specifier virtual [opt] :: [opt] nested-name-specifier
18140 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18141 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18142 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18143 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18146 cp_parser_base_specifier (cp_parser* parser)
18150 bool virtual_p = false;
18151 bool duplicate_virtual_error_issued_p = false;
18152 bool duplicate_access_error_issued_p = false;
18153 bool class_scope_p, template_p;
18154 tree access = access_default_node;
18157 /* Process the optional `virtual' and `access-specifier'. */
18160 /* Peek at the next token. */
18161 token = cp_lexer_peek_token (parser->lexer);
18162 /* Process `virtual'. */
18163 switch (token->keyword)
18166 /* If `virtual' appears more than once, issue an error. */
18167 if (virtual_p && !duplicate_virtual_error_issued_p)
18169 cp_parser_error (parser,
18170 "%<virtual%> specified more than once in base-specified");
18171 duplicate_virtual_error_issued_p = true;
18176 /* Consume the `virtual' token. */
18177 cp_lexer_consume_token (parser->lexer);
18182 case RID_PROTECTED:
18184 /* If more than one access specifier appears, issue an
18186 if (access != access_default_node
18187 && !duplicate_access_error_issued_p)
18189 cp_parser_error (parser,
18190 "more than one access specifier in base-specified");
18191 duplicate_access_error_issued_p = true;
18194 access = ridpointers[(int) token->keyword];
18196 /* Consume the access-specifier. */
18197 cp_lexer_consume_token (parser->lexer);
18206 /* It is not uncommon to see programs mechanically, erroneously, use
18207 the 'typename' keyword to denote (dependent) qualified types
18208 as base classes. */
18209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18211 token = cp_lexer_peek_token (parser->lexer);
18212 if (!processing_template_decl)
18213 error_at (token->location,
18214 "keyword %<typename%> not allowed outside of templates");
18216 error_at (token->location,
18217 "keyword %<typename%> not allowed in this context "
18218 "(the base class is implicitly a type)");
18219 cp_lexer_consume_token (parser->lexer);
18222 /* Look for the optional `::' operator. */
18223 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18224 /* Look for the nested-name-specifier. The simplest way to
18229 The keyword `typename' is not permitted in a base-specifier or
18230 mem-initializer; in these contexts a qualified name that
18231 depends on a template-parameter is implicitly assumed to be a
18234 is to pretend that we have seen the `typename' keyword at this
18236 cp_parser_nested_name_specifier_opt (parser,
18237 /*typename_keyword_p=*/true,
18238 /*check_dependency_p=*/true,
18240 /*is_declaration=*/true);
18241 /* If the base class is given by a qualified name, assume that names
18242 we see are type names or templates, as appropriate. */
18243 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18244 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18246 /* Finally, look for the class-name. */
18247 type = cp_parser_class_name (parser,
18251 /*check_dependency_p=*/true,
18252 /*class_head_p=*/false,
18253 /*is_declaration=*/true);
18255 if (type == error_mark_node)
18256 return error_mark_node;
18258 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18261 /* Exception handling [gram.exception] */
18263 /* Parse an (optional) exception-specification.
18265 exception-specification:
18266 throw ( type-id-list [opt] )
18268 Returns a TREE_LIST representing the exception-specification. The
18269 TREE_VALUE of each node is a type. */
18272 cp_parser_exception_specification_opt (cp_parser* parser)
18276 const char *saved_message;
18278 /* Peek at the next token. */
18279 token = cp_lexer_peek_token (parser->lexer);
18281 /* Is it a noexcept-specification? */
18282 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18285 cp_lexer_consume_token (parser->lexer);
18287 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18289 cp_lexer_consume_token (parser->lexer);
18291 /* Types may not be defined in an exception-specification. */
18292 saved_message = parser->type_definition_forbidden_message;
18293 parser->type_definition_forbidden_message
18294 = G_("types may not be defined in an exception-specification");
18296 expr = cp_parser_constant_expression (parser, false, NULL);
18298 /* Restore the saved message. */
18299 parser->type_definition_forbidden_message = saved_message;
18301 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18304 expr = boolean_true_node;
18306 return build_noexcept_spec (expr, tf_warning_or_error);
18309 /* If it's not `throw', then there's no exception-specification. */
18310 if (!cp_parser_is_keyword (token, RID_THROW))
18314 /* Enable this once a lot of code has transitioned to noexcept? */
18315 if (cxx_dialect == cxx0x && !in_system_header)
18316 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18317 "deprecated in C++0x; use %<noexcept%> instead");
18320 /* Consume the `throw'. */
18321 cp_lexer_consume_token (parser->lexer);
18323 /* Look for the `('. */
18324 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18326 /* Peek at the next token. */
18327 token = cp_lexer_peek_token (parser->lexer);
18328 /* If it's not a `)', then there is a type-id-list. */
18329 if (token->type != CPP_CLOSE_PAREN)
18331 /* Types may not be defined in an exception-specification. */
18332 saved_message = parser->type_definition_forbidden_message;
18333 parser->type_definition_forbidden_message
18334 = G_("types may not be defined in an exception-specification");
18335 /* Parse the type-id-list. */
18336 type_id_list = cp_parser_type_id_list (parser);
18337 /* Restore the saved message. */
18338 parser->type_definition_forbidden_message = saved_message;
18341 type_id_list = empty_except_spec;
18343 /* Look for the `)'. */
18344 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18346 return type_id_list;
18349 /* Parse an (optional) type-id-list.
18353 type-id-list , type-id ... [opt]
18355 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18356 in the order that the types were presented. */
18359 cp_parser_type_id_list (cp_parser* parser)
18361 tree types = NULL_TREE;
18368 /* Get the next type-id. */
18369 type = cp_parser_type_id (parser);
18370 /* Parse the optional ellipsis. */
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18373 /* Consume the `...'. */
18374 cp_lexer_consume_token (parser->lexer);
18376 /* Turn the type into a pack expansion expression. */
18377 type = make_pack_expansion (type);
18379 /* Add it to the list. */
18380 types = add_exception_specifier (types, type, /*complain=*/1);
18381 /* Peek at the next token. */
18382 token = cp_lexer_peek_token (parser->lexer);
18383 /* If it is not a `,', we are done. */
18384 if (token->type != CPP_COMMA)
18386 /* Consume the `,'. */
18387 cp_lexer_consume_token (parser->lexer);
18390 return nreverse (types);
18393 /* Parse a try-block.
18396 try compound-statement handler-seq */
18399 cp_parser_try_block (cp_parser* parser)
18403 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18404 try_block = begin_try_block ();
18405 cp_parser_compound_statement (parser, NULL, true);
18406 finish_try_block (try_block);
18407 cp_parser_handler_seq (parser);
18408 finish_handler_sequence (try_block);
18413 /* Parse a function-try-block.
18415 function-try-block:
18416 try ctor-initializer [opt] function-body handler-seq */
18419 cp_parser_function_try_block (cp_parser* parser)
18421 tree compound_stmt;
18423 bool ctor_initializer_p;
18425 /* Look for the `try' keyword. */
18426 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18428 /* Let the rest of the front end know where we are. */
18429 try_block = begin_function_try_block (&compound_stmt);
18430 /* Parse the function-body. */
18432 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18433 /* We're done with the `try' part. */
18434 finish_function_try_block (try_block);
18435 /* Parse the handlers. */
18436 cp_parser_handler_seq (parser);
18437 /* We're done with the handlers. */
18438 finish_function_handler_sequence (try_block, compound_stmt);
18440 return ctor_initializer_p;
18443 /* Parse a handler-seq.
18446 handler handler-seq [opt] */
18449 cp_parser_handler_seq (cp_parser* parser)
18455 /* Parse the handler. */
18456 cp_parser_handler (parser);
18457 /* Peek at the next token. */
18458 token = cp_lexer_peek_token (parser->lexer);
18459 /* If it's not `catch' then there are no more handlers. */
18460 if (!cp_parser_is_keyword (token, RID_CATCH))
18465 /* Parse a handler.
18468 catch ( exception-declaration ) compound-statement */
18471 cp_parser_handler (cp_parser* parser)
18476 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18477 handler = begin_handler ();
18478 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18479 declaration = cp_parser_exception_declaration (parser);
18480 finish_handler_parms (declaration, handler);
18481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18482 cp_parser_compound_statement (parser, NULL, false);
18483 finish_handler (handler);
18486 /* Parse an exception-declaration.
18488 exception-declaration:
18489 type-specifier-seq declarator
18490 type-specifier-seq abstract-declarator
18494 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18495 ellipsis variant is used. */
18498 cp_parser_exception_declaration (cp_parser* parser)
18500 cp_decl_specifier_seq type_specifiers;
18501 cp_declarator *declarator;
18502 const char *saved_message;
18504 /* If it's an ellipsis, it's easy to handle. */
18505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18507 /* Consume the `...' token. */
18508 cp_lexer_consume_token (parser->lexer);
18512 /* Types may not be defined in exception-declarations. */
18513 saved_message = parser->type_definition_forbidden_message;
18514 parser->type_definition_forbidden_message
18515 = G_("types may not be defined in exception-declarations");
18517 /* Parse the type-specifier-seq. */
18518 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18519 /*is_trailing_return=*/false,
18521 /* If it's a `)', then there is no declarator. */
18522 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18525 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18526 /*ctor_dtor_or_conv_p=*/NULL,
18527 /*parenthesized_p=*/NULL,
18528 /*member_p=*/false);
18530 /* Restore the saved message. */
18531 parser->type_definition_forbidden_message = saved_message;
18533 if (!type_specifiers.any_specifiers_p)
18534 return error_mark_node;
18536 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18539 /* Parse a throw-expression.
18542 throw assignment-expression [opt]
18544 Returns a THROW_EXPR representing the throw-expression. */
18547 cp_parser_throw_expression (cp_parser* parser)
18552 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18553 token = cp_lexer_peek_token (parser->lexer);
18554 /* Figure out whether or not there is an assignment-expression
18555 following the "throw" keyword. */
18556 if (token->type == CPP_COMMA
18557 || token->type == CPP_SEMICOLON
18558 || token->type == CPP_CLOSE_PAREN
18559 || token->type == CPP_CLOSE_SQUARE
18560 || token->type == CPP_CLOSE_BRACE
18561 || token->type == CPP_COLON)
18562 expression = NULL_TREE;
18564 expression = cp_parser_assignment_expression (parser,
18565 /*cast_p=*/false, NULL);
18567 return build_throw (expression);
18570 /* GNU Extensions */
18572 /* Parse an (optional) asm-specification.
18575 asm ( string-literal )
18577 If the asm-specification is present, returns a STRING_CST
18578 corresponding to the string-literal. Otherwise, returns
18582 cp_parser_asm_specification_opt (cp_parser* parser)
18585 tree asm_specification;
18587 /* Peek at the next token. */
18588 token = cp_lexer_peek_token (parser->lexer);
18589 /* If the next token isn't the `asm' keyword, then there's no
18590 asm-specification. */
18591 if (!cp_parser_is_keyword (token, RID_ASM))
18594 /* Consume the `asm' token. */
18595 cp_lexer_consume_token (parser->lexer);
18596 /* Look for the `('. */
18597 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18599 /* Look for the string-literal. */
18600 asm_specification = cp_parser_string_literal (parser, false, false);
18602 /* Look for the `)'. */
18603 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18605 return asm_specification;
18608 /* Parse an asm-operand-list.
18612 asm-operand-list , asm-operand
18615 string-literal ( expression )
18616 [ string-literal ] string-literal ( expression )
18618 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18619 each node is the expression. The TREE_PURPOSE is itself a
18620 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18621 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18622 is a STRING_CST for the string literal before the parenthesis. Returns
18623 ERROR_MARK_NODE if any of the operands are invalid. */
18626 cp_parser_asm_operand_list (cp_parser* parser)
18628 tree asm_operands = NULL_TREE;
18629 bool invalid_operands = false;
18633 tree string_literal;
18637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18639 /* Consume the `[' token. */
18640 cp_lexer_consume_token (parser->lexer);
18641 /* Read the operand name. */
18642 name = cp_parser_identifier (parser);
18643 if (name != error_mark_node)
18644 name = build_string (IDENTIFIER_LENGTH (name),
18645 IDENTIFIER_POINTER (name));
18646 /* Look for the closing `]'. */
18647 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18651 /* Look for the string-literal. */
18652 string_literal = cp_parser_string_literal (parser, false, false);
18654 /* Look for the `('. */
18655 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18656 /* Parse the expression. */
18657 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18658 /* Look for the `)'. */
18659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18661 if (name == error_mark_node
18662 || string_literal == error_mark_node
18663 || expression == error_mark_node)
18664 invalid_operands = true;
18666 /* Add this operand to the list. */
18667 asm_operands = tree_cons (build_tree_list (name, string_literal),
18670 /* If the next token is not a `,', there are no more
18672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18674 /* Consume the `,'. */
18675 cp_lexer_consume_token (parser->lexer);
18678 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18681 /* Parse an asm-clobber-list.
18685 asm-clobber-list , string-literal
18687 Returns a TREE_LIST, indicating the clobbers in the order that they
18688 appeared. The TREE_VALUE of each node is a STRING_CST. */
18691 cp_parser_asm_clobber_list (cp_parser* parser)
18693 tree clobbers = NULL_TREE;
18697 tree string_literal;
18699 /* Look for the string literal. */
18700 string_literal = cp_parser_string_literal (parser, false, false);
18701 /* Add it to the list. */
18702 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18703 /* If the next token is not a `,', then the list is
18705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18707 /* Consume the `,' token. */
18708 cp_lexer_consume_token (parser->lexer);
18714 /* Parse an asm-label-list.
18718 asm-label-list , identifier
18720 Returns a TREE_LIST, indicating the labels in the order that they
18721 appeared. The TREE_VALUE of each node is a label. */
18724 cp_parser_asm_label_list (cp_parser* parser)
18726 tree labels = NULL_TREE;
18730 tree identifier, label, name;
18732 /* Look for the identifier. */
18733 identifier = cp_parser_identifier (parser);
18734 if (!error_operand_p (identifier))
18736 label = lookup_label (identifier);
18737 if (TREE_CODE (label) == LABEL_DECL)
18739 TREE_USED (label) = 1;
18740 check_goto (label);
18741 name = build_string (IDENTIFIER_LENGTH (identifier),
18742 IDENTIFIER_POINTER (identifier));
18743 labels = tree_cons (name, label, labels);
18746 /* If the next token is not a `,', then the list is
18748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18750 /* Consume the `,' token. */
18751 cp_lexer_consume_token (parser->lexer);
18754 return nreverse (labels);
18757 /* Parse an (optional) series of attributes.
18760 attributes attribute
18763 __attribute__ (( attribute-list [opt] ))
18765 The return value is as for cp_parser_attribute_list. */
18768 cp_parser_attributes_opt (cp_parser* parser)
18770 tree attributes = NULL_TREE;
18775 tree attribute_list;
18777 /* Peek at the next token. */
18778 token = cp_lexer_peek_token (parser->lexer);
18779 /* If it's not `__attribute__', then we're done. */
18780 if (token->keyword != RID_ATTRIBUTE)
18783 /* Consume the `__attribute__' keyword. */
18784 cp_lexer_consume_token (parser->lexer);
18785 /* Look for the two `(' tokens. */
18786 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18787 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18789 /* Peek at the next token. */
18790 token = cp_lexer_peek_token (parser->lexer);
18791 if (token->type != CPP_CLOSE_PAREN)
18792 /* Parse the attribute-list. */
18793 attribute_list = cp_parser_attribute_list (parser);
18795 /* If the next token is a `)', then there is no attribute
18797 attribute_list = NULL;
18799 /* Look for the two `)' tokens. */
18800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18801 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18803 /* Add these new attributes to the list. */
18804 attributes = chainon (attributes, attribute_list);
18810 /* Parse an attribute-list.
18814 attribute-list , attribute
18818 identifier ( identifier )
18819 identifier ( identifier , expression-list )
18820 identifier ( expression-list )
18822 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18823 to an attribute. The TREE_PURPOSE of each node is the identifier
18824 indicating which attribute is in use. The TREE_VALUE represents
18825 the arguments, if any. */
18828 cp_parser_attribute_list (cp_parser* parser)
18830 tree attribute_list = NULL_TREE;
18831 bool save_translate_strings_p = parser->translate_strings_p;
18833 parser->translate_strings_p = false;
18840 /* Look for the identifier. We also allow keywords here; for
18841 example `__attribute__ ((const))' is legal. */
18842 token = cp_lexer_peek_token (parser->lexer);
18843 if (token->type == CPP_NAME
18844 || token->type == CPP_KEYWORD)
18846 tree arguments = NULL_TREE;
18848 /* Consume the token. */
18849 token = cp_lexer_consume_token (parser->lexer);
18851 /* Save away the identifier that indicates which attribute
18853 identifier = (token->type == CPP_KEYWORD)
18854 /* For keywords, use the canonical spelling, not the
18855 parsed identifier. */
18856 ? ridpointers[(int) token->keyword]
18859 attribute = build_tree_list (identifier, NULL_TREE);
18861 /* Peek at the next token. */
18862 token = cp_lexer_peek_token (parser->lexer);
18863 /* If it's an `(', then parse the attribute arguments. */
18864 if (token->type == CPP_OPEN_PAREN)
18867 int attr_flag = (attribute_takes_identifier_p (identifier)
18868 ? id_attr : normal_attr);
18869 vec = cp_parser_parenthesized_expression_list
18870 (parser, attr_flag, /*cast_p=*/false,
18871 /*allow_expansion_p=*/false,
18872 /*non_constant_p=*/NULL);
18874 arguments = error_mark_node;
18877 arguments = build_tree_list_vec (vec);
18878 release_tree_vector (vec);
18880 /* Save the arguments away. */
18881 TREE_VALUE (attribute) = arguments;
18884 if (arguments != error_mark_node)
18886 /* Add this attribute to the list. */
18887 TREE_CHAIN (attribute) = attribute_list;
18888 attribute_list = attribute;
18891 token = cp_lexer_peek_token (parser->lexer);
18893 /* Now, look for more attributes. If the next token isn't a
18894 `,', we're done. */
18895 if (token->type != CPP_COMMA)
18898 /* Consume the comma and keep going. */
18899 cp_lexer_consume_token (parser->lexer);
18901 parser->translate_strings_p = save_translate_strings_p;
18903 /* We built up the list in reverse order. */
18904 return nreverse (attribute_list);
18907 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18908 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18909 current value of the PEDANTIC flag, regardless of whether or not
18910 the `__extension__' keyword is present. The caller is responsible
18911 for restoring the value of the PEDANTIC flag. */
18914 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18916 /* Save the old value of the PEDANTIC flag. */
18917 *saved_pedantic = pedantic;
18919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18921 /* Consume the `__extension__' token. */
18922 cp_lexer_consume_token (parser->lexer);
18923 /* We're not being pedantic while the `__extension__' keyword is
18933 /* Parse a label declaration.
18936 __label__ label-declarator-seq ;
18938 label-declarator-seq:
18939 identifier , label-declarator-seq
18943 cp_parser_label_declaration (cp_parser* parser)
18945 /* Look for the `__label__' keyword. */
18946 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18952 /* Look for an identifier. */
18953 identifier = cp_parser_identifier (parser);
18954 /* If we failed, stop. */
18955 if (identifier == error_mark_node)
18957 /* Declare it as a label. */
18958 finish_label_decl (identifier);
18959 /* If the next token is a `;', stop. */
18960 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18962 /* Look for the `,' separating the label declarations. */
18963 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18966 /* Look for the final `;'. */
18967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18970 /* Support Functions */
18972 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18973 NAME should have one of the representations used for an
18974 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18975 is returned. If PARSER->SCOPE is a dependent type, then a
18976 SCOPE_REF is returned.
18978 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18979 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18980 was formed. Abstractly, such entities should not be passed to this
18981 function, because they do not need to be looked up, but it is
18982 simpler to check for this special case here, rather than at the
18985 In cases not explicitly covered above, this function returns a
18986 DECL, OVERLOAD, or baselink representing the result of the lookup.
18987 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18990 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18991 (e.g., "struct") that was used. In that case bindings that do not
18992 refer to types are ignored.
18994 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18997 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19000 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19003 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19004 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19005 NULL_TREE otherwise. */
19008 cp_parser_lookup_name (cp_parser *parser, tree name,
19009 enum tag_types tag_type,
19012 bool check_dependency,
19013 tree *ambiguous_decls,
19014 location_t name_location)
19018 tree object_type = parser->context->object_type;
19020 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19021 flags |= LOOKUP_COMPLAIN;
19023 /* Assume that the lookup will be unambiguous. */
19024 if (ambiguous_decls)
19025 *ambiguous_decls = NULL_TREE;
19027 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19028 no longer valid. Note that if we are parsing tentatively, and
19029 the parse fails, OBJECT_TYPE will be automatically restored. */
19030 parser->context->object_type = NULL_TREE;
19032 if (name == error_mark_node)
19033 return error_mark_node;
19035 /* A template-id has already been resolved; there is no lookup to
19037 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19039 if (BASELINK_P (name))
19041 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19042 == TEMPLATE_ID_EXPR);
19046 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19047 it should already have been checked to make sure that the name
19048 used matches the type being destroyed. */
19049 if (TREE_CODE (name) == BIT_NOT_EXPR)
19053 /* Figure out to which type this destructor applies. */
19055 type = parser->scope;
19056 else if (object_type)
19057 type = object_type;
19059 type = current_class_type;
19060 /* If that's not a class type, there is no destructor. */
19061 if (!type || !CLASS_TYPE_P (type))
19062 return error_mark_node;
19063 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19064 lazily_declare_fn (sfk_destructor, type);
19065 if (!CLASSTYPE_DESTRUCTORS (type))
19066 return error_mark_node;
19067 /* If it was a class type, return the destructor. */
19068 return CLASSTYPE_DESTRUCTORS (type);
19071 /* By this point, the NAME should be an ordinary identifier. If
19072 the id-expression was a qualified name, the qualifying scope is
19073 stored in PARSER->SCOPE at this point. */
19074 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19076 /* Perform the lookup. */
19081 if (parser->scope == error_mark_node)
19082 return error_mark_node;
19084 /* If the SCOPE is dependent, the lookup must be deferred until
19085 the template is instantiated -- unless we are explicitly
19086 looking up names in uninstantiated templates. Even then, we
19087 cannot look up the name if the scope is not a class type; it
19088 might, for example, be a template type parameter. */
19089 dependent_p = (TYPE_P (parser->scope)
19090 && dependent_scope_p (parser->scope));
19091 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19093 /* Defer lookup. */
19094 decl = error_mark_node;
19097 tree pushed_scope = NULL_TREE;
19099 /* If PARSER->SCOPE is a dependent type, then it must be a
19100 class type, and we must not be checking dependencies;
19101 otherwise, we would have processed this lookup above. So
19102 that PARSER->SCOPE is not considered a dependent base by
19103 lookup_member, we must enter the scope here. */
19105 pushed_scope = push_scope (parser->scope);
19107 /* If the PARSER->SCOPE is a template specialization, it
19108 may be instantiated during name lookup. In that case,
19109 errors may be issued. Even if we rollback the current
19110 tentative parse, those errors are valid. */
19111 decl = lookup_qualified_name (parser->scope, name,
19112 tag_type != none_type,
19113 /*complain=*/true);
19115 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19116 lookup result and the nested-name-specifier nominates a class C:
19117 * if the name specified after the nested-name-specifier, when
19118 looked up in C, is the injected-class-name of C (Clause 9), or
19119 * if the name specified after the nested-name-specifier is the
19120 same as the identifier or the simple-template-id's template-
19121 name in the last component of the nested-name-specifier,
19122 the name is instead considered to name the constructor of
19123 class C. [ Note: for example, the constructor is not an
19124 acceptable lookup result in an elaborated-type-specifier so
19125 the constructor would not be used in place of the
19126 injected-class-name. --end note ] Such a constructor name
19127 shall be used only in the declarator-id of a declaration that
19128 names a constructor or in a using-declaration. */
19129 if (tag_type == none_type
19130 && DECL_SELF_REFERENCE_P (decl)
19131 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19132 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19133 tag_type != none_type,
19134 /*complain=*/true);
19136 /* If we have a single function from a using decl, pull it out. */
19137 if (TREE_CODE (decl) == OVERLOAD
19138 && !really_overloaded_fn (decl))
19139 decl = OVL_FUNCTION (decl);
19142 pop_scope (pushed_scope);
19145 /* If the scope is a dependent type and either we deferred lookup or
19146 we did lookup but didn't find the name, rememeber the name. */
19147 if (decl == error_mark_node && TYPE_P (parser->scope)
19148 && dependent_type_p (parser->scope))
19154 /* The resolution to Core Issue 180 says that `struct
19155 A::B' should be considered a type-name, even if `A'
19157 type = make_typename_type (parser->scope, name, tag_type,
19158 /*complain=*/tf_error);
19159 decl = TYPE_NAME (type);
19161 else if (is_template
19162 && (cp_parser_next_token_ends_template_argument_p (parser)
19163 || cp_lexer_next_token_is (parser->lexer,
19165 decl = make_unbound_class_template (parser->scope,
19167 /*complain=*/tf_error);
19169 decl = build_qualified_name (/*type=*/NULL_TREE,
19170 parser->scope, name,
19173 parser->qualifying_scope = parser->scope;
19174 parser->object_scope = NULL_TREE;
19176 else if (object_type)
19178 tree object_decl = NULL_TREE;
19179 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19180 OBJECT_TYPE is not a class. */
19181 if (CLASS_TYPE_P (object_type))
19182 /* If the OBJECT_TYPE is a template specialization, it may
19183 be instantiated during name lookup. In that case, errors
19184 may be issued. Even if we rollback the current tentative
19185 parse, those errors are valid. */
19186 object_decl = lookup_member (object_type,
19189 tag_type != none_type);
19190 /* Look it up in the enclosing context, too. */
19191 decl = lookup_name_real (name, tag_type != none_type,
19193 /*block_p=*/true, is_namespace, flags);
19194 parser->object_scope = object_type;
19195 parser->qualifying_scope = NULL_TREE;
19197 decl = object_decl;
19201 decl = lookup_name_real (name, tag_type != none_type,
19203 /*block_p=*/true, is_namespace, flags);
19204 parser->qualifying_scope = NULL_TREE;
19205 parser->object_scope = NULL_TREE;
19208 /* If the lookup failed, let our caller know. */
19209 if (!decl || decl == error_mark_node)
19210 return error_mark_node;
19212 /* Pull out the template from an injected-class-name (or multiple). */
19214 decl = maybe_get_template_decl_from_type_decl (decl);
19216 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19217 if (TREE_CODE (decl) == TREE_LIST)
19219 if (ambiguous_decls)
19220 *ambiguous_decls = decl;
19221 /* The error message we have to print is too complicated for
19222 cp_parser_error, so we incorporate its actions directly. */
19223 if (!cp_parser_simulate_error (parser))
19225 error_at (name_location, "reference to %qD is ambiguous",
19227 print_candidates (decl);
19229 return error_mark_node;
19232 gcc_assert (DECL_P (decl)
19233 || TREE_CODE (decl) == OVERLOAD
19234 || TREE_CODE (decl) == SCOPE_REF
19235 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19236 || BASELINK_P (decl));
19238 /* If we have resolved the name of a member declaration, check to
19239 see if the declaration is accessible. When the name resolves to
19240 set of overloaded functions, accessibility is checked when
19241 overload resolution is done.
19243 During an explicit instantiation, access is not checked at all,
19244 as per [temp.explicit]. */
19246 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19251 /* Like cp_parser_lookup_name, but for use in the typical case where
19252 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19253 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19256 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19258 return cp_parser_lookup_name (parser, name,
19260 /*is_template=*/false,
19261 /*is_namespace=*/false,
19262 /*check_dependency=*/true,
19263 /*ambiguous_decls=*/NULL,
19267 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19268 the current context, return the TYPE_DECL. If TAG_NAME_P is
19269 true, the DECL indicates the class being defined in a class-head,
19270 or declared in an elaborated-type-specifier.
19272 Otherwise, return DECL. */
19275 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19277 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19278 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19281 template <typename T> struct B;
19284 template <typename T> struct A::B {};
19286 Similarly, in an elaborated-type-specifier:
19288 namespace N { struct X{}; }
19291 template <typename T> friend struct N::X;
19294 However, if the DECL refers to a class type, and we are in
19295 the scope of the class, then the name lookup automatically
19296 finds the TYPE_DECL created by build_self_reference rather
19297 than a TEMPLATE_DECL. For example, in:
19299 template <class T> struct S {
19303 there is no need to handle such case. */
19305 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19306 return DECL_TEMPLATE_RESULT (decl);
19311 /* If too many, or too few, template-parameter lists apply to the
19312 declarator, issue an error message. Returns TRUE if all went well,
19313 and FALSE otherwise. */
19316 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19317 cp_declarator *declarator,
19318 location_t declarator_location)
19320 unsigned num_templates;
19322 /* We haven't seen any classes that involve template parameters yet. */
19325 switch (declarator->kind)
19328 if (declarator->u.id.qualifying_scope)
19332 scope = declarator->u.id.qualifying_scope;
19334 while (scope && CLASS_TYPE_P (scope))
19336 /* You're supposed to have one `template <...>'
19337 for every template class, but you don't need one
19338 for a full specialization. For example:
19340 template <class T> struct S{};
19341 template <> struct S<int> { void f(); };
19342 void S<int>::f () {}
19344 is correct; there shouldn't be a `template <>' for
19345 the definition of `S<int>::f'. */
19346 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19347 /* If SCOPE does not have template information of any
19348 kind, then it is not a template, nor is it nested
19349 within a template. */
19351 if (explicit_class_specialization_p (scope))
19353 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19356 scope = TYPE_CONTEXT (scope);
19359 else if (TREE_CODE (declarator->u.id.unqualified_name)
19360 == TEMPLATE_ID_EXPR)
19361 /* If the DECLARATOR has the form `X<y>' then it uses one
19362 additional level of template parameters. */
19365 return cp_parser_check_template_parameters
19366 (parser, num_templates, declarator_location, declarator);
19372 case cdk_reference:
19374 return (cp_parser_check_declarator_template_parameters
19375 (parser, declarator->declarator, declarator_location));
19381 gcc_unreachable ();
19386 /* NUM_TEMPLATES were used in the current declaration. If that is
19387 invalid, return FALSE and issue an error messages. Otherwise,
19388 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19389 declarator and we can print more accurate diagnostics. */
19392 cp_parser_check_template_parameters (cp_parser* parser,
19393 unsigned num_templates,
19394 location_t location,
19395 cp_declarator *declarator)
19397 /* If there are the same number of template classes and parameter
19398 lists, that's OK. */
19399 if (parser->num_template_parameter_lists == num_templates)
19401 /* If there are more, but only one more, then we are referring to a
19402 member template. That's OK too. */
19403 if (parser->num_template_parameter_lists == num_templates + 1)
19405 /* If there are more template classes than parameter lists, we have
19408 template <class T> void S<T>::R<T>::f (); */
19409 if (parser->num_template_parameter_lists < num_templates)
19411 if (declarator && !current_function_decl)
19412 error_at (location, "specializing member %<%T::%E%> "
19413 "requires %<template<>%> syntax",
19414 declarator->u.id.qualifying_scope,
19415 declarator->u.id.unqualified_name);
19416 else if (declarator)
19417 error_at (location, "invalid declaration of %<%T::%E%>",
19418 declarator->u.id.qualifying_scope,
19419 declarator->u.id.unqualified_name);
19421 error_at (location, "too few template-parameter-lists");
19424 /* Otherwise, there are too many template parameter lists. We have
19427 template <class T> template <class U> void S::f(); */
19428 error_at (location, "too many template-parameter-lists");
19432 /* Parse an optional `::' token indicating that the following name is
19433 from the global namespace. If so, PARSER->SCOPE is set to the
19434 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19435 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19436 Returns the new value of PARSER->SCOPE, if the `::' token is
19437 present, and NULL_TREE otherwise. */
19440 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19444 /* Peek at the next token. */
19445 token = cp_lexer_peek_token (parser->lexer);
19446 /* If we're looking at a `::' token then we're starting from the
19447 global namespace, not our current location. */
19448 if (token->type == CPP_SCOPE)
19450 /* Consume the `::' token. */
19451 cp_lexer_consume_token (parser->lexer);
19452 /* Set the SCOPE so that we know where to start the lookup. */
19453 parser->scope = global_namespace;
19454 parser->qualifying_scope = global_namespace;
19455 parser->object_scope = NULL_TREE;
19457 return parser->scope;
19459 else if (!current_scope_valid_p)
19461 parser->scope = NULL_TREE;
19462 parser->qualifying_scope = NULL_TREE;
19463 parser->object_scope = NULL_TREE;
19469 /* Returns TRUE if the upcoming token sequence is the start of a
19470 constructor declarator. If FRIEND_P is true, the declarator is
19471 preceded by the `friend' specifier. */
19474 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19476 bool constructor_p;
19477 tree nested_name_specifier;
19478 cp_token *next_token;
19480 /* The common case is that this is not a constructor declarator, so
19481 try to avoid doing lots of work if at all possible. It's not
19482 valid declare a constructor at function scope. */
19483 if (parser->in_function_body)
19485 /* And only certain tokens can begin a constructor declarator. */
19486 next_token = cp_lexer_peek_token (parser->lexer);
19487 if (next_token->type != CPP_NAME
19488 && next_token->type != CPP_SCOPE
19489 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19490 && next_token->type != CPP_TEMPLATE_ID)
19493 /* Parse tentatively; we are going to roll back all of the tokens
19495 cp_parser_parse_tentatively (parser);
19496 /* Assume that we are looking at a constructor declarator. */
19497 constructor_p = true;
19499 /* Look for the optional `::' operator. */
19500 cp_parser_global_scope_opt (parser,
19501 /*current_scope_valid_p=*/false);
19502 /* Look for the nested-name-specifier. */
19503 nested_name_specifier
19504 = (cp_parser_nested_name_specifier_opt (parser,
19505 /*typename_keyword_p=*/false,
19506 /*check_dependency_p=*/false,
19508 /*is_declaration=*/false));
19509 /* Outside of a class-specifier, there must be a
19510 nested-name-specifier. */
19511 if (!nested_name_specifier &&
19512 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19514 constructor_p = false;
19515 else if (nested_name_specifier == error_mark_node)
19516 constructor_p = false;
19518 /* If we have a class scope, this is easy; DR 147 says that S::S always
19519 names the constructor, and no other qualified name could. */
19520 if (constructor_p && nested_name_specifier
19521 && TYPE_P (nested_name_specifier))
19523 tree id = cp_parser_unqualified_id (parser,
19524 /*template_keyword_p=*/false,
19525 /*check_dependency_p=*/false,
19526 /*declarator_p=*/true,
19527 /*optional_p=*/false);
19528 if (is_overloaded_fn (id))
19529 id = DECL_NAME (get_first_fn (id));
19530 if (!constructor_name_p (id, nested_name_specifier))
19531 constructor_p = false;
19533 /* If we still think that this might be a constructor-declarator,
19534 look for a class-name. */
19535 else if (constructor_p)
19539 template <typename T> struct S {
19543 we must recognize that the nested `S' names a class. */
19545 type_decl = cp_parser_class_name (parser,
19546 /*typename_keyword_p=*/false,
19547 /*template_keyword_p=*/false,
19549 /*check_dependency_p=*/false,
19550 /*class_head_p=*/false,
19551 /*is_declaration=*/false);
19552 /* If there was no class-name, then this is not a constructor. */
19553 constructor_p = !cp_parser_error_occurred (parser);
19555 /* If we're still considering a constructor, we have to see a `(',
19556 to begin the parameter-declaration-clause, followed by either a
19557 `)', an `...', or a decl-specifier. We need to check for a
19558 type-specifier to avoid being fooled into thinking that:
19562 is a constructor. (It is actually a function named `f' that
19563 takes one parameter (of type `int') and returns a value of type
19566 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19567 constructor_p = false;
19570 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19571 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19572 /* A parameter declaration begins with a decl-specifier,
19573 which is either the "attribute" keyword, a storage class
19574 specifier, or (usually) a type-specifier. */
19575 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19578 tree pushed_scope = NULL_TREE;
19579 unsigned saved_num_template_parameter_lists;
19581 /* Names appearing in the type-specifier should be looked up
19582 in the scope of the class. */
19583 if (current_class_type)
19587 type = TREE_TYPE (type_decl);
19588 if (TREE_CODE (type) == TYPENAME_TYPE)
19590 type = resolve_typename_type (type,
19591 /*only_current_p=*/false);
19592 if (TREE_CODE (type) == TYPENAME_TYPE)
19594 cp_parser_abort_tentative_parse (parser);
19598 pushed_scope = push_scope (type);
19601 /* Inside the constructor parameter list, surrounding
19602 template-parameter-lists do not apply. */
19603 saved_num_template_parameter_lists
19604 = parser->num_template_parameter_lists;
19605 parser->num_template_parameter_lists = 0;
19607 /* Look for the type-specifier. */
19608 cp_parser_type_specifier (parser,
19609 CP_PARSER_FLAGS_NONE,
19610 /*decl_specs=*/NULL,
19611 /*is_declarator=*/true,
19612 /*declares_class_or_enum=*/NULL,
19613 /*is_cv_qualifier=*/NULL);
19615 parser->num_template_parameter_lists
19616 = saved_num_template_parameter_lists;
19618 /* Leave the scope of the class. */
19620 pop_scope (pushed_scope);
19622 constructor_p = !cp_parser_error_occurred (parser);
19626 /* We did not really want to consume any tokens. */
19627 cp_parser_abort_tentative_parse (parser);
19629 return constructor_p;
19632 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19633 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19634 they must be performed once we are in the scope of the function.
19636 Returns the function defined. */
19639 cp_parser_function_definition_from_specifiers_and_declarator
19640 (cp_parser* parser,
19641 cp_decl_specifier_seq *decl_specifiers,
19643 const cp_declarator *declarator)
19648 /* Begin the function-definition. */
19649 success_p = start_function (decl_specifiers, declarator, attributes);
19651 /* The things we're about to see are not directly qualified by any
19652 template headers we've seen thus far. */
19653 reset_specialization ();
19655 /* If there were names looked up in the decl-specifier-seq that we
19656 did not check, check them now. We must wait until we are in the
19657 scope of the function to perform the checks, since the function
19658 might be a friend. */
19659 perform_deferred_access_checks ();
19663 /* Skip the entire function. */
19664 cp_parser_skip_to_end_of_block_or_statement (parser);
19665 fn = error_mark_node;
19667 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19669 /* Seen already, skip it. An error message has already been output. */
19670 cp_parser_skip_to_end_of_block_or_statement (parser);
19671 fn = current_function_decl;
19672 current_function_decl = NULL_TREE;
19673 /* If this is a function from a class, pop the nested class. */
19674 if (current_class_name)
19675 pop_nested_class ();
19678 fn = cp_parser_function_definition_after_declarator (parser,
19679 /*inline_p=*/false);
19684 /* Parse the part of a function-definition that follows the
19685 declarator. INLINE_P is TRUE iff this function is an inline
19686 function defined within a class-specifier.
19688 Returns the function defined. */
19691 cp_parser_function_definition_after_declarator (cp_parser* parser,
19695 bool ctor_initializer_p = false;
19696 bool saved_in_unbraced_linkage_specification_p;
19697 bool saved_in_function_body;
19698 unsigned saved_num_template_parameter_lists;
19701 saved_in_function_body = parser->in_function_body;
19702 parser->in_function_body = true;
19703 /* If the next token is `return', then the code may be trying to
19704 make use of the "named return value" extension that G++ used to
19706 token = cp_lexer_peek_token (parser->lexer);
19707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19709 /* Consume the `return' keyword. */
19710 cp_lexer_consume_token (parser->lexer);
19711 /* Look for the identifier that indicates what value is to be
19713 cp_parser_identifier (parser);
19714 /* Issue an error message. */
19715 error_at (token->location,
19716 "named return values are no longer supported");
19717 /* Skip tokens until we reach the start of the function body. */
19720 cp_token *token = cp_lexer_peek_token (parser->lexer);
19721 if (token->type == CPP_OPEN_BRACE
19722 || token->type == CPP_EOF
19723 || token->type == CPP_PRAGMA_EOL)
19725 cp_lexer_consume_token (parser->lexer);
19728 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19729 anything declared inside `f'. */
19730 saved_in_unbraced_linkage_specification_p
19731 = parser->in_unbraced_linkage_specification_p;
19732 parser->in_unbraced_linkage_specification_p = false;
19733 /* Inside the function, surrounding template-parameter-lists do not
19735 saved_num_template_parameter_lists
19736 = parser->num_template_parameter_lists;
19737 parser->num_template_parameter_lists = 0;
19739 start_lambda_scope (current_function_decl);
19741 /* If the next token is `try', then we are looking at a
19742 function-try-block. */
19743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19744 ctor_initializer_p = cp_parser_function_try_block (parser);
19745 /* A function-try-block includes the function-body, so we only do
19746 this next part if we're not processing a function-try-block. */
19749 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19751 finish_lambda_scope ();
19753 /* Finish the function. */
19754 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19755 (inline_p ? 2 : 0));
19756 /* Generate code for it, if necessary. */
19757 expand_or_defer_fn (fn);
19758 /* Restore the saved values. */
19759 parser->in_unbraced_linkage_specification_p
19760 = saved_in_unbraced_linkage_specification_p;
19761 parser->num_template_parameter_lists
19762 = saved_num_template_parameter_lists;
19763 parser->in_function_body = saved_in_function_body;
19768 /* Parse a template-declaration, assuming that the `export' (and
19769 `extern') keywords, if present, has already been scanned. MEMBER_P
19770 is as for cp_parser_template_declaration. */
19773 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19775 tree decl = NULL_TREE;
19776 VEC (deferred_access_check,gc) *checks;
19777 tree parameter_list;
19778 bool friend_p = false;
19779 bool need_lang_pop;
19782 /* Look for the `template' keyword. */
19783 token = cp_lexer_peek_token (parser->lexer);
19784 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19788 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19790 if (at_class_scope_p () && current_function_decl)
19792 /* 14.5.2.2 [temp.mem]
19794 A local class shall not have member templates. */
19795 error_at (token->location,
19796 "invalid declaration of member template in local class");
19797 cp_parser_skip_to_end_of_block_or_statement (parser);
19802 A template ... shall not have C linkage. */
19803 if (current_lang_name == lang_name_c)
19805 error_at (token->location, "template with C linkage");
19806 /* Give it C++ linkage to avoid confusing other parts of the
19808 push_lang_context (lang_name_cplusplus);
19809 need_lang_pop = true;
19812 need_lang_pop = false;
19814 /* We cannot perform access checks on the template parameter
19815 declarations until we know what is being declared, just as we
19816 cannot check the decl-specifier list. */
19817 push_deferring_access_checks (dk_deferred);
19819 /* If the next token is `>', then we have an invalid
19820 specialization. Rather than complain about an invalid template
19821 parameter, issue an error message here. */
19822 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19824 cp_parser_error (parser, "invalid explicit specialization");
19825 begin_specialization ();
19826 parameter_list = NULL_TREE;
19829 /* Parse the template parameters. */
19830 parameter_list = cp_parser_template_parameter_list (parser);
19832 /* Get the deferred access checks from the parameter list. These
19833 will be checked once we know what is being declared, as for a
19834 member template the checks must be performed in the scope of the
19835 class containing the member. */
19836 checks = get_deferred_access_checks ();
19838 /* Look for the `>'. */
19839 cp_parser_skip_to_end_of_template_parameter_list (parser);
19840 /* We just processed one more parameter list. */
19841 ++parser->num_template_parameter_lists;
19842 /* If the next token is `template', there are more template
19844 if (cp_lexer_next_token_is_keyword (parser->lexer,
19846 cp_parser_template_declaration_after_export (parser, member_p);
19849 /* There are no access checks when parsing a template, as we do not
19850 know if a specialization will be a friend. */
19851 push_deferring_access_checks (dk_no_check);
19852 token = cp_lexer_peek_token (parser->lexer);
19853 decl = cp_parser_single_declaration (parser,
19856 /*explicit_specialization_p=*/false,
19858 pop_deferring_access_checks ();
19860 /* If this is a member template declaration, let the front
19862 if (member_p && !friend_p && decl)
19864 if (TREE_CODE (decl) == TYPE_DECL)
19865 cp_parser_check_access_in_redeclaration (decl, token->location);
19867 decl = finish_member_template_decl (decl);
19869 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19870 make_friend_class (current_class_type, TREE_TYPE (decl),
19871 /*complain=*/true);
19873 /* We are done with the current parameter list. */
19874 --parser->num_template_parameter_lists;
19876 pop_deferring_access_checks ();
19879 finish_template_decl (parameter_list);
19881 /* Register member declarations. */
19882 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19883 finish_member_declaration (decl);
19884 /* For the erroneous case of a template with C linkage, we pushed an
19885 implicit C++ linkage scope; exit that scope now. */
19887 pop_lang_context ();
19888 /* If DECL is a function template, we must return to parse it later.
19889 (Even though there is no definition, there might be default
19890 arguments that need handling.) */
19891 if (member_p && decl
19892 && (TREE_CODE (decl) == FUNCTION_DECL
19893 || DECL_FUNCTION_TEMPLATE_P (decl)))
19894 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19897 /* Perform the deferred access checks from a template-parameter-list.
19898 CHECKS is a TREE_LIST of access checks, as returned by
19899 get_deferred_access_checks. */
19902 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19904 ++processing_template_parmlist;
19905 perform_access_checks (checks);
19906 --processing_template_parmlist;
19909 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19910 `function-definition' sequence. MEMBER_P is true, this declaration
19911 appears in a class scope.
19913 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19914 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19917 cp_parser_single_declaration (cp_parser* parser,
19918 VEC (deferred_access_check,gc)* checks,
19920 bool explicit_specialization_p,
19923 int declares_class_or_enum;
19924 tree decl = NULL_TREE;
19925 cp_decl_specifier_seq decl_specifiers;
19926 bool function_definition_p = false;
19927 cp_token *decl_spec_token_start;
19929 /* This function is only used when processing a template
19931 gcc_assert (innermost_scope_kind () == sk_template_parms
19932 || innermost_scope_kind () == sk_template_spec);
19934 /* Defer access checks until we know what is being declared. */
19935 push_deferring_access_checks (dk_deferred);
19937 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19939 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19940 cp_parser_decl_specifier_seq (parser,
19941 CP_PARSER_FLAGS_OPTIONAL,
19943 &declares_class_or_enum);
19945 *friend_p = cp_parser_friend_p (&decl_specifiers);
19947 /* There are no template typedefs. */
19948 if (decl_specifiers.specs[(int) ds_typedef])
19950 error_at (decl_spec_token_start->location,
19951 "template declaration of %<typedef%>");
19952 decl = error_mark_node;
19955 /* Gather up the access checks that occurred the
19956 decl-specifier-seq. */
19957 stop_deferring_access_checks ();
19959 /* Check for the declaration of a template class. */
19960 if (declares_class_or_enum)
19962 if (cp_parser_declares_only_class_p (parser))
19964 decl = shadow_tag (&decl_specifiers);
19969 friend template <typename T> struct A<T>::B;
19972 A<T>::B will be represented by a TYPENAME_TYPE, and
19973 therefore not recognized by shadow_tag. */
19974 if (friend_p && *friend_p
19976 && decl_specifiers.type
19977 && TYPE_P (decl_specifiers.type))
19978 decl = decl_specifiers.type;
19980 if (decl && decl != error_mark_node)
19981 decl = TYPE_NAME (decl);
19983 decl = error_mark_node;
19985 /* Perform access checks for template parameters. */
19986 cp_parser_perform_template_parameter_access_checks (checks);
19990 /* Complain about missing 'typename' or other invalid type names. */
19991 if (!decl_specifiers.any_type_specifiers_p)
19992 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19994 /* If it's not a template class, try for a template function. If
19995 the next token is a `;', then this declaration does not declare
19996 anything. But, if there were errors in the decl-specifiers, then
19997 the error might well have come from an attempted class-specifier.
19998 In that case, there's no need to warn about a missing declarator. */
20000 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20001 || decl_specifiers.type != error_mark_node))
20003 decl = cp_parser_init_declarator (parser,
20006 /*function_definition_allowed_p=*/true,
20008 declares_class_or_enum,
20009 &function_definition_p);
20011 /* 7.1.1-1 [dcl.stc]
20013 A storage-class-specifier shall not be specified in an explicit
20014 specialization... */
20016 && explicit_specialization_p
20017 && decl_specifiers.storage_class != sc_none)
20019 error_at (decl_spec_token_start->location,
20020 "explicit template specialization cannot have a storage class");
20021 decl = error_mark_node;
20025 pop_deferring_access_checks ();
20027 /* Clear any current qualification; whatever comes next is the start
20028 of something new. */
20029 parser->scope = NULL_TREE;
20030 parser->qualifying_scope = NULL_TREE;
20031 parser->object_scope = NULL_TREE;
20032 /* Look for a trailing `;' after the declaration. */
20033 if (!function_definition_p
20034 && (decl == error_mark_node
20035 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20036 cp_parser_skip_to_end_of_block_or_statement (parser);
20041 /* Parse a cast-expression that is not the operand of a unary "&". */
20044 cp_parser_simple_cast_expression (cp_parser *parser)
20046 return cp_parser_cast_expression (parser, /*address_p=*/false,
20047 /*cast_p=*/false, NULL);
20050 /* Parse a functional cast to TYPE. Returns an expression
20051 representing the cast. */
20054 cp_parser_functional_cast (cp_parser* parser, tree type)
20057 tree expression_list;
20061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20063 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20064 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20065 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20066 if (TREE_CODE (type) == TYPE_DECL)
20067 type = TREE_TYPE (type);
20068 return finish_compound_literal (type, expression_list);
20072 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20074 /*allow_expansion_p=*/true,
20075 /*non_constant_p=*/NULL);
20077 expression_list = error_mark_node;
20080 expression_list = build_tree_list_vec (vec);
20081 release_tree_vector (vec);
20084 cast = build_functional_cast (type, expression_list,
20085 tf_warning_or_error);
20086 /* [expr.const]/1: In an integral constant expression "only type
20087 conversions to integral or enumeration type can be used". */
20088 if (TREE_CODE (type) == TYPE_DECL)
20089 type = TREE_TYPE (type);
20090 if (cast != error_mark_node
20091 && !cast_valid_in_integral_constant_expression_p (type)
20092 && cp_parser_non_integral_constant_expression (parser,
20094 return error_mark_node;
20098 /* Save the tokens that make up the body of a member function defined
20099 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20100 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20101 specifiers applied to the declaration. Returns the FUNCTION_DECL
20102 for the member function. */
20105 cp_parser_save_member_function_body (cp_parser* parser,
20106 cp_decl_specifier_seq *decl_specifiers,
20107 cp_declarator *declarator,
20114 /* Create the FUNCTION_DECL. */
20115 fn = grokmethod (decl_specifiers, declarator, attributes);
20116 /* If something went badly wrong, bail out now. */
20117 if (fn == error_mark_node)
20119 /* If there's a function-body, skip it. */
20120 if (cp_parser_token_starts_function_definition_p
20121 (cp_lexer_peek_token (parser->lexer)))
20122 cp_parser_skip_to_end_of_block_or_statement (parser);
20123 return error_mark_node;
20126 /* Remember it, if there default args to post process. */
20127 cp_parser_save_default_args (parser, fn);
20129 /* Save away the tokens that make up the body of the
20131 first = parser->lexer->next_token;
20132 /* We can have braced-init-list mem-initializers before the fn body. */
20133 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20135 cp_lexer_consume_token (parser->lexer);
20136 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20137 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20139 /* cache_group will stop after an un-nested { } pair, too. */
20140 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20143 /* variadic mem-inits have ... after the ')'. */
20144 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20145 cp_lexer_consume_token (parser->lexer);
20148 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20149 /* Handle function try blocks. */
20150 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20151 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20152 last = parser->lexer->next_token;
20154 /* Save away the inline definition; we will process it when the
20155 class is complete. */
20156 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20157 DECL_PENDING_INLINE_P (fn) = 1;
20159 /* We need to know that this was defined in the class, so that
20160 friend templates are handled correctly. */
20161 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20163 /* Add FN to the queue of functions to be parsed later. */
20164 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20169 /* Parse a template-argument-list, as well as the trailing ">" (but
20170 not the opening ">"). See cp_parser_template_argument_list for the
20174 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20178 tree saved_qualifying_scope;
20179 tree saved_object_scope;
20180 bool saved_greater_than_is_operator_p;
20181 int saved_unevaluated_operand;
20182 int saved_inhibit_evaluation_warnings;
20186 When parsing a template-id, the first non-nested `>' is taken as
20187 the end of the template-argument-list rather than a greater-than
20189 saved_greater_than_is_operator_p
20190 = parser->greater_than_is_operator_p;
20191 parser->greater_than_is_operator_p = false;
20192 /* Parsing the argument list may modify SCOPE, so we save it
20194 saved_scope = parser->scope;
20195 saved_qualifying_scope = parser->qualifying_scope;
20196 saved_object_scope = parser->object_scope;
20197 /* We need to evaluate the template arguments, even though this
20198 template-id may be nested within a "sizeof". */
20199 saved_unevaluated_operand = cp_unevaluated_operand;
20200 cp_unevaluated_operand = 0;
20201 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20202 c_inhibit_evaluation_warnings = 0;
20203 /* Parse the template-argument-list itself. */
20204 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20205 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20206 arguments = NULL_TREE;
20208 arguments = cp_parser_template_argument_list (parser);
20209 /* Look for the `>' that ends the template-argument-list. If we find
20210 a '>>' instead, it's probably just a typo. */
20211 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20213 if (cxx_dialect != cxx98)
20215 /* In C++0x, a `>>' in a template argument list or cast
20216 expression is considered to be two separate `>'
20217 tokens. So, change the current token to a `>', but don't
20218 consume it: it will be consumed later when the outer
20219 template argument list (or cast expression) is parsed.
20220 Note that this replacement of `>' for `>>' is necessary
20221 even if we are parsing tentatively: in the tentative
20222 case, after calling
20223 cp_parser_enclosed_template_argument_list we will always
20224 throw away all of the template arguments and the first
20225 closing `>', either because the template argument list
20226 was erroneous or because we are replacing those tokens
20227 with a CPP_TEMPLATE_ID token. The second `>' (which will
20228 not have been thrown away) is needed either to close an
20229 outer template argument list or to complete a new-style
20231 cp_token *token = cp_lexer_peek_token (parser->lexer);
20232 token->type = CPP_GREATER;
20234 else if (!saved_greater_than_is_operator_p)
20236 /* If we're in a nested template argument list, the '>>' has
20237 to be a typo for '> >'. We emit the error message, but we
20238 continue parsing and we push a '>' as next token, so that
20239 the argument list will be parsed correctly. Note that the
20240 global source location is still on the token before the
20241 '>>', so we need to say explicitly where we want it. */
20242 cp_token *token = cp_lexer_peek_token (parser->lexer);
20243 error_at (token->location, "%<>>%> should be %<> >%> "
20244 "within a nested template argument list");
20246 token->type = CPP_GREATER;
20250 /* If this is not a nested template argument list, the '>>'
20251 is a typo for '>'. Emit an error message and continue.
20252 Same deal about the token location, but here we can get it
20253 right by consuming the '>>' before issuing the diagnostic. */
20254 cp_token *token = cp_lexer_consume_token (parser->lexer);
20255 error_at (token->location,
20256 "spurious %<>>%>, use %<>%> to terminate "
20257 "a template argument list");
20261 cp_parser_skip_to_end_of_template_parameter_list (parser);
20262 /* The `>' token might be a greater-than operator again now. */
20263 parser->greater_than_is_operator_p
20264 = saved_greater_than_is_operator_p;
20265 /* Restore the SAVED_SCOPE. */
20266 parser->scope = saved_scope;
20267 parser->qualifying_scope = saved_qualifying_scope;
20268 parser->object_scope = saved_object_scope;
20269 cp_unevaluated_operand = saved_unevaluated_operand;
20270 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20275 /* MEMBER_FUNCTION is a member function, or a friend. If default
20276 arguments, or the body of the function have not yet been parsed,
20280 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20282 /* If this member is a template, get the underlying
20284 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20285 member_function = DECL_TEMPLATE_RESULT (member_function);
20287 /* There should not be any class definitions in progress at this
20288 point; the bodies of members are only parsed outside of all class
20290 gcc_assert (parser->num_classes_being_defined == 0);
20291 /* While we're parsing the member functions we might encounter more
20292 classes. We want to handle them right away, but we don't want
20293 them getting mixed up with functions that are currently in the
20295 push_unparsed_function_queues (parser);
20297 /* Make sure that any template parameters are in scope. */
20298 maybe_begin_member_template_processing (member_function);
20300 /* If the body of the function has not yet been parsed, parse it
20302 if (DECL_PENDING_INLINE_P (member_function))
20304 tree function_scope;
20305 cp_token_cache *tokens;
20307 /* The function is no longer pending; we are processing it. */
20308 tokens = DECL_PENDING_INLINE_INFO (member_function);
20309 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20310 DECL_PENDING_INLINE_P (member_function) = 0;
20312 /* If this is a local class, enter the scope of the containing
20314 function_scope = current_function_decl;
20315 if (function_scope)
20316 push_function_context ();
20318 /* Push the body of the function onto the lexer stack. */
20319 cp_parser_push_lexer_for_tokens (parser, tokens);
20321 /* Let the front end know that we going to be defining this
20323 start_preparsed_function (member_function, NULL_TREE,
20324 SF_PRE_PARSED | SF_INCLASS_INLINE);
20326 /* Don't do access checking if it is a templated function. */
20327 if (processing_template_decl)
20328 push_deferring_access_checks (dk_no_check);
20330 /* Now, parse the body of the function. */
20331 cp_parser_function_definition_after_declarator (parser,
20332 /*inline_p=*/true);
20334 if (processing_template_decl)
20335 pop_deferring_access_checks ();
20337 /* Leave the scope of the containing function. */
20338 if (function_scope)
20339 pop_function_context ();
20340 cp_parser_pop_lexer (parser);
20343 /* Remove any template parameters from the symbol table. */
20344 maybe_end_member_template_processing ();
20346 /* Restore the queue. */
20347 pop_unparsed_function_queues (parser);
20350 /* If DECL contains any default args, remember it on the unparsed
20351 functions queue. */
20354 cp_parser_save_default_args (cp_parser* parser, tree decl)
20358 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20360 probe = TREE_CHAIN (probe))
20361 if (TREE_PURPOSE (probe))
20363 cp_default_arg_entry *entry
20364 = VEC_safe_push (cp_default_arg_entry, gc,
20365 unparsed_funs_with_default_args, NULL);
20366 entry->class_type = current_class_type;
20367 entry->decl = decl;
20372 /* FN is a FUNCTION_DECL which may contains a parameter with an
20373 unparsed DEFAULT_ARG. Parse the default args now. This function
20374 assumes that the current scope is the scope in which the default
20375 argument should be processed. */
20378 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20380 bool saved_local_variables_forbidden_p;
20381 tree parm, parmdecl;
20383 /* While we're parsing the default args, we might (due to the
20384 statement expression extension) encounter more classes. We want
20385 to handle them right away, but we don't want them getting mixed
20386 up with default args that are currently in the queue. */
20387 push_unparsed_function_queues (parser);
20389 /* Local variable names (and the `this' keyword) may not appear
20390 in a default argument. */
20391 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20392 parser->local_variables_forbidden_p = true;
20394 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20395 parmdecl = DECL_ARGUMENTS (fn);
20396 parm && parm != void_list_node;
20397 parm = TREE_CHAIN (parm),
20398 parmdecl = DECL_CHAIN (parmdecl))
20400 cp_token_cache *tokens;
20401 tree default_arg = TREE_PURPOSE (parm);
20403 VEC(tree,gc) *insts;
20410 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20411 /* This can happen for a friend declaration for a function
20412 already declared with default arguments. */
20415 /* Push the saved tokens for the default argument onto the parser's
20417 tokens = DEFARG_TOKENS (default_arg);
20418 cp_parser_push_lexer_for_tokens (parser, tokens);
20420 start_lambda_scope (parmdecl);
20422 /* Parse the assignment-expression. */
20423 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20424 if (parsed_arg == error_mark_node)
20426 cp_parser_pop_lexer (parser);
20430 if (!processing_template_decl)
20431 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20433 TREE_PURPOSE (parm) = parsed_arg;
20435 /* Update any instantiations we've already created. */
20436 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20437 VEC_iterate (tree, insts, ix, copy); ix++)
20438 TREE_PURPOSE (copy) = parsed_arg;
20440 finish_lambda_scope ();
20442 /* If the token stream has not been completely used up, then
20443 there was extra junk after the end of the default
20445 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20446 cp_parser_error (parser, "expected %<,%>");
20448 /* Revert to the main lexer. */
20449 cp_parser_pop_lexer (parser);
20452 /* Make sure no default arg is missing. */
20453 check_default_args (fn);
20455 /* Restore the state of local_variables_forbidden_p. */
20456 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20458 /* Restore the queue. */
20459 pop_unparsed_function_queues (parser);
20462 /* Parse the operand of `sizeof' (or a similar operator). Returns
20463 either a TYPE or an expression, depending on the form of the
20464 input. The KEYWORD indicates which kind of expression we have
20468 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20470 tree expr = NULL_TREE;
20471 const char *saved_message;
20473 bool saved_integral_constant_expression_p;
20474 bool saved_non_integral_constant_expression_p;
20475 bool pack_expansion_p = false;
20477 /* Types cannot be defined in a `sizeof' expression. Save away the
20479 saved_message = parser->type_definition_forbidden_message;
20480 /* And create the new one. */
20481 tmp = concat ("types may not be defined in %<",
20482 IDENTIFIER_POINTER (ridpointers[keyword]),
20483 "%> expressions", NULL);
20484 parser->type_definition_forbidden_message = tmp;
20486 /* The restrictions on constant-expressions do not apply inside
20487 sizeof expressions. */
20488 saved_integral_constant_expression_p
20489 = parser->integral_constant_expression_p;
20490 saved_non_integral_constant_expression_p
20491 = parser->non_integral_constant_expression_p;
20492 parser->integral_constant_expression_p = false;
20494 /* If it's a `...', then we are computing the length of a parameter
20496 if (keyword == RID_SIZEOF
20497 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20499 /* Consume the `...'. */
20500 cp_lexer_consume_token (parser->lexer);
20501 maybe_warn_variadic_templates ();
20503 /* Note that this is an expansion. */
20504 pack_expansion_p = true;
20507 /* Do not actually evaluate the expression. */
20508 ++cp_unevaluated_operand;
20509 ++c_inhibit_evaluation_warnings;
20510 /* If it's a `(', then we might be looking at the type-id
20512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20515 bool saved_in_type_id_in_expr_p;
20517 /* We can't be sure yet whether we're looking at a type-id or an
20519 cp_parser_parse_tentatively (parser);
20520 /* Consume the `('. */
20521 cp_lexer_consume_token (parser->lexer);
20522 /* Parse the type-id. */
20523 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20524 parser->in_type_id_in_expr_p = true;
20525 type = cp_parser_type_id (parser);
20526 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20527 /* Now, look for the trailing `)'. */
20528 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20529 /* If all went well, then we're done. */
20530 if (cp_parser_parse_definitely (parser))
20532 cp_decl_specifier_seq decl_specs;
20534 /* Build a trivial decl-specifier-seq. */
20535 clear_decl_specs (&decl_specs);
20536 decl_specs.type = type;
20538 /* Call grokdeclarator to figure out what type this is. */
20539 expr = grokdeclarator (NULL,
20543 /*attrlist=*/NULL);
20547 /* If the type-id production did not work out, then we must be
20548 looking at the unary-expression production. */
20550 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20551 /*cast_p=*/false, NULL);
20553 if (pack_expansion_p)
20554 /* Build a pack expansion. */
20555 expr = make_pack_expansion (expr);
20557 /* Go back to evaluating expressions. */
20558 --cp_unevaluated_operand;
20559 --c_inhibit_evaluation_warnings;
20561 /* Free the message we created. */
20563 /* And restore the old one. */
20564 parser->type_definition_forbidden_message = saved_message;
20565 parser->integral_constant_expression_p
20566 = saved_integral_constant_expression_p;
20567 parser->non_integral_constant_expression_p
20568 = saved_non_integral_constant_expression_p;
20573 /* If the current declaration has no declarator, return true. */
20576 cp_parser_declares_only_class_p (cp_parser *parser)
20578 /* If the next token is a `;' or a `,' then there is no
20580 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20581 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20584 /* Update the DECL_SPECS to reflect the storage class indicated by
20588 cp_parser_set_storage_class (cp_parser *parser,
20589 cp_decl_specifier_seq *decl_specs,
20591 location_t location)
20593 cp_storage_class storage_class;
20595 if (parser->in_unbraced_linkage_specification_p)
20597 error_at (location, "invalid use of %qD in linkage specification",
20598 ridpointers[keyword]);
20601 else if (decl_specs->storage_class != sc_none)
20603 decl_specs->conflicting_specifiers_p = true;
20607 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20608 && decl_specs->specs[(int) ds_thread])
20610 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20611 decl_specs->specs[(int) ds_thread] = 0;
20617 storage_class = sc_auto;
20620 storage_class = sc_register;
20623 storage_class = sc_static;
20626 storage_class = sc_extern;
20629 storage_class = sc_mutable;
20632 gcc_unreachable ();
20634 decl_specs->storage_class = storage_class;
20636 /* A storage class specifier cannot be applied alongside a typedef
20637 specifier. If there is a typedef specifier present then set
20638 conflicting_specifiers_p which will trigger an error later
20639 on in grokdeclarator. */
20640 if (decl_specs->specs[(int)ds_typedef])
20641 decl_specs->conflicting_specifiers_p = true;
20644 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20645 is true, the type is a user-defined type; otherwise it is a
20646 built-in type specified by a keyword. */
20649 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20651 location_t location,
20652 bool user_defined_p)
20654 decl_specs->any_specifiers_p = true;
20656 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20657 (with, for example, in "typedef int wchar_t;") we remember that
20658 this is what happened. In system headers, we ignore these
20659 declarations so that G++ can work with system headers that are not
20661 if (decl_specs->specs[(int) ds_typedef]
20663 && (type_spec == boolean_type_node
20664 || type_spec == char16_type_node
20665 || type_spec == char32_type_node
20666 || type_spec == wchar_type_node)
20667 && (decl_specs->type
20668 || decl_specs->specs[(int) ds_long]
20669 || decl_specs->specs[(int) ds_short]
20670 || decl_specs->specs[(int) ds_unsigned]
20671 || decl_specs->specs[(int) ds_signed]))
20673 decl_specs->redefined_builtin_type = type_spec;
20674 if (!decl_specs->type)
20676 decl_specs->type = type_spec;
20677 decl_specs->user_defined_type_p = false;
20678 decl_specs->type_location = location;
20681 else if (decl_specs->type)
20682 decl_specs->multiple_types_p = true;
20685 decl_specs->type = type_spec;
20686 decl_specs->user_defined_type_p = user_defined_p;
20687 decl_specs->redefined_builtin_type = NULL_TREE;
20688 decl_specs->type_location = location;
20692 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20693 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20696 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20698 return decl_specifiers->specs[(int) ds_friend] != 0;
20701 /* Issue an error message indicating that TOKEN_DESC was expected.
20702 If KEYWORD is true, it indicated this function is called by
20703 cp_parser_require_keword and the required token can only be
20704 a indicated keyword. */
20707 cp_parser_required_error (cp_parser *parser,
20708 required_token token_desc,
20711 switch (token_desc)
20714 cp_parser_error (parser, "expected %<new%>");
20717 cp_parser_error (parser, "expected %<delete%>");
20720 cp_parser_error (parser, "expected %<return%>");
20723 cp_parser_error (parser, "expected %<while%>");
20726 cp_parser_error (parser, "expected %<extern%>");
20728 case RT_STATIC_ASSERT:
20729 cp_parser_error (parser, "expected %<static_assert%>");
20732 cp_parser_error (parser, "expected %<decltype%>");
20735 cp_parser_error (parser, "expected %<operator%>");
20738 cp_parser_error (parser, "expected %<class%>");
20741 cp_parser_error (parser, "expected %<template%>");
20744 cp_parser_error (parser, "expected %<namespace%>");
20747 cp_parser_error (parser, "expected %<using%>");
20750 cp_parser_error (parser, "expected %<asm%>");
20753 cp_parser_error (parser, "expected %<try%>");
20756 cp_parser_error (parser, "expected %<catch%>");
20759 cp_parser_error (parser, "expected %<throw%>");
20762 cp_parser_error (parser, "expected %<__label__%>");
20765 cp_parser_error (parser, "expected %<@try%>");
20767 case RT_AT_SYNCHRONIZED:
20768 cp_parser_error (parser, "expected %<@synchronized%>");
20771 cp_parser_error (parser, "expected %<@throw%>");
20778 switch (token_desc)
20781 cp_parser_error (parser, "expected %<;%>");
20783 case RT_OPEN_PAREN:
20784 cp_parser_error (parser, "expected %<(%>");
20786 case RT_CLOSE_BRACE:
20787 cp_parser_error (parser, "expected %<}%>");
20789 case RT_OPEN_BRACE:
20790 cp_parser_error (parser, "expected %<{%>");
20792 case RT_CLOSE_SQUARE:
20793 cp_parser_error (parser, "expected %<]%>");
20795 case RT_OPEN_SQUARE:
20796 cp_parser_error (parser, "expected %<[%>");
20799 cp_parser_error (parser, "expected %<,%>");
20802 cp_parser_error (parser, "expected %<::%>");
20805 cp_parser_error (parser, "expected %<<%>");
20808 cp_parser_error (parser, "expected %<>%>");
20811 cp_parser_error (parser, "expected %<=%>");
20814 cp_parser_error (parser, "expected %<...%>");
20817 cp_parser_error (parser, "expected %<*%>");
20820 cp_parser_error (parser, "expected %<~%>");
20823 cp_parser_error (parser, "expected %<:%>");
20825 case RT_COLON_SCOPE:
20826 cp_parser_error (parser, "expected %<:%> or %<::%>");
20828 case RT_CLOSE_PAREN:
20829 cp_parser_error (parser, "expected %<)%>");
20831 case RT_COMMA_CLOSE_PAREN:
20832 cp_parser_error (parser, "expected %<,%> or %<)%>");
20834 case RT_PRAGMA_EOL:
20835 cp_parser_error (parser, "expected end of line");
20838 cp_parser_error (parser, "expected identifier");
20841 cp_parser_error (parser, "expected selection-statement");
20843 case RT_INTERATION:
20844 cp_parser_error (parser, "expected iteration-statement");
20847 cp_parser_error (parser, "expected jump-statement");
20850 cp_parser_error (parser, "expected class-key");
20852 case RT_CLASS_TYPENAME_TEMPLATE:
20853 cp_parser_error (parser,
20854 "expected %<class%>, %<typename%>, or %<template%>");
20857 gcc_unreachable ();
20861 gcc_unreachable ();
20866 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20867 issue an error message indicating that TOKEN_DESC was expected.
20869 Returns the token consumed, if the token had the appropriate type.
20870 Otherwise, returns NULL. */
20873 cp_parser_require (cp_parser* parser,
20874 enum cpp_ttype type,
20875 required_token token_desc)
20877 if (cp_lexer_next_token_is (parser->lexer, type))
20878 return cp_lexer_consume_token (parser->lexer);
20881 /* Output the MESSAGE -- unless we're parsing tentatively. */
20882 if (!cp_parser_simulate_error (parser))
20883 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20888 /* An error message is produced if the next token is not '>'.
20889 All further tokens are skipped until the desired token is
20890 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20893 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20895 /* Current level of '< ... >'. */
20896 unsigned level = 0;
20897 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20898 unsigned nesting_depth = 0;
20900 /* Are we ready, yet? If not, issue error message. */
20901 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20904 /* Skip tokens until the desired token is found. */
20907 /* Peek at the next token. */
20908 switch (cp_lexer_peek_token (parser->lexer)->type)
20911 if (!nesting_depth)
20916 if (cxx_dialect == cxx98)
20917 /* C++0x views the `>>' operator as two `>' tokens, but
20920 else if (!nesting_depth && level-- == 0)
20922 /* We've hit a `>>' where the first `>' closes the
20923 template argument list, and the second `>' is
20924 spurious. Just consume the `>>' and stop; we've
20925 already produced at least one error. */
20926 cp_lexer_consume_token (parser->lexer);
20929 /* Fall through for C++0x, so we handle the second `>' in
20933 if (!nesting_depth && level-- == 0)
20935 /* We've reached the token we want, consume it and stop. */
20936 cp_lexer_consume_token (parser->lexer);
20941 case CPP_OPEN_PAREN:
20942 case CPP_OPEN_SQUARE:
20946 case CPP_CLOSE_PAREN:
20947 case CPP_CLOSE_SQUARE:
20948 if (nesting_depth-- == 0)
20953 case CPP_PRAGMA_EOL:
20954 case CPP_SEMICOLON:
20955 case CPP_OPEN_BRACE:
20956 case CPP_CLOSE_BRACE:
20957 /* The '>' was probably forgotten, don't look further. */
20964 /* Consume this token. */
20965 cp_lexer_consume_token (parser->lexer);
20969 /* If the next token is the indicated keyword, consume it. Otherwise,
20970 issue an error message indicating that TOKEN_DESC was expected.
20972 Returns the token consumed, if the token had the appropriate type.
20973 Otherwise, returns NULL. */
20976 cp_parser_require_keyword (cp_parser* parser,
20978 required_token token_desc)
20980 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20982 if (token && token->keyword != keyword)
20984 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20991 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20992 function-definition. */
20995 cp_parser_token_starts_function_definition_p (cp_token* token)
20997 return (/* An ordinary function-body begins with an `{'. */
20998 token->type == CPP_OPEN_BRACE
20999 /* A ctor-initializer begins with a `:'. */
21000 || token->type == CPP_COLON
21001 /* A function-try-block begins with `try'. */
21002 || token->keyword == RID_TRY
21003 /* The named return value extension begins with `return'. */
21004 || token->keyword == RID_RETURN);
21007 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21011 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21015 token = cp_lexer_peek_token (parser->lexer);
21016 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21019 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21020 C++0x) ending a template-argument. */
21023 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21027 token = cp_lexer_peek_token (parser->lexer);
21028 return (token->type == CPP_COMMA
21029 || token->type == CPP_GREATER
21030 || token->type == CPP_ELLIPSIS
21031 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21034 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21035 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21038 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21043 token = cp_lexer_peek_nth_token (parser->lexer, n);
21044 if (token->type == CPP_LESS)
21046 /* Check for the sequence `<::' in the original code. It would be lexed as
21047 `[:', where `[' is a digraph, and there is no whitespace before
21049 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21052 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21053 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21059 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21060 or none_type otherwise. */
21062 static enum tag_types
21063 cp_parser_token_is_class_key (cp_token* token)
21065 switch (token->keyword)
21070 return record_type;
21079 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21082 cp_parser_check_class_key (enum tag_types class_key, tree type)
21084 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21085 permerror (input_location, "%qs tag used in naming %q#T",
21086 class_key == union_type ? "union"
21087 : class_key == record_type ? "struct" : "class",
21091 /* Issue an error message if DECL is redeclared with different
21092 access than its original declaration [class.access.spec/3].
21093 This applies to nested classes and nested class templates.
21097 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21099 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21102 if ((TREE_PRIVATE (decl)
21103 != (current_access_specifier == access_private_node))
21104 || (TREE_PROTECTED (decl)
21105 != (current_access_specifier == access_protected_node)))
21106 error_at (location, "%qD redeclared with different access", decl);
21109 /* Look for the `template' keyword, as a syntactic disambiguator.
21110 Return TRUE iff it is present, in which case it will be
21114 cp_parser_optional_template_keyword (cp_parser *parser)
21116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21118 /* The `template' keyword can only be used within templates;
21119 outside templates the parser can always figure out what is a
21120 template and what is not. */
21121 if (!processing_template_decl)
21123 cp_token *token = cp_lexer_peek_token (parser->lexer);
21124 error_at (token->location,
21125 "%<template%> (as a disambiguator) is only allowed "
21126 "within templates");
21127 /* If this part of the token stream is rescanned, the same
21128 error message would be generated. So, we purge the token
21129 from the stream. */
21130 cp_lexer_purge_token (parser->lexer);
21135 /* Consume the `template' keyword. */
21136 cp_lexer_consume_token (parser->lexer);
21144 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21145 set PARSER->SCOPE, and perform other related actions. */
21148 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21151 struct tree_check *check_value;
21152 deferred_access_check *chk;
21153 VEC (deferred_access_check,gc) *checks;
21155 /* Get the stored value. */
21156 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21157 /* Perform any access checks that were deferred. */
21158 checks = check_value->checks;
21161 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21162 perform_or_defer_access_check (chk->binfo,
21166 /* Set the scope from the stored value. */
21167 parser->scope = check_value->value;
21168 parser->qualifying_scope = check_value->qualifying_scope;
21169 parser->object_scope = NULL_TREE;
21172 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21173 encounter the end of a block before what we were looking for. */
21176 cp_parser_cache_group (cp_parser *parser,
21177 enum cpp_ttype end,
21182 cp_token *token = cp_lexer_peek_token (parser->lexer);
21184 /* Abort a parenthesized expression if we encounter a semicolon. */
21185 if ((end == CPP_CLOSE_PAREN || depth == 0)
21186 && token->type == CPP_SEMICOLON)
21188 /* If we've reached the end of the file, stop. */
21189 if (token->type == CPP_EOF
21190 || (end != CPP_PRAGMA_EOL
21191 && token->type == CPP_PRAGMA_EOL))
21193 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21194 /* We've hit the end of an enclosing block, so there's been some
21195 kind of syntax error. */
21198 /* Consume the token. */
21199 cp_lexer_consume_token (parser->lexer);
21200 /* See if it starts a new group. */
21201 if (token->type == CPP_OPEN_BRACE)
21203 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21204 /* In theory this should probably check end == '}', but
21205 cp_parser_save_member_function_body needs it to exit
21206 after either '}' or ')' when called with ')'. */
21210 else if (token->type == CPP_OPEN_PAREN)
21212 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21213 if (depth == 0 && end == CPP_CLOSE_PAREN)
21216 else if (token->type == CPP_PRAGMA)
21217 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21218 else if (token->type == end)
21223 /* Begin parsing tentatively. We always save tokens while parsing
21224 tentatively so that if the tentative parsing fails we can restore the
21228 cp_parser_parse_tentatively (cp_parser* parser)
21230 /* Enter a new parsing context. */
21231 parser->context = cp_parser_context_new (parser->context);
21232 /* Begin saving tokens. */
21233 cp_lexer_save_tokens (parser->lexer);
21234 /* In order to avoid repetitive access control error messages,
21235 access checks are queued up until we are no longer parsing
21237 push_deferring_access_checks (dk_deferred);
21240 /* Commit to the currently active tentative parse. */
21243 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21245 cp_parser_context *context;
21248 /* Mark all of the levels as committed. */
21249 lexer = parser->lexer;
21250 for (context = parser->context; context->next; context = context->next)
21252 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21254 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21255 while (!cp_lexer_saving_tokens (lexer))
21256 lexer = lexer->next;
21257 cp_lexer_commit_tokens (lexer);
21261 /* Abort the currently active tentative parse. All consumed tokens
21262 will be rolled back, and no diagnostics will be issued. */
21265 cp_parser_abort_tentative_parse (cp_parser* parser)
21267 cp_parser_simulate_error (parser);
21268 /* Now, pretend that we want to see if the construct was
21269 successfully parsed. */
21270 cp_parser_parse_definitely (parser);
21273 /* Stop parsing tentatively. If a parse error has occurred, restore the
21274 token stream. Otherwise, commit to the tokens we have consumed.
21275 Returns true if no error occurred; false otherwise. */
21278 cp_parser_parse_definitely (cp_parser* parser)
21280 bool error_occurred;
21281 cp_parser_context *context;
21283 /* Remember whether or not an error occurred, since we are about to
21284 destroy that information. */
21285 error_occurred = cp_parser_error_occurred (parser);
21286 /* Remove the topmost context from the stack. */
21287 context = parser->context;
21288 parser->context = context->next;
21289 /* If no parse errors occurred, commit to the tentative parse. */
21290 if (!error_occurred)
21292 /* Commit to the tokens read tentatively, unless that was
21294 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21295 cp_lexer_commit_tokens (parser->lexer);
21297 pop_to_parent_deferring_access_checks ();
21299 /* Otherwise, if errors occurred, roll back our state so that things
21300 are just as they were before we began the tentative parse. */
21303 cp_lexer_rollback_tokens (parser->lexer);
21304 pop_deferring_access_checks ();
21306 /* Add the context to the front of the free list. */
21307 context->next = cp_parser_context_free_list;
21308 cp_parser_context_free_list = context;
21310 return !error_occurred;
21313 /* Returns true if we are parsing tentatively and are not committed to
21314 this tentative parse. */
21317 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21319 return (cp_parser_parsing_tentatively (parser)
21320 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21323 /* Returns nonzero iff an error has occurred during the most recent
21324 tentative parse. */
21327 cp_parser_error_occurred (cp_parser* parser)
21329 return (cp_parser_parsing_tentatively (parser)
21330 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21333 /* Returns nonzero if GNU extensions are allowed. */
21336 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21338 return parser->allow_gnu_extensions_p;
21341 /* Objective-C++ Productions */
21344 /* Parse an Objective-C expression, which feeds into a primary-expression
21348 objc-message-expression
21349 objc-string-literal
21350 objc-encode-expression
21351 objc-protocol-expression
21352 objc-selector-expression
21354 Returns a tree representation of the expression. */
21357 cp_parser_objc_expression (cp_parser* parser)
21359 /* Try to figure out what kind of declaration is present. */
21360 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21364 case CPP_OPEN_SQUARE:
21365 return cp_parser_objc_message_expression (parser);
21367 case CPP_OBJC_STRING:
21368 kwd = cp_lexer_consume_token (parser->lexer);
21369 return objc_build_string_object (kwd->u.value);
21372 switch (kwd->keyword)
21374 case RID_AT_ENCODE:
21375 return cp_parser_objc_encode_expression (parser);
21377 case RID_AT_PROTOCOL:
21378 return cp_parser_objc_protocol_expression (parser);
21380 case RID_AT_SELECTOR:
21381 return cp_parser_objc_selector_expression (parser);
21387 error_at (kwd->location,
21388 "misplaced %<@%D%> Objective-C++ construct",
21390 cp_parser_skip_to_end_of_block_or_statement (parser);
21393 return error_mark_node;
21396 /* Parse an Objective-C message expression.
21398 objc-message-expression:
21399 [ objc-message-receiver objc-message-args ]
21401 Returns a representation of an Objective-C message. */
21404 cp_parser_objc_message_expression (cp_parser* parser)
21406 tree receiver, messageargs;
21408 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21409 receiver = cp_parser_objc_message_receiver (parser);
21410 messageargs = cp_parser_objc_message_args (parser);
21411 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21413 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21416 /* Parse an objc-message-receiver.
21418 objc-message-receiver:
21420 simple-type-specifier
21422 Returns a representation of the type or expression. */
21425 cp_parser_objc_message_receiver (cp_parser* parser)
21429 /* An Objective-C message receiver may be either (1) a type
21430 or (2) an expression. */
21431 cp_parser_parse_tentatively (parser);
21432 rcv = cp_parser_expression (parser, false, NULL);
21434 if (cp_parser_parse_definitely (parser))
21437 rcv = cp_parser_simple_type_specifier (parser,
21438 /*decl_specs=*/NULL,
21439 CP_PARSER_FLAGS_NONE);
21441 return objc_get_class_reference (rcv);
21444 /* Parse the arguments and selectors comprising an Objective-C message.
21449 objc-selector-args , objc-comma-args
21451 objc-selector-args:
21452 objc-selector [opt] : assignment-expression
21453 objc-selector-args objc-selector [opt] : assignment-expression
21456 assignment-expression
21457 objc-comma-args , assignment-expression
21459 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21460 selector arguments and TREE_VALUE containing a list of comma
21464 cp_parser_objc_message_args (cp_parser* parser)
21466 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21467 bool maybe_unary_selector_p = true;
21468 cp_token *token = cp_lexer_peek_token (parser->lexer);
21470 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21472 tree selector = NULL_TREE, arg;
21474 if (token->type != CPP_COLON)
21475 selector = cp_parser_objc_selector (parser);
21477 /* Detect if we have a unary selector. */
21478 if (maybe_unary_selector_p
21479 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21480 return build_tree_list (selector, NULL_TREE);
21482 maybe_unary_selector_p = false;
21483 cp_parser_require (parser, CPP_COLON, RT_COLON);
21484 arg = cp_parser_assignment_expression (parser, false, NULL);
21487 = chainon (sel_args,
21488 build_tree_list (selector, arg));
21490 token = cp_lexer_peek_token (parser->lexer);
21493 /* Handle non-selector arguments, if any. */
21494 while (token->type == CPP_COMMA)
21498 cp_lexer_consume_token (parser->lexer);
21499 arg = cp_parser_assignment_expression (parser, false, NULL);
21502 = chainon (addl_args,
21503 build_tree_list (NULL_TREE, arg));
21505 token = cp_lexer_peek_token (parser->lexer);
21508 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21510 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21511 return build_tree_list (error_mark_node, error_mark_node);
21514 return build_tree_list (sel_args, addl_args);
21517 /* Parse an Objective-C encode expression.
21519 objc-encode-expression:
21520 @encode objc-typename
21522 Returns an encoded representation of the type argument. */
21525 cp_parser_objc_encode_expression (cp_parser* parser)
21530 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21531 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21532 token = cp_lexer_peek_token (parser->lexer);
21533 type = complete_type (cp_parser_type_id (parser));
21534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21538 error_at (token->location,
21539 "%<@encode%> must specify a type as an argument");
21540 return error_mark_node;
21543 /* This happens if we find @encode(T) (where T is a template
21544 typename or something dependent on a template typename) when
21545 parsing a template. In that case, we can't compile it
21546 immediately, but we rather create an AT_ENCODE_EXPR which will
21547 need to be instantiated when the template is used.
21549 if (dependent_type_p (type))
21551 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21552 TREE_READONLY (value) = 1;
21556 return objc_build_encode_expr (type);
21559 /* Parse an Objective-C @defs expression. */
21562 cp_parser_objc_defs_expression (cp_parser *parser)
21566 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21567 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21568 name = cp_parser_identifier (parser);
21569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21571 return objc_get_class_ivars (name);
21574 /* Parse an Objective-C protocol expression.
21576 objc-protocol-expression:
21577 @protocol ( identifier )
21579 Returns a representation of the protocol expression. */
21582 cp_parser_objc_protocol_expression (cp_parser* parser)
21586 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21587 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21588 proto = cp_parser_identifier (parser);
21589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21591 return objc_build_protocol_expr (proto);
21594 /* Parse an Objective-C selector expression.
21596 objc-selector-expression:
21597 @selector ( objc-method-signature )
21599 objc-method-signature:
21605 objc-selector-seq objc-selector :
21607 Returns a representation of the method selector. */
21610 cp_parser_objc_selector_expression (cp_parser* parser)
21612 tree sel_seq = NULL_TREE;
21613 bool maybe_unary_selector_p = true;
21615 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21617 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21618 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21619 token = cp_lexer_peek_token (parser->lexer);
21621 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21622 || token->type == CPP_SCOPE)
21624 tree selector = NULL_TREE;
21626 if (token->type != CPP_COLON
21627 || token->type == CPP_SCOPE)
21628 selector = cp_parser_objc_selector (parser);
21630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21631 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21633 /* Detect if we have a unary selector. */
21634 if (maybe_unary_selector_p)
21636 sel_seq = selector;
21637 goto finish_selector;
21641 cp_parser_error (parser, "expected %<:%>");
21644 maybe_unary_selector_p = false;
21645 token = cp_lexer_consume_token (parser->lexer);
21647 if (token->type == CPP_SCOPE)
21650 = chainon (sel_seq,
21651 build_tree_list (selector, NULL_TREE));
21653 = chainon (sel_seq,
21654 build_tree_list (NULL_TREE, NULL_TREE));
21658 = chainon (sel_seq,
21659 build_tree_list (selector, NULL_TREE));
21661 token = cp_lexer_peek_token (parser->lexer);
21665 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21667 return objc_build_selector_expr (loc, sel_seq);
21670 /* Parse a list of identifiers.
21672 objc-identifier-list:
21674 objc-identifier-list , identifier
21676 Returns a TREE_LIST of identifier nodes. */
21679 cp_parser_objc_identifier_list (cp_parser* parser)
21685 identifier = cp_parser_identifier (parser);
21686 if (identifier == error_mark_node)
21687 return error_mark_node;
21689 list = build_tree_list (NULL_TREE, identifier);
21690 sep = cp_lexer_peek_token (parser->lexer);
21692 while (sep->type == CPP_COMMA)
21694 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21695 identifier = cp_parser_identifier (parser);
21696 if (identifier == error_mark_node)
21699 list = chainon (list, build_tree_list (NULL_TREE,
21701 sep = cp_lexer_peek_token (parser->lexer);
21707 /* Parse an Objective-C alias declaration.
21709 objc-alias-declaration:
21710 @compatibility_alias identifier identifier ;
21712 This function registers the alias mapping with the Objective-C front end.
21713 It returns nothing. */
21716 cp_parser_objc_alias_declaration (cp_parser* parser)
21720 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21721 alias = cp_parser_identifier (parser);
21722 orig = cp_parser_identifier (parser);
21723 objc_declare_alias (alias, orig);
21724 cp_parser_consume_semicolon_at_end_of_statement (parser);
21727 /* Parse an Objective-C class forward-declaration.
21729 objc-class-declaration:
21730 @class objc-identifier-list ;
21732 The function registers the forward declarations with the Objective-C
21733 front end. It returns nothing. */
21736 cp_parser_objc_class_declaration (cp_parser* parser)
21738 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21739 objc_declare_class (cp_parser_objc_identifier_list (parser));
21740 cp_parser_consume_semicolon_at_end_of_statement (parser);
21743 /* Parse a list of Objective-C protocol references.
21745 objc-protocol-refs-opt:
21746 objc-protocol-refs [opt]
21748 objc-protocol-refs:
21749 < objc-identifier-list >
21751 Returns a TREE_LIST of identifiers, if any. */
21754 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21756 tree protorefs = NULL_TREE;
21758 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21760 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21761 protorefs = cp_parser_objc_identifier_list (parser);
21762 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21768 /* Parse a Objective-C visibility specification. */
21771 cp_parser_objc_visibility_spec (cp_parser* parser)
21773 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21775 switch (vis->keyword)
21777 case RID_AT_PRIVATE:
21778 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21780 case RID_AT_PROTECTED:
21781 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21783 case RID_AT_PUBLIC:
21784 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21786 case RID_AT_PACKAGE:
21787 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21793 /* Eat '@private'/'@protected'/'@public'. */
21794 cp_lexer_consume_token (parser->lexer);
21797 /* Parse an Objective-C method type. Return 'true' if it is a class
21798 (+) method, and 'false' if it is an instance (-) method. */
21801 cp_parser_objc_method_type (cp_parser* parser)
21803 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21809 /* Parse an Objective-C protocol qualifier. */
21812 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21814 tree quals = NULL_TREE, node;
21815 cp_token *token = cp_lexer_peek_token (parser->lexer);
21817 node = token->u.value;
21819 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21820 && (node == ridpointers [(int) RID_IN]
21821 || node == ridpointers [(int) RID_OUT]
21822 || node == ridpointers [(int) RID_INOUT]
21823 || node == ridpointers [(int) RID_BYCOPY]
21824 || node == ridpointers [(int) RID_BYREF]
21825 || node == ridpointers [(int) RID_ONEWAY]))
21827 quals = tree_cons (NULL_TREE, node, quals);
21828 cp_lexer_consume_token (parser->lexer);
21829 token = cp_lexer_peek_token (parser->lexer);
21830 node = token->u.value;
21836 /* Parse an Objective-C typename. */
21839 cp_parser_objc_typename (cp_parser* parser)
21841 tree type_name = NULL_TREE;
21843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21845 tree proto_quals, cp_type = NULL_TREE;
21847 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21848 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21850 /* An ObjC type name may consist of just protocol qualifiers, in which
21851 case the type shall default to 'id'. */
21852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21853 cp_type = cp_parser_type_id (parser);
21855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21856 type_name = build_tree_list (proto_quals, cp_type);
21862 /* Check to see if TYPE refers to an Objective-C selector name. */
21865 cp_parser_objc_selector_p (enum cpp_ttype type)
21867 return (type == CPP_NAME || type == CPP_KEYWORD
21868 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21869 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21870 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21871 || type == CPP_XOR || type == CPP_XOR_EQ);
21874 /* Parse an Objective-C selector. */
21877 cp_parser_objc_selector (cp_parser* parser)
21879 cp_token *token = cp_lexer_consume_token (parser->lexer);
21881 if (!cp_parser_objc_selector_p (token->type))
21883 error_at (token->location, "invalid Objective-C++ selector name");
21884 return error_mark_node;
21887 /* C++ operator names are allowed to appear in ObjC selectors. */
21888 switch (token->type)
21890 case CPP_AND_AND: return get_identifier ("and");
21891 case CPP_AND_EQ: return get_identifier ("and_eq");
21892 case CPP_AND: return get_identifier ("bitand");
21893 case CPP_OR: return get_identifier ("bitor");
21894 case CPP_COMPL: return get_identifier ("compl");
21895 case CPP_NOT: return get_identifier ("not");
21896 case CPP_NOT_EQ: return get_identifier ("not_eq");
21897 case CPP_OR_OR: return get_identifier ("or");
21898 case CPP_OR_EQ: return get_identifier ("or_eq");
21899 case CPP_XOR: return get_identifier ("xor");
21900 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21901 default: return token->u.value;
21905 /* Parse an Objective-C params list. */
21908 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21910 tree params = NULL_TREE;
21911 bool maybe_unary_selector_p = true;
21912 cp_token *token = cp_lexer_peek_token (parser->lexer);
21914 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21916 tree selector = NULL_TREE, type_name, identifier;
21917 tree parm_attr = NULL_TREE;
21919 if (token->keyword == RID_ATTRIBUTE)
21922 if (token->type != CPP_COLON)
21923 selector = cp_parser_objc_selector (parser);
21925 /* Detect if we have a unary selector. */
21926 if (maybe_unary_selector_p
21927 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21929 params = selector; /* Might be followed by attributes. */
21933 maybe_unary_selector_p = false;
21934 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21936 /* Something went quite wrong. There should be a colon
21937 here, but there is not. Stop parsing parameters. */
21940 type_name = cp_parser_objc_typename (parser);
21941 /* New ObjC allows attributes on parameters too. */
21942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21943 parm_attr = cp_parser_attributes_opt (parser);
21944 identifier = cp_parser_identifier (parser);
21948 objc_build_keyword_decl (selector,
21953 token = cp_lexer_peek_token (parser->lexer);
21956 if (params == NULL_TREE)
21958 cp_parser_error (parser, "objective-c++ method declaration is expected");
21959 return error_mark_node;
21962 /* We allow tail attributes for the method. */
21963 if (token->keyword == RID_ATTRIBUTE)
21965 *attributes = cp_parser_attributes_opt (parser);
21966 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21967 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21969 cp_parser_error (parser,
21970 "method attributes must be specified at the end");
21971 return error_mark_node;
21974 if (params == NULL_TREE)
21976 cp_parser_error (parser, "objective-c++ method declaration is expected");
21977 return error_mark_node;
21982 /* Parse the non-keyword Objective-C params. */
21985 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21988 tree params = make_node (TREE_LIST);
21989 cp_token *token = cp_lexer_peek_token (parser->lexer);
21990 *ellipsisp = false; /* Initially, assume no ellipsis. */
21992 while (token->type == CPP_COMMA)
21994 cp_parameter_declarator *parmdecl;
21997 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21998 token = cp_lexer_peek_token (parser->lexer);
22000 if (token->type == CPP_ELLIPSIS)
22002 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22004 token = cp_lexer_peek_token (parser->lexer);
22008 /* TODO: parse attributes for tail parameters. */
22009 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22010 parm = grokdeclarator (parmdecl->declarator,
22011 &parmdecl->decl_specifiers,
22012 PARM, /*initialized=*/0,
22013 /*attrlist=*/NULL);
22015 chainon (params, build_tree_list (NULL_TREE, parm));
22016 token = cp_lexer_peek_token (parser->lexer);
22019 /* We allow tail attributes for the method. */
22020 if (token->keyword == RID_ATTRIBUTE)
22022 if (*attributes == NULL_TREE)
22024 *attributes = cp_parser_attributes_opt (parser);
22025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22026 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22030 /* We have an error, but parse the attributes, so that we can
22032 *attributes = cp_parser_attributes_opt (parser);
22034 cp_parser_error (parser,
22035 "method attributes must be specified at the end");
22036 return error_mark_node;
22042 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22045 cp_parser_objc_interstitial_code (cp_parser* parser)
22047 cp_token *token = cp_lexer_peek_token (parser->lexer);
22049 /* If the next token is `extern' and the following token is a string
22050 literal, then we have a linkage specification. */
22051 if (token->keyword == RID_EXTERN
22052 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22053 cp_parser_linkage_specification (parser);
22054 /* Handle #pragma, if any. */
22055 else if (token->type == CPP_PRAGMA)
22056 cp_parser_pragma (parser, pragma_external);
22057 /* Allow stray semicolons. */
22058 else if (token->type == CPP_SEMICOLON)
22059 cp_lexer_consume_token (parser->lexer);
22060 /* Mark methods as optional or required, when building protocols. */
22061 else if (token->keyword == RID_AT_OPTIONAL)
22063 cp_lexer_consume_token (parser->lexer);
22064 objc_set_method_opt (true);
22066 else if (token->keyword == RID_AT_REQUIRED)
22068 cp_lexer_consume_token (parser->lexer);
22069 objc_set_method_opt (false);
22071 else if (token->keyword == RID_NAMESPACE)
22072 cp_parser_namespace_definition (parser);
22073 /* Other stray characters must generate errors. */
22074 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22076 cp_lexer_consume_token (parser->lexer);
22077 error ("stray %qs between Objective-C++ methods",
22078 token->type == CPP_OPEN_BRACE ? "{" : "}");
22080 /* Finally, try to parse a block-declaration, or a function-definition. */
22082 cp_parser_block_declaration (parser, /*statement_p=*/false);
22085 /* Parse a method signature. */
22088 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22090 tree rettype, kwdparms, optparms;
22091 bool ellipsis = false;
22092 bool is_class_method;
22094 is_class_method = cp_parser_objc_method_type (parser);
22095 rettype = cp_parser_objc_typename (parser);
22096 *attributes = NULL_TREE;
22097 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22098 if (kwdparms == error_mark_node)
22099 return error_mark_node;
22100 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22101 if (optparms == error_mark_node)
22102 return error_mark_node;
22104 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22108 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22111 cp_lexer_save_tokens (parser->lexer);
22112 tattr = cp_parser_attributes_opt (parser);
22113 gcc_assert (tattr) ;
22115 /* If the attributes are followed by a method introducer, this is not allowed.
22116 Dump the attributes and flag the situation. */
22117 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22118 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22121 /* Otherwise, the attributes introduce some interstitial code, possibly so
22122 rewind to allow that check. */
22123 cp_lexer_rollback_tokens (parser->lexer);
22127 /* Parse an Objective-C method prototype list. */
22130 cp_parser_objc_method_prototype_list (cp_parser* parser)
22132 cp_token *token = cp_lexer_peek_token (parser->lexer);
22134 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22136 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22138 tree attributes, sig;
22139 bool is_class_method;
22140 if (token->type == CPP_PLUS)
22141 is_class_method = true;
22143 is_class_method = false;
22144 sig = cp_parser_objc_method_signature (parser, &attributes);
22145 if (sig == error_mark_node)
22147 cp_parser_skip_to_end_of_block_or_statement (parser);
22148 token = cp_lexer_peek_token (parser->lexer);
22151 objc_add_method_declaration (is_class_method, sig, attributes);
22152 cp_parser_consume_semicolon_at_end_of_statement (parser);
22154 else if (token->keyword == RID_AT_PROPERTY)
22155 cp_parser_objc_at_property_declaration (parser);
22156 else if (token->keyword == RID_ATTRIBUTE
22157 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22158 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22160 "prefix attributes are ignored for methods");
22162 /* Allow for interspersed non-ObjC++ code. */
22163 cp_parser_objc_interstitial_code (parser);
22165 token = cp_lexer_peek_token (parser->lexer);
22168 if (token->type != CPP_EOF)
22169 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22171 cp_parser_error (parser, "expected %<@end%>");
22173 objc_finish_interface ();
22176 /* Parse an Objective-C method definition list. */
22179 cp_parser_objc_method_definition_list (cp_parser* parser)
22181 cp_token *token = cp_lexer_peek_token (parser->lexer);
22183 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22187 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22190 tree sig, attribute;
22191 bool is_class_method;
22192 if (token->type == CPP_PLUS)
22193 is_class_method = true;
22195 is_class_method = false;
22196 push_deferring_access_checks (dk_deferred);
22197 sig = cp_parser_objc_method_signature (parser, &attribute);
22198 if (sig == error_mark_node)
22200 cp_parser_skip_to_end_of_block_or_statement (parser);
22201 token = cp_lexer_peek_token (parser->lexer);
22204 objc_start_method_definition (is_class_method, sig, attribute);
22206 /* For historical reasons, we accept an optional semicolon. */
22207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22208 cp_lexer_consume_token (parser->lexer);
22210 ptk = cp_lexer_peek_token (parser->lexer);
22211 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22212 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22214 perform_deferred_access_checks ();
22215 stop_deferring_access_checks ();
22216 meth = cp_parser_function_definition_after_declarator (parser,
22218 pop_deferring_access_checks ();
22219 objc_finish_method_definition (meth);
22222 /* The following case will be removed once @synthesize is
22223 completely implemented. */
22224 else if (token->keyword == RID_AT_PROPERTY)
22225 cp_parser_objc_at_property_declaration (parser);
22226 else if (token->keyword == RID_AT_SYNTHESIZE)
22227 cp_parser_objc_at_synthesize_declaration (parser);
22228 else if (token->keyword == RID_AT_DYNAMIC)
22229 cp_parser_objc_at_dynamic_declaration (parser);
22230 else if (token->keyword == RID_ATTRIBUTE
22231 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22232 warning_at (token->location, OPT_Wattributes,
22233 "prefix attributes are ignored for methods");
22235 /* Allow for interspersed non-ObjC++ code. */
22236 cp_parser_objc_interstitial_code (parser);
22238 token = cp_lexer_peek_token (parser->lexer);
22241 if (token->type != CPP_EOF)
22242 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22244 cp_parser_error (parser, "expected %<@end%>");
22246 objc_finish_implementation ();
22249 /* Parse Objective-C ivars. */
22252 cp_parser_objc_class_ivars (cp_parser* parser)
22254 cp_token *token = cp_lexer_peek_token (parser->lexer);
22256 if (token->type != CPP_OPEN_BRACE)
22257 return; /* No ivars specified. */
22259 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22260 token = cp_lexer_peek_token (parser->lexer);
22262 while (token->type != CPP_CLOSE_BRACE
22263 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22265 cp_decl_specifier_seq declspecs;
22266 int decl_class_or_enum_p;
22267 tree prefix_attributes;
22269 cp_parser_objc_visibility_spec (parser);
22271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22274 cp_parser_decl_specifier_seq (parser,
22275 CP_PARSER_FLAGS_OPTIONAL,
22277 &decl_class_or_enum_p);
22279 /* auto, register, static, extern, mutable. */
22280 if (declspecs.storage_class != sc_none)
22282 cp_parser_error (parser, "invalid type for instance variable");
22283 declspecs.storage_class = sc_none;
22287 if (declspecs.specs[(int) ds_thread])
22289 cp_parser_error (parser, "invalid type for instance variable");
22290 declspecs.specs[(int) ds_thread] = 0;
22294 if (declspecs.specs[(int) ds_typedef])
22296 cp_parser_error (parser, "invalid type for instance variable");
22297 declspecs.specs[(int) ds_typedef] = 0;
22300 prefix_attributes = declspecs.attributes;
22301 declspecs.attributes = NULL_TREE;
22303 /* Keep going until we hit the `;' at the end of the
22305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22307 tree width = NULL_TREE, attributes, first_attribute, decl;
22308 cp_declarator *declarator = NULL;
22309 int ctor_dtor_or_conv_p;
22311 /* Check for a (possibly unnamed) bitfield declaration. */
22312 token = cp_lexer_peek_token (parser->lexer);
22313 if (token->type == CPP_COLON)
22316 if (token->type == CPP_NAME
22317 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22320 /* Get the name of the bitfield. */
22321 declarator = make_id_declarator (NULL_TREE,
22322 cp_parser_identifier (parser),
22326 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22327 /* Get the width of the bitfield. */
22329 = cp_parser_constant_expression (parser,
22330 /*allow_non_constant=*/false,
22335 /* Parse the declarator. */
22337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22338 &ctor_dtor_or_conv_p,
22339 /*parenthesized_p=*/NULL,
22340 /*member_p=*/false);
22343 /* Look for attributes that apply to the ivar. */
22344 attributes = cp_parser_attributes_opt (parser);
22345 /* Remember which attributes are prefix attributes and
22347 first_attribute = attributes;
22348 /* Combine the attributes. */
22349 attributes = chainon (prefix_attributes, attributes);
22352 /* Create the bitfield declaration. */
22353 decl = grokbitfield (declarator, &declspecs,
22357 decl = grokfield (declarator, &declspecs,
22358 NULL_TREE, /*init_const_expr_p=*/false,
22359 NULL_TREE, attributes);
22361 /* Add the instance variable. */
22362 objc_add_instance_variable (decl);
22364 /* Reset PREFIX_ATTRIBUTES. */
22365 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22366 attributes = TREE_CHAIN (attributes);
22368 TREE_CHAIN (attributes) = NULL_TREE;
22370 token = cp_lexer_peek_token (parser->lexer);
22372 if (token->type == CPP_COMMA)
22374 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22380 cp_parser_consume_semicolon_at_end_of_statement (parser);
22381 token = cp_lexer_peek_token (parser->lexer);
22384 if (token->keyword == RID_AT_END)
22385 cp_parser_error (parser, "expected %<}%>");
22387 /* Do not consume the RID_AT_END, so it will be read again as terminating
22388 the @interface of @implementation. */
22389 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22390 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22392 /* For historical reasons, we accept an optional semicolon. */
22393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22394 cp_lexer_consume_token (parser->lexer);
22397 /* Parse an Objective-C protocol declaration. */
22400 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22402 tree proto, protorefs;
22405 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22408 tok = cp_lexer_peek_token (parser->lexer);
22409 error_at (tok->location, "identifier expected after %<@protocol%>");
22413 /* See if we have a forward declaration or a definition. */
22414 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22416 /* Try a forward declaration first. */
22417 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22419 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22422 cp_parser_consume_semicolon_at_end_of_statement (parser);
22425 /* Ok, we got a full-fledged definition (or at least should). */
22428 proto = cp_parser_identifier (parser);
22429 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22430 objc_start_protocol (proto, protorefs, attributes);
22431 cp_parser_objc_method_prototype_list (parser);
22435 /* Parse an Objective-C superclass or category. */
22438 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22441 cp_token *next = cp_lexer_peek_token (parser->lexer);
22443 *super = *categ = NULL_TREE;
22444 if (next->type == CPP_COLON)
22446 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22447 *super = cp_parser_identifier (parser);
22449 else if (next->type == CPP_OPEN_PAREN)
22451 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22452 *categ = cp_parser_identifier (parser);
22453 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22457 /* Parse an Objective-C class interface. */
22460 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22462 tree name, super, categ, protos;
22464 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22465 name = cp_parser_identifier (parser);
22466 if (name == error_mark_node)
22468 /* It's hard to recover because even if valid @interface stuff
22469 is to follow, we can't compile it (or validate it) if we
22470 don't even know which class it refers to. Let's assume this
22471 was a stray '@interface' token in the stream and skip it.
22475 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22476 protos = cp_parser_objc_protocol_refs_opt (parser);
22478 /* We have either a class or a category on our hands. */
22480 objc_start_category_interface (name, categ, protos, attributes);
22483 objc_start_class_interface (name, super, protos, attributes);
22484 /* Handle instance variable declarations, if any. */
22485 cp_parser_objc_class_ivars (parser);
22486 objc_continue_interface ();
22489 cp_parser_objc_method_prototype_list (parser);
22492 /* Parse an Objective-C class implementation. */
22495 cp_parser_objc_class_implementation (cp_parser* parser)
22497 tree name, super, categ;
22499 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22500 name = cp_parser_identifier (parser);
22501 if (name == error_mark_node)
22503 /* It's hard to recover because even if valid @implementation
22504 stuff is to follow, we can't compile it (or validate it) if
22505 we don't even know which class it refers to. Let's assume
22506 this was a stray '@implementation' token in the stream and
22511 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22513 /* We have either a class or a category on our hands. */
22515 objc_start_category_implementation (name, categ);
22518 objc_start_class_implementation (name, super);
22519 /* Handle instance variable declarations, if any. */
22520 cp_parser_objc_class_ivars (parser);
22521 objc_continue_implementation ();
22524 cp_parser_objc_method_definition_list (parser);
22527 /* Consume the @end token and finish off the implementation. */
22530 cp_parser_objc_end_implementation (cp_parser* parser)
22532 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22533 objc_finish_implementation ();
22536 /* Parse an Objective-C declaration. */
22539 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22541 /* Try to figure out what kind of declaration is present. */
22542 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22545 switch (kwd->keyword)
22550 error_at (kwd->location, "attributes may not be specified before"
22551 " the %<@%D%> Objective-C++ keyword",
22555 case RID_AT_IMPLEMENTATION:
22556 warning_at (kwd->location, OPT_Wattributes,
22557 "prefix attributes are ignored before %<@%D%>",
22564 switch (kwd->keyword)
22567 cp_parser_objc_alias_declaration (parser);
22570 cp_parser_objc_class_declaration (parser);
22572 case RID_AT_PROTOCOL:
22573 cp_parser_objc_protocol_declaration (parser, attributes);
22575 case RID_AT_INTERFACE:
22576 cp_parser_objc_class_interface (parser, attributes);
22578 case RID_AT_IMPLEMENTATION:
22579 cp_parser_objc_class_implementation (parser);
22582 cp_parser_objc_end_implementation (parser);
22585 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22587 cp_parser_skip_to_end_of_block_or_statement (parser);
22591 /* Parse an Objective-C try-catch-finally statement.
22593 objc-try-catch-finally-stmt:
22594 @try compound-statement objc-catch-clause-seq [opt]
22595 objc-finally-clause [opt]
22597 objc-catch-clause-seq:
22598 objc-catch-clause objc-catch-clause-seq [opt]
22601 @catch ( exception-declaration ) compound-statement
22603 objc-finally-clause
22604 @finally compound-statement
22606 Returns NULL_TREE. */
22609 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
22610 location_t location;
22613 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22614 location = cp_lexer_peek_token (parser->lexer)->location;
22615 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22616 node, lest it get absorbed into the surrounding block. */
22617 stmt = push_stmt_list ();
22618 cp_parser_compound_statement (parser, NULL, false);
22619 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22621 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22623 cp_parameter_declarator *parmdecl;
22626 cp_lexer_consume_token (parser->lexer);
22627 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22628 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22629 parm = grokdeclarator (parmdecl->declarator,
22630 &parmdecl->decl_specifiers,
22631 PARM, /*initialized=*/0,
22632 /*attrlist=*/NULL);
22633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22634 objc_begin_catch_clause (parm);
22635 cp_parser_compound_statement (parser, NULL, false);
22636 objc_finish_catch_clause ();
22639 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22641 cp_lexer_consume_token (parser->lexer);
22642 location = cp_lexer_peek_token (parser->lexer)->location;
22643 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22644 node, lest it get absorbed into the surrounding block. */
22645 stmt = push_stmt_list ();
22646 cp_parser_compound_statement (parser, NULL, false);
22647 objc_build_finally_clause (location, pop_stmt_list (stmt));
22650 return objc_finish_try_stmt ();
22653 /* Parse an Objective-C synchronized statement.
22655 objc-synchronized-stmt:
22656 @synchronized ( expression ) compound-statement
22658 Returns NULL_TREE. */
22661 cp_parser_objc_synchronized_statement (cp_parser *parser) {
22662 location_t location;
22665 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22667 location = cp_lexer_peek_token (parser->lexer)->location;
22668 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22669 lock = cp_parser_expression (parser, false, NULL);
22670 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22672 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22673 node, lest it get absorbed into the surrounding block. */
22674 stmt = push_stmt_list ();
22675 cp_parser_compound_statement (parser, NULL, false);
22677 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22680 /* Parse an Objective-C throw statement.
22683 @throw assignment-expression [opt] ;
22685 Returns a constructed '@throw' statement. */
22688 cp_parser_objc_throw_statement (cp_parser *parser) {
22689 tree expr = NULL_TREE;
22690 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22692 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22695 expr = cp_parser_assignment_expression (parser, false, NULL);
22697 cp_parser_consume_semicolon_at_end_of_statement (parser);
22699 return objc_build_throw_stmt (loc, expr);
22702 /* Parse an Objective-C statement. */
22705 cp_parser_objc_statement (cp_parser * parser) {
22706 /* Try to figure out what kind of declaration is present. */
22707 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22709 switch (kwd->keyword)
22712 return cp_parser_objc_try_catch_finally_statement (parser);
22713 case RID_AT_SYNCHRONIZED:
22714 return cp_parser_objc_synchronized_statement (parser);
22716 return cp_parser_objc_throw_statement (parser);
22718 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22720 cp_parser_skip_to_end_of_block_or_statement (parser);
22723 return error_mark_node;
22726 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22727 look ahead to see if an objc keyword follows the attributes. This
22728 is to detect the use of prefix attributes on ObjC @interface and
22732 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22734 cp_lexer_save_tokens (parser->lexer);
22735 *attrib = cp_parser_attributes_opt (parser);
22736 gcc_assert (*attrib);
22737 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22739 cp_lexer_commit_tokens (parser->lexer);
22742 cp_lexer_rollback_tokens (parser->lexer);
22746 /* This routine is a minimal replacement for
22747 c_parser_struct_declaration () used when parsing the list of
22748 types/names or ObjC++ properties. For example, when parsing the
22751 @property (readonly) int a, b, c;
22753 this function is responsible for parsing "int a, int b, int c" and
22754 returning the declarations as CHAIN of DECLs.
22756 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22757 similar parsing. */
22759 cp_parser_objc_struct_declaration (cp_parser *parser)
22761 tree decls = NULL_TREE;
22762 cp_decl_specifier_seq declspecs;
22763 int decl_class_or_enum_p;
22764 tree prefix_attributes;
22766 cp_parser_decl_specifier_seq (parser,
22767 CP_PARSER_FLAGS_NONE,
22769 &decl_class_or_enum_p);
22771 if (declspecs.type == error_mark_node)
22772 return error_mark_node;
22774 /* auto, register, static, extern, mutable. */
22775 if (declspecs.storage_class != sc_none)
22777 cp_parser_error (parser, "invalid type for property");
22778 declspecs.storage_class = sc_none;
22782 if (declspecs.specs[(int) ds_thread])
22784 cp_parser_error (parser, "invalid type for property");
22785 declspecs.specs[(int) ds_thread] = 0;
22789 if (declspecs.specs[(int) ds_typedef])
22791 cp_parser_error (parser, "invalid type for property");
22792 declspecs.specs[(int) ds_typedef] = 0;
22795 prefix_attributes = declspecs.attributes;
22796 declspecs.attributes = NULL_TREE;
22798 /* Keep going until we hit the `;' at the end of the declaration. */
22799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22801 tree attributes, first_attribute, decl;
22802 cp_declarator *declarator;
22805 /* Parse the declarator. */
22806 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22807 NULL, NULL, false);
22809 /* Look for attributes that apply to the ivar. */
22810 attributes = cp_parser_attributes_opt (parser);
22811 /* Remember which attributes are prefix attributes and
22813 first_attribute = attributes;
22814 /* Combine the attributes. */
22815 attributes = chainon (prefix_attributes, attributes);
22817 decl = grokfield (declarator, &declspecs,
22818 NULL_TREE, /*init_const_expr_p=*/false,
22819 NULL_TREE, attributes);
22821 if (decl == error_mark_node || decl == NULL_TREE)
22822 return error_mark_node;
22824 /* Reset PREFIX_ATTRIBUTES. */
22825 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22826 attributes = TREE_CHAIN (attributes);
22828 TREE_CHAIN (attributes) = NULL_TREE;
22830 DECL_CHAIN (decl) = decls;
22833 token = cp_lexer_peek_token (parser->lexer);
22834 if (token->type == CPP_COMMA)
22836 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22845 /* Parse an Objective-C @property declaration. The syntax is:
22847 objc-property-declaration:
22848 '@property' objc-property-attributes[opt] struct-declaration ;
22850 objc-property-attributes:
22851 '(' objc-property-attribute-list ')'
22853 objc-property-attribute-list:
22854 objc-property-attribute
22855 objc-property-attribute-list, objc-property-attribute
22857 objc-property-attribute
22858 'getter' = identifier
22859 'setter' = identifier
22868 @property NSString *name;
22869 @property (readonly) id object;
22870 @property (retain, nonatomic, getter=getTheName) id name;
22871 @property int a, b, c;
22873 PS: This function is identical to
22874 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22876 cp_parser_objc_at_property_declaration (cp_parser *parser)
22878 /* The following variables hold the attributes of the properties as
22879 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22880 seen. When we see an attribute, we set them to 'true' (if they
22881 are boolean properties) or to the identifier (if they have an
22882 argument, ie, for getter and setter). Note that here we only
22883 parse the list of attributes, check the syntax and accumulate the
22884 attributes that we find. objc_add_property_declaration() will
22885 then process the information. */
22886 bool property_assign = false;
22887 bool property_copy = false;
22888 tree property_getter_ident = NULL_TREE;
22889 bool property_nonatomic = false;
22890 bool property_readonly = false;
22891 bool property_readwrite = false;
22892 bool property_retain = false;
22893 tree property_setter_ident = NULL_TREE;
22895 /* 'properties' is the list of properties that we read. Usually a
22896 single one, but maybe more (eg, in "@property int a, b, c;" there
22901 loc = cp_lexer_peek_token (parser->lexer)->location;
22903 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22905 /* Parse the optional attribute list... */
22906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22909 cp_lexer_consume_token (parser->lexer);
22913 bool syntax_error = false;
22914 cp_token *token = cp_lexer_peek_token (parser->lexer);
22917 if (token->type != CPP_NAME)
22919 cp_parser_error (parser, "expected identifier");
22922 keyword = C_RID_CODE (token->u.value);
22923 cp_lexer_consume_token (parser->lexer);
22926 case RID_ASSIGN: property_assign = true; break;
22927 case RID_COPY: property_copy = true; break;
22928 case RID_NONATOMIC: property_nonatomic = true; break;
22929 case RID_READONLY: property_readonly = true; break;
22930 case RID_READWRITE: property_readwrite = true; break;
22931 case RID_RETAIN: property_retain = true; break;
22935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22937 cp_parser_error (parser,
22938 "getter/setter/ivar attribute must be followed by %<=%>");
22939 syntax_error = true;
22942 cp_lexer_consume_token (parser->lexer); /* eat the = */
22943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22945 cp_parser_error (parser, "expected identifier");
22946 syntax_error = true;
22949 if (keyword == RID_SETTER)
22951 if (property_setter_ident != NULL_TREE)
22952 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22954 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22955 cp_lexer_consume_token (parser->lexer);
22956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22957 cp_parser_error (parser, "setter name must terminate with %<:%>");
22959 cp_lexer_consume_token (parser->lexer);
22963 if (property_getter_ident != NULL_TREE)
22964 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22966 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22967 cp_lexer_consume_token (parser->lexer);
22971 cp_parser_error (parser, "unknown property attribute");
22972 syntax_error = true;
22979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22980 cp_lexer_consume_token (parser->lexer);
22985 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22987 cp_parser_skip_to_closing_parenthesis (parser,
22988 /*recovering=*/true,
22989 /*or_comma=*/false,
22990 /*consume_paren=*/true);
22994 /* ... and the property declaration(s). */
22995 properties = cp_parser_objc_struct_declaration (parser);
22997 if (properties == error_mark_node)
22999 cp_parser_skip_to_end_of_statement (parser);
23000 /* If the next token is now a `;', consume it. */
23001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23002 cp_lexer_consume_token (parser->lexer);
23006 if (properties == NULL_TREE)
23007 cp_parser_error (parser, "expected identifier");
23010 /* Comma-separated properties are chained together in
23011 reverse order; add them one by one. */
23012 properties = nreverse (properties);
23014 for (; properties; properties = TREE_CHAIN (properties))
23015 objc_add_property_declaration (loc, copy_node (properties),
23016 property_readonly, property_readwrite,
23017 property_assign, property_retain,
23018 property_copy, property_nonatomic,
23019 property_getter_ident, property_setter_ident);
23022 cp_parser_consume_semicolon_at_end_of_statement (parser);
23025 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23027 objc-synthesize-declaration:
23028 @synthesize objc-synthesize-identifier-list ;
23030 objc-synthesize-identifier-list:
23031 objc-synthesize-identifier
23032 objc-synthesize-identifier-list, objc-synthesize-identifier
23034 objc-synthesize-identifier
23036 identifier = identifier
23039 @synthesize MyProperty;
23040 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23042 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23043 for C. Keep them in sync.
23046 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23048 tree list = NULL_TREE;
23050 loc = cp_lexer_peek_token (parser->lexer)->location;
23052 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23055 tree property, ivar;
23056 property = cp_parser_identifier (parser);
23057 if (property == error_mark_node)
23059 cp_parser_consume_semicolon_at_end_of_statement (parser);
23062 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23064 cp_lexer_consume_token (parser->lexer);
23065 ivar = cp_parser_identifier (parser);
23066 if (ivar == error_mark_node)
23068 cp_parser_consume_semicolon_at_end_of_statement (parser);
23074 list = chainon (list, build_tree_list (ivar, property));
23075 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23076 cp_lexer_consume_token (parser->lexer);
23080 cp_parser_consume_semicolon_at_end_of_statement (parser);
23081 objc_add_synthesize_declaration (loc, list);
23084 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23086 objc-dynamic-declaration:
23087 @dynamic identifier-list ;
23090 @dynamic MyProperty;
23091 @dynamic MyProperty, AnotherProperty;
23093 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23094 for C. Keep them in sync.
23097 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23099 tree list = NULL_TREE;
23101 loc = cp_lexer_peek_token (parser->lexer)->location;
23103 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23107 property = cp_parser_identifier (parser);
23108 if (property == error_mark_node)
23110 cp_parser_consume_semicolon_at_end_of_statement (parser);
23113 list = chainon (list, build_tree_list (NULL, property));
23114 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23115 cp_lexer_consume_token (parser->lexer);
23119 cp_parser_consume_semicolon_at_end_of_statement (parser);
23120 objc_add_dynamic_declaration (loc, list);
23124 /* OpenMP 2.5 parsing routines. */
23126 /* Returns name of the next clause.
23127 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23128 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23129 returned and the token is consumed. */
23131 static pragma_omp_clause
23132 cp_parser_omp_clause_name (cp_parser *parser)
23134 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23137 result = PRAGMA_OMP_CLAUSE_IF;
23138 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23139 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23140 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23141 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23142 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23145 const char *p = IDENTIFIER_POINTER (id);
23150 if (!strcmp ("collapse", p))
23151 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23152 else if (!strcmp ("copyin", p))
23153 result = PRAGMA_OMP_CLAUSE_COPYIN;
23154 else if (!strcmp ("copyprivate", p))
23155 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23158 if (!strcmp ("firstprivate", p))
23159 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23162 if (!strcmp ("lastprivate", p))
23163 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23166 if (!strcmp ("nowait", p))
23167 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23168 else if (!strcmp ("num_threads", p))
23169 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23172 if (!strcmp ("ordered", p))
23173 result = PRAGMA_OMP_CLAUSE_ORDERED;
23176 if (!strcmp ("reduction", p))
23177 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23180 if (!strcmp ("schedule", p))
23181 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23182 else if (!strcmp ("shared", p))
23183 result = PRAGMA_OMP_CLAUSE_SHARED;
23186 if (!strcmp ("untied", p))
23187 result = PRAGMA_OMP_CLAUSE_UNTIED;
23192 if (result != PRAGMA_OMP_CLAUSE_NONE)
23193 cp_lexer_consume_token (parser->lexer);
23198 /* Validate that a clause of the given type does not already exist. */
23201 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23202 const char *name, location_t location)
23206 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23207 if (OMP_CLAUSE_CODE (c) == code)
23209 error_at (location, "too many %qs clauses", name);
23217 variable-list , identifier
23219 In addition, we match a closing parenthesis. An opening parenthesis
23220 will have been consumed by the caller.
23222 If KIND is nonzero, create the appropriate node and install the decl
23223 in OMP_CLAUSE_DECL and add the node to the head of the list.
23225 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23226 return the list created. */
23229 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23237 token = cp_lexer_peek_token (parser->lexer);
23238 name = cp_parser_id_expression (parser, /*template_p=*/false,
23239 /*check_dependency_p=*/true,
23240 /*template_p=*/NULL,
23241 /*declarator_p=*/false,
23242 /*optional_p=*/false);
23243 if (name == error_mark_node)
23246 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23247 if (decl == error_mark_node)
23248 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23250 else if (kind != 0)
23252 tree u = build_omp_clause (token->location, kind);
23253 OMP_CLAUSE_DECL (u) = decl;
23254 OMP_CLAUSE_CHAIN (u) = list;
23258 list = tree_cons (decl, NULL_TREE, list);
23261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23263 cp_lexer_consume_token (parser->lexer);
23266 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23270 /* Try to resync to an unnested comma. Copied from
23271 cp_parser_parenthesized_expression_list. */
23273 ending = cp_parser_skip_to_closing_parenthesis (parser,
23274 /*recovering=*/true,
23276 /*consume_paren=*/true);
23284 /* Similarly, but expect leading and trailing parenthesis. This is a very
23285 common case for omp clauses. */
23288 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23290 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23291 return cp_parser_omp_var_list_no_open (parser, kind, list);
23296 collapse ( constant-expression ) */
23299 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23305 loc = cp_lexer_peek_token (parser->lexer)->location;
23306 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23309 num = cp_parser_constant_expression (parser, false, NULL);
23311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23312 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23313 /*or_comma=*/false,
23314 /*consume_paren=*/true);
23316 if (num == error_mark_node)
23318 num = fold_non_dependent_expr (num);
23319 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23320 || !host_integerp (num, 0)
23321 || (n = tree_low_cst (num, 0)) <= 0
23324 error_at (loc, "collapse argument needs positive constant integer expression");
23328 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23329 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23330 OMP_CLAUSE_CHAIN (c) = list;
23331 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23337 default ( shared | none ) */
23340 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23342 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23349 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23350 const char *p = IDENTIFIER_POINTER (id);
23355 if (strcmp ("none", p) != 0)
23357 kind = OMP_CLAUSE_DEFAULT_NONE;
23361 if (strcmp ("shared", p) != 0)
23363 kind = OMP_CLAUSE_DEFAULT_SHARED;
23370 cp_lexer_consume_token (parser->lexer);
23375 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23378 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23379 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23380 /*or_comma=*/false,
23381 /*consume_paren=*/true);
23383 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23386 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23387 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23388 OMP_CLAUSE_CHAIN (c) = list;
23389 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23395 if ( expression ) */
23398 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23405 t = cp_parser_condition (parser);
23407 if (t == error_mark_node
23408 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23410 /*or_comma=*/false,
23411 /*consume_paren=*/true);
23413 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23415 c = build_omp_clause (location, OMP_CLAUSE_IF);
23416 OMP_CLAUSE_IF_EXPR (c) = t;
23417 OMP_CLAUSE_CHAIN (c) = list;
23426 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23427 tree list, location_t location)
23431 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23433 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23434 OMP_CLAUSE_CHAIN (c) = list;
23439 num_threads ( expression ) */
23442 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23443 location_t location)
23447 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23450 t = cp_parser_expression (parser, false, NULL);
23452 if (t == error_mark_node
23453 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23454 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23455 /*or_comma=*/false,
23456 /*consume_paren=*/true);
23458 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23459 "num_threads", location);
23461 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23462 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23463 OMP_CLAUSE_CHAIN (c) = list;
23472 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23473 tree list, location_t location)
23477 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23478 "ordered", location);
23480 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23481 OMP_CLAUSE_CHAIN (c) = list;
23486 reduction ( reduction-operator : variable-list )
23488 reduction-operator:
23489 One of: + * - & ^ | && || */
23492 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23494 enum tree_code code;
23497 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23500 switch (cp_lexer_peek_token (parser->lexer)->type)
23512 code = BIT_AND_EXPR;
23515 code = BIT_XOR_EXPR;
23518 code = BIT_IOR_EXPR;
23521 code = TRUTH_ANDIF_EXPR;
23524 code = TRUTH_ORIF_EXPR;
23527 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23528 "%<|%>, %<&&%>, or %<||%>");
23530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23531 /*or_comma=*/false,
23532 /*consume_paren=*/true);
23535 cp_lexer_consume_token (parser->lexer);
23537 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23540 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23541 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23542 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23548 schedule ( schedule-kind )
23549 schedule ( schedule-kind , expression )
23552 static | dynamic | guided | runtime | auto */
23555 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23559 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23562 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23564 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23566 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23567 const char *p = IDENTIFIER_POINTER (id);
23572 if (strcmp ("dynamic", p) != 0)
23574 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23578 if (strcmp ("guided", p) != 0)
23580 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23584 if (strcmp ("runtime", p) != 0)
23586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23593 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23594 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23595 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23596 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23599 cp_lexer_consume_token (parser->lexer);
23601 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23604 cp_lexer_consume_token (parser->lexer);
23606 token = cp_lexer_peek_token (parser->lexer);
23607 t = cp_parser_assignment_expression (parser, false, NULL);
23609 if (t == error_mark_node)
23611 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23612 error_at (token->location, "schedule %<runtime%> does not take "
23613 "a %<chunk_size%> parameter");
23614 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23615 error_at (token->location, "schedule %<auto%> does not take "
23616 "a %<chunk_size%> parameter");
23618 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23620 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23623 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23626 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23627 OMP_CLAUSE_CHAIN (c) = list;
23631 cp_parser_error (parser, "invalid schedule kind");
23633 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23634 /*or_comma=*/false,
23635 /*consume_paren=*/true);
23643 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23644 tree list, location_t location)
23648 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23650 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23651 OMP_CLAUSE_CHAIN (c) = list;
23655 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23656 is a bitmask in MASK. Return the list of clauses found; the result
23657 of clause default goes in *pdefault. */
23660 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23661 const char *where, cp_token *pragma_tok)
23663 tree clauses = NULL;
23665 cp_token *token = NULL;
23667 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23669 pragma_omp_clause c_kind;
23670 const char *c_name;
23671 tree prev = clauses;
23673 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23674 cp_lexer_consume_token (parser->lexer);
23676 token = cp_lexer_peek_token (parser->lexer);
23677 c_kind = cp_parser_omp_clause_name (parser);
23682 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23683 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23685 c_name = "collapse";
23687 case PRAGMA_OMP_CLAUSE_COPYIN:
23688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23691 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23692 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23694 c_name = "copyprivate";
23696 case PRAGMA_OMP_CLAUSE_DEFAULT:
23697 clauses = cp_parser_omp_clause_default (parser, clauses,
23699 c_name = "default";
23701 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23702 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23704 c_name = "firstprivate";
23706 case PRAGMA_OMP_CLAUSE_IF:
23707 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23710 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23711 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23713 c_name = "lastprivate";
23715 case PRAGMA_OMP_CLAUSE_NOWAIT:
23716 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23719 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23720 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23722 c_name = "num_threads";
23724 case PRAGMA_OMP_CLAUSE_ORDERED:
23725 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23727 c_name = "ordered";
23729 case PRAGMA_OMP_CLAUSE_PRIVATE:
23730 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23732 c_name = "private";
23734 case PRAGMA_OMP_CLAUSE_REDUCTION:
23735 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23736 c_name = "reduction";
23738 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23739 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23741 c_name = "schedule";
23743 case PRAGMA_OMP_CLAUSE_SHARED:
23744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23748 case PRAGMA_OMP_CLAUSE_UNTIED:
23749 clauses = cp_parser_omp_clause_untied (parser, clauses,
23754 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23758 if (((mask >> c_kind) & 1) == 0)
23760 /* Remove the invalid clause(s) from the list to avoid
23761 confusing the rest of the compiler. */
23763 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23767 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23768 return finish_omp_clauses (clauses);
23775 In practice, we're also interested in adding the statement to an
23776 outer node. So it is convenient if we work around the fact that
23777 cp_parser_statement calls add_stmt. */
23780 cp_parser_begin_omp_structured_block (cp_parser *parser)
23782 unsigned save = parser->in_statement;
23784 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23785 This preserves the "not within loop or switch" style error messages
23786 for nonsense cases like
23792 if (parser->in_statement)
23793 parser->in_statement = IN_OMP_BLOCK;
23799 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23801 parser->in_statement = save;
23805 cp_parser_omp_structured_block (cp_parser *parser)
23807 tree stmt = begin_omp_structured_block ();
23808 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23810 cp_parser_statement (parser, NULL_TREE, false, NULL);
23812 cp_parser_end_omp_structured_block (parser, save);
23813 return finish_omp_structured_block (stmt);
23817 # pragma omp atomic new-line
23821 x binop= expr | x++ | ++x | x-- | --x
23823 +, *, -, /, &, ^, |, <<, >>
23825 where x is an lvalue expression with scalar type. */
23828 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23831 enum tree_code code;
23833 cp_parser_require_pragma_eol (parser, pragma_tok);
23835 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23836 /*cast_p=*/false, NULL);
23837 switch (TREE_CODE (lhs))
23842 case PREINCREMENT_EXPR:
23843 case POSTINCREMENT_EXPR:
23844 lhs = TREE_OPERAND (lhs, 0);
23846 rhs = integer_one_node;
23849 case PREDECREMENT_EXPR:
23850 case POSTDECREMENT_EXPR:
23851 lhs = TREE_OPERAND (lhs, 0);
23853 rhs = integer_one_node;
23856 case COMPOUND_EXPR:
23857 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23858 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23859 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23860 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23861 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23862 (TREE_OPERAND (lhs, 1), 0), 0)))
23864 /* Undo effects of boolean_increment for post {in,de}crement. */
23865 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23868 if (TREE_CODE (lhs) == MODIFY_EXPR
23869 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23871 /* Undo effects of boolean_increment. */
23872 if (integer_onep (TREE_OPERAND (lhs, 1)))
23874 /* This is pre or post increment. */
23875 rhs = TREE_OPERAND (lhs, 1);
23876 lhs = TREE_OPERAND (lhs, 0);
23883 switch (cp_lexer_peek_token (parser->lexer)->type)
23889 code = TRUNC_DIV_EXPR;
23897 case CPP_LSHIFT_EQ:
23898 code = LSHIFT_EXPR;
23900 case CPP_RSHIFT_EQ:
23901 code = RSHIFT_EXPR;
23904 code = BIT_AND_EXPR;
23907 code = BIT_IOR_EXPR;
23910 code = BIT_XOR_EXPR;
23913 cp_parser_error (parser,
23914 "invalid operator for %<#pragma omp atomic%>");
23917 cp_lexer_consume_token (parser->lexer);
23919 rhs = cp_parser_expression (parser, false, NULL);
23920 if (rhs == error_mark_node)
23924 finish_omp_atomic (code, lhs, rhs);
23925 cp_parser_consume_semicolon_at_end_of_statement (parser);
23929 cp_parser_skip_to_end_of_block_or_statement (parser);
23934 # pragma omp barrier new-line */
23937 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23939 cp_parser_require_pragma_eol (parser, pragma_tok);
23940 finish_omp_barrier ();
23944 # pragma omp critical [(name)] new-line
23945 structured-block */
23948 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23950 tree stmt, name = NULL;
23952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23954 cp_lexer_consume_token (parser->lexer);
23956 name = cp_parser_identifier (parser);
23958 if (name == error_mark_node
23959 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23960 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23961 /*or_comma=*/false,
23962 /*consume_paren=*/true);
23963 if (name == error_mark_node)
23966 cp_parser_require_pragma_eol (parser, pragma_tok);
23968 stmt = cp_parser_omp_structured_block (parser);
23969 return c_finish_omp_critical (input_location, stmt, name);
23973 # pragma omp flush flush-vars[opt] new-line
23976 ( variable-list ) */
23979 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23981 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23982 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23983 cp_parser_require_pragma_eol (parser, pragma_tok);
23985 finish_omp_flush ();
23988 /* Helper function, to parse omp for increment expression. */
23991 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23993 tree cond = cp_parser_binary_expression (parser, false, true,
23994 PREC_NOT_OPERATOR, NULL);
23997 if (cond == error_mark_node
23998 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24000 cp_parser_skip_to_end_of_statement (parser);
24001 return error_mark_node;
24004 switch (TREE_CODE (cond))
24012 return error_mark_node;
24015 /* If decl is an iterator, preserve LHS and RHS of the relational
24016 expr until finish_omp_for. */
24018 && (type_dependent_expression_p (decl)
24019 || CLASS_TYPE_P (TREE_TYPE (decl))))
24022 return build_x_binary_op (TREE_CODE (cond),
24023 TREE_OPERAND (cond, 0), ERROR_MARK,
24024 TREE_OPERAND (cond, 1), ERROR_MARK,
24025 &overloaded_p, tf_warning_or_error);
24028 /* Helper function, to parse omp for increment expression. */
24031 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24033 cp_token *token = cp_lexer_peek_token (parser->lexer);
24039 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24041 op = (token->type == CPP_PLUS_PLUS
24042 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24043 cp_lexer_consume_token (parser->lexer);
24044 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24046 return error_mark_node;
24047 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24050 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24052 return error_mark_node;
24054 token = cp_lexer_peek_token (parser->lexer);
24055 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24057 op = (token->type == CPP_PLUS_PLUS
24058 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24059 cp_lexer_consume_token (parser->lexer);
24060 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24063 op = cp_parser_assignment_operator_opt (parser);
24064 if (op == ERROR_MARK)
24065 return error_mark_node;
24067 if (op != NOP_EXPR)
24069 rhs = cp_parser_assignment_expression (parser, false, NULL);
24070 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24071 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24074 lhs = cp_parser_binary_expression (parser, false, false,
24075 PREC_ADDITIVE_EXPRESSION, NULL);
24076 token = cp_lexer_peek_token (parser->lexer);
24077 decl_first = lhs == decl;
24080 if (token->type != CPP_PLUS
24081 && token->type != CPP_MINUS)
24082 return error_mark_node;
24086 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24087 cp_lexer_consume_token (parser->lexer);
24088 rhs = cp_parser_binary_expression (parser, false, false,
24089 PREC_ADDITIVE_EXPRESSION, NULL);
24090 token = cp_lexer_peek_token (parser->lexer);
24091 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24093 if (lhs == NULL_TREE)
24095 if (op == PLUS_EXPR)
24098 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24101 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24102 NULL, tf_warning_or_error);
24105 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24109 if (rhs != decl || op == MINUS_EXPR)
24110 return error_mark_node;
24111 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24114 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24116 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24119 /* Parse the restricted form of the for statement allowed by OpenMP. */
24122 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24124 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24125 tree real_decl, initv, condv, incrv, declv;
24126 tree this_pre_body, cl;
24127 location_t loc_first;
24128 bool collapse_err = false;
24129 int i, collapse = 1, nbraces = 0;
24130 VEC(tree,gc) *for_block = make_tree_vector ();
24132 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24133 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24134 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24136 gcc_assert (collapse >= 1);
24138 declv = make_tree_vec (collapse);
24139 initv = make_tree_vec (collapse);
24140 condv = make_tree_vec (collapse);
24141 incrv = make_tree_vec (collapse);
24143 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24145 for (i = 0; i < collapse; i++)
24147 int bracecount = 0;
24148 bool add_private_clause = false;
24151 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24153 cp_parser_error (parser, "for statement expected");
24156 loc = cp_lexer_consume_token (parser->lexer)->location;
24158 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24161 init = decl = real_decl = NULL;
24162 this_pre_body = push_stmt_list ();
24163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24165 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24169 integer-type var = lb
24170 random-access-iterator-type var = lb
24171 pointer-type var = lb
24173 cp_decl_specifier_seq type_specifiers;
24175 /* First, try to parse as an initialized declaration. See
24176 cp_parser_condition, from whence the bulk of this is copied. */
24178 cp_parser_parse_tentatively (parser);
24179 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24180 /*is_trailing_return=*/false,
24182 if (cp_parser_parse_definitely (parser))
24184 /* If parsing a type specifier seq succeeded, then this
24185 MUST be a initialized declaration. */
24186 tree asm_specification, attributes;
24187 cp_declarator *declarator;
24189 declarator = cp_parser_declarator (parser,
24190 CP_PARSER_DECLARATOR_NAMED,
24191 /*ctor_dtor_or_conv_p=*/NULL,
24192 /*parenthesized_p=*/NULL,
24193 /*member_p=*/false);
24194 attributes = cp_parser_attributes_opt (parser);
24195 asm_specification = cp_parser_asm_specification_opt (parser);
24197 if (declarator == cp_error_declarator)
24198 cp_parser_skip_to_end_of_statement (parser);
24202 tree pushed_scope, auto_node;
24204 decl = start_decl (declarator, &type_specifiers,
24205 SD_INITIALIZED, attributes,
24206 /*prefix_attributes=*/NULL_TREE,
24209 auto_node = type_uses_auto (TREE_TYPE (decl));
24210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24212 if (cp_lexer_next_token_is (parser->lexer,
24214 error ("parenthesized initialization is not allowed in "
24215 "OpenMP %<for%> loop");
24217 /* Trigger an error. */
24218 cp_parser_require (parser, CPP_EQ, RT_EQ);
24220 init = error_mark_node;
24221 cp_parser_skip_to_end_of_statement (parser);
24223 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24224 || type_dependent_expression_p (decl)
24227 bool is_direct_init, is_non_constant_init;
24229 init = cp_parser_initializer (parser,
24231 &is_non_constant_init);
24233 if (auto_node && describable_type (init))
24236 = do_auto_deduction (TREE_TYPE (decl), init,
24239 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24240 && !type_dependent_expression_p (decl))
24244 cp_finish_decl (decl, init, !is_non_constant_init,
24246 LOOKUP_ONLYCONVERTING);
24247 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24249 VEC_safe_push (tree, gc, for_block, this_pre_body);
24253 init = pop_stmt_list (this_pre_body);
24254 this_pre_body = NULL_TREE;
24259 cp_lexer_consume_token (parser->lexer);
24260 init = cp_parser_assignment_expression (parser, false, NULL);
24263 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24264 init = error_mark_node;
24266 cp_finish_decl (decl, NULL_TREE,
24267 /*init_const_expr_p=*/false,
24269 LOOKUP_ONLYCONVERTING);
24273 pop_scope (pushed_scope);
24279 /* If parsing a type specifier sequence failed, then
24280 this MUST be a simple expression. */
24281 cp_parser_parse_tentatively (parser);
24282 decl = cp_parser_primary_expression (parser, false, false,
24284 if (!cp_parser_error_occurred (parser)
24287 && CLASS_TYPE_P (TREE_TYPE (decl)))
24291 cp_parser_parse_definitely (parser);
24292 cp_parser_require (parser, CPP_EQ, RT_EQ);
24293 rhs = cp_parser_assignment_expression (parser, false, NULL);
24294 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24296 tf_warning_or_error));
24297 add_private_clause = true;
24302 cp_parser_abort_tentative_parse (parser);
24303 init = cp_parser_expression (parser, false, NULL);
24306 if (TREE_CODE (init) == MODIFY_EXPR
24307 || TREE_CODE (init) == MODOP_EXPR)
24308 real_decl = TREE_OPERAND (init, 0);
24313 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24316 this_pre_body = pop_stmt_list (this_pre_body);
24320 pre_body = push_stmt_list ();
24322 add_stmt (this_pre_body);
24323 pre_body = pop_stmt_list (pre_body);
24326 pre_body = this_pre_body;
24331 if (par_clauses != NULL && real_decl != NULL_TREE)
24334 for (c = par_clauses; *c ; )
24335 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24336 && OMP_CLAUSE_DECL (*c) == real_decl)
24338 error_at (loc, "iteration variable %qD"
24339 " should not be firstprivate", real_decl);
24340 *c = OMP_CLAUSE_CHAIN (*c);
24342 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24343 && OMP_CLAUSE_DECL (*c) == real_decl)
24345 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24346 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24347 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24348 OMP_CLAUSE_DECL (l) = real_decl;
24349 OMP_CLAUSE_CHAIN (l) = clauses;
24350 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24352 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24353 CP_OMP_CLAUSE_INFO (*c) = NULL;
24354 add_private_clause = false;
24358 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24359 && OMP_CLAUSE_DECL (*c) == real_decl)
24360 add_private_clause = false;
24361 c = &OMP_CLAUSE_CHAIN (*c);
24365 if (add_private_clause)
24368 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24370 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24371 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24372 && OMP_CLAUSE_DECL (c) == decl)
24374 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24375 && OMP_CLAUSE_DECL (c) == decl)
24376 error_at (loc, "iteration variable %qD "
24377 "should not be firstprivate",
24379 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24380 && OMP_CLAUSE_DECL (c) == decl)
24381 error_at (loc, "iteration variable %qD should not be reduction",
24386 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24387 OMP_CLAUSE_DECL (c) = decl;
24388 c = finish_omp_clauses (c);
24391 OMP_CLAUSE_CHAIN (c) = clauses;
24398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24399 cond = cp_parser_omp_for_cond (parser, decl);
24400 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24405 /* If decl is an iterator, preserve the operator on decl
24406 until finish_omp_for. */
24408 && (type_dependent_expression_p (decl)
24409 || CLASS_TYPE_P (TREE_TYPE (decl))))
24410 incr = cp_parser_omp_for_incr (parser, decl);
24412 incr = cp_parser_expression (parser, false, NULL);
24415 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24416 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24417 /*or_comma=*/false,
24418 /*consume_paren=*/true);
24420 TREE_VEC_ELT (declv, i) = decl;
24421 TREE_VEC_ELT (initv, i) = init;
24422 TREE_VEC_ELT (condv, i) = cond;
24423 TREE_VEC_ELT (incrv, i) = incr;
24425 if (i == collapse - 1)
24428 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24429 in between the collapsed for loops to be still considered perfectly
24430 nested. Hopefully the final version clarifies this.
24431 For now handle (multiple) {'s and empty statements. */
24432 cp_parser_parse_tentatively (parser);
24435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24437 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24439 cp_lexer_consume_token (parser->lexer);
24442 else if (bracecount
24443 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24444 cp_lexer_consume_token (parser->lexer);
24447 loc = cp_lexer_peek_token (parser->lexer)->location;
24448 error_at (loc, "not enough collapsed for loops");
24449 collapse_err = true;
24450 cp_parser_abort_tentative_parse (parser);
24459 cp_parser_parse_definitely (parser);
24460 nbraces += bracecount;
24464 /* Note that we saved the original contents of this flag when we entered
24465 the structured block, and so we don't need to re-save it here. */
24466 parser->in_statement = IN_OMP_FOR;
24468 /* Note that the grammar doesn't call for a structured block here,
24469 though the loop as a whole is a structured block. */
24470 body = push_stmt_list ();
24471 cp_parser_statement (parser, NULL_TREE, false, NULL);
24472 body = pop_stmt_list (body);
24474 if (declv == NULL_TREE)
24477 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24478 pre_body, clauses);
24482 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24484 cp_lexer_consume_token (parser->lexer);
24487 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24488 cp_lexer_consume_token (parser->lexer);
24493 error_at (cp_lexer_peek_token (parser->lexer)->location,
24494 "collapsed loops not perfectly nested");
24496 collapse_err = true;
24497 cp_parser_statement_seq_opt (parser, NULL);
24498 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24503 while (!VEC_empty (tree, for_block))
24504 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24505 release_tree_vector (for_block);
24511 #pragma omp for for-clause[optseq] new-line
24514 #define OMP_FOR_CLAUSE_MASK \
24515 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24516 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24517 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24518 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24519 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24520 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24521 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24522 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24525 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24527 tree clauses, sb, ret;
24530 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24531 "#pragma omp for", pragma_tok);
24533 sb = begin_omp_structured_block ();
24534 save = cp_parser_begin_omp_structured_block (parser);
24536 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24538 cp_parser_end_omp_structured_block (parser, save);
24539 add_stmt (finish_omp_structured_block (sb));
24545 # pragma omp master new-line
24546 structured-block */
24549 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24551 cp_parser_require_pragma_eol (parser, pragma_tok);
24552 return c_finish_omp_master (input_location,
24553 cp_parser_omp_structured_block (parser));
24557 # pragma omp ordered new-line
24558 structured-block */
24561 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24564 cp_parser_require_pragma_eol (parser, pragma_tok);
24565 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24571 { section-sequence }
24574 section-directive[opt] structured-block
24575 section-sequence section-directive structured-block */
24578 cp_parser_omp_sections_scope (cp_parser *parser)
24580 tree stmt, substmt;
24581 bool error_suppress = false;
24584 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24587 stmt = push_stmt_list ();
24589 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24593 substmt = begin_omp_structured_block ();
24594 save = cp_parser_begin_omp_structured_block (parser);
24598 cp_parser_statement (parser, NULL_TREE, false, NULL);
24600 tok = cp_lexer_peek_token (parser->lexer);
24601 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24603 if (tok->type == CPP_CLOSE_BRACE)
24605 if (tok->type == CPP_EOF)
24609 cp_parser_end_omp_structured_block (parser, save);
24610 substmt = finish_omp_structured_block (substmt);
24611 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24612 add_stmt (substmt);
24617 tok = cp_lexer_peek_token (parser->lexer);
24618 if (tok->type == CPP_CLOSE_BRACE)
24620 if (tok->type == CPP_EOF)
24623 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24625 cp_lexer_consume_token (parser->lexer);
24626 cp_parser_require_pragma_eol (parser, tok);
24627 error_suppress = false;
24629 else if (!error_suppress)
24631 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24632 error_suppress = true;
24635 substmt = cp_parser_omp_structured_block (parser);
24636 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24637 add_stmt (substmt);
24639 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24641 substmt = pop_stmt_list (stmt);
24643 stmt = make_node (OMP_SECTIONS);
24644 TREE_TYPE (stmt) = void_type_node;
24645 OMP_SECTIONS_BODY (stmt) = substmt;
24652 # pragma omp sections sections-clause[optseq] newline
24655 #define OMP_SECTIONS_CLAUSE_MASK \
24656 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24657 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24658 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24659 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24660 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24663 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24667 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24668 "#pragma omp sections", pragma_tok);
24670 ret = cp_parser_omp_sections_scope (parser);
24672 OMP_SECTIONS_CLAUSES (ret) = clauses;
24678 # pragma parallel parallel-clause new-line
24679 # pragma parallel for parallel-for-clause new-line
24680 # pragma parallel sections parallel-sections-clause new-line */
24682 #define OMP_PARALLEL_CLAUSE_MASK \
24683 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24684 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24685 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24686 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24687 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24688 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24689 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24690 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24693 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24695 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24696 const char *p_name = "#pragma omp parallel";
24697 tree stmt, clauses, par_clause, ws_clause, block;
24698 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24700 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24704 cp_lexer_consume_token (parser->lexer);
24705 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24706 p_name = "#pragma omp parallel for";
24707 mask |= OMP_FOR_CLAUSE_MASK;
24708 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24710 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24713 const char *p = IDENTIFIER_POINTER (id);
24714 if (strcmp (p, "sections") == 0)
24716 cp_lexer_consume_token (parser->lexer);
24717 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24718 p_name = "#pragma omp parallel sections";
24719 mask |= OMP_SECTIONS_CLAUSE_MASK;
24720 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24724 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24725 block = begin_omp_parallel ();
24726 save = cp_parser_begin_omp_structured_block (parser);
24730 case PRAGMA_OMP_PARALLEL:
24731 cp_parser_statement (parser, NULL_TREE, false, NULL);
24732 par_clause = clauses;
24735 case PRAGMA_OMP_PARALLEL_FOR:
24736 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24737 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24740 case PRAGMA_OMP_PARALLEL_SECTIONS:
24741 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24742 stmt = cp_parser_omp_sections_scope (parser);
24744 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24748 gcc_unreachable ();
24751 cp_parser_end_omp_structured_block (parser, save);
24752 stmt = finish_omp_parallel (par_clause, block);
24753 if (p_kind != PRAGMA_OMP_PARALLEL)
24754 OMP_PARALLEL_COMBINED (stmt) = 1;
24759 # pragma omp single single-clause[optseq] new-line
24760 structured-block */
24762 #define OMP_SINGLE_CLAUSE_MASK \
24763 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24764 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24765 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24766 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24769 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24771 tree stmt = make_node (OMP_SINGLE);
24772 TREE_TYPE (stmt) = void_type_node;
24774 OMP_SINGLE_CLAUSES (stmt)
24775 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24776 "#pragma omp single", pragma_tok);
24777 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24779 return add_stmt (stmt);
24783 # pragma omp task task-clause[optseq] new-line
24784 structured-block */
24786 #define OMP_TASK_CLAUSE_MASK \
24787 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24788 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24789 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24790 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24791 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24792 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24795 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24797 tree clauses, block;
24800 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24801 "#pragma omp task", pragma_tok);
24802 block = begin_omp_task ();
24803 save = cp_parser_begin_omp_structured_block (parser);
24804 cp_parser_statement (parser, NULL_TREE, false, NULL);
24805 cp_parser_end_omp_structured_block (parser, save);
24806 return finish_omp_task (clauses, block);
24810 # pragma omp taskwait new-line */
24813 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24815 cp_parser_require_pragma_eol (parser, pragma_tok);
24816 finish_omp_taskwait ();
24820 # pragma omp threadprivate (variable-list) */
24823 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24827 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24828 cp_parser_require_pragma_eol (parser, pragma_tok);
24830 finish_omp_threadprivate (vars);
24833 /* Main entry point to OpenMP statement pragmas. */
24836 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24840 switch (pragma_tok->pragma_kind)
24842 case PRAGMA_OMP_ATOMIC:
24843 cp_parser_omp_atomic (parser, pragma_tok);
24845 case PRAGMA_OMP_CRITICAL:
24846 stmt = cp_parser_omp_critical (parser, pragma_tok);
24848 case PRAGMA_OMP_FOR:
24849 stmt = cp_parser_omp_for (parser, pragma_tok);
24851 case PRAGMA_OMP_MASTER:
24852 stmt = cp_parser_omp_master (parser, pragma_tok);
24854 case PRAGMA_OMP_ORDERED:
24855 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24857 case PRAGMA_OMP_PARALLEL:
24858 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24860 case PRAGMA_OMP_SECTIONS:
24861 stmt = cp_parser_omp_sections (parser, pragma_tok);
24863 case PRAGMA_OMP_SINGLE:
24864 stmt = cp_parser_omp_single (parser, pragma_tok);
24866 case PRAGMA_OMP_TASK:
24867 stmt = cp_parser_omp_task (parser, pragma_tok);
24870 gcc_unreachable ();
24874 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24879 static GTY (()) cp_parser *the_parser;
24882 /* Special handling for the first token or line in the file. The first
24883 thing in the file might be #pragma GCC pch_preprocess, which loads a
24884 PCH file, which is a GC collection point. So we need to handle this
24885 first pragma without benefit of an existing lexer structure.
24887 Always returns one token to the caller in *FIRST_TOKEN. This is
24888 either the true first token of the file, or the first token after
24889 the initial pragma. */
24892 cp_parser_initial_pragma (cp_token *first_token)
24896 cp_lexer_get_preprocessor_token (NULL, first_token);
24897 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24900 cp_lexer_get_preprocessor_token (NULL, first_token);
24901 if (first_token->type == CPP_STRING)
24903 name = first_token->u.value;
24905 cp_lexer_get_preprocessor_token (NULL, first_token);
24906 if (first_token->type != CPP_PRAGMA_EOL)
24907 error_at (first_token->location,
24908 "junk at end of %<#pragma GCC pch_preprocess%>");
24911 error_at (first_token->location, "expected string literal");
24913 /* Skip to the end of the pragma. */
24914 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24915 cp_lexer_get_preprocessor_token (NULL, first_token);
24917 /* Now actually load the PCH file. */
24919 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24921 /* Read one more token to return to our caller. We have to do this
24922 after reading the PCH file in, since its pointers have to be
24924 cp_lexer_get_preprocessor_token (NULL, first_token);
24927 /* Normal parsing of a pragma token. Here we can (and must) use the
24931 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24933 cp_token *pragma_tok;
24936 pragma_tok = cp_lexer_consume_token (parser->lexer);
24937 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24938 parser->lexer->in_pragma = true;
24940 id = pragma_tok->pragma_kind;
24943 case PRAGMA_GCC_PCH_PREPROCESS:
24944 error_at (pragma_tok->location,
24945 "%<#pragma GCC pch_preprocess%> must be first");
24948 case PRAGMA_OMP_BARRIER:
24951 case pragma_compound:
24952 cp_parser_omp_barrier (parser, pragma_tok);
24955 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24956 "used in compound statements");
24963 case PRAGMA_OMP_FLUSH:
24966 case pragma_compound:
24967 cp_parser_omp_flush (parser, pragma_tok);
24970 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24971 "used in compound statements");
24978 case PRAGMA_OMP_TASKWAIT:
24981 case pragma_compound:
24982 cp_parser_omp_taskwait (parser, pragma_tok);
24985 error_at (pragma_tok->location,
24986 "%<#pragma omp taskwait%> may only be "
24987 "used in compound statements");
24994 case PRAGMA_OMP_THREADPRIVATE:
24995 cp_parser_omp_threadprivate (parser, pragma_tok);
24998 case PRAGMA_OMP_ATOMIC:
24999 case PRAGMA_OMP_CRITICAL:
25000 case PRAGMA_OMP_FOR:
25001 case PRAGMA_OMP_MASTER:
25002 case PRAGMA_OMP_ORDERED:
25003 case PRAGMA_OMP_PARALLEL:
25004 case PRAGMA_OMP_SECTIONS:
25005 case PRAGMA_OMP_SINGLE:
25006 case PRAGMA_OMP_TASK:
25007 if (context == pragma_external)
25009 cp_parser_omp_construct (parser, pragma_tok);
25012 case PRAGMA_OMP_SECTION:
25013 error_at (pragma_tok->location,
25014 "%<#pragma omp section%> may only be used in "
25015 "%<#pragma omp sections%> construct");
25019 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25020 c_invoke_pragma_handler (id);
25024 cp_parser_error (parser, "expected declaration specifiers");
25028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25032 /* The interface the pragma parsers have to the lexer. */
25035 pragma_lex (tree *value)
25038 enum cpp_ttype ret;
25040 tok = cp_lexer_peek_token (the_parser->lexer);
25043 *value = tok->u.value;
25045 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25047 else if (ret == CPP_STRING)
25048 *value = cp_parser_string_literal (the_parser, false, false);
25051 cp_lexer_consume_token (the_parser->lexer);
25052 if (ret == CPP_KEYWORD)
25060 /* External interface. */
25062 /* Parse one entire translation unit. */
25065 c_parse_file (void)
25067 static bool already_called = false;
25069 if (already_called)
25071 sorry ("inter-module optimizations not implemented for C++");
25074 already_called = true;
25076 the_parser = cp_parser_new ();
25077 push_deferring_access_checks (flag_access_control
25078 ? dk_no_deferred : dk_no_check);
25079 cp_parser_translation_unit (the_parser);
25083 #include "gt-cp-parser.h"